Python: Fix Magentic manager duplicating conversation history#6297
Open
ghominejad wants to merge 1 commit into
Open
Python: Fix Magentic manager duplicating conversation history#6297ghominejad wants to merge 1 commit into
ghominejad wants to merge 1 commit into
Conversation
_complete() reused one persistent AgentSession, so the default history provider re-injected prior turns on top of the full prompt the manager already rebuilds each call — duplicating task/facts/plan and compounding every round. Use a fresh session per call; keep self._session only for checkpointing. GroupChatOrchestrator is unaffected. Add a regression test and update the session-propagation test.
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR updates StandardMagenticManager to avoid duplicated prompt/history injection by using a fresh AgentSession for each _complete() call, and adds regression coverage to ensure the manager doesn’t resend already-sent turns.
Changes:
- Switch
_complete()to create a new session per call (instead of reusingself._session) to prevent history duplication. - Update existing session assertions to require non-
Nonesessions while enforcing “fresh session per call”. - Add a new regression test that inspects the exact message list sent to the chat client and asserts prompts aren’t duplicated.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| python/packages/orchestrations/tests/test_magentic.py | Adds/adjusts regressions tests validating non-duplicated history and fresh-per-call sessions. |
| python/packages/orchestrations/agent_framework_orchestrations/_magentic.py | Changes manager _complete() to use a new session each call; updates inline documentation around session usage. |
Comment on lines
+1213
to
+1216
| async def _get() -> ChatResponse: | ||
| return ChatResponse(messages=Message(role="assistant", contents=["recorded"])) | ||
|
|
||
| return _get() |
Comment on lines
+1198
to
+1199
| facts_marker = ORCHESTRATOR_TASK_LEDGER_FACTS_PROMPT.split("{")[0].strip() | ||
| plan_marker = ORCHESTRATOR_TASK_LEDGER_PLAN_PROMPT.split("{")[0].strip() |
Comment on lines
571
to
575
| self._agent: SupportsAgentRun = agent | ||
| # Retained only for checkpoint save/restore continuity. LLM calls in `_complete` | ||
| # use a fresh session each time so the agent's history provider cannot re-inject | ||
| # (and thereby duplicate) the conversation the manager already passes in full. | ||
| self._session: AgentSession = self._agent.create_session() |
| providers configured on the manager agent are still invoked (regression #4371). | ||
| """ | ||
| response: AgentResponse = await self._agent.run(messages, session=self._session) | ||
| response: AgentResponse = await self._agent.run(messages, session=self._agent.create_session()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #6298
Motivation and Context
StandardMagenticManager._complete()ran every manager model call against a single persistentAgentSession(self._session). For local sessions the defaultInMemoryHistoryProviderreloads and prepends previously stored input/response messages on each call. But the manager already rebuilds the complete prompt it wants the model to see on every call ([*magentic_context.chat_history, ...]inplan,replan, andcreate_progress_ledger). The two history mechanisms stacked, so the task/facts/plan were re-sent and compounded every round — wasting tokens and degrading the manager's context with duplicate turns.GroupChatOrchestratordoes not have this problem: it deliberately keeps history in the persistent session and passes only the new delta per call, so it's left unchanged.Description
StandardMagenticManager._complete()now creates a fresh session per call (session=self._agent.create_session()) instead of reusingself._session. This keeps each call stateless — matching the manager's design of supplying the full prompt itself — while still passing a non-Nonesession so context providers configured on the manager agent are invoked (preserving the intent of Python: [Bug]: StandardMagenticManager does not propagate session to manager agent, silently breaking context providers (e.g. RedisHistoryProvider) #4371).self._sessionis retained solely for checkpoint save/restore continuity.packages/orchestrations/tests/test_magentic.py):test_standard_manager_does_not_duplicate_history, which drives a realAgentthrough the real session machinery with a recording chat client and asserts the facts pre-survey reaches the model exactly once per call. It fails on the previous code (2 == 1) and passes with the fix.test_standard_manager_propagates_session_to_agent: still asserts a session is propagated (notNone) on each call, but now asserts each call uses a fresh session rather than one shared, accumulating instance.test_magentic.pyandtest_group_chat.pysuites pass.Contribution Checklist