Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

perf(transcript-index): search with one case-insensitive RegExp, not a lowered copy per entry

Brandon Bridgesc9e881781f6e
diff --git a/CLAUDE.md b/CLAUDE.md
index fde016d4d..6c038e929 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -9215,6 +9215,13 @@ script (`fetch('http://localhost:9333/json')`, then a WebSocket to the page):
ones went from painting LAST (1.5–2.1 s) to first (0.55–1.1 s), 0 extra logins (#1057).
- **A hint must fail toward the old behavior.** `onScreen` absent/unknown = on screen = the old
FIFO; a coalesced read is never a cache (a spawn after it settles reads again).
+- **Search a large in-memory text set with one case-insensitive RegExp per query, never by
+ lower-casing each entry.** The transcript index (`core/transcript-index-core.ts`, `queryRegExp`)
+ lowered every entry per keystroke: 20-52 ms and 7-15 MB garbage per search on a 16 MB index,
+ vs 2-13 ms and 0 MB as one `RegExp(escaped, 'iu')`. A stored lowered copy would retain +32 MB.
+ `iu`, not `i`: `i` loses hits `toLowerCase` found (Kelvin K, Å, ẞ); escape only SyntaxCharacters
+ and `/` (`\-` is a SyntaxError in `u` mode). The snippet uses the same matcher. The index file
+ persists through `writeFileAtomic` (0600), only when a `dirty` flag says the file is behind.
## SSH projects on Windows: the in-process transport
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index b5fae7db6..352058a2e 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -1364,6 +1364,8 @@ bite most often:
`terminal/board-render-pause.ts`. Every "is the board up" check includes the Omni board
(`isGlobalKanbanOpen()`), and the board never feeds the visibility verdict that drives
hibernation and the WebGL budget.
+- Search big in-memory text with one escaped `RegExp(q, 'iu')` per query, never `toLowerCase()`
+ per entry (transcript index: 20-52 ms + 7-15 MB garbage → 2-13 ms + 0 MB per search).
**SSH projects on Windows** run over an in-process transport (`src/core/remote-ssh/native/`),
not the ssh binary. If you add an ssh call site, route it through `useNativeSsh()` like the
diff --git a/src/core/transcript-index-core.test.ts b/src/core/transcript-index-core.test.ts
index 25782d4b4..a7e1654f5 100644
--- a/src/core/transcript-index-core.test.ts
+++ b/src/core/transcript-index-core.test.ts
@@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest'
import {
extractEntryFields,
makeSnippet,
+ queryRegExp,
searchEntries,
planRefresh,
INDEX_TEXT_CAP_BYTES,
@@ -79,6 +80,51 @@ describe('searchEntries', () => {
})
})
+describe('searchEntries matching (RegExp iu, audit #5)', () => {
+ const one = (text: string, title = 'x'): TranscriptIndexEntry[] => [
+ { sessionId: 's', transcriptPath: '/p/s.jsonl', cwd: '/c', mtime: 1, title, text }
+ ]
+ const hit = (text: string, q: string): boolean => searchEntries(one(text), q).length === 1
+
+ it('folds non-ASCII case the way toLowerCase().includes did', () => {
+ expect(hit('Ein Café in München', 'CAFÉ IN MÜNCHEN')).toBe(true)
+ expect(hit('ПРИВЕТ мир', 'привет МИР')).toBe(true)
+ expect(hit('ΑΛΦΑ beta', 'αλφα')).toBe(true)
+ expect(hit('日本語のテキスト', '日本語')).toBe(true)
+ // The cases plain `i` would have LOST (toLowerCase folds them, simple-uppercase canonicalising
+ // does not) — the reason the flag is `iu`.
+ expect(hit('temperature in \u212A', 'in k')).toBe(true) // Kelvin sign
+ expect(hit('unit \u212B', 'it å')).toBe(true) // Ångström sign
+ expect(hit('GROSS\u1E9E', 'sß')).toBe(true) // capital sharp s
+ expect(hit('Deseret \u{10400}', 't \u{10428}')).toBe(true) // astral case pair
+ })
+
+ it('treats regex metacharacters in the query literally', () => {
+ expect(hit('axb', 'a.b')).toBe(false)
+ expect(hit('see a.b here', 'A.B')).toBe(true)
+ expect(hit('cost $5 (approx)', '$5 (approx)')).toBe(true)
+ expect(hit('path\\to\\file', '\\to\\')).toBe(true)
+ expect(hit('aaa', 'a*')).toBe(false)
+ // Characters that are NOT SyntaxCharacters must stay unescaped: `\-` is a SyntaxError in `u` mode.
+ for (const q of ['a-b', 'x]y', 'p}q', 'a/b', 'a\\', 'oops)', '[]', '$^', '((', '{1,2}', '?+|']) {
+ expect(() => searchEntries(one('irrelevant'), q)).not.toThrow()
+ expect(hit(`pre ${q.toUpperCase()} post`, q)).toBe(true)
+ }
+ })
+
+ it('a stateless matcher: the same query matches every entry, not every other one', () => {
+ const many = Array.from({ length: 5 }, (_, i) => ({ ...one('needle')[0], sessionId: `s${i}`, mtime: i }))
+ expect(searchEntries(many, 'needle')).toHaveLength(5)
+ const re = queryRegExp('needle')
+ expect([re.test('needle'), re.test('needle')]).toEqual([true, true])
+ })
+
+ it('centres the snippet on the same match the filter found', () => {
+ const [h] = searchEntries(one('z'.repeat(400) + ' the \u212Aelvin scale ' + 'z'.repeat(400)), 'kelvin')
+ expect(h.snippet).toContain('\u212Aelvin')
+ })
+})
+
describe('planRefresh', () => {
const prior: TranscriptIndexEntry[] = [
{ sessionId: 'a', transcriptPath: '/p/a.jsonl', cwd: '/c', mtime: 100, title: 't', text: 'x' },
diff --git a/src/core/transcript-index-core.ts b/src/core/transcript-index-core.ts
index 464ce57f5..ccfd9cae8 100644
--- a/src/core/transcript-index-core.ts
+++ b/src/core/transcript-index-core.ts
@@ -43,8 +43,26 @@ export function extractEntryFields(rawText: string): { cwd: string; title: strin
return { cwd: firstCwd(rawText), title, text }
}
-export function makeSnippet(text: string, query: string): string {
- const i = text.toLowerCase().indexOf(query.toLowerCase())
+// One case-insensitive matcher per query, shared by the filter and the snippet. The previous
+// `e.text.toLowerCase().includes(q)` built a lowered copy of every entry on every debounced
+// keystroke: on the user's 16 MB / 235-entry index that measured 20-52 ms and 7-15 MB of garbage
+// per search on the main thread; this RegExp measured 2-13 ms and 0 MB (audit #5, main-2/agents-2,
+// `main-process-bench/search.ts`). Storing a lowered copy instead would retain +32 MB — don't.
+//
+// `iu`, not `i`: neither flag reproduces `toLowerCase().includes` exactly, so the criterion is "no
+// match the old code found is lost". Over every BMP case pair, plain `i` disagreed 160 times and
+// LOST hits (Kelvin sign K, Ångström Å, ẞ, Ohm Ω all stopped matching k/å/ß/ω); `iu` disagreed 94
+// times, every one an EXTRA hit (ſ~s, µ~μ, Greek symbol variants, final ς~σ), and also folds astral
+// pairs (Deseret) that `i` misses. Turkish İ (one char lowering to two) diverges under all three.
+// The escape set is exactly SyntaxCharacter plus `/` — the only identity escapes `u` mode accepts;
+// escaping anything else (`-`, say) is a SyntaxError there. No `g`: `test` would turn stateful.
+export function queryRegExp(query: string): RegExp {
+ return new RegExp(query.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&'), 'iu')
+}
+
+export function makeSnippet(text: string, query: string | RegExp): string {
+ // Same matcher as the filter, so a hit never gets a head-of-text snippet because the two disagreed.
+ const i = (typeof query === 'string' ? queryRegExp(query) : query).exec(text)?.index ?? -1
if (i < 0) return text.slice(0, 160).replace(/\s+/g, ' ').trim()
const start = Math.max(0, i - 60)
return (
@@ -61,16 +79,17 @@ export function searchEntries(
* hit must run under that account, or the CLI answers "No conversation found". */
accountOf?: (transcriptPath: string) => string | undefined
): TranscriptHit[] {
- const q = query.trim().toLowerCase()
+ const q = query.trim()
if (q.length < 2) return []
+ const re = queryRegExp(q)
return entries
- .filter((e) => e.title.toLowerCase().includes(q) || e.text.toLowerCase().includes(q))
+ .filter((e) => re.test(e.title) || re.test(e.text))
.sort((a, b) => b.mtime - a.mtime)
.slice(0, limit)
.map((e) => ({
sessionId: e.sessionId,
title: e.title || e.sessionId,
- snippet: makeSnippet(`${e.title}\n${e.text}`, q),
+ snippet: makeSnippet(`${e.title}\n${e.text}`, re),
cwd: e.cwd,
projectLabel: e.cwd ? e.cwd.split('/').filter(Boolean).pop() ?? e.cwd : '',
mtime: e.mtime,