Skip to content

fix(agents): clear empty uncharged recovery residue - #166294

Merged
steipete merged 2 commits into
openclaw:mainfrom
sunlit-deng:sunlit/fix/issue-166203
Oct 7, 2026
Merged

steipete merged 2 commits into
openclaw:mainfrom
sunlit-deng:sunlit/fix/issue-166203

Conversation

@sunlit-deng

@sunlit-deng sunlit-deng commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Completed subagent results can fail to start their requester turn with SESSION_WORK_START_CHANGED when the requester retains an empty, uncharged restart-recovery record. The same session stays blocked on later completions.

Fixes #166203

User Impact

Healthy sessions can receive completed subagent results again. Interrupted work and pending execution or delivery retain recovery custody.

Why This Change Was Made

A foreground turn can settle while an automatic recovery reservation remains. Cancelling that reservation refunds its last charge and previously left an ownerless aggregate behind. The recovery state owner now clears that residue during cancellation. Its existing reconciliation and foreground cleanup paths also retire residue already persisted by older versions. Admission continues to require a healthy, fully cleared session.

No configuration, schema, or public protocol changes. The helper stays separate because the state owner's existing file-size limit prevents inlining it. Maintainer cleanup factors duplicated admission conditions and adds producer/reconciliation coverage, including statusless requester admission.

Evidence

Validated head: 21328d2afeba369256dc1fac9b2adfbe382dbf3f. Current-main source still contains the defect; its recovery state/store owners do not clean empty healthy aggregates. Independent review found no actionable P0–P2 findings. ClawSweeper's latest review of this head likewise found no correctness or security defect; its remaining request for per-file wall time and CI seconds is supplied below.

Local static checks passed: targeted oxfmt, oxlint with config/tsconfig/oxlint.core.json, and git diff --check. No contributor runtime code executed on the maintainer host.

Crabbox Untrusted AWS lease cbx_421cea415919 (stopped after proof): trusted-main bootstrap verified the full pushed SHA and no instance-role credentials; no Tailscale or credential hydration. Core typechecking and all 132 tests across four files passed, in 139.12 s wall time:

pnpm tsgo:core && pnpm test \
  src/agents/main-session-recovery/main-session-recovery-empty-aggregate.test.ts \
  src/agents/agent-command-recovery-owner.test.ts \
  src/agents/main-session-recovery/main-session-recovery-state.test.ts \
  src/agents/main-session-recovery/main-session-recovery-store.test.ts \
  --maxWorkers=1

On the same lease and head, the negative control restored only production code to merge-base 4de57f22aa1dcd118ec156a08588adb1b54404bd (including removal of the newly added predicate module), keeping the new tests:

git checkout 4de57f22aa1dcd118ec156a08588adb1b54404bd -- \
  src/agents/main-session-recovery/main-session-recovery-state.ts
rm src/agents/main-session-recovery/main-session-recovery-empty-aggregate.ts
pnpm test src/agents/main-session-recovery/main-session-recovery-empty-aggregate.test.ts \
  src/agents/agent-command-recovery-owner.test.ts --maxWorkers=1 \
  -t 'clears the last cancelled reservation|reconciles existing empty residue|resumes requester settle'

The producer and reconciliation assertions failed in 29.35 s. Because the runner stops after its first failing shard, the persisted admission regression ran separately with pnpm test src/agents/agent-command-recovery-owner.test.ts --maxWorkers=1 -t 'resumes requester settle': both statusless and completed-session cases failed in 38.26 s.

clears the last cancelled reservation after a foreground turn settles:
AssertionError: expected { cycleId: 'cycle-1', … } to be undefined
Received: chargedAttempts: 0, cycleId: 'cycle-1', revision: 5,
          executionIdentity: undefined, foregroundClaims: undefined,
          reservation: undefined

resumes requester settle from persisted empty recovery (status=undefined / done):
AssertionError: promise rejected instead of resolving
Serialized Error: { code: 'SESSION_WORK_START_CHANGED' }

Restoring the two production files to the pushed head made both complete changed files pass again. This proves persisted command admission and recovery transitions; the executor callback uses synthetic text and does not contact a provider or channel.

Test cost

Measured individually with pnpm test <file> --maxWorkers=1 on the same AWS lease. CI seconds are each file's shard begin-to-end wall time from the successful exact-head CI job.

Test file Tests AWS command wall time CI shard wall time
src/agents/main-session-recovery/main-session-recovery-empty-aggregate.test.ts 17 35.75 s 7.10 s (Bun lane)
src/agents/agent-command-recovery-owner.test.ts 44 14.72 s 44.80 s (Node lane)

The >30 s observations are dominated by existing import transformation: 94% of the AWS empty-aggregate Vitest duration and 87% of the CI requester-owner duration. The regression uses the real transition/lifecycle owners; the persisted test requires the actual session writer and admission boundary. Mocking those owners would miss the defect. The added tests contain no sleeps, real timers, polling, process boots, or test-only production seams. Exact-head CI completed successfully.

Diff size and credit

Production +25 / -1 (net +24); tests +304 / -0; docs +5 / -0, from git diff --numstat origin/main...HEAD:

5   0  docs/gateway/restart-recovery.md
76  0  src/agents/agent-command-recovery-owner.test.ts
228 0  src/agents/main-session-recovery/main-session-recovery-empty-aggregate.test.ts
18  0  src/agents/main-session-recovery/main-session-recovery-empty-aggregate.ts
7   1  src/agents/main-session-recovery/main-session-recovery-state.ts

Production LOC exception: the final delta remains positive. Deslop removes duplicated admission conditions; further reduction would require a separate refactor of inspection ordering or removing custody checks. The existing owner file-size gate also requires retaining the small predicate module. No guard was weakened or unrelated file changed to meet a line-count target.

Original fix by @sunlit-deng; maintainer cleanup adds producer-side cancellation repair, recovery reconciliation, statusless admission coverage, documentation, and isolated positive/negative proof. Contributor commits and attribution are preserved.

@clawsweeper

clawsweeper Bot commented Oct 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 agents Agent runtime and tooling size: S labels Oct 6, 2026
@clawsweeper clawsweeper Bot added P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. 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. labels Oct 6, 2026
@clawsweeper

clawsweeper Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed October 6, 2026, 9:21 PM ET / October 7, 2026, 01:21 UTC (Revision 4).

ClawSweeper review

What this changes

Clear empty, uncharged restart-recovery records during reservation cancellation, reconciliation, and foreground admission so completed subagent results can start a requester turn.

Merge readiness

✅ Ready for maintainer review

This PR remains necessary: current main still leaves empty recovery residue. No actionable correctness or security finding remains, and the updated evidence resolves the previous review's test-cost request.

Priority: P1
Reviewed head: 21328d2afeba369256dc1fac9b2adfbe382dbf3f

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused owner-level repair with credible persisted-boundary negative controls, preserved custody, and the previous review's remaining evidence request satisfied.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (terminal): Captured exact-head AWS before/after evidence exercises the real session writer and requester command-admission owner, showing persisted empty residue clears and admission succeeds while interrupted custody remains. Provider/channel delivery is outside this proof's claim. Existing-state compatibility is supported by statusless and completed persisted-row cases; no schema or serialized-format change is introduced.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (terminal): Captured exact-head AWS before/after evidence exercises the real session writer and requester command-admission owner, showing persisted empty residue clears and admission succeeds while interrupted custody remains. Provider/channel delivery is outside this proof's claim. Existing-state compatibility is supported by statusless and completed persisted-row cases; no schema or serialized-format change is introduced.
Evidence reviewed 8 items Pinned introduced change: Reviewed the merge-base-to-head delta, including both production changes, the complete new regression suite, added requester-admission cases, and documentation. Endpoint drift and the stale test merge were not treated as PR-owned changes.
Custody-preserving cleanup: The predicate requires a non-interrupted entry, zero charged attempts, and absence of started execution, execution identity, reservation, foreground claims, tombstone, recovery runs, delivery run, and pending final delivery. Cancellation retains its exact reservation checks; foreground cleanup retains session identity and candidate checks.
Existing writer and admission boundary: The unchanged store owner applies transitions to the latest row inside the existing session writer operation. Unclaimed admission still requires the expected session with its recovery aggregate and run fences fully absent; the patch does not add a competing admission path.
Findings None None.
Security None None.

How this fits together

Restart recovery keeps interrupted agent work and pending delivery under durable ownership. Foreground admission reads that ownership before allowing a new turn, including a follow-up carrying completed subagent results.

flowchart TD
  A[Completed subagent result] --> B[Requester turn admission]
  C[Persisted recovery record] --> D[Recovery state owner]
  E[Reservation cancellation or reconciliation] --> D
  D --> F{Remaining work or custody?}
  F -->|Yes| G[Preserve recovery ownership]
  F -->|No| H[Clear empty residue]
  H --> B
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source +24, Tests +304, Docs +5. Total +333 across 5 files.

View PR surface stats
Area Files Added Removed Net
Source 2 25 1 +24
Tests 2 304 0 +304
Docs 1 5 0 +5
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 5 334 1 +333

Review metrics

Metric Value Why it matters
Production versus regression coverage production net +24 lines; tests +304 lines Production growth is explained by the shared custody predicate and producer cleanup, with the helper split required by the existing owner file-size gate.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #166203
Summary: This PR is the candidate repair for empty-record requester admission; terminal-only residue and historical unmatched-fence replay are separate cases.

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 cleanup in the existing state owner, retire only ownerless residue, and preserve every live execution or delivery obligation.

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

Yes: a healthy persisted session with an empty, uncharged recovery aggregate reaches invalidated requester admission on current main. The captured isolated negative control confirms SESSION_WORK_START_CHANGED; this read-only review did not execute target code.

Is this the best way to solve the issue?

Yes: repairing cancellation at its producer and reusing the existing reconciliation and admission owner is a narrow fix, while explicit custody exclusions preserve interrupted work.

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

No label changes.

Label justifications:

  • P1: The linked production report establishes recurring suppressed subagent results because requester admission remains wedged on the same session.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): Captured exact-head AWS before/after evidence exercises the real session writer and requester command-admission owner, showing persisted empty residue clears and admission succeeds while interrupted custody remains. Provider/channel delivery is outside this proof's claim. Existing-state compatibility is supported by statusless and completed persisted-row cases; no schema or serialized-format change is introduced.
  • proof: sufficient: Contributor real behavior proof is sufficient. Captured exact-head AWS before/after evidence exercises the real session writer and requester command-admission owner, showing persisted empty residue clears and admission succeeds while interrupted custody remains. Provider/channel delivery is outside this proof's claim. Existing-state compatibility is supported by statusless and completed persisted-row cases; no schema or serialized-format change is introduced.

Evidence

What I checked:

  • Pinned introduced change: Reviewed the merge-base-to-head delta, including both production changes, the complete new regression suite, added requester-admission cases, and documentation. Endpoint drift and the stale test merge were not treated as PR-owned changes. (src/agents/main-session-recovery/main-session-recovery-state.ts:499, 21328d2afeba)
  • Custody-preserving cleanup: The predicate requires a non-interrupted entry, zero charged attempts, and absence of started execution, execution identity, reservation, foreground claims, tombstone, recovery runs, delivery run, and pending final delivery. Cancellation retains its exact reservation checks; foreground cleanup retains session identity and candidate checks. (src/agents/main-session-recovery/main-session-recovery-empty-aggregate.ts:3, 21328d2afeba)
  • Existing writer and admission boundary: The unchanged store owner applies transitions to the latest row inside the existing session writer operation. Unclaimed admission still requires the expected session with its recovery aggregate and run fences fully absent; the patch does not add a competing admission path. (src/agents/main-session-recovery/main-session-recovery-store.ts:246, 21328d2afeba)
  • Current main still needs the repair: At fetched main, foreground cleanup accepts terminal-only records but not empty records, and reservation cancellation refunds the charge without retiring the resulting empty aggregate. The earlier merged terminal-residue fix requires recorded terminal runs and therefore does not cover this shape. (src/agents/main-session-recovery/main-session-recovery-state.ts:479, ed4974479682)
  • Captured production-boundary proof and existing-state compatibility: The supplied complete body, captured under sourceRevision dcfe648782fd1d8a41ed924cf9148c0c12e3918e6ae52b9f0d8d04a1172d61d0, reports exact-head secretless AWS execution through the real session writer and command admission owner. Persisted statusless and done entries admit the requester callback and lose only empty recovery residue; interrupted custody remains. Restoring pre-fix production code makes both persisted admission cases fail with SESSION_WORK_START_CHANGED. Producer cancellation and reconciliation also have negative controls. The synthetic executor does not prove provider or channel delivery, which this internal admission repair does not change. (src/agents/agent-command-recovery-owner.test.ts:225, 21328d2afeba)
  • Previous review request resolved: The previous completed review of this same head had no findings and requested per-file test wall time and CI seconds. The captured body now reports 35.75 seconds locally and 7.10 CI seconds for the empty-aggregate suite, and 14.72 seconds locally and 44.80 CI seconds for requester-owner coverage, with the import-cost and boundary justification required by repository policy. Public docs: docs/help/testing/writing-tests.md. (docs/help/testing/writing-tests.md, 21328d2afeba)

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • ejc3: 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 (3 earlier review cycles)
  • reviewed 2026-10-06T21:06:35.083Z sha 2471700e19ac6951e1ee2a34645338ccf73c9362 :: needs real behavior proof before merge. :: [P1] Preserve recovery custody for interrupted empty aggregates
  • reviewed 2026-10-06T23:54:15.167Z sha 71c62a4 :: needs changes before merge. :: none
  • reviewed 2026-10-07T01:05:45.915Z sha 21328d2 :: needs changes before merge. :: none

@sunlit-deng
sunlit-deng force-pushed the sunlit/fix/issue-166203 branch from 2471700 to 71c62a4 Compare October 6, 2026 23:47
@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. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Oct 6, 2026
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime labels Oct 7, 2026
@steipete
steipete merged commit 5ea2516 into openclaw:main Oct 7, 2026
126 of 127 checks passed
@steipete

steipete commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 7, 2026
* fix(agents): preserve interrupted empty recovery custody

* fix(agents): retire empty recovery state after reservation cancellation

---------

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

Labels

agents Agent runtime and tooling docs Improvements or additions to documentation gateway Gateway runtime 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: 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.

[Bug]: Empty uncharged mainRestartRecovery aggregate makes every requester-settle wake fail SESSION_WORK_START_CHANGED; subagent result never delivered

2 participants