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:
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user