Repository navigation
perf(sessions): avoid archive recovery stalls before replies - #158559
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: needs maintainer review before merge. Reviewed September 26, 2026, 12:39 AM ET / 04:39 UTC (Revision 5). ClawSweeper reviewWhat this changesThe branch reads session rows and pending-archive state in one SQLite worker snapshot, skips empty archive recovery during ordinary updates, and delays maintenance writer admission until native work needs it. Merge readiness✅ Ready for maintainer review Keep open: current main still takes the archive-probe path associated with the reported reply stalls. The branch has credible Gateway timing evidence, and this review found no definite introduced defect. Priority: P2 Review scores
Verification
How this fits togetherThe session lifecycle layer updates per-agent SQLite session rows as chat turns proceed. Its history worker supplies row and archive facts; archive publication and maintenance can otherwise delay the next reply. flowchart LR
A[Chat turn] --> B[Session lifecycle update]
B --> C[History worker snapshot]
C --> D[Writer commit]
D --> E{Archive recovery needed?}
E -- No --> F[Reply continues]
E -- Yes --> G[Archive publisher]
G --> F
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +52, Tests +88, Docs +8. Total +148 across 25 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep ordinary replies free of empty archive-recovery waits while preserving pending-archive recovery, database replacement checks, and native write settlement. Do we have a high-confidence way to reproduce the issue? No current-main runtime replay was performed in this read-only review. Current main retains the reported probe path, and the PR supplies a detailed Gateway before-and-after run on an earlier baseline and candidate. Is this the best way to solve the issue? Yes. Reusing the existing history worker and retaining the native archive publisher is a focused way to remove the empty-recovery delay without adding a second storage owner. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 3afde1f98247. 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)
|
Move replacement request and result types into a leaf contract so lifecycle archive admission does not cycle through the agent execution adapter and maintenance implementation. Migrate every consumer without compatibility re-exports. Validation: the existing Madge cycle gate fails on f838c34 and passes after extraction; core typechecking, targeted lint, formatting, and diff checks pass. Existing modules compile to identical normalized runtime JavaScript. Independent Codex review found no actionable P0-P2 findings.
The pending-archive probe now shares the history reader. Update the existing diagnostics case to require one worker across both warm cleanup passes, preserving native failure identity, phase diagnostics, and stored data assertions. Reproduced the exact CI assertion locally with seven passing siblings. All eight tests pass after the correction (110.38 seconds command wall); independent Codex review found no actionable P0-P2 findings.
…ter spans Keep the exact maintenance coalescing sequence while dropping the retired outer finalization and empty archive-probe writer spans. Require the three native commit admissions and one finalization diagnostic, and preserve persisted stale/active entry checks. The full file reproduced the CI failure with seven passing siblings. All eight tests pass after correction (72.41 seconds wall); independent Codex review found no actionable P0-P2 findings. A bounded sibling audit found no further stale admission or worker-count expectations.
Await retained reader cleanup before forcing lifecycle cold validation. Verify asynchronous lifecycle callbacks and foreground progress during maintenance validation while preserving native integrity, authority, commit order, and archive postconditions. Reproduced the five stale assertions in the complete prepared-admission file. The repaired file plus maintenance settlement sibling pass all 31 tests (80.65 seconds wall). Independent Codex review found no actionable P0-P2 findings.
|
Merged via squash.
|
…w#158559) * perf(sessions): keep archive recovery off empty chat upserts * refactor(sessions): share lifecycle mutation parameter type * fix(sessions): break lifecycle replacement contract import cycle Move replacement request and result types into a leaf contract so lifecycle archive admission does not cycle through the agent execution adapter and maintenance implementation. Migrate every consumer without compatibility re-exports. Validation: the existing Madge cycle gate fails on f838c34 and passes after extraction; core typechecking, targeted lint, formatting, and diff checks pass. Existing modules compile to identical normalized runtime JavaScript. Independent Codex review found no actionable P0-P2 findings. * test(sessions): require archive reader reuse across warm cleanup The pending-archive probe now shares the history reader. Update the existing diagnostics case to require one worker across both warm cleanup passes, preserving native failure identity, phase diagnostics, and stored data assertions. Reproduced the exact CI assertion locally with seven passing siblings. All eight tests pass after the correction (110.38 seconds command wall); independent Codex review found no actionable P0-P2 findings. * test(sessions): assert native maintenance writer admission without outer spans Keep the exact maintenance coalescing sequence while dropping the retired outer finalization and empty archive-probe writer spans. Require the three native commit admissions and one finalization diagnostic, and preserve persisted stale/active entry checks. The full file reproduced the CI failure with seven passing siblings. All eight tests pass after correction (72.41 seconds wall); independent Codex review found no actionable P0-P2 findings. A bounded sibling audit found no further stale admission or worker-count expectations. * test(sessions): align cold admission fixtures with worker-backed reads Await retained reader cleanup before forcing lifecycle cold validation. Verify asynchronous lifecycle callbacks and foreground progress during maintenance validation while preserving native integrity, authority, commit order, and archive postconditions. Reproduced the five stale assertions in the complete prepared-admission file. The repaired file plus maintenance settlement sibling pass all 31 tests (80.65 seconds wall). Independent Codex review found no actionable P0-P2 findings.
What Problem This Solves
Fixes pauses before replies when an existing session checks for pending transcript archives behind background reclamation, even though no archives need recovery.
User Impact
Ordinary lifecycle upserts skip empty archive recovery. Real archive recovery, retention, and write settlement remain intact; there are no protocol, configuration, schema, or upgrade changes.
Why This Change Was Made
The lifecycle row reader now carries a pending-archive fact from the same snapshot, bound to the physical database identity. Unknown or replaced identities retain conservative recovery, and archive-producing mutations and Doctor transfers still publish. Standalone probes reuse the history reader without archive FIFO or writer admission. Maintenance finalization requests admission for native access and keeps it through commit publication, while preparation and validation allow foreground writes to proceed.
Evidence
Same Testbox (4 CPUs, Linux, Node 26.9.0), exact baseline
cc23d2a7f8bversus candidatef838c345e0d; fresh isolated state, mock provider, three turns per client, A/B/B/A order with identical timeline instrumentation. Values below are medians of the two trial medians; warm means turns 2–3.Archive/reclamation worker constructions fell from 6 → 2 for one client and 19 → 3/2 for five; background reclamation remained enabled. All 72 turns completed, with no failed/pending turns, disconnects, or integrity errors. Both five-client candidate trials recorded one pre-load
chat.metadataaccess-changeUNAVAILABLEerror, also observed in the earlier campaign investigation. Those runs remaincompleted-with-errors; these small instrumented trials do not establish 50-session capacity.Dedicated Testbox run: full builds passed in 7:37.75 / 8:09.72;
node scripts/check-changed.mjspassed in 24:50.00. The same changed gate also passed locally (3828.31 s).All 120 candidate tests passed. Commands used
node scripts/run-vitest.mjs <file> --maxWorkers=1:src/config/sessions/)session-accessor.sqlite-archive-session.test.tssession-accessor.sqlite-fresh-upsert-archive.test.tssession-accessor.sqlite-maintenance-admission.test.tssession-entry-read.worker.test.tsThe archive suite retains native file-publication, crash-recovery, and worker-retirement contracts that a mocked helper cannot prove; its wall time includes worker preparation. CI test seconds are pending.
The two new empty-recovery assertions and revised maintenance-admission assertion all fail on the original code (21.73 s), with only those two test files copied onto the baseline. Raw assertions confirm the intended failures. The proof script's strict text classifier reported a false negative because Vitest abbreviated the maintenance expected array; this was manually verified against the failing source line, without rerunning or changing the test.
Every Gateway exited normally, all isolated states and child processes were cleaned up, the result archive checksum matched, and the lease is stopped. Initial proof attempts were stopped before measurements for a native-sync source-identity mismatch, slow local wrapper preparation, and the now-fixed line-cap violation.
The local focused run passed 66 tests across archive publication/recovery, lifecycle upserts, snapshot freshness, maintenance admission, and cached-handle closure (1156.51 s wall on the saturated host, including cold worker compilation). The parameter-type extraction afterward produces identical normalized JavaScript.
Deslop completed without further changes. Independent Codex review of the final branch found no actionable P0–P2 findings.
Production: +212/-160 lines (net +52); tests: +181/-93 (net +88); docs: +8. The production growth carries snapshot/identity facts through the existing reader and replaces the old per-probe worker operation. The existing lifecycle parameter type was moved to its types owner to keep the projection module within its line cap.
Overlap inspected: #158530 (diagnostic reader), #158450 (artifact reader), #158401 (creation path aliases), and #157934 (archive-worker identity). Their production hunks are separate from this recovery/admission cutover; the worker operations use distinct request kinds and the same existing reader owner. No PR is stacked here.
Landing CI repair
Exact-head CI run 36210394268 found a static import cycle through lifecycle state, agent execution, and the replacement implementation; CI fail-fast then cancelled the remaining jobs. The same cycle reproduced locally on
f838c345e0dd128649952e799cbc953dbc66299e. Replacement request/result types now live in a leaf contract consumed directly by all four callers. No runtime behavior changed: all four existing modules produce identical normalized JavaScript.The existing
check:madge-import-cyclesregression gate failed before this repair and passes afterward with zero cycles. Core typechecking, targeted lint, formatting, andgit diff --checkalso pass. No duplicate source-shape test was added.Independent Codex review of the landing repair found no actionable P0–P2 findings.
The next exact-head run, 36212378732, passed the architecture repair and exposed a stale assertion in
session-accessor.sqlite-cleanup-reclamation.test.ts: two warm empty cleanup passes expected two worker constructions, while the new shared history reader correctly constructs one. The full local file reproduced that single failure with seven siblings passing. The existing case now explicitly requires warm reader reuse while retaining its diagnostic-phase, native-error identity, cleanup-result, and persisted-data checks. All eight tests pass after the correction (110.38 s command wall usingnode scripts/run-vitest.mjs <file> --maxWorkers=1; 6.28 s summed test execution in the preceding CI run). No production behavior or timeout changed for this correction.Independent Codex review of the test correction found no actionable P0–P2 findings.
Run 36213751658 then exposed the corresponding stale writer-diagnostic sequence in
session-accessor.sqlite-writer-owners.test.ts. The approved scheduling change removes the outer maintenance-finalize admission and the empty pending-archive probe's writer admission. Its existing coalescing case now requires the exact remaining sequence, including all three native worker-commit admissions and one finalization diagnostic; stale-row deletion and active-row preservation checks remain. The full local file reproduced the failure with seven siblings passing, then passed all eight tests after correction (72.41 s command wall; 7.02 s summed execution in the preceding CI run). Independent Codex review found no actionable P0–P2 findings, and a bounded sibling audit found no additional stale worker-count or admission expectations. No CI run was retried without a diagnosed repair.Run 36214968626 exposed five prepared-admission fixture assertions: lifecycle callbacks now await the history reader, and maintenance validation intentionally permits foreground writers before native commit admission. The cases now require those behaviors while retaining final callback/FIFO, integrity, authority, and archival checks. Cold lifecycle fixtures also await retained reader cleanup before invalidating admission; synchronous close could leave resources draining and make best-effort finalization return before the probe. A complete local replay reproduced all five failures with 25 siblings passing. The repaired file plus the maintenance commit-settlement sibling pass all 31 tests (80.65 s grouped command wall; prepared-admission recorded 25.06 s test execution in the preceding CI run). These native integrity and deletion/archival boundary cases retain their real worker/subprocess proof; no timers or timeouts were added. Independent Codex review found no actionable P0–P2 findings, and the broader sibling FIFO audit found no further affected expectations.
Run 36216465597 was blocked by an unrelated main regression: the UI E2E graph imported
ControlUiRootStatefrom its retired exporter after #158464. The same TS2459 reproduced on current main and the corrected import passed the exact graph. Main fixda8149b8ba7d88d76dbe7979986e307a4542e6f0landed concurrently with that diagnosis; no duplicate fix was published. Current main was merged into this PR without conflicts to incorporate it and obtain fresh merge-ref CI.