From a50e8648f6b060ff0ed21a74c637dec3a1080e82 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 9 Oct 2026 20:20:44 +0100 Subject: [PATCH] Add service status and usage dashboard, refresh externally published edits --- README.md | 8 +- docs/cli.md | 42 ++++++- docs/installation-verification.md | 19 +++ docs/model-results.md | 6 + internal/cli/app.go | 56 +++++++++ internal/cli/app_test.go | 33 +++++ internal/cli/commands.go | 3 +- internal/cli/help.go | 4 +- internal/installation/installation.go | 2 +- internal/pipeline/stats.go | 57 +++++++++ internal/pipeline/stats_test.go | 87 +++++++++++++ internal/protocol/message.go | 2 + internal/protocol/stats.go | 25 ++++ internal/protocol/status.go | 27 ++++ internal/server/status.go | 144 ++++++++++++++++++++++ internal/server/status_test.go | 77 ++++++++++++ internal/server/websocket.go | 9 +- internal/stage/command_diagnostic_test.go | 26 ++++ internal/stage/opencode.go | 38 ++++-- internal/statusview/status.go | 63 ++++++++++ internal/statusview/status_test.go | 21 ++++ internal/tui/create_test.go | 12 +- internal/tui/dashboard.go | 52 +++++--- internal/tui/editor_publication.go | 129 +++++++++++++++++++ internal/tui/editor_publication_test.go | 89 +++++++++++++ internal/tui/home_failed_runs_test.go | 17 +-- internal/tui/model.go | 48 +++++++- internal/tui/pipeline_test.go | 17 ++- internal/tui/stats.go | 57 +++++++++ internal/tui/status.go | 78 ++++++++++++ internal/tui/status_test.go | 77 ++++++++++++ 31 files changed, 1283 insertions(+), 42 deletions(-) create mode 100644 internal/pipeline/stats.go create mode 100644 internal/pipeline/stats_test.go create mode 100644 internal/protocol/stats.go create mode 100644 internal/protocol/status.go create mode 100644 internal/server/status.go create mode 100644 internal/server/status_test.go create mode 100644 internal/stage/command_diagnostic_test.go create mode 100644 internal/statusview/status.go create mode 100644 internal/statusview/status_test.go create mode 100644 internal/tui/editor_publication.go create mode 100644 internal/tui/editor_publication_test.go create mode 100644 internal/tui/stats.go create mode 100644 internal/tui/status.go create mode 100644 internal/tui/status_test.go diff --git a/README.md b/README.md index 3b3bf77d..23b1d224 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,13 @@ From a run, press **p** / **:pipeline** to open the pipeline version it used. Initial run input also records `pwd`. Press **r** / **:retry** on a failed run (or `pipelines retry `) to start a new attempt with the same original input, directory, and pipeline version. Completed side effects are not undone. -On Home, **r** opens the failed-only runs list; both Home panels show the shortcut. +On Home, **r** opens all recent runs; both Home panels show the shortcut. +Wide terminals show runs and pipelines side by side; narrow terminals stack them. +The dashboard includes all-history usage, success rate, average completed duration, +per-pipeline usage, and a 14-day activity chart. CLI: `pipelines stats [--json]`. +Use **:status** for a scrollable server/ntfy popup with health, ports, server-computer +IPs and candidate URLs. CLI: `pipelines status [--json]`. Candidate URLs are not +verified reachable; publishing endpoints and credentials are not disclosed. Retry is offered only after the failed run and its agent have fully stopped. The run inspector shows its actual path above the details, using the same graph renderer as pipeline definitions. **g** focuses the graph (arrows pan, j/k scroll). diff --git a/docs/cli.md b/docs/cli.md index 6ddc5349..8ac73c8d 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -8,6 +8,34 @@ service manager directly. Legacy Python-managed installations require an explici For source development, use `go run ./cmd/pipelines help`; start the native server separately. Set `PIPELINES_URL` or pass `--url` to select another server. +## Dashboard and status + +Home's `r` opens **all** recent runs (latest 50), not only failures. Wide terminals +show runs and pipelines side by side, with stacked panels on narrow terminals. +Run cards show status, duration, start time and the current stage when active. +The usage summary is aggregated over all stored history, including retry attempts: +total/active runs, outcomes, success rate, average completed duration, pipeline +usage and a zero-filled 14-day daily activity chart (UTC). Success rate is +succeeded / (succeeded + failed); active, canceled and interrupted runs are excluded. +The average includes all runs with a finish timestamp. No model cost/token counts +are inferred from missing data. `pipelines stats --json` exposes these aggregates. + +Use `:status` on any TUI page to open server and ntfy information. Scroll with +arrows or `j/k`, PgUp/PgDn; `r` refreshes and Esc closes without changing the page. +`pipelines status [--json]` returns the same server data, including hostname, +start/uptime, the connected server endpoint/port, active interface IPs, ntfy +publishing health, public phone URL/topic/port and candidate URLs. It does not +start services or send a notification. The ntfy health probe has a two-second +deadline; it checks the publishing server, not phone reachability. + +IPs belong to the **server computer or container**, not necessarily the client. +Candidate IP/port combinations are not reachability checks: loopback bindings, +firewalls, container port mapping and TLS can prevent access. ntfy candidates +are only derived when a configured endpoint identifies this computer; remote +ntfy hosts are not misrepresented as local. Publishing URLs and credentials are +never returned. Missing public phone configuration is labeled explicitly. +`pipelines service status` remains the separate OS autostart status command. + ## Draft-first OpenCode workflow ```sh @@ -42,6 +70,13 @@ the existing draft, `v` for server validation, and `s` for submission confirmati `r` refreshes the list. Submission preserves create-only/version guards and rejects drafts belonging to another server. An unnamed draft must be named through OpenCode or CLI `submit --name`. Draft selection/editor completion never publishes it. +On editor exit, the connected TUI checks the authoritative server before asking +to submit. If the draft source is already published (including a newer version +after the draft's base that was subsequently superseded), it skips confirmation +and reloads the latest pipeline/page and Home list. Local-only changes still need +explicit submission. A failed check retains the draft and says publication could +not be verified; it never assumes publication from an editor's exit code. Original +draft conflict guards and retained draft files remain unchanged. New unnamed drafts open OpenCode so the skill can name them. For named drafts, the default editor is OpenCode; explicit `VISUAL`/`EDITOR` overrides remain. @@ -81,7 +116,9 @@ Submitting activates included schedules, so review them before publishing. | Run history/inspection | `runs --json`, `inspect ID --json` | | Cancel | `cancel ID --json` | | Connection and phone settings | `connection --json`, `ping --json` | -| Operational diagnostics | installed `logs`, `status`, `doctor` | +| Server/ntfy status popup | `status --json` | +| Usage statistics/charts | `stats --json` | +| Legacy operational diagnostics | managed `logs`, `doctor` | The TUI does not spawn CLI processes for each request. Both front ends share `internal/client`'s command transport and `internal/draft`'s persisted drafts; @@ -94,7 +131,8 @@ printf '%s' '{"type":"get_pipeline","pipeline_id":1,"version":1}' | pipelines rp Existing protocol commands: `ping`, `list_pipelines`, `get_pipeline`, `list_templates`, `validate`, `submit`, `run_pipeline`, `list_runs`, `get_run`, -`cancel_run`, `retry_run`. See `internal/protocol/message.go` for JSON envelopes. Mutating RPCs +`cancel_run`, `retry_run`, `get_status`. `list_runs` also returns all-history `usage` +aggregates. See `internal/protocol/message.go` for JSON envelopes. Mutating RPCs have real effects; they are not previews. TUI-only animation, scrolling, and transient client logs are presentation, not separate server capabilities. diff --git a/docs/installation-verification.md b/docs/installation-verification.md index dba9cede..59bf695b 100644 --- a/docs/installation-verification.md +++ b/docs/installation-verification.md @@ -85,3 +85,22 @@ and optional file/command permission rules. A native stable no-inference live contract test accepted a `json_schema` message with `retryCount=0` and `noReply`. Real model-driven schema generation, formatter behavior and tool-permission enforcement remain **not tested**. + +Status/dashboard checks execute server and fake ntfy HTTP health endpoints, +CLI JSON/plain status and statistics, all-history SQL aggregation with more than +50 runs, interrupted/unused pipeline counts, zero-filled daily activity, responsive +dashboard columns, and status popup navigation from a run without losing its +selection. Compiled-binary PTY integration also exercises `pipelines status`, +`pipelines stats`, `:status`, dashboard activity and Home's all-runs `r` shortcut. +Candidate URL reachability and phone reception are **not tested** and are not +reported as verified by the UI. + +Editor-return checks query actual published history before showing confirmation; +tests cover a submitted draft followed by a newer publication, unsent edits, +failed/stale verification, and immutable history. Compiled CLI/TUI PTY integration +uses a mock OpenCode editor that submits with the real CLI, then verifies automatic +reload to version 2 without another submission prompt. Another mock editor only +saves changes and still requires confirmation. A real local V2 no-inference +create/wait/delete contract succeeded; the reported remote planner `/wait` failure +could not be reproduced here. Safe CLI failure classification/exit-code reporting +was executed with a failing subprocess fixture. diff --git a/docs/model-results.md b/docs/model-results.md index def5bdf3..7859d856 100644 --- a/docs/model-results.md +++ b/docs/model-results.md @@ -41,6 +41,12 @@ Repair records include the original/candidate response, parsing diagnostic, method and acceptance flag in the bounded transcript preview. `pipelines inspect --json` and the TUI evidence section expose these records. +OpenCode CLI failures include the HTTP status when supplied, process exit code, +and a recognized safe error category (such as session not found or service +unavailable). Arbitrary stderr, provider messages and response bodies are not +copied into the diagnostic. A `/wait` command failure is not evidence that the +agent prompt failed to start, and does not trigger replay of stage work. + ## Optional machine-readable restrictions `tools` enables actions per stage. A stage can narrow edit paths and shell diff --git a/internal/cli/app.go b/internal/cli/app.go index 452ea370..3fb82352 100644 --- a/internal/cli/app.go +++ b/internal/cli/app.go @@ -19,6 +19,7 @@ import ( "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/runinspect" + "git.a-wan.net/alan/pipelines/internal/statusview" "git.a-wan.net/alan/pipelines/internal/tui" tea "github.com/charmbracelet/bubbletea" ) @@ -238,6 +239,17 @@ func (a App) Run(ctx context.Context, args []string) error { c, err := client.Open(dialCtx, o.url) cancel() if err != nil { + if command == "status" && len(pos) == 0 { + s := statusview.Offline(o.url) + if o.json { + if outputErr := a.output(s); outputErr != nil { + return outputErr + } + } else { + fmt.Fprintln(a.Out, strings.Join(statusview.Lines(s), "\n")) + } + return fmt.Errorf("Pipelines server unavailable") + } return err } defer c.Close() @@ -272,6 +284,50 @@ func (a App) Run(ctx context.Context, args []string) error { return pos[0], nil } switch command { + case "stats": + if len(pos) != 0 { + return fmt.Errorf("stats takes no arguments") + } + response, err := call(protocol.Message{Type: protocol.ListRuns}) + if err != nil { + return err + } + if response.Usage == nil { + return fmt.Errorf("server did not return usage statistics; update pipelines-server") + } + if o.json { + return a.output(response.Usage) + } + u := response.Usage + average := "—" + if u.Total > u.Active { + average = time.Duration(u.AverageSeconds * float64(time.Second)).Round(time.Second).String() + } + fmt.Fprintf(a.Out, "Usage · all history\n%d runs · %d succeeded · %d failed · %d canceled · %d interrupted · %d active\nAverage completed duration: %s\n\nPipeline\tRuns\tSucceeded\tFailed\tActive\n", u.Total, u.Succeeded, u.Failed, u.Canceled, u.Interrupted, u.Active, average) + for _, p := range u.Pipelines { + fmt.Fprintf(a.Out, "%s\t%d\t%d\t%d\t%d\n", p.Name, p.Total, p.Succeeded, p.Failed, p.Active) + } + fmt.Fprintln(a.Out, "\nDaily activity (UTC)") + for _, d := range u.Days { + fmt.Fprintf(a.Out, "%s\t%d\n", d.Date, d.Runs) + } + return nil + case "status": + if len(pos) != 0 { + return fmt.Errorf("status takes no arguments") + } + response, err := call(protocol.Message{Type: protocol.GetStatus}) + if err != nil { + return err + } + if response.Status == nil { + return fmt.Errorf("server did not return status; update pipelines-server") + } + if o.json { + return a.output(response.Status) + } + _, err = fmt.Fprintln(a.Out, strings.Join(statusview.Lines(response.Status), "\n")) + return err case "debug": value, err := one() if err != nil { diff --git a/internal/cli/app_test.go b/internal/cli/app_test.go index fba7e456..ca382bce 100644 --- a/internal/cli/app_test.go +++ b/internal/cli/app_test.go @@ -53,6 +53,39 @@ func TestCLIHelpDoesNotNeedServer(t *testing.T) { } } +func TestCLIStatusAndStatistics(t *testing.T) { + t.Setenv("PIPELINES_NTFY_URL", "") + run, _, _ := setup(t) + output, err := run("status") + if err != nil { + t.Fatal(err) + } + for _, text := range []string{"Pipelines server", "ntfy", "not configured", "Server computer IPs", "Port:"} { + if !strings.Contains(output, text) { + t.Fatal("missing CLI status", text, output) + } + } + output, err = run("status", "--json") + if err != nil { + t.Fatal(err) + } + var status protocol.SystemStatus + if err := json.Unmarshal([]byte(output), &status); err != nil || status.Server.Health != "healthy" || status.Server.Port == "" { + t.Fatal(output, err) + } + output, err = run("stats", "--json") + if err != nil { + t.Fatal(err) + } + var usage protocol.UsageStats + if err := json.Unmarshal([]byte(output), &usage); err != nil || usage.Total != 0 || len(usage.Days) != 14 { + t.Fatal(output, err) + } + if _, err := run("status", "extra"); err == nil { + t.Fatal("status accepted extra positional") + } +} + func TestCLIDraftValidateSubmitAndImmutableHistory(t *testing.T) { run, store, _ := setup(t) output, err := run("new", "example", "--template", "bug-fix", "--json") diff --git a/internal/cli/commands.go b/internal/cli/commands.go index beeacb31..b4ab080b 100644 --- a/internal/cli/commands.go +++ b/internal/cli/commands.go @@ -22,6 +22,8 @@ var usage = map[string]string{ "debug": "debug [--json | --no-open]", "connection": "connection [--json]", "ping": "ping [--json]", + "status": "status [--json]", + "stats": "stats [--json]", "rpc": "rpc ", "tui": "tui", } @@ -44,7 +46,6 @@ var serviceHelp = map[string]string{ "start": "Start the per-user native pipeline server and this installation's Docker ntfy together.", "stop": "Stop both services. Active execution is interrupted; file changes are not rolled back.", "restart": "Stop, then start both services, retaining definitions, run history, and credentials.", - "status": "Show pipeline/ntfy health and the public phone connection (no credentials).", "doctor": "Check both health endpoints and host OpenCode API discovery; does not execute a model.", "logs": "Follow the native server log. Press Ctrl+C to stop following.", "uninstall": "Remove verified Go binaries from this command's bin directory; retain data and drafts. --yes skips confirmation. Existing managed installations can be selected explicitly with PIPELINES_PREFIX.", diff --git a/internal/cli/help.go b/internal/cli/help.go index 0a9e2eca..fb04e943 100644 --- a/internal/cli/help.go +++ b/internal/cli/help.go @@ -28,6 +28,8 @@ EXECUTE debug Ask OpenCode questions about a recorded or active run draft-delete Delete a selected retained draft and its metadata connection Server and public phone connection (no secrets) + status Server/ntfy health, ports and server computer IPs + stats All-history usage, pipeline counts and daily activity ping Check the server connection rpc Send any protocol command (automation escape hatch) @@ -40,7 +42,7 @@ SERVER / INTERFACE uninstall [--yes] Remove verified Go binaries; retain data and drafts Legacy managed installs: select PIPELINES_PREFIX explicitly for start/stop, -restart/status/doctor/logs and retained-data or --purge uninstallation. +restart/doctor/logs and retained-data or --purge uninstallation. Options: --url ws://127.0.0.1:8080/connect --json --help Use pipelines help for its flags. Names with spaces must be quoted. diff --git a/internal/installation/installation.go b/internal/installation/installation.go index 5bcc0e9d..464978b3 100644 --- a/internal/installation/installation.go +++ b/internal/installation/installation.go @@ -68,7 +68,7 @@ func (c Config) Environment() error { func Service(action string) bool { switch action { - case "start", "stop", "restart", "status", "doctor", "logs", "uninstall": + case "start", "stop", "restart", "doctor", "logs", "uninstall": return true } return false diff --git a/internal/pipeline/stats.go b/internal/pipeline/stats.go new file mode 100644 index 00000000..d779c3a5 --- /dev/null +++ b/internal/pipeline/stats.go @@ -0,0 +1,57 @@ +package pipeline + +import ( + "context" + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +const countsSQL = `COUNT(r.id),COALESCE(SUM(r.status='succeeded'),0),COALESCE(SUM(r.status='failed'),0),COALESCE(SUM(r.status='canceled'),0),COALESCE(SUM(r.status='interrupted'),0),COALESCE(SUM(r.status IN ('queued','running')),0),COALESCE(AVG(CASE WHEN r.finished_at IS NOT NULL THEN MAX(0,(julianday(r.finished_at)-julianday(r.created_at))*86400) END),0)` + +func (s *Store) UsageStats(ctx context.Context) (*protocol.UsageStats, error) { + tx, err := s.db.BeginTx(ctx, nil) + if err != nil { + return nil, err + } + defer tx.Rollback() + stats := &protocol.UsageStats{Pipelines: []protocol.PipelineUsage{}, Days: []protocol.DailyUsage{}} + c := &stats.RunCounts + if err := tx.QueryRowContext(ctx, "SELECT "+countsSQL+" FROM pipeline_runs r").Scan(&c.Total, &c.Succeeded, &c.Failed, &c.Canceled, &c.Interrupted, &c.Active, &c.AverageSeconds); err != nil { + return nil, err + } + rows, err := tx.QueryContext(ctx, "SELECT p.id,p.name,"+countsSQL+" FROM pipelines p LEFT JOIN pipeline_runs r ON r.pipeline_id=p.id GROUP BY p.id ORDER BY COUNT(r.id) DESC,p.name") + if err != nil { + return nil, err + } + for rows.Next() { + var p protocol.PipelineUsage + c := &p.RunCounts + if err := rows.Scan(&p.PipelineID, &p.Name, &c.Total, &c.Succeeded, &c.Failed, &c.Canceled, &c.Interrupted, &c.Active, &c.AverageSeconds); err != nil { + rows.Close() + return nil, err + } + stats.Pipelines = append(stats.Pipelines, p) + } + if err := rows.Err(); err != nil { + rows.Close() + return nil, err + } + rows.Close() + rows, err = tx.QueryContext(ctx, `WITH RECURSIVE days(day) AS (SELECT date('now','-13 days') UNION ALL SELECT date(day,'+1 day') FROM days WHERE day=day AND r.created_at .01 { + t.Fatal("incorrect all-history aggregation", stats) + } + if len(stats.Pipelines) != 2 || stats.Pipelines[0].Total != 60 || stats.Pipelines[1].Total != 0 { + t.Fatal("missing unused pipeline or per-pipeline usage", stats.Pipelines) + } + total := 0 + for _, day := range stats.Days { + total += day.Runs + } + if total != 60 { + t.Fatal("daily totals", stats.Days) + } + latest, err := store.ListRuns(ctx) + if err != nil || len(latest) != 50 { + t.Fatal("fixture must exceed latest-runs cap", latest, err) + } +} diff --git a/internal/protocol/message.go b/internal/protocol/message.go index f45f0e96..182856c3 100644 --- a/internal/protocol/message.go +++ b/internal/protocol/message.go @@ -94,6 +94,8 @@ type PipelineSummary struct { // Message is the JSON envelope shared by the server and client. type Message struct { + Usage *UsageStats `json:"usage,omitempty"` + Status *SystemStatus `json:"status,omitempty"` Graph *Graph `json:"graph,omitempty"` ExpectedVersion int `json:"expected_version,omitempty"` Templates []Template `json:"templates,omitempty"` diff --git a/internal/protocol/stats.go b/internal/protocol/stats.go new file mode 100644 index 00000000..195d034a --- /dev/null +++ b/internal/protocol/stats.go @@ -0,0 +1,25 @@ +package protocol + +type RunCounts struct { + Total int `json:"total"` + Succeeded int `json:"succeeded"` + Failed int `json:"failed"` + Canceled int `json:"canceled"` + Interrupted int `json:"interrupted"` + Active int `json:"active"` + AverageSeconds float64 `json:"average_seconds"` +} +type PipelineUsage struct { + PipelineID int64 `json:"pipeline_id"` + Name string `json:"name"` + RunCounts +} +type DailyUsage struct { + Date string `json:"date"` + Runs int `json:"runs"` +} +type UsageStats struct { + RunCounts + Pipelines []PipelineUsage `json:"pipelines"` + Days []DailyUsage `json:"days"` +} diff --git a/internal/protocol/status.go b/internal/protocol/status.go new file mode 100644 index 00000000..9d67035f --- /dev/null +++ b/internal/protocol/status.go @@ -0,0 +1,27 @@ +package protocol + +const GetStatus = "get_status" +const Status = "status" + +type NetworkAddress struct { + Interface string `json:"interface"` + IP string `json:"ip"` +} + +type ServiceStatus struct { + Health string `json:"health"` + URL string `json:"url,omitempty"` + Port string `json:"port,omitempty"` + Candidates []string `json:"candidate_urls,omitempty"` +} + +type SystemStatus struct { + Hostname string `json:"hostname"` + StartedAt string `json:"started_at"` + Uptime string `json:"uptime"` + Addresses []NetworkAddress `json:"addresses"` + Server ServiceStatus `json:"server"` + Ntfy ServiceStatus `json:"ntfy"` + Topic string `json:"topic,omitempty"` + Note string `json:"note"` +} diff --git a/internal/server/status.go b/internal/server/status.go new file mode 100644 index 00000000..7d8c607b --- /dev/null +++ b/internal/server/status.go @@ -0,0 +1,144 @@ +package server + +import ( + "context" + "encoding/json" + "io" + "net" + "net/http" + "net/url" + "os" + "sort" + "time" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +func networkAddresses() []protocol.NetworkAddress { + result := []protocol.NetworkAddress{} + interfaces, _ := net.Interfaces() + for _, iface := range interfaces { + if iface.Flags&net.FlagUp == 0 { + continue + } + addresses, _ := iface.Addrs() + for _, address := range addresses { + ip, _, err := net.ParseCIDR(address.String()) + if err != nil || ip.IsUnspecified() { + continue + } + text := ip.String() + if ip.IsLinkLocalUnicast() && ip.To4() == nil { + text += "%" + iface.Name + } + result = append(result, protocol.NetworkAddress{Interface: iface.Name, IP: text}) + } + } + sort.Slice(result, func(i, j int) bool { + if result[i].Interface != result[j].Interface { + return result[i].Interface < result[j].Interface + } + return result[i].IP < result[j].IP + }) + return result +} + +func endpointPort(u *url.URL) string { + if port := u.Port(); port != "" { + return port + } + if u.Scheme == "https" || u.Scheme == "wss" { + return "443" + } + return "80" +} + +func candidateURLs(addresses []protocol.NetworkAddress, scheme, port, path string) []string { + result := []string{} + for _, address := range addresses { + u := url.URL{Scheme: scheme, Host: net.JoinHostPort(address.IP, port), Path: path} + result = append(result, u.String()) + } + return result +} + +func localEndpoint(u *url.URL, hostname string, addresses []protocol.NetworkAddress) bool { + host := u.Hostname() + if host == "localhost" || host == hostname || net.ParseIP(host).IsLoopback() { + return true + } + for _, address := range addresses { + if host == address.IP { + return true + } + } + return false +} + +func systemStatus(r *http.Request, started time.Time, runErr error) *protocol.SystemStatus { + hostname, _ := os.Hostname() + s := &protocol.SystemStatus{Hostname: hostname, StartedAt: started.UTC().Format(time.RFC3339), Uptime: time.Since(started).Round(time.Second).String(), Addresses: networkAddresses(), Server: protocol.ServiceStatus{Health: "healthy"}, Ntfy: protocol.ServiceStatus{Health: "not configured"}, Note: "Addresses belong to the server computer (or container). Candidate URLs are not reachability checks; binding, firewalls, port mapping and TLS may differ."} + if runErr != nil { + s.Server.Health = "execution unavailable" + } + if address, ok := r.Context().Value(http.LocalAddrContextKey).(net.Addr); ok { + _, port, err := net.SplitHostPort(address.String()) + if err == nil { + scheme := "ws" + if r.TLS != nil { + scheme = "wss" + } + s.Server.URL = (&url.URL{Scheme: scheme, Host: address.String(), Path: "/connect"}).String() + s.Server.Port = port + s.Server.Candidates = candidateURLs(s.Addresses, scheme, port, "/connect") + } + } + endpoints := notificationEndpoints() + if len(endpoints) == 0 { + return s + } + s.Topic = endpoints[0].Topic + s.Ntfy.Health = "not checked" + // Only the validated public base is returned. Publishing URL and tokens + // never leave the server, even on probe failure. + s.Ntfy.URL = endpoints[0].ServerURL + if u, err := url.Parse(s.Ntfy.URL); err == nil && u.Host != "" { + s.Ntfy.Port = endpointPort(u) + if localEndpoint(u, hostname, s.Addresses) { + s.Ntfy.Candidates = candidateURLs(s.Addresses, u.Scheme, s.Ntfy.Port, "/"+s.Topic) + } + } + publish, err := url.Parse(os.Getenv("PIPELINES_NTFY_URL")) + if err != nil { + return s + } + if s.Ntfy.URL == "" && localEndpoint(publish, hostname, s.Addresses) { + s.Ntfy.Port = endpointPort(publish) + s.Ntfy.Candidates = candidateURLs(s.Addresses, publish.Scheme, s.Ntfy.Port, "/"+s.Topic) + } + publish.Path = "/v1/health" + publish.RawPath = "" + publish.RawQuery = "" + publish.Fragment = "" + ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second) + defer cancel() + req, err := http.NewRequestWithContext(ctx, http.MethodGet, publish.String(), nil) + if err != nil { + return s + } + client := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }} + response, err := client.Do(req) + s.Ntfy.Health = "unreachable" + if err != nil { + return s + } + defer response.Body.Close() + s.Ntfy.Health = "unhealthy" + var health struct { + Healthy bool `json:"healthy"` + } + if response.StatusCode == http.StatusOK && json.NewDecoder(io.LimitReader(response.Body, 4096)).Decode(&health) == nil && health.Healthy { + s.Ntfy.Health = "healthy" + } + return s +} diff --git a/internal/server/status_test.go b/internal/server/status_test.go new file mode 100644 index 00000000..b3b05363 --- /dev/null +++ b/internal/server/status_test.go @@ -0,0 +1,77 @@ +package server + +import ( + "context" + "encoding/json" + "git.a-wan.net/alan/pipelines/internal/client" + "git.a-wan.net/alan/pipelines/internal/protocol" + "net/http" + "net/http/httptest" + "net/url" + "strings" + "testing" + "time" +) + +func TestStatusRPCReportsServerAndNtfyWithoutSecrets(t *testing.T) { + ntfy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/v1/health" { + t.Error("unexpected probe", r.URL.Path) + } + w.Write([]byte(`{"healthy":true}`)) + })) + defer ntfy.Close() + t.Setenv("PIPELINES_NTFY_URL", ntfy.URL+"/phone") + t.Setenv("PIPELINES_NTFY_PUBLIC_URL", ntfy.URL) + t.Setenv("PIPELINES_NTFY_TOKEN", "never-return-this-token") + srv := httptest.NewServer(Handler(context.Background())) + defer srv.Close() + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + c, err := client.Open(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect") + if err != nil { + t.Fatal(err) + } + defer c.Close() + response, err := c.Call(ctx, protocol.Message{Type: protocol.GetStatus}) + if err != nil { + t.Fatal(err) + } + s := response.Status + if s == nil || s.Server.Health != "healthy" || s.Ntfy.Health != "healthy" || s.Ntfy.URL != ntfy.URL || s.Topic != "phone" || len(s.Addresses) == 0 || s.Hostname == "" || s.Server.Port == "" || s.Ntfy.Port == "" || s.StartedAt == "" || len(s.Ntfy.Candidates) == 0 { + t.Fatal("incomplete status", s) + } + encoded, _ := json.Marshal(s) + if strings.Contains(string(encoded), "never-return") { + t.Fatal("credentials leaked", string(encoded)) + } + if !strings.Contains(s.Server.URL, s.Server.Port) || !strings.Contains(s.Note, "not reachability checks") { + t.Fatal("ports or safety disclaimer missing", s) + } +} + +func TestStatusUnconfiguredUnhealthyAndIPv6Candidates(t *testing.T) { + t.Setenv("PIPELINES_NTFY_URL", "") + t.Setenv("PIPELINES_NTFY_PUBLIC_URL", "") + r := httptest.NewRequest("GET", "http://localhost/connect", nil) + if s := systemStatus(r, time.Now(), nil); s.Ntfy.Health != "not configured" { + t.Fatal(s) + } + for _, body := range []string{`{"healthy":false}`, `not-json`} { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(body)) })) + t.Setenv("PIPELINES_NTFY_URL", srv.URL+"/topic") + s := systemStatus(r, time.Now(), nil) + srv.Close() + if s.Ntfy.Health != "unhealthy" || s.Ntfy.URL != "" || s.Ntfy.Port == "" { + t.Fatal(s) + } + } + addresses := []protocol.NetworkAddress{{Interface: "test", IP: "::1"}, {Interface: "test", IP: "192.0.2.1"}} + urls := candidateURLs(addresses, "http", "8090", "/topic") + if urls[0] != "http://[::1]:8090/topic" || urls[1] != "http://192.0.2.1:8090/topic" { + t.Fatal(urls) + } + if localEndpoint(&url.URL{Host: "remote.example:8090", Scheme: "http"}, "test", addresses) { + t.Fatal("remote endpoint guessed local") + } +} diff --git a/internal/server/websocket.go b/internal/server/websocket.go index ff02685b..0ff57df7 100644 --- a/internal/server/websocket.go +++ b/internal/server/websocket.go @@ -17,6 +17,7 @@ import ( // Handler serves the persistent connection. Canceling ctx stops active clients. func Handler(ctx context.Context, stores ...*pipeline.Store) http.Handler { + started := time.Now() mux := http.NewServeMux() var runs *runner var runErr error @@ -66,7 +67,10 @@ func Handler(ctx context.Context, stores ...*pipeline.Store) http.Handler { if request.Type == protocol.List || request.Type == protocol.Submit { response.NotificationEndpoints = notificationEndpoints() } - if request.Type == protocol.Ping { + if request.Type == protocol.GetStatus { + response.Type = protocol.Status + response.Status = systemStatus(r, started, runErr) + } else if request.Type == protocol.Ping { response.Time = time.Now().UTC().Format(time.RFC3339) } else if request.Type == protocol.ListTemplates { response.Type = protocol.Templates @@ -133,6 +137,9 @@ func Handler(ctx context.Context, stores ...*pipeline.Store) http.Handler { case protocol.ListRuns: response.Type = protocol.Runs response.Runs, err = runs.listRuns() + if err == nil { + response.Usage, err = stores[0].UsageStats(ctx) + } case protocol.GetRun: response.Type = protocol.Run response.RunDetail, err = runs.getRun(request.RunID) diff --git a/internal/stage/command_diagnostic_test.go b/internal/stage/command_diagnostic_test.go new file mode 100644 index 00000000..d0bd8ef3 --- /dev/null +++ b/internal/stage/command_diagnostic_test.go @@ -0,0 +1,26 @@ +package stage + +import ( + "context" + "os" + "path/filepath" + "strings" + "testing" +) + +func TestActualCLIErrorReportsSafeClassificationAndExitCode(t *testing.T) { + path := filepath.Join(t.TempDir(), "opencode") + script := `#!/bin/sh +echo '{"_tag":"SessionNotFoundError","message":"secret credential and private project data"}' +echo 'HTTP 404 Not Found; secret stderr' >&2 +exit 7 +` + if err := os.WriteFile(path, []byte(script), 0700); err != nil { + t.Fatal(err) + } + t.Setenv("PIPELINES_OPENCODE", path) + _, err := (OpenCodeClient{Directory: t.TempDir()}).call(context.Background(), "post", "/api/experimental/session/ses_test/wait", nil) + if err == nil || !strings.Contains(err.Error(), "HTTP 404") || !strings.Contains(err.Error(), "CLI exit code 7") || !strings.Contains(err.Error(), "session not found") || strings.Contains(err.Error(), "secret") || strings.Contains(err.Error(), "private project") { + t.Fatal("unsafe or opaque command error", err) + } +} diff --git a/internal/stage/opencode.go b/internal/stage/opencode.go index 1e95941c..f3056d13 100644 --- a/internal/stage/opencode.go +++ b/internal/stage/opencode.go @@ -32,18 +32,23 @@ type OpenCodeClient struct { } type openCodeError struct { - operation string - status int - cause error + operation string + status int + cause error + diagnostic string } func (e *openCodeError) Unwrap() error { return e.cause } func (e *openCodeError) Error() string { - if e.status > 0 { - return fmt.Sprintf("OpenCode %s failed (HTTP %d)", e.operation, e.status) + detail := "" + if e.diagnostic != "" { + detail = ": " + e.diagnostic } - return fmt.Sprintf("OpenCode %s command failed", e.operation) + if e.status > 0 { + return fmt.Sprintf("OpenCode %s failed (HTTP %d)%s", e.operation, e.status, detail) + } + return fmt.Sprintf("OpenCode %s command failed%s", e.operation, detail) } func (e *openCodeError) Retryable() bool { return e.status == 429 || e.status == 502 || e.status == 503 || e.status == 504 @@ -104,7 +109,7 @@ func (c OpenCodeClient) call(ctx context.Context, method, path string, body any) status, _ = strconv.Atoi(match[1]) } // Do not embed provider responses, prompts, credentials, or CLI logs in errors. - return nil, &openCodeError{operation: method + " " + path, status: status} + return nil, &openCodeError{operation: method + " " + path, status: status, diagnostic: safeCommandDiagnostic(err, stdout.Bytes())} } if stdout.overflow { return nil, fmt.Errorf("OpenCode response exceeds 1 MiB limit") @@ -112,6 +117,25 @@ func (c OpenCodeClient) call(ctx context.Context, method, path string, body any) return stdout.Bytes(), nil } +func safeCommandDiagnostic(err error, data []byte) string { + var exit *exec.ExitError + detail := "could not execute CLI" + if errors.As(err, &exit) { + detail = fmt.Sprintf("CLI exit code %d", exit.ExitCode()) + } + var response struct { + Tag string `json:"_tag"` + } + if json.Unmarshal(data, &response) == nil { + // Never copy provider messages, arbitrary stderr or response bodies. + reasons := map[string]string{"SessionNotFoundError": "session not found on the selected OpenCode service", "LocationNotFoundError": "session location not found", "ServiceUnavailableError": "OpenCode service unavailable", "UnauthorizedError": "OpenCode service authentication failed", "InvalidRequestError": "OpenCode rejected the request", "SessionBusyError": "OpenCode session is busy"} + if reason := reasons[response.Tag]; reason != "" { + detail += "; " + reason + } + } + return detail +} + func (c OpenCodeClient) Generate(ctx context.Context, config ModelConfig, prompt string) (text string, err error) { if c.command == nil { connection, err := opencode.Open(ctx, opencode.Executable(), c.Directory, c.Backend) diff --git a/internal/statusview/status.go b/internal/statusview/status.go new file mode 100644 index 00000000..1847a8e7 --- /dev/null +++ b/internal/statusview/status.go @@ -0,0 +1,63 @@ +package statusview + +import ( + "fmt" + "git.a-wan.net/alan/pipelines/internal/protocol" + "net/url" +) + +func Offline(endpoint string) *protocol.SystemStatus { + s := &protocol.SystemStatus{Server: protocol.ServiceStatus{Health: "unreachable"}, Ntfy: protocol.ServiceStatus{Health: "unknown (server unavailable)"}, Note: "Server information and ntfy configuration cannot be retrieved while disconnected."} + if u, err := url.Parse(endpoint); err == nil { + u.User = nil + u.RawQuery = "" + u.Fragment = "" + s.Server.URL = u.String() + s.Server.Port = u.Port() + } + return s +} + +// Lines is shared by the plain CLI and TUI popup. No credentials are included. +func Lines(s *protocol.SystemStatus) []string { + if s == nil { + return []string{"No status returned by server."} + } + lines := []string{"Pipelines server", " Health: " + s.Server.Health} + if s.Hostname != "" { + lines = append(lines, " Host: "+s.Hostname, " Started: "+s.StartedAt+" · uptime "+s.Uptime) + } + lines = append(lines, " Connection: "+s.Server.URL, " Port: "+s.Server.Port, "", "ntfy", " Publishing health: "+s.Ntfy.Health) + if s.Ntfy.URL != "" { + lines = append(lines, " Phone server: "+s.Ntfy.URL) + } + if s.Topic != "" { + lines = append(lines, " Topic: "+s.Topic) + } + if s.Ntfy.Port != "" { + lines = append(lines, " Port: "+s.Ntfy.Port) + } + if s.Topic != "" && s.Ntfy.URL == "" { + lines = append(lines, " Phone address not configured (PIPELINES_NTFY_PUBLIC_URL).") + } + lines = append(lines, "", "Server computer IPs") + for _, address := range s.Addresses { + lines = append(lines, fmt.Sprintf(" %s · %s", address.Interface, address.IP)) + } + if len(s.Addresses) == 0 { + lines = append(lines, " No address information available.") + } + if len(s.Server.Candidates) > 0 { + lines = append(lines, "", "Candidate Pipelines URLs (not verified)") + for _, candidate := range s.Server.Candidates { + lines = append(lines, " "+candidate) + } + } + if len(s.Ntfy.Candidates) > 0 { + lines = append(lines, "", "Candidate ntfy topic URLs (not verified)") + for _, candidate := range s.Ntfy.Candidates { + lines = append(lines, " "+candidate) + } + } + return append(lines, "", s.Note) +} diff --git a/internal/statusview/status_test.go b/internal/statusview/status_test.go new file mode 100644 index 00000000..53828edf --- /dev/null +++ b/internal/statusview/status_test.go @@ -0,0 +1,21 @@ +package statusview + +import ( + "strings" + "testing" +) + +func TestOfflineStatusIncludesBothServicesAndRedactsURLCredentials(t *testing.T) { + s := Offline("ws://user:password@localhost:8080/connect?token=secret#secret") + text := strings.Join(Lines(s), "\n") + for _, secret := range []string{"password", "token=", "secret", "user:"} { + if strings.Contains(text, secret) { + t.Fatal("credentials exposed", text) + } + } + for _, expected := range []string{"Pipelines server", "unreachable", "ntfy", "unknown (server unavailable)", "8080"} { + if !strings.Contains(text, expected) { + t.Fatal("missing offline information", expected, text) + } + } +} diff --git a/internal/tui/create_test.go b/internal/tui/create_test.go index 77f5b426..58dd2315 100644 --- a/internal/tui/create_test.go +++ b/internal/tui/create_test.go @@ -48,7 +48,15 @@ func TestCreateAndSubmit(t *testing.T) { for _, draft := range []string{source, source, "invalid"} { updated, send := m.Update(editedMsg{name: "demo", path: "draft.md", source: draft}) m = updated.(Model) - if send != nil || !m.confirmOpen { + if send == nil || m.confirmOpen { + t.Fatal("editor publication check missing") + } + updated, _ = m.Update(send()) + m = updated.(Model) + if !m.confirmOpen && strings.Contains(m.submitNotice, "Already published") { + continue + } + if !m.confirmOpen { t.Fatal("editor completion bypassed explicit approval") } updated, send = m.updateConfirm(tea.KeyMsg{Type: tea.KeyEnter}) @@ -61,7 +69,7 @@ func TestCreateAndSubmit(t *testing.T) { m = updated.(Model) } text := strings.Join(m.lines, "\n") - for _, expected := range []string{"Submitted demo: version 1", "no changes", "unexpected text outside a block"} { + for _, expected := range []string{"Submitted demo: version 1", "Already published", "unexpected text outside a block"} { if !strings.Contains(text, expected) { t.Fatalf("missing %q in %s", expected, text) } diff --git a/internal/tui/dashboard.go b/internal/tui/dashboard.go index a1b4811e..fa748c74 100644 --- a/internal/tui/dashboard.go +++ b/internal/tui/dashboard.go @@ -13,7 +13,7 @@ import ( "git.a-wan.net/alan/pipelines/internal/protocol" ) -var commands = []string{":logs", ":new", ":drafts", ":templates", ":refresh", ":ping", ":edit", ":submit", ":versions", ":run", ":runs", ":retry", ":debug", ":pipeline", ":home", ":quit"} +var commands = []string{":status", ":logs", ":new", ":drafts", ":templates", ":refresh", ":ping", ":edit", ":submit", ":versions", ":run", ":runs", ":retry", ":debug", ":pipeline", ":home", ":quit"} func (m Model) footerContent() string { label := fmt.Sprintf(" LOGS %d · :logs ", len(m.lines)) @@ -39,17 +39,27 @@ func (m Model) footerContent() string { } func (m Model) homeLines() []string { - runs := []string{bar("Latest runs", fmt.Sprint(len(m.runs)), max(1, m.width-4), true), - "r failed runs", - lipgloss.NewStyle().Foreground(themeMuted).Render("Pipeline · Version · Outcome · Finished"), - "Inspect with :runs; open a pipeline then :run to start."} + panelWidth := m.width + if m.width >= 96 { + panelWidth = (m.width - 1) / 2 + } + runs := []string{bar("Latest runs", fmt.Sprint(len(m.runs)), max(1, panelWidth-4), true), + "r all runs · :status server & ntfy", + lipgloss.NewStyle().Foreground(themeMuted).Render("Pipeline · Version · Outcome · Duration"), + "Inspect with :runs; use :run in a pipeline."} if len(m.runs) == 0 { runs = append(runs, "No runs yet.") } - for _, r := range m.runs { - runs = append(runs, fmt.Sprintf("#%d %s · v%d · %s · %s", r.ID, r.Name, r.Version, m.progress(r.Status), r.FinishedAt)) + for _, r := range m.runs[:min(8, len(m.runs))] { + runs = append(runs, fmt.Sprintf("#%d %s · v%d · %s · %s", r.ID, r.Name, r.Version, m.progress(r.Status), runDuration(r.CreatedAt, r.FinishedAt)), " Started: "+dateLabel(r.CreatedAt)) + if r.CurrentStage != "" && (r.Status == "running" || r.Status == "queued") { + runs = append(runs, " Stage: "+r.CurrentStage) + } } - lines := []string{bar("Existing pipelines", fmt.Sprintf("%d", len(m.pipelines)), max(1, m.width-4), true), "r failed runs", ""} + if len(m.runs) > 8 { + runs = append(runs, "More runs: r or :runs (latest 50)") + } + lines := []string{bar("Existing pipelines", fmt.Sprintf("%d", len(m.pipelines)), max(1, panelWidth-4), true), "r all runs · / search · n new", ""} if !m.loaded { lines = append(lines, "Waiting for pipeline data…") } else if len(m.pipelines) == 0 { @@ -58,22 +68,34 @@ func (m Model) homeLines() []string { for _, p := range m.pipelines { lines = append(lines, fmt.Sprintf("%s v%d · %d stages", p.Name, p.Version, p.Stages), - " Created: "+p.CreatedAt+" · Updated: "+p.UpdatedAt) + " Created: "+dateLabel(p.CreatedAt), " Updated: "+dateLabel(p.UpdatedAt)) + if m.usage != nil { + for _, u := range m.usage.Pipelines { + if u.PipelineID == p.ID { + lines = append(lines, fmt.Sprintf(" %d runs · %s · avg %s", u.Total, successLabel(u.RunCounts), averageLabel(u.RunCounts))) + break + } + } + } } } if m.width < 8 { return append(append(runs, ""), lines...) } - widget := func(rows []string) []string { + widget := func(rows []string, width int) string { for i := range rows { - rows[i] = ansi.Truncate(rows[i], m.width-4, "…") + rows[i] = ansi.Truncate(rows[i], max(1, width-4), "…") } content := lipgloss.NewStyle().Border(lipgloss.RoundedBorder()). - BorderForeground(themeMuted).Padding(0, 1).Width(m.width - 2). + BorderForeground(themeMuted).Padding(0, 1).Width(width - 2). Render(strings.Join(rows, "\n")) - return strings.Split(content, "\n") + return content } - return append(append(widget(runs), ""), widget(lines)...) + panels := widget(runs, panelWidth) + "\n\n" + widget(lines, panelWidth) + if m.width >= 96 { + panels = lipgloss.JoinHorizontal(lipgloss.Top, widget(runs, panelWidth), " ", widget(lines, m.width-panelWidth-1)) + } + return strings.Split(widget(m.usageLines(), m.width)+"\n"+panels, "\n") } func (m Model) homePage() bool { @@ -156,6 +178,8 @@ func (m Model) updatePalette(msg tea.KeyMsg) (tea.Model, tea.Cmd) { m.paletteOpen = false m.keys.palette = false switch command { + case ":status": + return m.openStatus() case ":debug": return m.debugRun() case ":retry": diff --git a/internal/tui/editor_publication.go b/internal/tui/editor_publication.go new file mode 100644 index 00000000..4210ba3b --- /dev/null +++ b/internal/tui/editor_publication.go @@ -0,0 +1,129 @@ +package tui + +import ( + "context" + "fmt" + "git.a-wan.net/alan/pipelines/internal/client" + "git.a-wan.net/alan/pipelines/internal/protocol" + tea "github.com/charmbracelet/bubbletea" + "strings" + "time" +) + +type editorPublicationMsg struct { + edit editedMsg + generation uint64 + pipelines []protocol.PipelineSummary + detail *protocol.PipelineDetail + published bool + err error +} + +func checkEditorPublication(ctx context.Context, endpoint string, edit editedMsg, generation uint64) tea.Cmd { + return func() tea.Msg { + result := editorPublicationMsg{edit: edit, generation: generation} + ctx, cancel := context.WithTimeout(ctx, 10*time.Second) + defer cancel() + c, err := client.Open(ctx, endpoint) + if err != nil { + result.err = err + return result + } + defer c.Close() + list, err := c.Call(ctx, protocol.Message{Type: protocol.List}) + if err != nil { + result.err = err + return result + } + result.pipelines = list.Pipelines + for _, p := range list.Pipelines { + if p.Name != strings.TrimSpace(edit.name) { + continue + } + response, err := c.Call(ctx, protocol.Message{Type: protocol.GetPipeline, PipelineID: p.ID}) + if err != nil { + result.err = err + return result + } + result.detail = response.Detail + if result.detail == nil { + result.err = fmt.Errorf("server returned no pipeline detail") + return result + } + if result.detail.Source == edit.source { + result.published = true + return result + } + // If another version was published afterward, the editor's submitted + // source still must not be offered for duplicate submission. Only + // inspect versions newer than this draft's immutable starting point. + for _, v := range result.detail.Versions { + if v.Version <= edit.baseVersion || v.Version == result.detail.Version { + continue + } + response, err := c.Call(ctx, protocol.Message{Type: protocol.GetPipeline, PipelineID: p.ID, Version: v.Version}) + if err != nil { + result.err = err + return result + } + if response.Detail != nil && response.Detail.Source == edit.source { + result.published = true + return result + } + } + return result + } + return result + } +} + +func (m Model) finishEditorPublication(msg editorPublicationMsg) (tea.Model, tea.Cmd) { + if msg.generation != m.editorCheckGeneration { + return m, nil + } + m.editorChecking = false + if msg.err == nil { + m.pipelines = msg.pipelines + m.loaded = true + if msg.detail != nil { + m.detail = msg.detail + m.keys.detail = true + m.detailLoading = false + m.detailError = "" + m.detailRequest = "" + m.requestedPipelineID = msg.detail.ID + m.requestedVersion = msg.detail.Version + m.resetPipelineView() + } + } + if msg.published { + m.pendingDraft = nil + m.confirmOpen = false + m.keys.confirm = false + m.draftOpen = false + m.keys.drafts = false + m.templateOpen = false + m.keys.templates = false + m.submitNotice = "Already published; reloaded latest version." + m.appendLine(m.submitNotice) + return m, nil + } + if msg.edit.unchanged { + m.confirmOpen = false + m.keys.confirm = false + m.submitNotice = "No local changes; page refreshed." + if msg.err != nil { + m.submitNotice = "No local changes; refresh failed: " + msg.err.Error() + } + m.appendLine(m.submitNotice) + return m, nil + } + m.pendingDraft = &msg.edit + m.confirmOpen = true + m.keys.confirm = true + m.submitNotice = "" + if msg.err != nil { + m.submitNotice = "Could not check publication; draft retained: " + msg.err.Error() + } + return m, nil +} diff --git a/internal/tui/editor_publication_test.go b/internal/tui/editor_publication_test.go new file mode 100644 index 00000000..fbd8e1d2 --- /dev/null +++ b/internal/tui/editor_publication_test.go @@ -0,0 +1,89 @@ +package tui + +import ( + "context" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/server" + "net/http/httptest" + "path/filepath" + "strings" + "testing" + "time" +) + +func TestEditorReturnFindsPublishedVersionAndReloadsLatest(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + store, err := pipeline.Open(filepath.Join(t.TempDir(), "publication.db")) + if err != nil { + t.Fatal(err) + } + defer store.Close() + handler := server.Handler(ctx, store) + srv := httptest.NewServer(handler) + defer func() { cancel(); srv.Close(); server.WaitRuns(handler) }() + source := pipeline.Template([]string{"planner"}, true) + if _, err := store.Submit(ctx, "test", source); err != nil { + t.Fatal(err) + } + items, _ := store.List(ctx) + original, _ := store.Get(ctx, items[0].ID, 1) + edited := strings.Replace(source, "Describe the task.", "Published in OpenCode.", 1) + if edited == source { + t.Fatal("fixture did not change source") + } + if _, err := store.Submit(ctx, "test", edited); err != nil { + t.Fatal(err) + } + latest := strings.Replace(edited, "Published in OpenCode.", "Another newer update.", 1) + if _, err := store.Submit(ctx, "test", latest); err != nil { + t.Fatal(err) + } + m := New(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect") + connected := m.connect()().(connectedMsg) + defer connected.conn.CloseNow() + updated, _ := m.Update(connected) + m = updated.(Model) + m.detail = original + m.keys.detail = true + updated, check := m.Update(editedMsg{name: "test", path: "retained.md", source: edited, baseVersion: 1}) + m = updated.(Model) + if check == nil || m.confirmOpen { + t.Fatal("must check before asking to submit") + } + updated, _ = m.Update(check()) + m = updated.(Model) + if m.confirmOpen || m.pendingDraft != nil || m.detail.Version != 3 || m.detail.Source != latest || m.pipelines[0].Version != 3 || !strings.Contains(m.submitNotice, "Already published") { + t.Fatal("did not find earlier publication and reload latest", m.submitNotice, m.detail) + } + items, _ = store.List(ctx) + if items[0].Version != 3 { + t.Fatal("publication check created a duplicate version") + } + // A local unsent draft must still need approval, even if the server changed. + unsent := strings.Replace(edited, "Published in OpenCode.", "Local unsent edits.", 1) + updated, check = m.Update(editedMsg{name: "test", path: "retained.md", source: unsent, baseVersion: 1}) + m = updated.(Model) + updated, _ = m.Update(check()) + m = updated.(Model) + if !m.confirmOpen || m.pendingDraft.source != unsent || m.pendingDraft.baseVersion != 1 { + t.Fatal("unsent changes or original conflict guard lost") + } +} + +func TestEditorPublicationFailureAndStaleResultDoNotDiscardDraft(t *testing.T) { + m := New(context.Background(), "") + m.editorCheckGeneration = 2 + edit := editedMsg{name: "test", source: "unsent", baseVersion: 1} + updated, _ := m.finishEditorPublication(editorPublicationMsg{generation: 1, edit: edit, published: true}) + m = updated.(Model) + if m.confirmOpen { + t.Fatal("stale result changed editor state") + } + result := checkEditorPublication(context.Background(), "invalid-url", edit, 2)().(editorPublicationMsg) + updated, _ = m.finishEditorPublication(result) + m = updated.(Model) + if !m.confirmOpen || m.pendingDraft.source != "unsent" || !strings.Contains(m.submitNotice, "Could not check") { + t.Fatal("failed check lost local edits", m.submitNotice) + } +} diff --git a/internal/tui/home_failed_runs_test.go b/internal/tui/home_failed_runs_test.go index b9be4888..9a29fc8b 100644 --- a/internal/tui/home_failed_runs_test.go +++ b/internal/tui/home_failed_runs_test.go @@ -13,7 +13,7 @@ import ( "github.com/charmbracelet/x/ansi" ) -func TestHomeShortcutOpensFailedRunsAndDisplaysHintInBothPanels(t *testing.T) { +func TestHomeShortcutOpensAllRunsAndDisplaysHintInBothPanels(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() srv := httptest.NewServer(server.Handler(ctx)) @@ -27,25 +27,26 @@ func TestHomeShortcutOpensFailedRunsAndDisplaysHintInBothPanels(t *testing.T) { m.height = 40 m.runs = []protocol.RunSummary{{ID: 1, Status: "succeeded", Name: "passed"}, {ID: 2, Status: "failed", Name: "broken", FinishedAt: "2026-10-09T00:00:00Z"}, {ID: 3, Status: "running", Name: "active"}, {ID: 4, Status: "canceled", Name: "canceled"}, {ID: 5, Status: "failed", Name: "also broken", FinishedAt: "2026-10-09T00:00:00Z"}} view := ansi.Strip(strings.Join(m.homeLines(), "\n")) - if strings.Count(view, "r failed runs") != 2 { + if strings.Count(view, "r all runs") < 2 || strings.Contains(view, "r failed runs") { t.Fatal("missing shortcut in both panels", view) } updated, send := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'r'}}) m = updated.(Model) - if send == nil || !m.runOpen || !m.keys.runs || !m.runFailedOnly || m.runIndex != 0 { - t.Fatal("home shortcut did not open failed list") + if send == nil || !m.runOpen || !m.keys.runs || m.runFailedOnly || m.runIndex != 0 { + t.Fatal("home shortcut did not open all runs") } if _, ok := send().(sentMsg); !ok { t.Fatal("failed list refresh request failed") } visible := m.visibleRuns() - if len(visible) != 2 || visible[0].ID != 2 || visible[1].ID != 5 { - t.Fatal("nonfailed runs leaked into list", visible) + if len(visible) != 5 || visible[0].ID != 1 || visible[1].ID != 2 { + t.Fatal("home runs were filtered", visible) } view = ansi.Strip(m.runsView(30)) - if !strings.Contains(view, "Failed runs") || strings.Contains(view, "passed") || strings.Contains(view, "active") { - t.Fatal("failed list view is incorrect", view) + if strings.Contains(view, "Failed runs") || !strings.Contains(view, "passed") || !strings.Contains(view, "active") { + t.Fatal("all runs view is incorrect", view) } + m.runIndex = 1 updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'r'}}) m = updated.(Model) if cmd == nil || m.runStartRequest == "" { diff --git a/internal/tui/model.go b/internal/tui/model.go index 40c74197..f3174a08 100644 --- a/internal/tui/model.go +++ b/internal/tui/model.go @@ -85,11 +85,18 @@ func (k keyMap) ShortHelp() []key.Binding { if k.detail { return []key.Binding{k.commands, k.search, key.NewBinding(key.WithKeys("n", "p"), key.WithHelp("n/p", "next/prev stage")), key.NewBinding(key.WithKeys("tab"), key.WithHelp("Tab", "pane")), key.NewBinding(key.WithKeys("r"), key.WithHelp("r", "runs")), key.NewBinding(key.WithKeys("j", "k"), key.WithHelp("j/k", "scroll")), key.NewBinding(key.WithKeys("esc"), key.WithHelp("esc", "home"))} } - return []key.Binding{k.commands, k.search, k.create, key.NewBinding(key.WithKeys("r"), key.WithHelp("r", "failed runs")), key.NewBinding(key.WithKeys("d"), key.WithHelp("d", "drafts")), key.NewBinding(key.WithKeys("b"), key.WithHelp("b", "templates")), k.ping, k.quit} + return []key.Binding{k.commands, k.search, k.create, key.NewBinding(key.WithKeys("r"), key.WithHelp("r", "all runs")), key.NewBinding(key.WithKeys("d"), key.WithHelp("d", "drafts")), key.NewBinding(key.WithKeys("b"), key.WithHelp("b", "templates")), k.ping, k.quit} } func (k keyMap) FullHelp() [][]key.Binding { return [][]key.Binding{k.ShortHelp()} } type Model struct { + editorCheckGeneration uint64 + editorChecking bool + statusOpen bool + statusTop int + statusRequest, statusError string + systemStatus *protocol.SystemStatus + usage *protocol.UsageStats draftOpen bool draftItems []draft.Draft draftIndex, draftTop int @@ -295,6 +302,16 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } return m.Update(receivedMsg(msg.message)) case receivedMsg: + if msg.Type == protocol.Status && msg.ID == m.statusRequest { + m.systemStatus = msg.Status + m.statusError = "" + m.statusRequest = "" + return m, m.read() + } + if msg.Type == protocol.Error && msg.ID != "" && msg.ID == m.statusRequest { + m.statusError = msg.Error + m.statusRequest = "" + } if msg.Type == protocol.PipelineList || msg.Type == protocol.Submitted { m.notificationEndpoints = msg.NotificationEndpoints } @@ -303,6 +320,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.templateIndex = 0 m.renderTemplate() } else if msg.Type == protocol.Runs { + m.usage = msg.Usage m.runs = msg.Runs m.runIndex = min(m.runIndex, max(0, len(m.visibleRuns())-1)) } else if msg.Type == protocol.Run || msg.Type == protocol.RunStarted { @@ -416,6 +434,14 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.appendLine("editor: " + msg.err.Error()) return m, nil } + if m.conn != nil { + m.editorCheckGeneration++ + m.editorChecking = true + m.confirmOpen = false + m.keys.confirm = false + m.submitNotice = "Checking whether edits were published…" + return m, checkEditorPublication(m.ctx, m.url, msg, m.editorCheckGeneration) + } if msg.unchanged { m.confirmOpen = false m.keys.confirm = false @@ -429,6 +455,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.keys.confirm = true m.submitNotice = "" return m, nil + case editorPublicationMsg: + return m.finishEditorPublication(msg) case errorMsg: if msg.conn != nil && msg.conn != m.conn { return m, nil @@ -437,6 +465,10 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, nil } m.status = "Reconnecting…" + if m.statusOpen { + m.statusError = "Connection lost; reconnecting. Press r to refresh after reconnect." + m.statusRequest = "" + } m.appendLine("error: " + msg.err.Error()) m.detailRequest = "" m.detailLoading = m.keys.detail && m.requestedPipelineID > 0 @@ -454,9 +486,15 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if msg.String() == "ctrl+c" { return m, tea.Quit } + if m.editorChecking { + return m, nil + } if m.editorOpen { return m.updateEditorPicker(msg) } + if m.statusOpen { + return m.updateStatus(msg) + } if m.logOpen { return m.updateLogs(msg) } @@ -487,7 +525,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch { case m.homePage() && msg.String() == "r": m.closeRuns() - m.runFailedOnly = true + m.runFailedOnly = false m.runOpen = true m.keys.runs = true m.runIndex = 0 @@ -673,6 +711,12 @@ func (m Model) View() string { if m.editorOpen { content = m.editorPickerView(bodyHeight) } + if m.statusOpen { + content = m.statusView(bodyHeight) + } + if m.editorChecking { + content = popup(m.width, bodyHeight, 76, []string{"Checking published edits", "Refreshing pipeline information…", "Local draft remains retained."}) + } parts = append(parts, content) } return strings.Join(append(parts, footer), "\n") diff --git a/internal/tui/pipeline_test.go b/internal/tui/pipeline_test.go index 642da10c..6cbd0263 100644 --- a/internal/tui/pipeline_test.go +++ b/internal/tui/pipeline_test.go @@ -74,7 +74,12 @@ func TestSearchVersionsAndEditSubmission(t *testing.T) { newSource := strings.Replace(source, "Describe the task.", "Edited from version one.", 1) updated, send = m.Update(editedMsg{name: "demo", path: "draft.md", source: newSource, confirm: true}) m = updated.(Model) - if send != nil || !m.confirmOpen { + if send == nil || m.confirmOpen { + t.Fatal("publication check missing") + } + updated, _ = m.Update(send()) + m = updated.(Model) + if !m.confirmOpen { t.Fatal("edited pipeline should require explicit submission") } items, err := store.List(ctx) @@ -96,8 +101,16 @@ func TestSearchVersionsAndEditSubmission(t *testing.T) { t.Fatal("historic version mutated") } for _, draft := range []string{newSource, "invalid"} { - updated, _ = m.Update(editedMsg{name: "demo", path: "draft.md", source: draft, confirm: true}) + updated, send = m.Update(editedMsg{name: "demo", path: "draft.md", source: draft, confirm: true}) m = updated.(Model) + updated, _ = m.Update(send()) + m = updated.(Model) + if draft == newSource { + if m.confirmOpen || m.pendingDraft != nil { + t.Fatal("already-published edit offered duplicate submission") + } + continue + } updated, send = m.Update(tea.KeyMsg{Type: tea.KeyEnter}) m = updated.(Model) send() diff --git a/internal/tui/stats.go b/internal/tui/stats.go new file mode 100644 index 00000000..d563b1ee --- /dev/null +++ b/internal/tui/stats.go @@ -0,0 +1,57 @@ +package tui + +import ( + "fmt" + "git.a-wan.net/alan/pipelines/internal/protocol" + "strings" + "time" +) + +func successLabel(c protocol.RunCounts) string { + resolved := c.Succeeded + c.Failed + if resolved == 0 { + return "success —" + } + return fmt.Sprintf("success %.0f%%", 100*float64(c.Succeeded)/float64(resolved)) +} +func averageLabel(c protocol.RunCounts) string { + if c.Total-c.Active <= 0 { + return "—" + } + return (time.Duration(c.AverageSeconds * float64(time.Second))).Round(time.Second).String() +} +func usageChart(days []protocol.DailyUsage) string { + maxRuns := 0 + for _, day := range days { + maxRuns = max(maxRuns, day.Runs) + } + var b strings.Builder + levels := []rune("▁▂▃▄▅▆▇█") + for _, day := range days { + if day.Runs == 0 { + b.WriteRune('·') + } else { + level := min(7, max(0, (day.Runs*8-1)/maxRuns)) + b.WriteRune(levels[level]) + } + } + return b.String() +} +func (m Model) usageLines() []string { + lines := []string{"Usage · all history"} + if m.usage == nil { + return append(lines, "Waiting for usage statistics…", ":status server & ntfy · r all runs") + } + c := m.usage.RunCounts + lines = append(lines, fmt.Sprintf("%d pipelines · %d runs · %d active · %s · avg completed %s", len(m.pipelines), c.Total, c.Active, successLabel(c), averageLabel(c)), fmt.Sprintf("Outcomes: %d succeeded · %d failed · %d canceled · %d interrupted", c.Succeeded, c.Failed, c.Canceled, c.Interrupted)) + if len(m.usage.Days) > 0 { + days := m.usage.Days + total, peak := 0, 0 + for _, day := range days { + total += day.Runs + peak = max(peak, day.Runs) + } + lines = append(lines, fmt.Sprintf("14d activity %s · %d runs · peak %d/day (UTC)", usageChart(days), total, peak)) + } + return lines +} diff --git a/internal/tui/status.go b/internal/tui/status.go new file mode 100644 index 00000000..09f33b08 --- /dev/null +++ b/internal/tui/status.go @@ -0,0 +1,78 @@ +package tui + +import ( + "fmt" + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/statusview" + tea "github.com/charmbracelet/bubbletea" + "net/url" +) + +func (m Model) openStatus() (tea.Model, tea.Cmd) { + m.statusOpen = true + m.statusTop = 0 + m.systemStatus = nil + m.statusError = "" + return m.refreshStatus() +} +func (m Model) refreshStatus() (tea.Model, tea.Cmd) { + if m.conn == nil { + m.statusError = "Not connected to the Pipelines server." + return m, nil + } + m.nextID++ + m.statusRequest = fmt.Sprintf("status-%d", m.nextID) + m.statusError = "" + return m, m.send(protocol.Message{Type: protocol.GetStatus, ID: m.statusRequest}) +} +func (m Model) updateStatus(msg tea.KeyMsg) (tea.Model, tea.Cmd) { + switch msg.String() { + case "esc", "q": + m.statusOpen = false + return m, nil + case "r": + return m.refreshStatus() + case "j", "down": + m.statusTop++ + case "k", "up": + m.statusTop-- + case "pgdown": + m.statusTop += max(1, m.height-10) + case "pgup": + m.statusTop -= max(1, m.height-10) + } + m.statusTop = max(0, min(m.statusTop, max(0, len(m.statusLines())-max(1, m.height-9)))) + return m, nil +} +func (m Model) statusLines() []string { + connection := "invalid URL" + if u, err := url.Parse(m.url); err == nil { + u.User = nil + u.RawQuery = "" + u.Fragment = "" + connection = u.String() + } + lines := []string{"Client connection: " + connection} + if m.statusRequest != "" { + lines = append(lines, "Checking server and ntfy…") + } + if m.statusError != "" { + lines = append(lines, m.statusError) + if m.systemStatus == nil { + lines = append(lines, "ntfy: unknown (server information unavailable)") + } + } + if m.systemStatus != nil { + lines = append(lines, " ") + lines = append(lines, statusview.Lines(m.systemStatus)...) + } + return lines +} +func (m Model) statusView(height int) string { + lines := m.statusLines() + page := max(1, height-5) + top := min(m.statusTop, max(0, len(lines)-page)) + rows := []string{"Status · Pipelines & ntfy", "↑/↓ scroll · PgUp/PgDn · r refresh · Esc close", ""} + rows = append(rows, lines[top:min(len(lines), top+page)]...) + return popup(m.width, height, 108, rows) +} diff --git a/internal/tui/status_test.go b/internal/tui/status_test.go new file mode 100644 index 00000000..3127dbe8 --- /dev/null +++ b/internal/tui/status_test.go @@ -0,0 +1,77 @@ +package tui + +import ( + "context" + "git.a-wan.net/alan/pipelines/internal/protocol" + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/x/ansi" + "strings" + "testing" +) + +func TestStatusPalettePopupAndClosePreservesUnderlyingPage(t *testing.T) { + m := New(context.Background(), "ws://127.0.0.1:8080/connect") + m.width = 120 + m.height = 40 + m.runOpen = true + m.keys.runs = true + m.runDetail = &protocol.RunDetail{RunSummary: protocol.RunSummary{ID: 42}} + m = press(m, ":") + m = press(m, "status") + m = press(m, "enter") + if !m.statusOpen || m.paletteOpen || !strings.Contains(ansi.Strip(m.View()), "Not connected") { + t.Fatal("status unavailable off Home", m.View()) + } + m.statusRequest = "status-1" + updated, _ := m.Update(receivedMsg(protocol.Message{Type: protocol.Status, ID: "status-1", Status: &protocol.SystemStatus{Hostname: "test-machine", Server: protocol.ServiceStatus{Health: "healthy", Port: "8080"}, Ntfy: protocol.ServiceStatus{Health: "healthy", URL: "http://192.0.2.2:8090", Port: "8090"}, Addresses: []protocol.NetworkAddress{{Interface: "en0", IP: "192.0.2.2"}}}})) + m = updated.(Model) + view := ansi.Strip(m.View()) + for _, text := range []string{"Pipelines server", "ntfy", "test-machine", "8080", "8090", "192.0.2.2"} { + if !strings.Contains(view, text) { + t.Fatal("missing status info", text, view) + } + } + m = press(m, "esc") + if m.statusOpen || m.runDetail.ID != 42 { + t.Fatal("status destroyed run selection") + } + updated, _ = m.Update(tea.WindowSizeMsg{Width: 40, Height: 12}) + m = updated.(Model) + m.statusOpen = true + if len(strings.Split(ansi.Strip(m.View()), "\n")) != 12 { + t.Fatal("popup overflowed short terminal") + } +} + +func TestDashboardSideBySideStatsAndNarrowStacking(t *testing.T) { + m := New(context.Background(), "") + m.width = 120 + m.loaded = true + m.pipelines = []protocol.PipelineSummary{{ID: 1, Name: "popular", Version: 2, Stages: 3}} + m.usage = &protocol.UsageStats{RunCounts: protocol.RunCounts{Total: 80, Succeeded: 60, Failed: 20, AverageSeconds: 60}, Pipelines: []protocol.PipelineUsage{{PipelineID: 1, Name: "popular", RunCounts: protocol.RunCounts{Total: 80, Succeeded: 60, Failed: 20, AverageSeconds: 60}}}, Days: []protocol.DailyUsage{{Runs: 0}, {Runs: 1}, {Runs: 8}}} + view := ansi.Strip(strings.Join(m.homeLines(), "\n")) + for _, text := range []string{"80 runs", "success 75%", "avg completed 1m0s", "14d activity", "60 succeeded", "20 failed", "avg 1m0s"} { + if !strings.Contains(view, text) { + t.Fatal("missing dashboard metric", text, view) + } + } + sameLine := false + for _, line := range strings.Split(view, "\n") { + if strings.Contains(line, "Latest runs") && strings.Contains(line, "Existing pipelines") { + sameLine = true + } + } + if !sameLine { + t.Fatal("wide dashboard did not use columns", view) + } + m.width = 60 + view = ansi.Strip(strings.Join(m.homeLines(), "\n")) + for _, line := range strings.Split(view, "\n") { + if strings.Contains(line, "Latest runs") && strings.Contains(line, "Existing pipelines") { + t.Fatal("narrow dashboard did not stack") + } + } + if usageChart([]protocol.DailyUsage{{Runs: 0}, {Runs: 0}}) != "··" { + t.Fatal("empty activity fabricated bars") + } +}