Repository navigation
Python: Preserve registered tool policy in declarative agents - #10295
Conversation
Code coverage overviewLanguages: Python Python / code-coverage/python
View coverage of the 1 most impacted file.
ℹ️ Calculations represent line coverage. Results may change as calculations are updated. Updated |
There was a problem hiding this comment.
🟡 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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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]: |
There was a problem hiding this comment.
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.

Motivation & Context
Declarative agent bindings can reference an existing
FunctionTool.AgentFactorycurrently 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
AgentFactory._parse_toolnow recognizes boundFunctionToolinstances 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, bothsafe_modevalues, parser failures, and the original tool's invocation and exception limits. Two overlong core error-message lines are wrapped without changing behavior._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
AI assistance details: Copilot assisted with implementation analysis, focused regression tests, validation, review-feedback handling, and this pull request description.
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after a language prefix such asPython:or.NET:— a workflow keeps the label and title prefix in sync automatically.