"encoding/json"
"errors"
"fmt"
- "log"
+ "log/slog"
"maps"
"os"
"path/filepath"
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
}
r.mutex.Lock()
+
closed := r.closed
r.mutex.Unlock()
// 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()
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.logger.Error("subagent lost at restart", "name", name, "err", err)
r.report(name, subagentReport(name, []string{"failed: " + err.Error()}), subagentFailed)
}
}
// 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()
ctx, run.cancel = context.WithCancel(context.WithoutCancel(ctx))
r.mutex.Lock()
+
_, found := r.running[run.subagent.name]
if r.closed || found {
r.mutex.Unlock()
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,
}
var file SubagentWorkFile
+
err = json.Unmarshal(data, &file)
if err != nil {
return nil, fmt.Errorf("decode running subagents: %w", err)
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 = run.stopped
switch {
case err != nil && stopped:
more = err == nil && ctx.Err() == nil && len(run.pending) > 0
run.pending = nil
+
var saveErr error
+
if !more {
delete(r.running, run.subagent.name)
+
if !r.closed {
saveErr = r.save()
}
}
+
r.mutex.Unlock()
if saveErr != nil {
status = subagentStopped
}
- length := durationText(r.subagents.clock().Sub(run.started))
+ length := r.subagents.clock().Sub(run.started)
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:
- 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:
- 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)