Repository navigation
Add zero-model-call prompt-assembly snapshot gate - #2145
Conversation
Closes #2119. Adds a deterministic test (tests/prompt_assembly_snapshot.rs) that calls the real production assembly functions — PromptAssembler:: assemble_static (the documented DB-free path for assemble()), context_ops::WorkspaceContext::format_for_prompt, routing:: render_candidates_for_prompt, routing::stage2_tools, STAGE1_SYSTEM_PROMPT, and stage1_tool_definitions() — against committed, in-process fixtures, and diffs the result against a checked-in golden file per assembly site. Fixtures are built without a DB: skill instructions come from the real skill_pipeline::seed_skill_nodes() corpus run through prepare_nodes_from_ template and the same flatten_subtree_content function production's render_skill_instructions calls, with a hand-built node_map/adjacency_list standing in for the DB round trip. Schema metadata is encoded through the real EntityTypeDescriptor::to_json contract rather than a hand-rolled shape. Failure output is a similar::TextDiff line diff naming which golden section and which line changed, not a raw blob comparison. A golden is only ever written when UPDATE_GOLDEN=1 is set; a bare test failure never touches the checked-in file, and a missing golden is a hard failure rather than a silent auto-create. Verified the gate catches drift with an actual edit: appended text to agent_guidance.rs's SCHEMA_CREATION_RULES, confirmed exactly the resident- system-prompt test failed with a diff pinpointing the added sentence while the other four stayed green, then reverted (working tree is byte-identical to before). Co-Authored-By: Claude <[email protected]>
Review on PR #2145 found that resident_system_prompt_matches_golden used PromptAssembler::assemble_static(), which renders each seed's raw markdown_content string directly. Real PromptAssembler::assemble() instead parses each seed through prepare_nodes_from_template (splitting a "HEADER:\n- bullet" body into a parent/children node subtree) and reassembles it via flatten_subtree_content, which strips each bullet's "- " prefix and rejoins with blank lines. For agent_guidance.rs's multi-line TOOL_STRATEGY_RULES/SCHEMA_CREATION_RULES bodies this produces materially different text than assemble_static's raw passthrough, so the golden did not reflect what production actually sends and gave no protection against a regression in that parse/flatten mechanism (the exact "seed prompt body dropped" bug class fetch_prompt_body's own doc comment names). Adds assemble_resident_system_prompt(), which reproduces assemble()'s composition (seed -> prepare_nodes_from_template -> flatten_subtree_content -> Minijinja render -> join) using the same fixture-building blocks already used correctly for the Stage-2 skill-instruction fixture, instead of calling assemble_static(). Regenerated resident_system_prompt.golden accordingly and re-verified the drift-detection proof against both a single-line and a multi-line (bullet-list) constant edit. Co-Authored-By: Claude <[email protected]>
Follow-up to the previous commit's fix: the resident_system_prompt_matches_golden test's own doc comment still described the site as going through PromptAssembler::assemble_static()'s "documented DB-free path", which is no longer accurate now that it uses assemble_resident_system_prompt() instead. Co-Authored-By: Claude <[email protected]>
Adversarial review + fixSpawned an adversarial review focused specifically on "can this gate be silently defeated" (fixture-to-production fidelity, whether goldens could be too permissive, whether the never-auto-update guarantee actually holds, determinism, scope discipline). Full findings: BLOCKER (fixed): Fixed in 550cb21 (and its predecessor d0d248e): added Re-verified the drift-detection proof twice against the corrected implementation:
Both reverted cleanly ( SHOULD-FIX (addressed): documented that the fixture's workspace context never approaches the 4000-char budget, so Everything else the review checked came back clean: Final self-verification before merge
Self-approving and merging. |
Summary
Closes #2119. Adds
packages/agent/tests/prompt_assembly_snapshot.rs— a deterministic, zero-model-call test that calls the REAL production prompt-assembly functions against committed fixtures and diffs the result against a checked-in golden file. This is the snapshot half of the golden-prompt methodology thatlive_stage1_golden_prompts.rs's header names as "not yet written": "does this exact prompt text get the right tool call" (answerable there, live-model) vs "does the real pipeline assemble that exact prompt from real inputs" (this test, zero-model-call).Coverage — all 5 sites, 5 tests, 5 golden fragments
resident_system_prompt_matches_goldenPromptAssembler::assemble_static(documented DB-free path forassemble())resident_system_prompt.goldenresident_workspace_context_matches_goldencontext_ops::WorkspaceContext::format_for_prompt—RELEVANT ENTITY TYPESsite 2resident_workspace_context.goldenstage2_candidate_block_matches_goldenrouting::render_candidates_for_prompt(incl. rendered instruction subtrees) —RELEVANT ENTITY TYPESsite 1stage2_candidate_block.goldenstage2_tool_surface_matches_goldenrouting::stage2_tools(names, descriptions, full parameter schemas)stage2_tool_surface.goldenstage1_request_matches_goldenSTAGE1_SYSTEM_PROMPT+stage1_tool_definitions()stage1_request.goldenAll 5 functions under test are synchronous and pure — the whole suite runs in ~10ms as part of the default
cargo test -p nodespace-agent, no--ignoredneeded.Design decisions
Fixture format — real production code, fixture data, no DB
Rather than hand-authoring prompt strings (which is what
packages/agent/goldens/*.toml— the #2122 corpus — already does for the live-model tuning loop), this test calls the actual assembly functions and supplies only their inputs as committed fixtures:PromptAssembler::assemble_static()isPromptAssembler's own documented DB-free test path — it runs the exact same seed content (seed_agent_guidance_nodes(), sourced fromagent_guidance.rs) through the same Minijinja renderingassemble()uses. It skips only the graph fetch (a DB round trip that hands back the identical committed seed content), not any model-facing logic.skill_pipeline::seed_skill_nodes()— the real production skill corpus, not text authored for this test — run through the realprepare_nodes_from_templateparser. The resultingPreparedNodes are assembled into an in-memorynode_map/adjacency_listand passed to the sameflatten_subtree_contentfunction thatskill_ops::render_skill_instructionsandPromptAssembler::fetch_prompt_bodyboth call against a live DB. The seed parse and the subtree flatten are each production code; only the DB round trip between them is skipped.SchemaNodefixtures (ticket,adr,release) standing in for what semantic schema retrieval would return — retrieval itself is out of scope (no embedding service, no DB) per the issue's hard requirements. Fixture schemas are chosen to exercise every rendering branch: required field, enum field, plain field,title_templatepresent/absent, and a name-only related type.schema_metadatafor Stage-2 candidates is encoded through the realEntityTypeDescriptor::to_json()contract (the same reversible encode/decodeskill_opsandrouting::render_schema_metadatause), not a hand-rolled JSON shape.model_facing_tool_definitions()— the exact fallback production's ownavailable_tools()returns whennode_serviceisNone(i.e. no DB), so it's a real production code path, not a substitute.WORKSPACE_CONTEXT_MAX_CHARS = 4000mirrorslocal_agent_service.rs's realcontext.format_for_prompt(4000)call, so a future change to that budget is itself something this gate would need a golden update for.Diff mechanism
similar::TextDiff(line-level) renders a readable unified-style diff on mismatch — old lines prefixed-, new lines+, 3 lines of context — naming the failing section and the golden file path. This is a small, zero-transitive-dependency, MIT-licensed crate (same oneinstauses internally for its own diff rendering); adding it directly rather than depending oninstakeeps the update-gating logic (see below) fully explicit and auditable in this PR rather than resting on a third-party crate's default-mode semantics.Regeneration command / never-auto-update guarantee
A golden file is written in exactly one place in the code (
golden::assert_matches'supdate_requested()branch), gated onUPDATE_GOLDEN=1:A bare
cargo testnever reaches that branch. A missing golden file is a hard failure with an explanatory panic message (never silently created), and a content mismatch is a hard failure with a diff (never a silent pass). Proved this with an actual edit — see below.What day-one golden content represents
Per the issue's explicit instruction to make this choice visible rather than pick silently: the committed goldens reflect CURRENT production assembly output, not the #2122 tuned target. These tests call the real assembly code against fixtures and pin whatever it emits today. They do not compare against
packages/agent/goldens/*.toml's hand-authored, live-model-validated content, and they do not judge or fix any gap between the two. core#2120 is the follow-up that brings production's emitted text in line with the tuned corpus — at which point its PR updates these goldens (deliberately, viaUPDATE_GOLDEN=1, with the diff reviewed) as part of that work.Verified: the gate actually catches drift
Per the acceptance criteria, proved this with a real edit rather than by inspection:
agent_guidance.rs'sSCHEMA_CREATION_RULES("TEMP DRIFT-PROOF EDIT — DO NOT COMMIT.").cargo test -p nodespace-agent --test prompt_assembly_snapshot: exactlyresident_system_prompt_matches_goldenfailed, with a diff pinpointing the added sentence on its own line; the other 4 tests (which don't readagent_guidance.rs) stayed green — confirming the gate isolates drift to the actual site that changed.shasumthat the golden file itself was untouched by the failing run.git diff --stat packages/agent/src/agent_guidance.rsis empty and all 5 tests pass again.Testing
cargo test -p nodespace-agent --test prompt_assembly_snapshot— 5/5 pass, run 3× to confirm determinism (identical output every run)cargo test -p nodespace-agent(full crate suite) — 0 failurescargo clippy -p nodespace-agent --all-targets -- -D warnings -D clippy::unused-async— cleancargo clippy --all-targetsacross every other workspace member (nodespace-types,nodespace-core,nodespace-nlp-engine,nodespace-proto,nodespace-daemon,nodespace-cli) — clean.nodespace-app(desktop-app) fails its build script on this box looking for a pinnedbinaries/nodespaced-aarch64-apple-darwinartifact — reproduced identically with these changes stashed out on unmodifiedmain, so it's a pre-existing environment issue, not caused by this PR.cargo fmt -p nodespace-agent -- --check— cleanOut of scope (per the issue, respected here)
tools.rs,agent_guidance.rs,skill_pipeline.rs,routing.rs,context_ops.rsare read-only inputs to this test (the drift-proof edit above was reverted, not kept).golden_runner.rsor thepackages/agent/goldens/*.tomlcorpus.🤖 Generated with Claude Code