Skip to content

fix(sqlite): prevent writes after failed nested rollback - #139700

Merged
steipete merged 5 commits into
mainfrom
codex/db-next-recovery-land
Sep 6, 2026
Merged

steipete merged 5 commits into
mainfrom
codex/db-next-recovery-land

Conversation

@steipete

@steipete steipete commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes an issue where a failed nested SQLite write could hide the original disk-full error and allow a caller that caught it to persist later writes outside the intended transaction. Doctor plugin-state imports could also treat an aborted batch as a resumable row refusal.

Why This Change Was Made

The transaction owner now preserves the first terminal failure, discards staged state and post-commit observers, and closes a connection when rollback or savepoint recovery fails. A later operation acquires a fresh connection through its database owner. Ordinary savepoint rollback and successful-prefix import behavior remain intact.

Maintainer decision, September 5: accepted this scoped recovery repair as part of the SQLite cleanup campaign. It changes failed-transaction recovery only; there is no schema, version, configuration, retention, or durable representation change. Earlier completed import batches remain durable and the aborted batch contributes no rows. Reverting the source needs no migration, but restores the demonstrated unsafe continuation.

User Impact

Storage failures retain their useful original cause. Catching a nested failure cannot accidentally commit subsequent writes or publish notifications for rolled-back state. Doctor can reacquire the database and resume from earlier committed batches.

Evidence

  • The original file-backed SQLITE_FULL reproduction persisted a continuation row after SQLite had aborted the enclosing transaction. The candidate preserves errcode 13, refuses continuation, reopens cleanly, and passes integrity_check on Node 24.20.0 and 26.8.1.
  • Four new regressions failed before the repair. All 66 focused and sibling tests pass, covering ordinary rollback, failed rollback/release, staged-state ordering, transformed SDK identity, and importer abort/reopen/resume.
  • Final composition includes main 29d30210a930feba3cff57bc732e906ddd320643, preserving writer admission. Terminal failures are also rechecked at async entry, after preparation, and after retry waits before deadline handling. Native Node 24/26 probes preserve the original SQLite error across each boundary and reopen with no aborted rows. All 33 focused transaction/import tests, the full changed-file gate, and the final independent P0–P2 review pass. Earlier CI failures came from main's stale suppression inventory and renamed session-history fixture fields; both canonical upstream fixes are included. The session-history fixture is identical to main.
  • Local transaction microbenchmarks: successful median 1.429 → 1.461 μs; nested 2.107 → 2.121 μs. Shared-host measurements are not production throughput guarantees.

Proof uses synthetic native databases, not production state or an external provider. SQLite documents the automatic-rollback behavior in transaction error handling and savepoint semantics.

@clawsweeper

clawsweeper Bot commented Sep 6, 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 size: M maintainer Maintainer-authored PR labels Sep 6, 2026
@steipete
steipete marked this pull request as ready for review September 6, 2026 03:56
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Sep 6, 2026
@clawsweeper

clawsweeper Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 6, 2026, 4:54 AM ET / 08:54 UTC.

ClawSweeper review

What this changes

The PR prevents continued writes after failed SQLite rollback, preserves the original error, discards staged notifications, and prevents Doctor imports from committing an aborted batch.

Merge readiness

✅ Ready for maintainer review

The repair remains necessary on current main. No actionable introduced defect was found, and the PR records acceptance of its scoped recovery behavior.

Priority: P2
Reviewed head: 8408ea830f22b20134625fcaf8367a4f788dd590

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused owner-level repair with meaningful regression coverage and no identified blocking defect.
Proof confidence 🌊 off-meta tidepool Not applicable: The maintainer-authored PR is exempt from the external-contributor proof gate. Its body reports native file-backed fault probes covering original-error preservation, refused continuation, and clean reopening; regression tests provide supplemental coverage, and no runtime probe was executed during this review.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The maintainer-authored PR is exempt from the external-contributor proof gate. Its body reports native file-backed fault probes covering original-error preservation, refused continuation, and clean reopening; regression tests provide supplemental coverage, and no runtime probe was executed during this review.
Evidence reviewed 8 items Pinned introduction boundary: The complete host-provided introduction delta and local comparison identify only the SQLite helpers, their regression tests, the plugin-state importer, and database documentation. Unrelated endpoint drift was excluded. The checkout remained clean.
Current main still needs the repair: Current main performs nested ROLLBACK TO followed by RELEASE in a finally block without marking or closing the abandoned connection. It lacks the proposed terminal-error checks across asynchronous preparation and admission waits.
Terminal failure and staged-state cleanup: The transaction owner records the first terminal failure, clears staged state and cached queries, closes the connection, and rejects reuse. Staged callbacks explicitly have a nonthrowing contract; their production relocation caller restores map state without database I/O.
Findings None None.
Security None None.

How this fits together

OpenClaw’s shared SQLite transaction helpers coordinate database writes and publish staged state after commit. Plugin-state imports use these helpers to process resumable batches while database owners manage connection recovery.

flowchart TD
  A[Database writes and Doctor imports] --> B[Shared transaction helper]
  B --> C[Nested savepoint]
  C --> D{Rollback succeeds?}
  D -->|Yes| E[Preserve outer transaction]
  D -->|No| F[Discard staged state and close connection]
  F --> G[Report original failure]
  G --> H[Database owner acquires fresh connection]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source +49, Tests +244, Docs +2. Total +295 across 6 files.

View PR surface stats
Area Files Added Removed Net
Source 3 64 15 +49
Tests 2 244 0 +244
Docs 1 2 0 +2
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 6 310 15 +295

Review metrics

Metric Value Why it matters
Production versus regression coverage Production net +49 lines; tests +244 lines Production growth implements shared terminal-failure handling, with most added coverage exercising rollback and recovery boundaries.

Technical review

Best possible solution:

Keep failed-transaction recovery in the shared transaction owner, preserving ordinary savepoint behavior and reacquiring abandoned connections through existing database owners.

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

Yes, from source: force SQLITE_FULL inside a nested transaction, catch the failure, and attempt another write after SQLite has discarded the transaction. The supplied native reproduction and regression cases target this path; this read-only review did not execute them.

Is this the best way to solve the issue?

Yes. The patch repairs the shared lifecycle owner and the importer’s error classification while preserving ordinary rollback, existing batch durability, and the database format.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 7b481f3e2e38.

Labels

Label justifications:

  • P2: This is a bounded storage-integrity repair triggered by failed transaction cleanup, without evidence of an ongoing widespread outage.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The maintainer-authored PR is exempt from the external-contributor proof gate. Its body reports native file-backed fault probes covering original-error preservation, refused continuation, and clean reopening; regression tests provide supplemental coverage, and no runtime probe was executed during this review.

Evidence

What I checked:

  • Pinned introduction boundary: The complete host-provided introduction delta and local comparison identify only the SQLite helpers, their regression tests, the plugin-state importer, and database documentation. Unrelated endpoint drift was excluded. The checkout remained clean. (8408ea830f22)
  • Current main still needs the repair: Current main performs nested ROLLBACK TO followed by RELEASE in a finally block without marking or closing the abandoned connection. It lacks the proposed terminal-error checks across asynchronous preparation and admission waits. (src/infra/sqlite-transaction.ts, 7b481f3e2e38)
  • Terminal failure and staged-state cleanup: The transaction owner records the first terminal failure, clears staged state and cached queries, closes the connection, and rejects reuse. Staged callbacks explicitly have a nonthrowing contract; their production relocation caller restores map state without database I/O. (src/infra/sqlite-transaction.ts:279, 8408ea830f22)
  • Existing connection recovery owner: The shared state database owner returns cached connections only while open and disposes stale cached handles before reopening. The importer now propagates lost-transaction failures while retaining its existing successful-prefix path for ordinary row refusals. (src/state/openclaw-state-db.ts:590, 8408ea830f22)
  • Regression coverage: Inspected tests exercise native SQLITE_FULL, failed savepoint cleanup, original-error identity, blocked continuation, reverse-order staged rollback, asynchronous admission boundaries, and reopening. Import coverage distinguishes RAISE(ABORT) prefix retention from RAISE(ROLLBACK), which retains only the earlier 500-row batch before resuming. Tests were inspected, not executed. (src/plugin-state/plugin-state-store.import.test.ts:82, 8408ea830f22)
  • SQLite dependency contract: The patch directly uses SQLite savepoints and relies on automatic transaction rollback. Official transaction documentation confirms SQLITE_FULL can cancel the entire transaction; savepoint documentation confirms cleanup fails when the named savepoint no longer exists. No Codex dependency contract is implicated.

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • Jason (Json): Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

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 (8 earlier review cycles)
  • reviewed 2026-09-06T04:01:26.113Z sha 5776b23 :: blocked before merge. :: none
  • reviewed 2026-09-06T04:38:02.829Z sha 31d9114 :: needs maintainer review before merge. :: none
  • reviewed 2026-09-06T04:46:59.136Z sha 31d9114 :: needs maintainer review before merge. :: none
  • reviewed 2026-09-06T05:13:21.779Z sha e5bf761 :: needs maintainer review before merge. :: none
  • reviewed 2026-09-06T06:18:21.491Z sha 904a69f :: blocked before merge. :: none
  • reviewed 2026-09-06T06:46:45.181Z sha 904a69f :: blocked before merge. :: none
  • reviewed 2026-09-06T07:26:42.346Z sha 904a69f :: blocked before merge. :: none
  • reviewed 2026-09-06T08:47:40.302Z sha 8408ea8 :: needs maintainer review before merge. :: none

@openclaw-barnacle openclaw-barnacle Bot added the gateway Gateway runtime label Sep 6, 2026
@steipete steipete closed this Sep 6, 2026
@steipete steipete reopened this Sep 6, 2026
@openclaw-barnacle openclaw-barnacle Bot closed this Sep 6, 2026
@openclaw-barnacle openclaw-barnacle Bot reopened this Sep 6, 2026
@openclaw-barnacle openclaw-barnacle Bot closed this Sep 6, 2026
@openclaw-barnacle openclaw-barnacle Bot reopened this Sep 6, 2026
@openclaw-barnacle openclaw-barnacle Bot removed the gateway Gateway runtime label Sep 6, 2026
@steipete
steipete merged commit 3d0254a into main Sep 6, 2026
184 checks passed
@steipete
steipete deleted the codex/db-next-recovery-land branch September 6, 2026 09:08
@steipete

steipete commented Sep 6, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 7, 2026
…9700)

* fix(sqlite): prevent writes after failed nested rollback

* test(gateway): derive history fixture state from its producer
necromman pushed a commit to necromman/openclaw that referenced this pull request Sep 20, 2026
…9700)

* fix(sqlite): prevent writes after failed nested rollback

* test(gateway): derive history fixture state from its producer

(cherry picked from commit 3d0254a)
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 maintainer Maintainer-authored PR P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: M 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