Repository navigation
perf(sessions): reduce preparation waits across sessions - #160183
Conversation
|
🦞👀 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: blocked before merge. Reviewed September 28, 2026, 9:03 AM ET / 13:03 UTC (Revision 5). ClawSweeper reviewWhat this changesThe branch lets independent existing sessions prepare replies concurrently through a keyed store queue while preserving ordering for overlapping sessions and store-wide operations. Merge readiness⛔ Blocked before merge - 3 items remain Current main still serializes session store work, so this PR remains useful. Its Gateway load evidence supports the improvement, but the measured event-loop tail regression still needs an explicit Gateway owner decision. Priority: P2 Review scores
Verification
How this fits togetherGateway chat sends and channel replies pass through session admission and shared store coordination before reply preparation. The prepared reply then streams back to the client. flowchart LR
A[Chat send] --> B[Gateway admission]
B --> C[Session store queue]
C --> D[SQLite writer]
D --> E[Reply preparation]
E --> F[Streamed response]
Decision needed
Why: The paired runs measure competing user-facing outcomes, and the repository defines no threshold that resolves their balance automatically. Before merge
Agent review detailsSecurityNone. PR surfaceSource +107, Tests +316, Docs +9. Total +432 across 13 files. View PR surface stats
Review metrics
Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Retain one keyed queue with complete conflict identities and full-store barriers under an owner-approved Gateway concurrency envelope. Do we have a high-confidence way to reproduce the issue? Yes: the PR specifies a paired Gateway and WebSocket workload, while current main retains the serial queue. This read-only review did not rerun the workload. Is this the best way to solve the issue? Yes at the ownership boundary: the shared queue is the narrow place to schedule independent sessions while retaining full-store barriers. The acceptable event-loop cost remains a maintainer choice. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 6d6ea35f7616. LabelsLabel changes: No label changes. Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (4 earlier review cycles)
|
|
Admin-merging: the red checks on head 726fc17 were each checked, and none reproduces as a regression from this change.
164 other checks passed. On the merged tree, the queue/admission/placement/session-init suites (72 tests) pass, the changed-lane gate passes with zero lint findings, and Codex autoreview is clean through P2. The PR body has the pinned 100/50 A/B: shared-lane wait p50 6.29 s → 0.77 s, first delta p50 −21.5%, main CPU −8.5%. |
What Problem This Solves
Fixes long response-start waits when many Control UI sessions prepare replies on one Gateway: one session can hold the shared store lane while waiting for the SQLite broker and physical writer.
User Impact
Independent existing sessions can prepare concurrently. Same-session ordering, live admission checks, and placement settlement remain protected. No configuration, protocol, schema, durability, retention, or update migration changes are required.
The measured first-delta improvement comes with a tradeoff: event-loop p99 increased, and ACK-to-provider p50 changed little. Remaining main-thread stalls still need work.
Why This Change Was Made
The existing queue owner now accepts complete conflict-key sets, preserves FIFO between overlapping waiters, and treats an unkeyed task as a full-store barrier. Barriers wait for earlier accepted work and exclude later writers. Cancellation removes waiting work only; active work retains settlement and caller context. Reentrancy requires complete held-key coverage.
Only audited canonical reply admission, qualified chat admission, and eligible existing-session initialization opt in. Creation hooks, parent forks, legacy-main retirement, rollovers, unscoped validators, migration, and placement retain store-wide exclusion. A creation observed after waiting retries before invoking its hooks. The initializer's shared result type moved to a type-only module to satisfy its existing line-growth ratchet; both consumers moved with it.
Related: #159632 changes admission worker custody; #159527 cleans up the reply owners. Their current diffs were inspected and reconciled with these call sites. Neither implements keyed store scheduling; the claim lifecycle and cleanup logic are unchanged here.
Evidence
Same isolated AWS c7a.8xlarge, Gateway pinned with
taskset -c 0-3, four driver processes, Node 26.9.0, pnpm 12.5.1. Each invocation used 100 WebSocket clients / 50 active sessions, three sequential turns per client, 40 mock chunks/second, seed 1, and fresh synthetic state. Queue/broker observation was enabled in both variants. Baseline:c849a831a17fd4508684a5cda94e688b0c00c7eb. No real provider or browser contexts were used.Steady-state latency means turns 2 and 3, 100 observations per complete run. CPU covers the entire fixed 150-turn workload; lower elapsed time therefore does not inflate the CPU win. Percentiles are independent and must not be added.
The first two complete pairs delivered 150/150 replies with no integrity errors. Their median first-delta p50 fell 21.5%, while total main-thread CPU fell 8.5%. ACK-to-provider p50 was essentially unchanged. Main-thread busy percentage rose from 94.53–94.75% to 97.13–98.54%; this is a latency/CPU-cost improvement with worse event-loop tails, not a claim that the demo stall objective is solved.
Shared-store waiting per steady turn fell from p50 6.280–6.299 s to 0.590–0.948 s; its median fraction of ACK-to-provider time fell from 54.5–55.8% to 4.8–9.3%. Time-weighted shared queue depth fell from 15.39–15.44 to 1.27–2.32. Initialization queue wait p50 fell from 2.597–2.772 s to 14.0–26.5 ms. Individual initialization holds increased from 393–410 ms to 669–759 ms, but those holds now overlap across distinct keys. Their measured nested-queue waits were 367–378 ms before and 646–727 ms after; the remaining 29–30 ms before / 11–13 ms after is a residual wall-time bound, not exclusive CPU attribution. Admission hold p50 was 1.04–1.06 ms before / 2.39–2.55 ms after; admission queue wait p50 fell from 868–968 ms to 25.9–29.3 ms.
¹ B3 retained 149/150 harness-correlated replies, with one pending input and an unattributed terminal execution. Server input→execution correlation proves that execution delivered 66 text deltas and a text-bearing final, 33.334 ms before the input's empty final. The harness cannot retrospectively adopt an already-terminal execution. All 12 empty-input cases across the candidate runs have explicit execution mappings and completed text replies; these cases occurred 4/150 per candidate and 0/150 in these baselines. The frequency shift and durable transcript coverage remain limitations. B3's idle deadline tail excludes it from aggregate CPU/throughput claims. The original result is retained; streaming framing and harness correlation were not changed. An SSH ownership-renewal failure during that run was reconciled through the native Crabbox owner before further commands; all harness Gateway children exited and synthetic state was removed.
Validation:
node scripts/run-vitest.mjs src/shared/store-writer-queue.test.ts --maxWorkers=1: 17 passed, 24.11 s wall on AWS. The disjoint-progress regression fails against the original queue at the expected ordering assertion.node scripts/run-vitest.mjs src/sessions/session-lifecycle-admission.test.ts --maxWorkers=1: 31 passed, 11.09 s wall.node scripts/run-vitest.mjs src/gateway/server-worker-placement-move-barrier.test.ts --maxWorkers=1: 10 passed, 21.59 s wall.node scripts/check-changed.mjs: passed in 772.90 s wall on the exact committed source tree; changed-core lint reported 0 warnings and 0 errors. The initial line-growth rejection was fixed through the type extraction above.Release-note context: reduce cross-session response preparation waits on shared Gateways while retaining same-session and placement lifecycle ordering.