Skip to content

Python: preserve hosted tool results in ToolResultCompactionStrategy - #9887

Open
Kip (uragoodman) wants to merge 1 commit into
microsoft:mainfrom
uragoodman:fix/tool-result-compaction
Open

Kip (uragoodman) wants to merge 1 commit into
microsoft:mainfrom
uragoodman:fix/tool-result-compaction

Conversation

@uragoodman

Copy link
Copy Markdown

Motivation & Context

ToolResultCompactionStrategy collapses old tool-call groups into a short
summary, but its result extraction only handles function_result,
mcp_server_tool_result, and search_tool_result. Groups containing
shell_tool_result, code_interpreter_tool_result,
image_generation_tool_result, or computer_tool_result are still excluded,
yet their summary says [Tool results: no results]. The tool output is silently
lost from the compacted conversation.

This is reachable through ContextWindowCompactionStrategy, which uses
ToolResultCompactionStrategy internally, as well as by callers using the
strategy directly.

Based on upstream main at commit 4d4e216 (agent-framework-core 1.24.0).

Fixes #9886

The issue was filed with a complete reproduction. This PR is submitted
concurrently for early direction feedback; happy to adjust the approach or wait
if maintainers prefer to discuss the direction first.

Description & Review Guide

What are the major changes?

  • Add result extraction for the four hosted result types in
    ToolResultCompactionStrategy.
  • Add _hosted_tool_result_summary to render:
    • shell stdout/stderr/exit code/timed-out state;
    • code interpreter text, error, and status output;
    • image generation and computer results without embedding data URIs or
      base64 payloads;
    • an explicit no output returned fallback instead of no results when
      there is genuinely no result text.
  • Add regression tests covering all four types, status-only/timeout cases,
    mixed hosted results, multiple outputs/errors, original-message exclusion,
    and payload exclusion.

What is the impact of these changes?

Only older tool-call groups containing these hosted result types change. The
function, MCP, and search paths are unchanged. No public API changes. Hosted
image payloads are represented by metadata (media_type, file_id, or
screenshot) rather than copied into the summary.

This PR intentionally scopes itself to ToolResultCompactionStrategy.
SummarizationStrategy has a related rendering gap and is left for a separate
follow-up.

What do you want reviewers to focus on?

  • Whether labeling hosted results by content type (rather than call/tool name)
    is acceptable, since these content types have no separate tool name.
  • Whether the metadata-only rendering for image/computer results and the
    explicit no output returned fallback are the right level of detail.

AI Assistance

  • This is an AI-assisted contribution. I reviewed, understood, and verified all submitted content and accept responsibility for it.

AI assistance details: an AI coding assistant helped with code-path analysis,
implementation, tests, and drafting this PR description. I reviewed and
verified the change and ran the focused tests below.

Contribution Checklist

  • The changed package imports cleanly and the focused compaction suite passes:
    pytest tests/core/test_compaction.py -q — 168 passed on Python 3.12.16.
  • New regression tests were added and pass.
  • ruff check agent_framework/_compaction.py tests/core/test_compaction.py
    passes with the repository-pinned Ruff 0.17.1.
  • Full workspace build and the full unit-test suite were not run locally; CI will run them.
  • This is not a breaking change.
  • No ready-for-implementation label was present when this PR was opened; it is submitted for early direction feedback.

Handle shell, code interpreter, image generation, and computer tool results
when collapsing old tool-call groups. Preserve status and timeout information,
avoid embedding image payloads, and fall back to an explicit "no output
returned" instead of "no results".

Add regression tests for all four result types, status-only and timeout cases,
mixed hosted results, multiple outputs/errors, and payload exclusion.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@uragoodman

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

This branch was successfully deployed

No deployments
github-app-auth — 594cac80 Deployed Oct 30, 2026 by uragoodman via team_check #8144
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]: ToolResultCompactionStrategy drops hosted tool results during compaction

2 participants