Skip to content

fix: preserve agent database ownership during concurrent initialization - #174251

Merged
steipete merged 1 commit into
mainfrom
fix/agent-db-concurrent-owner
Oct 16, 2026
Merged

steipete merged 1 commit into
mainfrom
fix/agent-db-concurrent-owner

Conversation

@steipete

@steipete steipete commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Two concurrent initializers can both report success for one unowned agent database, letting the later initializer replace the first agent's ownership metadata. This fixes the ownership failure in main CI.

User Impact

The first successful claim remains authoritative; a competing agent is refused. Existing schema, migration, deletion-fence, and clean-close guarantees remain intact. No storage format or operator action changes.

Why This Change Was Made

#172373 removed transaction-local checks under the assumption that “synchronous preparation remains under Gateway or exclusive maintenance custody; unsupported writes between preparation and the transaction are not polled for.” The initializer nevertheless accepts competing first claims before either has established ownership. Its saved empty-file observation can outlive a lock wait, and the metadata upsert then overwrites the winner.

Move the existing owner/version/migration decisions into the synchronous schema write transaction. Refresh only schema facts there, preserving the independent physical-integrity proof used by clean-close seals. This adds no retry, timer, or ownership protocol.

Evidence

  • A deterministic test commits a real competing initialization immediately before the pending schema transaction. It fails on unchanged production code in all three fixed-count experiments under two CPU load processes. The original cross-process claim test and its assertions are retained.
  • Six focused files pass 113 tests with --maxWorkers=1; one existing platform-specific skip is unchanged. This includes creation/query-count, migration rollback, deletion fencing, and all clean-close/seal cases.
  • Core and owning test types, targeted lint, formatting, and independent P2 review pass.
  • The complete hosted 61-selector storage shard collected 60 files: 758 passed, one inherited update-wizard assertion failed, and eight existing skips remained. Both claim cases passed. The wizard failure reproduces on current main 88ef99e66fc9 when its file runs in order (31 passed, one failed); it observes an internal file-watcher helper as the first spawn. Its isolated case passes. No update-wizard assertion was changed.
  • Ten additional runs of both claim cases passed under four CPU load processes (20 case executions, zero failures). This follows three pre-fix failures of the deterministic case under two CPU load processes. Exact-head PR CI passed, including openclaw/ci-gate; no CI dispatch or rerun was requested.

The first local repair invalidated physical-integrity facts too broadly and failed eight existing seal tests. Narrowing invalidation to schema facts fixes those failures; no assertion was changed.

Read the authoritative owner, schema version and migration state inside the
schema write transaction instead of retaining observations made before BEGIN.
Invalidate only schema facts so physical integrity proof and clean-close seals
remain available. No schema or storage-format changes.

The deterministic native-handle regression fails on main in 3/3 CPU-pressure
runs; focused ownership, migration, deletion and seal suites pass 113 tests.
Core/test types, targeted lint/format and independent P2 review pass.
@clawsweeper

clawsweeper Bot commented Oct 16, 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.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-16T13:30:14.981833Z 9d812c0 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation size: S labels Oct 16, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 16, 2026
@clawsweeper

clawsweeper Bot commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR reads agent ownership, schema version, and migration eligibility after acquiring the database write lock.

Example: worker-b initializes shared.sqlite while worker-a is preparing its first claim.

  • Before: worker-a can overwrite worker-b's ownership metadata and also report success.
  • After: worker-a reads the committed owner inside its transaction and refuses the claim.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) A focused, source-supported repair with meaningful native-database regression coverage; validation evidence is primarily reported focused tests and ongoing CI.
Proof confidence 🦐 gold shrimp (3/6) This maintainer-authored repair is outside the ordinary contributor proof gate; supplied native-SQLite regression evidence exercises competing claims through the production schema owner, while no shipped-entry-point capture is supplied.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Two agents can both claim one newly initialized database, leaving ownership assigned to the later initializer.
Reason: This restores the established single-owner contract, and the maintainer-authored direction requires no additional product decision.

Regression provenance

Possible regression — suspected (reviewed change). No predecessor PR is attributed.

Merge readiness

✅ Ready for maintainer review

Keep open: this maintainer-authored PR repairs a concrete ownership regression still present on current main; no introduced correctness defect was found.

Priority: P2
Reviewed head: 9d812c0a09cfbdb40b520fe56e4ed90e75176627

Before merge

None.

Findings

None.

Agent review details

How this fits together

Agent database admission accepts a SQLite connection and agent identity, validates ownership and migration eligibility, then initializes or upgrades the store and registers its owner.

flowchart TD
A[Agent database opener] --> B[Integrity admission]
B --> C[Acquire write transaction]
C --> D[Read owner and schema]
D --> E[Refuse competing agent]
D --> F[Initialize or migrate]
F --> G[Publish facts and register]
Loading

Technical review

Best possible solution:

Retain the existing schema owner and make its ownership and migration decisions from current facts inside the write transaction.

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

Current-main source exposes the stale pre-lock observation and subsequent metadata upsert; the added deterministic regression targets that sequence, with baseline failures reported by the author but no execution performed during this review.

Is this the best way to solve the issue?

The repair addresses the owning invariant with the existing transaction and invalidation mechanisms, preserving migration authority checks and avoiding a parallel ownership protocol.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 88ef99e66fc9.

Provenance checked

  • Transaction-local ownership, version, and migration decisions changes intended behavior with a stated reason (d6bed86: Reduce repeated admission queries by relying on Gateway or exclusive maintenance custody during synchronous preparation.)
  • Fresh-schema admission and reusable integrity facts keeps the original intent (0122cae: Install canonical schema directly for fresh databases and reuse owner-published admission facts.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

  • Introduced repair: The pinned introduction diff moves existing decisions into the transaction and adds schema-only invalidation before reading ownership; migration bodies and persisted formats remain unchanged. (src/state/openclaw-agent-db-schema.ts:294, 9d812c0a09cf)
  • Current-main defect: Current main reads ownership and version before BEGIN, then uses those saved facts when writing schema metadata after lock acquisition. (src/state/openclaw-agent-db-schema.ts:284, 88ef99e66fc9)
  • Deterministic ownership regression: The added test commits a competing initialization through the real schema owner using a second native SQLite connection, then checks rejection, retained ownership, and registry contents; the existing cross-process claim test remains intact. (src/state/openclaw-agent-db.test.ts:2308, 9d812c0a09cf)
  • Original optimization and explained reversal: refactor(state): reduce agent database admission queries #172373 intentionally removed transaction-local checks under exclusive-custody assumptions; this PR explains why competing initial claims violate that assumption. (src/state/openclaw-agent-db-schema.ts, d6bed862664b)
  • Admission lifecycle: Schema invalidation expires schema-dependent ownership facts and forces a transaction-local catalog capture without revoking independent physical-integrity admission; the documented ownership contract treats current ownership separately from reusable integrity facts. (src/infra/sqlite-schema-facts.ts:74, 9d812c0a09cf)
  • Discussion and validation limits: The full body reports baseline failure and 113 passing focused tests; comments contain no additional runtime capture. Exact-head CI remains pending without an observed failed check. The linked main job failed its Node test step, but its redirected log host was blocked, so its failure trace was not inspected. (9d812c0a09cf)

PR surface

Source +2, Tests +44, Docs +2. Total +48 across 3 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 39 37 +2
Tests 1 44 0 +44
Docs 1 2 0 +2
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 85 37 +48

Labels

Label changes:

  • add P2: Concurrent first initialization can replace agent ownership, but the demonstrated trigger has a limited scope.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • P2: Concurrent first initialization can replace agent ownership, but the demonstrated trigger has a limited scope.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed October 16, 2026, 9:40 AM ET / 13:40 UTC.

@steipete
steipete merged commit 2ce7c92 into main Oct 16, 2026
151 checks passed
@steipete
steipete deleted the fix/agent-db-concurrent-owner branch October 16, 2026 13:51
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 17, 2026
…ion (openclaw#174251)

Read the authoritative owner, schema version and migration state inside the
schema write transaction instead of retaining observations made before BEGIN.
Invalidate only schema facts so physical integrity proof and clean-close seals
remain available. No schema or storage-format changes.

The deterministic native-handle regression fails on main in 3/3 CPU-pressure
runs; focused ownership, migration, deletion and seal suites pass 113 tests.
Core/test types, targeted lint/format and independent P2 review pass.
RomneyDa pushed a commit that referenced this pull request Oct 20, 2026
…ion (#174251)

Read the authoritative owner, schema version and migration state inside the
schema write transaction instead of retaining observations made before BEGIN.
Invalidate only schema facts so physical integrity proof and clean-close seals
remain available. No schema or storage-format changes.

The deterministic native-handle regression fails on main in 3/3 CPU-pressure
runs; focused ownership, migration, deletion and seal suites pass 113 tests.
Core/test types, targeted lint/format and independent P2 review pass.

(cherry picked from commit 2ce7c92)
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 P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: S 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