diff --git a/README.md b/README.md index cb38ede..65e1a12 100644 --- a/README.md +++ b/README.md @@ -1,38 +1,42 @@ # gitmoot-dashboard Read-only web backend for the [gitmoot](https://github.com/jerryfane/gitmoot) -orchestration dashboard. It serves a live orchestration-DAG UI and streams run -state from gitmoot's store. +dashboard. It serves the embedded dashboard UI and a JSON API over gitmoot's +store. -This is a small Go module (stdlib only, no third-party dependencies). The React -UI lives under `web/` and is built into `web/dist`, which is embedded into the -binary; a minimal placeholder ships until the UI lands. +This is a small Go module (stdlib only, no third-party dependencies). The UI +lives in `web/dist` and is embedded into the binary. ## Contract -The package exposes a frozen `DataSource` interface and an HTTP `Serve` entry -point (see `datasource.go`): +The package exposes a `DataSource` interface (plus optional extensions such as +`WorkflowDataSource`, `OverviewDataSource`, `TasksDataSource`, +`OrgDataSource` and `ChangeCursorDataSource`) and an HTTP `Serve` entry point; +see `datasource.go`. ```go -type DataSource interface { - Runs(ctx context.Context) ([]RunSummary, error) - State(ctx context.Context, runID string) (State, error) // runID "" => active/most-recent - Job(ctx context.Context, jobID string) (Node, error) - Subscribe(ctx context.Context, runID string) (<-chan State, func(), error) // for SSE -} - func Serve(ds DataSource) http.Handler ``` HTTP surface: -| Route | Returns | -| ----------------------- | ------------------------------------ | -| `GET /api/runs` | `[]RunSummary` | -| `GET /api/state?run=` | `State` | -| `GET /api/job/{id}` | `Node` | -| `GET /events?run=` | SSE stream of `State` | -| everything else | embedded static UI (SPA fallback) | +| Route | Returns | +| --- | --- | +| `GET /api/overview` | `Overview` | +| `GET /api/tasks` | `[]TaskSummary` | +| `GET /api/workflows`, `GET /api/workflow/{label}` | `[]WorkflowIndexEntry`, `WorkflowView` | +| `GET /api/jobs`, `GET /api/job/{id}` | `[]JobSummary`, `Node` | +| `GET /api/job/{id}/checks` | `JobChecks` | +| `GET /api/agents`, `GET /api/agent/{name}` | `[]AgentSummary`, `AgentDetail` | +| `GET /api/org`, `GET /api/org/role/{name}` | `OrgView`, `OrgRoleView` | +| `GET /api/graph?repo=` | `Graph` (Galaxy) | +| `GET /api/charts?days=` | `Charts` | +| `GET /api/health` | `Health` | +| `GET /api/config` | `ConfigSnapshot` | +| `GET /api/attention` | `Attention` | +| `GET /api/events` | SSE change pings (`event: changed`) | +| other `/api/...` paths and `/events` | 404 | +| everything else | embedded static UI (SPA fallback) | The server is **read-only** and must never run in the gitmoot daemon path. diff --git a/api.go b/api.go index c84334b..e07a303 100644 --- a/api.go +++ b/api.go @@ -228,7 +228,7 @@ func writeJSON(w http.ResponseWriter, code int, v any) { // statusForError maps a DataSource error to an HTTP status code: not-found // sentinels become 404, everything else 500. func statusForError(err error) int { - if errors.Is(err, ErrRunNotFound) || errors.Is(err, ErrJobNotFound) || errors.Is(err, ErrAgentNotFound) || errors.Is(err, ErrOrgRoleNotFound) || errors.Is(err, ErrWorkflowNotFound) { + if errors.Is(err, ErrJobNotFound) || errors.Is(err, ErrAgentNotFound) || errors.Is(err, ErrOrgRoleNotFound) || errors.Is(err, ErrWorkflowNotFound) { return http.StatusNotFound } return http.StatusInternalServerError @@ -531,21 +531,8 @@ func (s *server) handleWorkflow(w http.ResponseWriter, r *http.Request) { writeJSON(w, http.StatusOK, view) } -// handleRuns serves GET /api/runs -> []RunSummary. -func (s *server) handleRuns(w http.ResponseWriter, r *http.Request) { - runs, err := s.ds.Runs(r.Context()) - if err != nil { - http.Error(w, err.Error(), statusForError(err)) - return - } - if runs == nil { - runs = []RunSummary{} - } - writeJSON(w, http.StatusOK, runs) -} - // handleJobs serves GET /api/jobs -> []JobSummary, every job across all runs -// (the client filters). Mirrors handleRuns. +// (the client filters). func (s *server) handleJobs(w http.ResponseWriter, r *http.Request) { jobs, err := s.ds.Jobs(r.Context()) if err != nil { @@ -558,7 +545,7 @@ func (s *server) handleJobs(w http.ResponseWriter, r *http.Request) { writeJSON(w, http.StatusOK, jobs) } -// handleAgents serves GET /api/agents -> []AgentSummary. Mirrors handleRuns. +// handleAgents serves GET /api/agents -> []AgentSummary. func (s *server) handleAgents(w http.ResponseWriter, r *http.Request) { agents, err := s.ds.Agents(r.Context()) if err != nil { @@ -584,15 +571,11 @@ func (s *server) handleAgent(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), statusForError(err)) return } - if detail.Versions == nil { - detail.Versions = []TemplateVersionInfo{} - } writeJSON(w, http.StatusOK, detail) } // handleCharts serves GET /api/charts?days=N -> Charts. days accepts only 0 // (all history), 7, 30 or 90; any missing/invalid/other value defaults to 30. -// Mirrors handleRuns. func (s *server) handleCharts(w http.ResponseWriter, r *http.Request) { days := 30 switch r.URL.Query().Get("days") { @@ -622,16 +605,13 @@ func (s *server) handleCharts(w http.ResponseWriter, r *http.Request) { writeJSON(w, http.StatusOK, charts) } -// handleHealth serves GET /api/health -> Health. Mirrors handleRuns. +// handleHealth serves GET /api/health -> Health. func (s *server) handleHealth(w http.ResponseWriter, r *http.Request) { h, err := s.ds.Health(r.Context()) if err != nil { http.Error(w, err.Error(), statusForError(err)) return } - if h.Locks == nil { - h.Locks = []HealthLock{} - } if h.ResourceLocks == nil { h.ResourceLocks = []HealthResourceLock{} } @@ -680,32 +660,9 @@ func (s *server) handleConfig(w http.ResponseWriter, r *http.Request) { writeJSON(w, http.StatusOK, c) } -// handleLearningSkills serves GET /api/learning/skills -> Skills, the SkillOpt -// evolution overview. Mirrors handleRuns; every list is coerced non-nil so the -// client always sees JSON arrays. -func (s *server) handleLearningSkills(w http.ResponseWriter, r *http.Request) { - skills, err := s.ds.Skills(r.Context()) - if err != nil { - http.Error(w, err.Error(), statusForError(err)) - return - } - if skills.Templates == nil { - skills.Templates = []SkillTemplate{} - } - for i := range skills.Templates { - if skills.Templates[i].Versions == nil { - skills.Templates[i].Versions = []SkillVersion{} - } - if skills.Templates[i].Pending == nil { - skills.Templates[i].Pending = []SkillCandidate{} - } - } - writeJSON(w, http.StatusOK, skills) -} - // handleAttention serves GET /api/attention -> Attention, the "Needs a human" -// view (gitmoot #528). Mirrors handleRuns; Gates is coerced non-nil so the -// client always sees a JSON array. +// view (gitmoot #528). Gates is coerced non-nil so the client always sees a +// JSON array. func (s *server) handleAttention(w http.ResponseWriter, r *http.Request) { att, err := s.ds.Attention(r.Context()) if err != nil { @@ -738,18 +695,6 @@ func (s *server) handleJobChecks(w http.ResponseWriter, r *http.Request) { writeJSON(w, http.StatusOK, checks) } -// handleState serves GET /api/state?run= -> State. An empty run resolves to -// the active/most-recent run. -func (s *server) handleState(w http.ResponseWriter, r *http.Request) { - run := r.URL.Query().Get("run") - state, err := s.ds.State(r.Context(), run) - if err != nil { - http.Error(w, err.Error(), statusForError(err)) - return - } - writeJSON(w, http.StatusOK, state) -} - // handleJob serves GET /api/job/{id} -> Node. func (s *server) handleJob(w http.ResponseWriter, r *http.Request) { id := r.PathValue("id") diff --git a/api_test.go b/api_test.go index 14ec5d2..6bdb2bc 100644 --- a/api_test.go +++ b/api_test.go @@ -252,38 +252,6 @@ func TestHandleChangeEventsRegistryCapAndCleanup(t *testing.T) { } } -func TestHandleRuns(t *testing.T) { - srv := httptest.NewServer(Serve(NewFakeDataSource())) - defer srv.Close() - - resp, err := http.Get(srv.URL + "/api/runs") - if err != nil { - t.Fatalf("GET /api/runs: %v", err) - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK { - t.Fatalf("status = %d, want 200", resp.StatusCode) - } - if ct := resp.Header.Get("Content-Type"); !strings.HasPrefix(ct, "application/json") { - t.Fatalf("Content-Type = %q, want application/json", ct) - } - - var runs []RunSummary - if err := json.NewDecoder(resp.Body).Decode(&runs); err != nil { - t.Fatalf("decode: %v", err) - } - if len(runs) != 1 { - t.Fatalf("len(runs) = %d, want 1", len(runs)) - } - if runs[0].RunID != fakeRunID { - t.Fatalf("runs[0].RunID = %q, want %q", runs[0].RunID, fakeRunID) - } - if runs[0].Title == "" || runs[0].State == "" { - t.Fatalf("run summary missing title/state: %+v", runs[0]) - } -} - func TestHandleWorkflowUnsupportedDataSource(t *testing.T) { ds := noWorkflowDataSource{DataSource: NewFakeDataSource()} srv := httptest.NewServer(Serve(ds)) @@ -500,10 +468,10 @@ func TestFakeOrgContractAndDeterminism(t *testing.T) { if len(org.Escalations) != 2 || org.Health.OpenEscalations != len(org.Escalations) { t.Fatalf("org escalations health=%+v escalations=%+v", org.Health, org.Escalations) } - if len(org.Feed) != 3 { + if len(org.Feed) != 2 { t.Fatalf("org feed = %+v", org.Feed) } - allowedKinds := map[string]bool{"blocked_since": true, "recycle_overdue": true, "recycle": true} + allowedKinds := map[string]bool{"blocked_since": true, "recycle": true} for _, row := range org.Feed { if !allowedKinds[row.Kind] || row.Role == "" || row.At == "" { t.Fatalf("invalid org feed row = %+v", row) @@ -690,14 +658,6 @@ func TestHandleWorkflowClampsCursorsAndLimits(t *testing.T) { func TestFakeWorkflowContractAndPagination(t *testing.T) { t.Setenv("FAKEFEED_WORKFLOWS", "1") ds := NewFakeDataSource() - - state, err := ds.State(context.Background(), fakeRunID) - if err != nil { - t.Fatalf("State: %v", err) - } - if state.Workflow != fakeWorkflow { - t.Fatalf("State.Workflow = %q, want %q", state.Workflow, fakeWorkflow) - } graph, err := ds.Graph(context.Background(), "") if err != nil { t.Fatalf("Graph: %v", err) @@ -768,6 +728,7 @@ func TestFakeWorkflowContractAndPagination(t *testing.T) { } func TestFakeGalaxyAgentRecordCoverage(t *testing.T) { + const panelAgent = "project-lead" ds := NewFakeDataSource() agents, err := ds.Agents(context.Background()) if err != nil { @@ -783,7 +744,7 @@ func TestFakeGalaxyAgentRecordCoverage(t *testing.T) { t.Fatalf("Graph: %v", err) } seenPanelAgent := false - seenEphemeral := false + seenUnregistered := false for _, node := range graph.Nodes { if node.Type != "agent" { continue @@ -793,55 +754,55 @@ func TestFakeGalaxyAgentRecordCoverage(t *testing.T) { name = node.Label } _, registered := byName[name] - if name == fakeEphemeralAgent { - seenEphemeral = true + if name == fakeUnregisteredName { + seenUnregistered = true if registered { - t.Fatalf("ephemeral galaxy agent %q unexpectedly has an AgentSummary", name) + t.Fatalf("unregistered galaxy agent %q unexpectedly has an AgentSummary", name) } continue } if !registered { t.Fatalf("registered galaxy agent %q has no matching AgentSummary", name) } - if name == fakeGalaxyPanelAgent { + if name == panelAgent { seenPanelAgent = true } } if !seenPanelAgent { - t.Fatalf("graph missing full-panel fixture agent %q", fakeGalaxyPanelAgent) + t.Fatalf("graph missing full-panel fixture agent %q", panelAgent) } - if !seenEphemeral { - t.Fatalf("graph missing ephemeral-panel fixture agent %q", fakeEphemeralAgent) + if !seenUnregistered { + t.Fatalf("graph missing unregistered-panel fixture agent %q", fakeUnregisteredName) } - if _, err := ds.Agent(context.Background(), fakeEphemeralAgent); err != ErrAgentNotFound { - t.Fatalf("Agent(%q) error = %v, want %v", fakeEphemeralAgent, err, ErrAgentNotFound) + if _, err := ds.Agent(context.Background(), fakeUnregisteredName); err != ErrAgentNotFound { + t.Fatalf("Agent(%q) error = %v, want %v", fakeUnregisteredName, err, ErrAgentNotFound) } - detail, err := ds.Agent(context.Background(), fakeGalaxyPanelAgent) + detail, err := ds.Agent(context.Background(), panelAgent) if err != nil { - t.Fatalf("Agent(%q): %v", fakeGalaxyPanelAgent, err) + t.Fatalf("Agent(%q): %v", panelAgent, err) } - if detail.Template == nil || len(detail.Versions) == 0 || !detail.Versions[0].Current { - t.Fatalf("full-panel fixture missing template/current version: %+v", detail) + if detail.Config == nil { + t.Fatalf("full-panel fixture missing config section: %+v", detail) } jobs, err := ds.Jobs(context.Background()) if err != nil { t.Fatalf("Jobs: %v", err) } - var hasTokenJob, hasEphemeralTokenJob bool + var hasTokenJob, hasUnregisteredTokenJob bool for _, job := range jobs { - if job.Agent == fakeGalaxyPanelAgent && job.TokensIn > 0 && job.TokensOut > 0 { + if job.Agent == panelAgent && job.TokensIn > 0 && job.TokensOut > 0 { hasTokenJob = true } - if job.Agent == fakeEphemeralAgent && job.TokensIn > 0 && job.TokensOut > 0 { - hasEphemeralTokenJob = true + if job.Agent == fakeUnregisteredName && job.TokensIn > 0 && job.TokensOut > 0 { + hasUnregisteredTokenJob = true } } if !hasTokenJob { - t.Fatalf("full-panel fixture agent %q has no token-bearing job", fakeGalaxyPanelAgent) + t.Fatalf("full-panel fixture agent %q has no token-bearing job", panelAgent) } - if !hasEphemeralTokenJob { - t.Fatalf("ephemeral-panel fixture agent %q has no token-bearing job", fakeEphemeralAgent) + if !hasUnregisteredTokenJob { + t.Fatalf("unregistered-panel fixture agent %q has no token-bearing job", fakeUnregisteredName) } } @@ -938,13 +899,6 @@ func TestWorkflowSummaryJSONContract(t *testing.T) { func TestFakeWorkflowDisabled(t *testing.T) { t.Setenv("FAKEFEED_WORKFLOWS", "0") ds := NewFakeDataSource() - state, err := ds.State(context.Background(), fakeRunID) - if err != nil { - t.Fatalf("State: %v", err) - } - if state.Workflow != "" { - t.Fatalf("disabled State.Workflow = %q", state.Workflow) - } graph, err := ds.Graph(context.Background(), "") if err != nil { t.Fatalf("Graph: %v", err) @@ -1035,22 +989,10 @@ func TestHandleAgents(t *testing.T) { t.Fatalf("len(agents) = 0, want > 0") } - var ephemeral, registered int for _, a := range agents { if a.Name == "" || a.Runtime == "" { t.Fatalf("agent missing name/runtime: %+v", a) } - if a.Ephemeral { - ephemeral++ - } else { - registered++ - } - } - if ephemeral != 1 { - t.Fatalf("ephemeral rollup rows = %d, want exactly 1", ephemeral) - } - if registered == 0 { - t.Fatalf("registered agents = 0, want > 0") } } @@ -1058,85 +1000,29 @@ func TestHandleAgent(t *testing.T) { srv := httptest.NewServer(Serve(NewFakeDataSource())) defer srv.Close() - // The one seeded agent that carries a template + version history. + // researcher carries an [agents.] config section. var detail AgentDetail - if err := json.Unmarshal(getRaw(t, srv.URL+"/api/agent/"+fakeTemplatedAgent), &detail); err != nil { + if err := json.Unmarshal(getRaw(t, srv.URL+"/api/agent/researcher"), &detail); err != nil { t.Fatalf("decode: %v", err) } - if detail.Name != fakeTemplatedAgent { - t.Fatalf("detail.Name = %q, want %q", detail.Name, fakeTemplatedAgent) + if detail.Name != "researcher" { + t.Fatalf("detail.Name = %q, want researcher", detail.Name) } if detail.Runtime == "" { t.Fatalf("detail missing embedded summary (runtime): %+v", detail) } - if detail.Template == nil { - t.Fatalf("expected a template for %q", fakeTemplatedAgent) - } - if detail.Template.ID == "" { - t.Fatalf("template missing id: %+v", detail.Template) - } - // The seeded templated agent carries an [agents.] config section, so the - // detail's config projection is exercised. if detail.Config == nil { - t.Fatalf("expected a config section for %q", fakeTemplatedAgent) - } - // Pool knobs are populated (parse-time defaults are folded in server-side). - if detail.Config.MaxBackground == 0 || detail.Config.IdleTimeout == "" || detail.Config.JobTimeout == "" { - t.Fatalf("expected config pool knobs populated: %+v", detail.Config) - } - // The per-agent detail carries the template's full prompt body (multi-line). - if !strings.Contains(detail.Template.Content, "\n") || !strings.Contains(detail.Template.Content, "Researcher agent") { - t.Fatalf("template content missing/not the multi-line prompt body: %q", detail.Template.Content) + t.Fatalf("expected a config section for researcher") } - if len(detail.Versions) != 3 { - t.Fatalf("len(Versions) = %d, want 3", len(detail.Versions)) - } - // Newest first, exactly one Current marker, states cover current/canary/ - // pending, the canary version carries a sample, and every version carries its - // own full prompt body — distinct per version so the content viewer is exercised. - var current, canarySample int - states := map[string]bool{} - seenContent := map[string]bool{} - for i, v := range detail.Versions { - if v.ID == "" || v.State == "" { - t.Fatalf("version missing id/state: %+v", v) - } - if v.Content == "" { - t.Fatalf("version %s missing content body: %+v", v.ID, v) - } - if seenContent[v.Content] { - t.Fatalf("versions share identical content (expected distinct per version): %+v", detail.Versions) - } - seenContent[v.Content] = true - states[v.State] = true - if i > 0 && detail.Versions[i-1].Number < v.Number { - t.Fatalf("versions not newest-first: %+v", detail.Versions) - } - if v.Current { - current++ - } - if v.State == "canary" && v.CanarySample > 0 { - canarySample++ - } - } - if current != 1 { - t.Fatalf("Current markers = %d, want exactly 1", current) - } - if canarySample == 0 { - t.Fatalf("expected a canary version with a CanarySample: %+v", detail.Versions) - } - for _, want := range []string{"current", "canary", "pending"} { - if !states[want] { - t.Fatalf("versions missing state %q: %+v", want, detail.Versions) - } + if detail.Config.JobTimeout == "" || detail.Config.Model == "" { + t.Fatalf("expected config knobs populated: %+v", detail.Config) } } -func TestHandleAgentNoTemplate(t *testing.T) { +func TestHandleAgentWithoutConfig(t *testing.T) { srv := httptest.NewServer(Serve(NewFakeDataSource())) defer srv.Close() - // An agent without a template: Template omitted, Versions still non-nil. var detail AgentDetail if err := json.Unmarshal(getRaw(t, srv.URL+"/api/agent/ci-runner"), &detail); err != nil { t.Fatalf("decode: %v", err) @@ -1144,15 +1030,6 @@ func TestHandleAgentNoTemplate(t *testing.T) { if detail.Name != "ci-runner" { t.Fatalf("detail.Name = %q, want ci-runner", detail.Name) } - if detail.Template != nil { - t.Fatalf("expected nil template for ci-runner: %+v", detail.Template) - } - if detail.Versions == nil { - t.Fatalf("Versions must be non-nil even without a template") - } - if len(detail.Versions) != 0 { - t.Fatalf("len(Versions) = %d, want 0 for a template-less agent", len(detail.Versions)) - } // ci-runner has no [agents.] section, so its detail omits Config entirely. if detail.Config != nil { t.Fatalf("expected nil config for ci-runner (no config section): %+v", detail.Config) @@ -1178,102 +1055,14 @@ func TestHandleAgentDeterministic(t *testing.T) { srv := httptest.NewServer(Serve(NewFakeDataSource())) defer srv.Close() - // fakeTemplatedAgent does not appear in the seeded run, so its detail (summary - // + template + versions) is byte-stable across calls. - url := srv.URL + "/api/agent/" + fakeTemplatedAgent + // researcher does not appear in the seeded run, so its detail (summary + + // config) is byte-stable across calls. + url := srv.URL + "/api/agent/researcher" if a, b := getRaw(t, url), getRaw(t, url); !bytes.Equal(a, b) { t.Fatalf("agent detail not byte-stable across calls\nfirst: %s\nsecond: %s", a, b) } } -func TestHandleState(t *testing.T) { - srv := httptest.NewServer(Serve(NewFakeDataSource())) - defer srv.Close() - - resp, err := http.Get(srv.URL + "/api/state?run=" + fakeRunID) - if err != nil { - t.Fatalf("GET /api/state: %v", err) - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK { - t.Fatalf("status = %d, want 200", resp.StatusCode) - } - - var st State - if err := json.NewDecoder(resp.Body).Decode(&st); err != nil { - t.Fatalf("decode: %v", err) - } - if st.RunID != fakeRunID { - t.Fatalf("RunID = %q, want %q", st.RunID, fakeRunID) - } - if len(st.Nodes) != 6 { - t.Fatalf("len(Nodes) = %d, want 6 (coordinator + 3 implement + integrate + synthesize)", len(st.Nodes)) - } - - // Verify the graph shape: root, three parallel children, an integrate node - // depending on all three, and a synthesize continuation depending on it. - byID := map[string]Node{} - for _, n := range st.Nodes { - byID[n.ID] = n - } - root, ok := byID["job-1"] - if !ok || root.ParentID != "" || root.Depth != 0 { - t.Fatalf("root node malformed: %+v", root) - } - for _, id := range []string{"job-2", "job-3", "job-4"} { - n := byID[id] - if n.ParentID != "job-1" || n.Depth != 1 { - t.Fatalf("child %s malformed: %+v", id, n) - } - } - integ := byID["job-5"] - if len(integ.Deps) != 3 { - t.Fatalf("integrate node deps = %v, want 3", integ.Deps) - } - synth := byID["job-6"] - if len(synth.Deps) != 1 || synth.Deps[0] != "job-5" { - t.Fatalf("synthesize node deps = %v, want [job-5]", synth.Deps) - } -} - -func TestHandleStateEmptyRunReturnsActive(t *testing.T) { - srv := httptest.NewServer(Serve(NewFakeDataSource())) - defer srv.Close() - - resp, err := http.Get(srv.URL + "/api/state") - if err != nil { - t.Fatalf("GET /api/state: %v", err) - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK { - t.Fatalf("status = %d, want 200", resp.StatusCode) - } - var st State - if err := json.NewDecoder(resp.Body).Decode(&st); err != nil { - t.Fatalf("decode: %v", err) - } - if st.RunID != fakeRunID { - t.Fatalf("empty run should resolve to active run; got %q", st.RunID) - } -} - -func TestHandleStateNotFound(t *testing.T) { - srv := httptest.NewServer(Serve(NewFakeDataSource())) - defer srv.Close() - - resp, err := http.Get(srv.URL + "/api/state?run=does-not-exist") - if err != nil { - t.Fatalf("GET /api/state: %v", err) - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusNotFound { - t.Fatalf("status = %d, want 404", resp.StatusCode) - } -} - func TestHandleJob(t *testing.T) { srv := httptest.NewServer(Serve(NewFakeDataSource())) defer srv.Close() @@ -1523,19 +1312,19 @@ func TestHandleHealth(t *testing.T) { if u := h.Update.ReleaseURL; u != "" && !strings.HasPrefix(u, "https://") && !strings.HasPrefix(u, "http://") { t.Fatalf("update release url not http(s): %q", u) } - if h.Locks == nil || h.ResourceLocks == nil || h.Stuck == nil || h.RecentFailures == nil { + if h.ResourceLocks == nil || h.Stuck == nil || h.RecentFailures == nil { t.Fatalf("health slices must be non-nil: %+v", h) } if len(h.Stuck) == 0 { t.Fatalf("expected at least one stuck job") } - if len(h.Locks) == 0 { - t.Fatalf("expected at least one branch lock") + if len(h.ResourceLocks) == 0 { + t.Fatalf("expected at least one resource lock") } - // Locks oldest first. - for i := 1; i < len(h.Locks); i++ { - if h.Locks[i-1].AcquiredAt > h.Locks[i].AcquiredAt { - t.Fatalf("locks not oldest-first: %+v", h.Locks) + // Resource locks oldest first. + for i := 1; i < len(h.ResourceLocks); i++ { + if h.ResourceLocks[i-1].AcquiredAt > h.ResourceLocks[i].AcquiredAt { + t.Fatalf("resource locks not oldest-first: %+v", h.ResourceLocks) } } // Stuck oldest first. @@ -1593,8 +1382,6 @@ func TestHandleConfig(t *testing.T) { }{ "github.rate_limit_per_hour": {"4200", "4500", "int", false}, "orchestrate.blocked_ttl": {`"30m"`, `"30m"`, "duration", true}, - "skillopt.auto_promote": {"false", "false", "flag", true}, - "skillopt.pace_gate": {"true", "true", "flag", true}, } gotKnobs := map[string]bool{} for si, section := range cfg.Sections { @@ -1835,131 +1622,13 @@ func assertContinuousDays(t *testing.T, days []ChartDay) { } } -// realSkillStates is the set of version states the live SkillOpt store can emit; -// the fake feed must never present a state outside it. Promotion writes 'current' -// to the live version and 'superseded' to the one it replaces (store.go -// PromoteAgentTemplateVersion); there is NO 'promoted' version state. Candidates -// are 'pending' or an in-flight 'canary', and a declined candidate is 'rejected'. -var realSkillStates = map[string]bool{"current": true, "superseded": true, "canary": true, "pending": true, "rejected": true} - -func TestHandleLearningSkills(t *testing.T) { - srv := httptest.NewServer(Serve(NewFakeDataSource())) - defer srv.Close() - - var skills Skills - if err := json.Unmarshal(getRaw(t, srv.URL+"/api/learning/skills"), &skills); err != nil { - t.Fatalf("decode: %v", err) - } - if skills.Templates == nil { - t.Fatalf("Templates must be non-nil: %+v", skills) - } - if n := len(skills.Templates); n < 2 || n > 3 { - t.Fatalf("len(Templates) = %d, want 2..3", n) - } - - var canaryTemplates, pendingSum, roseFive int - for _, tpl := range skills.Templates { - if tpl.TemplateID == "" { - t.Fatalf("template missing id: %+v", tpl) - } - if tpl.Versions == nil || len(tpl.Versions) == 0 { - t.Fatalf("template %s has no versions", tpl.TemplateID) - } - if tpl.Pending == nil { - t.Fatalf("template %s Pending must be non-nil", tpl.TemplateID) - } - // Versions ascending by Number (sparkline order); states real-emittable; - // scored versions carry HasScore. - var scored []float64 - for i, v := range tpl.Versions { - if !realSkillStates[v.State] { - t.Fatalf("template %s version %d has non-real state %q", tpl.TemplateID, v.Number, v.State) - } - if i > 0 && tpl.Versions[i-1].Number >= v.Number { - t.Fatalf("template %s versions not ascending by Number: %+v", tpl.TemplateID, tpl.Versions) - } - if v.HasScore { - scored = append(scored, v.Score) - } - } - if tpl.CanarySample > 0 { - canaryTemplates++ - } - pendingSum += len(tpl.Pending) - // The healthy template is the 5-version one with a strictly rising score. - if len(tpl.Versions) == 5 { - rising := len(scored) == 5 - for i := 1; i < len(scored); i++ { - if scored[i] <= scored[i-1] { - rising = false - } - } - if rising { - roseFive++ - } - } - // Every pending candidate must map to a real version number in the template. - for _, c := range tpl.Pending { - if c.VersionID == "" { - t.Fatalf("pending candidate missing versionId: %+v", c) - } - found := false - for _, v := range tpl.Versions { - if v.Number == c.Number { - found = true - } - } - if !found { - t.Fatalf("pending candidate %d not among template %s versions", c.Number, tpl.TemplateID) - } - } - } - if roseFive != 1 { - t.Fatalf("want exactly one healthy 5-version rising-score template, got %d", roseFive) - } - if canaryTemplates == 0 { - t.Fatalf("expected at least one template with an active canary") - } - if skills.ActiveCanaries != canaryTemplates { - t.Fatalf("ActiveCanaries = %d, want %d (templates with CanarySample>0)", skills.ActiveCanaries, canaryTemplates) - } - if skills.PendingTotal != pendingSum { - t.Fatalf("PendingTotal = %d, want %d (sum of per-template Pending)", skills.PendingTotal, pendingSum) - } - if skills.PendingTotal == 0 { - t.Fatalf("expected at least one pending candidate") - } - - // Sort order: pending-first, then most-recently-promoted (LastPromotedAt desc). - for i := 1; i < len(skills.Templates); i++ { - a, b := skills.Templates[i-1], skills.Templates[i] - ap, bp := len(a.Pending) > 0, len(b.Pending) > 0 - if !ap && bp { - t.Fatalf("templates not pending-first: %q(pending=%v) before %q(pending=%v)", a.TemplateID, ap, b.TemplateID, bp) - } - if ap == bp && a.LastPromotedAt < b.LastPromotedAt { - t.Fatalf("templates not most-recently-promoted within group: %q(%d) before %q(%d)", a.TemplateID, a.LastPromotedAt, b.TemplateID, b.LastPromotedAt) - } - } -} - -func TestHandleLearningSkillsDeterministic(t *testing.T) { - srv := httptest.NewServer(Serve(NewFakeDataSource())) - defer srv.Close() - - url := srv.URL + "/api/learning/skills" - if a, b := getRaw(t, url), getRaw(t, url); !bytes.Equal(a, b) { - t.Fatalf("skills not byte-stable across calls\nfirst: %s\nsecond: %s", a, b) - } -} - -func TestStateResponseIsIndented(t *testing.T) { +func TestJSONResponseIsIndented(t *testing.T) { srv := httptest.NewServer(Serve(NewFakeDataSource())) defer srv.Close() - resp, err := http.Get(srv.URL + "/api/state") + resp, err := http.Get(srv.URL + "/api/job/job-1") if err != nil { - t.Fatalf("GET /api/state: %v", err) + t.Fatalf("GET /api/job/job-1: %v", err) } defer resp.Body.Close() diff --git a/datasource.go b/datasource.go index 3de0815..c635783 100644 --- a/datasource.go +++ b/datasource.go @@ -1,17 +1,17 @@ // Package dashboard provides the read-only backend for the gitmoot web -// dashboard: a small HTTP server that serves a live orchestration-DAG UI and +// dashboard: a small HTTP server that serves the embedded dashboard UI and // reads live state from gitmoot's store through the DataSource interface. package dashboard import "context" -// NodeState is the lifecycle state of a single orchestration node. +// NodeState is the lifecycle state of a single job. type NodeState string // "queued" "running" "succeeded" "failed" "blocked" "cancelled" // Event is a label attached to a node's timeline. // // Wall-clock timestamps in this package (Node.StartedAt/EndedAt, -// RunSummary.Updated) are epoch milliseconds (JS Date compatible). Event.T is a +// JobSummary.Updated) are epoch milliseconds (JS Date compatible). Event.T is a // monotonic ordering key for a node's timeline: epoch milliseconds where the // feed has per-event times, or a 1-based index otherwise. Clients should sort a // node's events by T rather than treat it as wall-clock. @@ -20,8 +20,7 @@ type Event struct { Label string `json:"label"` } -// Node is a single job/agent in the orchestration graph. Edges are derived -// client-side from ParentID + Deps. +// Node is the full detail of a single job served by GET /api/job/{id}. type Node struct { ID string `json:"id"` ParentID string `json:"parentId,omitempty"` @@ -41,45 +40,9 @@ type Node struct { Events []Event `json:"events"` } -// State is a snapshot of one orchestration run. -type State struct { - RunID string `json:"runId"` - Title string `json:"title"` - Workflow string `json:"workflow,omitempty"` // root workflow label, when this run is labeled - Nodes []Node `json:"nodes"` // edges are derived client-side from ParentID + Deps -} - -// RunSummary is a lightweight listing entry for a run. Beyond identity/state it -// carries enough shape for the Runs column to group, disambiguate, and search -// runs without fetching each run's full graph: kind/significance drive the -// Active/Orchestrations/one-shots sectioning, and the counts/snippet -// disambiguate same-titled runs. -type RunSummary struct { - RunID string `json:"runId"` - Title string `json:"title"` - State NodeState `json:"state"` - // Kind is the run's entrypoint: ask | review | implement | orchestrate | goal. - Kind string `json:"kind,omitempty"` - // Significance is "orchestration" for a multi-node delegation tree, else - // "one-shot" for a single-node ask/review (used to fold noise in the UI). - Significance string `json:"significance,omitempty"` - Agent string `json:"agent,omitempty"` // coordinator/agent name - Repo string `json:"repo,omitempty"` - PR int `json:"pr,omitempty"` - NodeCount int `json:"nodeCount"` // jobs in the run tree - Depth int `json:"depth"` // delegation levels (1-based) - DoneCount int `json:"doneCount"` // finished (terminal) nodes - Snippet string `json:"snippet,omitempty"` // first line of the root prompt - Started int64 `json:"started,omitempty"` // epoch milliseconds - Updated int64 `json:"updated"` // epoch milliseconds - Duration int64 `json:"duration,omitempty"` // milliseconds (updated-started) -} - -// JobSummary is a flattened listing entry for a single job, across every run. -// Unlike RunSummary (which rolls a whole delegation tree into one row) each -// JobSummary is one node in one run, carrying enough identity/timing/state for -// the Jobs page to list, group by Run, and search without fetching each run's -// full graph. +// JobSummary is a flattened listing entry for a single job, across every run, +// carrying enough identity/timing/state for the Jobs page to list, group by +// Run, and search without fetching each job's full detail. type JobSummary struct { ID string `json:"id"` Title string `json:"title"` // first line of prompt, fallback id @@ -89,7 +52,7 @@ type JobSummary struct { // Kind is the job's action/type: ask | review | implement | ... Kind string `json:"kind,omitempty"` State NodeState `json:"state"` - Depth int `json:"depth"` // delegation depth (0 = root) + Depth int `json:"depth"` // job-tree depth (0 = root) Run string `json:"run,omitempty"` // root/run id this job belongs to PR int `json:"pr,omitempty"` Started int64 `json:"started,omitempty"` // epoch ms @@ -100,8 +63,7 @@ type JobSummary struct { } // AgentSummary is a listing entry for the Agents page: one row per registered -// agent, plus a single synthetic rollup row for the fleet of ephemeral workers. -// The counts aggregate every job the agent has run across all runs. +// agent. The counts aggregate every job the agent has run across all runs. type AgentSummary struct { Name string `json:"name"` Role string `json:"role,omitempty"` @@ -111,71 +73,27 @@ type AgentSummary struct { Capabilities []string `json:"capabilities,omitempty"` AutonomyPolicy string `json:"autonomyPolicy,omitempty"` Health string `json:"health,omitempty"` - // Ephemeral is true only for the synthetic ephemeral-workers rollup row. - Ephemeral bool `json:"ephemeral,omitempty"` - JobCount int `json:"jobCount"` - RunningCount int `json:"runningCount"` - SucceededCount int `json:"succeededCount"` - FailedCount int `json:"failedCount"` - LastActive int64 `json:"lastActive,omitempty"` // epoch ms of most recent job update -} - -// TemplateVersionInfo is one entry in an agent template's version history. -type TemplateVersionInfo struct { - ID string `json:"id"` - Number int `json:"number"` - State string `json:"state"` // e.g. promoted | pending | canary | rejected (pass through the store's value) - Name string `json:"name,omitempty"` - Description string `json:"description,omitempty"` - SourceRef string `json:"sourceRef,omitempty"` - ResolvedCommit string `json:"resolvedCommit,omitempty"` - CreatedAt int64 `json:"createdAt,omitempty"` // epoch ms, 0 unknown - PromotedAt int64 `json:"promotedAt,omitempty"` // epoch ms, 0 unknown - CanarySample float64 `json:"canarySample,omitempty"` - Current bool `json:"current,omitempty"` // true for the version the template currently resolves to - // Content is this version's full prompt text (the template body captured at - // this version). Can be multi-KB; served only by the per-agent detail endpoint. - Content string `json:"content,omitempty"` -} - -// AgentTemplateInfo describes the template an agent is instantiated from: its -// identity and where its definition is sourced/resolved from. -type AgentTemplateInfo struct { - ID string `json:"id"` - Name string `json:"name,omitempty"` - Description string `json:"description,omitempty"` - SourceRepo string `json:"sourceRepo,omitempty"` - SourceRef string `json:"sourceRef,omitempty"` - SourcePath string `json:"sourcePath,omitempty"` - ResolvedCommit string `json:"resolvedCommit,omitempty"` - // Content is the agent's full prompt text (the currently resolved template - // body). Can be multi-KB; served only by the per-agent detail endpoint. - Content string `json:"content,omitempty"` + JobCount int `json:"jobCount"` + RunningCount int `json:"runningCount"` + SucceededCount int `json:"succeededCount"` + FailedCount int `json:"failedCount"` + LastActive int64 `json:"lastActive,omitempty"` // epoch ms of most recent job update } // AgentConfigInfo holds the agent's [agents.] config-section values as // resolved at config parse time (parse-time defaults included, so a field can be // populated even when the section did not set it explicitly). These are -// configured knobs, not live runtime state: the pool knobs (MaxBackground, -// IdleTimeout, JobTimeout) only take effect for the managed instances / temp -// workers gitmoot spins up for this agent, so they do not describe a one-off -// foreground invocation. +// configured knobs, not live runtime state. type AgentConfigInfo struct { - MaxBackground int `json:"maxBackground,omitempty"` - IdleTimeout string `json:"idleTimeout,omitempty"` - JobTimeout string `json:"jobTimeout,omitempty"` - Model string `json:"model,omitempty"` - Template string `json:"template,omitempty"` - Capabilities []string `json:"capabilities,omitempty"` + JobTimeout string `json:"jobTimeout,omitempty"` + Model string `json:"model,omitempty"` + Capabilities []string `json:"capabilities,omitempty"` } // AgentDetail is the Agents page's click-through detail: the summary plus the -// agent's template and its version history (newest first). Template is nil for -// agents with no template. +// agent's config section. type AgentDetail struct { AgentSummary - Template *AgentTemplateInfo `json:"template,omitempty"` - Versions []TemplateVersionInfo `json:"versions"` // Config is the agent's [agents.] config section, or nil when the agent // has no such section. Config *AgentConfigInfo `json:"config,omitempty"` @@ -200,8 +118,8 @@ type GraphNode struct { TokensOut int `json:"tokensOut,omitempty"` } -// GraphLink is an edge in the galaxy graph. Kind is "parent"/"dep" (delegation -// and sibling links between jobs), "repo" (job -> its repo hub), "agent" +// GraphLink is an edge in the galaxy graph. Kind is "parent"/"dep" (parent and +// dependency links between jobs), "repo" (job -> its repo hub), "agent" // (job -> its agent hub), or "workflow" (labeled job -> its workflow hub). type GraphLink struct { Source string `json:"source"` @@ -532,7 +450,7 @@ type OrgEscalation struct { // OrgFeedRow is one organization signal. type OrgFeedRow struct { - Kind string `json:"kind"` // blocked_since | recycle_overdue | recycle + Kind string `json:"kind"` // blocked_since | recycle Role string `json:"role"` At string `json:"at"` // RFC3339 Since string `json:"since,omitempty"` @@ -658,7 +576,6 @@ type ConfigAgent struct { Model string `json:"model"` Capabilities []string `json:"capabilities"` AutonomyPolicy string `json:"autonomy_policy"` - MaxBackground int `json:"max_background"` } // KeychainView is the metadata-only registry projection shown on the Config @@ -704,7 +621,7 @@ type ConfigSnapshot struct { Keychain KeychainView `json:"keychain"` } -// HealthDaemon reports the orchestration daemon's liveness. +// HealthDaemon reports the gitmoot daemon's liveness. type HealthDaemon struct { Running bool `json:"running"` PID int `json:"pid,omitempty"` @@ -732,15 +649,7 @@ type HealthTotals struct { Cancelled int `json:"cancelled"` } -// HealthLock is a held branch/checkout lock. -type HealthLock struct { - Repo string `json:"repo"` - Branch string `json:"branch"` - Owner string `json:"owner"` - AcquiredAt int64 `json:"acquiredAt,omitempty"` // epoch ms -} - -// HealthResourceLock is a held non-branch resource lock (runtime session, etc.). +// HealthResourceLock is a held resource lock (runtime session, etc.). type HealthResourceLock struct { Key string `json:"key"` Owner string `json:"owner,omitempty"` @@ -769,63 +678,16 @@ type HealthFailure struct { } // Health is the data behind the Health page: daemon liveness, fleet totals, held -// locks, wedged jobs and recent failures. +// resource locks, wedged jobs and recent failures. type Health struct { Daemon HealthDaemon `json:"daemon"` Update *HealthUpdate `json:"update,omitempty"` // daemon-binary update check; nil when unavailable Totals HealthTotals `json:"totals"` - Locks []HealthLock `json:"locks"` // branch/checkout locks, oldest first ResourceLocks []HealthResourceLock `json:"resourceLocks"` // runtime-session etc., oldest first Stuck []HealthStuckJob `json:"stuck"` // blocked jobs + queued older than 10 min, oldest first RecentFailures []HealthFailure `json:"recentFailures"` // last 10 failed, newest first } -// Skills — the SkillOpt evolution overview. - -// SkillVersion is one version in a skill template's evolution history, in -// sparkline order. Score is present only when the version was scored (HasScore). -type SkillVersion struct { - Number int `json:"number"` - State string `json:"state"` - Score float64 `json:"score,omitempty"` - HasScore bool `json:"hasScore,omitempty"` - CreatedAt int64 `json:"createdAt,omitempty"` // epoch ms - PromotedAt int64 `json:"promotedAt,omitempty"` -} - -// SkillCandidate is a pending version awaiting review/promotion. Score is passed -// through in the review's stored string form (e.g. a decimal or a verdict). -type SkillCandidate struct { - VersionID string `json:"versionId"` - Number int `json:"number"` - Score string `json:"score,omitempty"` // pass through the review's stored form - CreatedAt int64 `json:"createdAt,omitempty"` -} - -// SkillTemplate is one skill template's evolution: its version history (for the -// sparkline), the version it currently resolves to, any in-flight canary, and -// its pending candidates. -type SkillTemplate struct { - TemplateID string `json:"templateId"` - Name string `json:"name,omitempty"` - Agents []string `json:"agents,omitempty"` // registered agents using it, sorted - Versions []SkillVersion `json:"versions"` // ascending by Number (sparkline order) - CurrentVersion int `json:"currentVersion,omitempty"` - CurrentState string `json:"currentState,omitempty"` - CanarySample float64 `json:"canarySample,omitempty"` - CanaryStartedAt int64 `json:"canaryStartedAt,omitempty"` - LastPromotedAt int64 `json:"lastPromotedAt,omitempty"` - Pending []SkillCandidate `json:"pending"` -} - -// Skills is the data behind the Learning page's Skills view: every skill -// template's evolution, plus range rollups for the header. -type Skills struct { - Templates []SkillTemplate `json:"templates"` // sorted: pending-first, then most-recently-promoted - ActiveCanaries int `json:"activeCanaries"` - PendingTotal int `json:"pendingTotal"` -} - // Attention + result checks — surfacing evaluator output and human gates where a // human (or the planned Slack/media bridge, gitmoot #519) manages work // (gitmoot #528). @@ -885,17 +747,16 @@ type ChangeCursorDataSource interface { // DataSource is the read-only feed the dashboard renders. Implementations must // be safe for concurrent use. type DataSource interface { - Runs(ctx context.Context) ([]RunSummary, error) - State(ctx context.Context, runID string) (State, error) // runID "" => active/most-recent + // Job returns one job's full detail. Unknown ids return a not-found + // sentinel (mapped to 404 by the API layer). Job(ctx context.Context, jobID string) (Node, error) // Jobs returns every job across all runs, flattened, sorted Updated desc. Jobs(ctx context.Context) ([]JobSummary, error) - // Agents returns the registered agents plus one synthetic rollup row for the - // fleet of ephemeral workers (the row with Ephemeral == true). + // Agents returns the registered agents. Agents(ctx context.Context) ([]AgentSummary, error) // Agent returns the click-through detail for a single agent by name: its - // summary plus template and version history. Unknown names return a - // not-found sentinel (mapped to 404 by the API layer). + // summary plus config section. Unknown names return a not-found sentinel + // (mapped to 404 by the API layer). Agent(ctx context.Context, name string) (AgentDetail, error) // Graph returns the whole-history galaxy graph. Empty repo => all runs; a // non-empty repo scopes to that repository's jobs (and their hubs). @@ -905,7 +766,7 @@ type DataSource interface { // days of 0 means all history. Days is oldest->newest and zero-filled // continuous across the whole window. Charts(ctx context.Context, days int) (Charts, error) - // Health returns the daemon liveness, fleet totals, held locks, wedged jobs + // Health returns the daemon liveness, fleet totals, held resource locks, wedged jobs // and recent failures behind the Health page. Health(ctx context.Context) (Health, error) // Config returns the versioned, sanitized effective configuration behind the @@ -913,17 +774,6 @@ type DataSource interface { // by name only, and the keychain projection contains metadata only. Sections, // knobs, agents, unknown keys and keychain rows are deterministic. Config(ctx context.Context) (ConfigSnapshot, error) - // Skills returns the template evolution overview behind the Learning page's - // Skills view: per-template version history, active canaries and pending - // candidates. Ordering must be deterministic (the UI polls with a - // signature-skip): templates pending-first then most-recently-promoted, each - // template's versions ascending by Number. - // - // Skills is REQUIRED (gitmoot #2206) while Knowledge and ChatThreads left this - // interface: gitmoot serves Skills from its surviving agent-template tables, so - // the page renders real data, whereas Knowledge and Chat were backed by tables - // gitmoot retired and could only ever answer empty. - Skills(ctx context.Context) (Skills, error) // Attention returns the "Needs a human" view (gitmoot #528): blocked job // gates and the headline waiting total. Ordering must be deterministic (the // UI polls with a signature-skip). @@ -932,5 +782,4 @@ type DataSource interface { // #711): the deterministic result checks its result failed plus the home-wide // policy mode. An unknown job yields an empty Failed with the resolved Mode. JobChecks(ctx context.Context, jobID string) (JobChecks, error) - Subscribe(ctx context.Context, runID string) (<-chan State, func(), error) // for SSE } diff --git a/datasource_test.go b/datasource_test.go index 92f8f79..dd1a1e6 100644 --- a/datasource_test.go +++ b/datasource_test.go @@ -7,7 +7,7 @@ import ( ) func TestWorkflowAdditiveJSONContracts(t *testing.T) { - t.Run("legacy graph node and state omit workflow fields", func(t *testing.T) { + t.Run("legacy graph node omits workflow fields", func(t *testing.T) { node := GraphNode{ID: "job-1", Type: "job", Label: "legacy", State: "running"} got, err := json.Marshal(node) if err != nil { @@ -17,25 +17,9 @@ func TestWorkflowAdditiveJSONContracts(t *testing.T) { if string(got) != wantNode { t.Fatalf("legacy GraphNode JSON = %s, want %s", got, wantNode) } - - const statePayload = `{"runId":"run-1","title":"legacy","nodes":[]}` - var state State - if err := json.Unmarshal([]byte(statePayload), &state); err != nil { - t.Fatalf("unmarshal legacy State: %v", err) - } - if state.Workflow != "" { - t.Fatalf("legacy State.Workflow = %q, want empty", state.Workflow) - } - stateJSON, err := json.Marshal(state) - if err != nil { - t.Fatalf("marshal legacy State: %v", err) - } - if string(stateJSON) != statePayload { - t.Fatalf("legacy State JSON = %s, want %s", stateJSON, statePayload) - } }) - t.Run("labeled graph node and state round trip", func(t *testing.T) { + t.Run("labeled graph node round trips", func(t *testing.T) { const payload = `{"id":"workflow::panel","type":"workflow","label":"panel","jobCount":7,"noteCount":3,"tokensIn":22400,"tokensOut":9700}` var node GraphNode if err := json.Unmarshal([]byte(payload), &node); err != nil { @@ -51,23 +35,6 @@ func TestWorkflowAdditiveJSONContracts(t *testing.T) { if !bytes.Equal(got, []byte(payload)) { t.Fatalf("workflow GraphNode JSON = %s, want %s", got, payload) } - - state := State{RunID: "run-1", Title: "labeled", Workflow: "panel", Nodes: []Node{}} - stateJSON, err := json.Marshal(state) - if err != nil { - t.Fatalf("marshal labeled State: %v", err) - } - const wantState = `{"runId":"run-1","title":"labeled","workflow":"panel","nodes":[]}` - if string(stateJSON) != wantState { - t.Fatalf("labeled State JSON = %s, want %s", stateJSON, wantState) - } - var roundTrip State - if err := json.Unmarshal(stateJSON, &roundTrip); err != nil { - t.Fatalf("unmarshal labeled State: %v", err) - } - if roundTrip.Workflow != "panel" { - t.Fatalf("round-trip State.Workflow = %q, want panel", roundTrip.Workflow) - } }) } diff --git a/fakefeed.go b/fakefeed.go index 10675c0..425c763 100644 --- a/fakefeed.go +++ b/fakefeed.go @@ -12,10 +12,8 @@ import ( ) // Sentinel errors returned by FakeDataSource (and mapped to HTTP status codes -// by the API/SSE handlers). +// by the API handlers). var ( - // ErrRunNotFound indicates the requested run does not exist. - ErrRunNotFound = errors.New("run not found") // ErrJobNotFound indicates the requested job/node does not exist. ErrJobNotFound = errors.New("job not found") // ErrAgentNotFound indicates the requested agent does not exist. @@ -27,12 +25,11 @@ var ( ) const ( - fakeRunID = "run-noted-001" - fakeRunTitle = "noted: add note search, delete, and export" - fakeWorkflow = "fable/dashboard-redesign" - fakeStalledWorkflow = "fable/arxiv-retry-hardening" - fakeEphemeralAgent = "wave-impl-temp-7f3a" - fakeEphemeralJobID = "job-ephemeral-demo" + fakeRunID = "run-noted-001" + fakeWorkflow = "fable/dashboard-redesign" + fakeStalledWorkflow = "fable/arxiv-retry-hardening" + fakeUnregisteredName = "retired-impl-7f3a" + fakeUnregisteredJob = "job-unregistered-demo" // fakeRepo is the repository the fake run operates on (shared by Jobs/Graph). fakeRepo = "acme/webapp" // fakeTickInterval is how often the background goroutine advances the run. @@ -40,20 +37,18 @@ const ( ) // FakeDataSource is an in-memory DataSource used for local development and -// tests. It models a realistic `noted` orchestration run: a coordinator that -// fans out three parallel implement jobs, an integrate/review/open-PR job that -// depends on all three, and a final synthesis continuation. A background -// goroutine advances node states (queued -> running -> succeeded) on a timer -// and broadcasts each new snapshot to SSE subscribers. +// tests. It models a realistic `noted` run: a coordinator job with three +// parallel implement jobs, an integrate/review/open-PR job that depends on all +// three, and a final summary job. A background goroutine advances job states +// (queued -> running -> succeeded) on a timer and moves the change cursor. // // FakeDataSource is safe for concurrent use. type FakeDataSource struct { interval time.Duration - broker *broker workflowsEnabled bool mu sync.Mutex - st State + nodes []Node step int started bool } @@ -61,111 +56,51 @@ type FakeDataSource struct { // NewFakeDataSource returns a FakeDataSource seeded with the noted run and // starts the background goroutine that advances it. func NewFakeDataSource() *FakeDataSource { - workflowsEnabled := os.Getenv("FAKEFEED_WORKFLOWS") != "0" f := &FakeDataSource{ interval: fakeTickInterval, - broker: newBroker(), - workflowsEnabled: workflowsEnabled, - st: initialFakeState(workflowsEnabled), + workflowsEnabled: os.Getenv("FAKEFEED_WORKFLOWS") != "0", + nodes: initialFakeNodes(), } f.start() return f } -// initialFakeState builds the seeded graph with the coordinator running and -// every downstream node queued. -func initialFakeState(workflowsEnabled bool) State { +// initialFakeNodes builds the seeded run with the coordinator running and every +// downstream job queued. +func initialFakeNodes() []Node { now := time.Now().UnixMilli() - st := State{ - RunID: fakeRunID, - Title: fakeRunTitle, - Nodes: []Node{ - { - ID: "job-1", - Title: "coordinate: note search, delete, export", - Agent: "project-lead", - Runtime: "codex", - Model: "gpt-5.5", - State: "running", - Depth: 0, - StartedAt: now, - WorkerID: "codex-coordinator", - Events: []Event{ - {T: now, Label: "queued"}, - {T: now, Label: "started"}, - {T: now, Label: "decomposed into 3 delegations"}, - }, - }, - { - ID: "job-2", - ParentID: "job-1", - Title: "implement: note search", - Agent: "implementer", - Runtime: "codex", - Model: "gpt-5.5", - State: "queued", - Depth: 1, - Events: []Event{{T: now, Label: "delegation_enqueued"}}, - }, - { - ID: "job-3", - ParentID: "job-1", - Title: "implement: note delete", - Agent: "implementer", - Runtime: "codex", - Model: "gpt-5.5", - State: "queued", - Depth: 1, - Events: []Event{{T: now, Label: "delegation_enqueued"}}, - }, - { - ID: "job-4", - ParentID: "job-1", - Title: "implement: note export", - Agent: "implementer", - Runtime: "codex", - Model: "gpt-5.5", - State: "queued", - Depth: 1, - Events: []Event{{T: now, Label: "delegation_enqueued"}}, - }, - { - ID: "job-5", - ParentID: "job-1", - Deps: []string{"job-2", "job-3", "job-4"}, - Title: "integrate + review + open PR", - Agent: "integrator", - Runtime: "codex", - Model: "gpt-5.5", - State: "queued", - Depth: 1, - Events: []Event{{T: now, Label: "delegation_enqueued (awaiting deps)"}}, - }, - { - ID: "job-6", - ParentID: "job-1", - Deps: []string{"job-5"}, - Title: "synthesize: summarize outcome", - Agent: "project-lead", - Runtime: "codex", - Model: "gpt-5.5", - State: "queued", - Depth: 1, - Events: []Event{{T: now, Label: "delegation_enqueued (continuation)"}}, - }, - }, + queued := func(id, title, agent string, deps ...string) Node { + return Node{ + ID: id, ParentID: "job-1", Deps: deps, Title: title, Agent: agent, + Runtime: "codex", Model: "gpt-5.5", State: "queued", Depth: 1, + Events: []Event{{T: now, Label: "queued"}}, + } } - if workflowsEnabled { - st.Workflow = fakeWorkflow + return []Node{ + { + ID: "job-1", + Title: "coordinate: note search, delete, export", + Agent: "project-lead", + Runtime: "codex", + Model: "gpt-5.5", + State: "running", + StartedAt: now, + WorkerID: "codex-coordinator", + Events: []Event{{T: now, Label: "queued"}, {T: now, Label: "started"}}, + }, + queued("job-2", "implement: note search", "implementer"), + queued("job-3", "implement: note delete", "implementer"), + queued("job-4", "implement: note export", "implementer"), + queued("job-5", "integrate + review + open PR", "integrator", "job-2", "job-3", "job-4"), + queued("job-6", "summarize outcome", "project-lead", "job-5"), } - return st } // fakeStep mutates the run for a single tick. Each step is applied with f.mu held. type fakeStep func(f *FakeDataSource) // fakeSteps is the ordered timeline the background goroutine walks. The initial -// snapshot already has the coordinator running and children queued. +// snapshot already has the coordinator running and the other jobs queued. var fakeSteps = []fakeStep{ func(f *FakeDataSource) { f.transition("job-2", "running", "worker started: note search") }, func(f *FakeDataSource) { f.transition("job-3", "running", "worker started: note delete") }, @@ -178,7 +113,7 @@ var fakeSteps = []fakeStep{ f.setPRURL("job-5", "https://github.com/jerryfane/noted/pull/42") f.transition("job-5", "succeeded", "integration PR opened, review clean") }, - func(f *FakeDataSource) { f.transition("job-6", "running", "synthesizing outcome") }, + func(f *FakeDataSource) { f.transition("job-6", "running", "summarizing outcome") }, func(f *FakeDataSource) { f.transition("job-6", "succeeded", "summary posted") }, func(f *FakeDataSource) { f.transition("job-1", "succeeded", "run complete") }, } @@ -194,8 +129,8 @@ func (f *FakeDataSource) start() { go f.run() } -// run advances the timeline on a ticker and broadcasts each new snapshot, -// stopping once the run reaches its terminal state. +// run advances the timeline on a ticker, stopping once the run reaches its +// terminal state. Each step moves the change cursor. func (f *FakeDataSource) run() { t := time.NewTicker(f.interval) defer t.Stop() @@ -207,9 +142,7 @@ func (f *FakeDataSource) run() { } fakeSteps[f.step](f) f.step++ - snap := f.cloneStateLocked() f.mu.Unlock() - f.broker.publish(snap) } } @@ -217,8 +150,8 @@ func (f *FakeDataSource) run() { // called with f.mu held. func (f *FakeDataSource) transition(id string, state NodeState, label string) { now := time.Now().UnixMilli() - for i := range f.st.Nodes { - n := &f.st.Nodes[i] + for i := range f.nodes { + n := &f.nodes[i] if n.ID != id { continue } @@ -238,9 +171,9 @@ func (f *FakeDataSource) transition(id string, state NodeState, label string) { } func (f *FakeDataSource) setPRURL(id, url string) { - for i := range f.st.Nodes { - if f.st.Nodes[i].ID == id { - f.st.Nodes[i].PRURL = url + for i := range f.nodes { + if f.nodes[i].ID == id { + f.nodes[i].PRURL = url return } } @@ -254,71 +187,33 @@ func isTerminal(s NodeState) bool { return false } -// cloneStateLocked returns a deep copy of the current run state so callers can -// read/encode it without holding f.mu. Must be called with f.mu held. -func (f *FakeDataSource) cloneStateLocked() State { - out := State{RunID: f.st.RunID, Title: f.st.Title, Workflow: f.st.Workflow} - out.Nodes = make([]Node, len(f.st.Nodes)) - for i, n := range f.st.Nodes { +// cloneNodesLocked returns a deep copy of the current run's nodes so callers +// can read/encode them without holding f.mu. Must be called with f.mu held. +func (f *FakeDataSource) cloneNodesLocked() []Node { + out := make([]Node, len(f.nodes)) + for i, n := range f.nodes { cp := n if n.Deps != nil { cp.Deps = append([]string(nil), n.Deps...) } cp.Events = append([]Event(nil), n.Events...) - out.Nodes[i] = cp + out[i] = cp } return out } -// overallStateLocked derives the run-level state from the coordinator (root) -// node, defaulting to running. Must be called with f.mu held. -func (f *FakeDataSource) overallStateLocked() (NodeState, int64) { - var updated int64 - for _, n := range f.st.Nodes { - for _, e := range n.Events { - if e.T > updated { - updated = e.T - } - } - } - root := NodeState("running") - for _, n := range f.st.Nodes { - if n.ID == "job-1" { - root = n.State - break - } - } - return root, updated -} - -// Runs implements DataSource. -func (f *FakeDataSource) Runs(ctx context.Context) ([]RunSummary, error) { +// anchor returns the run start, the time every fixture is anchored on. +func (f *FakeDataSource) anchor() int64 { f.mu.Lock() defer f.mu.Unlock() - state, updated := f.overallStateLocked() - return []RunSummary{{ - RunID: f.st.RunID, - Title: f.st.Title, - State: state, - Updated: updated, - }}, nil -} - -// State implements DataSource. An empty runID returns the active/most-recent run. -func (f *FakeDataSource) State(ctx context.Context, runID string) (State, error) { - f.mu.Lock() - defer f.mu.Unlock() - if runID != "" && runID != fakeRunID { - return State{}, ErrRunNotFound - } - return f.cloneStateLocked(), nil + return f.nodes[0].StartedAt } // Job implements DataSource. func (f *FakeDataSource) Job(ctx context.Context, jobID string) (Node, error) { f.mu.Lock() defer f.mu.Unlock() - for _, n := range f.st.Nodes { + for _, n := range f.nodes { if n.ID == jobID { cp := n cp.Deps = append([]string(nil), n.Deps...) @@ -327,39 +222,17 @@ func (f *FakeDataSource) Job(ctx context.Context, jobID string) (Node, error) { } } if f.workflowsEnabled && jobID == "demo-single-job" { - return fakeWorkflowSingleNode(f.st.Nodes[0].StartedAt), nil + return fakeWorkflowSingleNode(f.nodes[0].StartedAt), nil } - if jobID == fakeEphemeralJobID { - return fakeEphemeralGalaxyNode(f.st.Nodes[0].StartedAt), nil + if jobID == fakeUnregisteredJob { + return fakeUnregisteredGalaxyNode(f.nodes[0].StartedAt), nil } return Node{}, ErrJobNotFound } -// Subscribe implements DataSource. It registers an SSE subscriber, immediately -// delivers the current snapshot, and receives every subsequent snapshot the -// background goroutine broadcasts. The returned cancel func unregisters the -// subscriber; it is also invoked automatically when ctx is done. -func (f *FakeDataSource) Subscribe(ctx context.Context, runID string) (<-chan State, func(), error) { - if runID != "" && runID != fakeRunID { - return nil, nil, ErrRunNotFound - } - f.mu.Lock() - snap := f.cloneStateLocked() - f.mu.Unlock() - - ch, cancel := f.broker.subscribe(snap) - - // Automatically unregister when the request context is cancelled. - go func() { - <-ctx.Done() - cancel() - }() - return ch, cancel, nil -} - // ChangeCursor implements the optional dashboard invalidation stream. Each // completed fake timeline mutation increments step, so the opaque cursor moves -// in lockstep with the state snapshots served by the dev harness. +// in lockstep with the jobs served by the dev harness. func (f *FakeDataSource) ChangeCursor(context.Context) (string, error) { f.mu.Lock() defer f.mu.Unlock() @@ -371,7 +244,7 @@ func (f *FakeDataSource) ChangeCursor(context.Context) (string, error) { // Galaxy view standalone. func (f *FakeDataSource) Graph(ctx context.Context, repo string) (Graph, error) { f.mu.Lock() - snap := f.cloneStateLocked() + nodes := f.cloneNodesLocked() workflowsEnabled := f.workflowsEnabled f.mu.Unlock() g := Graph{Nodes: []GraphNode{}, Links: []GraphLink{}, Repos: []string{fakeRepo}} @@ -379,12 +252,12 @@ func (f *FakeDataSource) Graph(ctx context.Context, repo string) (Graph, error) return g, nil } ids := map[string]bool{} - for _, n := range snap.Nodes { + for _, n := range nodes { ids[n.ID] = true } agents := map[string]bool{} - for _, n := range snap.Nodes { - g.Nodes = append(g.Nodes, GraphNode{ID: n.ID, Type: "job", Label: n.Title, State: n.State, Agent: n.Agent, Repo: fakeRepo, Run: snap.RunID}) + for _, n := range nodes { + g.Nodes = append(g.Nodes, GraphNode{ID: n.ID, Type: "job", Label: n.Title, State: n.State, Agent: n.Agent, Repo: fakeRepo, Run: fakeRunID}) if n.ParentID != "" && ids[n.ParentID] { g.Links = append(g.Links, GraphLink{Source: n.ParentID, Target: n.ID, Kind: "parent"}) } @@ -399,19 +272,19 @@ func (f *FakeDataSource) Graph(ctx context.Context, repo string) (Graph, error) g.Links = append(g.Links, GraphLink{Source: n.ID, Target: "agent::" + n.Agent, Kind: "agent"}) } } - ephemeral := fakeEphemeralGalaxyNode(snap.Nodes[0].StartedAt) - g.Nodes = append(g.Nodes, GraphNode{ID: ephemeral.ID, Type: "job", Label: ephemeral.Title, State: ephemeral.State, Agent: ephemeral.Agent, Repo: fakeRepo, Run: snap.RunID}) + unregistered := fakeUnregisteredGalaxyNode(nodes[0].StartedAt) + g.Nodes = append(g.Nodes, GraphNode{ID: unregistered.ID, Type: "job", Label: unregistered.Title, State: unregistered.State, Agent: unregistered.Agent, Repo: fakeRepo, Run: fakeRunID}) g.Links = append(g.Links, - GraphLink{Source: ephemeral.ID, Target: "repo::" + fakeRepo, Kind: "repo"}, - GraphLink{Source: ephemeral.ID, Target: "agent::" + ephemeral.Agent, Kind: "agent"}, + GraphLink{Source: unregistered.ID, Target: "repo::" + fakeRepo, Kind: "repo"}, + GraphLink{Source: unregistered.ID, Target: "agent::" + unregistered.Agent, Kind: "agent"}, ) - agents[ephemeral.Agent] = true + agents[unregistered.Agent] = true g.Nodes = append(g.Nodes, GraphNode{ID: "repo::" + fakeRepo, Type: "repo", Label: fakeRepo, Repo: fakeRepo}) for a := range agents { g.Nodes = append(g.Nodes, GraphNode{ID: "agent::" + a, Type: "agent", Label: a, Agent: a}) } if workflowsEnabled { - extra := fakeWorkflowSingleNode(snap.Nodes[0].StartedAt) + extra := fakeWorkflowSingleNode(nodes[0].StartedAt) g.Nodes = append(g.Nodes, GraphNode{ID: extra.ID, Type: "job", Label: extra.Title, State: extra.State, Agent: extra.Agent, Repo: fakeRepo, Run: "run-demo-single"}) g.Links = append(g.Links, GraphLink{Source: extra.ID, Target: "repo::" + fakeRepo, Kind: "repo"}, @@ -420,25 +293,26 @@ func (f *FakeDataSource) Graph(ctx context.Context, repo string) (Graph, error) if !agents[extra.Agent] { g.Nodes = append(g.Nodes, GraphNode{ID: "agent::" + extra.Agent, Type: "agent", Label: extra.Agent, Agent: extra.Agent}) } - for _, n := range append(snap.Nodes, extra) { + for _, n := range append(nodes, extra) { g.Links = append(g.Links, GraphLink{Source: n.ID, Target: "workflow::" + fakeWorkflow, Kind: "workflow"}) } g.Nodes = append(g.Nodes, GraphNode{ ID: "workflow::" + fakeWorkflow, Type: "workflow", Label: fakeWorkflow, - JobCount: len(snap.Nodes) + 1, NoteCount: len(fakeWorkflowNotes(snap.Nodes[0].StartedAt)), TokensIn: 1_230_000, TokensOut: 15_300, + JobCount: len(nodes) + 1, NoteCount: len(fakeWorkflowNotes(nodes[0].StartedAt)), TokensIn: 1_230_000, TokensOut: 15_300, }) } return g, nil } -// fakeEphemeralGalaxyNode is intentionally present in Graph and Jobs but absent -// from Agents/Agent, matching a one-off worker with no persistent registration. -func fakeEphemeralGalaxyNode(anchor int64) Node { +// fakeUnregisteredGalaxyNode is intentionally present in Graph and Jobs but +// absent from Agents/Agent, matching history from an agent that is no longer +// registered. +func fakeUnregisteredGalaxyNode(anchor int64) Node { started := anchor - int64(4*time.Minute/time.Millisecond) ended := started + int64(2*time.Minute/time.Millisecond) return Node{ - ID: fakeEphemeralJobID, Title: "implement: transient delegation patch", - Agent: fakeEphemeralAgent, Runtime: "codex", Model: "gpt-5.5", + ID: fakeUnregisteredJob, Title: "implement: patch from a retired agent", + Agent: fakeUnregisteredName, Runtime: "codex", Model: "gpt-5.5", State: "succeeded", StartedAt: started, EndedAt: ended, Events: []Event{{T: started, Label: "started"}, {T: ended, Label: "completed"}}, } @@ -654,10 +528,7 @@ func (f *FakeDataSource) Workflows(ctx context.Context) ([]WorkflowIndexEntry, e if !f.workflowsEnabled { return []WorkflowIndexEntry{}, nil } - f.mu.Lock() - anchor := f.st.Nodes[0].StartedAt - f.mu.Unlock() - return fakeWorkflowIndex(anchor), nil + return fakeWorkflowIndex(f.anchor()), nil } // Overview implements the optional operator-first landing-page contract. Every @@ -713,7 +584,7 @@ func fakeTaskTitles() []string { "Design review for dashboard #78 information architecture", "Backtest sweep r8 for the bitcoin price model", "Org chart role scopes for the review fleet", - "Health page locks-aging amber thresholds", + "Health page resource-lock amber thresholds", } } @@ -730,9 +601,7 @@ func fakeTaskAgent(i int) string { // Tasks implements the optional task-board contract with the mockup's target // volumes: 10 PR-open, 5 blocked, and 4 merged. func (f *FakeDataSource) Tasks(ctx context.Context) ([]TaskSummary, error) { - f.mu.Lock() - anchor := f.st.Nodes[0].StartedAt - f.mu.Unlock() + anchor := f.anchor() titles := fakeTaskTitles() counts := []struct { state string @@ -824,7 +693,6 @@ func fakeOrgView() OrgView { Escalations: escalations, Feed: []OrgFeedRow{ {Kind: "blocked_since", Role: "g4", At: "2026-07-23T01:31:00Z", Since: "14m", Detail: "waiting on an API contract decision"}, - {Kind: "recycle_overdue", Role: "vetrina", At: "2026-07-23T01:22:00Z", Since: "23m", Detail: "visual audit handoff is overdue"}, {Kind: "recycle", Role: "joltra", At: "2026-07-23T00:58:00Z", Detail: "handoff recorded after the weekly clip batch"}, }, } @@ -907,9 +775,7 @@ func (f *FakeDataSource) Workflow(ctx context.Context, label string, q WorkflowQ if !f.workflowsEnabled || (label != fakeWorkflow && label != fakeStalledWorkflow) { return WorkflowView{}, ErrWorkflowNotFound } - f.mu.Lock() - snap := f.cloneStateLocked() - f.mu.Unlock() + anchor := f.anchor() if q.MaxRuns <= 0 || q.MaxRuns > workflowMaxRuns { q.MaxRuns = workflowMaxRuns @@ -918,8 +784,8 @@ func (f *FakeDataSource) Workflow(ctx context.Context, label string, q WorkflowQ q.MaxNotes = workflowMaxNotes } - runs := fakeWorkflowRuns(snap.Nodes[0].StartedAt) - notes := fakeWorkflowNotes(snap.Nodes[0].StartedAt) + runs := fakeWorkflowRuns(anchor) + notes := fakeWorkflowNotes(anchor) state := "active" coordinator := WorkflowCoordinator{Author: "claude-coordinator", Pane: "fable", SessionID: "7b2e04"} workDir := "/root/gitmoot" @@ -983,21 +849,7 @@ func (f *FakeDataSource) Workflow(ctx context.Context, label string, q WorkflowQ return view, nil } -func (f *FakeDataSource) overallStateLockedForSnapshot(snap State) (NodeState, int64) { - state := NodeState("running") - var updated int64 - for _, n := range snap.Nodes { - if n.ParentID == "" { - state = n.State - } - if u := nodeUpdated(n); u > updated { - updated = u - } - } - return state, updated -} - -// nodeUpdatedLocked returns a node's most-recent activity time (epoch ms): its +// nodeUpdated returns a node's most-recent activity time (epoch ms): its // last timeline event, falling back to EndedAt/StartedAt. func nodeUpdated(n Node) int64 { updated := n.StartedAt @@ -1012,12 +864,10 @@ func nodeUpdated(n Node) int64 { return updated } -// fakeKind maps a fake node to a plausible job kind (ask|review|implement| -// orchestrate) from its depth/title so the Jobs page has something to group on. +// fakeKind maps a fake node to a plausible job kind (ask|review|implement) from +// its title so the Jobs page has something to group on. func fakeKind(n Node) string { switch { - case n.Depth == 0: - return "orchestrate" case strings.HasPrefix(n.Title, "implement"): return "implement" case strings.Contains(n.Title, "review"): @@ -1081,14 +931,14 @@ func jobSummaryFor(n Node, runID string) JobSummary { // that run's nodes into per-job rows, sorted Updated desc. func (f *FakeDataSource) Jobs(ctx context.Context) ([]JobSummary, error) { f.mu.Lock() - snap := f.cloneStateLocked() + nodes := f.cloneNodesLocked() f.mu.Unlock() - jobs := make([]JobSummary, 0, len(snap.Nodes)) - for _, n := range snap.Nodes { - jobs = append(jobs, jobSummaryFor(n, snap.RunID)) + jobs := make([]JobSummary, 0, len(nodes)+1) + for _, n := range nodes { + jobs = append(jobs, jobSummaryFor(n, fakeRunID)) } - jobs = append(jobs, jobSummaryFor(fakeEphemeralGalaxyNode(snap.Nodes[0].StartedAt), snap.RunID)) + jobs = append(jobs, jobSummaryFor(fakeUnregisteredGalaxyNode(nodes[0].StartedAt), fakeRunID)) sort.SliceStable(jobs, func(i, j int) bool { if jobs[i].Updated != jobs[j].Updated { return jobs[i].Updated > jobs[j].Updated // Updated desc @@ -1125,24 +975,23 @@ type fakeAgent struct { // config values are constant (no time.Now()) so a clicked-through detail is // byte-stable. var fakeAgents = []fakeAgent{ - {name: "project-lead", role: "coordinator", runtime: "codex", model: "gpt-5.5", capabilities: []string{"orchestrate", "review"}, autonomyPolicy: "workspace-write", health: "healthy", repoScope: []string{fakeRepo}, - config: &AgentConfigInfo{MaxBackground: 4, IdleTimeout: "10m", JobTimeout: "1h", Model: "gpt-5.5", Template: "coordinator", Capabilities: []string{"orchestrate", "review"}}}, + {name: "project-lead", role: "coordinator", runtime: "codex", model: "gpt-5.5", capabilities: []string{"ask", "review"}, autonomyPolicy: "workspace-write", health: "healthy", repoScope: []string{fakeRepo}, + config: &AgentConfigInfo{JobTimeout: "1h", Model: "gpt-5.5", Capabilities: []string{"ask", "review"}}}, {name: "implementer", role: "implementer", runtime: "codex", model: "gpt-5.5", capabilities: []string{"implement"}, autonomyPolicy: "workspace-write", health: "healthy", repoScope: []string{fakeRepo}, - config: &AgentConfigInfo{MaxBackground: 6, IdleTimeout: "5m", JobTimeout: "45m", Model: "gpt-5.5", Capabilities: []string{"implement"}}}, + config: &AgentConfigInfo{JobTimeout: "45m", Model: "gpt-5.5", Capabilities: []string{"implement"}}}, {name: "integrator", role: "integrator", runtime: "codex", capabilities: []string{"review", "integrate"}, autonomyPolicy: "workspace-write", health: "healthy", repoScope: []string{fakeRepo}}, {name: "researcher", role: "researcher", runtime: "claude", model: "claude-opus-4-8", capabilities: []string{"research"}, autonomyPolicy: "read-only", health: "healthy", - config: &AgentConfigInfo{MaxBackground: 2, IdleTimeout: "15m", JobTimeout: "30m", Model: "claude-opus-4-8", Template: "researcher", Capabilities: []string{"research"}}}, + config: &AgentConfigInfo{JobTimeout: "30m", Model: "claude-opus-4-8", Capabilities: []string{"research"}}}, {name: "reviewer-kimi", role: "reviewer", runtime: "kimi", model: "kimi-code", capabilities: []string{"review"}, autonomyPolicy: "read-only", health: "degraded", - config: &AgentConfigInfo{MaxBackground: 3, IdleTimeout: "10m", JobTimeout: "30m", Model: "kimi-code", Template: "reviewer", Capabilities: []string{"review"}}}, + config: &AgentConfigInfo{JobTimeout: "30m", Model: "kimi-code", Capabilities: []string{"review"}}}, {name: "ci-runner", role: "ci", runtime: "shell", capabilities: []string{"ci", "lint"}, autonomyPolicy: "workspace-write", health: "healthy", repoScope: []string{fakeRepo}}, } -// Agents implements DataSource. It returns the registered agents (with job -// counts aggregated live from the seeded run) plus one synthetic rollup row for -// the fleet of ephemeral workers (Ephemeral == true). +// Agents implements DataSource. It returns the registered agents with job +// counts aggregated live from the seeded run. func (f *FakeDataSource) Agents(ctx context.Context) ([]AgentSummary, error) { f.mu.Lock() - snap := f.cloneStateLocked() + nodes := f.cloneNodesLocked() f.mu.Unlock() // Aggregate per-agent counts from the run's jobs. @@ -1151,7 +1000,7 @@ func (f *FakeDataSource) Agents(ctx context.Context) ([]AgentSummary, error) { lastActive int64 } byAgent := map[string]*agg{} - for _, n := range snap.Nodes { + for _, n := range nodes { a := byAgent[n.Agent] if a == nil { a = &agg{} @@ -1171,7 +1020,7 @@ func (f *FakeDataSource) Agents(ctx context.Context) ([]AgentSummary, error) { } } - out := make([]AgentSummary, 0, len(fakeAgents)+1) + out := make([]AgentSummary, 0, len(fakeAgents)) for _, fa := range fakeAgents { s := AgentSummary{ Name: fa.name, @@ -1193,175 +1042,12 @@ func (f *FakeDataSource) Agents(ctx context.Context) ([]AgentSummary, error) { out = append(out, s) } - // One synthetic rollup row for the fleet of ephemeral workers. - out = append(out, AgentSummary{ - Name: "ephemeral-workers", - Role: "worker", - Runtime: "codex", - Capabilities: []string{"implement", "review"}, - AutonomyPolicy: "workspace-write", - Health: "healthy", - Ephemeral: true, - JobCount: 128, - RunningCount: 3, - SucceededCount: 119, - FailedCount: 6, - LastActive: time.Now().UnixMilli(), - }) return out, nil } -// fakeTemplatedAgent is the one seeded agent whose click-through detail carries -// a template and version history; every other agent's AgentDetail has Template -// nil. It is deliberately an agent that does not appear in the seeded run so its -// summary counts (and thus its whole AgentDetail) are byte-stable across calls. -const fakeTemplatedAgent = "researcher" - -// fakeGalaxyPanelAgent appears in the seeded graph and carries a compact -// template record so the Galaxy inspector exercises its complete state. -const fakeGalaxyPanelAgent = "project-lead" - -// The three prompt bodies below are fakeTemplatedAgent's full template content at -// each version. They are multi-line markdown and deliberately DIFFERENT per -// version (v1 base → v2 widens fan-out → v3 adds a verification pass) so the -// detail view's content viewer is exercised, and each contains angle brackets, -// ampersands, and quotes so the client's HTML-escaping is exercised too. Constant -// (no time.Now()) so the whole AgentDetail stays byte-stable across calls. -const ( - fakeResearcherPromptV1 = `# Researcher agent - -You are a **research agent**. Given a question, find the current -state-of-the-art answer & cite every source. - -## Method -1. Decompose the question into sub-queries. -2. Search the web; prefer primary sources & standards bodies over blog summaries. -3. Synthesize a concise answer. - -## Output -Return findings as blocks and quote sources verbatim inside -"double quotes". Never invent a citation. -` - fakeResearcherPromptV2 = `# Researcher agent - -You are a **research agent**. Given a question, find the current -state-of-the-art answer & cite every source. - -## Method -1. Decompose the question into up to 8 sub-queries. -2. Fan out all sub-queries in parallel, then dedupe the results. -3. Search the web; prefer primary sources & standards bodies over blog summaries. -4. Synthesize a concise answer. - -## Output -Return findings as blocks and quote sources verbatim inside -"double quotes". Never invent a citation. -` - fakeResearcherPromptV3 = `# Researcher agent - -You are a **research agent**. Given a question, find the current -state-of-the-art answer & cite every source. - -## Method -1. Decompose the question into up to 8 sub-queries. -2. Fan out all sub-queries in parallel, then dedupe the results. -3. Search the web; prefer primary sources & standards bodies over blog summaries. -4. Adversarially verify each claim against a second source; drop any - claim you cannot corroborate. -5. Synthesize a concise answer. - -## Output -Return findings as blocks and quote sources verbatim inside -"double quotes". Never invent a citation. -` -) - -// fakeAgentTemplate is the template fakeTemplatedAgent is instantiated from. Its -// ResolvedCommit and Content match the currently-promoted version below (v1, the -// version the template currently resolves to). All values are constant so the -// detail is deterministic. -var fakeAgentTemplate = AgentTemplateInfo{ - ID: "tmpl-researcher", - Name: "researcher", - Description: "SOTA / best-solution research agent that cites its sources", - SourceRepo: "jerryfane/gitmoot", - SourceRef: "main", - SourcePath: "agents/researcher.md", - ResolvedCommit: "3c3824f9a1b2c4d5e6f70819a2b3c4d5e6f70819", - Content: fakeResearcherPromptV1, -} - -// fakeAgentVersions is fakeTemplatedAgent's version history, newest first, across -// the pending/canary/promoted states so the version-history UI is fully -// exercised: v1 is the promoted version the template currently resolves to -// (Current), v2 is a canary being sampled (CanarySample), v3 is a newly proposed -// pending candidate. Timestamps are anchored on fakeChartsNow (never time.Now()) -// so the detail is byte-stable across calls. -var fakeAgentVersions = []TemplateVersionInfo{ - { - ID: "tmpl-researcher-v3", - Number: 3, - State: "pending", - Name: "researcher", - Description: "propose: add adversarial claim-verification pass", - SourceRef: "main", - CreatedAt: fakeChartsNow.Add(-6 * time.Hour).UnixMilli(), - Content: fakeResearcherPromptV3, - }, - { - ID: "tmpl-researcher-v2", - Number: 2, - State: "canary", - Name: "researcher", - Description: "widen source fan-out to 8 parallel searches", - SourceRef: "main", - ResolvedCommit: "9f8e7d6c5b4a39281706f5e4d3c2b1a09f8e7d6c", - CreatedAt: fakeChartsNow.AddDate(0, 0, -2).UnixMilli(), - CanarySample: 0.15, - Content: fakeResearcherPromptV2, - }, - { - ID: "tmpl-researcher-v1", - Number: 1, - State: "current", // live version (store emits 'current', never 'promoted') - Name: "researcher", - Description: "initial captured researcher agent", - SourceRef: "main", - ResolvedCommit: "3c3824f9a1b2c4d5e6f70819a2b3c4d5e6f70819", - CreatedAt: fakeChartsNow.AddDate(0, 0, -9).UnixMilli(), - PromotedAt: fakeChartsNow.AddDate(0, 0, -8).UnixMilli(), - Current: true, - Content: fakeResearcherPromptV1, - }, -} - -var fakeCoordinatorTemplate = AgentTemplateInfo{ - ID: "tmpl-coordinator", - Name: "coordinator", - Description: "Coordinates delegation trees and synthesizes their outcomes", - SourceRepo: "jerryfane/gitmoot", - SourceRef: "main", - SourcePath: "agents/coordinator.md", - ResolvedCommit: "6f4c9d218a37b510e2f6548bd90ac1734ef21865", -} - -var fakeCoordinatorVersions = []TemplateVersionInfo{{ - ID: "tmpl-coordinator-v4", - Number: 4, - State: "current", - Name: "coordinator", - Description: "current coordinator policy", - SourceRef: "main", - ResolvedCommit: "6f4c9d218a37b510e2f6548bd90ac1734ef21865", - CreatedAt: fakeChartsNow.AddDate(0, 0, -6).UnixMilli(), - PromotedAt: fakeChartsNow.AddDate(0, 0, -5).UnixMilli(), - Current: true, -}} - // Agent implements DataSource. It returns the click-through detail for a single // agent: the same AgentSummary row Agents() lists (so counts line up with the -// Agents page), plus template histories for the Agents-page fixture and the -// Galaxy-visible coordinator. Unknown names return ErrAgentNotFound. +// Agents page) plus its config section. Unknown names return ErrAgentNotFound. func (f *FakeDataSource) Agent(ctx context.Context, name string) (AgentDetail, error) { agents, err := f.Agents(ctx) if err != nil { @@ -1371,16 +1057,7 @@ func (f *FakeDataSource) Agent(ctx context.Context, name string) (AgentDetail, e if a.Name != name { continue } - detail := AgentDetail{AgentSummary: a, Versions: []TemplateVersionInfo{}} - if name == fakeTemplatedAgent { - tmpl := fakeAgentTemplate - detail.Template = &tmpl - detail.Versions = append([]TemplateVersionInfo(nil), fakeAgentVersions...) - } else if name == fakeGalaxyPanelAgent { - tmpl := fakeCoordinatorTemplate - detail.Template = &tmpl - detail.Versions = append([]TemplateVersionInfo(nil), fakeCoordinatorVersions...) - } + detail := AgentDetail{AgentSummary: a} // Config section comes from the registered fakeAgent (nil config => "no // config entry" in the panel). Constant, so the detail stays byte-stable // across calls. @@ -1401,7 +1078,7 @@ func (f *FakeDataSource) Agent(ctx context.Context, name string) (AgentDetail, e } // fakeChartsNow is the fixed reference instant the Charts/Health fake views -// treat as "now". Unlike the live-advancing run state (Runs/State/Jobs/Agents, +// treat as "now". Unlike the live-advancing run (Jobs/Agents, // which embed time.Now() and evolve as the background goroutine ticks), the // Charts and Health views must be byte-stable across polls so their handlers' // repeat-call equality tests hold. Every relative value they derive — day @@ -1655,8 +1332,8 @@ const ( ) // Health implements DataSource. It derives the daemon liveness, fleet totals, -// held locks, wedged jobs and recent failures from the fixed fakeHistory fixture -// (and fixed lock fixtures), anchored on fakeChartsNow. Output is deterministic +// held resource locks, wedged jobs and recent failures from the fixed +// fakeHistory fixture (and fixed lock fixtures), anchored on fakeChartsNow. Output is deterministic // and byte-stable across calls. func (f *FakeDataSource) Health(ctx context.Context) (Health, error) { recs := fakeHistory() @@ -1683,17 +1360,10 @@ func (f *FakeDataSource) Health(ctx context.Context) (Health, error) { } } - // Branch/checkout locks, oldest first. - locks := []HealthLock{ - {Repo: fakeRepo, Branch: "feat/export-csv", Owner: "implementer", AcquiredAt: now - 8*minute}, - {Repo: "jerryfane/noted", Branch: "feat/bulk-delete", Owner: "implementer", AcquiredAt: now - 3*minute}, - } - sort.SliceStable(locks, func(i, j int) bool { return locks[i].AcquiredAt < locks[j].AcquiredAt }) - - // Non-branch resource locks (runtime sessions, etc.), oldest first. + // Resource locks (runtime sessions, etc.), oldest first. resLocks := []HealthResourceLock{ {Key: "runtime:claude:sess-7f3a", Owner: "researcher", AcquiredAt: now - 12*minute, ExpiresAt: now + 18*minute}, - {Key: "skillopt-train-generation:acme", Owner: "ci-runner", AcquiredAt: now - 2*minute, ExpiresAt: now + 28*minute}, + {Key: "runtime:codex:sess-91c2", Owner: "implementer", AcquiredAt: now - 2*minute, ExpiresAt: now + 28*minute}, } sort.SliceStable(resLocks, func(i, j int) bool { return resLocks[i].AcquiredAt < resLocks[j].AcquiredAt }) @@ -1760,7 +1430,6 @@ func (f *FakeDataSource) Health(ctx context.Context) (Health, error) { CheckedAt: now - 30*minute, }, Totals: totals, - Locks: locks, ResourceLocks: resLocks, Stuck: stuck, RecentFailures: failures, @@ -1781,18 +1450,14 @@ func (f *FakeDataSource) Config(ctx context.Context) (ConfigSnapshot, error) { {Key: "rate_limit_per_hour", Value: 4200, Default: 4500, IsDefault: false, Kind: "int", Doc: "Reserve-aware GitHub request budget per hour."}, }}, {Name: "orchestrate", Knobs: []ConfigKnob{ - {Key: "blocked_ttl", Value: "30m", Default: "30m", IsDefault: true, Kind: "duration", Doc: "Time a blocked orchestration remains resumable."}, - }}, - {Name: "skillopt", Knobs: []ConfigKnob{ - {Key: "auto_promote", Value: false, Default: false, IsDefault: true, Kind: "flag", Doc: "Promote passing template candidates without a human gate."}, - {Key: "pace_gate", Value: true, Default: true, IsDefault: true, Kind: "flag", Doc: "Require PACE quality gates during template optimization."}, + {Key: "blocked_ttl", Value: "30m", Default: "30m", IsDefault: true, Kind: "duration", Doc: "Maximum time a blocked job remains awaiting a human; empty disables expiry."}, }}, }, Agents: []ConfigAgent{ - {Name: "galaxy-impl", Runtime: "codex", Model: "gpt-5.6-codex", Capabilities: []string{"ask", "implement"}, AutonomyPolicy: "workspace-write", MaxBackground: 2}, - {Name: "lead", Runtime: "claude", Model: "opus-4.6", Capabilities: []string{"ask", "implement", "review"}, AutonomyPolicy: "workspace-write", MaxBackground: 3}, - {Name: "researcher", Runtime: "kimi", Model: "kimi-k2.5", Capabilities: []string{"ask", "review"}, AutonomyPolicy: "read-only", MaxBackground: 1}, - {Name: "reviewer", Runtime: "codex", Model: "gpt-5.6-codex", Capabilities: []string{"review"}, AutonomyPolicy: "read-only", MaxBackground: 1}, + {Name: "galaxy-impl", Runtime: "codex", Model: "gpt-5.6-codex", Capabilities: []string{"ask", "implement"}, AutonomyPolicy: "workspace-write"}, + {Name: "lead", Runtime: "claude", Model: "opus-4.6", Capabilities: []string{"ask", "implement", "review"}, AutonomyPolicy: "workspace-write"}, + {Name: "researcher", Runtime: "kimi", Model: "kimi-k2.5", Capabilities: []string{"ask", "review"}, AutonomyPolicy: "read-only"}, + {Name: "reviewer", Runtime: "codex", Model: "gpt-5.6-codex", Capabilities: []string{"review"}, AutonomyPolicy: "read-only"}, }, UnknownKeys: []string{"experimental.scheduler_bias", "plugins.private_token"}, Keychain: fakeKeychain(), @@ -1841,105 +1506,6 @@ func fakeMissingKeychain() KeychainView { } } -// fakeSkills builds the fixed SkillOpt evolution fixture behind the Learning -// page's Skills view. Every timestamp is anchored on fakeChartsNow (never -// time.Now()) so the view is byte-stable across polls. It models three templates -// that together exercise the UI: a healthy rising-score template evolved through -// five versions (researcher); a template with an in-flight canary being sampled -// AND a freshly proposed pending candidate (reviewer); and a flat two-version -// template whose score never moved (coordinator). Only real-emittable version -// states are used: the live version is "current" and each promoted-then-replaced -// predecessor is "superseded" (the store never emits a "promoted" version state — -// PromoteAgentTemplateVersion writes 'current'/'superseded'), plus "canary" and -// "pending" for in-flight candidates. -func fakeSkills() Skills { - dA := func(n int) int64 { return fakeChartsNow.AddDate(0, 0, -n).UnixMilli() } - hA := func(n int) int64 { return fakeChartsNow.Add(-time.Duration(n) * time.Hour).UnixMilli() } - - templates := []SkillTemplate{ - // Healthy, rising score: four superseded predecessors and the live "current" - // version (the store supersedes the old current on each promotion). - { - TemplateID: "skill-researcher", - Name: "researcher", - Agents: []string{"researcher"}, - Versions: []SkillVersion{ - {Number: 1, State: "superseded", Score: 0.62, HasScore: true, CreatedAt: dA(20), PromotedAt: dA(19)}, - {Number: 2, State: "superseded", Score: 0.68, HasScore: true, CreatedAt: dA(15), PromotedAt: dA(14)}, - {Number: 3, State: "superseded", Score: 0.71, HasScore: true, CreatedAt: dA(10), PromotedAt: dA(9)}, - {Number: 4, State: "superseded", Score: 0.77, HasScore: true, CreatedAt: dA(5), PromotedAt: dA(4)}, - {Number: 5, State: "current", Score: 0.83, HasScore: true, CreatedAt: dA(2), PromotedAt: dA(1)}, - }, - CurrentVersion: 5, - CurrentState: "current", - LastPromotedAt: dA(1), - Pending: []SkillCandidate{}, - }, - // Active canary being sampled at 0.15, plus a pending candidate awaiting - // review while the live "current" version stays in production. - { - TemplateID: "skill-reviewer", - Name: "reviewer", - Agents: []string{"reviewer-kimi"}, - Versions: []SkillVersion{ - {Number: 1, State: "current", Score: 0.70, HasScore: true, CreatedAt: dA(9), PromotedAt: dA(8)}, - {Number: 2, State: "canary", CreatedAt: dA(2)}, // mid-canary: no final score yet - {Number: 3, State: "pending", CreatedAt: hA(6)}, - }, - CurrentVersion: 1, - CurrentState: "current", - CanarySample: 0.15, - CanaryStartedAt: dA(2), - LastPromotedAt: dA(8), - Pending: []SkillCandidate{ - {VersionID: "skill-reviewer-v3", Number: 3, Score: "0.81", CreatedAt: hA(6)}, - }, - }, - // Flat: a superseded predecessor and the live "current" version, unchanged score. - { - TemplateID: "skill-coordinator", - Name: "coordinator", - Agents: []string{"project-lead"}, - Versions: []SkillVersion{ - {Number: 1, State: "superseded", Score: 0.75, HasScore: true, CreatedAt: dA(12), PromotedAt: dA(11)}, - {Number: 2, State: "current", Score: 0.75, HasScore: true, CreatedAt: dA(6), PromotedAt: dA(5)}, - }, - CurrentVersion: 2, - CurrentState: "current", - LastPromotedAt: dA(5), - Pending: []SkillCandidate{}, - }, - } - - // Pending-first, then most-recently-promoted (LastPromotedAt desc), with a - // TemplateID tie-break so the order is fully deterministic. - sort.SliceStable(templates, func(i, j int) bool { - pi, pj := len(templates[i].Pending) > 0, len(templates[j].Pending) > 0 - if pi != pj { - return pi - } - if templates[i].LastPromotedAt != templates[j].LastPromotedAt { - return templates[i].LastPromotedAt > templates[j].LastPromotedAt - } - return templates[i].TemplateID < templates[j].TemplateID - }) - - skills := Skills{Templates: templates} - for i := range templates { - if templates[i].CanarySample > 0 { - skills.ActiveCanaries++ - } - skills.PendingTotal += len(templates[i].Pending) - } - return skills -} - -// Skills implements DataSource. It returns the fixed fakeSkills fixture; output -// is deterministic and byte-stable across calls. -func (f *FakeDataSource) Skills(ctx context.Context) (Skills, error) { - return fakeSkills(), nil -} - // fakeJobChecks builds the fixed job-detail failed-check fixture (gitmoot #711), // keyed by job id. job-5 (the integrate/review/open-PR job) failed two // deterministic result checks under a block policy; every other job passed diff --git a/server.go b/server.go index 0482fcd..6848d80 100644 --- a/server.go +++ b/server.go @@ -9,25 +9,18 @@ import ( ) // Serve returns an http.Handler serving the read-only dashboard: the embedded -// static UI (with SPA fallback to index.html) plus the JSON API (handleRuns/ -// handleJobs/handleAgents/handleAgent/handleCharts/handleHealth/ -// handleLearningSkills/handleOverview/handleTasks/ -// handleOrg/handleOrgRole/handleWorkflows/handleWorkflow/ -// handleAttention/handleJobChecks/ -// handleState/handleJob/handleGraph/handleChangeEvents in api.go) and the -// run-state SSE stream (handleEvents in sse.go). +// static UI (with SPA fallback to index.html) plus the JSON API and the +// /api/events change-ping stream (api.go). func Serve(ds DataSource) http.Handler { s := newServer(ds) mux := http.NewServeMux() - mux.HandleFunc("GET /api/runs", s.handleRuns) mux.HandleFunc("GET /api/jobs", s.handleJobs) mux.HandleFunc("GET /api/agents", s.handleAgents) mux.HandleFunc("GET /api/agent/{name}", s.handleAgent) mux.HandleFunc("GET /api/charts", s.handleCharts) mux.HandleFunc("GET /api/health", s.handleHealth) mux.HandleFunc("GET /api/config", s.handleConfig) - mux.HandleFunc("GET /api/learning/skills", s.handleLearningSkills) mux.HandleFunc("GET /api/overview", s.handleOverview) mux.HandleFunc("GET /api/tasks", s.handleTasks) mux.HandleFunc("GET /api/org", s.handleOrg) @@ -36,11 +29,9 @@ func Serve(ds DataSource) http.Handler { mux.HandleFunc("GET /api/workflow/{label}", s.handleWorkflow) mux.HandleFunc("GET /api/attention", s.handleAttention) mux.HandleFunc("GET /api/job/{id}/checks", s.handleJobChecks) - mux.HandleFunc("GET /api/state", s.handleState) mux.HandleFunc("GET /api/job/{id}", s.handleJob) mux.HandleFunc("GET /api/graph", s.handleGraph) mux.HandleFunc("GET /api/events", s.handleChangeEvents) - mux.HandleFunc("GET /events", s.handleEvents) // Everything else is served from the embedded static assets, with an SPA // fallback to index.html for unknown paths. @@ -63,18 +54,11 @@ func newServer(ds DataSource) *server { return s } -// The JSON API handlers (handleRuns/handleJobs/handleAgents/handleAgent/ -// handleCharts/handleHealth/handleLearningSkills/ -// handleOverview/handleTasks/ -// handleOrg/handleOrgRole/handleWorkflows/handleWorkflow/ -// handleAttention/handleJobChecks/ -// handleState/handleJob/handleGraph/handleChangeEvents) live in api.go and the -// run-state SSE handler (handleEvents) lives in sse.go. - // staticHandler serves the embedded web/dist assets. Requests that do not map // to an existing file fall back to index.html so the client-side router can -// take over — except under /api/, where an unknown or removed endpoint must -// answer 404 rather than a 200 HTML shell a client would try to parse as JSON. +// take over — except under /api/ and at /events (the retired run-state stream), +// where an unknown or removed endpoint must answer 404 rather than a 200 HTML +// shell a stale client would try to parse as JSON or an event stream. func (s *server) staticHandler() http.Handler { dist := webDistFS() fileServer := http.FileServer(http.FS(dist)) @@ -91,7 +75,7 @@ func (s *server) staticHandler() http.Handler { if upath == "" { upath = "index.html" } - if upath == "api" || strings.HasPrefix(upath, "api/") { + if upath == "api" || strings.HasPrefix(upath, "api/") || upath == "events" { http.NotFound(w, r) return } diff --git a/server_test.go b/server_test.go index 051c415..66f257b 100644 --- a/server_test.go +++ b/server_test.go @@ -47,10 +47,11 @@ func TestServeSPAFallback(t *testing.T) { } } -// TestRemovedAPIRoutesNotFound pins that endpoints removed from the contract -// (the wake ledger and the SkillOpt verdicts stub, gitmoot/gitmoot#2593) and -// any other unknown /api/ path answer 404 instead of the 200 SPA shell. -func TestRemovedAPIRoutesNotFound(t *testing.T) { +// TestRemovedRoutesNotFound pins that endpoints removed from the contract (the +// wake ledger, the SkillOpt verdicts stub, the single-run graph feed and its +// /events stream, and the Learning page, gitmoot/gitmoot#2593) and any other +// unknown /api/ path answer 404 instead of the 200 SPA shell. +func TestRemovedRoutesNotFound(t *testing.T) { srv := httptest.NewServer(Serve(NewFakeDataSource())) defer srv.Close() @@ -59,6 +60,12 @@ func TestRemovedAPIRoutesNotFound(t *testing.T) { "/api/wakes/summary", "/api/wakes/receipts", "/api/run/eval-reviewer-0007/verdicts", + "/api/runs", + "/api/state", + "/api/state?run=run-noted-001", + "/api/learning/skills", + "/events", + "/events?run=run-noted-001", "/api/no-such-endpoint", "/api", } { @@ -76,27 +83,6 @@ func TestRemovedAPIRoutesNotFound(t *testing.T) { } } -// TestStateRouteRegistered asserts the /api/state route is wired into the mux -// and served by the JSON handler (not the static SPA fallback, which would -// return an HTML index). -func TestStateRouteRegistered(t *testing.T) { - srv := httptest.NewServer(Serve(NewFakeDataSource())) - defer srv.Close() - - resp, err := http.Get(srv.URL + "/api/state") - if err != nil { - t.Fatalf("GET /api/state: %v", err) - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK { - t.Fatalf("GET /api/state status = %d, want 200", resp.StatusCode) - } - if ct := resp.Header.Get("Content-Type"); !strings.HasPrefix(ct, "application/json") { - t.Fatalf("GET /api/state Content-Type = %q, want application/json", ct) - } -} - // TestOrgRoutesRegistered asserts both optional Org API routes are wired into // the mux and served as JSON rather than falling through to the SPA shell. func TestOrgRoutesRegistered(t *testing.T) { diff --git a/sse.go b/sse.go deleted file mode 100644 index a59a448..0000000 --- a/sse.go +++ /dev/null @@ -1,126 +0,0 @@ -package dashboard - -import ( - "encoding/json" - "errors" - "fmt" - "io" - "net/http" - "sync" - "time" -) - -// sseKeepAlive is how often an idle SSE connection emits a comment line so -// proxies and clients keep the stream open. -const sseKeepAlive = 15 * time.Second - -// broker is a small subscribe/broadcast hub. Producers publish State snapshots -// and every live subscriber receives a copy on its own buffered channel. A slow -// subscriber that cannot keep up drops snapshots rather than blocking the -// broadcaster. broker is safe for concurrent use. -type broker struct { - mu sync.Mutex - seq int - subs map[int]chan State -} - -func newBroker() *broker { - return &broker{subs: make(map[int]chan State)} -} - -// subscribe registers a new subscriber, seeds it with the given initial -// snapshot, and returns its channel plus an idempotent cancel func that -// unregisters and closes the channel. -func (b *broker) subscribe(initial State) (<-chan State, func()) { - b.mu.Lock() - defer b.mu.Unlock() - id := b.seq - b.seq++ - ch := make(chan State, 16) - ch <- initial // buffered, never blocks - b.subs[id] = ch - return ch, func() { b.unsubscribe(id) } -} - -func (b *broker) unsubscribe(id int) { - b.mu.Lock() - defer b.mu.Unlock() - if ch, ok := b.subs[id]; ok { - delete(b.subs, id) - close(ch) - } -} - -// publish broadcasts a snapshot to every current subscriber, dropping the -// snapshot for any subscriber whose buffer is full. -func (b *broker) publish(st State) { - b.mu.Lock() - defer b.mu.Unlock() - for _, ch := range b.subs { - select { - case ch <- st: - default: - // Subscriber is behind; drop rather than block the broadcaster. - } - } -} - -// handleEvents serves GET /events?run= as a Server-Sent Events stream. It -// subscribes to the DataSource for the requested run and writes each State -// snapshot as a `data: \n\n` frame, flushing after every write. A -// periodic keep-alive comment holds the connection open, and the subscription -// is cleaned up when the client disconnects. -func (s *server) handleEvents(w http.ResponseWriter, r *http.Request) { - flusher, ok := w.(http.Flusher) - if !ok { - http.Error(w, "streaming unsupported", http.StatusInternalServerError) - return - } - - run := r.URL.Query().Get("run") - ch, cancel, err := s.ds.Subscribe(r.Context(), run) - if err != nil { - if errors.Is(err, ErrRunNotFound) { - http.Error(w, err.Error(), http.StatusNotFound) - return - } - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - defer cancel() - - h := w.Header() - h.Set("Content-Type", "text/event-stream") - h.Set("Cache-Control", "no-cache") - h.Set("Connection", "keep-alive") - w.WriteHeader(http.StatusOK) - flusher.Flush() - - keepAlive := time.NewTicker(sseKeepAlive) - defer keepAlive.Stop() - - ctx := r.Context() - for { - select { - case <-ctx.Done(): - return - case <-keepAlive.C: - if _, err := io.WriteString(w, ": keep-alive\n\n"); err != nil { - return - } - flusher.Flush() - case st, ok := <-ch: - if !ok { - return - } - data, err := json.Marshal(st) - if err != nil { - continue - } - if _, err := fmt.Fprintf(w, "data: %s\n\n", data); err != nil { - return - } - flusher.Flush() - } - } -} diff --git a/sse_test.go b/sse_test.go deleted file mode 100644 index 4245372..0000000 --- a/sse_test.go +++ /dev/null @@ -1,148 +0,0 @@ -package dashboard - -import ( - "bufio" - "context" - "encoding/json" - "net/http" - "net/http/httptest" - "strings" - "testing" - "time" -) - -// readFirstSSEData reads the response body line by line and returns the JSON -// payload of the first `data:` frame. -func readFirstSSEData(t *testing.T, r *bufio.Reader) string { - t.Helper() - deadline := time.After(5 * time.Second) - lines := make(chan string, 1) - errs := make(chan error, 1) - go func() { - for { - line, err := r.ReadString('\n') - if err != nil { - errs <- err - return - } - if strings.HasPrefix(line, "data:") { - lines <- strings.TrimSpace(strings.TrimPrefix(line, "data:")) - return - } - } - }() - select { - case l := <-lines: - return l - case err := <-errs: - t.Fatalf("reading SSE stream: %v", err) - case <-deadline: - t.Fatal("timed out waiting for first SSE data frame") - } - return "" -} - -func TestEventsStreamsInitialState(t *testing.T) { - srv := httptest.NewServer(Serve(NewFakeDataSource())) - defer srv.Close() - - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - req, err := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL+"/events?run="+fakeRunID, nil) - if err != nil { - t.Fatalf("new request: %v", err) - } - resp, err := http.DefaultClient.Do(req) - if err != nil { - t.Fatalf("GET /events: %v", err) - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK { - t.Fatalf("status = %d, want 200", resp.StatusCode) - } - if ct := resp.Header.Get("Content-Type"); !strings.HasPrefix(ct, "text/event-stream") { - t.Fatalf("Content-Type = %q, want text/event-stream", ct) - } - - data := readFirstSSEData(t, bufio.NewReader(resp.Body)) - var st State - if err := json.Unmarshal([]byte(data), &st); err != nil { - t.Fatalf("unmarshal SSE data %q: %v", data, err) - } - if st.RunID != fakeRunID { - t.Fatalf("streamed RunID = %q, want %q", st.RunID, fakeRunID) - } - if len(st.Nodes) == 0 { - t.Fatalf("streamed state has no nodes") - } - // Disconnect; the server-side subscription must be cleaned up. - cancel() -} - -func TestEventsUnknownRunReturns404(t *testing.T) { - srv := httptest.NewServer(Serve(NewFakeDataSource())) - defer srv.Close() - - resp, err := http.Get(srv.URL + "/events?run=does-not-exist") - if err != nil { - t.Fatalf("GET /events: %v", err) - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusNotFound { - t.Fatalf("status = %d, want 404", resp.StatusCode) - } -} - -// TestBrokerBroadcast exercises the hub directly: every subscriber receives a -// published snapshot, and unsubscribing closes the channel. -func TestBrokerBroadcast(t *testing.T) { - b := newBroker() - initial := State{RunID: "r0"} - ch, cancel := b.subscribe(initial) - - if got := <-ch; got.RunID != "r0" { - t.Fatalf("initial snapshot RunID = %q, want r0", got.RunID) - } - - b.publish(State{RunID: "r1"}) - select { - case got := <-ch: - if got.RunID != "r1" { - t.Fatalf("published RunID = %q, want r1", got.RunID) - } - case <-time.After(time.Second): - t.Fatal("did not receive published snapshot") - } - - cancel() - if _, open := <-ch; open { - t.Fatal("channel should be closed after cancel") - } - - // publish after all subscribers are gone must not panic. - b.publish(State{RunID: "r2"}) -} - -// TestBrokerDropsWhenFull verifies a slow subscriber does not block the -// broadcaster: publishing far more than the buffer capacity returns promptly. -func TestBrokerDropsWhenFull(t *testing.T) { - b := newBroker() - _, cancel := b.subscribe(State{RunID: "seed"}) - defer cancel() - - done := make(chan struct{}) - go func() { - for i := 0; i < 1000; i++ { - b.publish(State{RunID: "x"}) - } - close(done) - }() - select { - case <-done: - case <-time.After(2 * time.Second): - t.Fatal("publish blocked on a full subscriber buffer") - } -} diff --git a/web/dist/index.html b/web/dist/index.html index e997d27..5814b96 100644 --- a/web/dist/index.html +++ b/web/dist/index.html @@ -12,25 +12,21 @@ -