Log an Assistant turn's phases at debug level
Each Assistant turn now logs one debug line when it ends: the turn id, attempt, outcome, whether its process was cold, and the milliseconds from the runner accepting it to its process starting (prompt written), its first tool call, its first token and its reply being queued for the instance. It holds no text, no tokens and not the person. Coordinator and job lanes log nothing new. The runner logged at info only, so EQUIPO_RUNNER_LOG_LEVEL=debug (in the environment or the env file) now turns its debug lines on.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+140
−1
cmd/equipo-runner/main.go+10 −1cmd/equipo-runner/main_test.go+17 −0internal/runner/thin/assistant_test.go+85 −0internal/runner/thin/engine.go+28 −0
Intent
diff --git a/cmd/equipo-runner/main.go b/cmd/equipo-runner/main.go| index bfdafd3c..6f714040 100644 |
| --- a/cmd/equipo-runner/main.go |
| +++ b/cmd/equipo-runner/main.go |
| @@ -122,7 +122,7 @@ func run(ctx context.Context, args []string, input io.Reader, output, errorOutpu |
| err = runner.Reset(ctx, cfg) |
| case "run": |
| opts := thin.OptionsFromEnv(getenv) |
| - opts.Version, opts.Logger = rastrillo.BuildVersion, slog.New(slog.NewTextHandler(errorOutput, nil)) |
| + opts.Version, opts.Logger = rastrillo.BuildVersion, slog.New(slog.NewTextHandler(errorOutput, logOptions(getenv))) |
| logStart(opts.Logger, cfg, opts.Version, envFile) |
| err = thin.Run(ctx, cfg, opts) |
| if errors.Is(err, thin.ErrSuperseded) { |
| @@ -175,6 +175,15 @@ func main() { |
| // logStart is the runner's first line: what starts, where it connects |
| // (the origin's host only) and from which env file. No credential. |
| +// logOptions is the runner's log level: info, or debug with |
| +// EQUIPO_RUNNER_LOG_LEVEL=debug (an Assistant turn's phases, for one). |
| +func logOptions(getenv func(string) string) *slog.HandlerOptions { |
| + if getenv("EQUIPO_RUNNER_LOG_LEVEL") == "debug" { |
| + return &slog.HandlerOptions{Level: slog.LevelDebug} |
| + } |
| + return &slog.HandlerOptions{Level: slog.LevelInfo} |
| +} |
| + |
| func logStart(log *slog.Logger, cfg runner.Config, version, envFile string) { |
| role := wire.RolePrimary |
| if cfg.WorkerOnly || runner.ReadRole(cfg.StateDir) == wire.RoleWorker { |
diff --git a/cmd/equipo-runner/main_test.go b/cmd/equipo-runner/main_test.go| index 57e7da8f..a056e12f 100644 |
| --- a/cmd/equipo-runner/main_test.go |
| +++ b/cmd/equipo-runner/main_test.go |
| @@ -3,6 +3,7 @@ package main |
| import ( |
| "bytes" |
| "context" |
| + "log/slog" |
| "os" |
| "path/filepath" |
| "strings" |
| @@ -93,3 +94,19 @@ func TestInstallOpenCodeArguments(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// The runner logs at info unless EQUIPO_RUNNER_LOG_LEVEL=debug asks for |
| +// its debug lines too (an Assistant turn's phases among them). |
| +func TestRunnerLogLevel(t *testing.T) { |
| + for v, want := range map[string]slog.Level{"": slog.LevelInfo, "info": slog.LevelInfo, "verbose": slog.LevelInfo, "debug": slog.LevelDebug} { |
| + o := logOptions(func(k string) string { |
| + if k == "EQUIPO_RUNNER_LOG_LEVEL" { |
| + return v |
| + } |
| + return "" |
| + }) |
| + if got := o.Level.Level(); got != want { |
| + t.Errorf("EQUIPO_RUNNER_LOG_LEVEL=%q: level %v, want %v", v, got, want) |
| + } |
| + } |
| +} |
diff --git a/internal/runner/thin/assistant_test.go b/internal/runner/thin/assistant_test.go| index 7631d240..eea601b2 100644 |
| --- a/internal/runner/thin/assistant_test.go |
| +++ b/internal/runner/thin/assistant_test.go |
| @@ -4,10 +4,12 @@ import ( |
| "context" |
| "database/sql" |
| "encoding/json" |
| + "log/slog" |
| "os" |
| "path/filepath" |
| "slices" |
| "strings" |
| + "sync" |
| "testing" |
| "time" |
| @@ -533,3 +535,86 @@ func TestForgetKeepsPreview(t *testing.T) { |
| t.Fatal("the preview stayed up after the forget that releases it") |
| } |
| } |
| + |
| +// timedCoordinator replies with the timing a harness reports. |
| +type timedCoordinator struct{ timing *wire.TurnTiming } |
| + |
| +func (c *timedCoordinator) Turn(_ context.Context, _ TurnInput, emit func(string)) TurnResult { |
| + emit("private words") |
| + return TurnResult{Text: "private words", Timing: c.timing} |
| +} |
| +func (c *timedCoordinator) Close() {} |
| + |
| +// An Assistant turn logs its phases at debug level, with durations from |
| +// when the runner accepted it: the process started, the first token, the |
| +// reply queued. The line holds no text and no tokens; a coordinator turn |
| +// logs none. |
| +func TestAssistantTurnLogsItsPhases(t *testing.T) { |
| + ctx, cancel := context.WithCancel(context.Background()) |
| + defer cancel() |
| + state, err := OpenState(ctx, t.TempDir()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer state.Close() |
| + var mu sync.Mutex |
| + var out strings.Builder |
| + logger := slog.New(slog.NewTextHandler(writerFunc(func(p []byte) (int, error) { |
| + mu.Lock() |
| + defer mu.Unlock() |
| + return out.Write(p) |
| + }), &slog.HandlerOptions{Level: slog.LevelDebug})) |
| + coord := &timedCoordinator{timing: &wire.TurnTiming{Cold: true, StartMs: 3900, FirstToolMs: 3500, FirstTextMs: 4900, ResultMs: 5500}} |
| + e := &Engine{State: state, Logger: logger, Emit: func(wire.DeltaReport) {}, Notify: func() {}, Open: func(string, string, runner.AccountBinding) (Coordinator, error) { |
| + return coord, nil |
| + }} |
| + if err := e.Start(ctx); err != nil { |
| + t.Fatal(err) |
| + } |
| + defer e.Wait() |
| + defer cancel() |
| + p := "ana@example.com" |
| + for _, m := range []wire.MessageCommand{{Person: p, TurnID: "tc", Attempt: 1, Text: "private words"}, {Person: p, TurnID: "ta", Attempt: 1, Text: "private words", Lane: wire.LaneAssistant}} { |
| + if err := state.Accept(ctx, "f"+m.TurnID, m); err != nil { |
| + t.Fatal(err) |
| + } |
| + e.Wake(p, m.Lane) |
| + } |
| + deadline := time.Now().Add(5 * time.Second) |
| + var lines []string |
| + for time.Now().Before(deadline) { |
| + mu.Lock() |
| + lines = nil |
| + for _, l := range strings.Split(out.String(), "\n") { |
| + if strings.Contains(l, `msg="assistant turn"`) { |
| + lines = append(lines, l) |
| + } |
| + } |
| + mu.Unlock() |
| + if len(lines) > 0 { |
| + break |
| + } |
| + time.Sleep(20 * time.Millisecond) |
| + } |
| + if len(lines) != 1 { |
| + t.Fatalf("phase lines %q\n%s", lines, out.String()) |
| + } |
| + line := lines[0] |
| + for _, want := range []string{"level=DEBUG", "turn=ta", "outcome=replied", "cold=true", "started_ms=3900", "first_tool_ms=7400", "first_token_ms=8800", "reply_ms="} { |
| + if !strings.Contains(line, want) { |
| + t.Errorf("phase line lacks %s: %s", want, line) |
| + } |
| + } |
| + mu.Lock() |
| + all := out.String() |
| + mu.Unlock() |
| + for _, never := range []string{"private", "turn=tc", "token=", p} { |
| + if strings.Contains(all, never) { |
| + t.Errorf("the log holds %q: %s", never, all) |
| + } |
| + } |
| +} |
| + |
| +type writerFunc func([]byte) (int, error) |
| + |
| +func (f writerFunc) Write(p []byte) (int, error) { return f(p) } |
diff --git a/internal/runner/thin/engine.go b/internal/runner/thin/engine.go| index 493b6bb4..70514b47 100644 |
| --- a/internal/runner/thin/engine.go |
| +++ b/internal/runner/thin/engine.go |
| @@ -220,6 +220,33 @@ func (e *Engine) log(msg string, err error) { |
| } |
| } |
| +// phases logs an Assistant turn's phases at debug level, each as the |
| +// milliseconds since the runner accepted the turn: its process started |
| +// (the prompt written; a cold turn includes the process's start), its |
| +// first tool call, its first token, and its reply queued for the |
| +// instance. Only the turn id, never text, tokens or the person. |
| +func (e *Engine) phases(r run, res TurnResult) { |
| + if e.Logger == nil || r.Lane != wire.LaneAssistant || r.Received.IsZero() { |
| + return |
| + } |
| + outcome := res.Reason |
| + if outcome == "" { |
| + outcome = wire.TurnReplied |
| + } |
| + attrs := []any{"turn", r.TurnID, "attempt", r.Attempt, "outcome", outcome} |
| + if t := res.Timing; t != nil { |
| + at := func(ms int64) int64 { |
| + if ms == 0 { |
| + return 0 |
| + } |
| + return t.StartMs + ms |
| + } |
| + attrs = append(attrs, "cold", t.Cold, "started_ms", t.StartMs, "first_tool_ms", at(t.FirstToolMs), "first_token_ms", at(t.FirstTextMs)) |
| + } |
| + attrs = append(attrs, "reply_ms", time.Since(r.Received).Milliseconds()) |
| + e.Logger.Debug("assistant turn", attrs...) |
| +} |
| + |
| func (e *Engine) loop(k workerKey, w *worker) { |
| defer e.wg.Done() |
| defer func() { |
| @@ -301,6 +328,7 @@ func (e *Engine) runOne(person string, w *worker, r run) { |
| } |
| e.log("runner report", err) |
| e.Notify() |
| + e.phases(r, res) |
| } |
| if r.Cancel { |
| report(TurnResult{Reason: "cancelled"}) |