Skip to content

fix(session): stop repeated restart recovery after retry budget - #96230

Merged
steipete merged 43 commits into
openclaw:mainfrom
zhangguiping-xydt:feat/issue-95750
Jul 17, 2026
Merged

steipete merged 43 commits into
openclaw:mainfrom
zhangguiping-xydt:feat/issue-95750

Conversation

@zhangguiping-xydt

@zhangguiping-xydt zhangguiping-xydt commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Related: #95750
Follow-up: #105877

Current Status

The implementation and validation work is complete on the frozen final range:

base: ee80377a22b36b1bff6faed20c9233134e2a186d
head: edc24088387a8d573798233979a71f3b40a24ac9

The branch intentionally stays on that reviewed and verified baseline. It will not continue chasing later main movement. GitHub currently reports the PR as CONFLICTING / DIRTY because main advanced again after the final rebase; that repository-level conflict is the only known landability blocker.

What Problem This Solves

A Gateway restart could repeatedly select the same interrupted main session for automatic recovery. The existing retry loop was process-local, so supervisor backoff only delayed the next attempt: a session that never became healthy could be dispatched again after every restart without a durable ceiling.

This PR adds a durable, session-owned recovery fuse. The first three accepted or acceptance-uncertain automatic recovery attempts may dispatch. Before a fourth dispatch, OpenClaw persists an operator notice, tombstones the session, and requires recovery through inspection and /new or reset.

Why This Change Was Made

Recovery ownership

The durable state is the core-internal InternalSessionEntry.mainRestartRecovery aggregate, separate from the public SessionEntry contract. It records the recovery cycle and revision, charged attempts, the exact pre-dispatch reservation, lifecycle-generation foreground claim tokens, and an optional terminal tombstone.

src/agents/main-session-recovery-state.ts owns the pure transitions. src/agents/main-session-recovery-store.ts owns atomic persistence against the canonical SQLite row. Gateway and direct agent-command ingress use the same identity, revision, reservation, lifecycle-generation, and foreground-owner rules.

The policy is:

  • persist the charge and reservation before dispatch;
  • roll back only when the exact request is known not to have been accepted;
  • retain the charge when acceptance is uncertain, including a stale reservation after restart;
  • allow three charged attempts, then persist the notice and tombstone before a fourth dispatch;
  • do not reset the budget merely because time passes; and
  • clear it after healthy non-aborted settlement, session replacement, or reset.

restartRecoveryRuns remains a lifecycle-generation fence rather than the durable retry budget. Subagent orphan recovery remains independently owned and retains its existing policy.

Concurrency and restart safety

The final implementation includes:

  • lifecycle-generation fences so stale cleanup cannot erase new work;
  • Gateway recovery leases bound to the exact recovery identity;
  • snapshot revision and foreground-claim checks around concurrent admission;
  • stale lifecycle-clear suppression after tombstoning;
  • predecessor preclaims during freshness rollover;
  • exact reservation rollback across session-key canonicalization; and
  • transcript-only exhaustion notice persistence before tombstoning, with retry when notice persistence fails.

Plugin SDK boundary

Recovery coordination remains internal and does not become a plugin contract.

  • Public session reads project internal rows to SessionEntry.
  • Modern patch/upsert APIs strip escaped private input and preserve internal recovery state.
  • Deprecated loadSessionStore and updateSessionStore now also project public rows and reconcile public mutations while preserving private recovery state and deletion semantics.
  • The public testing result excludes the internal recovery candidate.
  • The generated Plugin SDK surface contains no recovery-internal concept, and no checksum-only baseline update hides a semantic change.

The broader shipped Plugin SDK testing-barrel question remains tracked separately in #105877.

User Impact

A wedged main session can no longer trigger unbounded automatic resume across Gateway restarts. After three charged accepted or acceptance-uncertain attempts, OpenClaw persists the operator notice and quarantines the session before another automatic dispatch. Healthy progress, replacement, or reset restores the full budget; waiting alone does not.

Plugins continue to see the public session contract and cannot observe or accidentally overwrite the internal recovery state through either modern or deprecated session-store APIs.

Evidence

Exact frozen range:

ee80377a22b36b1bff6faed20c9233134e2a186d..edc24088387a8d573798233979a71f3b40a24ac9
  • Focused final regression set: 227 tests passed (14 recovery-state, 15 Gateway lifecycle, 198 agent tests).
  • Broader restart-recovery suite: 86 passed.
  • Agent-command suite: 112 passed.
  • Core production and test tsgo lanes passed.
  • Targeted and changed-file oxfmt / oxlint passed.
  • LOC ratchet passed for all 53 changed production TypeScript files.
  • Full changed-file gate passed against the frozen base, including core/extension types, lint, Plugin SDK surface/API/exports, and repository guards.
  • Full local build passed on Node 24.15.0.
  • Mandatory default-Codex Auto Review covered the final change in six synthetic slices plus the final dirty follow-up; no accepted/actionable finding remains.
  • Thread-aware GitHub inspection found no unresolved inline review threads; the current maintainer review decision is APPROVED.

Final end-to-end proof used a local Docker container, actual provider local-container, lease cbx_9736f8111fa1, Ubuntu 26.04:

  • complete container build passed;
  • a real Gateway started successfully;
  • exhausted recovery ended with status=failed, abortedLastRun=false, and chargedAttempts=3;
  • the transcript notice was persisted; and
  • the tombstone was written before any fourth dispatch.

Exact-head GitHub automation has passed the dependency and security guards plus one real-behavior proof run. The standard full CI workflow did not dispatch for this fork-head update. Exact-merge-result CI cannot exist while GitHub reports the later-main conflict.

Known Blocker

The code, tests, review findings, and exact-head behavior proof are complete. GitHub currently reports mergeable=CONFLICTING and mergeStateStatus=DIRTY after main advanced again. Per the frozen-branch decision, this PR will not be rebased or merged with another moving main baseline.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime agents Agent runtime and tooling size: L labels Jun 24, 2026
@clawsweeper

clawsweeper Bot commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 17, 2026, 10:32 AM ET / 14:32 UTC.

Summary
Adds durable main-session restart-recovery coordination, retry-exhaustion quarantine, Plugin SDK isolation, lifecycle and Doctor handling, documentation, and regression coverage.

Reproducibility: yes. The canonical issue provides a concrete cross-boot reproduction, and the baseline Telegram run demonstrates repeated exhausted-session replay on main before the candidate suppresses it.

Review metrics: 3 noteworthy metrics.

  • Persisted Recovery Surface: 1 internal aggregate added. The aggregate owns attempts, reservations, foreground claims, revisions, and tombstones across process restarts.
  • Recovery Ceiling: 3 charged attempts before quarantine. This intentionally replaces unbounded cross-boot replay with an operator-visible terminal state.
  • Gateway Request Boundary: 1 additive dispatch observer. The observer distinguishes explicit pre-dispatch rejection from uncertain post-dispatch failure for charge rollback.

Stored data model
Persistent data-model change detected: migration/backfill/repair: docs/gateway/doctor.md, migration/backfill/repair: src/agents/main-session-recovery-lifecycle.ts, migration/backfill/repair: src/commands/doctor-main-session-recovery.ts, migration/backfill/repair: src/commands/doctor-state-integrity-format.ts, migration/backfill/repair: src/commands/doctor-state-integrity.ts, serialized state: src/agents/command/session-store.test.ts, and 40 more. Confirm migration or upgrade compatibility proof before merge.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #95750
Summary: This PR is the complete current-main-integrated candidate for the canonical cross-boot recovery-loop issue; the split PRs cover prerequisites or partial surfaces rather than the full user-visible repair.

Members:

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

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🦞 diamond lobster ✨ media proof bonus
Patch quality: 🐚 platinum hermit
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • Complete the current exact-head CI and merge-result validation before landing.

Risk before merge

  • [P1] Exact-head CI remains incomplete, including an unresolved changed-path scanner result, so the broad integration is not yet fully merge-gated.
  • [P1] The durable fuse intentionally changes upgrade behavior: after three charged attempts, an existing session is quarantined and requires inspection, /new, or reset instead of further automatic recovery.
  • [P2] The patch changes persisted session ownership and Plugin SDK projection across gateway and agent paths; a regression could restore restart loops, expose private state, or incorrectly block recoverable work.

Maintainer options:

  1. Finish Exact-Head Validation (recommended)
    Wait for the current head's focused tests, type checks, build, compatibility guards, and recovery gates before merging the integrated patch.
  2. Accept The Recovery Policy
    Merge after confirming that three charged attempts followed by quarantine is the intended upgrade behavior for existing sessions.
  3. Return To The Split Series
    Pause this PR only if maintainers prefer landing the dependency-ordered partial PRs and will complete the remaining production wiring separately.

Next step before merge

  • [P2] No narrow autonomous repair defect remains; maintainers should complete current-head validation and ordinary merge-risk review.

Security
Cleared: The patch changes trusted runtime and session coordination but introduces no concrete dependency, workflow, credential, permission, download, publishing, or secret-handling concern.

Review details

Best possible solution:

Land the integrated durable-owner design only after exact-head gates finish and focused recovery, Plugin SDK compatibility, upgrade, and real Gateway exhaustion evidence remain green.

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

Yes. The canonical issue provides a concrete cross-boot reproduction, and the baseline Telegram run demonstrates repeated exhausted-session replay on main before the candidate suppresses it.

Is this the best way to solve the issue?

Yes for the architecture: durable pre-dispatch reservation, exact rollback, atomic session-store ownership, and private recovery state address the root invariant. The current head should merge only after exact-head validation completes.

AGENTS.md: found and applied where relevant.

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

Label changes

Label justifications:

  • P1: The PR addresses a reproduced cross-boot recovery loop that can exhaust supervisor restarts and make Gateway-backed channel workflows unavailable.
  • merge-risk: 🚨 compatibility: Existing sessions may stop automatic recovery after the durable budget and require inspection, /new, or reset.
  • merge-risk: 🚨 session-state: The patch changes persisted attempts, reservations, lifecycle clearing, ownership claims, tombstones, and plugin-facing session projection.
  • merge-risk: 🚨 availability: Incorrect integration could either restore the restart death loop or quarantine a session that should still recover.
  • 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 (recording): A real Docker Gateway run proves notice persistence and tombstoning before a fourth dispatch, while Telegram Desktop media shows exhausted replay suppression and successful fresh messaging; posted artifacts should remain redacted.
  • proof: sufficient: Contributor real behavior proof is sufficient. A real Docker Gateway run proves notice persistence and tombstoning before a fourth dispatch, while Telegram Desktop media shows exhausted replay suppression and successful fresh messaging; posted artifacts should remain redacted.
  • proof: 🎥 video: Contributor real behavior proof includes video or recording evidence. A real Docker Gateway run proves notice persistence and tombstoning before a fourth dispatch, while Telegram Desktop media shows exhausted replay suppression and successful fresh messaging; posted artifacts should remain redacted.
  • mantis: telegram-visible-proof: Mantis should capture Telegram visible proof. The change visibly suppresses exhausted Telegram recovery replay while preserving fresh-message handling, which is directly demonstrable in Telegram Desktop.
Evidence reviewed

What I checked:

  • Current integrated head: Live PR state identifies 4731eb0 as a mergeable head based on current main, superseding the stale frozen-conflict description in the PR body. (4731eb0aef1c)
  • Durable recovery policy: The recovery state owner defines a three-attempt durable ceiling and centralizes cycle, revision, reservation, claim, and tombstone transitions. (src/agents/main-session-recovery-state.ts:21, 4731eb0aef1c)
  • Atomic persistence owner: The recovery store applies state transitions through the canonical session accessor with exact reservation and ownership identities. (src/agents/main-session-recovery-store.ts:1, 4731eb0aef1c)
  • Plugin boundary: Plugin-facing session projection removes the private mainRestartRecovery aggregate while preserving public session fields and compatible mutations. (src/plugin-sdk/session-store-runtime-internal.ts:34, 4731eb0aef1c)
  • Regression coverage: Dedicated suites cover pure recovery transitions, SQLite-backed ownership, restart scanning and dispatch, foreground admission, lifecycle clearing, resets, and Plugin SDK projection. (src/agents/main-session-recovery-state.test.ts:1, 4731eb0aef1c)
  • Real behavior proof: The PR discussion records a real Docker Gateway run where the operator notice persisted and the session tombstoned before a fourth dispatch, plus Telegram Desktop before/after proof showing exhausted replay suppression while fresh messaging still worked. (edc24088387a)

Likely related people:

  • steipete: Authored the extensive recovery repair series and force-pushed the current-main-integrated head after the branch had been frozen. (role: recent area contributor and integrator; confidence: high; commits: 648f6bab2c0d, e50966b8caf7, 4731eb0aef1c; files: src/agents/main-session-recovery-state.ts, src/agents/main-session-recovery-store.ts, src/agents/main-session-restart-recovery.ts)
  • hxy91819: Defined the durable recovery-fuse and owner-boundary direction, repeatedly reviewed race and compatibility behavior, and approved the resulting implementation. (role: reviewer and architecture contributor; confidence: high; files: src/agents/main-session-recovery-state.ts, src/agents/main-session-recovery-store.ts, src/plugin-sdk/session-store-runtime.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

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.

How this review workflow works
  • 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.
Review history (42 earlier review cycles; latest 8 shown)
  • reviewed 2026-07-13T08:54:03.661Z sha 5d7c3d78e54e328aa5448ba2b1a4a8fdc49a1078 :: needs maintainer review before merge. :: none
  • reviewed 2026-07-13T10:56:32.460Z sha 5d7c3d78e54e328aa5448ba2b1a4a8fdc49a1078 :: needs maintainer review before merge. :: none
  • reviewed 2026-07-13T13:04:01.749Z sha 5d7c3d78e54e328aa5448ba2b1a4a8fdc49a1078 :: found issues before merge. :: [P1] Project deprecated whole-store APIs at the plugin boundary
  • reviewed 2026-07-13T20:17:54.118Z sha edc24088387a8d573798233979a71f3b40a24ac9 :: needs maintainer review before merge. :: none
  • reviewed 2026-07-17T03:25:09.039Z sha 1dfb28636f3f3f99ad65ce24010d3efc40dcc73a :: needs maintainer review before merge. :: none
  • reviewed 2026-07-17T04:12:34.929Z sha 5381e0de476ec451d05b25de122fa08152702668 :: needs maintainer review before merge. :: none
  • reviewed 2026-07-17T04:53:57.333Z sha 5381e0de476ec451d05b25de122fa08152702668 :: needs maintainer review before merge. :: none
  • reviewed 2026-07-17T14:15:10.025Z sha e50966b :: needs maintainer review before merge. :: none

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. labels Jun 24, 2026
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Jun 24, 2026
@zhangguiping-xydt

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@clawsweeper clawsweeper Bot added 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. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. labels Jun 24, 2026
@clawsweeper clawsweeper Bot added rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. labels Jul 1, 2026
@clawsweeper clawsweeper Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label Jul 3, 2026
@steipete

Copy link
Copy Markdown
Contributor

Merged via squash.

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

Labels

agents Agent runtime and tooling commands Command implementations docs Improvements or additions to documentation gateway Gateway runtime mantis: telegram-visible-proof Mantis should capture Telegram visible proof. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. proof: 🎥 video Contributor real behavior proof includes video or recording evidence. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: XL status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. triage: blank-template Candidate: PR template appears mostly untouched. triage: dirty-candidate Candidate: broad unrelated surfaces; may need splitting or cleanup.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants