Repository navigation
fix(state): tolerate a sidecar deleted while hardening the state database - #125451
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. |
|
Codex review: needs maintainer review before merge. Reviewed August 29, 2026, 10:00 AM ET / 14:00 UTC. ClawSweeper reviewWhat this changesThe branch tolerates an SQLite WAL, SHM, or journal sidecar disappearing during shared state-database permission hardening while keeping a disappearing main database fatal. Regression provenancePossible regression — probable (reviewed change; failure trace). No predecessor PR is attributed. Merge readinessKeep this PR open: current main still has the check-then-chmod race, and the branch repairs it at the shared-state permission owner without weakening the main database loss boundary. No blocking correctness finding was identified; wait for exact-head CI before merge. Priority: P1 Review scores
Verification
How this fits togetherGateway and CLI startup open OpenClaw’s shared SQLite state database after hardening its directory, main file, and transient sidecars. This permission step feeds directly into the state database used by later runtime and session operations. flowchart LR
A[Gateway or CLI startup] --> B[Shared state database opener]
B --> C[Permission hardening]
C --> D{Existing SQLite candidate}
D --> E[Main database errors stay fatal]
D --> F[Vanished sidecar is skipped]
E --> G[Open shared SQLite state]
F --> G
Before merge
Agent review detailsSecurityNone. PR surfaceSource +12, Tests +46. Total +58 across 2 files. View PR surface stats
Review metrics
Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Keep the sidecar-only catch in the shared-state permission owner: a global ENOENT exemption would weaken unrelated credentials-adjacent chmod failures, while copying the per-agent sweep would break its pre-create contract. Do we have a high-confidence way to reproduce the issue? Yes, at source level: the changed test creates each sidecar, removes it in the existence-to-chmod interval, and exercises the production state opener. Supplied two-process evidence corroborates the real cross-process race. Is this the best way to solve the issue? Yes. The shared-state permission sweep is the only owner that knows both that the candidate was observed and whether it is the durable main database or a transient sidecar. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 55f127f10294. LabelsLabel justifications:
EvidenceWhat I checked:
Likely related people:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (11 earlier review cycles; latest 8 shown)
|
22096a3 to
bf708a9
Compare
bf708a9 to
3e06b08
Compare
3e06b08 to
2514827
Compare
2514827 to
6e42b7f
Compare
|
Both P1 items addressed in Keeping the main database fatal. The finding was right, and it was a regression I introduced. if (candidate === pathname || !hasErrnoCode(error, "ENOENT")) {
throw error;
}New regression test: Real behavior proof. You asked for a real state database surviving removal of a real sidecar rather than mocked chmod faults. Worth noting why the shape is what it is: Same harness, only Main database present at the end of both runs. Paths redacted, isolated dir under My first attempt at this proof was vacuous and I nearly posted it: I opened and closed the database in a loop, got 400 clean opens, and only noticed because the counter I had added reported
|
6e42b7f to
af89e34
Compare
|
Rebased to I applied exactly the suggested change to The reason is a precondition difference I had not noticed until it broke:
So on the per-agent path a main-file ENOENT is the ordinary pre-create case: callers ensure permissions on a database path that does not exist yet, and the swallow is what makes that work. Making it fatal turns first-run and preflight into hard failures, which is what those 12 tests are. Recovering the same protection there would need the file's existence established before the sweep, which is the TOCTOU window that comment is explicitly avoiding. That looks like a real design question about the per-agent sweep rather than a mechanical mirror of this PR, so I do not think it belongs in this change. I have reverted the attempt; the branch carries only the shared-state repair it started with. Happy to be told I have misread the per-agent contract, or to open a separate issue for it if you would rather track it. Branch state at |
af89e34 to
e58fbb8
Compare
b4e63b5 to
44d137d
Compare
|
Rebased onto current All three are guard/automation workflows that call the GitHub API and got 500s or a temporarily-unavailable diff back. None of them ran a test, and there is no test failure anywhere in those jobs. Worth noting for anyone triaging by check colour: these are exactly the checks a reader would expect to mean "this PR touches security-sensitive paths and failed review". They do not. The substantive state of the PR is unchanged from the last review round: the main-file ENOENT boundary is scoped to sidecars, the regression fails on the unfixed condition, and the two-process real-relay proof is in the body. Nothing to push. These should clear on any re-run that lands while the API is healthy. |
44d137d to
b55af76
Compare
|
The three red checks here are all the same GitHub-side failure, not this branch.
This is the second time these three have gone red this way here, so rather than leave the marks sitting, I rebased onto current The change itself is unchanged: one |
…base ensureOpenClawStatePermissions checked existsSync on each SQLite sidecar and then chmodded it. SQLite deletes -wal and -shm at checkpoint or close, so a concurrent opener can remove a sidecar inside that window. ENOENT is not in CHMOD_UNSUPPORTED_CODES, so applyPrivateModeSync rethrew it and the database open failed outright. A sidecar that no longer exists has nothing left to harden, so treat ENOENT as a skip and keep every other chmod fault fatal. The agent database sweep in openclaw-agent-db-permissions.ts already does this; the state sweep was left behind when both files were split out of their monoliths. Keeps the existsSync fast path: unlike the agent sweep, the state sweep classifies EPERM and EROFS through hasRestrictivePermissions, which needs the target to exist, so chmodding absent candidates would make those codes fatal.
… guard The assertion-safety ratchet grandfathers this file at zero uncommented type assertions, and `(error as NodeJS.ErrnoException).code` added one. src/infra/errno.ts already owns this narrowing, so use hasErrnoCode instead of annotating a cast.
…cars resolveSqliteDatabaseFilePaths lists the unsuffixed database first, so the ENOENT skip added for transient sidecars also swallowed the main database disappearing between the existence check and the chmod. That turned a fail-loud boundary into a silent fall-through to a SQLite open that would create a fresh empty database instead of surfacing the loss. Confine the skip to sidecar candidates and cover the main-file case.
b55af76 to
1ff92f6
Compare
|
Thanks @ayaangazali! Proof: Exact current main failed the real shared-state open when each existing SQLite sidecar disappeared before
Example
|
Keep shared state database opens alive when SQLite removes a transient WAL, shared-memory, or rollback-journal sidecar during permission hardening. Ignore only sidecar `ENOENT` after a positive existence check. Keep main database loss and every other permission error fatal. Co-authored-by: Ayaan Gazali <[email protected]> Co-authored-by: Ayaan Zaidi <[email protected]>
Problem
OpenClaw can abort a shared state database open when SQLite removes an optional
-wal,-shm, or-journalsidecar after the permission sweep observes it but beforechmodruns.Root cause
The shared state permission owner checked each SQLite file with
existsSyncand then hardened it. SQLite can remove sidecars inside that time-of-check/time-of-use window. The generic permission helper correctly treatsENOENTas fatal because other credentials-adjacent targets are required.Fix
The shared state permission owner now ignores
ENOENTonly for a suffixed SQLite sidecar that vanished after the existence check. It still rethrows main databaseENOENTand every other permission error. This prevents a missing main database from falling through to a writable SQLite open that could create an empty replacement.Proof
chmod; the real state database open aborted withENOENTfor-wal,-shm, and-journal.33256187233passed for1ff92f61d55bd2214a83561b9373b998b8568f7d.Change size
The production growth keeps the exception at the only boundary that knows the main file from optional SQLite sidecars. Moving
ENOENTinto the generic permission helper would weaken unrelated required-file checks.AI-assisted: yes. The contributor and maintainer verified the failure path, SQLite lifecycle contract, owner boundary, and exact-head behavior.