Skip to content

MiddlewareSet fix#461

Merged
rodrigobr-msft merged 17 commits into
mainfrom
users/robrandao/middleware-set
Jul 13, 2026
Merged

MiddlewareSet fix#461
rodrigobr-msft merged 17 commits into
mainfrom
users/robrandao/middleware-set

Conversation

@rodrigobr-msft

Copy link
Copy Markdown
Contributor

This pull request makes significant improvements to the MiddlewareSet pipeline in the microsoft-agents-hosting-core library, focusing on correctness, flexibility, and test coverage. The changes refactor how middleware is registered and invoked, clarify method signatures and return values, and add comprehensive tests to ensure expected behavior. Additionally, the pull request improves type annotations and documentation throughout the middleware system.

Middleware pipeline improvements and refactoring

  • Refactored MiddlewareSet to clarify and streamline middleware registration and invocation:

    • The use method now returns the MiddlewareSet instance for chaining and raises a TypeError if invalid middleware is registered.
    • Middleware short-circuiting and context passing are handled more explicitly.
    • The internal middleware execution logic was moved to a private method (_receive_activity_internal) and method signatures were updated for clarity and type safety.
    • The on_turn and receive_activity_with_status methods now properly run middleware and final logic, and do not return the result of the final logic (always returning None).
  • Updated the Middleware protocol and all relevant implementations to accept an optional logic parameter and improved docstrings for clarity. [1] [2]

Type annotations and API consistency

  • Updated type annotations for middleware and callback functions throughout the middleware and transcript logger code, ensuring that logic is consistently typed as an optional callable accepting a TurnContext. [1] [2]

Test coverage

  • Added a new test suite, test_middleware_set.py, with comprehensive tests for MiddlewareSet covering registration, chaining, short-circuiting, context passing, nesting, and exception propagation.
  • Improved transcript logger middleware tests to cover outgoing activity logging by callbacks.

Public API updates

  • Exported MiddlewareSet from the package’s __init__.py and updated the __all__ list to include it, making it officially part of the public API. [1] [2]

Minor fixes

  • Updated run_pipeline in channel_adapter.py to no longer return the result of the middleware pipeline, aligning with the new middleware pipeline behavior.

Copilot AI review requested due to automatic review settings July 10, 2026 18:31
@rodrigobr-msft rodrigobr-msft marked this pull request as ready for review July 10, 2026 18:31
@rodrigobr-msft rodrigobr-msft requested a review from a team as a code owner July 10, 2026 18:31

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 the MiddlewareSet execution pipeline in microsoft-agents-hosting-core to clarify middleware registration/execution behavior, align adapter pipeline behavior with the new semantics, and add targeted test coverage for middleware ordering, short-circuiting, and transcript logging.

Changes:

  • Refactors MiddlewareSet to support fluent .use() chaining, stricter middleware validation, and clearer internal execution flow.
  • Updates middleware callback typing/invocation to consistently pass TurnContext into the “next” logic function.
  • Adds new unit tests for MiddlewareSet behavior and extends transcript logger middleware tests to cover outgoing activities sent by callback logic.

Reviewed changes

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

Show a summary per file
File Description
tests/hosting_core/test_middleware_set.py Adds comprehensive tests for middleware registration, ordering, context passing, short-circuiting, and nesting.
tests/hosting_core/storage/test_transcript_logger_middleware.py Adds a regression test ensuring outgoing activities sent by callback are logged to the transcript.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/storage/transcript_logger.py Updates middleware logic typing and invocation to pass context into the callback.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/middleware_set.py Refactors middleware pipeline internals and public methods (use, on_turn, receive_activity_with_status).
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/channel_adapter.py Aligns adapter pipeline to no longer return the middleware pipeline result.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/init.py Exports MiddlewareSet as part of the public hosting core API.
Comments suppressed due to low confidence (1)

libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/storage/transcript_logger.py:120

  • The logic callback is now invoked as await logic(context), but the docstring still describes it generically as a no-arg end-of-chain callback. Updating the docstring to reflect the callable's signature (and that it may be None) will prevent confusion for middleware authors.
    async def on_turn(
        self, context: TurnContext, logic: Callable[[TurnContext], Awaitable] | None
    ):
        """Initialization for middleware.
        :param context: Context for the current turn of conversation with the user.
        :param logic: Function to call at the end of the middleware chain.
        """

💡 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:42

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 3 comments.

Copilot AI review requested due to automatic review settings July 10, 2026 21:24

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 3 comments.

Comment thread tests/hosting_core/test_middleware_set.py
Copilot AI review requested due to automatic review settings July 10, 2026 21:30

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 2 comments.

Copilot AI review requested due to automatic review settings July 13, 2026 16:39
@rodrigobr-msft rodrigobr-msft linked an issue Jul 13, 2026 that may be closed by this pull request

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 7 out of 7 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/storage/transcript_logger.py:120

  • TranscriptLoggerMiddleware.on_turn now invokes the continuation as logic(context), but the docstring still describes it generically as a function called at the end of the chain. Updating the docstring to reflect the required argument and the middleware-continuation semantics will prevent incorrect implementations/callers.
        """Initialization for middleware.
        :param context: Context for the current turn of conversation with the user.
        :param logic: Function to call at the end of the middleware chain.
        """

Comment thread test_samples/app_style/empty_agent.py
@rodrigobr-msft rodrigobr-msft merged commit 12a5d53 into main Jul 13, 2026
12 checks passed
@rodrigobr-msft rodrigobr-msft deleted the users/robrandao/middleware-set branch July 13, 2026 17: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.

MiddlewareSet revisions needed

3 participants