Skip to content

Commit 6be89ba

Browse files
authored
fix(session-search): order by newest message and hide sub-agent sessions (#6736)
Session search (Cmd/Ctrl+Shift+F) listed sub-agent sessions and ordered typed results by fuzzy score. Drop child sessions with getRootSessions, normalize second-based engine times, and rank matches that contain every typed word newest first, with looser fuzzy matches after them. Fixes ENG-886
1 parent 0d6c7ef commit 6be89ba

5 files changed

Lines changed: 148 additions & 22 deletions

File tree

‎apps/app/src/react-app/domains/session/search/session-search.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import fuzzysort from "fuzzysort";
2+
13
/** A session the search dialog can match by title. */
24
export type SearchableSession = {
35
workspaceId: string;
46
sessionId: string;
57
title: string;
68
workspaceTitle: string;
9+
/** Last activity (in practice, the newest message) in milliseconds. */
710
updatedAt: number;
811
};
912

@@ -20,3 +23,33 @@ export function dedupeSearchableSessions(sessions: SearchableSession[]): Searcha
2023
}
2124
return [...unique.values()];
2225
}
26+
27+
const newestFirst = (a: SearchableSession, b: SearchableSession) => b.updatedAt - a.updatedAt;
28+
29+
/**
30+
* Sessions to show for a query, newest message first. Sessions whose title or
31+
* workspace contains every typed word come first; looser fuzzy matches follow
32+
* by match quality so a typo still finds something without outranking them.
33+
*/
34+
export function rankSearchableSessions(
35+
sessions: SearchableSession[],
36+
query: string,
37+
limit: number,
38+
): SearchableSession[] {
39+
const words = query.trim().toLowerCase().split(/\s+/).filter(Boolean);
40+
if (words.length === 0) return [...sessions].sort(newestFirst).slice(0, limit);
41+
42+
const contains = sessions.filter((session) => {
43+
const text = `${session.title} ${session.workspaceTitle}`.toLowerCase();
44+
return words.every((word) => text.includes(word));
45+
});
46+
contains.sort(newestFirst);
47+
if (contains.length >= limit) return contains.slice(0, limit);
48+
49+
const included = new Set(contains);
50+
const fuzzy = fuzzysort
51+
.go(query.trim(), sessions, { keys: ["title", "workspaceTitle"] })
52+
.map((hit) => hit.obj)
53+
.filter((session) => !included.has(session));
54+
return [...contains, ...fuzzy].slice(0, limit);
55+
}

‎apps/app/src/react-app/domains/session/sidebar/utils.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function formatSessionRelativeTime(updatedAt: number | null | undefined):
4545
return `${Math.round(hours / 24)}d`;
4646
}
4747

48-
const normalizeSessionParentID = (session: SessionListItem) => {
48+
const normalizeSessionParentID = (session: { parentID?: string | null }) => {
4949
const parentID = session.parentID?.trim();
5050
return parentID || "";
5151
};
@@ -55,7 +55,7 @@ const normalizeSessionParentID = (session: SessionListItem) => {
5555
* in the loaded page (the parent may be archived, deleted, or beyond the list
5656
* limit). Children are only reached from the task card in their parent.
5757
*/
58-
export const getRootSessions = (sessions: WorkspaceSessionGroup["sessions"]) =>
58+
export const getRootSessions = <T extends { parentID?: string | null }>(sessions: T[]): T[] =>
5959
sessions.filter((session) => !normalizeSessionParentID(session));
6060

6161
/**

‎apps/app/src/react-app/shell/command-palette-sessions.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { getDisplaySessionTitle } from "@/app/lib/session-title";
22
import { t } from "@/i18n";
3+
import { sessionActivityAt } from "@/react-app/domains/session/sidebar/session-settle";
4+
import { getRootSessions } from "@/react-app/domains/session/sidebar/utils";
35

46
import type { SessionOption } from "./command-palette";
57
import type { RouteSession, RouteWorkspace } from "./route-workspaces";
@@ -33,11 +35,12 @@ export function buildCommandPaletteSessions(
3335
workspace.path?.trim() ||
3436
t("session.workspace_fallback");
3537

36-
for (const session of sessionsByWorkspaceId[workspace.id] ?? []) {
38+
// Sub-agent sessions live inside their parent's task card, not in pickers.
39+
for (const session of getRootSessions(sessionsByWorkspaceId[workspace.id] ?? [])) {
3740
const sessionId = session.id.trim();
3841
if (!sessionId) continue;
3942
const title = getDisplaySessionTitle(session.title ?? "");
40-
const updatedAt = session.time.updated ?? session.time.created;
43+
const updatedAt = sessionActivityAt(session);
4144
options.push({
4245
workspaceId: workspace.id,
4346
sessionId,

‎apps/app/src/react-app/shell/session-search-dialog.tsx‎

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/** @jsxImportSource react */
22
import { useEffect, useMemo, useState } from "react";
3-
import fuzzysort from "fuzzysort";
43
import { ClockIcon, TypeIcon } from "lucide-react";
54

65
import {
@@ -23,6 +22,7 @@ import {
2322
import { formatRelativeTime } from "@/app/utils";
2423
import {
2524
dedupeSearchableSessions,
25+
rankSearchableSessions,
2626
type SearchableSession,
2727
} from "@/react-app/domains/session/search/session-search";
2828

@@ -49,8 +49,9 @@ export type SessionSearchDialogProps = {
4949
};
5050

5151
/**
52-
* Session search across every workspace by title (Cmd/Ctrl+Shift+F). With an
53-
* empty query it lists recent sessions.
52+
* Session search across every workspace by title (Cmd/Ctrl+Shift+F), newest
53+
* message first. With an empty query it lists recent sessions. Sub-agent
54+
* sessions are left out upstream; they open from their parent's task card.
5455
*/
5556
export function SessionSearchDialog(props: SessionSearchDialogProps) {
5657
const [query, setQuery] = useState("");
@@ -66,24 +67,19 @@ export function SessionSearchDialog(props: SessionSearchDialogProps) {
6667

6768
const groups = useMemo<ResultGroup[]>(() => {
6869
if (!trimmedQuery) {
69-
const recent = [...sessions]
70-
.sort((a, b) => b.updatedAt - a.updatedAt)
71-
.slice(0, RECENT_LIMIT)
72-
.map((session) => ({
73-
id: `recent:${session.workspaceId}:${session.sessionId}`,
74-
kind: "recent" as const,
75-
session,
76-
}));
70+
const recent = rankSearchableSessions(sessions, "", RECENT_LIMIT).map((session) => ({
71+
id: `recent:${session.workspaceId}:${session.sessionId}`,
72+
kind: "recent" as const,
73+
session,
74+
}));
7775
return recent.length > 0 ? [{ value: "Recent sessions", items: recent }] : [];
7876
}
7977

80-
const titleItems = fuzzysort
81-
.go(trimmedQuery, sessions, { keys: ["title", "workspaceTitle"], limit: RESULT_LIMIT })
82-
.map((hit) => ({
83-
id: `title:${hit.obj.workspaceId}:${hit.obj.sessionId}`,
84-
kind: "title" as const,
85-
session: hit.obj,
86-
}));
78+
const titleItems = rankSearchableSessions(sessions, trimmedQuery, RESULT_LIMIT).map((session) => ({
79+
id: `title:${session.workspaceId}:${session.sessionId}`,
80+
kind: "title" as const,
81+
session,
82+
}));
8783
return titleItems.length > 0 ? [{ value: "Sessions", items: titleItems }] : [];
8884
}, [sessions, trimmedQuery]);
8985

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { describe, expect, test } from "bun:test";
2+
3+
import {
4+
rankSearchableSessions,
5+
type SearchableSession,
6+
} from "../src/react-app/domains/session/search/session-search";
7+
import { buildCommandPaletteSessions } from "../src/react-app/shell/command-palette-sessions";
8+
import type { RouteSession, RouteWorkspace } from "../src/react-app/shell/route-workspaces";
9+
10+
const NOW = new Date(2026, 9, 29, 15, 0).getTime();
11+
const MINUTE = 60 * 1000;
12+
13+
function searchable(sessionId: string, title: string, updatedAt: number, workspaceTitle = "Lounge"): SearchableSession {
14+
return { workspaceId: "lounge", sessionId, title, workspaceTitle, updatedAt };
15+
}
16+
17+
function routeSession(id: string, updated: number, extra: Record<string, unknown> = {}): RouteSession {
18+
// Only the fields the builder reads; the rest of the shape is irrelevant here.
19+
return JSON.parse(JSON.stringify({ id, title: id, time: { created: updated, updated }, ...extra }));
20+
}
21+
22+
function workspace(id: string): RouteWorkspace {
23+
return JSON.parse(JSON.stringify({ id, name: id, path: `/tmp/${id}` }));
24+
}
25+
26+
const ids = (sessions: { sessionId: string }[]) => sessions.map((session) => session.sessionId);
27+
28+
describe("rankSearchableSessions", () => {
29+
test("lists the newest message first with no query", () => {
30+
const sessions = [
31+
searchable("old", "Quarterly report", NOW - 60 * MINUTE),
32+
searchable("new", "Launch plan", NOW - MINUTE),
33+
searchable("mid", "Hiring notes", NOW - 10 * MINUTE),
34+
];
35+
expect(ids(rankSearchableSessions(sessions, "", 15))).toEqual(["new", "mid", "old"]);
36+
expect(ids(rankSearchableSessions(sessions, "", 2))).toEqual(["new", "mid"]);
37+
});
38+
39+
test("orders title matches by newest message, not match quality", () => {
40+
const sessions = [
41+
searchable("exact-old", "report", NOW - 60 * MINUTE),
42+
searchable("partial-new", "Weekly report draft", NOW - MINUTE),
43+
searchable("unrelated", "Launch plan", NOW),
44+
];
45+
expect(ids(rankSearchableSessions(sessions, "report", 50))).toEqual(["partial-new", "exact-old"]);
46+
});
47+
48+
test("matches every word across title and workspace", () => {
49+
const sessions = [
50+
searchable("both", "Budget review", NOW - 5 * MINUTE, "Finance"),
51+
searchable("title-only", "Budget ideas", NOW - MINUTE, "Lounge"),
52+
];
53+
expect(ids(rankSearchableSessions(sessions, "budget finance", 50))).toEqual(["both"]);
54+
});
55+
56+
test("keeps looser fuzzy matches below every contained match", () => {
57+
const sessions = [
58+
searchable("typo-new", "Reprot cleanup", NOW),
59+
searchable("contains-old", "Monthly report", NOW - 60 * MINUTE),
60+
];
61+
const ranked = ids(rankSearchableSessions(sessions, "report", 50));
62+
expect(ranked[0]).toBe("contains-old");
63+
});
64+
});
65+
66+
describe("buildCommandPaletteSessions", () => {
67+
test("leaves out sub-agent sessions", () => {
68+
const options = buildCommandPaletteSessions(
69+
[workspace("lounge")],
70+
{
71+
lounge: [
72+
routeSession("parent", NOW - MINUTE),
73+
routeSession("child", NOW, { parentID: "parent" }),
74+
routeSession("blank-parent", NOW - 2 * MINUTE, { parentID: " " }),
75+
],
76+
},
77+
"lounge",
78+
);
79+
expect(ids(options)).toEqual(["parent", "blank-parent"]);
80+
});
81+
82+
test("normalizes engines that report seconds", () => {
83+
const options = buildCommandPaletteSessions(
84+
[workspace("seconds"), workspace("millis")],
85+
{
86+
seconds: [routeSession("in-seconds", Math.floor(NOW / 1000))],
87+
millis: [routeSession("in-millis", NOW - MINUTE)],
88+
},
89+
"",
90+
);
91+
expect(ids(options)).toEqual(["in-seconds", "in-millis"]);
92+
expect(options[0]?.updatedAt).toBe(Math.floor(NOW / 1000) * 1000);
93+
});
94+
});

0 commit comments

Comments
 (0)