diff --git a/application/state/sessionHistorySearch.test.ts b/application/state/sessionHistorySearch.test.ts index a601afa03..373afb582 100644 --- a/application/state/sessionHistorySearch.test.ts +++ b/application/state/sessionHistorySearch.test.ts @@ -702,8 +702,9 @@ test("filterSessionHistory caps the query and matches on its capped prefix", () const session = createSession("a", "nginx restart", [{ content: "reload nginx" }]); // A pasted multi-megabyte query must not be re-normalized per session: it is - // capped (MAX_SEARCH_QUERY_LENGTH) and normalized once per scan, so matching - // falls back to the capped prefix (which still spans tokens here). + // capped via truncateQueryForMatch (MAX_SEARCH_QUERY_LENGTH) at this scan's + // input and normalized once per scan, so matching falls back to the capped + // prefix (which still spans tokens here). const longQuery = `${"nginx ".repeat(200_000)}zzz-impossible-tail`; assert.deepEqual(filterSessionHistory([session], longQuery), [session]); // The uncapped-looking but normal query still matches exactly. diff --git a/application/state/sessionHistorySearch.ts b/application/state/sessionHistorySearch.ts index fa57c4519..5ab2ae9ab 100644 --- a/application/state/sessionHistorySearch.ts +++ b/application/state/sessionHistorySearch.ts @@ -5,7 +5,7 @@ import { type SessionHistorySearchTarget, type SessionSearchIndex, } from '../../domain/sessionHistorySearch'; -import { prepareSearchQuery } from '../../lib/searchMatcher'; +import { prepareSearchQuery, truncateQueryForMatch } from '../../lib/searchMatcher'; /** * Memory bounds for the per-session search index cache. `filterSessionHistory` @@ -290,10 +290,12 @@ function filterSessionHistoryStore( // Bound and prepare the query once per scan: a pasted-in multi-megabyte // query would otherwise be re-normalized (NFKC), tokenized and compacted // inside `matchesSessionSearchIndex` for every session, freezing the - // renderer. `prepareSearchQuery` caps the input (`MAX_SEARCH_QUERY_LENGTH`) - // and computes the normalized forms a single time so the per-session work - // below only does `includes` scans over the cached haystacks. - const preparedQuery = prepareSearchQuery(trimmed); + // renderer. The cap (`truncateQueryForMatch`, `MAX_SEARCH_QUERY_LENGTH`) is + // applied here at this scan's input — not inside the shared `prepareSearchQuery` + // — so the meaning of over-limit queries stays local to session history and + // the normalized forms are computed a single time, letting the per-session + // work below only do `includes` scans over the cached haystacks. + const preparedQuery = prepareSearchQuery(truncateQueryForMatch(trimmed)); // A new scan starts with no visited sessions: the touched set records only // this scan's visits so `canCacheSessionSearchIndex` can never mistake a // previous scan's keys for keys the current scan still needs. diff --git a/domain/searchMatcher.test.ts b/domain/searchMatcher.test.ts index ac62cf7dc..e440dd534 100644 --- a/domain/searchMatcher.test.ts +++ b/domain/searchMatcher.test.ts @@ -125,6 +125,18 @@ test("punctuation-only query does not match every field", () => { assert.equal(matchesSearchQuery("-", "some-host"), false); }); +test("over-limit query keeps its full meaning instead of matching on a truncated prefix", () => { + // The shared matcher never truncates: a >256-char query whose distinguishing + // suffix is beyond the suggested `MAX_SEARCH_QUERY_LENGTH` cap must not + // match a field that only contains the cap-bounded prefix. Callers that + // rescan many haystacks (session history) bound their own input instead. + const field = "a".repeat(300); + const query = `${"a".repeat(300)} missing-suffix`; + assert.equal(matchesSearchQuery(query, field), false); + // The same query still matches when the whole query is present. + assert.equal(matchesSearchQuery(query, `${field} missing-suffix`), true); +}); + test("host search avoids compact hostname false positives on numeric segments", () => { assert.equal( matchesHostSearchQuery("61", { diff --git a/lib/searchMatcher.ts b/lib/searchMatcher.ts index 05830269a..e49547321 100644 --- a/lib/searchMatcher.ts +++ b/lib/searchMatcher.ts @@ -100,14 +100,20 @@ export type SearchMatchOptions = { }; /** - * Maximum accepted query length. `matchesPreparedSearchQuery` historically - * re-normalized, tokenized and compacted the raw query for every haystack it - * was matched against; capping the input bounds that per-haystack cost even - * before callers prepare the query once and reuse it. + * Suggested query bound for callers that rescan many haystacks per keystroke + * (e.g. session-history search). The shared matcher never truncates: callers + * apply this cap at their own input so the bound stays local to them and the + * global match semantics (full-query meaning) are unchanged for everyone else. */ export const MAX_SEARCH_QUERY_LENGTH = 256; -function truncateQueryForMatch(query: string): string { +/** + * Bound a caller-side search query to `MAX_SEARCH_QUERY_LENGTH` UTF-16 code + * units without splitting a UTF-16 surrogate pair when the cap lands mid-pair + * (e.g. an astral-plane character such as an emoji; drop the lone high + * surrogate). Truncation is a caller concern: see `MAX_SEARCH_QUERY_LENGTH`. + */ +export function truncateQueryForMatch(query: string): string { if (query.length <= MAX_SEARCH_QUERY_LENGTH) return query; const truncated = query.slice(0, MAX_SEARCH_QUERY_LENGTH); // Avoid splitting a UTF-16 surrogate pair when the cap lands mid-pair (e.g. @@ -141,7 +147,11 @@ const EMPTY_PREPARED_QUERY: PreparedSearchQuery = { }; export function prepareSearchQuery(query: string): PreparedSearchQuery { - const normalized = normalizeText(truncateQueryForMatch(query)); + // No length cap here: truncating globally would silently change match + // semantics for every caller (an over-limit query's tail would be dropped, + // so a field containing only the prefix would report a match). Callers that + // rescan many haystacks bound their own input via `truncateQueryForMatch`. + const normalized = normalizeText(query); if (!normalized) return EMPTY_PREPARED_QUERY; return { normalized,