Skip to content

[AI-4207] Answer a call for a tool the model was not offered - #2714

Open
kanat wants to merge 8 commits into
mainfrom
kanat/ai-4207-unknown-tool-ends-turn
Open

kanat wants to merge 8 commits into
mainfrom
kanat/ai-4207-unknown-tool-ends-turn

Conversation

@kanat

@kanat kanat commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

When the model called a tool the session does not offer, for example one the router hides, the agent dropped the call but still recorded the reply with that call on it. Nothing ever answered the call, so no follow-up request went out and the turn stayed open, which shows up as a command stuck in "thinking". Now the agent answers each such call with a tool error that names only the tool the model asked for, so the model is asked again and the turn ends with a reply.

How it works

model reply: tool_calls [refund_order (not offered), lookup_order]
│
agent.finish                                  (agent.go:3903 collects, :3984-3985 starts)
├─ lookup_order   offered      -> harness.Requested -> executeTool -> runs, result
└─ refund_order   not offered  -> startTool(call, notOffered(name)) -> executeTool
                                  -> error result, the tool is never run
│
both results in history -> one follow-up request -> the model answers -> turn ends

The error the model reads is: That did not work: refund_order is not one of your tools. Use only the tools you were given. Tell the caller, in your own words. It carries no description of the hidden tool and no reason it is hidden.

Each refused call keeps its own answer. finish hands the refusal to startTool as a value, so nothing is looked up by call id. executeTool also looks the tool up when a call arrives without one (telephony.go:680-688), and refuses it when it is not offered, so a call with no tool is never run.

Why

On d734ca0bb, Harness.Requested skipped a call for an unknown tool (internal/harness/harness.go:773), while agent.finish still recorded the call and reported the reply as having work pending. A follow-up request is only sent once every call has a result, so it never went out. A voice-to-voice session already refused such a call (internal/agent/native.go:214-218); a cascade now does the same, with the same message.

Change Result Test
A call for a tool the reply was not offered gets an error result The model is asked again and the turn ends TestACallForAHiddenToolIsAnsweredAndTheTurnEnds (also checks the call's events carry the turn's TurnID), TestASpokenCallForAToolNeverOfferedIsAnsweredAndSaid
Known calls in the same reply still run One follow-up with both results TestAReplyCallingAKnownAndAnUnknownToolRunsTheKnownOne
Calls that share an id each get their own answer Both refused; a known call sharing the id of a refused one still runs TestTwoHiddenCallsSharingAnIDAreBothRefused, TestAKnownAndAHiddenCallSharingAnIDRunOnlyTheKnownOne, TestTwoNativeCallsSharingAnIDForAToolNotOfferedNeverRun
A call that arrives without a tool is looked up Refused when not offered TestACallThatCarriesNoToolIsRefusedWhenItIsNotOffered
A tool loaded in the same reply is still not offered to that reply Refused, even once the load has finished TestACallForAToolLoadedInTheSameReplyIsRefused, TestACallForAToolLoadedWhileItWaitsIsRefused (the call's start waits until the tool is loaded, so the check does not depend on timing)
A refused press or transfer also gets a follow-up Those tools normally wait for the phone menu, which a refused call never reaches TestASpokenCallToPressThatWasNeverOfferedIsStillAnswered, TestAWrittenReplyThatPressesIsAnswered (written conversation)
A call for an offered tool Unchanged from main TestACallForAnOfferedToolStillRunsAndIsAnswered
Before After
Model calls a hidden tool No further request, turn never ends Error result, a second request, the turn ends with a reply
Mixed reply The known tool runs, and the follow-up replays a call with no result Both calls answered in one follow-up
Voice-to-voice, unknown tool agent: X is not one of the model's tools X is not one of your tools. Use only the tools you were given

Rounds

Round Result
1 NO-GO from security and main review: a refusal kept by call id let calls sharing an id get the wrong answer; two new lines had no test; three comments were unclear.
2 NO-GO on one test: the value finish passes was pinned only by a test that failed about half the runs under the mutant. TestACallForAToolLoadedWhileItWaitsIsRefused fails it 20/20.
3 Pending.
Mutations and commands

Base repro on d734ca0bb: the hidden, spoken and mixed tests fail (the model was never answered after it called a hidden tool, the caller was left in silence..., the unknown call had no result); the control passes.

Mutation Killed by
Invert the offered check in finish the unknown-tool tests
finish passes nil instead of notOffered(call.Name) (agent.go:3985) TestACallForAToolLoadedWhileItWaitsIsRefused (20/20 runs)
Skip starting the unknown calls hidden, spoken, mixed, press
Count only known calls as pending mixed (20/20 runs)
Drop the tool name from the message hidden, spoken, mixed, press
Message not Correctable (model reads "the tool failed") hidden, spoken, mixed, press, TestANativeCallOfAToolTheModelIsNotOfferedIsNotRun
Pending counted only without a harness hidden, spoken, mixed, press
Lookup in executeTool removed (telephony.go:683-688) TestACallThatCarriesNoToolIsRefusedWhenItIsNotOffered
Refused call not followed, first follows site TestASpokenCallToPressThatWasNeverOfferedIsStillAnswered
Refused call not followed, second follows site (telephony.go, written conversation) TestAWrittenReplyThatPressesIsAnswered
Refused call started without TurnID: response.ID (agent.go:3985) TestACallForAHiddenToolIsAnsweredAndTheTurnEnds

Not killed: the closed guard in startTool (moved unchanged from the harness event loop; the same guard was untested on main), and the refusal in native.go:218 alone, because the lookup in executeTool gives the same answer.

go test ./internal/agent/ ./internal/harness/ -count=1
go test ./internal/agent/ -run 'TestAgentSuite/(TestASpokenCallToPress|TestAWrittenReplyThatPresses|TestACallForAHiddenTool|TestASpokenCallForATool|TestAReplyCallingAKnown|TestACallForAnOfferedTool|TestTwo|TestAKnownAndAHidden|TestACallThatCarries|TestACallForAToolLoaded)' -count=50 -cpu 1
go test ./internal/agent/ -run '<same>' -count=50 -race
go vet ./internal/agent/ ./internal/harness/ && go vet -tags integration ./internal/agent/ ./internal/harness/

Ticket: AI-4207

🤖 Generated with Claude Code

kanat and others added 3 commits November 29, 2026 12:27
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Text review: 3 unclear of 7

Three code comments read unclearly. Three unclear comments in one PR count as one Should fix (T1.1). The PR opening and the model-facing error text (notOffered) read clearly.

[Should fix] T1.1 The two comments below sit partly outside the changed lines, so they are here and not inline.

acceleration/internal/agent/agent.go:3897-3903: the first sentence holds four conditions and does not say what it decides. Suggested:

// A call for a tool the reply was not offered gets an error as its result. A call nobody answers
// keeps the follow-up request from going out, so the turn would never end.
//
// The offered tools are read while the reply is still generating, since SetTools waits for a quiet
// turn. The calls are counted pending under the lock that ends the reply, so a tool change waits for them.

acceleration/internal/harness/harness.go:763-769: "a tool acts on the call" can be read as the tool call, but it seems to mean the phone call. QUESTION: is that right? Suggested, if so:

// A call for a tool that was never offered is not passed on and never runs, since models sometimes
// invent tool names. Whoever records the reply must still answer such a call, or the turn waits for a
// result that never comes. The agent runs the other calls itself, since a tool can act on the phone
// call and the harness knows nothing about it.

Comment on lines +1026 to +1028
// refuseCallLocked has the call id answered with why it was not run (executeTool), rather
// than run. A model can ask for a tool it is not offered, which would otherwise run with no
// approval to ask. The caller holds a.mu.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Should fix] T1.1 The first sentence has no clear subject and verb. Suggested:

Suggested change
// refuseCallLocked has the call id answered with why it was not run (executeTool), rather
// than run. A model can ask for a tool it is not offered, which would otherwise run with no
// approval to ask. The caller holds a.mu.
// refuseCallLocked records an error as the result of call id, so executeTool returns it instead of
// running the tool. A model can ask for a tool it was not offered, and that tool would run with
// nobody asked to approve it. The caller holds a.mu.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5d8245f. refuseCallLocked is gone (c468f51), so its point now sits on executeTool and on the check that refuses a call whose tool is not offered. The comments in agent.go (finish) and harness.go (Requested) use the suggested wording. In harness.go, "the call" is the phone or video call the agent is on: the harness has no telephony code, and a transfer or a hang-up acts on that call, so the comment now says so.

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Security review, round 1: NO-GO, one Blocker.

The fix answers a call for a tool the reply was not offered, and that part holds: the refusal names only the tool the model asked for, every such name gets the same answer, names are matched exactly, and the follow-ups stop at the tool-round limit. The Blocker is in how a refusal is matched to its call. Details are in the inline comment.

Comment thread acceleration/internal/agent/agent.go Outdated
a.pendingTools += pending
a.pendingTools += len(calls)
for _, call := range unknown {
a.refuseCallLocked(call.ID, notOffered(call.Name))

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Blocker] S1.1 The refusal is recorded under the call's id (refusedCalls, read back at telephony.go:682), and the id comes from the provider, not from the router. If two calls in one reply share an id, they share one refusal. The first executeTool takes it, and the other call runs through callTool as an ordinary call. That call has no tool definition, so no approval question is asked either. On main these calls never reached executeTool, so this path is new.

Fix: give each refused call its own refusal, not an entry in a map keyed by id. For example, startTool can pass the refusal and executeTool can take it as an argument on this path. Another option is to refuse every call in a reply whose id repeats. A test for it is in the ticket.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in c468f51 and ca3834f. Each refused call now carries its own refusal to executeTool as a value, so the id-keyed map is gone. A call that reaches executeTool with no tool and no refusal, and whose tool is not offered now, is refused. Tests: TestTwoHiddenCallsSharingAnIDAreBothRefused, TestAKnownAndAHiddenCallSharingAnIDRunOnlyTheKnownOne (failed 32 of 200 runs on 328a080, passes 200 of 200 now, and 50 of 50 with -cpu 1 and with -race), TestACallThatCarriesNoToolIsRefusedWhenItIsNotOffered, TestACallForAToolLoadedInTheSameReplyIsRefused. The voice-to-voice path passes its refusal the same way; TestTwoNativeCallsSharingAnIDForAToolNotOfferedNeverRun covers it.

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: NO-GO

Reviewed 328a080 (it already contains the current main, d734ca0). The root cause holds: on main the harness skips a call for a tool that was not offered, the agent records the reply with that call on it, and nothing ever answers it, so no follow-up request goes out. The four new behaviour tests fail on main and pass here, and the control passes on both. go test ./internal/agent/ ./internal/harness/ passes, and so do go vet with and without the integration tag.

Checked and sound:

  • A tool that was not offered never runs. The refusal is the first branch in executeTool, before approval and callTool. A mutant where the harness passes the unknown call on as well is caught by the hidden, spoken and mixed tests.
  • The model is told only the tool name it used. The next request does not offer the hidden tool.
  • A model that keeps calling the hidden tool still stops. In probes, in writing and with a refused press on a call, the turn ended after 9 requests: maxToolRounds (8), then the reply that is offered no tools.
  • In a mixed reply the known tool runs, and one follow-up answers both calls.

[Should fix] R1.1 Two new lines can be broken without any test failing. Both are commented inline with a test row that I checked in a scratch copy: each fails under its mutant and passes on this head.

[Nit] R1.2 agent.go:4502: the closed guard in startTool is untested. It was already untested on main: removing it there also passes the full agent suite. For a ticket.

[Question] R1.3 telephony.go:1038: a deferred tool that the model calls before it loads it gets "Use only the tools you were given". That may steer the model away from load_tool. This is still better than main, where the turn hangs. For a ticket: decide whether a deferred tool that is not loaded yet should point at load_tool.

CI: the PR is a draft, so go and integration were skipped. They run at gh pr ready.

Verdict: request changes, for R1.1.

// somebody asked a question and is waiting on the answer, so the agent says it
// rather than sitting on it until they happen to speak again.
if a.followsTool(requested) {
if follows {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Should fix] R1.1 If this goes back to if a.followsTool(requested) {, every test still passes. In a written conversation a refused press then never gets a reply, and the turn hangs as it does on main: a voice call gets its reply from follow() once the agent's speech ends, but a text turn has nothing else to start it. Test row for unknown_tool_test.go: joinText(), ownsTools("x"), model.reply = ["Pressing."], model.then = ["There is no menu here."], asksFor(toolPress, {"digits":"1"}), RespondTo("press one"); expect 2 requests and endsWithAnswer. It fails under the mutant and on main, and passes here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 77af010: TestAWrittenReplyThatPressesIsAnswered fails when this line goes back to a.followsTool(requested).

Comment thread acceleration/internal/agent/agent.go Outdated
if currentHarness != nil && len(calls) > 0 {
currentHarness.Requested(response.ID, calls, offered)
for _, call := range unknown {
a.startTool(harness.ToolRequested{TurnID: response.ID, Call: call})

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Should fix] R1.1 If TurnID: response.ID is dropped here, the full agent suite still passes. The refused call's ToolStarted/ToolRan events then have no turn, and the session and the conversation store use that turn to place them (session.go:1910-1914, conversation.go:2008, conversation.go:2038). Test row: in TestACallForAHiddenToolIsAnsweredAndTheTurnEnds, take ran := s.awaitToolRan() and assert s.Equal(s.model.requests()[0].ID, ran.TurnID). It fails under the mutant and passes here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 77af010: TestACallForAHiddenToolIsAnsweredAndTheTurnEnds now checks that the refused call ToolRan carries the first request ID, and fails when TurnID: response.ID is dropped.

kanat and others added 4 commits November 29, 2026 13:14
A refused call now carries its refusal to executeTool as a value instead of
through a map keyed by call id, so two calls in one reply that share an id
each keep their own answer. A call that reaches executeTool with no tool and
no refusal, and whose tool is not offered now, is refused rather than run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… repeats

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: NO-GO

Delta round 2, head 5d8245f (on main d734ca0). One Should fix is open; the Blocker is fixed.

Prior findings

  • S1.1 (Blocker): fixed. Each call now carries its own refusal, and a call with no tool that is not offered now is refused in executeTool. The same-id probes pass: mixed case 200/200, two hidden calls 50/50, native repeat 50/50, with -race and with -cpu 1. The hidden-call loop stays bounded (9 requests in 3 s). No path reaches callTool or the tool runner without a tool definition or a refusal.
  • R1.1: fixed. Reverting the second follows site fails TestAWrittenReplyThatPressesIsAnswered. Dropping the turn id fails TestACallForAHiddenToolIsAnsweredAndTheTurnEnds.
  • T1.1, T1.2: fixed, the comments match the code.
  • R1.2 (nit, closed guard untested, same on base) and R1.3 (question, wording for a deferred tool called before it is loaded): still open, ticket.

New

  • R2.1 [Should fix], see the inline comment.
  • R2.2 [Nit]: the PR description is stale after this round (file:line references, the mutation table, and no row for the per-call refusal, the lookup in executeTool and the new tests). Refresh it at GO.

Suites: go test ./internal/agent/ ./internal/harness/ pass, -cpu 1 and -race pass on the touched tests. CI: draft, go and integration skipped, GitGuardian passed, CodeQL pending.

if currentHarness != nil && len(calls) > 0 {
currentHarness.Requested(response.ID, calls, offered)
for _, call := range unknown {
a.startTool(harness.ToolRequested{TurnID: response.ID, Call: call}, notOffered(call.Name))

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Should fix] R2.1: the refusal passed here is pinned only by a test that depends on timing.

If this argument is changed to nil, the unknown-tool tests still pass, because the lookup in executeTool answers the same way. Only TestACallForAToolLoadedInTheSameReplyIsRefused fails, and only when the load finishes first: 23 of 40 runs here, 14 of 20 in the fixer's run. A change caught about half the time is not covered.

Add a row that does not depend on timing, to unknown_tool_test.go: defersRefunds, joinText, then set s.agent.options.OnToolStarted so the start of a refund_last_order call waits (a plain poll with a deadline) until availableTools() contains refund_last_order. The reply calls load_tool for it and then refund_last_order. Expect s.runner.asked() empty and the model asked again once. I checked it in a scratch copy: 0 of 100 failures on this head, 100 of 100 with nil here. The row is in the ticket ledger as delta2/det_row_test.go.txt.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ff38607: TestACallForAToolLoadedWhileItWaitsIsRefused (unknown_tool_test.go). The refused call's start waits, with a deadline, until the tool is offered, then the test asserts the runner was never asked. With finish passing nil it fails 20/20; on head it passes -count=100, -cpu 1 and -race.

…t depend on timing

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: GO

Delta round 3, head ff38607 (old 5d8245f; origin/main unchanged, no merge needed).

R2.1 is fixed. The round adds one test and changes no production code (git diff 5d8245f48 ff38607ad --stat: unknown_tool_test.go, 33 insertions).

  • Mutant: finish passes nil instead of notOffered(call.Name) at agent.go:3985. TestACallForAToolLoadedWhileItWaitsIsRefused fails 20/20 runs ("a tool the reply was not offered ran").
  • Head: the same test passes -count=50 -race and -count=50 -cpu 1.

R2.2 is fixed. I checked the PR body against the code: the file:line refs (agent.go:3903, :3984-3985; telephony.go:680-688; native.go:214-218; harness.go:773 on the base), all 13 test names, and the quoted refusal text (telephony.go:352 and :758, telephony.go:1032) match. The "how a call could run" wording is neutral.

Prior findings:

  • S1.1 (Blocker): fixed in round 2, unchanged here.
  • R1.1: fixed in round 2, unchanged here.
  • T1.1, T1.2: fixed in round 2, unchanged here.
  • R1.2 (Nit, closed guard in startTool): open, ticketed as before.
  • R1.3 (Question, deferred tool wording): open, ticketed as before.

New findings: none.

Suites on ff38607: go test ./internal/agent -run 'AgentSuite/Test.*(Unknown|Hidden|Refus|Offered|Loaded|Native|Press|Written|Sharing|Carries)' -count=5 -race: ok (367 s). The round-2 full run of ./internal/agent and ./internal/harness passed on this test file's predecessor; this round changed only the test file.
CI: draft, go and integration skipped, GitGuardian pass, CodeQL pending.

@kanat
kanat marked this pull request as ready for review November 29, 2026 19:11

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant