fix(agent): a forced complete_step turn answered with another tool asks again, not MODEL_PROVIDER_FAILED

Merged
OK
2mo ago
+170-6

On the last two turns of a step (and after a loop guard or a low clock), the loop offers only complete_step and forces it with toolChoice: { type: 'tool', toolName: 'complete_step' }. A model that still calls another tool, usually one it saw earlier in the history, makes the AI SDK throw AI_ToolChoiceViolationError before anything runs. On main that went through the generic catch and failed the step as MODEL_PROVIDER_FAILED: an infrastructure code with retries, for model behavior.

#739 already handles the SDK throwing this error for a prose reply (finishReason: 'stop', no tool call). That case means the provider downgraded the forced choice, so the loop switches to auto. A reply that calls the wrong tool is a different case: the provider answered with a tool call, and the model broke the protocol. So it is handled the way the SDK handles a call to a tool the turn does not offer:

  • the turn counts as a model call and a turn, like the prose case (recordThrownTurn, now shared by both)
  • the reply goes into the history as the SDK's own history would keep it, with text, reasoning, and tool calls plus their provider metadata (thought signatures). Each call gets an error-text result: peek did not run: only complete_step is available now. Call complete_step with your verdict.
  • the model is asked again on the same turn budget, still forced
  • once the turns run out, the step fails with STEP_NO_CONCLUSION, like any step that never concluded
flowchart LR
  F[forced complete_step turn] --> V{AI_ToolChoiceViolationError}
  V -- prose, finishReason stop --> A[provider downgraded: retry with auto]
  V -- other tool calls --> E[error results, ask again]
  E -- turns left --> F
  E -- out of turns --> N[STEP_NO_CONCLUSION]

Code: packages/e2e/src/agent/tool-loop.ts correctForcedTurn, packages/e2e/src/agent/model/tool-choice.ts isForcedToolCallMismatched.

Verified

Ran it locally: yes

  • A scratch project in /tmp with a stub engine and a scripted V4 model, maxModelCalls: 3. The model calls the project tool peek on turn 1, and calls peek again on the first forced complete_step turn. Ran the built CLI, e2e run tests/wrong-tool.e2e.ts, against main's dist and then this branch's.
mainthis branch
MODEL_PROVIDER_FAILED: the model provider failed: Model response did not contain a call to the required tool 'complete_step'. (1 model call)✓ agent.act "look around, then conclude" · 3 model calls
  • Same project with a model that never concludes (NEVER_CONCLUDE=1). The branch fails with STEP_NO_CONCLUSION: agent.act failed: the agent used 3 of 3 turn(s) without calling complete_step. The --debug transcript:
--- turn 2 ---
tool call: peek({})
[peek] error: peek did not run: only complete_step is available now. Call complete_step with your verdict.
[loop] turn 2 called other tools in place of complete_step: asking again
--- turn 3 ---
tool call: peek({})
[peek] error: peek did not run: only complete_step is available now. Call complete_step with your verdict.
  • Two new tests in tool-loop-tool-choice.test.ts fail on main with MODEL_PROVIDER_FAILED. Mutation checks: dropping the out-of-turns break, the error result text, or the provider metadata on the call each fails a test.