Skip to content

use proper state loading condition#460

Merged
kylerohn-msft merged 12 commits into
mainfrom
users/kylerohn/fix-state-load-condition
Jul 13, 2026
Merged

use proper state loading condition#460
kylerohn-msft merged 12 commits into
mainfrom
users/kylerohn/fix-state-load-condition

Conversation

@kylerohn-msft

@kylerohn-msft kylerohn-msft commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

fixes #440 using same design pattern as Agents-for-net

This pull request refactors the state loading logic in the agent state management code to improve cache handling and test clarity. The main changes introduce a helper method to determine when state should be loaded from storage, and update tests to use the new cache access pattern.

Agent state loading improvements:

  • Added a private _should_load method to encapsulate the logic for when the agent state should be loaded from storage, improving readability and maintainability. The load method now uses this helper instead of directly checking the cache.

Testing enhancements:

  • Updated the test for cached state hash computation to explicitly retrieve the cached state using the get_cached_state method after update, ensuring tests reflect the current cache access pattern.

Also removes a module which misuses the state loading/saving design pattern, and tests which should have already been deleted

Copilot AI review requested due to automatic review settings July 10, 2026 18:11
@kylerohn-msft kylerohn-msft requested a review from a team as a code owner July 10, 2026 18:11

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.

Pull request overview

This PR refactors AgentState.load() to centralize the “should we read from storage?” decision into a new _should_load() helper, with the goal of preventing unnecessary storage reads and aligning the caching pattern with the .NET SDK (per issue #440).

Changes:

  • Replaced the inline load() guard (force or not self._cached_state) with a new _should_load() helper.
  • Updated the load condition to consult the turn-scoped cached state (turn_context.turn_state) and to additionally consider “empty cached state”.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings July 10, 2026 18:56

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

@kylerohn-msft kylerohn-msft marked this pull request as draft July 10, 2026 19:17
Copilot AI review requested due to automatic review settings July 10, 2026 22:34

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.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 9 comments.

Comment thread tests/hosting_dialogs/test_dialog_extensions.py
Comment thread tests/hosting_dialogs/memory/scopes/test_memory_scopes.py Outdated
Comment thread tests/hosting_dialogs/memory/scopes/test_memory_scopes.py Outdated
Comment thread dev/hosting_dialogs/memory/scopes/test_memory_scopes.py Outdated
Comment thread dev/hosting_dialogs/memory/scopes/test_memory_scopes.py Outdated
Copilot AI review requested due to automatic review settings July 10, 2026 22:38

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.

Pull request overview

Copilot reviewed 41 out of 45 changed files in this pull request and generated 4 comments.

Comments suppressed due to low confidence (2)

dev/hosting_dialogs/memory/scopes/test_memory_scopes.py:216

  • Calling ConversationState.save() here is a no-op because the state has not been loaded/initialized yet (AgentState._cached_state is still None). If the intent is to ensure the ConversationState cache exists for this turn, call load() instead.
    dev/hosting_dialogs/memory/scopes/test_memory_scopes.py:288
  • Calling ConversationState.save() here is a no-op because the state has not been loaded/initialized yet (AgentState._cached_state is still None). If the intent is to ensure the ConversationState cache exists for this turn, call load() instead.

Comment thread tests/hosting_dialogs/test_dialog_extensions.py Outdated
Comment thread tests/hosting_dialogs/memory/scopes/test_memory_scopes.py Outdated
Comment thread tests/hosting_dialogs/memory/scopes/test_memory_scopes.py Outdated
Copilot AI review requested due to automatic review settings July 10, 2026 22:44

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.

Pull request overview

Copilot reviewed 41 out of 45 changed files in this pull request and generated 5 comments.

Comment thread tests/hosting_dialogs/test_dialog_extensions.py
Comment thread tests/hosting_dialogs/memory/scopes/test_memory_scopes.py Outdated
Comment thread tests/hosting_dialogs/memory/scopes/test_memory_scopes.py Outdated
Copilot AI review requested due to automatic review settings July 10, 2026 22:52

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.

Pull request overview

Copilot reviewed 41 out of 45 changed files in this pull request and generated 3 comments.

Comment thread tests/hosting_dialogs/test_dialog_extensions.py
Copilot AI review requested due to automatic review settings July 10, 2026 22:57

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.

Pull request overview

Copilot reviewed 41 out of 45 changed files in this pull request and generated 3 comments.

Copilot AI review requested due to automatic review settings July 10, 2026 23:02

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.

Pull request overview

Copilot reviewed 41 out of 45 changed files in this pull request and generated 1 comment.

Copilot AI review requested due to automatic review settings July 10, 2026 23:51

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.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/state/agent_state.py:269

  • delete_value() checks self._cached_state.state.get(property_name) before deleting. This fails to delete keys whose stored value is falsy (e.g., False/0/empty string), even though the property exists. Use membership (property_name in ...) instead of truthiness.
        if not property_name:
            raise TypeError(
                "AgentState.delete_value(): property_name cannot be None or empty."
            )

        if self._cached_state.state.get(property_name):
            del self._cached_state.state[property_name]

Comment thread libraries/microsoft-agents-hosting-dialogs/readme.md
Copilot AI review requested due to automatic review settings July 11, 2026 00:09

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.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.

Comments suppressed due to low confidence (1)

libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/state/agent_state.py:269

  • delete_value() uses self._cached_state.state.get(property_name) as the existence check. That fails to delete entries whose stored value is falsey (e.g., 0, "", False), leaving the key in state unexpectedly.
                "AgentState.delete_value(): property_name cannot be None or empty."
            )

        if self._cached_state.state.get(property_name):
            del self._cached_state.state[property_name]

Copilot AI review requested due to automatic review settings July 13, 2026 18:40

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.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 5 comments.

Comments suppressed due to low confidence (1)

libraries/microsoft-agents-hosting-dialogs/microsoft_agents/hosting/dialogs/dialog_manager.py:93

  • on_turn() now skips resolving ConversationState from context.turn_state, but still unconditionally uses self.conversation_state immediately after. If conversation_state was not configured, this will raise an AttributeError at create_property(). Fail fast with a clear error (or restore a resolution mechanism) before using conversation_state.
        if self.conversation_state is not None:
            await self.conversation_state.load(context, False)
            if self.user_state is not None:
                await self.user_state.load(context, False)

        # Create property accessors
        last_access_property = self.conversation_state.create_property(self.last_access)
        last_access: datetime = cast(

Copilot AI review requested due to automatic review settings July 13, 2026 21:12
@kylerohn-msft kylerohn-msft force-pushed the users/kylerohn/fix-state-load-condition branch from bb678f8 to 8f91a50 Compare July 13, 2026 21:15
@kylerohn-msft kylerohn-msft marked this pull request as ready for review July 13, 2026 21:17

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.

Pull request overview

Copilot reviewed 37 out of 41 changed files in this pull request and generated 5 comments.

Comment thread tests/hosting_core/state/test_agent_state.py
Copilot AI review requested due to automatic review settings July 13, 2026 21:33

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.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Comment thread tests/hosting_core/state/test_agent_state.py
@kylerohn-msft kylerohn-msft merged commit e95352e into main Jul 13, 2026
11 checks passed
@kylerohn-msft kylerohn-msft deleted the users/kylerohn/fix-state-load-condition branch July 13, 2026 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AgentState.load() uses instance-level _cached_state as cache guard, causing cross-turn state pollution under concurrent traffic

3 participants