Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

Session search: a word typed out ranks above turns that only begin with it (#1151)

Dee10c9e742026d
diff --git a/cli/src/lib/sessionSearch/store.spec.ts b/cli/src/lib/sessionSearch/store.spec.ts
index 93b0db567..5707ee479 100644
--- a/cli/src/lib/sessionSearch/store.spec.ts
+++ b/cli/src/lib/sessionSearch/store.spec.ts
@@ -4,7 +4,7 @@ import { join } from 'node:path'
import { afterEach, describe, expect, it } from 'vitest'
-import { MARK_CLOSE, MARK_OPEN, SessionSearchStore, makeSnippet, queryTerms, type IndexedSession } from './store.js'
+import { MARK_CLOSE, MARK_OPEN, SessionSearchStore, makeSnippet, queryTerms, wordFamily, type IndexedSession } from './store.js'
import type { IndexedTurn } from './turns.js'
const DAY = 86_400_000
@@ -132,6 +132,30 @@ describe('SessionSearchStore', () => {
expect(hits.find((hit) => hit.sessionId === 'dial')).toMatchObject({ turn: 1, field: 'ask', agentId: 'agent-dial', engine: 'claude' })
})
+ // On one Mac's index (2026-10-10) "dark mode" put a desk-lamp *model* first and "mode" found only "model".
+ it('puts a word typed out above turns that only begin with it, and otherwise keeps the order', () => {
+ const store = open()
+ store.writeSession(session('lamp', 'Desk lamp', NOW), 0, [turn(0, 'model a desk lamp in the dark room')])
+ store.writeSession(session('theme', 'Theme', NOW - 5 * DAY), 0, [turn(0, 'the dark modes need more contrast')])
+ store.writeSession(session('night', 'Night', NOW - 9 * DAY), 0, [turn(0, 'dark mode by default at night')])
+ expect(store.search('dark mode', { now: NOW }).map((hit) => hit.sessionId)).toEqual(['theme', 'night', 'lamp'])
+ // A word still being typed begins every hit alike, so it moves nothing: relevance and recency decide, as
+ // before (two words in one turn, then the newer), and the lamp is no longer last.
+ expect(store.search('dark mod', { now: NOW }).map((hit) => hit.sessionId)).toEqual(['theme', 'lamp', 'night'])
+ // A turn holding the word as a word elsewhere is not one that only begins with it.
+ store.writeSession(session('both', 'Both', NOW), 0, [turn(0, 'the model picker', 'and the plan mode')])
+ expect(store.search('mode', { now: NOW, limit: 2 }).map((hit) => hit.sessionId)).toEqual(['both', 'theme'])
+ })
+
+ it('knows a word by its common endings, the index keeping words as written', () => {
+ expect(wordFamily('mode')).toEqual(['mode', 'modes', 'moded', 'moding'])
+ expect(wordFamily('search')).toEqual(['search', 'searchs', 'searches', 'searched', 'searching'])
+ expect(wordFamily('copy')).toEqual(['copy', 'copys', 'copyes', 'copyed', 'copying', 'copies', 'copied'])
+ expect(wordFamily('play')).not.toContain('plaies')
+ expect(wordFamily('v2')).toEqual(['v2'])
+ expect(wordFamily('a')).toEqual(['a'])
+ })
+
it('ranks a turn holding every word above a session that has them only in different turns', () => {
const store = open()
store.writeSession(session('together', 'A', NOW - 20 * DAY), 0, [turn(0, 'port the daemon to windows')])
diff --git a/cli/src/lib/sessionSearch/store.ts b/cli/src/lib/sessionSearch/store.ts
index ae891fc3d..3b1e196c4 100644
--- a/cli/src/lib/sessionSearch/store.ts
+++ b/cli/src/lib/sessionSearch/store.ts
@@ -215,6 +215,25 @@ function wordPattern(parts: string[]): RegExp {
return new RegExp(`(?<![\\p{L}\\p{N}])${body}[\\p{L}\\p{N}]*`, 'giu')
}
+/**
+ * A word as typed and its common English endings: "mode" is mode, modes, moded and moding, never "model". The
+ * index keeps words as written (unicode61, no stemming), so a typed-out word would otherwise miss its plural.
+ */
+export function wordFamily(word: string): string[] {
+ if (!/^\p{L}{2,}$/u.test(word)) return [word]
+ const forms = new Set([word, `${word}s`])
+ if (word.endsWith('e')) { forms.add(`${word}d`); forms.add(`${word.slice(0, -1)}ing`) }
+ else { forms.add(`${word}es`); forms.add(`${word}ed`); forms.add(`${word}ing`) }
+ if (/[^aeiou]y$/u.test(word)) { forms.add(`${word.slice(0, -1)}ies`); forms.add(`${word.slice(0, -1)}ied`) }
+ return [...forms]
+}
+
+/** Where a query word occurs as a whole word, in any form of its family; a phrase of parts, as itself. */
+function wholePattern(parts: string[]): RegExp {
+ const body = parts.length > 1 ? parts.map(escapeRegExp).join('[^\\p{L}\\p{N}]+') : `(?:${wordFamily(parts[0]).map(escapeRegExp).join('|')})`
+ return new RegExp(`(?<![\\p{L}\\p{N}])${body}(?![\\p{L}\\p{N}])`, 'giu')
+}
+
const SNIPPET_BEFORE = 4
const SNIPPET_WORDS = 12
@@ -685,13 +704,17 @@ export class SessionSearchStore {
return { sid, match, session, score }
})
.sort((a, b) => Number(b.match.together) - Number(a.match.together) || b.score - a.score)
- .slice(0, limit)
+ // A few more than are shown, for step 5 to choose from.
+ .slice(0, Math.min(100, limit * 3))
// 4. For the hits only: which field matched, and the words around it — what the person asked
// says the most about a session, then its name, then the answer, then the tools.
- const patterns = queryWords(query).map(wordPattern)
+ const words = queryWords(query)
+ const patterns = words.map(wordPattern)
+ const wholes = words.map(wholePattern)
+ const holds = (pattern: RegExp, text: string) => { pattern.lastIndex = 0; return pattern.test(text) }
const text = this.statement('SELECT name, ask, answer, tools FROM turns WHERE id = CAST(? AS INTEGER)')
- return ranked.map(({ sid, match, session, score }) => {
+ const hits = ranked.map(({ sid, match, session, score }) => {
const row = text.get(match.id)
let field: (typeof FIELDS)[number] = 'ask'
let snippet = ''
@@ -703,7 +726,7 @@ export class SessionSearchStore {
break
}
}
- return {
+ const hit: SearchHit = {
sessionId: sid,
agentId: session.agentId,
engine: session.engine,
@@ -716,7 +739,18 @@ export class SessionSearchStore {
score: Math.round(score * 1000) / 1000,
...externalOf(session),
}
+ // A word typed out that this turn holds only as the start of a longer one: "mode" in "model".
+ const said = FIELDS.map((name) => String(row?.[name] ?? '')).join('\n')
+ return { hit, partial: words.some((_, at) => holds(patterns[at], said) && !holds(wholes[at], said)) }
})
+ // 5. Such a hit goes below the ones that hold the words as words; otherwise the order above stands. On one
+ // Mac's index (2026-10-10) "dark mode" put a desk-lamp *model* first and "mode" found only "model". A
+ // word still being typed is a part of every hit alike, so it moves nothing.
+ return hits
+ .map((entry, at) => ({ ...entry, at }))
+ .sort((a, b) => Number(b.hit.together) - Number(a.hit.together) || Number(a.partial) - Number(b.partial) || a.at - b.at)
+ .slice(0, limit)
+ .map((entry) => entry.hit)
}
/** The last window asked about, and its sessions: typing on, the window stays the same. */