Skip to content

fix(server): byte-budget snapshot assembly under the wire cap (F11) - #90

Merged
lg2m merged 1 commit into
fix/eng-804-f23-tool-result-projectionfrom
fix/eng-796-f11-snapshot-byte-budget
Sep 21, 2026
Merged

lg2m merged 1 commit into
fix/eng-804-f23-tool-result-projectionfrom
fix/eng-796-f11-snapshot-byte-budget

Conversation

@lg2m

@lg2m lg2m commented Sep 21, 2026

Copy link
Copy Markdown
Collaborator

Linear: ENG-796. Audit finding F11 (P1), work-order 2. Stack 2/3 — based on #89 (F23); merge that first, then retarget this to main. Only the ledger file overlaps.

Bug

load_snapshot bounded session bodies by row count only (session_limit, message_limit, 4 096 tool calls) while the transport refuses responses over MAX_SNAPSHOT_BYTES (8 MiB). A session with 40 retained 300 KiB tool results produced a 12.3 MiB body, so the TUI could never attach to or resnapshot it. Legal content made the workspace unreachable. Reproduced on main by the new test (fails at snapshot is 12325038 bytes, cap 8388608).

Fix

One SnapshotBudget (6 MiB of JSON-escaped text + 512 B per-row charge) spans the focused body first, then included bodies in request order. Within a body, messages and tool calls are charged newest-first, so a cut drops the oldest rows and the client keeps a contiguous newest tail. Size is accounted from row text with the existing escaped_len; the response is never serialized twice.

A budget cut sets the existing has_older_messages / has_older_tool_calls flags exactly as a count cut does — no wire change, no new endpoint. The TUI's cold-body fetch and tool-result recall already serve older rows. Run rows and summaries are never cut by transcript size.

Small side fix: the tool-call ordering query gained r.rowid DESC alongside r.created_at_ms DESC so same-millisecond runs keep a deterministic newest tail (the message query already did this).

Test

sessions::tests::runs::snapshots_stay_under_the_wire_cap_for_large_sessions: seeds ~12 MiB of retained results; asserts serialized size ≤ cap, the kept tool calls are the newest contiguous runs with the flag set, small sessions are untouched, and an included-only body is budgeted the same way. Assembly of the 12 MiB seed ≈ 1 s.

Verification

fmt, clippy -D warnings, cargo test --workspace on the stacked tip (same pre-existing load flakes as #89, green in isolation). docs/design/protocol.md snapshot section and limits table updated.

Deferred

The TUI does not yet page older rows on demand beyond its existing cold-body fetch; a clipped focused body shows the newest tail with the flag set.

`load_snapshot` bounded session bodies by row count only (`session_limit`,
`message_limit`, 4 096 tool calls) while the transport refuses responses over
`MAX_SNAPSHOT_BYTES` (8 MiB). A session with 40 retained 300 KiB tool results
produced a 12.3 MiB body, so the TUI could never attach to or resnapshot a
large session — legal content made the workspace unreachable.

Rows are now admitted under one `SnapshotBudget` (6 MiB of JSON-escaped text
plus a 512-byte per-row charge for ids, ordinals, and punctuation) that spans
the focused body first and then the included bodies in request order. Within
a body, messages and tool calls are charged newest-first, so a cut drops the
oldest rows and the client keeps a contiguous newest tail. A budget cut sets
the existing `has_older_messages` / `has_older_tool_calls` flags exactly as a
count cut does, so there is no wire change and no new endpoint: the TUI's
cold-body fetch and tool-result recall already serve older rows. Run rows and
summaries are charged at the row overhead only and are never cut by
transcript size. The size is accounted from row text with `escaped_len`; the
response is never serialized twice.

The tool-call ordering query gained `r.rowid DESC` alongside
`r.created_at_ms DESC` so runs created in the same millisecond keep a
deterministic newest tail, matching the message query.

Test: `snapshots_stay_under_the_wire_cap_for_large_sessions` seeds ~12 MiB of
retained results, asserts the serialized snapshot is under the cap, that the
kept tool calls are the newest contiguous runs with the flag set, that small
sessions are untouched, and that an included-only body is budgeted the same
way. Assembly of the 12 MiB seed takes about 1 s; small snapshots are
unaffected beyond one byte count per row.

Closes ENG-796.
@lg2m
lg2m added this pull request to stack #93 September 21, 2026 02:53
@lg2m
lg2m merged commit 96962c5 into main Sep 21, 2026
4 checks passed
@lg2m
lg2m deleted the fix/eng-796-f11-snapshot-byte-budget 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