Repository navigation
Honor FunctionInvocationContext.Terminate when the function throws - #7801
Merged
Merged
Conversation
FunctionInvocationProcessor hard-coded terminate: false in the exception path, so a function that set Terminate = true and then threw did not stop the function-invocation loop. Use context.Terminate as the success path does. Affects FunctionInvokingChatClient (streaming, non-streaming, approvals) and FunctionInvokingRealtimeClientSession. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Copilot review overview
ðĄ Changes recommended
The exception path terminates correctly but omits the established termination diagnostic log.
Review effort: Balanced
Findings: 1
Open (1)
What changed in this PR
Ensures function-invocation loops honor termination requests even when functions throw.
Changes:
- Preserves
Terminateon exception results. - Adds chat, approval, concurrency, streaming, and realtime coverage.
| File | Description |
|---|---|
FunctionInvocationProcessor.cs |
Propagates termination on exceptions. |
FunctionInvokingChatClientTests.cs |
Tests chat invocation scenarios. |
FunctionInvokingChatClientApprovalsTests.cs |
Tests approved-function termination. |
FunctionInvokingRealtimeClientTests.cs |
Tests realtime termination. |
ðĄ Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
Addresses review feedback: LogFunctionRequestedTermination was only emitted on the success path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Collaborator
ð Good job! The coverage increased ð
Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=1622283&view=codecoverage-tab |
Contributor
Author
|
@dotnet-policy-service agree company="Microsoft" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Fixes #7800
Problem
If an
AIFunctionsetsFunctionInvocationContext.Terminate = trueand then throws, the loop does not terminate.FunctionInvocationProcessor.ProcessSingleFunctionCallAsynchard-codedterminate: falsein its exception path.Fix
One-line change: pass
context.Terminatein the exception path, as the success path already does.Behavior
FunctionInvokingChatClient(streaming, non-streaming, approved-function path) stops after the error result.AllowConcurrentInvocation, all calls in the turn finish, then the loop stops.FunctionInvokingRealtimeClientSessionstops without sending results back, matching non-throwing termination.FunctionInvocationResultcan now haveTerminate == truewithStatus == Exception.Tests
Added 6 tests (chat serial/concurrent/streaming, exception preserved on result, approvals, realtime). All 6 fail without the fix. The full
Microsoft.Extensions.AI.Testsproject passes on net10.0 (767/767).ðĪ Generated with Claude Code
Microsoft Reviewers: Open in CodeFlow