Skip to content

The agent that grows with you

fix(desktop): keep the sidebar search's exact-id hit on top

Konstantin Khlopkovac5ab522725e
diff --git a/apps/desktop/src/app/chat/sidebar/index.tsx b/apps/desktop/src/app/chat/sidebar/index.tsx
index aa191c24561..71fb3a85a86 100644
--- a/apps/desktop/src/app/chat/sidebar/index.tsx
+++ b/apps/desktop/src/app/chat/sidebar/index.tsx
@@ -291,7 +291,7 @@ function searchResultToSession(result: SessionSearchResult): SessionInfo {
_lineage_root_id: result.lineage_root ?? null,
input_tokens: 0,
is_active: false,
- last_active: ts,
+ last_active: result.last_active ?? ts,
message_count: 0,
model: result.model ?? null,
output_tokens: 0,
@@ -303,6 +303,53 @@ function searchResultToSession(result: SessionSearchResult): SessionInfo {
}
}
+export function mergeSearchResults(
+ sortedSessions: readonly SessionInfo[],
+ query: string,
+ serverMatches: readonly SessionSearchResult[],
+ sessionByAnyId: ReadonlyMap<string, SessionInfo>,
+ searchPending: boolean
+): SessionInfo[] {
+ if (!query) {
+ return []
+ }
+
+ // 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
+ // 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) {
+ for (const s of sortedSessions) {
+ if (sessionMatchesSearch(s, query)) {
+ out.set(s.id, s)
+ }
+ }
+ }
+
+ for (const match of serverMatches) {
+ if (out.has(match.session_id)) {
+ continue
+ }
+
+ const loaded = sessionByAnyId.get(match.session_id)
+ out.set(match.session_id, loaded ?? searchResultToSession(match))
+ }
+
+ // Client-only matches that the server didn't return (e.g. cwd/git-branch
+ // fields the FTS index doesn't cover) still deserve a row — after the
+ // ranked hits, in recency order.
+ for (const s of sortedSessions) {
+ if (!out.has(s.id) && sessionMatchesSearch(s, query)) {
+ out.set(s.id, s)
+ }
+ }
+
+ return [...out.values()]
+}
+
interface ChatSidebarProps extends React.ComponentProps<typeof Sidebar> {
currentView: AppView
onNavigate: (item: SidebarNavItem) => void
@@ -694,30 +741,10 @@ export function ChatSidebar({
}
}, [trimmedQuery])
- const searchResults = useMemo(() => {
- if (!trimmedQuery) {
- return []
- }
-
- const out = new Map<string, SessionInfo>()
-
- for (const s of sortedSessions) {
- if (sessionMatchesSearch(s, trimmedQuery)) {
- out.set(s.id, s)
- }
- }
-
- for (const match of serverMatches) {
- if (out.has(match.session_id)) {
- continue
- }
-
- const loaded = sessionByAnyId.get(match.session_id)
- out.set(match.session_id, loaded ?? searchResultToSession(match))
- }
-
- return [...out.values()]
- }, [trimmedQuery, sortedSessions, serverMatches, sessionByAnyId])
+ const searchResults = useMemo(
+ () => mergeSearchResults(sortedSessions, trimmedQuery, serverMatches, sessionByAnyId, searchPending),
+ [sortedSessions, trimmedQuery, serverMatches, sessionByAnyId, searchPending]
+ )
const unpinnedAgentSessions = useMemo(
() => sortedSessions.filter(s => !isPinnedSession(s)),
@@ -1682,6 +1709,7 @@ export function ChatSidebar({
onToggleUnread={toggleUnread}
open
pinned={false}
+ preserveOrder
rootClassName="min-h-32 flex-1 overflow-hidden p-0"
sessions={searchResults}
showProfileTags={showAllProfiles}
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
new file mode 100644
index 00000000000..fe7ec556842
--- /dev/null
+++ b/apps/desktop/src/app/chat/sidebar/search-results-order.test.ts
@@ -0,0 +1,103 @@
+// Regression: pasting a session's exact id into the sidebar search buried the
+// id's own row under recency-sorted client matches. Cross-reference workflows
+// open sessions with a pointer line quoting another session's id, and the
+// sidebar preview is built from that first message — so those sessions match
+// the query client-side forever, while the backend ranks exact id matches
+// first and the sidebar dropped that ranking at the merge.
+import { describe, expect, it } from 'vitest'
+
+import { makeSessionInfo } from '@/test/session-info'
+import type { SessionInfo, SessionSearchResult } from '@/types/hermes'
+
+import { mergeSearchResults } from './index'
+
+const TARGET_ID = '20260914_183005_3738d0'
+
+const quotingSession = makeSessionInfo({
+ id: '20260915_090000_quote01',
+ last_active: 5_000,
+ preview: `resume from ${TARGET_ID}`,
+ started_at: 4_900,
+ title: 'Handoff pointer'
+})
+
+const targetSession = makeSessionInfo({
+ id: TARGET_ID,
+ last_active: 1_000,
+ started_at: 900,
+ title: 'The actual target'
+})
+
+const idHit: SessionSearchResult = {
+ last_active: 1_000,
+ lineage_root: null,
+ model: null,
+ role: null,
+ session_id: TARGET_ID,
+ session_started: 900,
+ snippet: `Session ID: ${TARGET_ID}`,
+ source: null
+}
+
+const indexByAnyId = (sessions: SessionInfo[]) => new Map(sessions.map(session => [session.id, session]))
+
+describe('mergeSearchResults', () => {
+ it('keeps the exact-id server hit above newer client matches once the response lands', () => {
+ // The quoting session is more recent and matches the pasted id through its
+ // preview; the backend still ranks the id's own conversation first.
+ const merged = mergeSearchResults(
+ [quotingSession, targetSession],
+ TARGET_ID,
+ [idHit],
+ indexByAnyId([targetSession]),
+ false
+ )
+
+ expect(merged.map(session => session.id)).toEqual([TARGET_ID, quotingSession.id])
+ })
+
+ it('leads client matches while the server request is still in flight', () => {
+ const merged = mergeSearchResults(
+ [quotingSession, targetSession],
+ TARGET_ID,
+ [],
+ indexByAnyId([quotingSession, targetSession]),
+ true
+ )
+
+ // Recency order until the ranked server response arrives — typing keeps
+ // its instant feedback.
+ expect(merged.map(session => session.id)).toEqual([quotingSession.id, TARGET_ID])
+ })
+
+ it('keeps a loaded conversation at its server rank, not its recency slot', () => {
+ const merged = mergeSearchResults(
+ [quotingSession, targetSession],
+ TARGET_ID,
+ [idHit],
+ indexByAnyId([quotingSession, targetSession]),
+ false
+ )
+
+ // The loaded row object renders (richer than the mapped hit), but the
+ // server's position decides where.
+ expect(merged.map(session => session.id)).toEqual([TARGET_ID, quotingSession.id])
+ expect(merged[0]).toBe(targetSession)
+ })
+
+ it('maps an unloaded server hit with the backend last_active, not its start time', () => {
+ const merged = mergeSearchResults([], TARGET_ID, [idHit], new Map(), false)
+
+ expect(merged).toHaveLength(1)
+ expect(merged[0].started_at).toBe(900)
+ expect(merged[0].last_active).toBe(1_000)
+ })
+
+ it('falls back to the start time when the hit carries no last_active', () => {
+ const bareHit: SessionSearchResult = { ...idHit, last_active: null }
+
+ const merged = mergeSearchResults([], TARGET_ID, [bareHit], new Map(), false)
+
+ expect(merged[0].last_active).toBe(900)
+ })
+})
diff --git a/apps/desktop/src/app/chat/sidebar/sessions-section.tsx b/apps/desktop/src/app/chat/sidebar/sessions-section.tsx
index 7cde3552845..fe9310622a0 100644
--- a/apps/desktop/src/app/chat/sidebar/sessions-section.tsx
+++ b/apps/desktop/src/app/chat/sidebar/sessions-section.tsx
@@ -180,6 +180,11 @@ interface SidebarSessionsSectionProps {
// pinned, messaging groups, and the project overview, where the order isn't
// strictly by recency so a bucket would be misleading.
grouping?: 'date' | 'none' | 'status'
+ // Keep the caller's row order instead of re-sorting by group recency
+ // (defaults to `pinned`, the only pre-ordered caller). The search results
+ // set it too: their order is the backend's ranking (exact id matches
+ // first), which recency re-sorting would bury under newer quoting rows.
+ preserveOrder?: boolean
// Inbox style: render every flat session row as a three-line card (project ·
// age / title / model · size). A render variant that composes with whichever
// grouping is active — the flat recents list opts in; dense tree surfaces
@@ -230,6 +235,7 @@ export function SidebarSessionsSection({
dndSensors,
showProfileTags = false,
grouping = 'none',
+ preserveOrder = pinned,
card = false
}: SidebarSessionsSectionProps) {
const { t } = useI18n()
@@ -262,8 +268,8 @@ export function SidebarSessionsSection({
// recency sort — the drag order is layered on per date group below, so the
// buckets stay truthful and a reorder never costs the list its dividers.
const displayEntries = useMemo(
- () => flattenSessionsWithBranches(sessions, { preserveOrder: pinned }),
- [sessions, pinned]
+ () => flattenSessionsWithBranches(sessions, { preserveOrder }),
+ [sessions, preserveOrder]
)
const renderRow = useCallback(
diff --git a/apps/desktop/src/types/hermes.ts b/apps/desktop/src/types/hermes.ts
index 5630eafc8ce..cf58230dc43 100644
--- a/apps/desktop/src/types/hermes.ts
+++ b/apps/desktop/src/types/hermes.ts
@@ -1272,6 +1272,10 @@ export interface ComputerUseStatus {
}
export interface SessionSearchResult {
+ /** Recency of the matched conversation, straight from the sessions row —
+ * present on hits backed by a rich row (the search endpoint fills it).
+ * Used to order unloaded hits honestly; falls back to session_started. */
+ last_active?: number | null
/** Lineage root of the matched conversation. Stable across compression and
* used as the durable pin id; falls back to session_id when absent. */
lineage_root?: string | null