Skip to content

.NET: Add pending tool approval closure semantics (#7872) - #7909

Draft
Shivani . (Shivani767) wants to merge 2 commits into
microsoft:mainfrom
Shivani767:fix/7872-pending-tool-approval-closure
Draft

.NET: Add pending tool approval closure semantics (#7872)#7909
Shivani . (Shivani767) wants to merge 2 commits into
microsoft:mainfrom
Shivani767:fix/7872-pending-tool-approval-closure

Conversation

@Shivani767

@Shivani767 Shivani . (Shivani767) commented Aug 27, 2026

Copy link
Copy Markdown

Motivation & Context

When a run is cancelled, refreshed, or otherwise interrupted after the model emits a tool call (or after ToolApprovalRequestContent is surfaced), the session can retain pending approval state with no public, reliable way for the host to enumerate or close it. Separately, ApprovalResponseBindingChatClient cleared the pending bag on any subsequent turn—even an unrelated user message with no approval response—so a restored session could lose binding authority while a dangling FunctionCallContent remained in history (#7862 / #7872).

This PR addresses the triage-sized slice of #7872: stop the silent consume bug and give hosts a public drain/closure path. Moving the FICC loop out of Microsoft.Extensions.AI and adding a configurable auto-drain policy are left for follow-up.

Assigned on the issue to Roger Barreto (@rogerbarreto) — happy to coordinate or close if a maintainer PR supersedes this.

Description & Review Guide

  • What are the major changes?

    • Do not clear pending approvals on unrelated turns. ValidateInboundApprovalResponses only consumes bag entries when a matching ToolApprovalResponseContent is bound in the current turn; an ordinary user follow-up leaves pending state intact.
    • Public session helpers in ToolApprovalAgentSessionExtensions:
      • TryGetPendingToolApprovalRequests — enumerate pending approvals (including after session restore).
      • CreatePendingApprovalRejections — drain as explicit rejections the host can send on the next run so FICC can emit terminal FunctionResultContent.
      • ClearPendingToolApprovalRequests — drop bag entries without producing responses (documented as the weaker option).
    • Unit tests covering preserve-on-unrelated-turn, serialize/deserialize round-trip, consume-after-response, reject drain, and clear.
  • What is the impact of these changes?

    • Restored sessions no longer silently lose approval binding authority on the next normal user message.
    • Hosts can discover and deliberately close pending approvals without reading private state bag keys.
    • Does not auto-drain on every new user turn (configurable policy still open) and does not cover non-approval dangling FunctionCallContent closure in FICC.
  • What do you want reviewers to focus on?

Related Issue

Fixes #7872

Related: #7862, #7867

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

Keep pending approval bag entries across unrelated user turns, and expose
enumerate/reject/clear helpers so hosts can drain restored sessions before
the next normal message (microsoft#7872).
Copilot AI balanced review requested due to automatic review settings August 27, 2026 10:45
@agent-framework-automation agent-framework-automation Bot added the .NET Usage: [Issues, PRs], Target: .Net label Aug 27, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds durable pending tool-approval discovery and explicit closure APIs while preserving pending approvals across unrelated turns.

Changes:

  • Preserves pending approvals until matching responses arrive.
  • Adds APIs to enumerate, reject, or clear pending approvals.
  • Adds lifecycle and serialization tests.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
ApprovalResponseBindingChatClient.cs Retains and selectively consumes pending approvals.
ToolApprovalAgentSessionExtensions.cs Adds public pending-approval session helpers.
ApprovalResponseBindingChatClientTests.cs Tests preservation, restoration, rejection, and clearing.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread dotnet/src/Microsoft.Agents.AI/ChatClient/ToolApprovalAgentSessionExtensions.cs Outdated
Comment thread dotnet/src/Microsoft.Agents.AI/ChatClient/ToolApprovalAgentSessionExtensions.cs Outdated
Return cloned FunctionCallContent arguments from TryGet, and leave pending
entries in place until CreatePendingApprovalRejections responses are bound
so restore-path rejections are not dropped as unbound.
@soul-soft

Copy link
Copy Markdown

Thanks for the update. I want to double-check one remaining case from #7872.

This PR appears to address pending approvals, but does it also handle ordinary non-approval tool calls that become
dangling when agent.run is cancelled before the function loop drains/completes them?

My concern is that a cancelled run can still leave unresolved FunctionCallContent for normal tools, and the next turn
may proceed without an explicit rejection/closure message for those calls. That seems separate from the approval queue
case.

Is that scenario expected to be fixed in this PR, or should #7872 remain open / be tracked by a follow-up issue?

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

Labels

.NET Usage: [Issues, PRs], Target: .Net

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET: [Bug]: Add explicit closure semantics for dangling tool call / approval flows

3 participants