Merge: search results newest first
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../data/search_repository.dart';
|
||||
import 'search_models.dart';
|
||||
import 'search_order.dart';
|
||||
import 'search_query.dart';
|
||||
|
||||
/// Wording for a server whose search does not know the `chat` kind (it
|
||||
@@ -51,9 +52,11 @@ class SearchChatSlice {
|
||||
truncated = false;
|
||||
error = unsupportedChatSearchCopy;
|
||||
} else {
|
||||
results = response.results
|
||||
.where((hit) => hit['kind'] == SearchKind.chat.wire)
|
||||
.toList(growable: false);
|
||||
results = searchHitsNewestFirst(
|
||||
response.results
|
||||
.where((hit) => hit['kind'] == SearchKind.chat.wire)
|
||||
.toList(growable: false),
|
||||
);
|
||||
truncated = response.truncated;
|
||||
}
|
||||
} catch (failure) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import '../../../core/api_gateway.dart';
|
||||
import '../data/search_repository.dart';
|
||||
import 'search_chat_slice.dart';
|
||||
import 'search_models.dart';
|
||||
import 'search_order.dart';
|
||||
import 'search_query.dart';
|
||||
import 'search_row.dart';
|
||||
|
||||
@@ -246,7 +247,7 @@ class OmniSearchController extends ChangeNotifier {
|
||||
_notify();
|
||||
return;
|
||||
}
|
||||
_results = response.results;
|
||||
_results = searchHitsNewestFirst(response.results);
|
||||
_truncated = response.truncated;
|
||||
_scannedSteps = response.scannedSteps;
|
||||
_scannedBytes = response.scannedBytes;
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
|
||||
/// Search hits newest first, the order the server sends them in. The app
|
||||
/// sorts again so each displayed group stays newest first even from an older
|
||||
/// server that ranked by match tier. Equal or missing dates keep the server's
|
||||
/// order; a hit without a date sorts last.
|
||||
List<JsonMap> searchHitsNewestFirst(List<JsonMap> hits) {
|
||||
final indexed = [
|
||||
for (var i = 0; i < hits.length; i++) (i, _hitMillis(hits[i]), hits[i]),
|
||||
];
|
||||
indexed.sort((a, b) {
|
||||
final byDate = b.$2.compareTo(a.$2);
|
||||
return byDate != 0 ? byDate : a.$1.compareTo(b.$1);
|
||||
});
|
||||
return [for (final entry in indexed) entry.$3];
|
||||
}
|
||||
|
||||
/// A chat hit's last-used time (`chat.updated_at`, epoch milliseconds), else
|
||||
/// any hit's `recency` timestamp; -1 when neither is present.
|
||||
int _hitMillis(JsonMap hit) {
|
||||
final chat = hit['chat'];
|
||||
if (chat is Map) {
|
||||
final updated = chat['updated_at'];
|
||||
if (updated is num && updated > 0) return updated.toInt();
|
||||
}
|
||||
final recency = DateTime.tryParse('${hit['recency'] ?? ''}');
|
||||
return recency?.millisecondsSinceEpoch ?? -1;
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/search/domain/search_order.dart';
|
||||
|
||||
import 'search_test_support.dart';
|
||||
|
||||
JsonMap _run(int id, String recency, {int score = 1}) => {
|
||||
'kind': 'run',
|
||||
'target': 'run:$id',
|
||||
'score': score,
|
||||
'recency': recency,
|
||||
'run': {'id': id},
|
||||
};
|
||||
|
||||
JsonMap _chat(String id, int updatedAt, {String? snippet}) {
|
||||
final hit = chatHit(id, snippet: snippet);
|
||||
return {
|
||||
...hit,
|
||||
'chat': <String, dynamic>{...hit['chat'] as Map, 'updated_at': updatedAt},
|
||||
};
|
||||
}
|
||||
|
||||
List<Object?> _targets(List<JsonMap> hits) => [
|
||||
for (final hit in hits) hit['target'],
|
||||
];
|
||||
|
||||
void main() {
|
||||
test('hits sort newest first whatever their score; ties and undated hits '
|
||||
'keep the server order, undated last', () {
|
||||
final sorted = searchHitsNewestFirst([
|
||||
_run(1, '2026-01-01T00:00:00Z', score: 3),
|
||||
{
|
||||
'kind': 'run',
|
||||
'target': 'run:9',
|
||||
'run': {'id': 9},
|
||||
},
|
||||
_run(2, '2026-03-01T00:00:00Z'),
|
||||
_run(3, '2026-02-01T00:00:00Z'),
|
||||
_run(4, '2026-02-01T00:00:00Z'),
|
||||
]);
|
||||
expect(_targets(sorted), ['run:2', 'run:3', 'run:4', 'run:1', 'run:9']);
|
||||
});
|
||||
|
||||
test('chat hits sort by last use, not by matched field', () {
|
||||
final sorted = searchHitsNewestFirst([
|
||||
_chat('title_old', 1000),
|
||||
_chat('message_new', 3000, snippet: 'flaky'),
|
||||
_chat('dir_mid', 2000),
|
||||
]);
|
||||
expect(_targets(sorted), [
|
||||
'chat:message_new',
|
||||
'chat:dir_mid',
|
||||
'chat:title_old',
|
||||
]);
|
||||
});
|
||||
|
||||
test('the controller shows both groups newest first', () async {
|
||||
final gateway = QueuedGateway(holdChats: true);
|
||||
final controller = controllerFor(gateway);
|
||||
addTearDown(controller.dispose);
|
||||
|
||||
controller.updateQuery('flaky');
|
||||
await pumpEventQueue();
|
||||
gateway.succeed(
|
||||
0,
|
||||
envelope([
|
||||
_run(1, '2026-01-01T00:00:00Z', score: 3),
|
||||
_run(2, '2026-05-01T00:00:00Z'),
|
||||
]),
|
||||
);
|
||||
gateway.chatPending.single.complete(
|
||||
envelope([_chat('a', 1000), _chat('b', 5000, snippet: 'flaky')]),
|
||||
);
|
||||
await pumpEventQueue();
|
||||
expect(_targets(controller.shownResults), ['run:2', 'run:1']);
|
||||
expect(_targets(controller.shownChats), ['chat:b', 'chat:a']);
|
||||
});
|
||||
}
|
||||
+9
-5
@@ -107,10 +107,12 @@ confirmation.
|
||||
- `--type` is the only narrowing mechanism and takes one kind: `pipeline`,
|
||||
`version`, `run`, `step`, `field` or `artifact`. An unknown kind is an error,
|
||||
never a silently ignored filter.
|
||||
- Ranking: an exact name match (score 3) beats a name substring (2) beats a
|
||||
body-text match (1); equal tiers break by recency, then a stable kind/target
|
||||
order. Every result carries `score`, `matched_field` and `recency` so a client
|
||||
can re-rank.
|
||||
- Order: results are newest first by `recency` (pipeline/version creation,
|
||||
run creation, step start), then numeric id and target, before the cap applies,
|
||||
so the cap drops the oldest hits; each kind's row scan also walks newest
|
||||
first. `score` still records the match tier (exact name 3, name substring 2,
|
||||
body text 1). Every result carries `score`, `matched_field` and `recency` so a
|
||||
client can re-rank.
|
||||
- Bounds: the result cap defaults to 50 and clamps to 200 (`0` = default);
|
||||
every kind scans at most 2000 rows, and field-text scanning stops at 2000 steps
|
||||
or 1 MiB decoded. `truncated` plus `scanned_steps`, `scanned_bytes`, `cap` and
|
||||
@@ -128,7 +130,9 @@ confirmation.
|
||||
reports how many transcripts were read. Each chat yields at most one hit
|
||||
(`target` `chat:<session id>`, `matched_field` `title`, `directory` or
|
||||
`message`) whose `chat` payload carries the title, directory, harness and,
|
||||
for a message match, the message id, role and a snippet. The server answers it
|
||||
for a message match, the message id, role and a snippet. Hits are ordered by
|
||||
the chat's last use, newest first, whatever field matched, before the cap
|
||||
applies. The server answers it
|
||||
off the connection's read loop, so it never delays other replies; a newer chat
|
||||
search on the same connection cancels the previous one. The app sends it after
|
||||
its run search and shows the hits as a Chats group.
|
||||
|
||||
+12
-39
@@ -90,10 +90,10 @@ func (s *Store) Search(ctx context.Context, req protocol.SearchRequest, now time
|
||||
for i := range results {
|
||||
results[i].Score = 0
|
||||
}
|
||||
sortRecentSearchResults(results)
|
||||
} else {
|
||||
sortSearchResults(results)
|
||||
}
|
||||
// Hits are ordered newest first before the cap, so the cap drops the
|
||||
// oldest; Score still records how each hit matched.
|
||||
sortRecentSearchResults(results)
|
||||
if len(results) > response.Cap {
|
||||
results = results[:response.Cap]
|
||||
response.Truncated = true
|
||||
@@ -220,24 +220,9 @@ func searchContains(value, text string) bool {
|
||||
return text != "" && strings.Contains(strings.ToLower(value), strings.ToLower(text))
|
||||
}
|
||||
|
||||
func sortSearchResults(results []protocol.SearchResult) {
|
||||
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
|
||||
}
|
||||
if a.Kind != b.Kind {
|
||||
return a.Kind < b.Kind
|
||||
}
|
||||
return a.Target < b.Target
|
||||
})
|
||||
}
|
||||
|
||||
// Browsing has no relevance score. Numeric entity IDs, then visit/version IDs,
|
||||
// break equal timestamps deterministically (not lexical "run:9" vs "run:10").
|
||||
// sortRecentSearchResults orders hits newest first by Recency, for a text
|
||||
// search and a browse alike. Numeric entity IDs, then visit/version IDs, break
|
||||
// equal timestamps deterministically (not lexical "run:9" vs "run:10").
|
||||
func sortRecentSearchResults(results []protocol.SearchResult) {
|
||||
identity := func(hit protocol.SearchResult) (int64, int) {
|
||||
switch {
|
||||
@@ -284,7 +269,7 @@ WHERE v.version=(SELECT MAX(version) FROM pipeline_versions WHERE pipeline_id=p.
|
||||
if terms.Browse {
|
||||
query += " ORDER BY v.created_at DESC,v.pipeline_id DESC,v.version DESC LIMIT ?"
|
||||
} else {
|
||||
query += ` AND lower(p.name) LIKE ? ESCAPE '\' ORDER BY p.name,p.id LIMIT ?`
|
||||
query += ` AND lower(p.name) LIKE ? ESCAPE '\' ORDER BY v.created_at DESC,v.pipeline_id DESC LIMIT ?`
|
||||
args = append(args, searchLike(terms.Text))
|
||||
}
|
||||
args = append(args, searchCandidateLimit(terms))
|
||||
@@ -345,11 +330,7 @@ FROM pipeline_versions v`
|
||||
args = append(args, n)
|
||||
wanted, numeric = n, true
|
||||
}
|
||||
if terms.Browse {
|
||||
query += " ORDER BY v.created_at DESC,v.pipeline_id DESC,v.version DESC LIMIT ?"
|
||||
} else {
|
||||
query += " ORDER BY v.pipeline_id,v.version DESC LIMIT ?"
|
||||
}
|
||||
query += " ORDER BY v.created_at DESC,v.pipeline_id DESC,v.version DESC LIMIT ?"
|
||||
args = append(args, searchCandidateLimit(terms))
|
||||
rows, err := tx.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
@@ -407,11 +388,7 @@ func searchRuns(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *search
|
||||
pattern := searchLike(terms.Text)
|
||||
args = append(args, pattern, pattern, pattern, pattern, terms.Text)
|
||||
}
|
||||
if terms.Browse {
|
||||
query += " ORDER BY r.created_at DESC,r.id DESC LIMIT ?"
|
||||
} else {
|
||||
query += " ORDER BY r.id DESC LIMIT ?"
|
||||
}
|
||||
query += " ORDER BY r.created_at DESC,r.id DESC LIMIT ?"
|
||||
args = append(args, searchCandidateLimit(terms))
|
||||
rows, err := tx.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
@@ -474,7 +451,7 @@ func searchSteps(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *searc
|
||||
if terms.Browse {
|
||||
query += " ORDER BY s.started_at DESC,s.run_id DESC,s.sequence DESC LIMIT ?"
|
||||
} else {
|
||||
query += ` WHERE lower(s.stage_key) LIKE ? ESCAPE '\' OR lower(s.outcome) LIKE ? ESCAPE '\' OR lower(s.target) LIKE ? ESCAPE '\' OR lower(s.diagnostic) LIKE ? ESCAPE '\' ORDER BY s.run_id DESC,s.sequence DESC LIMIT ?`
|
||||
query += ` WHERE lower(s.stage_key) LIKE ? ESCAPE '\' OR lower(s.outcome) LIKE ? ESCAPE '\' OR lower(s.target) LIKE ? ESCAPE '\' OR lower(s.diagnostic) LIKE ? ESCAPE '\' ORDER BY s.started_at DESC,s.run_id DESC,s.sequence DESC LIMIT ?`
|
||||
pattern := searchLike(terms.Text)
|
||||
args = append(args, pattern, pattern, pattern, pattern)
|
||||
}
|
||||
@@ -543,11 +520,7 @@ func searchFields(ctx context.Context, tx *sql.Tx, terms searchTerms, scan *sear
|
||||
// One extra step is requested so a scan that hits the step bound can see that
|
||||
// more rows remain and report Truncated rather than ending silently.
|
||||
query := `SELECT s.run_id,s.sequence,s.input_json,s.output_json,s.transcript,` + searchStepEvidence + `,` + searchStepRecency + ` ` + searchStepJoin
|
||||
if terms.Browse {
|
||||
query += " ORDER BY s.started_at DESC,s.run_id DESC,s.sequence DESC LIMIT ?"
|
||||
} else {
|
||||
query += " ORDER BY s.run_id DESC,s.sequence DESC LIMIT ?"
|
||||
}
|
||||
query += " ORDER BY s.started_at DESC,s.run_id DESC,s.sequence DESC LIMIT ?"
|
||||
rows, err := tx.QueryContext(ctx, query, protocol.SearchMaxScanSteps+1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -622,7 +595,7 @@ FROM step_artifacts a JOIN pipeline_runs r ON r.id=a.run_id`
|
||||
FROM pipeline_runs r CROSS JOIN step_artifacts a ON a.run_id=r.id
|
||||
ORDER BY r.created_at DESC,r.id DESC,a.sequence DESC LIMIT ?`
|
||||
} else {
|
||||
query += ` WHERE lower(a.artifacts_json) LIKE ? ESCAPE '\' ORDER BY a.run_id DESC,a.sequence DESC LIMIT ?`
|
||||
query += ` WHERE lower(a.artifacts_json) LIKE ? ESCAPE '\' ORDER BY r.created_at DESC,r.id DESC,a.sequence DESC LIMIT ?`
|
||||
args = append(args, searchLike(terms.Text))
|
||||
}
|
||||
// Metadata rows can contain several artifacts or malformed retained JSON.
|
||||
|
||||
@@ -205,16 +205,14 @@ func TestSearchRankingExactSubstringBodyAndRecency(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
seen := map[int]bool{}
|
||||
for i, hit := range response.Results {
|
||||
for _, hit := range response.Results {
|
||||
seen[hit.Score] = true
|
||||
if i > 0 && response.Results[i-1].Score < hit.Score {
|
||||
t.Fatalf("scores not descending: %+v", response.Results)
|
||||
}
|
||||
}
|
||||
assertNewestFirst(t, response.Results)
|
||||
if !seen[3] || !seen[2] || !seen[1] {
|
||||
t.Fatalf("expected exact/substring/body tiers: %+v", response.Results)
|
||||
}
|
||||
// The exact pipeline match outranks the substring pipeline match.
|
||||
// Scores still record how a pipeline name matched.
|
||||
exactIndex, substringIndex := -1, -1
|
||||
for i, hit := range response.Results {
|
||||
if hit.Pipeline == nil {
|
||||
@@ -233,11 +231,11 @@ func TestSearchRankingExactSubstringBodyAndRecency(t *testing.T) {
|
||||
t.Fatalf("substring name scored %d", hit.Score)
|
||||
}
|
||||
}
|
||||
if exactIndex < 0 || substringIndex < 0 || exactIndex > substringIndex {
|
||||
t.Fatalf("exact name must outrank a name substring: %+v", response.Results)
|
||||
if exactIndex < 0 || substringIndex < 0 {
|
||||
t.Fatalf("both pipeline names must match: %+v", response.Results)
|
||||
}
|
||||
|
||||
// Equal-tier ties break by recency.
|
||||
// Hits order by recency whatever their tier.
|
||||
older, _, err := store.CreateRun(ctx, definition, "alpha-older", "/work", false, nil, map[string]any{"prompt": "alpha"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -265,7 +263,71 @@ func TestSearchRankingExactSubstringBodyAndRecency(t *testing.T) {
|
||||
}
|
||||
}
|
||||
if olderIndex < 0 || newerIndex < 0 || newerIndex > olderIndex {
|
||||
t.Fatalf("recency tie-break lost: older=%d newer=%d", olderIndex, newerIndex)
|
||||
t.Fatalf("recency order lost: older=%d newer=%d", olderIndex, newerIndex)
|
||||
}
|
||||
assertNewestFirst(t, response.Results)
|
||||
}
|
||||
|
||||
func assertNewestFirst(t *testing.T, results []protocol.SearchResult) {
|
||||
t.Helper()
|
||||
for i := 1; i < len(results); i++ {
|
||||
if results[i-1].Recency < results[i].Recency {
|
||||
t.Fatalf("results not newest first at %d: %+v", i, results)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestSearchOrdersByDateAndCapKeepsNewest: an exact pipeline-name match on an
|
||||
// old pipeline sorts below a newer run's body match, and a cap keeps the
|
||||
// newest hits rather than the best-scored ones.
|
||||
func TestSearchOrdersByDateAndCapKeepsNewest(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(filepath.Join(t.TempDir(), "order.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
if _, err := store.Submit(ctx, "gamma", Template([]string{"one"}, true)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, _ := store.List(ctx)
|
||||
definition, _ := store.Get(ctx, items[0].ID, 1)
|
||||
if _, err := store.db.Exec("UPDATE pipeline_versions SET created_at='2020-01-01 00:00:00'"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var runs []int64
|
||||
for i, created := range []string{"2026-03-01 00:00:00", "2026-01-01 00:00:00", "2026-02-01 00:00:00"} {
|
||||
run, _, err := store.CreateRun(ctx, definition, "key-"+strconv.Itoa(i), "/work/gamma-"+strconv.Itoa(i), false, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
setCreated(t, store, run, created)
|
||||
runs = append(runs, run)
|
||||
}
|
||||
response, err := store.Search(ctx, protocol.SearchRequest{Query: "gamma", Kind: protocol.SearchKindRun}, time.Now())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(response.Results) != 3 || response.Results[0].Run.ID != runs[0] || response.Results[1].Run.ID != runs[2] || response.Results[2].Run.ID != runs[1] {
|
||||
t.Fatalf("runs = %+v, want created newest first", response.Results)
|
||||
}
|
||||
|
||||
all, err := store.Search(ctx, protocol.SearchRequest{Query: "gamma"}, time.Now())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertNewestFirst(t, all.Results)
|
||||
last := all.Results[len(all.Results)-1]
|
||||
if last.Pipeline == nil || last.Score != 3 {
|
||||
t.Fatalf("oldest hit = %+v, want the exact-name pipeline last despite its score", last)
|
||||
}
|
||||
|
||||
capped, err := store.Search(ctx, protocol.SearchRequest{Query: "gamma", Limit: 2}, time.Now())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !capped.Truncated || len(capped.Results) != 2 || capped.Results[0].Run.ID != runs[0] || capped.Results[1].Run.ID != runs[2] {
|
||||
t.Fatalf("capped = %+v, want the two newest runs", capped.Results)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -82,10 +82,10 @@ type SearchRequest struct {
|
||||
Limit int `json:"limit,omitempty"`
|
||||
}
|
||||
|
||||
// SearchResponse is the ranked, bounded set of matches. Results are ordered by
|
||||
// score, then recency, then a stable kind/target tie-break.
|
||||
// For kind-only browsing, order is recency descending, numeric entity/visit IDs
|
||||
// descending, then target ascending, without a text-relevance score.
|
||||
// SearchResponse is the bounded set of matches. Results are ordered by recency
|
||||
// descending (a chat's last use for kind chat), numeric entity/visit IDs
|
||||
// descending, then target ascending, before the cap applies; Score records the
|
||||
// match tier. Kind-only browsing carries no text-relevance score.
|
||||
// Truncated reports that either the result cap or the field-scan budget dropped matches;
|
||||
// ScannedSteps/ScannedBytes and ScanBoundBytes make the body-scan bound
|
||||
// observable.
|
||||
|
||||
@@ -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