Merge: search finds chats and opens them
This commit is contained in:
@@ -0,0 +1,228 @@
|
||||
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: a title match outranks a directory match, which outranks a
|
||||
// message-text match; recency breaks ties.
|
||||
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. 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
|
||||
}
|
||||
}
|
||||
sort.SliceStable(results, func(i, j int) bool {
|
||||
a, b := results[i], results[j]
|
||||
if a.Score != b.Score {
|
||||
return a.Score > b.Score
|
||||
}
|
||||
if a.Recency != b.Recency {
|
||||
return a.Recency > b.Recency
|
||||
}
|
||||
return a.Target < b.Target
|
||||
})
|
||||
if len(results) > response.Cap {
|
||||
results = results[:response.Cap]
|
||||
response.Truncated = true
|
||||
}
|
||||
if results != nil {
|
||||
response.Results = results
|
||||
}
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// 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))
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
"github.com/coder/websocket/wsjson"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// transcriptChat serves a fixed newest page per session. A session listed in
|
||||
// slow blocks until its read is canceled, like a harness that never answers.
|
||||
type transcriptChat struct {
|
||||
*fakeChat
|
||||
pages map[string][]protocol.ChatMessage
|
||||
slow map[string]bool
|
||||
}
|
||||
|
||||
func (c *transcriptChat) Messages(ctx context.Context, sessionID string, limit int, _ string) (protocol.ChatHistoryPage, error) {
|
||||
if c.slow[sessionID] {
|
||||
<-ctx.Done()
|
||||
return protocol.ChatHistoryPage{}, ctx.Err()
|
||||
}
|
||||
if limit != protocol.SearchChatMessagesPerChat {
|
||||
return protocol.ChatHistoryPage{}, context.Canceled
|
||||
}
|
||||
return protocol.ChatHistoryPage{Messages: c.pages[sessionID]}, nil
|
||||
}
|
||||
|
||||
func chatText(id, role, text string) protocol.ChatMessage {
|
||||
return protocol.ChatMessage{ID: id, Role: role, Parts: []protocol.ChatPart{{Type: protocol.ChatPartText, Text: text}}}
|
||||
}
|
||||
|
||||
func chatSearchFixture(t *testing.T, broker *transcriptChat) (context.Context, *websocket.Conn) {
|
||||
t.Helper()
|
||||
store := newOperatorStore(t)
|
||||
ctx, _, conn := dialChatOptions(t, Options{Chat: broker, OperatorRegistry: store})
|
||||
rows := []struct{ id, directory, title, backend string }{
|
||||
{"ses_title", "/work/site", "Flaky test hunt", "claude"},
|
||||
{"ses_dir", "/work/flaky-repo", "Docs pass", "codex"},
|
||||
{"ses_text", "/work/api", "API refactor", ""},
|
||||
{"ses_none", "/work/other", "Unrelated", ""},
|
||||
{"ses_slow", "/work/slow", "Slow harness", ""},
|
||||
}
|
||||
for _, row := range rows {
|
||||
if err := store.RecordOperatorSession(ctx, row.id, row.directory, row.title, protocol.ChatSessionTagOperator); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if row.backend != "" {
|
||||
if err := store.SetOperatorSessionBackend(ctx, row.id, row.backend); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return ctx, conn
|
||||
}
|
||||
|
||||
func chatSearch(t *testing.T, ctx context.Context, conn *websocket.Conn, id, query string) {
|
||||
t.Helper()
|
||||
writeChat(t, ctx, conn, protocol.Message{Type: protocol.Search, ID: id, Search: &protocol.SearchRequest{Query: query, Kind: protocol.SearchKindChat}})
|
||||
}
|
||||
|
||||
func readReply(t *testing.T, ctx context.Context, conn *websocket.Conn, id string) protocol.Message {
|
||||
t.Helper()
|
||||
for {
|
||||
var reply protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &reply); err != nil {
|
||||
t.Fatalf("read reply %s: %v", id, err)
|
||||
}
|
||||
if reply.ID == id {
|
||||
return reply
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatSearchMatchesTitleDirectoryAndMessageText(t *testing.T) {
|
||||
broker := &transcriptChat{fakeChat: scriptedChat(), pages: map[string][]protocol.ChatMessage{
|
||||
"ses_text": {
|
||||
chatText("m1", protocol.ChatRoleUser, "Why is the build flaky?"),
|
||||
chatText("m2", protocol.ChatRoleAssistant, "The FLAKY part is the retry loop in client.go."),
|
||||
{ID: "m3", Role: protocol.ChatRoleAssistant, Parts: []protocol.ChatPart{{Type: protocol.ChatPartReasoning, Text: "flaky reasoning is not searched"}}},
|
||||
},
|
||||
"ses_none": {chatText("n1", protocol.ChatRoleUser, "nothing relevant")},
|
||||
"ses_title": {chatText("t1", protocol.ChatRoleUser, "flaky again")},
|
||||
}}
|
||||
ctx, conn := chatSearchFixture(t, broker)
|
||||
chatSearch(t, ctx, conn, "c1", "flaky")
|
||||
reply := readReply(t, ctx, conn, "c1")
|
||||
if reply.Type != protocol.SearchResults || reply.SearchResults == nil {
|
||||
t.Fatalf("reply = %+v", reply)
|
||||
}
|
||||
got := reply.SearchResults
|
||||
if got.Kind != protocol.SearchKindChat || got.Truncated || got.ScannedChats != 3 {
|
||||
t.Fatalf("bounds = kind %q truncated %v scanned %d, want chat, complete, 3 transcripts (title/dir hits skip readback)", got.Kind, got.Truncated, got.ScannedChats)
|
||||
}
|
||||
if len(got.Results) != 3 {
|
||||
t.Fatalf("results = %+v, want title, directory and message hits", got.Results)
|
||||
}
|
||||
title, dir, text := got.Results[0], got.Results[1], got.Results[2]
|
||||
if title.Target != "chat:ses_title" || title.MatchedField != "title" || title.Chat.Backend != "claude" || title.Chat.Snippet != "" {
|
||||
t.Fatalf("title hit = %+v %+v", title, title.Chat)
|
||||
}
|
||||
if dir.Target != "chat:ses_dir" || dir.MatchedField != "directory" || dir.Chat.Directory != "/work/flaky-repo" || dir.Chat.Backend != "codex" {
|
||||
t.Fatalf("directory hit = %+v %+v", dir, dir.Chat)
|
||||
}
|
||||
if text.Target != "chat:ses_text" || text.MatchedField != "message" || text.Chat.MessageID != "m2" || text.Chat.Role != protocol.ChatRoleAssistant || text.Chat.Backend != "opencode" {
|
||||
t.Fatalf("message hit = %+v %+v (want the newest matching text part)", text, text.Chat)
|
||||
}
|
||||
if text.Chat.Snippet != "The FLAKY part is the retry loop in client.go." || text.Chat.Title != "API refactor" || text.Recency == "" {
|
||||
t.Fatalf("message hit facts = %+v", text.Chat)
|
||||
}
|
||||
}
|
||||
|
||||
// TestChatSearchBudgetNeverBlocksOtherReplies: a harness that never answers
|
||||
// only costs its own transcript. The connection keeps answering other requests
|
||||
// while the chat search waits, and the chat reply still arrives within the
|
||||
// budget with the registry and finished-transcript hits, marked Truncated.
|
||||
func TestChatSearchBudgetNeverBlocksOtherReplies(t *testing.T) {
|
||||
previous := chatSearchBudget
|
||||
chatSearchBudget = 400 * time.Millisecond
|
||||
t.Cleanup(func() { chatSearchBudget = previous })
|
||||
broker := &transcriptChat{fakeChat: scriptedChat(), slow: map[string]bool{"ses_slow": true}, pages: map[string][]protocol.ChatMessage{
|
||||
"ses_text": {chatText("m1", protocol.ChatRoleUser, "a flaky retry")},
|
||||
}}
|
||||
ctx, conn := chatSearchFixture(t, broker)
|
||||
started := time.Now()
|
||||
chatSearch(t, ctx, conn, "c1", "flaky")
|
||||
writeChat(t, ctx, conn, protocol.Message{Type: protocol.Ping, ID: "p1"})
|
||||
var first protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &first); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if first.ID != "p1" {
|
||||
t.Fatalf("first reply = %s %s, want the ping answered while transcripts load", first.ID, first.Type)
|
||||
}
|
||||
reply := readReply(t, ctx, conn, "c1")
|
||||
if elapsed := time.Since(started); elapsed > 3*time.Second {
|
||||
t.Fatalf("chat search took %v, budget is %v", elapsed, chatSearchBudget)
|
||||
}
|
||||
got := reply.SearchResults
|
||||
if got == nil || !got.Truncated || len(got.Results) != 3 {
|
||||
t.Fatalf("reply = %+v, want three hits and Truncated", got)
|
||||
}
|
||||
if got.Results[2].Target != "chat:ses_text" {
|
||||
t.Fatalf("message hit missing: %+v", got.Results)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatSearchBlankListsRecentAndNeedsRegistry(t *testing.T) {
|
||||
broker := &transcriptChat{fakeChat: scriptedChat()}
|
||||
ctx, conn := chatSearchFixture(t, broker)
|
||||
chatSearch(t, ctx, conn, "b1", " ")
|
||||
reply := readReply(t, ctx, conn, "b1")
|
||||
if reply.SearchResults == nil || len(reply.SearchResults.Results) != 5 || reply.SearchResults.ScannedChats != 0 {
|
||||
t.Fatalf("blank chat search = %+v, want every listed chat and no transcript reads", reply.SearchResults)
|
||||
}
|
||||
|
||||
ctx, _, bare := dialChatOptions(t, Options{Chat: broker})
|
||||
chatSearch(t, ctx, bare, "n1", "x")
|
||||
if reply := readReply(t, ctx, bare, "n1"); reply.Type != protocol.Error {
|
||||
t.Fatalf("no-registry chat search = %+v, want an error", reply)
|
||||
}
|
||||
}
|
||||
@@ -217,6 +217,11 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
||||
if notes != nil {
|
||||
defer notes.detach(clientID)
|
||||
}
|
||||
// A chat search runs beside the read loop so its transcript readback
|
||||
// never delays the run search sent with it; a newer one cancels the
|
||||
// previous, whose reply the client would discard as stale anyway.
|
||||
cancelChatSearch := func() {}
|
||||
defer func() { cancelChatSearch() }()
|
||||
for {
|
||||
var request protocol.Message
|
||||
if err := wsjson.Read(connCtx, conn, &request); err != nil {
|
||||
@@ -227,6 +232,16 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "pipeline authoring is disabled on this execution server; ask the operator to publish through an explicitly enabled authoring server"})
|
||||
continue
|
||||
}
|
||||
if isChatSearch(request) {
|
||||
cancelChatSearch()
|
||||
searchCtx, cancel := context.WithCancel(connCtx)
|
||||
cancelChatSearch = cancel
|
||||
go func(request protocol.Message) {
|
||||
defer cancel()
|
||||
_ = write(chatSearchReply(searchCtx, request, options.Chat, registry))
|
||||
}(request)
|
||||
continue
|
||||
}
|
||||
if handleChat(connCtx, request, options.Chat, hub, registry, clientID, write) {
|
||||
continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user