diff --git a/internal/taskstate/taskstate.go b/internal/taskstate/taskstate.go index b9cc8542a..3b6c2cd82 100644 --- a/internal/taskstate/taskstate.go +++ b/internal/taskstate/taskstate.go @@ -143,3 +143,54 @@ func CheckDecline(status string) error { } return fmt.Errorf("task cannot be declined while %s", status) } + +// StatusMeta describes how to present a lifecycle status. It is the single +// source for the dashboard's label, pill severity and "needs you" grouping, so +// the frontend never has to keep a hand-synced copy of the vocabulary. +type StatusMeta struct { + ID string `json:"id"` + Label string `json:"label"` + Kind string `json:"kind"` // pill severity: idle, info, ok, warn, danger + NeedsYou bool `json:"needs_you"` // counts toward the "Needs you" filter +} + +// ActionMeta describes how to present an operator control. Confirmation text +// uses "{title}" as a placeholder for the task title. +type ActionMeta struct { + ID string `json:"id"` + Label string `json:"label"` + Kind string `json:"kind"` // button variant: primary, quiet, danger, link + Confirm string `json:"confirm"` // optional confirm prompt, may be "" +} + +// Statuses returns the presentation catalog for every lifecycle status, in +// display order. Callers receive a fresh slice. +func Statuses() []StatusMeta { + return []StatusMeta{ + {ID: Queued, Label: "Queued", Kind: "idle"}, + {ID: Running, Label: "Working", Kind: "info"}, + {ID: WaitingHuman, Label: "Waiting for you", Kind: "warn", NeedsYou: true}, + {ID: PROpen, Label: "In review", Kind: "ok"}, + {ID: Merged, Label: "Merged", Kind: "ok"}, + {ID: Parked, Label: "Parked", Kind: "warn", NeedsYou: true}, + {ID: Dead, Label: "Stopped (too many retries)", Kind: "danger"}, + {ID: Rejected, Label: "Rejected", Kind: "danger"}, + {ID: Declined, Label: "Won't do", Kind: "idle"}, + } +} + +// ActionCatalog returns the presentation catalog for every operator control. +// Callers receive a fresh slice. +func ActionCatalog() []ActionMeta { + return []ActionMeta{ + {ID: string(ActionCancel), Label: "Cancel", Kind: "quiet", Confirm: `Cancel "{title}"? This closes the forge issue.`}, + {ID: string(ActionStop), Label: "Stop", Kind: "primary", Confirm: `Stop "{title}"? Recoverable work will remain parked.`}, + {ID: string(ActionApprove), Label: "Approve", Kind: "primary"}, + {ID: string(ActionReject), Label: "Reject", Kind: "quiet", Confirm: `Reject "{title}"? This closes the forge issue.`}, + {ID: string(ActionRetry), Label: "Retry", Kind: "primary"}, + {ID: string(ActionAbandon), Label: "Abandon", Kind: "quiet", Confirm: `Abandon "{title}"? This closes the forge issue.`}, + {ID: string(ActionArchive), Label: "Archive", Kind: "quiet", Confirm: `Archive the local record for "{title}"?`}, + {ID: string(ActionOpenPR), Label: "Open PR", Kind: "link"}, + {ID: string(ActionOpenIssue), Label: "Open issue", Kind: "link"}, + } +} diff --git a/internal/taskstate/taskstate_test.go b/internal/taskstate/taskstate_test.go index c2aa92e39..32bb90063 100644 --- a/internal/taskstate/taskstate_test.go +++ b/internal/taskstate/taskstate_test.go @@ -79,6 +79,73 @@ func TestRejectedAndDeclinedAreDistinct(t *testing.T) { } } +// The presentation catalog must cover every status and action the lifecycle +// exposes, so the frontend can render anything the rules allow without holding +// its own copy of the vocabulary. +func TestPresentationCatalog(t *testing.T) { + allowedKinds := map[string]bool{"idle": true, "info": true, "ok": true, "warn": true, "danger": true} + + statuses := Statuses() + if len(statuses) == 0 { + t.Fatal("Statuses() returned no entries") + } + seen := map[string]bool{} + for _, meta := range statuses { + if meta.ID == "" || meta.Label == "" || !allowedKinds[meta.Kind] { + t.Errorf("status %q: id=%q label=%q kind=%q", meta.ID, meta.ID, meta.Label, meta.Kind) + } + if seen[meta.ID] { + t.Errorf("duplicate status id %q", meta.ID) + } + seen[meta.ID] = true + if meta.NeedsYou && meta.Kind == "idle" { + t.Errorf("status %q is marked needs_you but has idle severity", meta.ID) + } + } + // Every lifecycle status must have presentation metadata. + for _, status := range []string{Queued, Running, WaitingHuman, PROpen, Merged, Parked, Dead, Rejected, Declined} { + if !seen[status] { + t.Errorf("status %q missing from Statuses()", status) + } + } + + actions := ActionCatalog() + if len(actions) == 0 { + t.Fatal("ActionCatalog() returned no entries") + } + actionSeen := map[string]bool{} + for _, meta := range actions { + if meta.ID == "" || meta.Label == "" { + t.Errorf("action %q: id=%q label=%q", meta.ID, meta.ID, meta.Label) + } + if actionSeen[meta.ID] { + t.Errorf("duplicate action id %q", meta.ID) + } + actionSeen[meta.ID] = true + // link actions navigate; the rest must carry a button kind. + if meta.Kind != "link" && meta.Kind != "primary" && meta.Kind != "quiet" && meta.Kind != "danger" { + t.Errorf("action %q has unknown kind %q", meta.ID, meta.Kind) + } + } + + // Every action the lifecycle table can emit must be describable, otherwise + // the frontend has no way to present it. + byID := func(status string) map[Action]bool { + out := map[Action]bool{} + for _, a := range Actions(status) { + out[a] = true + } + return out + } + for _, status := range []string{Queued, Running, WaitingHuman, PROpen, Parked, Merged, Rejected, Dead, Declined} { + for a := range byID(status) { + if !actionSeen[string(a)] { + t.Errorf("action %q from status %q has no ActionCatalog entry", a, status) + } + } + } +} + func TestActionsReturnsIndependentSlices(t *testing.T) { first := Actions(WaitingHuman) first[0] = ActionArchive diff --git a/internal/webui/api_task_meta.go b/internal/webui/api_task_meta.go new file mode 100644 index 000000000..8bd0c311b --- /dev/null +++ b/internal/webui/api_task_meta.go @@ -0,0 +1,19 @@ +package webui + +import ( + "net/http" + + "github.com/samcharles93/archie-core/internal/taskstate" +) + +// handleTaskMeta serves the lifecycle presentation catalog: the status +// vocabulary (with label, pill severity, "needs you" grouping) and the operator +// action controls (with label, button variant, confirm prompt). Everything the +// dashboard can present is derived here, so the frontend never has to keep a +// hand-synced copy of the vocabulary or the control set. +func (s *Server) handleTaskMeta(w http.ResponseWriter, _ *http.Request) { + writeJSON(w, map[string]any{ + "statuses": taskstate.Statuses(), + "actions": taskstate.ActionCatalog(), + }) +} diff --git a/internal/webui/api_task_meta_test.go b/internal/webui/api_task_meta_test.go new file mode 100644 index 000000000..457a7fc0e --- /dev/null +++ b/internal/webui/api_task_meta_test.go @@ -0,0 +1,57 @@ +package webui + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" +) + +// The dashboard derives everything it can present from /api/task-meta. If the +// catalog is empty or malformed the frontend falls back to its own defaults and +// the whole point of the endpoint -- one server-held vocabulary -- is lost. +func TestTaskMetaCatalog(t *testing.T) { + srv := newTestServer(t) + + req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/task-meta", nil) + w := httptest.NewRecorder() + srv.Handler().ServeHTTP(w, req) + if w.Code != http.StatusOK { + t.Fatalf("status = %d, body %s", w.Code, w.Body) + } + + var got struct { + Statuses []struct { + ID string `json:"id"` + Label string `json:"label"` + Kind string `json:"kind"` + NeedsYou bool `json:"needs_you"` + } `json:"statuses"` + Actions []struct { + ID string `json:"id"` + Label string `json:"label"` + Kind string `json:"kind"` + Confirm string `json:"confirm"` + } `json:"actions"` + } + if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { + t.Fatalf("unmarshal: %v", err) + } + + if len(got.Statuses) == 0 { + t.Error("statuses empty") + } + if len(got.Actions) == 0 { + t.Error("actions empty") + } + for _, s := range got.Statuses { + if s.ID == "" || s.Label == "" { + t.Errorf("status missing id/label: %+v", s) + } + } + for _, a := range got.Actions { + if a.ID == "" || a.Label == "" { + t.Errorf("action missing id/label: %+v", a) + } + } +} diff --git a/internal/webui/server.go b/internal/webui/server.go index a22fc2452..5a72cb838 100644 --- a/internal/webui/server.go +++ b/internal/webui/server.go @@ -249,6 +249,7 @@ func (s *Server) Handler() http.Handler { mux.HandleFunc("GET /api/summary", s.handleSummary) mux.HandleFunc("GET /api/setup", s.handleSetup) mux.HandleFunc("GET /api/tasks", s.handleTasks) + 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/workflows", s.handleWorkflows) diff --git a/ui/src/base/api.js b/ui/src/base/api.js index 525130eff..f589923d5 100644 --- a/ui/src/base/api.js +++ b/ui/src/base/api.js @@ -54,6 +54,7 @@ export class ApiError extends Error { export const api = { summary: () => req("/api/summary"), tasks: () => req("/api/tasks"), + taskMeta: () => req("/api/task-meta"), task: (id) => req(`/api/tasks/${id}`), taskAction: async (id, action) => { const res = await fetch(`/api/tasks/${id}/action`, { diff --git a/ui/src/base/task-meta.js b/ui/src/base/task-meta.js new file mode 100644 index 000000000..21666a31f --- /dev/null +++ b/ui/src/base/task-meta.js @@ -0,0 +1,90 @@ +// Central lifecycle vocabulary for the dashboard. The backend is the source of +// truth: /api/task-meta returns the statuses (label, pill severity, "needs you" +// grouping) and operator actions (label, button variant, confirm prompt). This +// module fetches that catalog and exposes the accessors the rest of the UI +// reads, so no surface keeps a hand-synced copy of the vocabulary. +// +// The defaults below are a freeze-dried snapshot that render immediately and +// keep working when archied is unreachable; loadTaskMeta() replaces them with +// the live catalog as soon as it arrives. They must match the backend catalog +// so drawing the page never flashes a stale label. +import { api } from "./api.js"; + +const DEFAULT_STATUSES = [ + { id: "queued", label: "Queued", kind: "idle" }, + { id: "running", label: "Working", kind: "info" }, + { id: "waiting_human", label: "Waiting for you", kind: "warn", needs_you: true }, + { id: "pr_open", label: "In review", kind: "ok" }, + { id: "merged", label: "Merged", kind: "ok" }, + { id: "parked", label: "Parked", kind: "warn", needs_you: true }, + { id: "dead", label: "Stopped (too many retries)", kind: "danger" }, + { id: "rejected", label: "Rejected", kind: "danger" }, + { id: "closed_wont_do", label: "Won't do", kind: "idle" }, +]; + +const DEFAULT_ACTIONS = [ + { id: "cancel", label: "Cancel", kind: "quiet", confirm: `Cancel "{title}"? This closes the forge issue.` }, + { id: "stop", label: "Stop", kind: "primary", confirm: `Stop "{title}"? Recoverable work will remain parked.` }, + { id: "approve", label: "Approve", kind: "primary" }, + { id: "reject", label: "Reject", kind: "quiet", confirm: `Reject "{title}"? This closes the forge issue.` }, + { id: "retry", label: "Retry", kind: "primary" }, + { id: "abandon", label: "Abandon", kind: "quiet", confirm: `Abandon "{title}"? This closes the forge issue.` }, + { id: "archive", label: "Archive", kind: "quiet", confirm: `Archive the local record for "{title}"?` }, + { id: "open_pr", label: "Open PR", kind: "link" }, + { id: "open_issue", label: "Open issue", kind: "link" }, +]; + +let statuses = [...DEFAULT_STATUSES]; +let actions = [...DEFAULT_ACTIONS]; +let statusById = new Map(statuses.map((s) => [s.id, s])); +let actionById = new Map(actions.map((a) => [a.id, a])); + +function rebuildIndexes() { + statusById = new Map(statuses.map((s) => [s.id, s])); + actionById = new Map(actions.map((a) => [a.id, a])); +} + +// loadTaskMeta upgrades the dashboard from the server catalog. It never throws: +// a failed fetch keeps the defaults so the UI still renders. +export async function loadTaskMeta() { + try { + const data = await api.taskMeta(); + if (Array.isArray(data?.statuses)) statuses = data.statuses; + if (Array.isArray(data?.actions)) actions = data.actions; + rebuildIndexes(); + } catch { + // archied unreachable or not yet serving this route; keep the defaults. + } +} + +// statusLabel is the human-readable name for a lifecycle status. +export function statusLabel(id) { + return statusById.get(id)?.label || id || "Unknown"; +} + +// statusKind is the pill severity token (idle, info, ok, warn, danger). +export function statusKind(id) { + return statusById.get(id)?.kind || "idle"; +} + +// statusList returns every known lifecycle status in display order. +export function statusList() { + return statuses; +} + +// statusIds returns every known lifecycle status id, in display order. +export function statusIds() { + return statuses.map((s) => s.id); +} + +// attentionStatusIds is the set of statuses that count toward the "Needs you" +// filter (work that is waiting on a human). +export function attentionStatusIds() { + return new Set(statuses.filter((s) => s.needs_you).map((s) => s.id)); +} + +// actionFor returns the presentation metadata for an operator control, or null +// if the backend has never seen it. +export function actionFor(id) { + return actionById.get(id) || null; +} diff --git a/ui/test/task-meta.test.js b/ui/test/task-meta.test.js new file mode 100644 index 000000000..0ebd60571 --- /dev/null +++ b/ui/test/task-meta.test.js @@ -0,0 +1,61 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import "./shim.js"; +import { + actionFor, + attentionStatusIds, + statusIds, + statusKind, + statusLabel, +} from "../src/base/task-meta.js"; + +test("statusLabel returns the human label for known statuses", () => { + assert.equal(statusLabel("pr_open"), "In review"); + assert.equal(statusLabel("waiting_human"), "Waiting for you"); +}); + +test("statusLabel falls back to the raw id for unknown statuses", () => { + assert.equal(statusLabel("quantum_superposition"), "quantum_superposition"); +}); + +test("statusKind returns the pill severity for a status", () => { + assert.equal(statusKind("running"), "info"); + assert.equal(statusKind("pr_open"), "ok"); + assert.equal(statusKind("parked"), "warn"); + assert.equal(statusKind("dead"), "danger"); +}); + +test("statusKind defaults to idle for unknown statuses", () => { + assert.equal(statusKind("quantum_superposition"), "idle"); +}); + +test("statusIds includes every lifecycle status", () => { + assert.deepEqual(new Set(statusIds()), new Set([ + "queued", "running", "waiting_human", "pr_open", "merged", + "parked", "dead", "rejected", "closed_wont_do", + ])); +}); + +test("attentionStatusIds groups the statuses that need a human", () => { + const ids = attentionStatusIds(); + assert.ok(ids.has("waiting_human")); + assert.ok(ids.has("parked")); + assert.ok(!ids.has("queued")); +}); + +test("actionFor returns label, kind and confirm from the catalog", () => { + const reject = actionFor("reject"); + assert.ok(reject); + assert.equal(reject.label, "Reject"); + assert.equal(reject.kind, "quiet"); + assert.ok(reject.confirm.includes("{title}")); +}); + +test("actionFor marks navigation links as link kind", () => { + assert.equal(actionFor("open_pr").kind, "link"); + assert.equal(actionFor("open_issue").kind, "link"); +}); + +test("actionFor returns null for an unknown action", () => { + assert.equal(actionFor("teleport"), null); +});