Merge pull request #848 from samcharles93/feat/state-store-task-log-download

feat(staterpc): read task logs through the State Store and make them downloadable
This commit is contained in:
sam authored and GitHub committed 2026-09-17 22:38:16 +10:00
commit 2fd0f23971
30 files changed
+2317 -387

No files matched your search

+16
View File
@@ -125,6 +125,22 @@ func (b *boot) openStateStore(ctx context.Context) error {
// degrades that group rather than aborting boot.
func (b *boot) stateStoreDeps(grants *staterpc.TaskGrants) staterpc.Deps {
deps := staterpc.Deps{Tasks: b.st, Log: b.log, Grants: grants}
// Task logs live in the state directory, which this process owns, and the
// dashboard process owns no such directory -- so this is where a task-log
// read is served from (docs/prds/ui-service-boundary.md). The reader is
// the daemon's own registry, built over the same state-directory
// derivation this process uses for its SQLite file; both resolve from the
// same configured DBPath, so they agree on where archie keeps its state.
//
// The nil check is on the registry, not on the interface it is assigned
// to: a nil *logging.TaskRegistry stored in this field produces a non-nil
// interface holding a nil pointer, which the server's own
// `deps.TaskLogs == nil` guard would not catch. Leaving the field unset is
// what makes the RPC answer Unavailable -- the honest "this process cannot
// read logs" -- instead of depending on nil-receiver behaviour to notice.
if b.taskLogs != nil {
deps.TaskLogs = b.taskLogs
}
if cs, ok := b.st.(storecontract.CaptureStore); ok {
deps.Captures = cs
}
+31
View File
@@ -11,7 +11,9 @@ import (
"github.com/samcharles93/archie-core/internal/config"
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
"github.com/samcharles93/archie-core/internal/logging"
"github.com/samcharles93/archie-core/internal/store"
)
@@ -98,6 +100,35 @@ func TestServeStateStoreServesContract(t *testing.T) {
}
}
// TestStateStoreDepsServeTaskLogs proves the standalone State Store process is
// where a task-log read is served from: the daemon process holds no dashboard
// listener any more, so the service the UI process dials is the only place this
// contract can have a server-side reader, and it must be the daemon's own
// registry over the state directory.
//
// Without this wiring the RPC exists and answers Unavailable for every task,
// which the dashboard reports as "this process cannot read logs" -- honest, but
// still not a working download (archie-core-iaqx).
func TestStateStoreDepsServeTaskLogs(t *testing.T) {
b := newBootstrap()
logs := logging.NewTaskRegistry(filepath.Join(t.TempDir(), "logs", "tasks"), logging.NewFeed(10), logging.TaskSinkOptions{})
b.taskLogs = logs
deps := b.stateStoreDeps(&staterpc.TaskGrants{})
if deps.TaskLogs == nil {
t.Fatal("stateStoreDeps leaves TaskLogs nil; the State Store process is the only server-side reader the dashboard can reach")
}
if deps.TaskLogs != storecontract.TaskLogStore(logs) {
t.Fatalf("TaskLogs = %T, want the daemon's own registry over the state directory", deps.TaskLogs)
}
// A boot without a registry must not fabricate one: an empty reader would
// answer found=false for every attempt, which reads as "the attempt has no
// log" rather than "this process cannot read logs".
if plain := (&boot{}).stateStoreDeps(&staterpc.TaskGrants{}); plain.TaskLogs != nil {
t.Errorf("TaskLogs = %T with no registry on the boot, want nil so the RPC reports unavailability", plain.TaskLogs)
}
}
// TestStateStoreDataSurvivesRestart is the .4.7 restart/recovery check: a
// task written by one archie-state-store process must still be there for a
// fresh process opening the same archie.db path, proving b.openStateStore
+27 -1
View File
@@ -38,12 +38,17 @@ type deps struct {
// UpdateRepoField: the configuration owner stays the daemon. The read
// crosses as the snapshot ConfigSource renders; the write routes answer
// 503 and the page hides their controls (archie-core-ymut).
// - LogFeed, TaskLogs, Events, Channels, ReloadChannel, Curators, Memory,
// - LogFeed, Events, Channels, ReloadChannel, Curators, Memory,
// Workflows, WorkRequests, RunningVersions, Chat.Updates,
// UpdateReportPath: no contract exists for these yet.
// Defining one amends the owning service's contract first
// (docs/prds/ui-service-boundary.md:205-207); the route migration is
// archie-core-8cda.5.3.
// - TaskLogs: wired below from the store client's TaskLogStore contract.
// Task log files live in the state directory the State Store owns, so this
// process reads one attempt's log over that contract rather than opening
// the file itself -- which would only work on a single host and is exactly
// what the boundary forbids (archie-core-iaqx).
// - Captures, CaptureIntake, CaptureMaxEvents: wired below. From the
// cutover change (archie-core-8cda.5.4) this process is the only
// listener serving POST /webhooks/capture/{source}: the receiver is a
@@ -77,10 +82,31 @@ func compose(d deps) *webui.Server {
if d.Chat != nil {
srv.Chat = &webui.ChatService{Contract: d.Chat}
}
// The task-log read crosses the State Store contract: the files live in
// the state directory that process owns, so this one asks for the log
// rather than opening a path (docs/prds/ui-service-boundary.md). Withholding
// it would degrade a page that has an owner, and would leave the dashboard
// claiming task logging was not enabled -- see wireTaskLogs.
wireTaskLogs(d, srv)
wireCaptureSurfaces(d, srv)
return srv
}
// wireTaskLogs attaches the task-log read when the store client carries the
// contract, and logs the absence when it does not: a store without it leaves
// the page reporting that this process cannot read logs, which is true, rather
// than that the attempt has none, which it cannot know.
func wireTaskLogs(d deps, srv *webui.Server) {
logs, ok := d.Store.(storecontract.TaskLogStore)
if !ok {
if d.Store != nil {
d.Log.Warn("task logs unavailable: state store does not implement TaskLogStore")
}
return
}
srv.TaskLogs = logs
}
// wireCaptureSurfaces attaches the capture read and the intake receiver.
// Both resolve from d.Store's contracts: CaptureStore backs the inspector's
// list and the mapping preview's scan window, BindingDispatcher resolves the
+10 -1
View File
@@ -88,7 +88,6 @@ func TestComposeUIServerHoldsNoDaemonState(t *testing.T) {
"Memory": srv.Memory,
"Channels": srv.Channels,
"LogFeed": srv.LogFeed,
"TaskLogs": srv.TaskLogs,
"WorkRequests": srv.WorkRequests,
}
for name, handle := range unwired {
@@ -105,6 +104,16 @@ func TestComposeUIServerHoldsNoDaemonState(t *testing.T) {
if srv.Mappings == nil || srv.Bindings == nil {
t.Error("Mappings/Bindings are unwired; both are State Store contracts the composed client implements")
}
// TaskLogs is the same shape as Mappings/Bindings -- the store client
// carries TaskLogStore -- and it is the one that used to answer "task
// logging was not enabled for this run" for every attempt in a deployment
// where logging is unconditional (archie-core-iaqx).
if srv.TaskLogs == nil {
t.Error("TaskLogs is unwired; the State Store client implements TaskLogStore, so leaving it nil degrades a page that has an owner")
}
if _, ok := srv.TaskLogs.(*staterpc.Client); !ok {
t.Errorf("TaskLogs = %T, want the remote *staterpc.Client: the UI process owns no state directory to read a log file from", srv.TaskLogs)
}
if srv.UpdateReportPath != "" {
t.Error("UpdateReportPath is set; host-local update relay stays with the daemon")
}
+17 -5
View File
@@ -14,6 +14,7 @@ import (
"google.golang.org/grpc"
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
"github.com/samcharles93/archie-core/internal/logging"
"github.com/samcharles93/archie-core/internal/store"
"github.com/samcharles93/archie-core/internal/webui"
)
@@ -51,11 +52,13 @@ func TestRoutesWithoutAContractDegradeExplicitly(t *testing.T) {
want: http.StatusServiceUnavailable,
},
{
// The task exists; its logs do not, because the files live on
// the daemon's host. "disabled" is the same answer the daemon
// gives when task logging is off, and the page already reads it.
// Task logs are a State Store read contract now
// (archie-core-iaqx), so the route answers with real data
// rather than the degrade. The task exists and its attempt
// has no log file, which is the found=false answer -- the
// page says the attempt has no log, not that logging is off.
name: "per-task logs", method: http.MethodGet, path: "/api/tasks/" + strconv.FormatInt(taskID, 10) + "/logs",
want: http.StatusOK, emptyJSON: `"disabled":true`,
want: http.StatusOK, emptyJSON: `"found":false`,
},
{
name: "release version and update flow", method: http.MethodGet, path: "/api/version",
@@ -159,8 +162,17 @@ func composeUIProcess(t *testing.T) (*webui.Server, int64) {
}
t.Cleanup(func() { _ = st.Close() })
// The task-log reader lives on the state-directory side of the boundary:
// the State Store process holds a registry over the directory it owns, and
// the dashboard reads through it (archie-core-iaqx). Nothing is written to
// it here, so every attempt reads as found=false.
logs := logging.NewTaskRegistry(filepath.Join(t.TempDir(), "logs", "tasks"), logging.NewFeed(10), logging.TaskSinkOptions{})
target, stop := serveGRPC(t, func(r grpc.ServiceRegistrar) {
staterpc.RegisterServer(r, staterpc.Deps{Tasks: st, Captures: st, BindingDispatcher: st, ConfigSnapshots: st, Log: slog.New(slog.DiscardHandler)})
staterpc.RegisterServer(r, staterpc.Deps{
Tasks: st, Captures: st, BindingDispatcher: st, ConfigSnapshots: st,
TaskLogs: logs, Log: slog.New(slog.DiscardHandler),
})
})
t.Cleanup(stop)
client, closeClient, err := staterpc.Dial(target, "")
+201
View File
@@ -0,0 +1,201 @@
package archieui
import (
"encoding/json"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"path/filepath"
"strconv"
"strings"
"testing"
"google.golang.org/grpc"
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
"github.com/samcharles93/archie-core/internal/logging"
"github.com/samcharles93/archie-core/internal/store"
"github.com/samcharles93/archie-core/internal/webui"
)
// TestUIProcessReadsTaskLogsOverTheStateStoreContract is the dashboard bug,
// pinned end to end: the extracted UI process serves GET /api/tasks/{id}/logs
// and has no way to read a task's persisted log at all, because the only reader
// it ever had was a daemon-local file handle. The page therefore answered "Task
// logging is optional and was not enabled for this run" for every attempt of
// every task, on a deployment where task logging is unconditional and the files
// exist.
//
// The fix is a State Store read contract, so this drives the real thing: a log
// file written on the state-directory side, a gRPC server over it, a dashboard
// composed exactly as the UI process composes one, and an HTTP request through
// the composed handler.
func TestUIProcessReadsTaskLogsOverTheStateStoreContract(t *testing.T) {
srv, taskID, wantAttempt := composeUIProcessWithLog(t)
ts := httptest.NewServer(srv.Handler())
defer ts.Close()
path := "/api/tasks/" + strconv.FormatInt(taskID, 10) + "/logs"
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, ts.URL+path, nil)
if err != nil {
t.Fatalf("request: %v", err)
}
resp, err := ts.Client().Do(req)
if err != nil {
t.Fatalf("GET %s: %v", path, err)
}
defer resp.Body.Close()
raw, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("read body: %v", err)
}
var body struct {
Entries []logging.Entry `json:"entries"`
Attempt int `json:"attempt"`
Found bool `json:"found"`
File string `json:"file"`
}
if err := json.Unmarshal(raw, &body); err != nil {
t.Fatalf("decode response: %v (%s)", err, raw)
}
if !body.Found {
t.Errorf("found = false for an attempt whose log exists on the state-directory side (%s)", raw)
}
if body.Attempt != wantAttempt {
t.Errorf("attempt = %d, want %d (the task's current attempt, resolved without a query param)", body.Attempt, wantAttempt)
}
if len(body.Entries) != 1 || body.Entries[0].Message != "gate failed" {
t.Fatalf("entries = %+v, want the one entry the log holds", body.Entries)
}
if got, ok := body.Entries[0].Fields["component"]; !ok || got != "gate" {
t.Errorf("entry fields = %+v, want the structured component field to survive the hop", body.Entries[0].Fields)
}
// The path is reported, not used: the UI process must not be able to open
// it, and the page only names where the log came from.
if !strings.HasSuffix(body.File, "attempt-"+strconv.Itoa(wantAttempt)+".jsonl") {
t.Errorf("file = %q, want the attempt's own log path", body.File)
}
}
// TestUIProcessServesTaskLogDownload drives the download endpoint through the
// real composition: the bytes arrive over the contract, and the response is an
// attachment named for the attempt rather than an SPA page.
//
// A GET that falls through to the asset handler answers 200 with HTML, which
// is why this asserts the attachment headers and the content, not the status.
func TestUIProcessServesTaskLogDownload(t *testing.T) {
srv, taskID, wantAttempt := composeUIProcessWithLog(t)
ts := httptest.NewServer(srv.Handler())
defer ts.Close()
path := "/api/tasks/" + strconv.FormatInt(taskID, 10) + "/logs/download"
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, ts.URL+path, nil)
if err != nil {
t.Fatalf("request: %v", err)
}
resp, err := ts.Client().Do(req)
if err != nil {
t.Fatalf("GET %s: %v", path, err)
}
defer resp.Body.Close()
raw, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("read body: %v", err)
}
wantName := "task-" + strconv.FormatInt(taskID, 10) + "-attempt-" + strconv.Itoa(wantAttempt) + ".log"
if got := resp.Header.Get("Content-Disposition"); got != `attachment; filename="`+wantName+`"` {
t.Errorf("Content-Disposition = %q, want the attempt's own filename %q (status %d, body %s)", got, wantName, resp.StatusCode, raw)
}
if !strings.Contains(string(raw), "gate failed") {
t.Errorf("download body = %s, want the attempt's own log content", raw)
}
}
// TestUIProcessReportsMissingTaskLogsHonestly pins the second half of the fix:
// with the reader wired and no log on disk, the page must be able to say that
// the ATTEMPT has no log. It is a different claim from "this process cannot
// read logs", and only the second one is about the operator's configuration.
func TestUIProcessReportsMissingTaskLogsHonestly(t *testing.T) {
srv, taskID := composeUIProcess(t)
ts := httptest.NewServer(srv.Handler())
defer ts.Close()
path := "/api/tasks/" + strconv.FormatInt(taskID, 10) + "/logs"
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, ts.URL+path, nil)
if err != nil {
t.Fatalf("request: %v", err)
}
resp, err := ts.Client().Do(req)
if err != nil {
t.Fatalf("GET %s: %v", path, err)
}
defer resp.Body.Close()
raw, _ := io.ReadAll(resp.Body)
var body struct {
Disabled bool `json:"disabled"`
Found bool `json:"found"`
}
if err := json.Unmarshal(raw, &body); err != nil {
t.Fatalf("decode response: %v (%s)", err, raw)
}
if body.Disabled {
t.Fatalf("disabled = true for a process that can read task logs (%s); the page turns that into \"logging was not enabled for this run\"", raw)
}
if body.Found {
t.Errorf("found = true for an attempt with no log file (%s)", raw)
}
}
// composeUIProcessWithLog is composeUIProcess plus one real log on the
// state-directory side: a gRPC server whose task-log reader holds an attempt's
// file, so the dashboard's read has to cross the wire to see anything.
func composeUIProcessWithLog(t *testing.T) (*webui.Server, int64, int) {
t.Helper()
st, err := store.Open(t.Context(), filepath.Join(t.TempDir(), "tasks.sqlite"))
if err != nil {
t.Fatalf("open store: %v", err)
}
t.Cleanup(func() { _ = st.Close() })
task, err := st.EnqueueChatTask(t.Context(), "acme", "widget", "a task", "body", "implement", "")
if err != nil {
t.Fatalf("seed task: %v", err)
}
// The state directory the State Store process owns. The dashboard never
// learns this path: it asks the contract for the log.
logs := logging.NewTaskRegistry(filepath.Join(t.TempDir(), "logs", "tasks"), logging.NewFeed(10), logging.TaskSinkOptions{})
if err := logs.Open(task.ID, task.Attempt); err != nil {
t.Fatalf("open task log: %v", err)
}
logs.Write(task.ID, logging.Entry{
Level: "ERROR", Message: "gate failed", Fields: map[string]any{"component": "gate"},
})
if err := logs.Close(task.ID); err != nil {
t.Fatalf("close task log: %v", err)
}
target, stop := serveGRPC(t, func(r grpc.ServiceRegistrar) {
staterpc.RegisterServer(r, staterpc.Deps{
Tasks: st, Captures: st, BindingDispatcher: st, ConfigSnapshots: st,
TaskLogs: logs, Log: slog.New(slog.DiscardHandler),
})
})
t.Cleanup(stop)
client, closeClient, err := staterpc.Dial(target, "")
if err != nil {
t.Fatalf("dial state store: %v", err)
}
t.Cleanup(closeClient)
options := Options{Listen: "127.0.0.1:0", DependencyTimeout: defaultDependencyTimeout}
srv := compose(deps{
Options: options,
Log: slog.New(slog.DiscardHandler),
Store: client,
Health: newReadinessRegistry(options, client, nil),
})
return srv, task.ID, task.Attempt
}
File diff suppressed because it is too large. Load diff
+99 -2
View File
@@ -45,6 +45,8 @@ const (
StateStoreService_TokensByDay_FullMethodName = "/state.v1.StateStoreService/TokensByDay"
StateStoreService_PutConfigSnapshot_FullMethodName = "/state.v1.StateStoreService/PutConfigSnapshot"
StateStoreService_GetConfigSnapshot_FullMethodName = "/state.v1.StateStoreService/GetConfigSnapshot"
StateStoreService_ReadTaskLog_FullMethodName = "/state.v1.StateStoreService/ReadTaskLog"
StateStoreService_StreamTaskLogContent_FullMethodName = "/state.v1.StateStoreService/StreamTaskLogContent"
StateStoreService_InsertCapture_FullMethodName = "/state.v1.StateStoreService/InsertCapture"
StateStoreService_ListCaptures_FullMethodName = "/state.v1.StateStoreService/ListCaptures"
StateStoreService_StreamCaptures_FullMethodName = "/state.v1.StateStoreService/StreamCaptures"
@@ -114,6 +116,17 @@ type StateStoreServiceClient interface {
// configuration page").
PutConfigSnapshot(ctx context.Context, in *PutConfigSnapshotRequest, opts ...grpc.CallOption) (*PutConfigSnapshotResponse, error)
GetConfigSnapshot(ctx context.Context, in *GetConfigSnapshotRequest, opts ...grpc.CallOption) (*GetConfigSnapshotResponse, error)
// Task log: one task attempt's persisted log. Task log files live in the
// state directory this process owns, so the dashboard reads them over this
// contract rather than opening them itself
// (docs/prds/ui-service-boundary.md).
ReadTaskLog(ctx context.Context, in *ReadTaskLogRequest, opts ...grpc.CallOption) (*ReadTaskLogResponse, error)
// StreamTaskLogContent returns one attempt's log verbatim, one chunk per
// message, for a download. A log file is unbounded input (it rotates at
// DefaultMaxSizeMB), so a unary response would exceed gRPC's 4MiB message
// cap on a real log -- the same reason ListCaptures was superseded by
// StreamCaptures.
StreamTaskLogContent(ctx context.Context, in *StreamTaskLogContentRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[StreamTaskLogContentResponse], error)
// Capture
InsertCapture(ctx context.Context, in *InsertCaptureRequest, opts ...grpc.CallOption) (*InsertCaptureResponse, error)
// Deprecated: Do not use.
@@ -419,6 +432,35 @@ func (c *stateStoreServiceClient) GetConfigSnapshot(ctx context.Context, in *Get
return out, nil
}
func (c *stateStoreServiceClient) ReadTaskLog(ctx context.Context, in *ReadTaskLogRequest, opts ...grpc.CallOption) (*ReadTaskLogResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(ReadTaskLogResponse)
err := c.cc.Invoke(ctx, StateStoreService_ReadTaskLog_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *stateStoreServiceClient) StreamTaskLogContent(ctx context.Context, in *StreamTaskLogContentRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[StreamTaskLogContentResponse], error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
stream, err := c.cc.NewStream(ctx, &StateStoreService_ServiceDesc.Streams[0], StateStoreService_StreamTaskLogContent_FullMethodName, cOpts...)
if err != nil {
return nil, err
}
x := &grpc.GenericClientStream[StreamTaskLogContentRequest, StreamTaskLogContentResponse]{ClientStream: stream}
if err := x.ClientStream.SendMsg(in); err != nil {
return nil, err
}
if err := x.ClientStream.CloseSend(); err != nil {
return nil, err
}
return x, nil
}
// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name.
type StateStoreService_StreamTaskLogContentClient = grpc.ServerStreamingClient[StreamTaskLogContentResponse]
func (c *stateStoreServiceClient) InsertCapture(ctx context.Context, in *InsertCaptureRequest, opts ...grpc.CallOption) (*InsertCaptureResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(InsertCaptureResponse)
@@ -442,7 +484,7 @@ func (c *stateStoreServiceClient) ListCaptures(ctx context.Context, in *ListCapt
func (c *stateStoreServiceClient) StreamCaptures(ctx context.Context, in *StreamCapturesRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[StreamCapturesResponse], error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
stream, err := c.cc.NewStream(ctx, &StateStoreService_ServiceDesc.Streams[0], StateStoreService_StreamCaptures_FullMethodName, cOpts...)
stream, err := c.cc.NewStream(ctx, &StateStoreService_ServiceDesc.Streams[1], StateStoreService_StreamCaptures_FullMethodName, cOpts...)
if err != nil {
return nil, err
}
@@ -602,7 +644,7 @@ func (c *stateStoreServiceClient) ListUndispatchedCaptures(ctx context.Context,
func (c *stateStoreServiceClient) StreamUndispatchedCaptures(ctx context.Context, in *StreamUndispatchedCapturesRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[StreamUndispatchedCapturesResponse], error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
stream, err := c.cc.NewStream(ctx, &StateStoreService_ServiceDesc.Streams[1], StateStoreService_StreamUndispatchedCaptures_FullMethodName, cOpts...)
stream, err := c.cc.NewStream(ctx, &StateStoreService_ServiceDesc.Streams[2], StateStoreService_StreamUndispatchedCaptures_FullMethodName, cOpts...)
if err != nil {
return nil, err
}
@@ -677,6 +719,17 @@ type StateStoreServiceServer interface {
// configuration page").
PutConfigSnapshot(context.Context, *PutConfigSnapshotRequest) (*PutConfigSnapshotResponse, error)
GetConfigSnapshot(context.Context, *GetConfigSnapshotRequest) (*GetConfigSnapshotResponse, error)
// Task log: one task attempt's persisted log. Task log files live in the
// state directory this process owns, so the dashboard reads them over this
// contract rather than opening them itself
// (docs/prds/ui-service-boundary.md).
ReadTaskLog(context.Context, *ReadTaskLogRequest) (*ReadTaskLogResponse, error)
// StreamTaskLogContent returns one attempt's log verbatim, one chunk per
// message, for a download. A log file is unbounded input (it rotates at
// DefaultMaxSizeMB), so a unary response would exceed gRPC's 4MiB message
// cap on a real log -- the same reason ListCaptures was superseded by
// StreamCaptures.
StreamTaskLogContent(*StreamTaskLogContentRequest, grpc.ServerStreamingServer[StreamTaskLogContentResponse]) error
// Capture
InsertCapture(context.Context, *InsertCaptureRequest) (*InsertCaptureResponse, error)
// Deprecated: Do not use.
@@ -800,6 +853,12 @@ func (UnimplementedStateStoreServiceServer) PutConfigSnapshot(context.Context, *
func (UnimplementedStateStoreServiceServer) GetConfigSnapshot(context.Context, *GetConfigSnapshotRequest) (*GetConfigSnapshotResponse, error) {
return nil, status.Error(codes.Unimplemented, "method GetConfigSnapshot not implemented")
}
func (UnimplementedStateStoreServiceServer) ReadTaskLog(context.Context, *ReadTaskLogRequest) (*ReadTaskLogResponse, error) {
return nil, status.Error(codes.Unimplemented, "method ReadTaskLog not implemented")
}
func (UnimplementedStateStoreServiceServer) StreamTaskLogContent(*StreamTaskLogContentRequest, grpc.ServerStreamingServer[StreamTaskLogContentResponse]) error {
return status.Error(codes.Unimplemented, "method StreamTaskLogContent not implemented")
}
func (UnimplementedStateStoreServiceServer) InsertCapture(context.Context, *InsertCaptureRequest) (*InsertCaptureResponse, error) {
return nil, status.Error(codes.Unimplemented, "method InsertCapture not implemented")
}
@@ -1346,6 +1405,35 @@ func _StateStoreService_GetConfigSnapshot_Handler(srv interface{}, ctx context.C
return interceptor(ctx, in, info, handler)
}
func _StateStoreService_ReadTaskLog_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(ReadTaskLogRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(StateStoreServiceServer).ReadTaskLog(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: StateStoreService_ReadTaskLog_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(StateStoreServiceServer).ReadTaskLog(ctx, req.(*ReadTaskLogRequest))
}
return interceptor(ctx, in, info, handler)
}
func _StateStoreService_StreamTaskLogContent_Handler(srv interface{}, stream grpc.ServerStream) error {
m := new(StreamTaskLogContentRequest)
if err := stream.RecvMsg(m); err != nil {
return err
}
return srv.(StateStoreServiceServer).StreamTaskLogContent(m, &grpc.GenericServerStream[StreamTaskLogContentRequest, StreamTaskLogContentResponse]{ServerStream: stream})
}
// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name.
type StateStoreService_StreamTaskLogContentServer = grpc.ServerStreamingServer[StreamTaskLogContentResponse]
func _StateStoreService_InsertCapture_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(InsertCaptureRequest)
if err := dec(in); err != nil {
@@ -1785,6 +1873,10 @@ var StateStoreService_ServiceDesc = grpc.ServiceDesc{
MethodName: "GetConfigSnapshot",
Handler: _StateStoreService_GetConfigSnapshot_Handler,
},
{
MethodName: "ReadTaskLog",
Handler: _StateStoreService_ReadTaskLog_Handler,
},
{
MethodName: "InsertCapture",
Handler: _StateStoreService_InsertCapture_Handler,
@@ -1855,6 +1947,11 @@ var StateStoreService_ServiceDesc = grpc.ServiceDesc{
},
},
Streams: []grpc.StreamDesc{
{
StreamName: "StreamTaskLogContent",
Handler: _StateStoreService_StreamTaskLogContent_Handler,
ServerStreams: true,
},
{
StreamName: "StreamCaptures",
Handler: _StateStoreService_StreamCaptures_Handler,
@@ -18,12 +18,14 @@ package storecontract
import (
"context"
"errors"
"io"
"time"
"github.com/samcharles93/archie-core/internal/domain/binding"
"github.com/samcharles93/archie-core/internal/domain/mapping"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/logging"
)
// TaskStore is the full store surface the daemon needs.
@@ -160,6 +162,27 @@ type BindingTaskCreator interface {
EnqueueBindingTask(ctx context.Context, owner, repo, title, body, wf, identity string, bindingID int64, bindingVersion int) (*task.Task, error)
}
// TaskLogStore reads one task attempt's persisted log. Deliberately separate
// from TaskQueries: the log is not a row, it is a file the logging package
// owns the format of, and a process that can read the task board should not
// have to acquire a log reader to do it. The dashboard's UI process is the
// consumer this exists for -- it holds no daemon-local file handle, so the
// read crosses the State Store contract (docs/prds/ui-service-boundary.md).
//
// Found=false with a nil error is the not-found convention every read here
// uses, and it is load-bearing: "this attempt has no log" is not "this
// process cannot read logs". The second one is an error
// (logging.ErrTaskLogsUnavailable), because a caller that conflates them
// reports a configuration change where none is needed.
type TaskLogStore interface {
// TaskLog returns a page of one attempt's decoded log. attempt 0 selects
// the task's current attempt.
TaskLog(ctx context.Context, taskID int64, attempt int, q logging.Query) (logging.TaskLogPage, error)
// TaskLogContent writes one attempt's log verbatim to w, for a download.
// found is false, with a nil error, when the attempt has no log file.
TaskLogContent(ctx context.Context, taskID int64, attempt int, w io.Writer) (found bool, err error)
}
// CapturedEvent is one unbound inbound webhook capture: no workflow binding,
// no forge/task association -- just what arrived, from where, and when.
// See docs/prds/event-capture-storage.md.
@@ -14,6 +14,7 @@ import (
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/logging"
)
// Client is the single remote adapter wrapping one StateStoreServiceClient,
@@ -36,6 +37,7 @@ func (c *Client) Close() error { return nil }
var (
_ task.Store = (*Client)(nil)
_ storecontract.TaskStore = (*Client)(nil)
_ storecontract.TaskLogStore = (*Client)(nil)
_ storecontract.CaptureStore = (*Client)(nil)
_ storecontract.MappingStore = (*Client)(nil)
_ storecontract.BindingStore = (*Client)(nil)
@@ -417,6 +419,58 @@ func (c *Client) EnqueueBindingTask(ctx context.Context, owner, repo, title, bod
return taskValue(r.Task), nil
}
// Task log
// TaskLog reads one task attempt's decoded log over the wire. found=false with
// a nil error is the attempt-has-no-log answer; a process with no reader at
// all comes back as an unavailable error, not as an empty page, so a caller
// can tell the two apart.
func (c *Client) TaskLog(ctx context.Context, taskID int64, attempt int, q logging.Query) (logging.TaskLogPage, error) {
r, err := c.client.ReadTaskLog(ctx, taskLogRequestProto(taskID, attempt, q))
if err != nil {
return logging.TaskLogPage{}, unmapError(err)
}
if !r.Found {
return logging.TaskLogPage{
Entries: []logging.Entry{},
Components: []string{},
}, nil
}
return logging.TaskLogPage{
Entries: mapValues(r.Entries, taskLogEntryValue),
Truncated: r.Truncated,
File: r.File,
Found: true,
Components: r.Components,
}, nil
}
// TaskLogContent streams one attempt's log verbatim into w, for a download.
// found is false, with a nil error, when the attempt has no log file.
func (c *Client) TaskLogContent(ctx context.Context, taskID int64, attempt int, w io.Writer) (bool, error) {
stream, err := c.client.StreamTaskLogContent(ctx, &pb.StreamTaskLogContentRequest{TaskId: taskID, Attempt: int64(attempt)})
if err != nil {
return false, unmapError(err)
}
found := false
for {
r, err := stream.Recv()
if errors.Is(err, io.EOF) {
return found, nil
}
if err != nil {
return found, unmapError(err)
}
found = found || r.Found
if len(r.Chunk) == 0 {
continue
}
if _, err := w.Write(r.Chunk); err != nil {
return found, err
}
}
}
func derefTasks(in []*pb.Task) []task.Task {
if in == nil {
return nil
@@ -31,13 +31,31 @@ type contract interface {
store.ConfigSnapshotStore
}
// taskLogContract is the task-log read group, driven separately because it is
// not a *store.Store surface: the reader is internal/logging's own registry,
// which owns the log format, so the local side of this contract is a registry
// rather than a store.
type taskLogContract interface {
store.TaskLogStore
}
func remoteContract(t *testing.T, local *store.Store) contract {
t.Helper()
return remoteTaskStore(t, local, nil)
}
// remoteTaskStore serves local behind a bufconn listener with a task-log
// reader attached, and returns a client for it. The reader is a parameter
// because the log files are internal/logging's, not the store's: this contract
// fronts a reader rather than a table.
func remoteTaskStore(t *testing.T, local *store.Store, logs store.TaskLogStore) *Client {
t.Helper()
listener := bufconn.Listen(1 << 20)
server := grpc.NewServer()
RegisterServer(server, Deps{
Tasks: local, Captures: local, Mappings: local, Bindings: local,
BindingDispatcher: local, BindingTaskCreator: local, ConfigSnapshots: local,
TaskLogs: logs,
})
go func() { _ = server.Serve(listener) }()
t.Cleanup(func() { server.Stop(); _ = listener.Close() })
+135 -1
View File
@@ -5,7 +5,9 @@
package staterpc
import (
"bytes"
"context"
"errors"
"log/slog"
"time"
@@ -17,6 +19,7 @@ import (
"github.com/samcharles93/archie-core/internal/domain/binding"
"github.com/samcharles93/archie-core/internal/domain/mapping"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/logging"
)
// Unavailable-capability errors, mirroring gatewayrpc's missing-session-store
@@ -30,10 +33,23 @@ var (
errBindingUnavailable = status.Error(codes.Unavailable, "binding store unavailable")
errBindingDispatchUnavailable = status.Error(codes.Unavailable, "binding dispatcher unavailable")
errBindingTaskCreatorUnavailable = status.Error(codes.Unavailable, "binding task creator unavailable")
// errTaskLogsUnavailable is the "this process cannot read task logs at
// all" answer, and it is deliberately distinct from a found=false read
// result: only the first is a deployment matter. A dashboard that receives
// this reports that the log cannot be read here; it must never turn it
// into "task logging was not enabled for this run", which is what the
// absent-handle path used to do.
errTaskLogsUnavailable = status.Error(codes.Unavailable, msgTaskLogsUnavailable)
)
func timeSeconds(s int64) time.Duration { return time.Duration(s) * time.Second }
// taskLogChunkBytes bounds one streamed message. A log file rotates at
// logging.DefaultMaxSizeMB, well past gRPC's 4MiB unary cap, so the download
// is served in pieces (the same reason StreamCaptures superseded ListCaptures)
// and the pieces must be small enough to carry.
const taskLogChunkBytes = 256 << 10
// Deps groups the store surfaces the StateStore service fronts. TaskStore is
// required; the rest are optional (nil disables that group's RPCs, returning
// codes.Unavailable) exactly as their daemon-side consumers already treat a
@@ -47,7 +63,14 @@ type Deps struct {
Bindings storecontract.BindingStore
BindingDispatcher storecontract.BindingDispatcher
BindingTaskCreator storecontract.BindingTaskCreator
Log *slog.Logger
// TaskLogs reads one task attempt's persisted log out of the state
// directory this process owns. Optional, and nil is the honest default for
// a store service that shares no state directory with the daemon:
// ReadTaskLog then answers codes.Unavailable, which the dashboard reports
// as "this process cannot read logs" rather than as "this attempt has no
// log". Conflating those two is the bug this contract exists to fix.
TaskLogs storecontract.TaskLogStore
Log *slog.Logger
}
type server struct {
@@ -565,6 +588,117 @@ func (s *server) EnqueueBindingTask(ctx context.Context, r *pb.EnqueueBindingTas
return &pb.EnqueueBindingTaskResponse{Task: taskProto(t)}, nil
}
// Task log
func (s *server) taskLogs() (storecontract.TaskLogStore, error) {
if s.deps.TaskLogs == nil {
return nil, errTaskLogsUnavailable
}
return s.deps.TaskLogs, nil
}
func (s *server) ReadTaskLog(ctx context.Context, r *pb.ReadTaskLogRequest) (*pb.ReadTaskLogResponse, error) {
logs, err := s.taskLogs()
if err != nil {
return nil, err
}
page, err := logs.TaskLog(ctx, r.TaskId, int(r.Attempt), taskLogQueryValue(r))
if err != nil {
// A store that cannot read its own state directory keeps its own
// identity: this is unavailability, not a missing log, and a caller
// has to be able to tell them apart.
if errors.Is(err, logging.ErrTaskLogsUnavailable) {
return nil, errTaskLogsUnavailable
}
return nil, s.logErr("ReadTaskLog", err)
}
if !page.Found {
return &pb.ReadTaskLogResponse{Attempt: r.Attempt}, nil
}
return &pb.ReadTaskLogResponse{
Entries: mapValues(page.Entries, taskLogEntryProto),
Truncated: page.Truncated,
Attempt: r.Attempt,
Found: true,
Components: page.Components,
File: page.File,
}, nil
}
// StreamTaskLogContent sends one attempt's log verbatim, one chunk per
// message, for a download. A single reply reports found=false with no chunks:
// the attempt has no log file, which is a normal state rather than a failure.
func (s *server) StreamTaskLogContent(r *pb.StreamTaskLogContentRequest, stream pb.StateStoreService_StreamTaskLogContentServer) error {
logs, err := s.taskLogs()
if err != nil {
return err
}
// The chunking the client sees is owned by the reader that writes into
// this buffer, so the wire never holds a whole log to re-slice it.
buf := &chunkWriter{stream: stream, attempt: r.Attempt}
found, err := logs.TaskLogContent(stream.Context(), r.TaskId, int(r.Attempt), buf)
if err != nil {
if errors.Is(err, logging.ErrTaskLogsUnavailable) {
return errTaskLogsUnavailable
}
return s.logErr("StreamTaskLogContent", err)
}
if err := buf.flush(); err != nil {
return err
}
// A found log with no bytes still reports found: an attempt whose file is
// empty did produce a log, and found is what tells the download it may
// write an empty file rather than answer 404.
if found && buf.sent {
return nil
}
return stream.Send(&pb.StreamTaskLogContentResponse{Attempt: r.Attempt, Found: found})
}
// chunkWriter packs everything a log reader writes into stream-sized messages
// on its way through. It exists so the read can be one io.CopyBuffer of the
// file into this writer: the reader keeps its own chunk size, and the bytes
// are never all held at once.
type chunkWriter struct {
stream pb.StateStoreService_StreamTaskLogContentServer
attempt int64
pending []byte
// sent records whether any chunk reached the client, which is how the
// caller tells "the file was empty" from "the file was read".
sent bool
}
func (w *chunkWriter) Write(p []byte) (int, error) {
w.pending = append(w.pending, p...)
for len(w.pending) >= taskLogChunkBytes {
if err := w.emit(w.pending[:taskLogChunkBytes]); err != nil {
return 0, err
}
w.pending = w.pending[taskLogChunkBytes:]
}
return len(p), nil
}
func (w *chunkWriter) emit(chunk []byte) error {
w.sent = true
// The slice is copied into the message: the reader's buffer is reused
// across writes and must not be aliased by a message in flight.
return w.stream.Send(&pb.StreamTaskLogContentResponse{
Chunk: bytes.Clone(chunk),
Attempt: w.attempt,
Found: true,
})
}
func (w *chunkWriter) flush() error {
if len(w.pending) == 0 {
return nil
}
chunk := w.pending
w.pending = nil
return w.emit(chunk)
}
func mappingProtoPtr(m *mapping.Mapping) *pb.Mapping {
if m == nil {
return nil
@@ -0,0 +1,200 @@
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
}
+74 -1
View File
@@ -17,6 +17,7 @@ import (
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/logging"
)
func timestamp(t time.Time) *timestamppb.Timestamp {
@@ -245,6 +246,12 @@ const (
msgBindingTransition = "binding transition rejected"
msgAlreadyDispatched = "already dispatched"
msgInternal = "state store: internal error"
// msgTaskLogsUnavailable is the public phrase for "this service has no
// task-log reader". It is a wire contract like the sentinels above: the
// client rehydrates logging.ErrTaskLogsUnavailable from (Unavailable, this
// message), and a caller depends on that to tell "this process cannot read
// logs" from "this attempt has no log".
msgTaskLogsUnavailable = "task log reader unavailable"
)
// mapError converts a store sentinel error into a structured gRPC status
@@ -301,6 +308,12 @@ func mapError(err error) error {
// *status.Error whose code is DeadlineExceeded or Canceled, so we rehydrate
// those back to the standard context sentinels -- otherwise the consumer
// would (wrongly) park a task that was merely interrupted.
//
// Unavailable carries one message this package owns: the absent task-log
// reader. It is rehydrated for the same reason the error sentinels are --
// the dashboard's whole bug was rendering "this process cannot read logs" as
// "the attempt has no log", and only the typed error makes that distinction
// available to a caller.
func unmapError(err error) error {
if err == nil {
return nil
@@ -314,6 +327,19 @@ func unmapError(err error) error {
return context.Canceled
case codes.DeadlineExceeded:
return context.DeadlineExceeded
}
if sentinel := sentinelForStatus(st); sentinel != nil {
return sentinel
}
return fmt.Errorf("state store: %s: %w", st.Message(), err)
}
// sentinelForStatus maps a status to the store or logging sentinel it stands
// for, matched on (code, exact canonical message) -- those message constants
// are part of the wire contract (§4). A code whose message is not one this
// package defines returns nil, so the caller falls back to the wrapped form.
func sentinelForStatus(st *status.Status) error {
switch st.Code() {
case codes.FailedPrecondition:
switch st.Message() {
case msgStaleTransition:
@@ -334,8 +360,12 @@ func unmapError(err error) error {
if st.Message() == msgAlreadyDispatched {
return storecontract.ErrAlreadyDispatched
}
case codes.Unavailable:
if st.Message() == msgTaskLogsUnavailable {
return logging.ErrTaskLogsUnavailable
}
}
return fmt.Errorf("state store: %s: %w", st.Message(), err)
return nil
}
// configSnapshotProto and configSnapshotValue carry the dashboard's
@@ -359,3 +389,46 @@ func configSnapshotValue(snapshot *pb.ConfigSnapshot) storecontract.ConfigSnapsh
PublishedAt: timeValue(snapshot.PublishedAt),
}
}
// taskLogEntryProto and taskLogEntryValue mirror internal/logging.Entry, whose
// Fields map crosses as a JSON object string exactly as events.Event.Data does
// (see eventDataJSON above). The logging package owns that format end to end;
// this is a transport of it, not a second definition.
func taskLogEntryProto(e logging.Entry) *pb.TaskLogEntry {
return &pb.TaskLogEntry{
Id: e.ID, Time: timestamp(e.Time), Level: e.Level, Msg: e.Message,
FieldsJson: eventDataJSON(e.Fields),
}
}
func taskLogEntryValue(e *pb.TaskLogEntry) logging.Entry {
if e == nil {
return logging.Entry{}
}
return logging.Entry{
ID: e.Id, Time: timeValue(e.Time), Level: e.Level, Message: e.Msg,
Fields: eventDataValue(e.FieldsJson),
}
}
// taskLogRequestProto is the client half of the ReadTaskLog mapping and
// taskLogQueryValue the server half: the whole logging.Query crosses, not a
// subset of it, so a filter cannot silently stop working at the boundary.
func taskLogRequestProto(taskID int64, attempt int, q logging.Query) *pb.ReadTaskLogRequest {
return &pb.ReadTaskLogRequest{
TaskId: taskID, Attempt: int64(attempt), Limit: int64(q.Limit),
Levels: q.Levels, Component: q.Component, Contains: q.Contains,
Since: timestamp(q.Since), Until: timestamp(q.Until),
}
}
func taskLogQueryValue(r *pb.ReadTaskLogRequest) logging.Query {
return logging.Query{
Levels: r.Levels,
Component: r.Component,
Contains: r.Contains,
Limit: int(r.Limit),
Since: timeValue(r.Since),
Until: timeValue(r.Until),
}
}
+98
View File
@@ -2,8 +2,11 @@ package logging
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"slices"
"strings"
@@ -65,6 +68,88 @@ type Result struct {
Truncated bool `json:"truncated"`
// File is the path read, for the UI to show where this came from.
File string `json:"file"`
// Found reports whether the file existed. False with a nil error is not a
// failure: a caller asking for a log that has no file yet, or one whose
// attempt produced no output, must be told that rather than shown an empty
// page indistinguishable from a log that is genuinely empty. This is also
// what separates "there is no log for this attempt" from "this process
// cannot read logs at all" -- two conditions that a reader has to report
// differently because only the second is a configuration matter.
Found bool `json:"found"`
}
// TaskLogPage is one task attempt's log as a task-log reader returns it: the
// page Tail selected, whether the attempt has a file at all, and the distinct
// components present in the file's tail so a filter is built from what is
// actually there rather than a hardcoded list that drifts.
type TaskLogPage struct {
Result
Components []string `json:"components"`
}
// ErrTaskLogsUnavailable reports that this process cannot read task logs at
// all -- no reader is configured here. It is deliberately not the same answer
// as "this attempt has no log" (Result.Found = false): a caller that conflates
// them tells an operator task logging is switched off when it is not, which is
// the bug this error exists to prevent.
var ErrTaskLogsUnavailable = errors.New("logging: task log reader unavailable")
// taskLogChunkBytes bounds one write a TaskLogContent makes, so a reader that
// pushes into a bounded transport emits chunks that transport can carry.
// io.Copy would otherwise hand it reads of its own choosing.
const taskLogChunkBytes = 256 << 10
// TaskLog reads one task attempt's persisted log from this registry. A nil
// registry (task logging not configured for this process) reports
// ErrTaskLogsUnavailable, never an empty page: a process that cannot read logs
// must say so rather than implicate the attempt. Safe to call on a nil
// receiver, like every other method here.
func (r *TaskRegistry) TaskLog(_ context.Context, taskID int64, attempt int, q Query) (TaskLogPage, error) {
if r == nil {
return TaskLogPage{}, ErrTaskLogsUnavailable
}
res, err := Tail(TaskLogPath(r.baseDir, taskID, attempt), q)
if err != nil {
return TaskLogPage{}, err
}
page := TaskLogPage{Result: res, Components: []string{}}
// The filter list is a convenience: failing to build it must not cost the
// caller their entries. Components only reads the tail this page already
// read, so its error is the same "treat as empty" case the daemon-wide
// handler has always applied -- it is dropped deliberately, not missed.
if res.Found {
if components, err := Components(res.File); err == nil {
page.Components = components
}
}
return page, nil
}
// TaskLogContent writes one task attempt's log to w exactly as it is on disk,
// for a caller that wants the file rather than a decoded page. found is false,
// with a nil error, when the attempt has no log file -- a normal state for an
// attempt that produced no output, and not an error. A nil registry reports
// ErrTaskLogsUnavailable, matching TaskLog.
//
// The content is streamed rather than returned as one slice because a log file
// is unbounded input (it rotates at DefaultMaxSizeMB), and a caller moving it
// across a transport with a message-size limit needs it in pieces.
func (r *TaskRegistry) TaskLogContent(_ context.Context, taskID int64, attempt int, w io.Writer) (bool, error) {
if r == nil {
return false, ErrTaskLogsUnavailable
}
f, err := os.Open(TaskLogPath(r.baseDir, taskID, attempt))
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, fmt.Errorf("logging: open task log: %w", err)
}
defer func() { _ = f.Close() }()
if _, err := io.CopyBuffer(w, f, make([]byte, taskLogChunkBytes)); err != nil {
return false, fmt.Errorf("logging: read task log: %w", err)
}
return true, nil
}
// PageResult is what a single forward-paged call returns.
@@ -293,6 +378,19 @@ func Tail(path string, q Query) (Result, error) {
limit = MaxTailLines
}
// found is read before readLines so an absent file is reported as such
// rather than only as an empty entry list -- see Result.Found. It is a
// Stat rather than a second readWindow: readLines reads the whole scan
// window, and doing that twice per Tail call would double the cost of
// every log view for one boolean.
if strings.TrimSpace(path) != "" {
if _, err := os.Stat(path); err == nil {
res.Found = true
} else if !os.IsNotExist(err) {
return Result{}, fmt.Errorf("logging: stat %s: %w", path, err)
}
}
matches := make([]Entry, 0, limit)
truncated, _, ok, err := readLines(path, q, 0, func(e Entry, _ int64) bool {
if len(matches) == limit {
+1
View File
@@ -25,6 +25,7 @@ type (
TaskRetryer = storecontract.TaskRetryer
TaskQueries = storecontract.TaskQueries
TaskEvents = storecontract.TaskEvents
TaskLogStore = storecontract.TaskLogStore
CaptureStore = storecontract.CaptureStore
ConfigSnapshotStore = storecontract.ConfigSnapshotStore
MappingStore = storecontract.MappingStore
+18 -7
View File
@@ -56,7 +56,12 @@ type daemonActions struct {
// cfg is the daemon's configuration, held by the stand-in daemon
// rather than by the dashboard: retry caps are the task owner's
// policy and the dashboard does not hold configuration.
cfg *config.Holder
cfg *config.Holder
// logs is the daemon's own task-log registry. Archiving removes a
// task's log files, and that is the daemon's cleanup over the state
// directory it owns -- the dashboard reads logs through a contract and
// has no directory to remove anything from.
logs *logging.TaskRegistry
issues *recordingCloser
stopper *recordingTaskStopper
// scopes records the identity each action arrived with, one per call.
@@ -97,10 +102,10 @@ func (d *daemonActions) closeIssue() func(context.Context, string, string, int,
}
func (d *daemonActions) removeLogs() func(int64) error {
if d.srv.TaskLogs == nil {
if d.logs == nil {
return nil
}
return d.srv.TaskLogs.Remove
return d.logs.Remove
}
func (d *daemonActions) publish() func(events.Event) {
@@ -701,11 +706,17 @@ func TestArchiveRemovesOnlyOneTerminalTask(t *testing.T) {
// logging: without this, an archived task's log files (and every rotated
// generation under it) would accumulate on disk forever, since nothing else
// in the system ever visits a task again once it leaves the active board.
//
// The removal is the task owner's, not the dashboard's: the log files live in
// the state directory, which the daemon owns, so the stand-in daemon holds the
// registry the way the real one does. The dashboard reads logs through a
// contract and has no directory to remove anything from.
func TestArchiveRemovesTheTaskLogDirectory(t *testing.T) {
srv := newTestServer(t)
ctx := t.Context()
baseDir := t.TempDir()
srv.TaskLogs = logging.NewTaskRegistry(baseDir, logging.NewFeed(10), logging.TaskSinkOptions{})
logs := logging.NewTaskRegistry(baseDir, logging.NewFeed(10), logging.TaskSinkOptions{})
operatorActions(t, srv).logs = logs
if _, err := srv.Store.EnqueueIssue(ctx, "acme", "widget", 1, "done", "", "", ""); err != nil {
t.Fatal(err)
@@ -715,11 +726,11 @@ func TestArchiveRemovesTheTaskLogDirectory(t *testing.T) {
t.Fatal(err)
}
if err := srv.TaskLogs.Open(task.ID, task.Attempt); err != nil {
if err := logs.Open(task.ID, task.Attempt); err != nil {
t.Fatal(err)
}
srv.TaskLogs.Write(task.ID, logging.Entry{Message: "line"})
if err := srv.TaskLogs.Close(task.ID); err != nil {
logs.Write(task.ID, logging.Entry{Message: "line"})
if err := logs.Close(task.ID); err != nil {
t.Fatal(err)
}
dir := logging.TaskLogDir(baseDir, task.ID)
@@ -0,0 +1,161 @@
package webui
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"github.com/samcharles93/archie-core/internal/logging"
)
// TestHandleTaskLogsDistinguishesNoLogForTheAttempt covers the dashboard's
// misleading report. A process that HAS a task-log reader and an attempt that
// genuinely has no file on disk is a different answer from a process that
// cannot read logs at all, and only the second one is "logging is not enabled
// here". The response has to carry that difference or the page cannot state it.
func TestHandleTaskLogsDistinguishesNoLogForTheAttempt(t *testing.T) {
srv := newTestServer(t)
ctx := t.Context()
srv.TaskLogs = logging.NewTaskRegistry(t.TempDir(), logging.NewFeed(10), logging.TaskSinkOptions{})
if _, err := srv.Store.EnqueueIssue(ctx, "acme", "widget", 1, "task", "", "", ""); err != nil {
t.Fatal(err)
}
task, err := srv.Store.TaskByIssue(ctx, "acme", "widget", 1)
if err != nil {
t.Fatal(err)
}
// No log is ever opened for this task: the reader works, the attempt has
// no file.
w := getTaskLogs(t, srv, task.ID, nil)
if w.Code != http.StatusOK {
t.Fatalf("status = %d, body %s", w.Code, w.Body)
}
var body struct {
Disabled bool `json:"disabled"`
Found bool `json:"found"`
}
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
t.Fatalf("decode response: %v (%s)", err, w.Body)
}
if body.Disabled {
t.Error("disabled = true for a process that has a task-log reader; that is the report the page turns into \"logging was not enabled for this run\"")
}
if body.Found {
t.Error("found = true for an attempt with no log file on disk")
}
if !strings.Contains(w.Body.String(), `"found"`) {
t.Errorf("body = %s, want an explicit found marker so the page can say the attempt has no log rather than that logging is off", w.Body)
}
}
// TestHandleTaskLogsDownloadServesTheAttemptLog pins the deliverable: the
// endpoint hands back the attempt's log as a file download. The route falling
// through to the SPA asset handler would answer 200 with an HTML page, so this
// asserts on the attachment headers and the body, not merely on the status.
func TestHandleTaskLogsDownloadServesTheAttemptLog(t *testing.T) {
srv := newTestServer(t)
ctx := t.Context()
logs := logging.NewTaskRegistry(t.TempDir(), logging.NewFeed(10), logging.TaskSinkOptions{})
srv.TaskLogs = logs
if _, err := srv.Store.EnqueueIssue(ctx, "acme", "widget", 1, "task", "", "", ""); err != nil {
t.Fatal(err)
}
task, err := srv.Store.TaskByIssue(ctx, "acme", "widget", 1)
if err != nil {
t.Fatal(err)
}
if err := logs.Open(task.ID, task.Attempt); err != nil {
t.Fatal(err)
}
logs.Write(task.ID, logging.Entry{Level: "ERROR", Message: "gate failed"})
if err := logs.Close(task.ID); err != nil {
t.Fatal(err)
}
url := "/api/tasks/" + strconv.FormatInt(task.ID, 10) + "/logs/download"
req := httptest.NewRequestWithContext(ctx, http.MethodGet, url, nil)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("GET %s = %d, body %s", url, w.Code, w.Body)
}
wantName := "task-" + strconv.FormatInt(task.ID, 10) + "-attempt-" + strconv.Itoa(task.Attempt) + ".log"
if got := w.Header().Get("Content-Disposition"); got != `attachment; filename="`+wantName+`"` {
t.Errorf("Content-Disposition = %q, want an attachment named %q", got, wantName)
}
if !strings.Contains(w.Body.String(), "gate failed") {
t.Errorf("download body = %s, want the attempt's own log content", w.Body)
}
}
// TestHandleTaskLogsDownloadWithoutALogAnswersNotFound keeps the download
// honest: there is nothing to attach, so it says so rather than handing back a
// zero-byte file that looks like a successful download of nothing.
func TestHandleTaskLogsDownloadWithoutALogAnswersNotFound(t *testing.T) {
srv := newTestServer(t)
ctx := t.Context()
srv.TaskLogs = logging.NewTaskRegistry(t.TempDir(), logging.NewFeed(10), logging.TaskSinkOptions{})
if _, err := srv.Store.EnqueueIssue(ctx, "acme", "widget", 1, "task", "", "", ""); err != nil {
t.Fatal(err)
}
task, err := srv.Store.TaskByIssue(ctx, "acme", "widget", 1)
if err != nil {
t.Fatal(err)
}
url := "/api/tasks/" + strconv.FormatInt(task.ID, 10) + "/logs/download"
req := httptest.NewRequestWithContext(ctx, http.MethodGet, url, nil)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusNotFound {
t.Fatalf("GET %s with no log = %d, want 404", url, w.Code)
}
// The response is an error, not a file. Attachment headers are written
// before the read (the body streams, so the size is unknown up front), and
// leaving them on a 404 hands the caller a .log attachment containing only
// the error text.
if got := w.Header().Get("Content-Disposition"); got != "" {
t.Errorf("Content-Disposition = %q on a 404, want it cleared so the error is not presented as a download", got)
}
if got := w.Header().Get("Content-Type"); strings.Contains(got, "ndjson") {
t.Errorf("Content-Type = %q on a 404, want the error content type rather than the log's", got)
}
}
// TestHandleTaskLogsDownloadWithoutAReaderAnswersServiceUnavailable is the
// degradation the boundary permits: a process with no task-log contract answers
// 503 rather than 404, because "I cannot read logs" and "there is no log" are
// not the same claim.
func TestHandleTaskLogsDownloadWithoutAReaderAnswersServiceUnavailable(t *testing.T) {
srv := newTestServer(t)
ctx := t.Context()
if _, err := srv.Store.EnqueueIssue(ctx, "acme", "widget", 1, "task", "", "", ""); err != nil {
t.Fatal(err)
}
task, err := srv.Store.TaskByIssue(ctx, "acme", "widget", 1)
if err != nil {
t.Fatal(err)
}
url := "/api/tasks/" + strconv.FormatInt(task.ID, 10) + "/logs/download"
req := httptest.NewRequestWithContext(ctx, http.MethodGet, url, nil)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusServiceUnavailable {
body, _ := io.ReadAll(w.Result().Body)
t.Fatalf("GET %s without a reader = %d (%s), want 503", url, w.Code, body)
}
// Same rule as the 404: an unavailable reader is an error, not a file.
if got := w.Header().Get("Content-Disposition"); got != "" {
t.Errorf("Content-Disposition = %q on a 503, want it cleared so the error is not presented as a download", got)
}
}
+190 -58
View File
@@ -1,6 +1,9 @@
package webui
import (
"context"
"errors"
"io"
"net/http"
"strconv"
"strings"
@@ -16,84 +19,213 @@ import (
// Attempt from the store, since that is what a human or Archie's own chat
// tool means by "why did task N park?" almost every time: the most recent
// run, not an arbitrary earlier retry.
//
// Two outcomes are kept apart on purpose, because only one of them is about
// the operator's configuration. A process with no task-log reader cannot say
// anything about an attempt, so it reports disabled and the page explains that
// this process cannot read logs. A process WITH a reader and an attempt with
// no log file reports found=false, which the page states as no log recorded
// for the attempt. Collapsing the second into the first is what made the
// dashboard claim "task logging is optional and was not enabled for this run"
// for every attempt of every task in a deployment where logging is
// unconditional.
func (s *Server) handleTaskLogs(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.Error(w, "bad id", http.StatusBadRequest)
id, attempt, ok := s.taskLogTarget(w, r)
if !ok {
return
}
task, err := s.Store.TaskByID(r.Context(), id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if task == nil {
http.Error(w, "task not found", http.StatusNotFound)
reader := s.taskLogReader()
if reader == nil {
writeDisabledTaskLogs(w, attempt)
return
}
q := r.URL.Query()
attempt := task.Attempt
if raw := strings.TrimSpace(q.Get("attempt")); raw != "" {
attempt, err = strconv.Atoi(raw)
if err != nil {
http.Error(w, "bad attempt", http.StatusBadRequest)
return
}
}
path := s.TaskLogs.Path(id, attempt)
if path == "" {
// Not an error: task logging is optional, same convention as
// handleLogs for the daemon-wide log.
writeJSON(w, map[string]any{
"entries": []logging.Entry{},
"file": "",
"disabled": true,
"attempt": attempt,
})
limit, ok := taskLogLimit(w, q.Get("limit"))
if !ok {
return
}
limit := logging.DefaultTailLines
if raw := strings.TrimSpace(q.Get("limit")); raw != "" {
parsed, err := strconv.Atoi(raw)
if err != nil {
http.Error(w, "bad limit", http.StatusBadRequest)
return
}
switch {
case parsed <= 0:
limit = logging.DefaultTailLines
case parsed > logging.MaxTailLines:
limit = logging.MaxTailLines
default:
limit = parsed
}
}
result, err := logging.Tail(path, logging.Query{
page, err := reader.TaskLog(r.Context(), id, attempt, logging.Query{
Levels: splitCSV(q.Get("level")),
Component: strings.TrimSpace(q.Get("component")),
Contains: strings.TrimSpace(q.Get("q")),
Limit: limit,
})
if err != nil {
// A reader that exists but cannot read here is still a statement about
// this process rather than about the attempt, so it keeps the disabled
// answer instead of claiming the attempt has no log.
if errors.Is(err, logging.ErrTaskLogsUnavailable) {
writeDisabledTaskLogs(w, attempt)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
components, err := logging.Components(path)
if err != nil {
s.Log.Warn("task log components unavailable", "task_id", id, "err", err)
components = []string{}
}
writeJSON(w, map[string]any{
"entries": result.Entries,
"truncated": result.Truncated,
"file": result.File,
"components": components,
"entries": page.Entries,
"truncated": page.Truncated,
"file": page.File,
"components": page.Components,
"attempt": attempt,
// found is the distinction the page needs: a reader answering with no
// entries because the file is absent is not a reader that is absent.
"found": page.Found,
})
}
// handleTaskLogDownload serves one task attempt's log verbatim as a file
// download -- the log itself rather than a decoded view of it, because an
// operator who asks for a download wants to read it in their own tooling or
// hand it to someone else.
//
// The conditions stay apart here too. A process with no reader answers 503,
// not 404: "there is no log for this attempt" is a claim only a process that
// can look is in a position to make.
func (s *Server) handleTaskLogDownload(w http.ResponseWriter, r *http.Request) {
id, attempt, ok := s.taskLogTarget(w, r)
if !ok {
return
}
reader := s.taskLogReader()
if reader == nil {
http.Error(w, logging.ErrTaskLogsUnavailable.Error(), http.StatusServiceUnavailable)
return
}
// Headers are set before the read because the body streams straight from
// the reader into the response: a log file is unbounded input, so
// buffering it to size the response would defeat the reason the reader
// streams at all. The attempt's own name is known up front.
//
// Everything after this point must therefore CLEAR them before writing an
// error, never add to them: a 404 still carrying `Content-Disposition:
// attachment` is a browser download of the error text under a .log
// filename, and the NDJSON type would misdescribe it too.
w.Header().Set("Content-Type", "application/x-ndjson")
w.Header().Set("Content-Disposition", `attachment; filename="`+taskLogFilename(id, attempt)+`"`)
clearAttachment := func() {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Del("Content-Disposition")
}
found, err := reader.TaskLogContent(r.Context(), id, attempt, w)
if err != nil {
if errors.Is(err, logging.ErrTaskLogsUnavailable) {
clearAttachment()
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
// Once a 200 and a body are on the wire there is no status left to
// send, so a failure partway through is logged where an operator can
// find it rather than silently truncating the file.
s.logf("task log download failed", "task", id, "attempt", attempt, "err", err)
return
}
if !found {
clearAttachment()
http.Error(w, "no log recorded for this attempt", http.StatusNotFound)
}
}
// taskLogTarget resolves the {id} path value and the task it names, returning
// the attempt to read: the "attempt" query parameter when the request names
// one, otherwise the task's current Attempt. It answers the request itself and
// reports false when either step fails.
func (s *Server) taskLogTarget(w http.ResponseWriter, r *http.Request) (id int64, attempt int, ok bool) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.Error(w, "bad id", http.StatusBadRequest)
return 0, 0, false
}
task, err := s.Store.TaskByID(r.Context(), id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return 0, 0, false
}
if task == nil {
http.Error(w, "task not found", http.StatusNotFound)
return 0, 0, false
}
attempt = task.Attempt
if raw := strings.TrimSpace(r.URL.Query().Get("attempt")); raw != "" {
parsed, err := strconv.Atoi(raw)
if err != nil {
http.Error(w, "bad attempt", http.StatusBadRequest)
return 0, 0, false
}
attempt = parsed
}
return id, attempt, true
}
// taskLogLimit bounds a page the way handleLogs bounds the daemon-wide read:
// the limit is caller-supplied and a log file is unbounded, so a request must
// not be able to pull an arbitrary amount of it into memory.
func taskLogLimit(w http.ResponseWriter, raw string) (int, bool) {
raw = strings.TrimSpace(raw)
if raw == "" {
return logging.DefaultTailLines, true
}
parsed, err := strconv.Atoi(raw)
if err != nil {
http.Error(w, "bad limit", http.StatusBadRequest)
return 0, false
}
switch {
case parsed <= 0:
return logging.DefaultTailLines, true
case parsed > logging.MaxTailLines:
return logging.MaxTailLines, true
default:
return parsed, true
}
}
// writeDisabledTaskLogs is the "this process cannot read task logs" answer,
// the same convention handleLogs uses for the daemon-wide log: an optional
// capability that is absent here must not turn "why did this task park?" into
// a 500 for every task.
func writeDisabledTaskLogs(w http.ResponseWriter, attempt int) {
writeJSON(w, map[string]any{
"entries": []logging.Entry{},
"file": "",
"disabled": true,
"attempt": attempt,
})
}
// taskLogFilename names a downloaded attempt's log. It carries the task and
// the attempt because an operator downloading two attempts of the same task
// must not end up with one file overwriting the other, and the attempt is the
// one identifier the log's own path would have given them.
func taskLogFilename(taskID int64, attempt int) string {
return "task-" + strconv.FormatInt(taskID, 10) + "-attempt-" + strconv.Itoa(attempt) + ".log"
}
// taskLogReader is the task-log read capability this process has, or nil when
// it has none. The seam is a narrow interface rather than a concrete registry
// so the same handlers serve both processes: the daemon holds a registry over
// its own state directory, the dashboard holds the State Store client. A
// consumer-owned seam is the difference between the dashboard degrading
// honestly and the dashboard opening another process's files.
func (s *Server) taskLogReader() TaskLogSource {
if s == nil {
return nil
}
return s.TaskLogs
}
// TaskLogSource is the read seam the task-log handlers consume. It is the
// logging package's own reader contract, so a registry and a remote client
// satisfy it without either one restating the log format -- the package that
// owns the format also owns the shape of a read of it.
type TaskLogSource interface {
// TaskLog returns a page of one attempt's decoded log.
TaskLog(ctx context.Context, taskID int64, attempt int, q logging.Query) (logging.TaskLogPage, error)
// TaskLogContent writes one attempt's log verbatim to w, for a download.
TaskLogContent(ctx context.Context, taskID int64, attempt int, w io.Writer) (bool, error)
}
+7 -5
View File
@@ -14,7 +14,8 @@ func TestHandleTaskLogsReturnsEntriesForLatestAttemptByDefault(t *testing.T) {
srv := newTestServer(t)
ctx := t.Context()
baseDir := t.TempDir()
srv.TaskLogs = logging.NewTaskRegistry(baseDir, logging.NewFeed(10), logging.TaskSinkOptions{})
logs := logging.NewTaskRegistry(baseDir, logging.NewFeed(10), logging.TaskSinkOptions{})
srv.TaskLogs = logs
if _, err := srv.Store.EnqueueIssue(ctx, "acme", "widget", 1, "task", "", "", ""); err != nil {
t.Fatal(err)
@@ -24,13 +25,13 @@ func TestHandleTaskLogsReturnsEntriesForLatestAttemptByDefault(t *testing.T) {
t.Fatal(err)
}
if err := srv.TaskLogs.Open(task.ID, task.Attempt); err != nil {
if err := logs.Open(task.ID, task.Attempt); err != nil {
t.Fatal(err)
}
srv.TaskLogs.Write(task.ID, logging.Entry{
logs.Write(task.ID, logging.Entry{
Level: "ERROR", Message: "gate failed", Fields: map[string]any{"component": "gate"},
})
if err := srv.TaskLogs.Close(task.ID); err != nil {
if err := logs.Close(task.ID); err != nil {
t.Fatal(err)
}
@@ -58,7 +59,8 @@ func TestHandleTaskLogsHonoursExplicitAttempt(t *testing.T) {
srv := newTestServer(t)
ctx := t.Context()
baseDir := t.TempDir()
srv.TaskLogs = logging.NewTaskRegistry(baseDir, logging.NewFeed(10), logging.TaskSinkOptions{})
logs := logging.NewTaskRegistry(baseDir, logging.NewFeed(10), logging.TaskSinkOptions{})
srv.TaskLogs = logs
if _, err := srv.Store.EnqueueIssue(ctx, "acme", "widget", 1, "task", "", "", ""); err != nil {
t.Fatal(err)
+11 -4
View File
@@ -43,10 +43,16 @@ type Server struct {
// LogFeed is the daemon diagnostic stream. It is separate from Events,
// which contains persisted task lifecycle activity only.
LogFeed *logging.Feed
// TaskLogs holds each task's persisted log output. Archiving a task
// removes its log files here too, via TaskLogs.Remove -- nil (task
// logging not configured) makes that a no-op.
TaskLogs *logging.TaskRegistry
// TaskLogs reads each task's persisted log output. Composition gives this
// process whichever implementation it can use: the daemon (whose state
// directory holds the files) its own *logging.TaskRegistry, and the
// dashboard process, which owns no such directory, the State Store client
// -- the read crosses a contract rather than opening a file
// (docs/prds/ui-service-boundary.md). Nil means this process has no
// task-log capability at all, which the handlers report as disabled rather
// than as "the attempt has no log": those are different claims and only one
// of them is about configuration.
TaskLogs TaskLogSource
// UpdateConfig applies a set of dotted-path config updates through
// the same validate-persist-publish path as reload (wired by the
@@ -238,6 +244,7 @@ func (s *Server) registerTaskRoutes(mux *http.ServeMux) {
mux.HandleFunc("POST /api/tasks/{id}/action", s.handleTaskAction)
mux.HandleFunc("GET /api/tasks/{id}", s.handleTask)
mux.HandleFunc("GET /api/tasks/{id}/logs", s.handleTaskLogs)
mux.HandleFunc("GET /api/tasks/{id}/logs/download", s.handleTaskLogDownload)
}
func (s *Server) registerMappingAndBindingRoutes(mux *http.ServeMux) {