fix(agent): warn a new failure streak after a success

AA
2h ago
+72-5

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

  1. checkFailureStreak returns a key on its warning, failures@<index of the streak's first result> (packages/core/src/agent/loop-guards.ts:358). That is how checkLoopGuards keys a wait since #1993.
  2. 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.
  3. 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: three poke {target: 'a'} calls, a peek, then three poke {target: 'b'} calls warn only the first run. Fixing that changes checkLoopGuards, 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": boom fails 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/core and this branch: a project tool boom that throws, with a different input each turn so only the streak guard applies, run with createToolLoopExecutor. Notices the loop added before each turn:

scriptmainthis PR
boom 1-3, peek 4, boom 5-7, conclude 8turn 4: last 3turn 4: last 3, turn 8: last 3
boom every turnturn 4: last 3, turn 5: last 4, turn 6: stop at 5turn 4: last 3, turn 5: last 4, turn 6: stop at 5
  • The new test, the failure-streak warning in packages/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:ab table: 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/**/*.mdx page for the behavior, and skills/e2e/ if the skill describes it. (docs/agent-steps.mdx already says the loop warns after 3 consecutive failed actions; no change.)
  • Contract change: emitted .d.ts reviewed, tests/types/sdk-types.ts updated; wire change edits the schema and both fixtures.
  • Engine contract change (e2e/engine): changesets for e2e, @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.