Repository navigation
fix: recreate agent database after explicit close - #132955
jerrodcodes wants to merge 1 commit into
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Thanks for the context here. I swept through the related work, and this is now duplicate or superseded. Close this PR as superseded by #137424, whose merged repair covers explicit-close recreation and remains on current main. Root-cause cluster Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Canonical path: Keep the merged shared-validation lifecycle and connection-based schema checks, and close this redundant branch. So I’m closing this here and keeping the remaining discussion on #137424. Review detailsBest possible solution: Keep the merged shared-validation lifecycle and connection-based schema checks, and close this redundant branch. Do we have a high-confidence way to reproduce the issue? No current-main failure was executed in this read-only review; source and retained tests explicitly cover the reported close/delete/reopen sequence. Is this the best way to solve the issue? The merged repair is the better current solution: it handles registry lifecycle transitions and empty replacements at their existing owners, while preserving ordinary reopen performance without this branch's separate identity bookkeeping. Security review: Security review cleared: The introduced patch retains ownership checks and adds no dependency, credential, permission, or authorization surface. AGENTS.md: found and applied where relevant. What I checked:
Codex review notes: model internal, reasoning medium; reviewed against 7a16e5678921. |
Yigtwxx
left a comment
There was a problem hiding this comment.
Adding a platform data point the checks on head 9327d90357f do not have: all 82 successful runs on that commit are Linux, and the identity this PR keys on behaves differently on NTFS.
Probe on Windows 11 (Node 22.23.2, fs.statSync(path, { bigint: true })): write a file, delete it, write it again under the same name, three times in a row.
| recreation | ino changed |
birthtimeNs changed |
|---|---|---|
| immediate | yes | no, identical to the ns |
| after 100 ms | yes | no, identical to the ns |
That is NTFS file-system tunneling: a file recreated under a recently deleted name inherits the old creation time. So on Windows the birthtimeNs half of isSameAgentDatabaseFileIdentity never discriminates; dev + ino does all the work, and it is sufficient because the Windows ino is the 64-bit file reference including the MFT sequence number, which changes on reuse. Net effect: the new test passes here (1/1 green, and 1/1 red with openclaw-agent-db.ts reverted to main: expected {} to deeply equal { …(49) }), so the fix holds on NTFS, just not for the reason the body gives.
The direction that can hurt is the opposite one. Node's Stats documentation states that on filesystems without a birth time the field "may instead hold either the ctime or 1970-01-01T00:00Z". On the ctime variant any write to the database moves birthtimeNs, so isValidatedReopen is false on every explicit reopen of an unchanged file and each one takes registerOpenClawAgentDatabase plus the write-capable schema path, which is exactly the regression the body says the identity check avoids. The epoch variant is harmless (equal on both sides). I cannot tell you which of the filesystems OpenClaw runs on report ctime there, so I would either drop the birth-time term and accept the rapid same-inode window that dev + ino leaves open, or keep it and say in the comment above AgentDatabaseFileIdentity that on ctime-reporting filesystems the fast reopen path is lost rather than corrupted. Either is a one-line decision for the maintainer; the schema-recreation fix itself is right and the branch is still MERGEABLE/CLEAN after the three commits main has since made to this file.
|
ClawSweeper recorded closeout evidence for this proposed close.
|
|
Spent a lot of time on this and it was an actual bug but okay. Thanks for the communication. |
Related: #129185
Related: #131331
What Problem This Solves
Fixes an issue where users reopening an agent database after an explicit close could get an uninitialized replacement database when the original SQLite file had been deleted or recreated during the same process.
Why This Change Was Made
Successful schema validation is now retained only while the pathname still identifies the same filesystem object. An unchanged database keeps the fast read-only reopen path, while a replaced or recreated file falls back to full owner/schema initialization. The identity check uses the repository's canonical filesystem identity helper plus birth time to distinguish rapid inode reuse. This does not change the database schema.
User Impact
Agents can recover correctly when their database is explicitly closed and then replaced or recreated, without regressing prompt reopen behavior for an unchanged WAL database with another writer active.
Evidence
session_key_contract.src/state/openclaw-agent-db.test.ts— 121 passed, 4 skipped.scripts/check-changed.mjs -- src/state/openclaw-agent-db.ts src/state/openclaw-agent-db.test.tspassed, including formatting, lint, core/test typechecks, import-cycle checks, and database guards.pnpm buildpassed under Node 24.15.0.openclaw/ci-gateandcheck-sqlite-session-lifecycle.git diff --checkpassed.Direct runtime proof
Executed against this PR's production database opener under Node 24.15.0 using an isolated temporary
OPENCLAW_STATE_DIR. The script opened and explicitly closed an agent database, removed its database/WAL/SHM files, reopened it, and queried the resulting live SQLite schema. It then explicitly closed and reopened the unchanged file while an independent connection held a WALBEGIN IMMEDIATEwriter:{ "explicitCloseSucceeded": true, "recreatedSchema": { "open": true, "userVersion": 19, "tableCount": 49, "sessionKeyContract": { "name": "session_key_contract" } }, "unchangedWalResult": { "open": true, "elapsedUnderOneSecond": true, "elapsedMs": 13.8 } }Production code grows to retain and compare a bounded filesystem identity. That state is required to distinguish replacement from an unchanged explicit reopen without forcing every reopen through the write-capable schema path.
AI assistance disclosure: OpenClaw-managed Codex was used for review and identified the unchanged-WAL reopen regression, which is covered on current
main. The repository's standaloneautoreviewhelper was unavailable because it uses separate CLI authentication; submission was explicitly authorized with that limitation.