Class SubagentsMiddleware

java.lang.Object
io.agentscope.harness.agent.middleware.SubagentsMiddleware
All Implemented Interfaces:
io.agentscope.core.middleware.MiddlewareBase, HarnessRuntimeMiddleware

public class SubagentsMiddleware extends Object implements HarnessRuntimeMiddleware
Middleware that provides the managed subagent mechanism.

In default mode (standalone harness setup), this middleware creates an AgentSpawnTool backed by a DefaultAgentManager. In session mode (orchestrated via AgentBootstrap), an external tool (typically SessionsTool) is injected, replacing the default AgentSpawnTool.

Responsibilities:

  1. Exposes the subagent tool and TaskTool as agent tools (callers query via getTools() and register them on the toolkit at orchestration time).
  2. On every onAgent(io.agentscope.core.agent.Agent, io.agentscope.core.agent.RuntimeContext, io.agentscope.core.middleware.AgentInput, java.util.function.Function<io.agentscope.core.middleware.AgentInput, reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>>), reloads subagent declarations from the workspace filesystem (namespace-scoped) to support per-user subagent isolation.
  3. Prepends rich subagent usage guidance and current async task summary to the leading SYSTEM message of every ReasoningInput. Because the framework rebuilds the SYSTEM message from a frozen base each iteration, this is safe — content never accumulates across iterations.
  • Constructor Details

  • Method Details

    • enableAgentGenerateTool

      public SubagentsMiddleware enableAgentGenerateTool(SubagentSpecGenerator generator)
      Enables the agent_generate tool, which lets the LLM author new subagent specs from a description. Off by default — the generator needs a Model and writes to the workspace, so callers opt in explicitly.

      Only effective in default (non-session) mode, where this middleware owns a DefaultAgentManager. In session mode the manager is external and this call is a no-op.

      Parameters:
      generator - a SubagentSpecGenerator backed by the model that should author specs
      Returns:
      this middleware for chaining
    • getTaskRepository

      public TaskRepository getTaskRepository()
      Returns the TaskRepository used by this middleware.
    • wireMessageBus

      public SubagentsMiddleware wireMessageBus(MessageBus messageBus, String agentId)
      Wires a MessageBus so that background task completions are pushed to the session inbox and a wakeup signal is enqueued. This enables the WakeupDispatcher to re-trigger idle sessions when subagent work finishes.

      Registers a TaskRepository.TaskCompletionCallback on the underlying repository. Safe to call multiple times — each call replaces the previous callback.

      Parameters:
      messageBus - the application message bus
      agentId - the parent agent id (for wakeup routing)
      Returns:
      this middleware for chaining
    • setGatewayBridge

      public SubagentsMiddleware setGatewayBridge(SubagentGatewayBridge bridge)
      Wires a gateway bridge into the internal AgentSpawnTool, enabling spawned subagents to be exposed as user-addressable threads. Only effective in default (non-session) mode.
      Parameters:
      bridge - the bridge implementation (typically obtained from GatewayBootstrap.gatewayBridge())
      Returns:
      this middleware for chaining
    • getAgentManager

      public DefaultAgentManager getAgentManager()
      Returns the internal DefaultAgentManager that can re-materialize subagents, or null in session mode (external tool). Used to wire a gateway materializer for cross-node exposed-subagent recovery.
    • getTools

      public List<Object> getTools()
      Returns the tool instances this middleware contributes to the agent toolkit. The caller is responsible for registering them on the toolkit at orchestration time.

      When enableAgentGenerateTool(io.agentscope.harness.agent.subagent.SubagentSpecGenerator) has been called, the returned list additionally contains an AgentGenerateTool.

    • onAgent

      public reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent> onAgent(io.agentscope.core.agent.Agent agent, io.agentscope.core.agent.RuntimeContext ctx, io.agentscope.core.middleware.AgentInput input, Function<io.agentscope.core.middleware.AgentInput,reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>> next)
      Specified by:
      onAgent in interface io.agentscope.core.middleware.MiddlewareBase
    • onReasoning

      public reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent> onReasoning(io.agentscope.core.agent.Agent agent, io.agentscope.core.agent.RuntimeContext ctx, io.agentscope.core.middleware.ReasoningInput input, Function<io.agentscope.core.middleware.ReasoningInput,reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>> next)
      Specified by:
      onReasoning in interface io.agentscope.core.middleware.MiddlewareBase
    • renderSubagentSection

      public static String renderSubagentSection(List<SubagentEntry> entries, boolean isSessionMode)
      Renders the ## Subagents system-prompt section for the supplied entries. Shared with DynamicSubagentsMiddleware.

      Filters out entries whose declaration sets hidden=true or mode=PRIMARY: the LLM should not see internal-use subagents (hidden) and cannot spawn primary-only declarations (the DefaultAgentManager rejects such spawns anyway, so advertising them would be a footgun). Programmatic registrations without a declaration (entry.declaration() == null) are always shown.

    • buildTaskSummary

      public static String buildTaskSummary(TaskRepository repo, io.agentscope.core.agent.RuntimeContext ctx)
      Builds a concise summary of in-flight tasks for the current session, or null if none. Shared with DynamicSubagentsMiddleware.

      Phase B-3: already-delivered tasks are filtered out — they live in the conversation history as <system-reminder> blocks, so re-mentioning them here would (a) waste tokens, (b) push genuinely still-running tasks past the MAX_TASK_SUMMARY_ENTRIES cap, and (c) invite the LLM to second-guess the push by re-fetching results.