Skip to content

fix(runtime): persist the model-facing tool-result projection for replay (F23) - #89

Merged
lg2m merged 1 commit into
mainfrom
fix/eng-804-f23-tool-result-projection
Sep 21, 2026
Merged

lg2m merged 1 commit into
mainfrom
fix/eng-804-f23-tool-result-projection

Conversation

@lg2m

@lg2m lg2m commented Sep 21, 2026

Copy link
Copy Markdown
Collaborator

Linear: ENG-804. Audit finding F23, work-order 2. Stack 1/3 — next: F11 (snapshot byte budget), then F03/F20 docs.

Bug

The 96 KiB per-turn tool-output budget (TurnOutputBudget) re-bounded late results only when the live run built the turn's result message — after every per-call result had been persisted whole in tool_calls.result. Context assembly replayed the larger rows, so a follow-up turn, a reopened store, and the compaction summarizer all saw different bytes than the model saw live.

Reproduced by a session test: four ~30 KiB read_file calls in one turn (each inside the tool's own 32 KiB bound, together over 96 KiB). Live request carried turn budget reached cuts; the follow-up request carried the full rows. Fails on main at the follow-up-equality assertion.

Fix: reconstruct, don't persist

TurnOutputBudget::admit is deterministic over its input, so it is now the one projection both paths share: the live run applies it as results enter context; append_run_turns applies it to stored rows in block order (= call order). Assembly gets the two extra inputs it needs (tool_calls.id, tool_spills.digest) via one added LEFT JOIN in the existing query. No schema bump, nothing new written.

Rejected alternatives (in commit body): persisting the projection (duplicates up to 96 KiB/turn and can drift); making projected text the authoritative row (a row's text would depend on sibling results in the turn; changes capacity accounting and ToolCallFinished semantics).

Recall path: a budget cut of an unspilled result used to say not stored. It now names the stored row through the same handle grammar (t:<tool>:<call8>:<digest8>), and read_tool_spill falls back to tool_calls.result when no spill row matches (digest must agree; session scoping preserved). ResultRecall names the three cases at both call sites.

Tests

  • sessions::tests::replay_identity::turn_budget_projection_replays_identically_after_follow_up_and_reopen — live == follow-up == reopened; joined loader == reference oracle; every cut span reads back through its handle.
  • tools::output::tests::a_budget_cut_of_an_unspilled_result_names_the_stored_row.
  • Reference oracle in sessions/tests.rs applies the same projection.

Verification

fmt, clippy -D warnings, cargo test --workspace (29 binaries; headless::tests::* and one runtime::tests case flake under full-workspace parallel load and pass in isolation — pre-existing). context_assembly bench: 41–52 µs before vs 44–55 µs after, within A/A spread; hashing only on the cold cut path.

Per workflow.md §4, sessions/ changes want a second reviewer.

Deferred

JEV checkpoint annotations appended to retained results are still live-only; out of F23's scope, noted in the ledger.

…lay (F23)

Bug (ENG-804, audit F23): the 96 KiB per-turn tool-output budget
(`TurnOutputBudget`) re-bounded late results only when the live run built
the turn's result message, after every per-call result had already been
persisted whole in `tool_calls.result`. Context assembly replayed those
larger rows, so a follow-up turn, a reopened store, and the compaction
summarizer all saw a different context than the model saw live. A session
test with four ~30 KiB `read_file` calls in one turn (each inside the tool's
own 32 KiB bound, together over 96 KiB) reproduced it: the live request
carried `turn budget reached` cuts, the follow-up request carried the full
rows.

Design: reconstruct, do not persist. `TurnOutputBudget::admit` is already
deterministic over its input, so it becomes the single projection that both
paths share: the live run applies it as results enter context, and
`append_run_turns` applies it to the stored rows in block order (block order
is call order). Nothing new is written and there is no schema bump; the
projection is a pure function of `result`, the call id, and the spill digest,
which assembly now reads with one extra `LEFT JOIN tool_spills`. Persisting
the projection was rejected because it would duplicate up to 96 KiB per turn
and could drift from the rows it derives from; making the projected text the
authoritative row was rejected because the budget depends on sibling results
in the same turn, so a row's model-facing text would no longer be a property
of the call alone (and the same-bound capacity accounting and the
`ToolCallFinished` event would change meaning).

Recall path: a budget cut of a call that did not itself spill used to say
`not stored`. It now names the stored result row through the same handle
grammar (`t:<tool>:<call8>:<digest8>`, digest of the stored result text,
with `read_tool_result offset=`), and `read_tool_spill` falls back to
`tool_calls.result` when no spill row matches. `ResultRecall` names the
three cases (`None`, `Spill`, `StoredResult`) at both call sites; the
handle grammar lives in one `result_handle`.

Tests:
- `sessions::tests::replay_identity::turn_budget_projection_replays_identically_after_follow_up_and_reopen`:
  live request == follow-up assembly == reopened-store assembly, joined
  loader == reference oracle, and every cut span reads back through the
  handle. Fails on the previous code at the follow-up comparison.
- `tools::output::tests::a_budget_cut_of_an_unspilled_result_names_the_stored_row`:
  handle shape and determinism of the projection.
- The reference oracle in `sessions/tests.rs` applies the same projection;
  `context_capacity` fixture uses a valid tool-call id.

Verification: `cargo fmt --all -- --check`; `cargo clippy --workspace
--all-targets --all-features -- -D warnings`; `cargo test --workspace`
(qq-core 674 passed). The one workspace failure,
`xtask release::tests::workspace_version_is_not_behind_the_newest_release_tag`,
fails identically on the unmodified branch base: `main` cut v0.1.1 after
this branch forked and is unrelated to this change.

Perf: `cargo bench -p qq-core --bench context_assembly` assemble 41–52 µs
before vs 44–55 µs after across two runs each; within the same-binary A/A
spread. The projection only hashes on the cold cut path.

Closes ENG-804.
@lg2m
lg2m added this pull request to stack #93 September 21, 2026 02:53
@lg2m
lg2m merged commit 46b3b4d into main Sep 21, 2026
4 checks passed
@lg2m
lg2m deleted the fix/eng-804-f23-tool-result-projection branch September 21, 2026 02:56
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.

1 participant