Skip to content

Python: Fix Foundry reasoning MCP compaction#6907

Merged
moonbox3 merged 3 commits into
microsoft:mainfrom
godququ5-code:godququ5/issue-6883
Jul 9, 2026
Merged

Python: Fix Foundry reasoning MCP compaction#6907
moonbox3 merged 3 commits into
microsoft:mainfrom
godququ5-code:godququ5/issue-6883

Conversation

@godququ5-code

@godququ5-code godququ5-code commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Preserve Foundry reasoning output as text_reasoning with the provider reasoning id when reconstructing response history.
  • Treat mcp_server_tool_call as a tool-call assistant item during compaction so reasoning + MCP call pairs stay atomic.
  • Add focused regressions for both Foundry reasoning reconstruction paths and sliding-window MCP compaction.

Fixes #6883

Root cause

Foundry response reconstruction converted reasoning summaries into ordinary text content, dropping the reasoning id. Core compaction also only recognized function_call assistant messages as tool-call items, so an MCP call following a reasoning item could be grouped separately and survive after its reasoning prefix was excluded.

Test plan

  • uv run pytest packages/core/tests/core/test_compaction.py packages/foundry_hosting/tests/test_responses.py -q
  • uv run prek run --files packages/core/agent_framework/_compaction.py packages/core/tests/core/test_compaction.py packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py packages/foundry_hosting/tests/test_responses.py

Note: the pytest command exits 0, but the local environment emits unrelated OpenTelemetry exporter shutdown noise against 127.0.0.1:7897 after the tests finish.

Copilot AI review requested due to automatic review settings July 3, 2026 19:40
@giles17 giles17 added the python Usage: [Issues, PRs], Target: Python label Jul 3, 2026
@github-actions github-actions Bot changed the title Fix Foundry reasoning MCP compaction Python: Fix Foundry reasoning MCP compaction Jul 3, 2026

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

Fixes Python response-history reconstruction and core compaction behavior so Foundry reasoning items and subsequent MCP tool calls remain correctly paired and valid for downstream APIs (avoiding “orphaned mcp_call” 400s in multi-turn reasoning flows).

Changes:

  • Preserve Foundry reasoning summaries as text_reasoning content (instead of plain text) while retaining the provider reasoning id.
  • Treat mcp_server_tool_call assistant messages as tool-call items during compaction grouping so reasoning + MCP call pairs stay atomic.
  • Add targeted regression tests covering both Foundry reasoning reconstruction paths and sliding-window MCP compaction grouping.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py Reconstruct reasoning summaries as text_reasoning while preserving reasoning IDs.
python/packages/foundry_hosting/tests/test_responses.py Adds assertions ensuring reconstructed reasoning content type and ID are preserved.
python/packages/core/agent_framework/_compaction.py Expands tool-call detection to include MCP server tool calls for proper grouping/compaction.
python/packages/core/tests/core/test_compaction.py Adds regression ensuring sliding-window compaction keeps reasoning + MCP calls in the same group.

@godququ5-code

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@godququ5-code
godququ5-code marked this pull request as ready for review July 3, 2026 19:54
@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/anthropic/agent_framework_anthropic
   _chat_client.py5173792%467, 470, 551, 668, 680, 768, 802, 892, 931–932, 1010, 1012, 1042–1043, 1088, 1104–1105, 1112–1114, 1118–1120, 1124–1127, 1244, 1254, 1306, 1454–1455, 1472, 1485, 1498, 1523–1524
packages/core/agent_framework
   _compaction.py7026091%117–118, 126, 193–194, 212–213, 230, 264, 278, 285, 300–301, 355, 362, 378, 428, 452, 483, 530, 536, 538, 557, 601–606, 618, 690, 692, 707, 751, 813, 939, 945–949, 952–954, 962, 1037, 1039, 1055, 1062, 1067, 1082, 1090, 1184, 1187, 1205, 1221, 1317, 1344, 1349, 1428
packages/foundry_hosting/agent_framework_foundry_hosting
   _responses.py8169088%186–189, 276, 281, 305, 360–361, 375, 378–379, 427–428, 438, 483, 515–517, 520–521, 532, 575, 589, 664, 667, 673, 675, 747, 834–835, 1146, 1159, 1656–1658, 1660, 1707–1708, 1710–1711, 1713–1714, 1716–1717, 1722, 1731, 1734–1736, 1738, 1748, 1752, 1765, 1810–1811, 1813, 1818–1822, 1824, 1831–1832, 1834–1835, 1841, 1843–1847, 1854, 1860, 1882, 1888, 1894, 1896, 1898–1901, 1909, 1911, 1962–1964, 1974–1975
packages/openai/agent_framework_openai
   _chat_client.py127712790%307, 320, 670–674, 682–685, 691–695, 745–752, 754–756, 763–765, 823, 831, 854, 972, 1071, 1130, 1132, 1134, 1136, 1202, 1216, 1296, 1306, 1311, 1354, 1465–1466, 1481, 1705, 1710, 1714–1716, 1720–1721, 1804, 1814, 1841, 1847, 1857, 1863, 1868, 1874, 1879–1880, 1960, 2004, 2007–2010, 2024, 2026, 2034–2035, 2047, 2089, 2154, 2171, 2174, 2201–2203, 2242, 2259, 2262, 2324, 2331, 2355–2356, 2391, 2429–2430, 2448–2449, 2492, 2619–2620, 2638, 2721–2729, 2759, 2904, 2919, 2968–2971, 2981–2983, 3010–3012, 3022–3023, 3029, 3044, 3166–3167
   _chat_completion_client.py3731895%440, 536–537, 541, 782, 784, 789, 792, 883, 885, 902, 923, 931, 955, 968, 992, 1012, 1327
TOTAL45087541887% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
8912 33 💤 0 ❌ 0 🔥 2m 23s ⏱️

Comment thread python/packages/core/agent_framework/_compaction.py
Comment thread python/packages/core/agent_framework/_compaction.py
@godququ5-code
godququ5-code force-pushed the godququ5/issue-6883 branch from 1ddcc09 to e7b8f93 Compare July 7, 2026 13:25
@moonbox3
moonbox3 enabled auto-merge July 9, 2026 05:15
@moonbox3
moonbox3 added this pull request to the merge queue Jul 9, 2026
Merged via the queue into microsoft:main with commit 978cfcd Jul 9, 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

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Orphaned mcp_call items cause HTTP 400 in multi-turn reasoning model flows

5 participants