Skip to content

Python: Reset Magentic participant sessions on replan - #9131

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
microsoft:mainfrom
Aditya-XR:fix/magentic-reset-session
Oct 12, 2026
Merged

Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
microsoft:mainfrom
Aditya-XR:fix/magentic-reset-session

Conversation

@Aditya-XR

Copy link
Copy Markdown
Contributor

Motivation & Context

When a Magentic workflow stalls and replans, participant agents are supposed to start fresh. Instead they keep their whole earlier conversation. MagenticAgentExecutor.handle_magentic_reset assigns the new session to self._agent_thread, but AgentExecutor has run the agent with self._session since #3850, so the new session is never used and the old history is sent again after every reset.

Description & Review Guide

  • What are the major changes?

    In handle_magentic_reset:

             self._pending_agent_requests.clear()
             self._pending_responses_to_agent.clear()
    +        self._pending_request_order.clear()
             # Reset sessions
    -        self._agent_thread = self._agent.create_session()
    +        self._session = self._agent.create_session()

    _pending_request_order is cleared along with the other pending-request state the handler already resets, so it can't keep IDs of requests that no longer exist.

    The new test test_magentic_reset_gives_participants_a_fresh_session drives a real Agent through a scripted stall, reset and replan, and checks what the agent sends to the model after the reset. Without the change it fails:

    E   AssertionError: agentA kept its pre-reset session history
    E   assert ['before reset', 'reply-1', 'after reset'] == ['after reset']
    
  • What is the impact of these changes?

    After a reset, each participant gets a new session, as the class and handler docstrings describe. For the repro in the linked issue, the second call is now ['after reset'] instead of ['before reset', 'reply-1', 'after reset']. Nothing changes for workflows that never reset.

  • What do you want reviewers to focus on?

    Whether clearing _pending_request_order belongs here. I added it because the handler already clears _pending_agent_requests and _pending_responses_to_agent, and the order list only tracks IDs from those. I can drop it to keep the change to the session line only.

Related Issue

Fixes #9129

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.

MagenticAgentExecutor.handle_magentic_reset assigned the new session to
_agent_thread, an attribute nothing reads since the AgentExecutor
session attribute was renamed to _session, so participants kept their
whole conversation across every reset. Assign _session instead and
also clear _pending_request_order with the other pending request state.

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 reached their quota limit.

@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Oct 7, 2026
@eavanvalkenburg

Copy link
Copy Markdown
Member

/review

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

MAF Automated Review — Iteration 1

Result: No findings
Scope: full PR (1 commit(s)): 1bed6c084b12
Model: gpt-5.6-sol

Overview

The change restores the documented fresh-session invariant by resetting the _session field actually used by both streaming and non-streaming agent runs. It also clears all three correlated pending-request structures, and the end-to-end regression test demonstrates that pre-reset history no longer reaches a participant after replanning. No concrete Critical, High, or Medium regression remains after reconciling the reset lifecycle, checkpoint behavior, provider contracts, and existing safeguards.

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.

Merged via the queue into microsoft:main with commit 60bc6ef Oct 12, 2026
49 of 50 checks passed

This branch was previously deployed

1 inactive deployment
github-app-auth — 1bed6c08 Deployed Oct 7, 2026 by Aditya-XR via team_check #6185
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]: Magentic reset does not reset participant sessions, so agents keep their history across replans

3 participants