X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/37d5f21b23da5fbd6b44fed41f653927669571ed..7c84ed87ef71088b880a3765b2653e1ded57bf14:/subagent_run.go diff --git a/subagent_run.go b/subagent_run.go index 16cf2bd..67cf79d 100644 --- a/subagent_run.go +++ b/subagent_run.go @@ -5,12 +5,14 @@ import ( "encoding/json" "errors" "fmt" + "log" "maps" "os" "path/filepath" "slices" "strings" "sync" + "time" ) const ( @@ -43,9 +45,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 +69,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 +241,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 lost at restart: %v", name, err) r.report(name, subagentReport(name, []string{"failed: " + err.Error()}), subagentFailed) } } @@ -313,8 +318,8 @@ func (r *SubagentRuns) effort(subagent *Subagent) (string, error) { return "", fmt.Errorf("the log of %s does not start with a subagent note", subagent.name) } -// start runs run in the background. Its context keeps the values of ctx, but -// only Stop and Close end it. +// 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)) @@ -344,6 +349,8 @@ func (r *SubagentRuns) start(ctx context.Context, run *SubagentRun) error { r.workers.Add(1) r.mutex.Unlock() + run.started = r.subagents.clock() + r.logger.Printf("subagent %s started", run.subagent.name) r.notify(Event{ Kind: "subagent", Text: run.subagent.name, @@ -398,14 +405,15 @@ func (r *SubagentRuns) workPath() string { return filepath.Join(r.agent.dir, subagentWorkFileName) } -// run runs the chains of run and reports its end, unless Close ended it. A -// chain follows while messages wait after the final answer of the chain before +// 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". func (r *SubagentRuns) run(ctx context.Context, run *SubagentRun) { defer r.workers.Done() defer run.cancel() var replies []string + var failure error stopped := false status := subagentFinished for more := true; more; { @@ -419,6 +427,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 +456,16 @@ func (r *SubagentRuns) run(ctx context.Context, run *SubagentRun) { status = subagentStopped } + length := durationText(r.subagents.clock().Sub(run.started)) + switch status { + case subagentStopped: + r.logger.Printf("subagent %s stopped after %s", run.subagent.name, length) + case subagentFailed: + r.logger.Printf("subagent %s failed after %s: %v", run.subagent.name, length, failure) + default: + r.logger.Printf("subagent %s done after %s", run.subagent.name, length) + } + r.report(run.subagent.name, subagentReport(run.subagent.name, replies), status) } @@ -505,7 +524,7 @@ func (r *SubagentRuns) take(run *SubagentRun) []Message { // 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, -// and whether Stop ended it. The mutex of SubagentRuns protects the messages +// whether Stop ended it, and the time it started. The mutex of SubagentRuns protects the messages // and the stop. type SubagentRun struct { subagent *Subagent @@ -514,6 +533,7 @@ type SubagentRun struct { cancel context.CancelFunc pending []Message stopped bool + started time.Time } // SubagentWorkFile is the content of work.json.