Repository navigation
[BREAKING] Python: Protect AG-UI tool approval state from client Shared State - #9368
Conversation
…ed State AG-UI merges client Shared State into AgentSession.state, minus a protected set of server-owned namespaces. That set comes from optional attributes of the agent object AG-UI is handed, so a wrapper that does not expose the wrapped agent's ToolApprovalMiddleware left its custom source_id unprotected, and client state could supply or replace that middleware's approval state. Protect tool approval state by shape as well as by name: a client value holding a non-empty rules, queued_approval_requests or collected_approval_responses list is not merged into the session whatever its item encoding, and a key that already holds approval state in restored server state cannot be replaced by client values. A warning names each ignored key. Client state keys carrying those lists are no longer mirrored into AgentSession.state for any agent; they remain in Shared State and the model context.
There was a problem hiding this comment.
🟡 Changes recommended
Object-valued ToolApprovalState remains replaceable when middleware metadata is hidden.
1 open finding
What changed in this PR
Protects AG-UI tool-approval state when wrappers hide middleware metadata.
Changes:
- Detects approval authority by state shape.
- Protects restored server-owned approval namespaces.
- Adds wrapper scenarios, security tests, and guidance.
| File | Description |
|---|---|
python/packages/ag-ui/agent_framework_ag_ui/_agent_run.py |
Adds shape-based approval-state protection. |
python/packages/ag-ui/tests/ag_ui/test_run.py |
Tests protection helpers and field synchronization. |
python/packages/ag-ui/tests/ag_ui/test_endpoint.py |
Tests wrapped-agent approval scenarios. |
python/packages/ag-ui/AGENTS.md |
Documents the state-authority rule. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
MAF Automated Review — Iteration 1
Result: No findings
Scope: full PR (1 commit(s)): 4535f4ae8514
Model: gpt-5.6-sol
Overview
The PR adds shape-based filtering so client Shared State cannot inject tool-approval rules, queued requests, or collected responses through middleware namespaces hidden by wrappers. Its strongest guardrails combine name-based and shape-based protection before the session merge, with coverage for request and snapshot origins, alternate encodings, forged approval content, trusted restored mappings, and human approval resume. After deduplication against existing feedback and reconciliation against the base behavior, no additional publishable issue remains.
Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
No publishable findings remained after source verification for this scope.
A server-created conversation can hold approval rules as a ToolApprovalState object rather than a mapping. The restored-state check only recognized mappings, so client Shared State could replace that object under a middleware namespace that a wrapper hides, erasing the trusted rules or failing the run. Recognize ToolApprovalState objects as server-owned approval state too, and cover the object-valued form with unit and endpoint tests.

Motivation & Context
AG-UI merges client Shared State into
AgentSession.state, minus a protected set of server-owned namespaces. That set is derived from optional attributes of the object AG-UI is handed (middleware,context_providers,service_session_state_keys), none of which are part ofSupportsAgentRun.#8714 added custom
ToolApprovalMiddleware.source_idnamespaces to that set, but only for agents that expose their middleware. A wrapper that does not expose it leaves the wrapped agent's custom namespace unprotected. That covers a plainSupportsAgentRunimplementation, and aBaseAgentsubclass that delegatesrun()while keeping its own emptymiddleware. Client Shared State, including Shared State replayed from a stored Thread Snapshot, could then supply or replace the standing approval state that the wrapped middleware reads.This completes the protection from #8714 for wrapped agents.
Description & Review Guide
_agent_run.pynow protects tool approval state by shape as well as by name. A top-level client Shared State value holding a non-emptyrules,queued_approval_requestsorcollected_approval_responseslist is not merged intoAgentSession.state, whatever its item encoding (ToolApprovalStateloads more than JSON objects, for example key/value pair lists). A key that already holds approval state in restored server state, as a mapping or aToolApprovalStateobject, cannot be replaced by client values, whatever their shape. A bounded warning names the ignored keys.BaseAgent-derived wrapper (request and snapshot state, trusted and untrusted). New tests cover client values overwriting a trusted rule (held as a mapping or as aToolApprovalStateobject), rule encodings, forged queued and collected approvals, the compatibility boundary, a human approval round trip through the wrapper, and the helpers, including a pin of the field names toToolApprovalState.python/packages/ag-ui/AGENTS.mddocuments the rule.rules,queued_approval_requestsorcollected_approval_responseslist is no longer mirrored intoAgentSession.state. It stays in Shared State, snapshots and the model context. This is why the PR is marked[BREAKING].ToolApprovalStateaccepts.{"policy": {"rules": [{"id": 1}]}}would otherwise be persisted as server-owned and then frozen against client updates.Related Issue
N/A - no separate issue was filed. This completes the protection added in #8714 for wrapped agents.
AI Assistance
AI assistance details: implementation, tests, the documentation note, and this PR description were prepared with GitHub Copilot.
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.