Repository navigation
refactor(gateway): share one shutdown drain wait - #165574
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 October 5, 2026, 9:52 AM ET / 13:52 UTC (Revision 3). ClawSweeper reviewWhat this changesThe PR shares shutdown waiting and remaining-work formatting between CLI shutdown and direct Gateway close while preserving their existing work scopes and cleanup ordering. Merge readiness✅ Ready for maintainer review The prior reply-scope documentation finding is resolved, and no introduced correctness defect was found. This remains useful work absent from current main; the earlier green CI result does not establish passing checks for the pinned head. Priority: P3 Review scores
Verification
How this fits togetherGateway shutdown waits for admitted work before cancellation and resource retirement. CLI shutdown inspects process-wide activity; direct Gateway restart inspects its own chat runs and queued turns alongside the existing process-wide pending-reply count. flowchart TD
A[CLI shutdown] --> C[Process-wide activity]
B[Direct Gateway restart] --> D[Gateway chat and queue activity]
E[Process-wide pending replies] --> D
C --> F[Shared deadline wait]
D --> F
F --> G[Drain result and named counts]
G --> H[Existing recovery and cleanup]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource -38, Tests +31, Docs +5. Total -2 across 9 files. View PR surface stats
Review metricsNone. Technical reviewBest possible solution: Keep one shared deadline waiter while callers retain their existing activity scopes, timer behavior, and persistence-before-retirement ordering. Do we have a high-confidence way to reproduce the issue? Not applicable: this PR consolidates existing shutdown behavior rather than claiming a newly reproduced defect; source inspection confirms the duplicate waiting paths. Is this the best way to solve the issue? Yes: a shared inspector-driven waiter removes duplicate deadline handling without moving cancellation or persistence ownership. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 9251c970d840. 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 (2 earlier review cycles)
|
Keep process-wide CLI and instance-owned Gateway inspectors separate while sharing deadline waiting and count formatting. Preserve restart metadata normalization, terminal persistence joins, recovery marking, and cancellation ordering. No schema, durability, or drain-budget changes.
9c540fb to
b31be07
Compare
|
Merged via explicitly authorized admin squash with attributed pre-existing CI failures.
|
* refactor(gateway): share one shutdown drain wait Keep process-wide CLI and instance-owned Gateway inspectors separate while sharing deadline waiting and count formatting. Preserve restart metadata normalization, terminal persistence joins, recovery marking, and cancellation ordering. No schema, durability, or drain-budget changes. * docs(gateway): describe the process-wide pending-reply count in Gateway close drain
Related: #164403, #165524
What Problem This Solves
CLI shutdown and direct Gateway close maintained separate drain loops and log formatters, making their timing and ownership behavior harder to keep consistent. This is the maintainer-requested S6 restart-reliability audit slice.
User Impact
Existing drain budgets and recovery behavior are preserved. CLI shutdown waits for process-wide work; direct restart close waits for its Gateway's active chat runs and accepted queued turns, plus the process-wide pending-reply count (the existing
getTotalPendingRepliescounter is process-global; this PR does not change that scope). Remaining work uses the same named-count log format. No configuration, schema, durability, or migration changes are required. The change takes effect in the next Gateway process; it cannot modify shutdown already running older code.Why This Change Was Made
One inspector-driven wait owns deadline handling and polling. Callers retain their scope, polling intervals (250 ms process-wide, 100 ms per Gateway), and timer-reference behavior. Close preparation normalizes restart mode once while retaining the public
restartExpectedMsadapter. Terminal persistence, recovery marking, cancellation, resource retirement, and the native process-exit path retain their ordering.The duplicate reply poller and formatter are removed. Production delta: 81 additions, 119 deletions; net −38 lines, excluding tests and docs. This is an ownership-preserving refactor, with no shutdown-speed claim.
Evidence
A historical read-only audit found 9 of 11 restarts exhausted their bounded drain while agent turns were executing. Eight logged recovery marking before a later cleanup deadline; the ninth last logged a pending marker, although a zero-result completion could be silent. Deadline messages repeated the last drain snapshot and did not identify the later blocked cleanup owner. No attributable registration-release bug was established, so this change does not widen budgets or remove joins.
All heavy validation ran on Blacksmith Testbox (
blacksmith-testbox, profileopenclaw-check), with the base and candidate source hashes verified. The restart proof used leasetbx_01m45zb6tm0gzs02dtthy1w2v1, based on5cd4a04fe65dplus the reviewed patch. Production and test files remain byte-identical after the final mechanical rebase. The final docs-only commit corrects the description of the process-wide pending-reply counter.291 passed; 231.25 s wall, including real OS SIGTERM, direct close and CLI paths, two-Gateway isolation, queued input, terminal persistence, database-close receipts, and native deadlines. Changed-test execution cost: active-work suite 40 ms; shutdown suite 10.48 s (existing native process-tree cases). New coverage uses fake timers and replaces the narrower reply-grace case; no new process or Gateway boots were added.
Before the refactor, the original four focused suites passed 257 tests in 89.22 s on
tbx_01m45wdkv2vcp2cpgkce72j489. The final run includes additional native-exit coverage and a newer base, so these are test-cost measurements, not a before/after performance benchmark.Earlier validation found inherited stale-baseline and unused-export failures. Their upstream fixes were incorporated by rebase, with no unrelated fixes copied into this diff. The rebase preserves #165524's native exit path and received fresh independent review: scoped-clean through P2.
node scripts/check-changed.mjs --stagedpassed formatting, all ratchets, core production types, and all 27 core-test type graphs. It stopped at two inherited unused exports:src/agents/runtime/index.ts: BranchSummaryResultandsrc/gateway/chat-sanitize.ts: stripEnvelopeFromMessage. Removing this lane's patch and runningnode --import tsx scripts/check-deadcode-exports.mtson untouched base5cd4a04fe65dreproduced the same failures. The nine remaining planned lint/schema/storage/import-cycle/auth checks were then run individually and all passed; changed-file lint reported zero warnings/errors and import-cycle checking found zero cycles. The full gate is reported as an inherited failure, not green.The first CI run was cancelled by fail-fast after five unrelated PR-tooling tests failed: two in
eager-import-closure.test.ts, three inpr-worktree-provision.test.ts. The CI merge base omitteddeferred-plugin-migrations.contract.tsfrom the wrapper inventory. Upstream fix044859704ebais now incorporated by rebase. The patch ID and all nine reviewed files are unchanged; the earlier review and behavior proof remain valid.Exact-head CI run 37314223759 passed on
b31be072c3f4ccfefaf64db37dd4ad960de6a032: all 62 jobs settled, no failures. The 10 executed Node-test jobs consumed 2,260 runner-seconds, including setup; the longest was 416 s. That result is historical evidence for the unchanged production and test patch; the final docs-only head has the inherited CI failure documented below.Inherited CI failure — authorized admin landing
The reviewed head is
fff8393d0ed53d6e79767c2569b562a34ca6158e. ClawSweeper's durable review names this head, reports no actionable findings, and confirms the documentation scope correction.Exact-head CI run 37318297550, attempt 1 has one non-aggregate failed job:
check-test-types-core-5. It reportstest/gateway-rpc-exporters.test.ts(110,9): TS2322, assigningGatewayMethodDescriptorInput[]toGatewayMethodDescriptor[]without requiredprofileAccess. The failure was introduced on main by3e7f2d88803e294a0a0b03cc136014648c0e5ddd(test(gateway): register descriptors in RPC exporter fixture) and is tracked by #165597. This PR does not modify that fixture or the descriptor/registry type contracts.The inspected CI checkout is
91a7e8b8b51abbc1b5130a558495046afdef526d, merging this head into baseline740ddaa458e3a6d931a03a323b6f853767227c0f. The failure inputs are unchanged between that baseline and tested merge.openclaw/ci-gateaggregates the same failure. The successfulpr-fail-fastmonitor cancelledchecks-node-changed-1andcheck-test-types-core-2,check-test-types-core-3, andcheck-test-types-core-4; those cancelled jobs remain incomplete coverage, not passing evidence.security-fastpassed.The maintainer authorized the native inherited-failure admin exception for this exact head. No CI rerun or source push is requested to obtain green. Existing reviewed Testbox proof and historical green CI remain scoped evidence for the unchanged production/test patch; current-head CI is explicitly not green.