agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Bug]:Subagent completion notification lacks global progress; SubagentsMiddleware.wireMessageBus is unreachable code

Open sailor-smile opened this issue 3 months ago • 3 comments

Describe the bug

Two related problems. The second one is what blocks fixing the first.

Problem 1 — completion notifications carry no global progress. After spawning N async subagents, each completion pushes its own notification. The notification only states that a task finished; it carries no "M of N terminal" information. On receiving the first notification the LLM cannot tell whether other tasks are still running, treats it as an overall completion signal, wraps up early and reports back.

Problem 2 — the extension point for adding progress is unreachable. SubagentsMiddleware.wireMessageBus looks like the public entry point for registering the completion notification callback, but it is never invoked by the SDK, so overriding or replacing it has no effect whatsoever.

To Reproduce

  1. You code
// Problem 1
HarnessAgent supervisor = HarnessAgent.builder()
        .model(model)
        .messageBus(messageBus)
        .subagentFactory("worker", name -> workerAgent)
        .build();

// Spawn 5+ async subagents (background task mode) with clearly different durations,
// e.g. the first one much faster than the rest.
supervisor.streamEvents(new UserMessage(promptThatSpawnsManyBackgroundTasks), ctx).subscribe();

// Problem 2: try to customise the completion notification through the public method.
subagentsMiddleware.wireMessageBus(messageBus);  // never called by the SDK -> no effect
  1. How to execute

For problem 1, run the flow and watch what the LLM does right after the first completion notification arrives, while other tasks are still running.

For problem 2, register a custom completion callback via wireMessageBus and check whether it is ever invoked during a run that includes async subagents.

  1. See error

Problem 1: the LLM finishes and reports results while other subagents are still executing; their results are lost — they do complete later and push notifications, but nothing is waiting for them by then.

Problem 2: the callback registered through wireMessageBus is never invoked, and the notification format is unchanged.

Expected behavior

  1. Completion notifications should carry enough information to judge overall progress (for example [3/8 tasks terminal]), so the LLM can distinguish "one of them finished" from "all finished".
  2. As a public method, wireMessageBus should be on the SDK's actual execution path; otherwise it should not exist in the form of a public extension point.

Error messages

No exception in either case; both failures are silent.

For problem 2 in particular, the silence is the harmful part: HarnessAgent$Builder.build() performs the registration through its own private static method wireTaskRepositoryMessageBus, a different path entirely. Code that patches wireMessageBus compiles and appears correct, yet the notification format never changes at runtime and nothing indicates why.

Environment (please complete the following information):

  • AgentScope-Java Version: 2.0.0 (agentscope-harness 2.0.0)
  • Java Version: 17
  • OS: macos

Additional context

Root cause.

Problem 1: notification text is built from the perspective of a single task, while TaskRepository actually holds every task of the session together with its status — global progress is computable at notification time. The information is available, just unused.

Problem 2: the public wireMessageBus coexists with the private wireTaskRepositoryMessageBus that actually takes effect, and the public one is unreachable. This is both dead code and an API contract problem: the public signature implies an extension point that is not one.

The only workaround available externally is to reflectively read messageBus and taskRepository after build() and re-register through WorkspaceTaskRepository.setCompletionCallback, relying on the undocumented "later registration wins" behaviour. That path couples to private field names and can break silently on every SDK upgrade.

Suggested fix.

Problem 1: when building the completion notification, count terminal and total tasks via TaskRepository.listTasks(ctx, sessionId, null) and include progress in the text, e.g.

Subagent task {taskId} completed [3/8 tasks terminal]: ...

TaskStatus.isTerminal() already exists, so no new API is needed.

Problem 2: either

  1. have build() go through the public wireMessageBus so it becomes a real extension point (preferred — customising the notification format is a legitimate need), or
  2. remove wireMessageBus if opening it up is not intended, so the public signature stops implying otherwise.

Either way, consider offering a supported public route for customising completion notifications, for example exposing the completion callback on the builder, so integrators do not need reflection over private fields.

Affected classes:

  • io.agentscope.harness.agent.middleware.SubagentsMiddleware
  • io.agentscope.harness.agent.HarnessAgent$Builder (wireTaskRepositoryMessageBus)
  • io.agentscope.harness.agent.subagent.task.WorkspaceTaskRepository (setCompletionCallback)

sailor-smile avatar Aug 20 '26 08:08 sailor-smile

Issue Evaluation

Category: bug | Status: Partly confirmed — P1 partly confirmed, P2 not reproducible as written (verified against main@ea78c31)

Thanks for the precise report. Verification of each claim:

Claim Result Evidence
P1: completion notification carries no "M of N terminal" progress Partly confirmed The pushed notification is built per single task — taskId + agent + result, wrapped as one "hint" — with no sibling/pending count (SubagentsMiddleware.java:296-315; the repository fires only single-task arguments, WorkspaceTaskRepository.java:966-974). However, two compensating channels exist: every reasoning pass injects a current task-list/status summary (SubagentsMiddleware.java:435-446, :685-725), and wait_async_results(wait_all=true) snapshots all non-terminal tasks and blocks until all reach terminal state (WaitAsyncResultsTool.java:152-154, :285-329). So the model can know others are still running — but only if it consults those channels, and the notification itself gives no signal. The early wrap-up outcome is plausible and consistent with the known wait-budget gap in #2791.
P2: wireMessageBus is unreachable code, never invoked by the SDK Not reproducible It is on the SDK execution path: HarnessAgent.build() calls subagentsMw.wireMessageBus(messageBus, agentId) whenever a messageBus is configured (HarnessAgent.java:2666-2669; same pattern for teams at :2635). A manual post-build call from outside the builder is indeed a no-op for SDK wiring, but not because the method is dead.
P2 root cause (what likely was observed) Confirmed (different mechanism) WorkspaceTaskRepository.setCompletionCallback is a single-slot field assignment (:257-259), and the javadoc on wireMessageBus acknowledges replacement (SubagentsMiddleware.java:283-285). A user-registered callback races with the SDK's own build-time registration — whichever wireMessageBus call runs last owns the notification format, so an override can be silently clobbered (or can clobber the SDK path). Same overwrite issue already flagged in #2791.

Severity: medium — notification payload lacks progress context; the "unreachable extension point" is actually the single-slot callback overwrite, which is a real API-design gap but not dead code.

Suggested minimal fix

  1. SubagentsMiddleware.wireMessageBus callback: snapshot pending/terminal counts from WorkspaceTaskRepository and prefix the notification with e.g. [M/N tasks terminal].
  2. WorkspaceTaskRepository.setCompletionCallback: compose into a listener list (or expose a public addCompletionListener) instead of overwriting, so user extensions and the SDK path coexist.
  3. If composition is deemed too invasive, at minimum make wireMessageBus builder-internal (package-private) so it stops looking like a supported override point.

Items 1 and 2 overlap with the fix options already pending maintainer signal in #2791 — worth converging both into one change to the completion-notification path.


Automated evaluation by github-manager (oss-maintainer)

oss-maintainer avatar Sep 25 '26 18:09 oss-maintainer

Hi, I'd like to work on this. Problem 2 is the actionable core: SubagentsMiddleware.wireMessageBus is never invoked by the SDK, so the completion-notification callback is unoverridable. Plan: wire it where the message bus is connected (and/or have the middleware register its callback in its own lifecycle), then build Problem 1's M-of-N progress on top of that reachable hook. Tests asserting the callback fires and notifications carry progress. PR coming shortly.

aiyili avatar Oct 22 '26 03:10 aiyili

Root-cause update from my investigation (claimed earlier; sharing before any code):

Problem 2 — wireMessageBus never invoked: already fixed on current main. HarnessAgent now wires it at build time whenever a messageBus is configured — both TeamsMiddleware (right after construction) and SubagentsMiddleware (after buildSubagentsMiddleware, via the resolved agent id used for wakeup routing). The issue was filed against an older tree (2026-08-20); the extension point is reachable today. If you're still seeing it unwired, that would be a different construction path (e.g. manually assembled middleware chains), which wireMessageBus remaining public still covers.

Problem 1 — M-of-N progress in completion notifications: still open, and now purely an enhancement. With the callback reachable, the fix is contained in SubagentsMiddleware.wireMessageBus's callback: at completion time, query taskRepository for the parent session's task set and compute terminal count, then include M of N tasks terminal in the <system-reminder> payload (and optionally skip the reminder when unterminal siblings still exist and the platform prefers batched notices). The callback already receives (rc, taskId, subAgentId, sessionId, result) — everything needed except the sibling query.

Proposed wording: <system-reminder>Background subagent task 'X' completed (3 of 5 tasks terminal).</system-reminder>. If maintainers want this, it's a small, self-contained change to the hint formatting plus a repository count method — I can implement on top of current main.

aiyili avatar Oct 26 '26 02:10 aiyili