Skip to content

perf(sessions): avoid payload copies during transcript rewrites - #171019

Merged
steipete merged 2 commits into
mainfrom
steipete/perf-micro-session-persistence-clone-stall-20260919
Oct 13, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/perf-micro-session-persistence-clone-stall-20260919

Conversation

@steipete

@steipete steipete commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

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 structuredClone on 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 f20ef9a3befd and candidate:

node --import ./scripts/tsx.mjs scripts/bench-session-manager-rewrite.ts --check
Measurement Before After
Median maximum event-loop stall 134.47 ms 1.72 ms
Worst event-loop stall 217.10 ms 12.85 ms
Median snapshot preparation 70.05 ms 0.43 ms
Median awaited commit 70.40 ms 5.14 ms

The baseline fails the 50 ms stall regression; the candidate passes all five samples. The benchmark uses a setImmediate heartbeat with performance.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.
  • Changed-file formatting and repository lint; full typecheck/build/closure checks and broad suites delegated to PR CI. No Testbox/Crabbox leases.
  • independent subagent review (autoreview route unavailable): clean P0–P2 review. Autoreview preflight refused the selected provider/config mismatch; no operator configuration was changed. Review's CI timing-flake finding was addressed by moving the strict budget to the benchmark.

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.cause in 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 8b3ea7cc284aec813443f481d8ab8d3678e50f98 in merge 8cae901a82de874f9ce0c044e4a7521356658caf. Test lanes, typechecks, build, docs, source/closure checks, and security passed. The only failed nonaggregate job was checks-fast-baseline-ratchets (job); the aggregate gate reflects that failure.

Inherited finding: src/cli/run-main.test-support.ts:388 mocks ../config/io.factory.js without an isolation annotation. The repository's native TypeScript parser and mock-factory scanner reject the single file identically on CI's main parent 7c72968482197590814397a78738f1d0121eb64a and the tested merge: line: 388, specifier: ../config/io.factory.js, allowed: false. Both file blobs have SHA-256 5ba83bf16be1792fec5c751dd85c00f67aa29de99f1250e65564d519137459e6; 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.

Retain independently mutable rewrite snapshots while reusing string values. Preserve native opaque SDK graph semantics and add controlled 50 MiB durable rewrite stall proof.
@clawsweeper

clawsweeper Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@steipete
steipete marked this pull request as ready for review October 13, 2026 12:02
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T12:18:11.457785Z 8b3ea7c New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation scripts Repository scripts agents Agent runtime and tooling size: S labels Oct 13, 2026
@steipete

Copy link
Copy Markdown
Contributor Author

Landing under the authorized inherited-failure exception for head 8b3ea7cc284aec813443f481d8ab8d3678e50f98.

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 src/cli/run-main.test-support.ts:388 (../config/io.factory.js). The repository scanner produces the identical disallowed finding on CI main parent 7c72968482197590814397a78738f1d0121eb64a and tested merge 8cae901a82de874f9ce0c044e4a7521356658caf; file SHA-256 is identical (5ba83bf16be1792fec5c751dd85c00f67aa29de99f1250e65564d519137459e6) and the scanner/baseline/target inputs are unchanged. Main's subsequent fix 3cffaf5fc5a6 yields no findings in the same single-file probe.

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.

@steipete
steipete merged commit fed5b24 into main Oct 13, 2026
147 of 151 checks passed
@steipete
steipete deleted the steipete/perf-micro-session-persistence-clone-stall-20260919 branch October 13, 2026 12:37
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 14, 2026
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling docs Improvements or additions to documentation scripts Repository scripts size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant