search: browse bounded recent items with kind-only queries
This commit is contained in:
+151
-39
@@ -29,13 +29,17 @@ func (s *Store) Search(ctx context.Context, req protocol.SearchRequest, now time
|
|||||||
Cap: searchLimit(req.Limit),
|
Cap: searchLimit(req.Limit),
|
||||||
ScanBoundBytes: protocol.SearchMaxScanBytes,
|
ScanBoundBytes: protocol.SearchMaxScanBytes,
|
||||||
}
|
}
|
||||||
// An empty query is a well-defined no-op, never a match-everything scan.
|
// Empty text without a kind remains a no-op. A validated kind explicitly
|
||||||
if strings.TrimSpace(req.Query) == "" {
|
// requests bounded recent canonical rows, never a wildcard text search.
|
||||||
|
browse := strings.TrimSpace(req.Query) == ""
|
||||||
|
if browse && kind == "" {
|
||||||
return response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
terms := parseSearchTerms(req.Query, now)
|
terms := parseSearchTerms(req.Query, now)
|
||||||
|
terms.Browse = browse
|
||||||
|
terms.Limit = response.Cap
|
||||||
// A bare status word or date selects runs; it is never treated as body text.
|
// A bare status word or date selects runs; it is never treated as body text.
|
||||||
selectorOnly := terms.Text == ""
|
selectorOnly := terms.Text == "" && !browse
|
||||||
|
|
||||||
tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -82,7 +86,14 @@ func (s *Store) Search(ctx context.Context, req protocol.SearchRequest, now time
|
|||||||
response.Truncated = true
|
response.Truncated = true
|
||||||
}
|
}
|
||||||
|
|
||||||
sortSearchResults(results)
|
if browse {
|
||||||
|
for i := range results {
|
||||||
|
results[i].Score = 0
|
||||||
|
}
|
||||||
|
sortRecentSearchResults(results)
|
||||||
|
} else {
|
||||||
|
sortSearchResults(results)
|
||||||
|
}
|
||||||
if len(results) > response.Cap {
|
if len(results) > response.Cap {
|
||||||
results = results[:response.Cap]
|
results = results[:response.Cap]
|
||||||
response.Truncated = true
|
response.Truncated = true
|
||||||
@@ -118,6 +129,8 @@ func searchLimit(limit int) int {
|
|||||||
// searchTerms is the parsed query. Text carries free text; Status and the
|
// 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.
|
// From/To window carry a bare status word or a date expression that selects runs.
|
||||||
type searchTerms struct {
|
type searchTerms struct {
|
||||||
|
Browse bool
|
||||||
|
Limit int
|
||||||
Text string
|
Text string
|
||||||
Status string
|
Status string
|
||||||
HasDate bool
|
HasDate bool
|
||||||
@@ -174,6 +187,13 @@ const (
|
|||||||
// scale without limit. Field text uses the separate SearchMaxScanSteps budget.
|
// scale without limit. Field text uses the separate SearchMaxScanSteps budget.
|
||||||
const searchRowBudget = 2000
|
const searchRowBudget = 2000
|
||||||
|
|
||||||
|
func searchCandidateLimit(terms searchTerms) int {
|
||||||
|
if terms.Browse {
|
||||||
|
return searchLimit(terms.Limit) + 1
|
||||||
|
}
|
||||||
|
return searchRowBudget + 1
|
||||||
|
}
|
||||||
|
|
||||||
// searchLike builds a case-insensitive substring pattern, escaping LIKE
|
// searchLike builds a case-insensitive substring pattern, escaping LIKE
|
||||||
// metacharacters so a query can never broaden the pattern.
|
// metacharacters so a query can never broaden the pattern.
|
||||||
func searchLike(text string) string {
|
func searchLike(text string) string {
|
||||||
@@ -216,15 +236,59 @@ func sortSearchResults(results []protocol.SearchResult) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Browsing has no relevance score. Numeric entity IDs, then visit/version IDs,
|
||||||
|
// break equal timestamps deterministically (not lexical "run:9" vs "run:10").
|
||||||
|
func sortRecentSearchResults(results []protocol.SearchResult) {
|
||||||
|
identity := func(hit protocol.SearchResult) (int64, int) {
|
||||||
|
switch {
|
||||||
|
case hit.Pipeline != nil:
|
||||||
|
return hit.Pipeline.ID, hit.Pipeline.Version
|
||||||
|
case hit.Version != nil:
|
||||||
|
return hit.Version.PipelineID, hit.Version.Version
|
||||||
|
case hit.Run != nil:
|
||||||
|
return hit.Run.ID, 0
|
||||||
|
case hit.Step != nil:
|
||||||
|
return hit.Step.RunID, hit.Step.Sequence
|
||||||
|
case hit.Field != nil:
|
||||||
|
return hit.Field.RunID, hit.Field.Sequence
|
||||||
|
case hit.Artifact != nil:
|
||||||
|
return hit.Artifact.RunID, hit.Artifact.Sequence
|
||||||
|
}
|
||||||
|
return 0, 0
|
||||||
|
}
|
||||||
|
sort.SliceStable(results, func(i, j int) bool {
|
||||||
|
a, b := results[i], results[j]
|
||||||
|
if a.Recency != b.Recency {
|
||||||
|
return a.Recency > b.Recency
|
||||||
|
}
|
||||||
|
ai, as := identity(a)
|
||||||
|
bi, bs := identity(b)
|
||||||
|
if ai != bi {
|
||||||
|
return ai > bi
|
||||||
|
}
|
||||||
|
if as != bs {
|
||||||
|
return as > bs
|
||||||
|
}
|
||||||
|
return a.Target < b.Target
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func searchPipelines(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *searchScan) ([]protocol.SearchResult, error) {
|
func searchPipelines(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *searchScan) ([]protocol.SearchResult, error) {
|
||||||
if terms.Text == "" {
|
if terms.Text == "" && !terms.Browse {
|
||||||
return nil, nil
|
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),'')
|
query := `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
|
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)
|
WHERE v.version=(SELECT MAX(version) FROM pipeline_versions WHERE pipeline_id=p.id)`
|
||||||
AND lower(p.name) LIKE ? ESCAPE '\'
|
var args []any
|
||||||
ORDER BY p.name,p.id LIMIT ?`, searchLike(terms.Text), searchRowBudget+1)
|
if terms.Browse {
|
||||||
|
query += " ORDER BY v.created_at DESC,v.pipeline_id DESC,v.version DESC LIMIT ?"
|
||||||
|
} else {
|
||||||
|
query += ` AND lower(p.name) LIKE ? ESCAPE '\' ORDER BY p.name,p.id LIMIT ?`
|
||||||
|
args = append(args, searchLike(terms.Text))
|
||||||
|
}
|
||||||
|
args = append(args, searchCandidateLimit(terms))
|
||||||
|
rows, err := tx.QueryContext(ctx, query, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -244,6 +308,9 @@ ORDER BY p.name,p.id LIMIT ?`, searchLike(terms.Text), searchRowBudget+1)
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
score, ok := searchTier(name, terms.Text)
|
score, ok := searchTier(name, terms.Text)
|
||||||
|
if terms.Browse {
|
||||||
|
score, ok = 0, true
|
||||||
|
}
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -257,12 +324,20 @@ ORDER BY p.name,p.id LIMIT ?`, searchLike(terms.Text), searchRowBudget+1)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func searchVersions(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *searchScan) ([]protocol.SearchResult, error) {
|
func searchVersions(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *searchScan) ([]protocol.SearchResult, error) {
|
||||||
if terms.Text == "" {
|
if terms.Text == "" && !terms.Browse {
|
||||||
return nil, nil
|
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
|
source := "v.source"
|
||||||
FROM pipeline_versions v WHERE lower(v.source) LIKE ? ESCAPE '\'`
|
if terms.Browse {
|
||||||
args := []any{searchLike(terms.Text)}
|
source = "''" // Browsing needs metadata only, never retained source text.
|
||||||
|
}
|
||||||
|
query := `SELECT v.pipeline_id,v.version,v.content_hash,COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ',v.created_at),''),` + source + `
|
||||||
|
FROM pipeline_versions v`
|
||||||
|
var args []any
|
||||||
|
if !terms.Browse {
|
||||||
|
query += ` WHERE lower(v.source) LIKE ? ESCAPE '\'`
|
||||||
|
args = append(args, searchLike(terms.Text))
|
||||||
|
}
|
||||||
var wanted int64
|
var wanted int64
|
||||||
numeric := false
|
numeric := false
|
||||||
if n, err := parsePositiveInt(terms.Text); err == nil {
|
if n, err := parsePositiveInt(terms.Text); err == nil {
|
||||||
@@ -270,8 +345,12 @@ FROM pipeline_versions v WHERE lower(v.source) LIKE ? ESCAPE '\'`
|
|||||||
args = append(args, n)
|
args = append(args, n)
|
||||||
wanted, numeric = n, true
|
wanted, numeric = n, true
|
||||||
}
|
}
|
||||||
query += " ORDER BY v.pipeline_id,v.version DESC LIMIT ?"
|
if terms.Browse {
|
||||||
args = append(args, searchRowBudget+1)
|
query += " ORDER BY v.created_at DESC,v.pipeline_id DESC,v.version DESC LIMIT ?"
|
||||||
|
} else {
|
||||||
|
query += " ORDER BY v.pipeline_id,v.version DESC LIMIT ?"
|
||||||
|
}
|
||||||
|
args = append(args, searchCandidateLimit(terms))
|
||||||
rows, err := tx.QueryContext(ctx, query, args...)
|
rows, err := tx.QueryContext(ctx, query, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -292,7 +371,9 @@ FROM pipeline_versions v WHERE lower(v.source) LIKE ? ESCAPE '\'`
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
score, matched := 1, "source"
|
score, matched := 1, "source"
|
||||||
if numeric && int64(version) == wanted {
|
if terms.Browse {
|
||||||
|
score, matched = 0, ""
|
||||||
|
} else if numeric && int64(version) == wanted {
|
||||||
score, matched = 3, "version"
|
score, matched = 3, "version"
|
||||||
} else if !searchContains(source, terms.Text) {
|
} else if !searchContains(source, terms.Text) {
|
||||||
continue
|
continue
|
||||||
@@ -313,6 +394,8 @@ func searchRuns(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *search
|
|||||||
var query string
|
var query string
|
||||||
var args []any
|
var args []any
|
||||||
switch {
|
switch {
|
||||||
|
case terms.Browse:
|
||||||
|
query = base
|
||||||
case terms.Status != "":
|
case terms.Status != "":
|
||||||
query = base + " WHERE r.status=?"
|
query = base + " WHERE r.status=?"
|
||||||
args = append(args, terms.Status)
|
args = append(args, terms.Status)
|
||||||
@@ -324,8 +407,12 @@ func searchRuns(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *search
|
|||||||
pattern := searchLike(terms.Text)
|
pattern := searchLike(terms.Text)
|
||||||
args = append(args, pattern, pattern, pattern, pattern, terms.Text)
|
args = append(args, pattern, pattern, pattern, pattern, terms.Text)
|
||||||
}
|
}
|
||||||
query += " ORDER BY r.id DESC LIMIT ?"
|
if terms.Browse {
|
||||||
args = append(args, searchRowBudget+1)
|
query += " ORDER BY r.created_at DESC,r.id DESC LIMIT ?"
|
||||||
|
} else {
|
||||||
|
query += " ORDER BY r.id DESC LIMIT ?"
|
||||||
|
}
|
||||||
|
args = append(args, searchCandidateLimit(terms))
|
||||||
rows, err := tx.QueryContext(ctx, query, args...)
|
rows, err := tx.QueryContext(ctx, query, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -345,7 +432,9 @@ func searchRuns(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *search
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
score, matched := 1, "status"
|
score, matched := 1, "status"
|
||||||
if terms.Status != "" {
|
if terms.Browse {
|
||||||
|
score, matched = 0, ""
|
||||||
|
} else if terms.Status != "" {
|
||||||
matched = "status"
|
matched = "status"
|
||||||
} else if terms.HasDate {
|
} else if terms.HasDate {
|
||||||
matched = "created_at"
|
matched = "created_at"
|
||||||
@@ -377,14 +466,20 @@ func searchRuns(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *search
|
|||||||
}
|
}
|
||||||
|
|
||||||
func searchSteps(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *searchScan) ([]protocol.SearchResult, error) {
|
func searchSteps(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *searchScan) ([]protocol.SearchResult, error) {
|
||||||
if terms.Text == "" {
|
if terms.Text == "" && !terms.Browse {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
pattern := searchLike(terms.Text)
|
query := `SELECT s.run_id,s.sequence,s.stage_key,s.outcome,s.target,s.diagnostic,` + searchStepRecency + `,` + searchStepEvidence + ` ` + searchStepJoin
|
||||||
rows, err := tx.QueryContext(ctx, `SELECT s.run_id,s.sequence,s.stage_key,s.outcome,s.target,s.diagnostic,`+searchStepRecency+`,`+searchStepEvidence+`
|
var args []any
|
||||||
`+searchStepJoin+`
|
if terms.Browse {
|
||||||
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 '\'
|
query += " ORDER BY s.started_at DESC,s.run_id DESC,s.sequence DESC LIMIT ?"
|
||||||
ORDER BY s.run_id DESC,s.sequence DESC LIMIT ?`, pattern, pattern, pattern, pattern, searchRowBudget+1)
|
} else {
|
||||||
|
query += ` 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 := searchLike(terms.Text)
|
||||||
|
args = append(args, pattern, pattern, pattern, pattern)
|
||||||
|
}
|
||||||
|
args = append(args, searchCandidateLimit(terms))
|
||||||
|
rows, err := tx.QueryContext(ctx, query, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -404,7 +499,9 @@ ORDER BY s.run_id DESC,s.sequence DESC LIMIT ?`, pattern, pattern, pattern, patt
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
score, matched := 1, "diagnostic"
|
score, matched := 1, "diagnostic"
|
||||||
if s, ok := searchTier(stage, terms.Text); ok {
|
if terms.Browse {
|
||||||
|
score, matched = 0, ""
|
||||||
|
} else if s, ok := searchTier(stage, terms.Text); ok {
|
||||||
score, matched = s, "stage"
|
score, matched = s, "stage"
|
||||||
} else if searchContains(outcome, terms.Text) {
|
} else if searchContains(outcome, terms.Text) {
|
||||||
matched = "outcome"
|
matched = "outcome"
|
||||||
@@ -440,14 +537,18 @@ type searchScan struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func searchFields(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *searchScan) ([]protocol.SearchResult, error) {
|
func searchFields(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *searchScan) ([]protocol.SearchResult, error) {
|
||||||
if terms.Text == "" {
|
if terms.Text == "" && !terms.Browse {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
// One extra step is requested so a scan that hits the step bound can see that
|
// 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.
|
// 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+`
|
query := `SELECT s.run_id,s.sequence,s.input_json,s.output_json,s.transcript,` + searchStepEvidence + `,` + searchStepRecency + ` ` + searchStepJoin
|
||||||
`+searchStepJoin+`
|
if terms.Browse {
|
||||||
ORDER BY s.run_id DESC,s.sequence DESC LIMIT ?`, protocol.SearchMaxScanSteps+1)
|
query += " ORDER BY s.started_at DESC,s.run_id DESC,s.sequence DESC LIMIT ?"
|
||||||
|
} else {
|
||||||
|
query += " ORDER BY s.run_id DESC,s.sequence DESC LIMIT ?"
|
||||||
|
}
|
||||||
|
rows, err := tx.QueryContext(ctx, query, protocol.SearchMaxScanSteps+1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -498,7 +599,7 @@ ORDER BY s.run_id DESC,s.sequence DESC LIMIT ?`, protocol.SearchMaxScanSteps+1)
|
|||||||
Field: &protocol.SearchField{RunID: runID, Sequence: sequence, Field: field.name, Snippet: searchSnippet(text, terms.Text)},
|
Field: &protocol.SearchField{RunID: runID, Sequence: sequence, Field: field.name, Snippet: searchSnippet(text, terms.Text)},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if searchContains(transcript, terms.Text) {
|
if searchContains(transcript, terms.Text) || (terms.Browse && transcript != "") {
|
||||||
results = append(results, protocol.SearchResult{
|
results = append(results, protocol.SearchResult{
|
||||||
Kind: protocol.SearchKindField, Target: searchFieldTarget(runID, sequence, "transcript"),
|
Kind: protocol.SearchKindField, Target: searchFieldTarget(runID, sequence, "transcript"),
|
||||||
Score: 1, MatchedField: "transcript", Recency: recency,
|
Score: 1, MatchedField: "transcript", Recency: recency,
|
||||||
@@ -510,14 +611,23 @@ ORDER BY s.run_id DESC,s.sequence DESC LIMIT ?`, protocol.SearchMaxScanSteps+1)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func searchArtifacts(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *searchScan) ([]protocol.SearchResult, error) {
|
func searchArtifacts(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *searchScan) ([]protocol.SearchResult, error) {
|
||||||
if terms.Text == "" {
|
if terms.Text == "" && !terms.Browse {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
pattern := searchLike(terms.Text)
|
query := `SELECT a.run_id,a.sequence,a.artifacts_json,COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ',r.created_at),'')
|
||||||
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`
|
||||||
FROM step_artifacts a JOIN pipeline_runs r ON r.id=a.run_id
|
var args []any
|
||||||
WHERE lower(a.artifacts_json) LIKE ? ESCAPE '\'
|
if terms.Browse {
|
||||||
ORDER BY a.run_id DESC,a.sequence DESC LIMIT ?`, pattern, searchRowBudget+1)
|
query = `SELECT a.run_id,a.sequence,a.artifacts_json,COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ',r.created_at),'')
|
||||||
|
FROM pipeline_runs r CROSS JOIN step_artifacts a ON a.run_id=r.id
|
||||||
|
ORDER BY r.created_at DESC,r.id DESC,a.sequence DESC LIMIT ?`
|
||||||
|
} else {
|
||||||
|
query += ` WHERE lower(a.artifacts_json) LIKE ? ESCAPE '\' ORDER BY a.run_id DESC,a.sequence DESC LIMIT ?`
|
||||||
|
args = append(args, searchLike(terms.Text))
|
||||||
|
}
|
||||||
|
// Metadata rows can contain several artifacts or malformed retained JSON.
|
||||||
|
args = append(args, searchRowBudget+1)
|
||||||
|
rows, err := tx.QueryContext(ctx, query, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -544,7 +654,9 @@ ORDER BY a.run_id DESC,a.sequence DESC LIMIT ?`, pattern, searchRowBudget+1)
|
|||||||
}
|
}
|
||||||
for _, file := range evidence.Files {
|
for _, file := range evidence.Files {
|
||||||
score, matched := 1, "content_type"
|
score, matched := 1, "content_type"
|
||||||
if s, ok := searchTier(file.Name, terms.Text); ok {
|
if terms.Browse {
|
||||||
|
score, matched = 0, ""
|
||||||
|
} else if s, ok := searchTier(file.Name, terms.Text); ok {
|
||||||
score, matched = s, "name"
|
score, matched = s, "name"
|
||||||
} else if !searchContains(file.ContentType, terms.Text) {
|
} else if !searchContains(file.ContentType, terms.Text) {
|
||||||
continue
|
continue
|
||||||
@@ -565,7 +677,7 @@ ORDER BY a.run_id DESC,a.sequence DESC LIMIT ?`, pattern, searchRowBudget+1)
|
|||||||
func findMatch(value any, text string) (string, bool) {
|
func findMatch(value any, text string) (string, bool) {
|
||||||
switch typed := value.(type) {
|
switch typed := value.(type) {
|
||||||
case string:
|
case string:
|
||||||
if searchContains(typed, text) {
|
if searchContains(typed, text) || (text == "" && typed != "") {
|
||||||
return typed, true
|
return typed, true
|
||||||
}
|
}
|
||||||
case map[string]any:
|
case map[string]any:
|
||||||
|
|||||||
@@ -0,0 +1,152 @@
|
|||||||
|
package pipeline
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSearchKindOnlyCanonicalRows(t *testing.T) {
|
||||||
|
store, _ := searchFixture(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
for _, kind := range protocol.SearchKinds {
|
||||||
|
t.Run(kind, func(t *testing.T) {
|
||||||
|
for _, query := range []string{"", " \t\n "} {
|
||||||
|
response, err := store.Search(ctx, protocol.SearchRequest{Query: query, Kind: " " + kind + " "}, time.Now())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(response.Results) == 0 || response.Kind != kind || response.Query != query {
|
||||||
|
t.Fatalf("missing kind-only results: %+v", response)
|
||||||
|
}
|
||||||
|
for _, hit := range response.Results {
|
||||||
|
if hit.Kind != kind || hit.Target == "" {
|
||||||
|
t.Fatalf("invalid canonical hit: %+v", hit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSearchKindOnlyValidationAndLimits(t *testing.T) {
|
||||||
|
store, _ := searchFixture(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
for _, kind := range []string{"bogus", "PIPELINE", "pipeline' OR 1=1 --"} {
|
||||||
|
if _, err := store.Search(ctx, protocol.SearchRequest{Kind: kind}, time.Now()); err == nil {
|
||||||
|
t.Fatalf("blank text bypassed validation for %q", kind)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, query := range []string{"", " \n\t"} {
|
||||||
|
response, err := store.Search(ctx, protocol.SearchRequest{Query: query}, time.Now())
|
||||||
|
if err != nil || response.Results == nil || len(response.Results) != 0 || response.Truncated {
|
||||||
|
t.Fatalf("empty all-kind query must remain a no-op: %+v %v", response, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, limit := range []int{-1, 0, 1, protocol.SearchMaxLimit + 1} {
|
||||||
|
response, err := store.Search(ctx, protocol.SearchRequest{Kind: protocol.SearchKindField, Limit: limit}, time.Now())
|
||||||
|
if err != nil || response.Cap != searchLimit(limit) || len(response.Results) > response.Cap {
|
||||||
|
t.Fatalf("kind-only limit %d: %+v %v", limit, response, err)
|
||||||
|
}
|
||||||
|
if limit == 1 && (len(response.Results) != 1 || !response.Truncated) {
|
||||||
|
t.Fatalf("expected bounded/truncated fields: %+v", response)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSearchKindOnlyRecencyThenNumericID(t *testing.T) {
|
||||||
|
store, run := searchFixture(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
for i := 0; i < 11; i++ {
|
||||||
|
_, err := store.db.Exec(`INSERT INTO pipeline_runs(pipeline_id,version,request_key,workspace,allow_tools,write_paths,input_json,created_at)
|
||||||
|
SELECT pipeline_id,version,printf('recent-%d',?),workspace,allow_tools,write_paths,input_json,'2020-01-01 00:00:00' FROM pipeline_runs WHERE id=?`, i, run)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// The oldest ID is newest by timestamp, followed by equal-time numeric IDs.
|
||||||
|
if _, err := store.db.Exec("UPDATE pipeline_runs SET created_at='2030-01-01 00:00:00' WHERE id=?", run); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
response, err := store.Search(ctx, protocol.SearchRequest{Kind: protocol.SearchKindRun, Limit: 4}, time.Now())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var ids []int64
|
||||||
|
for _, hit := range response.Results {
|
||||||
|
ids = append(ids, hit.Run.ID)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(ids, []int64{run, run + 11, run + 10, run + 9}) || !response.Truncated {
|
||||||
|
t.Fatalf("recency/ID ordering or limit: %+v", response)
|
||||||
|
}
|
||||||
|
again, err := store.Search(ctx, protocol.SearchRequest{Kind: protocol.SearchKindRun, Limit: 4}, time.Now())
|
||||||
|
if err != nil || !reflect.DeepEqual(response, again) {
|
||||||
|
t.Fatalf("kind-only results changed between reads: %+v %v", again, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSearchRecentCanonicalIndexes(t *testing.T) {
|
||||||
|
store, _ := searchFixture(t)
|
||||||
|
for _, tc := range []struct{ query, index string }{
|
||||||
|
{"SELECT pipeline_id,version FROM pipeline_versions ORDER BY created_at DESC,pipeline_id DESC,version DESC LIMIT 51", "search_versions_recent"},
|
||||||
|
{"SELECT p.id 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) ORDER BY v.created_at DESC,v.pipeline_id DESC,v.version DESC LIMIT 51", "search_versions_recent"},
|
||||||
|
{"SELECT id FROM pipeline_runs ORDER BY created_at DESC,id DESC LIMIT 51", "search_runs_recent"},
|
||||||
|
{"SELECT run_id,sequence FROM run_steps ORDER BY started_at DESC,run_id DESC,sequence DESC LIMIT 51", "search_steps_recent"},
|
||||||
|
{"SELECT a.run_id,a.sequence FROM pipeline_runs r CROSS JOIN step_artifacts a ON a.run_id=r.id ORDER BY r.created_at DESC,r.id DESC,a.sequence DESC LIMIT 2001", "search_runs_recent"},
|
||||||
|
} {
|
||||||
|
rows, err := store.db.Query("EXPLAIN QUERY PLAN " + tc.query)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var plan string
|
||||||
|
for rows.Next() {
|
||||||
|
var id, parent, unused int
|
||||||
|
var detail string
|
||||||
|
if err := rows.Scan(&id, &parent, &unused, &detail); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
plan += detail
|
||||||
|
}
|
||||||
|
rows.Close()
|
||||||
|
if !strings.Contains(plan, tc.index) || strings.Contains(plan, "USE TEMP B-TREE FOR ORDER BY") {
|
||||||
|
t.Fatalf("recent rows must use ordered canonical index: %s", plan)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSearchKindOnlyMaximumResultCap(t *testing.T) {
|
||||||
|
store, run := searchFixture(t)
|
||||||
|
_, err := store.db.Exec(`WITH RECURSIVE n(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM n WHERE x<250)
|
||||||
|
INSERT INTO pipeline_runs(pipeline_id,version,request_key,workspace,allow_tools,write_paths,input_json)
|
||||||
|
SELECT r.pipeline_id,r.version,printf('cap-%d',n.x),r.workspace,r.allow_tools,r.write_paths,r.input_json FROM pipeline_runs r CROSS JOIN n WHERE r.id=?`, run)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, limit := range []int{0, 1, 9999} {
|
||||||
|
response, err := store.Search(context.Background(), protocol.SearchRequest{Kind: protocol.SearchKindRun, Limit: limit}, time.Now())
|
||||||
|
if err != nil || len(response.Results) != searchLimit(limit) || !response.Truncated || response.ScannedSteps != 0 || response.ScannedBytes != 0 {
|
||||||
|
t.Fatalf("kind-only cap %d not enforced: %+v %v", limit, response, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSearchKindOnlyPipelineIsLatestCanonicalVersion(t *testing.T) {
|
||||||
|
store, _ := searchFixture(t)
|
||||||
|
_, err := store.db.Exec(`INSERT INTO pipeline_versions(pipeline_id,version,source,content_hash,graph_json,created_at)
|
||||||
|
SELECT pipeline_id,version+1,source,content_hash||'-next',graph_json,'2031-01-01 00:00:00' FROM pipeline_versions`)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
response, err := store.Search(context.Background(), protocol.SearchRequest{Kind: protocol.SearchKindPipeline}, time.Now())
|
||||||
|
if err != nil || len(response.Results) != 1 || response.Results[0].Pipeline.Version != 2 {
|
||||||
|
t.Fatalf("pipeline browse must return latest canonical version only: %+v %v", response, err)
|
||||||
|
}
|
||||||
|
versions, err := store.Search(context.Background(), protocol.SearchRequest{Kind: protocol.SearchKindVersion}, time.Now())
|
||||||
|
if err != nil || len(versions.Results) != 2 || versions.Results[0].Version.Version != 2 {
|
||||||
|
t.Fatalf("version browse must retain history in recent order: %+v %v", versions, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package pipeline
|
||||||
|
|
||||||
|
// Recent-kind browsing orders canonical table rows before applying the existing
|
||||||
|
// row/body budgets. These covering order keys avoid sorting the whole history.
|
||||||
|
// No duplicate search store, FTS wildcard, artifact-content scan or write during
|
||||||
|
// Search is needed; SQLite maintains the indexes with canonical writes.
|
||||||
|
func (s *Store) initSearch() error {
|
||||||
|
_, err := s.db.Exec(`
|
||||||
|
CREATE INDEX IF NOT EXISTS search_versions_recent ON pipeline_versions(created_at DESC,pipeline_id DESC,version DESC);
|
||||||
|
CREATE INDEX IF NOT EXISTS search_runs_recent ON pipeline_runs(created_at DESC,id DESC);
|
||||||
|
CREATE INDEX IF NOT EXISTS search_steps_recent ON run_steps(started_at DESC,run_id DESC,sequence DESC);
|
||||||
|
`)
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -538,6 +538,10 @@ func TestSearchScanBoundTruncates(t *testing.T) {
|
|||||||
t.Fatalf("scan passed the bound and matched: %+v", hit.Field)
|
t.Fatalf("scan passed the bound and matched: %+v", hit.Field)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
recent, err := store.Search(ctx, protocol.SearchRequest{Kind: protocol.SearchKindField}, time.Now())
|
||||||
|
if err != nil || !recent.Truncated || len(recent.Results) != 0 || recent.ScannedBytes > protocol.SearchMaxScanBytes {
|
||||||
|
t.Fatalf("kind-only fields bypassed the byte budget: %+v %v", recent, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestSearchScanBoundTruncatesByRowCount proves the step-count bound is
|
// TestSearchScanBoundTruncatesByRowCount proves the step-count bound is
|
||||||
@@ -584,6 +588,10 @@ func TestSearchScanBoundTruncatesByRowCount(t *testing.T) {
|
|||||||
if response.ScannedBytes > protocol.SearchMaxScanBytes {
|
if response.ScannedBytes > protocol.SearchMaxScanBytes {
|
||||||
t.Fatalf("scanned_bytes=%d exceeds bound %d", response.ScannedBytes, protocol.SearchMaxScanBytes)
|
t.Fatalf("scanned_bytes=%d exceeds bound %d", response.ScannedBytes, protocol.SearchMaxScanBytes)
|
||||||
}
|
}
|
||||||
|
recent, err := store.Search(ctx, protocol.SearchRequest{Kind: protocol.SearchKindField}, time.Now())
|
||||||
|
if err != nil || !recent.Truncated || recent.ScannedSteps != protocol.SearchMaxScanSteps {
|
||||||
|
t.Fatalf("kind-only fields bypassed the step budget: %+v %v", recent, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func setCreated(t *testing.T, store *Store, runID int64, timestamp string) {
|
func setCreated(t *testing.T, store *Store, runID int64, timestamp string) {
|
||||||
|
|||||||
@@ -58,6 +58,10 @@ CREATE TABLE IF NOT EXISTS submissions (id INTEGER PRIMARY KEY, name TEXT NOT NU
|
|||||||
db.Close()
|
db.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if err := s.initSearch(); err != nil {
|
||||||
|
db.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
if err := s.initRunNotifications(); err != nil {
|
if err := s.initRunNotifications(); err != nil {
|
||||||
db.Close()
|
db.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -61,6 +61,8 @@ const (
|
|||||||
// SearchRequest is the search request body. Query is the free text, a bare
|
// 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;
|
// status word or a date expression; Kind optionally narrows to one SearchKind;
|
||||||
// Limit caps the returned results (0 = server default).
|
// Limit caps the returned results (0 = server default).
|
||||||
|
// Blank Query with a valid nonempty Kind browses recent items of that kind;
|
||||||
|
// blank Query with empty Kind remains a no-op. Invalid kinds always fail.
|
||||||
type SearchRequest struct {
|
type SearchRequest struct {
|
||||||
Query string `json:"query"`
|
Query string `json:"query"`
|
||||||
Kind string `json:"kind,omitempty"`
|
Kind string `json:"kind,omitempty"`
|
||||||
@@ -68,8 +70,10 @@ type SearchRequest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SearchResponse is the ranked, bounded set of matches. Results are ordered by
|
// SearchResponse is the ranked, bounded set of matches. Results are ordered by
|
||||||
// score, then recency, then a stable kind/target tie-break. Truncated reports
|
// score, then recency, then a stable kind/target tie-break.
|
||||||
// that either the result cap or the field-scan budget dropped matches;
|
// For kind-only browsing, order is recency descending, numeric entity/visit IDs
|
||||||
|
// descending, then target ascending, without a text-relevance score.
|
||||||
|
// Truncated reports that either the result cap or the field-scan budget dropped matches;
|
||||||
// ScannedSteps/ScannedBytes and ScanBoundBytes make the body-scan bound
|
// ScannedSteps/ScannedBytes and ScanBoundBytes make the body-scan bound
|
||||||
// observable.
|
// observable.
|
||||||
type SearchResponse struct {
|
type SearchResponse struct {
|
||||||
|
|||||||
@@ -9,6 +9,10 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/coder/websocket"
|
||||||
|
"github.com/coder/websocket/wsjson"
|
||||||
|
|
||||||
"git.a-wan.net/alan/pipelines/internal/cli"
|
"git.a-wan.net/alan/pipelines/internal/cli"
|
||||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||||
@@ -102,4 +106,42 @@ func TestCLISearchEndToEndEveryKind(t *testing.T) {
|
|||||||
if _, err := runCLI("search", "zephyr", "--type", "bogus"); err == nil || !strings.Contains(err.Error(), "unknown search 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)
|
t.Fatalf("invalid kind must error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The browser sends an actual empty query, not an invented wildcard. Exercise
|
||||||
|
// that unchanged wire shape over /connect against this throwaway store.
|
||||||
|
wireCtx, wireCancel := context.WithTimeout(ctx, 10*time.Second)
|
||||||
|
defer wireCancel()
|
||||||
|
conn, _, err := websocket.Dial(wireCtx, url, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer conn.CloseNow()
|
||||||
|
for _, kind := range append(append([]string{}, protocol.SearchKinds...), "", "bogus") {
|
||||||
|
request := protocol.Message{Type: protocol.Search, ID: "recent-" + kind, Search: &protocol.SearchRequest{Query: "", Kind: kind, Limit: 1}}
|
||||||
|
if err := wsjson.Write(wireCtx, conn, request); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var reply protocol.Message
|
||||||
|
for reply.ID != request.ID {
|
||||||
|
if err := wsjson.Read(wireCtx, conn, &reply); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if kind == "bogus" {
|
||||||
|
if !strings.Contains(reply.Error, "unknown search kind") {
|
||||||
|
t.Fatalf("blank text must not bypass kind validation: %+v", reply)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if reply.SearchResults == nil || reply.SearchResults.Cap != 1 || reply.SearchResults.Query != "" {
|
||||||
|
t.Fatalf("invalid kind-only reply: %+v", reply)
|
||||||
|
}
|
||||||
|
if kind == "" {
|
||||||
|
if len(reply.SearchResults.Results) != 0 {
|
||||||
|
t.Fatalf("blank unfiltered query must remain a no-op: %+v", reply)
|
||||||
|
}
|
||||||
|
} else if hits := reply.SearchResults.Results; len(hits) != 1 || hits[0].Kind != kind {
|
||||||
|
t.Fatalf("missing bounded kind-only result for %q: %+v", kind, reply)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-7
@@ -158,19 +158,26 @@ rename's exact server reason and, under `--decide`, the full live decision flow.
|
|||||||
|
|
||||||
## Search
|
## Search
|
||||||
|
|
||||||
Type-prefix follow-up (2026-10-20, **partial — isolated browser fixture, not a
|
Type-prefix follow-up (2026-10-20, **partial — isolated browser fixture and
|
||||||
live-server acceptance run**): unquoted, case-insensitive `t:` / `type:` tokens
|
scratch server-command tests, not a deployed-server acceptance run**): unquoted, case-insensitive `t:` / `type:` tokens
|
||||||
accept the existing singular kinds (`pipeline`, `run`, `step`, `field`,
|
accept the existing singular kinds (`pipeline`, `run`, `step`, `field`,
|
||||||
`artifact`, `version`). The last recognized token overrides the selector and is
|
`artifact`, `version`). The last recognized token overrides the selector and is
|
||||||
reflected in Kind and the summary. Selecting Kind or Clear filter removes all
|
reflected in Kind and the summary. Selecting Kind or Clear filter removes all
|
||||||
recognized tokens; unsupported and quoted tokens remain literal text. Only the
|
recognized tokens; unsupported and quoted tokens remain literal text. Only the
|
||||||
remaining text and the effective `kind` reach the unchanged `search` request.
|
remaining text and the effective `kind` reach the unchanged `search` request.
|
||||||
Type-only queries show explicit help, not results: `Store.Search` deliberately
|
Type-only queries now send a real empty `query` plus a valid nonempty `kind` to
|
||||||
returns no results for blank text. Parser tests plus `node test/ui-search-types.mjs`
|
list bounded recent canonical items. The server uses recency indexes on existing
|
||||||
|
tables, then numeric entity/visit IDs and target as deterministic tie-breaks;
|
||||||
|
existing result caps and field-scan budgets still apply. Blank text with no kind
|
||||||
|
remains a no-op, and an invalid kind still errors. No wildcard text is invented.
|
||||||
|
The UI labels recent items and result/scan caps, and distinguishes an empty kind
|
||||||
|
from a scan-limited empty result. Parser tests plus `node test/ui-search-types.mjs`
|
||||||
exercise the real view/repository with recorded fixture requests and no service
|
exercise the real view/repository with recorded fixture requests and no service
|
||||||
connection. Captures: `captures/search-types/01-type-filter.png` through
|
connection. Captures: `captures/search-types-recent/01-type-filter.png` through
|
||||||
`06-cleared-filter.png` (including precedence, selector replacement, literal
|
`09-cleared-filter.png` (including precedence, selector replacement, literal
|
||||||
tokens and type-only help). This browser-only shorthand is not yet on Android;
|
tokens, type-only pipeline/artifact results and both empty states). Server tests
|
||||||
|
exercise empty-query searches for all six kinds over an actual `/connect`
|
||||||
|
WebSocket against a throwaway database. This browser-only shorthand is not yet on Android;
|
||||||
the earlier live-search acceptance evidence below is unchanged.
|
the earlier live-search acceptance evidence below is unchanged.
|
||||||
|
|
||||||
| # | Feature | Protocol | Status |
|
| # | Feature | Protocol | Status |
|
||||||
|
|||||||
+9
-8
@@ -61,7 +61,6 @@ export function parseSearchQuery(value, selectedKind = DEFAULT_SEARCH_KIND) {
|
|||||||
return { query, kind, hasType };
|
return { query, kind, hasType };
|
||||||
}
|
}
|
||||||
|
|
||||||
export const SEARCH_TYPE_HELP = "Add search text after the type filter (for example, t:pipeline plan). This server does not list results by type alone.";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The orders the search sort control offers. "Relevance" keeps the server's own
|
* The orders the search sort control offers. "Relevance" keeps the server's own
|
||||||
@@ -337,19 +336,21 @@ export function searchPanelEmptyMessage(state, shown, kind) {
|
|||||||
if ((shown ?? []).length > 0) return null;
|
if ((shown ?? []).length > 0) return null;
|
||||||
if (state?.loading) return null;
|
if (state?.loading) return null;
|
||||||
const query = String(state?.query ?? "");
|
const query = String(state?.query ?? "");
|
||||||
if (query.trim() === "") return null;
|
if (query.trim() === "") {
|
||||||
|
if (!state?.loaded || !kind) return null;
|
||||||
|
const label = searchKindLabel(kind).toLowerCase();
|
||||||
|
return state?.truncated ? `No ${label} found within the server's scan limit.` : `No ${label} yet.`;
|
||||||
|
}
|
||||||
if (kind === "" || kind == null) return `No matches for "${query}".`;
|
if (kind === "" || kind == null) return `No matches for "${query}".`;
|
||||||
return `No ${searchKindLabel(kind).toLowerCase()} match "${query}".`;
|
return `No ${searchKindLabel(kind).toLowerCase()} match "${query}".`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether a keystroke's text is worth asking the server about. A blank or
|
* Text searches and valid kind-only browsing are sendable. Empty text with no
|
||||||
* whitespace-only query is cleared locally and never sent, so the server is not
|
* supported kind still clears locally, without an all-kind scan.
|
||||||
* asked to scan for nothing. The one predicate the field's input handler and its
|
|
||||||
* immediate-send path share.
|
|
||||||
*/
|
*/
|
||||||
export function searchQuerySendable(query) {
|
export function searchQuerySendable(query, kind = "") {
|
||||||
return String(query ?? "").trim() !== "";
|
return String(query ?? "").trim() !== "" || SEARCH_KINDS.some((option) => option.value !== "" && option.value === kind);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+27
-27
@@ -12,7 +12,6 @@ import {
|
|||||||
SEARCH_DEBOUNCE_MS,
|
SEARCH_DEBOUNCE_MS,
|
||||||
SEARCH_KINDS,
|
SEARCH_KINDS,
|
||||||
SEARCH_SORTS,
|
SEARCH_SORTS,
|
||||||
SEARCH_TYPE_HELP,
|
|
||||||
parseSearchQuery,
|
parseSearchQuery,
|
||||||
SearchSequencer,
|
SearchSequencer,
|
||||||
filterSearchResults,
|
filterSearchResults,
|
||||||
@@ -87,9 +86,9 @@ function openSearch() {
|
|||||||
state.searchFocusPending = true;
|
state.searchFocusPending = true;
|
||||||
renderHeader();
|
renderHeader();
|
||||||
renderSearch();
|
renderSearch();
|
||||||
// Resume: the previous results show immediately, and a non-blank query is
|
// Resume: show previous results immediately, then refresh text or kind-only
|
||||||
// refreshed so the list is not stale.
|
// searches so the list is not stale.
|
||||||
if (searchQuerySendable(state.searchQuery)) {
|
if (searchQuerySendable(state.searchQuery, parsedSearch().kind)) {
|
||||||
state.searchLoading = true;
|
state.searchLoading = true;
|
||||||
const sent = state.searchSequencer.begin(state.searchSequencer.edit(state.searchQuery));
|
const sent = state.searchSequencer.begin(state.searchSequencer.edit(state.searchQuery));
|
||||||
if (sent) runSearch(sent);
|
if (sent) runSearch(sent);
|
||||||
@@ -134,16 +133,16 @@ function cancelSearchDebounce() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A keystroke. A blank/whitespace query clears the results locally and sends
|
* A keystroke. Blank text without a kind clears locally and sends nothing;
|
||||||
* nothing; otherwise the previous results stay visible, the request is marked in
|
* kind-only searches browse recent items. Previous results stay visible while
|
||||||
* flight and the send is debounced `SEARCH_DEBOUNCE_MS` from the last keystroke.
|
* loading; sending is debounced `SEARCH_DEBOUNCE_MS` from the last keystroke.
|
||||||
* Recording the edit is itself enough to drop any older reply still in flight.
|
* Recording the edit is itself enough to drop any older reply still in flight.
|
||||||
*/
|
*/
|
||||||
function onSearchInput(value) {
|
function onSearchInput(value) {
|
||||||
state.searchQuery = value;
|
state.searchQuery = value;
|
||||||
state.searchError = "";
|
state.searchError = "";
|
||||||
cancelSearchDebounce();
|
cancelSearchDebounce();
|
||||||
if (!searchQuerySendable(value)) {
|
if (!searchQuerySendable(value, parsedSearch().kind)) {
|
||||||
state.searchSequencer.edit("");
|
state.searchSequencer.edit("");
|
||||||
state.searchResults = [];
|
state.searchResults = [];
|
||||||
state.searchLoading = false;
|
state.searchLoading = false;
|
||||||
@@ -165,7 +164,7 @@ function onSearchInput(value) {
|
|||||||
function sendSearchNow() {
|
function sendSearchNow() {
|
||||||
const query = state.searchQuery;
|
const query = state.searchQuery;
|
||||||
cancelSearchDebounce();
|
cancelSearchDebounce();
|
||||||
if (!searchQuerySendable(query)) {
|
if (!searchQuerySendable(query, parsedSearch().kind)) {
|
||||||
state.searchSequencer.edit("");
|
state.searchSequencer.edit("");
|
||||||
state.searchResults = [];
|
state.searchResults = [];
|
||||||
state.searchLoading = false;
|
state.searchLoading = false;
|
||||||
@@ -180,8 +179,8 @@ function sendSearchNow() {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* A kind-filter change: the in-hand filter is re-applied immediately so the list
|
* A kind-filter change: the in-hand filter is re-applied immediately so the list
|
||||||
* always matches the control, and — when the query is non-blank — a fresh server
|
* always matches the control. Text or a valid kind sends a fresh server request;
|
||||||
* request is sent with the new kind. A sort change never sends a request.
|
* clearing both stays local. A sort change never sends a request.
|
||||||
*/
|
*/
|
||||||
function setSearchKind(kind) {
|
function setSearchKind(kind) {
|
||||||
state.searchQuery = parsedSearch().query;
|
state.searchQuery = parsedSearch().query;
|
||||||
@@ -204,15 +203,8 @@ function setSearchSort(sort) {
|
|||||||
*/
|
*/
|
||||||
async function runSearch(sent) {
|
async function runSearch(sent) {
|
||||||
const parsed = parseSearchQuery(sent.query, state.searchKind);
|
const parsed = parseSearchQuery(sent.query, state.searchKind);
|
||||||
if (!parsed.query.trim()) {
|
|
||||||
state.searchResults = [];
|
|
||||||
state.searchLoading = false;
|
|
||||||
state.searchLoaded = false;
|
|
||||||
refreshSearch();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
const reply = await repo.search(parsed.query, parsed.kind);
|
const reply = await repo.search(parsed.query.trim() ? parsed.query : "", parsed.kind);
|
||||||
if (!state.searchSequencer.accepts(sent.token)) return;
|
if (!state.searchSequencer.accepts(sent.token)) return;
|
||||||
if (reply == null) {
|
if (reply == null) {
|
||||||
state.searchLoading = false;
|
state.searchLoading = false;
|
||||||
@@ -222,6 +214,7 @@ async function runSearch(sent) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
state.searchResults = Array.isArray(reply.results) ? reply.results : [];
|
state.searchResults = Array.isArray(reply.results) ? reply.results : [];
|
||||||
|
state.searchTruncated = Boolean(reply.truncated);
|
||||||
state.searchActiveIndex = 0;
|
state.searchActiveIndex = 0;
|
||||||
state.searchLoading = false;
|
state.searchLoading = false;
|
||||||
state.searchError = "";
|
state.searchError = "";
|
||||||
@@ -315,7 +308,7 @@ function renderSearch() {
|
|||||||
}
|
}
|
||||||
if (event.key === "Enter") {
|
if (event.key === "Enter") {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
if (shown.length && !state.searchLoading && !state.searchError && parsedSearch().query.trim()) {
|
if (shown.length && !state.searchLoading && !state.searchError) {
|
||||||
routeSearchResult(shown[state.searchActiveIndex] ?? shown[0]);
|
routeSearchResult(shown[state.searchActiveIndex] ?? shown[0]);
|
||||||
} else sendSearchNow();
|
} else sendSearchNow();
|
||||||
}
|
}
|
||||||
@@ -355,7 +348,7 @@ function renderSearch() {
|
|||||||
"div",
|
"div",
|
||||||
{ id: "searchPanel", class: "search-panel", role: "dialog", "aria-modal": "true", "aria-label": "Search" },
|
{ id: "searchPanel", class: "search-panel", role: "dialog", "aria-modal": "true", "aria-label": "Search" },
|
||||||
h("div", { class: "search-modal", role: "search" }, fieldRow, controls,
|
h("div", { class: "search-modal", role: "search" }, fieldRow, controls,
|
||||||
h("p", { class: "muted", text: "Filter with t:pipeline or type:artifact (pipeline, run, step, field, artifact, version). Last type wins; choosing Kind replaces it. Quote tokens to search literally." }), area),
|
h("p", { class: "muted", text: "Filter with t:pipeline or type:artifact (pipeline, run, step, field, artifact, version). Type alone lists recent items. Last type wins; choosing Kind replaces it. Quote tokens to search literally." }), area),
|
||||||
);
|
);
|
||||||
// A spotlight: clicking the dimmed backdrop closes it (the modal itself stops).
|
// A spotlight: clicking the dimmed backdrop closes it (the modal itself stops).
|
||||||
panel.addEventListener("click", (event) => {
|
panel.addEventListener("click", (event) => {
|
||||||
@@ -377,19 +370,20 @@ function renderSearch() {
|
|||||||
*/
|
*/
|
||||||
function fillSearchResults(area) {
|
function fillSearchResults(area) {
|
||||||
const parsed = parsedSearch();
|
const parsed = parsedSearch();
|
||||||
if (parsed.hasType && !parsed.query.trim()) {
|
|
||||||
area.replaceChildren(h("div", { class: "search-empty muted", text: SEARCH_TYPE_HELP }));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const shown = sortSearchResults(
|
const shown = sortSearchResults(
|
||||||
filterSearchResults(state.searchResults, parsed.kind),
|
filterSearchResults(state.searchResults, parsed.kind),
|
||||||
state.searchSort,
|
state.searchSort,
|
||||||
);
|
);
|
||||||
const nodes = [];
|
const nodes = [];
|
||||||
if (searchPanelProgress(state)) {
|
const panelState = {
|
||||||
|
query: parsed.query, loading: state.searchLoading, loaded: state.searchLoaded,
|
||||||
|
error: state.searchError, results: state.searchResults,
|
||||||
|
truncated: state.searchTruncated,
|
||||||
|
};
|
||||||
|
if (searchPanelProgress(panelState)) {
|
||||||
nodes.push(h("div", { class: "search-progress", role: "progressbar", "aria-label": "Searching" }));
|
nodes.push(h("div", { class: "search-progress", role: "progressbar", "aria-label": "Searching" }));
|
||||||
}
|
}
|
||||||
const body = searchPanelBody(state, shown, parsed.kind);
|
const body = searchPanelBody(panelState, shown, parsed.kind);
|
||||||
if (body.kind === "error") {
|
if (body.kind === "error") {
|
||||||
nodes.push(h("div", { class: "search-error", text: body.message }));
|
nodes.push(h("div", { class: "search-error", text: body.message }));
|
||||||
} else if (body.kind === "results") {
|
} else if (body.kind === "results") {
|
||||||
@@ -414,6 +408,12 @@ function fillSearchResults(area) {
|
|||||||
} else if (body.kind === "empty") {
|
} else if (body.kind === "empty") {
|
||||||
nodes.push(h("div", { class: "search-empty muted", text: body.message }));
|
nodes.push(h("div", { class: "search-empty muted", text: body.message }));
|
||||||
}
|
}
|
||||||
|
if (body.kind === "results" && !state.searchLoading && parsed.kind && !parsed.query.trim()) {
|
||||||
|
nodes.push(h("div", {
|
||||||
|
class: "search-summary muted",
|
||||||
|
text: state.searchTruncated ? "Recent items · limited by the server's result or scan cap." : "Recent items",
|
||||||
|
}));
|
||||||
|
}
|
||||||
area.replaceChildren(...nodes);
|
area.replaceChildren(...nodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { test } from "node:test";
|
import { test } from "node:test";
|
||||||
import assert from "node:assert/strict";
|
import assert from "node:assert/strict";
|
||||||
import { parseSearchQuery, SEARCH_KINDS } from "../src/search.js";
|
import { parseSearchQuery, searchQuerySendable, searchPanelBody, SEARCH_KINDS } from "../src/search.js";
|
||||||
|
|
||||||
test("both type aliases and kinds are case insensitive", () => {
|
test("both type aliases and kinds are case insensitive", () => {
|
||||||
for (const { value } of SEARCH_KINDS.filter((kind) => kind.value)) {
|
for (const { value } of SEARCH_KINDS.filter((kind) => kind.value)) {
|
||||||
@@ -36,3 +36,22 @@ test("type-only query is distinguishable from literal text and blank input", ()
|
|||||||
assert.deepEqual(parseSearchQuery("t:pipeline"), { query: "", kind: "pipeline", hasType: true });
|
assert.deepEqual(parseSearchQuery("t:pipeline"), { query: "", kind: "pipeline", hasType: true });
|
||||||
assert.deepEqual(parseSearchQuery(""), { query: "", kind: "", hasType: false });
|
assert.deepEqual(parseSearchQuery(""), { query: "", kind: "", hasType: false });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("blank text is sendable only with a supported nonempty kind", () => {
|
||||||
|
for (const { value } of SEARCH_KINDS) {
|
||||||
|
assert.equal(searchQuerySendable(" \n\t ", value), value !== "");
|
||||||
|
}
|
||||||
|
assert.equal(searchQuerySendable("", "unknown"), false);
|
||||||
|
assert.equal(searchQuerySendable("plan"), true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("kind-only responses distinguish loading, honest empty state and cleared filters", () => {
|
||||||
|
assert.equal(searchPanelBody({ loading: true, query: "" }, [], "pipeline").kind, "searching");
|
||||||
|
assert.deepEqual(searchPanelBody({ loaded: true, query: "" }, [], "pipeline"), {
|
||||||
|
kind: "empty", message: "No pipelines yet.",
|
||||||
|
});
|
||||||
|
assert.equal(searchPanelBody({ loaded: false, query: "" }, [], "pipeline").kind, "idle");
|
||||||
|
assert.equal(searchPanelBody({ loaded: true, query: "" }, [], "").kind, "idle");
|
||||||
|
assert.equal(searchPanelBody({ loaded: true, query: "", truncated: true }, [], "field").message,
|
||||||
|
"No fields found within the server's scan limit.");
|
||||||
|
});
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import { fileURLToPath } from "node:url";
|
|||||||
import { launchChrome, eventually } from "./browser.mjs";
|
import { launchChrome, eventually } from "./browser.mjs";
|
||||||
|
|
||||||
const root = fileURLToPath(new URL("../", import.meta.url));
|
const root = fileURLToPath(new URL("../", import.meta.url));
|
||||||
const out = resolve(process.argv[2] || "captures/search-types");
|
const out = resolve(process.argv[2] || "captures/search-types-recent");
|
||||||
await mkdir(out, { recursive: true });
|
await mkdir(out, { recursive: true });
|
||||||
const server = createServer(async (req, res) => {
|
const server = createServer(async (req, res) => {
|
||||||
try {
|
try {
|
||||||
@@ -39,7 +39,14 @@ try {
|
|||||||
window.framesSent = [];
|
window.framesSent = [];
|
||||||
const repo = new PipelinesRepository({ call: async (frame) => {
|
const repo = new PipelinesRepository({ call: async (frame) => {
|
||||||
framesSent.push(frame);
|
framesSent.push(frame);
|
||||||
return { search_results: { results: [] } };
|
const fixtures = {
|
||||||
|
pipeline: [{ kind: 'pipeline', target: 'pipeline:12', pipeline: { id: 12, name: 'Recent fixture pipeline', version: 1, stages: 2 } }],
|
||||||
|
artifact: [{ kind: 'artifact', target: '/artifacts/12/1/report.txt', artifact: { run_id: 12, sequence: 1, name: 'Recent fixture report.txt', url: '/artifacts/12/1/report.txt', content_type: 'text/plain', size_bytes: 20 } }],
|
||||||
|
};
|
||||||
|
return { search_results: {
|
||||||
|
results: frame.search.query === '' ? fixtures[frame.search.kind] || [] : [],
|
||||||
|
truncated: frame.search.query === '' && ['field', 'pipeline'].includes(frame.search.kind),
|
||||||
|
} };
|
||||||
}});
|
}});
|
||||||
const state = { searchQuery: '', searchKind: '', searchSort: 'relevance',
|
const state = { searchQuery: '', searchKind: '', searchSort: 'relevance',
|
||||||
searchResults: [], searchSequencer: new SearchSequencer(), searchActiveIndex: 0 };
|
searchResults: [], searchSequencer: new SearchSequencer(), searchActiveIndex: 0 };
|
||||||
@@ -69,17 +76,30 @@ try {
|
|||||||
assert.equal((await last()).search.query, '"t:run" t:unknown');
|
assert.equal((await last()).search.query, '"t:run" t:unknown');
|
||||||
await page.screenshot(`${out}/04-literal-tokens.png`);
|
await page.screenshot(`${out}/04-literal-tokens.png`);
|
||||||
|
|
||||||
const before = await page.evaluate("framesSent.length");
|
await search("t:pipeline");
|
||||||
await page.fill("#searchInput", "t:pipeline");
|
assert.deepEqual((await last()), { type: "search", search: { query: "", kind: "pipeline", limit: 0 } });
|
||||||
await eventually(() => page.evaluate("document.querySelector('#searchResults').textContent.includes('Add search text')"));
|
await eventually(() => page.evaluate("document.querySelector('#searchResults').textContent.includes('Recent fixture pipeline')"));
|
||||||
await new Promise((done) => setTimeout(done, 400));
|
await page.screenshot(`${out}/05-type-only-pipelines.png`);
|
||||||
assert.equal(await page.evaluate("framesSent.length"), before);
|
await search("type:ARTIFACT");
|
||||||
await page.screenshot(`${out}/05-type-only-help.png`);
|
assert.deepEqual((await last()).search, { query: "", kind: "artifact", limit: 0 });
|
||||||
|
await eventually(() => page.evaluate("document.querySelector('#searchResults').textContent.includes('Recent fixture report.txt')"));
|
||||||
|
await page.screenshot(`${out}/06-type-only-artifacts.png`);
|
||||||
|
|
||||||
|
await page.evaluate("document.querySelector('#searchKind').value = 'step'; document.querySelector('#searchKind').dispatchEvent(new Event('change'))");
|
||||||
|
await eventually(() => page.evaluate("document.querySelector('#searchResults').textContent === 'No steps yet.'"));
|
||||||
|
assert.deepEqual((await last()).search, { query: "", kind: "step", limit: 0 });
|
||||||
|
await page.screenshot(`${out}/07-empty-kind.png`);
|
||||||
|
await page.evaluate("document.querySelector('#searchKind').value = 'field'; document.querySelector('#searchKind').dispatchEvent(new Event('change'))");
|
||||||
|
await eventually(() => page.evaluate("document.querySelector('#searchResults').textContent.includes(\"No fields found within the server's scan limit.\")"));
|
||||||
|
await page.screenshot(`${out}/08-scan-bound-empty.png`);
|
||||||
|
const beforeClear = await page.evaluate("framesSent.length");
|
||||||
await page.click("#searchClearFilter");
|
await page.click("#searchClearFilter");
|
||||||
assert.equal(await page.evaluate("document.querySelector('#searchInput').value"), "");
|
assert.equal(await page.evaluate("document.querySelector('#searchInput').value"), "");
|
||||||
assert.equal(await page.evaluate("document.querySelector('#searchKind').value"), "");
|
assert.equal(await page.evaluate("document.querySelector('#searchKind').value"), "");
|
||||||
await page.screenshot(`${out}/06-cleared-filter.png`);
|
assert.equal(await page.evaluate("document.querySelector('#searchResults').textContent"), "");
|
||||||
console.log(`PASS: type tokens, wire kind/query, precedence, selector replacement, literals, type-only help and clear. Captures: ${out}`);
|
assert.equal(await page.evaluate("framesSent.length"), beforeClear);
|
||||||
|
await page.screenshot(`${out}/09-cleared-filter.png`);
|
||||||
|
console.log(`PASS: type tokens, wire kind/query, precedence, selector replacement, literals, kind-only results/empty state and clear. Captures: ${out}`);
|
||||||
} finally {
|
} finally {
|
||||||
await browser?.close();
|
await browser?.close();
|
||||||
await new Promise((done) => server.close(done));
|
await new Promise((done) => server.close(done));
|
||||||
|
|||||||
Reference in New Issue
Block a user