Repository navigation
fix(gateway): preserve active turns across event-loop stalls - #131966
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. |
|
Codex review: needs maintainer review before merge. Reviewed August 28, 2026, 10:58 PM ET / August 29, 2026, 02:58 UTC. ClawSweeper reviewWhat this changesThe PR prevents Gateway timing-gap recovery from restarting active channel work until the Gateway is idle, while retrying only channel accounts whose recovery was incomplete. Regression provenancePossible regression — probable (reviewed change; failure trace). No predecessor PR is attributed. Merge readinessKeep open for normal maintainer review: current main still restarts channels unconditionally after a timer gap, while this PR adds a coherent idle/admission fence and targeted recovery retries. The supplied exact-head Gateway traces satisfy the internal reliability proof bar, and no blocking patch defect was found. Priority: P1 Review scores
Verification
How this fits togetherGateway maintenance detects long timer gaps and coordinates channel recovery with active-work admission. Its decisions affect live channel connections, health snapshots, and whether in-flight replies continue uninterrupted. flowchart LR
A[Maintenance timer] --> B[Timing-gap detection]
B --> C[Admission and active-work check]
C --> D[Deferred channel recovery]
C --> E[Health and presence refresh]
D --> F[Channel manager]
F --> G[Channel connections]
E --> H[Gateway clients]
Before merge
Agent review detailsSecurityNone. PR surfaceSource +231, Tests +379, Docs +3. Total +613 across 12 files. View PR surface stats
Review metrics
Root-cause clusterRelationship: Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Land the single admission-gated thaw-recovery path with its typed channel-manager outcomes, rather than adding a parallel restart mechanism or a configuration switch. Do we have a high-confidence way to reproduce the issue? Yes, source-reproducible: fetched main unconditionally restarts channels after a qualifying timer gap, and the linked report supplies the active-turn scenario. I did not execute the untrusted contributor branch locally. Is this the best way to solve the issue? Yes. Reusing the existing global admission fence and active-work inventory while retaining restart outcomes at the channel-manager owner is the narrowest coherent repair. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 5d8ef653cd71. LabelsLabel justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (10 earlier review cycles; latest 8 shown)
|
|
@clawsweeper re-review Updated the durable PR body for exact head |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
|
@clawsweeper re-review Final head The prior P2 remains addressed with typed start outcomes: disabled/unconfigured/manual/suppressed/unsupported accounts are intentional skips, while only transient pre-handoff ownership conflicts retain a thaw target. The PR body now contains fresh redacted exact-head isolated-Gateway traces for both requested cases:
Integrated-head verification: 198 focused tests passed; |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
|
@clawsweeper re-review Exact head: Addressed the P1 removed-account security-boundary finding: deferred thaw recovery now requires the account to remain present in the current runtime snapshot both before and after its awaited stop, and discards removed targets instead of explicitly starting an obsolete account ID. Added a focused removed-target regression. Current-head verification: 199 focused tests, full |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
obviyus
left a comment
There was a problem hiding this comment.
Maintainer acceptance: this exact admission-fenced, ChannelManager-owned targeted-retry flow is the permanent canonical recovery policy for long timer gaps. The +231 net production-line growth is justified because it keeps admission, active-work inspection, account handoff, and retry state in one owner path without protocol, configuration, or persistence changes.
|
Thanks @PollyBot13! Proof: The same isolated built-Gateway scenario ran on current main and this exact head. A real tool-using turn stayed active across an 80.5-second process pause. Main restarted the QA channel 236 ms before the turn ended. This head kept the channel generation unchanged, completed and persisted the reply, then reconnected on the next idle maintenance tick.
Example
|
…w#131966) Prevent timer-only Gateway stalls from restarting channel connections while replies or agent startup work remains active. Recovery now closes admission, defers reconnect until the canonical active-work inventory is idle, and retries only incomplete account handoffs. Co-authored-by: PollyBot13 <[email protected]> Co-authored-by: Ayaan Zaidi <[email protected]>
Closes #131046
AI-assisted by Codex.
What Problem This Solves
Fixes an issue where a long Gateway event-loop stall could be mistaken for a host sleep/wake cycle and immediately restart channel connections while a reply or agent startup was still active.
Why This Change Was Made
Timer-gap recovery still reconnects channels for real sleep, VM pause,
SIGSTOP, and other unmanaged host pauses. Before doing that disruptive step, it now atomically closes new Gateway work admission and checks the existing active-work inventory. If tracked work is still active, only the reconnect stays pending and retries on a later maintenance tick; event-loop reset, health refresh, and presence refresh remain single-shot for that timing-gap episode. Once idle, the same admission fence remains closed until channel recovery completes.Inspection failures reopen admission. Stop or startup-handoff failures retain only the affected account targets, so successful siblings are not restarted again by a deferred retry. A newly detected thaw always merges a fresh running-account snapshot with older failures. Startup success comes from the channel manager's typed task-handoff result: transient pre-handoff ownership conflicts remain retryable, while disabled, unconfigured, manually stopped, suppressed, unsupported, or removed accounts complete as intentional skips. Deferred recovery rechecks the current account snapshot both before and after its awaited stop, so hot-reload removal cannot accidentally start an obsolete account ID against root channel configuration. Failures after handoff remain owned by the existing restart supervisor. Closed recovery outcomes keep active-work deferral, closed admission, and incomplete account recovery distinct in logs.
This preserves the unmanaged-sleep recovery contract without adding a platform wake API, config setting, protocol change, or dependency.
User Impact
An active reply or agent startup is no longer aborted merely because the Gateway event loop was busy. Real host pauses still reconnect stale channel sockets as soon as the Gateway is quiescent. Transient account-level failures remain eligible for targeted retry, while accounts intentionally disabled or made unconfigured during recovery do not enter an indefinite retry loop.
Evidence
62c311937af0eacb13179fbf97179f771e0b0962(currentmainintegrated after its CI shard split).pnpm exec vitest run src/gateway/host-thaw-recovery.test.ts src/gateway/server-maintenance.test.ts src/gateway/server-channels.test.ts src/infra/gateway-suspend-coordinator.test.ts— 199 passed on the current head.pnpm check:changed --base upstream/main -- <12 changed files>passed formatting, lint, production/test typechecking, boundary, dependency, database, import-cycle, dead-code, and repository guards on the current head.pnpm buildpassed on the current head.OPENCLAW_BUILD_PRIVATE_QA=1 pnpm buildalso passed to stage the repository's private isolated-Gateway QA runtime.ChannelAccountStartOutcome; only transient ownership conflicts retain a thaw target. Its subsequent P1 security-boundary finding is addressed by discarding targets absent from the current account snapshot before and after the recovery stop. Focused regressions cover disabled, unconfigured, and removed accounts.Exact-head isolated-Gateway trace: active to idle
Real built Gateway child,
qa-channel, and mock provider on62c311937af0eacb13179fbf97179f771e0b0962:SIGSTOP/SIGCONTpause: 80,001 ms;host thaw channel restart deferred: gateway still has active work;agent.waittimed out while recovery was pending, then completedok; the reply persisted inchat.history;lastStartAtstayed exactly1787962898784during active recovery;lastStartAtto1787963075075.Exact-head isolated-Gateway trace: intentional skip
A second real-Gateway run held a turn active across an 80,002 ms pause, queued thaw recovery, then changed
channels.qa-channel.enabledtofalsewhile channel reload was waiting for the turn to become idle:qa-channel/defaultsettled atenabled=false,running=false,lastStartAt=1787962898784;lastStartAtunchanged;host-thaw restart failedorone or more channel accounts remain pendinglines appeared.No real external channel account was used. The isolated QA transport exercises the production channel-manager and Gateway maintenance boundaries without credentials or network delivery.
Owner acceptance card
Maintainer live red/green proof (2026-08-29)
The same isolated built-Gateway contract ran against frozen current
mainand this exact head. Each run started the QA channel and mock provider, admitted a real tool-using turn, confirmed the provider request, paused the Gateway process for 80.5 seconds withSIGSTOP, resumed it withSIGCONT, then checked channel generation and persisted chat history.mainfeb1161a1e6456050bd495452aaf8a9409053296was red: channellastStartAtadvanced during recovery 236 ms before the active turn reached its terminal timestamp.62c311937af0eacb13179fbf97179f771e0b0962was green:lastStartAtstayed unchanged after resume, the Gateway loggedhost thaw channel restart deferred: gateway still has active work, the turn completed and persisted its unique marker, then the next idle maintenance tick advancedlastStartAt.exectool completed,agent.waitreturnedok, andchat.historycontained the revision-specific final marker.Maintainer acceptance of lifecycle growth
Maintainers accept the justified +231 net production-line growth. The new state belongs to the single canonical thaw-recovery flow: Gateway maintenance owns the atomic admission fence and active-work snapshot, while the ChannelManager owns typed account handoff and targeted retry outcomes. This avoids a parallel recovery path and keeps protocol, configuration, and persistence surfaces unchanged.
Rank-up move disposition
Skipped the requested rebase and duplicate live proof. The PR is
CLEAN; the landing workflow merges BEHIND heads and rebases only CONFLICTING heads. Freshorigin/mainat116425e9b3044c88c20c8727360663b6ce9eeed9changed none of the Gateway maintenance, thaw recovery, channel manager, startup wiring, active-work, admission, or recovery-doc files since the proven main SHA. The exact PR patch is unchanged, its live proof is under 24 hours old, and exact-head CI is green.