Skip to content

fix: agent database opens skip the integrity check for a replaced file while the Gateway holds it - #183052

Merged
steipete merged 3 commits into
mainfrom
steipete/agent-db-borrow-provenance
Oct 25, 2026
Merged

steipete merged 3 commits into
mainfrom
steipete/agent-db-borrow-provenance

Conversation

@steipete

@steipete steipete commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Related: #182984

What Problem This Solves

Fixes: since #182984, a process opening an agent database while the Gateway holds it could skip the integrity check for a different file that was renamed over the same path.

User Impact

Agent database opens again verify any file the live Gateway did not itself admit, while CLI runs against the Gateway's own file keep skipping the redundant multi-minute scan on Linux.

Why This Change Was Made

ClawSweeper's review of #182984 found that the borrow rule matched the peer by path, completed admission, and PID start time, but not by physical file. A file renamed over the path while the peer is alive could therefore borrow the peer's admission.

Borrowing now also requires the peer's recorded lease provenance to equal the claimant's. Provenance is an existing lease field that hashes boot, PID namespace, build, and file device:inode. The claim hands that value to the opener, which re-reads it after the native SQLite open and borrows only if it still matches, so a swap between claim and open also gets the full check (raised by an independent Codex review of this fix). Lease provenance is Linux-only, so other platforms fall back to the full check as before #182984; the Team Gateway, where the scans were measured, runs Linux.

Accepted residual: node:sqlite exposes no file descriptor, so identity is a path re-stat after open; a file renamed back to the original inode between the two reads is not detected.

Evidence

  • New does not lend a live peer admission to a file replaced at the same path: a live child holds the admitted database, an index-corrupted copy is renamed over the path, and a fresh open must reject it. Fails on current main (opens without a check), passes with this change.
  • New borrows a live peer admission only for the file opened after the claim: provenance stubbed so the claim matches and the post-open read sees a swapped file. The swapped case fails when the post-open re-check is disabled and passes with it. The unswapped case borrows and fails when borrowing is disabled. A borrower closing last publishes no clean-close seal; a process that ran the full check does.
  • Lease unit cases: matching provenance lends; a replaced file, missing provenance, unfinished admission, or alias path does not.
  • The real child-process borrow test from fix: CLI runs stall for minutes scanning an agent database the Gateway already opened #182984 now runs on Linux only, since that is where provenance exists. A first child creates the file (a lease claim records provenance only for an existing file), and the tests remove that child's clean-close seal so the result cannot come from seal reuse instead of borrowing. Earlier CI runs of this PR caught both setup mistakes; neither affected production code.
  • openclaw-agent-db.integrity-lease, openclaw-agent-db-lease, integrity-cache, open-timing, server-close.agent-databases, and sqlite-integrity suites pass; tsgo:core and the core oxlint shard pass; independent Codex review (P0–P2) scoped-clean after one accepted finding.

A process reusing a live peer's admission (#182984) matched the peer by
path, completed admission and PID start time only. If a different file
was renamed over the path while the peer stayed alive, a fresh process
could borrow the peer's admission and skip integrity_check for a file
nobody verified.

Borrowing now also requires the peer's lease provenance (boot, PID
namespace, build, file inode) to equal the claimant's, and the opener
re-reads provenance after the native open so a file swapped between
claim and open still gets the full check. Provenance exists only on
Linux, so other platforms fall back to the full check.
@clawsweeper

clawsweeper Bot commented Oct 25, 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 25, 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-25T02:08:11.874594Z 3f0ff0f New commits
ℹ️ 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bedd2ee72f

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +237 to +238
held.provenance !== null &&
held.provenance === owner.provenance &&

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Record file provenance after admitting a newly created database

On Linux, when the Gateway creates an agent database, its lease is claimed before the native SQLite open creates the file, so readAgentDatabaseLeaseProvenance() records null. recordOpenClawAgentDatabaseAdmission() subsequently updates only opened_at, leaving that live lease permanently unable to satisfy this new condition. Separate CLI processes therefore resume full integrity scans until the creating handle closes, even though the Gateway admitted the same file. This also affects the existing reuses a live peer process admission instead of rescanning the file test, whose child creates the database from an empty state directory. Publish the admitted file's provenance when completing admission, while retaining the replacement-file checks.

Useful? React with 👍 / 👎.

@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. P2 Normal backlog priority with limited blast radius. 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. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Oct 25, 2026
@clawsweeper

clawsweeper Bot commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge.

What this changes

Bind borrowed agent-database admission to matching physical-file provenance and recheck it after SQLite opens.

Example: An index-corrupted replacement is renamed over agent.sqlite while the Gateway holds the original file.

  • Before: A new process can borrow the Gateway’s admission and skip checking the replacement.
  • After: Different file provenance prevents borrowing, so integrity verification rejects the corrupted replacement.

Review scores

Measure Result What it means
Overall readiness 🦪 silver shellfish (2/6) Useful, focused repair with production-owner regression coverage, but an introduced defect is confirmed by CI; the diff changes no persisted format or schema.
Proof confidence 🦐 gold shrimp (3/6) This maintainer-authored PR is outside the ordinary contributor proof gate; its supplied evidence exercises the database opener with real child processes, while current-head CI exposes the fresh-file regression.
Patch quality 🦪 silver shellfish (2/6) 1 actionable review finding remain.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: A process can accept an unchecked replacement agent database by borrowing admission for the original file.
Reason: The maintainer-authored direction restores the existing physical-file integrity invariant without new configuration or another state owner.

Regression provenance

Possible regression — probable (reviewed change; failure trace). No predecessor PR is attributed.

Merge readiness

⛔ Needs changes before merge - 1 item remains

Keep open: this PR repairs a real integrity-admission defect, but introduces a fresh-database performance regression confirmed by current-head CI.

Priority: P2
Reviewed head: bedd2ee72f60c03cca69f19037a2c5d7ea6c1fd7

Before merge

  • Publish file provenance when a newly created database finishes admission (P2) - On Linux, opening a previously absent agent database claims its lease before SQLite creates the file, so the stored provenance is null. Admission completion updates only opened_at. This new non-null requirement therefore prevents every separate CLI process from borrowing that live owner's admission until its handle closes, restoring repeated full scans as the database grows. The existing fresh-directory child-process test fails on this exact head with healthy instead of cached admission, as confirmed by CI. Record provenance for the admitted file through the completion owner, retaining the replacement-file safeguards. This also confirms the concern in fix: agent database opens skip the integrity check for a replaced file while the Gateway holds it #183052 (comment).

Findings

  • [P2] Publish file provenance when a newly created database finishes admission — src/state/openclaw-agent-db-lease.ts:237-238

Tests

  • Missing end-to-end proof: No shipped Gateway or CLI demonstration of this repair is supplied; the child-process regression covers replacement rejection, while current-head CI disproves fresh-file reuse.
Agent review details

How this fits together

Gateway and CLI database opens claim shared-state leases, consult reusable admission facts, and pass through schema and integrity validation before exposing a database connection.

flowchart TD
 A[Gateway or CLI] --> B[Agent database opener]
 B --> C[Shared lease owner]
 C --> D[File provenance comparison]
 D --> E[Schema and integrity gate]
 E --> F[Admitted database connection]
 F --> G[Close and seal owner]
Loading

Technical review

Best possible solution:

Publish the admitted physical file’s provenance through the existing admission-completion owner while preserving claim-time and post-open replacement checks.

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

Source establishes the replacement-file bypass on fetched main; the supplied corrupted-replacement regression exercises that boundary, and current-head CI independently confirms the newly introduced fresh-file borrowing failure.

Is this the best way to solve the issue?

Reusing existing provenance and admission owners is the smallest appropriate repair, but provenance must also be published when a previously missing database finishes admission.

Full review comments:

  • [P2] Publish file provenance when a newly created database finishes admission — src/state/openclaw-agent-db-lease.ts:237-238
    On Linux, opening a previously absent agent database claims its lease before SQLite creates the file, so the stored provenance is null. Admission completion updates only opened_at. This new non-null requirement therefore prevents every separate CLI process from borrowing that live owner's admission until its handle closes, restoring repeated full scans as the database grows. The existing fresh-directory child-process test fails on this exact head with healthy instead of cached admission, as confirmed by CI. Record provenance for the admitted file through the completion owner, retaining the replacement-file safeguards. This also confirms the concern in fix: agent database opens skip the integrity check for a replaced file while the Gateway holds it #183052 (comment).
    Confidence: 0.99

Overall correctness: patch is incorrect
Overall confidence: 0.98

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 9289367925e6.

Provenance checked

  • Live-peer integrity reuse changes intended behavior with a stated reason (0a59270: Avoid repeated full integrity scans when another live process already admitted the database.)
  • Lease provenance and admission completion keeps the original intent (147975c: Bind reusable crash-admission evidence to host, boot, PID namespace, build, and physical file while recording completed admission separately.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

Acceptance criteria:

  • pnpm test src/state/openclaw-agent-db.integrity-lease.test.ts --maxWorkers=1
  • pnpm test src/state/openclaw-agent-db-lease.test.ts --maxWorkers=1
  • pnpm test src/state/openclaw-agent-db.integrity-cache.test.ts --maxWorkers=1
  • pnpm test src/state/openclaw-agent-db.open-timing.test.ts --maxWorkers=1
  • git diff --check

What I checked:

Likely related people:

  • Omar Shahine: Raw commit 70c2e66 adds src/state/openclaw-agent-db-lease.ts:64 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: medium; commits: 70c2e666ec6a; files: src/state/openclaw-agent-db-lease.ts)

PR surface

Source +12, Tests +87, Docs +3. Total +102 across 5 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 2 23 11 +12
Tests 2 116 29 +87
Docs 1 18 15 +3
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 5 157 55 +102

Labels

Label changes:

  • add P2: This is a focused integrity-admission repair with a concrete performance regression for databases created by a live owner.
  • add rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦐 gold shrimp and patch quality is 🦪 silver shellfish.
  • add status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action.

Label justifications:

  • P2: This is a focused integrity-admission repair with a concrete performance regression for databases created by a live owner.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦐 gold shrimp and patch quality is 🦪 silver shellfish.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action.

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 24, 2026, 8:38 PM ET / October 25, 2026, 00:38 UTC.

@steipete
steipete merged commit feee07c into main Oct 25, 2026
251 of 257 checks passed
@steipete
steipete deleted the steipete/agent-db-borrow-provenance branch October 25, 2026 03:31
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via explicitly authorized admin squash with attributed pre-existing CI failures.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 25, 2026
…e while the Gateway holds it (openclaw#183052)

* fix(state): bind borrowed agent database admission to the peer's file

A process reusing a live peer's admission (openclaw#182984) matched the peer by
path, completed admission and PID start time only. If a different file
was renamed over the path while the peer stayed alive, a fresh process
could borrow the peer's admission and skip integrity_check for a file
nobody verified.

Borrowing now also requires the peer's lease provenance (boot, PID
namespace, build, file inode) to equal the claimant's, and the opener
re-reads provenance after the native open so a file swapped between
claim and open still gets the full check. Provenance exists only on
Linux, so other platforms fall back to the full check.

* test(state): let a separate process create the borrowed database

* test(state): prove borrowing without the creator's clean-close seal
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: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: S 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