Skip to content

Python: Fix StandardMagenticManager to propagate session to manager agent#4409

Merged
moonbox3 merged 7 commits intomicrosoft:mainfrom
moonbox3:agent/fix-4371-1
Mar 3, 2026
Merged

Python: Fix StandardMagenticManager to propagate session to manager agent#4409
moonbox3 merged 7 commits intomicrosoft:mainfrom
moonbox3:agent/fix-4371-1

Conversation

@moonbox3
Copy link
Contributor

@moonbox3 moonbox3 commented Mar 3, 2026

Motivation and Context

StandardMagenticManager did not create or pass an AgentSession when calling its internal agent's run() 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() called self._agent.run(messages) without a session argument, so context providers never received the session they need to function. The fix creates a persistent AgentSession during manager initialization and passes it on every run() call. Additionally, the session is included in checkpoint save/restore so it survives orchestration snapshots.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

Note: PR autogenerated by moonbox3's agent

…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>
Copilot AI review requested due to automatic review settings March 3, 2026 04:15
Copy link
Contributor Author

@moonbox3 moonbox3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 def but lack @pytest.mark.asyncio decorators. If the project does not use asyncio_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 of if session_payload: to guard against edge cases where AgentSession.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 AgentSession is already imported in test_magentic.py; the diff does not show it being added to the test file's imports, yet the type annotation list[AgentSession | None] on line 1084 depends on it.
  • Consider a test that calls _complete (or plan) after on_checkpoint_restore to verify the restored session is actually propagated to subsequent agent.run() calls, not just that the session_id matches.

Automated review by moonbox3's agents

@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented Mar 3, 2026

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/orchestrations/agent_framework_orchestrations
   _magentic.py5919184%64–73, 78, 82–93, 258, 269, 273, 293, 354, 363, 365, 407, 424, 433–434, 436–438, 440, 451, 594, 596, 636, 684, 720–722, 724, 734, 742–743, 810–813, 904, 910, 916, 958, 996, 1028, 1045, 1056, 1111–1112, 1116–1118, 1142, 1166–1167, 1180, 1196, 1218, 1266–1267, 1305–1306, 1469, 1472, 1481, 1484, 1489, 1540–1541, 1582–1583, 1631, 1661, 1719, 1733, 1744
TOTAL22259275287% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
4728 247 💤 0 ❌ 0 🔥 1m 18s ⏱️

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 AgentSession in StandardMagenticManager and pass it to every internal agent.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.

moonbox3 and others added 6 commits March 3, 2026 13:20
…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>
@moonbox3 moonbox3 self-assigned this Mar 3, 2026
@moonbox3 moonbox3 added the agent orchestration Issues related to agent orchestration label Mar 3, 2026
@moonbox3 moonbox3 enabled auto-merge March 3, 2026 09:54
@moonbox3 moonbox3 added this pull request to the merge queue Mar 3, 2026
Merged via the queue into microsoft:main with commit c5ed820 Mar 3, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent orchestration Issues related to agent orchestration python

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: StandardMagenticManager does not propagate session to manager agent, silently breaking context providers (e.g. RedisHistoryProvider)

5 participants