Files
archie/internal/infrastructure/staterpc/task_log_test.go
T
sam cf7cc9d891 feat(webui): read a task's log through the State Store and let it be downloaded
The dashboard's attempt-log panel answered every read with "task logging is
optional and was not enabled for this run". That message was false: logging is
wired unconditionally in the daemon, and the log files exist. The dashboard
process simply had no reader -- composition left TaskLogs absent because no
contract carried it -- so the handler saw a nil registry and reported the
disabled shape, which the page rendered as a claim about configuration.

internal/logging now answers whether an attempt has a log at all: Tail sets
Result.Found, so "this attempt has no log" is a value rather than an empty
list indistinguishable from a read that never happened. Its registry gains
TaskLog (a decoded page) and TaskLogContent (the file verbatim, streamed,
because a log is unbounded input), and an absent reader reports
ErrTaskLogsUnavailable rather than an empty page -- a process that cannot read
logs must not implicate the attempt.

Those two answers travel the State Store contract as storecontract.TaskLogStore
(ReadTaskLog, StreamTaskLogContent) and are rehydrated on the client by
(Unavailable, canonical message), so archie-ui composes the reader from its own
store client instead of opening another process's files. The daemon's registry
is wired as the server-side reader in the State Store process only.

On top of that read, GET /api/tasks/{id}/logs/download serves the attempt's log
as a Content-Disposition attachment named task-<id>-attempt-<n>.log, and the
panel offers it. The JSON route keeps its shape and gains found, so the page
states the attempt has no log instead of blaming a setting.

Task log files stay owned by their directory's owner: archiving still removes a
task's log directory, and that removal is the daemon's, not the dashboard's.
2026-09-17 12:12:43 +00:00

201 lines
7.6 KiB
Go

package staterpc
import (
"bytes"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"github.com/samcharles93/archie-core/internal/logging"
"github.com/samcharles93/archie-core/internal/store"
)
// TestTaskLogContract drives the task-log read group through both adapters,
// matching the §11 conformance requirement the rest of this package already
// satisfies: the same battery runs against the local reader (a
// *logging.TaskRegistry over this process's own state directory) and against
// the remote *Client.
//
// The two answers this pins as DIFFERENT are the point of the contract. A
// process with no reader reports unavailability; an attempt with no log file
// reports found=false with no error. A caller that cannot tell those apart
// tells an operator task logging is switched off when it is not.
func TestTaskLogContract(t *testing.T) {
for _, mode := range []string{"local", "grpc"} {
t.Run(mode, func(t *testing.T) {
ctx := t.Context()
baseDir := filepath.Join(t.TempDir(), "logs", "tasks")
reader := logging.NewTaskRegistry(baseDir, logging.NewFeed(10), logging.TaskSinkOptions{})
const taskID = 7
if err := reader.Open(taskID, 0); err != nil {
t.Fatalf("open sink: %v", err)
}
reader.Write(taskID, logging.Entry{Level: "INFO", Message: "starting", Fields: map[string]any{"component": "daemon"}})
reader.Write(taskID, logging.Entry{Level: "ERROR", Message: "gate failed", Fields: map[string]any{"component": "gate"}})
if err := reader.Close(taskID); err != nil {
t.Fatalf("close sink: %v", err)
}
var c taskLogContract = reader
if mode == "grpc" {
c = remoteTaskStore(t, store.OpenTest(t), reader)
}
t.Run("a page carries the decoded entries and their fields", func(t *testing.T) {
page, err := c.TaskLog(ctx, taskID, 0, logging.Query{})
if err != nil {
t.Fatalf("TaskLog: %v", err)
}
if !page.Found {
t.Fatal("Found = false for an attempt whose log exists")
}
if len(page.Entries) != 2 {
t.Fatalf("entries = %d, want 2", len(page.Entries))
}
if page.Entries[1].Message != "gate failed" || page.Entries[1].Level != "ERROR" {
t.Errorf("entry = %+v, want the ERROR line the sink wrote", page.Entries[1])
}
if got, _ := page.Entries[0].Fields["component"].(string); got != "daemon" {
t.Errorf("fields = %+v, want the structured component to survive the hop", page.Entries[0].Fields)
}
if !strings.HasSuffix(page.File, "attempt-0.jsonl") {
t.Errorf("file = %q, want the attempt's own path", page.File)
}
})
t.Run("filters and the limit cross the wire", func(t *testing.T) {
page, err := c.TaskLog(ctx, taskID, 0, logging.Query{Levels: []string{"ERROR"}})
if err != nil {
t.Fatalf("TaskLog: %v", err)
}
if len(page.Entries) != 1 || page.Entries[0].Message != "gate failed" {
t.Fatalf("level filter = %+v, want just the ERROR line", page.Entries)
}
page, err = c.TaskLog(ctx, taskID, 0, logging.Query{Component: "daemon"})
if err != nil {
t.Fatalf("TaskLog: %v", err)
}
if len(page.Entries) != 1 || page.Entries[0].Message != "starting" {
t.Fatalf("component filter = %+v, want just the daemon line", page.Entries)
}
page, err = c.TaskLog(ctx, taskID, 0, logging.Query{Contains: "gate"})
if err != nil {
t.Fatalf("TaskLog: %v", err)
}
if len(page.Entries) != 1 || page.Entries[0].Message != "gate failed" {
t.Fatalf("contains filter = %+v, want just the gate line", page.Entries)
}
page, err = c.TaskLog(ctx, taskID, 0, logging.Query{Limit: 1})
if err != nil {
t.Fatalf("TaskLog: %v", err)
}
if len(page.Entries) != 1 || !page.Truncated {
t.Fatalf("limit = 1 gave %d entries (truncated %v), want one and a truncated marker", len(page.Entries), page.Truncated)
}
})
t.Run("components list what the log actually contains", func(t *testing.T) {
page, err := c.TaskLog(ctx, taskID, 0, logging.Query{})
if err != nil {
t.Fatalf("TaskLog: %v", err)
}
if len(page.Components) != 2 || page.Components[0] != "daemon" || page.Components[1] != "gate" {
t.Fatalf("components = %v, want the sorted distinct set", page.Components)
}
})
t.Run("an attempt with no log is found=false, not an error", func(t *testing.T) {
page, err := c.TaskLog(ctx, taskID, 99, logging.Query{})
if err != nil {
t.Fatalf("TaskLog for a missing attempt = %v, want no error", err)
}
if page.Found {
t.Error("Found = true for an attempt with no log file")
}
if len(page.Entries) != 0 {
t.Errorf("entries = %+v, want none", page.Entries)
}
})
t.Run("content is the file verbatim, and found is false when there is none", func(t *testing.T) {
var buf bytes.Buffer
found, err := c.TaskLogContent(ctx, taskID, 0, &buf)
if err != nil {
t.Fatalf("TaskLogContent: %v", err)
}
if !found {
t.Fatal("found = false for an attempt whose log exists")
}
body := buf.String()
if !strings.Contains(body, "gate failed") || !strings.Contains(body, "starting") {
t.Fatalf("content = %q, want the raw log lines", body)
}
// Byte-for-byte: the contract transports the log, it does not
// re-encode it. Every line must still parse as the package's
// own JSONL.
for line := range strings.Lines(strings.TrimSpace(body)) {
if _, ok := loggingTailDecodes(t, line); !ok {
t.Fatalf("line %q does not decode as the logging package's own format", line)
}
}
buf.Reset()
found, err = c.TaskLogContent(ctx, taskID, 99, &buf)
if err != nil {
t.Fatalf("TaskLogContent for a missing attempt = %v, want no error", err)
}
if found || buf.Len() != 0 {
t.Errorf("found = %v with %d bytes, want false and nothing", found, buf.Len())
}
})
})
}
}
// TestTaskLogContractWithoutAReaderIsUnavailable pins the other half of the
// distinction, and the one the dashboard's misleading message came from: a
// service with no task-log reader at all must say UNAVAILABLE rather than
// answer an empty page. An empty page is indistinguishable from an attempt
// that has no log, and the dashboard rendered that as "task logging is
// optional and was not enabled for this run".
func TestTaskLogContractWithoutAReaderIsUnavailable(t *testing.T) {
local := store.OpenTest(t)
// remoteContract leaves Deps.TaskLogs nil, which is what a store service
// sharing no state directory with the daemon looks like.
c := remoteContract(t, local)
logs, ok := c.(taskLogContract)
if !ok {
t.Fatalf("remote client does not carry the task-log contract (%T)", c)
}
if _, err := logs.TaskLog(t.Context(), 1, 0, logging.Query{}); !errors.Is(err, logging.ErrTaskLogsUnavailable) {
t.Errorf("TaskLog without a reader = %v, want ErrTaskLogsUnavailable; an empty page here is what the dashboard read as \"logging was not enabled\"", err)
}
var buf bytes.Buffer
if _, err := logs.TaskLogContent(t.Context(), 1, 0, &buf); !errors.Is(err, logging.ErrTaskLogsUnavailable) {
t.Errorf("TaskLogContent without a reader = %v, want ErrTaskLogsUnavailable", err)
}
}
// loggingTailDecodes reports whether one line is a record the logging package
// can decode, using that package's own reader rather than a second opinion
// about its format.
func loggingTailDecodes(t *testing.T, line string) (logging.Entry, bool) {
t.Helper()
path := filepath.Join(t.TempDir(), "one.jsonl")
if err := os.WriteFile(path, []byte(line+"\n"), 0o600); err != nil {
t.Fatalf("write temp log: %v", err)
}
res, err := logging.Tail(path, logging.Query{})
if err != nil {
t.Fatalf("Tail: %v", err)
}
if len(res.Entries) != 1 {
return logging.Entry{}, false
}
return res.Entries[0], true
}