Repository navigation
fix(gateway): let a supervisor keep stopped work from resuming at the next start - #201060
Conversation
…eration A graceful shutdown marks running sessions and the next start resumes them. A supervisor that stops a deployment on purpose (an operator stop) and later starts a new one cannot tell the next Gateway that the interrupted work was stopped, so the stopped turn runs again with a synthetic resume message. Add OPENCLAW_GATEWAY_RESTART_RESUME_SCOPE. Shutdown stamps restart markers with the current scope; startup resumes only markers written under the same scope. A marker from another scope, or an unscoped marker when a scope is set, is cleared and its queued inputs are settled like a crash's orphans. Without the variable nothing changes.
…ame in the budget
… boundary; format
…after clearing it The worker withdrawal keeps inputs listed in the session's current marker, so the orphaned-input settlement that ran before the marker was cleared left them queued. Settle them once the marker is gone, and drop the discovery filter that could not reach them.
…pe helper Keeps session-restart-resume.ts within the max-lines budget: the helper returns the pending-input target of an out-of-scope marker, and the existing orphaned-input settlement cancels its queued inputs after the marker is cleared.
…esume-scope Main (#200533) moved accepted queued input out of the restart marker into its own durable row and replays queued rows at startup after root resume, removing the marker's queuedInputIds and the orphaned-input interrupt. Conflict resolution: - types.ts: keep main's marker shape (operatorSource only) plus `scope`. - session-run-lifecycle-projection.ts, session-restart-resume.ts: stamp main's marker shape with createRestartResumeMarker. - An out-of-scope marker is still cleared without resuming. Its session's queued rows now end with `restart-unrecoverable` through the same startup replay owner (new `endCause` option of continueSessionQueuedInputs) instead of being replayed, so the sender sees that the Gateway restarted before the message ran. - The scope helper drops the pending-input target helper and exposes isRestartResumeMarkerOutOfScope. - The scope table test now expects the slimmer marker and checks that in-scope startup replays the queued row while out-of-scope startup ends it without a replay attempt.
…sor token The scope covers shutdown markers only; crash recovery of unmarked runs and of queued input does not consult it. Queued input behind an out-of-scope marker ends without running.
…store discovery Merging main left session-restart-resume.ts at 720 counted lines, over the 700-line cap. The marker selection (shutdown marker, parked yield, run orphaned in a crash window, legacy cycle) and its scope check move into resolveStartupRestartMarker; behavior is unchanged.
…ope helpers module-private The production unused-export scan flagged the env name and resolver, which only the test imported.
|
🦞👀 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. |
…nventory session-run-lifecycle-projection.ts, which the trusted-anchor PR wrapper extracts, now imports it (test/scripts/eager-import-closure.test.ts).
…listed module The session-identity ratchet allows no identity comparisons in the restart discovery module. Compare the selected run with the startup snapshot in session-restart-resume.ts, as on main, and pass the result to resolveStartupRestartMarker.
|
Codex review: needs maintainer review before merge. What this changesAdds an optional supervisor token that prevents graceful restart continuation and queued-message replay when the token changes. Example: A Gateway stops during
Review scores
ProductKind: Feature · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review This PR provides useful supervisor control over graceful restart recovery, with no concrete introduced defect found; current-head CI remains pending. Priority: P2 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherGateway shutdown records live sessions in persisted restart markers; startup consumes those markers and durable queued inputs through the existing recovery and authorization owners. flowchart TD
A[Supervisor token] --> B[Shutdown marker writers]
B --> C[Persisted session marker]
C --> D[Startup scope comparison]
D --> E[Normal authorized continuation]
D --> F[Clear mismatched marker]
F --> G[Record queued input ending]
Technical reviewBest possible solution: Retain the optional scope on the existing restart marker and settle mismatched queued inputs through the existing pending-input owner. Do we have a high-confidence way to reproduce the issue? Current-main source consumes graceful shutdown markers without a supervisor token comparison, so an intentional process stop followed by startup enters ordinary continuation; the PR supplies live before/after recovery traces, but this review ran no target code. Is this the best way to solve the issue? The change addresses the documented graceful-stop scenario at the existing recovery boundary, shares marker creation between both writers, and reuses queued-input settlement without adding a competing recovery owner. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 76d63b8aaf45. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +90, Tests +113, Docs +13, Other +2. Total +218 across 11 files. Added test files: 0. View PR surface stats
Preflight sourceSource unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified. Review metrics
LabelsLabel 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 (3 earlier review cycles)
Reviewed November 18, 2026, 5:01 AM ET / 10:01 UTC (Revision 4). |
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
Staging route 1 for openclaw#129388 (staging only; no PR ref move). Upstream main b894fe5 (fetched 2026-11-18T19:00Z), merge base 9122a49 (the chain's own absorb). 42 conflicted paths, all resolved here; rerere replayed none. Agent schema: upstream 703c82d (openclaw#200533) owns 43 (pending-input endings) and 44 is free in this tree, so continuation's recipient authority moves 43 -> 44: - SQLite owner runs upstream's 43 step, then the idempotent authority step; - PostgreSQL ladder: 42 tool usage, 43 endings (from V42), 44 authority (from V43); - backup scopes 43 session_pending_inputs (upstream), 44 session_recipient_authority; - port descriptor pin 44, historical 27-43; witness admits upstream's 43; - the fleet bridge classifies 38-43; it admits only upstream's own 43 there, so the never-deployed chain 43 (upstream 42 + authority) is refused before any write; - jump-14 42 (the fleet on ab5a30e) now runs upstream's 42 and 43, then authority; - package schemaVersions.agent 44; agent-schema-history doc rows 42/43/44 and the authority section (the chain had left it at 42). One owner where upstream now owns a fork extraction: - session-restart-resume: upstream's resolveStartupRestartMarker (openclaw#201060) replaces the fork's session-restart-resume-marker.ts (deleted); the fork's tombstone early return stays ahead of upstream's interrupted-settle check. - user-turn-transcript: upstream's applyUserTurnReplacementText replaces the fork's byte-identical applyPersistedUserTurnReplacementText (deleted). - migration witness: upstream's agentSchemaVersions (+43) replaces the fork's AGENT_WITNESS_SCHEMA_VERSIONS. - session-event delivery: the fork's #1447 cron failure-copy withholding moves into upstream's createSessionEventReplyDelivery; session-event-handoff equals upstream. Other resolutions keep both sides (import unions checked against real uses; upstream openclaw#200533 marker shape `{}`, end causes, lifecycleRestartScope; fork restart tombstones and operator recovery). attempt.cwd-split.test.ts follows upstream's deletion (openclaw#198916); its one fork case moves to attempt.continuation-tools.test.ts. Generated outputs regenerated (config docs, gateway validators, postgres schema, worker inventory). Compile follow-through: compactionTarget.storePath (openclaw#200714) and two fork test cleanups pass upstream's new finishPendingInput end cause. Refs: openclaw#129388 Co-Authored-By: Claude Opus 5.5 <[email protected]>
… next start (openclaw#201060) * fix(gateway): scope restart resume to the supervisor's deployment generation A graceful shutdown marks running sessions and the next start resumes them. A supervisor that stops a deployment on purpose (an operator stop) and later starts a new one cannot tell the next Gateway that the interrupted work was stopped, so the stopped turn runs again with a synthetic resume message. Add OPENCLAW_GATEWAY_RESTART_RESUME_SCOPE. Shutdown stamps restart markers with the current scope; startup resumes only markers written under the same scope. A marker from another scope, or an unscoped marker when a scope is set, is cleared and its queued inputs are settled like a crash's orphans. Without the variable nothing changes. * docs(gateway): document the restart resume scope and record its env name in the budget * test(gateway): settle out-of-scope queued input across a real process boundary; format * fix(gateway): cancel queued inputs of an out-of-scope restart marker after clearing it The worker withdrawal keeps inputs listed in the session's current marker, so the orphaned-input settlement that ran before the marker was cleared left them queued. Settle them once the marker is gone, and drop the discovery filter that could not reach them. * refactor(gateway): resolve the out-of-scope restart target in the scope helper Keeps session-restart-resume.ts within the max-lines budget: the helper returns the pending-input target of an out-of-scope marker, and the existing orphaned-input settlement cancels its queued inputs after the marker is cleared. * docs(gateway): describe the restart resume scope as an opaque supervisor token The scope covers shutdown markers only; crash recovery of unmarked runs and of queued input does not consult it. Queued input behind an out-of-scope marker ends without running. * refactor(gateway): select the startup restart marker next to restart store discovery Merging main left session-restart-resume.ts at 720 counted lines, over the 700-line cap. The marker selection (shutdown marker, parked yield, run orphaned in a crash window, legacy cycle) and its scope check move into resolveStartupRestartMarker; behavior is unchanged. * test(gateway): stub the restart resume scope with vi.stubEnv; keep scope helpers module-private The production unused-export scan flagged the env name and resolver, which only the test imported. * style(sessions): format the restart resume scope resolver * fix(scripts): add the restart resume scope module to the PR wrapper inventory session-run-lifecycle-projection.ts, which the trusted-anchor PR wrapper extracts, now imports it (test/scripts/eager-import-closure.test.ts). * fix(gateway): keep the run comparison of restart startup in its allowlisted module The session-identity ratchet allows no identity comparisons in the restart discovery module. Compare the selected run with the startup snapshot in session-restart-resume.ts, as on main, and pass the result to resolveStartupRestartMarker.
What Problem This Solves
A graceful Gateway shutdown marks the sessions that were working, and the next start resumes them with the hidden "The Gateway restarted while you were working" input. That is right for a restart, but a supervisor that stops a Gateway on purpose and later starts a new one has no way to say the interrupted work was stopped, not restarted. The stopped turn then runs again with no user message. In one managed deployment, an operator stopped an agent part-way through a long shell command; the next start resumed the turn and the agent ran the command again to completion.
User Impact
A supervisor can set
OPENCLAW_GATEWAY_RESTART_RESUME_SCOPEto an opaque token of its choice. Work interrupted under one token resumes only when the next Gateway starts with the same token. When the supervisor starts the next Gateway with a different token, the interrupted turn does not run again, and a message that was queued behind it ends with the existing "The Gateway restarted before this message ran. Send it again." notice instead of running. Without the variable nothing changes.Not covered: the scope applies to graceful-shutdown markers only. Crash recovery (a run left open by a crash or
SIGKILL, and queued input replayed after a crash) does not consult it; that path has no marker to carry a scope, and recording one would mean a schema change. The docs say so.The env-var budget goes from 461 to 462 for the new name (
config/env-var-count-budget.txt).Why This Change Was Made
resumeAfterRestart.scope), including the fallback marker written when shutdown cancels a run.scope-changed.restart-unrecoverablethrough the existing startup replay owner (continueSessionQueuedInputs) instead of being replayed, so it is visible to its sender and never runs after the work it was queued behind was ended.resolveStartupRestartMarkernext to restart store discovery, together with the scope check, sosession-restart-resume.tsstays under the 700-line cap after this change. The run comparison with the startup snapshot stays insession-restart-resume.tsand is passed in, so the session-identity allowlist is unchanged.Evidence
Behavior checked: a graceful shutdown followed by a start under the same token resumes the interrupted run or turn; a start under a different token, or a token added or removed between the two, does not resume, clears the marker, and ends the queued message as
restart-unrecoverablewithout a replay attempt; with no token on either side, behavior is unchanged.src/gateway/session-restart-resume.test.ts: 29/29 pass, including 8 new scope cases (a live agent run and a live session turn, each with a queued message, through the real shutdown marker write and startup recovery).endCausepass-through removed, the 4 out-of-scope cases fail because startup replays the queued message.session-restart-resume.{admission,upgrade,workspace,yield}.test.ts,session-restart-resume-custody.test.ts,queued-chat-inputs.metadata.test.ts,server-close.queued-inputs.test.ts(44/44).scopeis an optional field on the existing marker, with no migration. Markers written before this change carry no scope and resume as before when no token is set (the unscoped cases above);session-restart-resume.upgrade.test.ts(prior-release serialized records) passes unchanged. A marker written without a token is not resumed by a Gateway that starts with one, as documented.test/scripts/eager-import-closure.test.ts(PR wrapper inventory) andpnpm check:session-identity-ratchetpass.pnpm tsgo:coreand the core test typecheck shards, oxlint and oxfmt on the touched files,format:docs:check,docs:check-links,docs:check-mdx, and the test-lines ratchet (+120 of 400) pass.pnpm check:env-var-count: 462 names, equal to the new budget; the 461 to 462 growth is reported as a warning in Actions and needs maintainer approval.Real-behaviour proof: isolated Gateway, real model, real stop and start
These runs used a Gateway built from this head (
3cf718806fb,pnpm build,openclaw --versionprintsOpenClaw 2026.11.5 (3cf7188)) and the released[email protected]from npm, on a Linux x86-64 test machine. Each scenario had its own state directory. The Gateway listened on loopback with token auth and no channels. The agent's model was a real model served from a self-hosted model endpoint. Every turn below is a real model turn, and the exec calls are real tool calls that ransleep. Nothing was scripted or mocked. The Gateway ran under systemd, and "graceful stop" meanssystemctl stop, which sends SIGTERM to the Gateway process. The driver wasopenclaw gateway call chat.send/chat.historyfrom the same build, plus a read-only look at the agent database.Each scenario followed the same steps:
chat.historylists it underpendingInputswith statequeued.chat.historyand the database.aaresumed:1QUEUED-a3)abresumed:0 skipped:1 scope-changed:1cancelled,endCause: restart-unrecoverable; a fresh message afterwards is answeredresumed:1resumed:1xresumed:0 skipped:1 scope-changed:1cancelled,restart-unrecoverable; settings and session intact, and the old session still chatsAll of these match the description above and the docs. One thing worth knowing: a
/followup <text>queued from the token-authenticated CLI is stored without a recovery source. It endsrestart-unrecoverableeven within the same scope ("Queued input has no original operator source; submit a new request."). That is existing behaviour for input without a recovery source, and this PR does not change it. The runs above use a plain message, which carries a recovery source (origin_source_jsonset).A: same token (
athena)B: changed token (
athenb)C: no token on either side (control)
D: upgrade from v2026.11.5 (state and a mid-turn marker written by the release)
A marker written by v2026.11.5 has no
scope. Without a token it resumes, and its queued input replays, as before. When the first start after the upgrade sets a token, the marker counts as another scope's: the turn does not continue and its queued input endsrestart-unrecoverable. This matches the docs line "When a token is set, a marker without one counts as another scope's".Output above is trimmed to the relevant lines. Session keys are shortened (the message tags a3, b1, c1 and d are the real ones), and host, path and model-endpoint details are removed.