Class AgentSpawnTool
SessionsTool:
agent_spawn— spawn a subagent, run task, return result (sync or async)agent_send— send follow-up message to a previously spawned subagentagent_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 Summary
FieldsModifier and TypeFieldDescriptionstatic final StringRuntimeContextstring key for the immutable subagent registry selected by the current parent-agent invocation.static final StringRuntimeContextstring key for a per-call override of subagent user-exposure.static final StringRuntimeContextstring key that forces synchronous subagent execution for the current call.static final StringOptionalRuntimeContextoverride for the sync wait (seconds) whenCTX_FORCE_SYNCis enabled.static final StringRuntimeContextstring key holding aMap<String, Object>of caller-defined attributes to send with every remote subagent submission of the current call, ascontext.attributes: -
Constructor Summary
ConstructorsConstructorDescriptionAgentSpawnTool(DefaultAgentManager agentManager, TaskRepository taskRepository, int parentSpawnDepth) Creates anAgentSpawnToolthat derives the active user-id from each tool call'sRuntimeContext, rather than a shared supplier — this prevents identity races when a single agent instance serves concurrent callers.AgentSpawnTool(DefaultAgentManager agentManager, TaskRepository taskRepository, int parentSpawnDepth, SubagentGatewayBridge gatewayBridge) Creates anAgentSpawnToolwith an optional gateway bridge for exposing subagents as user-addressable threads. -
Method Summary
Modifier and TypeMethodDescriptionreactor.core.publisher.Mono<String>agentSend(io.agentscope.core.agent.RuntimeContext runtimeContext, io.agentscope.core.state.AgentState parentState, String agentKey, String label, String message, Integer timeoutSeconds) reactor.core.publisher.Mono<String>agentSpawn(io.agentscope.core.agent.RuntimeContext runtimeContext, io.agentscope.core.state.AgentState parentState, String agentId, String task, String label, Integer timeoutSeconds, Boolean exposeToUser) voidsetGatewayBridge(SubagentGatewayBridge gatewayBridge) Wires (or re-wires) the gateway bridge used to expose subagents as user-addressable threads.
-
Field Details
-
CTX_EXPOSE_TO_USER
RuntimeContextstring key for a per-call override of subagent user-exposure. Put aBoolean(or its string form) under this key to control exposure for everyagent_spawnin 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'sexpose_to_userargument →false.- See Also:
-
CTX_AGENT_MANAGER
RuntimeContextstring key for the immutable subagent registry selected by the current parent-agent invocation.SubagentsMiddlewareinstalls a namespace-scoped manager here so concurrent callers never overwrite each other's declarations.- See Also:
-
CTX_REMOTE_CONTEXT_ATTRIBUTES
RuntimeContextstring key holding aMap<String, Object>of caller-defined attributes to send with every remote subagent submission of the current call, ascontext.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
RuntimeContextstring key that forces synchronous subagent execution for the current call. Put aBoolean(or its string form) under this key to ignore LLM-requested background mode (timeout_seconds=0) and to disable timeout promotion to a backgroundtask_id.When force-sync is on:
timeout_seconds=0is coerced to the default sync timeout (30s), unlessCTX_FORCE_SYNC_TIMEOUT_SECONDSsupplies 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_spawnandagent_send. Multiple sync tool calls in one turn still run in parallel by default (Toolkitparallel=true).- See Also:
-
CTX_FORCE_SYNC_TIMEOUT_SECONDS
OptionalRuntimeContextoverride for the sync wait (seconds) whenCTX_FORCE_SYNCis enabled. Accepts anInteger/Numberor its string form.When present (and force-sync is on), this value fully replaces the LLM's
timeout_secondsfor the call. Values<= 0fall 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 anAgentSpawnToolthat derives the active user-id from each tool call'sRuntimeContext, rather than a shared supplier — this prevents identity races when a single agent instance serves concurrent callers.- Parameters:
agentManager- factory and invoker for subagentstaskRepository- background task storeparentSpawnDepth- current spawn-depth of the parent (0 for top-level main agent)
-
AgentSpawnTool
public AgentSpawnTool(DefaultAgentManager agentManager, TaskRepository taskRepository, int parentSpawnDepth, SubagentGatewayBridge gatewayBridge) Creates anAgentSpawnToolwith an optional gateway bridge for exposing subagents as user-addressable threads.- Parameters:
agentManager- factory and invoker for subagentstaskRepository- background task storeparentSpawnDepth- 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
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_spawnto 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 (seeHarnessAgent#ensureGateway).- Parameters:
gatewayBridge- the bridge implementation, ornullto 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()
-