Skip to content

fix(gateway): avoid failed worker cleanup blocking startup - #128103

Merged
steipete merged 4 commits into
mainfrom
steipete/fix-stale-worker-startup-replay
Aug 23, 2026
Merged

steipete merged 4 commits into
mainfrom
steipete/fix-stale-worker-startup-replay

Conversation

@steipete

@steipete steipete commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes an issue where a Gateway restart could remain unready while retrying cleanup for an already-failed cloud worker. If provider allocation had an indeterminate result, startup synchronously replayed the durable operation and waited for teardown even though that failed placement no longer held serving authority.

Why This Change Was Made

Startup now waits only for nonterminal placement authority that must be recovered or fenced before serving. Failed-placement cleanup keeps its existing provider lease-adoption safety, but begins immediately after readiness through the existing tracked reconciliation owner and is still drained during shutdown.

The guard permits this deferred cleanup only for one exact failed placement with no turn claim, no active owner epoch, and durable destruction intent. Ambiguous or authority-retaining states remain fenced.

AI-assisted; the implementation was independently reviewed and the final rebased patch was unchanged from the reviewed diff.

User Impact

Gateway restarts no longer remain unavailable while a terminal cloud-worker cleanup waits on a slow provider. Potentially live paid leases are still adopted and destroyed rather than being hidden or abandoned.

Evidence

  • Regression before the fix: the startup cleanup test observed provider replay before readiness and failed with expected 'provision' to be 'ready'.
  • Boundary regression after the fix: readiness completes first, the exact provider replay starts through tracked post-readiness reconciliation, shutdown waits for it, and the adopted lease is destroyed.
  • Focused suite after rebase: 26 test-file executions, 418 tests passed.
  • Production-boundary provider spies prove failed placements retaining a local claim or active owner epoch cannot invoke provisioning, inspection, or destruction during startup.
  • Absent-session retirement starts immediately after readiness in the existing tracked reconciliation slot; shutdown drains it while active placement recovery remains periodic.
  • The failed CI shard exposed a duplicate immediate placement sweep; the correction removes that duplicate owner and preserves the established periodic recovery/event cadence.
  • git diff --check origin/main...HEAD passed.
  • Targeted oxlint and oxfmt checks passed for all changed files.
  • Autoreview ran on both the working diff and final rebased branch; no accepted/actionable blocker findings.
  • Production delta: +28/-7 lines. The +21 net lines define the internal startup authority/cleanup boundary; provider, protocol, schema, config, and public SDK contracts are unchanged.

Real provider-transport proof

At exact head e3278e24860cc5b831b85e5e3c44b4274c29d583, I ran a local, secretless provider-transport proof twice using the actual bundled Crabbox plugin through its public top-level entry, the production runCommandWithTimeout OS-process path, the real SQLite-backed worker environment service/store, and the real Gateway placement startup runtime. Both runs passed and produced byte-identical sanitized artifacts (SHA-256 7a94cf634455579395c0c78b1fa5d952738336c14f4f5501cc858fea06f671f1).

  • Failed placement retaining a local turn claim: gateway-ready -> immediate-reconcile-complete; zero Crabbox warmup/inspect/run/stop process invocations.
  • Failed placement retaining an active owner epoch: gateway-ready -> immediate-reconcile-complete; zero Crabbox process invocations.
  • Authority-free failed placement: readiness completed first, then the actual provider subprocess sequence was warmup -> inspect -> run (node enrollment via stdin) -> inspect -> stop. sidecar.stop() remained pending while warmup was held and completed only after provider stop; the SQLite environment finished destroyed with the adopted fixed lease.

The proof used a deterministic local fake Crabbox executable as the process transport peer; it created no network or paid cloud lease. Temporary paths, IDs, hostnames, and credentials were redacted. Runs completed as 6/6 tests in 18.95s and 17.48s.

@clawsweeper

clawsweeper Bot commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@steipete
steipete marked this pull request as ready for review August 23, 2026 04:01
@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: M maintainer Maintainer-authored PR labels Aug 23, 2026
@clawsweeper clawsweeper Bot added P1 High-priority user-facing bug, regression, or broken workflow. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Aug 23, 2026
@clawsweeper

clawsweeper Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 23, 2026, 8:23 AM ET / 12:23 UTC.

ClawSweeper review

What this changes

The PR makes Gateway readiness wait for placements that retain worker authority, while terminal failed-worker cleanup continues through the tracked post-readiness environment reconciliation path.

Merge readiness

⚠️ Ready for maintainer review - 3 items remain

No blocking correctness finding. The PR’s authority-free cleanup split is coherent and its new transport proof addresses the prior final-effect gap; it remains open for explicit maintainer handling because it carries the protected maintainer label.

Priority: P1
Reviewed head: e3278e24860cc5b831b85e5e3c44b4274c29d583
Owner decision: Required. See Decision needed.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) The patch has a coherent owner-boundary repair, focused regression coverage, and sufficiently specific real transport proof.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (terminal): The exact-head provider-transport trace demonstrates retained-authority rejection before process I/O and permitted post-ready adoption, teardown, and shutdown drain through the real bundled transport client.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (terminal): The exact-head provider-transport trace demonstrates retained-authority rejection before process I/O and permitted post-ready adoption, teardown, and shutdown drain through the real bundled transport client.
Evidence reviewed 7 items Current-main blocking path: Current main awaits the full placement reconciliation before readiness; that reconciliation begins with the environment-wide sweep and then retries failed-placement teardown.
Startup-only owner boundary: The PR reconciles nonterminal placements individually at startup, skips failed placement teardown there, and leaves ordinary sweeps to retry it after readiness.
Final-effect authority gate: The reconcile guard admits a failed placement only when it has no turn claim or active owner epoch and durable destruction was requested; otherwise it rejects before the environment reconciliation core can reach the provider.
Findings None None.
Security None None.

Live Verification

Command: node scripts/run-vitest.mjs src/gateway/server-worker-placement-startup-cleanup.test.ts

Result: FAIL (partial) — step 2 expect_output worker placement startup cleanup ownership: expected terminal output was not visible within 30 seconds: "worker placement startup cleanup ownership"

node scripts/run-vitest.mjs src/gateway/server-worker-placement-startup-cleanup.test.ts
runner@runnervm76f27:/tmp/clawsweeper-live-proof-128103-OVv8cv/target$ node scripts/run-vitest.mjs src/gateway/server-worker-placement-startup-cleanup.test.ts
node scripts/run-vitest.mjs src/gateway/server-worker-placement-startup-cleanup.test.ts
[test] starting test/vitest/vitest.gateway.config.ts

 RUN  v4.1.10 /tmp/clawsweeper-live-proof-128103-OVv8cv/target

 ✓  gateway-server-isolated  ../../src/gateway/server-worker-placement-startup-cleanup.test.ts (3 tests) 939ms
     ✓ never reaches worker providers during startup while a failed placement retains 'a local turn claim'  496ms
 ✓  gateway-server  ../../src/gateway/server-worker-placement-startup-cleanup.test.ts (3 tests) 1066ms
     ✓ never reaches worker providers during startup while a failed placement retains 'a local turn claim'  407ms
     ✓ never reaches worker providers during startup while a failed placement retains 'an active owner epoch'  449ms

 Test Files  2 passed (2)
      Tests  6 passed (6)
   Start at  12:23:56
   Duration  27.28s (transform 20.37s, setup 785ms, import 24.17s, tests 2.01s, environment 0ms)

[test] passed 1 Vitest shard in 31.93s
runner@runnervm76f27:/tmp/clawsweeper-live-proof-128103-OVv8cv/target$ node scripts/run-vitest.mjs src/gateway/server-worker-placement-startup-cleanup.test.ts































Assertions:

  • FAIL expect_output: worker placement startup cleanup ownership

How this fits together

Gateway startup reconciles persisted worker placements before serving requests. Worker-environment reconciliation can invoke the Crabbox provider process transport, and shutdown drains outstanding cleanup before stopping environment services.

flowchart LR
  A[Persisted worker placements] --> B[Gateway startup]
  B --> C{Placement retains authority?}
  C -->|Yes| D[Recover or fence before ready]
  C -->|No, terminal failure| E[Post-ready environment reconciliation]
  D --> F[Gateway ready]
  F --> E
  E --> G[Provider process transport]
  G --> H[Shutdown drain]
Loading

Decision needed

Question Recommendation
Should this maintainer-labelled Gateway lifecycle change be merged once checks for its exact final head are green? Merge after exact-head checks: Accept the demonstrated startup boundary and merge once the currently resolved head has green required checks.

Why: The patch changes when provider-facing teardown can run relative to durable placement authority, and the protected maintainer label requires an explicit human merge decision.

Before merge

  • Resolve merge risk (P1) - The +21 net production-line bug fix deliberately creates separate startup and periodic reconciliation paths; preserve the demonstrated authority and shutdown-drain checks after any base refresh.
  • Resolve merge risk (P1) - The branch is behind current main, so the maintainer should rely on checks for the exact final head; current-main comparison found no changes to the affected worker-placement paths.
Agent review details

Security

None.

PR surface

Source +24, Tests +462. Total +486 across 11 files.

View PR surface stats
Area Files Added Removed Net
Source 6 31 7 +24
Tests 5 498 36 +462
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 11 529 43 +486

Review metrics

Metric Value Why it matters
Production versus test LOC production +28/-7 (net +21); tests +501/-36 (net +465) The production growth defines the startup authority boundary, while the larger test delta covers readiness ordering, retained-authority fencing, and shutdown drain behavior.

Stored data model

Persistent data-model change detected: durable storage schema: src/gateway/worker-environments/placement-dispatch-recovery.ts, durable storage schema: src/gateway/worker-environments/placement-dispatch-test-harness.ts, durable storage schema: src/gateway/worker-environments/worker-turn-launcher-failure-recovery.test.ts. Confirm migration or upgrade compatibility proof before merge.

Merge-risk options

Maintainer options:

  1. Merge with the demonstrated boundary (recommended)
    Retain the startup-only reconciliation mode and guard because the submitted proof covers both retained-authority rejection and authority-free post-ready teardown.

Technical review

Best possible solution:

Keep the explicit startup-only mode: recover or fence placements that retain authority before readiness, and let the environment-service owner perform authority-free terminal teardown after readiness with shutdown draining.

Do we have a high-confidence way to reproduce the issue?

Yes: current main awaits the environment-wide reconciliation and failed teardown before readiness, while the PR’s focused regression describes that same pre-fix ordering.

Is this the best way to solve the issue?

Yes: reusing the environment-service reconciliation owner after readiness avoids both the blocking full startup sweep and a duplicate cleanup owner, while retaining fencing for nonterminal authority.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 7d816a2a065b.

Labels

Label changes:

  • add proof: sufficient: Contributor real behavior proof is sufficient. The exact-head provider-transport trace demonstrates retained-authority rejection before process I/O and permitted post-ready adoption, teardown, and shutdown drain through the real bundled transport client.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The exact-head provider-transport trace demonstrates retained-authority rejection before process I/O and permitted post-ready adoption, teardown, and shutdown drain through the real bundled transport client.
  • remove rating: 🦪 silver shellfish: Current PR rating is rating: 🐚 platinum hermit, so this older rating label is no longer current.
  • remove status: 📣 needs proof: Current PR status label is status: 👀 ready for maintainer look.

Label justifications:

  • P1: The patch repairs Gateway readiness being delayed by failed worker cleanup.
  • merge-risk: 🚨 session-state: It changes reconciliation timing for persisted worker placements and session retirement.
  • merge-risk: 🚨 security-boundary: It changes the authority conditions that permit provider-facing reconciliation and teardown.
  • merge-risk: 🚨 availability: It changes work that gates Gateway startup readiness.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The exact-head provider-transport trace demonstrates retained-authority rejection before process I/O and permitted post-ready adoption, teardown, and shutdown drain through the real bundled transport client.
  • proof: sufficient: Contributor real behavior proof is sufficient. The exact-head provider-transport trace demonstrates retained-authority rejection before process I/O and permitted post-ready adoption, teardown, and shutdown drain through the real bundled transport client.

Evidence

What I checked:

  • Current-main blocking path: Current main awaits the full placement reconciliation before readiness; that reconciliation begins with the environment-wide sweep and then retries failed-placement teardown. (src/gateway/server-worker-placement-startup.ts:626, 7d816a2a065b)
  • Startup-only owner boundary: The PR reconciles nonterminal placements individually at startup, skips failed placement teardown there, and leaves ordinary sweeps to retry it after readiness. (src/gateway/worker-environments/placement-dispatch-recovery.ts:124, e3278e24860c)
  • Final-effect authority gate: The reconcile guard admits a failed placement only when it has no turn claim or active owner epoch and durable destruction was requested; otherwise it rejects before the environment reconciliation core can reach the provider. (src/gateway/server-worker-placement-reconcile-guard.ts:28, e3278e24860c)
  • Provider transport contract: The bundled Crabbox provider uses the public process runtime, executes warmup and inspect during provision, and executes stop during destroy; this supports the submitted process-sequence proof as a real transport-client boundary. (extensions/crabbox/src/crabbox-worker-provider.ts:410, e3278e24860c)
  • After-fix real behavior proof: The PR body reports two secretless exact-head runs through the bundled Crabbox provider and production process transport: retained claim/epoch cases made zero provider calls, while the authority-free case reached readiness before warmup, inspect, run, inspect, and stop, with shutdown draining to destroyed state. (e3278e24860c)
  • Current-main comparison: Current main advanced after the PR base, but the comparison found no changes in the affected startup, recovery, guard, or environment-service paths. (src/gateway/server-worker-placement-startup.ts, 7d816a2a065b)

Likely related people:

  • steipete: History shows repeated ownership of the Gateway worker-placement lifecycle, including the prior cloud-worker startup recovery and this exact-head repair. (role: feature owner and recent contributor; confidence: high; commits: 755dad74d1fc, e3278e24860c; files: src/gateway/server-worker-placement-startup.ts, src/gateway/worker-environments/placement-dispatch-recovery.ts, src/gateway/server-worker-placement-reconcile-guard.ts)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (6 earlier review cycles)
  • reviewed 2026-08-23T04:07:26.045Z sha 6bdda07 :: needs maintainer review before merge. :: none
  • reviewed 2026-08-23T07:00:03.387Z sha ba293e9 :: found issues before merge. :: [P1] Start session retirement immediately after readiness
  • reviewed 2026-08-23T07:18:53.593Z sha ba293e9 :: needs changes before merge. :: [P1] Start session retirement immediately after readiness
  • reviewed 2026-08-23T08:27:11.200Z sha 71f03cf :: needs real behavior proof before merge. :: none
  • reviewed 2026-08-23T09:44:26.929Z sha e3278e2 :: needs real behavior proof before merge. :: none
  • reviewed 2026-08-23T10:28:25.073Z sha e3278e2 :: needs real behavior proof before merge. :: none

@steipete

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

The head changed after the previous review because its only failed CI shard exposed a real duplicate reconciliation owner. The correction removes the new immediate full placement sweep; environment-owned failed cleanup still starts after readiness and remains shutdown-drained, while placement recovery/events keep their established periodic cadence. The corrected focused suite passes 414 tests and fresh autoreview is clean.

@clawsweeper

clawsweeper Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event exact_review_queue).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Re-review progress:

@clawsweeper clawsweeper Bot added merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Aug 23, 2026
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Aug 23, 2026
@steipete

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

Addressed the final-effect proof request on head e3278e24860cc5b831b85e5e3c44b4274c29d583. Startup now defers every terminal failed-placement teardown; normal and periodic reconciliation retain cleanup ownership after readiness. New production-boundary tests use the real placement store, worker environment service, and provider spies to prove failed placements retaining either a local claim or active owner epoch cannot invoke provider provisioning, inspection, or destruction during startup. The authority-free replay/adopt/destroy path and tracked session-retirement shutdown drain remain covered. The focused suite passes 418 tests and final autoreview is clean.

@clawsweeper

clawsweeper Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event exact_review_queue).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Re-review progress:

@clawsweeper clawsweeper Bot added merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. labels Aug 23, 2026
@steipete

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

Added the requested real provider-transport proof to the PR Evidence section. At exact head e3278e24860cc5b831b85e5e3c44b4274c29d583, the actual bundled Crabbox provider executed through its production OS-process transport against real SQLite-backed worker/Gateway startup state. Two deterministic runs produced byte-identical sanitized traces: retained local-claim and owner-epoch states made zero provider process calls, while the authority-free state reached readiness before the ordered warmup -> inspect -> run -> inspect -> stop transport sequence and shutdown drained it to a destroyed environment. No network or paid lease was created.

@clawsweeper

clawsweeper Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event exact_review_queue).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Re-review progress:

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Aug 23, 2026
@steipete
steipete merged commit 0661763 into main Aug 23, 2026
257 checks passed
@steipete
steipete deleted the steipete/fix-stale-worker-startup-replay branch August 23, 2026 13:06
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 24, 2026
…128103)

* fix(gateway): defer failed worker cleanup after readiness

* test(gateway): preserve periodic worker recovery ordering

* fix(gateway): track post-ready session retirement

* fix(gateway): defer failed teardown past readiness
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime maintainer Maintainer-authored PR merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: L status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant