Scenario
A flaky agent check that compared the wrong sessions
RolesAI Evaluation and ReliabilityAgent Systems Engineering
- Engine
- docker-agent v1.149.0 and sbx v0.47.0
- Model
- a recorded cassette of ai/qwen3:4b, replayed with no model
- Hardware
- one Mac, no GPU
Situation
A team keeps a small agent that lists the files of a repository and counts lines. Every change to its agent file goes through a check: run the agent, then compare the newest session with the one before it with sessions diff, and fail the job when the two differ. In one week the check gave three different results. Once it printed Error: unknown shorthand flag: '1' in -1. Once it passed. Once it failed with sessions diverged and a first divergence at turn 0, where one side called list_directory and the other side called shell.
The team reads the three results as proof that the agent is nondeterministic, and a pull request proposes to delete the check. You have the command lines, the outputs of the three comparisons, and the session store of the machine that ran them.
Questions
- Which of the three results says anything about the agent, and which say something about the command?
- Two runs from the same recording agreed with each other. What does a cassette fix, and what does it leave free?
- The divergence names
list_directoryagainstshellat turn 0. What are the two sessions, and how do you know? - What does the comparison compare, and why does it stop at the first difference?
- Rewrite the check so that a divergence can only come from a change to the agent.
Commit to a cause after each listing. Write it down before you read the next one.
Evidence
two runs of the files agent from the same cassette
Same flags, same cassette, same answer.
$ docker-agent run --exec --last --working-dir fixtures/repo --fake work/cassettes/18-files fixtures/agents/files.yaml 'List the files in the working directory.' 'How many lines does README.md have? Count them with a shell command.'
README.md has 1 line.
[exit 0]
$ docker-agent run --exec --last --working-dir fixtures/repo --fake work/cassettes/18-files fixtures/agents/files.yaml 'List the files in the working directory.' 'How many lines does README.md have? Count them with a shell command.'
README.md has 1 line.
[exit 0]Source: manuals/docker-sandboxes-101/capture/out/21-two-runs.txt, lines 1-7
the transcript that the cassette replays
Count the model replies: each one is a turn for the comparison.
user: List the files in the working directory.
root -> tool_call list_directory {"path": "."}
root <- tool_call_response List Directory: FILE README.md
root: The working directory contains 1 file: README.md (1 line).
stream_stopped (stop, normal)
user: How many lines does README.md have? Count them with a shell command.
tool_call_confirmation: {"agent_name": "root", "tool_call": {"id": "<call-id>", "type": "function", "function": {"name": "shell", "arguments": "{\"cmd\": \"wc -l README.md\"}"}}, "metadata": {"blast_radius": "safe", "category": "fs-read", "reason": "Command matches safe read-only pattern: wc -l <file>", "safety_label": "safe"}}
root <- tool_call_response Shell: The user rejected the tool call.
root -> tool_call read_file {"path": "README.md"}
root <- tool_call_response Read: hello
root: README.md has 1 line.
stream_stopped (stop, normal)Source: manuals/docker-sandboxes-101/capture/out/18-transcript.txt, lines 1-12
the first attempt, in the form the help text gives
The parser read the first reference as a flag.
$ docker-agent sessions diff -1 -2
Error: unknown shorthand flag: '1' in -1
[exit 1]Source: manuals/docker-sandboxes-101/capture/out/21-sessions-diff.txt, lines 1-3
the two replays compared
Five turns and no difference.
$ docker-agent sessions diff -- -1 -2
Comparing -1 (5 turns) against -2 (5 turns)
✅ Identical behaviour across all 5 turns.
[exit 0]Source: manuals/docker-sandboxes-101/capture/out/21-sessions-diff.txt, lines 5-9
the newest session against the third newest, with the CI flag
Read which tool each side called first, and the sentence before the error.
$ docker-agent sessions diff --fail-on-divergence -- -1 -3
Comparing -1 (5 turns) against -3 (6 turns)
❌ First divergence at turn 0 (after 0 matching turn(s)).
-1 called:
list_directory({"path": "."})
-3 called:
shell({"cmd": "echo m101-ok"})
Everything after this point is downstream of the divergence and is not compared.
Error: sessions diverged
[exit 1]Source: manuals/docker-sandboxes-101/capture/out/21-sessions-diff.txt, lines 11-22
the first turn of the restricted run of the guarded agent
This run came from another agent file, and its first tool call matches one side of the divergence.
user: Run the shell command: echo m101-ok
root -> tool_call shell {"cmd": "echo m101-ok"}
root <- tool_call_response Shell: m101-okSource: manuals/docker-sandboxes-101/capture/out/20-restricted.txt, lines 1-3
the sessions table and the five newest titles
Every exec run has the same title, so a title cannot tell runs apart.
$ sqlite3 work/data/session.db 'pragma table_info(sessions)' (12 rows)
id created_at tools_approved input_tokens output_tokens title cost send_user_message max_iterations working_dir starred permissions agent_model_overrides custom_models_used thinking parent_id instruction_context safety_policy attributes origin
$ sqlite3 work/data/session.db 'select id, title from sessions order by created_at desc limit 5'
<uuid> | Running agent
<uuid> | Running agent
<uuid> | Running agent
<uuid> | Running agent
<uuid> | Running agentSource: manuals/docker-sandboxes-101/capture/out/21-session-db.txt, lines 19-27
The answer
Show the walkthrough
- Read the two runs. Same flags, same working directory, same cassette, same two messages, and the same final answer both times. A cassette holds the recorded model replies, and
--fakereplays them with no model. The model is out of the loop, so the only free parts are the operands and the measure. - Count the turns. The transcript the cassette replays has five model replies: a
list_directorycall, an answer, ashellcall that the user rejected, aread_filecall, and an answer. Those five replies are the five turns the comparison reports. - Read the failed command. The help text gives the form with bare negative references, and the parser reads the first one as a flag. The error names the flag parser and nothing else. Put
--before the references, as the second command does. This result says nothing about the agent. - Read the pass. The two replays compared identical over all five turns, and the JSON form counts
turns_a5,turns_b5,turns_matched5. This is the result the check exists for. - Read the failure. The comparison is between the newest session, with 5 turns, and the third newest, with 6 turns. The newest called
list_directoryfirst. The third newest calledshellwithecho m101-okfirst. That first call matches the restricted run of a different agent file, the guarded agent, which answers a different user message. The reference-3did not point at a run of the files agent at all. - Read the session store. Relative references count every session in one
session.dbby creation time, and the table holds runs of every agent file plus the sub-sessions of the team runs. The five newest rows are all titledRunning agent, so a title cannot tell runs apart. Whatever ran last on this machine is-1. - Read what the comparison measures. It compares the sequence of tool calls and ignores the prose of the assistant. After the first difference every later turn is downstream, so it reports one divergence and stops.
Show the resolution
The agent did not change. The flaky result came from the operands: a relative reference into a shared session store that held runs of other agents, and a command form from the help text that the parser rejects. The one honest comparison, two replays of one cassette, matched on all five turns.
The repaired check fixes three things. It fixes the model, by recording one cassette with --record=PATH and --models-gateway and replaying both sides with --fake, one tool call per message, because a turn with two parallel calls answers 500 on replay. It fixes the operands, by giving the check its own data directory with --data-dir, so that its session.db holds only its own runs, and by writing -- before the references. It fixes the measure, by running sessions diff --fail-on-divergence and reading only the first divergence. The proof is the pass that already exists: with one cassette on both sides, the report counted 5 matched turns of 5, and the exit code was 0.
Show the wrong turns
“The agent is nondeterministic, so delete the check.”
Two replays from one cassette matched on all five turns. The comparison that failed compared two different agents.
“Quote the reference as
'-1'to fix the flag error.”The parser still sees a leading hyphen.
--ends flag parsing.“Compare the assistant text instead of the tool calls.”
Prose varies between live runs, and the comparison ignores it on purpose. The tool calls are the behaviour.
“Read the turns after turn 0 to find more bugs.”
Everything after the first divergence is downstream of it and is not compared.
Show the general rule
A regression check on an agent needs three fixed things: the model replies, the operands, and the measure. A cassette fixes the first. A private session store and explicit references fix the second. A comparison of tool calls that stops at the first difference fixes the third. When a check flips between pass and fail, find out what it compared before you blame what it measured.
Taught in
The sessions section shows the help form, the working form and this divergence.