Repository navigation
fix(server): byte-budget snapshot assembly under the wire cap (F11) - #90
Merged
lg2m merged 1 commit intoSep 21, 2026
Conversation
`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.
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-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_snapshotbounded session bodies by row count only (session_limit,message_limit, 4 096 tool calls) while the transport refuses responses overMAX_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 onmainby the new test (fails atsnapshot 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 existingescaped_len; the response is never serialized twice.A budget cut sets the existing
has_older_messages/has_older_tool_callsflags 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 DESCalongsider.created_at_ms DESCso 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 --workspaceon the stacked tip (same pre-existing load flakes as #89, green in isolation).docs/design/protocol.mdsnapshot 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.