agent-stall-watch

Watch a long autonomous session's background agents and wake the main session when the chain stalls. One node script (no dependencies) reads the agent transcripts, the background task output files and

SKILL.md

GitHub

Agent Stall Watch

In a long autonomous session the main session hands work to background agents, and they hand work to their own agents. The main session only acts when a notice arrives. If one agent waits on something that will never send a notice, the whole chain waits with it. On 2026-11-26 that cost 4 hours: fix agent FA started a Playwright run through a launcher that exited at once, so the real run's end produced no notice; FA waited, the lead waited on FA, and the main session waited on the lead. Earlier, agents sat up to 43 hours on a Bash call that a safety check held for the user's approval.

This watch runs as one background task. When something is stuck it ends with one line, and that line arrives in the main session as one notice.

Start it

The UserPromptSubmit hook does the asking. When the user's prompt asks for a long autonomous session, the hook adds the exact command, with the session id and the script's full path filled in. Run it with the Bash tool and run_in_background: true, running the script directly. From the repo root it is:

node .claude/skills/agent-stall-watch/scripts/stall-watch.mjs --session <session-id> --fresh
  • The session id is the folder above your scratchpad (...\<session-id>\scratchpad), and the hook prints it. --session latest picks the newest session of the project you are in.
  • --fresh is for a new long session: it forgets stalls reported earlier in this session. Leave it off when you restart the watch after a STALL, so a repeat can be seen as an IMPASSE.
  • A second start while a watcher for the same session is alive prints ALREADY and exits.
  • Never start it with nohup, &, Start-Process or a launcher. Its one line has to come back through the harness's own background task, or no notice comes.

What it watches

Every 60 seconds it looks at the agents whose transcripts changed since the watch started or in the last 6 hours (~/.claude/projects/<project>/<session-id>/subagents/agent-<id>.jsonl, with the description from agent-<id>.meta.json). It also checks any older agent whose meta file is not marked "finished": true, however old: such an agent still shows as running in Claude Code. On 2026-11-26 the 6-hour window hid f3's fix agent R, interrupted at a turned-down prompt on Nov 16, for 10 days. For each agent it reads the end of the transcript:

  • a last line [Request interrupted by user ...] means the agent was stopped at a turned-down prompt or an Esc and will never resume by itself
  • a tool_use in the agent's last message with no tool_result is an open call
  • a last message that ended the turn is idle; its text is the agent's last words
  • a tool result or message with no reply after it means the agent is working

Activity is the newest change time across those transcripts, the background task output files (%TEMP%\claude\<hash>\<session-id>\tasks\<task-id>.output, ended when the file ends with [exited with code N] or [killed]) and the scratchpad files (top level and one folder down). A long Playwright run that writes a log counts as activity. Dev-server logs do not: a file whose first 4 KB looks like a server (for example "msg":"api listening", Local: http://, HTTP access lines) and not like a test run is left out, or DONE would never come while the API logs a health check every two minutes.

What the line means

Line When What you do
STALL interrupted An agent's last line says it was interrupted (a turned-down tool call or an Esc). Reported at once, at any age, with its last call. Check whether another agent finished its work (the phase review usually says). If not, resume it with SendMessage and the rest of its brief. If so, send it "your work is done, make no tool calls, reply closed", which marks it finished. Then start the watch again.
STALL open-call An agent has had a tool call open over 15 min (the Bash foreground limit is 10, so it is a held permission prompt or a hang), or no reply 15 min after its last tool result. Read the command it prints. A held prompt cannot be cleared by SendMessage: tell the user which agent and command wait for approval, or stop that agent and redo its step. Then start the watch again.
STALL waiting An idle agent's last words say it waits, and the task it names (b plus 8 letters or digits) ended over 3 min ago, or nothing has moved for 30 min. Find the agent, read the log of the run it waits on (the line gives the output file), and wake it with SendMessage giving the result. Then start the watch again.
COST heavy An agent that will call again (working, or idle and waiting) sent over 450k tokens of context on its last call. Every later call re-reads all of it. Reported once per agent; it never becomes an IMPASSE. Tell the agent to hand off at its next commit or phase end: write a handoff file, stop everything it started (servers too), and end. Start a fresh agent from the handoff. Then start the watch again. If agents keep passing 450k, check that autoCompactWindow is set (see Cost below).
DONE No open calls, nothing active for 15 min (the main session's own transcript included), and no agent says it waits. Nothing. Do not start it again.
IMPASSE repeat A stall already reported (same agent, same open call or same last words) is still there 5 min after the restart. Waking did not help. Do not start it again. Tell the user in plain words what is stuck and on whom.
IMPASSE user Every waiting agent says it waits on the user ("needs the user", "waiting on the user", a Blocked: status) and nothing else runs. Do not start it again. Tell the user what each agent needs from them.
ALREADY A watcher for this session is alive. Nothing.

The line names the agent by its full id (the one SendMessage takes), its description, how long it has been stuck, the first 200 characters of the command or of its last words, and the task's state and output file. Other waiting agents are listed after also waiting:.

On a STALL

  1. Find the agent from the line (agent=<id> "<description>").
  2. Read the log of the run it waits on: the task output file in the line, or the run log the agent named in the scratchpad. Read the end of it, not the whole file.
  3. Wake the agent with SendMessage: say the run ended, give the counts or the error, and say what to do next. If the agent is no longer needed, say so and let it finish.
  4. Start the watch again with the same command, without --fresh.

If the stall is an agent you stopped on purpose or no longer need, start the watch again with --ignore <agent-id> so it stays out of the checks. The ignore list is kept in the state file.

On DONE or IMPASSE

Do not start it again on your own. On IMPASSE, tell the user in plain words: which agent, what it waits on, since when, and what would unblock it. If the user wants the rest of the chain watched while one agent stays stuck, start it again with --ignore <that agent's id>.

Commands

W=.claude/skills/agent-stall-watch/scripts/stall-watch.mjs   # from the repo root
node "$W" --session <id> --fresh          # start (run_in_background: true)
node "$W" --session <id> --once           # status table and what it would say now; writes nothing
node "$W" --session <id> --stop           # stop this session's watcher by its pid
node "$W" --help

/stall-watch start|stop|status|check-now wraps these. --stop uses process.kill on the pid in the state file, and only when that watcher's heartbeat is recent. From PowerShell the same works with Stop-Process -Id <pid>; read the pid with --once first.

Limits, all in minutes and all allowed to be fractions: --open-minutes 15, --quiet-minutes 30, --done-minutes 15, --grace-minutes 5, --wake-minutes 3, plus --window-hours 6 and --interval-ms 60000. --max-loops N is for the tests.

State file

<scratchpad>/agent-stall-watch.state.json: the watcher's pid, start time and heartbeat, every stall it reported (key, count, time, line), the last line and the ignore list. A watcher counts as alive only when its pid answers and its heartbeat is recent, so a reused pid on Windows is not mistaken for one. The script writes only this file and deletes nothing.

Briefs for agents in a long session

Put these two rules in every brief that might start a run and wait on it:

  1. A run you wait on must be the Bash tool's own run_in_background task running the command itself. Never nohup, &, disown, Start-Process or a launcher that exits at once: the task ends at once, and no notice comes when the real run ends.
  2. When you say you are waiting, name the task id, for example "Waiting for task bnm5j70bk (the focus test three times)". The watch then knows when that run ended.

For a lead, add three more:

  1. Lead one phase, then hand off. At the phase commit, write a handoff file, stop everything you started, and end. The main session starts the next lead from the handoff.
  2. Never hold long-lived dev servers. The main session owns them. Stop any server you start for a test before you report. An agent holding a server never finishes and shows as alive for good.
  3. Ask your subagents to write long reports to a file and return a short summary, because every word they return lands in your context.

Cost

Measured in CDP session 99e9d550 on 2026-11-26 and 27, with Claude Code 2.1.332 on Opus 5.5:

  • An idle agent makes no model calls, so it costs nothing while it waits. It pays when something wakes it: a task ending, a subagent reporting, a SendMessage. The lead made no calls during a 40-minute wait.
  • Each call re-sends the agent's whole context. Opus 5.5 compacts only at about 967k tokens by default, so worker agents grew to between 570k and 967k. The f10 lead read about 700k per call over 547 calls in a day.
  • Subagents get a 5-minute prompt cache. A longer wait rebuilds the whole context once. Setting subagentPromptCacheTtl to 1h would have saved only 3% across 8,827 calls, because every ordinary cache write then costs more. Leave it at the default.
  • A smaller autoCompactWindow is the setting that saves. Replaying the same 8,827 calls: 400k saves about 43% (37 extra compactions across 45 agents), 300k about 51% (66). CDP sets "autoCompactWindow": 400000 in .claude/settings.json. Subagents inherit it.
  • Closing an old idle agent with SendMessage re-reads its whole context: about 304k tokens for a one-line reply. Close old agents only when they would otherwise show as running.

Rules

  • Never run rm, rm -rf or rmdir while using this component; a safety check holds those for the user, which is one of the stalls this watch exists to catch. Overwrite files instead.
  • Do not call python3; it is the Microsoft Store stub on this machine and hangs.
  • The watch reads session transcripts and never writes them.
  • Stop the watcher you started when the session winds down, if it is still running.

Tests

node .claude/skills/agent-stall-watch/scripts/stall-watch.test.mjs --root <a scratch folder>

They build fake session folders under the root, set change times with fs.utimesSync, and cover: open call, waiting on an ended task, quiet chain, long run with a changing log, done with a dev server still logging, repeat, waiting on the user, ALREADY, --stop, --once, and the hook (matching, misspelled, not matching, notification, bad input, watcher alive, under 1 s).

Related

  • Agent agent-stall-watch-agent (runs --once and explains each stall with the next step).
  • Command /stall-watch. Hook .claude/hooks/scripts/agent-stall-watch-detect.sh (UserPromptSubmit, in .claude/settings.json).
  • Docs .claude/system_docs/agent_stall_watch/ (README, USAGE_GUIDE, ARCHITECTURE, SYSTEM_OVERVIEW).