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:
@@ -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