]> git.giacomolli.uk - onechat/blobdiff - subagent_run.go
Log each schedule run by its ID
[onechat] / subagent_run.go
index 16cf2bd03340a7271c985859f739777811810b46..7d3687c48f08691aed53f590dab7d10023e39e34 100644 (file)
@@ -5,6 +5,7 @@ import (
        "encoding/json"
        "errors"
        "fmt"
+       "log"
        "maps"
        "os"
        "path/filepath"
@@ -43,9 +44,10 @@ var errSubagentClosed = errors.New("the daemon stops, so no subagent starts")
 // of the subagent. A message of Tell that comes during a chain joins its next
 // request after the tool results. One that comes after the final answer starts
 // one more chain, which reads the log again. The report of the run goes to
-// report: the final answer of each chain, or the error of a failed call. Stop
-// ends one run, and its report says that it stopped. At the stop of the daemon
-// the runs end with no report.
+// report: the final answer of each chain, or the error of a failed call. The
+// logger gets one line at the end of a run: done, failed with the cause, or
+// stopped. Stop ends one run, and its report says that it stopped. At the stop
+// of the daemon the runs end with no report.
 //
 // 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
@@ -66,6 +68,7 @@ type SubagentRuns struct {
        report    func(name, text, status string)
        note      func(text string) error
        fail      func(err error)
+       logger    *log.Logger
        notify    func(Event)
 
        mutex   sync.Mutex
@@ -237,6 +240,7 @@ func (r *SubagentRuns) Resume(ctx context.Context) error {
        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)
                }
        }
@@ -406,6 +410,7 @@ func (r *SubagentRuns) run(ctx context.Context, run *SubagentRun) {
        defer run.cancel()
 
        var replies []string
+       var failure error
        stopped := false
        status := subagentFinished
        for more := true; more; {
@@ -419,6 +424,7 @@ func (r *SubagentRuns) run(ctx context.Context, run *SubagentRun) {
                case err != nil:
                        reply = "failed: " + err.Error()
                        status = subagentFailed
+                       failure = err
                }
 
                more = err == nil && ctx.Err() == nil && len(run.pending) > 0
@@ -447,6 +453,15 @@ func (r *SubagentRuns) run(ctx context.Context, run *SubagentRun) {
                status = subagentStopped
        }
 
+       switch status {
+       case subagentStopped:
+               r.logger.Printf("subagent %s is stopped by the user", run.subagent.name)
+       case subagentFailed:
+               r.logger.Printf("subagent %s failed, and its report tells the agent: %v", run.subagent.name, failure)
+       default:
+               r.logger.Printf("subagent %s is done, and its report goes to the agent", run.subagent.name)
+       }
+
        r.report(run.subagent.name, subagentReport(run.subagent.name, replies), status)
 }