fix: use getter for rootAgent to match Python SDK behavior #95
+47
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

Summary
Attempts to fix #82
I ran into this issue while building a TypeScript version of the weather bot tutorial. When a sub-agent completes and the model tries to return control to the root agent, it fails because the sub-agent's
rootAgentproperty points to itself rather than the actual root.Thanks to @santosflores for investigating this in #83, and to @ScottMansfield for pointing out that the Python SDK doesn't need recursive updates. It uses a computed property that walks up the parent chain each time.
This PR takes that same approach: replacing the cached
rootAgentproperty with a getter, matching how the Python SDK handles it:Changes
readonly rootAgent: BaseAgentproperty assignment in constructorget rootAgent(): BaseAgentgetter that computes the root by walking up the parent chainReproduction
Minimal example:
subAgentis created first, so its constructor setsrootAgent = this(pointing to itself). When it's later added torootAgentviasubAgents, the cached value is never updated.Before fix:
After fix:
Test Plan
core/test/agents/base_agent_test.tsto verify sub-agents correctly resolve their rootAgent