Repository navigation
improve(sessions): reduce stalls when reloading long chats - #202136
Conversation
Keep equal owner-sealed messages shared through reload and model-context publication. Preserve serialized property order, physical string reuse, and per-entry detachment before writable SDK exposure. Add aliasing regression coverage and a 5000-entry receiver-owner benchmark.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
Codex Review Summary: ✅ CompletedThis 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. |
|
Codex review: needs maintainer review before merge. What this changesThis PR reuses equal immutable transcript messages during reload while preserving serialized bytes and writable SDK isolation. Example: Reload a chat containing an unchanged read tool result with 16 KiB of text.
Review scores
ProductKind: Performance · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review Keep open: this maintainer-authored PR removes redundant reload copying still present on main, and source review found no introduced correctness or security defect. Priority: P2 Before mergeNone. FindingsNone. Tests
Agent review detailsHow this fits togetherSQLite hydration supplies immutable transcript snapshots to SessionManager, which publishes model context and detaches selected entries for writable SDK access. flowchart TD
A[SQLite transcript] --> B[History worker]
B --> C[Immutable snapshot]
C --> D[SessionManager reload codec]
E[Resident sealed messages] --> D
D --> F[Immutable model context]
D --> G[Detached writable SDK views]
Technical reviewBest possible solution: Keep the focused codec optimization with ordered equality, existing immutable admission, and existing SDK detachment. Do we have a high-confidence way to reproduce the issue? Current main clones frozen incoming messages during resident-string reuse and then clones mutable data during context publication; the body reports a base-failing regression, but no code was executed during this read-only review. Is this the best way to solve the issue? This is a small repair at the existing shared owner: ordered equality preserves bytes, immutable admission permits sharing, and changed or mutable payloads retain the established path. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 3f078860b549. Provenance checked
TestingProof path: in-process harness. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +31, Tests +52, Other +155. Total +238 across 4 files. Added test files: 0. View PR surface stats
Preflight sourceSource unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified. Review metrics
LabelsLabel changes:
Label justifications:
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment Reviewed November 19, 2026, 7:21 PM ET / November 20, 2026, 00:21 UTC. |
|
Merged via squash.
|
…202136) Keep equal owner-sealed messages shared through reload and model-context publication. Preserve serialized property order, physical string reuse, and per-entry detachment before writable SDK exposure. Add aliasing regression coverage and a 5000-entry receiver-owner benchmark.
What Problem This Solves
Reloading long chats unnecessarily copies unchanged tool-result messages on the Gateway main thread. The string-sharing path thaws immutable hydration payloads, so model-context publication must copy and freeze them again.
User Impact
Less CPU, allocation, and synchronous blocking while reloading large histories. Stored transcripts, model context, history bytes, and writable Plugin SDK views retain their existing behavior. No configuration or migration is required.
Why This Change Was Made
The existing codec now reuses an equal resident message when both payloads carry the transcript owner's immutable admission. Equality includes property order to preserve serialized bytes. Entry envelopes remain private, and the existing per-entry ownership path detaches nested payloads before SDK exposure. Changed messages and mutable resident views retain the existing string-sharing path.
Production delta is +31 lines: the ordered comparison and ownership checks avoid the redundant copies without giving up physical string sharing. No new cache, state owner, or API is introduced. Rewrite/rollback and mutable SDK copies remain because their callers hold independent writable references.
Evidence
The regression fails on base
abd4b4fc6a828and passes with the fix. Equal, changed, reordered, and mutable resident payloads preserve serialized bytes and mutation isolation after publication. The real asynchronous SQLite reload test also verifies physical string sharing, rewrite correctness, and nested SDK mutation isolation. 105 distinct focused tests passed, including model-context snapshots and prompt-prefix stability.Receiver-owner benchmark: 5,000 entries, 16 KiB tool results plus nested metadata; fresh structured-cloned reply each operation; two warmups and ten measured operations per process; five counterbalanced A/A and A/B pairs. Allocation was sampled separately, including collected objects. All 400 measured operations had identical history/model-context hashes.
A/A CPU: 77.41 / 78.57 ms; allocation: 62.99 / 62.82 MiB. Baseline CPU range 74.19–79.45 ms; candidate 47.90–50.94 ms. Maximum-task ranges 80.12–84.90 ms versus 58.35–63.67 ms. Host load is recorded per sample. This measures freeze, reload adoption, and initial-context publication; it excludes SQL, worker transport, and workspace discovery and makes no whole-Gateway stall claim.
Reproduce with
node --expose-gc --import ./scripts/tsx.mjs scripts/perf/transcript-hydration-clone-measure.ts --output /isolated/result.json; use--owner-file /isolated/base-codec.tsfor the base and--allocationin separate processes.Independent Codex autoreview: complete local diff against the base above, P0/P1, scoped-clean, no findings. Codec suite: 34 tests, 12.12 s wall; hydration suite: 21 tests, 40.51 s wall; remaining 50 focused tests cover snapshots and prompt stability. Existing subprocess/SQLite fixtures account for most runtime; the change adds no new boot fixture. No browser-runtime module or visual behavior changed.
pnpm check:changedcompleted all selected checks. It initially reported two proof-only type errors (test-message narrowing and a benchmark unique-symbol import); both were corrected and their affected typechecks now pass. Production types, scoped lint/format, zero unused exports, database/schema boundaries, and import-cycle guards passed. The final four-case regression and benchmark smoke also pass. Final Codex P1 review of the corrected diff is scoped-clean.