diff --git a/cmd/archie-state-store/main_test.go b/cmd/archie-state-store/main_test.go index 5be744ded..f00f5376f 100644 --- a/cmd/archie-state-store/main_test.go +++ b/cmd/archie-state-store/main_test.go @@ -313,9 +313,23 @@ func TestStateStoreRealProcessSmoke(t *testing.T) { if err := cl.Transition(ctx, task.ID, task.Status, "running", "smoke started"); err != nil { t.Fatalf("Transition: %v", err) } - if _, err := cl.InsertEvent(ctx, events.Event{Kind: "stage_finish", TaskID: task.ID, Data: map[string]any{"duration_ms": 12.0}}); err != nil { + if _, err := cl.InsertEvent(ctx, events.Event{Kind: "stage_finish", TaskID: task.ID, Attempt: 1, Data: map[string]any{"duration_ms": 12.0}}); err != nil { t.Fatalf("InsertEvent: %v", err) } + // R4's document is stored as an event, so the real process must round-trip + // it with its nested structure and its attempt key intact: the standalone + // State Store owns the only copy, and a lost field here is a Config tab + // that reads a run it cannot describe. The key is the daemon's own + // (internal/daemon/daemon.go writes "document"), asserted by name below. + if _, err := cl.InsertEvent(ctx, events.Event{ + Kind: events.KindConfigCaptured, TaskID: task.ID, Attempt: 1, + Data: map[string]any{ + "schema": events.ConfigCapturedSchema, + "document": map[string]any{"bot_user": "archie", "models": map[string]any{"implement": "anthropic/claude"}}, + }, + }); err != nil { + t.Fatalf("InsertEvent config_captured: %v", err) + } if capID, err := cl.InsertCapture(ctx, capture("sentry", `{"id":1}`), 0, 0); err != nil || capID == 0 { t.Fatalf("InsertCapture = (%d, %v)", capID, err) } @@ -324,9 +338,25 @@ func TestStateStoreRealProcessSmoke(t *testing.T) { if err != nil || got == nil || got.ID != task.ID || got.Status != "running" { t.Fatalf("TaskByID after transition = %+v, %v", got, err) } - if evs, err := cl.TaskEvents(ctx, task.ID); err != nil || len(evs) != 1 || evs[0].Kind != "stage_finish" { + evs, err := cl.TaskEvents(ctx, task.ID) + if err != nil || len(evs) != 2 || evs[0].Kind != "stage_finish" { t.Fatalf("TaskEvents = %+v, %v", evs, err) } + if evs[0].Attempt != 1 { + t.Errorf("TaskEvents attempt = %d, want 1 across the real process", evs[0].Attempt) + } + if evs[1].Kind != events.KindConfigCaptured || evs[1].Attempt != 1 || evs[1].Data["schema"] != events.ConfigCapturedSchema { + t.Errorf("config_captured event = %+v, want its kind, attempt and schema preserved", evs[1]) + } + if _, renamed := evs[1].Data["config"]; renamed { + t.Error(`the document crossed under "config"; the producer writes it under "document"`) + } + if len(evs[1].Data) != 2 { + t.Errorf("config_captured data keys = %v, want exactly schema and document", evs[1].Data) + } + if doc, ok := evs[1].Data["document"].(map[string]any); !ok || doc["bot_user"] != "archie" { + t.Errorf("config_captured payload = %#v, want the decoded document under its own key", evs[1].Data) + } if caps, err := cl.ListCaptures(ctx, 10); err != nil || len(caps) != 1 || caps[0].Source != "sentry" { t.Fatalf("ListCaptures = %+v, %v", caps, err) } diff --git a/internal/agentexec/taskevents_test.go b/internal/agentexec/taskevents_test.go index f82855014..0a6c0bfcf 100644 --- a/internal/agentexec/taskevents_test.go +++ b/internal/agentexec/taskevents_test.go @@ -46,9 +46,17 @@ func TestForwardTaskEventsPublishesEachEventOnItsTaskSubject(t *testing.T) { }() want := []events.Event{ - {Kind: events.KindStageStart, TaskID: 42, Stage: "baseline"}, - {Kind: events.KindStageFinish, TaskID: 42, Stage: "baseline", Data: map[string]any{"duration_ms": float64(120)}}, - {Kind: events.KindParked, TaskID: 42, Detail: "baseline red"}, + {Kind: events.KindStageStart, TaskID: 42, Stage: "baseline", Attempt: 1}, + {Kind: events.KindStageFinish, TaskID: 42, Stage: "baseline", Attempt: 1, Data: map[string]any{"duration_ms": float64(120)}}, + {Kind: events.KindParked, TaskID: 42, Attempt: 1, Detail: "baseline red"}, + // R4's document rides this bridge too: a worker captures its own + // effective configuration, and the daemon is a different process, so + // the payload has to survive the hop or the Config tab reads nothing. + // The daemon's own key for it is asserted below by name. + {Kind: events.KindConfigCaptured, TaskID: 42, Attempt: 1, Data: map[string]any{ + "schema": events.ConfigCapturedSchema, + "document": map[string]any{"bot_user": "archie"}, + }}, } for _, e := range want { bus.Publish(e) @@ -71,6 +79,21 @@ func TestForwardTaskEventsPublishesEachEventOnItsTaskSubject(t *testing.T) { if got.Kind != want[i].Kind || got.TaskID != want[i].TaskID || got.Stage != want[i].Stage || got.Detail != want[i].Detail { t.Errorf("event %d = %+v, want %+v", i, got, want[i]) } + if got.Attempt != want[i].Attempt { + t.Errorf("event %d attempt = %d, want %d (provenance must not be dropped on the hop)", i, got.Attempt, want[i].Attempt) + } + if got.Kind == events.KindConfigCaptured { + if got.Data["schema"] != events.ConfigCapturedSchema { + t.Errorf("event %d schema = %v, want %q", i, got.Data["schema"], events.ConfigCapturedSchema) + } + if _, renamed := got.Data["config"]; renamed { + t.Errorf(`event %d crossed under "config"; the producer writes the document under "document"`, i) + } + doc, ok := got.Data["document"].(map[string]any) + if !ok || doc["bot_user"] != "archie" { + t.Errorf("event %d document payload = %#v, want the decoded document under its own key", i, got.Data["document"]) + } + } if got.At.IsZero() { t.Errorf("event %d has zero At, want the bus-stamped publish time", i) } diff --git a/internal/app/agentworker/task_execution.go b/internal/app/agentworker/task_execution.go index 41cc0165b..b5832f16f 100644 --- a/internal/app/agentworker/task_execution.go +++ b/internal/app/agentworker/task_execution.go @@ -14,6 +14,7 @@ import ( "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/workflow" "github.com/samcharles93/archie-core/internal/domain/workflow/skillbuild" + "github.com/samcharles93/archie-core/internal/domain/workflow/task" "github.com/samcharles93/archie-core/internal/domain/workflow/wfeval" "github.com/samcharles93/archie-core/internal/events" "github.com/samcharles93/archie-core/internal/installtype" @@ -111,6 +112,16 @@ func (h *hybridTrees) ChangedLines(ctx context.Context, dir, base string) (int, return h.local.ChangedLines(ctx, dir, base) } +// ChangedFileStats is the capability the workflow captures a change through. +// It is not part of workflow.Trees: it is asserted for as the unexported +// optional interface package workflow declares, so a Trees implementation +// without it degrades to no capture. Without this forwarder the in-container +// path -- which is every production run, archie-agent executing the whole +// workflow -- would be exactly that silent no-op. +func (h *hybridTrees) ChangedFileStats(ctx context.Context, dir, base string) (task.ChangeStats, error) { + return h.local.ChangedFileStats(ctx, dir, base) +} + var _ workflow.Trees = (*hybridTrees)(nil) // chownTree recursively chowns dir to uid:gid so the daemon -- running as diff --git a/internal/app/agentworker/task_execution_test.go b/internal/app/agentworker/task_execution_test.go index 81d57b947..d9a1a203d 100644 --- a/internal/app/agentworker/task_execution_test.go +++ b/internal/app/agentworker/task_execution_test.go @@ -24,6 +24,7 @@ import ( "github.com/samcharles93/archie-core/internal/agentexec" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/workflow" + "github.com/samcharles93/archie-core/internal/domain/workflow/task" "github.com/samcharles93/archie-core/internal/domain/workintake" "github.com/samcharles93/archie-core/internal/events" "github.com/samcharles93/archie-core/internal/forge" @@ -421,6 +422,74 @@ func TestRunTaskExecutesBootstrapWorkflowEndToEnd(t *testing.T) { t.Fatalf("commit identity = author %q <%s>, committer %q <%s>", commit.Author.Name, commit.Author.Email, commit.Committer.Name, commit.Committer.Email) } + + // The change the run produced is captured at the push, through the + // container's hybridTrees, and persisted as durable provenance attributed + // to the attempt -- and captured once more by OpenPR, the first moment a + // capture can carry the pull-request number the view links. This is the + // only end-to-end path that matters: every production workflow.Run executes + // here. + timeline, err := st.TaskEvents(ctx, task.ID) + if err != nil { + t.Fatalf("TaskEvents: %v", err) + } + var captured []events.Event + for _, e := range timeline { + if e.Kind == events.KindChangesCaptured { + captured = append(captured, e) + } + } + if len(captured) != 2 { + t.Fatalf("changes_captured events = %d, want the push capture and the one OpenPR takes (%d)", len(captured), stored.PRNumber) + } + for i, e := range captured { + if e.Attempt != task.Attempt || task.Attempt == 0 { + t.Errorf("capture %d attempt = %d, want the task's own attempt %d", i, e.Attempt, task.Attempt) + } + } + if captured[0].Stage != "commit-push" { + t.Errorf("capture stage = %q, want the capturing stage", captured[0].Stage) + } + data, err := json.Marshal(captured[0].Data) + if err != nil { + t.Fatal(err) + } + var payload struct { + CapturedAfter string `json:"captured_after"` + PRNumber int `json:"pr_number"` + Files []struct { + Path string `json:"path"` + Status string `json:"status"` + } `json:"files"` + } + if err := json.Unmarshal(data, &payload); err != nil { + t.Fatalf("decode capture: %v", err) + } + if payload.CapturedAfter != "commit-push" { + t.Errorf("captured_after = %q, want commit-push", payload.CapturedAfter) + } + if payload.PRNumber != 0 { + t.Errorf("the push capture carries pr_number %d, want 0: no PR existed when it was taken", payload.PRNumber) + } + if len(payload.Files) != 1 || payload.Files[0].Path != ".archie/bootstrap.md" || payload.Files[0].Status != "added" { + t.Errorf("captured files = %+v, want the bootstrap marker as an added file", payload.Files) + } + + finalData, err := json.Marshal(captured[1].Data) + if err != nil { + t.Fatal(err) + } + var final struct { + CapturedAfter string `json:"captured_after"` + PRNumber int `json:"pr_number"` + } + if err := json.Unmarshal(finalData, &final); err != nil { + t.Fatalf("decode the final capture: %v", err) + } + if final.CapturedAfter != "open-pr" || final.PRNumber != stored.PRNumber || final.PRNumber == 0 { + t.Errorf("final capture = %q pr %d, want open-pr carrying the stored PR number %d, without which the changed-files view has nothing to link", + final.CapturedAfter, final.PRNumber, stored.PRNumber) + } } // TestRunTaskRestoresWorktreeOwnershipOnEveryExit pins the half of the @@ -768,3 +837,64 @@ func TestRouteTaskUsesRequestLabelOverride(t *testing.T) { t.Fatalf("routeTask() = %q, want %q (request label override not applied)", wf.Name, "security-review") } } + +// changeStatsReader mirrors the unexported optional interface package workflow +// type-asserts on TaskContext.Trees to capture what an attempt changed. It is +// restated here because an unexported interface cannot be named from this +// package; the assertion below is therefore the only way this side can pin +// that the in-container Trees is capable at all. +type changeStatsReader interface { + ChangedFileStats(ctx context.Context, dir, base string) (task.ChangeStats, error) +} + +// TestHybridTreesCanReportWhatChanged is the in-container half of the change +// capture. Every production workflow.Run executes in archie-agent against a +// hybridTrees, so a Trees without this capability would make the capture a +// silent no-op in exactly the deployment that produces all the real data -- +// the daemon-side manager being capable would prove nothing. +func TestHybridTreesCanReportWhatChanged(t *testing.T) { + var trees any = &hybridTrees{local: &worktree.Manager{}} + if _, ok := trees.(changeStatsReader); !ok { + t.Fatal("hybridTrees cannot report a diffstat; every capture inside a container would silently no-op") + } +} + +// TestHybridTreesForwardsChangedFileStatsToItsLocalManager drives the +// capability against a real worktree, so satisfaction of the interface is not +// mistaken for a working forwarder. +func TestHybridTreesForwardsChangedFileStatsToItsLocalManager(t *testing.T) { + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not installed") + } + ctx := t.Context() + host := newLocalRemote(t, "acme", "diffstat") + manager := &worktree.Manager{ + WorkDir: t.TempDir(), Token: "unused", + BotUser: "archie-bot", BotEmail: "archie-bot@example.com", BaseURL: "file://" + host, + } + dir, branch, err := manager.Prepare(ctx, "acme", "diffstat", "main", 4, "feat: widget", "", "") + if err != nil { + t.Fatalf("prepare: %v", err) + } + if err := os.WriteFile(filepath.Join(dir, "widget.txt"), []byte("one\ntwo\n"), 0o644); err != nil { + t.Fatal(err) + } + if _, err := manager.CommitAll(ctx, dir, "feat: widget"); err != nil { + t.Fatalf("commit: %v", err) + } + + trees := &hybridTrees{local: manager, localDir: dir, branch: branch} + stats, err := trees.ChangedFileStats(ctx, dir, "main") + if err != nil { + t.Fatalf("ChangedFileStats: %v", err) + } + if len(stats.Files) != 1 || stats.Files[0].Path != "widget.txt" { + t.Fatalf("files = %+v, want the one committed file", stats.Files) + } + if stats.Totals != (task.ChangeTotals{Files: 1, Additions: 2}) { + t.Errorf("totals = %+v, want the file's two added lines", stats.Totals) + } + if stats.HeadSHA == "" || stats.BaseSHA == "" { + t.Errorf("SHAs = (%q, %q), want both resolved", stats.BaseSHA, stats.HeadSHA) + } +} diff --git a/internal/app/archieui/run_test.go b/internal/app/archieui/run_test.go index f33d87109..38b5a16a5 100644 --- a/internal/app/archieui/run_test.go +++ b/internal/app/archieui/run_test.go @@ -9,13 +9,16 @@ import ( "net/http" "net/http/httptest" "path/filepath" + "strconv" "strings" "testing" + "time" "google.golang.org/grpc" "github.com/samcharles93/archie-core/internal/domain/health" "github.com/samcharles93/archie-core/internal/domain/messaging" + "github.com/samcharles93/archie-core/internal/events" "github.com/samcharles93/archie-core/internal/gateway" "github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc" "github.com/samcharles93/archie-core/internal/infrastructure/staterpc" @@ -100,13 +103,16 @@ func TestUIServesDashboardAgainstRemoteContracts(t *testing.T) { t.Fatalf("open temp store: %v", err) } defer st.Close() - if _, err := st.EnqueueChatTask(t.Context(), "acme", "widget", "remote summary", "body", "implement", ""); err != nil { + seeded, err := st.EnqueueChatTask(t.Context(), "acme", "widget", "remote summary", "body", "implement", "") + if err != nil { t.Fatalf("seed task: %v", err) } // The daemon publishes the configuration page's projection; this process - // only renders it (archie-core-ymut). + // only renders it (archie-core-ymut). The forge host is published too: it + // is what the run-detail reads resolve their repository and pull-request + // links from, the same projection the task list uses. published, err := json.Marshal(webui.ConfigView{ - Identity: webui.IdentityView{BotUser: "archie", ForgeType: "github"}, + Identity: webui.IdentityView{BotUser: "archie", ForgeType: "github", ForgeHost: "https://forge.example"}, Editable: true, }) if err != nil { @@ -118,6 +124,32 @@ func TestUIServesDashboardAgainstRemoteContracts(t *testing.T) { t.Fatalf("publish config snapshot: %v", err) } + // The other half of the configuration page is per-attempt provenance, and + // the three run-detail reads are backed by the same State Store contract as + // the rest of the task surface. The statements below are what other + // processes write in production -- the daemon's dispatch capture, the + // worker's change capture -- attributed to one attempt, so the assertions + // further down read them back through the real client rather than a fake. + seedAt := time.Now().UTC() + for _, e := range []events.Event{ + {Kind: events.KindStageStart, TaskID: seeded.ID, Attempt: 1, Stage: "prepare", At: seedAt}, + {Kind: events.KindStageFinish, TaskID: seeded.ID, Attempt: 1, Stage: "prepare", At: seedAt.Add(time.Second), Data: map[string]any{"duration_ms": 1000}}, + {Kind: events.KindConfigCaptured, TaskID: seeded.ID, Attempt: 1, At: seedAt, Data: map[string]any{ + "schema": events.ConfigCapturedSchema, "document": map[string]any{"bot_user": "archie"}, + }}, + {Kind: events.KindChangesCaptured, TaskID: seeded.ID, Attempt: 1, Stage: "open-pr", At: seedAt, Data: map[string]any{ + "schema": events.ChangesCapturedSchema, "owner": "acme", "repo": "widget", + "base": "main", "branch": "feat/1-widget", "head_sha": "head-sha", "base_sha": "base-sha", + "pr_number": 7, "captured_after": "open-pr", "truncated": false, + "files": []any{map[string]any{"path": "internal/x.go", "old_path": "", "status": "modified", "additions": 12, "deletions": 3, "binary": false}}, + "totals": map[string]any{"files": 1, "additions": 12, "deletions": 3}, + }}, + } { + if _, err := st.InsertEvent(t.Context(), e); err != nil { + t.Fatalf("seed %s event: %v", e.Kind, err) + } + } + stateTarget, stopState := serveGRPC(t, func(r grpc.ServiceRegistrar) { staterpc.RegisterServer(r, staterpc.Deps{Tasks: st, Captures: st, BindingDispatcher: st, ConfigSnapshots: st, Log: slog.New(slog.DiscardHandler)}) }) @@ -240,6 +272,96 @@ func TestUIServesDashboardAgainstRemoteContracts(t *testing.T) { t.Errorf("PATCH /api/config = %d, want 503: the daemon stays the configuration writer (PRD lines 95-96)", resp.StatusCode) } + // The run-detail reads. These three routes have no fake behind them: the + // rail, the capture and the raw event list all come back over the State + // Store listener the UI dialled, so each assertion below is the real + // client's answer about the attempt-attributed set seeded above. + taskPath := "/api/tasks/" + strconv.FormatInt(seeded.ID, 10) + code, body = get(taskPath + "/attempts") + if code != http.StatusOK { + t.Fatalf("GET /attempts = %d (%s), want 200", code, body) + } + var rail struct { + Attempts []struct { + Attempt int `json:"attempt"` + Status string `json:"status"` + Stages []struct { + Name string `json:"name"` + Status string `json:"status"` + } `json:"stages"` + } `json:"attempts"` + } + if err := json.Unmarshal(body, &rail); err != nil { + t.Fatalf("decode attempts: %v (%s)", err, body) + } + if len(rail.Attempts) != 1 || rail.Attempts[0].Attempt != 1 || rail.Attempts[0].Status != "ok" { + t.Fatalf("attempts = %s, want exactly the seeded attempt 1 recorded ok", body) + } + if len(rail.Attempts[0].Stages) != 1 || rail.Attempts[0].Stages[0].Name != "prepare" || rail.Attempts[0].Stages[0].Status != "ok" { + t.Fatalf("stage rail = %s, want the attempt's own prepare stage", body) + } + + code, body = get(taskPath + "/changes?attempt=1") + if code != http.StatusOK { + t.Fatalf("GET /changes = %d (%s), want 200", code, body) + } + var changes struct { + Found bool `json:"found"` + Captures []struct { + PRNumber int `json:"pr_number"` + PRURL string `json:"pr_url"` + Files []struct { + Path string `json:"path"` + } `json:"files"` + } `json:"captures"` + } + if err := json.Unmarshal(body, &changes); err != nil { + t.Fatalf("decode changes: %v (%s)", err, body) + } + if !changes.Found || len(changes.Captures) != 1 { + t.Fatalf("changes = %s, want the one capture recorded for the attempt", body) + } + if changes.Captures[0].PRNumber != 7 || changes.Captures[0].PRURL != "https://forge.example/acme/widget/pull/7" { + t.Errorf("capture = pr %d %q, want #7 linked through the published forge projection (%s)", + changes.Captures[0].PRNumber, changes.Captures[0].PRURL, body) + } + if len(changes.Captures[0].Files) != 1 || changes.Captures[0].Files[0].Path != "internal/x.go" { + t.Errorf("capture files = %s, want the file the attempt changed", body) + } + + code, body = get(taskPath + "/debug?attempt=1") + if code != http.StatusOK { + t.Fatalf("GET /debug = %d (%s), want 200", code, body) + } + var debug struct { + Attempt int `json:"attempt"` + Task struct { + ID int64 `json:"id"` + } `json:"task"` + Events []struct { + Kind string `json:"kind"` + Attempt int `json:"attempt"` + } `json:"events"` + } + if err := json.Unmarshal(body, &debug); err != nil { + t.Fatalf("decode debug: %v (%s)", err, body) + } + if debug.Task.ID != seeded.ID || debug.Attempt != 1 { + t.Errorf("debug = task %d attempt %d, want the seeded task and attempt 1 (%s)", debug.Task.ID, debug.Attempt, body) + } + kinds := map[string]int{} + for _, e := range debug.Events { + kinds[e.Kind]++ + if e.Attempt != 1 { + t.Errorf("debug event %s carries attempt %d, want 1: attribution must survive the store it was read from", e.Kind, e.Attempt) + } + } + for _, kind := range []string{events.KindStageStart, events.KindStageFinish, events.KindConfigCaptured, events.KindChangesCaptured} { + if kinds[kind] != 1 { + t.Errorf("debug events = %s, want exactly one %s", body, kind) + } + } + // Routes with no contract behind them degrade rather than panic. code, body = get("/api/logs") if code != http.StatusOK { diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index e4d419353..20ed3e2f7 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -981,6 +981,14 @@ func (d *Daemon) reconcilePRs(ctx context.Context) { d.Log.Warn("PR state check failed", "pr", t.PRNumber, "err", err) continue } + // OpenPRs returns a deliberately narrow projection of a pr_open task + // (id/owner/repo/issue/pr/status/source/identity) that does not carry + // the attempt, so the outcome event below reads it from the task's own + // row. A failed lookup costs attribution and never the merge handling: + // the status transition and the worktree cleanup still run. OpenPRs + // carries attempt, so these events are attributed without a second + // read per open PR on every reconcile tick. + attempt := t.Attempt switch state { case "merged": _ = d.Store.Transition(ctx, t.ID, workflow.StatusPROpen, workflow.StatusMerged, "") @@ -997,7 +1005,7 @@ func (d *Daemon) reconcilePRs(ctx context.Context) { )) d.Log.Info("PR merged", "repo", t.Owner+"/"+t.Repo, "pr", t.PRNumber) d.emit(events.Event{ - Kind: events.KindPRMerged, TaskID: t.ID, + Kind: events.KindPRMerged, TaskID: t.ID, Attempt: attempt, Repo: t.Owner + "/" + t.Repo, Issue: t.IssueNumber, Data: map[string]any{"pr": t.PRNumber}, }) @@ -1006,7 +1014,7 @@ func (d *Daemon) reconcilePRs(ctx context.Context) { _ = trees.Cleanup(t.Owner, t.Repo, t.IssueNumber) d.Log.Info("PR rejected", "repo", t.Owner+"/"+t.Repo, "pr", t.PRNumber) d.emit(events.Event{ - Kind: events.KindPRRejected, TaskID: t.ID, + Kind: events.KindPRRejected, TaskID: t.ID, Attempt: attempt, Repo: t.Owner + "/" + t.Repo, Issue: t.IssueNumber, Data: map[string]any{"pr": t.PRNumber}, }) @@ -1040,7 +1048,7 @@ func (d *Daemon) process(ctx context.Context, task *workflow.Task) { } d.recordPark(ctx, task.ID, reason) d.emit(events.Event{ - Kind: events.KindParked, TaskID: task.ID, + Kind: events.KindParked, TaskID: task.ID, Attempt: task.Attempt, Repo: repo.FullName(), Issue: task.IssueNumber, Detail: reason, }) return @@ -1054,7 +1062,7 @@ func (d *Daemon) process(ctx context.Context, task *workflow.Task) { } d.recordPark(ctx, task.ID, reason) d.emit(events.Event{ - Kind: events.KindParked, TaskID: task.ID, + Kind: events.KindParked, TaskID: task.ID, Attempt: task.Attempt, Repo: repo.FullName(), Issue: task.IssueNumber, Detail: reason, }) return @@ -1318,12 +1326,15 @@ func (d *Daemon) runViaAgent(ctx context.Context, task *workflow.Task, repo conf return } defer revoke() + cfg := d.configFor(task) + taskCfg := cfg.ForTask() + d.captureAttemptConfig(ctx, task, taskCfg) req := taskrun.Request{ Task: task, Repo: repo, - Cfg: d.configFor(task).ForTask(), - Providers: agentexec.ProvidersFromConfig(d.configFor(task).Providers), - MCPServers: d.configFor(task).Tools.MCPServers, + Cfg: taskCfg, + Providers: agentexec.ProvidersFromConfig(cfg.Providers), + MCPServers: cfg.Tools.MCPServers, WorktreeGrant: grant, KindWorkflows: d.KindWorkflows, LabelWorkflows: d.LabelWorkflows, @@ -1486,6 +1497,52 @@ func (d *Daemon) configFor(task *workflow.Task) config.Config { return d.Cfg.Get() } +// captureAttemptConfig persists the effective task configuration this attempt +// runs under, as a durable event on the attempt's own key. +// +// It is written where the configuration is materialised for the dispatch, so +// the document is exactly what the run received. The published configuration +// snapshot cannot answer this: it is the CURRENT configuration, replaced on +// every publish, so a run that finished last week has no record anywhere else. +// +// Fail-open by construction: a configuration that could not be recorded is +// worth a log line and nothing more. Parking a task over a reporting failure +// would trade the run for the receipt. +func (d *Daemon) captureAttemptConfig(ctx context.Context, task *workflow.Task, cfg config.TaskConfig) { + if d.Store == nil { + return + } + document, err := json.Marshal(cfg) + if err != nil { + d.Log.Warn("attempt configuration not captured", "task", task.ID, "attempt", task.Attempt, "err", err) + return + } + event := events.Event{ + At: time.Now().UTC(), + Kind: events.KindConfigCaptured, + TaskID: task.ID, + Repo: task.Owner + "/" + task.Repo, + Issue: task.IssueNumber, + Workflow: task.Workflow, + Attempt: task.Attempt, + Data: map[string]any{ + "schema": events.ConfigCapturedSchema, + // Raw so the document is embedded once, as it was encoded, rather + // than as a string a reader would have to decode again. + "document": json.RawMessage(document), + }, + } + id, err := d.Store.InsertEvent(ctx, event) + if err != nil { + d.Log.Warn("attempt configuration not captured", "task", task.ID, "attempt", task.Attempt, "err", err) + return + } + // The assigned ID tells the event sink to broadcast without inserting a + // duplicate row. + event.ID = id + d.emit(event) +} + func configForIdentity(root config.Config, identity config.IdentityConfig) config.Config { root.BotUser = identity.BotUser root.BotEmail = identity.BotEmail diff --git a/internal/daemon/daemon_test.go b/internal/daemon/daemon_test.go index 617e606ca..0d1ced73b 100644 --- a/internal/daemon/daemon_test.go +++ b/internal/daemon/daemon_test.go @@ -7,6 +7,7 @@ import ( "log/slog" "os" "slices" + "sort" "strings" "sync/atomic" "testing" @@ -1610,3 +1611,333 @@ func TestReconcilePRsSkipsChatTasks(t *testing.T) { t.Errorf("CloseIssue called for a chat task: issue number is synthetic") } } + +// daemonCanarySecrets are recognisable secret references and values that must +// never appear in a captured configuration document. The event this capture +// writes is readable by anyone who can read the task's timeline, so the +// document is a disclosure boundary, not just a convenience. +const ( + canaryForgeToken = "ghp_DAEMONCANARYFORGETOKEN01" + canaryWebhookSecret = "CANARY-WEBHOOK-SECRET-02" + canaryIdentityToken = "ghp_DAEMONCANARYIDENTITY03" + canaryProviderKey = "sk-DAEMONCANARYPROVIDERKEY04" + canaryBWSKeyName = "DAEMON-CANARY-BWS-KEY-NAME-05" + canaryTelegramToken = "tg-DAEMONCANARYTELEGRAMTOKEN06" + canaryNATSTokenEnv = "DAEMON_CANARY_NATS_TOKEN_ENV_07" + canaryStateStoreToken = "DAEMON-CANARY-STATE-STORE-TOKEN-08" +) + +// configWithCanarySecrets is a fully populated configuration carrying +// recognisable secrets in every place a deployment keeps one: the root forge, +// an identity's forge, the model providers, the chat channel, the state store +// and the NATS credential. A capture that serialized the wrong object -- the +// whole config, or a projection that forgot to drop a secret -- would carry +// one of them. +func configWithCanarySecrets() config.Config { + return config.Config{ + WorkDir: "/work/archie", + DBPath: "/work/archie/archie.db", + BotUser: "archie-bot", + BotEmail: "archie@example.com", + Label: "archie", + Forge: config.Forge{ + Type: "gitea", Host: "gitea.example.com", TokenEnv: "GITEA_TOKEN", + Token: secret.SecretRef{Engine: "bws", Key: canaryForgeToken}, + WebhookSecret: secret.SecretRef{Engine: "bws", Key: canaryWebhookSecret}, + Intake: "webhook", + }, + Models: map[string]string{"builder": "openai/gpt-4", "planner": "openai/gpt-4"}, + Providers: map[string]config.Provider{ + "openai": { + Class: "openai", APIKeyEnv: "OPENAI_API_KEY", BaseURL: "https://api.openai.com/v1", + APIKey: secret.SecretRef{Engine: "bws", Key: canaryBWSKeyName}, + }, + "custom": { + Class: "custom", APIKey: secret.SecretRef{Engine: "literal", Key: canaryProviderKey}, + }, + }, + Budgets: config.Budgets{MaxSteps: 40, GateMaxFailures: 2}, + DiffCapLines: 800, + Dispatch: config.Dispatch{ + Trigger: "either", AckReaction: "eyes", + Labels: map[string]string{"queued": "archie:queued"}, + }, + Notify: config.Notify{Webhook: "https://hooks.example.com/notify"}, + Tools: config.ToolsConfig{Policy: config.ToolPolicy{MaxResultChars: 12000, SpillDir: "/tmp/spill"}}, + NATS: config.NATSConfig{Mode: "external", URL: "nats://127.0.0.1:4222", TokenEnv: canaryNATSTokenEnv}, + Chat: config.ChatConfig{Operator: "sam", Telegram: config.TelegramConfig{TokenEnv: "TELEGRAM_TOKEN", Token: secret.SecretRef{Engine: "bws", Key: canaryTelegramToken}}}, + Services: config.Services{State: config.ServiceConnection{Target: "127.0.0.1:50051", TargetToken: canaryStateStoreToken}}, + ModelLimits: map[string]config.ModelLimits{ + "openai/gpt-4": {ContextWindow: 128000, MaxOutputTokens: 4096}, + }, + Identities: []config.IdentityConfig{ + { + Name: "gitea-bot", BotUser: "gitea-bot", BotEmail: "gitea-bot@example.com", + Forge: config.Forge{Type: "gitea", Host: "gitea.example.com", Token: secret.SecretRef{Engine: "bws", Key: canaryIdentityToken}}, + Models: map[string]string{"builder": "openai/gpt-4"}, + Budgets: config.Budgets{MaxSteps: 40}, + }, + }, + } +} + +// TestDispatchCapturesTheAttemptsEffectiveConfig is R4's producer half: the +// configuration an attempt ran under is retrievable for that attempt. +// +// It asserts three separate claims rather than one: the event is durable and +// attributed to the attempt, the document is the dispatch's own TaskConfig, +// and no secret value anywhere in the configuration reaches it. +func TestDispatchCapturesTheAttemptsEffectiveConfig(t *testing.T) { + d, s, busClient := daemonWithNATS(t) + d.Cfg.Set(configWithCanarySecrets()) + ctx := context.Background() + + if _, err := s.EnqueueIssue(ctx, "acme", "widget", 5, "t", "b", "", ""); err != nil { + t.Fatal(err) + } + task, err := s.ClaimNext(ctx) + if err != nil || task == nil { + t.Fatalf("claim: (%v, %v)", task, err) + } + if task.Attempt == 0 { + t.Fatal("claimed task has attempt 0; the event below could not be attributed to a run") + } + + sub, err := mustCoreConn(t, busClient).Subscribe(agentnats.SubjectForTask(task.ID), func(msg *natsio.Msg) { + data, _ := json.Marshal(taskrun.Response{Status: workflow.StatusPROpen}) + _ = msg.Respond(data) + }) + if err != nil { + t.Fatalf("subscribe: %v", err) + } + t.Cleanup(func() { _ = sub.Unsubscribe() }) + + d.runViaAgent(ctx, task, config.Repo{Owner: "acme", Name: "widget", Base: "main"}) + + timeline, err := s.TaskEvents(ctx, task.ID) + if err != nil { + t.Fatal(err) + } + var captures []events.Event + for _, e := range timeline { + if e.Kind == events.KindConfigCaptured { + captures = append(captures, e) + } + } + if len(captures) != 1 { + t.Fatalf("config_captured events = %d, want exactly 1 for the attempt", len(captures)) + } + captured := captures[0] + if captured.Attempt != task.Attempt { + t.Errorf("capture attempt = %d, want the dispatched attempt %d", captured.Attempt, task.Attempt) + } + + encoded, err := json.Marshal(captured) + if err != nil { + t.Fatal(err) + } + document := string(encoded) + for _, leak := range []string{ + canaryForgeToken, canaryWebhookSecret, canaryIdentityToken, canaryProviderKey, + canaryBWSKeyName, canaryTelegramToken, canaryNATSTokenEnv, canaryStateStoreToken, + } { + if strings.Contains(document, leak) { + t.Errorf("captured configuration document leaked %q:\n%s", leak, document) + } + } + + var data struct { + Schema string `json:"schema"` + Document json.RawMessage `json:"document"` + } + if err := json.Unmarshal(encoded, &data); err != nil { + t.Fatal(err) + } + // The event wrapper carries kind/stage/attempt; the document itself is + // what the Config tab renders. + var wrapper struct { + Data struct { + Schema string `json:"schema"` + Document json.RawMessage `json:"document"` + } `json:"data"` + } + if err := json.Unmarshal(encoded, &wrapper); err != nil { + t.Fatal(err) + } + if wrapper.Data.Schema != events.ConfigCapturedSchema { + t.Errorf("schema = %q, want %q", wrapper.Data.Schema, events.ConfigCapturedSchema) + } + var doc map[string]any + if err := json.Unmarshal(wrapper.Data.Document, &doc); err != nil { + t.Fatalf("document is not a JSON object: %v", err) + } + + // The document is exactly TaskConfig's field set, not the larger dashboard + // projection: a reader must be able to trust that it carries no providers, + // repositories, identities or lock state. + wantKeys := []string{ + "bot_email", "bot_user", "budgets", "diff_cap_lines", "dispatch", + "forge", "model_limits", "models", "notify", "tool_policy", + } + gotKeys := make([]string, 0, len(doc)) + for key := range doc { + gotKeys = append(gotKeys, key) + } + sort.Strings(gotKeys) + if !slices.Equal(gotKeys, wantKeys) { + t.Errorf("document keys = %v, want exactly %v", gotKeys, wantKeys) + } + if doc["forge"] == nil || !strings.Contains(document, "gitea.example.com") { + t.Error("document is missing the non-secret forge host the Config tab renders") + } + + // The size decides whether a per-attempt document belongs in an event at + // all; recorded so the number is reviewable, not assumed. + t.Logf("serialized config document: %d bytes for a fully populated deployment", len(wrapper.Data.Document)) + if len(wrapper.Data.Document) == 0 { + t.Error("document is empty; the Config tab would render nothing") + } + + // Models and ModelLimits are the only two fields whose size is set by the + // deployment rather than by the struct, so the growth factor gets measured + // too instead of assumed. + scaled := configWithCanarySecrets() + scaled.Models = map[string]string{} + scaled.ModelLimits = map[string]config.ModelLimits{} + for i := range 20 { + ref := fmt.Sprintf("openai/model-%d", i) + scaled.Models[fmt.Sprintf("role-%d", i)] = ref + scaled.ModelLimits[ref] = config.ModelLimits{ContextWindow: 128000, MaxOutputTokens: 4096} + } + scaledDoc, err := json.Marshal(scaled.ForTask()) + if err != nil { + t.Fatal(err) + } + t.Logf("serialized config document with 20 named models: %d bytes", len(scaledDoc)) +} + +// failingEventStore is the daemon's store surface with an event insert that +// always fails, so the fail-open policy of the capture can be observed without +// breaking everything else the dispatch needs. +type failingEventStore struct { + *store.Store +} + +func (f failingEventStore) InsertEvent(context.Context, events.Event) (int64, error) { + return 0, fmt.Errorf("events table unavailable") +} + +// TestCaptureAttemptConfigFailureDoesNotStopTheDispatch: a provenance record +// that could not be written must not cost the run. The taskrun request has to +// go out anyway, and nothing may park. +func TestCaptureAttemptConfigFailureDoesNotStopTheDispatch(t *testing.T) { + d, s, busClient := daemonWithNATS(t) + d.Store = failingEventStore{Store: s} + ctx := context.Background() + + if _, err := s.EnqueueIssue(ctx, "acme", "widget", 6, "t", "b", "", ""); err != nil { + t.Fatal(err) + } + task, err := s.ClaimNext(ctx) + if err != nil || task == nil { + t.Fatalf("claim: (%v, %v)", task, err) + } + + dispatched := make(chan struct{}, 1) + sub, err := mustCoreConn(t, busClient).Subscribe(agentnats.SubjectForTask(task.ID), func(msg *natsio.Msg) { + dispatched <- struct{}{} + data, _ := json.Marshal(taskrun.Response{Status: workflow.StatusPROpen}) + _ = msg.Respond(data) + }) + if err != nil { + t.Fatalf("subscribe: %v", err) + } + t.Cleanup(func() { _ = sub.Unsubscribe() }) + + d.runViaAgent(ctx, task, config.Repo{Owner: "acme", Name: "widget", Base: "main"}) + + select { + case <-dispatched: + case <-time.After(time.Second): + t.Fatal("the taskrun request was never published; a failed config capture stopped the dispatch") + } + + got, err := s.TaskByIssue(ctx, "acme", "widget", 6) + if err != nil || got == nil { + t.Fatalf("TaskByIssue: (%+v, %v)", got, err) + } + if got.Status == workflow.StatusParked { + t.Errorf("task parked with reason %q; a failed config capture must not park a run", got.ParkReason) + } +} + +// TestTaskAgnosticDaemonEventsCarryNoAttempt pins the other half of the +// attribution rule: an event produced before any task row exists has no +// attempt to name, and inventing one -- the deployment's "current" attempt, a +// zero-that-means-first -- would be a fabricated claim. It stays 0, which the +// reader reports as unattributed. +func TestTaskAgnosticDaemonEventsCarryNoAttempt(t *testing.T) { + d := &Daemon{Bus: events.NewBus(), Log: slog.New(slog.DiscardHandler)} + sub := d.Bus.Subscribe(4) + t.Cleanup(sub.Close) + + d.acknowledge(context.Background(), &recordingForge{}, config.Config{}, + config.Repo{Owner: "acme", Name: "widget"}, forge.Issue{Number: 3, Title: "a task"}) + + select { + case e := <-sub.C: + if e.Kind != events.KindTaskQueued { + t.Fatalf("kind = %q, want %q", e.Kind, events.KindTaskQueued) + } + if e.Attempt != 0 { + t.Errorf("queued event attempt = %d, want 0 (no task exists yet to attribute it to)", e.Attempt) + } + case <-time.After(time.Second): + t.Fatal("no queued event was published") + } +} + +// TestReconcilePRsAttributesTheOutcomeToTheTasksAttempt: a PR is merged or +// rejected against the attempt whose task recorded that PR number, so the +// timeline can attribute the outcome to the run that produced it instead of +// leaving it unattributed. +func TestReconcilePRsAttributesTheOutcomeToTheTasksAttempt(t *testing.T) { + d, s, fg := testDaemon(t, 3, 0) + ctx := context.Background() + d.Bus = events.NewBus() + sub := d.Bus.Subscribe(8) + t.Cleanup(sub.Close) + + if _, err := s.EnqueueIssue(ctx, "acme", "widget", 42, "a task", "", "", ""); err != nil { + t.Fatal(err) + } + task, err := s.ClaimNext(ctx) + if err != nil || task == nil { + t.Fatalf("ClaimNext = (%+v, %v)", task, err) + } + if task.Attempt == 0 { + t.Fatal("claimed task has attempt 0; this test could not tell a stamp from a coincidence") + } + task.PRNumber = 7 + if err := s.Update(ctx, task); err != nil { + t.Fatal(err) + } + if err := s.Transition(ctx, task.ID, workflow.StatusRunning, workflow.StatusPROpen, ""); err != nil { + t.Fatal(err) + } + fg.prStates = map[int]string{7: "merged"} + + d.reconcilePRs(ctx) + + select { + case e := <-sub.C: + if e.Kind != events.KindPRMerged { + t.Fatalf("kind = %q, want %q", e.Kind, events.KindPRMerged) + } + if e.Attempt != task.Attempt { + t.Errorf("pr_merged attempt = %d, want the task's attempt %d", e.Attempt, task.Attempt) + } + case <-time.After(time.Second): + t.Fatal("reconcilePRs published no merge event") + } +} diff --git a/internal/domain/taskactions/service.go b/internal/domain/taskactions/service.go index c28113228..047a80b50 100644 --- a/internal/domain/taskactions/service.go +++ b/internal/domain/taskactions/service.go @@ -22,6 +22,11 @@ type Task struct { Owner, Repo, Identity, Status, Stage, ParkReason string IssueNumber, RetryCount int ForgeBacked bool + // Attempt is the run the operator acted on. It is stamped onto the event + // this action records so an intervention is attributable to the run it + // changed course -- without it a retry or a stop is indistinguishable + // between attempts on the timeline. + Attempt int } type Store interface { @@ -85,7 +90,7 @@ func (s Service) apply(ctx context.Context, task *Task, identity *string, action if identity != nil { source = "chat" } - o := outcome{event: events.Event{TaskID: task.ID, Repo: task.Owner + "/" + task.Repo, Issue: task.IssueNumber}} + o := outcome{event: events.Event{TaskID: task.ID, Repo: task.Owner + "/" + task.Repo, Issue: task.IssueNumber, Attempt: task.Attempt}} switch action { case taskstate.ActionApprove: err := s.Store.Requeue(ctx, task.ID, "waiting_human", "implement") diff --git a/internal/domain/workflow/changes_test.go b/internal/domain/workflow/changes_test.go new file mode 100644 index 000000000..2af734bb2 --- /dev/null +++ b/internal/domain/workflow/changes_test.go @@ -0,0 +1,588 @@ +package workflow + +import ( + "context" + "encoding/json" + "fmt" + "log/slog" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/samcharles93/archie-core/internal/agentexec" + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/domain/workflow/task" + "github.com/samcharles93/archie-core/internal/events" + "github.com/samcharles93/archie-core/internal/logging" +) + +// recordingStore is the narrow Store the engine calls mid-run, keeping the +// events it was asked to persist. A capture is durable provenance, so the +// assertions below read this and not the lossy bus. +type recordingStore struct { + events []events.Event + transitions []transition + insertEr error +} + +// transition is one status change the engine asked the store for. Parking is +// observable only through these, which is what makes "a reporting failure +// never parks the task" a test rather than a reading of the code. +type transition struct{ from, to, detail string } + +func (s *recordingStore) Update(context.Context, *Task) error { return nil } + +func (s *recordingStore) Transition(_ context.Context, _ int64, from, to, detail string) error { + s.transitions = append(s.transitions, transition{from: from, to: to, detail: detail}) + return nil +} + +func (s *recordingStore) InsertEvent(_ context.Context, e events.Event) (int64, error) { + if s.insertEr != nil { + return 0, s.insertEr + } + s.events = append(s.events, e) + return int64(len(s.events)), nil +} + +func (s *recordingStore) ofKind(kind string) []events.Event { + var out []events.Event + for _, e := range s.events { + if e.Kind == kind { + out = append(out, e) + } + } + return out +} + +// capturingTrees is a Trees implementation that can also report what changed. +// fakeTrees deliberately stays without the capability, so every existing test +// that drives a stage through it exercises the degrade path. +type capturingTrees struct { + fakeTrees + stats task.ChangeStats + err error + calls int + dir string + base string +} + +func (c *capturingTrees) ChangedFileStats(_ context.Context, dir, base string) (task.ChangeStats, error) { + c.calls++ + c.dir, c.base = dir, base + return c.stats, c.err +} + +var ( + _ Trees = (*fakeTrees)(nil) + _ Trees = (*capturingTrees)(nil) + _ changeStatsReader = (*capturingTrees)(nil) + _ task.Store = (*recordingStore)(nil) +) + +// capturePayload mirrors the frozen persisted shape of a changes_captured +// event. Decoding the event's data through it is what proves the field names +// a reader depends on. +type capturePayload struct { + Schema string `json:"schema"` + Owner string `json:"owner"` + Repo string `json:"repo"` + Base string `json:"base"` + Branch string `json:"branch"` + HeadSHA string `json:"head_sha"` + BaseSHA string `json:"base_sha"` + PRNumber int `json:"pr_number"` + CapturedAfter string `json:"captured_after"` + Files []task.FileChange `json:"files"` + Totals task.ChangeTotals `json:"totals"` + Truncated bool `json:"truncated"` +} + +func decodeCapture(t *testing.T, e events.Event) capturePayload { + t.Helper() + raw, err := json.Marshal(e.Data) + if err != nil { + t.Fatalf("marshal capture data: %v", err) + } + var payload capturePayload + if err := json.Unmarshal(raw, &payload); err != nil { + t.Fatalf("decode capture data: %v", err) + } + return payload +} + +func sampleStats() task.ChangeStats { + return task.ChangeStats{ + BaseSHA: strings.Repeat("a", 40), + HeadSHA: strings.Repeat("b", 40), + Files: []task.FileChange{ + {Path: "internal/x.go", Status: task.ChangeModified, Additions: 12, Deletions: 3}, + {Path: "docs/y.md", OldPath: "docs/z.md", Status: task.ChangeRenamed}, + }, + Totals: task.ChangeTotals{Files: 2, Additions: 12, Deletions: 3}, + } +} + +func captureTaskContext(t *testing.T, trees Trees, store Store) *TaskContext { + t.Helper() + return &TaskContext{ + Task: &Task{ + ID: 11, Attempt: 2, Owner: "acme", Repo: "widget", IssueNumber: 7, + Stage: "commit-push", Branch: "feat/7-widget", + }, + Repo: config.Repo{Owner: "acme", Name: "widget", Base: "main"}, + Trees: trees, + Store: store, + Dir: "/worktree", + Branch: "feat/7-widget", + Log: slog.New(slog.DiscardHandler), + } +} + +// TestRunStampsTheAttemptOnEveryEventItEmits is R2's producer half. The +// attempt is read from the task record the run holds, so an event +// is attributable to one attempt without segmenting the stream by stage order, +// and nothing here guesses an attempt it does not have. +func TestRunStampsTheAttemptOnEveryEventItEmits(t *testing.T) { + bus := events.NewBus() + sub := bus.Subscribe(32) + t.Cleanup(sub.Close) + + tc := &TaskContext{ + Task: &Task{ID: 4, Attempt: 3, Owner: "o", Repo: "r", IssueNumber: 1, Status: StatusRunning}, + Repo: config.Repo{Owner: "o", Name: "r"}, + Store: &recordingStore{}, + Bus: bus, + Log: slog.New(slog.DiscardHandler), + } + Run(context.Background(), Workflow{Name: "w", Stages: []Stage{{Name: "build", Run: func(context.Context, *TaskContext) error { + return fmt.Errorf("boom") + }}}}, tc) + + var emitted []events.Event + for { + select { + case e := <-sub.C: + emitted = append(emitted, e) + default: + if len(emitted) == 0 { + t.Fatal("run emitted no events at all; the assertion below would be vacuous") + } + for _, e := range emitted { + if e.Attempt != 3 { + t.Errorf("event %s attempt = %d, want 3", e.Kind, e.Attempt) + } + } + return + } + } +} + +// TestRunBindsTheStageOntoTheTaskLoggerOnlyForTheStage is R5's producer half: +// a line a stage's own code writes must be selectable by stage, and a line +// written outside any stage must not be attributed to one. +// +// It asserts against the logging entry the reader actually filters on +// (internal/logging.Query.Stage matches Entry.Fields["stage"]) rather than +// against a rendered line, so a change in the handler cannot make this pass +// while the filter stays broken. +func TestRunBindsTheStageOntoTheTaskLoggerOnlyForTheStage(t *testing.T) { + feed := logging.NewFeed(32) + outer := slog.New(logging.NewFeedHandler(slog.NewJSONHandler(discardSink{}, nil), feed)) + tc := &TaskContext{ + Task: &Task{ID: 4, Attempt: 1, Owner: "o", Repo: "r", IssueNumber: 1, Status: StatusRunning}, + Repo: config.Repo{Owner: "o", Name: "r"}, + Store: &recordingStore{}, + Log: outer, + } + Run(context.Background(), Workflow{Name: "w", Stages: []Stage{{Name: "build", Run: func(_ context.Context, tc *TaskContext) error { + tc.Log.Info("inside the stage") + return nil + }}}}, tc) + + stages := map[string]string{} + for _, entry := range feed.Snapshot() { + stage, _ := entry.Fields["stage"].(string) + stages[entry.Message] = stage + } + starting, ok := stages["stage starting"] + if !ok { + t.Fatalf("no stage-start line was logged at all: %v", stages) + } + if starting != "build" { + t.Errorf("stage starting carries stage %q, want build", starting) + } + if got := stages["inside the stage"]; got != "build" { + t.Errorf("a line logged by the stage's own code carries stage %q, want build", got) + } + + // Restored afterwards: a line the engine writes outside a stage is not + // attributable to one, and claiming it were would make a stage filter lie. + if tc.Log != outer { + t.Error("tc.Log was not restored after the stage; the next stage's lines would inherit a stale stage") + } + outer.Info("after the run") + entries := feed.Snapshot() + last := entries[len(entries)-1] + if last.Message != "after the run" { + t.Fatalf("last entry = %q, want the line logged after the run", last.Message) + } + if stage, ok := last.Fields["stage"]; ok { + t.Errorf("a line logged after the run carries stage %v, want none", stage) + } +} + +// discardSink absorbs what the wrapped JSON handler renders, so this test's +// assertions read the feed and never a terminal. It cannot be io.Discard or +// slog.DiscardHandler: FeedHandler.Enabled delegates to the wrapped handler, +// and slog.DiscardHandler reports every record as disabled, which leaves the +// feed empty and the assertions below vacuous. +type discardSink struct{} + +func (discardSink) Write(p []byte) (int, error) { return len(p), nil } + +func TestStageCommitCapturesTheChangedFiles(t *testing.T) { + trees := &capturingTrees{commitAllChanged: true, stats: sampleStats()} + store := &recordingStore{} + tc := captureTaskContext(t, trees, store) + tc.Task.Stage = "commit-repro" + + if err := StageCommit("commit-repro", func(*TaskContext) string { return "msg" }).Run(context.Background(), tc); err != nil { + t.Fatalf("StageCommit.Run() = %v, want nil", err) + } + + captures := store.ofKind(events.KindChangesCaptured) + if len(captures) != 1 { + t.Fatalf("changes_captured events = %d, want exactly 1", len(captures)) + } + got := captures[0] + if got.Attempt != 2 { + t.Errorf("capture attempt = %d, want the task's own attempt 2", got.Attempt) + } + if got.Stage != "commit-repro" { + t.Errorf("capture stage = %q, want the capturing stage", got.Stage) + } + if got.TaskID != tc.Task.ID || got.Repo != "acme/widget" || got.Workflow != tc.Task.Workflow { + t.Errorf("capture identity = (%d, %q, %q), want the task's", got.TaskID, got.Repo, got.Workflow) + } + if trees.dir != tc.Dir || trees.base != "main" { + t.Errorf("diffstat read (%q, %q), want the worktree and the repo's base branch", trees.dir, trees.base) + } + + payload := decodeCapture(t, got) + if payload.Schema != events.ChangesCapturedSchema { + t.Errorf("payload schema = %q, want %q: a reader refuses a capture it cannot name", + payload.Schema, events.ChangesCapturedSchema) + } + if payload.Owner != "acme" || payload.Repo != "widget" || payload.Base != "main" || payload.Branch != "feat/7-widget" { + t.Errorf("payload coordinates = %+v", payload) + } + if payload.CapturedAfter != capturedAfterCommit { + t.Errorf("captured_after = %q, want %q", payload.CapturedAfter, capturedAfterCommit) + } + if payload.HeadSHA != strings.Repeat("b", 40) || payload.BaseSHA != strings.Repeat("a", 40) { + t.Errorf("payload SHAs = (%q, %q), want the measured pair", payload.BaseSHA, payload.HeadSHA) + } + if len(payload.Files) != 2 || payload.Files[0].Path != "internal/x.go" || payload.Files[1].OldPath != "docs/z.md" { + t.Errorf("payload files = %+v", payload.Files) + } + if payload.Totals != (task.ChangeTotals{Files: 2, Additions: 12, Deletions: 3}) { + t.Errorf("payload totals = %+v", payload.Totals) + } +} + +// TestStageCommitPushCapturesOnlyAfterThePush pins the second capture point: +// a push that failed is not a change that landed, and a capture claiming one +// would be worse than no capture. +func TestStageCommitPushCapturesOnlyAfterThePush(t *testing.T) { + tests := []struct { + name string + pushShares bool + }{ + {name: "after a successful push", pushShares: true}, + {name: "not after a failed push", pushShares: false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + trees := &capturingTrees{commitAllChanged: true, stats: sampleStats()} + if !tt.pushShares { + trees.pushErr = fmt.Errorf("push refused") + } + store := &recordingStore{} + tc := captureTaskContext(t, trees, store) + + err := StageCommitPush(func(*TaskContext) string { return "msg" }).Run(context.Background(), tc) + captures := store.ofKind(events.KindChangesCaptured) + if !tt.pushShares { + if err == nil { + t.Fatal("StageCommitPush.Run() = nil on a failed push, want the push error") + } + if len(captures) != 0 { + t.Fatalf("captures = %d after a failed push, want none", len(captures)) + } + return + } + if err != nil { + t.Fatalf("StageCommitPush.Run() = %v, want nil", err) + } + if len(captures) != 1 { + t.Fatalf("changes_captured events = %d, want exactly 1", len(captures)) + } + if got := decodeCapture(t, captures[0]).CapturedAfter; got != capturedAfterCommitPush { + t.Errorf("captured_after = %q, want %q", got, capturedAfterCommitPush) + } + }) + } +} + +// TestChangeCaptureDegradesWithoutTheCapability: the capture is an optional +// capability, so a Trees implementation that cannot report a diffstat must +// lose the record and nothing else. fakeTrees is exactly that implementation. +func TestChangeCaptureDegradesWithoutTheCapability(t *testing.T) { + trees := &fakeTrees{commitAllChanged: true} + store := &recordingStore{} + tc := captureTaskContext(t, trees, store) + + if err := StageCommit("commit", func(*TaskContext) string { return "msg" }).Run(context.Background(), tc); err != nil { + t.Fatalf("StageCommit.Run() = %v, want the stage to succeed without a capture", err) + } + if got := len(store.ofKind(events.KindChangesCaptured)); got != 0 { + t.Errorf("changes_captured events = %d, want none", got) + } +} + +// TestChangeCaptureFailureNeverFailsTheStage: reporting is not the work. A +// worktree that cannot be measured, and a store that rejects the write, must +// both leave the stage's own result untouched. +func TestChangeCaptureFailureNeverFailsTheStage(t *testing.T) { + tests := []struct { + name string + trees Trees + store Store + reason string + }{ + { + name: "the diffstat could not be read", + trees: &capturingTrees{commitAllChanged: true, err: fmt.Errorf("worktree vanished")}, + store: &recordingStore{}, + reason: "a worktree the daemon already deleted", + }, + { + name: "the capture could not be persisted", + trees: &capturingTrees{commitAllChanged: true, stats: sampleStats()}, + store: &recordingStore{insertEr: fmt.Errorf("store unavailable")}, + reason: "a store that refused the row", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tc := captureTaskContext(t, tt.trees, tt.store) + if err := StageCommit("commit", func(*TaskContext) string { return "msg" }).Run(context.Background(), tc); err != nil { + t.Fatalf("StageCommit.Run() = %v, want nil despite %s", err, tt.reason) + } + }) + } +} + +// TestOpenPRCapturesThePullRequestNumber is R3's link requirement at the +// producer. The captures taken at commit and push time run before the PR +// exists, so their pr_number is necessarily 0; OpenPR captures once more as +// soon as the number is recorded, which is what lets the changed-files view +// link the pull request. +func TestOpenPRCapturesThePullRequestNumber(t *testing.T) { + trees := &capturingTrees{stats: sampleStats()} + store := &recordingStore{} + forgeClient := &fakeForge{} + tc := captureTaskContext(t, trees, store) + tc.Forge = forgeClient + tc.Task.Stage = "open-pr" + tc.Task.Title = "widget fails" + + if err := OpenPR(context.Background(), tc, "body"); err != nil { + t.Fatalf("OpenPR() = %v, want nil", err) + } + if tc.Task.PRNumber == 0 { + t.Fatal("OpenPR recorded no PR number; the assertion below would be vacuous") + } + + captures := store.ofKind(events.KindChangesCaptured) + if len(captures) != 1 { + t.Fatalf("changes_captured events = %d, want the capture OpenPR records once the PR number exists", len(captures)) + } + payload := decodeCapture(t, captures[0]) + if payload.CapturedAfter != capturedAfterOpenPR { + t.Errorf("captured_after = %q, want %q", payload.CapturedAfter, capturedAfterOpenPR) + } + if payload.PRNumber != tc.Task.PRNumber { + t.Errorf("pr_number = %d, want the number OpenPR recorded (%d)", payload.PRNumber, tc.Task.PRNumber) + } + if captures[0].Stage != "open-pr" { + t.Errorf("capture stage = %q, want the capturing stage", captures[0].Stage) + } +} + +// TestChangeCaptureTruncatesTheFileListButNotTheTotals: one large refactor +// must not write an unbounded payload, and a capture that dropped entries must +// still report how much the attempt actually changed. +func TestChangeCaptureTruncatesTheFileListButNotTheTotals(t *testing.T) { + files := make([]task.FileChange, 0, task.MaxCapturedFiles+5) + for i := range task.MaxCapturedFiles + 5 { + files = append(files, task.FileChange{ + Path: fmt.Sprintf("file-%03d.go", i), Status: task.ChangeAdded, Additions: 1, + }) + } + stats := task.ChangeStats{ + Files: files, + Totals: task.ChangeTotals{Files: len(files), Additions: len(files)}, + } + trees := &capturingTrees{commitAllChanged: true, stats: stats} + store := &recordingStore{} + tc := captureTaskContext(t, trees, store) + + if err := StageCommit("commit", func(*TaskContext) string { return "msg" }).Run(context.Background(), tc); err != nil { + t.Fatal(err) + } + captures := store.ofKind(events.KindChangesCaptured) + if len(captures) != 1 { + t.Fatalf("changes_captured events = %d, want 1", len(captures)) + } + payload := decodeCapture(t, captures[0]) + if len(payload.Files) != task.MaxCapturedFiles { + t.Errorf("payload carries %d files, want the cap of %d", len(payload.Files), task.MaxCapturedFiles) + } + if !payload.Truncated { + t.Error("truncated = false, want true when entries were dropped") + } + if payload.Totals != (task.ChangeTotals{Files: len(files), Additions: len(files)}) { + t.Errorf("totals = %+v, want the full set %d", payload.Totals, len(files)) + } + if payload.Totals.Files == len(payload.Files) { + t.Error("totals were recomputed over the truncated list; a reader would understate the change") + } +} + +// TestBaselineFixCapturesItsOwnCommit pins the third capture point: the +// baseline repair is a real, gate-verified commit of its own, and it is the +// only record of that intermediate state once the later push capture folds it +// into one bigger change. +func TestBaselineFixCapturesItsOwnCommit(t *testing.T) { + dir := t.TempDir() + script := filepath.Join(dir, "gate.sh") + if err := os.WriteFile(script, []byte("#!/bin/sh\nexit 1\n"), 0o755); err != nil { + t.Fatal(err) + } + runner := agentRunnerFunc(func(context.Context, string, agentexec.Request, agentexec.ToolCallReporter) (agentexec.Result, error) { + return agentexec.Result{Version: agentexec.ProtocolVersion, Status: agentexec.StatusPassed}, nil + }) + + tests := []struct { + name string + changed bool + want int + }{ + {name: "a real fix is captured", changed: true, want: 1}, + {name: "a commit that produced nothing is not", changed: false, want: 0}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + trees := &capturingTrees{commitAllChanged: tt.changed, stats: sampleStats()} + store := &recordingStore{} + tc := &TaskContext{ + Task: &Task{ID: 9, Attempt: 1, Owner: "o", Repo: "r", Status: StatusRunning}, + Repo: config.Repo{Owner: "o", Name: "r", Gate: [][]string{{"sh", script}}}, + Cfg: config.Config{Models: map[string]string{"builder": "provider/model"}}, + Agent: runner, + Trees: trees, + Store: store, + Dir: dir, + Branch: "feat/9-x", + Log: slog.New(slog.DiscardHandler), + } + if err := StageBaselineGate().Run(context.Background(), tc); err != nil { + t.Fatalf("StageBaselineGate.Run() = %v", err) + } + captures := store.ofKind(events.KindChangesCaptured) + if len(captures) != tt.want { + t.Fatalf("changes_captured events = %d, want %d", len(captures), tt.want) + } + if tt.want == 0 { + return + } + if got := decodeCapture(t, captures[0]).CapturedAfter; got != capturedAfterBaselineFix { + t.Errorf("captured_after = %q, want %q", got, capturedAfterBaselineFix) + } + if !tc.BaselineFixed { + t.Error("BaselineFixed = false after a captured baseline commit") + } + }) + } +} + +// TestCaptureIsNeverCallableFromTrees makes the constraint explicit in code, +// next to the type that must not grow past it: the capture is an optional, +// unexported capability, and Trees -- the surface interpreted stage code is +// handed -- must not name it. wfextract's reachability test proves the same +// thing from the interpreted side. +func TestCaptureIsNeverCallableFromTrees(t *testing.T) { + if _, ok := any((*fakeTrees)(nil)).(changeStatsReader); ok { + t.Error("fakeTrees satisfies the capture capability; the degrade test above would be vacuous") + } + if _, ok := any(Trees(nil)).(changeStatsReader); ok { + t.Error("Trees names the capture capability; interpreted stage code could call it") + } +} + +// TestAFailedCaptureNeverParksTheTask runs the whole engine, not just the +// step: the only way a capture failure could reach the operator as a parked +// task is through Run's own error path, so the assertion has to be made +// against Run's transitions. +func TestAFailedCaptureNeverParksTheTask(t *testing.T) { + tests := []struct { + name string + trees Trees + }{ + { + name: "the diffstat could not be read", + trees: &capturingTrees{commitAllChanged: true, err: fmt.Errorf("worktree vanished")}, + }, + { + name: "the capture could not be persisted", + trees: &capturingTrees{commitAllChanged: true, stats: sampleStats()}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + store := &recordingStore{} + if tt.name == "the capture could not be persisted" { + // Only the capture's own insert fails; every other write the + // engine makes still has to succeed for the run to finish. + store = &recordingStore{insertEr: fmt.Errorf("store unavailable")} + } + tc := captureTaskContext(t, tt.trees, store) + tc.Task.Status = StatusRunning + + Run(context.Background(), Workflow{Name: "implement", Stages: []Stage{ + StageCommitPush(func(*TaskContext) string { return "msg" }), + {Name: "open-pr", Run: func(_ context.Context, tc *TaskContext) error { + tc.Outcome = Outcome{Status: StatusPROpen, Detail: "PR #1"} + return nil + }}, + }}, tc) + + for _, tr := range store.transitions { + if tr.to == StatusParked { + t.Fatalf("task parked (%s -> %s: %s); a change capture must never park a run that succeeded", + tr.from, tr.to, tr.detail) + } + } + if tc.Outcome.Status != StatusPROpen { + t.Errorf("outcome = %q, want the stage's own %q", tc.Outcome.Status, StatusPROpen) + } + }) + } +} diff --git a/internal/domain/workflow/implement.go b/internal/domain/workflow/implement.go index 32e79ef1d..74a489b1b 100644 --- a/internal/domain/workflow/implement.go +++ b/internal/domain/workflow/implement.go @@ -111,6 +111,7 @@ func StageBaselineGate() Stage { if changed { tc.BuildSummary = res.Summary tc.BaselineFixed = true + tc.captureChanges(ctx, capturedAfterBaselineFix) } } return nil diff --git a/internal/domain/workflow/implement_test.go b/internal/domain/workflow/implement_test.go index e74d6458a..2859a61fc 100644 --- a/internal/domain/workflow/implement_test.go +++ b/internal/domain/workflow/implement_test.go @@ -382,6 +382,7 @@ func TestOpenPRLinksSourceBranchBeforeCreatingPR(t *testing.T) { Task: &Task{ID: 1, Owner: "acme", Repo: "widget", IssueNumber: 42, Title: "Fix bug"}, Repo: config.Repo{Owner: "acme", Name: "widget", Base: "main"}, Branch: "fix/42-bug", + Log: slog.New(slog.DiscardHandler), } if err := OpenPR(context.Background(), tc, "summary"); err != nil { t.Fatal(err) @@ -420,10 +421,15 @@ func TestStageCommitPushDoesNotUseSyntheticIssueForChatNoOp(t *testing.T) { // fakeTrees implements Trees, tracking calls without touching a real // worktree. commitAllChanged controls what CommitAll reports. +// +// It deliberately does NOT implement the unexported changeStatsReader +// capability: every stage test that drives a fake tree therefore also covers +// the path where a capture cannot be taken at all. type fakeTrees struct { commitAllChanged bool pushed bool pushBranch string + pushErr error } func (f *fakeTrees) Prepare(context.Context, string, string, string, int, string, string, string) (string, string, error) { @@ -435,6 +441,9 @@ func (f *fakeTrees) CommitAll(context.Context, string, string) (bool, error) { } func (f *fakeTrees) Push(_ context.Context, _, branch string) error { + if f.pushErr != nil { + return f.pushErr + } f.pushed = true f.pushBranch = branch return nil @@ -464,6 +473,7 @@ func TestStageCommitPushPushesBaselineFixEvenWithNothingNewToCommit(t *testing.T Dir: "/tmp/test", Branch: "archie/issue-1", Task: &Task{ID: 1, Owner: "o", Repo: "r", IssueNumber: 1}, + Log: slog.New(slog.DiscardHandler), } if err := stage.Run(context.Background(), tc); err != nil { @@ -488,6 +498,7 @@ func TestStageCommitPushStillErrorsOnEmptyTreeWithoutBaselineFix(t *testing.T) { Dir: "/tmp/test", Branch: "archie/issue-1", Task: &Task{ID: 1, Owner: "o", Repo: "r", IssueNumber: 1}, + Log: slog.New(slog.DiscardHandler), } if err := stage.Run(context.Background(), tc); err == nil { @@ -595,6 +606,7 @@ func TestOpenPRSurvivesALinkBranchFailure(t *testing.T) { Task: &Task{ID: 1, Owner: "acme", Repo: "widget", IssueNumber: 42, Title: "Fix bug"}, Repo: config.Repo{Owner: "acme", Name: "widget", Base: "main"}, Branch: "fix/42-bug", + Log: slog.New(slog.DiscardHandler), } if err := OpenPR(context.Background(), taskCtx, "summary"); err != nil { diff --git a/internal/domain/workflow/steps.go b/internal/domain/workflow/steps.go index ea5d41e0f..d4cd15997 100644 --- a/internal/domain/workflow/steps.go +++ b/internal/domain/workflow/steps.go @@ -8,6 +8,8 @@ import ( "strings" "time" + "github.com/samcharles93/archie-core/internal/domain/workflow/task" + "github.com/samcharles93/archie-core/internal/events" "github.com/samcharles93/archie-core/internal/gate" "github.com/samcharles93/archie-core/internal/gate/gateeval" ) @@ -15,6 +17,91 @@ import ( // Shared step library. Every workflow composes these; workflow-specific // stages live next to their workflow definition. +// The captured_after values recorded inside a changes_captured event. They +// are part of the on-disk payload: a reader distinguishes a mid-workflow +// commit from the push that ended the run, so renaming one is a migration, +// not a rename. +const ( + capturedAfterCommit = "commit" + capturedAfterCommitPush = "commit-push" + capturedAfterBaselineFix = "baseline-fix" + // capturedAfterOpenPR is the final capture of a run: OpenPR takes it the + // moment the PR number is recorded, because that is the only point at which + // a capture can carry one. + capturedAfterOpenPR = "open-pr" +) + +// changeStatsReader is the optional capability through which a Trees +// implementation reports what an attempt changed. It is deliberately +// unexported, and deliberately NOT a method on Trees: Trees is projected into +// the interpreted-stage symbol table, so declaring it there would widen what +// repository-authored .archie/stages/*.go may call (pinned by +// wfextract/reachability_test.go). A Trees implementation without it degrades +// to no capture rather than failing the stage. +type changeStatsReader interface { + ChangedFileStats(ctx context.Context, dir, base string) (task.ChangeStats, error) +} + +// captureChanges records what this attempt has changed, read off the worktree +// at the moment it was committed or pushed -- the only point where the +// worktree still holds the change and a commit or push is known to have +// happened. A retry resets the branch onto its base and a terminal state +// deletes the worktree, so nothing can re-derive this afterwards. +// +// Capturing is reporting, not work: every failure path here logs and returns, +// because a provenance record that could not be written must never park or +// fail a run that otherwise succeeded. +func (tc *TaskContext) captureChanges(ctx context.Context, after string) { + if tc.Dir == "" { + tc.Log.Warn("change capture skipped: no worktree to read", "captured_after", after) + return + } + reader, ok := tc.Trees.(changeStatsReader) + if !ok { + tc.Log.Warn("change capture unavailable: this worktree implementation cannot report a diffstat", + "captured_after", after) + return + } + stats, err := reader.ChangedFileStats(ctx, tc.Dir, tc.Repo.BaseBranch()) + if err != nil { + tc.Log.Warn("change capture failed", "captured_after", after, "err", err) + return + } + // Durable, never bus-only: the worktree this was measured from is gone by + // the time anyone reads it, so a capture that only reached the live feed + // would be lost with the process. + if err := tc.EmitDurable(ctx, events.KindChangesCaptured, tc.Task.Stage, "", changeCaptureData(tc, after, stats)); err != nil { + tc.Log.Warn("change capture not persisted", "captured_after", after, "err", err) + } +} + +// changeCaptureData builds the persisted payload for one capture. Totals are +// taken over the FULL set of changed files, so a capture truncated at +// task.MaxCapturedFiles still reports how much the attempt changed -- only the +// per-file breakdown is bounded. +func changeCaptureData(tc *TaskContext, after string, stats task.ChangeStats) map[string]any { + files := stats.Files + truncated := false + if len(files) > task.MaxCapturedFiles { + files = files[:task.MaxCapturedFiles] + truncated = true + } + return map[string]any{ + "schema": events.ChangesCapturedSchema, + "owner": tc.Task.Owner, + "repo": tc.Task.Repo, + "base": tc.Repo.BaseBranch(), + "branch": tc.Branch, + "head_sha": stats.HeadSHA, + "base_sha": stats.BaseSHA, + "pr_number": tc.Task.PRNumber, + "captured_after": after, + "files": files, + "totals": stats.Totals, + "truncated": truncated, + } +} + // StagePrepareWorktree clones the repo fresh and checks out the task // branch. Skips if the daemon already prepared the worktree (Docker // containers require the tree before acquire). @@ -42,6 +129,7 @@ func StageCommit(name string, message func(*TaskContext) string) Stage { if !changed { return fmt.Errorf("worktree has no changes to commit") } + tc.captureChanges(ctx, capturedAfterCommit) return nil }} } @@ -69,7 +157,11 @@ func StageCommitPush(message func(*TaskContext) string) Stage { if !changed && !tc.BaselineFixed { return fmt.Errorf("worktree has no changes to commit") } - return tc.Trees.Push(ctx, tc.Dir, tc.Branch) + if err := tc.Trees.Push(ctx, tc.Dir, tc.Branch); err != nil { + return err + } + tc.captureChanges(ctx, capturedAfterCommitPush) + return nil }} } @@ -211,6 +303,12 @@ func OpenPR(ctx context.Context, tc *TaskContext, body string) error { } t.PRNumber = num tc.Outcome = Outcome{Status: StatusPROpen, Detail: fmt.Sprintf("PR #%d", num)} + // The captures taken while committing and pushing ran before this PR + // existed, so they carry no number and the changed-files view cannot link + // it. This is the one point where the number is known and the worktree is + // still there to measure, so it is captured here rather than left for a + // read to reconstruct. Reporting only: captureChanges never fails the stage. + tc.captureChanges(ctx, capturedAfterOpenPR) return nil } diff --git a/internal/domain/workflow/task/changes.go b/internal/domain/workflow/task/changes.go new file mode 100644 index 000000000..df4a895c9 --- /dev/null +++ b/internal/domain/workflow/task/changes.go @@ -0,0 +1,67 @@ +package task + +// ChangeStats is the change one attempt produced, measured at the moment it +// was committed or pushed. +// +// It is a measurement, not a view: the worktree it was read from is deleted on +// merge, close and no-PR terminal states, and a retry resets the branch onto +// its base, so this is the only record of what an attempt actually changed. +// Nothing re-derives it after the fact. +// +// The value types live in this subpackage rather than in package workflow or +// internal/worktree, for two reasons that were checked rather than assumed. +// Package workflow must never import internal/worktree -- it has no such +// import today -- so a shared value cannot live there. And this subpackage is +// not projected into the interpreted-stage symbol table (the generated +// wfextract table names no type from here), whereas a new exported type in +// package workflow would be reachable from repository-authored +// `.archie/stages/*.go`. Declaring it here widens nothing and needs no +// symbol-table regeneration. +type ChangeStats struct { + BaseSHA string `json:"base_sha"` + HeadSHA string `json:"head_sha"` + Files []FileChange `json:"files"` + Totals ChangeTotals `json:"totals"` +} + +// FileChange is one file's contribution to a change. +type FileChange struct { + Path string `json:"path"` + // OldPath is the pre-change path, populated only for a rename. + OldPath string `json:"old_path"` + // Status is one of the Change* values below. + Status string `json:"status"` + Additions int `json:"additions"` + Deletions int `json:"deletions"` + // Binary reports that the file produced no textual hunks. It is NOT a + // claim that the file is binary on disk: an empty or otherwise chunkless + // edit reports this too, so it must never be rendered as a file type. + Binary bool `json:"binary"` +} + +// ChangeTotals summarises a change. Totals always cover the FULL set of +// changed files, even when FileChange entries were dropped by the +// MaxCapturedFiles cap, so a truncated capture still reports honest totals. +type ChangeTotals struct { + Files int `json:"files"` + Additions int `json:"additions"` + Deletions int `json:"deletions"` +} + +// Change status values as they are persisted inside a changes_captured +// event's data. Like the task lifecycle statuses, these strings are part of +// the on-disk format: renaming one is a migration, not a rename. +const ( + ChangeAdded = "added" + ChangeModified = "modified" + ChangeDeleted = "deleted" + ChangeRenamed = "renamed" + ChangeTypeChanged = "typechange" +) + +// MaxCapturedFiles caps how many file entries one capture persists. Unlike +// Event.Detail, an event's data is not length-limited by the store, so this cap +// is what keeps one large refactor from writing an unbounded payload. A capture +// that dropped entries keeps the totals of the full set and reports itself as +// truncated. +const MaxCapturedFiles = 200 diff --git a/internal/domain/workflow/task/changes_test.go b/internal/domain/workflow/task/changes_test.go new file mode 100644 index 000000000..29c01ac73 --- /dev/null +++ b/internal/domain/workflow/task/changes_test.go @@ -0,0 +1,29 @@ +package task + +import "testing" + +// TestChangeStatusesAreSpelledAsPersisted pins the five status strings as +// literals. They are the on-disk vocabulary of a changes_captured payload and +// the dashboard mirrors them as its own key set +// (ui/src/tasks/changed-files.jsx FILE_STATUS), so renaming one here without +// the matching dashboard change renders raw status codes instead of words -- +// with both suites still green unless each side pins the literal. +func TestChangeStatusesAreSpelledAsPersisted(t *testing.T) { + statuses := []struct { + name string + got string + want string + }{ + {name: "ChangeAdded", got: ChangeAdded, want: "added"}, + {name: "ChangeModified", got: ChangeModified, want: "modified"}, + {name: "ChangeDeleted", got: ChangeDeleted, want: "deleted"}, + {name: "ChangeRenamed", got: ChangeRenamed, want: "renamed"}, + {name: "ChangeTypeChanged", got: ChangeTypeChanged, want: "typechange"}, + } + + for _, s := range statuses { + if s.got != s.want { + t.Errorf("%s = %q, want %q", s.name, s.got, s.want) + } + } +} diff --git a/internal/domain/workflow/wfextract/reachability_test.go b/internal/domain/workflow/wfextract/reachability_test.go index 48993ec26..d6d03384d 100644 --- a/internal/domain/workflow/wfextract/reachability_test.go +++ b/internal/domain/workflow/wfextract/reachability_test.go @@ -2,6 +2,7 @@ package wfextract import ( "context" + "reflect" "testing" "github.com/traefik/yaegi/interp" @@ -109,3 +110,69 @@ func TestWrapperNilGuardsPreserved(t *testing.T) { t.Errorf("ChangedLines on nil WChangedLines = %v, want nil", err) } } + +// TestChangeCaptureIsNotCallableFromInterpretedCode is the acceptance test for +// "the sandbox surface did not widen", in the same shape as the reachability +// test above and with the opposite assertion. +// +// An attempt's changed files are captured through an UNEXPORTED optional +// interface asserted on TaskContext.Trees, precisely so that the capability is +// not part of workflow.Trees: Trees is projected into this symbol table, so a +// method added to it would become callable by every repository-authored +// .archie/stages/*.go file. Both halves are asserted here -- the interface's +// method set, and the interpreted call that must not resolve -- because the +// second alone would be weaker: it would still pass if the symbol table lost +// Trees altogether. +func TestChangeCaptureIsNotCallableFromInterpretedCode(t *testing.T) { + trees := reflect.TypeFor[workflow.Trees]() + if _, ok := trees.MethodByName("ChangedFileStats"); ok { + t.Fatalf("workflow.Trees gained ChangedFileStats; interpreted stage code can now read a diffstat the engine only meant to capture") + } + + // The control first: an interpreted call to a method Trees really has must + // resolve, so the refusal below is the missing method and not a broken + // harness. + const control = `package main + +import ( + "context" + + "github.com/samcharles93/archie-core/internal/domain/workflow" +) + +func UseTrees(t workflow.Trees) { + _, _ = t.Diff(context.Background(), "", "") +} +` + const probe = `package main + +import ( + "context" + + "github.com/samcharles93/archie-core/internal/domain/workflow" +) + +// CaptureDiffstat is an interpreted stage trying to reach the engine's +// change capture through the surface TaskContext hands it. +func CaptureDiffstat(t workflow.Trees) { + stats, err := t.ChangedFileStats(context.Background(), "", "") + _, _ = stats.HeadSHA, err +} +` + + i, err := yaegiutil.New(interp.Options{}, Symbols) + if err != nil { + t.Fatalf("New: %v", err) + } + if _, err := yaegiutil.Resolve[func(workflow.Trees)](i, control, "main.UseTrees"); err != nil { + t.Fatalf("the control call Trees.Diff did not resolve (%v); this test cannot tell a missing capability from a broken symbol table", err) + } + + j, err := yaegiutil.New(interp.Options{}, Symbols) + if err != nil { + t.Fatalf("New: %v", err) + } + if _, err := yaegiutil.Resolve[func(workflow.Trees)](j, probe, "main.CaptureDiffstat"); err == nil { + t.Error("interpreted stage code resolved Trees.ChangedFileStats; the capture capability leaked into the projected surface") + } +} diff --git a/internal/domain/workflow/workflow.go b/internal/domain/workflow/workflow.go index 3cff53eb8..5054ac1cc 100644 --- a/internal/domain/workflow/workflow.go +++ b/internal/domain/workflow/workflow.go @@ -134,7 +134,13 @@ type TaskContext struct { } // Emit publishes an observability event stamped with the task's -// identity. Safe on a nil bus. +// identity and the attempt that produced it. Safe on a nil bus. +// +// The attempt comes from the task record this context runs against, so a +// reader can attribute the event to one run without segmenting the task's +// stream by stage order. It is never guessed: a producer with no attempt +// (the deliberately task-agnostic daemon events) leaves it zero, which reads +// as unattributed rather than as a first run. func (tc *TaskContext) Emit(kind, stage, detail string, data map[string]any) { if tc.Bus == nil { return @@ -145,6 +151,7 @@ func (tc *TaskContext) Emit(kind, stage, detail string, data map[string]any) { Repo: tc.Task.Owner + "/" + tc.Task.Repo, Issue: tc.Task.IssueNumber, Workflow: tc.Task.Workflow, + Attempt: tc.Task.Attempt, Stage: stage, Detail: detail, Data: data, @@ -161,7 +168,8 @@ func (tc *TaskContext) EmitDurable(ctx context.Context, kind, stage, detail stri } event := events.Event{ At: time.Now().UTC(), Kind: kind, TaskID: tc.Task.ID, Repo: tc.Task.Owner + "/" + tc.Task.Repo, - Issue: tc.Task.IssueNumber, Workflow: tc.Task.Workflow, Stage: stage, Detail: detail, Data: data, + Issue: tc.Task.IssueNumber, Workflow: tc.Task.Workflow, Attempt: tc.Task.Attempt, + Stage: stage, Detail: detail, Data: data, } id, err := tc.Store.InsertEvent(ctx, event) if err != nil { @@ -293,7 +301,17 @@ func Run(ctx context.Context, wf Workflow, tc *TaskContext) { for _, stage := range wf.Stages { t.Stage = stage.Name _ = tc.Store.Update(ctx, t) - log.Info("stage starting", "stage", stage.Name) + // The stage is bound onto the task logger for exactly the stage's own + // execution and removed afterwards, so every line a stage's code writes + // is selectable by stage (internal/logging.Query.Stage). It is restored + // rather than left in place because a line written outside any stage is + // not attributable to one. Agent and tool output is logged by the runtime + // that produced it, which never sees this logger, so it carries no stage: + // the stage filter narrows the log, it does not cover it. + previousLog := tc.Log + stageLog := previousLog.With("stage", stage.Name) + tc.Log = stageLog + stageLog.Info("stage starting") tc.Emit(events.KindStageStart, stage.Name, "", nil) started := time.Now() @@ -306,13 +324,14 @@ func Run(ctx context.Context, wf Workflow, tc *TaskContext) { } } tc.Emit(events.KindStageFinish, stage.Name, "", data) + tc.Log = previousLog if err != nil { // Daemon shutdown is not a workflow failure. Leave the task running // so Startup's existing crash recovery requeues it; parking here // would publish a false failure and require manual intervention. if ctx.Err() != nil { - log.Info("stage interrupted", "stage", stage.Name, "err", err) + stageLog.Info("stage interrupted", "err", err) return } t.ParkReason = fmt.Sprintf("stage %s: %v", stage.Name, err) diff --git a/internal/events/events.go b/internal/events/events.go index c735892ab..959024a2d 100644 --- a/internal/events/events.go +++ b/internal/events/events.go @@ -50,6 +50,26 @@ const ( KindWorkRequestSubmitted = "work_request_submitted" KindLog = "log" // data: level, msg + // KindChangesCaptured records the change one attempt produced, read off + // the worktree at the moment it was committed or pushed, plus a final + // capture once OpenPR knows the number. It is durable + // provenance, not a live view: the worktree is deleted on merge, close + // and no-PR terminal states, and a retry resets the branch onto its base, + // so nothing can re-derive this after the fact. data: schema + // (ChangesCapturedSchema), owner, repo, base, branch, head_sha, base_sha, + // pr_number, captured_after, files[], totals, truncated. + KindChangesCaptured = "changes_captured" + + // KindConfigCaptured records the effective configuration one attempt ran + // under, captured where it is materialised for the dispatch. It is durable + // by requirement, not convenience: the published configuration snapshot is + // the CURRENT configuration and is replaced on every publish, so it cannot + // answer what a run that finished last week was configured with. This is + // the only record of that. data: schema (ConfigCapturedSchema) and document + // (the JSON encoding of the dispatch's config.TaskConfig, non-secret by + // construction and by test). + KindConfigCaptured = "config_captured" + // Curator family activity (epic archie-core-yp9). Curator runs ride // the same bus so background agents mutating memory stay observable: // what ran, when, what changed, and why (archie-core-114). @@ -79,20 +99,39 @@ const ( KindUpdateReport = "update_report" ) +// ConfigCapturedSchema is the schema name carried inside a config_captured +// event's data. It is named on the wire so a reader can refuse a document it +// does not understand rather than rendering it as though it did. +const ConfigCapturedSchema = "archie/task-config@1" + +// ChangesCapturedSchema is the schema name carried inside a changes_captured +// event's data, for the same reason as ConfigCapturedSchema: a capture read by +// a build that does not know its shape is a read failure, and the dashboard has +// a state for that distinct from a capture it rendered as zeroes. +const ChangesCapturedSchema = "archie/task-changes@1" + // Event is the single wire type: task lifecycle, stage progress, agent // stats, and log lines all flow through it -- every consumer (SQLite // sink, SSE fan-out, future aggregators) is just another subscriber. type Event struct { - ID int64 `json:"id,omitempty"` // set by the store sink - At time.Time `json:"at"` - Kind string `json:"kind"` - TaskID int64 `json:"task_id,omitempty"` - Repo string `json:"repo,omitempty"` - Issue int `json:"issue,omitempty"` - Workflow string `json:"workflow,omitempty"` - Stage string `json:"stage,omitempty"` - Detail string `json:"detail,omitempty"` - Data map[string]any `json:"data,omitempty"` + ID int64 `json:"id,omitempty"` // set by the store sink + At time.Time `json:"at"` + Kind string `json:"kind"` + TaskID int64 `json:"task_id,omitempty"` + Repo string `json:"repo,omitempty"` + Issue int `json:"issue,omitempty"` + Workflow string `json:"workflow,omitempty"` + Stage string `json:"stage,omitempty"` + // Attempt attributes the event to one run of the task. Zero means + // UNATTRIBUTED, not "attempt zero": every row written before this field + // existed, and the deliberately task-agnostic producers, carry 0. A + // reader must report those as unattributable rather than presenting them + // as a first run. The tag is deliberately not omitempty so that + // unattributed and absent stay distinguishable on the wire and in the + // array the dashboard renders. + Attempt int `json:"attempt"` + Detail string `json:"detail,omitempty"` + Data map[string]any `json:"data,omitempty"` } // Sub is one subscriber: a bounded channel plus a drop counter. diff --git a/internal/events/events_test.go b/internal/events/events_test.go index 1cc805327..00bd9daa6 100644 --- a/internal/events/events_test.go +++ b/internal/events/events_test.go @@ -199,3 +199,18 @@ func TestPublishDoesNotOverwritePreSetAt(t *testing.T) { t.Errorf("At = %v, want %v (preset should not be overwritten)", e.At, preset) } } + +// TestCapturedSchemasAreSpelledAsTheDashboardExpects pins the two schema names +// as literals. They are wire vocabulary: the dashboard mirrors them +// (ui/src/tasks/attempt-config.jsx, and api_tasks_changes.go for the changes +// one), so changing a name here without changing the reader turns every +// capture into "recorded but could not be read". The literal assertion is what +// makes that a failing test rather than a silent one. +func TestCapturedSchemasAreSpelledAsTheDashboardExpects(t *testing.T) { + if ConfigCapturedSchema != "archie/task-config@1" { + t.Errorf("ConfigCapturedSchema = %q, want archie/task-config@1", ConfigCapturedSchema) + } + if ChangesCapturedSchema != "archie/task-changes@1" { + t.Errorf("ChangesCapturedSchema = %q, want archie/task-changes@1", ChangesCapturedSchema) + } +} diff --git a/internal/infrastructure/staterpc/conformance_test.go b/internal/infrastructure/staterpc/conformance_test.go index 90d1a9f92..2325a0808 100644 --- a/internal/infrastructure/staterpc/conformance_test.go +++ b/internal/infrastructure/staterpc/conformance_test.go @@ -96,10 +96,26 @@ func TestStateStoreConformance(t *testing.T) { if err := c.Update(ctx, task); err != nil { t.Fatalf("Update: %v", err) } - eventID, err := c.InsertEvent(ctx, events.Event{Kind: "stage_finish", TaskID: task.ID, Data: map[string]any{"duration_ms": 12.0}}) + eventID, err := c.InsertEvent(ctx, events.Event{Kind: "stage_finish", TaskID: task.ID, Attempt: 2, Data: map[string]any{"duration_ms": 12.0}}) if err != nil || eventID == 0 { t.Fatalf("InsertEvent: %v %v", eventID, err) } + // R4's wire acceptance: the per-attempt configuration document IS an + // event, so it has to survive the same hop an event does, including + // its nested structure and the attempt key that attributes it to one + // run. A payload that arrives flattened reads as a config with no + // fields at all. The document rides under the daemon's own key + // (internal/daemon/daemon.go), asserted by name below. + _, err = c.InsertEvent(ctx, events.Event{ + Kind: events.KindConfigCaptured, TaskID: task.ID, Attempt: 2, + Data: map[string]any{ + "schema": events.ConfigCapturedSchema, + "document": map[string]any{"bot_user": "archie", "models": map[string]any{"implement": "anthropic/claude"}}, + }, + }) + if err != nil { + t.Fatalf("InsertEvent config_captured: %v", err) + } // Stale transition: from no longer matches current status. err = c.Transition(ctx, task.ID, "queued", "merged", "") @@ -131,9 +147,26 @@ func TestStateStoreConformance(t *testing.T) { } // TaskEvents / EventsSince / stats. - if evs, err := c.TaskEvents(ctx, task.ID); err != nil || len(evs) != 1 || evs[0].Data["duration_ms"] != 12.0 { + evs, err := c.TaskEvents(ctx, task.ID) + if err != nil || len(evs) != 2 || evs[0].Data["duration_ms"] != 12.0 { t.Fatalf("TaskEvents: %+v %v", evs, err) } + if evs[0].Attempt != 2 { + t.Errorf("TaskEvents attempt = %d, want 2 (provenance must survive the wire)", evs[0].Attempt) + } + if evs[1].Kind != events.KindConfigCaptured || evs[1].Attempt != 2 || evs[1].Data["schema"] != events.ConfigCapturedSchema { + t.Errorf("config_captured event = %+v, want its kind, attempt and schema preserved", evs[1]) + } + if _, renamed := evs[1].Data["config"]; renamed { + t.Error(`the document crossed under "config"; the producer writes it under "document"`) + } + if len(evs[1].Data) != 2 { + t.Errorf("config_captured data keys = %v, want exactly schema and document", evs[1].Data) + } + doc, ok := evs[1].Data["document"].(map[string]any) + if !ok || doc["bot_user"] != "archie" { + t.Errorf("config_captured payload = %#v, want the decoded document under its own key", evs[1].Data) + } if _, err := c.EventsSince(ctx, 0, 10); err != nil { t.Fatalf("EventsSince: %v", err) } diff --git a/internal/infrastructure/staterpc/task_grants_test.go b/internal/infrastructure/staterpc/task_grants_test.go index 8a4c9b174..c3eca39bc 100644 --- a/internal/infrastructure/staterpc/task_grants_test.go +++ b/internal/infrastructure/staterpc/task_grants_test.go @@ -167,6 +167,41 @@ func TestTaskGrantScopesWorkerToItsOwnThreeRPCs(t *testing.T) { if _, err := workerA.InsertEvent(ctx, events.Event{TaskID: taskA.ID, Kind: "log"}); err != nil { t.Fatalf("task grant should authorize InsertEvent on its own task: %v", err) } + // The documented residual of R4, pinned rather than hidden: a task grant + // may insert events on its OWN task -- that is how tool_call and agent_finish + // already work -- so a task can write its own config_captured provenance, + // and only its own. The permission is the existing InsertEvent permission, + // not a new surface, which is why the provenance is exactly as trustworthy + // as the rest of that task's event stream. + configEvent := events.Event{ + TaskID: taskA.ID, Kind: events.KindConfigCaptured, Attempt: 1, + Data: map[string]any{"schema": events.ConfigCapturedSchema, "document": map[string]any{"bot_user": "archie"}}, + } + if _, err := workerA.InsertEvent(ctx, configEvent); err != nil { + t.Fatalf("task grant should authorize its own config_captured event: %v", err) + } + configEvent.TaskID = taskB.ID + if _, err := workerA.InsertEvent(ctx, configEvent); err == nil { + t.Fatal("task grant must not authorize a config_captured event on another task") + } + // The document rides under the producer's own key (internal/daemon/daemon.go + // writes "document"), so the fixture above is read back and asserted by key + // rather than trusted: a rename would otherwise leave this test green. + stored, err := admin.TaskEvents(ctx, taskA.ID) + if err != nil { + t.Fatalf("TaskEvents: %v", err) + } + for _, e := range stored { + if e.Kind != events.KindConfigCaptured { + continue + } + if _, renamed := e.Data["config"]; renamed { + t.Error(`the document crossed under "config"; the producer writes it under "document"`) + } + if _, ok := e.Data["document"].(map[string]any); !ok { + t.Errorf("config_captured data = %#v, want the document under the producer's key", e.Data) + } + } if err := workerA.Update(ctx, &workflow.Task{ID: taskA.ID, Owner: "acme", Repo: "widget"}); err != nil { t.Fatalf("task grant should authorize Update on its own task: %v", err) } diff --git a/internal/infrastructure/staterpc/values.go b/internal/infrastructure/staterpc/values.go index 91bec136c..4e82be238 100644 --- a/internal/infrastructure/staterpc/values.go +++ b/internal/infrastructure/staterpc/values.go @@ -113,8 +113,8 @@ func eventDataValue(s string) map[string]any { func eventProto(e events.Event) *pb.Event { return &pb.Event{ Id: e.ID, At: timestamp(e.At), Kind: e.Kind, TaskId: e.TaskID, Repo: e.Repo, - Issue: int64(e.Issue), Workflow: e.Workflow, Stage: e.Stage, Detail: e.Detail, - DataJson: eventDataJSON(e.Data), + Issue: int64(e.Issue), Workflow: e.Workflow, Stage: e.Stage, Attempt: int64(e.Attempt), + Detail: e.Detail, DataJson: eventDataJSON(e.Data), } } @@ -124,8 +124,8 @@ func eventValue(e *pb.Event) events.Event { } return events.Event{ ID: e.Id, At: timeValue(e.At), Kind: e.Kind, TaskID: e.TaskId, Repo: e.Repo, - Issue: int(e.Issue), Workflow: e.Workflow, Stage: e.Stage, Detail: e.Detail, - Data: eventDataValue(e.DataJson), + Issue: int(e.Issue), Workflow: e.Workflow, Stage: e.Stage, Attempt: int(e.Attempt), + Detail: e.Detail, Data: eventDataValue(e.DataJson), } } @@ -426,7 +426,7 @@ func taskLogEntryValue(e *pb.TaskLogEntry) logging.Entry { 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, + Levels: q.Levels, Component: q.Component, Stage: q.Stage, Contains: q.Contains, Since: timestamp(q.Since), Until: timestamp(q.Until), } } @@ -435,6 +435,7 @@ func taskLogQueryValue(r *pb.ReadTaskLogRequest) logging.Query { return logging.Query{ Levels: r.Levels, Component: r.Component, + Stage: r.Stage, Contains: r.Contains, Limit: int(r.Limit), Since: timeValue(r.Since), diff --git a/internal/infrastructure/staterpc/values_test.go b/internal/infrastructure/staterpc/values_test.go index 8d06c201d..03875ca61 100644 --- a/internal/infrastructure/staterpc/values_test.go +++ b/internal/infrastructure/staterpc/values_test.go @@ -4,8 +4,95 @@ import ( "encoding/json" "fmt" "testing" + + "github.com/samcharles93/archie-core/internal/events" + "github.com/samcharles93/archie-core/internal/logging" ) +func TestEventAttemptSurvivesTheWire(t *testing.T) { + // attempt is provenance, so it must cross the boundary or the dashboard + // cannot attribute an event to a run. Zero must survive too: it is the + // unattributed marker, and a mapping that turned it into an absent field + // would make "unattributed" unrepresentable. + for _, attempt := range []int{0, 1, 7} { + t.Run(fmt.Sprintf("attempt=%d", attempt), func(t *testing.T) { + original := events.Event{ + Kind: events.KindStageFinish, TaskID: 42, Stage: "implement", + Attempt: attempt, Detail: "done", + Data: map[string]any{"duration_ms": float64(1200)}, + } + got := eventValue(eventProto(original)) + if got.Attempt != attempt { + t.Fatalf("attempt round-trip = %d, want %d", got.Attempt, attempt) + } + if got.Kind != original.Kind || got.TaskID != original.TaskID || got.Stage != original.Stage { + t.Fatalf("event round-trip = %+v, want %+v", got, original) + } + if got.Data["duration_ms"] != float64(1200) { + t.Fatalf("data round-trip = %v", got.Data) + } + }) + } +} + +func TestConfigCapturedPayloadSurvivesTheWire(t *testing.T) { + // R4's document is a nested JSON object inside the event's data, so the + // round trip has to keep its structure rather than flattening it to a + // string: the Config tab reads fields out of it. The key is the daemon's own + // -- internal/daemon/daemon.go writes the document under "document" -- and + // is asserted by name here so a rename cannot ride along unnoticed. + document := map[string]any{ + "schema": events.ConfigCapturedSchema, + "document": map[string]any{ + "bot_user": "archie", + "diff_cap_lines": float64(4000), + "models": map[string]any{"implement": "anthropic/claude"}, + }, + } + got := eventValue(eventProto(events.Event{ + Kind: events.KindConfigCaptured, TaskID: 42, Attempt: 2, Data: document, + })) + if got.Kind != events.KindConfigCaptured || got.Attempt != 2 { + t.Fatalf("config event = %+v", got) + } + if _, renamed := got.Data["config"]; renamed { + t.Error(`the document crossed under "config"; the producer writes it under "document"`) + } + if len(got.Data) != 2 { + t.Errorf("data keys = %v, want exactly schema and document", got.Data) + } + doc, ok := got.Data["document"].(map[string]any) + if !ok { + t.Fatalf("document payload = %#v, want a decoded object under the producer's own key", got.Data) + } + if doc["bot_user"] != "archie" { + t.Errorf("document.bot_user = %v, want archie", doc["bot_user"]) + } + models, ok := doc["models"].(map[string]any) + if !ok || models["implement"] != "anthropic/claude" { + t.Errorf("document.models = %#v, want the nested map preserved", doc["models"]) + } + if got.Data["schema"] != events.ConfigCapturedSchema { + t.Errorf("schema = %v, want %q", got.Data["schema"], events.ConfigCapturedSchema) + } +} + +func TestTaskLogStageFilterSurvivesTheWire(t *testing.T) { + // The stage filter is only useful if it reaches the reader, and the wire + // mirrors the whole logging.Query for exactly that reason: a filter that + // silently stops working at the boundary looks like "no matches". + query := logging.Query{ + Levels: []string{"ERROR"}, Component: "daemon", Stage: "implement", Contains: "parked", Limit: 50, + } + got := taskLogQueryValue(taskLogRequestProto(42, 3, query)) + if got.Stage != "implement" { + t.Fatalf("stage round-trip = %q, want implement", got.Stage) + } + if got.Component != "daemon" || got.Contains != "parked" || got.Limit != 50 { + t.Fatalf("query round-trip = %+v, want the whole filter preserved", got) + } +} + // TestEventDataJSONPreservesMarshalErrorMarker pins §11's conformance // invariant: the local store (internal/store/events.go) records a // {"marshal_error":%q} marker when json.Marshal of event Data fails, so the diff --git a/internal/infrastructure/taskactions/store.go b/internal/infrastructure/taskactions/store.go index 085e5cec5..9ebae24e7 100644 --- a/internal/infrastructure/taskactions/store.go +++ b/internal/infrastructure/taskactions/store.go @@ -16,7 +16,7 @@ func (s Store) TaskByID(ctx context.Context, id int64) (*taskactions.Task, error if err != nil || t == nil { return nil, err } - return &taskactions.Task{ID: t.ID, Owner: t.Owner, Repo: t.Repo, Identity: t.Identity, Status: t.Status, Stage: t.Stage, ParkReason: t.ParkReason, IssueNumber: t.IssueNumber, RetryCount: t.RetryCount, ForgeBacked: t.IsForgeBacked()}, nil + return &taskactions.Task{ID: t.ID, Owner: t.Owner, Repo: t.Repo, Identity: t.Identity, Status: t.Status, Stage: t.Stage, ParkReason: t.ParkReason, IssueNumber: t.IssueNumber, RetryCount: t.RetryCount, Attempt: t.Attempt, ForgeBacked: t.IsForgeBacked()}, nil } func MaxRetries(cfg *config.Holder) func(*taskactions.Task) int { diff --git a/internal/logging/reader.go b/internal/logging/reader.go index 5d6a6df6e..6c1c0303e 100644 --- a/internal/logging/reader.go +++ b/internal/logging/reader.go @@ -38,6 +38,15 @@ type Query struct { Levels []string // Component matches the "component" field exactly. Empty means any. Component string + // Stage matches the entry's own "stage" field exactly (case-insensitive). + // Empty means any. + // + // Only entries a stage tagged carry that field. Agent and tool output is + // logged by the runtime that produced it and carries no stage, so a stage + // filter NARROWS the file rather than covering it: the caller is obliged to + // say that, because an honest-looking filter over a partial field is worse + // than no filter. An entry with no stage never matches. + Stage string // Contains matches the message or any field value, case-insensitively. Contains string // Limit caps returned entries. Zero selects DefaultTailLines; values @@ -563,6 +572,12 @@ func (q Query) matches(e Entry) bool { return false } } + if q.Stage != "" { + got, _ := e.Fields["stage"].(string) + if !strings.EqualFold(got, q.Stage) { + return false + } + } if !q.withinTimeBounds(e.Time) { return false } diff --git a/internal/logging/reader_test.go b/internal/logging/reader_test.go index 8aec0e8e6..698cfdaea 100644 --- a/internal/logging/reader_test.go +++ b/internal/logging/reader_test.go @@ -29,6 +29,52 @@ func line(level, msg string, extra ...string) string { return s.String() + "}" } +func TestTailStageFilter(t *testing.T) { + path := writeLog( + t, + line("INFO", "stage starting", `"stage":"prepare"`), + line("INFO", "stage finished", `"stage":"prepare"`, `"component":"daemon"`), + line("INFO", "stage starting", `"stage":"implement"`), + line("INFO", "agent turn", `"component":"daemon"`), + line("INFO", "stage starting", `"stage":"PREPARE"`), + ) + + tests := []struct { + name string + query Query + want []string + }{ + {"stage selects only that stage's entries", Query{Stage: "prepare"}, []string{"stage starting", "stage finished", "stage starting"}}, + {"stage match is case-insensitive", Query{Stage: "Implement"}, []string{"stage starting"}}, + {"an entry with no stage never matches", Query{Stage: "prepare"}, []string{"stage starting", "stage finished", "stage starting"}}, + {"stage combines with level", Query{Stage: "prepare", Levels: []string{"INFO"}}, []string{"stage starting", "stage finished", "stage starting"}}, + {"stage combines with component", Query{Stage: "prepare", Component: "daemon"}, []string{"stage finished"}}, + {"a stage nothing carries is empty, not an error", Query{Stage: "nonexistent"}, nil}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + res, err := Tail(path, tc.query) + if err != nil { + t.Fatalf("Tail: %v", err) + } + got := make([]string, 0, len(res.Entries)) + for _, e := range res.Entries { + got = append(got, e.Message) + } + if len(got) != len(tc.want) { + t.Fatalf("got %v, want %v", got, tc.want) + } + for i := range got { + if got[i] != tc.want[i] { + t.Errorf("got %v, want %v", got, tc.want) + break + } + } + }) + } +} + func TestTailFilters(t *testing.T) { path := writeLog( t, diff --git a/internal/store/events.go b/internal/store/events.go index 94a734dde..aabca3ddf 100644 --- a/internal/store/events.go +++ b/internal/store/events.go @@ -22,6 +22,7 @@ CREATE TABLE IF NOT EXISTS events ( issue INTEGER NOT NULL DEFAULT 0, workflow TEXT NOT NULL DEFAULT '', stage TEXT NOT NULL DEFAULT '', + attempt INTEGER NOT NULL DEFAULT 0, detail TEXT NOT NULL DEFAULT '', data TEXT NOT NULL DEFAULT '{}' ); @@ -47,10 +48,10 @@ func insertEvent(ctx context.Context, execer eventExecer, e events.Event) (int64 data = fmt.Appendf(nil, `{"marshal_error":%q}`, err.Error()) } res, err := execer.ExecContext(ctx, ` - INSERT INTO events (at, kind, task_id, repo, issue, workflow, stage, detail, data) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, + INSERT INTO events (at, kind, task_id, repo, issue, workflow, stage, attempt, detail, data) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, e.At.UTC().Format(time.RFC3339Nano), e.Kind, e.TaskID, e.Repo, e.Issue, - e.Workflow, e.Stage, clip(e.Detail, 4000), string(data)) + e.Workflow, e.Stage, e.Attempt, clip(e.Detail, 4000), string(data)) if err != nil { return 0, err } @@ -65,7 +66,7 @@ func scanEvents(rows *sql.Rows) (out []events.Event, retErr error) { var e events.Event var at, data string if err := rows.Scan(&e.ID, &at, &e.Kind, &e.TaskID, &e.Repo, &e.Issue, - &e.Workflow, &e.Stage, &e.Detail, &data); err != nil { + &e.Workflow, &e.Stage, &e.Attempt, &e.Detail, &data); err != nil { return nil, err } e.At, _ = time.Parse(time.RFC3339Nano, at) @@ -75,7 +76,7 @@ func scanEvents(rows *sql.Rows) (out []events.Event, retErr error) { return out, rows.Err() } -const eventCols = "id, at, kind, task_id, repo, issue, workflow, stage, detail, data" +const eventCols = "id, at, kind, task_id, repo, issue, workflow, stage, attempt, detail, data" // EventsSince returns up to limit events with id > sinceID, oldest // first -- SSE catch-up and the live feed. diff --git a/internal/store/store.go b/internal/store/store.go index 82d4ad348..b88494cf2 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -95,6 +95,15 @@ func migrateTasks(ctx context.Context, db *sql.DB) error { if err != nil { return err } + // events is created by `CREATE TABLE IF NOT EXISTS` (eventsSchema), which + // is a no-op against a database that already has the table. On an existing + // archie.db this read plus the `events` arm below is therefore the ONLY + // thing that adds events.attempt; without it the first insert fails with + // `table events has no column named attempt`. + eventsColumns, err := tableColumns(ctx, tx, "events") + if err != nil { + return err + } migrations := []struct { table string @@ -109,11 +118,15 @@ func migrateTasks(ctx context.Context, db *sql.DB) error { {"tasks", "binding_version", `ALTER TABLE tasks ADD COLUMN binding_version INTEGER NOT NULL DEFAULT 0`}, {"bindings", "owner", `ALTER TABLE bindings ADD COLUMN owner TEXT NOT NULL DEFAULT ''`}, {"bindings", "repo", `ALTER TABLE bindings ADD COLUMN repo TEXT NOT NULL DEFAULT ''`}, + {"events", "attempt", `ALTER TABLE events ADD COLUMN attempt INTEGER NOT NULL DEFAULT 0`}, } for _, migration := range migrations { present := columns - if migration.table == "bindings" { + switch migration.table { + case "bindings": present = bindingColumns + case "events": + present = eventsColumns } if present[migration.column] { continue @@ -544,9 +557,14 @@ func (s *Store) RecoverStale(ctx context.Context) (int64, error) { } // OpenPRs returns tasks whose PR state should be reconciled with GitHub. +// +// It carries attempt deliberately: the reconcile loop attributes pr_merged and +// pr_rejected to the attempt that opened the PR, and it reaches the state store +// over the wire, so the row must carry the value for taskProto to publish it. +// Narrowing this projection back silently sends those events out unattributed. func (s *Store) OpenPRs(ctx context.Context) (tasks []workflow.Task, retErr error) { rows, err := s.db.QueryContext(ctx, ` - SELECT id, owner, repo, issue_number, pr_number, status, source, identity + SELECT id, owner, repo, issue_number, pr_number, status, source, identity, attempt FROM tasks WHERE status=?`, workflow.StatusPROpen) if err != nil { return nil, err @@ -557,7 +575,7 @@ func (s *Store) OpenPRs(ctx context.Context) (tasks []workflow.Task, retErr erro for rows.Next() { var t workflow.Task if err := rows.Scan(&t.ID, &t.Owner, &t.Repo, &t.IssueNumber, &t.PRNumber, - &t.Status, &t.Source, &t.Identity); err != nil { + &t.Status, &t.Source, &t.Identity, &t.Attempt); err != nil { return nil, err } tasks = append(tasks, t) diff --git a/internal/store/store_test.go b/internal/store/store_test.go index 697710e44..e6a93c96e 100644 --- a/internal/store/store_test.go +++ b/internal/store/store_test.go @@ -111,6 +111,85 @@ func TestOpenMigratesUnversionedTaskSchemas(t *testing.T) { } } +// TestOpenMigratesLegacyEventsAttemptColumn covers the highest-risk step in +// attempt provenance: adding events.attempt to a database that predates it. +// eventsSchema is CREATE TABLE IF NOT EXISTS, so against an existing +// archie.db that statement is a no-op and the migrator arm is the ONLY thing +// that adds the column. A test that opens a fresh tempdir passes whether or +// not the arm exists, so the failure this pins is the one production would +// hit: `table events has no column named attempt`. +func TestOpenMigratesLegacyEventsAttemptColumn(t *testing.T) { + path := filepath.Join(t.TempDir(), "legacy.db") + raw, err := sql.Open("sqlite", path) + if err != nil { + t.Fatal(err) + } + _, err = raw.ExecContext(t.Context(), ` + CREATE TABLE events ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + at TEXT NOT NULL, + kind TEXT NOT NULL, + task_id INTEGER NOT NULL DEFAULT 0, + repo TEXT NOT NULL DEFAULT '', + issue INTEGER NOT NULL DEFAULT 0, + workflow TEXT NOT NULL DEFAULT '', + stage TEXT NOT NULL DEFAULT '', + detail TEXT NOT NULL DEFAULT '', + data TEXT NOT NULL DEFAULT '{}' + ); + INSERT INTO events (at, kind, task_id, stage, detail) + VALUES ('2026-01-01T00:00:00Z', 'stage_start', 7, 'prepare', 'pre-existing row'); + PRAGMA user_version = 3`) + if err != nil { + t.Fatal(err) + } + if err := raw.Close(); err != nil { + t.Fatal(err) + } + + s, err := Open(t.Context(), path) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = s.Close() }) + + // The row written before the column existed survives and reads as + // unattributed -- zero is not a first attempt. + legacy, err := s.TaskEvents(t.Context(), 7) + if err != nil { + t.Fatalf("TaskEvents after legacy migration: %v", err) + } + if len(legacy) != 1 || legacy[0].Detail != "pre-existing row" { + t.Fatalf("legacy events = %+v, want the preserved row", legacy) + } + if legacy[0].Attempt != 0 { + t.Errorf("legacy event attempt = %d, want 0 (unattributed)", legacy[0].Attempt) + } + + // Writing the new column is what actually fails in production when the + // migrator arm is missing. + if _, err := s.InsertEvent(t.Context(), events.Event{ + Kind: "stage_finish", TaskID: 7, Stage: "prepare", Attempt: 2, + Data: map[string]any{"duration_ms": 1200}, + }); err != nil { + t.Fatalf("InsertEvent with attempt after legacy migration: %v", err) + } + + timeline, err := s.TaskEvents(t.Context(), 7) + if err != nil { + t.Fatalf("TaskEvents after insert: %v", err) + } + if len(timeline) != 2 { + t.Fatalf("TaskEvents = %d events, want 2", len(timeline)) + } + if got := timeline[1].Attempt; got != 2 { + t.Errorf("attempt round-trip after legacy migration = %d, want 2", got) + } + if got := timeline[0].Attempt; got != 0 { + t.Errorf("legacy row attempt changed to %d, want 0", got) + } +} + func TestOpenRejectsNewerSchemaVersion(t *testing.T) { path := filepath.Join(t.TempDir(), "tasks.db") db, err := sql.Open("sqlite", path) @@ -526,12 +605,12 @@ func TestEventLogRoundTrip(t *testing.T) { s := openTest(t) ctx := context.Background() - id1, err := s.InsertEvent(ctx, events.Event{Kind: "stage_start", TaskID: 1, Stage: "plan"}) + id1, err := s.InsertEvent(ctx, events.Event{Kind: "stage_start", TaskID: 1, Stage: "plan", Attempt: 1}) if err != nil || id1 == 0 { t.Fatalf("insert = (%d, %v)", id1, err) } id2, err := s.InsertEvent(ctx, events.Event{ - Kind: "stage_finish", TaskID: 1, Workflow: "implement", Stage: "plan", + Kind: "stage_finish", TaskID: 1, Workflow: "implement", Stage: "plan", Attempt: 2, Data: map[string]any{"duration_ms": 1200}, }) if err != nil || id2 <= id1 { @@ -545,11 +624,18 @@ func TestEventLogRoundTrip(t *testing.T) { if evs[0].Data["duration_ms"] != float64(1200) { t.Fatalf("data round-trip = %v", evs[0].Data) } + if evs[0].Attempt != 2 { + t.Fatalf("EventsSince attempt round-trip = %d, want 2", evs[0].Attempt) + } timeline, err := s.TaskEvents(ctx, 1) if err != nil || len(timeline) != 2 { t.Fatalf("TaskEvents = (%d, %v)", len(timeline), err) } + // Two attempts of one task must stay distinguishable in its timeline. + if timeline[0].Attempt != 1 || timeline[1].Attempt != 2 { + t.Fatalf("TaskEvents attempts = (%d, %d), want (1, 2)", timeline[0].Attempt, timeline[1].Attempt) + } stats, err := s.StageStats(ctx) if err != nil || len(stats) != 1 || stats[0].AvgMs != 1200 || stats[0].Stage != "plan" { diff --git a/internal/webui/api_tasks_attempts.go b/internal/webui/api_tasks_attempts.go new file mode 100644 index 000000000..1882307dd --- /dev/null +++ b/internal/webui/api_tasks_attempts.go @@ -0,0 +1,343 @@ +package webui + +import ( + "net/http" + "sort" + "strconv" + "strings" + "time" + + "github.com/samcharles93/archie-core/internal/domain/workflow/task" + "github.com/samcharles93/archie-core/internal/events" +) + +// Stage/attempt statuses the attempt rail reports. They are one vocabulary +// because the rail renders one status per stage and one for the attempt it +// belongs to; "unknown" exists only for an attempt whose events carry no stage +// information at all, so a rail never claims a verdict it did not observe. +const ( + attemptStatusOK = "ok" + attemptStatusFailed = "failed" + attemptStatusInterrupted = "interrupted" + attemptStatusRunning = "running" + attemptStatusUnknown = "unknown" +) + +// taskAttemptsView is GET /api/tasks/{id}/attempts: every attempt of one task +// with its stages inline, so the rail and the attempt selector cannot disagree +// and the page makes no per-attempt calls of its own. +type taskAttemptsView struct { + TaskID int64 `json:"task_id"` + // CurrentAttempt is the task's own attempt, echoed so the page can show + // which run the daemon is on even when that attempt has no events yet. + CurrentAttempt int `json:"current_attempt"` + // UnattributedEvents counts events whose attempt is 0. It exists so the + // page can say honestly that older activity cannot be attributed instead + // of presenting it as a first run. + UnattributedEvents int `json:"unattributed_events"` + Attempts []taskAttemptView `json:"attempts"` +} + +// taskAttemptView is one attempt's rail entry. The bounds and duration are +// measured from the attempt's own events: the span between its first and last +// recorded event, which therefore includes any waiting-for-a-human gap rather +// than understating wall time (design AMENDMENTS 0.1, F3). Every optional +// field is omitted when it is not known rather than sent as a zero. +type taskAttemptView struct { + Attempt int `json:"attempt"` + Status string `json:"status"` + StartedAt *time.Time `json:"started_at,omitempty"` + FinishedAt *time.Time `json:"finished_at,omitempty"` + DurationMS *int64 `json:"duration_ms,omitempty"` + EventCount int `json:"event_count"` + Stages []taskStageView `json:"stages"` +} + +// taskStageView is one stage occurrence within an attempt. Error is always +// present (empty when the stage recorded none) so a consumer can tell "no error +// text" from a field it did not receive. +type taskStageView struct { + Name string `json:"name"` + Seq int `json:"seq"` + Status string `json:"status"` + StartedAt *time.Time `json:"started_at,omitempty"` + DurationMS *int64 `json:"duration_ms,omitempty"` + Error string `json:"error"` +} + +// handleTaskAttempts serves the stage rail: one task's attempts, each with the +// stages it recorded. +func (s *Server) handleTaskAttempts(w http.ResponseWriter, r *http.Request) { + t, ok := s.taskByPathID(w, r) + if !ok { + return + } + evs, err := s.Store.TaskEvents(r.Context(), t.ID) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + attempts, unattributed := attemptsFromEvents(evs, t.Status, t.Attempt) + writeJSON(w, taskAttemptsView{ + TaskID: t.ID, + CurrentAttempt: t.Attempt, + UnattributedEvents: unattributed, + Attempts: attempts, + }) +} + +// taskByPathID resolves a task route's {id} to the task it names, answering the +// request itself and reporting false when it cannot. An unknown task is 404 +// plain text -- TaskByID says "no such row" the same way it says "here is the +// row", so the distinction is drawn here rather than leaked to the page. +func (s *Server) taskByPathID(w http.ResponseWriter, r *http.Request) (*task.Task, bool) { + id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) + if err != nil { + http.Error(w, "bad id", http.StatusBadRequest) + return nil, false + } + t, err := s.Store.TaskByID(r.Context(), id) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return nil, false + } + if t == nil { + http.Error(w, "task not found", http.StatusNotFound) + return nil, false + } + return t, true +} + +// taskAttemptTarget resolves a per-attempt read: the task its {id} names and the +// attempt it asks for. An absent or zero "attempt" parameter selects the task's +// current attempt, which is what a human means by "why did this park?" -- the +// most recent run, not an arbitrary earlier retry. Zero is not an attempt: it is +// the value that means "unattributed" on an event, so reading "attempt zero" +// would read a bucket rather than a run. +func (s *Server) taskAttemptTarget(w http.ResponseWriter, r *http.Request) (*task.Task, int, bool) { + t, ok := s.taskByPathID(w, r) + if !ok { + return nil, 0, false + } + attempt := t.Attempt + raw := strings.TrimSpace(r.URL.Query().Get("attempt")) + if raw == "" { + return t, attempt, true + } + parsed, err := strconv.Atoi(raw) + if err != nil { + http.Error(w, "bad attempt", http.StatusBadRequest) + return nil, 0, false + } + if parsed != 0 { + attempt = parsed + } + return t, attempt, true +} + +// attemptsFromEvents folds a task's timeline into its rail: one entry per +// attempt the events attribute to, ordered by attempt, plus the number of events +// that carry no attempt at all. +// +// Events with attempt 0 are counted and then dropped, never grouped. Zero means +// unattributed -- every row written before the column existed and every +// deliberately task-agnostic producer carries it -- so grouping them would +// present unrelated activity as attempt zero's rail. +func attemptsFromEvents(evs []events.Event, taskStatus string, currentAttempt int) ([]taskAttemptView, int) { + grouped := make(map[int][]events.Event) + numbers := make([]int, 0, 4) + unattributed := 0 + for _, e := range evs { + if e.Attempt == 0 { + unattributed++ + continue + } + if _, seen := grouped[e.Attempt]; !seen { + numbers = append(numbers, e.Attempt) + } + grouped[e.Attempt] = append(grouped[e.Attempt], e) + } + sort.Ints(numbers) + + attempts := make([]taskAttemptView, 0, len(numbers)) + for _, number := range numbers { + attempts = append(attempts, attemptRail(number, grouped[number], taskStatus, currentAttempt)) + } + return attempts, unattributed +} + +// attemptRail derives one attempt's status, bounds and stages from its events. +func attemptRail(number int, evs []events.Event, taskStatus string, currentAttempt int) taskAttemptView { + // The task's own record is the only honest source for "is this still + // happening": an unpaired start on any other attempt ended without + // finishing -- parked, retried, crashed or restarted -- so it is reported + // as interrupted rather than left looking permanently in flight. + inFlight := number == currentAttempt && taskStatus == task.StatusRunning + + first, last := attemptBounds(evs) + stages := foldStages(evs, inFlight) + attempt := taskAttemptView{ + Attempt: number, + Status: attemptStatus(stages, inFlight), + EventCount: len(evs), + Stages: stages, + } + if !first.IsZero() { + attempt.StartedAt = &first + } + // A finished_at is only claimed for an attempt that has an end: a running + // one has none yet, and an unknown one never had stage information to time. + if !last.IsZero() && attempt.Status != attemptStatusRunning && attempt.Status != attemptStatusUnknown { + attempt.FinishedAt = &last + if !first.IsZero() { + span := last.Sub(first).Milliseconds() + attempt.DurationMS = &span + } + } + return attempt +} + +// attemptBounds is the span of an attempt's own recorded events: the earliest +// and latest timestamp any of them carries. A zero time contributes nothing -- a +// record whose timestamp did not parse is not evidence of when it happened. +func attemptBounds(evs []events.Event) (first, last time.Time) { + for _, e := range evs { + if e.At.IsZero() { + continue + } + if first.IsZero() || e.At.Before(first) { + first = e.At + } + if last.IsZero() || e.At.After(last) { + last = e.At + } + } + return first, last +} + +// foldStages builds an attempt's ordered stage occurrences. A stage start is +// folded as an OPEN stage (status empty) until a matching finish closes it; +// resolving what is left needs the task's own record, so an open stage is +// running only while the daemon is executing this attempt and interrupted +// otherwise. +func foldStages(evs []events.Event, inFlight bool) []taskStageView { + stages := []taskStageView{} + for _, e := range evs { + switch e.Kind { + case events.KindStageStart: + started := e.At + stages = append(stages, taskStageView{Name: e.Stage, Seq: len(stages), StartedAt: &started}) + case events.KindStageFinish: + stages = finishStage(stages, e) + } + } + for i := range stages { + if stages[i].Status != "" { + continue + } + stages[i].Status = attemptStatusInterrupted + if inFlight { + stages[i].Status = attemptStatusRunning + } + } + return stages +} + +// attemptStatus reads one status off an attempt's stages. The task's own record +// outranks the events: an attempt the daemon is executing right now is running +// whatever its events have recorded so far -- including the ones that have +// recorded no stages at all. Otherwise no stage is left RUNNING to read: every +// stage still open when the daemon is not executing this attempt is folded as +// interrupted, which is what the next case reports. +func attemptStatus(stages []taskStageView, inFlight bool) string { + if inFlight { + return attemptStatusRunning + } + switch { + case anyStageStatus(stages, attemptStatusFailed): + return attemptStatusFailed + case anyStageStatus(stages, attemptStatusInterrupted): + return attemptStatusInterrupted + case len(stages) == 0: + // The attempt exists only because events attributed to it do; if none + // of them recorded a stage there is no verdict to report. + return attemptStatusUnknown + default: + return attemptStatusOK + } +} + +// finishStage applies one stage_finish event, closing the earliest still-open +// stage of the same name. A finish with no matching start still yields the +// stage -- with no start time rather than an invented one -- because the stage +// demonstrably ran and dropping it would hide a failure. +func finishStage(stages []taskStageView, e events.Event) []taskStageView { + status, errText := stageOutcome(e.Data) + recorded, hasRecorded := durationMillis(e.Data["duration_ms"]) + + idx := -1 + for i := range stages { + if stages[i].Name == e.Stage && stages[i].Status == "" { + idx = i + break + } + } + if idx < 0 { + stages = append(stages, taskStageView{Name: e.Stage, Seq: len(stages)}) + idx = len(stages) - 1 + } + stages[idx].Status = status + stages[idx].Error = errText + switch { + case hasRecorded: + // The producer's own measurement of the stage body, preferred over the + // gap between two event timestamps. + stages[idx].DurationMS = &recorded + case stages[idx].StartedAt != nil && !e.At.IsZero(): + span := e.At.Sub(*stages[idx].StartedAt).Milliseconds() + stages[idx].DurationMS = &span + } + return stages +} + +// stageOutcome reads a stage_finish event's own verdict. An interrupted finish +// is interrupted even though it also carries the cancellation error text; a +// non-empty error is a failure; anything else is ok. There is no exit code +// anywhere in this system, so no stage is ever badged as passing -- the status +// reports how the stage ended, not how well it went. +func stageOutcome(data map[string]any) (status, errText string) { + errText, _ = data["error"].(string) + if interrupted, _ := data["interrupted"].(bool); interrupted { + return attemptStatusInterrupted, errText + } + if errText != "" { + return attemptStatusFailed, errText + } + return attemptStatusOK, "" +} + +// durationMillis reads a millisecond duration a producer recorded in an event's +// data. A value that crossed JSON is float64 and one built in this process is +// an integer, so both spellings are accepted; anything else is reported as "not +// recorded" rather than as zero. +func durationMillis(v any) (int64, bool) { + switch n := v.(type) { + case int: + return int64(n), true + case int64: + return n, true + case float64: + return int64(n), true + default: + return 0, false + } +} + +func anyStageStatus(stages []taskStageView, status string) bool { + for _, s := range stages { + if s.Status == status { + return true + } + } + return false +} diff --git a/internal/webui/api_tasks_attempts_test.go b/internal/webui/api_tasks_attempts_test.go new file mode 100644 index 000000000..7e7aa168f --- /dev/null +++ b/internal/webui/api_tasks_attempts_test.go @@ -0,0 +1,536 @@ +package webui + +import ( + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "strconv" + "strings" + "testing" + "time" + + "github.com/samcharles93/archie-core/internal/domain/workflow" + "github.com/samcharles93/archie-core/internal/events" +) + +// railBase is the fixed clock the rail fixtures hang off, so every timestamp +// and duration asserted below is a literal rather than a tolerance. +var railBase = time.Date(2026, 9, 18, 7, 0, 0, 0, time.UTC) + +func stageStartEvent(taskID int64, attempt int, stage string, at time.Time) events.Event { + return events.Event{Kind: events.KindStageStart, TaskID: taskID, Attempt: attempt, Stage: stage, At: at} +} + +func stageFinishEvent(taskID int64, attempt int, stage string, at time.Time, data map[string]any) events.Event { + return events.Event{Kind: events.KindStageFinish, TaskID: taskID, Attempt: attempt, Stage: stage, At: at, Data: data} +} + +func insertTaskEvent(t *testing.T, srv *Server, ev events.Event) { + t.Helper() + if _, err := srv.Store.InsertEvent(t.Context(), ev); err != nil { + t.Fatalf("insert %s event: %v", ev.Kind, err) + } +} + +// getTaskAPI issues one task-detail request through the real mux. A route that +// is not registered falls through to the SPA asset handler and answers 200 with +// an HTML page, so using the mux makes a missing registration fail the shape +// assertions below rather than passing quietly. +func getTaskAPI(t *testing.T, srv *Server, url string) *httptest.ResponseRecorder { + t.Helper() + req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, url, nil) + w := httptest.NewRecorder() + srv.Handler().ServeHTTP(w, req) + return w +} + +func taskRoute(id int64, suffix string) string { + return "/api/tasks/" + strconv.FormatInt(id, 10) + suffix +} + +// twoAttemptTask drives one task through the lifecycle the rail has to read: +// claim (attempt 1), park, retry, claim (attempt 2), leaving the task running +// on its second attempt. No test writes an attempt number directly, so the +// attempt under test is the one the real lifecycle produced. +func twoAttemptTask(t *testing.T, srv *Server) *workflow.Task { + t.Helper() + ctx := t.Context() + if _, err := srv.Store.EnqueueIssue(ctx, "acme", "widget", 1, "task", "", "", ""); err != nil { + t.Fatal(err) + } + first, err := srv.Store.ClaimNext(ctx) + if err != nil { + t.Fatal(err) + } + if first == nil { + t.Fatal("ClaimNext returned no task") + } + if first.Attempt != 1 { + t.Fatalf("first claim attempt = %d, want 1", first.Attempt) + } + if err := srv.Store.Transition(ctx, first.ID, workflow.StatusRunning, workflow.StatusParked, "stage implement: builder exited 1"); err != nil { + t.Fatal(err) + } + if err := srv.Store.RetryTask(ctx, first.ID, workflow.StatusParked, ""); err != nil { + t.Fatal(err) + } + second, err := srv.Store.ClaimNext(ctx) + if err != nil { + t.Fatal(err) + } + if second == nil { + t.Fatal("second claim returned no task") + } + if second.Attempt != 2 || second.Status != workflow.StatusRunning { + t.Fatalf("second claim = attempt %d status %q, want attempt 2 running", second.Attempt, second.Status) + } + return second +} + +// summarizeAttemptRail renders a folded rail as one line per attempt so a table +// case states the derivation it expects without a struct literal per stage: +// "attempt:status[name:status:duration[#error][@nostart],...] duration". +func summarizeAttemptRail(attempts []taskAttemptView) string { + lines := make([]string, 0, len(attempts)) + for _, a := range attempts { + stages := make([]string, 0, len(a.Stages)) + for _, s := range a.Stages { + stage := s.Name + ":" + s.Status + ":" + durationToken(s.DurationMS) + if s.StartedAt == nil { + stage += "@nostart" + } + if s.Error != "" { + stage += "#" + s.Error + } + stages = append(stages, stage) + } + line := strconv.Itoa(a.Attempt) + ":" + a.Status + "[" + strings.Join(stages, ",") + "] " + durationToken(a.DurationMS) + lines = append(lines, line) + } + return strings.Join(lines, " | ") +} + +func durationToken(ms *int64) string { + if ms == nil { + return "?" + } + return strconv.FormatInt(*ms, 10) +} + +// TestAttemptsFromEventsDerivesEveryRailRule is the table behind R1: every +// status rule the attempt rail depends on, one case each, plus the cases that +// would otherwise be answered by inventing data (a finish with no start, an +// event set with no stage information, events carrying no attempt at all). +func TestAttemptsFromEventsDerivesEveryRailRule(t *testing.T) { + at := railBase.Add + duration := func(ms int64) map[string]any { return map[string]any{"duration_ms": ms} } + + tests := []struct { + name string + taskStatus string + currentAttempt int + evs []events.Event + want string + wantUnattributed int + }{ + { + name: "a finished stage is ok and the attempt reports its measured span", + taskStatus: workflow.StatusParked, currentAttempt: 1, + evs: []events.Event{ + stageStartEvent(1, 1, "prepare", at(0)), + stageFinishEvent(1, 1, "prepare", at(time.Second), duration(1000)), + }, + want: "1:ok[prepare:ok:1000] 1000", + }, + { + name: "a failed stage fails the attempt and carries the error text", + taskStatus: workflow.StatusParked, currentAttempt: 1, + evs: []events.Event{ + stageStartEvent(1, 1, "implement", at(0)), + stageFinishEvent(1, 1, "implement", at(2*time.Second), map[string]any{ + "duration_ms": int64(2000), + "error": "stage implement: builder exited 1", + }), + }, + want: "1:failed[implement:failed:2000#stage implement: builder exited 1] 2000", + }, + { + name: "an interrupted stage reads interrupted even though the error text is recorded", + taskStatus: workflow.StatusParked, currentAttempt: 1, + evs: []events.Event{ + stageStartEvent(1, 1, "implement", at(0)), + stageFinishEvent(1, 1, "implement", at(time.Second), map[string]any{ + "duration_ms": int64(1000), + "error": "context canceled", + "interrupted": true, + }), + }, + want: "1:interrupted[implement:interrupted:1000#context canceled] 1000", + }, + { + name: "a failed stage outranks an interrupted one", + taskStatus: workflow.StatusParked, currentAttempt: 1, + evs: []events.Event{ + stageStartEvent(1, 1, "a", at(0)), + stageFinishEvent(1, 1, "a", at(time.Second), map[string]any{"duration_ms": int64(1000), "error": "boom"}), + stageStartEvent(1, 1, "b", at(2*time.Second)), + stageFinishEvent(1, 1, "b", at(3*time.Second), map[string]any{ + "duration_ms": int64(1000), "error": "canceled", "interrupted": true, + }), + }, + want: "1:failed[a:failed:1000#boom,b:interrupted:1000#canceled] 3000", + }, + { + name: "an unpaired start on the task's current in-flight attempt is running", + taskStatus: workflow.StatusRunning, currentAttempt: 2, + evs: []events.Event{stageStartEvent(1, 2, "implement", at(0))}, + want: "2:running[implement:running:?] ?", + }, + { + name: "an unpaired start on a superseded attempt is interrupted", + taskStatus: workflow.StatusRunning, currentAttempt: 2, + evs: []events.Event{stageStartEvent(1, 1, "implement", at(0))}, + want: "1:interrupted[implement:interrupted:?] 0", + }, + { + name: "an unpaired start on a parked task's own attempt is interrupted", + taskStatus: workflow.StatusParked, currentAttempt: 1, + evs: []events.Event{stageStartEvent(1, 1, "implement", at(0))}, + want: "1:interrupted[implement:interrupted:?] 0", + }, + { + name: "a finish with no start yields the stage without an invented start time", + taskStatus: workflow.StatusParked, currentAttempt: 1, + evs: []events.Event{ + stageFinishEvent(1, 1, "commit", at(0), duration(4000)), + }, + want: "1:ok[commit:ok:4000@nostart] 0", + }, + { + name: "a finish without a recorded duration derives it from its own start", + taskStatus: workflow.StatusParked, currentAttempt: 1, + evs: []events.Event{ + stageStartEvent(1, 1, "prepare", at(0)), + stageFinishEvent(1, 1, "prepare", at(10*time.Second), nil), + }, + want: "1:ok[prepare:ok:10000] 10000", + }, + { + name: "a repeated stage name yields one rail entry per occurrence", + taskStatus: workflow.StatusRunning, currentAttempt: 1, + evs: []events.Event{ + stageStartEvent(1, 1, "implement", at(0)), + stageFinishEvent(1, 1, "implement", at(time.Second), duration(1000)), + stageStartEvent(1, 1, "implement", at(2*time.Second)), + }, + want: "1:running[implement:ok:1000,implement:running:?] ?", + }, + { + name: "a later attempt does not absorb the earlier attempt's stages", + taskStatus: workflow.StatusRunning, currentAttempt: 2, + evs: []events.Event{ + stageStartEvent(1, 1, "prepare", at(0)), + stageFinishEvent(1, 1, "prepare", at(time.Second), duration(1000)), + stageStartEvent(1, 2, "implement", at(2*time.Second)), + }, + want: "1:ok[prepare:ok:1000] 1000 | 2:running[implement:running:?] ?", + }, + { + name: "events carrying no attempt are counted, never grouped", + taskStatus: workflow.StatusQueued, currentAttempt: 0, + evs: []events.Event{ + {Kind: events.KindStageStart, TaskID: 1, Stage: "prepare", At: at(0)}, + {Kind: events.KindStageFinish, TaskID: 1, Stage: "prepare", At: at(time.Second), Data: duration(1000)}, + }, + wantUnattributed: 2, + }, + { + name: "a task with no events has no attempts and nothing unattributed", + taskStatus: workflow.StatusQueued, currentAttempt: 0, + want: "", + }, + { + name: "an attempt whose events carry no stage information is unknown", + taskStatus: workflow.StatusParked, currentAttempt: 1, + evs: []events.Event{ + {Kind: events.KindAgentFinish, TaskID: 1, Attempt: 1, At: at(0), Data: map[string]any{"status": "ok"}}, + }, + want: "1:unknown[] ?", + }, + { + name: "the task's own record keeps the current attempt running with no stage events", + taskStatus: workflow.StatusRunning, currentAttempt: 1, + evs: []events.Event{ + {Kind: events.KindAgentFinish, TaskID: 1, Attempt: 1, At: at(0), Data: map[string]any{"status": "ok"}}, + }, + want: "1:running[] ?", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + attempts, unattributed := attemptsFromEvents(tt.evs, tt.taskStatus, tt.currentAttempt) + if got := summarizeAttemptRail(attempts); got != tt.want { + t.Errorf("rail = %q, want %q", got, tt.want) + } + if unattributed != tt.wantUnattributed { + t.Errorf("unattributed = %d, want %d", unattributed, tt.wantUnattributed) + } + }) + } +} + +// TestHandleTaskAttemptsRendersBothAttemptsOfARetry pins the wire shape: an +// attempt's ordered stages with name, start time, duration, terminal status and +// error text, the task's current attempt, and attempt 1's rail excluding +// attempt 2's stages. +func TestHandleTaskAttemptsRendersBothAttemptsOfARetry(t *testing.T) { + srv := newTestServer(t) + current := twoAttemptTask(t, srv) + at := railBase.Add + + insertTaskEvent(t, srv, stageStartEvent(current.ID, 1, "prepare", at(0))) + insertTaskEvent(t, srv, stageFinishEvent(current.ID, 1, "prepare", at(1200*time.Millisecond), + map[string]any{"duration_ms": int64(1200)})) + insertTaskEvent(t, srv, stageStartEvent(current.ID, 1, "implement", at(1323*time.Millisecond))) + insertTaskEvent(t, srv, stageFinishEvent(current.ID, 1, "implement", at(231323*time.Millisecond), + map[string]any{"duration_ms": int64(230000), "error": "stage implement: builder exited 1"})) + insertTaskEvent(t, srv, stageStartEvent(current.ID, 2, "implement", at(time.Hour))) + + w := getTaskAPI(t, srv, taskRoute(current.ID, "/attempts")) + if w.Code != http.StatusOK { + t.Fatalf("status = %d, body %s", w.Code, w.Body) + } + if raw := w.Body.String(); !strings.Contains(raw, `"error":""`) { + t.Errorf("body = %s, want the ok stage's error field present and empty rather than omitted", raw) + } + + var body struct { + TaskID int64 `json:"task_id"` + CurrentAttempt int `json:"current_attempt"` + UnattributedEvents int `json:"unattributed_events"` + Attempts []struct { + Attempt int `json:"attempt"` + Status string `json:"status"` + StartedAt *time.Time `json:"started_at"` + FinishedAt *time.Time `json:"finished_at"` + DurationMS *int64 `json:"duration_ms"` + EventCount int `json:"event_count"` + Stages []struct { + Name string `json:"name"` + Seq int `json:"seq"` + Status string `json:"status"` + StartedAt *time.Time `json:"started_at"` + DurationMS *int64 `json:"duration_ms"` + Error string `json:"error"` + } `json:"stages"` + } `json:"attempts"` + } + if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil { + t.Fatalf("decode response: %v (%s)", err, w.Body) + } + if body.TaskID != current.ID || body.CurrentAttempt != 2 || body.UnattributedEvents != 0 { + t.Fatalf("envelope = task %d, current attempt %d, unattributed %d; want %d, 2, 0", + body.TaskID, body.CurrentAttempt, body.UnattributedEvents, current.ID) + } + if len(body.Attempts) != 2 { + t.Fatalf("attempts = %d, want 2 (%s)", len(body.Attempts), w.Body) + } + + first := body.Attempts[0] + if first.Attempt != 1 || first.Status != "failed" { + t.Errorf("attempt 1 = %d/%q, want 1/failed", first.Attempt, first.Status) + } + if first.EventCount != 4 { + t.Errorf("attempt 1 event_count = %d, want 4", first.EventCount) + } + if first.StartedAt == nil || !first.StartedAt.Equal(at(0)) { + t.Errorf("attempt 1 started_at = %v, want %v", first.StartedAt, at(0)) + } + if first.FinishedAt == nil || !first.FinishedAt.Equal(at(231323*time.Millisecond)) { + t.Errorf("attempt 1 finished_at = %v, want %v", first.FinishedAt, at(231323*time.Millisecond)) + } + if first.DurationMS == nil || *first.DurationMS != 231323 { + t.Errorf("attempt 1 duration_ms = %v, want 231323", first.DurationMS) + } + if len(first.Stages) != 2 { + t.Fatalf("attempt 1 stages = %d, want 2 (%s)", len(first.Stages), w.Body) + } + prepare := first.Stages[0] + if prepare.Name != "prepare" || prepare.Seq != 0 || prepare.Status != "ok" || prepare.Error != "" { + t.Errorf("stage 0 = %+v, want prepare/0/ok with no error", prepare) + } + if prepare.DurationMS == nil || *prepare.DurationMS != 1200 { + t.Errorf("stage 0 duration_ms = %v, want 1200", prepare.DurationMS) + } + if prepare.StartedAt == nil || !prepare.StartedAt.Equal(at(0)) { + t.Errorf("stage 0 started_at = %v, want %v", prepare.StartedAt, at(0)) + } + implement := first.Stages[1] + if implement.Name != "implement" || implement.Seq != 1 || implement.Status != "failed" { + t.Errorf("stage 1 = %+v, want implement/1/failed", implement) + } + if implement.Error != "stage implement: builder exited 1" { + t.Errorf("stage 1 error = %q, want the recorded error text", implement.Error) + } + if implement.DurationMS == nil || *implement.DurationMS != 230000 { + t.Errorf("stage 1 duration_ms = %v, want 230000", implement.DurationMS) + } + + second := body.Attempts[1] + if second.Attempt != 2 || second.Status != "running" || second.EventCount != 1 { + t.Errorf("attempt 2 = %d/%q with %d events, want 2/running with 1", second.Attempt, second.Status, second.EventCount) + } + if second.FinishedAt != nil || second.DurationMS != nil { + t.Errorf("attempt 2 finished_at = %v, duration_ms = %v; both must be omitted for a running attempt", + second.FinishedAt, second.DurationMS) + } + if len(second.Stages) != 1 || second.Stages[0].Name != "implement" || second.Stages[0].Status != "running" { + t.Fatalf("attempt 2 stages = %+v, want one running implement", second.Stages) + } + if second.Stages[0].Error != "" { + t.Errorf("attempt 2 stage error = %q, want empty", second.Stages[0].Error) + } +} + +// TestHandleTaskAttemptsSeparatesUnattributedEventsFromAFirstRun pins the +// honesty requirement: events that carry no attempt cannot be presented as +// attempt 1, and a task with nothing recorded at all is a different answer from +// a task whose events predate attribution. +func TestHandleTaskAttemptsSeparatesUnattributedEventsFromAFirstRun(t *testing.T) { + t.Run("a task with no events at all", func(t *testing.T) { + srv := newTestServer(t) + if _, err := srv.Store.EnqueueIssue(t.Context(), "acme", "widget", 1, "task", "", "", ""); err != nil { + t.Fatal(err) + } + task, err := srv.Store.TaskByIssue(t.Context(), "acme", "widget", 1) + if err != nil { + t.Fatal(err) + } + + w := getTaskAPI(t, srv, taskRoute(task.ID, "/attempts")) + if w.Code != http.StatusOK { + t.Fatalf("status = %d, body %s", w.Code, w.Body) + } + raw := w.Body.String() + if !strings.Contains(raw, `"attempts":[]`) { + t.Errorf("body = %s, want an empty attempts array rather than null", raw) + } + if !strings.Contains(raw, `"unattributed_events":0`) { + t.Errorf("body = %s, want unattributed_events 0 for a task with nothing recorded", raw) + } + }) + + t.Run("events that carry no attempt", func(t *testing.T) { + srv := newTestServer(t) + if _, err := srv.Store.EnqueueIssue(t.Context(), "acme", "widget", 1, "task", "", "", ""); err != nil { + t.Fatal(err) + } + task, err := srv.Store.TaskByIssue(t.Context(), "acme", "widget", 1) + if err != nil { + t.Fatal(err) + } + insertTaskEvent(t, srv, stageStartEvent(task.ID, 0, "prepare", railBase)) + insertTaskEvent(t, srv, stageFinishEvent(task.ID, 0, "prepare", railBase.Add(time.Second), + map[string]any{"duration_ms": int64(1000)})) + + w := getTaskAPI(t, srv, taskRoute(task.ID, "/attempts")) + if w.Code != http.StatusOK { + t.Fatalf("status = %d, body %s", w.Code, w.Body) + } + var body struct { + UnattributedEvents int `json:"unattributed_events"` + Attempts []struct { + Attempt int `json:"attempt"` + } `json:"attempts"` + } + if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil { + t.Fatalf("decode response: %v (%s)", err, w.Body) + } + if body.UnattributedEvents != 2 { + t.Errorf("unattributed_events = %d, want 2", body.UnattributedEvents) + } + if len(body.Attempts) != 0 { + t.Errorf("attempts = %+v, want none: events carrying no attempt must never be presented as a run", body.Attempts) + } + }) +} + +// TestTaskDetailRoutesRejectUnknownTasksAndStoreFailures keeps the three new +// reads on the existing task-route conventions: an unknown task is 404 plain +// text (never 200 with an empty body), a store failure is 500, and a +// non-numeric id is 400. +func TestTaskDetailRoutesRejectUnknownTasksAndStoreFailures(t *testing.T) { + base := newTestServer(t) + if _, err := base.Store.EnqueueIssue(t.Context(), "acme", "widget", 1, "task", "", "", ""); err != nil { + t.Fatal(err) + } + existing, err := base.Store.TaskByIssue(t.Context(), "acme", "widget", 1) + if err != nil { + t.Fatal(err) + } + + eventsBroken := &Server{ + Store: &stubStore{TaskStore: base.Store, taskEventsErr: fmt.Errorf("db locked")}, + Log: base.Log, + } + lookupBroken := &Server{ + Store: &stubStore{TaskStore: base.Store, taskByIDErr: fmt.Errorf("db locked")}, + Log: base.Log, + } + + tests := []struct { + name string + srv *Server + id int64 + suffix string + wantStatus int + }{ + {"attempts unknown task", base, 999, "/attempts", http.StatusNotFound}, + {"changes unknown task", base, 999, "/changes", http.StatusNotFound}, + {"debug unknown task", base, 999, "/debug", http.StatusNotFound}, + {"attempts events failure", eventsBroken, existing.ID, "/attempts", http.StatusInternalServerError}, + {"changes events failure", eventsBroken, existing.ID, "/changes", http.StatusInternalServerError}, + {"debug events failure", eventsBroken, existing.ID, "/debug", http.StatusInternalServerError}, + {"attempts lookup failure", lookupBroken, existing.ID, "/attempts", http.StatusInternalServerError}, + {"changes lookup failure", lookupBroken, existing.ID, "/changes", http.StatusInternalServerError}, + {"debug lookup failure", lookupBroken, existing.ID, "/debug", http.StatusInternalServerError}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + w := getTaskAPI(t, tt.srv, taskRoute(tt.id, tt.suffix)) + if w.Code != tt.wantStatus { + t.Errorf("GET %s = %d (%s), want %d", tt.suffix, w.Code, w.Body, tt.wantStatus) + } + }) + } + + for _, suffix := range []string{"/attempts", "/changes", "/debug"} { + t.Run("non-numeric id "+suffix, func(t *testing.T) { + w := getTaskAPI(t, base, "/api/tasks/not-a-number"+suffix) + if w.Code != http.StatusBadRequest { + t.Errorf("GET %s = %d (%s), want 400", suffix, w.Code, w.Body) + } + }) + } + + // The attempt parameter belongs to the per-attempt reads: /attempts returns + // every attempt of the task, so it does not consume the parameter at all, + // while /changes and /debug resolve it and must refuse a value that is not + // an attempt number rather than silently read the current one. + t.Run("non-numeric attempt on /attempts is not a parameter of that read", func(t *testing.T) { + w := getTaskAPI(t, base, taskRoute(existing.ID, "/attempts?attempt=later")) + if w.Code != http.StatusOK { + t.Errorf("GET /attempts?attempt=later = %d (%s), want 200", w.Code, w.Body) + } + }) + for _, suffix := range []string{"/changes", "/debug"} { + t.Run("non-numeric attempt "+suffix, func(t *testing.T) { + w := getTaskAPI(t, base, taskRoute(existing.ID, suffix+"?attempt=later")) + if w.Code != http.StatusBadRequest { + t.Errorf("GET %s?attempt=later = %d (%s), want 400", suffix, w.Code, w.Body) + } + }) + } +} diff --git a/internal/webui/api_tasks_changes.go b/internal/webui/api_tasks_changes.go new file mode 100644 index 000000000..95e2fb32a --- /dev/null +++ b/internal/webui/api_tasks_changes.go @@ -0,0 +1,201 @@ +package webui + +import ( + "encoding/json" + "net/http" + "reflect" + "strings" + "time" + + "github.com/samcharles93/archie-core/internal/domain/workflow/task" + "github.com/samcharles93/archie-core/internal/events" +) + +// taskChangesView is GET /api/tasks/{id}/changes: the change captures recorded +// for one attempt. Found reports that a capture was recorded at all -- an empty +// captures list without it would tell an operator that nothing changed, which is +// not a claim this system can make about a run whose diffstat was never read. +type taskChangesView struct { + TaskID int64 `json:"task_id"` + Attempt int `json:"attempt"` + Found bool `json:"found"` + Captures []changeCaptureView `json:"captures"` +} + +// changeCaptureView is one capture: the diffstat measured at the moment the run +// was committed or pushed, plus the repository and pull-request links the page +// renders beside it. The URLs are filled from the same forge projection the task +// list uses, so a capture links exactly where its task does. +type changeCaptureView struct { + CapturedAt time.Time `json:"captured_at"` + CapturedAfter string `json:"captured_after"` + Stage string `json:"stage"` + Owner string `json:"owner"` + Repo string `json:"repo"` + Base string `json:"base"` + Branch string `json:"branch"` + HeadSHA string `json:"head_sha"` + BaseSHA string `json:"base_sha"` + PRNumber int `json:"pr_number"` + RepoURL string `json:"repo_url,omitempty"` + PRURL string `json:"pr_url,omitempty"` + Totals task.ChangeTotals `json:"totals"` + Truncated bool `json:"truncated"` + Files []task.FileChange `json:"files"` +} + +// capturePayload is one changes_captured event's data as the producer persists +// it. The measured change itself reuses the shared task.ChangeStats identity the +// producer writes with, so the two sides cannot drift into two spellings of one +// diffstat. +type capturePayload struct { + Schema string `json:"schema"` + Owner string `json:"owner"` + Repo string `json:"repo"` + Base string `json:"base"` + Branch string `json:"branch"` + PRNumber int `json:"pr_number"` + CapturedAfter string `json:"captured_after"` + Truncated bool `json:"truncated"` + task.ChangeStats +} + +// capturePayloadKeys is the exact key set one capture carries, derived from +// capturePayload's own json tags rather than restated beside them: the reader +// and the key set cannot disagree. A payload that renamed, dropped or added a +// key is refused, because reading it leniently is how a lost truncation marker +// becomes "not truncated" and a dropped file list becomes "nothing changed". +var capturePayloadKeys = structKeys(reflect.TypeFor[capturePayload]()) + +// structKeys collects the JSON key names a struct declares, recursing into the +// embedded value structs a decoder flattens. +func structKeys(t reflect.Type) map[string]struct{} { + keys := make(map[string]struct{}, t.NumField()) + for field := range t.Fields() { + if field.Anonymous { + for key := range structKeys(field.Type) { + keys[key] = struct{}{} + } + continue + } + if name, _, _ := strings.Cut(field.Tag.Get("json"), ","); name != "" && name != "-" { + keys[name] = struct{}{} + } + } + return keys +} + +// handleTaskChanges serves the changed files one attempt produced. +func (s *Server) handleTaskChanges(w http.ResponseWriter, r *http.Request) { + t, attempt, ok := s.taskAttemptTarget(w, r) + if !ok { + return + } + evs, err := s.Store.TaskEvents(r.Context(), t.ID) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + captures, recorded := capturedChanges(evs, attempt) + + // The links come from the task's own forge resolution, reused rather than + // re-derived: the capture's own owner/repo/pr decide the URL, so a capture + // taken before the PR existed has no pull-request link instead of one + // pointing at a later attempt's PR. + forge := s.resolveForge(r.Context()) + for i := range captures { + target := task.Task{ + Owner: captures[i].Owner, Repo: captures[i].Repo, + PRNumber: captures[i].PRNumber, Identity: t.Identity, + } + captures[i].RepoURL, _, captures[i].PRURL = taskURLs(target, forge(target)) + } + + writeJSON(w, taskChangesView{TaskID: t.ID, Attempt: attempt, Found: recorded, Captures: captures}) +} + +// capturedChanges folds a task's events into the captures recorded for one +// attempt, oldest first (events are read in insert order, which is capture +// order). +// +// recorded reports that the attempt has at least one capture event even when its +// payload could not be read: a capture that happened is provenance whether or not +// this read can decode it, and answering "no capture was recorded" would be a +// false statement about it. A payload that is absent, in a schema this build does +// not read, or otherwise malformed is skipped rather than failing the read. +func capturedChanges(evs []events.Event, attempt int) (captures []changeCaptureView, recorded bool) { + captures = []changeCaptureView{} + for _, e := range evs { + if e.Kind != events.KindChangesCaptured || e.Attempt != attempt { + continue + } + recorded = true + capture, ok := decodeCapture(e) + if !ok { + continue + } + captures = append(captures, capture) + } + return captures, recorded +} + +// decodeCapture reads one capture event. It reports false when the event carries +// no data, when its payload is not in the schema this build reads, when its key +// set is not exactly the one this reader declares, or when the payload is +// malformed. The totals are passed through as recorded and never recomputed from +// the file entries, because the producer's totals cover the full change set even +// when the entries were capped. +func decodeCapture(e events.Event) (changeCaptureView, bool) { + if len(e.Data) == 0 { + return changeCaptureView{}, false + } + raw, err := json.Marshal(e.Data) + if err != nil { + return changeCaptureView{}, false + } + var keys map[string]json.RawMessage + if err := json.Unmarshal(raw, &keys); err != nil { + return changeCaptureView{}, false + } + if len(keys) != len(capturePayloadKeys) { + return changeCaptureView{}, false + } + for key := range capturePayloadKeys { + if _, ok := keys[key]; !ok { + return changeCaptureView{}, false + } + } + var payload capturePayload + if err := json.Unmarshal(raw, &payload); err != nil { + return changeCaptureView{}, false + } + if payload.Schema != events.ChangesCapturedSchema { + return changeCaptureView{}, false + } + files := payload.Files + if len(files) > task.MaxCapturedFiles { + // The producer bounds the list at task.MaxCapturedFiles and reports the + // drop with truncated. More entries than the shared cap means the cap + // was lost, and rendering them would present the loss as a complete + // capture. + return changeCaptureView{}, false + } + if files == nil { + files = []task.FileChange{} + } + return changeCaptureView{ + CapturedAt: e.At, + CapturedAfter: payload.CapturedAfter, + Stage: e.Stage, + Owner: payload.Owner, + Repo: payload.Repo, + Base: payload.Base, + Branch: payload.Branch, + HeadSHA: payload.HeadSHA, + BaseSHA: payload.BaseSHA, + PRNumber: payload.PRNumber, + Totals: payload.Totals, + Truncated: payload.Truncated, + Files: files, + }, true +} diff --git a/internal/webui/api_tasks_changes_test.go b/internal/webui/api_tasks_changes_test.go new file mode 100644 index 000000000..b109635f7 --- /dev/null +++ b/internal/webui/api_tasks_changes_test.go @@ -0,0 +1,460 @@ +package webui + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "strings" + "testing" + "time" + + "github.com/samcharles93/archie-core/internal/domain/workflow/task" + "github.com/samcharles93/archie-core/internal/events" +) + +// captureData is the shape the producer persists for one capture: the diffstat +// measured at the moment a run was committed or pushed. The reader's fixtures +// are built here; nothing in this file re-derives a capture. +func captureData(files []any, totals map[string]any) map[string]any { + return map[string]any{ + "schema": events.ChangesCapturedSchema, + "owner": "acme", "repo": "widget", "base": "main", "branch": "feat/2-x", + "head_sha": "head-sha", "base_sha": "base-sha", "pr_number": 0, + "captured_after": "commit-push", + "files": files, "totals": totals, "truncated": false, + } +} + +func modifiedFile(path string, additions, deletions int) map[string]any { + return map[string]any{ + "path": path, "old_path": "", "status": "modified", + "additions": additions, "deletions": deletions, "binary": false, + } +} + +func insertCapture(t *testing.T, srv *Server, taskID int64, attempt int, stage string, data map[string]any) { + t.Helper() + insertTaskEvent(t, srv, events.Event{ + Kind: events.KindChangesCaptured, TaskID: taskID, Attempt: attempt, Stage: stage, + At: railBase.Add(time.Duration(attempt) * time.Minute), Data: data, + }) +} + +// changesResponse is the endpoint's body as the page consumes it, decoded by +// field name so the contract is asserted rather than substring-matched. +type changesResponse struct { + TaskID int64 `json:"task_id"` + Attempt int `json:"attempt"` + Found bool `json:"found"` + Captures []captureViewJSON `json:"captures"` +} + +type captureViewJSON struct { + CapturedAt time.Time `json:"captured_at"` + CapturedAfter string `json:"captured_after"` + Stage string `json:"stage"` + Owner string `json:"owner"` + Repo string `json:"repo"` + Base string `json:"base"` + Branch string `json:"branch"` + HeadSHA string `json:"head_sha"` + BaseSHA string `json:"base_sha"` + PRNumber int `json:"pr_number"` + RepoURL string `json:"repo_url"` + PRURL string `json:"pr_url"` + Totals changeTotalsJSON `json:"totals"` + Truncated bool `json:"truncated"` + Files []fileChangeJSON `json:"files"` +} + +type changeTotalsJSON struct { + Files int `json:"files"` + Additions int `json:"additions"` + Deletions int `json:"deletions"` +} + +type fileChangeJSON struct { + Path string `json:"path"` + OldPath string `json:"old_path"` + Status string `json:"status"` + Additions int `json:"additions"` + Deletions int `json:"deletions"` + Binary bool `json:"binary"` +} + +func decodeChanges(t *testing.T, body []byte) changesResponse { + t.Helper() + var decoded changesResponse + if err := json.Unmarshal(body, &decoded); err != nil { + t.Fatalf("decode response: %v (%s)", err, body) + } + return decoded +} + +// TestHandleTaskChangesKeepsFoundFalseApartFromAnEmptyFileList pins the +// distinction the changed-files view depends on: "no capture was recorded for +// this attempt" is not "no files changed". +func TestHandleTaskChangesKeepsFoundFalseApartFromAnEmptyFileList(t *testing.T) { + srv := newTestServer(t) + current := twoAttemptTask(t, srv) + insertCapture(t, srv, current.ID, 2, "commit", + captureData([]any{}, map[string]any{"files": 0, "additions": 0, "deletions": 0})) + + recorded := getTaskAPI(t, srv, taskRoute(current.ID, "/changes?attempt=2")) + if recorded.Code != http.StatusOK { + t.Fatalf("status = %d, body %s", recorded.Code, recorded.Body) + } + if raw := recorded.Body.String(); !strings.Contains(raw, `"files":[]`) { + t.Errorf("body = %s, want an empty files array rather than null", raw) + } + body := decodeChanges(t, recorded.Body.Bytes()) + if !body.Found || body.Attempt != 2 { + t.Fatalf("found = %v, attempt = %d; want true, 2", body.Found, body.Attempt) + } + if len(body.Captures) != 1 { + t.Fatalf("captures = %d, want 1 (%s)", len(body.Captures), recorded.Body) + } + if len(body.Captures[0].Files) != 0 { + t.Errorf("files = %+v, want none: a capture with an empty diff is still a capture", body.Captures[0].Files) + } + if body.Captures[0].Totals.Files != 0 { + t.Errorf("totals.files = %d, want 0", body.Captures[0].Totals.Files) + } + + unrecorded := getTaskAPI(t, srv, taskRoute(current.ID, "/changes?attempt=1")) + if unrecorded.Code != http.StatusOK { + t.Fatalf("status = %d, body %s", unrecorded.Code, unrecorded.Body) + } + if raw := unrecorded.Body.String(); !strings.Contains(raw, `"captures":[]`) { + t.Errorf("body = %s, want an empty captures array rather than null", raw) + } + if got := decodeChanges(t, unrecorded.Body.Bytes()); got.Found || len(got.Captures) != 0 || got.Attempt != 1 { + t.Errorf("attempt 1 = found %v with %d captures at attempt %d, want found false, none, attempt 1", + got.Found, len(got.Captures), got.Attempt) + } +} + +// TestHandleTaskChangesResolvesTheCurrentAttemptAndCarriesTheRecordedFields +// pins the wire shape field by field, and that an absent attempt parameter +// selects the task's current attempt. +func TestHandleTaskChangesResolvesTheCurrentAttemptAndCarriesTheRecordedFields(t *testing.T) { + srv := newTestServer(t) + current := twoAttemptTask(t, srv) + data := captureData([]any{modifiedFile("internal/x.go", 12, 3)}, + map[string]any{"files": 1, "additions": 12, "deletions": 3}) + data["pr_number"] = 7 + insertCapture(t, srv, current.ID, 2, "commit-push", data) + + w := getTaskAPI(t, srv, taskRoute(current.ID, "/changes")) + if w.Code != http.StatusOK { + t.Fatalf("status = %d, body %s", w.Code, w.Body) + } + body := decodeChanges(t, w.Body.Bytes()) + if body.TaskID != current.ID || body.Attempt != 2 || !body.Found { + t.Fatalf("envelope = task %d attempt %d found %v; want %d, 2, true", + body.TaskID, body.Attempt, body.Found, current.ID) + } + if len(body.Captures) != 1 { + t.Fatalf("captures = %d, want 1 (%s)", len(body.Captures), w.Body) + } + capture := body.Captures[0] + if !capture.CapturedAt.Equal(railBase.Add(2 * time.Minute)) { + t.Errorf("captured_at = %v, want the capture event's own time", capture.CapturedAt) + } + if capture.CapturedAfter != "commit-push" || capture.Stage != "commit-push" { + t.Errorf("captured_after = %q, stage = %q; want commit-push, commit-push", capture.CapturedAfter, capture.Stage) + } + if capture.Owner != "acme" || capture.Repo != "widget" || capture.Base != "main" || capture.Branch != "feat/2-x" { + t.Errorf("repository fields = %q/%q base %q branch %q", capture.Owner, capture.Repo, capture.Base, capture.Branch) + } + if capture.HeadSHA != "head-sha" || capture.BaseSHA != "base-sha" || capture.PRNumber != 7 { + t.Errorf("shas = %q/%q pr %d, want head-sha/base-sha/7", capture.HeadSHA, capture.BaseSHA, capture.PRNumber) + } + if capture.Totals.Files != 1 || capture.Totals.Additions != 12 || capture.Totals.Deletions != 3 { + t.Errorf("totals = %+v, want 1/12/3", capture.Totals) + } + if capture.Truncated { + t.Error("truncated = true for a capture that was not truncated") + } + if len(capture.Files) != 1 { + t.Fatalf("files = %+v, want one", capture.Files) + } + file := capture.Files[0] + if file.Path != "internal/x.go" || file.Status != "modified" || file.Additions != 12 || file.Deletions != 3 || file.Binary { + t.Errorf("file = %+v, want internal/x.go modified +12 -3, not binary", file) + } + if file.OldPath != "" { + t.Errorf("old_path = %q, want empty for a modify", file.OldPath) + } +} + +// TestHandleTaskChangesKeepsTheFullTotalsOfATruncatedCapture pins that totals +// are the producer's measurement of the whole change set, not a re-count of the +// file entries this read happens to carry. +func TestHandleTaskChangesKeepsTheFullTotalsOfATruncatedCapture(t *testing.T) { + srv := newTestServer(t) + current := twoAttemptTask(t, srv) + data := captureData([]any{modifiedFile("internal/x.go", 1, 1)}, + map[string]any{"files": 250, "additions": 900, "deletions": 10}) + data["truncated"] = true + insertCapture(t, srv, current.ID, 2, "commit", data) + + w := getTaskAPI(t, srv, taskRoute(current.ID, "/changes?attempt=2")) + if w.Code != http.StatusOK { + t.Fatalf("status = %d, body %s", w.Code, w.Body) + } + captures := decodeChanges(t, w.Body.Bytes()).Captures + if len(captures) != 1 { + t.Fatalf("captures = %d, want 1 (%s)", len(captures), w.Body) + } + capture := captures[0] + if !capture.Truncated { + t.Error("truncated = false for a capture that dropped file entries") + } + if len(capture.Files) != 1 { + t.Fatalf("files = %d, want the single entry the producer capped the list to", len(capture.Files)) + } + if capture.Totals.Files != 250 || capture.Totals.Additions != 900 || capture.Totals.Deletions != 10 { + t.Errorf("totals = %+v, want the full 250/900/10 rather than a count of the 1 file carried", capture.Totals) + } +} + +// TestHandleTaskChangesRefusesACaptureItCannotNameSchema pins the schema +// marker's job: a payload whose shape this build does not know is not a +// capture it may render. Rendering it would show a diffstat nobody read, so +// the attempt reports that a capture was recorded and none was readable -- +// the panel's read-failure state -- rather than a capture of zeroes. +func TestHandleTaskChangesRefusesACaptureItCannotNameSchema(t *testing.T) { + tests := []struct { + name string + mutate func(map[string]any) + }{ + { + name: "no schema at all", + mutate: func(data map[string]any) { delete(data, "schema") }, + }, + { + name: "a schema this build does not know", + mutate: func(data map[string]any) { data["schema"] = "archie/task-changes@2" }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + srv := newTestServer(t) + current := twoAttemptTask(t, srv) + data := captureData([]any{modifiedFile("internal/x.go", 12, 3)}, + map[string]any{"files": 1, "additions": 12, "deletions": 3}) + tt.mutate(data) + insertCapture(t, srv, current.ID, 2, "commit", data) + + w := getTaskAPI(t, srv, taskRoute(current.ID, "/changes?attempt=2")) + if w.Code != http.StatusOK { + t.Fatalf("status = %d, body %s", w.Code, w.Body) + } + body := decodeChanges(t, w.Body.Bytes()) + if !body.Found { + t.Error("found = false although a capture event was recorded for the attempt") + } + if len(body.Captures) != 0 { + t.Errorf("captures = %+v, want none: a payload this build cannot name must not be rendered as a capture", + body.Captures) + } + }) + } +} + +// TestHandleTaskChangesRefusesAPayloadWhoseKeySetDrifted pins the other half +// of the schema guard: the schema names the shape, the exact key set proves +// the shape arrived. A key renamed, dropped or added means the payload is not +// the one this reader knows, and reading it leniently is how a lost +// truncation marker becomes "not truncated" and a moved cap becomes "every +// file, none dropped". +func TestHandleTaskChangesRefusesAPayloadWhoseKeySetDrifted(t *testing.T) { + tests := []struct { + name string + mutate func(map[string]any) + }{ + { + name: "the truncation marker renamed", + mutate: func(data map[string]any) { + delete(data, "truncated") + data["truncated_files"] = true + }, + }, + { + name: "the file list dropped", + mutate: func(data map[string]any) { delete(data, "files") }, + }, + { + name: "a key this reader does not know", + mutate: func(data map[string]any) { data["schema_version"] = 1 }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + srv := newTestServer(t) + current := twoAttemptTask(t, srv) + data := captureData([]any{modifiedFile("internal/x.go", 1, 0)}, + map[string]any{"files": task.MaxCapturedFiles + 1, "additions": 1, "deletions": 0}) + tt.mutate(data) + insertCapture(t, srv, current.ID, 2, "commit", data) + + w := getTaskAPI(t, srv, taskRoute(current.ID, "/changes?attempt=2")) + if w.Code != http.StatusOK { + t.Fatalf("status = %d, body %s", w.Code, w.Body) + } + body := decodeChanges(t, w.Body.Bytes()) + if !body.Found { + t.Error("found = false although a capture event was recorded for the attempt") + } + if len(body.Captures) != 0 { + t.Errorf("captures = %+v, want none: a payload whose keys drifted must not be read as zero values", + body.Captures) + } + }) + } +} + +// TestHandleTaskChangesLinksThePullRequestOfTheFinalCapture is the reader half +// of the requirement the commit-time captures cannot meet: their pr_number is +// necessarily 0, and the capture OpenPR takes once the number exists is what +// gives the panel a pull-request link. +func TestHandleTaskChangesLinksThePullRequestOfTheFinalCapture(t *testing.T) { + srv := newTestServer(t) + srv.ConfigSource = func(_ context.Context) (ConfigView, bool, error) { + return multiIdentityView(), true, nil + } + current := twoAttemptTask(t, srv) + + beforePR := captureData([]any{modifiedFile("internal/x.go", 1, 0)}, + map[string]any{"files": 1, "additions": 1, "deletions": 0}) + beforePR["captured_after"] = "commit-push" + insertCapture(t, srv, current.ID, 2, "commit-push", beforePR) + + withPR := captureData([]any{modifiedFile("internal/x.go", 1, 0)}, + map[string]any{"files": 1, "additions": 1, "deletions": 0}) + withPR["captured_after"] = "open-pr" + withPR["pr_number"] = 7 + insertCapture(t, srv, current.ID, 2, "open-pr", withPR) + + w := getTaskAPI(t, srv, taskRoute(current.ID, "/changes?attempt=2")) + if w.Code != http.StatusOK { + t.Fatalf("status = %d, body %s", w.Code, w.Body) + } + captures := decodeChanges(t, w.Body.Bytes()).Captures + if len(captures) != 2 { + t.Fatalf("captures = %d, want both the push and the open-pr capture (%s)", len(captures), w.Body) + } + if captures[0].PRNumber != 0 || captures[0].PRURL != "" { + t.Errorf("the push capture = pr %d url %q, want no PR: it was taken before one existed", + captures[0].PRNumber, captures[0].PRURL) + } + if captures[1].CapturedAfter != "open-pr" || captures[1].PRNumber != 7 { + t.Fatalf("the final capture = %q pr %d, want open-pr, 7", captures[1].CapturedAfter, captures[1].PRNumber) + } + if captures[1].PRURL != "https://gitea.example/acme/widget/pulls/7" { + t.Errorf("pr_url = %q, want the pull request the capture names", captures[1].PRURL) + } +} + +// TestHandleTaskChangesRefusesACaptureThatLostItsFileCap pins the cap from the +// reading side: `files` is bounded at task.MaxCapturedFiles by the producer, +// and a payload carrying more than that is one whose cap was lost. Rendering +// it would advertise the loss as a complete capture of every file. +func TestHandleTaskChangesRefusesACaptureThatLostItsFileCap(t *testing.T) { + srv := newTestServer(t) + current := twoAttemptTask(t, srv) + files := make([]any, 0, task.MaxCapturedFiles+1) + for i := range task.MaxCapturedFiles + 1 { + files = append(files, modifiedFile(fmt.Sprintf("file-%03d.go", i), 1, 0)) + } + insertCapture(t, srv, current.ID, 2, "commit", + captureData(files, map[string]any{"files": len(files), "additions": len(files), "deletions": 0})) + + w := getTaskAPI(t, srv, taskRoute(current.ID, "/changes?attempt=2")) + if w.Code != http.StatusOK { + t.Fatalf("status = %d, body %s", w.Code, w.Body) + } + body := decodeChanges(t, w.Body.Bytes()) + if !body.Found { + t.Error("found = false although a capture event was recorded for the attempt") + } + if len(body.Captures) != 0 { + t.Errorf("captures = %d entries, want none: a payload over the cap is one whose cap was lost", + len(body.Captures)) + } +} + +// TestHandleTaskChangesSkipsCapturesWithNoReadablePayload pins the degrade +// rule: a capture whose data is absent or malformed is skipped rather than +// failing the read, and the attempt still reports that a capture was recorded. +func TestHandleTaskChangesSkipsCapturesWithNoReadablePayload(t *testing.T) { + srv := newTestServer(t) + current := twoAttemptTask(t, srv) + + insertTaskEvent(t, srv, events.Event{ + Kind: events.KindChangesCaptured, TaskID: current.ID, Attempt: 2, Stage: "commit", + At: railBase, Data: map[string]any{"files": "not-an-array"}, + }) + insertTaskEvent(t, srv, events.Event{ + Kind: events.KindChangesCaptured, TaskID: current.ID, Attempt: 2, Stage: "commit", + At: railBase.Add(time.Second), + }) + insertCapture(t, srv, current.ID, 2, "commit", + captureData([]any{modifiedFile("internal/kept.go", 1, 0)}, + map[string]any{"files": 1, "additions": 1, "deletions": 0})) + + w := getTaskAPI(t, srv, taskRoute(current.ID, "/changes?attempt=2")) + if w.Code != http.StatusOK { + t.Fatalf("status = %d, body %s", w.Code, w.Body) + } + body := decodeChanges(t, w.Body.Bytes()) + if !body.Found { + t.Error("found = false although a capture event was recorded for the attempt") + } + if len(body.Captures) != 1 { + t.Fatalf("captures = %+v, want only the readable one", body.Captures) + } + if got := body.Captures[0].Files[0].Path; got != "internal/kept.go" { + t.Errorf("kept file = %q, want internal/kept.go", got) + } +} + +// TestHandleTaskChangesLinksTheCaptureRepositoryAndPullRequest reuses the task +// list's own forge projection: the page needs a repository link and, once a PR +// exists, a pull-request link, and it must get them from the same source the +// task list uses rather than a second one. +func TestHandleTaskChangesLinksTheCaptureRepositoryAndPullRequest(t *testing.T) { + srv := newTestServer(t) + srv.ConfigSource = func(_ context.Context) (ConfigView, bool, error) { + return multiIdentityView(), true, nil + } + current := twoAttemptTask(t, srv) + + for _, prNumber := range []int{0, 7} { + data := captureData([]any{modifiedFile("internal/x.go", 1, 0)}, + map[string]any{"files": 1, "additions": 1, "deletions": 0}) + data["pr_number"] = prNumber + insertCapture(t, srv, current.ID, 2, "commit", data) + + w := getTaskAPI(t, srv, taskRoute(current.ID, "/changes?attempt=2")) + if w.Code != http.StatusOK { + t.Fatalf("pr %d: status = %d, body %s", prNumber, w.Code, w.Body) + } + captures := decodeChanges(t, w.Body.Bytes()).Captures + capture := captures[len(captures)-1] + if capture.RepoURL != "https://gitea.example/acme/widget" { + t.Errorf("pr %d: repo_url = %q, want the task's gitea repository", prNumber, capture.RepoURL) + } + wantPR := "" + if prNumber > 0 { + wantPR = "https://gitea.example/acme/widget/pulls/7" + } + if capture.PRURL != wantPR { + t.Errorf("pr %d: pr_url = %q, want %q", prNumber, capture.PRURL, wantPR) + } + } +} diff --git a/internal/webui/api_tasks_debug.go b/internal/webui/api_tasks_debug.go new file mode 100644 index 000000000..30dc08b22 --- /dev/null +++ b/internal/webui/api_tasks_debug.go @@ -0,0 +1,45 @@ +package webui + +import ( + "net/http" + + "github.com/samcharles93/archie-core/internal/domain/workflow/task" + "github.com/samcharles93/archie-core/internal/events" +) + +// taskDebugView is GET /api/tasks/{id}/debug: the stored task record as it is, +// and every event recorded for the task. It is deliberately not a generic +// store-dump route -- the task list is capped at 100 rows, so a page that wanted +// to show an older task's raw provenance had no row to join against. Nothing +// here is projected, filtered or re-spelled: reading it is the point. +// +// Events are UNFILTERED. A debug view that silently hid the events of other +// attempts would be worse than useless; each event carries its own attempt, so +// the operator sees everything and can attribute it. Attempt reports only which +// attempt the page had selected. No log payload is included: the log tab already +// serves it, and embedding it would duplicate a large body for no new +// information. +type taskDebugView struct { + TaskID int64 `json:"task_id"` + Attempt int `json:"attempt"` + Task task.Task `json:"task"` + Events []events.Event `json:"events"` +} + +// handleTaskDebug serves the task's raw record together with its whole event +// list, for whichever attempt the request names. +func (s *Server) handleTaskDebug(w http.ResponseWriter, r *http.Request) { + t, attempt, ok := s.taskAttemptTarget(w, r) + if !ok { + return + } + evs, err := s.Store.TaskEvents(r.Context(), t.ID) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + if evs == nil { + evs = []events.Event{} + } + writeJSON(w, taskDebugView{TaskID: t.ID, Attempt: attempt, Task: *t, Events: evs}) +} diff --git a/internal/webui/api_tasks_debug_test.go b/internal/webui/api_tasks_debug_test.go new file mode 100644 index 000000000..cf448b4fa --- /dev/null +++ b/internal/webui/api_tasks_debug_test.go @@ -0,0 +1,98 @@ +package webui + +import ( + "encoding/json" + "net/http" + "strings" + "testing" + "time" + + "github.com/samcharles93/archie-core/internal/domain/workflow" + "github.com/samcharles93/archie-core/internal/events" +) + +// TestHandleTaskDebugReturnsTheWholeRecordAndEveryEvent pins R7's read: the +// stored task record verbatim, and EVERY event of the task rather than only the +// selected attempt's -- each event carries its own attempt, so a filtered list +// would hide the very provenance the view exists to show. +func TestHandleTaskDebugReturnsTheWholeRecordAndEveryEvent(t *testing.T) { + srv := newTestServer(t) + current := twoAttemptTask(t, srv) + + insertTaskEvent(t, srv, events.Event{ + Kind: events.KindStageStart, TaskID: current.ID, Attempt: 1, Stage: "prepare", At: railBase, + }) + insertTaskEvent(t, srv, events.Event{ + Kind: events.KindAgentFinish, TaskID: current.ID, Attempt: 0, At: railBase.Add(time.Second), + Data: map[string]any{"status": "ok"}, + }) + insertTaskEvent(t, srv, events.Event{ + Kind: events.KindStageStart, TaskID: current.ID, Attempt: 2, Stage: "implement", + At: railBase.Add(2 * time.Second), + }) + + w := getTaskAPI(t, srv, taskRoute(current.ID, "/debug?attempt=1")) + if w.Code != http.StatusOK { + t.Fatalf("status = %d, body %s", w.Code, w.Body) + } + raw := w.Body.String() + if strings.Contains(raw, `"entries"`) { + t.Errorf("body carries a log payload: %s", raw) + } + + var body struct { + TaskID int64 `json:"task_id"` + Attempt int `json:"attempt"` + Task workflow.Task `json:"task"` + Events []struct { + Kind string `json:"kind"` + Attempt int `json:"attempt"` + } `json:"events"` + } + if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil { + t.Fatalf("decode response: %v (%s)", err, w.Body) + } + if body.TaskID != current.ID || body.Attempt != 1 { + t.Errorf("envelope = task %d attempt %d, want task %d attempt 1 (the selected attempt)", + body.TaskID, body.Attempt, current.ID) + } + if body.Task.ID != current.ID || body.Task.Status != workflow.StatusRunning || body.Task.Attempt != 2 { + t.Errorf("task record = id %d status %q attempt %d, want the stored record (%d, running, 2)", + body.Task.ID, body.Task.Status, body.Task.Attempt, current.ID) + } + if len(body.Events) != 3 { + t.Fatalf("events = %d, want all 3 including the one recorded against a different attempt (%s)", + len(body.Events), w.Body) + } + wantAttempts := []int{1, 0, 2} + for i, want := range wantAttempts { + if body.Events[i].Attempt != want { + t.Errorf("event %d attempt = %d, want %d (order and attribution preserved)", i, body.Events[i].Attempt, want) + } + } + if !strings.Contains(raw, `"attempt":0`) { + t.Errorf("body = %s, want attempt carried on every event so an unattributed one stays distinguishable", raw) + } +} + +// TestHandleTaskDebugResolvesTheCurrentAttemptWithoutAParameter pins that the +// envelope reports the attempt the page selected, defaulting to the task's +// current attempt exactly as the log read does. +func TestHandleTaskDebugResolvesTheCurrentAttemptWithoutAParameter(t *testing.T) { + srv := newTestServer(t) + current := twoAttemptTask(t, srv) + + w := getTaskAPI(t, srv, taskRoute(current.ID, "/debug")) + if w.Code != http.StatusOK { + t.Fatalf("status = %d, body %s", w.Code, w.Body) + } + var body struct { + Attempt int `json:"attempt"` + } + if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil { + t.Fatalf("decode response: %v (%s)", err, w.Body) + } + if body.Attempt != current.Attempt { + t.Errorf("attempt = %d, want the task's current attempt %d", body.Attempt, current.Attempt) + } +} diff --git a/internal/webui/api_tasks_logs.go b/internal/webui/api_tasks_logs.go index 1e9c9a29d..260e73972 100644 --- a/internal/webui/api_tasks_logs.go +++ b/internal/webui/api_tasks_logs.go @@ -48,8 +48,12 @@ func (s *Server) handleTaskLogs(w http.ResponseWriter, r *http.Request) { 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, + // Stage is a narrowing filter, not a scoping one: only entries a stage + // tagged carry the field, so the UI states that a stage filter matches + // the lines that record a stage and nothing else. + Stage: strings.TrimSpace(q.Get("stage")), + Contains: strings.TrimSpace(q.Get("q")), + Limit: limit, }) if err != nil { // A reader that exists but cannot read here is still a statement about @@ -134,32 +138,16 @@ func (s *Server) handleTaskLogDownload(w http.ResponseWriter, r *http.Request) { // 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. +// +// The resolution itself is taskAttemptTarget's, shared with the attempt rail, +// the changed-files read and the debug view, so every per-attempt read of one +// task selects the same attempt for the same request. 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) + t, attempt, ok := s.taskAttemptTarget(w, r) + if !ok { 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 + return t.ID, attempt, true } // taskLogLimit bounds a page the way handleLogs bounds the daemon-wide read: diff --git a/internal/webui/api_tasks_logs_test.go b/internal/webui/api_tasks_logs_test.go index a6ebf7879..a56469e03 100644 --- a/internal/webui/api_tasks_logs_test.go +++ b/internal/webui/api_tasks_logs_test.go @@ -96,6 +96,103 @@ func TestHandleTaskLogsHonoursExplicitAttempt(t *testing.T) { } } +// TestHandleTaskLogsFiltersByStageAndKeepsTheAttempt pins R5's read half: the +// stage parameter reaches the reader's query, narrows the attempt's entries to +// the lines that record that stage, and leaves both the attempt it resolved and +// the file's existence (found) untouched -- a filter that matches nothing is +// "nothing matching this filter", never "this deployment cannot read logs". +func TestHandleTaskLogsFiltersByStageAndKeepsTheAttempt(t *testing.T) { + srv := newTestServer(t) + ctx := t.Context() + baseDir := t.TempDir() + srv.TaskLogs = logging.NewTaskRegistry(baseDir, 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) + } + + // Written to attempt 1's file: an earlier attempt the task's current + // Attempt field does not point at, so the attempt must be requested. + sink, err := logging.NewTaskSink(baseDir, task.ID, 1, logging.TaskSinkOptions{}) + if err != nil { + t.Fatal(err) + } + sink.Logger().Warn("committing the worktree", "stage", "commit") + sink.Logger().Warn("agent produced a diff", "component", "agent") + if err := sink.Close(); err != nil { + t.Fatal(err) + } + + tests := []struct { + name string + query map[string]string + wantCount int + wantFound bool + }{ + { + name: "no stage filter", + query: map[string]string{"attempt": "1"}, + wantCount: 2, + wantFound: true, + }, + { + name: "the stage this attempt recorded", + query: map[string]string{"attempt": "1", "stage": "commit"}, + wantCount: 1, + wantFound: true, + }, + { + name: "a stage no entry records", + query: map[string]string{"attempt": "1", "stage": "implement"}, + wantCount: 0, + wantFound: true, + }, + { + name: "a stage filter on an attempt with no log at all", + query: map[string]string{"attempt": "2", "stage": "commit"}, + wantCount: 0, + wantFound: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + w := getTaskLogs(t, srv, task.ID, tt.query) + if w.Code != http.StatusOK { + t.Fatalf("status = %d, body %s", w.Code, w.Body) + } + var body struct { + Entries []logging.Entry `json:"entries"` + Attempt int `json:"attempt"` + Found bool `json:"found"` + Disabled bool `json:"disabled"` + } + if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil { + t.Fatalf("decode response: %v (%s)", err, w.Body) + } + wantAttempt := 1 + if tt.query["attempt"] == "2" { + wantAttempt = 2 + } + if body.Attempt != wantAttempt { + t.Errorf("attempt = %d, want the requested attempt %d", body.Attempt, wantAttempt) + } + if len(body.Entries) != tt.wantCount { + t.Errorf("entries = %d, want %d (%s)", len(body.Entries), tt.wantCount, w.Body) + } + if body.Found != tt.wantFound { + t.Errorf("found = %v, want %v: a filter that matches nothing is not a missing log", body.Found, tt.wantFound) + } + if body.Disabled { + t.Error("disabled = true for a process that has a task-log reader") + } + }) + } +} + func TestHandleTaskLogsBadID(t *testing.T) { srv := newTestServer(t) req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/tasks/not-a-number/logs", nil) diff --git a/internal/webui/server.go b/internal/webui/server.go index cd5cc2193..00f0f3606 100644 --- a/internal/webui/server.go +++ b/internal/webui/server.go @@ -243,6 +243,9 @@ func (s *Server) registerTaskRoutes(mux *http.ServeMux) { mux.HandleFunc("GET /api/task-meta", s.handleTaskMeta) mux.HandleFunc("POST /api/tasks/{id}/action", s.handleTaskAction) mux.HandleFunc("GET /api/tasks/{id}", s.handleTask) + mux.HandleFunc("GET /api/tasks/{id}/attempts", s.handleTaskAttempts) + mux.HandleFunc("GET /api/tasks/{id}/changes", s.handleTaskChanges) + mux.HandleFunc("GET /api/tasks/{id}/debug", s.handleTaskDebug) mux.HandleFunc("GET /api/tasks/{id}/logs", s.handleTaskLogs) mux.HandleFunc("GET /api/tasks/{id}/logs/download", s.handleTaskLogDownload) } diff --git a/internal/worktree/worktree.go b/internal/worktree/worktree.go index b511b2e9c..ea2fdde38 100644 --- a/internal/worktree/worktree.go +++ b/internal/worktree/worktree.go @@ -36,8 +36,11 @@ import ( "github.com/go-git/go-git/v6/plumbing" gitclient "github.com/go-git/go-git/v6/plumbing/client" "github.com/go-git/go-git/v6/plumbing/filemode" + fdiff "github.com/go-git/go-git/v6/plumbing/format/diff" "github.com/go-git/go-git/v6/plumbing/object" githttp "github.com/go-git/go-git/v6/plumbing/transport/http" + + "github.com/samcharles93/archie-core/internal/domain/workflow/task" ) // preparedSentinel marks a worktree as fully cloned, branched and @@ -598,38 +601,49 @@ func remoteUsesHTTP(r *git.Repository) bool { // Diffing against the remote tip instead would attribute every commit // landed on base since the branch started to this task. func (m *Manager) patch(ctx context.Context, dir, base string) (*object.Patch, error) { + p, _, err := m.patchFromMergeBase(ctx, dir, base) + return p, err +} + +// patchFromMergeBase is patch plus the commit the diff was actually taken +// against. That commit is the merge base of base and HEAD, which is NOT base's +// tip once anyone has landed on the base branch since this branch diverged. +// A capture records it beside the file list it measured, so the two describe +// one change set: re-resolving origin/ on the side would name the new +// tip instead. +func (m *Manager) patchFromMergeBase(ctx context.Context, dir, base string) (*object.Patch, plumbing.Hash, error) { r, err := git.PlainOpen(dir) if err != nil { - return nil, fmt.Errorf("open worktree: %w", err) + return nil, plumbing.ZeroHash, fmt.Errorf("open worktree: %w", err) } baseHash, err := resolveBase(r, base) if err != nil { - return nil, err + return nil, plumbing.ZeroHash, err } head, err := r.Head() if err != nil { - return nil, fmt.Errorf("resolve HEAD: %w", err) + return nil, plumbing.ZeroHash, fmt.Errorf("resolve HEAD: %w", err) } baseCommit, err := r.CommitObject(baseHash) if err != nil { - return nil, fmt.Errorf("load base commit: %w", err) + return nil, plumbing.ZeroHash, fmt.Errorf("load base commit: %w", err) } headCommit, err := r.CommitObject(head.Hash()) if err != nil { - return nil, fmt.Errorf("load head commit: %w", err) + return nil, plumbing.ZeroHash, fmt.Errorf("load head commit: %w", err) } bases, err := baseCommit.MergeBase(headCommit) if err != nil { - return nil, fmt.Errorf("find merge base for %s...HEAD: %w", remoteBase(base), err) + return nil, plumbing.ZeroHash, fmt.Errorf("find merge base for %s...HEAD: %w", remoteBase(base), err) } if len(bases) == 0 { - return nil, fmt.Errorf("find merge base for %s...HEAD: no common ancestor", remoteBase(base)) + return nil, plumbing.ZeroHash, fmt.Errorf("find merge base for %s...HEAD: no common ancestor", remoteBase(base)) } p, err := bases[0].PatchContext(ctx, headCommit) if err != nil { - return nil, fmt.Errorf("diff %s...HEAD: %w", remoteBase(base), err) + return nil, plumbing.ZeroHash, fmt.Errorf("diff %s...HEAD: %w", remoteBase(base), err) } - return p, nil + return p, bases[0].Hash, nil } // ChangedLines reports lines added+deleted vs the base branch -- the @@ -687,6 +701,104 @@ func (m *Manager) ChangedFiles(ctx context.Context, dir, base string) ([]string, return files, nil } +// fileTypeMask is the file-type subset of a tree entry's mode: the bits that +// distinguish a regular file from a symlink or a gitlink. A mode change inside +// one type (the executable bit) is a modification, exactly as git reports it; +// a change across types is a typechange. +const fileTypeMask = 0o170000 + +// ChangedFileStats reports what HEAD changed against base, as the per-file +// status and line counts one attempt's capture records. It reuses patch's +// three-dot semantics: diffing against the remote tip instead would attribute +// every commit landed on base since the branch started to this task. +// +// ChangedLines, Diff and ChangedFiles stay the shape stages call; this is the +// same measurement with the detail a capture needs, so there is still exactly +// one diff path -- and it reports the base that path diffed against. +func (m *Manager) ChangedFileStats(ctx context.Context, dir, base string) (task.ChangeStats, error) { + // The diffed base is the merge base patchFromMergeBase diffed against, not + // base's tip: the recorded pair has to describe the file list beside it. + p, diffedBase, err := m.patchFromMergeBase(ctx, dir, base) + if err != nil { + return task.ChangeStats{}, err + } + r, err := git.PlainOpen(dir) + if err != nil { + return task.ChangeStats{}, fmt.Errorf("open worktree: %w", err) + } + head, err := r.Head() + if err != nil { + return task.ChangeStats{}, fmt.Errorf("resolve HEAD: %w", err) + } + + patches := p.FilePatches() + // Paths come from FilePatches and counts from Stats, joined by position, + // because Stats() carries one entry per patch that produced textual chunks + // and SKIPS the rest (binary files, and any change whose content is + // identical). Reading the two lists off the same index would shift every + // later file's counts onto the wrong path as soon as one binary file is in + // the change. Totals come from Stats so they agree with ChangedLines, which + // sums the same list. + stats := p.Stats() + next := 0 + files := make([]task.FileChange, 0, len(patches)) + totals := task.ChangeTotals{Files: len(patches)} + for _, fp := range patches { + change := fileChange(fp) + if change.Binary || next >= len(stats) { + files = append(files, change) + continue + } + change.Additions = stats[next].Addition + change.Deletions = stats[next].Deletion + totals.Additions += stats[next].Addition + totals.Deletions += stats[next].Deletion + next++ + files = append(files, change) + } + + return task.ChangeStats{ + BaseSHA: diffedBase.String(), + HeadSHA: head.Hash().String(), + Files: files, + Totals: totals, + }, nil +} + +// fileChange classifies one file patch: whether the file was added, deleted, +// renamed, retyped or modified, and whether it produced any textual hunks. +func fileChange(fp fdiff.FilePatch) task.FileChange { + from, to := fp.Files() + change := task.FileChange{ + // No textual hunks. This is what go-git can tell us, not a claim about + // the file on disk: a binary file reports it, and so does a rename whose + // content did not change. + Binary: len(fp.Chunks()) == 0, + } + // At least one side is always present: a change with both entries empty is + // rejected upstream (object.Change.Action), so there is no third state to + // report here. + switch { + case from == nil: + change.Path = to.Path() + change.Status = task.ChangeAdded + case to == nil: + change.Path = from.Path() + change.Status = task.ChangeDeleted + case from.Path() != to.Path(): + change.Path = to.Path() + change.OldPath = from.Path() + change.Status = task.ChangeRenamed + case from.Mode()&fileTypeMask != to.Mode()&fileTypeMask: + change.Path = to.Path() + change.Status = task.ChangeTypeChanged + default: + change.Path = to.Path() + change.Status = task.ChangeModified + } + return change +} + // Snapshot exports HEAD's tracked files into a fresh, empty destDir with no // .git directory: file contents only, no commit history, branch name, or // reflog. Used to build the adversarial reviewer's isolated workspace diff --git a/internal/worktree/worktree_test.go b/internal/worktree/worktree_test.go index 5c9835e76..94c48ebc6 100644 --- a/internal/worktree/worktree_test.go +++ b/internal/worktree/worktree_test.go @@ -15,6 +15,7 @@ import ( "github.com/go-git/go-git/v6/plumbing/object" "github.com/samcharles93/archie-core/internal/container" + "github.com/samcharles93/archie-core/internal/domain/workflow/task" ) const testBase = "main" @@ -1127,3 +1128,334 @@ func TestTaskBriefIsNeverCommitted(t *testing.T) { t.Errorf("brief missing after commit: %v", err) } } + +func TestOperationsOnNonRepositoryDiffstat(t *testing.T) { + ctx := context.Background() + m := &Manager{WorkDir: t.TempDir()} + if _, err := m.ChangedFileStats(ctx, t.TempDir(), testBase); err == nil { + t.Error("ChangedFileStats() error = nil, want a failure") + } +} + +// diffstatSeed is one file written into the fixture's first commit. +type diffstatSeed struct { + path string + content string +} + +// newDiffstatRepo builds a repository whose only commit is reachable as both +// the branch tip and refs/remotes/origin/. That remote-tracking ref is +// what resolveBase prefers, so the three-dot base is the same commit whether +// or not the fixture ever had a remote -- and a diffstat fixture needs a base +// commit that already contains the paths a change modifies, renames or +// deletes. +func newDiffstatRepo(t *testing.T, seeds []diffstatSeed) (string, *git.Repository, plumbing.Hash) { + t.Helper() + dir := t.TempDir() + r, err := git.PlainInit(dir, false, git.WithDefaultBranch(plumbing.NewBranchReferenceName(testBase))) + if err != nil { + t.Fatalf("init fixture repo: %v", err) + } + for _, seed := range seeds { + writeSeedCommit(t, r, dir, seed.path, seed.content, "seed "+seed.path) + } + head, err := r.Head() + if err != nil { + t.Fatalf("resolve fixture HEAD: %v", err) + } + if err := r.Storer.SetReference(plumbing.NewHashReference( + plumbing.NewRemoteReferenceName(git.DefaultRemoteName, testBase), head.Hash(), + )); err != nil { + t.Fatalf("set remote-tracking base ref: %v", err) + } + return dir, r, head.Hash() +} + +// commitAll commits the fixture repo's working tree, failing the test on a +// commit error. +func commitAll(t *testing.T, r *git.Repository, dir, message string) { + t.Helper() + disableSigning(t, r) + wt, err := r.Worktree() + if err != nil { + t.Fatalf("worktree: %v", err) + } + if _, err := wt.Add("."); err != nil { + t.Fatalf("add all: %v", err) + } + sig := testSignature() + if _, err := wt.Commit(message, &git.CommitOptions{Author: sig, Committer: sig}); err != nil { + t.Fatalf("commit: %v", err) + } +} + +// TestChangedFileStatsReportsWhatOneAttemptChanged is the producer-side half +// of the changed-file capture: status and per-file line counts are read off +// the SAME three-dot patch the diff cap reads, where the merge base is +// resolved, so a capture can never disagree with the size gate about what +// changed. +func TestChangedFileStatsReportsWhatOneAttemptChanged(t *testing.T) { + binaryContent := []byte{0x00, 0x01, 0x02, 0xff, 0xfe} + + tests := []struct { + name string + seeds []diffstatSeed + mutate func(t *testing.T, dir string, r *git.Repository) + wantFiles []task.FileChange + wantTotal task.ChangeTotals + }{ + { + name: "added", + seeds: []diffstatSeed{{path: "README.md", content: "seed\n"}}, + mutate: func(t *testing.T, dir string, _ *git.Repository) { + t.Helper() + if err := os.WriteFile(filepath.Join(dir, "new.txt"), []byte("one\ntwo\n"), 0o600); err != nil { + t.Fatal(err) + } + }, + wantFiles: []task.FileChange{ + {Path: "new.txt", Status: task.ChangeAdded, Additions: 2}, + }, + wantTotal: task.ChangeTotals{Files: 1, Additions: 2}, + }, + { + name: "modified", + seeds: []diffstatSeed{{path: "a.txt", content: "one\n"}}, + mutate: func(t *testing.T, dir string, _ *git.Repository) { + t.Helper() + if err := os.WriteFile(filepath.Join(dir, "a.txt"), []byte("one\ntwo\n"), 0o600); err != nil { + t.Fatal(err) + } + }, + wantFiles: []task.FileChange{ + {Path: "a.txt", Status: task.ChangeModified, Additions: 1}, + }, + wantTotal: task.ChangeTotals{Files: 1, Additions: 1}, + }, + { + name: "deleted", + seeds: []diffstatSeed{{path: "a.txt", content: "one\ntwo\n"}}, + mutate: func(t *testing.T, dir string, _ *git.Repository) { + t.Helper() + if err := os.Remove(filepath.Join(dir, "a.txt")); err != nil { + t.Fatal(err) + } + }, + wantFiles: []task.FileChange{ + {Path: "a.txt", Status: task.ChangeDeleted, Deletions: 2}, + }, + wantTotal: task.ChangeTotals{Files: 1, Deletions: 2}, + }, + { + name: "renamed", + seeds: []diffstatSeed{{path: "old.txt", content: "same content\n"}}, + mutate: func(t *testing.T, dir string, _ *git.Repository) { + t.Helper() + if err := os.Remove(filepath.Join(dir, "old.txt")); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "new.txt"), []byte("same content\n"), 0o600); err != nil { + t.Fatal(err) + } + }, + // go-git reports the rename with its old path preserved and no line + // changes, because the content is identical: the line counts describe + // the content diff, not the path change. + wantFiles: []task.FileChange{ + {Path: "new.txt", OldPath: "old.txt", Status: task.ChangeRenamed}, + }, + wantTotal: task.ChangeTotals{Files: 1}, + }, + { + name: "binary alongside a text file keeps both paths with their own counts", + seeds: []diffstatSeed{{path: "README.md", content: "seed\n"}}, + mutate: func(t *testing.T, dir string, _ *git.Repository) { + t.Helper() + // The binary file sorts before the text file, and go-git's + // Patch.Stats skips it entirely (no textual chunks). A capture + // that read paths and counts off one shared index would hand the + // text file's counts to the binary file here. + if err := os.WriteFile(filepath.Join(dir, "aaa-image.bin"), binaryContent, 0o600); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "zzz-text.txt"), []byte("one\ntwo\n"), 0o600); err != nil { + t.Fatal(err) + } + }, + wantFiles: []task.FileChange{ + {Path: "aaa-image.bin", Status: task.ChangeAdded, Binary: true}, + {Path: "zzz-text.txt", Status: task.ChangeAdded, Additions: 2}, + }, + wantTotal: task.ChangeTotals{Files: 2, Additions: 2}, + }, + { + name: "a regular file replaced by a symlink is a typechange", + seeds: []diffstatSeed{{path: "link.txt", content: "target\n"}}, + mutate: func(t *testing.T, dir string, _ *git.Repository) { + t.Helper() + if err := os.Remove(filepath.Join(dir, "link.txt")); err != nil { + t.Fatal(err) + } + if err := os.Symlink("target", filepath.Join(dir, "link.txt")); err != nil { + t.Fatal(err) + } + }, + // git reports this as a typechange: the entry changed from a regular + // file to a symlink. The line counts are the content diff (a symlink's + // blob holds the target path, without the trailing newline). + wantFiles: []task.FileChange{ + {Path: "link.txt", Status: task.ChangeTypeChanged, Additions: 1, Deletions: 1}, + }, + wantTotal: task.ChangeTotals{Files: 1, Additions: 1, Deletions: 1}, + }, + { + name: "a rename alongside a binary file keeps every count with its own path", + seeds: []diffstatSeed{{path: "README.md", content: "seed\n"}, {path: "old.txt", content: "same content\n"}}, + mutate: func(t *testing.T, dir string, _ *git.Repository) { + t.Helper() + if err := os.Remove(filepath.Join(dir, "old.txt")); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "new.txt"), []byte("same content\n"), 0o600); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "aaa-image.bin"), binaryContent, 0o600); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "zzz-text.txt"), []byte("one\ntwo\n"), 0o600); err != nil { + t.Fatal(err) + } + }, + wantFiles: []task.FileChange{ + {Path: "aaa-image.bin", Status: task.ChangeAdded, Binary: true}, + {Path: "new.txt", OldPath: "old.txt", Status: task.ChangeRenamed}, + {Path: "zzz-text.txt", Status: task.ChangeAdded, Additions: 2}, + }, + wantTotal: task.ChangeTotals{Files: 3, Additions: 2}, + }, + { + name: "nothing changed", + seeds: []diffstatSeed{{path: "README.md", content: "seed\n"}}, + mutate: func(*testing.T, string, *git.Repository) {}, + // Empty, not nil: the persisted payload carries a list, and a null + // there would read as a missing measurement rather than a measured + // zero. + wantFiles: []task.FileChange{}, + wantTotal: task.ChangeTotals{}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dir, r, baseHash := newDiffstatRepo(t, tt.seeds) + tt.mutate(t, dir, r) + var head plumbing.Hash + if tt.name != "nothing changed" { + commitAll(t, r, dir, "change") + got, err := r.Head() + if err != nil { + t.Fatal(err) + } + head = got.Hash() + } else { + head = baseHash + } + + m := &Manager{WorkDir: t.TempDir()} + stats, err := m.ChangedFileStats(t.Context(), dir, testBase) + if err != nil { + t.Fatalf("ChangedFileStats() error = %v", err) + } + if stats.BaseSHA != baseHash.String() { + t.Errorf("BaseSHA = %q, want the resolved base %q", stats.BaseSHA, baseHash) + } + if stats.HeadSHA != head.String() { + t.Errorf("HeadSHA = %q, want HEAD %q", stats.HeadSHA, head) + } + if len(stats.Files) != len(tt.wantFiles) { + t.Fatalf("Files = %+v, want %d entries %+v", stats.Files, len(tt.wantFiles), tt.wantFiles) + } + for i, want := range tt.wantFiles { + if got := stats.Files[i]; got != want { + t.Errorf("Files[%d] = %+v, want %+v", i, got, want) + } + } + if stats.Totals != tt.wantTotal { + t.Errorf("Totals = %+v, want %+v", stats.Totals, tt.wantTotal) + } + }) + } +} + +// TestChangedFileStatsRecordsTheDiffedBaseNotTheAdvancedTip pins the identity +// of the recorded base to the commit the file list was actually diffed against +// -- the merge base -- and not to base's tip, which moves whenever anyone +// lands on the base branch mid-run. Naming the tip would pair a file list with +// a commit it was never compared to. +func TestChangedFileStatsRecordsTheDiffedBaseNotTheAdvancedTip(t *testing.T) { + ctx := context.Background() + host := newLocalRemote(t, "acme", "todo") + m := newManager(t, host) + + dir, _, err := m.Prepare(ctx, "acme", "todo", testBase, 8, "feat: mine", "", "feature") + if err != nil { + t.Fatalf("Prepare() error = %v", err) + } + r, err := git.PlainOpen(dir) + if err != nil { + t.Fatal(err) + } + diverged, err := r.ResolveRevision(plumbing.Revision(remoteBase(testBase))) + if err != nil { + t.Fatalf("resolve the base this branch diverged from: %v", err) + } + + if err := os.WriteFile(filepath.Join(dir, "mine.txt"), []byte("mine\n"), 0o600); err != nil { + t.Fatal(err) + } + if _, err := m.CommitAll(ctx, dir, "feat: mine"); err != nil { + t.Fatalf("CommitAll() error = %v", err) + } + + // Another author lands on the base branch and this clone fetches it, so + // origin/ now names a commit this branch never contained. + seed := filepath.Join(t.TempDir(), "other") + sr, err := git.PlainClone(seed, &git.CloneOptions{URL: filepath.Join(host, "acme", "todo.git")}) + if err != nil { + t.Fatalf("clone for second author: %v", err) + } + writeSeedCommit(t, sr, seed, "theirs.txt", "theirs\n", "chore: theirs") + ref := plumbing.NewBranchReferenceName(testBase) + if err := sr.Push(&git.PushOptions{ + RemoteName: git.DefaultRemoteName, + RefSpecs: []gitconfig.RefSpec{gitconfig.RefSpec(ref + ":" + ref)}, + }); err != nil { + t.Fatalf("second author push: %v", err) + } + if err := r.Fetch(&git.FetchOptions{RemoteName: git.DefaultRemoteName}); err != nil { + t.Fatalf("fetch: %v", err) + } + tip, err := r.ResolveRevision(plumbing.Revision(remoteBase(testBase))) + if err != nil { + t.Fatalf("resolve the advanced base tip: %v", err) + } + if tip == diverged { + t.Fatal("the base branch did not advance; this test would assert nothing") + } + + stats, err := m.ChangedFileStats(ctx, dir, testBase) + if err != nil { + t.Fatalf("ChangedFileStats() error = %v", err) + } + if stats.BaseSHA != diverged.String() { + t.Errorf("BaseSHA = %q, want the diffed merge base %q (the base tip is %q)", + stats.BaseSHA, diverged, tip) + } + // The same rule from the other side: a file list measured from the merge + // base cannot name a commit that landed on the base branch afterwards. + for _, f := range stats.Files { + if f.Path == "theirs.txt" { + t.Errorf("Files = %+v, includes another author's file: the diff is not merge-base relative", stats.Files) + } + } +}