Skip to content

fix(gateway): stop restart drains waiting on approval-blocked runs - #15

Merged
tomorrowflow merged 1 commit into
mainfrom
fix/restart-drain-approval-blocked-runs
Oct 5, 2026
Merged

tomorrowflow merged 1 commit into
mainfrom
fix/restart-drain-approval-blocked-runs

Conversation

@tomorrowflow

Copy link
Copy Markdown
Owner

Problem

openclaw gateway restart took ~5 minutes whenever an agent run needed plugin approval mid-turn. Observed live on 2026-10-05 (13:54:24 → 13:59:24):

  • The restart drain refuses plugin.approval.request / plugin.approval.waitDecision (unavailable during gateway restart), so the heimdallr Signal run's openhab-admin__execute_script call failed with "Plugin approval required (gateway unavailable)".
  • The model retried into the same refusal (~2.5 min per turn on qwen3.8:27b), so the run could never finish.
  • The drain waited its full 300 s budget (derived from TimeoutStopSec=330, which Doctor/update refreshed from the old 30 s per fix: refresh stale Gateway shutdown budgets during updates and Doctor openclaw/openclaw#153636), then shutdown marked the session for restart recovery and aborted the run anyway.

Solution

  • Approval hook (agent-tools.before-tool-call.approval.ts): when the gateway refuses an approval with the structured gateway-restarting reason during the local one-way restart drain, park the tool call on the run's abort signal instead of failing it to the model, and record the parked session with the admission owner (gateway-work-admission.ts). The record is released when the park ends and cleared on admission reset.
  • Active-work wait (gateway-active-work.ts): optional release predicate.
  • Restart drain (run-loop-drain.ts): release when every active embedded run is parked. Independent work (cron, background exec, ACP, media, session mutations, terminal sessions/persistence, lifecycle writes) keeps it waiting. Unattributed counts must stay within what the parked runs hold: one reply, root request, chat run, agent run and admission each, plus session-lane + global-lane tasks.
  • Run loop (run-loop.ts): when the drain released parked runs, server close gets drainTimeoutMs: 0, so its reply drain does not wait again. prepareGatewayRunShutdown still marks the runs for restart recovery and aborts them, exactly as on budget exhaustion.

Impact

A restart with a run blocked on approval now proceeds within one 250 ms poll instead of the full stop budget. The turn resumes through restart recovery on the successor gateway, where approval can be requested again. Restarts without such runs are unchanged.

Not covered: exec approvals have the same refusal shape (follow-up). If a run's session id rotates mid-run, the drain falls back to the full wait (safe direction).

Evidence

  • pnpm test src/cli/gateway-cli/run-loop.test.ts: 214 passed (~68 s wall for the file). New case: a foreign pending reply blocks release, then release fires and close gets drainTimeoutMs: 0. It fails on the prior code (no release predicate).
  • pnpm test src/agents/agent-tools.before-tool-call.approval-policy.test.ts: 4 passed. New case: a restart refusal parks until run abort and releases the record; a non-restart UNAVAILABLE does not park.
  • src/process/gateway-work-admission.test.ts (41) and src/infra/gateway-active-work.test.ts (7) passed.
  • pnpm tsgo:core clean; oxlint and oxfmt clean on changed files. tsgo:core:test fails only on ui/src/lib/nodes/index.ts (@noble/hashes not installed in the gateway-only worktree), which this change does not touch.
  • Fork tooling: the vitest worker compiler requires .github/workflows/plugin-npm-release.yml, which the fork deleted. It was restored temporarily for the local test runs only.
  • An independent review found two issues (a foreign in-flight reply could be cut short; a run that recovered could be aborted); both are fixed here.
  • Not yet proven against the live gateway; the deploy follows separately.

🤖 Generated with Claude Code

A restart drain refuses plugin.approval.request/waitDecision, so a run that
needs approval can never finish during the drain. The tool failure went
back to the model, which retried into the same refusal until the full drain
budget (300s with TimeoutStopSec=330) expired, and then shutdown aborted the
run into restart recovery anyway.

Park a tool call whose approval the restart drain refuses on its run's abort
signal and record it with the admission owner. The restart drain releases
as soon as every active embedded run is parked and no independent work is
active (unattributed replies, requests, and lane tasks stay within what the
parked runs hold). Server close then skips its reply drain for those runs
and marks/aborts them through the existing restart-recovery path, minutes
earlier.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@tomorrowflow
tomorrowflow merged this pull request into main Oct 5, 2026
@tomorrowflow
tomorrowflow deleted the fix/restart-drain-approval-blocked-runs branch October 5, 2026 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant