Repository navigation
perf(sessions): avoid duplicate reads during compaction - #169931
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review in progressClawSweeper is reviewing this revision. This supersedes any previous blocked status. |
|
Codex review: needs maintainer review before merge. What this changesThis PR combines transcript cursor and first-slice reads and reuses compaction facts collected after prior writers settle. Example: Session sess-compact-settled initially has no transcript; its previous writer commits a final message during settlement.
Review scores
ProductKind: Performance · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review Keep this PR open: it contributes useful compaction improvements absent from current main, and review found no introduced correctness or security defect. Priority: P2 Before mergeNone. FindingsNone. Tests
Agent review detailsHow this fits togetherThe Gateway compaction RPC coordinates session lifecycle ownership and invokes the compaction runner; the history worker reads canonical SQLite events and returns bounded transcript frames. flowchart TD
A[Compaction request] --> B[Prior writer settlement]
B --> C[Session lifecycle owner]
C --> D[Prepared compaction facts]
D --> E[Compaction runner]
E --> F[History worker snapshot]
F --> G[Bounded transcript frames]
E --> H[Guarded persistence]
Technical reviewBest possible solution: Keep the existing worker and lifecycle owners while removing their redundant reads. Do we have a high-confidence way to reproduce the issue? Current-main source shows per-event first-slice queries and an empty-transcript return before writer settlement; no target code was executed during this read-only review. Is this the best way to solve the issue? The patch removes duplicate work at the existing owners while preserving snapshot transfer, storage-error propagation, lifecycle exclusion and final persistence checks. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 63bd1e92db98. Provenance checked
TestingProof path: in-process harness. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource -2, Tests +63, Docs +5. Total +66 across 6 files. Added test files: 0. View PR surface stats
Review metrics
LabelsLabel changes: No label changes. Label justifications:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
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 HistoryReviewed October 12, 2026, 10:45 AM ET / 14:45 UTC (Revision 3). |
) Return the first bounded transcript byte slice from the ordered hydration cursor and reuse entry/statistics facts after writer settlement. Preserve snapshot consistency, backpressure, exact bytes, lifecycle ownership, and final mutation authority. Live paired proof: compaction statements 205 -> 142 (30.7% fewer), MAIN 0 -> 0. The changed operations remove 73 statements and one request; the raw phase includes seven additional queued/maintenance requests. Current-head CI and Codex review completed. The only Knip finding is the pre-existing durable receipt re-export already removed on main.
What Problem This Solves
Manual transcript compaction hydrates the transcript more than once. Its ordered cursor previously read event sequence numbers, then issued a separate query for the first 64 KiB of every event. Return that first bounded slice from the cursor and retain follow-up reads only for larger events. This preserves the SQLite snapshot, event order, database encoding, transport backpressure and bounded memory. Large payloads stay sliced in SQL.
Read compaction's entry and transcript statistics once after the previous writer settles, then reuse those facts while the lifecycle owner excludes competing session work. Keep the final persistence guard and live requester/operator authority checks. The regression starts with an empty transcript, commits the previous writer's final message and entry update during settlement, and verifies compaction sees both.
Measurement
One alternating baseline/candidate pair of the shared live SQL workload, with the workload unchanged and request attribution added in a private harness copy. Baseline
0cea8cd24977; candidatec62673a2ffb6. All workload assertions passed on both. Counts include all threads; warmed statistics cover ten turns per run.The baseline already has zero compaction MAIN SQL: the older census's two reads are absent. Changed owners have no Gateway-main-thread SQL calls: T1 0 → 0. CI's worker/T1 and dialect ratchets pass.
Operation attribution
streamSessionTranscriptHydrationcursor / slices / transaction envelopeThe changed operations remove 73 statements and one request: 72 per-event first-slice SELECTs and one duplicate statistics read. The entry reread removal is not assigned an additional measured saving because retained facts already elide it in this workload. Five additional exact-entry requests dispatch from
WorkerTaskPoolCore.finish, and two unchanged maintenance requests (purgeExpiredSecretStoreEntries,runMaintenance) fall inside the candidate's compaction window. These add ten statements and seven requests, explaining the observed −63 statements / +6 requests exactly. Whole-workload exact-entry requests fall from 101 to 91; no new request class or dispatch was added by this PR.The host load changed substantially between runs: complete workloads took 339.6s and 85.1s; compaction windows took 1.919s and 3.122s. Do not interpret these as latency benchmarks or credit the raw warmed-turn decrease to this PR. The modified full-hydration worker runs only during compaction in this workload; warmed turns use the separate bounded-hydration path. Background scheduling explains changes outside the touched operation. Exact operation savings above are directly counted, independent of elapsed time.
Shared-family statements per warmed turn:
No shared warmed family rises. This PR adds no cache, invalidation policy, schema, worker routing, or compatibility path.
Removed checks and accepted edges
No schema, update, migration, plugin-loading, service-lifecycle or public SDK change. No change to compaction checkpoints or transcript durability. No update cell or SDK API diff applies.
Evidence
git diff --checkpass.DurableDirectoryReceiptre-export. The same rule/symbol fails in scheduled main CI; newer main already removes it in9514dbad63f, and this PR does not reintroduce it. The initial concurrent full-tree scan timed out at 600s; the isolated rerun completed within the unchanged limit.pnpm test src/gateway/server.sessions.compaction.test.ts --maxWorkers=1: 20 passed, 1 timeout in the existing reset-waits-for-terminal-compaction test; 701.35s total under heavy load. The new regression passed. The timeout remains an unresolved local observation; passing current-head CI does not prove it fixed.LOC: production −2 (33 added / 35 removed); tests +63 (109 added / 46 removed); docs +5.