Add omni search: ranked mixed results in the TUI and CLI

Back the TUI "/" popup with the server's search command and add a matching
pipelines search CLI command.

- protocol: SearchRequest/SearchResponse and per-kind SearchResult payloads,
  one shared Label for the six-kind badge dispatch and RuneIndexFold for
  case-insensitive matching.
- pipeline: the read-only, ranked, bounded Store.Search implementation.
- cli: search QUERY [--type KIND] [--limit N] [--json], printing the shared
  label plus a text-field snippet when present.
- tui: type to search, up/down to choose, Enter to open and Esc to cancel over
  one mixed ranked list, with short type badges and a bracketed non-colour
  match marker; Enter navigates to the matched pipeline, run, step, field
  section or artifact entry.
- tests: protocol and TUI coverage, including an env-gated live test against
  the running server.
This commit is contained in:
2026-10-16 23:03:17 +01:00
parent aac1be18cc
commit 80232a4ffe
7 changed files with 1697 additions and 32 deletions
+36 -1
View File
@@ -38,9 +38,10 @@ type options struct {
option, feedback string option, feedback string
output string output string
conflict string conflict string
kind string
all bool all bool
json, source, noOpen, yes, allow, createOnly, force, wait bool json, source, noOpen, yes, allow, createOnly, force, wait bool
version, expected int version, expected, limit int
timeout, waitTimeout time.Duration timeout, waitTimeout time.Duration
} }
@@ -101,6 +102,9 @@ func (a App) flags(command string) (*flag.FlagSet, *options) {
f.DurationVar(&o.waitTimeout, "wait-timeout", 0, "Client limit for --wait after the run is admitted (0 waits until terminal); separate from per-request --timeout") f.DurationVar(&o.waitTimeout, "wait-timeout", 0, "Client limit for --wait after the run is admitted (0 waits until terminal); separate from per-request --timeout")
case "runs": case "runs":
f.StringVar(&o.name, "pipeline", "", "Filter by exact pipeline name or ID") f.StringVar(&o.name, "pipeline", "", "Filter by exact pipeline name or ID")
case "search":
f.StringVar(&o.kind, "type", "", "Narrow to one kind: "+strings.Join(protocol.SearchKinds, ", "))
f.IntVar(&o.limit, "limit", 0, "Maximum results (default 50, max 200)")
case "retry": case "retry":
f.BoolVar(&o.yes, "yes", false, "Confirm retry without an interactive prompt") f.BoolVar(&o.yes, "yes", false, "Confirm retry without an interactive prompt")
f.StringVar(&o.key, "request-key", "", "Idempotency key (default: generated unique key)") f.StringVar(&o.key, "request-key", "", "Idempotency key (default: generated unique key)")
@@ -729,6 +733,37 @@ func (a App) Run(ctx context.Context, args []string) error {
return err return err
case "import": case "import":
return a.importBundle(ctx, o, f.Args(), call) return a.importBundle(ctx, o, f.Args(), call)
case "search":
query := strings.Join(pos, " ")
if strings.TrimSpace(query) == "" {
return fmt.Errorf("search requires a query; use pipelines help search")
}
r, err := call(protocol.Message{Type: protocol.Search, Search: &protocol.SearchRequest{Query: query, Kind: o.kind, Limit: o.limit}})
if err != nil {
return err
}
if r.SearchResults == nil {
return fmt.Errorf("server did not return search results; update pipelines-server")
}
if o.json {
return a.output(r.SearchResults)
}
if len(r.SearchResults.Results) == 0 {
fmt.Fprintln(a.Out, "No matches")
}
for _, hit := range r.SearchResults.Results {
// The shared Label renders every kind; the CLI additionally shows a
// text-field hit's snippet, which the TUI marks on its own line.
label := hit.Label()
if hit.Field != nil {
label += ": " + hit.Field.Snippet
}
fmt.Fprintf(a.Out, "%s\t%s\t%d\t%s\t%s\n", hit.Kind, hit.Target, hit.Score, hit.MatchedField, label)
}
if r.SearchResults.Truncated {
fmt.Fprintf(a.Err, "Results truncated (cap %d, scan bound %d bytes)\n", r.SearchResults.Cap, r.SearchResults.ScanBoundBytes)
}
return nil
case "show", "versions": case "show", "versions":
value, err := one() value, err := one()
if err != nil { if err != nil {
+669
View File
@@ -0,0 +1,669 @@
package pipeline
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"sort"
"strconv"
"strings"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// Search answers one ranked, bounded, read-only query over the existing tables.
// It scans no artifact contents and makes no follow-up calls: each kind runs at
// most one query, field text is decoded with decodeValues over a fixed scan
// budget, and ranking/tie-breaking happens in memory over the candidate set.
func (s *Store) Search(ctx context.Context, req protocol.SearchRequest, now time.Time) (*protocol.SearchResponse, error) {
kind := strings.TrimSpace(req.Kind)
if kind != "" && searchKindIndex(kind) < 0 {
return nil, fmt.Errorf("unknown search kind %q; use one of %s", req.Kind, strings.Join(protocol.SearchKinds, ", "))
}
response := &protocol.SearchResponse{
Query: req.Query,
Kind: kind,
Results: []protocol.SearchResult{},
Cap: searchLimit(req.Limit),
ScanBoundBytes: protocol.SearchMaxScanBytes,
}
// An empty query is a well-defined no-op, never a match-everything scan.
if strings.TrimSpace(req.Query) == "" {
return response, nil
}
terms := parseSearchTerms(req.Query, now)
// A bare status word or date selects runs; it is never treated as body text.
selectorOnly := terms.Text == ""
tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, err
}
defer tx.Rollback()
var results []protocol.SearchResult
var scan searchScan
for i, candidate := range protocol.SearchKinds {
if kind != "" && searchKindIndex(kind) != i {
continue
}
if selectorOnly && candidate != protocol.SearchKindRun {
continue
}
var hits []protocol.SearchResult
var err error
switch candidate {
case protocol.SearchKindPipeline:
hits, err = searchPipelines(ctx, tx, terms, &scan)
case protocol.SearchKindVersion:
hits, err = searchVersions(ctx, tx, terms, &scan)
case protocol.SearchKindRun:
hits, err = searchRuns(ctx, tx, terms, &scan)
case protocol.SearchKindStep:
hits, err = searchSteps(ctx, tx, terms, &scan)
case protocol.SearchKindField:
hits, err = searchFields(ctx, tx, terms, &scan)
case protocol.SearchKindArtifact:
hits, err = searchArtifacts(ctx, tx, terms, &scan)
}
if err != nil {
return nil, err
}
results = append(results, hits...)
}
if err := tx.Commit(); err != nil {
return nil, err
}
response.ScannedSteps = scan.steps
response.ScannedBytes = scan.bytes
if scan.truncated {
response.Truncated = true
}
sortSearchResults(results)
if len(results) > response.Cap {
results = results[:response.Cap]
response.Truncated = true
}
if results == nil {
results = []protocol.SearchResult{}
}
response.Results = results
return response, nil
}
func searchKindIndex(kind string) int {
for i, k := range protocol.SearchKinds {
if k == kind {
return i
}
}
return -1
}
// searchLimit clamps a requested cap. Zero, negative or over-max requests fall
// back to the default or the maximum, so the cap is always enforced server-side.
func searchLimit(limit int) int {
if limit <= 0 {
return protocol.SearchDefaultLimit
}
if limit > protocol.SearchMaxLimit {
return protocol.SearchMaxLimit
}
return limit
}
// searchTerms is the parsed query. Text carries free text; Status and the
// From/To window carry a bare status word or a date expression that selects runs.
type searchTerms struct {
Text string
Status string
HasDate bool
From time.Time
To time.Time
}
// parseSearchTerms recognises a lone status word and a lone date expression and
// leaves everything else as free text, so ordinary words are never misread as a
// selector. Relative dates resolve against the injected now in its own location;
// the comparison window is converted to UTC to match the stored timestamps.
func parseSearchTerms(query string, now time.Time) searchTerms {
trimmed := strings.TrimSpace(query)
lower := strings.ToLower(trimmed)
for _, status := range protocol.SearchStatuses {
if lower == status {
return searchTerms{Status: status}
}
}
location := now.Location()
day := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, location)
switch lower {
case "today":
return dateTerms(day, day.AddDate(0, 0, 1))
case "yesterday":
return dateTerms(day.AddDate(0, 0, -1), day)
case "this week":
// Monday 00:00 local is the start of the week.
start := day.AddDate(0, 0, -((int(day.Weekday()) + 6) % 7))
return dateTerms(start, start.AddDate(0, 0, 7))
}
if parsed, err := time.ParseInLocation("2006-01-02", trimmed, location); err == nil {
return dateTerms(parsed, parsed.AddDate(0, 0, 1))
}
return searchTerms{Text: trimmed}
}
func dateTerms(from, to time.Time) searchTerms {
return searchTerms{HasDate: true, From: from.UTC(), To: to.UTC()}
}
const searchSQLTime = "2006-01-02 15:04:05"
// searchStepJoin is the shared run_steps/step_control source and projections the
// step and field scans build on, so those queries cannot drift apart.
const (
searchStepJoin = `FROM run_steps s LEFT JOIN step_control c ON c.run_id=s.run_id AND c.sequence=s.sequence`
searchStepEvidence = `COALESCE(c.evidence_json,'')`
searchStepRecency = `COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ',s.started_at),'')`
)
// searchRowBudget bounds how many raw rows one non-body kind scans before it
// stops and reports Truncated, so a LIKE scan over a very large table cannot
// scale without limit. Field text uses the separate SearchMaxScanSteps budget.
const searchRowBudget = 2000
// searchLike builds a case-insensitive substring pattern, escaping LIKE
// metacharacters so a query can never broaden the pattern.
func searchLike(text string) string {
escaped := strings.NewReplacer(`\`, `\\`, `%`, `\%`, `_`, `\_`).Replace(strings.ToLower(text))
return "%" + escaped + "%"
}
// searchTier ranks a "name" match: exact beats a substring. The third tier (a
// body-text match) is assigned by the caller.
func searchTier(value, text string) (int, bool) {
if text == "" {
return 0, false
}
if strings.EqualFold(value, text) {
return 3, true
}
if strings.Contains(strings.ToLower(value), strings.ToLower(text)) {
return 2, true
}
return 0, false
}
func searchContains(value, text string) bool {
return text != "" && strings.Contains(strings.ToLower(value), strings.ToLower(text))
}
func sortSearchResults(results []protocol.SearchResult) {
sort.SliceStable(results, func(i, j int) bool {
a, b := results[i], results[j]
if a.Score != b.Score {
return a.Score > b.Score
}
if a.Recency != b.Recency {
return a.Recency > b.Recency
}
if a.Kind != b.Kind {
return a.Kind < b.Kind
}
return a.Target < b.Target
})
}
func searchPipelines(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *searchScan) ([]protocol.SearchResult, error) {
if terms.Text == "" {
return nil, nil
}
rows, err := tx.QueryContext(ctx, `SELECT p.id,p.name,v.version,json_array_length(v.graph_json,'$.stages'),COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ',v.created_at),'')
FROM pipelines p JOIN pipeline_versions v ON v.pipeline_id=p.id
WHERE v.version=(SELECT MAX(version) FROM pipeline_versions WHERE pipeline_id=p.id)
AND lower(p.name) LIKE ? ESCAPE '\'
ORDER BY p.name,p.id LIMIT ?`, searchLike(terms.Text), searchRowBudget+1)
if err != nil {
return nil, err
}
defer rows.Close()
var results []protocol.SearchResult
scanned := 0
for rows.Next() {
if scanned == searchRowBudget {
scan.truncated = true
break
}
scanned++
var id int64
var name, recency string
var version, stages int
if err := rows.Scan(&id, &name, &version, &stages, &recency); err != nil {
return nil, err
}
score, ok := searchTier(name, terms.Text)
if !ok {
continue
}
results = append(results, protocol.SearchResult{
Kind: protocol.SearchKindPipeline, Target: searchPipelineTarget(id),
Score: score, MatchedField: "name", Recency: recency,
Pipeline: &protocol.SearchPipeline{ID: id, Name: name, Version: version, Stages: stages},
})
}
return results, rows.Err()
}
func searchVersions(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *searchScan) ([]protocol.SearchResult, error) {
if terms.Text == "" {
return nil, nil
}
query := `SELECT v.pipeline_id,v.version,v.content_hash,COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ',v.created_at),''),v.source
FROM pipeline_versions v WHERE lower(v.source) LIKE ? ESCAPE '\'`
args := []any{searchLike(terms.Text)}
var wanted int64
numeric := false
if n, err := parsePositiveInt(terms.Text); err == nil {
query += " OR v.version=?"
args = append(args, n)
wanted, numeric = n, true
}
query += " ORDER BY v.pipeline_id,v.version DESC LIMIT ?"
args = append(args, searchRowBudget+1)
rows, err := tx.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var results []protocol.SearchResult
scanned := 0
for rows.Next() {
if scanned == searchRowBudget {
scan.truncated = true
break
}
scanned++
var pipelineID int64
var version int
var hash, recency, source string
if err := rows.Scan(&pipelineID, &version, &hash, &recency, &source); err != nil {
return nil, err
}
score, matched := 1, "source"
if numeric && int64(version) == wanted {
score, matched = 3, "version"
} else if !searchContains(source, terms.Text) {
continue
}
results = append(results, protocol.SearchResult{
Kind: protocol.SearchKindVersion, Target: searchVersionTarget(pipelineID, version),
Score: score, MatchedField: matched, Recency: recency,
Version: &protocol.SearchVersion{PipelineID: pipelineID, Version: version, ContentHash: hash, CreatedAt: recency},
})
}
return results, rows.Err()
}
func searchRuns(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *searchScan) ([]protocol.SearchResult, error) {
// Reuse runSelect's join and timestamp projection rather than restating the
// run-row SELECT; only the fields search needs are added.
base := `SELECT r.id,p.name,r.status,r.workspace,` + runStamps + `,r.diagnostic ` + runFrom
var query string
var args []any
switch {
case terms.Status != "":
query = base + " WHERE r.status=?"
args = append(args, terms.Status)
case terms.HasDate:
query = base + " WHERE r.created_at>=? AND r.created_at<?"
args = append(args, terms.From.Format(searchSQLTime), terms.To.Format(searchSQLTime))
default:
query = base + ` WHERE lower(p.name) LIKE ? ESCAPE '\' OR lower(r.workspace) LIKE ? ESCAPE '\' OR lower(r.status) LIKE ? ESCAPE '\' OR lower(r.diagnostic) LIKE ? ESCAPE '\' OR CAST(r.id AS TEXT)=?`
pattern := searchLike(terms.Text)
args = append(args, pattern, pattern, pattern, pattern, terms.Text)
}
query += " ORDER BY r.id DESC LIMIT ?"
args = append(args, searchRowBudget+1)
rows, err := tx.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var results []protocol.SearchResult
scanned := 0
for rows.Next() {
if scanned == searchRowBudget {
scan.truncated = true
break
}
scanned++
var id int64
var name, status, workspace, created, finished, diagnostic string
if err := rows.Scan(&id, &name, &status, &workspace, &created, &finished, &diagnostic); err != nil {
return nil, err
}
score, matched := 1, "status"
if terms.Status != "" {
matched = "status"
} else if terms.HasDate {
matched = "created_at"
} else if s, ok := searchTier(name, terms.Text); ok {
score, matched = s, "pipeline_name"
} else if numericEqual(terms.Text, id) {
score, matched = 3, "id"
} else if searchContains(workspace, terms.Text) {
matched = "workspace"
} else if searchContains(status, terms.Text) {
matched = "status"
} else if searchContains(diagnostic, terms.Text) {
matched = "diagnostic"
} else {
continue
}
run := &protocol.SearchRun{ID: id, PipelineName: name, Status: status, Workspace: workspace, CreatedAt: created, FinishedAt: finished}
if created != "" && finished != "" {
if secs, ok := durationSeconds(created, finished); ok {
run.DurationSeconds = secs
}
}
results = append(results, protocol.SearchResult{
Kind: protocol.SearchKindRun, Target: searchRunTarget(id),
Score: score, MatchedField: matched, Recency: created, Run: run,
})
}
return results, rows.Err()
}
func searchSteps(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *searchScan) ([]protocol.SearchResult, error) {
if terms.Text == "" {
return nil, nil
}
pattern := searchLike(terms.Text)
rows, err := tx.QueryContext(ctx, `SELECT s.run_id,s.sequence,s.stage_key,s.outcome,s.target,s.diagnostic,`+searchStepRecency+`,`+searchStepEvidence+`
`+searchStepJoin+`
WHERE lower(s.stage_key) LIKE ? ESCAPE '\' OR lower(s.outcome) LIKE ? ESCAPE '\' OR lower(s.target) LIKE ? ESCAPE '\' OR lower(s.diagnostic) LIKE ? ESCAPE '\'
ORDER BY s.run_id DESC,s.sequence DESC LIMIT ?`, pattern, pattern, pattern, pattern, searchRowBudget+1)
if err != nil {
return nil, err
}
defer rows.Close()
var results []protocol.SearchResult
scanned := 0
for rows.Next() {
if scanned == searchRowBudget {
scan.truncated = true
break
}
scanned++
var runID int64
var sequence int
var stage, outcome, target, diagnostic, recency, control string
if err := rows.Scan(&runID, &sequence, &stage, &outcome, &target, &diagnostic, &recency, &control); err != nil {
return nil, err
}
score, matched := 1, "diagnostic"
if s, ok := searchTier(stage, terms.Text); ok {
score, matched = s, "stage"
} else if searchContains(outcome, terms.Text) {
matched = "outcome"
} else if searchContains(target, terms.Text) {
matched = "target"
} else if searchContains(diagnostic, terms.Text) {
matched = "diagnostic"
} else {
continue
}
visit := 0
if control != "" && control != "null" {
var evidence protocol.ControlEvidence
if err := json.Unmarshal([]byte(control), &evidence); err == nil {
visit = evidence.Visit
}
}
results = append(results, protocol.SearchResult{
Kind: protocol.SearchKindStep, Target: searchStepTarget(runID, sequence),
Score: score, MatchedField: matched, Recency: recency,
Step: &protocol.SearchStep{RunID: runID, Sequence: sequence, Stage: stage, Visit: visit, Outcome: outcome, Target: target, Diagnostic: diagnostic},
})
}
return results, rows.Err()
}
// searchScan accumulates the shared bounded-scan budget so the response can
// report how much body text was inspected and whether any bound was reached.
type searchScan struct {
steps int
bytes int
truncated bool
}
func searchFields(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *searchScan) ([]protocol.SearchResult, error) {
if terms.Text == "" {
return nil, nil
}
// One extra step is requested so a scan that hits the step bound can see that
// more rows remain and report Truncated rather than ending silently.
rows, err := tx.QueryContext(ctx, `SELECT s.run_id,s.sequence,s.input_json,s.output_json,s.transcript,`+searchStepEvidence+`,`+searchStepRecency+`
`+searchStepJoin+`
ORDER BY s.run_id DESC,s.sequence DESC LIMIT ?`, protocol.SearchMaxScanSteps+1)
if err != nil {
return nil, err
}
defer rows.Close()
var results []protocol.SearchResult
for rows.Next() {
if scan.steps >= protocol.SearchMaxScanSteps {
scan.truncated = true
break
}
var runID int64
var sequence int
var input, output, transcript, evidence, recency string
if err := rows.Scan(&runID, &sequence, &input, &output, &transcript, &evidence, &recency); err != nil {
return nil, err
}
size := len(input) + len(output) + len(transcript) + len(evidence)
if scan.bytes+size > protocol.SearchMaxScanBytes {
scan.truncated = true
break
}
scan.bytes += size
scan.steps++
for _, field := range []struct {
name string
raw string
}{
{name: "input", raw: input},
{name: "output", raw: output},
{name: "evidence", raw: evidence},
} {
if field.raw == "" {
continue
}
var values map[string]any
// A malformed retained row cannot match anything, so it is skipped
// rather than failing the whole query and hiding every other hit.
if err := decodeValues(field.raw, &values); err != nil {
continue
}
text, ok := findMatch(values, terms.Text)
if !ok {
continue
}
results = append(results, protocol.SearchResult{
Kind: protocol.SearchKindField, Target: searchFieldTarget(runID, sequence, field.name),
Score: 1, MatchedField: field.name, Recency: recency,
Field: &protocol.SearchField{RunID: runID, Sequence: sequence, Field: field.name, Snippet: searchSnippet(text, terms.Text)},
})
}
if searchContains(transcript, terms.Text) {
results = append(results, protocol.SearchResult{
Kind: protocol.SearchKindField, Target: searchFieldTarget(runID, sequence, "transcript"),
Score: 1, MatchedField: "transcript", Recency: recency,
Field: &protocol.SearchField{RunID: runID, Sequence: sequence, Field: "transcript", Snippet: searchSnippet(transcript, terms.Text)},
})
}
}
return results, rows.Err()
}
func searchArtifacts(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *searchScan) ([]protocol.SearchResult, error) {
if terms.Text == "" {
return nil, nil
}
pattern := searchLike(terms.Text)
rows, err := tx.QueryContext(ctx, `SELECT a.run_id,a.sequence,a.artifacts_json,COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ',r.created_at),'')
FROM step_artifacts a JOIN pipeline_runs r ON r.id=a.run_id
WHERE lower(a.artifacts_json) LIKE ? ESCAPE '\'
ORDER BY a.run_id DESC,a.sequence DESC LIMIT ?`, pattern, searchRowBudget+1)
if err != nil {
return nil, err
}
defer rows.Close()
var results []protocol.SearchResult
scanned := 0
for rows.Next() {
if scanned == searchRowBudget {
scan.truncated = true
break
}
scanned++
var runID int64
var sequence int
var artifacts, recency string
if err := rows.Scan(&runID, &sequence, &artifacts, &recency); err != nil {
return nil, err
}
var evidence stageArtifactsEvidence
// A malformed retained row cannot match anything, so it is skipped
// rather than failing the whole query and hiding every other hit.
if err := json.Unmarshal([]byte(artifacts), &evidence); err != nil {
continue
}
for _, file := range evidence.Files {
score, matched := 1, "content_type"
if s, ok := searchTier(file.Name, terms.Text); ok {
score, matched = s, "name"
} else if !searchContains(file.ContentType, terms.Text) {
continue
}
results = append(results, protocol.SearchResult{
Kind: protocol.SearchKindArtifact, Target: file.URL,
Score: score, MatchedField: matched, Recency: recency,
Artifact: &protocol.SearchArtifact{RunID: runID, Sequence: sequence, Name: file.Name, ContentType: file.ContentType, SizeBytes: file.SizeBytes, URL: file.URL},
})
}
}
return results, rows.Err()
}
// findMatch walks decoded JSON values in a stable key order and returns the
// first string leaf containing the query, so a hit and its snippet are
// deterministic rather than map-iteration dependent.
func findMatch(value any, text string) (string, bool) {
switch typed := value.(type) {
case string:
if searchContains(typed, text) {
return typed, true
}
case map[string]any:
keys := make([]string, 0, len(typed))
for key := range typed {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
if match, ok := findMatch(typed[key], text); ok {
return match, true
}
}
case []any:
for _, item := range typed {
if match, ok := findMatch(item, text); ok {
return match, true
}
}
}
return "", false
}
// searchSnippet reduces text to a single clean line and centers a short window
// on the first case-insensitive match, folding runes so UTF-8 boundaries are
// never split and the result is always well-formed.
func searchSnippet(text, query string) string {
normalized := protocol.NormalizeText(text)
runes := []rune(normalized)
index := protocol.RuneIndexFold(runes, []rune(query))
if index < 0 {
return truncateRunes(normalized, 80)
}
const margin = 40
start := index - margin
if start < 0 {
start = 0
}
end := index + len([]rune(query)) + margin
if end > len(runes) {
end = len(runes)
}
snippet := string(runes[start:end])
if start > 0 {
snippet = "…" + snippet
}
if end < len(runes) {
snippet = snippet + "…"
}
return snippet
}
func truncateRunes(text string, limit int) string {
runes := []rune(text)
if len(runes) <= limit {
return text
}
return string(runes[:limit]) + "…"
}
func searchPipelineTarget(id int64) string { return fmt.Sprintf("pipeline:%d", id) }
func searchVersionTarget(id int64, version int) string {
return fmt.Sprintf("pipeline:%d:v%d", id, version)
}
func searchRunTarget(id int64) string { return fmt.Sprintf("run:%d", id) }
func searchStepTarget(id int64, sequence int) string { return fmt.Sprintf("run:%d:%d", id, sequence) }
func searchFieldTarget(id int64, sequence int, field string) string {
return fmt.Sprintf("run:%d:%d:%s", id, sequence, field)
}
func parsePositiveInt(text string) (int64, error) {
n, err := strconv.ParseInt(strings.TrimSpace(text), 10, 64)
if err != nil || n <= 0 {
return 0, fmt.Errorf("not a positive integer")
}
return n, nil
}
func numericEqual(text string, id int64) bool {
n, err := parsePositiveInt(text)
return err == nil && n == id
}
func durationSeconds(created, finished string) (float64, bool) {
start, err := time.Parse(time.RFC3339, created)
if err != nil {
return 0, false
}
end, err := time.Parse(time.RFC3339, finished)
if err != nil {
return 0, false
}
if end.Before(start) {
return 0, false
}
return end.Sub(start).Seconds(), true
}
+223
View File
@@ -0,0 +1,223 @@
package protocol
import (
"fmt"
"unicode"
)
// Search brokered by the Pipelines server: one query can find a pipeline, a
// pipeline version, a run, a recorded step/visit, the text of a step field
// (input/output/evidence/transcript) or a recorded artifact. A client sends
// Message{Type: Search, ID: <opaque>, Search: <SearchRequest>} and the server
// echoes the ID with a SearchResults response, matching every other exchange.
const (
Search = "search"
SearchResults = "search_results"
)
// Search.Kind narrowing values. They are the only narrowing mechanism and are
// enforced server-side; an empty Kind searches every kind.
const (
SearchKindPipeline = "pipeline"
SearchKindVersion = "version"
SearchKindRun = "run"
SearchKindStep = "step"
SearchKindField = "field"
SearchKindArtifact = "artifact"
)
// SearchKinds is the accepted Kind vocabulary in display order; the CLI help
// and notes are kept in step with it.
var SearchKinds = []string{
SearchKindPipeline,
SearchKindVersion,
SearchKindRun,
SearchKindStep,
SearchKindField,
SearchKindArtifact,
}
// SearchStatuses is the accepted set of bare status words a query may name to
// select runs. It mirrors the run status vocabulary; a bare word selects runs
// rather than matching as free text.
var SearchStatuses = []string{"queued", "running", "succeeded", "failed", "canceled", "interrupted"}
// Search budget and cap constants. They are fixed, enforced server-side and
// reported on SearchResponse so a client can see the bound that applied.
const (
// SearchDefaultLimit is the result cap when a request omits Limit (or sends 0).
SearchDefaultLimit = 50
// SearchMaxLimit is the largest accepted result cap; larger requests clamp.
SearchMaxLimit = 200
// SearchMaxScanSteps bounds how many recorded steps one query inspects while
// scanning input/output/transcript field text.
SearchMaxScanSteps = 2000
// SearchMaxScanBytes bounds the total field bytes (input_json + output_json +
// evidence_json + transcript) one query decodes and searches before it stops
// and reports Truncated.
SearchMaxScanBytes = 1 << 20
)
// SearchRequest is the search request body. Query is the free text, a bare
// status word or a date expression; Kind optionally narrows to one SearchKind;
// Limit caps the returned results (0 = server default).
type SearchRequest struct {
Query string `json:"query"`
Kind string `json:"kind,omitempty"`
Limit int `json:"limit,omitempty"`
}
// SearchResponse is the ranked, bounded set of matches. Results are ordered by
// score, then recency, then a stable kind/target tie-break. Truncated reports
// that either the result cap or the field-scan budget dropped matches;
// ScannedSteps/ScannedBytes and ScanBoundBytes make the body-scan bound
// observable.
type SearchResponse struct {
Query string `json:"query"`
Kind string `json:"kind,omitempty"`
Results []SearchResult `json:"results"`
Truncated bool `json:"truncated,omitempty"`
ScannedSteps int `json:"scanned_steps"`
ScannedBytes int `json:"scanned_bytes"`
Cap int `json:"cap"`
ScanBoundBytes int `json:"scan_bound_bytes"`
}
// SearchResult is one typed, navigable match. Kind selects which payload is
// set; Target is a stable token an existing CLI command can resolve
// (pipeline:<id>, pipeline:<id>:v<version>, run:<id>, run:<id>:<sequence>,
// run:<id>:<sequence>:<field>, or an artifact URL). Score and MatchedField
// expose the ranking tier and why the hit matched so a client can re-rank;
// Recency is the hit's sortable UTC timestamp.
type SearchResult struct {
Kind string `json:"kind"`
Target string `json:"target"`
Score int `json:"score"`
MatchedField string `json:"matched_field"`
Recency string `json:"recency,omitempty"`
Pipeline *SearchPipeline `json:"pipeline,omitempty"`
Version *SearchVersion `json:"version,omitempty"`
Run *SearchRun `json:"run,omitempty"`
Step *SearchStep `json:"step,omitempty"`
Field *SearchField `json:"field,omitempty"`
Artifact *SearchArtifact `json:"artifact,omitempty"`
}
// SearchPipeline is a pipeline name/identity hit: its latest version and stage count.
type SearchPipeline struct {
ID int64 `json:"id"`
Name string `json:"name"`
Version int `json:"version"`
Stages int `json:"stages"`
}
// SearchVersion is a pipeline version/source hit.
type SearchVersion struct {
PipelineID int64 `json:"pipeline_id"`
Version int `json:"version"`
ContentHash string `json:"content_hash"`
CreatedAt string `json:"created_at"`
}
// SearchRun is a run identity/metadata hit. FinishedAt and DurationSeconds are
// omitted for a run without a recorded finish.
type SearchRun struct {
ID int64 `json:"id"`
PipelineName string `json:"pipeline_name"`
Status string `json:"status"`
Workspace string `json:"workspace,omitempty"`
CreatedAt string `json:"created_at"`
FinishedAt string `json:"finished_at,omitempty"`
DurationSeconds float64 `json:"duration_seconds,omitempty"`
}
// SearchStep is a recorded step/visit hit.
type SearchStep struct {
RunID int64 `json:"run_id"`
Sequence int `json:"sequence"`
Stage string `json:"stage"`
Visit int `json:"visit,omitempty"`
Outcome string `json:"outcome,omitempty"`
Target string `json:"target,omitempty"`
Diagnostic string `json:"diagnostic,omitempty"`
}
// SearchField is a stage input/output/evidence/transcript text hit with a
// snippet around the first match.
type SearchField struct {
RunID int64 `json:"run_id"`
Sequence int `json:"sequence"`
Field string `json:"field"`
Snippet string `json:"snippet"`
}
// SearchArtifact is a recorded artifact hit; contents are never read, only the
// recorded metadata.
type SearchArtifact struct {
RunID int64 `json:"run_id"`
Sequence int `json:"sequence"`
Name string `json:"name"`
ContentType string `json:"content_type"`
SizeBytes int64 `json:"size_bytes"`
URL string `json:"url"`
}
// Label is the compact human description of a hit, built from the result's
// typed payload rather than its target token, so it is the single definition
// shared by the CLI and the TUI. A text-field hit's snippet is deliberately not
// part of the label: the CLI appends it and the TUI renders it on its own
// marked line, so the two surfaces show the match differently without
// duplicating the per-kind dispatch.
func (r SearchResult) Label() string {
switch r.Kind {
case SearchKindPipeline:
if r.Pipeline != nil {
return fmt.Sprintf("%s · v%d · %d stages", r.Pipeline.Name, r.Pipeline.Version, r.Pipeline.Stages)
}
case SearchKindVersion:
if r.Version != nil {
return fmt.Sprintf("pipeline %d · v%d · %s", r.Version.PipelineID, r.Version.Version, r.Version.ContentHash)
}
case SearchKindRun:
if r.Run != nil {
return fmt.Sprintf("run %d · %s · %s", r.Run.ID, r.Run.PipelineName, r.Run.Status)
}
case SearchKindStep:
if r.Step != nil {
return fmt.Sprintf("run %d · step %d · %s · %s", r.Step.RunID, r.Step.Sequence, r.Step.Stage, r.Step.Outcome)
}
case SearchKindField:
if r.Field != nil {
return fmt.Sprintf("run %d · step %d · %s", r.Field.RunID, r.Field.Sequence, r.Field.Field)
}
case SearchKindArtifact:
if r.Artifact != nil {
return fmt.Sprintf("run %d · step %d · %s · %s", r.Artifact.RunID, r.Artifact.Sequence, r.Artifact.Name, r.Artifact.ContentType)
}
}
return r.Target
}
// RuneIndexFold returns the rune index where query first matches text
// case-insensitively, or -1 when it does not. It folds runes rather than bytes
// so a caller never splits a UTF-8 boundary, and an empty query never matches.
// It is the single definition shared by server-side snippet windowing and the
// TUI's marker placement.
func RuneIndexFold(text, query []rune) int {
if len(query) == 0 || len(query) > len(text) {
return -1
}
for i := 0; i+len(query) <= len(text); i++ {
match := true
for j := range query {
if unicode.ToLower(text[i+j]) != unicode.ToLower(query[j]) {
match = false
break
}
}
if match {
return i
}
}
return -1
}
+125
View File
@@ -0,0 +1,125 @@
package protocol
import (
"encoding/json"
"strings"
"testing"
)
func TestSearchRequestResponseRoundTrip(t *testing.T) {
request := Message{Type: Search, ID: "req-1", Search: &SearchRequest{Query: "release", Kind: SearchKindRun, Limit: 5}}
encoded, err := json.Marshal(request)
if err != nil {
t.Fatal(err)
}
var decoded Message
if err := json.Unmarshal(encoded, &decoded); err != nil {
t.Fatal(err)
}
if decoded.Type != Search || decoded.ID != "req-1" || decoded.Search == nil {
t.Fatalf("request envelope lost: %+v", decoded)
}
if decoded.Search.Query != "release" || decoded.Search.Kind != SearchKindRun || decoded.Search.Limit != 5 {
t.Fatalf("search request lost: %+v", decoded.Search)
}
response := Message{Type: SearchResults, ID: "req-1", SearchResults: &SearchResponse{
Query: "release",
Results: []SearchResult{{Kind: SearchKindPipeline, Target: "pipeline:1", Score: 3, MatchedField: "name", Pipeline: &SearchPipeline{ID: 1, Name: "release", Version: 2, Stages: 3}}},
Cap: 50,
ScanBoundBytes: SearchMaxScanBytes,
}}
encoded, err = json.Marshal(response)
if err != nil {
t.Fatal(err)
}
var echoed Message
if err := json.Unmarshal(encoded, &echoed); err != nil {
t.Fatal(err)
}
if echoed.ID != "req-1" || echoed.Type != SearchResults || echoed.SearchResults == nil {
t.Fatalf("response envelope or ID echo lost: %+v", echoed)
}
got := echoed.SearchResults
if got.Query != "release" || got.Cap != 50 || got.ScanBoundBytes != SearchMaxScanBytes || len(got.Results) != 1 {
t.Fatalf("search response lost: %+v", got)
}
hit := got.Results[0]
if hit.Pipeline == nil || hit.Pipeline.Name != "release" || hit.Pipeline.Version != 2 || hit.Pipeline.Stages != 3 || hit.Score != 3 || hit.MatchedField != "name" || hit.Target != "pipeline:1" {
t.Fatalf("pipeline payload lost: %+v", hit)
}
}
// TestRuneIndexFold pins the shared case-insensitive matcher used by both the
// server's snippet window and the TUI's marker, so it cannot drift per package.
func TestRuneIndexFold(t *testing.T) {
cases := []struct {
text, query string
want int
}{
{text: "prefix café résumé", query: "CAFÉ", want: 7},
{text: "Needle in a haystack", query: "needle", want: 0},
{text: "absent", query: "xyz", want: -1},
{text: "abc", query: "", want: -1},
{text: "ab", query: "abc", want: -1},
}
for _, tc := range cases {
if got := RuneIndexFold([]rune(tc.text), []rune(tc.query)); got != tc.want {
t.Fatalf("RuneIndexFold(%q, %q) = %d, want %d", tc.text, tc.query, got, tc.want)
}
}
}
// TestSearchLabelPerKind pins the single per-kind label shared by the CLI and
// TUI, so the six-kind dispatch lives in one place.
func TestSearchLabelPerKind(t *testing.T) {
cases := []struct {
result SearchResult
want string
}{
{SearchResult{Kind: SearchKindPipeline, Target: "pipeline:1", Pipeline: &SearchPipeline{Name: "demo", Version: 2, Stages: 3}}, "demo · v2 · 3 stages"},
{SearchResult{Kind: SearchKindVersion, Target: "pipeline:1:v2", Version: &SearchVersion{PipelineID: 1, Version: 2, ContentHash: "abc"}}, "pipeline 1 · v2 · abc"},
{SearchResult{Kind: SearchKindRun, Target: "run:7", Run: &SearchRun{ID: 7, PipelineName: "demo", Status: "succeeded"}}, "run 7 · demo · succeeded"},
{SearchResult{Kind: SearchKindStep, Target: "run:7:2", Step: &SearchStep{RunID: 7, Sequence: 2, Stage: "reviewer", Outcome: "success"}}, "run 7 · step 2 · reviewer · success"},
{SearchResult{Kind: SearchKindField, Target: "run:7:2:output", Field: &SearchField{RunID: 7, Sequence: 2, Field: "output", Snippet: "[needle] output"}}, "run 7 · step 2 · output"},
{SearchResult{Kind: SearchKindArtifact, Target: "/artifacts/7/2/shot.png", Artifact: &SearchArtifact{RunID: 7, Sequence: 2, Name: "shot.png", ContentType: "image/png"}}, "run 7 · step 2 · shot.png · image/png"},
// A result missing its typed payload falls back to the navigable target.
{SearchResult{Kind: SearchKindRun, Target: "run:9"}, "run:9"},
}
for _, tc := range cases {
if got := tc.result.Label(); got != tc.want {
t.Fatalf("Label(%s %s) = %q, want %q", tc.result.Kind, tc.result.Target, got, tc.want)
}
}
}
func TestSearchResultOmitsAbsentPayloadsAndEmptyKind(t *testing.T) {
encoded, err := json.Marshal(SearchResult{Kind: SearchKindRun, Target: "run:1", Score: 1, MatchedField: "status"})
if err != nil {
t.Fatal(err)
}
for _, key := range []string{"pipeline", "version", "run", "step", "field", "artifact", "recency"} {
if strings.Contains(string(encoded), `"`+key+`":`) {
t.Fatalf("absent %s payload must be omitted: %s", key, encoded)
}
}
var decoded SearchResult
if err := json.Unmarshal([]byte(`{"kind":"run","target":"run:1","score":1,"matched_field":"status"}`), &decoded); err != nil {
t.Fatal(err)
}
if decoded.Pipeline != nil || decoded.Version != nil || decoded.Run != nil || decoded.Step != nil || decoded.Field != nil || decoded.Artifact != nil {
t.Fatalf("absent payloads must decode nil: %+v", decoded)
}
response := Message{Type: SearchResults, ID: "x", SearchResults: &SearchResponse{Query: "q", Results: []SearchResult{}}}
encoded, err = json.Marshal(response)
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(encoded), `"kind"`) {
t.Fatalf("empty response kind must be omitted: %s", encoded)
}
if !strings.Contains(string(encoded), `"results":[]`) {
t.Fatalf("results must always be present: %s", encoded)
}
}
+259 -31
View File
@@ -7,7 +7,7 @@ import (
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
"github.com/sahilm/fuzzy" "github.com/charmbracelet/x/ansi"
"git.a-wan.net/alan/pipelines/internal/draft" "git.a-wan.net/alan/pipelines/internal/draft"
"git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/pipeline"
@@ -19,29 +19,57 @@ func selectionWindow(count, index, capacity int) (int, int) {
return start, min(count, start+capacity) return start, min(count, start+capacity)
} }
func (m Model) searchMatches() []protocol.PipelineSummary { // searchNav remembers the step and section to focus once an asynchronously
query := strings.TrimPrefix(strings.TrimSpace(m.commandInput.Value()), "/") // requested run arrives, so Enter on a step/artifact/field match lands exactly
if query == "" { // where it matched.
return m.pipelines type searchNav struct {
runID int64
sequence int
section int
artifact string
}
// requestSearch asks the server for the current query. At most one request is in
// flight: a keystroke while a request is outstanding is coalesced and the latest
// text is sent when the response arrives. Stale responses are dropped by ID, so
// no keystroke triggers overlapping or unbounded scans. The server owns parsing
// (status words, dates and the optional kind filter), so the client sends the
// raw text and needs no second matcher.
func (m Model) requestSearch() (tea.Model, tea.Cmd) {
query := strings.TrimSpace(m.commandInput.Value())
if m.conn == nil {
m.searchResults = nil
m.searchText = ""
return m, nil
} }
names := make([]string, len(m.pipelines)) if m.searchInFlight {
for i, p := range m.pipelines { m.searchQueued = true
names[i] = p.Name return m, nil
} }
result := []protocol.PipelineSummary{} m.nextID++
for _, match := range fuzzy.Find(query, names) { m.searchRequest = strconv.FormatUint(m.nextID, 10)
result = append(result, m.pipelines[match.Index]) m.searchInFlight = true
} m.searchError = ""
return result return m, m.send(protocol.Message{Type: protocol.Search, ID: m.searchRequest, Search: &protocol.SearchRequest{Query: query}})
}
func (m *Model) closeSearch() {
m.paletteOpen = false
m.searchOpen = false
m.keys.palette = false
m.searchResults = nil
m.searchText = ""
m.searchRequest = ""
m.searchInFlight = false
m.searchQueued = false
m.searchError = ""
} }
func (m Model) updateSearch(msg tea.KeyMsg) (tea.Model, tea.Cmd) { func (m Model) updateSearch(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
items := m.searchMatches() items := m.searchResults
switch msg.String() { switch msg.String() {
case "esc": case "esc":
m.paletteOpen = false m.closeSearch()
m.searchOpen = false
m.keys.palette = false
return m, nil return m, nil
case "down", "ctrl+n": case "down", "ctrl+n":
m.commandIndex = min(m.commandIndex+1, max(0, len(items)-1)) m.commandIndex = min(m.commandIndex+1, max(0, len(items)-1))
@@ -53,35 +81,235 @@ func (m Model) updateSearch(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
if len(items) == 0 { if len(items) == 0 {
return m, nil return m, nil
} }
selected := items[min(m.commandIndex, len(items)-1)] return m.openSearchResult(items[min(m.commandIndex, len(items)-1)])
m.paletteOpen = false
m.searchOpen = false
m.keys.palette = false
return m.requestPipeline(selected.ID, 0)
} }
var cmd tea.Cmd var cmd tea.Cmd
m.commandInput, cmd = m.commandInput.Update(msg) m.commandInput, cmd = m.commandInput.Update(msg)
m.commandIndex = 0 m.commandIndex = 0
return m, cmd updated, send := m.requestSearch()
return updated, tea.Batch(cmd, send)
}
// openSearchResult navigates straight to a result: a pipeline/version opens its
// detail, and a run/step/artifact/field match opens the run with that step and
// section selected once the run arrives.
func (m Model) openSearchResult(result protocol.SearchResult) (tea.Model, tea.Cmd) {
m.closeSearch()
switch result.Kind {
case protocol.SearchKindPipeline:
if result.Pipeline != nil {
return m.requestPipeline(result.Pipeline.ID, result.Pipeline.Version)
}
case protocol.SearchKindVersion:
if result.Version != nil {
return m.requestPipeline(result.Version.PipelineID, result.Version.Version)
}
}
runID, sequence, field, artifact := searchResultTarget(result)
if runID <= 0 {
return m, nil
}
m.closeRuns()
m.runOpen = true
m.keys.runs = true
m.runIndex = 0
nav := &searchNav{runID: runID, sequence: sequence, section: runSectionNone}
switch result.Kind {
case protocol.SearchKindStep:
nav.section = runSectionProgress
case protocol.SearchKindField:
nav.section = sectionForField(field)
case protocol.SearchKindArtifact:
// Artifacts have their own grouped view; open it with the matched entry
// selected so the operator can open or copy its URL.
nav.section = runSectionNone
nav.artifact = artifact
}
m.pendingRunNav = nav
return m.runRequest(protocol.Message{Type: protocol.GetRun, RunID: runID})
}
// searchResultTarget returns the run, step, field and artifact a result points
// at, read from its typed payload rather than re-parsing the target token.
func searchResultTarget(result protocol.SearchResult) (runID int64, sequence int, field, artifact string) {
switch result.Kind {
case protocol.SearchKindRun:
if result.Run != nil {
runID = result.Run.ID
}
case protocol.SearchKindStep:
if result.Step != nil {
runID, sequence = result.Step.RunID, result.Step.Sequence
}
case protocol.SearchKindField:
if result.Field != nil {
runID, sequence, field = result.Field.RunID, result.Field.Sequence, result.Field.Field
}
case protocol.SearchKindArtifact:
if result.Artifact != nil {
runID, sequence, artifact = result.Artifact.RunID, result.Artifact.Sequence, result.Artifact.Name
}
}
return runID, sequence, field, artifact
}
func sectionForField(field string) int {
switch field {
case "input":
return runSectionInput
case "output":
return runSectionResult
case "evidence", "transcript":
return runSectionEvidence
default:
return runSectionDetails
}
}
// applyRunNav selects the matched step and scrolls to the matched section.
func (m *Model) applyRunNav(nav searchNav) {
if m.runDetail == nil {
return
}
if nav.sequence > 0 {
for i, step := range m.runDetail.Steps {
if step.Sequence == nav.sequence {
m.selectRunStep(i + 1)
break
}
}
}
if nav.section != runSectionNone {
m.runGraphFocus = false
m.runDetailFocus = true
m.focusRunSection(nav.section)
}
if nav.section == runSectionNone && nav.artifact != "" {
m.openArtifacts()
m.selectArtifactByName(nav.artifact)
}
}
// focusRunSection opens the section and scrolls the run detail to its header,
// reusing the same shortcut markers runSectionLines renders.
func (m *Model) focusRunSection(section int) {
if m.runDetail == nil || section < 0 || section >= runSectionCount {
return
}
if m.runCollapsed == nil {
m.runCollapsed = map[string]bool{}
}
m.runCollapsed[m.runSectionKey(section)] = false
marker := "[" + runSections[section].Shortcut + "]"
for i, line := range m.runSectionLines(m.runDetailWidth()) {
if strings.HasPrefix(ansi.Strip(line), marker) {
m.runTop = i
return
}
}
}
// searchBadge is the short type label a result shows. A pipeline version folds
// into the pipeline badge and a body-text field into the match badge, so the
// row reads the way the operator thinks about it rather than the wire kind.
func searchBadge(kind string) string {
switch kind {
case protocol.SearchKindVersion:
return "pipeline"
case protocol.SearchKindField:
return "match"
default:
return kind
}
}
// markedSnippet brackets the first case-insensitive occurrence of query in a
// field snippet so the matched part is distinguishable without relying on
// colour, then clips the result to budget runes with the bracketed match kept
// inside the window. It tracks the exact marker span it inserts, so a snippet
// that itself contains '[' cannot make the window centre on the wrong bracket.
// The server centres the snippet on the match, so the text is present
// case-insensitively; the snippet is shown unchanged when it is not. The budget
// excludes the row's two-rune indent, since the result is clipped before the
// row is styled.
func markedSnippet(snippet, query string, budget int) string {
runes := []rune(snippet)
needle := []rune(strings.TrimSpace(query))
if len(needle) == 0 || len(runes) == 0 {
return snippet
}
at := protocol.RuneIndexFold(runes, needle)
if at < 0 {
return snippet
}
marked := string(runes[:at]) + "[" + string(runes[at:at+len(needle)]) + "]" + string(runes[at+len(needle):])
markedRunes := []rune(marked)
if budget <= 0 || len(markedRunes) <= budget {
return marked
}
// The inserted '[' sits at rune `at` and the ']' at at+len(needle)+1, so the
// span to keep in view is [at, at+len(needle)+2).
spanStart, spanEnd := at, at+len(needle)+2
if spanEnd-spanStart >= budget {
return string(markedRunes[:budget])
}
lo := max(0, spanStart-(budget-(spanEnd-spanStart))/2)
hi := min(len(markedRunes), lo+budget)
lo = max(0, hi-budget)
out := string(markedRunes[lo:hi])
if lo > 0 {
out = "…" + out
}
if hi < len(markedRunes) {
out += "…"
}
return out
}
// searchRowLines renders one result. A body-text match keeps its (windowed)
// snippet on its own indented line so the marker is visible at the popup width
// instead of being clipped after a long badge and label.
func searchRowLines(result protocol.SearchResult, query string, snippetBudget int) []string {
head := "[" + searchBadge(result.Kind) + "] " + result.Label()
if result.Kind == protocol.SearchKindField && result.Field != nil && result.Field.Snippet != "" {
return []string{head, " " + markedSnippet(result.Field.Snippet, query, snippetBudget)}
}
return []string{head}
} }
func (m Model) searchView(height int) string { func (m Model) searchView(height int) string {
field := m.commandInput field := m.commandInput
field.Width = max(1, min(54, m.width-10)) field.Width = max(1, min(64, m.width-10))
lines := []string{"Search pipelines", field.View(), ""} inner := min(72, max(1, m.width-6))
items := m.searchMatches() snippetBudget := max(8, inner-4)
if len(items) == 0 { lines := []string{"Search", field.View(), ""}
lines = append(lines, "No matching pipelines") items := m.searchResults
query := strings.TrimSpace(m.commandInput.Value())
switch {
case m.searchError != "":
lines = append(lines, "Search failed: "+m.searchError)
case len(items) == 0 && m.searchInFlight:
lines = append(lines, "Searching…")
case len(items) == 0 && query != "":
lines = append(lines, "No matches for "+query)
case len(items) == 0:
lines = append(lines, "Type to search pipelines, runs, steps, text and artifacts.")
} }
start, end := selectionWindow(len(items), m.commandIndex, max(1, height-5)) // A field match renders two lines, so budget the window by rendered lines.
start, end := selectionWindow(len(items), m.commandIndex, max(1, (height-4)/2))
for i := start; i < end; i++ { for i := start; i < end; i++ {
prefix := " " prefix := " "
if i == m.commandIndex { if i == m.commandIndex {
prefix = "› " prefix = "› "
} }
lines = append(lines, menuOption(fmt.Sprintf("%s%s · v%d · %d stages", prefix, items[i].Name, items[i].Version, items[i].Stages), i == m.commandIndex)) for j, row := range searchRowLines(items[i], m.searchText, snippetBudget) {
if j == 0 {
row = prefix + row
}
lines = append(lines, menuOption(row, i == m.commandIndex))
}
} }
return popup(m.width, height, 64, lines) return popup(m.width, height, 72, lines)
} }
func (m Model) requestPipeline(id int64, version int) (tea.Model, tea.Cmd) { func (m Model) requestPipeline(id int64, version int) (tea.Model, tea.Cmd) {
+149
View File
@@ -0,0 +1,149 @@
package tui
import (
"context"
"os"
"strconv"
"testing"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/ansi"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// TestLiveOmniSearchAgainstServer is an env-gated check against a running
// server: it drives the TUI's own '/' search, renders the popup, and presses
// Enter on a live result of each kind, reporting where navigation lands. It
// skips unless PIPELINES_LIVE_SEARCH is set so CI never needs a server.
func TestLiveOmniSearchAgainstServer(t *testing.T) {
if os.Getenv("PIPELINES_LIVE_SEARCH") == "" {
t.Skip("set PIPELINES_LIVE_SEARCH=1 to query the running server")
}
url := os.Getenv("PIPELINES_URL")
if url == "" {
url = "ws://127.0.0.1:8080/connect"
}
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
m := New(ctx, url)
m.width, m.height = 120, 40
connected := m.connect()().(connectedMsg)
defer connected.conn.CloseNow()
updated, _ := m.Update(connected)
m = updated.(Model)
// Plain-text (never a type prefix) candidates per kind. The live store keeps
// changing as runs land, so a fixed query can stop matching a kind; the test
// uses the first candidate that still returns the kind it is checking.
candidates := map[string][]string{
protocol.SearchKindPipeline: {"demo", "pipeline", "test", "a"},
protocol.SearchKindVersion: {"1", "demo", "review", "test"},
protocol.SearchKindRun: {"succeeded", "failed", "running", "1"},
protocol.SearchKindStep: {"implementer", "reviewer", "planner", "success"},
protocol.SearchKindField: {"identity", "search", "error", "the"},
protocol.SearchKindArtifact: {"identity", "png", "log", "txt"},
}
for _, kind := range protocol.SearchKinds {
picked, query := -1, ""
for _, candidate := range candidates[kind] {
m = resetLiveSearch(m)
m = liveType(m, tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'/'}})
m = liveType(m, tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(candidate)})
m = drainLiveSearch(t, m)
query = candidate
picked = -1
for i, result := range m.searchResults {
if result.Kind == kind {
picked = i
break
}
}
if picked >= 0 {
break
}
}
if picked < 0 {
t.Errorf("%s: no candidate query returned a %s result", kind, kind)
continue
}
m.commandIndex = picked
view := ansi.Strip(m.searchView(20))
t.Logf("%s: query %q rendered\n%s", kind, query, view)
updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyEnter})
m = updated.(Model)
if where := liveNavigation(m); where == "" {
t.Errorf("%s: Enter did not navigate (result %+v)", kind, m.searchResults[picked])
} else {
t.Logf("%s: Enter navigates to %s", kind, where)
}
}
}
// liveType delivers one key and runs any command it produced (a text-input
// update or a search request written to the socket), mirroring the real
// bubbletea loop.
func liveType(m Model, msg tea.Msg) Model {
updated, cmd := m.Update(msg)
m = updated.(Model)
if cmd == nil {
return m
}
if batch, ok := cmd().(tea.BatchMsg); ok {
for _, c := range batch {
if c != nil {
c()
}
}
}
return m
}
// drainLiveSearch reads server frames until the outstanding search request is
// answered, so a slow or interleaved reply is still observed.
func drainLiveSearch(t *testing.T, m Model) Model {
t.Helper()
for i := 0; i < 100; i++ {
if !m.searchInFlight {
return m
}
updated, _ := m.Update(m.read()())
m = updated.(Model)
}
t.Fatalf("timed out waiting for the search reply")
return m
}
// resetLiveSearch returns the model to the home screen with no overlay, ready
// for the next '/' query.
func resetLiveSearch(m Model) Model {
m.closeSearch()
m.closeRuns()
m.keys.detail = false
m.detail = nil
m.detailLoading = false
m.requestedPipelineID = 0
m.requestedVersion = 0
m.artifactsOpen = false
return m
}
// liveNavigation describes where the last Enter landed, read from the same
// state the views use.
func liveNavigation(m Model) string {
switch {
case m.detailLoading:
return "pipeline detail #" + strconv.FormatInt(m.requestedPipelineID, 10)
case m.runOpen && m.pendingRunNav != nil && m.pendingRunNav.artifact != "":
return "run #" + strconv.FormatInt(m.pendingRunNav.runID, 10) + " artifacts (" + m.pendingRunNav.artifact + ")"
case m.runOpen && m.pendingRunNav != nil && m.pendingRunNav.section >= 0:
return "run #" + strconv.FormatInt(m.pendingRunNav.runID, 10) + " section " + runSections[m.pendingRunNav.section].Title
case m.runOpen && m.pendingRunNav != nil:
return "run #" + strconv.FormatInt(m.pendingRunNav.runID, 10) + " detail"
case m.artifactsOpen:
return "artifacts view"
}
return ""
}
+236
View File
@@ -0,0 +1,236 @@
package tui
import (
"context"
"strings"
"testing"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/ansi"
"github.com/coder/websocket"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// searchFixtureModel renders the omni popup over a fixed, mixed result set so
// the badges, labels and marked snippet are asserted without a server.
func searchFixtureModel() Model {
m := New(context.Background(), "ws://localhost/connect")
m.width, m.height = 100, 30
m.conn = &websocket.Conn{}
m.paletteOpen = true
m.searchOpen = true
m.keys.palette = true
m.searchText = "needle"
m.searchResults = []protocol.SearchResult{
{Kind: protocol.SearchKindPipeline, Target: "pipeline:1", Pipeline: &protocol.SearchPipeline{ID: 1, Name: "demo", Version: 2, Stages: 3}},
{Kind: protocol.SearchKindRun, Target: "run:7", Run: &protocol.SearchRun{ID: 7, PipelineName: "demo", Status: "succeeded"}},
{Kind: protocol.SearchKindStep, Target: "run:7:1", Step: &protocol.SearchStep{RunID: 7, Sequence: 1, Stage: "reviewer"}},
{Kind: protocol.SearchKindArtifact, Target: "/artifacts/7/1/shot.png", Artifact: &protocol.SearchArtifact{RunID: 7, Sequence: 1, Name: "shot.png", URL: "/artifacts/7/1/shot.png"}},
{Kind: protocol.SearchKindField, Target: "run:7:1:output", Field: &protocol.SearchField{RunID: 7, Sequence: 1, Field: "output", Snippet: "…needle output…"}},
}
return m
}
func TestOmniSearchShowsBadgesAndMarkedSnippet(t *testing.T) {
m := searchFixtureModel()
view := ansi.Strip(m.searchView(20))
for _, badge := range []string{"[pipeline]", "[run]", "[step]", "[artifact]", "[match]"} {
if !strings.Contains(view, badge) {
t.Fatalf("missing badge %q in %q", badge, view)
}
}
if !strings.Contains(view, "[needle]") {
t.Fatalf("field snippet match must be marked without colour: %q", view)
}
}
// TestOmniSearchRealSnippetMarkerVisible guards the regression where a body
// match's snippet centres the token ~60 runes in, so a single-line
// "[match] label · snippet" row was clipped and the marker was invisible at
// every realistic width. The snippet is rendered on its own windowed line.
func TestOmniSearchRealSnippetMarkerVisible(t *testing.T) {
body := strings.Repeat("backgroundcontext ", 6) + "needle output"
for _, width := range []int{80, 100} {
m := searchFixtureModel()
m.width = width
m.searchResults[4].Field.Snippet = body
view := ansi.Strip(m.View())
if !strings.Contains(view, "[needle]") {
t.Fatalf("width %d: matched span not visible after ansi.Strip: %q", width, view)
}
}
}
// TestOmniSearchSnippetWithOwnBracket guards the regression where the window
// located the marker by the first '[' in the marked snippet. A snippet that
// itself contains a '[' before the match (common in code/log evidence) centred
// the window on the wrong bracket and clipped the real "[match]" span, so the
// match became indistinguishable without colour.
func TestOmniSearchSnippetWithOwnBracket(t *testing.T) {
m := searchFixtureModel()
m.width = 100
m.searchResults[4].Field.Snippet = "[x] " + strings.Repeat("word ", 30) + "needle"
view := ansi.Strip(m.View())
if !strings.Contains(view, "[needle]") {
t.Fatalf("real bracket pair must survive a snippet that contains '[': %q", view)
}
}
func TestOmniSearchEmptyStates(t *testing.T) {
t.Run("empty query", func(t *testing.T) {
m := searchFixtureModel()
m.searchResults = nil
if view := ansi.Strip(m.searchView(20)); !strings.Contains(view, "Type to search") {
t.Fatalf("empty query must prompt: %q", view)
}
})
t.Run("no matches", func(t *testing.T) {
m := searchFixtureModel()
m.searchResults = nil
m.commandInput.SetValue("zzz")
view := ansi.Strip(m.searchView(20))
if !strings.Contains(view, "No matches for zzz") || strings.Contains(view, "Type to search") {
t.Fatalf("non-empty query with no results must say so: %q", view)
}
})
t.Run("error", func(t *testing.T) {
m := searchFixtureModel()
m.searchResults = nil
m.commandInput.SetValue("zzz")
m.searchError = "boom"
view := ansi.Strip(m.searchView(20))
if !strings.Contains(view, "Search failed: boom") {
t.Fatalf("failed search must be distinct from no matches: %q", view)
}
})
t.Run("in flight", func(t *testing.T) {
m := searchFixtureModel()
m.searchResults = nil
m.commandInput.SetValue("zz")
m.searchInFlight = true
if view := ansi.Strip(m.searchView(20)); !strings.Contains(view, "Searching…") {
t.Fatalf("outstanding request must show progress: %q", view)
}
})
}
func TestOmniSearchEscCancels(t *testing.T) {
m := searchFixtureModel()
updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEsc})
m = updated.(Model)
if m.searchOpen || m.paletteOpen || m.searchResults != nil {
t.Fatal("Esc must cancel and clear the omni popup")
}
}
func TestOmniSearchDropsStaleResponses(t *testing.T) {
m := searchFixtureModel()
m.searchResults = nil
m.searchRequest = "2"
updated, _ := m.Update(receivedMsg{Type: protocol.SearchResults, ID: "1", SearchResults: &protocol.SearchResponse{Results: []protocol.SearchResult{{Kind: protocol.SearchKindRun}}}})
m = updated.(Model)
if len(m.searchResults) != 0 {
t.Fatal("stale search response replaced current results")
}
updated, _ = m.Update(receivedMsg{Type: protocol.SearchResults, ID: "2", SearchResults: &protocol.SearchResponse{Query: "demo", Results: []protocol.SearchResult{{Kind: protocol.SearchKindRun}}}})
m = updated.(Model)
if len(m.searchResults) != 1 || m.searchText != "demo" {
t.Fatal("matching search response was dropped or its query not retained")
}
}
func TestOmniSearchEnterNavigatesByKind(t *testing.T) {
detail := &protocol.RunDetail{
RunSummary: protocol.RunSummary{ID: 7, Name: "demo", Status: "succeeded"},
Steps: []protocol.RunStep{{Sequence: 1, Stage: "one"}, {Sequence: 2, Stage: "two"}},
}
cases := []struct {
result protocol.SearchResult
section int
}{
{protocol.SearchResult{Kind: protocol.SearchKindRun, Run: &protocol.SearchRun{ID: 7}}, -1},
{protocol.SearchResult{Kind: protocol.SearchKindStep, Step: &protocol.SearchStep{RunID: 7, Sequence: 2}}, 0},
{protocol.SearchResult{Kind: protocol.SearchKindField, Field: &protocol.SearchField{RunID: 7, Sequence: 2, Field: "input"}}, 1},
{protocol.SearchResult{Kind: protocol.SearchKindArtifact, Artifact: &protocol.SearchArtifact{RunID: 7, Sequence: 2, Name: "shot.png"}}, -2},
}
for _, tc := range cases {
m := searchFixtureModel()
m.searchResults = []protocol.SearchResult{tc.result}
updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
m = updated.(Model)
if !m.runOpen || m.searchOpen || m.pendingRunNav == nil {
t.Fatalf("%s: Enter did not open the run: %+v", tc.result.Kind, m)
}
updated, _ = m.Update(receivedMsg{Type: protocol.Run, ID: "run", RunDetail: detail})
m = updated.(Model)
if sequence := searchResultSequence(tc.result); sequence > 0 && m.runStepIndex != 2 {
t.Fatalf("%s: matched step not selected: %d", tc.result.Kind, m.runStepIndex)
}
if tc.result.Kind == protocol.SearchKindArtifact {
if !m.artifactsOpen {
t.Fatalf("artifact match must open the artifacts view: %+v", m)
}
continue
}
if tc.section >= 0 {
marker := "[" + runSections[tc.section].Shortcut + "]"
lines := m.runSectionLines(m.runDetailWidth())
if m.runTop >= len(lines) || !strings.HasPrefix(ansi.Strip(lines[m.runTop]), marker) {
t.Fatalf("%s: section %d not focused: top=%d", tc.result.Kind, tc.section, m.runTop)
}
}
}
}
func searchResultSequence(result protocol.SearchResult) int {
if result.Step != nil {
return result.Step.Sequence
}
if result.Field != nil {
return result.Field.Sequence
}
if result.Artifact != nil {
return result.Artifact.Sequence
}
return 0
}
func TestOmniSearchPipelineEnterOpensDetail(t *testing.T) {
m := searchFixtureModel()
m.searchResults = []protocol.SearchResult{{Kind: protocol.SearchKindPipeline, Pipeline: &protocol.SearchPipeline{ID: 3, Version: 4}}}
updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
m = updated.(Model)
if m.searchOpen || !m.detailLoading || m.requestedPipelineID != 3 || m.requestedVersion != 4 {
t.Fatalf("pipeline Enter did not request the detail: %+v", m)
}
}
func TestOmniSearchCoalescesInFlightRequests(t *testing.T) {
m := searchFixtureModel()
updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'d'}})
m = updated.(Model)
if !m.searchInFlight {
t.Fatal("first keystroke must start a search")
}
before := m.nextID
updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'e'}})
m = updated.(Model)
if m.searchInFlight == false || !m.searchQueued || m.nextID != before {
t.Fatalf("second keystroke must coalesce instead of overlapping: inFlight=%t queued=%t", m.searchInFlight, m.searchQueued)
}
}
func TestOmniSearchLayoutStaysWithinWidth(t *testing.T) {
for _, width := range []int{100, 40, 12} {
m := searchFixtureModel()
m.width = width
m.searchResults[4].Field.Snippet = strings.Repeat("wide ", 40) + "needle"
lines := strings.Split(ansi.Strip(m.View()), "\n")
for _, line := range lines {
if len([]rune(line)) > width {
t.Fatalf("width %d: line too wide: %q", width, line)
}
}
}
}