package server import ( "context" "sort" "strings" "time" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" ) // chatSearchBudget bounds how long one chat search waits for transcript // readback. Registry (title/directory) hits never wait on it; a transcript // still unread when it expires is left out and the reply says Truncated. // It is short because a client that serializes its requests (the app does) // holds its next request behind this reply. It is a variable so a test can // shorten it. var chatSearchBudget = time.Second // chatSearchConcurrency is how many transcripts one chat search reads at once. const chatSearchConcurrency = 4 // Chat hit scores record how a chat matched: title, directory or message // text. They describe the hit; results are ordered by recency alone. const ( chatScoreTitleExact = 4 chatScoreTitle = 3 chatScoreDirectory = 2 chatScoreMessage = 1 ) // isChatSearch reports a search request narrowed to chats. It is answered by // searchChats, off the connection's read loop, so it never delays the // run/artifact search a client sends beside it. func isChatSearch(request protocol.Message) bool { return request.Type == protocol.Search && request.Search != nil && strings.TrimSpace(request.Search.Kind) == protocol.SearchKindChat } // chatSearchReply answers one kind=chat search request. func chatSearchReply(ctx context.Context, request protocol.Message, broker chatBroker, registry operatorRegistry) protocol.Message { if registry == nil { return protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat search unavailable: operator session registry is not configured on this server"} } response, err := searchChats(ctx, *request.Search, broker, registry) if err != nil { return protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()} } return protocol.Message{Type: protocol.SearchResults, ID: request.ID, SearchResults: response} } // searchChats matches the operator's chat sessions: the title and directory of // every listed registry session (one bounded statement), then the newest // SearchChatMessagesPerChat messages of up to SearchMaxScanChats of the most // recently used sessions the registry did not already match, read through the // chat broker concurrently within chatSearchBudget. One session yields at most // one hit, its best match. Hits are ordered most recently used first before // the cap applies. A blank query lists the most recent chats. func searchChats(ctx context.Context, req protocol.SearchRequest, broker chatBroker, registry operatorRegistry) (*protocol.SearchResponse, error) { query := strings.TrimSpace(req.Query) response := &protocol.SearchResponse{ Query: req.Query, Kind: protocol.SearchKindChat, Results: []protocol.SearchResult{}, Cap: chatSearchLimit(req.Limit), ScanBoundBytes: protocol.SearchMaxScanBytes, } sessions, err := registry.OperatorSessions(ctx) if err != nil { return nil, err } var results []protocol.SearchResult var unmatched []pipeline.OperatorSession for _, session := range sessions { if query == "" { results = append(results, chatSearchHit(session, 0, "")) continue } switch { case strings.EqualFold(session.Title, query): results = append(results, chatSearchHit(session, chatScoreTitleExact, "title")) case containsFold(session.Title, query): results = append(results, chatSearchHit(session, chatScoreTitle, "title")) case containsFold(session.Directory, query): results = append(results, chatSearchHit(session, chatScoreDirectory, "directory")) default: unmatched = append(unmatched, session) } } if query != "" && broker != nil && len(unmatched) > 0 { if len(unmatched) > protocol.SearchMaxScanChats { unmatched = unmatched[:protocol.SearchMaxScanChats] } hits, scanned, complete := searchChatTranscripts(ctx, broker, unmatched, query) results = append(results, hits...) response.ScannedChats = scanned if !complete { response.Truncated = true } } sortChatSearchResults(results) if len(results) > response.Cap { results = results[:response.Cap] response.Truncated = true } if results != nil { response.Results = results } return response, nil } // sortChatSearchResults orders chat hits most recently used first, whatever // field matched, so the cap keeps the newest chats. Session id breaks ties. func sortChatSearchResults(results []protocol.SearchResult) { sort.SliceStable(results, func(i, j int) bool { a, b := results[i].Chat.UpdatedAt, results[j].Chat.UpdatedAt if a != b { return a > b } return results[i].Target < results[j].Target }) } // chatSearchLimit clamps a requested cap like the store's search does. func chatSearchLimit(limit int) int { if limit <= 0 { return protocol.SearchDefaultLimit } return min(limit, protocol.SearchMaxLimit) } // chatTranscriptHit is one transcript read's outcome. type chatTranscriptHit struct { hit *protocol.SearchResult scanned bool } // searchChatTranscripts reads each session's newest messages with bounded // concurrency and returns the message hits that arrived within the budget, // how many transcripts were read, and whether every read finished. Workers // that outlive the budget see a canceled context and write into a buffered // channel, so none of them blocks or leaks past its own read. func searchChatTranscripts(ctx context.Context, broker chatBroker, sessions []pipeline.OperatorSession, query string) ([]protocol.SearchResult, int, bool) { ctx, cancel := context.WithTimeout(ctx, chatSearchBudget) defer cancel() jobs := make(chan pipeline.OperatorSession, len(sessions)) for _, session := range sessions { jobs <- session } close(jobs) out := make(chan chatTranscriptHit, len(sessions)) for range min(chatSearchConcurrency, len(sessions)) { go func() { for session := range jobs { if ctx.Err() != nil { out <- chatTranscriptHit{} continue } out <- searchChatTranscript(ctx, broker, session, query) } }() } var hits []protocol.SearchResult scanned := 0 for range sessions { select { case result := <-out: if result.scanned { scanned++ } if result.hit != nil { hits = append(hits, *result.hit) } case <-ctx.Done(): return hits, scanned, false } } return hits, scanned, scanned == len(sessions) } // searchChatTranscript matches query against the text parts of one session's // newest messages, newest first, and returns a hit for the newest match. func searchChatTranscript(ctx context.Context, broker chatBroker, session pipeline.OperatorSession, query string) chatTranscriptHit { page, err := broker.Messages(ctx, session.ID, protocol.SearchChatMessagesPerChat, "") if err != nil || ctx.Err() != nil { return chatTranscriptHit{} } for i := len(page.Messages) - 1; i >= 0; i-- { message := page.Messages[i] for _, part := range message.Parts { if part.Type != protocol.ChatPartText || !containsFold(part.Text, query) { continue } hit := chatSearchHit(session, chatScoreMessage, "message") hit.Chat.MessageID = message.ID hit.Chat.Role = message.Role hit.Chat.Snippet = protocol.SearchSnippet(part.Text, query) return chatTranscriptHit{hit: &hit, scanned: true} } } return chatTranscriptHit{scanned: true} } func chatSearchHit(session pipeline.OperatorSession, score int, field string) protocol.SearchResult { backend := session.Backend if backend == "" { backend = "opencode" } hit := protocol.SearchResult{ Kind: protocol.SearchKindChat, Target: protocol.SearchChatTarget(session.ID), Score: score, MatchedField: field, Chat: &protocol.SearchChat{ SessionID: session.ID, Title: session.Title, Directory: session.Directory, Backend: backend, Closed: session.ClosedAt > 0, UpdatedAt: session.UpdatedAt, }, } if session.UpdatedAt > 0 { hit.Recency = time.UnixMilli(session.UpdatedAt).UTC().Format(time.RFC3339) } return hit } func containsFold(value, query string) bool { return query != "" && strings.Contains(strings.ToLower(value), strings.ToLower(query)) }