Multiplayer agent harness for work.
perf(search): stored tsvector column + saner short-term prefixing for ⌘K search (#597)
Joshua France62772f544801
diff --git a/src/sessions/entry-search.ts b/src/sessions/entry-search.tsindex d45264afb..f1ac986a8 100644--- a/src/sessions/entry-search.ts+++ b/src/sessions/entry-search.ts@@ -12,11 +12,17 @@ export function searchTerms(query: string): string[] {.slice(0, MAX_TERMS);}-/** A `to_tsquery('simple', …)` expression: every term required, last-token-friendly prefix match. */+/** Terms shorter than this match whole words only — 1-2 char prefixes expand to+ * enormous GIN key sets and dominate search latency. */+const MIN_PREFIX_LEN = 3;++/** A `to_tsquery('simple', …)` expression: every term required; terms long+ * enough to be selective prefix-match (last-token-friendly), shorter ones+ * match exactly. */export function tsPrefixQuery(query: string): string | null {const terms = searchTerms(query);if (!terms.length) return null;- return terms.map((t) => `${t}:*`).join(" & ");+ return terms.map((t) => (t.length >= MIN_PREFIX_LEN ? `${t}:*` : t)).join(" & ");}/** The searchable text of an entry payload — mirrors the Postgres `entry_search_text` function. */@@ -32,11 +38,12 @@ export function entrySearchAuthor(entry: Pick<SessionEntry, "type" | "payload">)return typeof name === "string" && name.trim() ? name.trim() : undefined;}-/** In-memory mirror of the tsquery semantics: every term prefix-matches some word. */+/** In-memory mirror of the tsquery semantics: every term matches some word —+ * by prefix when long enough, exactly otherwise. */export function matchesSearchTerms(text: string, terms: readonly string[]): boolean {if (!terms.length) return false;const words = text.toLowerCase().split(/[^\p{L}\p{N}]+/u);- return terms.every((term) => words.some((w) => w.startsWith(term)));+ return terms.every((term) => words.some((w) => (term.length >= MIN_PREFIX_LEN ? w.startsWith(term) : w === term)));}/** First occurrence of `term` in `lower` that starts at a word boundary —diff --git a/src/sessions/postgres-session-store.ts b/src/sessions/postgres-session-store.tsindex 523000c62..7c1abafbb 100644--- a/src/sessions/postgres-session-store.ts+++ b/src/sessions/postgres-session-store.ts@@ -39,8 +39,8 @@ import {userMessagePreview,} from "./session-store.ts";-export const SESSION_ENTRIES_SEARCH_INDEX_SQL = `CREATE INDEX CONCURRENTLY IF NOT EXISTS session_entries_search_fts- ON session_entries USING GIN (to_tsvector('simple', COALESCE(entry_search_text(payload), '')))+export const SESSION_ENTRIES_SEARCH_INDEX_SQL = `CREATE INDEX CONCURRENTLY IF NOT EXISTS session_entries_search_tsv+ ON session_entries USING GIN (search_tsv)WHERE type IN ('user', 'assistant', 'text')`;export function rowToSession(r: Record<string, unknown>): Session {@@ -264,7 +264,7 @@ export function createPostgresSessionStore(connectionString: string, opts: Store`CREATE INDEX IF NOT EXISTS session_entries_user_ts ON session_entries(created_at) WHERE type = 'user'`,`CREATE INDEX IF NOT EXISTS session_entries_session_created ON session_entries(session_id, created_at DESC)`,`CREATE OR REPLACE FUNCTION entry_search_text(payload text) RETURNS text- LANGUAGE plpgsql IMMUTABLE PARALLEL UNSAFE AS $entry_search_text$+ LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE AS $entry_search_text$DECLARE j json;BEGINj := replace(payload, '\\u0000', '')::json;@@ -273,7 +273,10 @@ export function createPostgresSessionStore(connectionString: string, opts: StoreELSE NULL END;EXCEPTION WHEN others THEN RETURN NULL;END $entry_search_text$`,+ `ALTER TABLE session_entries ADD COLUMN IF NOT EXISTS search_tsv tsvector+ GENERATED ALWAYS AS (to_tsvector('simple', COALESCE(entry_search_text(payload), ''))) STORED`,SESSION_ENTRIES_SEARCH_INDEX_SQL,+ `DROP INDEX IF EXISTS session_entries_search_fts`,`DELETE FROM session_entries WHERE session_id IN (SELECT id FROM sessions WHERE type IN ('channel','group') AND thread_ref ~ '^[a-z0-9_]+/[^:/]+$')`,`DELETE FROM participants WHERE session_id IN (SELECT id FROM sessions WHERE type IN ('channel','group') AND thread_ref ~ '^[a-z0-9_]+/[^:/]+$')`,`DELETE FROM session_leases WHERE session_id IN (SELECT id FROM sessions WHERE type IN ('channel','group') AND thread_ref ~ '^[a-z0-9_]+/[^:/]+$')`,@@ -758,7 +761,7 @@ export function createPostgresSessionStore(connectionString: string, opts: StoreJOIN participants p ON p.session_id = e.session_id AND p.principal_id = $1WHERE e.type IN ('user', 'assistant', 'text')AND ${withinParticipantWindow("e", "p")}- AND to_tsvector('simple', COALESCE(entry_search_text(e.payload), '')) @@ to_tsquery('simple', $2)+ AND e.search_tsv @@ to_tsquery('simple', $2)ORDER BY e.created_at DESC, e.session_id, e.seq DESCLIMIT $3`,[principalId, ts, Math.max(1, Math.min(limit, 200))],diff --git a/test/entry-search.test.ts b/test/entry-search.test.tsindex eb48d1b61..d561f3fa1 100644--- a/test/entry-search.test.ts+++ b/test/entry-search.test.ts@@ -20,11 +20,18 @@ test("tsPrefixQuery builds an AND-of-prefixes tsquery", () => {assert.equal(tsPrefixQuery("!!!"), null);});+test("tsPrefixQuery keeps short terms exact — tiny prefixes are too expensive to expand", () => {+ assert.equal(tsPrefixQuery("qm deploy"), "qm & deploy:*");+ assert.equal(tsPrefixQuery("de"), "de");+});+test("matchesSearchTerms mirrors prefix semantics", () => {assert.ok(matchesSearchTerms("Refreshed the memo board data", ["memo", "boar"]));assert.ok(!matchesSearchTerms("Refreshed the memo board data", ["memo", "missing"]));assert.ok(!matchesSearchTerms("emoboard", ["memo"]), "prefix, not substring");assert.ok(!matchesSearchTerms("anything", []));+ assert.ok(matchesSearchTerms("the qm deployment", ["qm"]), "short terms match whole words");+ assert.ok(!matchesSearchTerms("the deployment", ["de"]), "short terms do not prefix-match");});test("entrySearchText reads string payloads and payload.text", () => {