@@ -32,6 +32,7 @@ internal sealed class InvokeFunctionToolExecutor(
3232 private const string ApprovalSnapshotStateKey = nameof ( _approvalSnapshots ) ;
3333 private const string HasApprovalRequiredInvocationStateKey = nameof ( _hasApprovalRequiredInvocation ) ;
3434 private const string PendingCallIdsStateKey = nameof ( _pendingNonApprovalCallIds ) ;
35+ private const string AutoSendSnapshotsStateKey = nameof ( _autoSendSnapshots ) ;
3536 private const string LegacyApprovalSnapshotStateKey = "_approvalSnapshot" ;
3637
3738 /// <summary>
@@ -40,6 +41,7 @@ internal sealed class InvokeFunctionToolExecutor(
4041 /// response is captured.
4142 /// </summary>
4243 private readonly ConcurrentDictionary < string , ApprovalSnapshot > _approvalSnapshots = new ( StringComparer . Ordinal ) ;
44+ private readonly ConcurrentDictionary < string , bool > _autoSendSnapshots = new ( StringComparer . Ordinal ) ;
4345
4446 private bool _hasApprovalRequiredInvocation ;
4547
@@ -78,10 +80,12 @@ public static class Steps
7880 {
7981 string functionName = this . GetFunctionName ( ) ;
8082 bool requireApproval = this . GetRequireApproval ( functionName ) ;
83+ bool autoSend = this . GetAutoSendValue ( ) ;
8184 Dictionary < string , object ? > ? arguments = this . GetArguments ( ) ;
8285
8386 // Per-invocation request id stamped on the outbound content.
8487 string requestId = Guid . NewGuid ( ) . ToString ( "N" ) ;
88+ this . _autoSendSnapshots [ requestId ] = autoSend ;
8589
8690 // Create the function call content to send to the caller
8791 FunctionCallContent functionCall = new (
@@ -128,7 +132,6 @@ public async ValueTask CaptureResponseAsync(
128132 ExternalInputResponse response ,
129133 CancellationToken cancellationToken )
130134 {
131- bool autoSend = this . GetAutoSendValue ( ) ;
132135 string ? conversationId = this . GetConversationId ( ) ;
133136 string ? responseRequestId = response . RequestId ;
134137
@@ -155,6 +158,8 @@ public async ValueTask CaptureResponseAsync(
155158 && IsCorrelatedResponse ( r . CallId , responseRequestId ) ) ;
156159 }
157160
161+ bool autoSend = matchingResult is not null && this . GetResponseAutoSendValue ( response ) ;
162+
158163 // When the caller approved an approval-required function call but didn't execute it
159164 // locally (the hosted Foundry scenario, where mcp_approval_response is converted to a
160165 // ToolApprovalResponseContent only), invoke the registered AIFunction here so that the
@@ -183,16 +188,21 @@ public async ValueTask CaptureResponseAsync(
183188 else if ( ! approval . Approved )
184189 {
185190 this . _approvalSnapshots . TryRemove ( approval . RequestId , out _ ) ;
191+ this . _autoSendSnapshots . TryRemove ( approval . RequestId , out _ ) ;
186192 await this . AssignErrorAsync ( context , "Function invocation was not approved by user." ) . ConfigureAwait ( false ) ;
187193 }
188- else if ( this . _approvalSnapshots . TryRemove ( approval . RequestId , out ApprovalSnapshot ? snapshot ) )
189- {
190- matchingResult = await this . InvokeRegisteredFunctionAsync ( approval . RequestId , snapshot , cancellationToken ) . ConfigureAwait ( false ) ;
191- approvedFunctionResult = matchingResult ;
192- }
193194 else
194195 {
195- await this . AssignErrorAsync ( context , "No pending approval matched the response." ) . ConfigureAwait ( false ) ;
196+ autoSend = this . GetResponseAutoSendValue ( response ) ;
197+ if ( this . _approvalSnapshots . TryRemove ( approval . RequestId , out ApprovalSnapshot ? snapshot ) )
198+ {
199+ matchingResult = await this . InvokeRegisteredFunctionAsync ( approval . RequestId , snapshot , cancellationToken ) . ConfigureAwait ( false ) ;
200+ approvedFunctionResult = matchingResult ;
201+ }
202+ else
203+ {
204+ await this . AssignErrorAsync ( context , "No pending approval matched the response." ) . ConfigureAwait ( false ) ;
205+ }
196206 }
197207 }
198208 else if ( ! this . _approvalSnapshots . IsEmpty )
@@ -246,6 +256,7 @@ await this.AssignAsync(
246256 // Drop the per-invocation entry now that the response side effects have been processed.
247257 this . _pendingNonApprovalCallIds . TryRemove ( matchingResult . CallId , out _ ) ;
248258 this . _approvalSnapshots . TryRemove ( matchingResult . CallId , out _ ) ;
259+ this . _autoSendSnapshots . TryRemove ( matchingResult . CallId , out _ ) ;
249260 }
250261
251262 // Completes the action after processing the function result.
@@ -266,6 +277,7 @@ private IEnumerable<ChatMessage> GetSideEffectMessages(ExternalInputResponse res
266277 public override ValueTask ResetAsync ( )
267278 {
268279 this . _approvalSnapshots . Clear ( ) ;
280+ this . _autoSendSnapshots . Clear ( ) ;
269281 this . _hasApprovalRequiredInvocation = false ;
270282 this . _pendingNonApprovalCallIds . Clear ( ) ;
271283 return default ;
@@ -285,6 +297,8 @@ protected override async ValueTask OnCheckpointingAsync(IWorkflowContext context
285297
286298 List < string > pendingCopy = [ .. this . _pendingNonApprovalCallIds . Keys ] ;
287299 await context . QueueStateUpdateAsync ( PendingCallIdsStateKey , pendingCopy , null , cancellationToken ) . ConfigureAwait ( false ) ;
300+ Dictionary < string , bool > autoSendCopy = this . _autoSendSnapshots . ToDictionary ( kvp => kvp . Key , kvp => kvp . Value , StringComparer . Ordinal ) ;
301+ await context . QueueStateUpdateAsync ( AutoSendSnapshotsStateKey , autoSendCopy , null , cancellationToken ) . ConfigureAwait ( false ) ;
288302
289303 await base . OnCheckpointingAsync ( context , cancellationToken ) . ConfigureAwait ( false ) ;
290304 }
@@ -297,6 +311,16 @@ protected override async ValueTask OnCheckpointingAsync(IWorkflowContext context
297311 protected override async ValueTask OnCheckpointRestoredAsync ( IWorkflowContext context , CancellationToken cancellationToken = default )
298312 {
299313 await base . OnCheckpointRestoredAsync ( context , cancellationToken ) . ConfigureAwait ( false ) ;
314+ this . _autoSendSnapshots . Clear ( ) ;
315+ Dictionary < string , bool > ? autoSendSnapshots = await context . ReadStateAsync < Dictionary < string , bool > > (
316+ AutoSendSnapshotsStateKey , null , cancellationToken ) . ConfigureAwait ( false ) ;
317+ if ( autoSendSnapshots is not null )
318+ {
319+ foreach ( KeyValuePair < string , bool > entry in autoSendSnapshots )
320+ {
321+ this . _autoSendSnapshots [ entry . Key ] = entry . Value ;
322+ }
323+ }
300324 this . _approvalSnapshots . Clear ( ) ;
301325 Dictionary < string , ApprovalSnapshot > ? snapshots = await context . ReadStateAsync < Dictionary < string , ApprovalSnapshot > > (
302326 ApprovalSnapshotStateKey , null , cancellationToken ) . ConfigureAwait ( false ) ;
@@ -525,14 +549,38 @@ private string GetFunctionName() =>
525549 private bool GetRequireApproval ( string functionName )
526550 {
527551 bool requireApproval = this . Model . RequireApproval is not null
528- && this . Evaluator . GetValue ( this . Model . RequireApproval ) . Value ;
552+ && this . GetNonSensitiveValue ( this . Evaluator . GetValue ( this . Model . RequireApproval ) , "require approval" ) ;
529553
530554 return requireApproval
531555 || agentProvider . Functions ? . FirstOrDefault (
532556 f => string . Equals ( f . Name , functionName , StringComparison . Ordinal ) )
533557 ? . GetService < ApprovalRequiredAIFunction > ( ) is not null ;
534558 }
535559
560+ private bool GetResponseAutoSendValue ( ExternalInputResponse response )
561+ {
562+ AIContent [ ] contents = [ .. response . Messages . SelectMany ( message => message . Contents ) ] ;
563+ FunctionResultContent ? result = contents . OfType < FunctionResultContent > ( )
564+ . FirstOrDefault ( r => this . IsCorrelatedPendingNonApprovalResult ( r , response . RequestId ) ) ;
565+ if ( result is not null )
566+ {
567+ return this . _autoSendSnapshots . TryGetValue ( result . CallId , out bool resultAutoSend )
568+ ? resultAutoSend
569+ : this . GetAutoSendValue ( ) ;
570+ }
571+
572+ ToolApprovalResponseContent ? approval = contents . OfType < ToolApprovalResponseContent > ( )
573+ . FirstOrDefault ( r => this . _approvalSnapshots . ContainsKey ( r . RequestId ) &&
574+ IsCorrelatedResponse ( r . RequestId , response . RequestId ) ) ;
575+ if ( approval is not null && this . _autoSendSnapshots . TryGetValue ( approval . RequestId , out bool approvalAutoSend ) )
576+ {
577+ return approvalAutoSend ;
578+ }
579+
580+ // Older checkpoints and legacy responses have no captured setting.
581+ return this . GetAutoSendValue ( ) ;
582+ }
583+
536584 private bool GetAutoSendValue ( )
537585 {
538586 // InvokeToolOutput.AutoSend is never null — it returns a literal-false default
@@ -541,7 +589,7 @@ private bool GetAutoSendValue()
541589 // default as autoSend = true (the historical behavior).
542590 if ( this . Model . Output is { AutoSendIsDefaultValue : false } output )
543591 {
544- return this . Evaluator . GetValue ( output . AutoSend ) . Value ;
592+ return this . GetNonSensitiveValue ( this . Evaluator . GetValue ( output . AutoSend ) , "auto send" ) ;
545593 }
546594
547595 return true ;
0 commit comments