Repository navigation
perf(sessions): avoid payload copies during transcript rewrites - #171019
Conversation
Retain independently mutable rewrite snapshots while reusing string values. Preserve native opaque SDK graph semantics and add controlled 50 MiB durable rewrite stall proof.
|
🦞👀 Pull request received. I will update this pull request when review starts. |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Landing under the authorized inherited-failure exception for head CI run 38395776205 passed all selected checks except the baseline ratchet and its aggregate gate. The only underlying failure is the unchanged CLI mock at Independent review is clean. The 50 MiB persistence benchmark reduces median event-loop stall from 134.47 ms to 1.72 ms (worst 217.10 ms to 12.85 ms). No CI rerun or branch update was used to chase green. Full attribution and validation details are in the PR body. |
…claw#171019) * perf(sessions): avoid copying payload strings during transcript rewrites Retain independently mutable rewrite snapshots while reusing string values. Preserve native opaque SDK graph semantics and add controlled 50 MiB durable rewrite stall proof. * test(sessions): make rewrite snapshot proof portable across runtimes
What Problem This Solves
Fixes Gateway event-loop stalls when a transcript rewrite snapshots a loaded session containing large tool results or image payloads.
User Impact
Other sessions remain responsive while a large loaded transcript is prepared and published. Persistence ordering, atomicity, stored bytes, and update behavior are unchanged; no schema or configuration changes.
Why This Change Was Made
Rewrite preparation and publication used
structuredCloneon the complete loaded view before selecting the new entries to persist. This was a local snapshot, not the worker handoff: old payload strings were copied even though only the rewrite delta was sent to the writer.Replace all three rewrite snapshot calls with one scoped container copier. Mutable entries, maps, sets, aliases, and cycles remain isolated; already-owned string values are retained. The public
fromEntries()contract accepts opaque structured-cloneable SDK data, so such graphs retain whole-snapshot native cloning rather than losing cross-object references. No snapshot is cached.Runtime production delta: +72 lines. The growth preserves the existing independently mutable prepared-view contract; sharing mutable entries or freezing extension-owned views would break it. The required stat excluding tests/docs is +159 lines because it also includes an 87-line standalone performance regression. No parallel persistence path was added.
Evidence
Controlled Linux sandbox, Node 26.8.2, synthetic 50 MiB retained payload, five real SQLite rewrite commits per revision. Both revisions verify the durable payload and reopened leaf. Same benchmark command on base
f20ef9a3befdand candidate:The baseline fails the 50 ms stall regression; the candidate passes all five samples. The benchmark uses a
setImmediateheartbeat withperformance.now()and keeps fixture setup outside measurement. Strict latency checks are intentionally in this controlled benchmark, not shared CI timing assertions.pnpm test src/agents/sessions/session-manager-maintenance-async.test.ts src/agents/sessions/session-manager-mutation-boundaries.test.ts --maxWorkers=1: 23 passed, 88.93 s including compiler preparation; maintenance file 28.52 s total / about 20 s test time; sibling file 26.49 s total / about 8 s test time. Covers durable rewrite settlement, stale writers, intervening mutations, opaque graph identity, and prepared/publication isolation.Scope limitation: this removes byte copying for ordinary JSON transcript views. Opaque SDK graphs still use native cloning, and copying unusually large numbers of mutable containers remains proportional to object count. This is synthetic proof, not a production traffic replay.
CI follow-up
Initial run 38394389318 found two validation-only defects: Bun native cloning omits
Error.causein the opaque fixture, and the benchmark needed to assert a successful append ID before constructing its rewrite map. The fixture now proves opaque aliases using an ArrayBuffer and typed-array view. Both fixes received independent review; production code and measured behavior are unchanged.OPENCLAW_VITEST_RUNTIME=bun pnpm test src/agents/sessions/session-manager-maintenance-async.test.ts --maxWorkers=1: 16 passed, 55.29 s file duration / 60.37 s wrapper wall time. The initial affected CI file took about 14 s under Bun. Changed-file lint and format pass. An initial local native-Bun test-runner attempt timed out; the successful rerun used CI's Bun/Vitest route.Final CI and inherited-failure exception
Run 38395776205 tested head
8b3ea7cc284aec813443f481d8ab8d3678e50f98in merge8cae901a82de874f9ce0c044e4a7521356658caf. Test lanes, typechecks, build, docs, source/closure checks, and security passed. The only failed nonaggregate job waschecks-fast-baseline-ratchets(job); the aggregate gate reflects that failure.Inherited finding:
src/cli/run-main.test-support.ts:388mocks../config/io.factory.jswithout an isolation annotation. The repository's native TypeScript parser and mock-factory scanner reject the single file identically on CI's main parent7c72968482197590814397a78738f1d0121eb64aand the tested merge:line: 388, specifier: ../config/io.factory.js, allowed: false. Both file blobs have SHA-2565ba83bf16be1792fec5c751dd85c00f67aa29de99f1250e65564d519137459e6; all scanner, target, and baseline inputs are unchanged between those revisions. The same probe reports no findings for main's subsequent fix 3cffaf5fc5a6.This attribution ran against synthetic source fixtures in the isolated Linux sandbox. Landing uses the authorized inherited-only failure exception. CI was not rerun, main's fix was not transplanted, and the failed gate is not represented as passing.