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: , Search: } 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" // SearchKindChat narrows to operator chat sessions. It is not part of // SearchKinds: the store's all-kind search never includes chats, so an // older client's reply is unchanged. A client asks for chats with this // kind as its own request, which the server answers concurrently with // (and never ahead of or blocking) the run/artifact search. SearchKindChat = "chat" ) // 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 // SearchMaxScanChats bounds how many of the most recently used operator // chats one chat search reads message text from; titles and directories // of every listed chat are always matched. SearchMaxScanChats = 24 // SearchChatMessagesPerChat is how many of a chat's newest messages one // chat search reads. SearchChatMessagesPerChat = 100 // 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). // 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 { Query string `json:"query"` Kind string `json:"kind,omitempty"` Limit int `json:"limit,omitempty"` // IncludeDebug also returns runner-internal debugging artifacts (turn files // and private/ scratch, see ClassifyArtifacts). Off by default: artifact // search surfaces only deliverables. IncludeDebug bool `json:"include_debug,omitempty"` } // SearchResponse is the bounded set of matches. Results are ordered by recency // descending (a chat's last use for kind chat), numeric entity/visit IDs // descending, then target ascending, before the cap applies; Score records the // match tier. Kind-only browsing carries no 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 // 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"` // ScannedChats is how many chat transcripts a chat search read message // text from (kind chat only); additive, so older peers ignore it. ScannedChats int `json:"scanned_chats,omitempty"` } // SearchResult is one typed, navigable match. Kind selects which payload is // set; Target is a stable token an existing CLI command can resolve // (pipeline:, pipeline::v, run:, run::, // run:::, an artifact URL, or chat:). 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"` Chat *SearchChat `json:"chat,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"` // Group is the artifact's presentation group (see StageArtifact.Group). Group string `json:"group,omitempty"` } // SearchChat is an operator chat session hit: its registry facts and, for a // message-text match, the matching message's id, role and a snippet around // the first match. MatchedField is "title", "directory" or "message". type SearchChat struct { SessionID string `json:"session_id"` Title string `json:"title"` Directory string `json:"directory,omitempty"` Backend string `json:"backend,omitempty"` Closed bool `json:"closed,omitempty"` UpdatedAt int64 `json:"updated_at,omitempty"` MessageID string `json:"message_id,omitempty"` Role string `json:"role,omitempty"` Snippet string `json:"snippet,omitempty"` } // SearchChatTarget is a chat hit's target token. func SearchChatTarget(sessionID string) string { return "chat:" + sessionID } // 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) } case SearchKindChat: if r.Chat != nil { label := "chat · " + r.Chat.Title if r.Chat.Directory != "" { label += " · " + r.Chat.Directory } return label } } return r.Target } // 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. Text with no match is // truncated from its start. func SearchSnippet(text, query string) string { normalized := NormalizeText(text) runes := []rune(normalized) index := RuneIndexFold(runes, []rune(query)) if index < 0 { if len(runes) <= 80 { return normalized } return string(runes[:80]) + "…" } const margin = 40 start := max(index-margin, 0) end := min(index+len([]rune(query))+margin, len(runes)) snippet := string(runes[start:end]) if start > 0 { snippet = "…" + snippet } if end < len(runes) { snippet = snippet + "…" } return snippet } // 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 }