Skip to content

.NET: Preserve harness middleware for dynamic tools - #8402

Merged
Roger Barreto (rogerbarreto) merged 7 commits into
microsoft:mainfrom
rogerbarreto:rogerbarreto/function-middleware-harness
Sep 18, 2026
Merged

Roger Barreto (rogerbarreto) merged 7 commits into
microsoft:mainfrom
rogerbarreto:rogerbarreto/function-middleware-harness

Conversation

@rogerbarreto

Copy link
Copy Markdown
Member

Motivation & Context

Keep harness function middleware consistent when an agent's tool collection evolves during a run. This proposal preserves middleware composition for dynamic tools while retaining the existing function-calling pipeline and reusable run-options configuration.

Description & Review Guide

  • What are the major changes? Add a private, per-request FunctionMiddlewarePreservingChatClient and a middleware-aware tool collection. Preserve callback composition across tool additions, replacements, and option cloning without adding agent state to the options types. Extend .NET regression coverage and add Python regression-only coverage for its existing behavior.
  • What is the impact of these changes? Dynamic tools retain the registered callbacks without changing public APIs or dependencies. The existing function wrappers and FunctionInvokingChatClient remain responsible for their current roles; caller-owned options and shared clients are not modified.
  • What do you want reviewers to focus on? Compatibility of callback ordering and continuation behavior, collection replacement, approval handling, custom clients, and isolation when options or clients are reused across runs.

Related Issue

No public issue linked.

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.

Copilot AI balanced review requested due to automatic review settings September 15, 2026 15:54
@agent-framework-automation agent-framework-automation Bot added python Usage: [Issues, PRs], Target: Python .NET Usage: [Issues, PRs], Target: .Net labels Sep 15, 2026
@github-actions github-actions Bot changed the title .NET: Preserve harness middleware for dynamic tools Python: .NET: Preserve harness middleware for dynamic tools Sep 15, 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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@github-actions github-actions Bot 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.

MAF Automated Review — Iteration 1

Result: Findings reported
Scope: full PR (1 commit(s)): ef0182a95c90
Model: gpt-5.6-sol-fast

Overview

The change keeps function middleware request-scoped by cloning run and chat options, wraps collection mutations, and reapplies wrapping after whole-collection replacement; the added tests cover ordering, approval metadata, streaming parity, and shared-client isolation. One composition gap remains: middleware-chain discovery depends on every custom chat-client decorator forwarding GetService, so a supported opaque decorator can cause later replacement tools to lose callbacks.

Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
1 verified finding remained after source verification (1 medium) across 1 file. Details are attached to the affected lines below.

Affected areas: dotnet/src/Microsoft.Agents.AI/FunctionInvocationDelegatingAgent.cs

Comment thread dotnet/src/Microsoft.Agents.AI/FunctionInvocationDelegatingAgent.cs Outdated
Keep harness callbacks consistent as tool collections change.
Preserve existing function wrappers and request-local composition
without coupling reusable options to agent instances.
Keep callback composition independent of client service discovery.
Use request-local execution scopes without changing reusable options.
Cover scope restoration and tool repair after collection updates.
@rogerbarreto
Roger Barreto (rogerbarreto) force-pushed the rogerbarreto/function-middleware-harness branch from ef0182a to d9eb7fd Compare September 15, 2026 18:16
@rogerbarreto Roger Barreto (rogerbarreto) changed the title Python: .NET: Preserve harness middleware for dynamic tools .NET: Preserve harness middleware for dynamic tools Sep 15, 2026
Keep harness callback composition consistent across function-loop
iterations by retaining the full chain on function wrappers.
Cover real client-driven clones, repeated calls and approval flows.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Small nit on the code.

Comment thread python/packages/core/tests/core/test_function_invocation_logic.py Outdated
Cover Python harness callback consistency across progressive tool
loading, continuation changes, failures, approvals and reused options.

@github-actions github-actions Bot 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.

MAF Automated Review — Iteration 2

Result: Findings reported
Scope: full PR (4 commit(s)): af615706a689, d9eb7fd9fded, 20087cc93201, 33dd41ef4641
Model: gpt-5.6-sol-fast

Overview

The PR uses per-request client decoration, a middleware-aware tool collection, and wrapper-carried metadata to preserve callbacks across dynamic tool mutations and option clones. Its regression suite covers callback ordering, streaming, nested runs, approvals, failures, and caller-owned option isolation. Three composition boundaries remain unsafe: replacing client factories can discard the preserving layer, ambient run identity can merge unrelated clients, and opaque function decorators can cause duplicate callback execution.

Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
3 verified findings remained after source verification (3 medium) across 1 file. Details are attached to the affected lines below.

Affected areas: dotnet/src/Microsoft.Agents.AI/FunctionInvocationDelegatingAgent.cs

Comment thread dotnet/src/Microsoft.Agents.AI/FunctionInvocationDelegatingAgent.cs Outdated
Comment thread dotnet/src/Microsoft.Agents.AI/FunctionInvocationDelegatingAgent.cs Outdated
Comment thread dotnet/src/Microsoft.Agents.AI/FunctionInvocationDelegatingAgent.cs
Build callback chains around factory results instead of sharing them
between active client calls. Track active invocation callbacks across
opaque function decorators while preserving explicit continuations.

Cover replacement factories, captured clients and failure cleanup.
Comment thread dotnet/src/Microsoft.Agents.AI/FunctionInvocationDelegatingAgent.cs
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@rogerbarreto
Roger Barreto (rogerbarreto) added this pull request to the merge queue Sep 18, 2026
Merged via the queue into microsoft:main with commit f2ba504 Sep 18, 2026
47 checks passed
@rogerbarreto
Roger Barreto (rogerbarreto) deleted the rogerbarreto/function-middleware-harness branch September 18, 2026 14:50
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 python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants