Watchdogs for Long Sessions

An overnight program of feature lanes or a multi-hour bug hunt fails in two quiet ways. It exhausts the usage limit of the plan, or an agent, Docker or a test process hangs unnoticed. The ft-watch skill puts two watchdogs on such a run, and you start them only when the run needs them. Choose a scenario and watch one run end.

Skip the map

You

  • /ft-watchYou type this command in the Claude Code session that runs the long job.The command is the skill file skills/ft-watch/SKILL.md. It tells the main loop how to start, re-arm and stop the watchers, and what to do on each exit code.

The session

  • main loopThe Claude Code session in which you talk to the orchestrators. It starts the watchers and acts on each exit code.
  • lanesA lane is one running top-level orchestrator. Its agents, processes and containers do the long work.

The watchers in watch.py

  • token watcherReads the 5-hour and the 7-day usage window every 5 minutes.
  • freeze watcherRuns its checks every 2 minutes and reports each new finding once.

How a run ends

  • exit 10Re-arm: nothing new in this run.
  • exit 2Token STOP: a usage window reached the threshold.
  • exit 7Freeze: one or more new findings.
  • exit 3Bad arguments.

The token watcher reads

  • OAuth tokenYour Claude Code login token. The usage endpoint requires it.
  • usage endpointReturns how much of each usage window you used.

The freeze watcher reads

  • sub-agent transcriptsShow when each sub-agent last wrote, and its last step.
  • process listShows how long each Maven, Docker client and wait-loop process has run.
  • DockerAnswers docker info and lists the running containers.
  • lock filesThe age of a lock comes from its modification time.
  • your --extra checkYour own command. It prints one finding per line.
  • state fileHolds the key of each finding that the watcher already reported.

Blue: you and the session. Orange: the watchers and how a run ends. Grey: what the watchers read. Choose a scenario, or select an element to read about it.

Nothing new: exit 10

  1. You type /ft-watch. The main loop says in one line what is armed.
  2. The main loop starts watch.py as a background task from the project directory.
  3. Every 5 minutes the token watcher reads both usage windows. Both are under the threshold.
  4. The same task runs the freeze watcher.
  5. Every 2 minutes the freeze watcher runs its checks. Each check finds nothing new.
  6. After 25 minutes the run prints REARM and exits with 10.
  7. A task notification reaches the main loop. It sends you no message.
  8. The main loop starts the same command again. This re-arm is one turn.

The usage limit is near: exit 2

  1. The token watcher reads your OAuth token.
  2. It reads the usage figures. The 5-hour window shows 94%, and the threshold is 93%.
  3. It prints a TOKEN STOP line and exits with 2.
  4. A task notification reaches the main loop.
  5. The main loop launches no new agent. Running agents finish or reach their ## HANDOFF.
  6. It tells you the window, the percentage and the reset time.
  7. After the reset time, it re-arms the token watcher.

An agent is stuck: exit 7

  1. One transcript has no write for 30 minutes, and its last step is a tool call.
  2. The key agent:<id> is new, so the watcher stores it.
  3. It prints a FREEZE line and exits with 7.
  4. A task notification reaches the main loop.
  5. The main loop confirms with ListAgents, stops the agent and relaunches it from its last ## HANDOFF.
  6. It re-arms the watcher. The state file keeps this key, so this finding does not return.

Docker stops answering: exit 7

  1. docker info gives no answer within 20 s.
  2. The watcher prints a FREEZE docker-api line and exits with 7.
  3. A task notification reaches the main loop.
  4. The main loop probes Docker once more with a bounded call.
  5. A restart kills the containers of every lane. So the main loop needs your consent or a standing rule of the session.
  6. It re-arms the watcher.

The usage endpoint gives no answer

  1. The endpoint gives no answer. The watcher prints usage=unavailable and keeps going.
  2. The value is unknown, so no STOP can fire. The run ends with 10 after 25 minutes.
  3. A task notification reaches the main loop.
  4. The main loop re-arms the watcher.

A bad argument: exit 3

  1. You type /ft-watch tokens 120.
  2. The main loop passes the value as --stop 120.
  3. The value must be above 0 and at most 100. The script exits with 3 before it watches anything.
  4. A task notification reaches the main loop.
  5. The main loop shows you the error. It does not retry blindly.

How the parts connect

  • You type the command in the session, and the main loop answers you.
  • The main loop starts and re-arms the watchers as background tasks.
  • The token watcher reads the token, then calls the usage endpoint with it.
  • The freeze watcher runs each of these checks on every poll.
  • The freeze watcher stores each new key, and drops a key that it already knows.
  • A run with nothing new ends with exit 10.
  • A window at the threshold ends the run with exit 2.
  • A new finding ends the run with exit 7.
  • A bad argument ends the run with exit 3.
  • Each exit code reaches the main loop as a task notification.
  • The main loop launches no new agent after a STOP, and it handles a stuck agent.
  • The main loop probes Docker again after a docker-api finding.

The token watcher stops before the hard limit

A STOP is exit code 2 of this watcher: a usage window reached the threshold.

  1. 1. Read the token

    The watcher reads your Claude Code OAuth token. It never prints, logs or writes the token.
  2. 2. Read the usage

    It reads the 5-hour and the 7-day window every 5 minutes. The endpoint rate-limits, so never poll faster.
  3. 3. Compare with the threshold

    The default threshold is 93%. Set it with --stop.
    • BELOWIt prints an ok line and waits.
    • REACHEDIt prints a TOKEN STOP line and exits with 2.
    • NO ANSWERIt prints usage=unavailable and keeps going.
macOS, Linux
python3 ~/.claude/skills/ft-watch/watch.py tokens --stop 93 09:24:48 ok 5h=88% 7d=50% 09:29:48 ok 5h=91% 7d=50% 09:34:48 TOKEN STOP 5h=94% (threshold 93%) RESET_EPOCH=1792056000 RESET_LOCAL=2026-10-15 11:20 WINDOW=5h
IllustrationThe main loop runs this command for you. The line formats are real, and the values are invented.

A threshold below 100% leaves room for running agents to finish their step and write a handoff. Without it, they stop mid-edit at the hard limit.

The usage endpoint can change

The endpoint is not a documented API. When it does not answer, the value is unknown, and the watcher does not treat it as 0%. No STOP can fire until it answers again.

Where the figures and the token come from

The figures come from https://api.anthropic.com/api/oauth/usage, called with your OAuth token.

On macOS the watcher first reads the Keychain item Claude Code-credentials. Otherwise it reads ~/.claude/.credentials.json. It honours $CLAUDE_CONFIG_DIR for that file.

The freeze watcher reports what looks stuck

Every 2 minutes it runs the checks below. Each bar shows how long a thing may run before it becomes a finding. Open a row for its rule.

Limit before a finding

Docker gives no answer20 s

The key is docker-api:<hour>. docker info gives no answer within 20 s, or it fails. The watcher runs this check only when docker is on PATH. The key holds the hour, so this finding returns at most once per hour. No flag changes this limit.

Agent with no progress adjustable20 min

The key is agent:<id>. A sub-agent transcript has no write for 20 to 180 minutes, and its last step is a tool call with no later step. --idle-min sets the 20, and the 180 is fixed. A transcript with no write for more than 180 minutes is not a finding.

A transcript that ends in text or a hand-back is a finished agent. An agent that waits on a foreground child agent is not stuck, since the watcher checks the child on its own.

By default the watcher reads the subagents directory of the newest session of the current project: ~/.claude/projects/<project-slug>/<session>/subagents. Start the watcher from the project directory, or pass --subdir. With no directory it prints (no subagents dir), and this check finds nothing.

Docker client command30 min

The key is docker-cli:<pid>. A docker run or docker exec client runs for more than 30 minutes. No flag changes this limit.

Container adjustable30 min

The key is container:<name>. A container runs for more than 30 minutes. --container-min sets the limit. The watcher skips long-lived containers: a name or image that matches buildkit, mcp, kroki or ryuk.

Maven test or build60 min

The key is maven:<pid>. A Maven, Surefire or Failsafe process runs for more than 60 minutes. No flag changes this limit.

Shell wait loop60 min

The key is wait-loop:<pid>. A shell loop with until or while and sleep runs for more than 60 minutes. No flag changes this limit.

A foreground command that hits the timeout of the Bash tool moves to the background and keeps running. So a loop whose condition never comes true outlives the agent that started it.

Lock file or directory adjustable90 min

The key is lock:<path>:<modification time>. A lock is 90 minutes old or older. --lock-min sets the limit. A lock with a newer modification time is a new finding.

The default glob is ~/.local/state/ft-ai-toolkit/*.lock*. --lock replaces that glob, and you can repeat the flag.

A label marks a limit that a flag changes. Your own --extra check adds any other finding. The watcher reports each finding once and remembers its key in ~/.local/state/ft-ai-toolkit/watch-freeze.seen. Delete the file to see everything again.

A finding is a candidate

The watcher stops nothing by itself. The main loop verifies each finding before it acts, within these limits:

  • Stuck agent

    The main loop confirms with the ListAgents tool or the task status before it stops the agent. A run that completed is not stuck. It relaunches the agent from the last ## HANDOFF of its journal.
  • Long process or container

    It reads the log or the CPU use. It stops the process or container only if it is hung AND belongs to a lane of this session.
  • Old lock

    It finds the holder of the lock. It removes the lock only when the holder is gone.
  • Silent Docker

    It probes once more with a bounded call. A restart of Docker or OrbStack kills the containers of every lane, so it needs your consent or a standing rule of the session.

OrbStack is a Docker runtime for macOS. It is an external product, this repository does not define it, and this definition is unverified.

Each run ends with an exit code

A watcher never runs forever. Each run ends with an exit code, and the main loop gets a task notification and acts on that code.

Exit 10: nothing new → re-arm silently

The main loop starts the same command again and sends you no message. This new start is a re-arm.

Exit 2: token STOP → launch nothing new

The main loop launches no new agent, workflow or parallel batch of agents. Running agents finish or reach their ## HANDOFF, the resume block in the journal of an agent. The main loop tells you the window, the percentage and the reset time. It does not re-arm the token watcher at once. It re-arms that watcher after the reset time, when the window starts over.

Exit 7: new findings → verify, act, re-arm

Each finding is one FREEZE <key> <detail> line. The main loop verifies each one and acts within its limits. Then it re-arms the watcher.

Exit 3: bad arguments → show you the error

The main loop shows you the error and does not retry blindly.

Why the watchers are on demand

Every re-arm is one turn of the main loop, and that turn re-reads the whole conversation. Both costs below are estimates. They depend on the context size and the cache state.

25 minlongest runSet it with --minutes. Claude Code kills a background task after about 30 minutes.
~$0.15one re-armThe estimate for one turn at 300K tokens of context.
~$4ten hours of re-armsThe estimate for re-arming through a ten-hour run.

Start the watchers

Check the install once, then type one command in the session that runs the long job.

  1. 1

    Check the install

    The self-test covers the transcript states, the process, container and lock rules, the report-once state file, usage parsing and the STOP threshold. It does not reach Docker or the usage endpoint. A passing run prints selftest OK.

    macOS, Linux
    python3 ~/.claude/skills/ft-watch/watch.py --selftest selftest OK
    Real outputCaptured on macOS.
    Windows
    python %USERPROFILE%\.claude\skills\ft-watch\watch.py --selftest
    You
  2. 2

    Probe the usage endpoint

    This command makes one live call to the usage endpoint and exits with 10. It exits with 2 when a window is already at the threshold.

    macOS, Linux
    python3 ~/.claude/skills/ft-watch/watch.py tokens --minutes 0.1
    You
  3. 3

    Type one command

    Choose the command by what the run needs:

    Both watchers → /ft-watch

    The token threshold is 93%.

    Token watcher only → /ft-watch tokens 95

    The STOP comes at 95%. The number becomes --stop 95. With no number, the threshold is 93%.

    Freeze watcher only → /ft-watch freeze

    The token watcher stays off.

    Both, with your own limits → /ft-watch all 95 --idle-min 30

    Extra flags go to watch.py verbatim. The list is below these steps.

    End the watch → /ft-watch stop

    The main loop stops every running watcher of this session and says which ones it stopped.

    You
  4. 4

    Read the one-line confirmation

    The main loop first checks its background tasks, and it never runs two watchers of the same mode. It starts the command as a background task and confirms in one line what is armed, at which threshold. After that, it reports only a STOP or a finding.

    Main loop

Every flag of watch.py

Add any flag after the command, as in /ft-watch freeze --container-min 45.

FlagDefaultSets
--stop93Token STOP threshold, in percent.
--minutes25Run length before exit 10.
--poll120Freeze check interval, in seconds.
--token-poll300Usage probe interval, in seconds.
--subdirNewest session of this projectThe sub-agent directory to watch.
--idle-min20Agent idle limit, in minutes.
--container-min30Container age limit, in minutes.
--lock~/.local/state/ft-ai-toolkit/*.lock*Lock glob. Repeatable.
--lock-min90Lock age limit, in minutes.
--extraNoneYour own check command.
--state~/.local/state/ft-ai-toolkit/watch-freeze.seenThe state file of reported keys.

Platform differences

skills/ft-watch/watch.py is one stdlib-only Python script for macOS, Linux and Windows. It has no .sh and .ps1 pair, and these differences live inside the script.

ItemmacOS and LinuxWindows
Pythonpython3python or py -3
Process listps -A -o pid=,etime=,args=PowerShell Get-CimInstance Win32_Process
OAuth tokenKeychain on macOS, else ~/.claude/.credentials.json%USERPROFILE%\.claude\.credentials.json
--extraRuns through /bin/sh -cRuns as a command line
State file~/.local/state/ft-ai-toolkit/watch-freeze.seen%USERPROFILE%\.local\state\ft-ai-toolkit\watch-freeze.seen

Add your own check

Project-specific checks stay outside the toolkit and plug in through --extra. Examples are a demo host, a remote runner and a deploy lock.

  1. 1

    Write a command that prints findings

    The command runs on every freeze check. It prints one finding per line, as <key> <detail>. No output means nothing to report.

    The watcher reports each key once, so keep the key stable. A command that gives no answer in 90 s becomes the finding extra-timeout:<hour>.

    This script is an example only. It names one demo host and reports a deploy lock held for 4 hours or more.

    bash script: demo-check.sh
    #!/usr/bin/env bash # demo-check.sh: report a deploy lock held for 4 hours or more on a demo host age=$(ssh -o ConnectTimeout=15 demo 'echo $(( ($(date +%s) - $(stat -c %Y /root/DEMO-OPS.lock)) / 60 ))' 2>/dev/null) [ "${age:-0}" -ge 240 ] && echo "demo-lock DEMO-OPS.lock held $age min"
    You
  2. 2

    Start the freeze watcher with the command

    Claude Code session
    /ft-watch freeze --extra "bash ~/bin/demo-check.sh"
    You
Keep credentials out of the --extra command line

The process list and the session transcript show that command line. Use an SSH key, an agent or an environment variable that the script reads.

Questions about the watchers

The questions that readers ask before they arm a long run.

Can an agent start the watchers?

No. skills/ft-watch/SKILL.md sets disable-model-invocation: true, so only you invoke it. It costs no context in sessions that do not use it.

Does a token STOP end the freeze checks too?

A separate freeze watcher keeps running. /ft-watch all runs both watchers in one process, so its exit 2 also ends its freeze checks.

How do the watchers reach my machine?

The installers deploy/scripts/install.sh and install.ps1 install SKILL.md and watch.py with the other skills.

Related pages

The pages that this page builds on.

The harnessThe hooks around every agent call are on Agent Harness.
The termsLane, main loop and the other terms are in the Glossary.
The skill listThe ft-watch skill and the other skills are in the Claude toolkit reference.
Start the watchers