Skip to content

create_schema emits corrupted JSON field names under grammar-constrained decoding on retry (scenario 8b regression) #1926

Closed

Description

@malibio

Summary

Scenario 8b (second create_schema call in a chat that already contains one completed schema-creation turn) regressed from 3/3 to 0/3 after the #1925 history-compaction fix (94e4aa2c). Root-caused directly against the daemon log from the actual 3-rep matrix gate — not speculation.

Root cause (confirmed via daemon.log, not inferred)

The model's grammar-constrained JSON generation for the create_schema tool call corrupts the "type" key inside a fields entry with literal tokenizer-boundary artifacts:

args_preview={"fields":[{"<|"|>type<|"|>":"number","name":"replacement_cost"}],"name":"Venue Booking Tracker"}
result_preview={"error":"invalid params: fields[0] (\"replacement_cost\") is missing \"type\"...

This happens on the first create_schema call in the turn (not a duplicate-call issue per se). The tool correctly rejects the malformed args. The model retries with a second field a moment later (booking_date/date this time), which fails the same way (<|"|>type<|"|> again). The harness's own duplicate-tool-call-loop breaker then fires and forces a final response, so the turn ends with two failed create_schema calls and no successful one.

Tool executed tool=create_schema is_error=true ... args_preview={"fields":[{"<|"|>type<|"|>":"number","name":"replacement_cost"}],"name":"Venue Booking Tracker"}
Tool executed tool=create_schema is_error=true ... args_preview={"fields":[{"<|"|>type<|"|>":"date","name":"booking_date"}],"name":"Venue Booking Tracker"}
WARN Duplicate tool-call loop detected — breaking to force final response

This is a grammar/decoding-level artifact in the model's tool-call JSON generation, not a semantic misunderstanding of conversation history. Confirmed absent in both pre-compaction baseline matrix runs (matrix-1917-descfix, matrix-1917-filtertype, 6/6 clean single-call passes) — reproducible in all 3 reps of the post-compaction run, always on this exact turn (8b, the second create_schema call in a chat, following a longer/more-loaded prompt than 8a's).

Why this is plausibly connected to the compaction fix, without being a semantic issue

94e4aa2c changes prior-turn history rendering from narrative prose to terse "Fact: ..." lines for turns with completed writes. 8b's turn is the first scored turn after 8a's schema-creation turn, so its prompt now includes a terse Fact: a schema with id 'album_to_listen_to' ('Album To Listen To') was created, with fields listening_order (number). line where before it had the model's own narrative reply. This does not look like a comprehension failure (the model still identifies the right tool and mostly-correct args) — more likely the changed prompt shape shifts token boundaries in a way that interacts badly with the grammar-constrained decoder specifically around the "type" key of a fields array entry. Not confirmed to that level of mechanism; flagging as the most likely proximate cause for whoever picks this up.

Not the same issue as scenario 6

Investigated and ruled out a shared root cause with scenario 6 (which an earlier handoff speculated about). Scenario 6's failure mode is unchanged pre- and post-compaction across every run inspected: toolsCalled: [], reply "I need the ID of the laser cutter you are referring to..." — same clarifying-question-without-tool-call behavior as main@fdc1a256's baseline. The earlier claim of "zero tool calls, a different and arguably worse failure signature" was based on misreading the eval harness's rawOutput debug field (which happened to scrape empty on this run due to a harness log-format mismatch, unrelated to the model's actual behavior) rather than the reply field, which was consistent throughout. No new investigation needed there.

Suggested next step

Not a naive retry-suppression fix — the underlying JSON corruption should be understood first (is fields array + type key specifically prone to this under the current grammar? does it recur outside create_schema?). Needs its own investigation with the same rigor as the rest of this cluster (real trace evidence, not speculation) before attempting a fix.

Related: #1925 (the compaction fix that surfaced this), #1917 (standing orchestrator brief for the agent-matrix reliability effort).

Activity

  1. malibio commented on Aug 3, 2026

    @malibio
    CollaboratorAuthor

    Frontend: 171 test files, 4108 tests passed

  2. self-assigned this
    on Aug 3, 2026
  3. malibio commented on Aug 3, 2026

    @malibio
    CollaboratorAuthor

    Root cause confirmed, fix implemented and matrix-gated at 10.0/12 (10, 10, 10)

    What actually caused it (corrected from the issue's own working theory)

    The issue's original write-up interpreted {"<|"|>type<|"|>":"number",...} as invalid JSON that the tool correctly rejected. It's actually syntactically valid JSON — <|"|> is literal text the model emitted (a leaked Gemma 4 special "quote token"), producing a bizarre-but-parseable key <|"|>type<|"|> instead of type. The tool's rejection ("missing "type"") is a downstream symptom, not a JSON parse failure.

    First attempt, and why it didn't work (documented for anyone hitting this pattern again)

    The obvious fix — the codebase already has strip_gemma_special_tokens for exactly this token, applied to plain-text content — is to apply the same stripping to tool-call arguments at the streaming-delta layer in nlp-engine (emit_oai_delta). Implemented this, wrote unit tests reproducing the exact corrupted string from this issue's daemon-log evidence, confirmed the tests pass and correctly catch the regression when reverted.

    Live-tested against the real model and it did NOT fix the bug — the exact same corruption still appeared in args_preview on a freshly rebuilt binary. Root cause of the miss: <|"|> can straddle a delta boundary (one streamed chunk ending <|, the next starting "|>), and a per-delta substring strip can't match a token split across two separate deltas. Content already has boundary-carry handling for this exact reason (ChannelSplitter/partial_marker_suffix_len); tool-call arguments never did.

    The actual fix

    Reverted the nlp-engine change. Instead, added repair_leaked_special_token_keys in agent_loop.rs, a sibling to the existing repair_over_quoted_keys (#1876's fix for a related-but-distinct key-corruption shape). This operates on the fully accumulated, already-parsed serde_json::Value — immune to delta boundaries by construction, since the corrupted key already exists as one complete string by the time this runs. Wired in alongside repair_over_quoted_keys at both call sites: the real execution path and canonical_args (so a malformed call and its repaired retry share one duplicate-guard identity, same rationale as the existing repair).

    4 new unit tests (recursion at every depth, collision/empty-key guards, values never rewritten, duplicate-guard identity) — all pass, and verified to actually catch the regression by temporarily removing the call-site wiring.

    Live validation

    Branch: worktree-issue-1926-schema-json-corruption. Not yet committed/pushed/PR'd — holding for direction on next steps.

  4. malibio commented on Aug 3, 2026

    @malibio
    CollaboratorAuthor

    Merge — this is exactly the standard this cluster has been built on

    Excellent work: corrected the issue's own root-cause theory (leaked special token producing a valid-but-corrupted key, not a JSON parse failure), caught a real live-testing failure the unit tests alone would have missed (delta-boundary splitting defeating a streaming-layer fix), and landed a structurally sound repair at the accumulated-JSON layer instead — correctly recognized as a sibling to #1876's existing repair pattern rather than a new mechanism, with the duplicate-guard identity concern handled consistently.

    10.0/12, fully deterministic, matching this cluster's best-ever result, with zero regressions and 8b now solid.

    Proceed: commit, push, open the PR properly (worktree → PR → /pragmatic-code-review), merge once approved. Same flow as #1909.

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions