Class AgentSpawnTool

java.lang.Object
io.agentscope.harness.agent.tool.AgentSpawnTool

public class AgentSpawnTool extends Object
Simple subagent tool for agent-internal use. Much lighter than SessionsTool:
  • agent_spawn — spawn a subagent, run task, return result (sync or async)
  • agent_send — send follow-up message to a previously spawned subagent
  • agent_list — list active subagents

No sessions, no lanes, no run registry, no announce dispatch. Just "create agent, invoke, return result". Uses DefaultAgentManager for agent creation and invocation only.

Async tasks (timeout_seconds=0) are submitted to the TaskRepository scoped by the current session ID from RuntimeContext. This makes task state visible in workspace storage for cross-node retrieval and recovery after compaction.

Streaming

agent_spawn and agent_send return Mono<String> so that the framework's reactive tool-invocation pipeline (see ToolMethodInvoker) can subscribe them within the parent agent's streaming chain. When a SubagentEventBus is present in the Reactor Context (injected by AgentBase.createEventStream), every child Event is forwarded to the parent sink in real time, giving consumers a flattened event stream across the full call hierarchy. When no bus is present (plain call() mode), execution falls back to the non-streaming invokeAgent path with no overhead.

  • Field Details

    • CTX_EXPOSE_TO_USER

      public static final String CTX_EXPOSE_TO_USER
      RuntimeContext string key for a per-call override of subagent user-exposure. Put a Boolean (or its string form) under this key to control exposure for every agent_spawn in the current call, independent of what the LLM requests.

      Example:

      
       RuntimeContext ctx = RuntimeContext.builder()
           .userId("user-1")
           .put(AgentSpawnTool.CTX_EXPOSE_TO_USER, true)
           .build();
       

      Resolution precedence (highest first): this context value → the spawned subagent's SubagentDeclaration.getExposeToUser() policy → the LLM's expose_to_user argument → false.

      See Also:
    • CTX_AGENT_MANAGER

      public static final String CTX_AGENT_MANAGER
      RuntimeContext string key for the immutable subagent registry selected by the current parent-agent invocation. SubagentsMiddleware installs a namespace-scoped manager here so concurrent callers never overwrite each other's declarations.
      See Also:
    • CTX_REMOTE_CONTEXT_ATTRIBUTES

      public static final String CTX_REMOTE_CONTEXT_ATTRIBUTES
      RuntimeContext string key holding a Map<String, Object> of caller-defined attributes to send with every remote subagent submission of the current call, as context.attributes:
      
       RuntimeContext ctx = RuntimeContext.builder()
           .sessionId("s-1")
           .put(AgentSpawnTool.CTX_REMOTE_CONTEXT_ATTRIBUTES, Map.of("tenant", "acme"))
           .build();
       

      Merged over the subagent's static SubagentDeclaration.getRemoteContextAttributes(), so a per-call value wins on conflict. Values must be JSON-serializable.

      See Also:
    • CTX_FORCE_SYNC

      public static final String CTX_FORCE_SYNC
      RuntimeContext string key that forces synchronous subagent execution for the current call. Put a Boolean (or its string form) under this key to ignore LLM-requested background mode (timeout_seconds=0) and to disable timeout promotion to a background task_id.

      When force-sync is on:

      • timeout_seconds=0 is coerced to the default sync timeout (30s), unless CTX_FORCE_SYNC_TIMEOUT_SECONDS supplies an absolute override
      • If the sync wait exceeds the timeout, the subagent is interrupted and the tool returns status: timeout — it is not promoted to an async background task
      
       RuntimeContext ctx = RuntimeContext.builder()
           .sessionId("s-1")
           .put(AgentSpawnTool.CTX_FORCE_SYNC, true)
           .put(AgentSpawnTool.CTX_FORCE_SYNC_TIMEOUT_SECONDS, 120)
           .build();
       

      Applies to agent_spawn and agent_send. Multiple sync tool calls in one turn still run in parallel by default (Toolkit parallel=true).

      See Also:
    • CTX_FORCE_SYNC_TIMEOUT_SECONDS

      public static final String CTX_FORCE_SYNC_TIMEOUT_SECONDS
      Optional RuntimeContext override for the sync wait (seconds) when CTX_FORCE_SYNC is enabled. Accepts an Integer/Number or its string form.

      When present (and force-sync is on), this value fully replaces the LLM's timeout_seconds for the call. Values <= 0 fall back to the default sync timeout (30s); values above 600 are clamped.

      Ignored when force-sync is off.

      
       RuntimeContext ctx = RuntimeContext.builder()
           .put(AgentSpawnTool.CTX_FORCE_SYNC, true)
           .put(AgentSpawnTool.CTX_FORCE_SYNC_TIMEOUT_SECONDS, 120)
           .build();
       
      See Also:
  • Constructor Details

    • AgentSpawnTool

      public AgentSpawnTool(DefaultAgentManager agentManager, TaskRepository taskRepository, int parentSpawnDepth)
      Creates an AgentSpawnTool that derives the active user-id from each tool call's RuntimeContext, rather than a shared supplier — this prevents identity races when a single agent instance serves concurrent callers.
      Parameters:
      agentManager - factory and invoker for subagents
      taskRepository - background task store
      parentSpawnDepth - current spawn-depth of the parent (0 for top-level main agent)
    • AgentSpawnTool

      public AgentSpawnTool(DefaultAgentManager agentManager, TaskRepository taskRepository, int parentSpawnDepth, SubagentGatewayBridge gatewayBridge)
      Creates an AgentSpawnTool with an optional gateway bridge for exposing subagents as user-addressable threads.
      Parameters:
      agentManager - factory and invoker for subagents
      taskRepository - background task store
      parentSpawnDepth - current spawn-depth of the parent (0 for top-level main agent)
      gatewayBridge - optional bridge for thread exposure; null for standalone mode
  • Method Details

    • setGatewayBridge

      public void setGatewayBridge(SubagentGatewayBridge gatewayBridge)
      Wires (or re-wires) the gateway bridge used to expose subagents as user-addressable threads.

      Mutating the bridge on the live instance — rather than constructing a replacement — is essential: the toolkit binds agent_spawn to this exact object at orchestration time, so a replacement tool would never receive calls. The bridge is typically supplied lazily, after the agent is built, when its internal gateway is created (see HarnessAgent#ensureGateway).

      Parameters:
      gatewayBridge - the bridge implementation, or null to disable exposure
    • agentSpawn

      @Tool(name="agent_spawn", stateInjected=true, description="Spawn an isolated subagent for delegated or background work. Every response starts with three lines: agent_key (pass this verbatim to agent_send as agent_key), agent_id (the subagent type name), and session_id (internal; do not use as agent_key). Sync mode returns the reply below that; async (timeout_seconds=0) adds task_id for task_output or wait_async_results; task_id is NOT agent_key. Multiple sync tool calls in one turn run in parallel by default; pass a Toolkit with parallel=false to serialize, or use async tasks for fire-and-forget parallelism.") public reactor.core.publisher.Mono<String> agentSpawn(io.agentscope.core.agent.RuntimeContext runtimeContext, io.agentscope.core.state.AgentState parentState, @ToolParam(name="agent_id",description="Subagent identifier to instantiate") String agentId, @ToolParam(name="task",description="Task or prompt to send to the spawned agent",required=false) String task, @ToolParam(name="label",description="Optional human-readable label for referencing via agent_send",required=false) String label, @ToolParam(name="timeout_seconds",description="Max seconds to wait for the task result. 0=fire-and-forget, returns task_id. Default: 30. Max: 600.",required=false) Integer timeoutSeconds, @ToolParam(name="expose_to_user",description="When true, the spawned subagent becomes directly addressable by the user via a thread_id handle. Returns thread_id in the response. Requires a gateway bridge to be configured.",required=false) Boolean exposeToUser)
    • agentSend

      @Tool(name="agent_send", stateInjected=true, description="Send a message to an existing subagent. Use the exact string from the agent_key line of agent_spawn output (starts with agent:), or the label you set at spawn. Do not pass agent_id, session_id, or task_id here. timeout_seconds=0 returns task_id for task_output or wait_async_results.") public reactor.core.publisher.Mono<String> agentSend(io.agentscope.core.agent.RuntimeContext runtimeContext, io.agentscope.core.state.AgentState parentState, @ToolParam(name="agent_key",description="Exact value from agent_spawn\'s first line after \'agent_key: \' (format agent:<type>:<uuid>). Not agent_id, session_id, or task_id. Mutually exclusive with label.",required=false) String agentKey, @ToolParam(name="label",description="Agent label assigned at spawn time. Mutually exclusive with agent_key.",required=false) String label, @ToolParam(name="message",description="Message to send to the subagent") String message, @ToolParam(name="timeout_seconds",description="Max seconds to wait for a reply. 0=fire-and-forget, returns task_id. Default: 30. Max: 600.",required=false) Integer timeoutSeconds)
    • agentList

      @Tool(name="agent_list", description="List active subagents spawned by this agent.") public String agentList()