Skip to content

fix(harness): stop sync-spawned subagents on parent cancel - #2412

Merged
jujn merged 5 commits into
agentscope-ai:mainfrom
vowor:agent/fix-harness-subagent-cancellation
Nov 19, 2026
Merged

jujn merged 5 commits into
agentscope-ai:mainfrom
vowor:agent/fix-harness-subagent-cancellation

Conversation

@vowor

@vowor vowor commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Summary

  • route parent cancellation through a HarnessAgent to its ReActAgent delegate
  • interrupt the spawned sub-* session context instead of the parent run context
  • preserve cancellation-by-disposal for ordinary Agent implementations and timeout promotion behavior

Root cause

AgentSpawnTool.interruptAgent only handled direct ReActAgent instances, so a synchronously spawned HarnessAgent ignored parent cancellation. It also called interrupt with the parent RuntimeContext, while the child runs under a newly generated sub-* session.

This fix stays local to AgentSpawnTool and does not change the shared Agent interrupt API or the HarnessAgent facade, avoiding overlap with #1685 and #1990.

Tests

  • Added regression coverage for HarnessAgent delegate interruption.
  • Added regression coverage for child-session RuntimeContext identity.
  • Added coverage confirming ordinary Agent subscriptions are still disposed.
  • Verified the two new regressions fail before the production fix.
  • mvn spotless:check
  • mvn test -DargLine='-Djava.awt.headless=true -javaagent:/Users/jinjichang/.m2/repository/org/mockito/mockito-core/5.23.0/mockito-core-5.23.0.jar -Xms512m -Xmx1024m' (all 82 reactor modules passed)

Closes #2408

@codecov

codecov Bot commented Jul 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@oss-maintainer oss-maintainer 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.

✅ Approved — Clean, focused fix for #2408.

Two key changes both look correct:

  1. HarnessAgent delegate unwrapping in interruptAgent() — properly routes cancellation through the facade to the underlying ReActAgent
  2. Child session context — uses childContext with the correct sub-* session ID instead of the parent's runtimeContext, which was the secondary bug

Good regression test coverage including the HarnessAgent delegation path and session context identity check. The scope is well-contained to AgentSpawnTool without touching the shared interrupt API.

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

@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 an orphan sub-agent memory leak in AgentSpawnTool.execWithTimeoutPromotion. When a parent agent's subscription is cancelled, the spawned child agent was not properly interrupted because: (1) the parent's RuntimeContext was passed to interruptAgent, targeting the wrong session; (2) HarnessAgent instances were not handled by the instanceof check. The fix constructs a childContext with the correct (userId, sessionId) and adds HarnessAgent delegate unwrapping.

@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 an orphan sub-agent memory leak in AgentSpawnTool.execWithTimeoutPromotion. When a parent agent's subscription is cancelled, the spawned child agent was not properly interrupted because: (1) the parent's RuntimeContext was passed to interruptAgent, targeting the wrong session; (2) HarnessAgent instances were not handled by the instanceof check. The fix constructs a childContext with the correct (userId, sessionId) and adds HarnessAgent delegate unwrapping.

@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 an orphan sub-agent memory leak in AgentSpawnTool.execWithTimeoutPromotion. When a parent agent's subscription is cancelled, the spawned child agent was not properly interrupted because: (1) the parent's RuntimeContext was passed to interruptAgent, targeting the wrong session; (2) HarnessAgent instances were not handled by the instanceof check. The fix constructs a childContext with the correct (userId, sessionId) and adds HarnessAgent delegate unwrapping.

@jujn
jujn merged commit ef5653e into agentscope-ai:main Nov 19, 2026
4 of 5 checks passed
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]: Cancelling a parent run does not stop a sync-spawned HarnessAgent subagent (interruptAgent no-op + wrong ctx)

4 participants