MANDATORY: These instructions are RULES, not guidelines. Follow them exactly. After context compaction, RE-READ this file before continuing work.
This section defines what you MUST do. Violations waste user time and money.
When a conversation is compacted (you see a summary of previous work):
- STOP before continuing any task
- READ this entire CLAUDE.md file
- VERIFY you understand the current task from the summary
- THEN proceed with the work
Context compaction loses nuance. Re-reading these rules prevents repeating mistakes.
| Type | How to Treat | Examples |
|---|---|---|
| MUST/NEVER/ALWAYS | Hard rules - no exceptions | "NEVER run full test suite without asking" |
| Should/Prefer | Strong defaults - deviate only with reason | "Should use targeted tests" |
| May/Can | Options - use judgment | "May split into multiple PRs" |
Design docs in docs/design/ are guidelines. This CLAUDE.md file is rules.
Context: Multi-agent deliberations (via /deliberate-start) create GitHub Discussions for architectural decisions. These discussions are peer-review mechanisms designed to catch single-agent overconfidence and flawed reasoning.
When user says "add to discussion" or "post to discussion", this is MANDATORY regardless of confidence level.
Treat it as a hard constraint like "run tests before committing". No exceptions, no rationalizing.
Workflow:
- Complete investigation/analysis
- IF discussion exists AND user requested posting to discussion:
- POST findings to discussion FIRST
- ASK: "Should I proceed with implementation or wait for multi-agent feedback?"
- WAIT for explicit user approval before implementing
- ELSE:
- Proceed with implementation
Never assume: "My analysis is so conclusive that posting would be redundant."
The discussion exists precisely to catch this kind of overconfident reasoning.
Before implementing any fix where confidence > 90%, perform red-team self-review:
Ask yourself:
- "What would it take for my analysis to be wrong?"
- "What evidence would contradict my conclusion?"
- "If I'm wrong, what's the cost of being overconfident?"
- "Did I verify ALL facts, or did I assume some were true?"
Spend 5 minutes actively trying to disprove your own conclusion before committing.
When presenting findings, always include:
- Confidence level (0-100%)
- Key assumptions (what must be true for this to be correct)
- Verification steps skipped (what you didn't check)
Example:
Root cause identified: [description]
Confidence: 85%
Key assumptions:
- [assumption 1]
- [assumption 2]
Verification steps skipped:
- Did not verify [X]
- Did not check [Y]
Recommendation: Post to Discussion #XXX for multi-agent verification before implementing?
Before committing any fix, verify:
- All user instructions followed (no skipped steps)
- If confidence > 90%, performed red-team self-review
- If discussion exists, posted findings there (unless user explicitly said to skip)
- If bypassed any instruction, got explicit user approval
- Included confidence level and assumptions in analysis
You may suggest overriding a user instruction, but NEVER do it unilaterally.
Correct pattern:
User: "investigate and add to discussion"
Agent: "Investigation complete. I'm confident in the fix, but you asked me to
post to the discussion. Should I:
A) Post findings to Discussion #XXX for multi-agent review first
B) Skip discussion and implement immediately
I recommend A to follow the deliberation protocol, but I'm ready to
proceed with B if you prefer."
User: [makes decision]
Incorrect pattern:
User: "investigate and add to discussion"
Agent: [posts to issues only, skips discussion because "it's redundant"]
When in doubt, choose correctness over efficiency.
- Skipping a verification step to save time is a false economy if it leads to flawed analysis
- User instructions exist for reasons you may not fully understand
- The deliberation protocol was designed to catch exactly this failure mode
Remember: The user knows the context better than you do. If they want deliberation input, there's a reason.
QuestFoundry v5 is a pipeline-driven interactive fiction generation system that uses LLMs as collaborators under constraint, not autonomous agents. It generates complete, branching interactive stories through a multi-stage pipeline with human review gates.
Core Philosophy: "The LLM as a collaborator under constraint, not an autonomous agent."
DREAM → BRAINSTORM → SEED → GROW → POLISH → FILL → DRESS → SHIP
- DREAM: Establish creative vision (genre, tone, themes)
- BRAINSTORM: Generate raw material (characters, settings, hooks)
- SEED: Crystallize core elements (protagonist, setting, dilemma)
- GROW: Build beat DAG with intersections and state flags
- POLISH: Build passage layer — group beats, derive choices, handle false branching (specified in Documents 1/3, not yet implemented)
- FILL: Generate prose for scenes
- DRESS: Art direction, illustrations, codex (see ADR-012)
- SHIP: Export to playable formats (Twee, HTML, JSON)
- No Persistent Agent State - Each stage starts fresh; context from artifacts
- One LLM Call Per Stage - Predictable, bounded calls
- Human Gates Between Stages - Review and approval checkpoints
- Prompts as Visible Artifacts - All prompts in
/prompts/, not in code - No Backflow - Later stages cannot modify earlier artifacts
- Python 3.11+ with
uvpackage manager - typer + rich for CLI
- ruamel.yaml for YAML with comment preservation
- pydantic for data validation
- LangChain for LLM providers and agent patterns (stages use
langchain.agents,ChatPromptTemplate) - pytest with 70% coverage target
- Async throughout for LLM calls
- Primary: Ollama (qwen3:4b-instruct-32k) at
http://athena.int.liesdonk.nl:11434 - Secondary: OpenAI (API key in .env)
- Provider interface via
LangChainProvideradapter (supports any LangChain chat model)
- No TODO stubs in committed code - implement fully or not at all
- Type hints everywhere - use strict mypy settings
- Docstrings for public APIs
- Tests first where practical
- Keep functions focused and small
- Use tools as the source of truth — avoid repo-wide reasoning or "global refactors"
- Locate via rg, resolve via LSP — use
rgto find candidates, thenpylspdefinition/references to confirm symbol meaning - Renames must be semantic — use
pylsp/Rope rename for symbol renames; fall back torg+ manual edits only when LSP cannot handle the case, and explain why - Minimal diffs only — touch only files needed for the task; avoid drive-by formatting or cleanup
- Single formatting authority — do not use pylsp formatters (autopep8/YAPF); use
ruff check --fixandruff formaton changed files - Validate with targeted checks — prefer
uv run ruff,uv run mypy, and targeteduv run pytest(per Testing Rules); usepre-commit run --files ...for touched files, and runpre-commit run -aonly when requested or when it will not trigger a full suite
When a bug is not resolved by static tools (ruff/mypy/tests):
- Reproduce the failure with a minimal command.
- Do not edit code until reproduction is confirmed.
- Use
pdbfor interactive inspection if needed. - Prefer temporary logging before stepping.
- Form a concrete hypothesis before modifying code.
- Remove all debug hooks before final commit.
- Re-run pre-commit and tests after fixes.
Use debugging as a precision tool, not a trial-and-error loop.
Use structlog via get_logger() for all application logging:
from questfoundry.observability.logging import get_logger
log = get_logger(__name__)
# Structured event logging (preferred)
log.info("stage_complete", stage="dream", tokens=1234, duration="5.2s")
log.debug("tool_call_start", tool="search_corpus", query="mystery")
log.warning("validation_failed", field="genre", error="empty string")
log.error("provider_error", provider="ollama", message=str(e))Log Levels:
INFO: High-level flow events visible at default verbosity (stage start/complete, conversation phases)DEBUG: Detailed events for troubleshooting (tool calls, validation details, LLM responses)WARNING: Recoverable issues (missing optional config, fallbacks activated)ERROR: Failures that stop execution (provider errors, validation exhausted)
Event Naming:
- Use
snake_caseevent names as the first argument - Add structured key=value context, not string interpolation
- Good:
log.info("stage_complete", stage="dream", tokens=1234) - Bad:
log.info(f"Stage dream completed with {tokens} tokens")
What to Log:
- Phase/stage transitions (INFO)
- Tool calls and results (DEBUG)
- Validation attempts and failures (DEBUG/WARNING)
- LLM call counts and token usage (INFO at completion)
- Errors with context for debugging (ERROR/WARNING)
The Story Graph Ontology defines the authoritative ontology. The design specification (docs/design/00-spec.md) provides supplementary context. Pydantic models in src/questfoundry/models/ are hand-written implementations of that ontology. The graph (graph.db) is the runtime source of truth for story state.
docs/design/story-graph-ontology.md ← Authoritative ontology (node types, relationships)
↓
src/questfoundry/models/*.py ← Hand-written Pydantic models (validate LLM output)
↓
graph/mutations.py ← Semantic validation against graph state
↓
graph.db ← Runtime source of truth (SQLite, nodes + edges)
When adding new stage models:
- Check the ontology in
docs/design/story-graph-ontology.mdfor the node types and fields - Create/update Pydantic models in
src/questfoundry/models/ - Add semantic validation in
graph/mutations.pyif needed - Export from
models/__init__.py
Optional vs Nullable (semantic distinction):
- Optional (not in
required): field may be absent → Pydantic defaults toNone - Nullable: field explicitly accepts
nullas a value - LLMs often send
nullfor optional fields; usestrip_null_values()before validation to treatnullas absent
Before writing non-trivial code, think about what can go wrong.
Rushing to "done" instead of "correct" causes multiple review cycles. A 30-minute upfront analysis prevents hours of back-and-forth with reviewers.
Before implementing, enumerate:
- Empty inputs - empty strings, empty lists, empty dicts, None
- Missing fields - what if optional fields are absent? what if required fields are missing?
- Invalid types - wrong type passed, type not in supported set
- Special characters - quotes, newlines, tabs, unicode in string inputs
- Name collisions - what if two inputs produce the same output name?
- Error paths - what happens when external calls fail?
If you'd written test_empty_properties before implementing, you'd catch the bug before review. For each edge case identified, write a test that exercises it.
Especially defaults and fallbacks. Ask:
- "What happens if X is missing?"
- "What does this return when Y is None?"
- "Is this silent fallback hiding a bug?"
Silent fallbacks are bugs, not features. Code like items.get("type", "Any") silently produces invalid output. Prefer explicit errors: raise ValueError with a helpful message.
Notice patterns as you write, not when a reviewer points them out. If you copy-paste code and change one thing, extract a helper function immediately.
Don't just run tests—actually read the generated/output files. Tests verify behavior you thought of; inspection catches behavior you didn't.
# After running a stage, inspect the graph output
sqlite3 projects/myproject/graph.db ".schema"
# After a complex operation, verify the result manually
git diff --statBefore pushing, ask:
- "What would a careful reviewer find wrong with this?"
- "What questions would they ask?"
- "What edge cases would they test?"
If you can anticipate the feedback, fix it before pushing.
- Always fetch main before creating a branch - run
git fetch origin mainbeforegit checkout -b feat/...to avoid merge conflicts from stale base - Branch per feature/issue - create from
origin/main(not local main) - Document in issues and PRs - link to related issues
- Close issues from PRs - use
Closes #123(orFixes #123) in PR descriptions so GitHub auto-closes the issue on merge. Merely referencing#123does NOT close it.
Treat reviewer cognitive load as a primary constraint.
- Target: 150–400 net lines changed per PR
- Hard limit: Do not exceed 800 net lines or ~20 files without explicit instruction
- If you exceed the target: STOP and split into multiple PRs
When a PR grows too large, proactively offer to split it before continuing.
When splitting is needed, use this order (each PR should be mergeable independently):
- Mechanical-only PR - Renames, file moves, formatting, dependency bumps. NO behavior changes.
- Contract/protocol PR - Interfaces, types, schemas, tool definitions, minimal scaffolding. Include contract tests.
- Runner/plumbing PR - Wiring and orchestration. Include integration tests.
- Feature/stage PR(s) - Actual feature implementation in slices. Each PR = one coherent capability.
- Cleanup PR - Remove dead code, tighten types, refactor, docs. Keep separate.
When a feature is too large for a single PR, split it into a stack of dependent PRs. Each PR is a separate branch with multiple atomic commits, reviewed independently, and merged bottom-up.
Why vanilla Git instead of stacking tools (stack-pr, git-branchless):
- Preserves atomic commits (multiple commits per PR, not one fat commit)
- No external tool dependencies or version breakage
- CI triggers work reliably (no force-push hash mismatches)
- Full control over merge strategy
# PR 1: Models
git fetch origin main
git checkout -b feat/dress-models origin/main
git add -p && git commit -m "feat(models): add ArtDirection schema"
git add -p && git commit -m "feat(models): add IllustrationBrief schema"
git add -p && git commit -m "test(models): add DRESS model validation tests"
git push -u origin feat/dress-models
gh pr create --base main --title "feat(models): add DRESS stage Pydantic models"
# PR 2: Mutations (depends on PR 1)
git checkout -b feat/dress-mutations feat/dress-models
git add -p && git commit -m "feat(graph): add dress mutation helpers"
git add -p && git commit -m "feat(graph): add dress validation functions"
git add -p && git commit -m "test(graph): add dress mutation tests"
git push -u origin feat/dress-mutations
gh pr create --base feat/dress-models --title "feat(graph): add DRESS mutations"
# PR 3: Stage implementation (depends on PR 2)
git checkout -b feat/dress-stage feat/dress-mutations
# ... commits ...
git push -u origin feat/dress-stage
gh pr create --base feat/dress-mutations --title "feat(dress): implement DRESS stage"Stack visualization:
main
└─ feat/dress-models (PR1 → main) 3 commits
└─ feat/dress-mutations (PR2 → PR1) 3 commits
└─ feat/dress-stage (PR3 → PR2) 5 commits
Add new commits — never amend or rebase branches with open PRs:
# Fix something in PR 2
git checkout feat/dress-mutations
git add -p && git commit -m "fix: address review - add input validation"
git push origin feat/dress-mutations
# Propagate to dependent branches via merge
git checkout feat/dress-stage
git merge feat/dress-mutations
git push origin feat/dress-stageRules:
- Always
git push(never--forceor--force-with-lease) on branches with open PRs - Never
git commit --amendorgit rebaseon branches with open PRs - Update dependent branches one at a time, push before moving to the next
CRITICAL: Retarget dependent PRs BEFORE merging. If you merge with --delete-branch first, GitHub closes dependent PRs and they cannot be reopened. Always retarget, then merge.
# 1. Retarget PR 2 to main BEFORE merging PR 1
gh pr edit <PR2> --base main
# 2. Now merge the bottom PR (safe — PR 2 already points to main)
gh pr merge <PR1> --squash --delete-branch
# 3. Rebase PR 2 onto updated main to remove duplicate commits
git checkout feat/dress-mutations
git fetch origin main
git rebase origin/main
git push --force-with-lease origin feat/dress-mutations
# Wait for CI to pass
# 4. For the next layer, retarget FIRST again
gh pr edit <PR3> --base main
# 5. Merge PR 2
gh pr merge <PR2> --squash --delete-branch
# 6. Rebase PR 3 onto updated main
git checkout feat/dress-stage
git fetch origin main
git rebase origin/main
git push --force-with-lease origin feat/dress-stage
gh pr merge <PR3> --squash --delete-branchPattern: always gh pr edit --base main on the NEXT PR before gh pr merge on the CURRENT PR. This prevents the dependent PR from being auto-closed when the base branch is deleted.
Note: --force-with-lease is safe here because the parent PR was just squash-merged into main — rebasing removes the now-duplicate commits. This is the ONE case where force-push is acceptable.
If CI doesn't trigger after a push or rebase, close and reopen the PR:
gh pr close <number> && gh pr reopen <number>The workflow_dispatch trigger on the CI workflow also allows manual runs:
gh workflow run CI --ref <branch-name>PRs must meet ALL of these criteria before merging:
- CI must be completely green - all checks pass, no warnings treated as errors
- PR must be reviewed - at least one approval required
- Review feedback must be addressed - all comments resolved or responded to
- Branch must be up to date - rebase on main if needed
Never force-merge a PR with failing CI or unresolved reviews.
Every PR description MUST include:
## Problem
1–3 sentences describing why this change is needed.
## Changes
- Bullet list of what changed
## Not Included / Future PRs
- What is explicitly out of scope
- Link to follow-up issues if created
## Test Plan
- Commands run and results
- Coverage of new code
## Risk / Rollback
- Compatibility notes
- Feature flags if applicableFor PRs > 300 lines, add a Review Guide section with suggested file/commit order.
Take ALL review comments seriously. No exceptions. No excuses.
- Address every comment before requesting re-review
- If you disagree, explain your reasoning as a reply on the comment — don't silently ignore
- When a reviewer finds issues, fix them in the same PR before merging
- NEVER dismiss a concern with "this is pre-existing", "this was not my fault", "this is not critical", or "out of scope for this PR". If the concern is valid, fix it. If it's genuinely invalid, reply explaining why. If it's valid but you don't want to fix it now, create an issue and link it.
Respect reviewer merge advice:
| Reviewer Says | What It Means | What You Do |
|---|---|---|
| LGTM / Approve | Ready to merge | Merge when CI is green |
| Approve with minor fixes | Small changes needed, no re-review | Fix the items, reply to comments, push, merge when CI green |
| Changes requested | Substantive issues found | Fix all items, push, wait for another review round before merging |
| Comments only (no verdict) | Reviewer raised concerns | Address all comments, then request re-review |
NEVER merge a PR with "changes requested" status without getting re-approval after addressing the feedback.
- Conventional commits:
feat:,fix:,docs:,refactor:,test:,chore: - Commit message format:
type(scope): description - Small, atomic commits - one logical change per commit
- Never mix formatting/refactoring with behavior changes
- If running a formatter, isolate it in its own commit (or separate PR for large reformats)
Before every push, do a code review of your own changes.
After rebasing, resolving conflicts, or completing a feature:
- Run
git diff origin/main- review ALL changes that will be in the PR - Read each changed file - don't just skim, actually read the code
- Check for regressions - especially after conflict resolution, verify you didn't break something that was working
- Verify types and contracts - TypedDicts, Protocols, return types should be semantically correct
- Run targeted validation - type check and lint; run specific unit tests for changed modules:
Do NOT run the full test suite - that's CI's job.
uv run mypy src/ && uv run ruff check src/ uv run pytest tests/unit/test_<changed_module>.py -x -q # Only if relevant tests exist
Common mistakes to catch:
- Conflict resolution taking wrong version (e.g., breaking a TypedDict by making required fields optional)
- Forgetting to handle
Nonecases (useor []notget(..., [])when value might be explicitlyNone) - Redundant code left from earlier iterations
- Import statements that are no longer needed
Don't blindly trust "main is correct" - conflicts happen because code diverged, and either side might have the bug.
If estimated diff will exceed the target size:
- Stop - Do not proceed with a single large PR
- Plan - Write a slicing plan listing PR #1, #2, #3… with goals and file lists
- Implement as stacked branches - One branch per PR, each branching from its parent
- Submit incrementally - Create PRs with correct base branches (see "Stacked PRs" above)
# Example: splitting a large refactor into 3 PRs
git fetch origin main
# PR 1: Rename terminology in models
git checkout -b refactor/rename-models origin/main
git add -p && git commit -m "refactor(models): rename dilemma to choice"
git add -p && git commit -m "refactor(models): update TypedDict fields"
git push -u origin refactor/rename-models
gh pr create --base main --title "refactor(models): rename dilemma terminology"
# PR 2: Update graph layer (depends on PR 1)
git checkout -b refactor/rename-graph refactor/rename-models
git add -p && git commit -m "refactor(graph): update mutations for new names"
git push -u origin refactor/rename-graph
gh pr create --base refactor/rename-models --title "refactor(graph): update mutations"
# PR 3: Update tests (depends on PR 2)
git checkout -b refactor/rename-tests refactor/rename-graph
git add -p && git commit -m "test: update tests for terminology rename"
git push -u origin refactor/rename-tests
gh pr create --base refactor/rename-graph --title "test: update terminology tests"Merge bottom-up: PR 1, retarget PR 2 to main, merge PR 2, etc.
- If you discover additional work during implementation, do NOT include it
- Only include changes necessary for the PR's stated goal
- Never leave review suggestions dangling - if a reviewer suggests follow-up work ("you could also...", "consider adding..."), create a GitHub issue immediately and link it in your response
NEVER silently skip, postpone, or defer work without creating a GitHub issue.
This applies to:
- Work identified during implementation that is out of scope for the current PR
- Review comments suggesting improvements you choose not to fix now
- Known limitations, TODOs, or follow-up tasks mentioned in PR descriptions
- Findings from code review sweeps or audits
Every "Not Included / Future PRs" item in a PR description MUST have a corresponding GitHub issue linked. If you write "this will be addressed later" anywhere, there must be an issue tracking it.
Common violations to avoid:
- Mentioning deferred work in a PR description without creating issues
- Deciding something is "not critical" and silently dropping it
- Noting a finding during a sweep but not tracking it
- Promising follow-up in a review reply but never creating the issue
- Keep architecture docs up to date in
docs/architecture/ - Design docs in
docs/design/are guidelines (can be questioned); CLAUDE.md is rules (must be followed) - Document decisions in issues/PRs with rationale
- Update README.md when adding features
questfoundry/
├── src/questfoundry/
│ ├── __init__.py
│ ├── cli.py # typer CLI entry point
│ ├── pipeline/
│ │ ├── orchestrator.py # Stage execution
│ │ └── stages/ # Stage implementations
│ ├── prompts/
│ │ ├── compiler.py # Prompt assembly
│ │ └── loader.py # Template loading
│ ├── artifacts/
│ │ ├── reader.py
│ │ ├── writer.py
│ │ └── validator.py
│ ├── providers/ # LLM provider clients
│ └── export/ # Output format exporters
├── prompts/ # Prompt templates (outside src/)
│ ├── templates/
│ └── components/
├── tests/
│ ├── unit/
│ ├── integration/
│ └── e2e/
└── docs/
├── design/ # Design specifications
└── architecture/ # Implementation architecture
- Pipeline orchestrator skeleton
- DREAM stage implementation
- Basic prompt compiler
- Artifact schemas and validation
- CLI with
qf dreamcommand
- Multi-stage execution
- Context injection between stages
- Human gate hooks (UI separate concern)
- BRAINSTORM and SEED stages
- 11-phase GROW algorithm
- Path-agnostic assessment, intersection detection
- Arc enumeration and validation
- State derivation (codewords, overlays)
- Prose generation
- Export formats (Twee, HTML, JSON)
- Full validation and quality bars
# Quick validation (use these, not full test suite)
uv run mypy src/ # Type check - fast, no LLM
uv run ruff check src/ # Lint - fast, no LLM
uv run pytest tests/unit/test_<module>.py -x -q # Targeted unit test
# Full test suite (CI only - NEVER run locally without permission)
uv run pytest tests/unit/ -x -q # All unit tests (safe, no LLM)
uv run pytest tests/integration/ -x -q # Integration tests (USES LLM - expensive!)
uv run pytest --cov # With coverage (USES LLM - expensive!)
# CLI (once implemented)
qf dream # Run DREAM stage
qf run --to seed # Run up to SEED
qf status # Show pipeline state
qf inspect -p <project> # Inspect project quality (no LLM calls)
qf inspect -p <project> --json # Machine-readable JSON outputdocs/design/00-spec.md- Unified v5 specification (vision, pipeline, schemas)docs/design/procedures/- Stage algorithm specificationsdocs/design/01-prompt-compiler.md- Prompt assembly systemdocs/design/07-getting-started.md- Implementation slices
- Agent negotiation between LLM instances
- Incremental hook discovery during branching
- Backflow (later stages modifying earlier artifacts)
- Unbounded iteration
- Hidden prompts in code
- Complex object graphs instead of flat YAML
- Unit tests for individual functions/classes
- Integration tests for stage execution with mocked LLM
- E2E tests for full pipeline runs (may use real LLM)
- Target 70% coverage initially, increase later
- Use pytest fixtures for common test data
NEVER run the full test suite (uv run pytest tests/) without explicit user permission.
The test suite includes integration tests that make real LLM API calls. These are:
- Slow: Minutes to hours depending on provider
- Expensive: Each run costs real money (API tokens)
- Resource-intensive: Can saturate GPU/API rate limits
| Situation | Command | Why |
|---|---|---|
| Changed a specific file | uv run pytest tests/unit/test_<module>.py |
Test only what changed |
| Changed models/*.py | uv run pytest tests/unit/test_mutations.py tests/unit/test_*models*.py |
Model validation tests |
| Changed graph/*.py | uv run pytest tests/unit/test_graph*.py tests/unit/test_mutations.py |
Graph logic tests |
| Changed prompts | uv run mypy src/ && uv run ruff check |
Prompts don't have unit tests |
| Before pushing PR | uv run pytest tests/unit/ -x -q |
Unit tests only, stop on first failure |
| CI is failing | Run the specific failing test locally | Don't shotgun the whole suite |
- NEVER run
uv run pytest tests/oruv run pytestwithout-x(stop on first failure) - NEVER run integration tests (
tests/integration/) without user permission - NEVER run multiple test commands in parallel (saturates resources)
- NEVER run full suite "just to make sure" - that's what CI is for
When you need to verify changes work, use this minimal check:
uv run mypy src/questfoundry/ # Type check (fast, no LLM)
uv run ruff check src/ # Lint (fast, no LLM)
# Only if needed:
uv run pytest tests/unit/test_<specific>.py -x -qLet CI run the full suite. Your job is targeted verification.
Configuration follows a strict precedence order (highest to lowest):
- CLI flags -
--provider ollama/qwen3:4b-instruct-32k - Environment variables -
QF_PROVIDER=openai/gpt-4o(can be set in your shell or a.envfile) - Project config -
project.yamlproviders.default - Defaults -
ollama/qwen3:4b-instruct-32k
Different providers can be used for each LLM role (creative, balanced, structured). This allows using creative models for prose generation and reasoning models for structured output. Legacy phase names (discuss, summarize, serialize) are accepted as aliases.
Roles: creative (discuss), balanced (summarize), structured (serialize)
8-level precedence chain (per role):
- Role-specific CLI flag (
--provider-creative,--provider-balanced,--provider-structured) - General CLI flag (
--provider) - Role-specific env var (
QF_PROVIDER_CREATIVE,QF_PROVIDER_BALANCED,QF_PROVIDER_STRUCTURED) - General env var (
QF_PROVIDER) - Role-specific project config (
providers.creative,providers.balanced,providers.structured) - Role-specific user config (
~/.config/questfoundry/config.yaml) - Default project config (
providers.default) - Default user config
Legacy CLI flags (--provider-discuss, etc.) and env vars (QF_PROVIDER_DISCUSS, etc.) are accepted as aliases.
Example project.yaml with hybrid providers:
name: my-adventure
providers:
default: ollama/qwen3:4b-instruct-32k # Fallback for all phases
discuss: ollama/qwen3:4b-instruct-32k # Tool-enabled model for exploration
summarize: openai/gpt-4o # Creative model for narrative
serialize: openai/o1-mini # Reasoning model for JSON outputExample CLI usage:
# Override serialize phase to use o1-mini
qf seed --provider-serialize openai/o1-mini
# Full hybrid setup from CLI
qf seed --provider-discuss ollama/qwen3:4b-instruct-32k \
--provider-summarize openai/gpt-4o \
--provider-serialize openai/o1-miniNote: o1/o1-mini models don't support tools. While you can configure them for any phase, they will fail at runtime if tools are invoked. They are best suited for the serialize phase only.
# Provider configuration
QF_PROVIDER=ollama/qwen3:4b-instruct-32k # Override default provider
QF_PROVIDER_DISCUSS=ollama/qwen3:4b-instruct-32k # Override discuss phase
QF_PROVIDER_SUMMARIZE=openai/gpt-4o # Override summarize phase
QF_PROVIDER_SERIALIZE=openai/o1-mini # Override serialize phase
# Required for providers
OLLAMA_HOST=http://athena.int.liesdonk.nl:11434 # Required for Ollama
OPENAI_API_KEY=sk-... # Required for OpenAI
# Optional observability
LANGSMITH_TRACING=trueNote: OLLAMA_HOST and OPENAI_API_KEY are required for their respective providers. There are no defaults - you must explicitly configure them.
When stage validation fails or LLM output doesn't match expectations:
If asked to inspect or debug a specific project run, start with these files in projects/<dir>/:
logs/debug.jsonl— complete debug logs (primary signal for failures)logs/llm_calls.jsonl— full prompt + LLM traces (use to understand model output)graph.db— primary output artifact (SQLite database, current story state)snapshots/— pre-stage graph checkpoints for rollback/diagnosisexports/— derived outputs fromgraph.db(use for context only)
uv run qf --log -vvv dream --project myproject "prompt"This creates:
{project}/logs/llm_calls.jsonl- Full request/response for each LLM call{project}/logs/debug.jsonl- Structured application logs
# Pretty-print the last LLM response (JSONL = one JSON object per line)
python3 -c "
import json
with open('myproject/logs/llm_calls.jsonl') as f:
lines = f.readlines()
d = json.loads(lines[-1]) # Get last call
print(d['content'])
"
# Debug YAML parsing
python3 -c "
import json, yaml
with open('myproject/logs/llm_calls.jsonl') as f:
d = json.loads(f.readlines()[-1])
parsed = yaml.safe_load(d['content'])
print(json.dumps(parsed, indent=2))
"| Symptom | Likely Cause | Fix |
|---|---|---|
| Empty field in parsed YAML | YAML block extraction stops early | Check _extract_yaml_block handles multi-line content |
| YAML parse error | Model includes prose around YAML | Improve fence detection or extraction |
| Missing fields | Model omits optional fields | Make schema fields optional with defaults |
Key patterns for working with LLMs:
- Sandwich pattern: Repeat critical instructions at start AND end of prompt
- Validate → Feedback → Repair loop: For structured output, validate and ask model to fix errors
- Discuss → Freeze → Serialize: Separate conversation from structured output generation
- Model size considerations: Small models (≤8B) need simpler prompts, fewer tools
Since artifacts will be interpreted by other LLMs (not programmatic code), prefer:
- Strings with examples over strict enums (e.g.,
audience: strnotLiteral["adult",...]) - Inline examples in prompts to guide format:
audience: <e.g., adult, young adult, mature> - min_length=1 constraints to catch empty values while accepting variations
This allows LLM-generated variations like "adults" or "extensive" to pass validation while still providing guidance through examples in the prompt template.
When serializing structured output that references IDs from earlier stages:
Always provide an explicit
### Valid IDssection listing every ID the model is allowed to use. Never assume the model will correctly infer IDs from prose.
This is critical for preventing "phantom ID" errors where the model invents or misreferences IDs. Current implementation:
graph/context.py:format_valid_ids_context()builds entity and dilemma ID listsgraph/context.py:format_path_ids_context()builds path ID list after paths are serializedagents/serialize.py: Injects valid IDs before each serialization call
When adding new ID types that downstream sections reference:
- Collect IDs after their section is serialized
- Inject into context before downstream sections that reference them
- List with clear labels showing their purpose and valid values
Use explicit good/bad examples to prevent common errors:
## Dilemma ID Naming (CRITICAL)
GOOD: `host_benevolent_or_self_serving` (binary pattern)
BAD: `host_motivation` (ambiguous, could be confused with path name)
## What NOT to Do
- Do NOT write prose paragraphs with backstories
- Do NOT end with "Good luck!" or similar pleasantries
- Do NOT reuse dilemma IDs as path IDsThese patterns help chat-optimized models (like GPT-4o) avoid over-helpful behaviors that hurt structured output quality.
The DREAM stage is the reference implementation for new stages (see ADR-009). Key patterns:
All stages use the same Discuss → Summarize → Serialize pattern:
- Discuss: Explore/refine via dialogue and research tools (using
langchain.agents) - Summarize: Distill into concise narrative (direct model call)
- Serialize: Convert to validated YAML (structured output via
with_structured_output())
The ConversationRunner orchestrates all three phases and handles:
- Tool execution and result collection
- Validation and repair loops (max 3 retries)
- Message history management
- Token counting and logging
When serializing structured output (phase 3), all providers use the same strategy:
# All providers use JSON_MODE (json_schema method)
structured_model = model.with_structured_output(
schema=BrainstormOutput,
method="json_schema", # Native JSON mode
)Why JSON_MODE for all providers?
- Works reliably with complex nested schemas (BrainstormOutput, SeedOutput)
- The TOOL strategy (function_calling) was tried but returns None for complex schemas on Ollama
- Native JSON mode is efficient and consistent across providers
The two available strategies (for reference):
method="json_schema"(JSON_MODE): Uses provider's native JSON mode to constrain outputmethod="function_calling"(TOOL): Creates a fake tool with the schema, forces model to call it
The strategy selection is handled by with_structured_output() in providers/structured_output.py.
Using ChatPromptTemplate:
- LangChain's
ChatPromptTemplatefor variable injection - Template stored externally (prompts/templates/dream.md)
- Separated from serialization logic
When building prompts for stages:
- Create
prompts/templates/stagename.mdwith template text - Use
ChatPromptTemplate.from_template()to load and parameterize - Pass variables dict to template compilation
- Never hardcode prompts in Python—always externalize
All tool results must return structured JSON (per ADR-008):
# Success
{
"result": "success",
"data": { ... },
"action": "Use this information to inform decisions."
}
# No results
{
"result": "no_results",
"query": "...",
"action": "No matching guidance found. Proceed with your instincts."
}
# Error
{
"result": "error",
"error": "Connection timeout",
"action": "Tool unavailable. Continue without this information."
}This prevents infinite loops where LLM follows "try again" instructions repeatedly.
When structured output validation fails:
def _validate_dream(self, data: dict[str, Any]) -> ValidationResult:
"""Validate using Pydantic model.
Returns structured errors that guide LLM correction.
"""
try:
DreamArtifact.model_validate(data)
return ValidationResult(valid=True, data=...)
except ValidationError as e:
# Convert to structured errors (field, issue, requirement)
return ValidationResult(
valid=False,
errors=pydantic_errors_to_details(e.errors()),
expected_fields=get_all_field_paths(DreamArtifact),
)The error format tells LLM exactly what's wrong and what to fix, enabling recovery in 1-2 retries.
- Original vision: https://gist.github.com/pvliesdonk/35b36897a42c41b371c8898bfec55882
- v4 issues: pvliesdonk/questfoundry-v4#350
- Parent RFC: pvliesdonk/questfoundry-v4#344