6ca415c134
Chat hits sort by the chat's last use whatever field matched, and run, step, field, artifact and pipeline hits sort by their own date, before the result cap applies so the cap drops the oldest. Each kind's row scan also walks newest first. Score still reports the match tier. The app sorts both groups newest first as well.
231 lines
9.6 KiB
Go
231 lines
9.6 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/coder/websocket"
|
|
"github.com/coder/websocket/wsjson"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"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 chatHitsByTarget(results []protocol.SearchResult) map[string]protocol.SearchResult {
|
|
hits := map[string]protocol.SearchResult{}
|
|
for _, hit := range results {
|
|
hits[hit.Target] = hit
|
|
}
|
|
return hits
|
|
}
|
|
|
|
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)
|
|
}
|
|
hits := chatHitsByTarget(got.Results)
|
|
title, dir, text := hits["chat:ses_title"], hits["chat:ses_dir"], hits["chat:ses_text"]
|
|
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 _, ok := chatHitsByTarget(got.Results)["chat:ses_text"]; !ok {
|
|
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)
|
|
}
|
|
}
|
|
|
|
// listedSessions is an operator registry that lists fixed sessions, newest
|
|
// use first like the store. Search uses no other registry method.
|
|
type listedSessions struct {
|
|
operatorRegistry
|
|
sessions []pipeline.OperatorSession
|
|
}
|
|
|
|
func (r listedSessions) OperatorSessions(context.Context) ([]pipeline.OperatorSession, error) {
|
|
return r.sessions, nil
|
|
}
|
|
|
|
// TestChatSearchOrdersByLastUsedAndCapKeepsNewest: hits are ordered by the
|
|
// chat's last-used time whatever field matched, so a stale title match sorts
|
|
// below fresher message matches and is what the cap drops.
|
|
func TestChatSearchOrdersByLastUsedAndCapKeepsNewest(t *testing.T) {
|
|
registry := listedSessions{sessions: []pipeline.OperatorSession{
|
|
{ID: "ses_msg_new", Title: "Build work", Directory: "/work/a", UpdatedAt: 5000},
|
|
{ID: "ses_dir", Title: "Docs", Directory: "/work/flaky", UpdatedAt: 4000},
|
|
{ID: "ses_msg_old", Title: "Other", Directory: "/work/b", UpdatedAt: 3000},
|
|
{ID: "ses_none", Title: "Nothing", Directory: "/work/c", UpdatedAt: 2000},
|
|
{ID: "ses_title", Title: "flaky", Directory: "/work/d", UpdatedAt: 1000},
|
|
}}
|
|
broker := &transcriptChat{fakeChat: scriptedChat(), pages: map[string][]protocol.ChatMessage{
|
|
"ses_msg_new": {chatText("m1", protocol.ChatRoleUser, "flaky build")},
|
|
"ses_msg_old": {chatText("m2", protocol.ChatRoleUser, "flaky again")},
|
|
"ses_none": {chatText("m3", protocol.ChatRoleUser, "unrelated")},
|
|
}}
|
|
ctx := context.Background()
|
|
got, err := searchChats(ctx, protocol.SearchRequest{Query: "flaky", Kind: protocol.SearchKindChat}, broker, registry)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := []string{"chat:ses_msg_new", "chat:ses_dir", "chat:ses_msg_old", "chat:ses_title"}
|
|
if len(got.Results) != len(want) {
|
|
t.Fatalf("results = %+v, want %v", got.Results, want)
|
|
}
|
|
for i, target := range want {
|
|
if got.Results[i].Target != target {
|
|
t.Fatalf("result %d = %s (%s), want %s: order is last used first", i, got.Results[i].Target, got.Results[i].MatchedField, target)
|
|
}
|
|
}
|
|
if got.Results[3].MatchedField != "title" || got.Results[3].Score != chatScoreTitleExact {
|
|
t.Fatalf("stale title hit = %+v, want its title match still reported", got.Results[3])
|
|
}
|
|
|
|
capped, err := searchChats(ctx, protocol.SearchRequest{Query: "flaky", Kind: protocol.SearchKindChat, Limit: 2}, broker, registry)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !capped.Truncated || len(capped.Results) != 2 || capped.Results[0].Target != "chat:ses_msg_new" || capped.Results[1].Target != "chat:ses_dir" {
|
|
t.Fatalf("capped = %+v, want the two most recently used hits and Truncated", capped.Results)
|
|
}
|
|
}
|