picoclaw/pkg/tools/spawn.go
ZanzyTHEbar 161dd4e3ce fix: restore verified unified kernel baseline
- fix securebus execute-once, redaction-safe persistence, and failure terminalization across main and subagent runs
- harden session continuity, heartbeat isolation, and context-carried tool execution metadata without regressing eval workflows
- restore full verification baseline with prompt/eval/tooling fixes and sync docs to shipped branch truth
2026-04-24 11:51:21 +01:00

93 lines
2.7 KiB
Go

package tools
import (
"context"
"fmt"
)
type SpawnTool struct {
manager *SubagentManager
originChannel string
originChatID string
callback AsyncCallback // For async completion notification
}
func NewSpawnTool(manager *SubagentManager) *SpawnTool {
return &SpawnTool{
manager: manager,
originChannel: "cli",
originChatID: "direct",
}
}
// SetCallback implements AsyncTool interface for async completion notification
func (t *SpawnTool) SetCallback(cb AsyncCallback) {
t.callback = cb
}
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."
}
func (t *SpawnTool) Parameters() map[string]interface{} {
return map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"task": map[string]interface{}{
"type": "string",
"description": "The task for subagent to complete",
},
"label": map[string]interface{}{
"type": "string",
"description": "Optional short label for the task (for display)",
},
"delegated_scope": map[string]interface{}{
"type": "string",
"description": "What part of parent work is delegated. Required for nested delegation.",
},
"kept_work": map[string]interface{}{
"type": "string",
"description": "What work remains with delegator. Required for nested delegation.",
},
},
"required": []string{"task"},
}
}
func (t *SpawnTool) SetContext(channel, chatID string) {
t.originChannel = channel
t.originChatID = chatID
}
func (t *SpawnTool) Execute(ctx context.Context, args map[string]interface{}) *ToolResult {
task, ok := args["task"].(string)
if !ok {
return ErrorResult("task is required")
}
label, _ := args["label"].(string)
delegatedScope, _ := args["delegated_scope"].(string)
keptWork, _ := args["kept_work"].(string)
if t.manager == nil {
return ErrorResult("Subagent manager not configured")
}
originChannel, originChatID := ResolveExecutionTarget(ctx, t.originChannel, t.originChatID)
callback := AsyncCallbackFromContext(ctx)
if callback == nil && (originChannel == "" || originChatID == "") {
callback = t.callback
}
// Pass callback to manager for async completion notification
result, err := t.manager.Spawn(ctx, task, label, delegatedScope, keptWork, originChannel, originChatID, callback)
if err != nil {
return ErrorResult(fmt.Sprintf("failed to spawn subagent: %v", err))
}
// Return AsyncResult since the task runs in background
return AsyncResult(result)
}