Search: order results newest first in every group
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.
This commit is contained in:
@@ -21,8 +21,8 @@ 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.
|
||||
// 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
|
||||
@@ -55,7 +55,8 @@ func chatSearchReply(ctx context.Context, request protocol.Message, broker chatB
|
||||
// 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.
|
||||
// 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{
|
||||
@@ -98,16 +99,7 @@ func searchChats(ctx context.Context, req protocol.SearchRequest, broker chatBro
|
||||
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
|
||||
})
|
||||
sortChatSearchResults(results)
|
||||
if len(results) > response.Cap {
|
||||
results = results[:response.Cap]
|
||||
response.Truncated = true
|
||||
@@ -118,6 +110,18 @@ func searchChats(ctx context.Context, req protocol.SearchRequest, broker chatBro
|
||||
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 {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"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"
|
||||
)
|
||||
|
||||
@@ -76,6 +77,14 @@ func readReply(t *testing.T, ctx context.Context, conn *websocket.Conn, id strin
|
||||
}
|
||||
}
|
||||
|
||||
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": {
|
||||
@@ -99,7 +108,8 @@ func TestChatSearchMatchesTitleDirectoryAndMessageText(t *testing.T) {
|
||||
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]
|
||||
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)
|
||||
}
|
||||
@@ -144,7 +154,7 @@ func TestChatSearchBudgetNeverBlocksOtherReplies(t *testing.T) {
|
||||
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" {
|
||||
if _, ok := chatHitsByTarget(got.Results)["chat:ses_text"]; !ok {
|
||||
t.Fatalf("message hit missing: %+v", got.Results)
|
||||
}
|
||||
}
|
||||
@@ -164,3 +174,57 @@ func TestChatSearchBlankListsRecentAndNeedsRegistry(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user