Closes the loop #451-#453 opened. archie-agent (#453) now publishes its own logs to agentexec.SubjectForSystem(taskID) over NATS, and a task log sink (#452) exists to persist them, but nothing on the daemon side subscribed, demuxed, or wrote anything -- the messages had nowhere to land. - agentexec: SubjectSystemWildcard + TaskIDFromSystemSubject, the inverse of SubjectForSystem, for a daemon subscribing to every task's system subject with one wildcard subscription. - logging: TaskRegistry manages one open TaskSink per running task attempt, mirroring every write onto the live dashboard Feed the same way a locally-written log would appear. Nil-receiver-safe, matching every other optional composition-wired Daemon field. - daemon: Daemon.process opens a task's log sink for its own duration (covering both the sandboxed-container and in-process dispatch paths uniformly) and closes it on every return path. - cmd/archied: constructs the registry, subscribes core NATS to SubjectSystemWildcard, and wires TaskLogs into both Daemon and webui.Server. - webui: archiving a task now removes its log directory too, so archived tasks' logs don't accumulate forever. Documented as a known, accepted race: a task can reach terminal status (satisfying ArchiveTask's precondition) before process() itself returns and closes the sink, so a very late system-log write can race an archive's Remove. Not corrupting -- the write lands on a soon-to-be-unlinked inode and is discarded -- and not worth fixing here: it would mean tying Close to the store transition instead of to process()'s return, a larger change than this issue's scope. Adversarial review (fresh subagent) found and fixed two real issues in the first draft: a doc comment accidentally spliced between two functions during an extraction meant to fix a lint regression, and a genuine test gap on openTaskLog (added, using TaskRegistry.Write's own true/false contract as the observable -- no Store/Forge/Trees fakes needed, since openTaskLog only touches TaskLogs and Log).
123 lines
3.9 KiB
Go
123 lines
3.9 KiB
Go
package logging
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"os"
|
|
"sync"
|
|
)
|
|
|
|
// TaskRegistry manages one open log destination per currently-running task
|
|
// attempt, shared between whatever starts and ends a task's run (Open,
|
|
// Close) and whatever receives that task's remotely-shipped log records
|
|
// (Write) -- in the daemon, two different goroutines: task dispatch, and
|
|
// the NATS system-log subscription handler.
|
|
//
|
|
// The zero value is not usable; construct with NewTaskRegistry. A nil
|
|
// *TaskRegistry is safe to call every method on and always behaves as
|
|
// "task logging is not configured" -- the same "optional, nil means
|
|
// disabled" convention every other composition-wired field in Daemon
|
|
// follows, so callers never need a separate nil check before using one.
|
|
type TaskRegistry struct {
|
|
baseDir string
|
|
feed *Feed
|
|
opts TaskSinkOptions
|
|
|
|
mu sync.Mutex
|
|
open map[int64]*registeredSink
|
|
}
|
|
|
|
type registeredSink struct {
|
|
sink *TaskSink
|
|
logger *slog.Logger
|
|
}
|
|
|
|
// NewTaskRegistry builds a registry writing under baseDir, mirroring every
|
|
// write into feed (may be nil to skip mirroring) the same way a locally
|
|
// logged entry would appear on the dashboard.
|
|
func NewTaskRegistry(baseDir string, feed *Feed, opts TaskSinkOptions) *TaskRegistry {
|
|
return &TaskRegistry{baseDir: baseDir, feed: feed, opts: opts, open: make(map[int64]*registeredSink)}
|
|
}
|
|
|
|
// Open starts logging for a task attempt. A previous attempt already open
|
|
// under the same task ID is closed first: a retry reuses the task ID at a
|
|
// higher Attempt, and the old attempt's sink must stop receiving lines
|
|
// under its name rather than silently keep taking them.
|
|
func (r *TaskRegistry) Open(taskID int64, attempt int) error {
|
|
if r == nil {
|
|
return nil
|
|
}
|
|
sink, err := NewTaskSink(r.baseDir, taskID, attempt, r.opts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
logger := slog.New(NewFeedHandler(sink.Logger().Handler(), r.feed))
|
|
|
|
r.mu.Lock()
|
|
prev := r.open[taskID]
|
|
r.open[taskID] = ®isteredSink{sink: sink, logger: logger}
|
|
r.mu.Unlock()
|
|
|
|
if prev != nil {
|
|
return prev.sink.Close()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Close stops logging for a task and releases its file handle. Safe to call
|
|
// for a task with no open sink -- every caller ending a task's run calls
|
|
// this unconditionally, whether or not Open ever succeeded for it.
|
|
func (r *TaskRegistry) Close(taskID int64) error {
|
|
if r == nil {
|
|
return nil
|
|
}
|
|
r.mu.Lock()
|
|
entry, ok := r.open[taskID]
|
|
delete(r.open, taskID)
|
|
r.mu.Unlock()
|
|
if !ok {
|
|
return nil
|
|
}
|
|
return entry.sink.Close()
|
|
}
|
|
|
|
// Write appends one remotely-shipped entry to taskID's open sink, mirroring
|
|
// it into the live dashboard feed the same way a locally-written log would
|
|
// be. Reports false for a task with no open sink -- not an error: a system
|
|
// log message is fire-and-forget best effort, and a late or duplicate
|
|
// delivery after the task finished (or one this daemon instance never
|
|
// dispatched) is expected, not exceptional.
|
|
func (r *TaskRegistry) Write(taskID int64, entry Entry) bool {
|
|
if r == nil {
|
|
return false
|
|
}
|
|
r.mu.Lock()
|
|
got, ok := r.open[taskID]
|
|
r.mu.Unlock()
|
|
if !ok {
|
|
return false
|
|
}
|
|
|
|
var level slog.Level
|
|
_ = level.UnmarshalText([]byte(entry.Level)) // unparseable falls back to Info, slog.Level's zero value
|
|
|
|
attrs := make([]slog.Attr, 0, len(entry.Fields))
|
|
for k, v := range entry.Fields {
|
|
attrs = append(attrs, slog.Any(k, v))
|
|
}
|
|
got.logger.LogAttrs(context.Background(), level, entry.Message, attrs...)
|
|
return true
|
|
}
|
|
|
|
// Remove deletes taskID's entire log directory -- every attempt, every
|
|
// rotated generation -- so task logs don't accumulate forever once a task
|
|
// is archived. Safe to call whether or not a sink is currently open (Close
|
|
// first regardless: this only removes files, it does not stop writes to an
|
|
// open sink) and whether or not the directory exists at all.
|
|
func (r *TaskRegistry) Remove(taskID int64) error {
|
|
if r == nil {
|
|
return nil
|
|
}
|
|
return os.RemoveAll(TaskLogDir(r.baseDir, taskID))
|
|
}
|