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:
commit
2fd0f23971
30 files changed
+2317
-387
No files matched your search
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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, "")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user