Skip to content

Read-then-write turn completion is prose-only where the loop already has a structural-guard pattern for the same failure class #1804

Closed

Description

@malibio

Overview

A full read of the prompting system (prompt_assembler.rs, agent_guidance.rs, skill_pipeline.rs, skill_rules.rs, agent_loop.rs, tools.rs, response_processing.rs, skill_ops.rs, SKILL.md/gen_skill_md.rs, plus ADR-030/035/038/040/056) shows a system that is more structurally sound than #1802 characterized it. Real code-enforced guards already exist for several behavioral invariants (anti-fabrication, narrated-tool-call detection, duplicate-call loop breaking, cross-turn duplicate-write refusal, tool-failure surfacing, response-hygiene stripping), a real single-source-of-truth mechanism already exists for the schema-authoring rules shared between the local agent and SKILL.md (skill_rules.rs + gen_skill_md.rs --check, CI-verified), and a real eval-contamination guard already exists and is enforced by a passing test (guidance_is_not_contaminated_by_eval_prompts).

The system's actual problems are narrower and different in kind than "the resident block is too big": (1) one specific behavioral invariant — read-then-write turn completion — is enforced only by asking the model nicely, with no structural backstop, and it is the one invariant the evidence shows the model violates under the routing variant that exercises it; (2) one specific piece of parameter guidance (SCHEMA_CREATION_RULES's core_values key) is wrong and silently swallowed by the API instead of erroring, despite the underlying validation (validate_schema_field, validate_template_tokens in packages/core/src/behaviors/mod.rs) being real and already correct; (3) the measurement discipline that would let anyone tell a real regression from noise is not applied by default — single-run scores are still being cited as deltas.

This issue supersedes #1802's diagnosis (which attributed the failure to resident-block size and duplication generally) and narrows its proposed remedy to what the evidence actually supports.

Problem Statement

1. One invariant is prose-only where it should be structural: read-then-write turn completion

agent_loop.rs:597 ends a turn the moment the model emits zero tool calls in an iteration. Nothing downstream of that check knows whether the preceding tool call was a search_nodes that matched a write-shaped user request. The only thing standing between "search fired, write didn't" and a silently-incomplete turn is the READ-THEN-WRITE TURN COMPLETION bullet in TOOL_STRATEGY_RULES (packages/agent/src/agent_guidance.rs:49).

This is the one clear case, among many other invariants in this codebase, where prose is doing a job code should do. Contrast with what the loop already enforces structurally in the same file:

  • contains_action_claim + the anti-fabrication guard (agent_loop.rs:165-202, 627-638) — converts a fabricated success claim into a confirmation request.
  • looks_like_narrated_tool_call (agent_loop.rs:258-269, 648-661) — catches a tool call printed as prose instead of invoked.
  • The duplicate-call loop breaker (agent_loop.rs:794-808) and the cross-turn duplicate-write guard (agent_loop.rs:854-901, keyed on Tool::write_semantics in tools.rs:1029-1067) — both fully code-enforced, with dedicated tests (duplicate_tool_call_breaks_loop, and the RecordingToolExecutor tests) that assert on executor-level ground truth, not just response text.
  • Tool-failure surfacing (agent_loop.rs:663-702) — replaces a response that ignores a tool error with an honest failure message.

Read-then-write completion has none of this. It is the same class of problem as fabrication and narration (the model produces a plausible-looking turn that didn't actually do the thing), but it is the one member of that class left as a single prompt bullet. The eval evidence (#1802's variant B: 3/12, with all three schema-creation scenarios stopping after search_skills) is exactly this failure mode, one hop earlier — the model treats retrieval as if it completed the turn.

2. Schema field guidance has a real, narrow defect: core_values vs coreValues

SCHEMA_CREATION_RULES (agent_guidance.rs:20-27) teaches JSON key "core_values" for enum fields. SchemaField (packages/nodespace-types/src/schema.rs:29-38, #[serde(rename_all = "camelCase")]) accepts only coreValues on the wire. CreateSchemaParams (packages/core/src/schema/mod.rs:139-164) has no deny_unknown_fields, so a field object built from the resident guidance silently loses its enum values before it ever reaches validation — the field arrives at validate_schema_field (packages/core/src/behaviors/mod.rs:1306-1316) looking like an enum with none, and the correct-and-real validator (tested, and correctly rejecting genuinely-empty enums) fires a message that reads as "you forgot the values" when the actual defect is "the key you were told to use doesn't exist."

This is a narrower bug than #1802 framed it as ("two surfaces... hand-synced, and have drifted"). It is not evidence that the split between resident and retrieved guidance is unworkable — skill_pipeline.rs's schema_creation_guidance() gets this right (coreValues in every worked example, tools.rs:597-668 in the tool schema also uses coreValues throughout). It is evidence that exactly one hand-written prose string has a typo relative to the wire format it describes, caught nowhere because nothing asserts prose-vs-schema key agreement.

3. The measurement discipline exists on paper but was not applied

ADR-056 already states plainly: "The agent guidance contains verbatim prompt strings from the eval harness alongside the exact tool call each should produce, so scenarios can pass by matching a planted example rather than by generalizing... scores quoted from runs made under that condition... should be re-established." That contamination has since been fixed and is now a passing CI-adjacent test (guidance_is_not_contaminated_by_eval_prompts, agent_guidance.rs:311-426) with its own trip-wire (contamination_guard_detects_a_planted_example).

Separately, agent-eval.md's own numbers show three runs of identical code scoring 10/12, 9/12, 8/12 — a spread of 2 scenarios with nothing changed. The 6-vs-3-vs-5 comparison in #1802 was a single run per variant. Per the harness's own documented noise floor, the 6-vs-5 (variant A vs C) delta is not distinguishable from run-to-run noise; only the 6-vs-3 (variant A vs B) delta, which crosses the noise floor, is a defensible signal.

4. Layering is more coherent than #1802's framing suggests, with one real gap

The system already has a documented rule for what goes where (prompt-skill-system.md): resident prompt nodes carry safety/identity/formatting invariants that must be true on every turn regardless of intent; skills carry task-specific procedure delivered only when retrieved; the tool JSON schema carries parameter shape. skill_rules.rs is a genuine single source of truth for the schema-authoring rules that must appear on both the local-agent surface and the external SKILL.md surface — it renders both an imperative and a prose form from the same constant, and gen_skill_md.rs --check fails CI if SKILL.md's generated block drifts from it. This is real infrastructure, not aspirational.

The actual gap is narrower: SCHEMA_CREATION_RULES in agent_guidance.rs (resident, always paid) restates several rules that skill_rules.rs's SchemaRules already state once, correctly, and share with SKILL.md — but SCHEMA_CREATION_RULES is a separate hand-written string, not rendered from skill_rules.rs, so it can (and did) drift independently. The fix is not "move parameter mechanics into tool descriptions" (see below) — it's "stop hand-writing a second copy of rules skill_rules.rs already owns."

Why this does not adopt #1802's "move parameter mechanics into tool schemas" proposal

#1802 proposed: safety invariants resident, parameter mechanics in tool JSON description fields, worked examples in skills. This issue does not adopt that split, for reasons the evidence in this repo already surfaces:

  • The tool schema descriptions for create_schema/update_schema (tools.rs:597-668, 670-748) already carry real parameter mechanics — the title_template field description states the placeholder-must-have-a-field rule verbatim, and the coreValues field description states the enum shape. Comparing them to SCHEMA_CREATION_RULES shows the tool-schema copies are already correct where the resident prose is wrong. That is direct evidence against "prose is what's unreliable, schemas are what's reliable" as a general principle — here it's the reverse, and the fix is deleting the wrong copy, not relocating a category of rule.
  • There is no evidence in this codebase (or cited by Spike: re-evaluate the agent prompting architecture from first principles #1802) that an E4B-tier model attends to tool-schema description text with the same weight as system-prompt prose. ADR-038 Finding 2 shows the model reproduces a skill's pushed instruction subtree faithfully — that is evidence for skills-carry-procedure, not for tool-schema-carries-procedure. Relocating parameter mechanics into tool descriptions on the strength of an unverified attention-weighting assumption would trade a known-drift-prone location (hand-written prose) for an unmeasured one.
  • The title_template/coreValues invariants are the ones with real downstream structural validation already (behaviors/mod.rs:1306-1365, both under test). The lesson from the core_values bug is "the single point of prose-to-wire-format translation needs to not have typos," which skill_rules.rs's generation mechanism already solves for the rules it owns — extending its coverage is a smaller, better-evidenced change than restructuring where parameter mechanics live.

Proposed Solution

  1. Fix the core_values → coreValues defect in SCHEMA_CREATION_RULES (agent_guidance.rs:27) immediately — it is a plain typo relative to the wire format, independent of anything else in this issue.

  2. Delete SCHEMA_CREATION_RULES as hand-written prose; render its content from skill_rules.rs the same way skill_pipeline.rs's schema_creation_guidance() and gen_skill_md.rs already do. The resident copy keeps only what is genuinely turn-invariant and not already a SchemaRule (the NODE MODEL: / "DATABASE" = SCHEMA / INSTANCE vs TYPE intent-routing rules, which variant C's regression shows must stay resident). Everything else it currently restates (FIELD RULES, ENUM FIELDS, TITLE TEMPLATE) should be the same rendered SchemaRule.imperative strings skill_pipeline.rs uses, so there is exactly one hand-authored copy of each rule and SCHEMA_CREATION_RULES cannot drift from it by construction. Add a test analogous to gen_skill_md.rs's staleness check, or fold this into the same skill_rules.rs-sourced rendering prompt_assembler.rs:290 consumes, so drift is compile-time or CI-time impossible rather than a discipline.

  3. Make read-then-write turn completion a structural guard in agent_loop.rs, not a prose rule. When a turn's tool-call history in the current turn contains a search_nodes/resolve_query call and the model then emits zero tool calls with response text that does not itself request clarification, and the original user message is write-shaped (this needs a cheap, conservative signal — e.g. presence of an update/mark/change/set-type verb, or simply: any turn that called a read tool matching is_cross_turn_guarded_tool's sibling notion of "read used for a write-shaped request" and stopped) — treat it the same way contains_action_claim and looks_like_narrated_tool_call are already treated: do not accept the stop as final; either force one more iteration or convert to the existing CONFIRMATION_REQUEST. This is the same shape of guard as the two that already exist at agent_loop.rs:627-638 and 648-661; it is not a new mechanism, it is applying an existing mechanism to the one invariant that currently lacks it. Exact trigger heuristic is an implementation decision — the acceptance criteria below require it be conservative enough not to false-positive on legitimate read-only turns (a search that finds nothing, or a request that was genuinely just a lookup).

  4. Do not restructure resident-vs-retrieved-vs-schema layering beyond item 2. The token-budget argument in Spike: re-evaluate the agent prompting architecture from first principles #1802 (resident block is ~89% of prompt, doesn't scale with the registry) is real as a scaling concern but is not what the evidence shows caused the measured regressions — the measured regressions (6/12 → 3/12) came from the model stopping after search_skills before acting, which is the read-then-write gap (item 3), not from prompt size. Track registry-scaling as a separate, lower-priority concern; it is not blocking and should not be conflated with the correctness fixes here.

  5. Establish the eval-noise floor as a review gate, not just documentation. agent-eval.md already states the 2-scenario noise floor from three identical runs. Require any PR that reports a matrix score change to include at least 2 runs per variant compared (already partially required by Spike: re-evaluate the agent prompting architecture from first principles #1802's acceptance criteria; restate it here as this issue's own bar since Spike: re-evaluate the agent prompting architecture from first principles #1802 is being narrowed) and to explicitly note whether the claimed delta clears the documented noise floor.

Acceptance Criteria

  • core_values corrected to coreValues in SCHEMA_CREATION_RULES (agent_guidance.rs:27)
  • SCHEMA_CREATION_RULES's field/enum/title-template rules are rendered from skill_rules.rs SchemaRule constants (the same source skill_pipeline.rs::schema_creation_guidance() and gen_skill_md.rs already use), not hand-duplicated; a staleness test fails the build if they diverge
  • The resident-only intent-routing rules (NODE MODEL, "DATABASE" = SCHEMA, INSTANCE vs TYPE, NO CONFIRMATION FOR KNOWN TYPES, SCHEMA SUCCESS/FAILURE) remain resident and unchanged in substance — variant C's regression (schema turns calling no tools at all once resident schema rules were removed) must not reproduce
  • A structural guard in agent_loop.rs catches a turn that performed a read (search_nodes/resolve_query) for what the request implies is a write-shaped intent, then stopped without the write — implemented in the same style as contains_action_claim/looks_like_narrated_tool_call (converts to CONFIRMATION_REQUEST or forces continuation), with a unit test analogous to the existing anti-fabrication/narration tests
  • The new guard does not regress legitimate read-only turns — add a test asserting a plain search-and-report turn (no write-shaped intent) is left alone
  • Matrix re-measurement uses ≥2 runs per variant compared, purged DB + rebuilt daemon + skipped=0 verified per run (per agent-eval.md), and the PR states whether the observed delta clears the documented ~2-scenario noise floor
  • Per-scenario tool traces recorded, not just a headline count
  • Tests pass (bun run test:all)
  • Code passes bun run quality:fix

Technical Specifications

  • packages/agent/src/agent_guidance.rs:20-27 — SCHEMA_CREATION_RULES, containing the core_values/coreValues defect and the duplicated field/enum/title-template rules
  • packages/agent/src/skill_rules.rs:48-113 — SchemaRules (ONE_SCHEMA_PER_REQUEST through TITLE_TEMPLATE_PLACEHOLDERS), the correct single source these rules should render from; imperative/prose dual rendering already exists
  • packages/agent/src/skill_pipeline.rs:35-113 — schema_creation_guidance(), the existing correct consumer of skill_rules.rs for the retrieved surface; use as the template for how SCHEMA_CREATION_RULES should be built
  • packages/agent/src/bin/gen_skill_md.rs:1-131 — existing staleness-check pattern (--check/--write) to mirror for the new resident-render staleness test
  • packages/agent/src/prompt_assembler.rs:290 — where SCHEMA_CREATION_RULES + TOOL_STRATEGY_RULES are concatenated into the resident "Tool Strategy Guide" seed node; also prompt_assembler.rs:352-383, the seed_prompt_bodies_match_expected_bytes test that will need its expected-bytes literal updated when SCHEMA_CREATION_RULES changes shape
  • packages/agent/src/local_agent/agent_loop.rs:158-202 (contains_action_claim), :245-269 (looks_like_narrated_tool_call), :617-661 (where both guards are applied to the final-response path) — the existing guard pattern the new read-then-write guard should follow
  • packages/agent/src/local_agent/agent_loop.rs:597 — the zero-tool-calls turn-termination point where the new guard's check belongs
  • packages/nodespace-types/src/schema.rs:29-38 — SchemaField, #[serde(rename_all = "camelCase")], confirming coreValues is the only accepted wire key
  • packages/core/src/schema/mod.rs:139-164 — CreateSchemaParams, no deny_unknown_fields, confirming the silent-drop mechanism
  • packages/core/src/behaviors/mod.rs:1306-1316 (validate_schema_field, empty-enum rejection) and :1345-1365 (validate_template_tokens, placeholder validation) — the real, already-correct, already-tested structural validation that the core_values bug bypasses by never reaching it with populated data
  • packages/agent/src/local_agent/tools.rs:597-668 (def_create_schema) and :670-748 (def_update_schema) — tool schema description text, already correctly stating coreValues and the title_template placeholder rule; cited as evidence against relocating parameter mechanics here wholesale
  • scripts/eval/fixtures/agent-matrix.ts and ../nodespace-docs/development/agent-eval.md — eval harness and its documented noise floor (3 identical runs: 10/12, 9/12, 8/12)

Architecture Impact

  • ADR-038 (Accepted) — "routing must be retrieval, not enumeration." This issue does not touch routing and does not reverse or extend this ADR. It narrows Spike: re-evaluate the agent prompting architecture from first principles #1802's claim that resident-guidance token cost is the same anti-pattern ADR-038 addresses one layer up: the evidence here shows the measured regression is a missing structural guard, not resident-prompt token cost, so no companion ADR extension is proposed for that argument. No nodespace-docs change needed on this point.
  • ADR-030 (Superseded by ADR-038 for routing; node model retained) — unaffected. This issue keeps the existing prompt/skill node model and resident/retrieved split; it changes what generates one resident string's content, not the model.
  • ADR-056 (Accepted, locks gemma-4-e4b-q4km) — this issue's Scenario 6 analysis (search-then-no-write) is the same failure ADR-056 already documented as "still open" for a different proximate cause (wrong type reference / silent resolve_query degradation). This issue's structural guard (item 3) is a backstop for that failure class generally, not specific to the resolve_query type-lookup bug ADR-056 describes — both can and should be fixed; neither supersedes the other. No ADR change needed; this issue is implementation work under the existing decision.
  • No companion nodespace-docs change is required for this issue. prompt-skill-system.md's description of the resident/retrieved/schema layering already matches the target state this issue moves toward (item 2 makes SCHEMA_CREATION_RULES conform to the single-source-of-truth pattern the doc already describes for skill_rules.rs-derived content; it does not change the documented architecture).

Related Issues

Activity

  1. malibio commented on Jul 29, 2026

    @malibio
    CollaboratorAuthor

    Relationship to #1802 corrected: this is input to the spike, not a supersession

    This issue was filed by a background agent that outlived the review it belonged to, after the user had already decided #1802 should become a spike to re-evaluate the prompting architecture from first principles in a fresh session. The "supersedes #1802's diagnosis" framing in the Overview predates that decision and should be read as evidence feeding the spike, not as a replacement for it.

    Concretely:

    What was independently verified from this issue's claims

    • gen_skill_md.rs:112 implements --check, with checked_in_skill_md_generated_block_is_up_to_date (line 154) enforcing it — a real single-source-of-truth mechanism exists between skill_rules.rs and SKILL.md
    • agent_loop.rs carries genuine structural guards: contains_action_claim (:165), looks_like_narrated_tool_call (:258), the duplicate-call breaker, and the cross-turn duplicate-write guard

    Both check out. This is a material correction to #1802's earlier framing, which characterized behavioral invariants as prose-enforced generally. The accurate statement is that the codebase has an established pattern for making invariants structural, and read-then-write turn completion is the notable member of that class which has not adopted it.

    Correction carried back to #1802

    This issue's pushback on tool-schema descriptions is correct and matters: the tool schemas already state coreValues correctly while the resident prose has the typo. That is evidence the prose string is unreliable, not that schemas are inherently better attended by an ~8B model. #1802 already records the tool-schema-attention assumption as unproven; this sharpens why.

    What stays open for the spike rather than being settled here

    • Whether resident-vs-retrieved layering needs restructuring for registry scaling — this issue explicitly declines to recommend it, and that remains an open question rather than a closed one
    • Whether the stop-after-search cause is recency displacement or turn-budget economics — still two unfalsified mechanisms
    • Skill retrieval quality (Organization Guidance returned at 0.74–0.87 confidence for schema-creation queries)
    • The measurement design needed to detect a 2-scenario effect
  2. malibio commented on Jul 29, 2026

    @malibio
    CollaboratorAuthor

    Closing — content folded into #1802, which is the spike where this investigation lives. Filed by a background agent that outlived its parent review after #1802 had already been converted to a spike; keeping both would have split one investigation across two issues with competing framing.

    All findings survive in #1802: the existing structural guards in agent_loop.rs, the skill_rules.rs single-source-of-truth mechanism, the core_values → coreValues fix, the render-from-skill_rules.rs remedy, the read-then-write guard proposal, and the ≥2-runs-per-variant measurement bar. Nothing was dropped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions