Skip to content

fix(agent): refuse tool calls outside the dispatched toolChoice - #171

Open
alexb-openrouter wants to merge 1 commit into
mainfrom
devin/1796053191-enforce-tool-choice
Open

alexb-openrouter wants to merge 1 commit into
mainfrom
devin/1796053191-enforce-tool-choice

Conversation

@alexb-openrouter

@alexb-openrouter alexb-openrouter commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

toolChoice (including a set narrowed through nextTurnParams) is only sent to the server as a request. If the server or model ignores it and returns a call to a withheld tool, the engine ran that tool anyway. This PR makes the engine check each returned call against the toolChoice it actually dispatched for that turn.

  • beginToolChoiceDispatch now records the wire toolChoice in dispatchedToolChoice. This covers caller config, forced-choice relaxation, nextTurnParams, and doom-loop advisor overrides.
  • functionToolsPermittedByToolChoice maps it to the function tools that are allowed:
    • undefined / 'auto' / 'required' → no restriction
    • 'none' → none
    • { type: 'function', name } → {name}
    • allowed_tools → its function entries
    • other forced choices (server, MCP, shell, …) → no function tools
  • handleApprovalCheck calls the new denyCallsOutsideToolChoice before approval classification. Each out-of-set call:
    • is recorded in hookDeniedCalls, so executeSingleToolCall returns the same { error } output a PermissionRequest deny produces
    • never reaches requireApproval, PermissionRequest, PreToolUse, doom-loop declaration, or execute
    • is added to unsentToolResults as a rejected result when a permitted sibling pauses the run for approval
  • Out of scope:
    • Manual tools (no execute/onToolCalled) are never run by the engine, so they are still returned to the caller.
    • Calls to unknown tools keep their current handling.
    • Final and empty-output retry turns ('none') do not execute calls, so they are unchanged.

Tests

tests/unit/tool-choice-enforcement.test.ts covers:

  • allowed_tools, forced function, and 'none'
  • a set narrowed by nextTurnParams on the follow-up turn
  • no approval or PermissionRequest for a withheld tool
  • the approval-pause unsentToolResults path
  • 'auto' still running every tool

Six of the seven fail on main. The full unit suite passes (1299 tests), along with lint and typecheck.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

devin-ai-integration Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access that start with 'Devin'.

  • Disable automatic comment, CI, and merge conflict monitoring

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant