Repository navigation
Conversation
A session wrote one blob file per tool result, command output, and compaction record, so a long session held tens of thousands of small files, and each body cost two syncs. A body of at most 4 KiB that fx reads back through the session now goes into `blobs.pack`, one append-only file per session, synced once per batch before any line that names it. Larger bodies, and bodies a tool or the model opens by path, stay blob files. A line that names a packed body is written at format version 2, so an older fx opens such a session read-only. A writable open cuts only a torn tail that no durable line names; damage in the middle leaves the pack as it is and is reported. Forks copy the pack. D102 records the rules and amends D44 and D48, and `tests/tla/session_store/SmallBlobs.tla` checks that no line names an unsynced record.
Once a session has a pack, every line it writes is format version 2, snapshots included, so an older fx that checks only the newest lines opens the session read-only and appends nothing. A writable open cuts a damaged pack tail only when no durable line names a body there, and it takes names only from the lines' parsed blob lists, read in chunks, so a 64-character hex string in a prompt no longer blocks a resume. The cut and a refusal are reported as diagnostics. Forks check every reference against one pack index instead of walking the pack per reference. Command replays stay blob files, since their readers open them by path, and the testing store keeps small bodies out of the path API so that rule is tested. pack.zig's own tests and fuzz target now compile. The wasm core, which has no session folder, builds without packs.
A fork, a recover fork, and a child copy now decide from the source folder that the session has a pack before they write their first line, and a resume decides it from its own folder. So every line those sessions write is version 2, and an older fx opens them read-only.
The two subagent commit-failure tests broke the blobs folder to fail the child's commit. Small results now go to the session's pack, so the commit succeeded and the fault never fired. Each fault now also blocks the pack's creation, and the child's earlier output is over the pack limit, so the failing commit is the first packed write.
This branch has not been deployed
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.
Summary
A session that holds many small bodies, such as tool results, command outputs, and compaction records, used to write one blob file per body. A long session could hold tens of thousands of files, and each body cost two syncs. Small bodies now go into one pack file per session,
blobs.pack. The pack is appended with one sync per batch.Larger bodies, and bodies that a tool or the model opens by path, stay blob files. The decision is recorded as D102 in
tests/tla/session_manager/system-design.md, which amends D44 and D48.Why
On a synthetic session that matches a long real one (36,000 small bodies and 6,194 bodies of 18 KiB), the pack writes faster, reads at least as fast, and uses less disk:
du)Two runs per variant. The pack's open cost is its index build, 69 to 84 ms for 36,000 records, paid once per session on the first lock-free read. The index is estimated at about 3 MB for that many records; it was not measured.
Copying a 160 MB pack for a fork took 94 to 109 ms in the sandbox with a warm cache.
Safety rules
blobslist through the log's chunked reader. Free text, such as a 64-character hex string in a prompt, is never a name.pack_refused, with the record's offset. A cut is reported aspack_cut, with the bytes cut.Builds without packs
The wasm core (
fx-core.wasm) builds the session manager without the pack. One switch decides it:pack.enabled, set from thesession_packsbuild option, whichbuild.zigsets to false for wasm targets. In a build without packs, a small body is a blob file, and no code reads or writes a pack.The reason is that the wasm core has no session folder. Its ACP server does not open the native session store on wasm (
src/acp/server.zig, the store is opened only when!host_target.is_wasm), and its sessions go through the JS host's store (js_host_session_store). Its WASI shim has no preopened folder (sdk/wasm-runtime.js:fd_prestat_getreturns 8, andpath_openandfd_preadare unavailable). So the pack is unreachable there at runtime.The compile check confirms the compiler drops the pack's code. A temporary
@compileErrorat the file-storage entry points (openRoot,createFile,openFile) fails the wasm build, which shows that the storage code is in the wasm graph. The same probe at the pack's entry points (walk,readBody,appendRecord,copyInto) does not fail, which shows that the pack code is not. Both edits were reverted.Measured with
zig build fx-core-wasm -Dwasm-surface=core -Doptimize=ReleaseSmall, thennode sdk/scripts/optimize-wasm.mjs:origin/mainsdk/tests/package-size-budgets.json)The optimized file is 1,244 bytes above
origin/mainand 2,537 bytes under the budget. The budget is not raised in this change.A test,
a non-pack build writes files: a small body is a blob file, and no pack is made, runs in its own test binary built with the switch off. Thetest-session-managerstep runs that binary. In the packed build, the test skips.Older-reader behavior
Once a session has a pack, every line it writes is at format version 2, snapshots included. A reader built before this change stops at the first version 2 line and opens the session read-only. It never rewrites the session.
New session folders that hold a pack also write version 2: forks, recover forks, child copies, and resumed sessions. A fork or copy's first line is version 2, and from the first line that names a packed body on, every line is version 2. History copied from the source before its pack keeps the version it was written at. The tests are
a fork of a pack session writes version 2 on every later line, snapshots included,a recover fork of a pack session writes version 2 on every later line,a child copy of a pack child writes version 2 on every later line, anda resumed session whose folder holds a pack writes version 2 on every later line.Checked by hand against
origin/mainbuilt fromgit archive. The session was written by this branch's build, with one packed tool result, 18 lines of which 9 are version 2. Themainbuild resumed it withask --json --auto --resume <id>:"error":"UnsupportedSessionFormat";The unit test
an older reader stops at the first line naming a packed body, and leaves the session unwrittencovers the same rule.Refusals and where the cause shows
A named damaged record refuses a writable resume. The user sees the existing protocol code
InvalidSessionFormat, and the cause is not in the error text.pack_refused, with the record's byte offset. The adapter forwards it to the debug trace asevent=sessions_v2_diagnostic kind=pack_refused session=<id> offset=<n>in thesessionscope. The trace is written only when debug tracing is on, so a user does not see it by default.fx session recover <id>is not printed anywhere yet. It is the step this message should name.Tests
session.zig,pack.zig): packed and file bodies at the 4 KiB boundary; read-back by name; the version 2 rule for every line after the first pack, snapshots included; the crash before a line; a torn tail, cut and reported; an unnamed damaged last record, cut; a named damaged last record, read-only and reported; a body flip in the middle, later bodies still read and nothing cut; a header flip in the middle, read-only; a fork copy, with an append on either side never reaching the other; a fork search that walks the pack once; a hex string in a line's text is not a name; a rebuild on a strict miss, at most once per pack length; a non-pack build writes files.session_pack_records, with three saved inputs.sessions.test.ts): a tool result is packed, and a restarted fx reads it back. A short command output is paged by the model after a restart. This is a plain restart check: the shell's capture spools, so it does not reach the inline path, and the unit test above is the guard for that rule. A half-written record is cut on resume, and the packed body still reads. A moved side folder keeps its copy as a blob file, and the moved map is packed. A reader process runsfx sessionwhile a writer is held in a turn.file-tool-paths.test.ts): a corrupted stored tool image degrades to an explicit notice on resume. The damage is in the image's packed record, with a later packed result after it, so the record is mid-pack and the session stays writable.compaction-policy,gateway-stream-lifecycle): the compaction helper reads a record from its file or from the pack.pack.zig, which the session-manager test root now imports. See the next section for the failure this fixed.Fuzzing
The fuzz target
session_pack_recordsnever ran in CI. Its tests lived inpack.zig, which the session-manager test root did not import, so the fuzz build found no test and failed withbuild-error.api.zignow importspack.zigin its test block. The target builds, runs its replay, and the nightly driver passes locally (python3 -m scripts.fuzz_nightly run --target session_pack_records --budget-seconds 30: build ok, round 1 ok). The driver's unit tests pass:python3 -m unittest scripts.tests.test_fuzz_nightly, 21 tests, OK.Thread Sanitizer
zig build test-session-manager -Dsanitize-thread=true, withTSAN_OPTIONS=halt_on_error=0:abort_on_error=0, run in the sandbox. Two test binaries are linked with ThreadSanitizer, and the run reports zeroWARNING: ThreadSanitizerlines. The run's summary is the same as the normal run: 320 of 334 pass, and the same eight baseline failures. That run was before the wasm switch.Model
tests/tla/session_store/SmallBlobs.tlamodels the pack's ordering and crash rules.design-pack.cfgchecks the design: no line names an unsynced record (Danglingis never violated).mutant-early-ref.cfgplants a line that names a record before its sync, and the check requires thatDanglingis violated.check.shruns both, and the README has its row.The model does not cover forks, the index, the damage rules, or the wasm switch. The unit tests cover those.
Checks run
On the branch after the wasm switch:
zig build test-session-manager --summary all: 329 pass, 8 skip, 8 fail. The eight failures are the baseline (api D29, D39 and D49; storage read-only; in both build modes). The skips include the pack-only test in the packed binaries.sessions.test.ts,file-tool-paths.test.tsandcompaction-policy.test.ts: 157 pass, 32 skip (tmux), 0 fail.zig build fuzz -Dfuzz-target=session_pack_records: exit 0.zig fmt --check src/ build.zig: exit 0.scripts/check-public-surface.sh: exit 0.fx-core.wasmoptimized: 2,462,463 bytes, under the 2,465,000 budget.The full
zig build testlast ran before the wasm switch: 12,888 pass, 33 skip, and the eight baseline failures in both build modes. It was not rerun after the switch. The SDK package-size test (sdk/tests/test-package-size.mjs) was not run: it reads a packedlibfxtarball with a native addon for every platform, which this sandbox cannot build. Its wasm figure is the one measured above, from the same optimize step.Known limits
clonefileis a follow-up.blobsentry as a name. A damaged record that a line names refuses a writable open; it never loses data.InvalidSessionFormat. The cause shows only in the debug-trace diagnostic, and the recovery step is not printed. A clearer message needs a new public error code, which is a follow-up with the maintainer.tests/tla/session_store/. Whichever of the two merges second lists both models in onecheck.shand one README row.Docs
No fx.sh page describes the session folder layout or blob files, so the manual needs no change.