Skip to content

fix(agentchat): dispatch CodeExecutorAgent reflection via self.__class__ (#7205)#7645

Open
GopalGB wants to merge 1 commit intomicrosoft:mainfrom
GopalGB:gb-fix/code-executor-polymorphism-7205
Open

fix(agentchat): dispatch CodeExecutorAgent reflection via self.__class__ (#7205)#7645
GopalGB wants to merge 1 commit intomicrosoft:mainfrom
GopalGB:gb-fix/code-executor-polymorphism-7205

Conversation

@GopalGB
Copy link
Copy Markdown

@GopalGB GopalGB commented Apr 30, 2026

Closes #7205. Mirrors the AssistantAgent polymorphism fix from #5953.

Repro

CodeExecutorAgent.on_messages_stream calls _reflect_on_code_block_results_flow through the concrete CodeExecutorAgent. reference (_code_executor_agent.py:667):

async for reflection_response in CodeExecutorAgent._reflect_on_code_block_results_flow(
    ...
):

_reflect_on_code_block_results_flow is a @classmethod, so calling through the concrete class static-binds to the parent's implementation regardless of self's actual class. A subclass that overrides the reflection flow:

class MyAgent(CodeExecutorAgent):
    @classmethod
    async def _reflect_on_code_block_results_flow(cls, ...):
        # custom behavior
        ...

is silently ignored. The override never fires, the parent runs instead, no exception is raised, and the bug is invisible. The reporter found this exact pattern was previously fixed for AssistantAgent in #5953 — this is the unfixed twin in CodeExecutorAgent.

Fix

Route the call through self.__class__:

async for reflection_response in self.__class__._reflect_on_code_block_results_flow(
    ...
):

Because _reflect_on_code_block_results_flow is a @classmethod, dispatching through self.__class__ preserves the existing cls semantics without any signature change to the classmethod itself. Two-line change plus a docstring comment pointing at #7205 and #5953.

Test

test_subclass_overrides_reflect_on_code_block_results_flow defines a CustomCodeExecutorAgent(CodeExecutorAgent) that overrides _reflect_on_code_block_results_flow to yield a sentinel Response. After running on_messages_stream against a replay model client, the test asserts the final Response.chat_message.content equals the sentinel.

Before the fix:

  • Parent reflection runs against the replay client and yields its second canned response (TERMINATE).
  • Sentinel is never observed.
  • Test FAILS.

After the fix:

  • Subclass override fires.
  • Sentinel is the final response.
  • Test PASSES.

Verification:

# With fix
$ python -m pytest tests/test_code_executor_agent.py::test_subclass_overrides_reflect_on_code_block_results_flow
1 passed in 2.60s

# Reverting only the source change
$ git stash push python/packages/autogen-agentchat/src/autogen_agentchat/agents/_code_executor_agent.py
$ python -m pytest tests/test_code_executor_agent.py::test_subclass_overrides_reflect_on_code_block_results_flow
1 failed in 0.62s

Full test_code_executor_agent.py suite: 27 passed.

🤖 Generated with Claude Code

…class__

Fixes microsoft#7205. Mirrors the AssistantAgent polymorphism fix from microsoft#5953.

``CodeExecutorAgent.on_messages_stream`` calls
``_reflect_on_code_block_results_flow`` through the concrete
``CodeExecutorAgent.`` reference, which static-binds the call to the
parent's classmethod even when an instance is a subclass. Subclass
overrides of the reflection flow are silently ignored, the reflection
behavior subclasses configured never runs, and the bug is invisible
because no exception is raised.

The fix routes the call through ``self.__class__`` so the runtime
class drives method resolution. ``_reflect_on_code_block_results_flow``
is a ``@classmethod``, so dispatching through ``self.__class__``
preserves the existing ``cls`` semantics without requiring any signature
change to the classmethod itself.

Adds ``test_subclass_overrides_reflect_on_code_block_results_flow`` --
a subclass overrides ``_reflect_on_code_block_results_flow`` to yield a
sentinel ``Response``. Before the fix the parent's reflection runs
against the replay client and the sentinel is never observed; after the
fix the override fires and the sentinel is the final response. Verified
by reverting the source change and re-running -- the new test fails on
unpatched code and passes with the patch.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@GopalGB
Copy link
Copy Markdown
Author

GopalGB commented May 7, 2026

Friendly ping — fix(agentchat): dispatch CodeExecutorAgent reflection via self.__class__ (#7205). CI green, CLA signed. cc maintainers.

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.

Polymorphism issue: CodeExecutorAgent calls classmethod via concrete class in on_messages_stream

1 participant