Repository navigation
perf(gateway): avoid copying state during startup lock admission - #169097
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. What this changesThis PR inspects the previous Gateway lease in the existing read-only worker under physical state custody instead of copying the shared database. Example: Restart with a 175,747,072-byte shared database on the supplied isolated Linux rig.
Review scores
ProductKind: Performance · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review Keep this PR open: it removes measured startup overhead, preserves the existing ownership checks, and introduces no identified correctness defect; required CI remains unresolved. Priority: P2 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherGateway startup acquires exclusive state custody, reads the previous owner's persisted lease, checks liveness, and transactionally publishes its own identity before heartbeat readiness. flowchart TD
A[Gateway startup] --> B[Physical state custody]
B --> C[Read-only worker]
C --> D[Previous owner lease]
D --> E[Liveness check]
E --> F[Transactional publication]
F --> G[Heartbeat readiness]
Technical reviewBest possible solution: Use the existing custody-checked worker for startup inspection while retaining Doctor snapshots and transactional lease publication checks. Do we have a high-confidence way to reproduce the issue? Fetched main routes normal Gateway admission through the copying fallback despite holding custody, corroborated by the supplied baseline regression and startup profiles. Is this the best way to solve the issue? This is a focused solution that reuses the existing worker and ownership contract, removes the superseded path, and preserves Doctor's distinct repair requirements. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 3a0db8a3a052. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource -13, Tests +12, Docs +5. Total +4 across 4 files. Added test files: 0. View PR surface stats
Review metrics
LabelsLabel changes: No label 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 HistoryReview history (1 earlier review cycle)
Reviewed October 11, 2026, 8:41 AM ET / 12:41 UTC (Revision 2). |
|
Landing under the operator-authorized inherited-failure exception. CI run 38139414266 failed in The identical scan fails on unchanged CI baseline Independent P0–P2 review is clean. Focused ownership tests passed 116/116; core and all 28 test type graphs passed; both full rig builds passed; all remaining planned changed checks passed. The separate local unused-export failure was also reproduced on the unchanged original base, as recorded in the PR body. The native landing wrapper stopped before mutation because its canonical tooling checkout has stale dependencies. Using the authorized exact-head API fallback for |
What Problem This Solves
Gateway startup copied the entire shared database to inspect the previous owner's lease after it had already acquired exclusive physical state ownership. That adds avoidable I/O and a synchronous child-process wait to every restart.
User Impact
Shorter restart admission, especially with a large shared database. No schema, configuration, lease expiry, or update behavior changes; live and unverifiable owners still prevent startup.
Why This Change Was Made
Use the existing custody-checked read-only worker for every lock role. Make custody required at the sole call site and remove the unused snapshot fallback. Doctor retains its private snapshot for schema repair. The transaction still independently rechecks the previous owner before publishing the new lease, and heartbeat readiness remains mandatory.
Production code: 13 fewer lines. The regression extends the existing publication test rather than adding another Gateway boot fixture.
Evidence
30469f450063: fails specifically at the unwanted shared-state snapshot.tbx_01m4n9rfpvctd1b2y2600gqvns: baseline regression verified after dependency installation; subsequent candidate runs stopped before tests due to two sync timeouts. Lease stopped; validation continued on the authorized AWS fallback.Measurement
Same isolated Linux rig, Node 26.8.1, three retained-state unprofiled boots per revision, 3,000 synthetic conversations across three agents, synthetic transcripts, and a 175,747,072-byte shared database containing 100,000 synthetic audit rows. Each boot performs a real
chat.send/agent.waitthrough a loopback mock provider. Retryable not-ready responses reuse the same idempotency key. No production state or services were touched.Lock admission is 2.77× faster, saving 724 ms. End-to-end medians improve by 1.70 s to readiness and 3.54 s to first chat; the rig has variable background load, so the full end-to-end difference is not attributed solely to this patch. Post-ready transcript maintenance also varied (2,702 → 1,100 ms).
Separate
--cpu-profruns confirm the removed path: baseline spends 595 ms synchronously preparing the lock-inspection snapshot; the candidate has no samples on that path. The pre-custody preflight snapshot remains (720 / 633 ms). Early baseline module compilation accounts for about 912 ms:process.bootstrapis cumulative startup time, not an independent 11-second import operation.The remaining pre-custody snapshot and deferred agent admission still dominate parts of the outage. Real provider latency, production data, and production-host timing were not measured. No schema change or deferred authority check is introduced.
Validation
cbx_432c45522ae2, runrun_d05f5503540ff62ff2e1e79c11b30129:node scripts/run-vitest.mjs src/infra/gateway-owner-lease.test.ts src/infra/gateway-lock.test.ts src/infra/gateway-lock.write-failure.test.ts src/infra/gateway-lock.namespace.test.ts src/infra/gateway-state-owner.test.ts src/infra/gateway-state-owner.heartbeat.test.ts --maxWorkers=1— 116/116 passed, six files, 76.82 s wall time (one worker; about 77 CI worker-seconds). The extended publication regression itself took 899 ms. No new test file or test-only production seam.pnpm buildpassed on the isolated Linux rig.git diff --check: passed.run_5cc596a846e77df03cdfdfeee2f1b2f6: core typecheck, all 28 test type graphs, formatting, boundary and ratchet checks passed. The gate stopped at existing unused exports in agent, voice, and UI modules. Fresh Testboxtbx_01m4ney1zdwnt7x7rfd63ge741reproduced the identical production and full-tree export lists on a verified unchanged30469f450063tree. All remaining planned checks then passed on the candidate: typed lint (0 warnings/errors), native schema, database-first storage, media helpers, runtime sidecar loaders, import cycles, webhook body ordering, and pairing guards. No guard was disabled.Inherited hosted CI failure
CI run 38139414266 failed only at the repository duplicate scan (
check-guards); its downstream CI gate failed and fail-fast canceled sibling jobs. A clean worktree of the exact CI baseline0690a2ef175fc73d69d73105638b69a90af35fe6reproduces the same uncoveredworker-heap-flag.mjsand duplicate groups outside this PR. Verified on Testboxtbx_01m4ney1zdwnt7x7rfd63ge741withnode scripts/check-duplicates.mjs. Canceled hosted jobs are unrun; the scoped remote proof above is retained.