An agent on Windows runs echo "home=$HOME" and gets back the literal string "home=$HOME". The model wasn't careless. Its system prompt said the shell was bash, so it wrote bash. The tool ran the command through cmd.exe.
I fixed two bugs like this in open source agent frameworks this month. They live in different projects and different languages, but they have the same cause: what the model was told had drifted from what the code did. This post walks through both, then turns them into a checklist you can run against your own agent.
The model only knows what you put in the context
An LLM can't look at your runtime. It can't check which shell is installed or what a hook did to its arguments. Everything it believes about the world comes from three places: the system prompt, the message history, and the tool results you feed back.
Those get built in two different ways. Some are facts you state up front, like the OS, the shell, the working directory and the tools. The rest is a record of what happened: which tools were called, with what, and what came back. Each of the bugs below breaks one of these.
Bug 1: the prompt describes a shell the tool never uses
nanocoder is a terminal coding agent. Its system prompt includes a line like Default Shell: /bin/zsh, built by a small function in prompt-builder.ts. Its execute_bash tool, on Windows, always spawns ComSpec, which is cmd.exe.
The prompt builder checked things in this order:
const getDefaultShell = (): string => {
if (process.env.SHELL) return process.env.SHELL;
if (platform() === 'win32') return process.env.COMSPEC || 'cmd.exe';
if (platform() === 'darwin') return '/bin/zsh';
return '/bin/bash';
};
That looks reasonable until you start the agent from Git Bash or MSYS2. Both set SHELL to a bash path, so the first line wins and the prompt says C:\Program Files\Git\usr\bin\bash.exe. The executor never reads SHELL. It runs cmd.exe anyway.
So the model writes $HOME, single quotes, && chains with export, /dev/null. cmd.exe doesn't expand $HOME, has no export, and treats single quotes as ordinary characters. Commands fail or quietly do the wrong thing, and the model has no reason to suspect its own assumption, because the prompt told it so.
The fix is to check the platform first:
const getDefaultShell = (): string => {
+ // execute_bash runs commands through ComSpec on Windows, even when SHELL
+ // is set by Git Bash or MSYS2.
+ if (platform() === 'win32') return process.env.ComSpec || 'cmd.exe';
if (process.env.SHELL) return process.env.SHELL;
- if (platform() === 'win32') return process.env.COMSPEC || 'cmd.exe';
if (platform() === 'darwin') return '/bin/zsh';
return '/bin/bash';
};
The diff is small, but the real problem is bigger than the order of two ifs. "Which shell do we use" was answered in two places, once in the prompt builder and once in the executor, and each answered it its own way. Two sources of truth for one fact will drift sooner or later. The more robust version is for the prompt to ask the executor, or for both to read the same constant.
Bug 2: the history records arguments the tool never ran with
crewAI lets you register before_tool_call hooks that can change a tool's input before it runs. That's useful for clamping values, filling defaults, or adapting the input to a system's rules.
The issue that reported this had a nice example. A meeting assistant books a room through a reserve_room tool. The company calendar works in 15-minute blocks, so a hook rounds duration_minutes up. The user asks for 35 minutes, the hook makes it 45, and the tool saves a 45-minute booking. Then the conversation continues: the reporter passes result.messages into a second kickoff() and asks how long the booking is. The agent answers 35.
Here's why. The executor builds the assistant message, the one with tool_calls in it, from the model's output before any tool runs. Hooks then change tool_input in place. The history keeps the model's original arguments, so the next turn reasons from a call that never happened.
What makes this one sneaky is that nothing looks wrong inside a single run. The tool gets the right input, the booking is correct. It only shows up when the history is replayed, which is exactly what you do in a chat app or any multi-turn agent.
The fix records the effective arguments back into the reported call after each tool runs:
def _record_effective_tool_args(
report: dict[str, Any] | None, tool_args: dict[str, Any] | None
) -> None:
if report is None or tool_args is None:
return
reported = report["function"]["arguments"]
try:
unchanged = json.loads(reported) == tool_args
except (json.JSONDecodeError, TypeError):
unchanged = False
if not unchanged:
report["function"]["arguments"] = json.dumps(tool_args)
It's called in both the sequential and the parallel execution branches. It only rewrites the arguments when a hook actually changed them. Calls that no hook touched keep the model's exact original string, so the history doesn't fill up with reformatted JSON that means the same thing.
It isn't complete, and I said so in the PR. A separate executor used for planned steps builds its assistant message the same way, and Gemini keeps its own copy of the raw tool call parts. I left both out to keep the change small enough to review.
The pattern: told vs. did
Put next to each other, the two bugs are the same shape. Here are the places a model's beliefs come from, and how each one can go wrong:
| Where the model's belief comes from | What makes it wrong | Example |
|---|---|---|
| Environment facts in the system prompt (OS, shell, cwd, date) | Computed separately from the code that acts | Bug 1 |
| Tool call record in the history | Hooks or middleware change the input after it's recorded | Bug 2 |
| Tool results | Truncation or summarizing that hides the error | |
| Tool descriptions and schemas | Schema says optional, code requires it |
Two rules cover most of it. Record what ran, not what was requested. And describe the environment using the same code that acts on it.
How to test for it
Both fixes are tested the same way: run the real code path, then assert on what the model will see. Not on the tool, not on internal state, but on the prompt or the history.
For nanocoder, the test sets the platform to win32 and SHELL to a bash path, builds the system prompt, and checks that it names ComSpec. For crewAI, a scripted LLM makes one or two tool calls, a hook rewrites only the first, and the test reads the history that comes back:
@pytest.mark.parametrize("batch_size", [1, 2], ids=["serial", "parallel"])
def test_history_reports_arguments_after_hooks(batch_size):
@on(InterceptionPoint.PRE_TOOL_CALL)
def rewrite(context):
if context.tool_input.get("term") == "0":
context.tool_input["term"] = "effective"
agent = Agent(role="Tester", goal="Look up values", backstory="Test hooks",
llm=ScriptedLLM(batch_size), tools=[LookupTool()])
result = agent.kickoff("Look up values")
reported = [
call["function"]["arguments"]
for message in result.messages
if message.get("tool_calls")
for call in message["tool_calls"]
]
expected = [json.dumps({"term": "effective"})]
expected += [json.dumps({"term": str(i)}) for i in range(1, batch_size)]
assert reported == expected
The parallel case matters. It checks that the rewritten call gets the new arguments and that the untouched one keeps its original string. With the fix reverted, both cases fail, and that's how I know the test is checking the bug and not just the happy path.
A checklist for your own agent
- Open your system prompt builder. For every fact about the environment, ask whether it's computed by the same code the tools use. If it isn't, that's a drift waiting to happen.
- List every hook, middleware or wrapper that can change tool input. Is the history written before or after them?
- Do a replay test. Save
messagesfrom one run, start a new run with them, and check that the model's next step uses the effective state. - Run it on Windows once, from Git Bash. A POSIX-looking environment on top of a Windows executor is the classic trap.
- When a tool fails, look at what the model actually receives. Is it the real error text, or a summary that hides it?
The context is the model's only window into your system. When that window is wrong, the model is wrong in a confident and consistent way, and it usually looks like a model problem when it's a plumbing problem.
Have you hit a "told vs. did" bug in your own agent? I'm curious which kind shows up most.
Top comments (34)
The crewAI one is the sneakier bug — nothing looks wrong inside a single run, the booking is correct, and the lie only surfaces when the history gets replayed in a second kickoff. That "told vs. did" framing is the right way to think about it: the model's beliefs are only as good as the record, and the record is built before the tools run. Do you think frameworks should record effective args by default, or is that too much machinery for a corner case?
By default, I think. In crewAI the fix is one JSON compare per call, and the history is the model's only memory, so anyone who replays it hits this. The open question for me is how to show it. Silently swapping 35 for 45 makes it look like the model asked for 45, which can mislead the next turn in a different way. Recording the effective args and also saying in the tool result that a hook adjusted them (35 → 45, calendar blocks) is probably the more honest version.
That's the right split — recording the effective args fixes the replay, but the annotation in the tool result is what keeps the model's next turn honest, since it can see the adjustment happened instead of treating 45 as its own choice. The question I'd add is whether that annotation belongs in the result the model sees, or in a separate audit channel the model never reads — one fixes the reasoning, the other just the record. Which would you pick for crewAI?
In the result the model sees. An audit channel helps whoever reads the logs later, but the bug here is the model's next turn, so the note has to be where the model reads. Something short like
duration_minutes adjusted 35 -> 45 by hookadded to the tool output. I'd make it opt-in at first, because some hooks change input for reasons the model shouldn't see, like redacting a secret.That's the right placement — the audit channel fixes the log reader's view, but the model only ever sees the tool result, so the correction has to live there. The opt-in point is fair too, especially the redaction case: a hook that strips a secret shouldn't have to announce what it stripped. Would you put the note in the result unconditionally and let the hook decide the wording, or keep the whole thing behind a flag?
The replay test in your checklist is the one I'd stress most — it's the only check that catches drift you didn't think to look for. The crewAI bug is a great example: nothing looks wrong inside a single run, the agent just quietly reasons from arguments that were never actually used. And the Windows/Git Bash case in bug 1 is painfully relatable — a POSIX-looking environment on top of a Windows executor is exactly where these mismatches hide.
Agreed. It's also cheap to automate: snapshot the messages from a scripted run and diff them in CI, so any change to what the model sees shows up in review.
Same drift shows up at the permission layer too. mcp-server-kubernetes put kubectl_get on its read-only list, but the tool passed caller flags through, so a -s flag could point kubectl at a different server. The model was told read-only and the code did something else.
Good example, and it fills the row in my table that had no example. The same project had an even more literal one: GHSA-cr22-wjx7-2w6m, where
ALLOW_ONLY_READONLY_TOOLSfilteredtools/listbuttools/callstill ran anything by name. The difference from the two bugs in the post is who gets hurt: there the model reasons from a wrong picture, here the operator does, and the gap is exploitable. Enforcement has to live where the call executes, not where the tool is described.That one is worse in a way, because the filtered list looks like the control working. Anyone who checked by listing the tools would have signed off on it.
Right, and that's the uncomfortable part: the obvious way to check the control is the one check it passes. A test that calls a hidden tool by name would have caught it in one line.
The second bug is particularly important because it exposes a subtle failure in multi-turn agents: the tool can execute correctly while the conversation history still describes a different action. That means even a perfectly functioning tool can leave the agent reasoning from stale state.
I'd extend the testing strategy with one more invariant: every recorded tool call should reflect the effective arguments, execution status, and actual result not just the model's original request. This becomes even more important with parallel tool calls, middleware transformations, and retries.
At IT Path Solutions, this is the kind of detail that matters when moving agentic workflows into production. Reliable agents need consistency between declared capabilities, actual execution, and the state they carry into the next turn. Otherwise, what looks like a reasoning failure may actually be a runtime observability problem.
Retries are a good extension. If a call fails and the agent retries with different arguments, the history should make clear which attempt produced the result the model is reading, otherwise you get the same drift one level down.
"What the model was told had drifted from what the code did" is a change order that never made it back onto the drawings. On a job, the field swaps a beam size for a good reason, the building is right, and the next crew frames off the old sheet. Nobody was careless. The record was. The second bug is the one I'd worry about more, because nothing looks wrong inside a single run. The booking is correct and the history says something else happened. Writing the effective arguments back after the hook runs is the right fix: the log should say what was done, not what was asked for. I direct agents as a Language Modeler, and the shell line is a good reminder to check what the prompt claims about the machine against what the tool actually spawns.
The change order comparison fits well, especially "nobody was careless". Both bugs came from code that made sense on its own.
one i'd add is a server quietly capping the page size, the model asks for a lot more rows than it gets and then talks like it saw everything. echoing the limit that was really applied in the result seems like a cheap fix
Good one, and it fits the tool results row in the table. Returning the limit that was applied plus a
has_moreor cursor is cheap, and it lets the model say "first 100 of 2,300" instead of treating 100 rows as everything.i'd put that line above the rows too, a note sitting after a long list is easy for the model to skim past
The same shape shows up in agent memory: the model says "saved that", and the transcript carries a write that never happened, or happened with other content. It's your bug 2 one level up, the record describes the call the model meant. What helped me was a receipt the model can't produce itself: the write tool returns one, and before the reply goes out, every "saved" in it needs a matching receipt. Without one, the claim gets corrected before the user reads it.
The tools/list example further down hit close to home. Today I filtered the tool list on my hosted server, and the one thing I checked twice was that the list filter and the call check read the same function. A list that hides a tool while the call path still runs it is your two-sources-of-truth problem with a security label on it.
The receipt idea is nice because it changes the question from "did the model remember correctly" to "is there a record the model couldn't have written itself". And yes, the list filter and the call check reading the same function is exactly the fix for the tools/list case.
Right, and it's honest about its limit. The receipt is a short hash over topic, time and content, so the model can't invent one that fits. But it only proves that a write happened, not that what was written is true. And a claim phrased in unusual words still slips past the check. It catches the lazy "saved that", which is the common case.
The checklist is the right shape, but I would be careful about what it actually fixes. A checklist is a static audit, and the bug you describe is dynamic: it appears the moment someone changes one side and not the other. The next refactor after the audit puts you back here.
The durable version is to stop having two computations of the same fact. In your example the real defect is not that the prompt builder checks platform in the wrong order, it is that there are two functions that each decide what the shell is and nothing forces them to agree. Fixing the order in both leaves that structure in place. One function, called by both the prompt builder and the executor, removes the class of bug instead of this instance of it.
There is also a runtime version that catches the drift you cannot predict. Have the tool report the environment it actually used in its result, not just stdout. If every execute_bash result starts with the shell it really ran in, then the model is reading the truth rather than the prompt's claim about it, and it can correct itself on the next turn. That is the difference between a prompt that is right today and a loop that notices when it stops being right.
And the cheap CI version, which I would put in the post as the one-line takeaway: a test that asserts the value in the rendered system prompt equals the value the executor would use. Three lines, fails on the refactor that reintroduces this, and it does not depend on anyone remembering to re-read the checklist.
The general form of all three: drift between what the model is told and what the code does is a two-sources-of-truth problem, and two sources of truth are only safe when something checks they agree.
Agreed. The post makes the same point about two sources of truth, but the PR itself only fixed the order, to keep it small for review. Your CI test is better than mine. Mine asserts the prompt names ComSpec, which still encodes my own belief about the executor. Asserting that the prompt equals what the executor would actually use is the version that fails on the next refactor. I might send that as a follow-up.
The test strategy is the part I'd steal — asserting on what the model will see, not on internal state. Most agent tests I've seen check the tool ran; almost none check what the prompt or the history actually said afterwards. The Windows/Git Bash one hit home too — a POSIX-looking shell on top of a Windows executor is exactly the kind of setup that looks fine until a $HOME comes back literal. Does the replay test catch new drift on its own, or does it mostly just guard the two bugs you already fixed?
Mostly it guards. A replay test only catches drift on the paths it runs, so on its own it didn't find anything new. What helps is snapshotting the messages and diffing them in CI: then a new hook or middleware that changes the record shows up in review, even if nobody wrote a test for it.
This matches what we see in local assistants: the model's "memory" is just context, so if a retrieved doc or tool result changes, the belief has to change with it. Our rule for Atom Zero is boring but effective: every factual claim must carry a source and page/section, and a stale citation is a test failure, not a style issue. I'd add one item to the checklist: pin the runtime facts (OS, shell, cwd) in the same place you pin the model version, and re-assert them after any tool that can mutate the environment.