Skip to content

refactor(storage): keep lease transactions on their acquired database - #138507

Merged
steipete merged 1 commit into
mainfrom
codex/pgprep2-leases
Sep 4, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/pgprep2-leases

Conversation

@steipete

@steipete steipete commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Lease acquisition and release built their queries in the lifecycle module while reads and renewals already belonged to the lease store. The transaction helper acquired the shared database but did not pass that exact handle to its callback, leaving the ownership contract implicit.

Why This Change Was Made

Acquisition and release queries now live beside read/renew in the lease store, and the transaction callback receives the exact acquired database. The lifecycle owner retains timing, contention/backoff, worker heartbeat and cleanup responsibilities.

Expiry time is still sampled after entering the transaction. Owner comparisons, zero-busy contention behavior, worker connections and process-exit cleanup remain unchanged. This is a structural improvement; no wrong-database production failure was reproduced. There are no schema, index, configuration, public API, storage representation or concurrency-policy changes.

User Impact

Lease behavior remains the same, with an explicit connection boundary that a future database adapter can preserve.

Evidence

  • All 15 lease, worker heartbeat and process-exit tests pass. The complete required changed-file gate and fresh independent P0–P2 review pass.
  • Independent native proof at committed head 250412b7ed40906b715ce86f476a737c60964551 exercises two explicit database roots with the same lease key, wrong-root refusal, real child-process contention, expiry/replacement and stale-owner rejection. A worker heartbeat keeps a ten-second lease alive while the main event loop is blocked for 10.25 seconds. Release, worker termination, fresh-process reopen and integrity checks pass.
  • Source stayed clean and all proof state was synthetic and removed.

Production delta: +64/-49 (net +15). The growth makes the exact database handle explicit and consolidates row operations in one owner; it adds no parallel implementation or new policy. Existing tests cover the unchanged behavior.

Batch integration and CI refresh

The composed database candidate passed 489 tests across 36 files and ten test projects, 52 native phase records including real CLI/Gateway restart and fresh-process readback, and the complete changed-file gate. The branch was mechanically rebased onto the landed UI startup-budget repair (#138447); its stable patch identity is unchanged. Exact-head hosted CI is required before merge.

Exact-head native lease proof

Passed on Node v24.20.0 with real SQLite, a second Node process and the actual heartbeat worker. The fixture used two isolated synthetic database roots. Source was clean before and after:

$ git rev-parse HEAD HEAD^{tree}
9b9473f6a7f1c3dfb1f4bc26ae6f64c1c04e255a
a132e4634454b3e69021bcd6f76e546395f9cc64
$ git status --porcelain=v1 --untracked-files=normal
(empty before and after)
$ node --import ./scripts/tsx.mjs "$PROOF_SCRIPT"
exit 0

PROOF_SCRIPT denotes the task-local lease-process.mjs fixture; its absolute host path is omitted. Script SHA-256: 3430cc80d94693b513a507771c0baa5af6ae12819c8e6f7288a295c4c7489a86.

Native phase Observed output/assertions
Two explicit roots, same scope/key Both owners held independently; cross-database ownership assertion rejected; releasing one preserved the other; retained released context rejected.
Second-process contention secondProcessRefused: true; the contender received OPENCLAW_STATE_LEASE_TIMEOUT.
Replacement and expiry Stale renew/release could not alter the successor; an expired owner could not renew; expiry permitted the next owner to acquire.
Heartbeat during synchronous blocking synchronousBlockMs: 10250, leaseMs: 10000, ownershipRenewedWhileParentBlocked: true, workerTerminatedAndLeaseReleased: true.
Fresh-process reopen Both databases had zero fixture leases; SQLite quick and foreign-key checks passed. Synthetic state was removed.

Final emitted result:

{"ok":true,"proof":"two-database-leases-native-process-worker-and-fresh-reopen","noLeakedLeases":true,"quickCheck":"ok"}

Raw native-output SHA-256: d3697ce40ff36d692c36d1a3dd76170a2eec8e1b522fd187252bb86e0e9e277d. This is a native macOS process/worker proof; platform CI is separate.

@clawsweeper

clawsweeper Bot commented Sep 4, 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 size: S maintainer Maintainer-authored PR labels Sep 4, 2026
@clawsweeper

clawsweeper Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed September 4, 2026, 3:03 PM ET / 19:03 UTC.

ClawSweeper review

What this changes

The PR moves SQLite lease acquisition and release row operations into the lease store and makes lease write transactions explicitly use the database handle they first acquired.

Merge readiness

⛔ Blocked before merge - 3 items remain

No correctness defect was found in this focused lease-store refactor. Merge remains blocked on inspectable, exact-head real-behavior proof: the PR body describes a prior native run but supplies no trace, artifact, or link that can be audited for the current head.

Priority: P3
Reviewed head: 9b9473f6a7f1c3dfb1f4bc26ae6f64c1c04e255a

Review scores

Measure Result What it means
Overall readiness 🦪 silver shellfish (2/6) The refactor is coherent and source-consistent, but it lacks inspectable current-head real-behavior proof.
Proof confidence 🦪 silver shellfish (2/6) Needs stronger real behavior proof before merge: The changed production owner is the lease lifecycle’s write transaction, which now passes its acquired shared-state handle to the lease store. The PR body describes a two-root contention and stale-owner run at a different SHA, but supplies no auditable exact-head trace, logs, or artifact showing the after-fix result. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Needs proof Needs stronger real behavior proof before merge: The changed production owner is the lease lifecycle’s write transaction, which now passes its acquired shared-state handle to the lease store. The PR body describes a two-root contention and stale-owner run at a different SHA, but supplies no auditable exact-head trace, logs, or artifact showing the after-fix result. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed 6 items Changed owner boundary: The introduced transaction helper opens one state database and passes that exact handle through the database option to the write transaction; acquisition and release now execute against the callback’s database handle in the lease store.
Transaction contract: The state database layer returns an explicitly supplied database unchanged and runs the write transaction on its db field, so the new option propagation preserves the acquired connection through BEGIN IMMEDIATE and the lease query.
Existing boundary coverage: Current tests cover process-exit release and ownership revalidation inside a caller-held write transaction; heartbeat tests cover expiry, replacement, deletion, and worker loss. The PR does not alter test files.
Findings None None.
Security None None.

How this fits together

OpenClaw’s state lease subsystem coordinates exclusive work through rows in the shared SQLite state database. Lease lifecycle code acquires, renews, heartbeats, and releases those rows while the state database layer owns the connection and transaction boundary.

flowchart LR
  A[Lease caller] --> B[Lease lifecycle]
  B --> C[Shared state database]
  C --> D[Write transaction]
  D --> E[Lease store]
  E --> F[SQLite lease rows]
Loading

Before merge

  • Add real behavior proof - Needs stronger real behavior proof before merge: The changed production owner is the lease lifecycle’s write transaction, which now passes its acquired shared-state handle to the lease store. The PR body describes a two-root contention and stale-owner run at a different SHA, but supplies no auditable exact-head trace, logs, or artifact showing the after-fix result. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
  • Resolve merge risk (P1) - The current-head runtime claim cannot be audited from the supplied PR body: it names a different commit and provides no redacted native trace or artifact.
  • Complete next step (P2) - Add redacted, inspectable exact-head native runtime evidence for the lease database-root and contention scenarios before merge.
Agent review details

Security

None.

PR surface

Source +15. Total +15 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 2 64 49 +15
Tests 0 0 0 0
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 64 49 +15

Review metrics

Metric Value Why it matters
Production versus test LOC production +15 net (+64/-49); tests 0 changed The added lines make the existing database-ownership boundary explicit while consolidating acquisition and release with the existing lease-store queries.

Merge-risk options

Maintainer options:

  1. Decide the mitigation before merge
    Keep the single lease-store owner and explicit acquired-handle transaction boundary, then attach redacted exact-head runtime evidence that exercises separate database roots and the lease contention/release invariants.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Technical review

Best possible solution:

Keep the single lease-store owner and explicit acquired-handle transaction boundary, then attach redacted exact-head runtime evidence that exercises separate database roots and the lease contention/release invariants.

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

Not applicable: this PR describes a behavior-preserving ownership refactor, not a reported user-facing defect.

Is this the best way to solve the issue?

Yes: moving acquisition and release beside the existing read and renew operations gives the lease store one row-operation owner, while the lifecycle module retains scheduling, retries, and cleanup.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 38711206175b.

Labels

Label changes:

  • add P3: This is a small internal state-storage refactor with no intended user-visible behavior change.
  • add rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🐚 platinum hermit.
  • add status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs stronger real behavior proof before merge: The changed production owner is the lease lifecycle’s write transaction, which now passes its acquired shared-state handle to the lease store. The PR body describes a two-root contention and stale-owner run at a different SHA, but supplies no auditable exact-head trace, logs, or artifact showing the after-fix result. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Label justifications:

  • P3: This is a small internal state-storage refactor with no intended user-visible behavior change.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🐚 platinum hermit.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs stronger real behavior proof before merge: The changed production owner is the lease lifecycle’s write transaction, which now passes its acquired shared-state handle to the lease store. The PR body describes a two-root contention and stale-owner run at a different SHA, but supplies no auditable exact-head trace, logs, or artifact showing the after-fix result. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Evidence

What I checked:

  • Changed owner boundary: The introduced transaction helper opens one state database and passes that exact handle through the database option to the write transaction; acquisition and release now execute against the callback’s database handle in the lease store. (src/state/openclaw-state-lease.ts:193, 9b9473f6a7f1)
  • Transaction contract: The state database layer returns an explicitly supplied database unchanged and runs the write transaction on its db field, so the new option propagation preserves the acquired connection through BEGIN IMMEDIATE and the lease query. (src/state/openclaw-state-db.ts:581, 9b9473f6a7f1)
  • Existing boundary coverage: Current tests cover process-exit release and ownership revalidation inside a caller-held write transaction; heartbeat tests cover expiry, replacement, deletion, and worker loss. The PR does not alter test files. (src/state/openclaw-state-lease.test.ts:22, 9b9473f6a7f1)
  • Current-main comparison: Fetched main uses the same two pre-refactor blob IDs as the PR base, whereas the PR head uses new blobs; the refactor is not already present on current main. (src/state/openclaw-state-lease.ts:187, 38711206175b)
  • Feature history: Recent state-lease history includes the maintenance-lease renewal fix authored by Peter Steinberger, establishing a relevant current-main routing trail for this subsystem. (src/state/openclaw-state-lease.ts:321, dfd2e744caa3)
  • Proof gap: The PR body describes native proof at 250412b7ed40906b715ce86f476a737c60964551, not the reviewed head, and contains no inspectable terminal trace, artifact, or linked evidence. That commit object is unavailable in this partial checkout, so patch equivalence could not be independently checked.

Likely related people:

  • Peter Steinberger: Raw commit dfd2e74 adds src/state/openclaw-state-lease-store.ts:12 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: dfd2e744caa3; files: src/state/openclaw-state-lease-store.ts)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Add a redacted exact-head native trace or artifact showing two state-database roots, contention, expiry/replacement, and stale-owner release rejection; updating the PR body should trigger a fresh review.

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.

@steipete
steipete merged commit 4c314b3 into main Sep 4, 2026
185 checks passed
@steipete
steipete deleted the codex/pgprep2-leases branch September 4, 2026 20:20
@steipete

steipete commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer Maintainer-authored PR size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant