"encoding/json"
"errors"
"fmt"
+ "log"
"maps"
"os"
"path/filepath"
// 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
report func(name, text, status string)
note func(text string) error
fail func(err error)
+ logger *log.Logger
notify func(Event)
mutex sync.Mutex
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)
}
}
defer run.cancel()
var replies []string
+ var failure error
stopped := false
status := subagentFinished
for more := true; more; {
case err != nil:
reply = "failed: " + err.Error()
status = subagentFailed
+ failure = err
}
more = err == nil && ctx.Err() == nil && len(run.pending) > 0
status = subagentStopped
}
+ switch status {
+ case subagentStopped:
+ r.logger.Printf("subagent %s stopped", run.subagent.name)
+ case subagentFailed:
+ r.logger.Printf("subagent %s failed: %v", run.subagent.name, failure)
+ default:
+ r.logger.Printf("subagent %s done", run.subagent.name)
+ }
+
r.report(run.subagent.name, subagentReport(run.subagent.name, replies), status)
}