Skip to content

feat(tools): one bounding boundary, ToolOutput split, masking, per-turn budget (T1) - #31

Merged
lg2m merged 4 commits into
mainfrom
feat/tool-layer-t1-output-bounds
Sep 12, 2026
Merged

lg2m merged 4 commits into
mainfrom
feat/tool-layer-t1-output-bounds

Conversation

@lg2m

@lg2m lg2m commented Sep 12, 2026

Copy link
Copy Markdown
Collaborator

T1: one bounding boundary, ToolOutput split, masking, per-turn budget

Spec: docs/plans/tool-layer.md#d1--cross-cutting-primitives-t1 Linear: ENG-771

What changed

User-visible

  • Shell results start with shell exit=<code> elapsed=<s> bytes=<n> and the model sees a 16 KiB head+tail (was 128 KiB tail-cut). The 128 KiB capture and live ToolCallOutputDelta stream are unchanged. On a cat of a 1 MiB file the model-facing text drops ~8×.
  • Every truncated result carries exactly one marker naming what was cut: …[qq: 41,207 bytes / 1,142 lines omitted; not stored]…. Lines over 2000 bytes are clipped with …+N. Head and tail are cut at line boundaries so no partial line ever reaches the model.
  • Secrets (AWS keys, GitHub tokens, sk-/xox/Bearer, KEY=value credentials, user:pass@host) are masked as [masked:<kind>] in model text. File hashes are unaffected.
  • A turn's tool output is capped at 96 KiB in context; a late result is re-bounded to the remainder with turn budget reached in its marker. Persisted rows keep each call's own bounded text.
  • Edit/write diffs are UI payload only (ToolCallDisplay::Diff), never sent back to the model. Pruning stubs keep a result's header line.
  • TUI reads the shell verdict from the header and detects markers with one …[qq: prefix check.

Design

  • crates/qq-core/src/tools/output.rs (new): Bounds with named ceilings (128 KiB / 4000 lines / 2000-byte lines / 4 KiB floor), bound_text, Header, mask_secrets, TurnOutputBudget, MARKER_PREFIX.
  • ToolExecutionResult → ToolOutput { model_text, is_error, ui_payload, file_state }. Constructors mask then bound once; verbatim_error for constant runtime messages. Per-tool truncation removed from read/list/search; truncate_result and its 256 KiB tail cut are gone.
  • Masking is a hand-rolled byte matcher with a first-byte table and digraph prefilter — no regex dependency, no root request. Clean text returns without allocation.
  • Escape sizing is a 256-entry table; exceeds short-circuits on length; omitted-line counting uses the vectorized matches.

How verified

  • cargo test --workspace: 1283 passed / 0 failed (qq-core 481, qq-tui 234, qq 109, …). Two headless tests with 1–2 s wall-clock timeouts flake only under full-workspace CPU contention and pass alone and in cargo test -p qq.
  • cargo fmt --all -- --check ✓, cargo clippy --workspace --all-targets --all-features -- -D warnings ✓, cargo build --workspace ✓. qq-provider untouched.
  • 26 tests added: tools::output::* (21: ceilings/floors, head+tail at line boundaries, single marker, thousands grouping, note, line-count bound, …+N clipping incl. char boundaries and escape-dense lines, multibyte tail, determinism, header grammar, each masking kind, masking exemptions, no-alloc on clean text, turn budget floor, 1 MiB one-line shell capture), edits_carry_their_diff_as_a_ui_payload_never_as_model_text, secrets_in_results_are_masked_at_the_boundary, a_turns_tool_output_is_capped_and_persisted_results_stay_whole, pruning_stubs_keep_a_result_header_line, TUI collapsed_summaries_curate_known_tools (shell header case).
  • cargo run --release -p xtask -- perf r4-worker --case shell passes on the new contract (completion 93 ms, 16 payload txns).

Measurements

Gate Before After Budget
tool_dispatch read_tool_loop (A/B, 15 interleaved pairs, median) 51.5 µs 51.4 µs no regression
same-binary A/A control (median) 49.9 µs 48.4 µs —
tool_output fits_no_op_8k — 2.0 µs ≤ 5 µs
tool_output shell_16k_from_128k — 21 µs ≤ 100 µs
tool_output default_128k_from_1m — 122 µs —
tool_output mask_source_128k / x_filled worst case — 35 µs / 101 µs ≤ 200 µs

Loaded host; A/B delta is inside the A/A spread. Evidence: target/qq-perf/t1-2026-09-11/ (untracked).

Docs

  • docs/design/tools.md: § Output Bounding (new), § Built-In Tools, § Shell Execution, § Context Budget.
  • docs/plans/progress/tool-layer.md: T1 → In review, receipt appended.

Compatibility

  • PROTOCOL / DESCRIPTOR / schema: unchanged. ui_payload reuses the existing ToolCallSnapshot.display / display_json. Marker and shell header text changed; nothing parses the old ...[truncated by qq] or exit code: N outside the TUI and the xtask fixture, both updated.
  • Model-visible: shell text shape changed (header first, tail last, no exit code: trailer). Transcripts persisted before this change replay as-is.

Follow-ups / decisions needed

  • T4 replaces not stored with a spill handle; ToolOutput has no spill field until then.
  • AGENTS.md names Linear team DEV; the workspace's team is ENG. Issue filed under ENG / project qq.
  • list_dir/search TUI metrics still count body lines until T2 gives them headers.

…imitives

T1 of docs/plans/tool-layer.md. New `tools/output.rs` with `Bounds`
(ceilings 128 KiB / 4000 lines / 2000-byte lines), `bound_text` head+tail
line-boundary bounding with one lossless-accounting marker, the header
grammar helper, allocation-free secret masking, and the 96 KiB per-turn
output budget. Not yet wired into dispatch.
T1: `ToolExecutionResult` becomes `ToolOutput { model_text, ui_payload,
file_state }`. Constructors mask secrets then apply `Bounds` once; the
per-tool truncation in read/list/search and the 256 KiB tail-cut are gone.
Edit/write diffs move to `ui_payload` inside dispatch. Shell gets a
`shell exit= elapsed= bytes=` header and a 16 KiB head+tail model bound
while the 128 KiB capture and live stream cap stay. The turn loop applies
the 96 KiB per-turn output budget to what enters context; persisted rows
keep each call's own bounded text. Pruning stubs keep a header line.
Adds the `tool_output` bench (fits no-op, 16 KiB shell from 128 KiB,
128 KiB from 1 MiB, one huge line, masking over x-filled and source-like
text). Escape sizing is a 256-entry table, `exceeds` short-circuits on
length, line counting uses the vectorized `matches`, and the mask scan
skips to trigger bytes with `position`. TUI reads the shell verdict from
the header and treats `…[qq: ` lines as markers; the R4 shell fixture
asserts the new contract.
Design: § Output Bounding (ToolOutput split, Bounds, marker grammar,
headers, masking, per-turn budget); built-in and shell sections updated.
Ledger: T1 to In review with the receipt.
@lg2m
lg2m merged commit b0a18be into main Sep 12, 2026
4 checks passed
@lg2m
lg2m deleted the feat/tool-layer-t1-output-bounds branch September 12, 2026 07:31
@lg2m lg2m mentioned this pull request Sep 14, 2026
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