Skip to content

fix(gateway): retire worker placements without sessions - #123785

Merged
steipete merged 5 commits into
mainfrom
steipete/session-placement-retirement
Aug 14, 2026
Merged

steipete merged 5 commits into
mainfrom
steipete/session-placement-retirement

Conversation

@steipete

@steipete steipete commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes an issue where durable worker placements could survive after their authoritative session entry was deleted or replaced outside the explicit Gateway delete/reset RPCs. Those invisible rows could keep environments and worker authority alive, pin terminal environment retention, and be re-adopted after Gateway restart.

Why This Change Was Made

The Gateway worker lifecycle now reconciles each placement against canonical per-agent session evidence at startup, after identity mutations, and during periodic placement sweeps. The session-store owner provides one strict tri-state probe using indexed exact-key and current_session_id lookups: confirmed absence exact-CAS retires safe local or terminal rows; live or transitional placements first force-fence and destroy their environment; unreadable, ambiguous, or migration-invalid stores remain unknown and preserve the placement.

Startup and mutation-triggered retirement stay inside the reconciliation promise drained before environment shutdown. The change also moves the existing dispatch coordinator to its worker-environment owner so the startup composition stays below the 700-line budget.

AI-assisted; implementation and proof were reviewed by the author.

User Impact

Sessions deleted by maintenance, cron cleanup, plugin rollback, migration, or other canonical lifecycle owners no longer leave remote execution placements behind. A deleted session's worker environment is fenced and cleaned up even across restart, while archived sessions and temporarily unreadable session stores are retained safely.

Evidence

  • Direct canonical session deletion outside sessions.delete triggers placement retirement through the production Gateway startup context.
  • Unit coverage proves exact local retirement, live-environment fencing before failed-row retirement, indexed fallback lookup, and fail-closed handling for current, ambiguous, or migration-invalid evidence.
  • Production-resolver failure injection proves a migration-invalid session database reaches unknown instead of environment teardown.
  • Deferred-evidence startup coverage proves sidecar shutdown waits for retirement reconciliation before stopping environments, and concurrent stop callers join the same drain promise.
  • Focused Gateway and session-store suites pass, including placement coordinator, startup context, strict evidence, and retirement tests.
  • Full changed-file gate passed across core, core tests, and docs.
  • Branch-wide autoreview against origin/main: clean, no accepted/actionable findings.

@clawsweeper

clawsweeper Bot commented Aug 14, 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 14, 2026 18:02
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation app: macos App: macos gateway Gateway runtime plugin: cua-computer size: L maintainer Maintainer-authored PR labels Aug 14, 2026
@steipete
steipete force-pushed the steipete/session-placement-retirement branch from ad3c3a6 to bb5fa60 Compare August 14, 2026 18:44
@clawsweeper clawsweeper Bot added merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. P1 High-priority user-facing bug, regression, or broken workflow. 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 14, 2026
@clawsweeper

clawsweeper Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: found issues before merge. Reviewed August 14, 2026, 4:47 PM ET / 20:47 UTC.

ClawSweeper review

What this changes

This PR adds canonical-session evidence checks that retire orphaned Gateway worker placements during startup, identity changes, and periodic reconciliation.

Merge readiness

⚠️ Ready for maintainer review - 4 items remain

Keep open: the final head fixes the prior shutdown-drain finding, but concurrent calls to its new sidecar stop method can resolve before worker environments finish stopping.

Priority: P1
Reviewed head: 685c7a04ec8b00cda0e9bddbeaf8b8837227b90e

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The patch has strong focused boundary coverage and fixes the earlier drain gap, but the P2 concurrent-stop race prevents merge readiness.
Proof confidence 🦐 gold shrimp (3/6) Not applicable: This maintainer-authored, maintainer-labeled PR is not subject to the external-contributor proof gate; its loopback Gateway boundary test is useful supplemental evidence.
Patch quality 🦐 gold shrimp (3/6) 1 actionable review finding remain.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: This maintainer-authored, maintainer-labeled PR is not subject to the external-contributor proof gate; its loopback Gateway boundary test is useful supplemental evidence.
Evidence reviewed 5 items New shutdown race: The first stop call awaits reconciliation and environment shutdown, while a concurrent call returns immediately after observing stopped; this can falsely signal that the lifetime sidecar has drained.
Gateway caller awaits sidecars: Gateway close iterates and awaits registered lifetime sidecars, so an early-resolved second stop can let later teardown proceed before the first stop completes.
Prior finding addressed: Startup reconciliation is now placed in the tracked reconciliation slot before it can yield, and the added deferred-evidence test verifies that a first stop waits before stopping environments.
Findings 1 actionable finding [P2] Join concurrent sidecar shutdown calls
Security None None.

How this fits together

Gateway worker placements durably associate an agent session with a local or remote execution environment. The new path reads the canonical per-agent session store, retains uncertain records, and fences or retires records whose session is confirmed absent.

flowchart LR
  A[Canonical session store] --> B[Session evidence probe]
  C[Durable worker placement] --> B
  B --> D{Evidence state}
  D -->|Current or unknown| E[Keep placement]
  D -->|Absent| F[Fence and retire]
  F --> G[Worker environment cleanup]
  G --> H[Gateway shutdown]
Loading

Before merge

  • Join concurrent sidecar shutdown calls (P2) - Memoize the stop operation and return that promise once shutdown begins. A second caller currently returns at the stopped guard while the first still awaits reconciliation and environments.stop(), so Gateway teardown can proceed as if this worker sidecar were drained.
  • Resolve merge risk (P1) - Concurrent shutdown can advance Gateway teardown before the worker environment stop initiated by the first sidecar call has completed.
  • Resolve merge risk (P1) - The bug fix adds production net growth for new session-evidence and retirement ownership; the added owner boundaries are substantive, but the shutdown repair should avoid adding another parallel lifecycle path.
  • Complete next step (P2) - A narrow P2 shutdown-order repair is clear, but this maintainer-authored, protected-label PR remains for human review rather than automated repair routing.

Findings

  • [P2] Join concurrent sidecar shutdown calls — src/gateway/server-worker-placement-startup.ts:531-545
Agent review details

Security

None.

PR surface

Source +239, Tests +434, Docs +3. Total +676 across 13 files.

View PR surface stats
Area Files Added Removed Net
Source 6 377 138 +239
Tests 6 583 149 +434
Docs 1 5 2 +3
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 13 965 289 +676

Review metrics

Metric Value Why it matters
Production versus test LOC production +377/-138; tests +583/-149; docs +5/-2 The bug fix is production-net-positive because it introduces strict session evidence and retirement ownership; the moved dispatch coordinator accounts for part of the reshaping but does not remove the need to justify the new lifecycle surface.

Stored data model

Persistent data-model change detected: database schema: src/gateway/server-worker-placement-session-evidence.test.ts, durable storage schema: src/gateway/worker-environments/placement-dispatch-coordinator.test.ts, durable storage schema: src/gateway/worker-environments/placement-dispatch-coordinator.ts, durable storage schema: src/gateway/worker-environments/placement-session-retirement.test.ts, durable storage schema: src/gateway/worker-environments/placement-session-retirement.ts, migration/backfill/repair: src/gateway/server-worker-placement-session-evidence.test.ts, and 9 more. Confirm migration or upgrade compatibility proof before merge.

Merge-risk options

Maintainer options:

  1. Join concurrent shutdown calls (recommended)
    Store one stop promise, return it from every sidecar stop call, and prove that two callers both wait for deferred environment shutdown.

Technical review

Best possible solution:

Memoize the sidecar shutdown promise so every caller joins the same drain and environment-stop operation, with a concurrent-stop regression; the existing new evidence and retirement boundaries justify the remaining production growth because current explicit-mutation guards do not own startup or periodic orphan discovery.

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

Yes, from source: defer reconciliation or environment shutdown, call the new sidecar stop twice, and observe the second call resolve before the first has finished stopping environments.

Is this the best way to solve the issue?

No: the session-evidence approach is the right owner-boundary direction, but the changed shutdown path must make repeated stop calls join one shared operation.

Full review comments:

  • [P2] Join concurrent sidecar shutdown calls — src/gateway/server-worker-placement-startup.ts:531-545
    Memoize the stop operation and return that promise once shutdown begins. A second caller currently returns at the stopped guard while the first still awaits reconciliation and environments.stop(), so Gateway teardown can proceed as if this worker sidecar were drained.
    Confidence: 0.94

Overall correctness: patch is incorrect
Overall confidence: 0.94

AGENTS.md: found and applied where relevant.

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

Labels

Label justifications:

  • P1: The PR changes live Gateway worker-session lifecycle behavior and can leave teardown ordering incorrect during shutdown.
  • merge-risk: 🚨 session-state: The change retires durable worker placements from canonical session identity evidence.
  • merge-risk: 🚨 availability: A concurrent shutdown can report completion while the first caller still awaits worker environment teardown.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🦐 gold shrimp.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Not applicable: This maintainer-authored, maintainer-labeled PR is not subject to the external-contributor proof gate; its loopback Gateway boundary test is useful supplemental evidence.

Evidence

What I checked:

Likely related people:

  • steipete: Introduced the placement/dispatch path and later recovery, lifecycle-fence, and external session-identity work that this PR extends. (role: feature owner; confidence: high; commits: e98c7dfbcb9f, 411777dc1fcc, 5fb5eefa611e; files: src/gateway/server-worker-placement-startup.ts, src/gateway/worker-environments/placement-store.ts, src/config/sessions/session-accessor.sqlite-identity.ts)
  • joshavant: Contributed a current worker-authority preservation fix in the placement area. (role: adjacent worker-lifecycle contributor; confidence: medium; commits: 3b1460a74d17; files: src/gateway/server-worker-placement-startup.ts)
  • Josh Lehman: Recent session recovery work is adjacent to the canonical session-store evidence boundary used by this PR. (role: recent session-storage contributor; confidence: medium; commits: bb5ef9872a70; files: src/config/sessions/session-accessor.sqlite-entry-availability.ts)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Memoize and share the sidecar stop promise across concurrent callers.
  • Add a regression that defers environment shutdown and verifies both stop callers remain pending.
  • Re-run the exact-head Gateway checks after the repair.

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 (3 earlier review cycles)
  • reviewed 2026-08-14T18:48:55.323Z sha bb5fa609b0fdd594d677f369f30f4e500771097f :: found issues before merge. :: [P1] Preserve unreadable session evidence | [P2] Avoid full-store scans for every placement
  • reviewed 2026-08-14T19:11:09.389Z sha 5d38b066f13e29cc91741304081c7d693e9e1ada :: found issues before merge. :: [P1] Preserve unreadable session evidence | [P2] Probe session rows without full-store scans
  • reviewed 2026-08-14T20:25:20.788Z sha 728350c3fed81357a9d5e6300d690b36b04e1375 :: found issues before merge. :: [P2] Keep startup retirement inside the shutdown drain

@steipete
steipete force-pushed the steipete/session-placement-retirement branch from bb5fa60 to 5d38b06 Compare August 14, 2026 19:05
@clawsweeper clawsweeper Bot added merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Aug 14, 2026
@steipete
steipete force-pushed the steipete/session-placement-retirement branch from 728350c to 685c7a0 Compare August 14, 2026 20:42
@steipete
steipete force-pushed the steipete/session-placement-retirement branch from 685c7a0 to c41cf9c Compare August 14, 2026 20:54
@steipete
steipete merged commit 3c5e2ff into main Aug 14, 2026
190 of 193 checks passed
@steipete
steipete deleted the steipete/session-placement-retirement branch August 14, 2026 21:07
@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 15, 2026
)

* fix(gateway): retire placements without sessions

* fix(gateway): preserve unreadable placement evidence

* fix(gateway): drain placement retirement on shutdown

* test(sessions): auto-clean identity probe temp dirs

* fix(gateway): join placement sidecar shutdown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation gateway Gateway runtime maintainer Maintainer-authored PR merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. 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. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: XL 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.

1 participant