diff --git a/docs/cli.md b/docs/cli.md index a4116da5..c7ffe15c 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -48,13 +48,15 @@ Search includes pipeline name, version, status, outcome, stage and diagnostic; `#123` searches run-ID prefixes. Esc while searching clears the filter without closing the list. The filter survives inspection/back and live list refreshes. Home's `/` instead opens the omni popup, which searches pipelines, versions, runs, -stages/visits, artifact names, and the text of stage inputs, outputs and evidence. -A bare status word (`queued`, `running`, `succeeded`, `failed`, `canceled`, -`interrupted`) or a date (`today`, `yesterday`, `this week`, or `YYYY-MM-DD` -interpreted in UTC) selects runs, and a leading `pipeline:`/`version:`/`run:`/ -`step:`/`field:`/`artifact:`/`match:` prefix narrows the result kind. Enter opens -the matched pipeline, run, stage visit, artifact or field; unrecognised words -remain text. +steps/visits, artifact names, and the text of stage inputs, outputs and evidence. +Each row carries a short type badge (`pipeline`, `run`, `step`, `artifact` or +`match`); a text match also shows a snippet with the matched part bracketed so it +is legible without colour. A bare status word (`queued`, `running`, `succeeded`, +`failed`, `canceled`, `interrupted`) or a date (`today`, `yesterday`, `this week`, +or `YYYY-MM-DD` in local time) selects runs. Enter opens the matched pipeline, +run, stage visit, artifact or field. The same query is available non-interactively +as `pipelines search QUERY [--type KIND] [--limit N] [--json]`; `--type` is the +only kind-narrowing mechanism. Wide terminals show runs and pipelines side by side, with stacked panels on narrow terminals. Home run cards prioritize reported results, status, completed visits and duration; @@ -214,7 +216,7 @@ Submitting activates included schedules, so review them before publishing. | TUI action | Agent-capable CLI | | --- | --- | -| Browse/search pipelines | `list --json`, `show NAME --json` | +| Browse/search pipelines | `list --json`, `show NAME --json`, `search QUERY --json` | | Original source and history | `export NAME --version N`, `versions NAME --json` | | Browse templates/source | `templates --json`, `template KEY --source` | | Create or edit a definition | `new`, `edit`, `drafts` | @@ -241,9 +243,10 @@ 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`, `decide_run`, `set_run_title`, `get_run_evidence`, -`get_status`, `search`. `search` takes `query` and an -optional `kind` and `limit` and returns ranked typed `matches`, each with a stable -`target` the client can open. `list_runs` also returns all-history `usage` +`get_status`, `search`. `search` takes a `search` request object (`query`, with an +optional `kind` and `limit`) and returns a `search_results` response whose ranked +`results` each carry a stable `target` the client can open plus the typed payload +and matched field. `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/ui-ux-review.md b/docs/ui-ux-review.md index 8982002c..eac2fa2a 100644 --- a/docs/ui-ux-review.md +++ b/docs/ui-ux-review.md @@ -38,7 +38,7 @@ and external editor/OpenCode behavior were not assessed by that review. | Status | Narrow cards hide addresses; scrolling does not reveal them. | Wrap card contents and scroll the actual service-card canvas. | | Task form | Tool lists consume prompt space; wrong footer. | Larger task area, compact scope summary, correct submit/cancel controls. | | CLI pipeline picker | Background Home controls remain visible. | Picker-specific filter/select/cancel controls. | -| Search | “Omnisearch” promises broader scope than pipeline search. | “Search pipelines” and specific open/cancel instructions. | +| Search | Home `/` now opens one omni popup that searches pipelines, versions, runs, steps/visits, artifact names and stage input/output/evidence text. | Keep the omni popup: typed badges, a non-colour marked snippet for text matches, and Enter-to-open navigation; the run-list `/` filter is unchanged. | | Commands | No descriptions or indication of hidden entries. | Selected-command explanation and visible range/count. | | Versions | Selected/latest markers truncate on narrow screens. | Correct view-version footer; further metadata prioritization remains a follow-up. | | Naming/publication | Naming inherits publication shortcuts. | Input-specific name-saving/retention footer. | @@ -61,6 +61,9 @@ repeated stages; `0` returns to the overview. `j`/`k` scrolls Details or pans Gr depending on focus. `h`/`l` and arrow keys pan a focused graph; in Details, left/right selects the previous/next visit. `c` centers the focused graph. `o`, `s`, `i`, `t`, `m` disclose result, progress, input, evidence and execution details. +`A` opens the run's artifacts view: recorded files grouped by stage and visit, with +name, content type and human size in aligned columns, an `[img]` marker for images, +the URL per entry, and `Enter`/`o` to open or `y` to copy the selected entry. Home still advertises only `/` search and `:` commands. `q` never exits the client; use `:quit`. Consequential actions still require explicit confirmation. diff --git a/internal/cli/commands.go b/internal/cli/commands.go index 360af5a6..806251d5 100644 --- a/internal/cli/commands.go +++ b/internal/cli/commands.go @@ -10,6 +10,7 @@ var usage = map[string]string{ "validate": "validate [--json]", "submit": "submit [--name NAME] [--create-only] [--yes] [--json]", "list": "list [--json]", + "search": "search QUERY [--type KIND] [--limit N] [--json]", "show": "show [--version N] [--json]", "versions": "versions [--json]", "export": "export [name|id ...] [--output FILE.zip] [--all] [--version N]", @@ -49,7 +50,8 @@ var notes = map[string]string{ "title": "Rename a run's operator-facing title after the fact without touching its input, steps or execution. The title is collapsed to a single bounded line and recorded as explicitly set. `pipelines runs` and `pipelines inspect` then show it.", "decide": "Choose an option for a run waiting on a human stage. The run must be `waiting`; the option must be one of that stage's non-error edges. The stage is inferred from the run's current stage unless named. The choice and optional --feedback are recorded as the step's evidence (visible in inspect and the TUI) and the run resumes along the chosen edge. An unknown option is refused with the valid list; a repeated decision for an already-decided visit does not double-apply. Works across a server restart because the wait is persisted.", "debug": "Open a fresh configured harness conversation to investigate a run using inspect/show/runs CLI queries. --harness opencode|pi|nvim chooses another configured harness or read-only nvim packet. Does not resume implementation or retry. --json returns the prompt without launching anything.", - "rpc": "Accepts a JSON protocol envelope; '-' reads stdin. Every TUI server operation is available here too. Mutating commands are not dry runs. The omni-search command is `search` (fields query, optional kind and limit) and answers with ranked `search_results`.", + "rpc": "Accepts a JSON protocol envelope; '-' reads stdin. Every TUI server operation is available here too. Mutating commands are not dry runs.", + "search": "One read-only query over pipelines, versions, runs, steps, field text and artifacts. QUERY is free text, a bare status word (queued, running, succeeded, failed, canceled, interrupted) selecting those runs, or a lone date expression (today, yesterday, this week [Monday 00:00 local], or YYYY-MM-DD) selecting the runs created in that window. --type is the only narrowing mechanism and takes one kind: pipeline, version, run, step, field or artifact; an unknown kind is an error. Ranking: an exact name match (3) beats a name substring (2) beats a body-text match (1); equal tiers break by recency, then a stable kind/target tie-break, and every hit reports score, matched_field and recency so a client can re-rank. Bounds: the result cap defaults to 50 and clamps to 200 (0 = default); every kind scans at most 2000 rows and field-text scanning is additionally capped at 2000 steps / 1 MiB; Truncated plus scanned_steps, scanned_bytes and scan_bound_bytes report when a bound applied. Targets are stable: pipeline:, pipeline::v, run:, run::, run:::, or an artifact URL.", } var serviceHelp = map[string]string{ diff --git a/internal/cli/help.go b/internal/cli/help.go index 5bae85be..e72aac24 100644 --- a/internal/cli/help.go +++ b/internal/cli/help.go @@ -13,6 +13,7 @@ AUTHOR BROWSE list List registered pipelines + search QUERY Search pipelines, versions, runs, steps, field text, artifacts show Inspect graph, settings, source and history versions List immutable versions export [name|id ...] Select/export definitions as ZIP; one name prints Markdown @@ -54,6 +55,13 @@ 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. +Stage result format: + Model and agent stages accept a line-oriented result. The first non-empty line + is the declared outcome, followed by "# " sections. "# output" is required and + short (a JSON object carrying the next stage's data); additional sections are + free-form markdown evidence. The legacy {"outcome":...,"output":...} JSON + envelope remains accepted for compatibility. + Examples: pipelines edit "bug fix" "Add a review step" pipelines new "release notes" "Summarize changes and notify my phone" @@ -75,11 +83,10 @@ Nothing is published automatically. Every submission is validated server-side. Submission activates any schedules. JSON mode never launches an editor or picker; supply explicit arguments. -Search: the TUI '/' popup and the protocol 'search' command (via 'pipelines rpc') -find pipelines, versions, runs, stages/visits, artifact names and the text of stage -inputs, outputs and evidence. A bare status word (queued, running, succeeded, -failed, canceled, interrupted) or a date (today, yesterday, this week, or -YYYY-MM-DD interpreted in UTC) selects runs. A leading prefix pipeline:, version:, -run:, step:, field:, artifact: or match: (a field alias) narrows the result kind; -unrecognised words stay text. +Search: the 'pipelines search QUERY' command and the TUI '/' popup find pipelines, +versions, runs, steps/visits, artifact names and the text of stage inputs, +outputs and evidence. A bare status word (queued, running, succeeded, failed, +canceled, interrupted) or a date (today, yesterday, this week, or YYYY-MM-DD in +local time) selects runs. --type narrows to pipeline, version, run, step, field +or artifact; unrecognised words stay text. ` diff --git a/internal/pipeline/runs.go b/internal/pipeline/runs.go index 6b2d66a7..8531c825 100644 --- a/internal/pipeline/runs.go +++ b/internal/pipeline/runs.go @@ -241,11 +241,19 @@ func (s *Store) SetRunTitle(ctx context.Context, id int64, title summarise.Title return nil } +// runFrom is the shared pipeline_runs/pipelines join every run-row read builds +// on, so the run projections cannot drift. +const runFrom = `FROM pipeline_runs r JOIN pipelines p ON p.id=r.pipeline_id` + +// runStamps formats a run row's created/finished timestamps; reused by runSelect +// and searchRuns instead of copied per query. +const runStamps = `strftime('%Y-%m-%dT%H:%M:%SZ',r.created_at),COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ',r.finished_at),'')` + const runSelect = `SELECT r.id,r.pipeline_id,p.name,r.version,v.content_hash,r.status,r.outcome,r.current_stage, -strftime('%Y-%m-%dT%H:%M:%SZ',r.created_at),COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ',r.finished_at),''),r.diagnostic, +` + runStamps + `,r.diagnostic, CASE WHEN r.status='succeeded' THEN r.output_json ELSE COALESCE((SELECT output_json FROM run_steps WHERE run_id=r.id AND status='completed' ORDER BY sequence DESC LIMIT 1),'{}') END, (SELECT COUNT(*) FROM run_steps WHERE run_id=r.id AND status='completed'),r.workspace,r.title,r.model,r.stage_models -FROM pipeline_runs r JOIN pipelines p ON p.id=r.pipeline_id JOIN pipeline_versions v ON v.pipeline_id=r.pipeline_id AND v.version=r.version` +` + runFrom + ` JOIN pipeline_versions v ON v.pipeline_id=r.pipeline_id AND v.version=r.version` func scanRun(row interface{ Scan(...any) error }, r *protocol.RunSummary) error { var output, stageModels string diff --git a/internal/pipeline/search_live_test.go b/internal/pipeline/search_live_test.go new file mode 100644 index 00000000..37b40f75 --- /dev/null +++ b/internal/pipeline/search_live_test.go @@ -0,0 +1,119 @@ +package pipeline + +import ( + "context" + "database/sql" + "os" + "strings" + "testing" + "time" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// TestLiveSearchRealDatabase is an env-guarded read-only check against a real +// database. It opens the file read-only (no DDL, no journal change, no writes), +// derives queries from the data itself, and reports one result of each kind it +// can observe. Run it with: +// +// PIPELINES_LIVE_SEARCH_DB=/path/to/pipelines.db go test ./internal/pipeline -run LiveSearch -v +func TestLiveSearchRealDatabase(t *testing.T) { + path := os.Getenv("PIPELINES_LIVE_SEARCH_DB") + if path == "" { + t.Skip("set PIPELINES_LIVE_SEARCH_DB to a real pipelines.db") + } + store, err := OpenReadOnly(path) + if err != nil { + t.Fatalf("open read-only: %v", err) + } + defer store.Close() + requireSearchSchema(t, store) + ctx := context.Background() + pipelines, err := store.List(ctx) + if err != nil { + t.Fatal(err) + } + if len(pipelines) == 0 { + t.Skip("database has no pipelines") + } + now := time.Now() + seen := map[string]protocol.SearchResult{} + record := func(query string) { + response, err := store.Search(ctx, protocol.SearchRequest{Query: query}, now) + if err != nil { + t.Fatalf("search %q: %v", query, err) + } + for _, hit := range response.Results { + if _, ok := seen[hit.Kind]; !ok { + seen[hit.Kind] = hit + } + } + } + queries := []string{} + for _, p := range pipelines { + queries = append(queries, p.Name) + } + // Derive stage/field/artifact queries from a bounded number of recent runs. + runs, err := store.ListRuns(ctx) + if err != nil { + t.Fatal(err) + } + for i := 0; i < len(runs) && i < 5; i++ { + detail, err := store.GetRun(ctx, runs[i].ID) + if err != nil { + continue + } + for j := 0; j < len(detail.Steps) && j < 3; j++ { + step := detail.Steps[j] + if step.Stage != "" { + queries = append(queries, step.Stage) + } + for _, artifact := range step.Artifacts { + queries = append(queries, artifact.Name) + } + for _, key := range []string{"summary", "result", "report", "message", "evidence", "reason"} { + if text, ok := step.Output[key].(string); ok { + for _, word := range strings.Fields(text) { + if len([]rune(word)) >= 6 { + queries = append(queries, word) + break + } + } + } + } + } + } + for _, query := range queries { + record(query) + } + if len(seen) == 0 { + t.Fatal("live database returned no matches for any derived query") + } + for _, kind := range protocol.SearchKinds { + if hit, ok := seen[kind]; ok { + t.Logf("kind=%s target=%s score=%d matched=%s", kind, hit.Target, hit.Score, hit.MatchedField) + } else { + t.Logf("kind=%s not observed in this database", kind) + } + } + if _, ok := seen[protocol.SearchKindRun]; !ok { + t.Fatal("live database returned no run matches") + } +} + +// requireSearchSchema skips (rather than fails) when the supplied database +// predates the tables search reads, so pointing the check at a stale local file +// reports the mismatch instead of a confusing "no such table" error. +func requireSearchSchema(t *testing.T, store *Store) { + t.Helper() + for _, table := range []string{"pipelines", "pipeline_versions", "pipeline_runs", "run_steps", "step_control", "step_artifacts"} { + var name string + err := store.db.QueryRow("SELECT name FROM sqlite_master WHERE type='table' AND name=?", table).Scan(&name) + if err == sql.ErrNoRows { + t.Skipf("database lacks the current search schema (missing %q); point PIPELINES_LIVE_SEARCH_DB at a current database", table) + } + if err != nil { + t.Fatal(err) + } + } +} diff --git a/internal/pipeline/search_test.go b/internal/pipeline/search_test.go new file mode 100644 index 00000000..c259dc8c --- /dev/null +++ b/internal/pipeline/search_test.go @@ -0,0 +1,601 @@ +package pipeline + +import ( + "context" + "path/filepath" + "strconv" + "strings" + "testing" + "time" + "unicode/utf8" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// searchFixture builds one pipeline whose name, source, run, stage, field text +// and artifact all contain "zephyr", so a single free-text query exercises +// every kind. +func searchFixture(t *testing.T) (*Store, int64) { + t.Helper() + ctx := context.Background() + store, err := Open(filepath.Join(t.TempDir(), "search.db")) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { store.Close() }) + if _, err := store.Submit(ctx, "Zephyr Build", Template([]string{"zephyr"}, true)); err != nil { + t.Fatal(err) + } + items, err := store.List(ctx) + if err != nil { + t.Fatal(err) + } + definition, err := store.Get(ctx, items[0].ID, 1) + if err != nil { + t.Fatal(err) + } + run, _, err := store.CreateRun(ctx, definition, "zephyr-key", "/work/zephyr", false, nil, map[string]any{"prompt": "zephyr task"}) + if err != nil { + t.Fatal(err) + } + if err := store.BeginStep(ctx, run, 1, "zephyr", map[string]any{"prompt": "zephyr task"}); err != nil { + t.Fatal(err) + } + artifactURL := "/artifacts/" + strconv.FormatInt(run, 10) + "/1/zephyr.txt" + if err := store.FinishStep(ctx, run, protocol.RunStep{ + Sequence: 1, Status: "completed", Outcome: "success", Target: protocol.SuccessEnd, + Output: map[string]any{"summary": "zephyr completed"}, + Control: &protocol.ControlEvidence{Visit: 1, MaxVisits: 2, ModelTarget: "zephyr-model"}, + Artifacts: []protocol.StageArtifact{{Name: "zephyr.txt", SizeBytes: 12, ContentType: "text/plain", URL: artifactURL}}, + }); err != nil { + t.Fatal(err) + } + if err := store.FinishRun(ctx, run, "succeeded", "success", "", map[string]any{"summary": "zephyr completed"}); err != nil { + t.Fatal(err) + } + return store, run +} + +func searchKinds(response *protocol.SearchResponse) map[string]bool { + kinds := map[string]bool{} + for _, hit := range response.Results { + kinds[hit.Kind] = true + } + return kinds +} + +func TestSearchReturnsEveryKind(t *testing.T) { + store, run := searchFixture(t) + response, err := store.Search(context.Background(), protocol.SearchRequest{Query: "zephyr"}, time.Now()) + if err != nil { + t.Fatal(err) + } + kinds := searchKinds(response) + for _, kind := range protocol.SearchKinds { + if !kinds[kind] { + t.Fatalf("kind %q missing from %+v", kind, response.Results) + } + } + if response.Cap != protocol.SearchDefaultLimit || response.ScanBoundBytes != protocol.SearchMaxScanBytes { + t.Fatalf("bounds not reported: %+v", response) + } + if response.Truncated { + t.Fatalf("a tiny store must not report truncation: %+v", response) + } + // Every target must point at an addressable entity an existing command resolves. + ctx := context.Background() + for _, hit := range response.Results { + switch hit.Kind { + case protocol.SearchKindPipeline: + id := targetInt(t, hit.Target, "pipeline:") + if _, err := store.Get(ctx, id, 0); err != nil { + t.Fatalf("pipeline target %q does not resolve: %v", hit.Target, err) + } + case protocol.SearchKindVersion: + parts := strings.Split(strings.TrimPrefix(hit.Target, "pipeline:"), ":v") + if len(parts) != 2 { + t.Fatalf("bad version target %q", hit.Target) + } + if _, err := store.Get(ctx, parseInt(t, parts[0]), int(parseInt(t, parts[1]))); err != nil { + t.Fatalf("version target %q does not resolve: %v", hit.Target, err) + } + case protocol.SearchKindRun: + if _, err := store.GetRun(ctx, targetInt(t, hit.Target, "run:")); err != nil { + t.Fatalf("run target %q does not resolve: %v", hit.Target, err) + } + case protocol.SearchKindStep, protocol.SearchKindField: + fields := strings.Split(strings.TrimPrefix(hit.Target, "run:"), ":") + if len(fields) < 2 { + t.Fatalf("bad step/field target %q", hit.Target) + } + detail, err := store.GetRun(ctx, parseInt(t, fields[0])) + if err != nil { + t.Fatalf("step/field target %q does not resolve: %v", hit.Target, err) + } + sequence := parseInt(t, fields[1]) + found := false + for _, step := range detail.Steps { + if int64(step.Sequence) == sequence { + found = true + } + } + if !found { + t.Fatalf("step/field target %q names no recorded step", hit.Target) + } + case protocol.SearchKindArtifact: + if !strings.HasPrefix(hit.Target, "/artifacts/") { + t.Fatalf("artifact target %q is not a served URL", hit.Target) + } + } + } + if run <= 0 { + t.Fatal("fixture run missing") + } +} + +func targetInt(t *testing.T, target, prefix string) int64 { + t.Helper() + if !strings.HasPrefix(target, prefix) { + t.Fatalf("target %q lacks prefix %q", target, prefix) + } + return parseInt(t, strings.TrimPrefix(target, prefix)) +} + +func parseInt(t *testing.T, value string) int64 { + t.Helper() + n, err := parsePositiveInt(value) + if err != nil { + t.Fatalf("bad number %q: %v", value, err) + } + return n +} + +func TestSearchFieldTextCoversInputOutputEvidence(t *testing.T) { + store, _ := searchFixture(t) + response, err := store.Search(context.Background(), protocol.SearchRequest{Query: "zephyr", Kind: protocol.SearchKindField}, time.Now()) + if err != nil { + t.Fatal(err) + } + fields := map[string]bool{} + for _, hit := range response.Results { + if hit.Field == nil { + t.Fatalf("field kind without a field payload: %+v", hit) + } + fields[hit.Field.Field] = true + if !strings.Contains(hit.Field.Snippet, "zephyr") { + t.Fatalf("snippet lacks the match: %+v", hit.Field) + } + } + for _, field := range []string{"input", "output", "evidence"} { + if !fields[field] { + t.Fatalf("field text search missed %q: %+v", field, response.Results) + } + } +} + +func TestSearchRankingExactSubstringBodyAndRecency(t *testing.T) { + ctx := context.Background() + store, err := Open(filepath.Join(t.TempDir(), "ranking.db")) + if err != nil { + t.Fatal(err) + } + defer store.Close() + submit := func(name string) { + if _, err := store.Submit(ctx, name, Template([]string{"one"}, true)); err != nil { + t.Fatal(err) + } + } + submit("alpha") + submit("alphabet") + items, _ := store.List(ctx) + definition, _ := store.Get(ctx, items[0].ID, 1) + run, _, err := store.CreateRun(ctx, definition, "alpha-key", "/work", false, nil, map[string]any{"prompt": "body alpha match"}) + if err != nil { + t.Fatal(err) + } + if err := store.BeginStep(ctx, run, 1, "one", map[string]any{"prompt": "body alpha match"}); err != nil { + t.Fatal(err) + } + if err := store.FinishStep(ctx, run, protocol.RunStep{Sequence: 1, Status: "completed", Outcome: "success", Target: protocol.SuccessEnd}); err != nil { + t.Fatal(err) + } + + response, err := store.Search(ctx, protocol.SearchRequest{Query: "alpha"}, time.Now()) + if err != nil { + t.Fatal(err) + } + seen := map[int]bool{} + for i, hit := range response.Results { + seen[hit.Score] = true + if i > 0 && response.Results[i-1].Score < hit.Score { + t.Fatalf("scores not descending: %+v", response.Results) + } + } + if !seen[3] || !seen[2] || !seen[1] { + t.Fatalf("expected exact/substring/body tiers: %+v", response.Results) + } + // The exact pipeline match outranks the substring pipeline match. + exactIndex, substringIndex := -1, -1 + for i, hit := range response.Results { + if hit.Pipeline == nil { + continue + } + if hit.Pipeline.Name == "alpha" { + exactIndex = i + } + if hit.Pipeline.Name == "alphabet" { + substringIndex = i + } + if hit.Pipeline.Name == "alpha" && hit.Score != 3 { + t.Fatalf("exact name scored %d", hit.Score) + } + if hit.Pipeline.Name == "alphabet" && hit.Score != 2 { + t.Fatalf("substring name scored %d", hit.Score) + } + } + if exactIndex < 0 || substringIndex < 0 || exactIndex > substringIndex { + t.Fatalf("exact name must outrank a name substring: %+v", response.Results) + } + + // Equal-tier ties break by recency. + older, _, err := store.CreateRun(ctx, definition, "alpha-older", "/work", false, nil, map[string]any{"prompt": "alpha"}) + if err != nil { + t.Fatal(err) + } + newer, _, err := store.CreateRun(ctx, definition, "alpha-newer", "/work", false, nil, map[string]any{"prompt": "alpha"}) + if err != nil { + t.Fatal(err) + } + setCreated(t, store, older, "2026-01-01 00:00:00") + setCreated(t, store, newer, "2026-06-01 00:00:00") + response, err = store.Search(ctx, protocol.SearchRequest{Query: "alpha"}, time.Now()) + if err != nil { + t.Fatal(err) + } + var olderIndex, newerIndex = -1, -1 + for i, hit := range response.Results { + if hit.Run == nil || hit.Run.ID == 0 { + continue + } + if hit.Run.ID == older { + olderIndex = i + } + if hit.Run.ID == newer { + newerIndex = i + } + } + if olderIndex < 0 || newerIndex < 0 || newerIndex > olderIndex { + t.Fatalf("recency tie-break lost: older=%d newer=%d", olderIndex, newerIndex) + } +} + +func TestSearchStatusWordsSelectRuns(t *testing.T) { + ctx := context.Background() + store, err := Open(filepath.Join(t.TempDir(), "statuses.db")) + if err != nil { + t.Fatal(err) + } + defer store.Close() + if _, err := store.Submit(ctx, "statuses", Template([]string{"one"}, true)); err != nil { + t.Fatal(err) + } + items, _ := store.List(ctx) + definition, _ := store.Get(ctx, items[0].ID, 1) + // One run per accepted status word, each with a distinct created_at so a + // date selector can never be mistaken for the status selector. + byStatus := map[string]int64{} + for i, status := range protocol.SearchStatuses { + run, _, err := store.CreateRun(ctx, definition, "status-"+status, "/work", false, nil, nil) + if err != nil { + t.Fatal(err) + } + if _, err := store.db.Exec("UPDATE pipeline_runs SET status=?,created_at=? WHERE id=?", status, "2026-01-0"+strconv.Itoa(i+1)+" 09:00:00", run); err != nil { + t.Fatal(err) + } + byStatus[status] = run + } + now := time.Date(2026, 10, 16, 12, 0, 0, 0, time.UTC) + for _, status := range protocol.SearchStatuses { + response, err := store.Search(ctx, protocol.SearchRequest{Query: status}, now) + if err != nil { + t.Fatal(err) + } + var runs []int64 + for _, hit := range response.Results { + if hit.Run == nil { + t.Fatalf("status %q returned a non-run kind %q", status, hit.Kind) + } + runs = append(runs, hit.Run.ID) + } + if len(runs) != 1 || runs[0] != byStatus[status] { + t.Fatalf("status %q selected %v, want [%d]", status, runs, byStatus[status]) + } + } +} + +func TestSearchStatusAndDateSelectors(t *testing.T) { + ctx := context.Background() + store, err := Open(filepath.Join(t.TempDir(), "selector.db")) + if err != nil { + t.Fatal(err) + } + defer store.Close() + if _, err := store.Submit(ctx, "selectors", Template([]string{"one"}, true)); err != nil { + t.Fatal(err) + } + items, _ := store.List(ctx) + definition, _ := store.Get(ctx, items[0].ID, 1) + yesterdayRun, _, err := store.CreateRun(ctx, definition, "yesterday", "/work", false, nil, nil) + if err != nil { + t.Fatal(err) + } + todayRun, _, err := store.CreateRun(ctx, definition, "today", "/work", false, nil, nil) + if err != nil { + t.Fatal(err) + } + if err := store.FinishRun(ctx, yesterdayRun, "failed", "error", "boom", nil); err != nil { + t.Fatal(err) + } + setCreated(t, store, yesterdayRun, "2026-10-15 09:00:00") + setCreated(t, store, todayRun, "2026-10-16 09:00:00") + now := time.Date(2026, 10, 16, 12, 0, 0, 0, time.UTC) + + selects := func(query string) map[int64]bool { + t.Helper() + response, err := store.Search(ctx, protocol.SearchRequest{Query: query}, now) + if err != nil { + t.Fatal(err) + } + ids := map[int64]bool{} + for _, hit := range response.Results { + if hit.Run != nil { + ids[hit.Run.ID] = true + } + } + return ids + } + if ids := selects("failed"); !ids[yesterdayRun] || ids[todayRun] { + t.Fatalf("status selector: %+v", ids) + } + if ids := selects("today"); !ids[todayRun] || ids[yesterdayRun] { + t.Fatalf("today selector: %+v", ids) + } + if ids := selects("yesterday"); !ids[yesterdayRun] || ids[todayRun] { + t.Fatalf("yesterday selector: %+v", ids) + } + if ids := selects("2026-10-15"); !ids[yesterdayRun] || ids[todayRun] { + t.Fatalf("literal date selector: %+v", ids) + } + if ids := selects("this week"); !ids[yesterdayRun] || !ids[todayRun] { + t.Fatalf("this week selector: %+v", ids) + } + // Non-selector text stays free text. + if ids := selects("selectors"); !ids[yesterdayRun] || !ids[todayRun] { + t.Fatalf("free text was misread as a selector: %+v", ids) + } +} + +func TestParseSearchTerms(t *testing.T) { + now := time.Date(2026, 10, 16, 12, 0, 0, 0, time.UTC) + cases := []struct { + query string + status string + hasDate bool + from, to string + text string + }{ + {query: "failed", status: "failed"}, + {query: " RUNNING ", status: "running"}, + {query: "today", hasDate: true, from: "2026-10-16 00:00:00", to: "2026-10-17 00:00:00"}, + {query: "yesterday", hasDate: true, from: "2026-10-15 00:00:00", to: "2026-10-16 00:00:00"}, + {query: "this week", hasDate: true, from: "2026-10-12 00:00:00", to: "2026-10-19 00:00:00"}, + {query: "2026-10-05", hasDate: true, from: "2026-10-05 00:00:00", to: "2026-10-06 00:00:00"}, + {query: "failed to build", text: "failed to build"}, + {query: "build", text: "build"}, + } + for _, test := range cases { + terms := parseSearchTerms(test.query, now) + if terms.Status != test.status || terms.Text != test.text || terms.HasDate != test.hasDate { + t.Fatalf("parse %q = %+v", test.query, terms) + } + if test.hasDate { + if got := terms.From.Format(searchSQLTime); got != test.from { + t.Fatalf("parse %q from = %s, want %s", test.query, got, test.from) + } + if got := terms.To.Format(searchSQLTime); got != test.to { + t.Fatalf("parse %q to = %s, want %s", test.query, got, test.to) + } + } + } +} + +func TestSearchKindFilterAndLimits(t *testing.T) { + store, _ := searchFixture(t) + ctx := context.Background() + response, err := store.Search(ctx, protocol.SearchRequest{Query: "zephyr", Kind: protocol.SearchKindRun}, time.Now()) + if err != nil { + t.Fatal(err) + } + for _, hit := range response.Results { + if hit.Kind != protocol.SearchKindRun { + t.Fatalf("kind filter leaked %q", hit.Kind) + } + } + if _, err := store.Search(ctx, protocol.SearchRequest{Query: "zephyr", Kind: "bogus"}, time.Now()); err == nil { + t.Fatal("invalid kind must return a clear error") + } + for _, test := range []struct { + limit int + want int + }{{limit: 0, want: protocol.SearchDefaultLimit}, {limit: -5, want: protocol.SearchDefaultLimit}, {limit: 1, want: 1}, {limit: 500, want: protocol.SearchMaxLimit}} { + response, err := store.Search(ctx, protocol.SearchRequest{Query: "zephyr", Limit: test.limit}, time.Now()) + if err != nil { + t.Fatal(err) + } + if response.Cap != test.want { + t.Fatalf("limit %d => cap %d, want %d", test.limit, response.Cap, test.want) + } + if len(response.Results) > response.Cap { + t.Fatalf("limit %d returned %d results", test.limit, len(response.Results)) + } + } + // A cap below the candidate count drops matches and must report Truncated. + truncated, err := store.Search(ctx, protocol.SearchRequest{Query: "zephyr", Limit: 1}, time.Now()) + if err != nil { + t.Fatal(err) + } + if len(truncated.Results) != 1 || !truncated.Truncated { + t.Fatalf("result cap must truncate: %+v", truncated) + } +} + +func TestSearchEdgeCases(t *testing.T) { + store, run := searchFixture(t) + ctx := context.Background() + empty, err := store.Search(ctx, protocol.SearchRequest{Query: " "}, time.Now()) + if err != nil || len(empty.Results) != 0 { + t.Fatalf("empty query: %+v %v", empty, err) + } + none, err := store.Search(ctx, protocol.SearchRequest{Query: "no-such-token-xyzzy"}, time.Now()) + if err != nil || len(none.Results) != 0 { + t.Fatalf("no-match query: %+v %v", none, err) + } + if none.Results == nil { + t.Fatal("results must be an empty slice, not nil") + } + // A run without a finish reports no duration. + setFinishedNull(t, store, run) + response, err := store.Search(ctx, protocol.SearchRequest{Query: "zephyr", Kind: protocol.SearchKindRun}, time.Now()) + if err != nil { + t.Fatal(err) + } + for _, hit := range response.Results { + if hit.Run != nil && hit.Run.FinishedAt != "" { + t.Fatalf("null finished_at surfaced a value: %+v", hit.Run) + } + } +} + +func TestSearchSnippetBoundaries(t *testing.T) { + if got := searchSnippet("prefix café résumé naïve suffix", "naïve"); !strings.Contains(got, "naïve") || !utf8.ValidString(got) { + t.Fatalf("unicode snippet malformed: %q", got) + } + if got := searchSnippet("line one\n\tline two\r\n zephyr here", "zephyr"); strings.ContainsAny(got, "\n\r\t") || !strings.Contains(got, "zephyr") { + t.Fatalf("whitespace snippet not normalized: %q", got) + } + if got := searchSnippet(strings.Repeat("é", 200)+"needle"+strings.Repeat("é", 200), "needle"); !utf8.ValidString(got) || !strings.Contains(got, "needle") { + t.Fatalf("boundary snippet malformed: %q", got) + } + if got := searchSnippet("no match here", "absent"); got == "" || !utf8.ValidString(got) { + t.Fatalf("no-match snippet must stay a clean line: %q", got) + } +} + +func TestSearchScanBoundTruncates(t *testing.T) { + ctx := context.Background() + store, err := Open(filepath.Join(t.TempDir(), "scan.db")) + if err != nil { + t.Fatal(err) + } + defer store.Close() + if _, err := store.Submit(ctx, "scan", Template([]string{"one"}, true)); err != nil { + t.Fatal(err) + } + items, _ := store.List(ctx) + definition, _ := store.Get(ctx, items[0].ID, 1) + // An older step that does match, and a newer step whose body alone exceeds + // the scan budget: the budget must stop the scan and be reported. + older, _, err := store.CreateRun(ctx, definition, "scan-older", "/work", false, nil, nil) + if err != nil { + t.Fatal(err) + } + if err := store.BeginStep(ctx, older, 1, "one", nil); err != nil { + t.Fatal(err) + } + if err := store.FinishStep(ctx, older, protocol.RunStep{Sequence: 1, Status: "completed", Outcome: "success", Target: protocol.SuccessEnd, Output: map[string]any{"summary": "needle in older step"}}); err != nil { + t.Fatal(err) + } + newer, _, err := store.CreateRun(ctx, definition, "scan-newer", "/work", false, nil, nil) + if err != nil { + t.Fatal(err) + } + if err := store.BeginStep(ctx, newer, 1, "one", nil); err != nil { + t.Fatal(err) + } + blob := strings.Repeat("x", protocol.SearchMaxScanBytes+1024) + if err := store.FinishStep(ctx, newer, protocol.RunStep{Sequence: 1, Status: "completed", Outcome: "success", Target: protocol.SuccessEnd, Output: map[string]any{"blob": blob}}); err != nil { + t.Fatal(err) + } + response, err := store.Search(ctx, protocol.SearchRequest{Query: "needle"}, time.Now()) + if err != nil { + t.Fatal(err) + } + if !response.Truncated { + t.Fatalf("scan bound not reported: %+v", response) + } + for _, hit := range response.Results { + if hit.Field != nil { + t.Fatalf("scan passed the bound and matched: %+v", hit.Field) + } + } +} + +// TestSearchScanBoundTruncatesByRowCount proves the step-count bound is +// observable: more recorded steps than the budget sets Truncated even though +// every body is tiny and fits the byte budget. +func TestSearchScanBoundTruncatesByRowCount(t *testing.T) { + ctx := context.Background() + store, err := Open(filepath.Join(t.TempDir(), "scanrows.db")) + if err != nil { + t.Fatal(err) + } + defer store.Close() + if _, err := store.Submit(ctx, "scanrows", Template([]string{"one"}, true)); err != nil { + t.Fatal(err) + } + items, _ := store.List(ctx) + definition, _ := store.Get(ctx, items[0].ID, 1) + run, _, err := store.CreateRun(ctx, definition, "scanrows", "/work", false, nil, nil) + if err != nil { + t.Fatal(err) + } + tx, err := store.db.Begin() + if err != nil { + t.Fatal(err) + } + for sequence := 1; sequence <= protocol.SearchMaxScanSteps+1; sequence++ { + if _, err := tx.Exec(`INSERT INTO run_steps(run_id,sequence,stage_key,input_json) VALUES(?,?,?,?)`, run, sequence, "one", "{}"); err != nil { + t.Fatal(err) + } + } + if err := tx.Commit(); err != nil { + t.Fatal(err) + } + response, err := store.Search(ctx, protocol.SearchRequest{Query: "needle"}, time.Now()) + if err != nil { + t.Fatal(err) + } + if !response.Truncated { + t.Fatalf("step-count bound not reported: scanned_steps=%d total=%d", response.ScannedSteps, protocol.SearchMaxScanSteps+1) + } + if response.ScannedSteps != protocol.SearchMaxScanSteps { + t.Fatalf("scanned_steps=%d, want %d", response.ScannedSteps, protocol.SearchMaxScanSteps) + } + if response.ScannedBytes > protocol.SearchMaxScanBytes { + t.Fatalf("scanned_bytes=%d exceeds bound %d", response.ScannedBytes, protocol.SearchMaxScanBytes) + } +} + +func setCreated(t *testing.T, store *Store, runID int64, timestamp string) { + t.Helper() + if _, err := store.db.Exec("UPDATE pipeline_runs SET created_at=? WHERE id=?", timestamp, runID); err != nil { + t.Fatal(err) + } +} + +func setFinishedNull(t *testing.T, store *Store, runID int64) { + t.Helper() + if _, err := store.db.Exec("UPDATE pipeline_runs SET finished_at=NULL,status='running' WHERE id=?", runID); err != nil { + t.Fatal(err) + } +} diff --git a/internal/pipeline/store.go b/internal/pipeline/store.go index afdde185..ea69fb39 100644 --- a/internal/pipeline/store.go +++ b/internal/pipeline/store.go @@ -8,6 +8,7 @@ import ( "fmt" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/stage" + "os" "path/filepath" "strings" "sync" @@ -50,6 +51,26 @@ CREATE TABLE IF NOT EXISTS submissions (id INTEGER PRIMARY KEY, name TEXT NOT NU func (s *Store) Close() error { return s.db.Close() } +// OpenReadOnly opens an existing database without running schema DDL or +// changing journal mode, so a live database can be queried read-only while its +// server keeps writing. It fails when the file does not exist. It is used by +// tests that inspect a real database and is never part of server startup. +func OpenReadOnly(path string) (*Store, error) { + if _, err := os.Stat(path); err != nil { + return nil, err + } + db, err := sql.Open("sqlite", "file:"+path+"?mode=ro") + if err != nil { + return nil, err + } + db.SetMaxOpenConns(1) + if _, err := db.Exec("PRAGMA busy_timeout=5000"); err != nil { + db.Close() + return nil, err + } + return &Store{db: db, dir: filepath.Dir(path)}, nil +} + // Directory is the resolved directory holding the database, used to place // adjacent server state such as stage artifacts. func (s *Store) Directory() string { return s.dir } diff --git a/internal/protocol/message.go b/internal/protocol/message.go index 0fd2cb3f..dbac24f6 100644 --- a/internal/protocol/message.go +++ b/internal/protocol/message.go @@ -102,13 +102,11 @@ type PipelineSummary struct { UpdatedAt string `json:"updated_at"` } -// Message is the JSON envelope shared by the server and client. It also carries -// the omni-search request/response: a client sends Type "search" with an ID, the -// Query text, an optional Kind type filter and an optional Limit, and the server -// replies with Type "search_results", the echoed ID and ranked Matches. Every -// SearchMatch carries a stable Target plus the label and per-kind evidence; all -// search fields are optional so older peers ignore them. See search.go for the -// kind constants and wire-shape detail. +// Message is the JSON envelope shared by the server and client. It also brokers +// omni search: a client sends Type "search" with an ID and a SearchRequest, and +// the server replies with Type "search_results", the echoed ID and a +// SearchResponse. Both search fields are optional so older peers ignore them. +// See search.go for the per-type payloads and the ranking/kind/bound vocabulary. type Message struct { DefaultHarness string `json:"default_harness,omitempty"` Build *buildinfo.Info `json:"build,omitempty"` @@ -130,8 +128,6 @@ type Message struct { Source string `json:"source,omitempty"` Title string `json:"title,omitempty"` Version int `json:"version,omitempty"` - Query string `json:"query,omitempty"` - Kind string `json:"kind,omitempty"` Pipelines []PipelineSummary `json:"pipelines,omitempty"` PipelineID int64 `json:"pipeline_id,omitempty"` Detail *PipelineDetail `json:"detail,omitempty"` @@ -141,7 +137,6 @@ type Message struct { Stage string `json:"stage,omitempty"` Option string `json:"option,omitempty"` Feedback string `json:"feedback,omitempty"` - Matches []SearchMatch `json:"matches,omitempty"` Input map[string]any `json:"input,omitempty"` AllowTools bool `json:"allow_tools,omitempty"` WritePaths []string `json:"write_paths,omitempty"` @@ -153,7 +148,11 @@ type Message struct { // them; the same fields are echoed on the run record. Model string `json:"model,omitempty"` StageModels map[string]string `json:"stage_models,omitempty"` - Limit int `json:"limit,omitempty"` + // Search is the broad, read-only search traffic over pipelines, versions, + // runs, steps, field text and artifacts. See search.go for the per-type + // payloads and the ranking/kind/bound vocabulary. + Search *SearchRequest `json:"search,omitempty"` + SearchResults *SearchResponse `json:"search_results,omitempty"` // Chat is the brokered OpenCode chat traffic (route B). See chat.go for the // per-type payload structs and the wire shapes they mirror. ChatSessions []ChatSession `json:"chat_sessions,omitempty"` diff --git a/internal/server/search_test.go b/internal/server/search_test.go new file mode 100644 index 00000000..0b4a38b3 --- /dev/null +++ b/internal/server/search_test.go @@ -0,0 +1,105 @@ +package server + +import ( + "bytes" + "context" + "encoding/json" + "net/http/httptest" + "path/filepath" + "strconv" + "strings" + "testing" + + "git.a-wan.net/alan/pipelines/internal/cli" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// TestCLISearchEndToEndEveryKind seeds a store whose pipeline, source, run, +// stage, field text and artifact all contain one token, then runs the CLI +// search command over a real Handler and asserts the server returned one match +// of every kind and rejects an unknown --type rather than ignoring it. +func TestCLISearchEndToEndEveryKind(t *testing.T) { + t.Setenv("PIPELINES_HARNESS_CONFIG", filepath.Join(t.TempDir(), "harnesses.json")) + ctx, cancel := context.WithCancel(context.Background()) + store, err := pipeline.Open(filepath.Join(t.TempDir(), "search-e2e.db")) + if err != nil { + t.Fatal(err) + } + defer store.Close() + if _, err := store.Submit(ctx, "Zephyr Build", pipeline.Template([]string{"zephyr"}, true)); err != nil { + t.Fatal(err) + } + items, err := store.List(ctx) + if err != nil { + t.Fatal(err) + } + definition, err := store.Get(ctx, items[0].ID, 1) + if err != nil { + t.Fatal(err) + } + run, _, err := store.CreateRun(ctx, definition, "zephyr-key", "/work/zephyr", false, nil, map[string]any{"prompt": "zephyr task"}) + if err != nil { + t.Fatal(err) + } + if err := store.BeginStep(ctx, run, 1, "zephyr", map[string]any{"prompt": "zephyr task"}); err != nil { + t.Fatal(err) + } + if err := store.FinishStep(ctx, run, protocol.RunStep{ + Sequence: 1, Status: "completed", Outcome: "success", Target: protocol.SuccessEnd, + Output: map[string]any{"summary": "zephyr completed"}, + Artifacts: []protocol.StageArtifact{{Name: "zephyr.txt", SizeBytes: 12, ContentType: "text/plain", URL: "/artifacts/" + strconv.FormatInt(run, 10) + "/1/zephyr.txt"}}, + }); err != nil { + t.Fatal(err) + } + handler := Handler(ctx, store) + defer func() { cancel(); WaitRuns(handler) }() + http := httptest.NewServer(handler) + defer http.Close() + url := "ws" + strings.TrimPrefix(http.URL, "http") + "/connect" + + runCLI := func(args ...string) (string, error) { + var out bytes.Buffer + app := cli.App{Out: &out, Err: &out, In: strings.NewReader("")} + err := app.Run(ctx, append([]string{"--url", url}, args...)) + return out.String(), err + } + output, err := runCLI("search", "zephyr", "--json") + if err != nil { + t.Fatal(err) + } + var response protocol.SearchResponse + if err := json.Unmarshal([]byte(output), &response); err != nil { + t.Fatal(err, output) + } + kinds := map[string]bool{} + for _, hit := range response.Results { + kinds[hit.Kind] = true + if hit.Target == "" || hit.MatchedField == "" || hit.Score == 0 { + t.Fatalf("hit lacks ranking/navigation context: %+v", hit) + } + } + for _, kind := range protocol.SearchKinds { + if !kinds[kind] { + t.Fatalf("kind %q missing from end-to-end search: %s", kind, output) + } + } + + if output, err := runCLI("search", "zephyr", "--type", "run", "--json"); err != nil { + t.Fatal(err) + } else { + var filtered protocol.SearchResponse + if err := json.Unmarshal([]byte(output), &filtered); err != nil { + t.Fatal(err) + } + for _, hit := range filtered.Results { + if hit.Kind != protocol.SearchKindRun { + t.Fatalf("kind filter leaked %q", hit.Kind) + } + } + } + + if _, err := runCLI("search", "zephyr", "--type", "bogus"); err == nil || !strings.Contains(err.Error(), "unknown search kind") { + t.Fatalf("invalid kind must error, got %v", err) + } +} diff --git a/internal/server/websocket.go b/internal/server/websocket.go index 1f90d421..2c6389ec 100644 --- a/internal/server/websocket.go +++ b/internal/server/websocket.go @@ -217,14 +217,21 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin } else { response.Pipelines = items } - } else if request.Type == protocol.Search && len(stores) > 0 && runs != nil { - response.Type = protocol.SearchResults - matches, err := runs.search(request) - if err != nil { + } else if request.Type == protocol.Search { + if len(stores) == 0 { response.Type = protocol.Error - response.Error = err.Error() + response.Error = "search unavailable: no store" + } else if request.Search == nil { + response.Type = protocol.Error + response.Error = "search requires a query" } else { - response.Matches = matches + response.Type = protocol.SearchResults + var err error + response.SearchResults, err = stores[0].Search(ctx, *request.Search, time.Now()) + if err != nil { + response.Type = protocol.Error + response.Error = err.Error() + } } } else if runs != nil && (request.Type == protocol.RunPipeline || request.Type == protocol.RetryRun || request.Type == protocol.ListRuns || request.Type == protocol.GetRun || request.Type == protocol.CancelRun || request.Type == protocol.DecideRun || request.Type == protocol.SetRunTitle || request.Type == protocol.GetRunEvidence) { var err error diff --git a/internal/tui/artifacts.go b/internal/tui/artifacts.go new file mode 100644 index 00000000..af0148da --- /dev/null +++ b/internal/tui/artifacts.go @@ -0,0 +1,424 @@ +package tui + +import ( + "fmt" + "net/url" + "os/exec" + "runtime" + "sort" + "strings" + + "git.a-wan.net/alan/pipelines/internal/protocol" + "github.com/atotto/clipboard" + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" + "github.com/charmbracelet/x/ansi" +) + +// artifactVisit is one recorded stage visit and the artifacts the server +// observed for it. A visit is kept even when it recorded none, so the view can +// say plainly that nothing was captured rather than omitting it. +type artifactVisit struct { + stage string + visit int + step protocol.RunStep + artifacts []protocol.StageArtifact +} + +// runArtifactVisits groups a run's recorded steps by stage and visit in the +// order the run produced them: stages in first-appearance order and each +// stage's visits by sequence. The display visit number is the recorded control +// visit when present, else the position within the stage (mirroring the app's +// runTimeline/runArtifactGroups). Pure, O(n), and it never dedupes steps, so a +// repeated stage's visits stay distinct. +func runArtifactVisits(steps []protocol.RunStep) []artifactVisit { + order := make([]string, 0) + index := map[string]int{} + byStage := make([][]protocol.RunStep, 0) + for _, step := range steps { + at, ok := index[step.Stage] + if !ok { + at = len(order) + index[step.Stage] = at + order = append(order, step.Stage) + byStage = append(byStage, nil) + } + byStage[at] = append(byStage[at], step) + } + visits := make([]artifactVisit, 0, len(steps)) + for i, stage := range order { + stageSteps := byStage[i] + sort.SliceStable(stageSteps, func(a, b int) bool { return stageSteps[a].Sequence < stageSteps[b].Sequence }) + for j, step := range stageSteps { + visit := j + 1 + if step.Control != nil && step.Control.Visit > 0 { + visit = step.Control.Visit + } + visits = append(visits, artifactVisit{stage: stage, visit: visit, step: step, artifacts: step.Artifacts}) + } + } + return visits +} + +// artifactRef addresses one artifact row in the flat selection order the view +// uses (group order, then recorded order within the group). +type artifactRef struct { + visit int + artifact protocol.StageArtifact +} + +func artifactRefs(visits []artifactVisit) []artifactRef { + var refs []artifactRef + for i, visit := range visits { + for _, artifact := range visit.artifacts { + refs = append(refs, artifactRef{visit: i, artifact: artifact}) + } + } + return refs +} + +// isImageContentType mirrors the app's isImageContentType: an image the +// terminal cannot render and therefore marks distinctly. +func isImageContentType(contentType string) bool { + return strings.HasPrefix(contentType, "image/") +} + +// Column geometry for one aligned artifact row. The name and type widths are +// clamped so one very long value cannot widen every other row; the size column +// is right-aligned. +const ( + artifactCursorWidth = 2 + artifactMarkerWidth = 5 // "[img]" or five spaces + artifactColumnGap = 2 + artifactMinName = 8 + artifactMinURLWidth = 12 + artifactMaxName = 40 + artifactMaxType = 28 +) + +type artifactWidths struct{ name, typ, size int } + +func artifactWidthsFor(visits []artifactVisit) artifactWidths { + widths := artifactWidths{} + for _, visit := range visits { + for _, artifact := range visit.artifacts { + widths.name = min(artifactMaxName, max(widths.name, lipgloss.Width(ansi.Strip(artifact.Name)))) + widths.typ = min(artifactMaxType, max(widths.typ, lipgloss.Width(ansi.Strip(artifact.ContentType)))) + widths.size = max(widths.size, lipgloss.Width(humanBytes(artifact.SizeBytes))) + } + } + return widths +} + +func padRight(text string, width int) string { + if deficit := width - lipgloss.Width(text); deficit > 0 { + return text + strings.Repeat(" ", deficit) + } + return text +} + +func padLeft(text string, width int) string { + if deficit := width - lipgloss.Width(text); deficit > 0 { + return strings.Repeat(" ", deficit) + text + } + return text +} + +// artifactScrollTop returns the offset that keeps the selected row visible in a +// window of page rows, given the current offset and the total line count. It +// scrolls by the minimum needed, so a one-row move shifts the window by one +// rather than recentring it, and it never leaves the viewport past the end. +// Both the selection clamp and the renderer use it, so they can never disagree +// about which row is on screen. +func artifactScrollTop(from, row, page, total int) int { + if page <= 0 { + return 0 + } + maxTop := max(0, total-page) + from = max(0, min(from, maxTop)) + if row < from { + from = row + } else if row >= from+page { + from = row - page + 1 + } + return max(0, min(from, maxTop)) +} + +// artifactsViewHeight is the frame body height the artifacts panel is rendered +// at, matching View: a one-line header and a one-line footer, each followed by +// a separating row. +func (m Model) artifactsViewHeight() int { return max(0, m.height-4) } + +// artifactsPageSize is the number of artifact body rows the panel shows: the +// frame body less the panel's own bar and border, matching artifactsView. +func (m Model) artifactsPageSize() int { return max(0, m.artifactsViewHeight()-4) } + +// artifactRowText renders one artifact as an aligned row: the image marker, the +// name, the content type and the human-readable size, with the URL appended +// when the terminal is wide enough. Optional columns drop as width shrinks and +// every cell is truncated, so the row never exceeds width. +func artifactRowText(artifact protocol.StageArtifact, widths artifactWidths, width int, selected bool) string { + prefix := strings.Repeat(" ", artifactCursorWidth) + if selected { + prefix = "› " + } + marker := strings.Repeat(" ", artifactMarkerWidth) + if isImageContentType(artifact.ContentType) { + marker = "[img]" + } + prefix += marker + " " + available := width - lipgloss.Width(prefix) + if available <= 0 { + return ansi.Truncate(prefix, max(1, width), "") + } + name := ansi.Strip(artifact.Name) + contentType := ansi.Strip(artifact.ContentType) + if strings.TrimSpace(contentType) == "" { + contentType = "unknown type" + } + size := humanBytes(artifact.SizeBytes) + gap := strings.Repeat(" ", artifactColumnGap) + nameCell := func(w int) string { return padRight(ansi.Truncate(name, max(1, w), "…"), w) } + typeCell := padRight(ansi.Truncate(contentType, max(1, widths.typ), "…"), widths.typ) + sizeCell := padLeft(size, widths.size) + switch { + case available >= widths.name+artifactColumnGap+widths.typ+artifactColumnGap+widths.size+artifactColumnGap+artifactMinURLWidth: + urlWidth := available - (widths.name + artifactColumnGap + widths.typ + artifactColumnGap + widths.size + artifactColumnGap) + return prefix + nameCell(widths.name) + gap + typeCell + gap + sizeCell + gap + ansi.Truncate(artifact.URL, max(1, urlWidth), "…") + case available >= widths.name+artifactColumnGap+widths.typ+artifactColumnGap+widths.size: + return prefix + nameCell(widths.name) + gap + typeCell + gap + sizeCell + case available >= artifactMinName+artifactColumnGap+widths.size: + nameWidth := min(widths.name, available-artifactColumnGap-widths.size) + return prefix + nameCell(nameWidth) + gap + sizeCell + default: + return prefix + ansi.Truncate(name, max(1, available), "…") + } +} + +// artifactViewContent renders the grouped artifact lines for one content width +// and returns the line index of each flat artifact ref, so selection can scroll +// it into view. Group headers, the shared capture summary and the partial +// capture notes precede each visit's aligned rows. +func artifactViewContent(visits []artifactVisit, widths artifactWidths, width, selected int) ([]string, []int) { + if len(visits) == 0 { + return []string{"No recorded visits yet."}, nil + } + var lines []string + var refLines []int + ref := 0 + for i, visit := range visits { + if i > 0 { + lines = append(lines, "") + } + lines = append(lines, lipgloss.NewStyle().Bold(true).Foreground(themeAccent).Render(fmt.Sprintf("%s · visit %d", ansi.Strip(visit.stage), visit.visit))) + lines = append(lines, lipgloss.NewStyle().Foreground(themePrimary).Render(artifactsSummary(visit.step))) + for _, note := range artifactCaptureNotes(visit.step) { + lines = append(lines, lipgloss.NewStyle().Foreground(themeMuted).Render(note)) + } + for _, artifact := range visit.artifacts { + lines = append(lines, artifactRowText(artifact, widths, width, ref == selected)) + refLines = append(refLines, len(lines)-1) + ref++ + } + } + return lines, refLines +} + +func (m Model) artifactViewVisits() ([]artifactVisit, []artifactRef) { + if m.runDetail == nil { + return nil, nil + } + visits := runArtifactVisits(m.runDetail.Steps) + return visits, artifactRefs(visits) +} + +func (m *Model) openArtifacts() { + if m.runDetail == nil { + m.runError = "Open a run to inspect its artifacts." + return + } + m.artifactsOpen = true + m.artifactIndex = 0 + m.artifactTop = 0 +} + +func (m *Model) selectArtifactByName(name string) { + _, refs := m.artifactViewVisits() + for i, ref := range refs { + if ref.artifact.Name == name { + m.artifactIndex = i + break + } + } + m.clampArtifactScroll() +} + +// clampArtifactScroll keeps the selected artifact row inside the visible page +// after a selection move, using the same page size as the renderer. +func (m *Model) clampArtifactScroll() { + if m.runDetail == nil { + m.artifactIndex, m.artifactTop = 0, 0 + return + } + visits, refs := m.artifactViewVisits() + m.artifactIndex = max(0, min(m.artifactIndex, max(0, len(refs)-1))) + if len(refs) == 0 { + m.artifactTop = 0 + return + } + widths := artifactWidthsFor(visits) + inner := max(1, m.width-4) + lines, refLines := artifactViewContent(visits, widths, inner, m.artifactIndex) + if m.artifactIndex >= len(refLines) { + return + } + m.artifactTop = artifactScrollTop(m.artifactTop, refLines[m.artifactIndex], m.artifactsPageSize(), len(lines)) +} + +func (m Model) updateArtifacts(msg tea.KeyMsg) (tea.Model, tea.Cmd) { + _, refs := m.artifactViewVisits() + count := len(refs) + switch msg.String() { + case "esc": + m.artifactsOpen = false + return m, nil + case "j", "down": + m.artifactIndex = min(m.artifactIndex+1, max(0, count-1)) + case "k", "up": + m.artifactIndex = max(0, m.artifactIndex-1) + case "g", "home": + m.artifactIndex = 0 + case "G", "end": + m.artifactIndex = max(0, count-1) + case "enter", "o": + if count == 0 { + return m, nil + } + return m, openArtifactCmd(m.url, refs[m.artifactIndex].artifact) + case "y": + if count == 0 { + return m, nil + } + return m, copyArtifactCmd(m.url, refs[m.artifactIndex].artifact) + } + m.clampArtifactScroll() + return m, nil +} + +func (m Model) artifactsView(height int) string { + if m.runDetail == nil { + return fitBlock([]string{"Open a run to inspect its artifacts."}, m.width, height) + } + if height <= 0 || m.width <= 0 { + return "" + } + visits, refs := m.artifactViewVisits() + widths := artifactWidthsFor(visits) + selected := max(0, min(m.artifactIndex, max(0, len(refs)-1))) + inner := max(1, m.width-4) + lines, refLines := artifactViewContent(visits, widths, inner, selected) + page := max(0, height-4) + top := max(0, min(m.artifactTop, max(0, len(lines)-page))) + if selected < len(refLines) { + // Keep the selected row on screen even if a slightly different page + // size left the stored offset behind. + top = artifactScrollTop(top, refLines[selected], page, len(lines)) + } + body := lines[top:min(len(lines), top+page)] + title := fmt.Sprintf("Artifacts · run #%d", m.runDetail.ID) + if len(visits) > 0 { + title = fmt.Sprintf("Artifacts · run #%d · %d recorded · %d visits", m.runDetail.ID, len(refs), len(visits)) + } + right := "" + if len(refs) > 0 { + right = fmt.Sprintf("%d/%d", selected+1, len(refs)) + } + footer := "No artifacts recorded" + if len(refs) > 0 { + // Keep the selected entry's URL available even when the row is too + // narrow to carry its own URL column. + footer = refs[selected].artifact.URL + } + return panel(m.width, height, []string{bar(title, right, inner, true)}, body, footer, true) +} + +// artifactActionMsg reports the outcome of handing a URL to the opener or the +// clipboard, so a failure is surfaced instead of silently dropped. +type artifactActionMsg struct { + verb string + url string + err error +} + +// openExternal and copyText are seams so tests can observe the resolved URL +// without launching a process or touching the system clipboard. +var ( + openExternal = func(target string) error { return openerCommand(target).Run() } + copyText = clipboard.WriteAll +) + +// openerCommand is the platform command that hands target to the system opener. +func openerCommand(target string) *exec.Cmd { + switch runtime.GOOS { + case "darwin": + return exec.Command("open", target) + case "windows": + return exec.Command("rundll32", "url.dll,FileProtocolHandler", target) + default: + return exec.Command("xdg-open", target) + } +} + +// resolveArtifactURL resolves a server-relative artifact URL against the +// configured websocket server URL by mapping ws/wss to http/https and keeping +// host:port, mirroring the app's ServerUrl.resolveArtifactUrl; it never +// hardcodes a host. An already-absolute http(s) URL is returned unchanged, and +// a blank or invalid configuration fails. +func resolveArtifactURL(serverURL, artifactURL string) (string, bool) { + target := strings.TrimSpace(artifactURL) + if target == "" { + return "", false + } + if strings.HasPrefix(target, "http://") || strings.HasPrefix(target, "https://") { + return target, true + } + parsed, err := url.Parse(strings.TrimSpace(serverURL)) + if err != nil || (parsed.Scheme != "ws" && parsed.Scheme != "wss") || parsed.Hostname() == "" { + return "", false + } + scheme := "http" + if parsed.Scheme == "wss" { + scheme = "https" + } + if !strings.HasPrefix(target, "/") { + target = "/" + target + } + return scheme + "://" + parsed.Host + target, true +} + +func artifactURLAction(verb, serverURL string, artifact protocol.StageArtifact) tea.Cmd { + resolved, ok := resolveArtifactURL(serverURL, artifact.URL) + if !ok { + return func() tea.Msg { + return artifactActionMsg{verb: verb, err: fmt.Errorf("no server URL to resolve %q", artifact.Name)} + } + } + action := openExternal + if verb == "copy" { + action = copyText + } + return func() tea.Msg { + if err := action(resolved); err != nil { + return artifactActionMsg{verb: verb, url: resolved, err: err} + } + return artifactActionMsg{verb: verb, url: resolved} + } +} + +func openArtifactCmd(serverURL string, artifact protocol.StageArtifact) tea.Cmd { + return artifactURLAction("open", serverURL, artifact) +} + +func copyArtifactCmd(serverURL string, artifact protocol.StageArtifact) tea.Cmd { + return artifactURLAction("copy", serverURL, artifact) +} diff --git a/internal/tui/artifacts_live_test.go b/internal/tui/artifacts_live_test.go new file mode 100644 index 00000000..f80fe978 --- /dev/null +++ b/internal/tui/artifacts_live_test.go @@ -0,0 +1,52 @@ +package tui + +import ( + "context" + "encoding/json" + "os" + "strings" + "testing" + + "git.a-wan.net/alan/pipelines/internal/protocol" + "github.com/charmbracelet/x/ansi" +) + +// TestLiveArtifactsFromCapture renders the artifacts view from real +// `pipelines inspect RUN_ID --json` captures. It is env-gated and skips unless +// PIPELINES_ARTIFACT_CAPTURES names one or more capture files, so it never +// depends on a running server in CI; the operator points it at real captures to +// re-confirm that mixed artifact types, a partial capture, a running visit and +// a finished visit with none all render. +func TestLiveArtifactsFromCapture(t *testing.T) { + list := os.Getenv("PIPELINES_ARTIFACT_CAPTURES") + if strings.TrimSpace(list) == "" { + t.Skip("set PIPELINES_ARTIFACT_CAPTURES to comma-separated `pipelines inspect RUN_ID --json` files") + } + m := New(context.Background(), "ws://127.0.0.1:8080/connect") + m.width, m.height = 120, 60 + var rendered strings.Builder + for _, path := range strings.Split(list, ",") { + path = strings.TrimSpace(path) + if path == "" { + continue + } + data, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + var detail protocol.RunDetail + if err := json.Unmarshal(data, &detail); err != nil { + t.Fatal(err) + } + m.runDetail = &detail + view := ansi.Strip(m.artifactsView(50)) + t.Logf("capture %s (run #%d executing=%t):\n%s", path, detail.ID, detail.Executing, view) + rendered.WriteString(view) + } + text := rendered.String() + for _, want := range []string{"[img]", "partial capture", "capture pending", "not recorded for this visit", "image/png", "KiB"} { + if !strings.Contains(text, want) { + t.Errorf("live captures did not render %q", want) + } + } +} diff --git a/internal/tui/artifacts_test.go b/internal/tui/artifacts_test.go new file mode 100644 index 00000000..15eb5853 --- /dev/null +++ b/internal/tui/artifacts_test.go @@ -0,0 +1,274 @@ +package tui + +import ( + "context" + "fmt" + "strings" + "testing" + + "git.a-wan.net/alan/pipelines/internal/protocol" + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/x/ansi" +) + +func artifactsFixtureModel() Model { + m := New(context.Background(), "ws://127.0.0.1:8080/connect") + m.width, m.height = 100, 30 + m.runOpen = true + m.keys.runs = true + m.runDetailFocus = true + m.requestedRunID = 169 + m.runDetail = &protocol.RunDetail{ + RunSummary: protocol.RunSummary{ID: 169, Name: "demo", Version: 3, Status: "succeeded"}, + Steps: []protocol.RunStep{ + {Sequence: 1, Stage: "implementer", Status: "completed", Artifacts: []protocol.StageArtifact{ + {Name: "build.log", SizeBytes: 2337, ContentType: "text/plain; charset=utf-8", URL: "/artifacts/169/1/build.log"}, + }, ArtifactsSkipped: 1}, + {Sequence: 2, Stage: "ui_reviewer", Status: "completed", Artifacts: []protocol.StageArtifact{ + {Name: "01-pill.png", SizeBytes: 80544, ContentType: "image/png", URL: "/artifacts/169/2/01-pill.png"}, + {Name: "morph.mp4", SizeBytes: 645782, ContentType: "video/mp4", URL: "/artifacts/169/2/morph.mp4"}, + }}, + {Sequence: 3, Stage: "committer", Status: "completed"}, + {Sequence: 4, Stage: "reviewer", Status: "running"}, + }, + } + return m +} + +func artifactKey(m Model, key rune) Model { + updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{key}}) + return updated.(Model) +} + +func TestRunArtifactVisitsGroupAndOrder(t *testing.T) { + steps := []protocol.RunStep{ + {Sequence: 3, Stage: "build"}, + {Sequence: 2, Stage: "plan"}, + {Sequence: 1, Stage: "plan", Control: &protocol.ControlEvidence{Visit: 1}, Artifacts: []protocol.StageArtifact{{Name: "plan.md"}}}, + } + visits := runArtifactVisits(steps) + want := []struct { + stage string + visit int + }{{"build", 1}, {"plan", 1}, {"plan", 2}} + if len(visits) != len(want) { + t.Fatalf("visits = %d, want %d", len(visits), len(want)) + } + for i, w := range want { + if visits[i].stage != w.stage || visits[i].visit != w.visit { + t.Fatalf("visit %d = %s/%d, want %s/%d", i, visits[i].stage, visits[i].visit, w.stage, w.visit) + } + } +} + +func TestArtifactsViewGroupsMarkersAndWording(t *testing.T) { + m := artifactsFixtureModel() + m.width = 120 + view := ansi.Strip(m.artifactsView(24)) + for _, want := range []string{ + "implementer · visit 1", + "ui_reviewer · visit 1", + "committer · visit 1", + "reviewer · visit 1", + "Artifacts · 1 recorded · partial capture", + "1 files excluded from artifact capture.", + "Artifacts · 2 recorded", + "Artifacts · not recorded for this visit", + "Artifacts · capture pending", + "[img]", + "01-pill.png", + "morph.mp4", + "video/mp4", + "78.7 KiB", + "630.6 KiB", + "/artifacts/169/2/01-pill.png", + } { + if !strings.Contains(view, want) { + t.Fatalf("artifacts view missing %q:\n%s", want, view) + } + } + for _, line := range strings.Split(view, "\n") { + if strings.Contains(line, "morph.mp4") && strings.Contains(line, "[img]") { + t.Fatalf("non-image artifact marked as an image: %q", line) + } + } +} + +func TestArtifactsViewColumnsAlign(t *testing.T) { + m := artifactsFixtureModel() + m.width = 120 + lines := strings.Split(ansi.Strip(m.artifactsView(24)), "\n") + var image, video string + for _, line := range lines { + if strings.Contains(line, "01-pill.png") { + image = line + } + if strings.Contains(line, "morph.mp4") { + video = line + } + } + if image == "" || video == "" { + t.Fatalf("artifact rows not both rendered:\n%s", strings.Join(lines, "\n")) + } + name := strings.Index(image, "01-pill.png") + typeStart := strings.Index(image, "image/png") + sizeStart := strings.Index(image, "78.7 KiB") + if name != strings.Index(video, "morph.mp4") || name < 0 { + t.Fatalf("name column misaligned:\n%q\n%q", image, video) + } + if typeStart != strings.Index(video, "video/mp4") || typeStart < 0 { + t.Fatalf("type column misaligned:\n%q\n%q", image, video) + } + if sizeStart+len("78.7 KiB") != strings.Index(video, "630.6 KiB")+len("630.6 KiB") || sizeStart < 0 { + t.Fatalf("size column misaligned:\n%q\n%q", image, video) + } +} + +func TestArtifactsViewNeverOverflows(t *testing.T) { + for _, width := range []int{160, 120, 80, 63, 40, 24, 12} { + m := artifactsFixtureModel() + m.width = width + for _, line := range strings.Split(m.artifactsView(24), "\n") { + if got := ansi.StringWidth(line); got > width { + t.Fatalf("width %d: line width %d: %q", width, got, line) + } + } + } + m := artifactsFixtureModel() + m.width = 40 + view := ansi.Strip(m.artifactsView(24)) + if !strings.Contains(view, "/artifacts/169/1/build.log") { + t.Fatalf("narrow view must keep the selected URL available:\n%s", view) + } +} + +func TestArtifactsViewEmptyRun(t *testing.T) { + m := New(context.Background(), "ws://127.0.0.1:8080/connect") + m.width, m.height = 100, 24 + m.runDetail = &protocol.RunDetail{RunSummary: protocol.RunSummary{ID: 5}} + view := ansi.Strip(m.artifactsView(20)) + for _, want := range []string{"Artifacts · run #5", "No recorded visits yet.", "No artifacts recorded"} { + if !strings.Contains(view, want) { + t.Fatalf("empty artifacts view missing %q:\n%s", want, view) + } + } +} + +func artifactsScrollFixture() (Model, []string) { + m := New(context.Background(), "ws://127.0.0.1:8080/connect") + m.width, m.height = 80, 14 + m.runOpen = true + m.keys.runs = true + m.runDetailFocus = true + m.requestedRunID = 7 + var steps []protocol.RunStep + names := make([]string, 0, 12) + for i := 0; i < 12; i++ { + name := fmt.Sprintf("shot-%02d.png", i) + names = append(names, name) + steps = append(steps, protocol.RunStep{Sequence: i + 1, Stage: "ui_reviewer", Status: "completed", + Artifacts: []protocol.StageArtifact{{Name: name, SizeBytes: 1024, ContentType: "image/png", URL: "/artifacts/7/" + name}}}) + } + m.runDetail = &protocol.RunDetail{RunSummary: protocol.RunSummary{ID: 7}, Steps: steps} + m.artifactsOpen = true + return m, names +} + +func TestArtifactsViewKeepsSelectionVisible(t *testing.T) { + m, names := artifactsScrollFixture() + for i := 0; i < len(names)-1; i++ { + m = artifactKey(m, 'j') + } + if view := ansi.Strip(m.View()); !strings.Contains(view, names[len(names)-1]) { + t.Fatalf("last artifact scrolled out of view:\n%s", view) + } + for i := 0; i < len(names)-1; i++ { + m = artifactKey(m, 'k') + } + if view := ansi.Strip(m.View()); !strings.Contains(view, names[0]) { + t.Fatalf("first artifact not visible after scrolling back:\n%s", view) + } +} + +func TestArtifactsSelectByNameScrollsIntoView(t *testing.T) { + m, names := artifactsScrollFixture() + m.selectArtifactByName(names[len(names)-1]) + if view := ansi.Strip(m.View()); !strings.Contains(view, names[len(names)-1]) { + t.Fatalf("selectArtifactByName did not scroll the match into view:\n%s", view) + } +} + +func TestResolveArtifactURL(t *testing.T) { + cases := []struct { + server, artifact, want string + ok bool + }{ + {"ws://127.0.0.1:8080/connect", "/artifacts/1/2/a.png", "http://127.0.0.1:8080/artifacts/1/2/a.png", true}, + {"wss://host/connect", "/artifacts/1/2/a.png", "https://host/artifacts/1/2/a.png", true}, + {"ws://host/connect", "artifacts/1/2/a.png", "http://host/artifacts/1/2/a.png", true}, + {"ws://host/connect", "https://cdn.example/a.png", "https://cdn.example/a.png", true}, + {"", "/artifacts/1/2/a.png", "", false}, + {"http://host/connect", "/artifacts/1/2/a.png", "", false}, + } + for _, tc := range cases { + got, ok := resolveArtifactURL(tc.server, tc.artifact) + if ok != tc.ok || got != tc.want { + t.Fatalf("resolveArtifactURL(%q, %q) = %q,%t want %q,%t", tc.server, tc.artifact, got, ok, tc.want, tc.ok) + } + } +} + +func TestArtifactsViewOpenAndCopyKeys(t *testing.T) { + previousOpen, previousCopy := openExternal, copyText + defer func() { openExternal, copyText = previousOpen, previousCopy }() + var opened, copied string + openExternal = func(target string) error { opened = target; return nil } + copyText = func(text string) error { copied = text; return nil } + + m := artifactsFixtureModel() + m = artifactKey(m, 'A') + if !m.artifactsOpen { + t.Fatal("A did not open the artifacts view") + } + if m.artifactIndex != 0 { + t.Fatalf("selection = %d, want the first artifact", m.artifactIndex) + } + + updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter}) + m = updated.(Model) + if cmd == nil { + t.Fatal("Enter did not produce an open command") + } + updated, _ = m.Update(cmd()) + m = updated.(Model) + if opened != "http://127.0.0.1:8080/artifacts/169/1/build.log" { + t.Fatalf("opened %q", opened) + } + + m = artifactKey(m, 'j') + updated, cmd = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'y'}}) + m = updated.(Model) + if cmd == nil { + t.Fatal("y did not produce a copy command") + } + updated, _ = m.Update(cmd()) + m = updated.(Model) + if copied != "http://127.0.0.1:8080/artifacts/169/2/01-pill.png" { + t.Fatalf("copied %q", copied) + } + if !strings.Contains(strings.Join(m.lines, "\n"), "Copied artifact URL") { + t.Fatalf("copy outcome not surfaced: %v", m.lines) + } +} + +func TestArtifactsViewRenderedInRunDetail(t *testing.T) { + m := artifactsFixtureModel() + m.artifactsOpen = true + view := ansi.Strip(m.View()) + if !strings.Contains(view, "Artifacts · run #169") { + t.Fatalf("run view did not render the artifacts view:\n%s", view) + } + if !strings.Contains(m.activeFooter(), "y copy URL") { + t.Fatalf("footer does not advertise the artifact controls: %q", m.activeFooter()) + } +} diff --git a/internal/tui/context_footer.go b/internal/tui/context_footer.go index 55d3d63d..19ad0350 100644 --- a/internal/tui/context_footer.go +++ b/internal/tui/context_footer.go @@ -15,6 +15,8 @@ func (m Model) activeFooter() string { text = "Refreshing published state · draft retained" case m.decisionOpen: text = "↑/↓ choose · Tab feedback · Enter decide · Esc cancel" + case m.artifactsOpen: + text = "j/k select · Enter open · y copy URL · Esc back" case m.actionConfirm != nil || m.mutationCommand != nil: text = "y confirm · n/Esc cancel · j/k read" case m.draftDeletingPath != "": @@ -35,7 +37,7 @@ func (m Model) activeFooter() string { case m.versionsOpen: text = "↑/↓ choose · Enter view version · Esc back" case m.paletteOpen && m.searchOpen: - text = "Type to search · ↑/↓ choose · Enter open · Esc cancel" + text = "Search pipelines, runs, steps, artifacts and text · ↑/↓ choose · Enter open · Esc cancel" case m.paletteOpen: text = "Type a command · ↑/↓ choose · Enter execute · Esc cancel" case m.logOpen: @@ -63,6 +65,8 @@ func (m Model) activeFooter() string { text = "Refreshing · draft retained" case m.decisionOpen: text = "Esc cancel · Enter decide · ↑↓ select" + case m.artifactsOpen: + text = "Esc back · Enter open · y copy" case m.actionConfirm != nil || m.mutationCommand != nil: text = "Esc cancel · y confirm · j/k read" case m.draftDeletingPath != "": @@ -83,7 +87,7 @@ func (m Model) activeFooter() string { case m.versionsOpen: text = "Esc back · Enter view · ↑↓ select" case m.paletteOpen && m.searchOpen: - text = "Esc back · Enter open · ↑↓ select" + text = "Esc cancel · Enter open · ↑↓ select" case m.paletteOpen: text = "Esc back · Enter run · ↑↓ select" case m.logOpen: diff --git a/internal/tui/dashboard.go b/internal/tui/dashboard.go index 0689a3e2..7d87f553 100644 --- a/internal/tui/dashboard.go +++ b/internal/tui/dashboard.go @@ -374,6 +374,11 @@ func (m Model) updateLogs(msg tea.KeyMsg) (tea.Model, tea.Cmd) { return m, nil } +// popup centres a dialog of at most preferredWidth (clamped to width-6) and +// truncates every line to that width so a modal cannot break the layout. Below +// roughly 20 columns the boxed dialog no longer fits, so popup falls back to the +// bare truncated lines without chrome; the TUI is designed for terminals at +// least 20 columns wide. func popup(width, height, preferredWidth int, lines []string) string { inner := min(preferredWidth, max(1, width-6)) for i := range lines { diff --git a/internal/tui/dashboard_test.go b/internal/tui/dashboard_test.go index 91654fcc..c18d4956 100644 --- a/internal/tui/dashboard_test.go +++ b/internal/tui/dashboard_test.go @@ -143,7 +143,7 @@ func TestHomeOnlyAdvertisesSearchAndCommands(t *testing.T) { m.usage = &protocol.UsageStats{} } view := ansi.Strip(m.View()) - for _, hint := range []string{"/ search", ": commands"} { + for _, hint := range []string{"/ omni search", ": commands"} { if !strings.Contains(view, hint) { t.Fatal("missing home hint", hint, view) } diff --git a/internal/tui/file_changes.go b/internal/tui/file_changes.go index 6528268f..2319c6b2 100644 --- a/internal/tui/file_changes.go +++ b/internal/tui/file_changes.go @@ -57,23 +57,22 @@ func humanBytes(size int64) string { return fmt.Sprintf("%.1f %ciB", float64(size)/float64(div), "KMGTPE"[exp]) } -// artifactLines lists one step's recorded artifacts (name and size) for the -// retained-evidence view, matching the file-changes style. -func artifactLines(step protocol.RunStep) []string { +// artifactCaptureNotes lists the partial-capture notes for one visit: one line +// per non-zero excluded/omitted count, and nothing when the capture was +// complete. A visit with no recorded artifacts yields no notes, mirroring the +// app's artifactCaptureNotes, so notes never accompany an empty-state summary. +func artifactCaptureNotes(step protocol.RunStep) []string { if len(step.Artifacts) == 0 { return nil } - lines := []string{artifactsSummary(step)} - for _, artifact := range step.Artifacts { - lines = append(lines, fmt.Sprintf("%s · %s · %s", ansi.Strip(artifact.Name), humanBytes(artifact.SizeBytes), ansi.Strip(artifact.ContentType))) - } + var notes []string if step.ArtifactsSkipped > 0 { - lines = append(lines, fmt.Sprintf("%d files excluded from artifact capture.", step.ArtifactsSkipped)) + notes = append(notes, fmt.Sprintf("%d files excluded from artifact capture.", step.ArtifactsSkipped)) } if step.ArtifactsTruncated > 0 { - lines = append(lines, fmt.Sprintf("%d files omitted by artifact capture limits.", step.ArtifactsTruncated)) + notes = append(notes, fmt.Sprintf("%d files omitted by artifact capture limits.", step.ArtifactsTruncated)) } - return lines + return notes } func diffHighlight(line string) string { diff --git a/internal/tui/model.go b/internal/tui/model.go index 4d7263c5..66c12d65 100644 --- a/internal/tui/model.go +++ b/internal/tui/model.go @@ -134,7 +134,8 @@ type Model struct { commandIndex int homeTop int searchOpen bool - searchResults []protocol.SearchMatch + searchResults []protocol.SearchResult + searchText string searchRequest string searchInFlight bool searchQueued bool @@ -203,6 +204,9 @@ type Model struct { decisionIndex int decisionFeedback textinput.Model decisionFeedbackFocus bool + artifactsOpen bool + artifactIndex int + artifactTop int editorOpen bool editorInput textinput.Model editorIndex int @@ -275,6 +279,17 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.appendLine("Harness debug: " + msg.err.Error()) } return m, nil + case artifactActionMsg: + if msg.err != nil { + m.appendLine("Artifact " + msg.verb + ": " + msg.err.Error()) + return m, nil + } + if msg.verb == "copy" { + m.appendLine("Copied artifact URL: " + msg.url) + } else { + m.appendLine("Opened artifact URL: " + msg.url) + } + return m, nil case spinner.TickMsg: var cmd tea.Cmd m.runSpinner, cmd = m.runSpinner.Update(msg) @@ -360,11 +375,12 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.statusRequest = "" } if msg.Type == protocol.SearchResults { - if msg.ID == m.searchRequest { + if msg.ID == m.searchRequest && msg.SearchResults != nil { m.searchInFlight = false - m.searchResults = msg.Matches + m.searchResults = msg.SearchResults.Results + m.searchText = msg.SearchResults.Query m.searchError = "" - m.commandIndex = min(m.commandIndex, max(0, len(msg.Matches)-1)) + m.commandIndex = min(m.commandIndex, max(0, len(msg.SearchResults.Results)-1)) if m.searchQueued { m.searchQueued = false updated, cmd := m.requestSearch() @@ -442,6 +458,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.searchInFlight = false m.searchQueued = false m.searchResults = nil + m.searchText = "" m.searchError = msg.Error } } else if msg.Type == protocol.Submitted { @@ -638,6 +655,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if m.decisionOpen { return m.updateDecision(msg) } + if m.artifactsOpen { + return m.updateArtifacts(msg) + } if m.runOpen && (msg.String() != ":" || m.runSearchOpen) { return m.updateRuns(msg) } @@ -667,6 +687,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.commandInput = newInput() if m.searchOpen { m.searchResults = nil + m.searchText = "" m.searchError = "" m.commandInput.Placeholder = "Type to search…" return m, m.commandInput.Focus() @@ -845,6 +866,9 @@ func (m Model) View() string { if m.decisionOpen { content = m.decisionView(bodyHeight) } + if m.artifactsOpen { + content = m.artifactsView(bodyHeight) + } if m.mutationCommand != nil { paragraphs := []string{"This opens a new session/editor and may modify a retained draft.", "Published definitions and previous run history are unchanged."} if strings.Contains(m.mutationTitle, "debugging") { diff --git a/internal/tui/model_test.go b/internal/tui/model_test.go index 08772c17..fd456832 100644 --- a/internal/tui/model_test.go +++ b/internal/tui/model_test.go @@ -87,7 +87,7 @@ func TestLayoutResizes(t *testing.T) { if !strings.HasPrefix(lines[0], "Pipelines ") || !strings.HasSuffix(lines[0], "127.0.0.1:8080") || lipgloss.Width(lines[0]) != size.Width { t.Fatalf("invalid header: %q", lines[0]) } - if !strings.HasPrefix(lines[len(lines)-1], "/ search") || !strings.Contains(lines[len(lines)-1], ": commands") || !strings.Contains(lines[len(lines)-1], "● 20 NEW") { + if !strings.HasPrefix(lines[len(lines)-1], "/ omni search") || !strings.Contains(lines[len(lines)-1], ": commands") || !strings.Contains(lines[len(lines)-1], "● 20 NEW") { t.Fatalf("invalid footer: %q", lines[len(lines)-1]) } if strings.TrimSpace(lines[len(lines)-2]) != "" { diff --git a/internal/tui/pipeline_test.go b/internal/tui/pipeline_test.go index 6cbd0263..2157287b 100644 --- a/internal/tui/pipeline_test.go +++ b/internal/tui/pipeline_test.go @@ -17,6 +17,28 @@ import ( "git.a-wan.net/alan/pipelines/internal/server" ) +// typeSearch types into the omni popup, flushes the outgoing search request and +// reads the server response, so a test observes real server-side matches. +func typeSearch(t *testing.T, m Model, text string) Model { + t.Helper() + for _, r := range text { + updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{r}}) + m = updated.(Model) + if cmd != nil { + if batch, ok := cmd().(tea.BatchMsg); ok { + for _, c := range batch { + if c != nil { + c() + } + } + } + } + updated, _ = m.Update(m.read()()) + m = updated.(Model) + } + return m +} + func TestSearchVersionsAndEditSubmission(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() @@ -44,9 +66,9 @@ func TestSearchVersionsAndEditSubmission(t *testing.T) { updated, read = m.Update(read()) m = updated.(Model) m = press(m, "/") - m = press(m, "dmo") - if !m.searchOpen || !m.paletteOpen || len(m.searchMatches()) != 1 { - t.Fatal("omnisearch failed") + m = typeSearch(t, m, "demo") + if !m.searchOpen || !m.paletteOpen || len(m.searchResults) != 1 || m.searchResults[0].Kind != protocol.SearchKindPipeline { + t.Fatalf("omnisearch failed: %+v", m.searchResults) } updated, send := m.Update(tea.KeyMsg{Type: tea.KeyEnter}) m = updated.(Model) diff --git a/internal/tui/run_detail.go b/internal/tui/run_detail.go index 37a47814..79154144 100644 --- a/internal/tui/run_detail.go +++ b/internal/tui/run_detail.go @@ -101,6 +101,9 @@ func (m *Model) closeRuns() { m.runGraphFocus = false m.runGraphTop = 0 m.runGraphPan = 0 + m.artifactsOpen = false + m.artifactIndex = 0 + m.artifactTop = 0 m.closeDecision() } @@ -552,7 +555,9 @@ func (m Model) runSectionLines(width int) []string { } var diff []string if !m.runSectionClosed(runSectionFiles) { - diff = append(stageDiffLines(step), artifactLines(step)...) + // Artifacts have their own grouped view (A); this section is the + // observed workspace diff only. + diff = stageDiffLines(step) } content = append(content, diff) for _, receipt := range step.Notifications { @@ -646,7 +651,7 @@ func (m Model) runActions() string { text += " · D debug" } if m.runDetail != nil { - text += " · a evidence" + text += " · a evidence · A artifacts" } if m.runDetail != nil && m.runDetail.Status == protocol.RunStatusWaiting { text += " · c decide" @@ -729,7 +734,7 @@ func (m Model) runsDetailsView(height int) string { lines := m.runSectionLines(m.runDetailWidth()) page := max(0, innerHeight-5) top := max(0, min(m.runTop, max(0, len(lines)-page))) - footerHint := "o result · d diff · s progress · i input · t evidence · m details" + footerHint := "o result · d diff · s progress · i input · t evidence · m details · A artifacts" if r.Status == protocol.RunStatusWaiting { footerHint += " · c decide" } diff --git a/internal/tui/run_graph_test.go b/internal/tui/run_graph_test.go index c9fe6cf9..25d274bf 100644 --- a/internal/tui/run_graph_test.go +++ b/internal/tui/run_graph_test.go @@ -193,7 +193,7 @@ func TestRunningDurationAndOpenCodeEvidenceShortcut(t *testing.T) { m := runInspectionModel() m.runDetail.Steps[1].AgentActive = true m = runKey(m, "a") - if m.runStepIndex != 2 || !m.runDetailFocus || m.runSectionClosed(3) || !m.runSectionClosed(0) { + if m.runStepIndex != 2 || !m.runDetailFocus || m.runSectionClosed(runSectionEvidence) || !m.runSectionClosed(runSectionProgress) { t.Fatal("OpenCode evidence shortcut did not select active session") } view := ansi.Strip(strings.Join(m.runSectionLines(90), "\n")) diff --git a/internal/tui/runs.go b/internal/tui/runs.go index 2d239aec..a46dfa3e 100644 --- a/internal/tui/runs.go +++ b/internal/tui/runs.go @@ -322,10 +322,16 @@ func (m Model) updateRuns(msg tea.KeyMsg) (tea.Model, tea.Cmd) { return m.debugRun() case "a": return m.openOpenCodeEvidence() + case "A": + if m.runDetail != nil { + m.openArtifacts() + } + return m, nil case "r": return m.retrySelectedRun() case "esc": m.runGraphFocus = false + m.artifactsOpen = false if m.runDetail != nil || m.requestedRunID > 0 || m.runStartRequest != "" { m.runDetail = nil m.requestedRunID = 0 @@ -400,18 +406,21 @@ func (m Model) updateRuns(msg tea.KeyMsg) (tea.Model, tea.Cmd) { } case "s", "i", "o", "t", "m": if m.runDetail != nil { - m.toggleRunSection(strings.Index("siotm", msg.String())) + if index, ok := runSectionForShortcut(msg.String()); ok { + m.toggleRunSection(index) + } } case "d": if m.runDetail != nil { m.runGraphFocus = false m.runDetailFocus = true _, focused := runFocusedSection(m.runSectionLines(m.runDetailWidth()), m.runTop) - if m.runSectionClosed(5) || focused == "File changes" { - m.toggleRunSection(5) + if m.runSectionClosed(runSectionFiles) || focused == runSections[runSectionFiles].Title { + m.toggleRunSection(runSectionFiles) } + marker := "[" + runSections[runSectionFiles].Shortcut + "]" for i, line := range m.runSectionLines(m.runDetailWidth()) { - if strings.HasPrefix(ansi.Strip(line), "[d]") { + if strings.HasPrefix(ansi.Strip(line), marker) { m.runTop = i break }