Five identical calls, five rejections, exit 1
RolesAgent Systems EngineeringAI Evaluation and Reliability
The question
A coordinator agent runs in CI with --exec --json. The event stream shows the same run_background_agent call five times, each answered with The user rejected the tool call., then Error: Agent terminated: detected 5 consecutive identical calls to run_background_agent. The same agent file worked on a laptop that morning. Explain the chain, fix it for unattended runs, and add the check that would have caught the change before CI did.
Answer it first, aloud or on paper. Then open the answer.
The answer
Show short answer
Headless means nobody is there to answer a tool_call_confirmation, so the runtime turned the prompt into a rejection; run_background_agent prompted because its label is unknown, the file set no safety mode, and no allow pattern named it, and the model repeated the call until max_consecutive_tool_calls, whose default is 5, ended the run with exit 1. Fix it with --safety restricted plus allow rules for run_background_agent and for the tools the background sub-agent itself calls, because inside a running task a call that would ask is denied automatically; transfer_task needs nothing, since it is the one tool always approved without a prompt. Guard it with two recorded sessions and docker-agent sessions diff --fail-on-divergence -- -1 -2, which compares tool calls, ignores prose and stops at the first divergence.
Show full answer
Three ways to involve another agent
| Mechanism | Added by | What happens |
|---|---|---|
transfer_task | sub_agents | the child runs in a clean sub-session with a system message holding the task, returns its answer as the tool response, and never sees the user's history |
handoff | handoffs | the named agent becomes the active agent for the rest of the conversation and sees the whole history |
run_background_agent | the background_agents toolset | starts a task and returns an id at once; list, view and stop follow it |
transfer_task is always approved without a prompt. A delegation to an agent already in the chain fails, and nesting is capped at 10. A sub_agents entry that is a registry reference resolves its tag again on every run unless pinned to a digest, which is a second way a laptop run and a CI run differ.
Why the laptop passed
The laptop had a terminal. The same confirmation appeared and a person approved it, or the session had been given a mode. A session that never chooses a mode keeps the old default: read-only tools run and every other tool asks. In CI the ask has no answerer, the rejection goes back to the model as a tool result, the model does not change its plan, and the loop detector stops the run. session.db stores safety_policy per session, so you can read afterwards which mode each failed run had.
The fix, in order
--safety restricted, so unmatchedsafecalls run anddestructiveandunknowncalls fail closed instead of prompting.allowpatterns forrun_background_agentand for the tools the background sub-agent uses, because inside a running task a call that would normally ask is denied automatically.- Keep
denyfor anything destructive; no mode and no hook allow overrides a deny. - Pin external sub-agent references to a digest.
- Run the agent in a sandbox for isolation, because permissions are enforced by the client.
The regression check
Record a run with --record=PATH and --models-gateway, replay with --fake, and compare two sessions with sessions diff. The help's own example fails because the parser reads -1 as a flag; put -- before the references. A recorded pair of identical replays reports identical behaviour across all turns, and a run that called a different tool first diverges at turn 0 and exits 1 with --fail-on-divergence. Ask for one tool call per message when you record, because a turn with two parallel calls appends results in completion order and the replay answers with requested interaction not found.
Follow-ups
L3SeniorWhy does
transfer_tasknot need anallowwhilerun_background_agentdoes?Show expected answer
delegation is always approved by design, and a background start is a normal tool call that the mode and the patterns decide.
L3Senior
sessions diff -1 -2fails with an unknown shorthand flag. Why, and what do you type?Show expected answer
the parser reads
-1as a flag;sessions diff -- -1 -2.L4StaffThe writer sub-agent is pulled from a registry by tag rather than read from a file. What can change between the laptop and CI?
Show expected answer
the tag resolves again on every run, so pin a digest.
L4StaffWhy compare tool calls and not the assistant text?
Show expected answer
a live model words every answer differently, while the sequence of tool calls is the behaviour you shipped.
Wrong answers
Show the wrong answers that interviewers hear
“Use
--yoloin CI so nothing prompts.”That is
autonomous; destructive calls run too.restrictedfails closed on them.“Raise
max_consecutive_tool_calls.”It hides the loop; the rejection is the fault, and the detector did its job.
“Diff the transcripts.”
Prose varies between runs;
sessions diffcompares the tool-call sequence and stops at the first divergence.
Taught in
The sub-agents section records the rejected background call and the loop detector, the permissions section gives the mode table and the order, and the sessions section shows sessions diff and the replay rules.