MiddlewareSet fix#461
Conversation
…icrosoft/Agents-for-python into users/robrandao/middleware-set
There was a problem hiding this comment.
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
MiddlewareSetto support fluent.use()chaining, stricter middleware validation, and clearer internal execution flow. - Updates middleware callback typing/invocation to consistently pass
TurnContextinto the “next” logic function. - Adds new unit tests for
MiddlewareSetbehavior 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
logiccallback is now invoked asawait 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 beNone) 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.
…icrosoft/Agents-for-python into users/robrandao/middleware-set
There was a problem hiding this comment.
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_turnnow invokes the continuation aslogic(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.
"""
This pull request makes significant improvements to the
MiddlewareSetpipeline in themicrosoft-agents-hosting-corelibrary, 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
MiddlewareSetto clarify and streamline middleware registration and invocation:usemethod now returns theMiddlewareSetinstance for chaining and raises aTypeErrorif invalid middleware is registered._receive_activity_internal) and method signatures were updated for clarity and type safety.on_turnandreceive_activity_with_statusmethods now properly run middleware and final logic, and do not return the result of the final logic (always returningNone).Updated the
Middlewareprotocol and all relevant implementations to accept an optionallogicparameter and improved docstrings for clarity. [1] [2]Type annotations and API consistency
logicis consistently typed as an optional callable accepting aTurnContext. [1] [2]Test coverage
test_middleware_set.py, with comprehensive tests forMiddlewareSetcovering registration, chaining, short-circuiting, context passing, nesting, and exception propagation.Public API updates
MiddlewareSetfrom the package’s__init__.pyand updated the__all__list to include it, making it officially part of the public API. [1] [2]Minor fixes
run_pipelineinchannel_adapter.pyto no longer return the result of the middleware pipeline, aligning with the new middleware pipeline behavior.