fix(cli_driver): place extra_args before prompt positional

Bugbot flagged that `run_claude` placed the prompt as cmd[7] and then
appended extra_args after it. `claude --print` takes the prompt as the
final positional argument; flags appearing after it (e.g.
`--allowed-tools Bash`, `--image <path>`) are swallowed by the prompt
parser, which silently breaks the tool_use and vision cells.

Build the flag list first, then append the prompt last. Add a unit
test that pins the ordering.

Co-authored-by: Mateo Wang <mateo-berri@users.noreply.github.com>
This commit is contained in:
Cursor Agent 2026-05-06 03:40:16 +00:00 committed by mateo-berri
parent 2073f85eaf
commit d982aebe68
2 changed files with 29 additions and 1 deletions

View file

@ -63,6 +63,29 @@ def test_run_claude_assembles_command_correctly():
assert cmd[-1] == "hello"
def test_run_claude_places_extra_args_before_prompt():
"""`claude --print` expects the prompt as the final positional arg.
Flags appearing after the prompt are ignored or eaten by the prompt
parser, which silently broke the tool_use and vision cells before the
fix. Pin the ordering: every flag (including caller-supplied
`extra_args`) must precede the prompt.
"""
runner, captured = _make_runner(stdout="")
run_claude(
prompt="say hi",
model="claude-haiku-4-5",
base_url="http://localhost:4000",
api_key="sk-test",
extra_args=["--allowed-tools", "Bash"],
runner=runner,
)
cmd = captured["cmd"]
assert cmd[-1] == "say hi"
prompt_idx = cmd.index("say hi")
assert cmd[prompt_idx - 2 : prompt_idx] == ["--allowed-tools", "Bash"]
def test_run_claude_overlays_proxy_env():
runner, captured = _make_runner(stdout="")
run_claude(

View file

@ -78,6 +78,11 @@ def run_claude(
if not api_key:
raise ValueError("api_key must be a non-empty string")
# `claude --print` takes the prompt as the **last positional argument**.
# Flags must come before it, otherwise they're parsed as part of the
# prompt (or silently dropped, depending on the CLI version) and the
# tool_use / vision cells fail with confusing "no tool_use observed"
# errors. Build the flag list first, then append the prompt last.
cmd: List[str] = [
cli_path,
"--print",
@ -86,10 +91,10 @@ def run_claude(
"--verbose",
"--model",
model,
prompt,
]
if extra_args:
cmd.extend(extra_args)
cmd.append(prompt)
env = {**os.environ, **(extra_env or {})}
env["ANTHROPIC_BASE_URL"] = base_url