fix(agent): warn a new failure streak after a success
What
The agent loop warns "the last 3 actions failed in a row" for every streak of failures in a step, including one that starts after a success.
Why
The loop skipped a failure-streak warning whose reason matched the last one sent, and the reason carries only the streak's length. After a first streak was warned, a new streak of three got no notice until a fourth failure, or none if the model concluded first.
How it works
checkFailureStreakreturns akeyon its warning,failures@<index of the streak's first result>(packages/core/src/agent/loop-guards.ts:358). That is howcheckLoopGuardskeys a wait since #1993.- The loop dedups the streak warning on key and reason together (
packages/core/src/agent/tool-loop.ts:873). A new streak has a new key, so it is warned. A streak that keeps going still gets "the last 4" before the stop at five, as on main. Keying by start alone would drop that warning at four, so the reason stays in the check. - Not changed: the keyless repeat and cycle warnings are deduplicated the same way (
tool-loop.ts:845), and their reason names the tool but not its input: threepoke {target: 'a'}calls, apeek, then threepoke {target: 'b'}calls warn only the first run. Fixing that changescheckLoopGuards, so I left it for a follow-up.
Issue
Fixes #2724
Verified
Ran it locally: yes
-
agent-grammar-tools.test.ts"warns a streak again as it grows, then stops it":boomfails every turn, and the notices name streaks of 3, 4 and 5 before turns 4, 5 and 6. Deduping on the key alone drops the turn-5 notice and fails it. -
A scratch test on the fake-model harness, against a main build of
@e2e-dev/coreand this branch: a project toolboomthat throws, with a different input each turn so only the streak guard applies, run withcreateToolLoopExecutor. Notices the loop added before each turn:
| script | main | this PR |
|---|---|---|
boom 1-3, peek 4, boom 5-7, conclude 8 | turn 4: last 3 | turn 4: last 3, turn 8: last 3 |
boom every turn | turn 4: last 3, turn 5: last 4, turn 6: stop at 5 | turn 4: last 3, turn 5: last 4, turn 6: stop at 5 |
- The new test,
the failure-streak warninginpackages/e2e/tests/unit/agent-grammar-tools.test.ts:544, fails on main (expected [ 4 ] to deeply equal [ 4, 8 ]) and passes here. - No
pnpm bench:abtable: I have no model key, and the web benchmark's committed recordings replay without entering the tool loop.
Checklist
- Changeset added (
pnpm changeset) for any change to a published package, or the change is testbed, docs, or CI only. - Breaking change: title carries
!, the changeset body names what breaks and what replaces it, and the deprecation warning landed a window earlier (stability policy in CONTRIBUTING.md). - Docs updated in the same PR: the
docs/**/*.mdxpage for the behavior, andskills/e2e/if the skill describes it. (docs/agent-steps.mdxalready says the loop warns after 3 consecutive failed actions; no change.) - Contract change: emitted
.d.tsreviewed,tests/types/sdk-types.tsupdated; wire change edits the schema and both fixtures. - Engine contract change (
e2e/engine): changesets fore2e,@e2e-dev/web, and@e2e-dev/mobile.
Summary by CodeRabbit
- Bug Fixes
- Failure-streak warnings now appear when a run of failed actions reaches three, and again as that streak grows—even when the warning reason stays the same.
- A successful action resets the streak, allowing a later run of three consecutive failures to trigger its own warning.
- The loop guard’s final prompt continues to reflect the current failure-streak length.