Skip to content

Python: Preserve registered tool policy in declarative agents - #10295

Merged
Jose Alvarez (jpalvarezl) merged 3 commits into
mainfrom
jpalvarezl-preserve-bound-tool-policy
Nov 12, 2026
Merged

Jose Alvarez (jpalvarezl) merged 3 commits into
mainfrom
jpalvarezl-preserve-bound-tool-policy

Conversation

@jpalvarezl

@jpalvarezl Jose Alvarez (jpalvarezl) commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Motivation & Context

Declarative agent bindings can reference an existing FunctionTool. AgentFactory currently adapts that object into a new exposed tool but keeps only its callable, which drops the registered approval policy and other supported execution metadata from the tool seen by the function-invocation layer.

This change preserves the registered tool policy across that adaptation boundary while retaining the original tool as the callable so its lifetime invocation and exception counters remain authoritative. It also wraps two existing core error messages that exceeded the repository line-length limit and blocked package-wide Ruff validation on the current target branch.

Description & Review Guide

  • What are the major changes? AgentFactory._parse_tool now recognizes bound FunctionTool instances and carries their approval mode, kind, additional properties, and result parser onto the exposed declarative wrapper. Parser failures consume the registered tool's exception budget without parsing twice. Focused regressions exercise the real function-invocation approval loop, both safe_mode values, parser failures, and the original tool's invocation and exception limits. Two overlong core error-message lines are wrapped without changing behavior.
  • What is the impact of these changes? Registered approval requirements and supported metadata remain effective after declarative wrapping. Plain callable bindings are unchanged, and invocation limits are not copied onto the wrapper, avoiding duplicate counter enforcement. Package-wide Python formatting validation can run against the current target source.
  • What do you want reviewers to focus on? The metadata-preservation and parser-accounting boundary in _parse_tool, especially that the original registered tool remains the callable and sole owner of its lifetime counters. The core change is formatting-only.

Related Issue

N/A - trivial change. This is an isolated correction that preserves an already-configured tool policy during declarative adaptation, with no public API or design choice introduced. The additional core edits only restore existing formatting requirements. No competing open pull request was found.

AI Assistance

  • No material AI assistance was used.
  • This is an AI-assisted contribution. I reviewed, understood, and verified all submitted content and accept responsibility for it.

AI assistance details: Copilot assisted with implementation analysis, focused regression tests, validation, review-feedback handling, and this pull request description.

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 links to an agreed issue with no competing open PR, or the Related Issue section documents a trivial-change or repository-automation exception.
  • 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 a language prefix such as Python: or .NET: — a workflow keeps the label and title prefix in sync automatically.

Copilot AI balanced review requested due to automatic review settings November 12, 2026 09:44
@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Nov 12, 2026
@github-code-quality

github-code-quality Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Code coverage overview

Languages: Python

Python / code-coverage/python

main 01de197 jpalvarezl-preserve-... bcb744e +/-
92% 92% 0%
View coverage of the 1 most impacted file.
File main 01de197 jpalvarezl-preserve-... bcb744e +/-
packages/declar...tive/_loader.py 80% 82% +2%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.


Updated November 12, 2026 10:22 UTC

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.

🟡 Changes recommended

Parser failures increment only the wrapper’s exception counter, bypassing the registered tool’s invocation-exception limit and allowing subsequent calls that should be blocked.

1 open issue
What changed in this PR

This PR updates Python’s declarative AgentFactory to preserve registered FunctionTool approval policy and execution metadata when exposing tools under declarative names.

Changes:

  • Copies approval mode, kind, additional properties, and result parser onto the wrapper.
  • Keeps the registered tool as the callable rather than copying its invocation limits.
  • Adds regression coverage for approval requests under both safe-mode settings and for invocation and exception limits.
File Description
python/​packages/​declarative/​tests/​test_declarative_loader.py Adds policy-preservation and lifetime-limit regression tests.
python/​packages/​declarative/​tests/​conftest.py Adds a test client and fixture using the real function-invocation layer.
python/​packages/​declarative/​agent_framework_declarative/​_loader.py Preserves registered tool metadata during declarative wrapping.

Review effort: Balanced

@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)): bc0bc9aa0e52
Model: gpt-5.6-sol

Overview

The change preserves approval policy and execution metadata by keeping the registered FunctionTool as the declarative wrapper's callable, and the focused tests verify approval gating plus lifetime invocation and function-exception limits. However, copying the registered result parser onto an unlimited wrapper splits exception accounting: parser failures no longer consume the registered tool's configured exception allowance. This leaves a repeat-execution path despite the host's configured safeguard.

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: python/packages/declarative/agent_framework_declarative/_loader.py

@jpalvarezl
Jose Alvarez (jpalvarezl) added this pull request to the merge queue Nov 12, 2026
Merged via the queue into main with commit aad4666 Nov 12, 2026
57 checks passed
@jpalvarezl
Jose Alvarez (jpalvarezl) deleted the jpalvarezl-preserve-bound-tool-policy branch November 12, 2026 10:49

@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: 2 net-new commit(s): 91c1907615a2, bcb744ed8134
Model: gpt-5.6-sol

Overview

The change preserves registered approval and execution metadata while keeping invocation limits on the original registered tool, and the focused tests cover approval plus direct parser-failure accounting. The remaining risk is that the local parser closure is not copy-safe: deep-copied wrappers enforce the exception limit on one tool instance while charging failures to another.

Reviewed the supplied incremental 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: python/packages/declarative/agent_framework_declarative/_loader.py

if registered_tool is not None and callable(result_parser):
registered_result_parser = result_parser

def preserve_parser_exception_count(result: Any) -> str | list[Content]:

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.

Because this is a local closure, copy.deepcopy(wrapped_tool) deep-copies wrapped_tool.func but leaves result_parser closing over the original registered_tool. Parser failures on the copy increment the original counter while the copied nested tool stays at zero, so its max_invocation_exceptions guard never trips and repeated copied invocations continue executing. Please make the parser adapter copy-safe so its accounting target is the same nested FunctionTool that the wrapper invokes after copying.

This branch was successfully deployed

1 active deployment
github-app-auth — bcb744ed Deployed Nov 12, 2026 by jpalvarezl via team_check #9349
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants