Skip to content

refactor: move shared database admission into its worker - #146172

Merged
steipete merged 1 commit into
mainfrom
codex/shared-state-foundation-final
Sep 12, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/shared-state-foundation-final

Conversation

@steipete

@steipete steipete commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

The first shared-state worker read still opened and repaired the canonical database on the calling thread. Worker-only reads could also ignore a terminal database failure recorded by the host when no host connection was cached, and failed cleanup could lose track of a native coordinator lease.

Why This Change Was Made

Open and repair shared state inside the existing physical database worker actor, carrying environment and coordinator runtime facts captured by their canonical owners. Existing runtime.tasks.async reads use that context. The host retains live Gateway schema authority, validates its existing terminal failure record through worker inspection, and retains cleanup custody until native close or successful handoff to the existing idle pool.

No persisted schema, schema version, or row format changes. Input/result framing and current pooling limits remain intact. Inspection failures preserve a known terminal failure; only a successful generation mismatch clears it. Public synchronous APIs remain available for compatibility. Task/flow registry restoration and runtime configuration preparation still run synchronously and are separate follow-ups.

User Impact

Canonical database opening, repair, and reads execute in the worker while shutdown and terminal database errors remain observable to callers. Existing read payloads and synchronous callers remain compatible. Plugin KV activation and bundled caller migration are separate successors.

Evidence

  • 89 focused real SQLite, public SDK, coordinator, transport, and cleanup cases passed. These include cold create/repair with reopened-index verification, terminal admission, default pool reuse, retryable cleanup, actual 40/72 MiB append/reopen, cancellation/credits, partial staging drainage, and full result transport. Existing pathname-replacement probes were excluded from this scoped local run.
  • Full local changed-code gate passed, and the exact final-head packaged build passed in 6m 53.9s. Fresh independent Codex review found no actionable P0–P2 findings.
  • Earlier f1734c0f8c0e compiled Node 24.20 SDK measurements preserved Unicode payloads and restart/reopen behavior. Four warmed public reads took 3.73 ms with all six parent SQLite method counters at zero. Bun 1.4.2 canonical cold opening/read took 343.14 ms with the same zero counters and captured default pooling. These are individual synthetic timings, not comparative benchmarks.
  • Positive published-driver qualification passed on isolated Linux/Node: published 2026.9.4 synthetic task/flow rows and CLI payloads survived the real candidate update, Doctor, first Gateway startup, and restart. Actual async SDK reads and lifecycle drainage passed. The published backup was verified and restored by the candidate with identical rows. Installed runtime, lifecycle, and worker hashes matched the candidate package, and build IDs verified the artifact transition despite the unchanged package-version string. The test lease is released with zero owned containers.

The final qualified package SHA-256 is abbe9bc0003f918048203dcb7155629043caca687ec25f3dc63d888b268dff34, built from exact head 179469fb205e649b092c17a7baf34ab54922d621. Its independent published-driver run passed on the first attempt. Health/readiness returned 200 and RPC succeeded. Startup telemetry still reported event-loop degradation (p99/max 23.572 seconds over a 24.013-second sample); update took 178 seconds and startup/restart took 32/30 seconds. This establishes retained-data and artifact compatibility, not whole-Gateway cold-start performance. Earlier artifact receipts remain separate.

Current head 179469fb205e contains the reviewed shutdown composition on main 7753ffb4753f. It preserves the landed forced-no-retention cleanup from #146160, alongside retryable failed-close custody and captured runtime facts. The complete composition passed 69 focused cases, a fresh full P0–P2 review, and the full changed-code gate. The final conflict resolution only incorporates main's native-realpath helper/tests; an independent source review confirmed no foundation ownership or retention body changed. No Windows runtime claim is inferred from the local macOS tests.

Compiler routing fixes have since landed on main, so the redundant compiler commit was removed and the PR is back to 40 files. The final read-only inventory check assigns all 10,092 roots exactly once across the same 18 compiler graphs, with infra 671 and state/logging 404 roots. No test cases or caps were removed. Exact-head CI run 34711552584 is green. The final shutdown composition has its own source/runtime proof and exact-package upgrade receipt; no old artifact is being relabeled as final-head proof.

The canonical schema owner enters withStateSchemaFence before its synchronous migration transaction. Captured worker context reaches the live delegate assertion at that boundary. Real create/repair tests verify the reopened index; separate canonical refusal tests cover future schema and external-manager ownership. Revoked-delegate schema effects were not live-tested; the callback-level negative fixture is not presented as that evidence. The maintainer source review accepts this bounded scope with that limitation explicit.

@clawsweeper

clawsweeper Bot commented Sep 12, 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.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation cli CLI command changes plugin: file-transfer size: XL maintainer Maintainer-authored PR labels Sep 12, 2026
@steipete
steipete marked this pull request as ready for review September 12, 2026 16:08
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Sep 12, 2026
@clawsweeper

clawsweeper Bot commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed September 12, 2026, 3:46 PM ET / 19:46 UTC (Revision 7).

ClawSweeper review

What this changes

Moves shared SQLite opening and repair into the database worker while preserving terminal errors and retaining connection cleanup until it finishes.

Warning

SQLite table change

This PR modifies persisted SQLite tables in src/infra/sqlite-store.worker.ts, src/infra/sqlite-worker-store.ts. Prefer a design that avoids changing persisted SQLite tables. If the change is necessary, verify migration and upgrade compatibility against an existing database before merge.

Merge readiness

⛔ Blocked before merge - 5 items remain

This remains useful work absent from current main. The reported exact-head upgrade qualification addresses artifact compatibility, but the previously identified canonical-worker authority proof gap remains. No discrete blocking code defect was established.

Priority: P2
Reviewed head: 179469fb205e649b092c17a7baf34ab54922d621

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) Useful implementation and substantial positive qualification remain limited by the unresolved authority-effect proof gap.
Proof confidence 🦐 gold shrimp (3/6) Needs stronger real behavior proof before merge: Authority-chain proof required: the exact-head Linux/Node upgrade, SDK reads, restore, and drainage observations remain credited. The outstanding scoped gap is canonical-worker create/repair refusal with absent or revoked delegation; the body explicitly excludes revoked schema-effect coverage, and the negative fixture only exercises a callback. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Patch quality 🦐 gold shrimp (3/6) Security review found an item that needs attention.

Verification

Check Result Evidence
Real behavior Needs proof Needs stronger real behavior proof before merge: Authority-chain proof required: the exact-head Linux/Node upgrade, SDK reads, restore, and drainage observations remain credited. The outstanding scoped gap is canonical-worker create/repair refusal with absent or revoked delegation; the body explicitly excludes revoked schema-effect coverage, and the negative fixture only exercises a callback. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed 9 items Current main still opens shared state on the caller: The fetched main implementation calls openOpenClawStateDatabase inside the host's shared worker owner before opening an existing-only worker backend. The central worker-admission change is therefore still distinct.
Introduced worker admission and live authority: The worker now invokes the canonical opener. Gateway delegation retains the native coordinator, carries a shared liveness flag, and checks that flag through the schema fence; schema creation and repair remain in the canonical database owner.
Canonical schema effect boundary: ensureSchema enters withStateSchemaFence before the synchronous migration transaction, which can recreate indexes and execute the canonical schema. This connects the new delegation to actual persisted effects rather than merely worker routing.
Findings None None.
Security Needs attention Verify revoked delegation at canonical schema effects: The new live delegate permits worker-side schema mutation and remains attached across requests. Callback refusal is covered, but the supplied evidence does not establish that an undelegated or revoked canonical worker leaves schema unchanged before final I/O.

How this fits together

OpenClaw's shared-state database stores task and flow records used by plugin runtime reads. The host admits requests and retains Gateway ownership while a worker opens the canonical database, reads records, and returns results or classified errors.

flowchart TD
  A[Plugin task and flow reads] --> B[Host admission and terminal failure check]
  B --> C[Database worker]
  D[Live Gateway coordinator] --> E[Schema authority check]
  C --> E
  E --> F[Canonical SQLite database]
  F --> G[Results or classified errors]
  C --> H[Drain operations and release connections]
Loading

Before merge

  • Add real behavior proof - Needs stronger real behavior proof before merge: Authority-chain proof required: the exact-head Linux/Node upgrade, SDK reads, restore, and drainage observations remain credited. The outstanding scoped gap is canonical-worker create/repair refusal with absent or revoked delegation; the body explicitly excludes revoked schema-effect coverage, and the negative fixture only exercises a callback. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
  • Resolve security concern: Verify revoked delegation at canonical schema effects - The new live delegate permits worker-side schema mutation and remains attached across requests. Callback refusal is covered, but the supplied evidence does not establish that an undelegated or revoked canonical worker leaves schema unchanged before final I/O.
  • Resolve merge risk (P1) - The new worker delegation can survive asynchronous admission, but canonical create/repair refusal with absent or revoked authority remains unverified at the database-effect boundary.
  • Complete next step (P2) - Add canonical-worker refusal evidence before merge, using terminal output or logs with private details redacted. Update the PR body to trigger re-review; if it does not run, ask a maintainer to comment @clawsweeper re-review.
  • Add data-model compatibility proof - Confirm migration or upgrade compatibility proof before merge.

Findings

  • [medium] Verify revoked delegation at canonical schema effects — src/infra/state-database-coordinator.ts:364
Agent review details

Security

Needs attention: No concrete bypass or supply-chain regression was established; worker schema authority still needs final-effect refusal evidence.

PR surface

Source +1496, Tests +1445, Docs +12. Total +2953 across 40 files.

View PR surface stats
Area Files Added Removed Net
Source 26 1862 366 +1496
Tests 12 1447 2 +1445
Docs 2 14 2 +12
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 40 3323 370 +2953

Review metrics

Metric Value Why it matters
Production versus test growth Production net +1,496 lines; tests and test support net +1,445 lines The stated production growth supports worker admission, classified error transport, and retryable cleanup without adding a persisted schema.

Stored data model

Persistent data-model change detected: database schema: src/state/openclaw-state-db-cache.ts, database schema: src/state/openclaw-state-db-runtime-failure.ts, database schema: src/state/openclaw-state-worker-error.ts, migration/backfill/repair: src/state/openclaw-state-worker-error.ts, unknown-data-model-change: src/infra/sqlite-store.worker.ts, unknown-data-model-change: src/infra/sqlite-worker-store.ts. Confirm migration or upgrade compatibility proof before merge.

Merge-risk options

Maintainer options:

  1. Complete canonical authority evidence (recommended)
    Provide database-outcome evidence that undelegated workers and workers revoked during delayed admission cannot create or repair schema.

Technical review

Best possible solution:

Keep canonical worker opening and existing synchronous APIs, with live-owner refusal and complete cleanup demonstrated through the actual database path.

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

Yes, source inspection establishes that current main performs canonical opening on the caller thread and can reuse a worker without consulting the host terminal latch. No runtime reproduction was executed during this read-only review.

Is this the best way to solve the issue?

Yes, reusing the canonical opener and existing lifecycle owners is a coherent repair; the remaining qualification concerns authority refusal at actual schema effects.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 53c804f5ee18.

Labels

Label justifications:

  • P2: This improves shared-state responsiveness and failure handling without evidence of an urgent user-facing outage.
  • merge-risk: 🚨 security-boundary: The patch delegates Gateway schema authority across worker lifetime boundaries, and final-effect refusal after authority loss remains unproven.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🦐 gold shrimp.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs stronger real behavior proof before merge: Authority-chain proof required: the exact-head Linux/Node upgrade, SDK reads, restore, and drainage observations remain credited. The outstanding scoped gap is canonical-worker create/repair refusal with absent or revoked delegation; the body explicitly excludes revoked schema-effect coverage, and the negative fixture only exercises a callback. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Evidence

Security concerns:

  • [medium] Verify revoked delegation at canonical schema effects — src/infra/state-database-coordinator.ts:364
    The new live delegate permits worker-side schema mutation and remains attached across requests. Callback refusal is covered, but the supplied evidence does not establish that an undelegated or revoked canonical worker leaves schema unchanged before final I/O.
    Confidence: 0.94

What I checked:

  • Current main still opens shared state on the caller: The fetched main implementation calls openOpenClawStateDatabase inside the host's shared worker owner before opening an existing-only worker backend. The central worker-admission change is therefore still distinct. (src/state/openclaw-state-worker-store.ts:77, 53c804f5ee18)
  • Introduced worker admission and live authority: The worker now invokes the canonical opener. Gateway delegation retains the native coordinator, carries a shared liveness flag, and checks that flag through the schema fence; schema creation and repair remain in the canonical database owner. (src/infra/state-database-coordinator.ts:272, 179469fb205e)
  • Canonical schema effect boundary: ensureSchema enters withStateSchemaFence before the synchronous migration transaction, which can recreate indexes and execute the canonical schema. This connects the new delegation to actual persisted effects rather than merely worker routing. (src/state/openclaw-state-db.ts:357, 179469fb205e)
  • Positive schema tests and negative proof boundary: Canonical worker tests exercise real creation, missing-index repair, reopened-index verification, future-schema refusal, and external-manager refusal. The separate revoked-delegate fixture invokes a callback returning text; it does not attempt canonical schema changes or inspect their absence. (src/infra/state-database-coordinator.worker.test-support.mjs:17, 179469fb205e)
  • Captured runtime and upgrade evidence: The supplied body, captured under sourceRevision a1126fed9faaf35969fe23c49caea0dd2885490ef9d3481da50c369852053bac, reports a Linux/Node published-2026.9.4 upgrade through Doctor, startup, restart, async SDK reads, drainage, and backup restoration using exact-head package SHA-256 abbe9bc0003f918048203dcb7155629043caca687ec25f3dc63d888b268dff34. It explicitly excludes live revoked-delegate schema effects and records maintainer acceptance of that limited source review. These positive observations remain credited; they do not cover the excluded authority scenario. (179469fb205e)
  • Re-review continuity: The previous completed review inspected this same head and requested canonical-worker refusal evidence for absent or revoked delegation. The current body still explicitly excludes revoked-delegate schema effects. No new line-level finding is inferred from that unchanged evidence gap. (179469fb205e)

Likely related people:

  • Peter Steinberger: Raw commit 7fcf912 adds src/plugins/runtime/runtime-tasks-async.ts:24 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 7fcf9124ce20; files: src/plugins/runtime/runtime-tasks-async.ts)

Rank-up moves

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

  • Show canonical schema creation and repair are refused for an undelegated worker under a live Gateway owner and for delegation revoked during delayed admission.
  • Attach redacted terminal output or logs showing the database outcomes, preserving the existing artifact-specific upgrade evidence.

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-09-12T16:16:35.205Z sha f1734c0 :: needs real behavior proof before merge. :: none
  • reviewed 2026-09-12T16:28:08.247Z sha f1734c0 :: needs real behavior proof before merge. :: none
  • reviewed 2026-09-12T16:49:32.975Z sha 058ce78c0ad7b93f47acfd883a27ae3632775907 :: needs real behavior proof before merge. :: none
  • reviewed 2026-09-12T17:06:32.616Z sha 058ce78c0ad7b93f47acfd883a27ae3632775907 :: needs real behavior proof before merge. :: none
  • reviewed 2026-09-12T17:43:45.468Z sha c7e9dca6c68213c5f4eb18dc071dfb49ab46a2d8 :: needs real behavior proof before merge. :: none
  • reviewed 2026-09-12T18:42:16.335Z sha 179469f :: needs real behavior proof before merge. :: none

@steipete

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

The PR body now includes completed positive published-driver upgrade, Doctor, restart, async SDK/lifecycle, backup restoration, and installed runtime/worker hash evidence. The synthetic Linux/Node run passed and its lease is released. The qualified package pins f1734c0f8c0e; current head 058ce78c0ad7 differs only in two compiler-test configurations.

There is no persisted schema/version/row-format change. The canonical schema owner enters withStateSchemaFence before its synchronous transaction, and the captured worker context reaches the live delegate assertion at that boundary. Existing real create/repair tests verify the reopened index; canonical refusal tests cover future schema and external-manager ownership. Revoked-delegate schema effects were not live-tested, and the callback-level negative fixture is not offered as that proof. The maintainer source review accepts this bounded scope with that limitation explicit; no approval override or new boundary-race probe is requested.

Whole local changed checks, build, fresh P0–P2 review, 89 focused runtime cases, the compiler inventory guard, and both affected compiler graphs passed. Startup event-loop degradation remains documented. CI is separately waiting on an unrelated Gateway compiler-root budget repair on main.

@clawsweeper

clawsweeper Bot commented Sep 12, 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 removed the merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. label Sep 12, 2026
@steipete
steipete force-pushed the codex/shared-state-foundation-final branch from 058ce78 to c7e9dca Compare September 12, 2026 17:34
@steipete
steipete force-pushed the codex/shared-state-foundation-final branch from c7e9dca to 179469f Compare September 12, 2026 18:33
@steipete
steipete merged commit 7b87a49 into main Sep 12, 2026
194 checks passed
@steipete
steipete deleted the codex/shared-state-foundation-final branch September 12, 2026 19:58
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes docs Improvements or additions to documentation maintainer Maintainer-authored PR merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. P2 Normal backlog priority with limited blast radius. plugin: file-transfer rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: XL status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant