Python: Fix StandardMagenticManager to propagate session to manager agent#4409
Python: Fix StandardMagenticManager to propagate session to manager agent#4409moonbox3 merged 7 commits intomicrosoft:mainfrom
Conversation
…enticManager StandardMagenticManager._complete() was calling self._agent.run(messages) without passing a session. This caused context providers (e.g. RedisHistoryProvider) configured on the manager agent to silently fail, as each call created a new ephemeral session with a different session_id. Changes: - Create an AgentSession in StandardMagenticManager.__init__() - Pass session=self._session in _complete() calls to agent.run() - Persist/restore the session in checkpoint save/restore methods - Add regression tests for session propagation and checkpoint round-trip Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
moonbox3
left a comment
There was a problem hiding this comment.
Automated Code Review
Reviewers: 3 | Confidence: 77%
✓ Correctness
The diff correctly adds AgentSession propagation to StandardMagenticManager—creating a session in init, passing it through to agent.run(), and handling checkpoint save/restore. The implementation is consistent with existing patterns (e.g., task_ledger checkpoint handling). The two new async test functions are missing @pytest.mark.asyncio decorators, which will cause them to be silently skipped (or collected as warnings) unless the project has asyncio_mode='auto' configured globally. Additionally, test_standard_manager_checkpoint_preserves_session is declared async but never awaits anything.
✓ Security Reliability
The diff adds AgentSession lifecycle management to StandardMagenticManager, including checkpoint serialization/deserialization. The implementation follows existing patterns in the codebase. No critical security vulnerabilities were found. The deserialization path uses from_dict (not pickle), which is safe. The broad exception catch on restore is consistent with the adjacent task_ledger handling. One minor reliability concern: the truthiness check on the restored payload could silently skip restoration for edge-case falsy values.
✗ Test Coverage
Two new async tests cover session propagation and checkpoint round-tripping, with strong identity/value assertions. However, both async tests lack @pytest.mark.asyncio (a problem if the project does not use asyncio_mode='auto'), backward-compatible checkpoint restore (state without 'agent_session') is untested, and the AgentSession import may be missing from the test module.
Blocking Issues
- Both new async test functions lack the @pytest.mark.asyncio decorator. If the project's pytest config does not set asyncio_mode = 'auto', these tests will silently pass without executing any assertions, giving false confidence. Verify the pytest configuration, and if auto mode is not enabled, add the decorator.
Suggestions
- The two new test functions are
async defbut lack@pytest.mark.asynciodecorators. If the project does not useasyncio_mode = "auto", these tests will be silently skipped. Verify the pytest-asyncio configuration or add the decorators explicitly. - test_standard_manager_checkpoint_preserves_session does not await anything—it could be a plain synchronous test function, which would make it run regardless of asyncio configuration.
- Consider using
if session_payload is not None:instead ofif session_payload:to guard against edge cases whereAgentSession.to_dict()might return a valid but falsy value (e.g., an empty dict). The current truthiness check would silently skip restoration in that scenario, leaving the manager with a fresh session and no warning logged. - Add a test that restores a checkpoint saved without 'agent_session' (backward compatibility). The production code already handles this via
state.get(), but there is no test proving the old checkpoint format still works. - Confirm that
AgentSessionis already imported in test_magentic.py; the diff does not show it being added to the test file's imports, yet the type annotationlist[AgentSession | None]on line 1084 depends on it. - Consider a test that calls
_complete(orplan) afteron_checkpoint_restoreto verify the restored session is actually propagated to subsequent agent.run() calls, not just that thesession_idmatches.
Automated review by moonbox3's agents
python/packages/orchestrations/agent_framework_orchestrations/_magentic.py
Show resolved
Hide resolved
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug in the Python Magentic orchestration where StandardMagenticManager invoked its underlying manager agent without an AgentSession, preventing session-scoped context providers (e.g., history providers) from working correctly.
Changes:
- Create a persistent
AgentSessioninStandardMagenticManagerand pass it to every internalagent.run(...)call. - Persist/restore the manager’s session via checkpoint save/restore.
- Add regression tests validating session propagation and checkpoint preservation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| python/packages/orchestrations/agent_framework_orchestrations/_magentic.py | Create/reuse an AgentSession for manager-agent runs and include it in checkpoint state. |
| python/packages/orchestrations/tests/test_magentic.py | Add tests ensuring session is propagated consistently and survives checkpoint restore. |
…in tests Address PR review feedback: add # type: ignore[reportPrivateUsage] comments to _session attribute accesses in the new regression tests, matching the existing convention used elsewhere in test_magentic.py (e.g., lines 401-406). The @pytest.mark.asyncio decorator is not needed because pyproject.toml sets asyncio_mode = "auto". Fixes microsoft#4371 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…rosoft#4371) Replace direct mgr._session access with getattr(mgr, "_session") to avoid reportPrivateUsage type-checking warnings without needing type: ignore comments. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ess (microsoft#4371) - Use 'is not None' instead of truthiness check for session_payload restore - Use getattr() for private _session attribute access in tests - Add backward-compatibility test for on_checkpoint_restore with empty state Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…crosoft#4409) Tests that never await anything don't need to be async. Using plain def ensures they always run regardless of pytest-asyncio configuration. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Motivation and Context
StandardMagenticManagerdid not create or pass anAgentSessionwhen calling its internal agent'srun()method. This caused context providers (e.g.RedisHistoryProvider) configured on the manager agent to silently fail, since they rely on a session to scope and retrieve conversation history.Fixes #4371
Description
The root cause was that
StandardMagenticManager._complete()calledself._agent.run(messages)without asessionargument, so context providers never received the session they need to function. The fix creates a persistentAgentSessionduring manager initialization and passes it on everyrun()call. Additionally, the session is included in checkpoint save/restore so it survives orchestration snapshots.Contribution Checklist