diff --git a/internal/logging/tasksink.go b/internal/logging/tasksink.go new file mode 100644 index 000000000..eee7cb98e --- /dev/null +++ b/internal/logging/tasksink.go @@ -0,0 +1,65 @@ +package logging + +import ( + "fmt" + "log/slog" + "path/filepath" + "strconv" +) + +// TaskLogPath returns the log file path for one task attempt under baseDir +// (typically /logs/tasks). Each task gets its own subdirectory so +// an attempt's rotated generations (attempt-N.jsonl.1, .2, ...) stay grouped +// with that task rather than interleaved with every other task's. +func TaskLogPath(baseDir string, taskID int64, attempt int) string { + return filepath.Join(baseDir, strconv.FormatInt(taskID, 10), fmt.Sprintf("attempt-%d.jsonl", attempt)) +} + +// TaskSinkOptions configures a per-task log sink. The zero value selects the +// same defaults as the daemon-wide log: DefaultMaxSizeMB, DefaultKeep, and +// info level. +type TaskSinkOptions struct { + // MaxSizeMB rotates the file once it exceeds this size. Zero selects + // DefaultMaxSizeMB. + MaxSizeMB int + // Keep is how many rotated files to retain. Zero selects DefaultKeep. + Keep int + // Level is "debug", "info", "warn" or "error". Empty selects info. + Level string +} + +// TaskSink is a per-task, per-attempt log destination. It writes the same +// on-disk JSONL shape as the daemon-wide log (see Entry, Tail, Query, +// Components), so a task's history is readable with the same reader -- +// there is deliberately no second log format to keep in sync. +type TaskSink struct { + logger *slog.Logger + file *rotatingFile +} + +// NewTaskSink opens the JSONL sink for one task attempt under baseDir, +// creating its directory if needed, with the same size-based rotation as +// the daemon-wide log. +// +// Unlike New (the daemon-wide logger), a task sink that fails to open +// returns the error rather than falling back to stderr: New degrades +// because losing the daemon over a broken log path is worse than losing the +// durable copy, but there is no equivalent daemon-level fallback for one +// task's log -- the caller (a workflow stage, or the daemon's NATS log +// consumer) is better placed to decide what a missing sink means for that +// task than this package is. +func NewTaskSink(baseDir string, taskID int64, attempt int, opts TaskSinkOptions) (*TaskSink, error) { + path := TaskLogPath(baseDir, taskID, attempt) + w, err := newRotatingFile(path, opts.MaxSizeMB, opts.Keep) + if err != nil { + return nil, fmt.Errorf("logging: open task sink %s: %w", path, err) + } + handler := slog.NewJSONHandler(w, &slog.HandlerOptions{Level: parseLevel(opts.Level)}) + return &TaskSink{logger: slog.New(handler), file: w}, nil +} + +// Logger returns the slog.Logger writing to this sink. +func (s *TaskSink) Logger() *slog.Logger { return s.logger } + +// Close closes the underlying file. +func (s *TaskSink) Close() error { return s.file.Close() } diff --git a/internal/logging/tasksink_test.go b/internal/logging/tasksink_test.go new file mode 100644 index 000000000..d5b18c488 --- /dev/null +++ b/internal/logging/tasksink_test.go @@ -0,0 +1,190 @@ +package logging + +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" + "strings" + "testing" +) + +func TestTaskLogPath(t *testing.T) { + tests := []struct { + name string + baseDir string + taskID int64 + attempt int + want string + }{ + {"first attempt", "/var/log/archie/tasks", 42, 1, "/var/log/archie/tasks/42/attempt-1.jsonl"}, + {"later attempt", "/var/log/archie/tasks", 42, 3, "/var/log/archie/tasks/42/attempt-3.jsonl"}, + {"different task", "/var/log/archie/tasks", 7, 1, "/var/log/archie/tasks/7/attempt-1.jsonl"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := TaskLogPath(tt.baseDir, tt.taskID, tt.attempt) + want := filepath.FromSlash(tt.want) + if got != want { + t.Errorf("TaskLogPath(%q, %d, %d) = %q, want %q", tt.baseDir, tt.taskID, tt.attempt, got, want) + } + }) + } +} + +// TestNewTaskSinkWritesEntriesReadableByTail pins that a task sink's on-disk +// format is exactly what Tail/Query/Components already know how to parse -- +// a task's log history must be readable with the same reader the daemon-wide +// log uses, not a second incompatible format. +func TestNewTaskSinkWritesEntriesReadableByTail(t *testing.T) { + baseDir := t.TempDir() + + sink, err := NewTaskSink(baseDir, 42, 1, TaskSinkOptions{}) + if err != nil { + t.Fatalf("NewTaskSink: %v", err) + } + sink.Logger().Warn("gate failed", "component", "gate", "cmd", "go build ./...") + if err := sink.Close(); err != nil { + t.Fatalf("Close: %v", err) + } + + path := TaskLogPath(baseDir, 42, 1) + result, err := Tail(path, Query{}) + if err != nil { + t.Fatalf("Tail: %v", err) + } + if len(result.Entries) != 1 { + t.Fatalf("got %d entries, want 1: %+v", len(result.Entries), result.Entries) + } + entry := result.Entries[0] + if entry.Message != "gate failed" { + t.Errorf("Message = %q, want %q", entry.Message, "gate failed") + } + if entry.Level != "WARN" { + t.Errorf("Level = %q, want WARN", entry.Level) + } + if got := entry.Fields["component"]; got != "gate" { + t.Errorf("Fields[component] = %v, want gate", got) + } +} + +func TestNewTaskSinkCreatesNestedTaskDirectory(t *testing.T) { + baseDir := t.TempDir() + + sink, err := NewTaskSink(baseDir, 99, 1, TaskSinkOptions{}) + if err != nil { + t.Fatalf("NewTaskSink: %v", err) + } + t.Cleanup(func() { _ = sink.Close() }) + + if _, err := os.Stat(TaskLogPath(baseDir, 99, 1)); err != nil { + t.Errorf("task log file not created: %v", err) + } +} + +// TestNewTaskSinkHonoursRotationOptions confirms TaskSinkOptions.MaxSizeMB +// and .Keep both actually reach the underlying rotatingFile, not just +// MaxSizeMB. Writing enough to rotate past DefaultKeep (5) generations while +// asking for Keep: 2 is deliberate: if Keep were silently dropped and +// defaulted, generation .3 would still exist and this test would catch it -- +// a smaller write volume that only ever produces 1-2 rotations would pass +// whether or not Keep was wired through at all. Rotation ordering itself +// (which generation holds what) is already covered by TestRotation; this +// only checks that TaskSink forwards both options. +func TestNewTaskSinkHonoursRotationOptions(t *testing.T) { + baseDir := t.TempDir() + + sink, err := NewTaskSink(baseDir, 1, 1, TaskSinkOptions{MaxSizeMB: 1, Keep: 2}) + if err != nil { + t.Fatalf("NewTaskSink: %v", err) + } + line := strings.Repeat("x", 64*1024) + for range 200 { // ~12MB against a 1MB cap: well past DefaultKeep (5) generations. + sink.Logger().Info(line) + } + if err := sink.Close(); err != nil { + t.Fatal(err) + } + + path := TaskLogPath(baseDir, 1, 1) + for i := 1; i <= 2; i++ { + if _, err := os.Stat(fmt.Sprintf("%s.%d", path, i)); err != nil { + t.Errorf("rotated file .%d missing, want rotation to have engaged: %v", i, err) + } + } + if _, err := os.Stat(path + ".3"); err == nil { + t.Error("generation .3 exists, want retention capped at Keep=2 (not the default of 5, " + + "which .3 surviving would indicate Keep was dropped)") + } +} + +func TestNewTaskSinkLevelFiltersEntries(t *testing.T) { + baseDir := t.TempDir() + + sink, err := NewTaskSink(baseDir, 1, 1, TaskSinkOptions{Level: "warn"}) + if err != nil { + t.Fatalf("NewTaskSink: %v", err) + } + sink.Logger().Info("info-line") + sink.Logger().Warn("warn-line") + if err := sink.Close(); err != nil { + t.Fatal(err) + } + + body, err := os.ReadFile(TaskLogPath(baseDir, 1, 1)) + if err != nil { + t.Fatal(err) + } + if strings.Contains(string(body), "info-line") { + t.Error("info-line present, want it filtered at warn level") + } + if !strings.Contains(string(body), "warn-line") { + t.Error("warn-line missing") + } +} + +// TestNewTaskSinkRejectsUnusableDirectory confirms a task sink surfaces the +// error instead of quietly falling back -- unlike the daemon-wide New, which +// intentionally degrades to stderr because losing the daemon over a log path +// is worse than losing the durable copy. A task sink has no such tradeoff: +// there is no equivalent fallback destination for one task's log, so a task +// sink that fails to open should fail loudly and let the caller decide. +func TestNewTaskSinkRejectsUnusableDirectory(t *testing.T) { + blocker := filepath.Join(t.TempDir(), "not-a-dir") + if err := os.WriteFile(blocker, []byte("x"), 0o600); err != nil { + t.Fatal(err) + } + + _, err := NewTaskSink(blocker, 1, 1, TaskSinkOptions{}) + if err == nil { + t.Error("NewTaskSink returned nil error for an unusable base directory") + } +} + +func TestTaskSinkOutputIsValidJSONLines(t *testing.T) { + baseDir := t.TempDir() + sink, err := NewTaskSink(baseDir, 1, 1, TaskSinkOptions{}) + if err != nil { + t.Fatalf("NewTaskSink: %v", err) + } + sink.Logger().Info("one") + sink.Logger().Info("two") + if err := sink.Close(); err != nil { + t.Fatal(err) + } + + body, err := os.ReadFile(TaskLogPath(baseDir, 1, 1)) + if err != nil { + t.Fatal(err) + } + lines := strings.Split(strings.TrimSpace(string(body)), "\n") + if len(lines) != 2 { + t.Fatalf("got %d lines, want 2", len(lines)) + } + for _, line := range lines { + var raw map[string]any + if err := json.Unmarshal([]byte(line), &raw); err != nil { + t.Errorf("line is not JSON: %v (%q)", err, line) + } + } +}