Skip to content

fix(gateway): reclaim cloud workers before session recovery - #128597

Merged
steipete merged 3 commits into
mainfrom
steipete/fix-session-recovery-cloud-placement
Aug 24, 2026
Merged

steipete merged 3 commits into
mainfrom
steipete/fix-session-recovery-cloud-placement

Conversation

@steipete

@steipete steipete commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Closes #128596

What Problem This Solves

Fixes an issue where restart recovery could archive a source session and create its successor while the source still owned an active cloud worker. That could leave remote execution and final workspace changes attached to a session identity the Gateway had already retired.

Why This Change Was Made

Recovery now settles the source placement through the canonical archive/reclaim boundary before committing the SQLite source archive. Reclaim carries the runtime authority guard to each post-await workspace, tunnel, and environment effect; if authority closes before an effect, the placement stays retryable. Once environment destruction commits, durable local/reclaimed bookkeeping finishes even if authority closes during that await, avoiding a placement that points at a deleted environment. Recovery then re-reads the authoritative session, placement generation, owner epoch, environment, and lifecycle revision before the SQLite compare-and-swap.

Production LOC: +112/-38 (net +74) | Tests/test support: +597/-6 | Tooling baseline: +1/-1

The production growth establishes two coupled authority boundaries: recovery coordinates async placement reclaim outside the transaction, and the canonical reclaim owner fences every privileged final effect while preserving crash-safe completion after destruction. A downstream guard or post-archive cleanup cannot safely express either ordering.

AI-assisted: yes. I reviewed restart tombstones, session recovery ownership, placement reclaim/final synchronization, generation and owner-epoch checks, SQLite compare-and-swap inputs, archive lifecycle reuse, and failure guidance and understand the change.

User Impact

Recovering a cloud-backed session now either safely reclaims and synchronizes its worker before creating the successor, or fails visibly with instructions to stop/reclaim the worker and retry. Recovery no longer silently retires a session that still has a live remote owner.

Evidence

  • Pre-fix regression failed because recovery committed the source archive before placement reclaim.
  • Final post-rebase focused proof: 92 Gateway recovery/archive/reclaim tests and 3 SQLite recovery tests passed.
  • Full changed-file gate: node scripts/check-changed.mjs passed with no new temp-directory, max-lines, type, lint, architecture, or database warnings.
  • ClawSweeper’s P1 was accepted and fixed at the canonical reclaim owner: authority now revalidates before workspace reconciliation, result acceptance, environment teardown, and failed-placement teardown.
  • Crash-safe regressions prove revocation before destroy blocks the effect, while revocation during successful destroy still completes durable placement bookkeeping.
  • Fresh post-fix autoreview: no accepted/actionable P0/P1 findings (0.90).
  • Coverage also includes reclaim failure guidance, canonical persisted placement mismatch, generation/epoch/environment replacement, source lifecycle revision changes, idempotent already-recovered calls, archive reuse, and final workspace synchronization.

@clawsweeper

clawsweeper Bot commented Aug 24, 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 24, 2026 07:29
@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: L maintainer Maintainer-authored PR labels Aug 24, 2026
@clawsweeper clawsweeper Bot added merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Aug 24, 2026
@clawsweeper

clawsweeper Bot commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Codex review: found issues before merge. Reviewed August 24, 2026, 3:36 AM ET / 07:36 UTC.

ClawSweeper review

What this changes

The PR makes Gateway session recovery reclaim and verify an active cloud worker before atomically archiving the source session and creating its successor.

Merge readiness

⛔ Blocked by patch quality or review findings - 7 items remain

Keep open and block merge: the branch correctly adds reclaim-before-archive ordering, but a revoked runtime authority can still complete cloud-worker reconciliation and teardown before the recovery code notices the revocation.

Priority: P1
Reviewed head: 746cae3e20279aa470aa95e9c1c939d02acfca07

Review scores

Measure Result What it means
Overall readiness 🦪 silver shellfish (2/6) The patch has strong ordering coverage but retains a high-severity stale-authority path through worker teardown.
Proof confidence 🌊 off-meta tidepool Not applicable: Not applicable to this maintainer-authored PR; its focused tests are supplemental and do not clear the final-effect authority finding.
Patch quality 🦪 silver shellfish (2/6) Security review found an item that needs attention.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: Not applicable to this maintainer-authored PR; its focused tests are supplemental and do not clear the final-effect authority finding.
Evidence reviewed 6 items Changed recovery path: The PR calls the canonical placement-reclaim helper before the SQLite recovery commit, then checks the caller guard only after that awaited reclaim returns.
Final-effect gap: The production reclaim barrier authorizes before beginning drain, then awaits reclaim; it does not reauthorize before reclaim returns.
Remote side effect after awaits: Reclaim performs asynchronous workspace reconciliation and destroys the attached environment later in the sequence, after the single barrier authorization check.
Findings 1 actionable finding [P1] Revalidate authority at the reclaim final effect
Security Needs attention Revoked authority can complete worker teardown: The newly forwarded authorization callback is consumed before the asynchronous reclaim work; the real reclaim later reconciles workspace state and destroys the environment without a final revalidation.

How this fits together

Gateway recovery converts a restart-tombstoned session into a successor session. It must settle the attached cloud-worker placement first so remote execution, workspace synchronization, and SQLite session state retain one authoritative lifecycle owner.

flowchart LR
  A[Restart tombstone] --> B[Gateway recovery]
  B --> C[Read source session]
  C --> D[Reclaim cloud worker]
  D --> E[Revalidate authority and ownership]
  E --> F[Atomic SQLite recovery]
  F --> G[Successor session]
  G --> H[Recovery continuation]
Loading

Before merge

  • Revalidate authority at the reclaim final effect (P1) - Passing commitGuard here authorizes the reclaim barrier only before it starts draining; the production barrier then awaits the full reclaim, whose workspace synchronization and environment destruction occur later. If authority closes during that interval, this method rejects the SQLite archive only after the worker has already been reclaimed. Thread the guard to the reclaim final-effect fence and cover revocation after reclaim starts.
  • Resolve security concern: Revoked authority can complete worker teardown - The newly forwarded authorization callback is consumed before the asynchronous reclaim work; the real reclaim later reconciles workspace state and destroys the environment without a final revalidation.
  • Resolve merge risk (P1) - A runtime authority revoked during workspace reconciliation can still synchronize and destroy the cloud worker before recovery rejects the later SQLite archive.
  • Resolve merge risk (P1) - This bug fix adds net 51 production lines; the authorization fence should live at the canonical reclaim final-effect boundary rather than remain a recovery-specific partial guard.
  • Complete next step (P2) - A maintainer should require final-effect authority fencing before merge; this existing PR already owns the repair.
  • Improve patch quality - Thread the authority guard to the reclaim final-effect fences rather than checking it only before drain and after return.
  • Improve patch quality - Add production-barrier proof that revocation after reclaim starts prevents workspace and environment side effects.

Findings

  • [P1] Revalidate authority at the reclaim final effect — src/gateway/worker-environments/session-placement-lifecycle.ts:195-197
  • [high] Revoked authority can complete worker teardown — src/gateway/worker-environments/session-placement-lifecycle.ts:196
Agent review details

Security

Needs attention: The new authority handoff can let a revoked runtime identity complete cloud-worker side effects before the later archive guard runs.

PR surface

Source +51, Tests +452, Other 0. Total +503 across 8 files.

View PR surface stats
Area Files Added Removed Net
Source 4 79 28 +51
Tests 3 456 4 +452
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 1 1 1 0
Total 8 536 33 +503

Review metrics

Metric Value Why it matters
Production versus test LOC production +79/-28 (net +51); tests +456/-4; tooling baseline +1/-1 The added recovery boundary is substantial for a bug fix and should centralize its final authorization fence in the shared reclaim owner.

Stored data model

Persistent data-model change detected: durable storage schema: src/gateway/worker-environments/session-placement-lifecycle.ts, serialized state: src/config/sessions/session-accessor.recovery.test.ts, serialized state: src/config/sessions/session-accessor.sqlite-recovery.ts, serialized state: src/gateway/server-methods/sessions-recover.ts, serialized state: src/gateway/session-recovery-service.ts, unknown-data-model-change: src/gateway/session-recovery-service.ts. Confirm migration or upgrade compatibility proof before merge.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #128596
Summary: This PR is the explicit candidate repair for the reported active-cloud-worker recovery ordering bug.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge-risk options

Maintainer options:

  1. Fence reclaim final effects (recommended)
    Revalidate the live recovery authority immediately before the reclaim path performs workspace reconciliation, environment teardown, and placement completion, with a regression that revokes authority after reclaim starts.
  2. Hold this recovery change
    Do not merge while a revoked caller can still complete cloud-worker side effects during recovery.

Technical review

Best possible solution:

Thread the recovery authority guard through the reclaim final-effect fences, reject revocation before workspace or environment effects, and retain the existing pre-archive lifecycle and SQLite compare-and-swap checks.

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

Yes, from source: revoke the runtime guard after reclaim begins but before its workspace/environment completion; the current code checks it only after reclaim returns. This review did not execute the path because the checkout is read-only.

Is this the best way to solve the issue?

No. Reclaim-before-archive and the added SQLite lifecycle comparison are the right shape, but the authority check must guard final reclaim effects rather than only the later archive commit.

Full review comments:

  • [P1] Revalidate authority at the reclaim final effect — src/gateway/worker-environments/session-placement-lifecycle.ts:195-197
    Passing commitGuard here authorizes the reclaim barrier only before it starts draining; the production barrier then awaits the full reclaim, whose workspace synchronization and environment destruction occur later. If authority closes during that interval, this method rejects the SQLite archive only after the worker has already been reclaimed. Thread the guard to the reclaim final-effect fence and cover revocation after reclaim starts.
    Confidence: 0.96

Overall correctness: patch is incorrect
Overall confidence: 0.96

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 92a074944d97.

Labels

Label changes:

  • add P1: A recoverable cloud-backed session can otherwise act on a worker after its runtime authority has been revoked.
  • add merge-risk: 🚨 security-boundary: The new recovery-to-reclaim authority handoff can allow a revoked runtime identity to reach workspace and environment side effects.
  • add rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🌊 off-meta tidepool and patch quality is 🦪 silver shellfish.
  • add status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Not applicable: Not applicable to this maintainer-authored PR; its focused tests are supplemental and do not clear the final-effect authority finding.

Label justifications:

  • P1: A recoverable cloud-backed session can otherwise act on a worker after its runtime authority has been revoked.
  • merge-risk: 🚨 security-boundary: The new recovery-to-reclaim authority handoff can allow a revoked runtime identity to reach workspace and environment side effects.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🌊 off-meta tidepool and patch quality is 🦪 silver shellfish.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Not applicable: Not applicable to this maintainer-authored PR; its focused tests are supplemental and do not clear the final-effect authority finding.

Evidence

Security concerns:

  • [high] Revoked authority can complete worker teardown — src/gateway/worker-environments/session-placement-lifecycle.ts:196
    The newly forwarded authorization callback is consumed before the asynchronous reclaim work; the real reclaim later reconciles workspace state and destroys the environment without a final revalidation.
    Confidence: 0.96

What I checked:

Likely related people:

  • steipete: Authored the PR and multiple earlier cloud-placement recovery and archive-lifecycle changes in the same files. (role: recent area contributor; confidence: high; commits: 746cae3e2027, 411777dc1fcc, b4104e29b5ff; files: src/gateway/session-recovery-service.ts, src/gateway/worker-environments/session-placement-lifecycle.ts, src/gateway/server-worker-placement-reclaim.ts)
  • Josh Lehman: Local history identifies a prior restart-session recovery change on the recovery service. (role: prior restart-recovery contributor; confidence: medium; commits: bb5ef9872a70; files: src/gateway/session-recovery-service.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.

@steipete
steipete force-pushed the steipete/fix-session-recovery-cloud-placement branch from 746cae3 to 578c883 Compare August 24, 2026 09:17
@steipete
steipete requested a review from a team as a code owner August 24, 2026 09:17
@steipete

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

Accepted and fixed the P1 on exact head 578c88399cfd438ecb5a2253e77b39d9ce92ad93.

The authority callback is now threaded through the canonical active and failed reclaim owners. It revalidates after waits and immediately before journal recovery, tunnel/workspace effects, result acceptance/finalization, and environment destruction. Recovery still revalidates source lifecycle/ownership and canonical persisted placement before the SQLite archive.

The follow-up crash-consistency edge is also covered: once destroy() succeeds, durable local/reclaimed placement completion is allowed to finish even if authority closes during that await. This prevents a draining or failed placement from pointing at a deleted environment.

New production-boundary regressions cover:

  • revocation during workspace reconciliation blocks result acceptance and destroy;
  • revocation after tunnel cleanup blocks failed-placement destroy;
  • revocation during successful active destroy still completes reclaimed bookkeeping;
  • revocation during successful failed-placement destroy still completes local bookkeeping.

Final rebased validation: 92 Gateway recovery/archive/reclaim tests plus 3 SQLite recovery tests passed; the complete changed-file gate passed with no warnings; fresh P0/P1 autoreview reported no accepted/actionable findings (0.90). Exact-head CI is running now. Please re-review only; do not merge.

@clawsweeper

clawsweeper Bot commented Aug 24, 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:

@steipete

Copy link
Copy Markdown
Contributor Author

Land-ready proof for exact head 578c88399cfd438ecb5a2253e77b39d9ce92ad93:

  • CI run 32710691685: green, including the required openclaw/ci-gate and security-sensitive guards.
  • Final focused proof: 92 Gateway recovery/archive/reclaim tests and 3 SQLite recovery tests passed.
  • node scripts/check-changed.mjs: passed with no temp-directory, max-lines, type, lint, architecture, or database warnings.
  • Fresh P0/P1 autoreview after both authority fixes: no accepted/actionable findings (0.90).

ClawSweeper’s seven before-merge items are addressed:

  1. Authority is threaded to the canonical reclaim final-effect owner.
  2. Active and failed teardown revalidate immediately before each privileged effect after waits.
  3. Revocation during workspace reconciliation prevents result acceptance and environment destruction.
  4. The security boundary is shared by reclaim/move ownership, not recovery-only code.
  5. Production growth is justified by that shared authority boundary and its crash-safe handoff.
  6. Production-boundary revocation tests exercise the real placement dispatch owner.
  7. If authority closes during a successful destroy await, durable terminal bookkeeping completes so no placement points to a deleted environment.

Proof gap: the final paid-cloud restart/recovery matrix awaits the separately owned worker infrastructure ingress lane. The deterministic authority, workspace, environment, and SQLite owner boundaries changed by this PR are fully covered.

@steipete
steipete merged commit 7d5b986 into main Aug 24, 2026
232 checks passed
@steipete
steipete deleted the steipete/fix-session-recovery-cloud-placement branch August 24, 2026 09:31
@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 25, 2026
…#128597)

* fix(gateway): reclaim cloud workers before recovery

* fix(workers): fence reclaim final effects

* test(workers): track reclaim authority temp dirs
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: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: L status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Session recovery can archive a source with an active cloud worker

1 participant