Skip to content

Python: Fix AG-UI tool history replay sanitization  - #6581

Merged
moonbox3 merged 2 commits into
microsoft:mainfrom
moonbox3:moonbox3/ag-ui-tool-history-replay
Jun 19, 2026
Merged

Python: Fix AG-UI tool history replay sanitization #6581
moonbox3 merged 2 commits into
microsoft:mainfrom
moonbox3:moonbox3/ag-ui-tool-history-replay

Conversation

@moonbox3

Copy link
Copy Markdown
Contributor

Motivation & Context

AG-UI replay can reconstruct assistant/tool histories that violate provider message contracts, causing OpenAI/Azure OpenAI requests to fail when assistant tool_calls are not followed by matching tool result messages.

This change keeps replayed AG-UI history provider-valid for interrupted or incomplete tool-call sequences, and adds coverage for approved HITL tool results being persisted into MESSAGES_SNAPSHOT so the next replay does not re-send raw approval payloads.

Description & Review Guide

Related Issue

Fixes #5855
Fixes #6513

No other open PR was found for these issues.

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 review requested due to automatic review settings June 18, 2026 06:13
@moonbox3 moonbox3 self-assigned this Jun 18, 2026
@moonbox3 moonbox3 added ui integration python Usage: [Issues, PRs], Target: Python labels Jun 18, 2026
@github-actions

github-actions Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/ag-ui/agent_framework_ag_ui
   _message_adapters.py6084892%148–149, 158–160, 163, 166–170, 172–177, 180, 189–191, 194–197, 232, 397, 518–520, 574, 577, 579, 582, 585, 601, 618, 640, 740, 756–757, 828, 850, 920, 955–956, 1024, 1067
TOTAL39875449788% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
7990 34 💤 0 ❌ 0 🔥 2m 9s ⏱️

@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.

Automated Code Review

Reviewers: 5 | Confidence: 90% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Failure Modes, Design Approach


Automated review by moonbox3's agents

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.

Pull request overview

This PR hardens the Python AG-UI message replay path so outbound provider messages remain contract-valid when tool-call sequences are interrupted, incomplete, or interleaved, and adds regression coverage for HITL approval replay so approved tool outputs are persisted for subsequent replays.

Changes:

  • Extend _sanitize_tool_history to deterministically close pending assistant tool calls by injecting synthetic tool results before later assistant messages, non-standard roles, or end-of-history.
  • Preserve tool-call ordering when injecting synthetic results by tracking pending tool call ids in-order.
  • Add tests covering consecutive assistant tool-call messages, histories ending with unresolved tool calls, and HITL approval snapshots replaying as tool results (not raw approval payloads).

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/_message_adapters.py Refactors and extends tool-history sanitization to inject synthetic tool results at more replay boundary points, using ordered pending call tracking.
python/packages/ag-ui/tests/ag_ui/test_message_adapters.py Adds regression tests for consecutive assistant tool-call messages, end-of-history pending tool calls, and updated behavior for non-standard roles.
python/packages/ag-ui/tests/ag_ui/test_approval_result_event.py Adds coverage ensuring approved HITL tool executions are persisted into MESSAGES_SNAPSHOT for replay, and that replay normalizes to provider-valid function_result content.

Comment thread python/packages/ag-ui/agent_framework_ag_ui/_message_adapters.py Outdated
Comment thread python/packages/ag-ui/tests/ag_ui/test_message_adapters.py
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@moonbox3
moonbox3 merged commit 5145d50 into microsoft:main Jun 19, 2026
37 checks passed
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

3 participants