package main import ( "context" "errors" "fmt" "os" "slices" "strings" "sync" ) const ( // subagentNote is the first message of the log of a subagent. subagentNote = "subagent" // subagentNoteEffort follows subagentNote when the subagent has its own // effort. subagentNoteEffort = " at effort " ) // errSubagentClosed tells that the runs of the subagents ended with the loop. var errSubagentClosed = errors.New("the daemon stops, so no subagent starts") // SubagentRuns runs the subagents of one agent in the background. Each run is // a goroutine with its own Chain and its own context, which only Close ends. // The first request of each chain holds the view of the agent at the spawn, // then the state of the turn, then the whole log of the subagent. The report of // the run goes to report: its final answer, or the error of a failed call. At // the stop of the daemon the runs end with no report. // // Spawn splits the view with the layout of the agent, which has no lock. Spawn // is a tool, so it runs in the goroutine of the turns, as each split of a turn // does. type SubagentRuns struct { agent *Agent memory *Memory subagents *Subagents layout *ViewLayout client ChainClient state func() string report func(text string, stopped bool) notify func(Event) mutex sync.Mutex running map[string]*SubagentRun closed bool workers sync.WaitGroup } // Spawn starts one subagent for each task and returns their names at once. It // waits until the view has no pending summary, and gives the view to each // subagent. A nil effort is the effort of the agent. func (r *SubagentRuns) Spawn(ctx context.Context, tasks []string, effort *string) (string, error) { if len(tasks) == 0 { return "", errors.New("no tasks") } r.mutex.Lock() closed := r.closed r.mutex.Unlock() if closed { return "", errSubagentClosed } note := subagentNote runEffort := r.agent.config.Effort if effort != nil && !slices.Contains(promptEfforts, *effort) { return "", fmt.Errorf("no effort %q: one of %s", *effort, strings.Join(promptEfforts, ", ")) } if effort != nil && *effort != runEffort { note += subagentNoteEffort + *effort runEffort = *effort } err := r.memory.Settle(ctx) if err != nil { return "", fmt.Errorf("wait for the summaries: %w", err) } view, err := r.layout.Split(r.memory.ViewLines(nil)) if err != nil { return "", err } names := make([]string, 0, len(tasks)) for _, task := range tasks { name, err := r.create(ctx, note, task, view, runEffort) if err != nil && len(names) > 0 { return "", fmt.Errorf("started %s, then: %w", strings.Join(names, ", "), err) } if err != nil { return "", err } names = append(names, name) } return "Started " + strings.Join(names, ", ") + ". Each one's report will reach you as a message when it finishes.", nil } // create makes a new subagent with its note, its task, and its work directory, // and starts its run. func (r *SubagentRuns) create(ctx context.Context, note, task string, view []string, effort string) (string, error) { subagent, err := r.subagents.Create(ctx) if err != nil { return "", err } for _, message := range [][2]string{{"note", note}, {"user", task}} { _, err = subagent.Append(message[0], message[1]) if err != nil { return "", err } } err = subagent.Flush(ctx) if err != nil { return "", err } err = os.MkdirAll(subagent.WorkDir(), 0o700) if err != nil { return "", fmt.Errorf("create work directory: %w", err) } return subagent.name, r.start(ctx, &SubagentRun{ subagent: subagent, view: view, effort: effort, }) } // start runs run in the background. Its context keeps the values of ctx, but // only Close ends it. func (r *SubagentRuns) start(ctx context.Context, run *SubagentRun) error { ctx, run.cancel = context.WithCancel(context.WithoutCancel(ctx)) r.mutex.Lock() if r.closed { r.mutex.Unlock() run.cancel() return errSubagentClosed } r.running[run.subagent.name] = run r.workers.Add(1) r.mutex.Unlock() r.notify(Event{ Kind: "subagent", Text: run.subagent.name, }) go r.run(ctx, run) return nil } // run runs the chain of run and reports its end, unless Close ended it. func (r *SubagentRuns) run(ctx context.Context, run *SubagentRun) { defer r.workers.Done() defer run.cancel() reply := r.chain(ctx, run) r.mutex.Lock() delete(r.running, run.subagent.name) r.mutex.Unlock() if ctx.Err() != nil { return } r.report(subagentReport(run.subagent.name, []string{reply}), false) } // chain runs one request chain of run and returns its final answer, or the // error of the chain after "failed: ". func (r *SubagentRuns) chain(ctx context.Context, run *SubagentRun) string { instructions, err := r.agent.SubagentPrompt() if err != nil { return "failed: " + err.Error() } log, err := run.subagent.Show() if err != nil { return "failed: " + err.Error() } input := make([]any, 0, len(run.view)+2) for _, text := range run.view { input = append(input, modelUserMessage(text)) } input = append(input, modelUserMessage(r.state()), modelUserMessage(log)) chain := &Chain{ client: r.client, tools: &Toolbox{ log: run.subagent, chat: r.memory, subagents: r.subagents, workdir: run.subagent.WorkDir(), }, request: ModelRequest{ Model: r.agent.config.Model, Effort: run.effort, Instructions: instructions, Tools: promptSubagentTools(), Agent: run.subagent.name, }, take: func() []Message { return nil }, } reply, err := chain.Run(ctx, input) if err != nil { return "failed: " + err.Error() } return reply } // Close ends every run and waits for them. No subagent starts after Close. func (r *SubagentRuns) Close() { r.mutex.Lock() r.closed = true for _, run := range r.running { run.cancel() } r.mutex.Unlock() r.workers.Wait() } // SubagentRun is one run of a subagent: the view that its requests get, its // effort, and the end of its context. type SubagentRun struct { subagent *Subagent view []string effort string cancel context.CancelFunc } // subagentReport returns the report of the subagent name with its replies: the // name in brackets, a new line, the replies, and the zoom that gives its whole // chat. func subagentReport(name string, replies []string) string { return "[" + name + "]\n" + strings.Join(replies, "\n\n") + "\n\nFull chat: zoom(\"" + name + "\")" }