Skip to content

fix(agents): serialize subagent orphan recovery - #117283

Merged
vincentkoc merged 1 commit into
mainfrom
perf/collector-generation-ownership-20260731
Aug 2, 2026
Merged

vincentkoc merged 1 commit into
mainfrom
perf/collector-generation-ownership-20260731

Conversation

@vincentkoc

@vincentkoc vincentkoc commented Aug 1, 2026 •

Copy link
Copy Markdown
Member

Fixes #103724

What Problem This Solves

Two independent orphan-recovery scheduler families could overlap after a Gateway restart. Each family owned private de-duplication state, so both could observe the same abortedLastRun child and dispatch duplicate resume turns.

Why This Change Was Made

This replaces the old 118-file draft with a current-main, scheduler-only fix.

scheduleOrphanRecovery now owns one process-wide recovery family. Calls before the first scan coalesce because that scan reads the live registry. Calls arriving after an attempt starts queue one follow-up scan, including during retry backoff, so new recovery work is not lost while the family is active.

The scanner, dispatch, remap, session-marker, protocol, config, and persistence behavior are otherwise unchanged.

User Impact

One Gateway process cannot run overlapping orphan-recovery scans. This prevents the reported duplicate resume turns and avoids repeated full-registry scans during startup recovery bursts.

Evidence

  • Exact signed head: f00bf01d1f1f9f5532f493b4fe26c0a3f332ca37
  • Focused proof: 177/177 tests passed
    • scheduler coordination: 6/6
    • orphan recovery: 29/29
    • restart integration: 3/3
    • subagent registry: 139/139
  • Targeted oxfmt, oxlint, and git diff --check: passed
  • Canonical P1 autoreview: no accepted/actionable findings
  • Six independent exact-SHA reviews: approved with no P0/P1
  • Hosted changed gate: passed on Testbox tbx_01kyye5erdthnmyh9k35b61neh (run 30695937637)

@vincentkoc vincentkoc self-assigned this Aug 1, 2026
@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime agents Agent runtime and tooling size: XL maintainer Maintainer-authored PR labels Aug 1, 2026
@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. labels Aug 1, 2026
@clawsweeper

clawsweeper Bot commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 1, 2026, 10:52 PM ET / August 2, 2026, 02:52 UTC.

ClawSweeper review

What this changes

Serializes delayed subagent orphan-recovery scheduling within one Gateway process, coalescing duplicate startup requests and queuing one follow-up scan while a recovery family is active.

Merge readiness

⚠️ Ready for maintainer review - 4 items remain

Keep this PR open for maintainer review. The focused scheduler change addresses the linked duplicate-resume bug at the process-wide scheduling owner, but it is behind current main while coordinating persisted interrupted-session recovery, so the focused recovery proof needs to run on the current merge result before landing.

Priority: P1
Reviewed head: f00bf01d1f1f9f5532f493b4fe26c0a3f332ca37
Owner decision: Required. See Decision needed.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The patch is focused and has relevant regression coverage, but current-main merge-result validation is still needed for this persisted recovery path.
Proof confidence 🌊 off-meta tidepool Not applicable: This MEMBER-authored internal recovery fix includes focused exact-head validation in the PR body; the remaining merge gate is rerunning that targeted proof against current main rather than requesting external contributor media.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: This MEMBER-authored internal recovery fix includes focused exact-head validation in the PR body; the remaining merge gate is rerunning that targeted proof against current main rather than requesting external contributor media.
Evidence reviewed 6 items Current-main behavior remains uncoordinated: Current main creates a fresh delayed recovery schedule, resumedSessionKeys, and pendingStaleFinalizations for every call; the registry comment explicitly says attempts own independent root admission. The PR's module-level scheduler state is not present on current main.
Multiple production scheduling sources: The registry schedules orphan recovery after restoration/reactive activity, and the Gateway post-attach startup flow invokes the same registry scheduler. Those independent triggers support a single process-wide scheduling owner rather than per-call de-duplication.
Proposed owner-boundary repair: The branch adds module-level scheduled/running state and one pending follow-up request, releasing the family only after recovery, terminal finalization, or retry exhaustion. This directly serializes the timer family rather than adding another per-session downstream guard.
Findings None None.
Security None None.

How this fits together

After a Gateway restart, both the subagent registry and the post-startup Gateway flow can request orphan recovery for interrupted child sessions. The scheduler waits for Gateway readiness, scans the live run registry, resumes eligible child sessions, and then records the resulting session and registry state.

flowchart LR
  A[Gateway restart] --> B[Registry and startup recovery requests]
  B --> C[Orphan recovery scheduler]
  C --> D[Scan interrupted child sessions]
  D --> E[Gateway resume dispatch]
  E --> F[Session marker and registry update]
Loading

Decision needed

Question Recommendation
After focused recovery proof passes on the current merge result, should the owner accept a single process-wide scheduler that coalesces startup requests and permits one queued follow-up scan? Validate and accept the scheduler family: Run the focused coordination, orphan-recovery, restart-integration, and registry checks on the current merge result, then accept the process-wide scheduler if all preserve recovery behavior.

Why: This is a maintainer-owned P1 change to Gateway recovery semantics; source supports the scheduling layer, but accepting the exact coalescing and follow-up policy requires owner judgment after current-main validation.

Before merge

  • Resolve merge risk (P1) - The branch is behind current main; its exact-head focused proof does not establish recovery behavior after the current three-way merge.
  • Resolve merge risk (P1) - This scheduler governs interrupted-session recovery, so an unnoticed merge interaction could reintroduce duplicate resume turns or delay eligible recovery work.
  • Complete next step (P2) - This maintainer-labeled PR needs current-merge focused proof and recovery-owner acceptance; it is not a safe autonomous repair lane.
Agent review details

Security

None.

PR surface

Source +36, Tests +249. Total +285 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 1 44 8 +36
Tests 2 249 0 +249
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 293 8 +285

Review metrics

None.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #103724
Summary: This PR is a candidate repair for the canonical duplicate orphan-resume report; the other open candidate uses a different per-session admission strategy and should not be treated as a safe replacement yet.

Members:

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

Merge-risk options

Maintainer options:

  1. Validate the current merge result (recommended)
    Before merge, run the focused recovery suite against the current three-way merge so queued follow-up and retry-backoff behavior is proven with current session-state code.
  2. Pause for a recovery-owner decision
    Keep the PR open if maintainers prefer per-session recovery admission over one process-wide scheduler family.

Technical review

Best possible solution:

Retain one process-wide orphan-recovery scheduler, then validate its active-scan, retry-backoff, and terminal-finalization paths on the current merge result before a maintainer accepts the session-state behavior.

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

Yes, source-reproducible with high confidence: the linked report identifies separate registry and Gateway scheduling paths, and the branch adds focused tests that park recovery dispatch while another schedule request arrives. This read-only review did not execute that path.

Is this the best way to solve the issue?

Yes in design: process-wide serialization fixes the duplicate-scan owner boundary more directly than a second per-session guard, while retaining the live registry scan for follow-up work. The remaining requirement is proof on the current merge result, not a different product feature.

AGENTS.md: found and applied where relevant.

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

Labels

Label justifications:

  • P1: Overlapping restart recovery can dispatch the same interrupted child session twice, duplicating agent work and external side effects.
  • merge-risk: 🚨 session-state: The PR changes scheduling around persisted aborted-session markers and registry remapping, where a merge interaction could duplicate or defer recovery.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; 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: This MEMBER-authored internal recovery fix includes focused exact-head validation in the PR body; the remaining merge gate is rerunning that targeted proof against current main rather than requesting external contributor media.

Evidence

What I checked:

  • Current-main behavior remains uncoordinated: Current main creates a fresh delayed recovery schedule, resumedSessionKeys, and pendingStaleFinalizations for every call; the registry comment explicitly says attempts own independent root admission. The PR's module-level scheduler state is not present on current main. (src/agents/subagent-orphan-recovery.ts:606, e49e9e2aa193)
  • Multiple production scheduling sources: The registry schedules orphan recovery after restoration/reactive activity, and the Gateway post-attach startup flow invokes the same registry scheduler. Those independent triggers support a single process-wide scheduling owner rather than per-call de-duplication. (src/agents/subagent-registry.ts:102, e49e9e2aa193)
  • Proposed owner-boundary repair: The branch adds module-level scheduled/running state and one pending follow-up request, releasing the family only after recovery, terminal finalization, or retry exhaustion. This directly serializes the timer family rather than adding another per-session downstream guard. (src/agents/subagent-orphan-recovery.ts:624, f00bf01d1f1f)
  • Focused regression coverage: The added tests cover pre-scan coalescing, a follow-up request during an active dispatch, retry-backoff scheduling, unavailable runtime paths, and terminal-finalization exhaustion. (src/agents/subagent-orphan-recovery.coordination.test.ts:135, f00bf01d1f1f)
  • Current-main provenance and ownership signal: Blame attributes the present scheduler block to Peter Steinberger on the current checkout; the proposed branch commit is Vincent Koc's scheduler-specific change. The local history is grafted, so this is a recent-area routing signal rather than full feature ancestry. (src/agents/subagent-orphan-recovery.ts:606, e49e9e2aa193)
  • Not implemented or shipped: The PR head is not an ancestor of current main and is not contained by the stated latest release commit, so this remains an open implementation candidate rather than an implemented-on-main cleanup. (src/agents/subagent-orphan-recovery.ts:624, f00bf01d1f1f)

Likely related people:

  • Peter Steinberger: Current-main blame attributes the scheduler block to this author, including the independent-root-admission behavior that the PR replaces. (role: recent area contributor; confidence: medium; commits: e49e9e2aa193; files: src/agents/subagent-orphan-recovery.ts, src/agents/subagent-registry.ts)

Rank-up moves

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

  • Run the stated focused recovery and registry checks on the current merge result.
  • Have the recovery-area owner confirm the single-family coalescing policy after that proof.

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 (7 earlier review cycles)
  • reviewed 2026-08-01T07:39:47.194Z sha 079c183ba628d95c7276899626e26fbad0cf0260 :: needs maintainer review before merge. :: none
  • reviewed 2026-08-01T10:44:44.793Z sha f00bf01 :: needs maintainer review before merge. :: none
  • reviewed 2026-08-01T15:06:40.642Z sha f00bf01 :: needs maintainer review before merge. :: none
  • reviewed 2026-08-01T15:16:05.510Z sha f00bf01 :: needs maintainer review before merge. :: none
  • reviewed 2026-08-01T18:59:52.011Z sha f00bf01 :: needs maintainer review before merge. :: none
  • reviewed 2026-08-01T23:52:29.656Z sha f00bf01 :: needs maintainer review before merge. :: none
  • reviewed 2026-08-02T00:30:13.092Z sha f00bf01 :: needs maintainer review before merge. :: none

@vincentkoc
vincentkoc force-pushed the perf/collector-generation-ownership-20260731 branch from 079c183 to f00bf01 Compare August 1, 2026 10:39
@vincentkoc vincentkoc changed the title fix(agents): prevent duplicate subagent recovery and delivery fix(agents): serialize subagent orphan recovery Aug 1, 2026
@vincentkoc vincentkoc removed size: XL merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. labels Aug 1, 2026
@vincentkoc

Copy link
Copy Markdown
Member Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Aug 1, 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: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Re-review progress:

@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. labels Aug 1, 2026
@vincentkoc
vincentkoc marked this pull request as ready for review August 2, 2026 02:47
@vincentkoc

Copy link
Copy Markdown
Member Author

Land-ready verification for exact head f00bf01d1f1f9f5532f493b4fe26c0a3f332ca37:

  • 177/177 focused tests passed: scheduler coordination 6/6, orphan recovery 29/29, restart integration 3/3, subagent registry 139/139.
  • Targeted oxfmt, oxlint, and git diff --check passed.
  • Hosted changed gate passed on Testbox tbx_01kyye5erdthnmyh9k35b61neh: https://github.com/openclaw/openclaw/actions/runs/30695937637
  • Canonical P1 autoreview found no accepted/actionable findings.
  • Six independent exact-SHA reviews found no P0/P1 issues.
  • Latest ClawSweeper review is 4/6 Platinum Hermit with no findings or security findings.
  • Repo-native review artifacts validate as READY FOR /prepare-pr with zero findings.

Known gaps: none for the scoped scheduler invariant. The PR does not change registry persistence, dispatch/remap semantics, protocol, configuration, or public APIs.

@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. labels Aug 2, 2026
@vincentkoc
vincentkoc merged commit 10ae5b6 into main Aug 2, 2026
193 of 205 checks passed
@vincentkoc
vincentkoc deleted the perf/collector-generation-ownership-20260731 branch August 2, 2026 03:00
@vincentkoc

Copy link
Copy Markdown
Member Author

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 gateway Gateway runtime maintainer Maintainer-authored PR 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. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. 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.

Subagent orphan recovery can double-resume the same orphaned session on restart (no in-progress guard across concurrent scans)

1 participant