Skip to content

fix: agent turns recover when database worker exits between operations - #163258

Merged
steipete merged 3 commits into
openclaw:mainfrom
goutamadwant:fix/native-owner-preflight-recovery
Oct 5, 2026
Merged

steipete merged 3 commits into
openclaw:mainfrom
goutamadwant:fix/native-owner-preflight-recovery

Conversation

@goutamadwant

@goutamadwant goutamadwant commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #163151

What Problem This Solves

Fixes: an agent turn fails when its retained database worker exits between operations, even though the pending operation has not been dispatched.

User Impact

The pending operation can run on a fresh native database generation without requiring the user to resend the turn. Already-dispatched operations are not replayed. No schema, durability, retention, configuration, or migration changes are required.

Why This Change Was Made

The execution owner settles the failed generation's native close and lease cleanup before admitting replacement work, then rechecks owner, source, caller, and cancellation authority. The prepared-generation fast path also reports an unavailable generation as unprepared so its caller can take the existing preparation path. Previously captured generation claims remain invalid after recovery.

Original fix by @goutamadwant; maintainer cleanup simplifies the close call, preserves the retirement invariant in a concise comment, adapts the fix to the current native-generation contract, and extends the existing native-worker regression to both prepare and runExisting. Main was integrated through a normal merge commit; contributor history is preserved.

Evidence

Current proof is bound to pushed head 051376e82d032b0ecaccdbfae90860e9182ecaed.

  • Isolated Crabbox AWS: lease cbx_6070fd20640d, Linux, Node 24.21.0, pnpm 12.5.1. Trusted-main bootstrap verified the full head and IMDSv2 no-role boundary; no hydration, no Tailscale, isolated HOME, and only CI forwarded. Lease stopped and confirmed released.
  • pnpm tsgo:core — passed in 17.15 seconds on that lease.
  • pnpm test src/state/openclaw-agent-execution.close-wedge.test.ts --maxWorkers=1 — 8 passed. The tests terminate a real native worker, recover through both entry points, read through the replacement, and verify the old claim cannot become valid again.
  • Revert-fails proof on the same lease: restored only src/state/openclaw-agent-execution.ts from main commit f12c30cd91c8f952e573536d9a242da0fb68f850, then ran pnpm test src/state/openclaw-agent-execution.close-wedge.test.ts --maxWorkers=1 -t "reopens a native generation lost between operations before dispatch". Both new cases failed with Agent database execution lost its native owner: the prepared-claim path and pre-dispatch runExisting path. Restored the pushed production file afterward.
  • Exact-head CI — succeeded, including production types. Changed-test job — 8/8 passed.
  • Local oxfmt, direct oxlint with the core tsconfig, and git diff --check passed. Repository lint/type wrappers refused the shared dependency layout; the remote typecheck and CI supply that proof. Independent review completed with no actionable P0–P2 findings.

LOC versus origin/main: production +6/−8 (net −2); tests +43/−0.

Test cost: pnpm test src/state/openclaw-agent-execution.close-wedge.test.ts --maxWorkers=1 took 46.85 seconds wall time in isolated AWS, including compiled-worker preparation; Vitest duration was 20.60 seconds. The two new cases took 1.90 and 1.91 seconds. CI recorded 15.62 seconds of Vitest duration and 43.93 seconds including the test wrapper. Real worker termination and native lease settlement require this boundary; mocks would not establish that custody has ended before replacement.

The original production exit cause and long-running live Gateway behavior were not reproduced. This proves the worker-loss transition and recovery of an operation that had not yet been dispatched.

@clawsweeper

clawsweeper Bot commented Oct 2, 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.

@clawsweeper clawsweeper Bot added P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. 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 Oct 2, 2026
@clawsweeper

clawsweeper Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed October 5, 2026, 4:19 AM ET / 08:19 UTC (Revision 5).

ClawSweeper review

What this changes

The PR lets pending agent database operations recover from a worker that exited between operations, while keeping previously captured worker-generation claims invalid.

Merge readiness

✅ Ready for maintainer review

Keep open: current main and the latest release retain the recovery gap. The patch has no actionable correctness findings, and the recorded native-worker evidence sufficiently proves the scoped recovery behavior.

Priority: P1
Reviewed head: 051376e82d032b0ecaccdbfae90860e9182ecaed

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused lifecycle repair with convincing native-worker before/after evidence and no blocking findings.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (terminal): Recorded exact-head isolated Linux proof exercises the production execution owner and real SQLite workers: terminating the retained worker permits a replacement read through both entry points, dispatches the callback once, and permanently invalidates the old claim. Both cases fail with base production code restored. No stored-data contract changes require migration proof.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (terminal): Recorded exact-head isolated Linux proof exercises the production execution owner and real SQLite workers: terminating the retained worker permits a replacement read through both entry points, dispatches the callback once, and permanently invalidates the old claim. Both cases fail with base production code restored. No stored-data contract changes require migration proof.
Evidence reviewed 9 items Verified introduced change: The pinned merge-base-to-head diff adds failed-generation retirement before dispatch and makes an unavailable prepared generation fall back to preparation. The remaining production change shortens an existing lifecycle comment.
Still necessary on current main: Comparison with fetched main shows both recovery checks remain absent there. No merged replacement establishing this behavior was supplied or identified.
Latest release retains the gap: The v2026.9.8 source retries retained cleanup failure but does not retire an already-unavailable generation before dispatch. GitHub’s annotated tag resolves to fc23bc8.
Findings None None.
Security None None.

How this fits together

Agent database execution connects session and agent operations to each agent’s SQLite worker. Its lifecycle owner controls preparation, dispatch, native cleanup, and replacement workers.

flowchart TD
  A[Agent or session operation] --> B[Database execution owner]
  B --> C{Retained worker available}
  C -->|Yes| F[Guarded operation dispatch]
  C -->|No| D[Settle worker and lease cleanup]
  D --> E[Recheck caller and database ownership]
  E --> G[Prepare replacement worker]
  G --> F
  F --> H[Operation result]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source -2, Tests +43. Total +41 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 6 8 -2
Tests 1 43 0 +43
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 49 8 +41

Review metrics

Metric Value Why it matters
Native-worker regression cost 46.85 s isolated wall time; 43.93 s CI wrapper time The PR records the required test cost and explains why real worker termination and lease settlement need this boundary.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #163151
Summary: This PR is the candidate fix for the reported pre-dispatch native-worker recovery gap.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Technical review

Best possible solution:

Keep recovery inside the existing execution owner, replace only a failed generation before dispatch, and preserve cleanup custody and stale-claim rejection.

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

Yes: terminate a retained native worker between operations, then call prepare or runExisting. Current-main source establishes the failure, and the recorded base-restoration experiment makes both regressions fail; this review did not execute them.

Is this the best way to solve the issue?

Yes: retiring the unavailable generation before dispatch is a narrow lifecycle repair that preserves the existing owner and avoids replaying submitted operations.

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

No label changes.

Label justifications:

  • P1: The linked production report records failed agent turns and chat dispatches that this pre-dispatch recovery repairs.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): Recorded exact-head isolated Linux proof exercises the production execution owner and real SQLite workers: terminating the retained worker permits a replacement read through both entry points, dispatches the callback once, and permanently invalidates the old claim. Both cases fail with base production code restored. No stored-data contract changes require migration proof.
  • proof: sufficient: Contributor real behavior proof is sufficient. Recorded exact-head isolated Linux proof exercises the production execution owner and real SQLite workers: terminating the retained worker permits a replacement read through both entry points, dispatches the callback once, and permanently invalidates the old claim. Both cases fail with base production code restored. No stored-data contract changes require migration proof.

Evidence

What I checked:

  • Verified introduced change: The pinned merge-base-to-head diff adds failed-generation retirement before dispatch and makes an unavailable prepared generation fall back to preparation. The remaining production change shortens an existing lifecycle comment. (src/state/openclaw-agent-execution.ts:294, 051376e82d03)
  • Still necessary on current main: Comparison with fetched main shows both recovery checks remain absent there. No merged replacement establishing this behavior was supplied or identified. (src/state/openclaw-agent-execution.ts:293, b658e685c314)
  • Latest release retains the gap: The v2026.9.8 source retries retained cleanup failure but does not retire an already-unavailable generation before dispatch. GitHub’s annotated tag resolves to fc23bc8. (src/state/openclaw-agent-execution.ts, fc23bc864e45)
  • Cleanup and authority remain with existing owners: Replacement follows successful native close and lease settlement, then rechecks logical ownership, source, borrower, and cancellation. Native admission continues checking physical database identity and current caller authority; captured old-generation claims remain fenced. No authority transfer, schema, stored-format, or permission change was found. (src/state/openclaw-agent-execution-native.ts:203, 051376e82d03)
  • Real native-worker recovery proof: The captured PR body binds isolated Linux Crabbox proof to the reviewed head: eight cases passed using real SQLite workers. Both new entry-point cases terminate the retained worker, read through its replacement, count one callback dispatch, and reject the old claim. Restoring the base production file made both cases fail with the reported native-owner error. The original production exit cause and prolonged Gateway behavior remain outside this bounded proof. (src/state/openclaw-agent-execution.close-wedge.test.ts:225, 051376e82d03)
  • Exact-head validation: GitHub confirms https://github.com/openclaw/openclaw/actions/runs/37280838086 succeeded for the exact reviewed head. The linked changed-test job also succeeded; the captured body records successful production types and eight passing native-worker cases. No tests were executed during this read-only review. (051376e82d03)

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • aspalagin: 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 (4 earlier review cycles)
  • reviewed 2026-10-02T04:48:02.043Z sha f8646f6 :: needs changes before merge. :: none
  • reviewed 2026-10-02T04:58:47.810Z sha f8646f6 :: needs maintainer review before merge. :: none
  • reviewed 2026-10-02T05:12:40.120Z sha f8646f6 :: needs maintainer review before merge. :: none
  • reviewed 2026-10-05T08:04:42.756Z sha 051376e :: needs maintainer review before merge. :: none

steipete and others added 2 commits October 5, 2026 00:55
Integrate the current native generation contract and adapt the recovery preflight to its failure classification.
Let preparation fall back to the execution owner when its retained native generation has failed. Keep cleanup and authority checks before replacement work, and cover prepare and existing operations with the native worker regression.

Co-authored-by: goutamadwant <[email protected]>
@steipete
steipete merged commit 88095dc into openclaw:main Oct 5, 2026
127 of 128 checks passed
@steipete

steipete commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 6, 2026
openclaw#163258)

* fix(state): reopen unavailable agent native owner before dispatch

* fix: recover prepared database claims after worker exit

Let preparation fall back to the execution owner when its retained native generation has failed. Keep cleanup and authority checks before replacement work, and cover prepare and existing operations with the native worker regression.

Co-authored-by: goutamadwant <[email protected]>

---------

Co-authored-by: Peter Steinberger <[email protected]>
Co-authored-by: goutamadwant <[email protected]>
RomneyDa pushed a commit that referenced this pull request Oct 6, 2026
#163258)

* fix(state): reopen unavailable agent native owner before dispatch

* fix: recover prepared database claims after worker exit

Let preparation fall back to the execution owner when its retained native generation has failed. Keep cleanup and authority checks before replacement work, and cover prepare and existing operations with the native worker regression.

Co-authored-by: goutamadwant <[email protected]>

---------

Co-authored-by: Peter Steinberger <[email protected]>
Co-authored-by: goutamadwant <[email protected]>
(cherry picked from commit 88095dc)
wheakerd pushed a commit to wheakerd/clawdbot that referenced this pull request Oct 22, 2026
Adapt the native-generation recovery fix to the extended-stable failed() generation API.

Co-authored-by: RomneyDa <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S 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.

[Bug]: "Agent database execution lost its native owner" fails the turn instead of reopening on a fresh native generation (2026.9.7)

2 participants