+// 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()
+}
+