Repository navigation
fix(runtime): persist the model-facing tool-result projection for replay (F23) - #89
Merged
Merged
Conversation
…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.
This was referenced Sep 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 intool_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_filecalls in one turn (each inside the tool's own 32 KiB bound, together over 96 KiB). Live request carriedturn budget reachedcuts; the follow-up request carried the full rows. Fails onmainat the follow-up-equality assertion.Fix: reconstruct, don't persist
TurnOutputBudget::admitis 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_turnsapplies 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 addedLEFT JOINin 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
ToolCallFinishedsemantics).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>), andread_tool_spillfalls back totool_calls.resultwhen no spill row matches (digest must agree; session scoping preserved).ResultRecallnames 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.sessions/tests.rsapplies the same projection.Verification
fmt, clippy
-D warnings,cargo test --workspace(29 binaries;headless::tests::*and oneruntime::testscase flake under full-workspace parallel load and pass in isolation — pre-existing).context_assemblybench: 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.