| @@ -71,13 +71,42 @@ func (e Event) MarshalJSON() ([]byte, error) { |
| 71 | 71 | return json.Marshal(m) |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | + // sink is the writer a logger and everything derived from it share. Derived |
| 75 | + // loggers exist so a subagent can stamp its id on every line without opening a |
| 76 | + // second file, and sharing the lock is what keeps their lines whole. |
| 77 | + type sink struct { |
| 78 | + mu sync.Mutex |
| 79 | + w io.Writer |
| 80 | + closer io.Closer |
| 81 | + } |
| 82 | + |
| 74 | 83 | // Logger appends events to a writer. The zero value and a nil *Logger are both |
| 75 | 84 | // usable and simply drop events, so callers never need a nil check. |
| 76 | 85 | type Logger struct { |
| 77 | | - mu sync.Mutex |
| 78 | | - w io.Writer |
| 79 | | - closer io.Closer |
| 86 | + sink *sink |
| 80 | 87 | session string |
| 88 | + // agent is the subagent this logger belongs to, empty for the session's |
| 89 | + // own. It is stamped into every event so one log file can be split by |
| 90 | + // agent afterwards. |
| 91 | + agent string |
| 92 | + } |
| 93 | + |
| 94 | + // For returns a logger that stamps every event with an agent id and writes to |
| 95 | + // the same place this one does. Closing the original closes both; a derived |
| 96 | + // logger owns no file of its own. |
| 97 | + func (l *Logger) For(agent string) *Logger { |
| 98 | + if l == nil { |
| 99 | + return nil |
| 100 | + } |
| 101 | + return &Logger{sink: l.sink, session: l.session, agent: agent} |
| 102 | + } |
| 103 | + |
| 104 | + // Agent reports which subagent this logger stamps, empty for the session's own. |
| 105 | + func (l *Logger) Agent() string { |
| 106 | + if l == nil { |
| 107 | + return "" |
| 108 | + } |
| 109 | + return l.agent |
| 81 | 110 | } |
| 82 | 111 | |
| 83 | 112 | // Open appends to path, creating parent directories as needed. |
| @@ -91,13 +120,13 @@ func Open(path, session string) (*Logger, error) { |
| 91 | 120 | if err != nil { |
| 92 | 121 | return nil, fmt.Errorf("metrics: %w", err) |
| 93 | 122 | } |
| 94 | | - return &Logger{w: f, closer: f, session: session}, nil |
| 123 | + return &Logger{sink: &sink{w: f, closer: f}, session: session}, nil |
| 95 | 124 | } |
| 96 | 125 | |
| 97 | 126 | // NewWriter logs to an arbitrary writer. Useful in tests and in the eval runner, |
| 98 | 127 | // which keeps a separate log per run. |
| 99 | 128 | func NewWriter(w io.Writer, session string) *Logger { |
| 100 | | - return &Logger{w: w, session: session} |
| 129 | + return &Logger{sink: &sink{w: w}, session: session} |
| 101 | 130 | } |
| 102 | 131 | |
| 103 | 132 | // Session returns the session id stamped onto every event. |
| @@ -111,7 +140,7 @@ func (l *Logger) Session() string { |
| 111 | 140 | // Log writes one event. Errors are dropped: a broken metrics file must never |
| 112 | 141 | // take down a run. |
| 113 | 142 | func (l *Logger) Log(ev Event) { |
| 114 | | - if l == nil || l.w == nil { |
| 143 | + if l == nil || l.sink == nil || l.sink.w == nil { |
| 115 | 144 | return |
| 116 | 145 | } |
| 117 | 146 | if ev.Time.IsZero() { |
| @@ -120,13 +149,25 @@ func (l *Logger) Log(ev Event) { |
| 120 | 149 | if ev.Session == "" { |
| 121 | 150 | ev.Session = l.session |
| 122 | 151 | } |
| 152 | + if l.agent != "" { |
| 153 | + // A copy, because the caller's map is theirs: stamping it in place |
| 154 | + // would mutate a map they may reuse or share with another goroutine. |
| 155 | + extra := make(map[string]any, len(ev.Extra)+1) |
| 156 | + for k, v := range ev.Extra { |
| 157 | + extra[k] = v |
| 158 | + } |
| 159 | + if _, taken := extra["agent"]; !taken { |
| 160 | + extra["agent"] = l.agent |
| 161 | + } |
| 162 | + ev.Extra = extra |
| 163 | + } |
| 123 | 164 | b, err := json.Marshal(ev) |
| 124 | 165 | if err != nil { |
| 125 | 166 | return |
| 126 | 167 | } |
| 127 | | - l.mu.Lock() |
| 128 | | - defer l.mu.Unlock() |
| 129 | | - l.w.Write(append(b, '\n')) |
| 168 | + l.sink.mu.Lock() |
| 169 | + defer l.sink.mu.Unlock() |
| 170 | + l.sink.w.Write(append(b, '\n')) |
| 130 | 171 | } |
| 131 | 172 | |
| 132 | 173 | // ToolCall records one dispatch. err nil means success. |
| @@ -147,8 +188,8 @@ func (l *Logger) ToolCall(name string, d time.Duration, err error, extra map[str |
| 147 | 188 | |
| 148 | 189 | // Close closes the underlying file, if there is one. |
| 149 | 190 | func (l *Logger) Close() error { |
| 150 | | - if l == nil || l.closer == nil { |
| 191 | + if l == nil || l.sink == nil || l.sink.closer == nil { |
| 151 | 192 | return nil |
| 152 | 193 | } |
| 153 | | - return l.closer.Close() |
| 194 | + return l.sink.closer.Close() |
| 154 | 195 | } |