Skip to content

fix(gateway): preserve channel restart ownership - #104492

Closed
arcabotai wants to merge 1 commit into
openclaw:mainfrom
arcabotai:fix/channel-health-restart-ownership
Closed

arcabotai wants to merge 1 commit into
openclaw:mainfrom
arcabotai:fix/channel-health-restart-ownership

Conversation

@arcabotai

@arcabotai arcabotai commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • keep ChannelManager authoritative while crash-loop backoff is active
  • expose a closed restart state (idle | backoff | gave-up) to the health monitor
  • prevent health-monitor sweeps from clearing active manager retry progress
  • preserve the existing bounded post-give-up health-monitor revival policy
  • preserve monitor-owned recovery for genuinely idle, unhealthy, and timed-out recovery paths

Fixes #104458

What Problem This Solves

The channel manager owns crash-loop attempts and backs off between retries. The health monitor separately evaluates stopped accounts and calls resetRestartAttempts() before recovery.

During an active manager backoff, a health sweep could therefore:

  1. clear the manager's private attempt count,
  2. call startChannel(), which no-ops because the manager task still exists,
  3. let the manager resume with its retry progress erased.

That composition prevents the manager from reaching its retry cap and can keep a persistently failing account in an unbounded restart loop.

The monitor cannot safely infer manager ownership from snapshot fields such as running, restartPending, and reconnectAttempts, because timed-out monitor recovery also uses restartPending. This patch puts the ownership decision at the manager boundary that owns the private attempt map.

Implementation

ChannelManager.getRestartState() returns:

  • idle: no active manager backoff; the health monitor may own recovery
  • backoff: manager retry progress is active and must not be reset
  • gave-up: the manager exhausted its retry budget

The health monitor skips only backoff. A gave-up account continues through the monitor's existing cooldown and maxRestartsPerHour budget, preserving the established bounded revival contract. Timed-out stop recovery takes precedence over retained crash-attempt state and reports idle, allowing the monitor to issue the second start required to replace the stale task.

Evidence

RED: current-main behavior before the fix

The manager-plus-monitor integration regression uses the real createChannelManager, real startChannelHealthMonitor, production retry limit, and deterministic fake time.

Before the fix, the monitor cleared active manager progress and the failing account reached 16 starts instead of terminating its first manager cycle at 11.

active-backoff regression:
  expected first manager cycle: 11 starts
  received:                     16 starts

GREEN: exact-head behavior after the fix

With the monitor active through the first six attempts, the manager now observes the complete monotonic sequence:

reconnectAttempts at each start: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
first manager cycle:             11 starts

The monitor is then stopped in that test so the first cycle can be asserted independently from the separate post-give-up policy.

A second real manager-plus-monitor integration test configures maxRestartsPerHour: 1 and proves the existing bounded revival contract remains intact:

initial manager cycle: 11 starts
one monitor revival:   11 starts
total:                 22 starts
further revival:       blocked by monitor hourly budget

A composed timeout regression proves the hot-reload sequence with retained retry history:

prior crash attempts:            2
third provider task:             running, then ignores abort
non-manual stop:                 times out
first reload-style start:        arms timed-out recovery
restart state after first start: idle (monitor-owned)
next monitor sweep:              replaces stale task
replacement reconnectAttempts:   0

Focused validation:

$ pnpm test \
  src/gateway/channel-health-monitor.test.ts \
  src/gateway/channel-health-policy.test.ts \
  src/gateway/server-channels.test.ts \
  src/gateway/server/readiness.test.ts

Test Files  14 passed (14)
Tests       398 passed (398)

Changed-file gate

$ OPENCLAW_CHECK_CHANGED_REMOTE_CHILD=1 CI=1 \
  pnpm check:changed -- \
  src/gateway/channel-health-monitor.ts \
  src/gateway/channel-health-monitor.test.ts \
  src/gateway/server-channels.ts \
  src/gateway/server-channels.test.ts \
  src/gateway/server/readiness.test.ts

exit 0

This includes production and test typechecks, changed-file lint, dependency/package guards, database-first guards, and runtime import-cycle checks.

Review feedback addressed

The reviews identified two distinct ownership boundaries: manager give-up must preserve bounded monitor revival, and timed-out stop recovery must remain monitor-owned even when prior crash attempts are retained. The revised patch now:

  • protects only active manager backoff,
  • preserves post-give-up bounded revival,
  • restores the focused monitor contract test,
  • adds cross-module proof that the revival remains capped,
  • gives timed-out recovery precedence over attempt-based backoff classification,
  • adds the composed prior-crashes → reload timeout → monitor replacement regression.

Compatibility and risk

  • no configuration changes
  • no protocol changes
  • no provider/channel-specific behavior
  • existing bounded post-give-up revival is preserved
  • ordinary idle unhealthy accounts remain restartable
  • manual and terminal-disconnect guards are unchanged
  • timed-out recovery remains monitor-owned
  • stable runs still reset crash-loop attempts through the existing manager path

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: S labels Jul 11, 2026
@clawsweeper clawsweeper Bot added 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. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. labels Jul 11, 2026
@clawsweeper

clawsweeper Bot commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the context here. I did a careful shell check against current main, and this is already implemented.

Close: current main already preserves manager-owned crash-loop backoff using a narrower recorded ownership fact, so this conflicting branch no longer provides a distinct fix.

So I’m closing this as already implemented rather than keeping a duplicate issue open.

Review details

Best possible solution:

Keep current main’s recorded retry-supervisor ownership boundary and route any distinct current-main failure to a new focused report.

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

No. Current main records active retry-supervisor ownership and its focused tests prove the monitor neither resets attempts nor starts the account during that backoff.

Is this the best way to solve the issue?

No. The branch infers ownership from retry state, whereas current main records the authoritative in-flight supervisor fact and preserves the distinct timed-out-stop recovery path.

Security review:

Security review cleared: The branch changes only gateway lifecycle TypeScript and tests, with no dependency, workflow, credential, permission, or artifact-execution surface.

AGENTS.md: found and applied where relevant.

What I checked:

Likely related people:

  • Peter Steinberger: Commit history identifies the matching crash-loop give-up repair as the canonical gateway fix. (role: canonical fix author; confidence: high; commits: 991a521d12a9; files: src/gateway/channel-health-monitor.ts, src/gateway/server-channels.ts)
  • Josh Avant: Current-source blame attributes the active ownership guard to this available mainline source commit; shallow history limits stronger attribution. (role: recent area contributor; confidence: medium; commits: 618fba92bf01; files: src/gateway/channel-health-monitor.ts, src/gateway/server-channels.ts)

Codex review notes: model internal, reasoning high; reviewed against c925634ae83b; fix evidence: commit 618fba92bf01, main fix timestamp 2026-08-04T20:05:15-05:00.

@arcabotai

Copy link
Copy Markdown
Contributor Author

Revised the policy boundary at 2ea60ecfc183 as requested.

  • the health monitor now skips only active manager backoff
  • established bounded post-give-up revival is preserved
  • integration coverage proves uninterrupted manager attempts 0..10 while the monitor is active
  • a separate real manager-plus-monitor test proves one configured bounded revival: 11 + 11 = 22 starts, then the hourly cap blocks more
  • 396/396 focused tests pass; check:changed passes

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 11, 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.

Re-review progress:

@clawsweeper clawsweeper Bot added P1 High-priority user-facing bug, regression, or broken workflow. and removed P2 Normal backlog priority with limited blast radius. labels Jul 11, 2026
@arcabotai
arcabotai force-pushed the fix/channel-health-restart-ownership branch from 2ea60ec to f5f7628 Compare July 11, 2026 19:31
@arcabotai

Copy link
Copy Markdown
Contributor Author

Addressed the timed-out hot-reload ownership finding at f5f7628aa1ea.

getRestartState() now gives recoveryStopTimedOut precedence over retained crash-attempt classification, so the monitor can issue the required second recovery start instead of skipping it as backoff.

Added a composed production-manager + production-monitor regression: two prior crashes → hanging third task → non-manual stop timeout → first reload-style start arms recovery → monitor sweep replaces stale task → reconnect attempts reset to zero.

Focused result: 14 files / 398 tests passed. check:changed passed before the final rebase, and the exact post-rebase focused suite passed.

The PR body now contains this sequence. I am still treating the requested live gateway/channel transcript as a separate proof requirement rather than pretending fake-time output is live evidence.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 11, 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.

Re-review progress:

@arcabotai
arcabotai force-pushed the fix/channel-health-restart-ownership branch from f5f7628 to 1d7a7be Compare July 12, 2026 21:54
@arcabotai
arcabotai force-pushed the fix/channel-health-restart-ownership branch from 1d7a7be to be8ca9e Compare July 23, 2026 03:11
@clawsweeper clawsweeper Bot removed merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. labels Aug 5, 2026
@clawsweeper

clawsweeper Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper applied the proposed close for this PR.

@clawsweeper clawsweeper Bot closed this Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime P1 High-priority user-facing bug, regression, or broken workflow. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: S status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Channel health monitor clears crash-loop progress and revives accounts after give-up

1 participant