Skip to content

Conversation

@tconley1428
Copy link
Contributor

Summary

  • Fixed a Python closure bug in _convert_agent where handoff functions were incorrectly captured by reference instead of by value
  • Added test case validating correct handoff routing with multiple agents

Problem

The _convert_agent function had a closure bug where the async def on_invoke function was defined inside a loop and captured the original_invoke variable by reference. This caused all handoff calls to route to the last agent processed in the loop instead of the intended agent.

For example, when calling transfer_to_planner, it would incorrectly route to the writer agent instead of the planner agent.

Solution

  • Used a default parameter (invoke_func=original_invoke) to capture the handoff function by value instead of by reference
  • This ensures each handoff closure captures its own unique function reference

Test

Added test_multiple_handoffs_workflow that:

  • Creates a workflow with multiple agents (Planner, Writer)
  • Verifies transfer_to_planner correctly routes to the Planner agent
  • Validates the handoff mechanism works as expected with multiple handoffs

🤖 Generated with Claude Code

Fixed a Python closure bug in _convert_agent where handoff
functions were incorrectly captured by reference instead of by value.
This caused all handoff calls to route to the last agent processed
in the loop instead of the intended agent.

- Fix: Use default parameter to capture handoff function by value
- Add: Test case validating correct handoff routing with multiple agents
- Test: Verifies transfer_to_planner correctly routes to Planner agent

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@tconley1428 tconley1428 requested a review from a team as a code owner January 29, 2026 17:19
Copy link
Contributor

@THardy98 THardy98 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - as a sanity check, could we add an assertion after the planner response to check that the subsequent handoff to the writer is also correct

@tconley1428
Copy link
Contributor Author

It actually doesn't bother handing off to the writer because the planner doesn't have the writer as a subagent and the planner doesn't know what to plan. Sufficient to test the issue though.

@tconley1428 tconley1428 merged commit ca7f098 into main Jan 29, 2026
26 of 27 checks passed
@tconley1428 tconley1428 deleted the fix-handoff-closure-bug branch January 29, 2026 23:40
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.

4 participants