Skip to content

fix(state): retire stale workers before replacement admission - #159944

Merged
steipete merged 4 commits into
mainfrom
fix/shared-worker-active-refusal-0927
Sep 28, 2026
Merged

steipete merged 4 commits into
mainfrom
fix/shared-worker-active-refusal-0927

Conversation

@steipete

@steipete steipete commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Builds on Vincent Koc’s #159757, preserving his original commit attribution and adding actor-wide active-callback refusal.

Fixes: state migrations lose their lease when a retired database's filesystem identity is reused by another database while its lazy worker is still retained.

User Impact

Database operations stop reusing invalidated workers, and a lazy worker refuses a removed or replaced original database instead of recreating it. No schema, configuration, or public API changes are required.

Why This Change Was Made

The database lifecycle already invalidated the old admission, but shared-worker selection checked only the physical file key. Record that canonical lifecycle admission once at worker entry creation, then retire and join the exact stale actor before reuse. Keep it separate from the caller's short-lived schema admission so successive valid schema scopes can share the same worker. If any callback still holds the stale actor, reject replacement immediately; joining its retirement from that callback would wait on itself. Once those callbacks settle, replacement joins retirement before using the new database.

The existing-only backend also checks its captured identity at the first native open. Aliases retain their existing sharing behavior; there is no automatic alias retargeting or fallback.

Known boundary: external same-path, same-inode replacement without canonical lifecycle invalidation remains a separate follow-up. Raw birth timestamps cannot safely establish a universal file generation.

Evidence

Merged as 50864f52176af234dcf4baea0af7640421f67e79; CI 36361602386 passed on exact head e22489aeddb7b0a687e08d8a8ad0d75847a9cd6e. Main ancestry and Vincent Koc’s merge attribution are verified. All task-owned Testbox leases are stopped. The source-pinned proof and intermediate investigations below remain historical evidence.

  • Exact signed source: 5f623a384410d1f8a00a28413b54ac8655df5354; tree f94140e32e968917dc57a0fcb65b685ea6d0b984.
  • Linux Blacksmith proof: native Testbox run, one worker per file. Node 24.19.0: 129 passed. Maintained Bun 1.4.3, revision a70be26b47560ede9b7646451e66e3f735195672: 127 passed and two pre-existing Node-only topology skips.
  • The unchanged original migration file passed all 23 cases in declaration order on each runtime. Focused coverage includes retained-worker retirement and join, zero/changing file metadata, sequential schema scopes, unexpected-error propagation, lazy missing/replaced locators, aliases, and asynchronous lifecycle behavior.
  • Actual Vitest reporter/fork executable hashes and native worker manifests were verified, including the patched backend input digest 58eb3fb2eddda3435942b05bd4d4fa716459fbd348d602ffa00f38db1ce2a8c3.
  • All six-path changed-file gates passed (1,280.162s), including formatting, core typecheck, all 25 test type graphs, lint (zero warnings/errors), and schema/lifecycle guards. The remote command exited 0; local wrapper cleanup exited 2 after a disk-full receipt write failure. The Testbox was stopped; its local capsule is retained by the native admitted-state guard. This is passing remote proof, not a successful local artifact or cleanup receipt.
  • Independent owner review and installed source review found no remaining actionable finding. Production: +49 net lines for lifecycle ownership and the lazy-open invariant; tests: +220 net lines.

The two Bun skips assume four shared carriers, which does not match Bun's existing dedicated-worker topology; no new skip was added. Dedicated-slot saturation coverage is a separate follow-up. The maintained Bun run's known directory-FD warning is repaired separately in openclaw/bun#16; it did not fail these tests.

Proof cost

One Testbox lease, observed queued at 15:01:43 UTC and stopped at 15:36:32 UTC on 2026-09-27. The passing native command took 28m2.580s; focused file wrappers totaled 5m54.345s and changed gates took 21m20.162s. An earlier pre-test observer assertion incorrectly compared the transport parent to the source branch parent; it failed before tests, was corrected to the receiver-installed transport base, and reused the same immutable source and lease.

Integration for the migration CI failure

The migration completed/refused failure blocking #159481 was traced to a revoked worker selected after inode reuse, writing lease rows into the previous fixture database. The initially landed repairs e52420 and 601bb5 were reverted. This replacement retains canonical database-generation admission independently of a caller's lexical schema scope; it does not reclassify ended schema authority as a cache miss.

The active-callback behavior is now explicit: replacement rethrows the original invalidation while any callback retains that actor. This preserves accepted-work custody and avoids a callback waiting for its own close. The check uses the existing actor-wide operation owner, including other maintenance scopes. Idle stale actors still retire and join before replacement admission. No write is replayed, and no durable data, schema, retention, permission, config or public SDK contract changes. Existing-only lazy workers refuse a removed/replaced original locator instead of creating or adopting another file. Existing installations need no migration or update action.

Two additional regression cases keep a callback active through a real filesystem rename, then request replacement in the same scope or another maintenance scope. A pass-through observation of real retirement lets the old implementation report retiring and settle naturally. On provider blacksmith-testbox, lease tbx_01m3j6nby3jq9g0et6qshevs88, run 36345784969, both cases failed for that exact reason on the original PR head in 16.387 s; neither timed out. The receipt classifier additionally expected the label AssertionError, while Vitest's resolves wrapper reported Error. That harness rejection is retained; direct inspection verified the exact expected failures, and the negative was not rerun. The corrected implementation passed the full admission file: 13 tests in 20.808 s. Assertions, retry counts, timeouts and mock scope were not weakened.

Current-source proof: provider blacksmith-testbox, lease tbx_01m3jg140mvzve7v6rafdhdy94, run 36355707484, tested head 9c2d0b4e8dce5c1d922491711d81a95ebf412802, tree 8abd8f5e3d3a598a443f1aa005af3c07ad099b1a on base e74690a70ca826da3c40e745099482e8c9401da6. The matching class32 runner exposed eight logical CPUs and 33,229,729,792 bytes RAM; actual Node 24.19.0 and pnpm 12.5.0 were recorded.

The clean-main negative, with only the final tests applied, failed six regressions in 21.478 s: three stale-worker replacements bypassed retirement, two active-callback replacements wrote instead of refusing, and one recorded admission error was ignored. The receipt checker expected only the three stale-retirement failures; its rejection is retained. Direct inspection confirmed that all six failures express the missing canonical-admission check. No negative was rerun and no assertions or matcher were changed.

The full candidate passed all 67 focused cases using node scripts/run-vitest.mjs <file> --maxWorkers=1: admission 13/25.408 s; worker lease 7/17.909 s; settlement 6/33.432 s; resident subagent state 39/28.502 s; existing-schema 2/21.248 s. The latter two protect the ended-schema semantics involved in the reverted repair.

python3 run-repro.py mcp-small16 <candidate-checkout> replayed the original first infra CI group with the same environment, two-worker setting, class32 machine and surviving file order: 436 passed, two existing skips, 183.835 s. Four of the original 46 paths were deleted by the separately landed Tasks removal; all 42 surviving paths were supplied. The later Gateway groups cannot influence the earlier infra failure and were not rerun. node scripts/run-tsgo-core-test-shards.mjs --ci-graphs-json '["core","core-test-state-logging"]' --concurrency 2 passed in 42.506 s; node scripts/run-oxlint.mjs --tsconfig config/tsconfig/oxlint.core.json <six changed files> --threads=1 passed in 21.835 s.

An earlier integrated attempt lost its transport during the network outage (exit 255, empty output, no phase receipts). Its expired lease could not return evidence, so no result was claimed for that attempt. Only those unproven phases were resumed on the fresh lease; the completed original-head controls were retained.

Publication head 70615a358655ef2095da3741f037041e84146674 is a patch-identical rebase onto main 4994ec450134ed826e6e4fb63f6a277aee53d4a1. GitHub's enforced checks gate its exact-head auto-merge.

Deslop and fresh isolated Codex review through P2 are clean. The complete replacement and active-callback addition were rebased onto current main with both commits patch-identical. Full PR LOC: production +53/-1/net +52; tests +316/-9/net +307; docs +0/-0/net 0. The additional production delta beyond the original PR is +4/-1/net +3; the added regression coverage is +76/-1/net +75.

Native-handle fixture correction after CI

Run 36357143646, job 108727843346, exposed four ENOENT failures in openclaw-state.worker.summary.test.ts. These direct-backend fixtures constructed an existing-only backend with no file, then expected its first write to create the database. The real broker rejects an absent existing-only path before worker dispatch. Its existing missing-path/no-dispatch regression is unchanged.

The fixture now supplies admitted input: KV cases use an empty existing file, while health/diagnostic cases initialize the canonical schema through the database owner and fully close it before capturing admission. Read assertions compare exact bytes before and after. Every transaction-grant, promotion, shared-handle, survivor, close, and durable-reopen assertion remains. The ordinary-creation case still begins with an absent path. Production and the identity guards are unchanged.

On the same blacksmith-testbox lease tbx_01m3jg140mvzve7v6rafdhdy94 / run 36355707484, the historical CI merge 69491cd90bbc298eeaea8d10688e1008e5cdb673 reproduced all four failures in the exact exercised prefix: 50 Gateway inputs followed by 80 unit inputs, original environment and worker settings, 124.453 s. The first attempt stopped before the target group because Bun was absent; it is recorded as a prerequisite failure. CI's exact pinned Bun binary (1.4.3-canary.1+ddfce5d01) was installed with the action's archive and binary checksum checks, then that complete environment was used.

The first fixture candidate passed three cases but exposed missing-schema errors in the two health cases. The final setup uses the actual canonical schema for those cases; no baseline was rerun. pnpm test src/state/openclaw-state.worker.summary.test.ts --maxWorkers=1 passed all five tests in 18.060 s. The same two-group prefix passed all 1,715 tests across 130 files in 109.120 s (705 Gateway Node, 20 Gateway Bun, 990 unit tests). Summary-only typed lint passed in 19.512 s. The tested combined fixture tree was 434c7be6fe1ab1092003264e46799bd74c385291; source hashes were verified. Both fixture refinements have clean isolated Codex P2 reviews and deslop.

Current head e22489aeddb7b0a687e08d8a8ad0d75847a9cd6e rebases all four commits unchanged onto main 1fea5ee10891ea6105f8757ec8695f9583a134ca. Exact-head auto-merge is re-armed; required GitHub CI remains the merge gate. The cumulative fixture delta is tests +18/-6/net +12, with no production change.

@clawsweeper

clawsweeper Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@clawsweeper

clawsweeper Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 27, 2026, 8:28 PM ET / September 28, 2026, 00:28 UTC (Revision 2).

ClawSweeper review

What this changes

The branch makes OpenClaw retire a stale shared SQLite worker before admitting replacement database work and makes a lazy worker refuse a removed or replaced original file.

Merge readiness

✅ Ready for maintainer review

Current main still lacks the database-generation check, and this PR supplies a focused fix with regression evidence. No actionable patch defect emerged from the reviewed head.

Priority: P2
Reviewed head: e22489aeddb7b0a687e08d8a8ad0d75847a9cd6e

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) The focused owner-level repair has strong reported before-and-after boundary proof and no concrete review finding.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (terminal): The changed shared-worker owner is exercised through a real plugin-state client and SQLite file rename on Testbox: the PR records clean-main failures, candidate recovery and refusal, and a passing replay after fixture correction. The source review supports that boundary, while the remote observations remain those recorded in the PR body. No stored-data format or schema changes.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (terminal): The changed shared-worker owner is exercised through a real plugin-state client and SQLite file rename on Testbox: the PR records clean-main failures, candidate recovery and refusal, and a passing replay after fixture correction. The source review supports that boundary, while the remote observations remain those recorded in the PR body. No stored-data format or schema changes.
Evidence reviewed 8 items Introduced owner check: The PR compares each matching retained or retiring worker's captured database admission and joins retirement of an invalidated idle actor before replacement admission.
Lazy native-open guard: An existing-only backend captures its original file identity and checks it before its first native open; the new guard also covers direct device-identity loading and existing-schema lease acquisition.
Current main remains unfixed: Fetched main still iterates retained stores without checking their recorded database generation, and its lazy backend has no original-file identity guard.
Findings None None.
Security None None.

How this fits together

OpenClaw sends shared-state database operations through a worker owner that selects a SQLite worker and retains it across calls. Database lifecycle admission and file identity determine whether that worker can serve a later operation or must retire first.

flowchart LR
A[State operation] --> B[Database admission]
B --> C[Shared worker owner]
C --> D{Original generation current?}
D -->|Yes| E[SQLite worker result]
D -->|No| F[Retire worker or refuse active callback]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source +52, Tests +307. Total +359 across 7 files.

View PR surface stats
Area Files Added Removed Net
Source 3 53 1 +52
Tests 4 316 9 +307
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 7 369 10 +359

Review metrics

Metric Value Why it matters
Code growth by purpose production net +52 lines; tests net +307 lines The production growth has a stated lifecycle and native-open purpose; the larger test delta exercises the failure and the fixture correction.

Root-cause cluster

Relationship: canonical
Canonical: #159944
Summary: The earlier draft PR addresses the same database-generation reuse defect; this branch carries that work forward and adds active-callback refusal.

Members:

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

Technical review

Best possible solution:

Keep database-generation custody with the existing worker owner, settle the old actor before replacement work, and preserve refusal when an active callback or changed file makes reuse unsafe.

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

Yes. The added real-file rename scenario reaches the plugin-state client and shared worker owner, and the PR reports clean-main failures for that path; this reviewer did not execute it.

Is this the best way to solve the issue?

Yes. Capturing canonical database admission in the existing worker owner and checking the original file at lazy open repairs the stated invariant without a new store, schema, or public API.

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

  • add P2: This is a bounded shared-state worker defect with potential database misdirection under file-identity reuse.
  • add proof: sufficient: Contributor real behavior proof is sufficient. The changed shared-worker owner is exercised through a real plugin-state client and SQLite file rename on Testbox: the PR records clean-main failures, candidate recovery and refusal, and a passing replay after fixture correction. The source review supports that boundary, while the remote observations remain those recorded in the PR body. No stored-data format or schema changes.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The changed shared-worker owner is exercised through a real plugin-state client and SQLite file rename on Testbox: the PR records clean-main failures, candidate recovery and refusal, and a passing replay after fixture correction. The source review supports that boundary, while the remote observations remain those recorded in the PR body. No stored-data format or schema changes.

Label justifications:

  • P2: This is a bounded shared-state worker defect with potential database misdirection under file-identity reuse.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The changed shared-worker owner is exercised through a real plugin-state client and SQLite file rename on Testbox: the PR records clean-main failures, candidate recovery and refusal, and a passing replay after fixture correction. The source review supports that boundary, while the remote observations remain those recorded in the PR body. No stored-data format or schema changes.
  • proof: sufficient: Contributor real behavior proof is sufficient. The changed shared-worker owner is exercised through a real plugin-state client and SQLite file rename on Testbox: the PR records clean-main failures, candidate recovery and refusal, and a passing replay after fixture correction. The source review supports that boundary, while the remote observations remain those recorded in the PR body. No stored-data format or schema changes.

Evidence

What I checked:

  • Introduced owner check: The PR compares each matching retained or retiring worker's captured database admission and joins retirement of an invalidated idle actor before replacement admission. (src/state/openclaw-state-worker-owner.ts:387, e22489aeddb7)
  • Lazy native-open guard: An existing-only backend captures its original file identity and checks it before its first native open; the new guard also covers direct device-identity loading and existing-schema lease acquisition. (src/state/openclaw-state.worker.ts:87, e22489aeddb7)
  • Current main remains unfixed: Fetched main still iterates retained stores without checking their recorded database generation, and its lazy backend has no original-file identity guard. (src/state/openclaw-state-worker-owner.ts:386, e205c9d6fced)
  • Existing storage contract: The database-worker documentation already requires an open that observed an existing file to refuse a disappeared or changed generation rather than recreate the file. Public docs: docs/reference/database-schemas/worker-access.md. (docs/reference/database-schemas/worker-access.md:91, e22489aeddb7)
  • Production-path regression coverage: The added admission tests drive a plugin-state operation through the shared worker owner while renaming a real SQLite file; cases cover stale-worker retirement, active-callback refusal, separate schema scopes, and unexpected admission errors. (src/state/openclaw-state-db-read-admission.test.ts:36, e22489aeddb7)
  • Reported before-and-after Testbox proof: The captured PR body reports six clean-main regression failures, 67 passing focused candidate cases, and a passing 1,715-case CI-prefix replay after correcting four existing-only test fixtures. These are contributor-reported remote observations; this read-only review did not execute the tests. (e22489aeddb7)

Likely related people:

  • Peter Steinberger: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

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 (1 earlier review cycle)
  • reviewed 2026-09-27T23:05:19.046Z sha 70615a358655ef2095da3741f037041e84146674 :: needs maintainer review before merge. :: none

@steipete
steipete force-pushed the fix/shared-worker-active-refusal-0927 branch from 70615a3 to e22489a Compare September 28, 2026 00:16
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. 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. labels Sep 28, 2026
@steipete
steipete merged commit 50864f5 into main Sep 28, 2026
216 checks passed
@steipete
steipete deleted the fix/shared-worker-active-refusal-0927 branch September 28, 2026 00:30
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 28, 2026
…aw#159944)

* fix(state): retire workers after database admission invalidation

* fix(state): refuse stale actor replacement during active callbacks

---------

Co-authored-by: Vincent Koc <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: M 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.

2 participants