+// Tell sends message to the subagent name. A running subagent reads it between
+// its tool calls, and its report answers it. A finished subagent goes on from
+// its log with a new run, with the view of now and the effort of its note.
+func (r *SubagentRuns) Tell(ctx context.Context, name, message string) (string, error) {
+ r.mutex.Lock()
+ run, found := r.running[name]
+ if found {
+ defer r.mutex.Unlock()
+
+ told, err := run.subagent.Append("user", message)
+ if err != nil {
+ return "", err
+ }
+
+ run.pending = append(run.pending, told)
+
+ return "Sent. " + name + " reads it between its tool calls, and its report answers it.", nil
+ }
+
+ closed := r.closed
+ r.mutex.Unlock()
+
+ if closed {
+ return "", errSubagentClosed
+ }
+
+ subagent, err := r.subagents.Get(ctx, name)
+ if err != nil {
+ return "", err
+ }
+
+ effort, err := r.effort(subagent)
+ if err != nil {
+ return "", err
+ }
+
+ 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
+ }
+
+ _, err = subagent.Append("user", message)
+ if err == nil {
+ err = subagent.Flush(ctx)
+ }
+
+ if err != nil {
+ return "", err
+ }
+
+ err = r.start(ctx, &SubagentRun{
+ subagent: subagent,
+ view: view,
+ effort: effort,
+ })
+ if err != nil {
+ return "", err
+ }
+
+ return "Sent. " + name + "'s reply will reach you as a message.", nil
+}
+
+// Stop ends the run of the subagent name and tells if it ran. The chat gets the
+// note "Stopped by the user: NAME." under the mutex, so the note comes before
+// the report of the run.
+func (r *SubagentRuns) Stop(name string) (bool, error) {
+ r.mutex.Lock()
+ defer r.mutex.Unlock()
+
+ run, found := r.running[name]
+ if !found {
+ return false, nil
+ }
+
+ err := r.note("Stopped by the user: " + name + ".")
+ if err != nil {
+ return true, err
+ }
+
+ run.stopped = true
+ run.cancel()
+
+ return true, nil
+}
+
+// Resume goes on with the runs that a stop of the daemon cut, as work.json
+// names them. The chat gets the note "The daemon restarted, cutting NAMES;
+// nothing was lost: go on.", and each cut subagent gets the same text as a
+// message of Tell. A subagent that cannot go on gets a failed report.
+func (r *SubagentRuns) Resume(ctx context.Context) error {
+ names, err := r.load()
+ if err != nil || len(names) == 0 {
+ return err
+ }
+
+ text := "The daemon restarted, cutting " + strings.Join(names, ", ") + "; nothing was lost: go on."
+ err = r.note(text)
+ if err != nil {
+ return err
+ }
+
+ for _, name := range names {
+ _, err = r.Tell(ctx, name, text)
+ if err != nil {
+ r.logger.Printf("subagent %s cannot go on after the restart, and its report tells the agent: %v", name, err)
+ r.report(name, subagentReport(name, []string{"failed: " + err.Error()}), subagentFailed)
+ }
+ }
+
+ // A subagent that cannot go on does not run, so work.json no longer
+ // names it, and the next start does not try it again.
+ r.mutex.Lock()
+ defer r.mutex.Unlock()
+
+ return r.save()
+}
+
+// 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()
+}
+