Skip to content

fix(agent): count shell failure streaks per program, not per tool - #4527

Merged
kovtcharov-amd merged 1 commit into
amd:mainfrom
kovtcharov:kalin/turn-scope-per-program
Oct 1, 2026
Merged

kovtcharov-amd merged 1 commit into
amd:mainfrom
kovtcharov:kalin/turn-scope-per-program

Conversation

@kovtcharov

@kovtcharov kovtcharov commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

The turn guard ends a turn after the same tool is refused four times in a row. It counted every shell command as one tool, so an agent that tried python, then ls, then pip and was refused each time looked like it was stuck in a loop, and its turn ended. On a SWE-bench pylint task this ended the run with no patch. Streaks are now counted per program run through the shell (a leading cd <dir> && is skipped), so a real loop on one program still ends the turn and trying different programs doesn't.

The data is a single task. The same pylint task (pylint-4970, DeepSeek V4.1 Flash) ended with no patch in all 3 runs made with the guard (#4480) before this change, and was resolved in the run after it.

Test plan

  • python -m pytest tests/unit/agents/test_turn_scope.py

@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve with suggestions

The agent was ending its turn when it tried several different shell programs and each was refused, because every shell command counted toward one shared "same tool keeps failing" streak. Streaks are now counted per program, so an agent exploring python → ls → pip keeps going, and one that keeps hammering python is still stopped. The change is small and well tested. There are no blocking issues, only two optional polish items:

  • The refusal message now names the tool as "run_shell_command (python)", followed by "Do not call it again". A small model could read that as "stop using the shell entirely", which is the over-stop this PR is fixing. Rewording it for the per-program case would help.
  • Commands starting with an env-var assignment (FOO=1 python …) or a quoted path containing spaces are keyed on the wrong word. This is harmless today, but those programs can dodge or share a streak.

Real-world evidence

N/A: no evidence bundle for this run. This is an internal agent-loop guard change with no CLI, API, or UI surface of its own, so the verdict rests on static review. The PR's SWE-bench result (pylint-4970: 0/3 before, resolved after) is a single task, as the author notes. I couldn't run the unit tests here because the runner lacks the dev dependencies.

🔍 Technical details

Issues

🟢 Correction text reads as a whole-shell ban (turn_scope.py:231). FAILURE_STREAK_CORRECTION is now formatted with tool=key, so the model sees "run_shell_command (python) has failed 4 times … Do not call it again". Consider phrasing the shell case as "Do not run python again". One option is a second template used when key != tool, so the model knows the rest of the shell is still open.

🟢 Program extraction edge cases (turn_scope.py:_streak_key):

  • FOO=1 python x gives the key (foo=1). sudo, env, and timeout wrappers become the key instead of the program, so a looping model can rotate wrappers to reset its streak.
  • "C:\Program Files\Python\python.exe" -c x gets split on whitespace before the quotes are stripped, so the key becomes (program), and every quoted-space path shares one streak.

Skipping leading NAME=value tokens is cheap and worth doing. Consider shlex-style tokenisation for the first word, or at least matching a quoted leading token as a whole, like _CD_PREFIX already does.

🟢 Keying applies to any tool with a string command arg, not only the shell tool. That looks harmless (a non-shell tool just gets a finer-grained key), but it's worth a one-line note in the docstring, or a gate on the shell tool names, so a future tool with a command field isn't surprised.

Strengths

  • end_key is threaded through to _end_turn_on_scope, so the loop-break summary reports the streak that actually tripped rather than always showing 0 for the bare tool name.
  • The tests cover both directions: distinct programs (including cd /d and a piped command) don't trip the guard, and one program with changing arguments still does. The test also checks that python.exe via a full Windows path joins the python streak and that git isn't blocked by python's streak.
  • Success and mutation clearing (failures.pop(key) / failures.clear() on mutating tools) keep their existing semantics.

The turn-scope guard ends a turn when one tool keeps failing with new
arguments. A shell tool runs many programs, so 'python -c' refused, then
ls denied outside the sandbox, then pip refused counted as one tool
failing four times, and a SWE-bench task was ended before any edit was
made. A shell line's streak is now keyed on the program it runs; a
single program refused with new arguments still stops as before.
@kovtcharov
kovtcharov force-pushed the kalin/turn-scope-per-program branch from 57a1615 to ff8ed0b Compare October 1, 2026 09:00
@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Oct 1, 2026
Merged via the queue into amd:main with commit 8ae15a6 Oct 1, 2026
40 of 43 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the kalin/turn-scope-per-program branch October 1, 2026 16:22
kovtcharov-amd pushed a commit to kovtcharov/gaia that referenced this pull request Oct 1, 2026
…md#4536)

A failed tool call is shown once and then scrolls up the conversation,
so the agent walked into the same wall again several steps later: 22% of
failed calls in 32 SWE-bench task runs repeated a failure from earlier
in the same task. Now each failure is recorded as a lesson: what failed
and why, plus what later worked for that program. The full list rides on
every later failed result, so the agent sees it exactly when it is
exploring. Lessons come only from the harness's record of what ran,
never from content a tool returned, and they last for the turn.

| 6 hard SWE-bench tasks, DeepSeek V4.1 Flash | without (2 runs) | with
|
|---|---|---|
| Repeated failures / failed calls | 6/22, 5/23 | **1/13** |
| Steps | 215, 233 | 201 |
| Solved | 2/6, 3/6 | 3/6 |
| Cost | $0.52, $0.68 | $0.55 |

It cuts the waste it targets without adding prompt text or model calls.
It doesn't by itself solve more tasks.

Depends on amd#4527, which adds the per-program key this uses. The first
commit here is amd#4527's; review the second.

## Test plan
- [ ] `python -m pytest tests/unit/agents/test_task_lessons.py
tests/unit/agents/test_turn_scope.py`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants