Skip to content

feat(tools): spill store and read_tool_result for cut tool outputs (T4) - #36

Merged
lg2m merged 3 commits into
mainfrom
feat/tool-layer-t4-spill-store
Sep 14, 2026
Merged

lg2m merged 3 commits into
mainfrom
feat/tool-layer-t4-spill-store

Conversation

@lg2m

@lg2m lg2m commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

T4: spill store and read_tool_result — a cut result is a pointer, not a loss

Spec: docs/plans/tool-layer.md § D4 Linear: ENG-774 ADR: docs/adr/0019-spill-handles.md Follows: #35 (T3)

Touches sessions/store (schema 27 → 28). Per the plan this slice needs a second-agent review. The review focus is § Store below.

What changed

User-visible

  • When bounding cuts a tool result in a session run, the marker now reads
    …[qq: 41,207 bytes / 1,142 lines omitted; full output t:shell:9f3a2c1d:b7e0d4a2; read_tool_result offset=213]…
    instead of …; not stored]…. Direct runs (qq run without a session) have no store and keep not stored.
  • New tool read_tool_result (session runs only): offset/limit page the stored output line-numbered like read_file; query (+ regex) returns matching lines as L<n>: text. Pages stop on a whole line at 32 KiB and name next=.
  • Explicit reads return exact, unmasked bytes. The model asked for a range of something it already produced; masking there would make .env debugging impossible. The inline preview stays masked (tested both ways in one flow).
  • Handles are session-scoped: another session holding the string reads handle_foreign_session, never data.
  • The per-turn budget re-cut (turn budget reached) points its marker at the same handle.
  • Typed failures: handle_invalid, spill_missing (no row, or digest disagrees), spill_evicted, handle_foreign_session, invalid_regex, invalid_offset, invalid_limit, range_out_of_bounds.
  • Prompt teaches the marker → read_tool_result path; TUI row Recall <handle> L1000-1002 of 1501 lines / 2/3 hits; read_tool_result is prunable and in the config vocabulary.

Store (the part that needs the second reviewer)

  • tool_spills(tool_call_id PK, session_id, run_id, tool, digest, content BLOB, content_bytes, omitted_from_line, created_at_ms, evicted_at_ms) + index on (session_id, created_at_ms). Created by the schema-28 step with a has_table guard; 22 existing migration tests bumped to "28", one new test for 28.
  • finish_tool_call inserts the spill between the result UPDATE and append_event, in the same begin_unit transaction — replay and crash recovery see both or neither. The event envelope does not carry the spill (would blow MAX_PERSISTED_EVENT_BYTES).
  • Eviction: after insert, if SUM(content_bytes) for the session exceeds 64 MiB, the oldest rows of runs NOT IN ('running','preparing') have content set to NULL and evicted_at_ms stamped. Rows are kept so the handle answers spill_evicted, not spill_missing. The running run's own spills are never evicted.
  • read_tool_spill runs on Priority::AwaitControl (same reasoning as search_history): prefix range scan on the PK, digest prefix must agree, session must match.
  • delete_idle_session deletes tool_spills by session_id before tool_calls. qq sessions prune only touches sessions with no runs, which have no spills.

Runtime / boundary

  • ToolOutput::bounded clones the text only when len() > max_bytes || len() > max_lines (a length compare on the common path), before masking, and keeps it as SpillRecord { text, digest, omitted_from_line } when the cut actually happened and the text ≤ 8 MiB.
  • bound_text writes the marker provisionally (not stored) and records omitted_from_line; the runtime's cite_spill finalizes it with the handle before the yield, only when a SpillReader is installed. The digest is of the complete text, so it is known at the boundary; the store trusts it.
  • RuntimeEvent::ToolCallFinished gains spill: Option<SpillRecord>; execution.rs passes it to finish_tool_call.
  • ToolHost::ReadToolResult, StaticFilter.read_tool_result, SessionSpillReader (mirrors SessionHistorySearcher), runtime/spill.rs (spec, handle parser, renderer).
  • MARKER_RESERVE_BYTES 160 → 224 to fit the handle and offset within the bound.

How verified

  • cargo test --workspace: 1363 passed / 0 failed; provider minimal profile 162; fmt and clippy -D warnings clean.
  • 8 focused tests + 1 end-to-end: a_cut_result_names_a_handle_the_model_can_page_and_search_exactly runs a real shell producing 90 KiB, asserts the 16 KiB inline result carries t:shell:<this call's id8>:…, masks AKIA…, and that the next turn's read_tool_result offset=1000 limit=3 returns line 1000 exact and unmasked with L1000-1002/1501 next=1003. Store tests cover same-transaction commit, exact read, digest mismatch → missing, foreign session, 3×30 MiB eviction keeping rows, schema migration, and session deletion emptying the table.
  • Marker tests: finalize idempotence, content lines that look like markers are skipped, no-handle leaves not stored, turn-budget marker carries the handle.

Measurements

Gate Result
tool_dispatch A/B (pinned core, 10 pairs, median) 45.2 → 44.7 µs (neutral)
store_output_batch (fairness gate) 88 → 87 ms/batch, 43 → 42 µs/delta (unchanged)

Evidence: target/qq-perf/t4-2026-09-14/ (untracked).

Docs

  • ADR-0019 (new), docs/adr/README.md, root.md ADR row → written.
  • docs/design/tools.md: § Output Bounding marker updated; § Spilled Outputs (new).
  • Ledger: T3 shipped, T4 in review + receipt.

Compatibility

  • Store schema 27 → 28 (additive table). PROTOCOL / DESCRIPTOR unchanged. Tool declaration hash unchanged for the built-in six (read_tool_result is a session-only static tool like search_history).
  • Model-visible: markers in session runs gain the handle/offset clause. Old transcripts with not stored markers replay as-is.
  • Store size can grow by up to 64 MiB per session (bounded, evicting).

Deviations from the plan (in the ledger)

  • Marker finalized by the runtime, not bound_text, so direct runs stay honest.
  • query mode has no context argument (fixed at 0 as the plan states).
  • 8 MiB item cap enforced at the boundary (not spilled) rather than by the store.
  • search_history does not yet search spills.

Follow-ups

  • T6/T7 may raise the 128 KiB shell capture cap now that the bytes have a home.
  • Client affordance to open a handle from the TUI row.

When bounding cuts a tool result, the complete text (unmasked, up to
8 MiB) now travels with the result to the session store, which writes it
to a new tool_spills table in the same transaction as the tool_calls
row (schema 27 -> 28). The marker changes from `; not stored]…` to
`; full output t:<tool>:<call8>:<digest8>; read_tool_result offset=N]…`
where the call prefix is the tool call id and the digest is the SHA-256
of the stored bytes; direct runs keep no store and their markers still
say not stored. The turn-budget re-cut points its marker at the same
handle.

read_tool_result pages (offset/limit, line-numbered like read_file) or
searches (query, regex) within a stored output. Explicit reads return
exact bytes: the model asked for a range of something it already
produced, and masking there would make .env debugging impossible; the
inline preview stays masked. Handles are session-scoped: another
session reads handle_foreign_session, never data. Typed failures:
handle_invalid, spill_missing, spill_evicted, handle_foreign_session,
invalid_regex, invalid_offset, invalid_limit, range_out_of_bounds.

Bounds: 8 MiB per item, 64 MiB per session with oldest-finished-run
eviction that nulls content but keeps the row so the handle answers
spill_evicted rather than spill_missing. Rows delete with the session.

Declared only for session runs (ToolHost::ReadToolResult, installed by
SessionSpillReader); prunable; TUI row `Recall <handle> L…`.
@lg2m
lg2m merged commit 80e7396 into main Sep 14, 2026
4 checks passed
@lg2m
lg2m deleted the feat/tool-layer-t4-spill-store branch September 14, 2026 18:24
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