Python: Core: notify agent of external AgentModeProvider mode changes#5650
Open
eavanvalkenburg wants to merge 1 commit intomicrosoft:mainfrom
Open
Python: Core: notify agent of external AgentModeProvider mode changes#5650eavanvalkenburg wants to merge 1 commit intomicrosoft:mainfrom
eavanvalkenburg wants to merge 1 commit intomicrosoft:mainfrom
Conversation
When the operating mode is changed externally (e.g. via a slash-command handler calling set_agent_mode), the agent's chat history still shows the prior set_mode tool call near the end. Updating only the system instructions is insufficient — models tend to anchor on the recent tool call and ignore the new mode. Mirror the .NET AgentModeProvider behavior: when set_agent_mode detects an actual mode change, record the previous mode in provider state. On the next before_run, the provider pops that flag and injects a user-role notification message announcing the switch, so the most recent context unambiguously reflects the current mode. The agent-driven set_mode tool path bypasses this so it does not trigger a redundant notification on its own change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the Python AgentModeProvider (Agent Harness) so that externally-triggered mode changes (e.g., via set_agent_mode from a slash-command handler) are surfaced to the model as a fresh user-role notification message on the next run, preventing the model from anchoring on a recent set_mode tool call in the prior chat history.
Changes:
- Track external mode transitions by recording the previous mode in session state when
set_agent_modeactually changes the mode. - Inject a one-time
usernotification message during the nextAgentModeProvider.before_run, then clear the marker so it won’t repeat. - Add unit tests validating the state marker behavior, one-time injection, and that agent-driven
set_modedoes not queue notifications.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| python/packages/core/agent_framework/_harness/_mode.py | Records previous mode for external changes and injects a one-time user notification message on the next run; agent tool path bypasses the notification marker. |
| python/packages/core/tests/core/test_harness_mode.py | Adds tests covering the external-change marker, no-op behavior, single injection + clearing, and no reinjection on subsequent runs. |
westey-m
approved these changes
May 5, 2026
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.
Description
Follow-up to #5611 (already merged) that addresses reviewer feedback about external mode changes confusing the agent.
When the user changes the mode externally (e.g. via a slash-command handler calling
set_agent_mode), the chat history still shows the agent's priorset_modetool call near the end. Updating only the system instructions wasn't enough — models tend to anchor on the recent tool call and ignore the new mode. The reviewer's example:The agent typically thought it was still in execute mode.
This PR aligns with the existing .NET
AgentModeProviderbehavior:set_agent_mode(the public helper used by external callers) now records the previous mode in provider state when the mode actually changes.before_run, the provider pops that flag and injects auser-role notification message into the context (Mode changed: ... switched from "X" to "Y". You must now adjust your behavior...), so the most recent context unambiguously reflects the current mode.set_modetool bypasses this — it writescurrent_modedirectly so it doesn't trigger a redundant self-notification.Contribution Checklist
Tests
5 new tests in
test_harness_mode.py:set_modetool does not queue a notification.