Skip to content

Add zero-model-call prompt-assembly snapshot gate - #2145

Merged
mstomar125 merged 3 commits into
mainfrom
issue-2119-prompt-assembly-snapshot-gate
Aug 18, 2026
Merged

mstomar125 merged 3 commits into
mainfrom
issue-2119-prompt-assembly-snapshot-gate

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

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 that live_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

Test Production function(s) exercised Golden file
resident_system_prompt_matches_golden PromptAssembler::assemble_static (documented DB-free path for assemble()) resident_system_prompt.golden
resident_workspace_context_matches_golden context_ops::WorkspaceContext::format_for_prompt — RELEVANT ENTITY TYPES site 2 resident_workspace_context.golden
stage2_candidate_block_matches_golden routing::render_candidates_for_prompt (incl. rendered instruction subtrees) — RELEVANT ENTITY TYPES site 1 stage2_candidate_block.golden
stage2_tool_surface_matches_golden routing::stage2_tools (names, descriptions, full parameter schemas) stage2_tool_surface.golden
stage1_request_matches_golden STAGE1_SYSTEM_PROMPT + stage1_tool_definitions() stage1_request.golden

All 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 --ignored needed.

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:

  • Resident prompt nodes: PromptAssembler::assemble_static() is PromptAssembler's own documented DB-free test path — it runs the exact same seed content (seed_agent_guidance_nodes(), sourced from agent_guidance.rs) through the same Minijinja rendering assemble() 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 instructions: built from skill_pipeline::seed_skill_nodes() — the real production skill corpus, not text authored for this test — run through the real prepare_nodes_from_template parser. The resulting PreparedNodes are assembled into an in-memory node_map/adjacency_list and passed to the same flatten_subtree_content function that skill_ops::render_skill_instructions and PromptAssembler::fetch_prompt_body both 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.
  • Schema/entity-type data: hand-built SchemaNode fixtures (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_template present/absent, and a name-only related type. schema_metadata for Stage-2 candidates is encoded through the real EntityTypeDescriptor::to_json() contract (the same reversible encode/decode skill_ops and routing::render_schema_metadata use), not a hand-rolled JSON shape.
  • Tool surface: model_facing_tool_definitions() — the exact fallback production's own available_tools() returns when node_service is None (i.e. no DB), so it's a real production code path, not a substitute.
  • Budget constants match production: WORKSPACE_CONTEXT_MAX_CHARS = 4000 mirrors local_agent_service.rs's real context.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 one insta uses internally for its own diff rendering); adding it directly rather than depending on insta keeps 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's update_requested() branch), gated on UPDATE_GOLDEN=1:

UPDATE_GOLDEN=1 cargo test -p nodespace-agent --test prompt_assembly_snapshot

A bare cargo test never 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, via UPDATE_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:

  1. Appended a sentence to agent_guidance.rs's SCHEMA_CREATION_RULES ("TEMP DRIFT-PROOF EDIT — DO NOT COMMIT.").
  2. Ran cargo test -p nodespace-agent --test prompt_assembly_snapshot: exactly resident_system_prompt_matches_golden failed, with a diff pinpointing the added sentence on its own line; the other 4 tests (which don't read agent_guidance.rs) stayed green — confirming the gate isolates drift to the actual site that changed.
  3. Confirmed via shasum that the golden file itself was untouched by the failing run.
  4. Reverted the edit; git diff --stat packages/agent/src/agent_guidance.rs is 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 failures
  • cargo clippy -p nodespace-agent --all-targets -- -D warnings -D clippy::unused-async — clean
  • cargo clippy --all-targets across 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 pinned binaries/nodespaced-aarch64-apple-darwin artifact — reproduced identically with these changes stashed out on unmodified main, so it's a pre-existing environment issue, not caused by this PR.
  • cargo fmt -p nodespace-agent -- --check — clean

Out of scope (per the issue, respected here)

  • No change to any model-facing constant's content — tools.rs, agent_guidance.rs, skill_pipeline.rs, routing.rs, context_ops.rs are read-only inputs to this test (the drift-proof edit above was reverted, not kept).
  • Does not fix any divergence between production assembly and the Author and tune the golden prompt corpus: production-shaped, dev-domain prompt strings #2122 tuned corpus — that's core#2120.
  • Does not touch golden_runner.rs or the packages/agent/goldens/*.toml corpus.

🤖 Generated with Claude Code

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]>
mstomar125 and others added 2 commits August 18, 2026 15:13
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]>
@mstomar125

Copy link
Copy Markdown
Contributor Author

Adversarial review + fix

Spawned 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): resident_system_prompt_matches_golden originally called 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 — which splits a "HEADER:\n- bullet" body into a parent/children node subtree — then reassembles it via flatten_subtree_content, stripping each bullet's "- " prefix and rejoining 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 didn't reflect real production output, and the gate gave zero protection against a regression in that parse/flatten mechanism (precisely the "seed prompt body dropped" bug class fetch_prompt_body's own doc comment names).

Fixed in 550cb21 (and its predecessor d0d248e): added assemble_resident_system_prompt(), which reproduces assemble()'s real composition (seed → prepare_nodes_from_template → flatten_subtree_content → Minijinja render → join) using the same fixture-building approach already used correctly for the Stage-2 skill-instruction fixture. Regenerated resident_system_prompt.golden accordingly.

Re-verified the drift-detection proof twice against the corrected implementation:

  1. A single-line constant edit (SCHEMA_CREATION_RULES) — caught, isolated to the one affected test.
  2. A bullet added to the multi-line TOOL_STRATEGY_RULES body — caught with a pinpoint diff, confirming the exact mechanism the review flagged is now genuinely exercised.

Both reverted cleanly (git diff --stat empty on agent_guidance.rs after each).

SHOULD-FIX (addressed): documented that the fixture's workspace context never approaches the 4000-char budget, so format_for_prompt's truncation branches aren't exercised by this gate (that's context_ops.rs's own unit-test territory). Also corrected a second stale doc comment left over from the blocker fix.

Everything else the review checked came back clean: schema_metadata fixture matches production's real EntityTypeDescriptor::to_json encoding, skill-instruction fixture faithfully mirrors get_subtree_data's node_map/adjacency shape, model_facing_tool_definitions() genuinely matches production's no-DB fallback, stage2_tools genuinely hits its scoped (non-fail-open) branch, the never-auto-update guarantee has no bypass path, no hidden non-determinism, and scope is clean (no production file content touched).

Final self-verification before merge

  • Read the full PR diff end-to-end (all 3 commits).
  • cargo test -p nodespace-agent --test prompt_assembly_snapshot — 5/5 pass, run repeatedly for determinism.
  • cargo test -p nodespace-agent (full crate) — 0 failures.
  • cargo clippy -p nodespace-agent --all-targets -- -D warnings -D clippy::unused-async — clean.
  • cargo fmt -p nodespace-agent -- --check — clean.
  • Confirmed agent_guidance.rs has zero diff (both drift-proof edits fully reverted).

Self-approving and merging.

@mstomar125
mstomar125 merged commit 8c53781 into main Aug 18, 2026
@mstomar125
mstomar125 deleted the issue-2119-prompt-assembly-snapshot-gate branch August 18, 2026 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prompt-assembly snapshot gate: assert production reproduces the tuned golden prompt (zero model calls)

1 participant