+// 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 {
+ if err != nil {
+ return fmt.Errorf("load interrupted workers: %w", err)
+ }
+
+ return nil
+ }
+
+ text := "The daemon restarted, cutting " + strings.Join(names, ", ") + "; nothing was lost: go on."
+ err = r.note(text)
+ if err != nil {
+ return fmt.Errorf("record interrupted workers: %w", err)
+ }
+
+ for _, name := range names {
+ _, err = r.Tell(ctx, name, text)
+ if err != nil {
+ r.logger.Error("subagent lost at restart", "name", name, "err", 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()
+
+ err = r.save()
+ if err != nil {
+ return fmt.Errorf("save resumed workers: %w", err)
+ }
+
+ return nil
+}
+
+// load reads the names of work.json. A missing file names none.
+func (r *SubagentRuns) load() ([]string, error) {
+ data, err := os.ReadFile(r.workPath())
+ if errors.Is(err, os.ErrNotExist) {
+ return nil, nil
+ }
+
+ if err != nil {
+ return nil, fmt.Errorf("read running subagents: %w", err)
+ }
+
+ var file SubagentWorkFile
+
+ err = json.Unmarshal(data, &file)
+ if err != nil {
+ return nil, fmt.Errorf("decode running subagents: %w", err)
+ }
+
+ return file.Running, nil
+}
+
+// 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 "", fmt.Errorf("append running worker message: %w", 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 "", fmt.Errorf("open worker log: %w", err)
+ }
+
+ effort, err := r.effort(subagent)
+ if err != nil {
+ return "", fmt.Errorf("read worker effort: %w", 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 "", fmt.Errorf("split worker view: %w", err)
+ }
+
+ _, err = subagent.Append("user", message)
+ if err == nil {
+ err = subagent.Flush(ctx)
+ }
+
+ if err != nil {
+ return "", fmt.Errorf("save worker message: %w", err)
+ }
+
+ err = r.start(ctx, &SubagentRun{
+ subagent: subagent,
+ view: view,
+ effort: effort,
+ })
+ if err != nil {
+ return "", fmt.Errorf("start told worker: %w", err)
+ }
+
+ return "Sent. " + name + "'s reply will reach you as a message.", nil
+}
+
+// effort returns the effort of subagent from the note at the start of its log:
+// the effort of the agent, or the effort that the note names.
+func (r *SubagentRuns) effort(subagent *Subagent) (string, error) {
+ note, err := subagent.First()
+ if err != nil {
+ return "", fmt.Errorf("read worker note: %w", err)
+ }
+
+ effort, own := strings.CutPrefix(note.Text, subagentNote+subagentNoteEffort)
+ switch {
+ case note.Kind != "note":
+ case own:
+ return effort, nil
+ case note.Text == subagentNote:
+ return r.agent.config.Effort, nil
+ }
+
+ return "", fmt.Errorf("the log of %s does not start with a subagent note", subagent.name)
+}
+
+// Snapshot locks the run state, memory, and subagent logs while snapshot reads
+// the agent files. The lock order agrees with Stop, which logs under the run
+// lock.
+func (r *SubagentRuns) Snapshot(ctx context.Context, snapshot func() error) error {
+ r.mutex.Lock()
+ defer r.mutex.Unlock()
+
+ return r.memory.Snapshot(ctx, func() error {
+ return r.subagents.Snapshot(ctx, snapshot)
+ })
+}
+
+// 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()