2026-02-04 11:06:13 +00:00
package tools
import (
"context"
"fmt"
2026-02-24 20:39:49 +00:00
"strings"
2026-02-04 11:06:13 +00:00
)
type SpawnTool struct {
2026-03-17 04:50:32 +00:00
spawner SubTurnSpawner
defaultModel string
maxTokens int
temperature float64
feat: add multi-agent routing with declarative bindings
Implement per-agent workspace/model/session isolation with 7-level
priority routing cascade (peer > parent_peer > guild > team > account >
channel > default). Backward compatible - empty agents.list creates
implicit "main" agent from defaults.
Core components:
- routing/agent_id.go: ID normalization with pre-compiled regex
- routing/session_key.go: 4 DM scope modes with identity links
- routing/route.go: RouteResolver with priority-based binding matcher
- agent/instance.go: Per-agent state (workspace, sessions, tools, model)
- agent/registry.go: Agent lifecycle, route resolution, subagent ACL
Integration:
- config.go: AgentModelConfig (flexible JSON), bindings, session config
- loop.go: Complete rewrite for multi-agent dispatch
- Channel adapters: peer_kind/peer_id metadata (telegram, discord, slack)
- spawn.go: Subagent allowlist enforcement per agent
Validated end-to-end with Discord channel-based bindings, default
fallback routing, and per-agent session persistence.
2026-02-13 15:12:33 +00:00
allowlistCheck func ( targetAgentID string ) bool
2026-02-04 11:06:13 +00:00
}
2026-03-05 01:57:33 +00:00
// Compile-time check: SpawnTool implements AsyncExecutor.
var _ AsyncExecutor = ( * SpawnTool ) ( nil )
2026-02-04 11:06:13 +00:00
func NewSpawnTool ( manager * SubagentManager ) * SpawnTool {
2026-03-19 05:51:11 +00:00
if manager == nil {
return & SpawnTool { }
}
2026-02-04 11:06:13 +00:00
return & SpawnTool {
2026-03-17 04:50:32 +00:00
defaultModel : manager . defaultModel ,
maxTokens : manager . maxTokens ,
temperature : manager . temperature ,
2026-02-04 11:06:13 +00:00
}
}
2026-03-17 04:50:32 +00:00
// SetSpawner sets the SubTurnSpawner for direct sub-turn execution.
func ( t * SpawnTool ) SetSpawner ( spawner SubTurnSpawner ) {
t . spawner = spawner
}
2026-02-04 11:06:13 +00:00
func ( t * SpawnTool ) Name ( ) string {
return "spawn"
}
func ( t * SpawnTool ) Description ( ) string {
return "Spawn a subagent to handle a task in the background. Use this for complex or time-consuming tasks that can run independently. The subagent will complete the task and report back when done."
}
2026-02-18 19:48:23 +00:00
func ( t * SpawnTool ) Parameters ( ) map [ string ] any {
return map [ string ] any {
2026-02-04 11:06:13 +00:00
"type" : "object" ,
2026-02-18 19:48:23 +00:00
"properties" : map [ string ] any {
"task" : map [ string ] any {
2026-02-04 11:06:13 +00:00
"type" : "string" ,
"description" : "The task for subagent to complete" ,
} ,
2026-02-18 19:48:23 +00:00
"label" : map [ string ] any {
2026-02-04 11:06:13 +00:00
"type" : "string" ,
"description" : "Optional short label for the task (for display)" ,
} ,
2026-02-18 19:48:23 +00:00
"agent_id" : map [ string ] any {
feat: add multi-agent routing with declarative bindings
Implement per-agent workspace/model/session isolation with 7-level
priority routing cascade (peer > parent_peer > guild > team > account >
channel > default). Backward compatible - empty agents.list creates
implicit "main" agent from defaults.
Core components:
- routing/agent_id.go: ID normalization with pre-compiled regex
- routing/session_key.go: 4 DM scope modes with identity links
- routing/route.go: RouteResolver with priority-based binding matcher
- agent/instance.go: Per-agent state (workspace, sessions, tools, model)
- agent/registry.go: Agent lifecycle, route resolution, subagent ACL
Integration:
- config.go: AgentModelConfig (flexible JSON), bindings, session config
- loop.go: Complete rewrite for multi-agent dispatch
- Channel adapters: peer_kind/peer_id metadata (telegram, discord, slack)
- spawn.go: Subagent allowlist enforcement per agent
Validated end-to-end with Discord channel-based bindings, default
fallback routing, and per-agent session persistence.
2026-02-13 15:12:33 +00:00
"type" : "string" ,
"description" : "Optional target agent ID to delegate the task to" ,
} ,
2026-02-04 11:06:13 +00:00
} ,
"required" : [ ] string { "task" } ,
}
}
feat: add multi-agent routing with declarative bindings
Implement per-agent workspace/model/session isolation with 7-level
priority routing cascade (peer > parent_peer > guild > team > account >
channel > default). Backward compatible - empty agents.list creates
implicit "main" agent from defaults.
Core components:
- routing/agent_id.go: ID normalization with pre-compiled regex
- routing/session_key.go: 4 DM scope modes with identity links
- routing/route.go: RouteResolver with priority-based binding matcher
- agent/instance.go: Per-agent state (workspace, sessions, tools, model)
- agent/registry.go: Agent lifecycle, route resolution, subagent ACL
Integration:
- config.go: AgentModelConfig (flexible JSON), bindings, session config
- loop.go: Complete rewrite for multi-agent dispatch
- Channel adapters: peer_kind/peer_id metadata (telegram, discord, slack)
- spawn.go: Subagent allowlist enforcement per agent
Validated end-to-end with Discord channel-based bindings, default
fallback routing, and per-agent session persistence.
2026-02-13 15:12:33 +00:00
func ( t * SpawnTool ) SetAllowlistChecker ( check func ( targetAgentID string ) bool ) {
t . allowlistCheck = check
}
2026-02-18 19:48:23 +00:00
func ( t * SpawnTool ) Execute ( ctx context . Context , args map [ string ] any ) * ToolResult {
2026-03-05 01:57:33 +00:00
return t . execute ( ctx , args , nil )
}
// ExecuteAsync implements AsyncExecutor. The callback is passed through to the
// subagent manager as a call parameter — never stored on the SpawnTool instance.
2026-03-21 09:12:45 +00:00
func ( t * SpawnTool ) ExecuteAsync (
ctx context . Context ,
args map [ string ] any ,
cb AsyncCallback ,
) * ToolResult {
2026-03-05 01:57:33 +00:00
return t . execute ( ctx , args , cb )
}
2026-03-21 09:12:45 +00:00
func ( t * SpawnTool ) execute (
ctx context . Context ,
args map [ string ] any ,
cb AsyncCallback ,
) * ToolResult {
2026-02-04 11:06:13 +00:00
task , ok := args [ "task" ] . ( string )
2026-02-24 20:39:49 +00:00
if ! ok || strings . TrimSpace ( task ) == "" {
return ErrorResult ( "task is required and must be a non-empty string" )
2026-02-04 11:06:13 +00:00
}
label , _ := args [ "label" ] . ( string )
feat: add multi-agent routing with declarative bindings
Implement per-agent workspace/model/session isolation with 7-level
priority routing cascade (peer > parent_peer > guild > team > account >
channel > default). Backward compatible - empty agents.list creates
implicit "main" agent from defaults.
Core components:
- routing/agent_id.go: ID normalization with pre-compiled regex
- routing/session_key.go: 4 DM scope modes with identity links
- routing/route.go: RouteResolver with priority-based binding matcher
- agent/instance.go: Per-agent state (workspace, sessions, tools, model)
- agent/registry.go: Agent lifecycle, route resolution, subagent ACL
Integration:
- config.go: AgentModelConfig (flexible JSON), bindings, session config
- loop.go: Complete rewrite for multi-agent dispatch
- Channel adapters: peer_kind/peer_id metadata (telegram, discord, slack)
- spawn.go: Subagent allowlist enforcement per agent
Validated end-to-end with Discord channel-based bindings, default
fallback routing, and per-agent session persistence.
2026-02-13 15:12:33 +00:00
agentID , _ := args [ "agent_id" ] . ( string )
// Check allowlist if targeting a specific agent
if agentID != "" && t . allowlistCheck != nil {
if ! t . allowlistCheck ( agentID ) {
2026-02-13 15:24:26 +00:00
return ErrorResult ( fmt . Sprintf ( "not allowed to spawn agent '%s'" , agentID ) )
feat: add multi-agent routing with declarative bindings
Implement per-agent workspace/model/session isolation with 7-level
priority routing cascade (peer > parent_peer > guild > team > account >
channel > default). Backward compatible - empty agents.list creates
implicit "main" agent from defaults.
Core components:
- routing/agent_id.go: ID normalization with pre-compiled regex
- routing/session_key.go: 4 DM scope modes with identity links
- routing/route.go: RouteResolver with priority-based binding matcher
- agent/instance.go: Per-agent state (workspace, sessions, tools, model)
- agent/registry.go: Agent lifecycle, route resolution, subagent ACL
Integration:
- config.go: AgentModelConfig (flexible JSON), bindings, session config
- loop.go: Complete rewrite for multi-agent dispatch
- Channel adapters: peer_kind/peer_id metadata (telegram, discord, slack)
- spawn.go: Subagent allowlist enforcement per agent
Validated end-to-end with Discord channel-based bindings, default
fallback routing, and per-agent session persistence.
2026-02-13 15:12:33 +00:00
}
}
2026-02-04 11:06:13 +00:00
2026-03-17 04:50:32 +00:00
// Build system prompt for spawned subagent
2026-03-21 09:12:45 +00:00
systemPrompt := fmt . Sprintf (
` You are a spawned subagent running in the background . Complete the given task independently and report back when done .
2026-02-04 11:06:13 +00:00
2026-03-21 09:12:45 +00:00
Task : % s ` ,
task ,
)
2026-03-17 04:50:32 +00:00
if label != "" {
2026-03-21 09:12:45 +00:00
systemPrompt = fmt . Sprintf (
` You are a spawned subagent labeled "%s" running in the background . Complete the given task independently and report back when done .
2026-03-17 04:50:32 +00:00
2026-03-21 09:12:45 +00:00
Task : % s ` ,
label ,
task ,
)
2026-03-05 01:57:33 +00:00
}
2026-03-17 04:50:32 +00:00
// Use spawner if available (direct SpawnSubTurn call)
if t . spawner != nil {
// Launch async sub-turn in goroutine
go func ( ) {
result , err := t . spawner . SpawnSubTurn ( ctx , SubTurnConfig {
Model : t . defaultModel ,
Tools : nil , // Will inherit from parent via context
SystemPrompt : systemPrompt ,
MaxTokens : t . maxTokens ,
Temperature : t . temperature ,
Async : true , // Async execution
} )
if err != nil {
result = ErrorResult ( fmt . Sprintf ( "Spawn failed: %v" , err ) ) . WithError ( err )
}
// Call callback if provided
if cb != nil {
cb ( ctx , result )
}
} ( )
// Return immediate acknowledgment
if label != "" {
return AsyncResult ( fmt . Sprintf ( "Spawned subagent '%s' for task: %s" , label , task ) )
}
return AsyncResult ( fmt . Sprintf ( "Spawned subagent for task: %s" , task ) )
2026-02-04 11:06:13 +00:00
}
2026-03-17 04:50:32 +00:00
// Fallback: spawner not configured
2026-03-19 05:51:11 +00:00
return ErrorResult ( "Subagent manager not configured" )
2026-02-04 11:06:13 +00:00
}