subagent_resume.go RawHistoryFolder
1package tools
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "strings"
8
9 "github.com/bnema/zut/packages/agent/subagents"
10 "github.com/bnema/zut/packages/core"
11)
12
13// SubagentResumeTool gives a sub-agent a follow-up turn while preserving its
14// existing session context. Every explicit follow-up is accepted durably and
15// runs FIFO after any active resident turn.
16type SubagentResumeTool struct {
17 ResidentManager *subagents.ResidentManager
18 Enabled func() bool
19}
20
21type subagentResumeArgs struct {
22 AgentID string `json:"agent_id"`
23 Prompt string `json:"prompt"`
24}
25
26const subagentResumeSchema = `{
27 "type": "object",
28 "properties": {
29 "agent_id": {
30 "type": "string",
31 "description": "Child id or unique id prefix for the resident sub-agent to continue."
32 },
33 "prompt": {
34 "type": "string",
35 "description": "New manager follow-up for the sub-agent. Its earlier task and conversation remain available in the retained session."
36 }
37 },
38 "required": ["agent_id", "prompt"]
39}`
40
41func (t *SubagentResumeTool) Name() string { return SubagentResumeToolName }
42
43func (t *SubagentResumeTool) Description() string {
44 return "Continue a resident sub-agent with a follow-up prompt and retained session."
45}
46
47func (t *SubagentResumeTool) Schema() json.RawMessage {
48 return json.RawMessage(subagentResumeSchema)
49}
50
51func (t *SubagentResumeTool) Execute(ctx context.Context, raw json.RawMessage, _ func(string)) (core.ToolResult, error) {
52 if ctx != nil {
53 select {
54 case <-ctx.Done():
55 return core.ToolResult{}, ctx.Err()
56 default:
57 }
58 }
59 prefix := t.Name()
60 if t.ResidentManager == nil {
61 return protocolToolError(prefix + ": subagent runtime not available in this mode")
62 }
63 if t.Enabled == nil || !t.Enabled() {
64 return protocolToolError(prefix + ": subagent management is unavailable in this mode")
65 }
66
67 var args subagentResumeArgs
68 if err := decodeSubagentArgs(raw, &args); err != nil {
69 return core.ToolResult{}, err
70 }
71 id := strings.TrimSpace(args.AgentID)
72 if id == "" {
73 return protocolToolError(prefix + ": agent_id is required")
74 }
75 if strings.TrimSpace(args.Prompt) == "" {
76 return protocolToolError(prefix + ": prompt is required")
77 }
78 snapshot, ok := findResidentStatusSnapshot(t.ResidentManager.Snapshot(), id)
79 if !ok {
80 return protocolToolError(fmt.Sprintf("%s: no such agent %q", prefix, id))
81 }
82 if err := t.ResidentManager.Resume(ctx, snapshot.ID, args.Prompt); err != nil {
83 return protocolToolError(prefix + ": " + err.Error())
84 }
85 if updated, ok := t.ResidentManager.SnapshotFor(snapshot.ID); ok {
86 snapshot = updated
87 }
88 return renderResidentAction("resumed", publicResidentStatus(snapshot))
89}