]> git.giacomolli.uk - onechat/blobdiff - subagent_run.go
Make intentional error discards explicit
[onechat] / subagent_run.go
index 67cf79d7ac14949738e2e167ee8269d45b9409a2..24e4f8229c975a3f9fca2e7e3002530e7da76a99 100644 (file)
@@ -5,7 +5,7 @@ import (
        "encoding/json"
        "errors"
        "fmt"
        "encoding/json"
        "errors"
        "fmt"
-       "log"
+       "log/slog"
        "maps"
        "os"
        "path/filepath"
        "maps"
        "os"
        "path/filepath"
@@ -53,8 +53,8 @@ var errSubagentClosed = errors.New("the daemon stops, so no subagent starts")
 // work.json in the directory of the agent holds the names of the running
 // subagents. Each start and each end of a run writes it whole, but Close does
 // not, so after a stop of the daemon it names the runs that the stop cut, and
 // work.json in the directory of the agent holds the names of the running
 // subagents. Each start and each end of a run writes it whole, but Close does
 // not, so after a stop of the daemon it names the runs that the stop cut, and
-// Resume goes on with them. The logs alone cannot tell this, because a stop
-// can cut a run just after its final reply.
+// Resume goes on with them. The logs alone cannot tell this, because a stop can
+// cut a run just after its final reply.
 //
 // Spawn and Tell split the view with the layout of the agent, which has no
 // lock. They are tools, so they run in the goroutine of the turns, as each
 //
 // Spawn and Tell split the view with the layout of the agent, which has no
 // lock. They are tools, so they run in the goroutine of the turns, as each
@@ -69,7 +69,7 @@ type SubagentRuns struct {
        report    func(name, text, status string)
        note      func(text string) error
        fail      func(err error)
        report    func(name, text, status string)
        note      func(text string) error
        fail      func(err error)
-       logger    *log.Logger
+       logger    *slog.Logger
        notify    func(Event)
 
        mutex   sync.Mutex
        notify    func(Event)
 
        mutex   sync.Mutex
@@ -78,6 +78,25 @@ type SubagentRuns struct {
        workers sync.WaitGroup
 }
 
        workers sync.WaitGroup
 }
 
+// SubagentRun is one run of a subagent: the view that its requests get, its
+// effort, the end of its context, the messages of Tell that wait for a request,
+// whether Stop ended it, and the time it started. The mutex of SubagentRuns
+// protects the messages and the stop.
+type SubagentRun struct {
+       subagent *Subagent
+       view     []string
+       effort   string
+       cancel   context.CancelFunc
+       pending  []Message
+       stopped  bool
+       started  time.Time
+}
+
+// SubagentWorkFile is the content of work.json.
+type SubagentWorkFile struct {
+       Running []string `json:"running"`
+}
+
 // 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.
 // 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.
@@ -87,6 +106,7 @@ func (r *SubagentRuns) Spawn(ctx context.Context, tasks []string, effort *string
        }
 
        r.mutex.Lock()
        }
 
        r.mutex.Lock()
+
        closed := r.closed
        r.mutex.Unlock()
 
        closed := r.closed
        r.mutex.Unlock()
 
@@ -132,140 +152,6 @@ func (r *SubagentRuns) Spawn(ctx context.Context, tasks []string, effort *string
        return "Started " + strings.Join(names, ", ") + ". Each one's report will reach you as a message when it finishes.", nil
 }
 
        return "Started " + strings.Join(names, ", ") + ". Each one's report will reach you as a message when it finishes.", 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 "", 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 lost at restart: %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()
-}
-
 // 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) {
 // 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) {
@@ -298,32 +184,13 @@ func (r *SubagentRuns) create(ctx context.Context, note, task string, view []str
        })
 }
 
        })
 }
 
-// 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 "", 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)
-}
-
 // start runs run in the background and logs its start. Its context keeps the
 // values of ctx, but only Stop and Close end it.
 func (r *SubagentRuns) start(ctx context.Context, run *SubagentRun) error {
        ctx, run.cancel = context.WithCancel(context.WithoutCancel(ctx))
 
        r.mutex.Lock()
 // start runs run in the background and logs its start. Its context keeps the
 // values of ctx, but only Stop and Close end it.
 func (r *SubagentRuns) start(ctx context.Context, run *SubagentRun) error {
        ctx, run.cancel = context.WithCancel(context.WithoutCancel(ctx))
 
        r.mutex.Lock()
+
        _, found := r.running[run.subagent.name]
        if r.closed || found {
                r.mutex.Unlock()
        _, found := r.running[run.subagent.name]
        if r.closed || found {
                r.mutex.Unlock()
@@ -350,7 +217,7 @@ func (r *SubagentRuns) start(ctx context.Context, run *SubagentRun) error {
        r.mutex.Unlock()
 
        run.started = r.subagents.clock()
        r.mutex.Unlock()
 
        run.started = r.subagents.clock()
-       r.logger.Printf("subagent %s started", run.subagent.name)
+       r.logger.Info("subagent started", "name", run.subagent.name)
        r.notify(Event{
                Kind: "subagent",
                Text: run.subagent.name,
        r.notify(Event{
                Kind: "subagent",
                Text: run.subagent.name,
@@ -361,28 +228,8 @@ func (r *SubagentRuns) start(ctx context.Context, run *SubagentRun) error {
        return nil
 }
 
        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
-}
-
-// save writes the names of the running subagents to work.json. The caller
-// holds the mutex.
+// save writes the names of the running subagents to work.json. The caller holds
+// the mutex.
 func (r *SubagentRuns) save() error {
        file := SubagentWorkFile{
                Running: append([]string{}, slices.Sorted(maps.Keys(r.running))...),
 func (r *SubagentRuns) save() error {
        file := SubagentWorkFile{
                Running: append([]string{}, slices.Sorted(maps.Keys(r.running))...),
@@ -401,25 +248,26 @@ func (r *SubagentRuns) save() error {
        return nil
 }
 
        return nil
 }
 
-func (r *SubagentRuns) workPath() string {
-       return filepath.Join(r.agent.dir, subagentWorkFileName)
-}
-
 // run runs the chains of run and reports and logs its end with the length of
 // run runs the chains of run and reports and logs its end with the length of
-// the run, unless Close ended it. A chain follows while messages wait after the final answer of the chain before
-// it, and no chain failed. A chain that Stop ends gives the reply "stopped".
+// the run, unless Close ended it. A chain follows while messages wait after the
+// final answer of the chain before it, and no chain failed. A chain that Stop
+// ends gives the reply "stopped".
 func (r *SubagentRuns) run(ctx context.Context, run *SubagentRun) {
        defer r.workers.Done()
        defer run.cancel()
 
 func (r *SubagentRuns) run(ctx context.Context, run *SubagentRun) {
        defer r.workers.Done()
        defer run.cancel()
 
-       var replies []string
-       var failure error
+       var (
+               replies []string
+               failure error
+       )
+
        stopped := false
        status := subagentFinished
        for more := true; more; {
                reply, err := r.chain(ctx, run)
 
                r.mutex.Lock()
        stopped := false
        status := subagentFinished
        for more := true; more; {
                reply, err := r.chain(ctx, run)
 
                r.mutex.Lock()
+
                stopped = run.stopped
                switch {
                case err != nil && stopped:
                stopped = run.stopped
                switch {
                case err != nil && stopped:
@@ -432,13 +280,17 @@ func (r *SubagentRuns) run(ctx context.Context, run *SubagentRun) {
 
                more = err == nil && ctx.Err() == nil && len(run.pending) > 0
                run.pending = nil
 
                more = err == nil && ctx.Err() == nil && len(run.pending) > 0
                run.pending = nil
+
                var saveErr error
                var saveErr error
+
                if !more {
                        delete(r.running, run.subagent.name)
                if !more {
                        delete(r.running, run.subagent.name)
+
                        if !r.closed {
                                saveErr = r.save()
                        }
                }
                        if !r.closed {
                                saveErr = r.save()
                        }
                }
+
                r.mutex.Unlock()
 
                if saveErr != nil {
                r.mutex.Unlock()
 
                if saveErr != nil {
@@ -456,19 +308,23 @@ func (r *SubagentRuns) run(ctx context.Context, run *SubagentRun) {
                status = subagentStopped
        }
 
                status = subagentStopped
        }
 
-       length := durationText(r.subagents.clock().Sub(run.started))
+       length := r.subagents.clock().Sub(run.started)
        switch status {
        case subagentStopped:
        switch status {
        case subagentStopped:
-               r.logger.Printf("subagent %s stopped after %s", run.subagent.name, length)
+               r.logger.Info("subagent stopped", "name", run.subagent.name, "after", length)
        case subagentFailed:
        case subagentFailed:
-               r.logger.Printf("subagent %s failed after %s: %v", run.subagent.name, length, failure)
+               r.logger.Error("subagent failed", "name", run.subagent.name, "after", length, "err", failure)
        default:
        default:
-               r.logger.Printf("subagent %s done after %s", run.subagent.name, length)
+               r.logger.Info("subagent done", "name", run.subagent.name, "after", length)
        }
 
        r.report(run.subagent.name, subagentReport(run.subagent.name, replies), status)
 }
 
        }
 
        r.report(run.subagent.name, subagentReport(run.subagent.name, replies), status)
 }
 
+func (r *SubagentRuns) workPath() string {
+       return filepath.Join(r.agent.dir, subagentWorkFileName)
+}
+
 // chain runs one request chain of run and returns its final answer.
 func (r *SubagentRuns) chain(ctx context.Context, run *SubagentRun) (string, error) {
        instructions, err := r.agent.SubagentPrompt()
 // chain runs one request chain of run and returns its final answer.
 func (r *SubagentRuns) chain(ctx context.Context, run *SubagentRun) (string, error) {
        instructions, err := r.agent.SubagentPrompt()
@@ -489,11 +345,13 @@ func (r *SubagentRuns) chain(ctx context.Context, run *SubagentRun) (string, err
        input = append(input, modelUserMessage(r.state()), modelUserMessage(log))
        chain := &Chain{
                client: r.client,
        input = append(input, modelUserMessage(r.state()), modelUserMessage(log))
        chain := &Chain{
                client: r.client,
+               images: r.agent.Images(),
                tools: &Toolbox{
                        log:       run.subagent,
                        chat:      r.memory,
                        subagents: r.subagents,
                        workdir:   run.subagent.WorkDir(),
                tools: &Toolbox{
                        log:       run.subagent,
                        chat:      r.memory,
                        subagents: r.subagents,
                        workdir:   run.subagent.WorkDir(),
+                       images:    r.agent.Images(),
                },
                request: ModelRequest{
                        Model:        r.agent.config.Model,
                },
                request: ModelRequest{
                        Model:        r.agent.config.Model,
@@ -510,8 +368,8 @@ func (r *SubagentRuns) chain(ctx context.Context, run *SubagentRun) (string, err
        return chain.Run(ctx, input)
 }
 
        return chain.Run(ctx, input)
 }
 
-// take returns the messages of Tell that wait for a request of run, and
-// removes them.
+// take returns the messages of Tell that wait for a request of run, and removes
+// them.
 func (r *SubagentRuns) take(run *SubagentRun) []Message {
        r.mutex.Lock()
        defer r.mutex.Unlock()
 func (r *SubagentRuns) take(run *SubagentRun) []Message {
        r.mutex.Lock()
        defer r.mutex.Unlock()
@@ -522,23 +380,194 @@ func (r *SubagentRuns) take(run *SubagentRun) []Message {
        return messages
 }
 
        return messages
 }
 
-// SubagentRun is one run of a subagent: the view that its requests get, its
-// effort, the end of its context, the messages of Tell that wait for a request,
-// whether Stop ended it, and the time it started. The mutex of SubagentRuns protects the messages
-// and the stop.
-type SubagentRun struct {
-       subagent *Subagent
-       view     []string
-       effort   string
-       cancel   context.CancelFunc
-       pending  []Message
-       stopped  bool
-       started  time.Time
+// 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
 }
 
 }
 
-// SubagentWorkFile is the content of work.json.
-type SubagentWorkFile struct {
-       Running []string `json:"running"`
+// 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.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()
+
+       return r.save()
+}
+
+// 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 "", 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
+}
+
+// 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 "", 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()
 }
 
 // subagentReport returns the report of the subagent name with its replies: the
 }
 
 // subagentReport returns the report of the subagent name with its replies: the