Skip to content

The agent that grows with you

fix(desktop): drop stale server search hits while a new query is pending

Konstantin Khlopkovaca184382321
diff --git a/apps/desktop/src/app/chat/sidebar/index.tsx b/apps/desktop/src/app/chat/sidebar/index.tsx
index 71fb3a85a86..2f85d6490fb 100644
--- a/apps/desktop/src/app/chat/sidebar/index.tsx
+++ b/apps/desktop/src/app/chat/sidebar/index.tsx
@@ -315,18 +315,21 @@ export function mergeSearchResults(
}
// While the request is in flight the client's own recency-ordered matches
- // are all there is — instant feedback while typing. Once the ranked server
+ // are all there is — instant feedback while typing, and no leftovers from
+ // whatever the previous query's request returned. Once the ranked server
// response lands, it decides the order: the backend runs direct id matches
// before FTS content hits, so pasting a session's exact id must keep that
// hit on top instead of letting newer quoting sessions bury it.
const out = new Map<string, SessionInfo>()
- if (searchPending || serverMatches.length === 0) {
+ if (searchPending) {
for (const s of sortedSessions) {
if (sessionMatchesSearch(s, query)) {
out.set(s.id, s)
}
}
+
+ return [...out.values()]
}
for (const match of serverMatches) {
diff --git a/apps/desktop/src/app/chat/sidebar/search-results-order.test.ts b/apps/desktop/src/app/chat/sidebar/search-results-order.test.ts
index fe7ec556842..d3756eaf1ad 100644
--- a/apps/desktop/src/app/chat/sidebar/search-results-order.test.ts
+++ b/apps/desktop/src/app/chat/sidebar/search-results-order.test.ts
@@ -70,6 +70,22 @@ describe('mergeSearchResults', () => {
expect(merged.map(session => session.id)).toEqual([quotingSession.id, TARGET_ID])
})
+ it("drops the previous query's server hits while the next request is in flight", () => {
+ // The previous query landed a server-only row for the target; the user
+ // then kept typing. Until the new response arrives, only the new query's
+ // client matches may render — a fast typist could otherwise click a
+ // result the search box no longer matches.
+ const merged = mergeSearchResults(
+ [quotingSession, targetSession],
+ 'Handoff',
+ [idHit],
+ indexByAnyId([quotingSession, targetSession]),
+ true
+ )
+
+ expect(merged.map(session => session.id)).toEqual([quotingSession.id])
+ })
+
it('keeps a loaded conversation at its server rank, not its recency slot', () => {
const merged = mergeSearchResults(
[quotingSession, targetSession],