Skip to content

Python: (ag-ui): fix approval payloads being re-processed on subsequent conversation turns#4232

Merged
moonbox3 merged 3 commits intomicrosoft:mainfrom
moonbox3:4075-fix
Feb 26, 2026
Merged

Python: (ag-ui): fix approval payloads being re-processed on subsequent conversation turns#4232
moonbox3 merged 3 commits intomicrosoft:mainfrom
moonbox3:4075-fix

Conversation

@moonbox3
Copy link
Contributor

Motivation and Context

Fixes a bug where approval payloads in ag-ui were being re-executed on subsequent conversation turns, causing duplicate tool invocations and stale approvals to be re-processed.

When a user approves a tool call in ag-ui:

  1. The approval payload ({"accepted": true}) is processed and the tool executes
  2. The snapshot sent back to CopilotKit still contains the raw approval payload
  3. On the next turn, CopilotKit re-sends the full history including the approval
  4. The adapter re-detects the approval and re-executes the tool

This caused tools to run multiple times and approval history to become corrupted.

Description

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

@moonbox3 moonbox3 self-assigned this Feb 24, 2026
Copilot AI review requested due to automatic review settings February 24, 2026 23:05
@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented Feb 24, 2026

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/ag-ui/agent_framework_ag_ui
   _agent_run.py4348680%148–155, 194–195, 202, 299, 311, 315, 317–318, 334, 361–362, 417–419, 431–433, 537, 542–543, 545, 649, 651–652, 705, 720–721, 728, 777–779, 796, 802, 810, 812, 820, 850–856, 859–862, 864–873, 876, 884–887, 894, 897–898, 903, 909–911, 915, 920–924, 950–952
TOTAL21837276787% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
4562 244 💤 0 ❌ 0 🔥 1m 19s ⏱️

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes AG-UI human-in-the-loop approval payloads being re-processed on subsequent turns by cleaning approval JSON payloads out of the outgoing snapshot and replacing them with the executed tool result, preventing duplicate tool invocations when CopilotKit replays history.

Changes:

  • Add _clean_resolved_approvals_from_snapshot to rewrite tool-role snapshot messages containing approval payloads into the corresponding tool result.
  • Invoke snapshot cleaning after _resolve_approval_responses in run_agent_stream as a defense-in-depth measure.
  • Add regression tests covering snapshot cleaning, “no-op” behavior when no approvals exist, prevention of stale approval reprocessing, and ensuring fresh approvals still get processed.

Reviewed changes

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

File Description
python/packages/ag-ui/agent_framework_ag_ui/_agent_run.py Adds snapshot-cleaning logic and calls it after approval resolution to prevent stale approvals from being replayed.
python/packages/ag-ui/tests/ag_ui/test_message_hygiene.py Adds tests validating snapshot cleaning and that cleaned snapshots don’t re-trigger approvals on later turns.
python/packages/ag-ui/tests/ag_ui/test_message_adapters.py Adds a test ensuring a fresh approval still produces a function_approval_response.

@moonbox3 moonbox3 enabled auto-merge February 26, 2026 01:52
@moonbox3 moonbox3 added this pull request to the merge queue Feb 26, 2026
Merged via the queue into microsoft:main with commit cfaa0c6 Feb 26, 2026
30 checks passed
@github-project-automation github-project-automation bot moved this from In Review to Done in Agent Framework Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Python: [AG-UI]Tool execution repeats after Human-in-the-loop approval

5 participants