Skip to main content

Keep a run going

When an agent stops making progress, its task shows as stalled, and another agent can take it over. ctrlb watch does this for you. It runs on your machine, watches one run, and starts a new agent for each of your tasks that needs one, within limits that you set. CtrlB itself starts nothing.

Before you start​

  • Install the ctrlb binary (Run Recall on your machine). ctrlb watch reads the URL, key and sessions dataset from ~/.ctrlb/config.json, which ctrlb init writes for local mode and the configure step writes for hosted CtrlB. It does not read CTRLB_URL or CTRLB_KEY. It uses HTTPS except on loopback and never prints the key. The agents it starts get the URL and key from the same config.json (see What a started agent does), so the key does not need to be in your shell.
  • The agents it starts must run the CtrlB hooks: Claude Code with the plugin, or Codex with the installer run and its hooks approved.
  • Write ~/.ctrlb/watch.json with the command that starts an agent (below).
Keep the key out of your shell profile

On a machine that runs the watcher, do not export CTRLB_KEY in ~/.zprofile, ~/.zshrc or ~/.bash_profile. An agent can run a login shell, which reads your profile again, and then it can read the key.

Start the watcher​

ctrlb watch --run my-run

--run defaults to $CTRLB_RUN. --harness NAME picks a command from watch.json other than its default, and --config PATH reads another file in place of ~/.ctrlb/watch.json.

The watcher acts only on tasks that your own user registered. In the tasks answer, such a task has own: true. A task registered by another user, or by a registration that names no user, has own: false, and the watcher never starts an agent for it. Agent names are not compared, because two users can use the same agent name.

Recovery (the default)​

The watcher starts an agent for:

  • an own task that is stalled. The new agent takes it over.
  • an own task that a watcher on this machine started an agent for in the current round, when that agent has exited and the task is still open or was left with an open handoff. The new agent claims it.

It never starts an agent for a finished task, for a task with a live hold that is not stalled, or for a task whose last agent is still running.

Fill (--fill)​

ctrlb watch --run my-run --fill

Fill does what recovery does, and also starts an agent for each own open task that nobody holds. It skips a task that waits on an open question named in its ask_about, and a task that had a launch in the last claimGraceSeconds. This time starts when the watcher reserves a launch, in any round of the task, also when no agent starts. These two checks apply only to the tasks that fill adds. A task of recovery does not wait for claimGraceSeconds or for an open ask_about question: it gets a new agent when the wait after its last agent ends (see watch.json). Use --fill only when no person or orchestrator also starts agents for the run. If both start agents, they compete for the same tasks.

watch.json​

{
"harness": "codex",
"harnesses": {
"codex": ["codex", "exec", "{prompt}"],
"claude": ["claude", "-p", "{prompt}"]
}
}

Each harness is a command as a list of arguments. No shell runs it. {prompt} is replaced by a fixed prompt that is part of the binary; when no argument is {prompt}, the prompt goes at the end. An agent starts with no terminal input, so give the command the permissions the agent needs to work without asking. An unknown field in the file is an error.

The other fields are optional:

FieldDefaultMeaning
dirthe watcher's directoryWorking directory of each agent.
maxWorkers2Agents of this watcher that run at the same time.
maxAttemptsPerTask3Attempts at one task in one round (see below).
maxClaimFailuresPerTask5Launches for one task in one round whose claim was refused.
maxLaunchesTotal20Launches for the run from this machine, in all rounds. A launch counts when the watcher reserves it, also when it stops before an agent starts.
maxRuntimeMinutes480After this time the watcher starts no more agents. It checks this again just before each start.
workerTimeoutMinutes60Time limit of one agent.
maxReportedUsd0 (none)Stop starting agents when the run's reported cost reaches this amount.
claimGraceSeconds90With --fill, time to wait after a launch is reserved before fill starts another agent for the same task. Recovery does not wait for it.
pollSeconds15Time between two checks of the run.

An attempt is a launch that the watcher reserved in its ledger, unless the agent's claim of the task was refused:

  • A refused launch is a claim failure, not an attempt. Three refused launches do not use the default three attempts; they count toward maxClaimFailuresPerTask.
  • A launch that the watcher reserved but stopped before the agent started (the watcher stopped, or the start took too long) is an attempt, although no agent ran.
  • A launch whose agent did not report its claim is an attempt.

Before the next launch for a task, the watcher waits after the last launch ended: 30 s after the first launch of the task in the round, 60 s after the second, and 120 s after each later one. This wait counts launches, not attempts, so refused launches also make it longer.

maxReportedUsd stops on reported spend only. Codex sessions report no cost, and session uploads arrive late, so the real guard is the launch limits above.

When a task reaches maxAttemptsPerTask or maxClaimFailuresPerTask, the watcher asks the agent that registered the task one question, and starts nothing more for that task in that round until an answer gives more attempts. This stays true when a later result lowers the count, for example when an attempt turns out to be a refused claim. An answer that starts with retry, in any case, gives one more attempt. When a number of 1 or more follows, as in retry 2, the answer gives that number of attempts, at most 100. An ASCII letter or underscore directly after retry makes a different word, so retryLater gives none. Any other answer gives none. Each question starts with an escalation id, for example ctrlb watch escalation 0123456789abcdef:. The watcher records the id before it asks. If the watcher stops after it records the id and before it records the question, nothing starts for that task until the next watcher finds the question by its id, also when the question has an answer. It does not ask again. It reads the questions back to the time it recorded the id, and asks only when no question has the id. If it cannot read the questions, it asks nothing and starts nothing for that task, and its log says so. The watcher can ask a second time, for example when CtrlB received the question but the reply did not arrive. The two questions have the same escalation id. A retry on either question gives the attempts.

What a started agent does​

Each agent gets one task. The watcher chooses the task before it starts the agent and gives the task's name in CTRLB_TASK, with CTRLB_RUN, CTRLB_AGENT, CTRLB_TASK_MODE (claim, or takeover for a stalled task) and a launch id in CTRLB_LAUNCH. It also gets the connection that its CtrlB plugin and relay tools use, from the config.json the watcher read: CTRLB_URL, CTRLB_KEY, CTRLB_MCP_URL, CTRLB_DATASET, CTRLB_MODE and CTRLB_HOME. The watcher removes every other CTRLB_ variable of its own environment, so an agent always works in the watcher's workspace. The agent's name is the watcher's name, the harness and the start of the launch id, for example watch-1a2b3c4d.codex-5e6f7a8b.

  • The one-task rule. The first session of a launch is told to claim that task by name, to post one note and stop if the claim is refused, and to call done and stop when the task's test passes. It is never told to claim another task.
  • The descendant rule. Every other session of the same launch, such as a subagent or an agent the worker starts from its shell, is told to work for the agent that started it and to edit only the files that agent asked it to edit. It does not claim the task's files, because the agent that started it holds them. When an edit is refused, it tells that agent the change it wants, and that agent makes it. It claims by path only a file that belongs to no task, and releases it when it finishes. When a claim answers belongs_to_task, it stops and tells the agent that started it. It does not claim, reopen or finish a task.

Each agent runs in its own process group with its output in ~/.ctrlb/watch/log/. At workerTimeoutMinutes the watcher sends SIGTERM to the group, so the agent's session-end handoff runs, and SIGKILL 20 seconds later. These time limits have their own timer: a long pollSeconds or a slow answer from CtrlB does not delay them. When an agent exits and leaves programs running in its process group, the watcher stops them in the same way: SIGTERM at once and SIGKILL 20 seconds later. Until the group is empty, the agent counts toward maxWorkers and its task gets no new agent. Each start and each question also posts a note to the run, so you can follow the watcher in the run's timeline. A start note gives the launch number and the attempts and claim failures used before it. The signals go to the agent's process group: a program that the agent moved out of the group is not stopped.

One watcher per run​

Run a run's watcher on one machine. The watcher counts its starts in a ledger file on that machine, under ~/.ctrlb/watch/. There is one ledger for each workspace, sessions dataset and run. CtrlB names the workspace of your key in its answers, or the database of the local server in local mode, and the watcher uses that name. The name contains no part of the key. A new key for the same workspace keeps the counts, and two workspaces with the same run name have separate counts. A CtrlB version that does not send this name is too old for the watcher: it stops with a message. A watcher of the same run on another machine starts its counts at zero. To keep this folder somewhere else, set CTRLB_WATCH_DIR for the watcher; the agents it starts get the same value.

A second watcher of the run on the same machine stops at once. A watcher on another machine is refused: each watcher claims slot:watcher in the run, and the message names the holder. If two agents still start for one task, the claim decides: one holds the task and the other is refused and stops.

Check a run​

ctrlb watch --run my-run --status

It prints the ledger: launches (also the launches that stopped before an agent started), attempts and claim failures for each task and round, questions asked, retries given, and agents that have not exited. It also lists the run's tasks with own and stalled, the reported cost, and stalled holds of files and slots that belong to no task. The watcher does not act on those holds. --status changes nothing and does not need watch.json.

Stop the watcher​

Press Ctrl+C or send SIGTERM. The watcher exits and releases slot:watcher. Its agents keep running, and the next ctrlb watch of the run on the same machine adopts them. An agent's time limit applies only while a watcher of the run is running.

The watcher stops only programs that it can prove belong to an agent. If an agent exits and leaves programs in its process group, the watcher sends SIGKILL to them before it stops. If an agent exits while no watcher of the run is running, the next watcher does not adopt it and counts it as one attempt. It reads the agent's result first: when the result says that the claim was refused, the launch counts as a claim failure instead. It does not stop the programs that the agent left. Stop them yourself.

After maxLaunchesTotal, maxReportedUsd or maxRuntimeMinutes, or when another watcher has taken slot:watcher, the watcher starts no more agents and exits when its agents have ended.