Skip to content

fix(subagent): propagate parent RuntimeContext to child agents#1833

Merged
chickenlj merged 2 commits into
mainfrom
fix/1739-subagent-runtime-context
Jun 25, 2026
Merged

fix(subagent): propagate parent RuntimeContext to child agents#1833
chickenlj merged 2 commits into
mainfrom
fix/1739-subagent-runtime-context

Conversation

@chickenlj

Copy link
Copy Markdown
Collaborator

Summary

  • DefaultAgentManager.invokeAgent() built a fresh RuntimeContext with only sessionId/userId, discarding all parent context extras (typed attributes, string attributes, ToolExecutionContext, tenant info, outbound addresses, etc.).
  • Added overloaded invokeAgent/invokeAgentStream methods that accept an optional parent RuntimeContext and use RuntimeContext.builder(parentRc) to preserve extras while overriding session identity.
  • Updated all call sites in AgentSpawnTool (5 locations) to forward the parent context.
  • Backward-compatible: existing 4-arg calls still work via delegation.

Fixes #1739
Fixes #1731

DefaultAgentManager.invokeAgent() built a fresh RuntimeContext with only
sessionId and userId, discarding all parent context extras (typed
attributes, string attributes, ToolExecutionContext, etc.). This caused
subagents to lose tenant info, outbound addresses, MCP metadata, and
other per-call context set by the parent.

Add overloaded invokeAgent/invokeAgentStream methods that accept an
optional parent RuntimeContext and use RuntimeContext.builder(parentRc)
to preserve extras while overriding session identity. Update all call
sites in AgentSpawnTool to forward the parent context.

Fixes #1739
Fixes #1731
@chickenlj chickenlj requested review from a team and Copilot June 18, 2026 07:06

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@codecov

codecov Bot commented Jun 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 70.58824% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...pe/harness/agent/subagent/DefaultAgentManager.java 71.42% 2 Missing and 2 partials ⚠️
.../agentscope/harness/agent/tool/AgentSpawnTool.java 66.66% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 AI Review

This PR fixes a real and important bug: DefaultAgentManager.invokeAgent() / invokeAgentStream() built a fresh RuntimeContext that discarded all parent context extras (typed attributes, string attributes, ToolExecutionContext, tenant info, etc.), causing child agents to lose critical contextual information. The fix uses RuntimeContext.builder(parentRc) to inherit parent attributes while overriding session identity, and all 6 call sites in AgentSpawnTool are consistently updated. The approach is backward-compatible via delegation from the old signatures. One defensive-programming suggestion: explicitly null out agentState in the child builder to prevent parent state leakage into child agents that don't manage their own state (e.g., HarnessAgent).

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 AI Review

This PR fixes a real and important bug: DefaultAgentManager.invokeAgent() / invokeAgentStream() built a fresh RuntimeContext that discarded all parent context extras (typed attributes, string attributes, ToolExecutionContext, tenant info, etc.), causing child agents to lose critical contextual information. The fix uses RuntimeContext.builder(parentRc) to inherit parent attributes while overriding session identity, and all 6 call sites in AgentSpawnTool are consistently updated. The approach is backward-compatible via delegation from the old signatures. One defensive-programming suggestion: explicitly null out agentState in the child builder to prevent parent state leakage into child agents that don't manage their own state (e.g., HarnessAgent).

@AgentScopeJavaBot AgentScopeJavaBot added bug Something isn't working area/harness agentscope-harness (test/runtime support) labels Jun 19, 2026

@itxaiohanglover itxaiohanglover 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.

Clean implementation — the overloaded invokeAgent and invokeAgentStream methods preserve backward compatibility while allowing parent RuntimeContext propagation. The conditional builder pattern (parentRc != null ? RuntimeContext.builder(parentRc) : RuntimeContext.builder()) is the right approach — it doesn't force callers to pass parentRc.

@chickenlj chickenlj merged commit 44bdbf3 into main Jun 25, 2026
10 checks passed
@chickenlj chickenlj deleted the fix/1739-subagent-runtime-context branch June 25, 2026 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/harness agentscope-harness (test/runtime support) bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]:子Agent丢失父Agent的Session [Bug]:Harness subagent 委派时会丢失大部分 RuntimeContext

4 participants