Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Fix /sessions/<id> session links and make sessions searchable by title/id (#2357)

Regan Bellcd8c2f75b6a3
diff --git a/plugins/web-ui/src/deep-link.ts b/plugins/web-ui/src/deep-link.ts
index 13321e560..ffe191fcd 100644
--- a/plugins/web-ui/src/deep-link.ts
+++ b/plugins/web-ui/src/deep-link.ts
@@ -49,7 +49,7 @@ export function parseDeepLink(
if (pathView === "projects") {
projectItem = projectKind ? decodeSegment(segments[2] ?? "") : decodeSegment(segments[1] ?? "");
}
- const sessionRoute = pathView === "s" || pathView === "c";
+ const sessionRoute = pathView === "s" || pathView === "c" || pathView === "sessions";
const sessionSeg = sessionRoute ? decodeSegment(segments[1] ?? "") : null;
const viewFor = (): string | null => {
if (pathView === "projects") return "contexts";
diff --git a/plugins/web-ui/test/deep-link.test.ts b/plugins/web-ui/test/deep-link.test.ts
index 4d7e1f7c8..384239cb8 100644
--- a/plugins/web-ui/test/deep-link.test.ts
+++ b/plugins/web-ui/test/deep-link.test.ts
@@ -158,3 +158,13 @@ test("other links are not treated as session links", () => {
assert.equal(sessionLinkTarget(`${origin}/s/abc`, origin, "/ui"), null);
assert.equal(sessionLinkTarget("not a url", "null", ""), null);
});
+
+test("legacy /sessions/<id> links resolve to the session", () => {
+ const uuid = "98e9f742-698f-4bd4-aaaa-4f474019fec6";
+ assert.deepEqual(parseDeepLink("", `/sessions/${uuid}`, ""), { view: "chats", session: uuid, item: null });
+ assert.deepEqual(parseDeepLink("/web-ui", `/web-ui/sessions/${uuid}`, ""), {
+ view: "chats",
+ session: uuid,
+ item: null,
+ });
+});
diff --git a/src/api/app-sessions.ts b/src/api/app-sessions.ts
index a058cbecb..2f219a44f 100644
--- a/src/api/app-sessions.ts
+++ b/src/api/app-sessions.ts
@@ -213,6 +213,33 @@ export function createSessionMethods(
});
};
+ /** Sessions whose title matches the query — the entry index only covers message text. */
+ const titleSearchHits = async (principalId: string, query: string, capped: number): Promise<SessionSearchHit[]> => {
+ const matches = await deps.sessions.searchSessionTitles(principalId, query, capped);
+ if (!matches.length) return [];
+ const allowed = new Map(
+ await Promise.all(
+ [...new Set(matches.map((s) => s.scopeId))].map(
+ async (scope) => [scope, (await managedProjectMembership(scope, principalId)) !== false] as const,
+ ),
+ ),
+ );
+ return matches
+ .filter((s) => allowed.get(s.scopeId))
+ .map((s) => ({
+ sessionId: s.id,
+ title: s.title ?? null,
+ scopeId: s.scopeId,
+ ...(s.channelName ? { channelName: s.channelName } : {}),
+ ...(s.surface ? { surface: s.surface } : {}),
+ seq: 0,
+ entryType: "title",
+ snippet: s.title ?? s.id,
+ createdAt: s.lastActivityAt ?? s.createdAt,
+ ...(s.archived ? { archived: true } : {}),
+ }));
+ };
+
return {
async getSession(sessionId, window) {
const session = await deps.sessions.get(sessionId);
@@ -508,7 +535,8 @@ export function createSessionMethods(
async searchSessions(principalId, query, limit = SEARCH_HIT_LIMIT): Promise<SessionSearchHit[]> {
const capped = Math.max(1, Math.min(Math.floor(limit), 100));
const hits = await deps.sessions.searchEntries(principalId, query, capped);
- if (!hits.length) return [];
+ const titleHits = await titleSearchHits(principalId, query, capped);
+ if (!hits.length) return titleHits;
const allowed = new Map(
await Promise.all(
[...new Set(hits.map((hit) => hit.scopeId))].map(
@@ -525,7 +553,7 @@ export function createSessionMethods(
);
const principal = { ...deps.identity.classify(principalId), teamIds: memberships.filter((id) => id !== null) };
const terms = searchTerms(query);
- return hits.flatMap((hit) => {
+ const entryHits: SessionSearchHit[] = hits.flatMap((hit) => {
if (!allowed.get(hit.scopeId)) return [];
if (!principalEntitledToScope(principal, hit.entryScopeLabel, hit.scopeId, scopeId("org", orgIdOf())))
return [];
@@ -546,6 +574,8 @@ export function createSessionMethods(
},
];
});
+ const entryHitSessions = new Set(entryHits.map((hit) => hit.sessionId));
+ return [...titleHits.filter((hit) => !entryHitSessions.has(hit.sessionId)), ...entryHits].slice(0, capped);
},
async sessionBackground(sessionId, viewer) {
diff --git a/src/sessions/memory-session-store.ts b/src/sessions/memory-session-store.ts
index e4ad6f83c..28c74f3ed 100644
--- a/src/sessions/memory-session-store.ts
+++ b/src/sessions/memory-session-store.ts
@@ -522,6 +522,23 @@ export function createMemorySessionStore(opts: StoreOptions = {}): SessionStore
return opts?.limit !== undefined ? visible.slice(-opts.limit) : visible;
},
+ async searchSessionTitles(principalId, query, limit = 20): Promise<Session[]> {
+ const needle = query.trim();
+ if (needle.length < 2) return [];
+ const lower = needle.toLowerCase();
+ const ids = participants.get(principalId);
+ if (!ids) return [];
+ const out: Session[] = [];
+ for (const id of ids) {
+ const row = participantSession(id, principalId);
+ if (!row) continue;
+ if ((row.title ?? "").toLowerCase().includes(lower)) out.push(row);
+ }
+ return out
+ .sort((a, b) => (b.lastActivityAt ?? b.createdAt) - (a.lastActivityAt ?? a.createdAt))
+ .slice(0, Math.max(1, Math.min(Math.floor(limit), 100)));
+ },
+
async searchEntries(principalId, query, limit = 40): Promise<EntrySearchHit[]> {
const terms = searchTerms(query);
if (!terms.length) return [];
diff --git a/src/sessions/postgres-session-store.ts b/src/sessions/postgres-session-store.ts
index 0ca6b8cbe..9d0fc0fe1 100644
--- a/src/sessions/postgres-session-store.ts
+++ b/src/sessions/postgres-session-store.ts
@@ -1571,6 +1571,19 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
return (await q(sql, params)).map(rowToEntry).reverse();
},
+ async searchSessionTitles(principalId, query, limit = 20): Promise<Session[]> {
+ const needle = query.trim();
+ if (needle.length < 2) return [];
+ const like = `%${needle.replace(/[\\%_]/g, (c) => `\\${c}`)}%`;
+ const rows = await q(
+ participantSessionsSql(" AND COALESCE(p.title, s.title) ILIKE $2") +
+ " ORDER BY COALESCE(s.last_activity, s.created_at) DESC, s.id LIMIT $3",
+ [principalId, like, Math.max(1, Math.min(Math.floor(limit), 100))],
+ { timeoutMs: SEARCH_TIMEOUT_MS },
+ );
+ return rows.map(rowToParticipantSession);
+ },
+
async searchEntries(principalId, query, limit = 40): Promise<EntrySearchHit[]> {
const ts = tsPrefixQuery(query);
if (!ts) return [];
diff --git a/src/sessions/session-store.ts b/src/sessions/session-store.ts
index 848e686a1..1d2033f2b 100644
--- a/src/sessions/session-store.ts
+++ b/src/sessions/session-store.ts
@@ -613,6 +613,9 @@ export interface SessionStore {
searchEntries(principalId: string, query: string, limit?: number): Promise<EntrySearchHit[]>;
+ /** Sessions whose (participant or session) title contains the query — newest activity first. */
+ searchSessionTitles(principalId: string, query: string, limit?: number): Promise<Session[]>;
+
appendSearchEntries(lease: Lease, rows: readonly NewSearchEntry[]): Promise<void>;
searchIndexCoverage(sessionId: string): Promise<number>;
missingSearchEntries(sessionId: string): Promise<number>;
diff --git a/test/session-search.test.ts b/test/session-search.test.ts
index 215e3c504..babb4bd16 100644
--- a/test/session-search.test.ts
+++ b/test/session-search.test.ts
@@ -130,3 +130,17 @@ test("app.searchSessions decorates hits with session metadata and enforces visib
assert.deepEqual(await app.searchSessions("U1", ""), [], "empty query is empty, not an error");
});
+
+test("app.searchSessions finds sessions by title", async () => {
+ const dataDir = mkdtempSync(join(tmpdir(), "search-"));
+ const { app, sessions } = buildApp(testConfig({ dataDir }));
+
+ const id = await seed(sessions, "web:U1:titled", "U1", ["hello there"]);
+ await sessions.updateTitle(id, "Quarterly roadmap review");
+ await seed(sessions, "web:U2:other2", "U2", ["hello there"]);
+
+ const byTitle = await app.searchSessions("U1", "roadmap");
+ assert.ok(byTitle.some((h) => h.sessionId === id && h.entryType === "title"));
+
+ assert.deepEqual(await app.searchSessions("U2", "roadmap"), [], "title matches respect participation");
+});