@lizimin/dsh-session-search 1.3.7 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,15 @@ All package version changes must have an entry here before release. Entries are
4
4
 
5
5
  Versions 1.2.0–1.2.2 were initially installed only locally and their changes first reached GitHub in **1.2.3**. They are now backfilled as separate historical tags/releases from recovered development snapshots; the existing 1.2.3 tag is unchanged. Each 1.3.x version also has its own tag and release. Earlier private-bundle version numbers are not public-package releases. / 1.2.0–1.2.2 最初仅在本地安装,其改动首次随 **1.2.3** 上传 GitHub;现已从开发快照恢复并补发独立历史标签与 Release,原 1.2.3 标签保持不变。1.3.x 各版本也分别发布,不合并成单一版本。更早的私有包版本号不属于本公开包的发布历史。
6
6
 
7
+ ## [1.4.0] - 2026-11-18
8
+
9
+ ### Added / 新增
10
+ - Cache projected documents across searches, per session and content mode, validated by the catalog stamp (`updatedAt` + `formatStatus`) and the Host's liveness signals. Repeat and refined content searches over large profiles no longer re-read and re-parse every session log per keystroke (issue #3: sequential scans of 400+ cold sessions hit a ~0% Host prepared-observation reuse rate and could exhaust the 4 GB Host heap). Sessions the Host reports live are always read fresh, and a live observation drops the session's cached entry. / 按会话与内容模式跨查询缓存投影文档,以目录行戳(`updatedAt` + `formatStatus`)与宿主活跃信号校验。大配置下反复/逐步细化内容搜索不再每次按键都重读并重解析全部会话日志(issue #3:400+ 冷会话的顺序扫描对宿主预备观察的复用率约为 0,可能耗尽 4 GB 宿主堆内存)。宿主报告为活跃的会话始终现读,活跃观察会丢弃该会话的缓存条目。
11
+ - Add a per-scan fresh-read budget (`scanMaxChars`, default 256 MiB of projected text) with `scanTruncated` in the response when a pathological profile stops a scan early; cached sessions never consume the budget, so truncated scans still complete from cache on retry. Fresh reads now yield to the event loop between sessions. / 为每次扫描的新鲜读取加入预算(`scanMaxChars`,默认 256 MiB 投影文本),预算耗尽时响应带 `scanTruncated` 标记;缓存会话不消耗预算,被截断的扫描重试时仍可由缓存完成。会话间的新鲜读取现让出事件循环。
12
+
13
+ ### Changed / 变更
14
+ - Compile the exact pattern and fuzzy folding tables once per query instead of once per session, and compute the cursor version digest only when a page cursor is actually issued, removing avoidable per-session work during long scans. / 精确匹配模式与模糊折叠表改为每次查询编译一次而非每会话一次;游标版本摘要仅在真正下发分页游标时计算,减少长扫描中的每会话开销。
15
+
7
16
  ## [1.3.7] - 2026-11-17
8
17
 
9
18
  ### Fixed / 修复
package/README.md CHANGED
@@ -6,7 +6,7 @@ Search stored DeepSeek Harness (DSH) conversation text across all or selected wo
6
6
 
7
7
  See the bilingual [Changelog](<CHANGELOG.md>) for changes in every package version.
8
8
 
9
- An independent community plugin, not an official DSH package. **Package version: 1.3.7.**
9
+ An independent community plugin, not an official DSH package. **Package version: 1.4.0.**
10
10
 
11
11
  ## Features
12
12
 
package/README.zh-CN.md CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  每个插件包版本的改动见双语[更新日志](<CHANGELOG.md>)。
8
8
 
9
- 本项目为独立社区插件,并非 DSH 官方插件。**插件包版本:1.3.7。**
9
+ 本项目为独立社区插件,并非 DSH 官方插件。**插件包版本:1.4.0。**
10
10
 
11
11
  ## 功能
12
12
 
package/index.js CHANGED
@@ -1,6 +1,7 @@
1
- import {search, highlightText} from './search.js';
1
+ import {search, highlightText, createSessionCache} from './search.js';
2
2
  import {createSettingsOperation, readSettingsBody} from './settings.js';
3
3
  export {Config} from './settings.js';
4
+ export {createSessionCache, CACHE_MAX_CHARS, SCAN_MAX_CHARS} from './search.js';
4
5
  export const inject = ['sessionQuery', 'sessionController', 'workspaceController', 'connection', 'webServer', 'tools', 'configEditor'];
5
6
  const MAX_HIGHLIGHT_BODY_BYTES = 2 * 1024 * 1024;
6
7
  async function readHighlightBody(req) {
@@ -18,7 +19,11 @@ async function readHighlightBody(req) {
18
19
  try {return JSON.parse(Buffer.concat(chunks).toString('utf8'));} catch {throw new Error('Invalid highlight JSON');}
19
20
  }
20
21
  export function apply(ctx, config) {
21
- const operation = (input, signal) => search(ctx, input, signal);
22
+ // One cache for the plugin's lifetime: repeat content searches reuse the
23
+ // projected documents of unchanged cold sessions instead of re-reading and
24
+ // re-parsing every log (issue #3 Host OOM on large profiles).
25
+ const cache = createSessionCache();
26
+ const operation = (input, signal) => search(ctx, input, signal, cache);
22
27
  const settings = createSettingsOperation(ctx, config);
23
28
  ctx.effect(() => ctx.provide('localSessionSearch', {search: operation, settings}));
24
29
  ctx.effect(() => ctx.webServer.register({kind: 'exact', path: '/api/local-session-search-settings', async handler(req, res) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lizimin/dsh-session-search",
3
- "version": "1.3.7",
3
+ "version": "1.4.0",
4
4
  "description": "Search stored DSH conversation text across all or selected workspaces and sessions, with exact, regular-expression, typo-tolerant or word-internal subsequence matching.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/search.js CHANGED
@@ -68,8 +68,7 @@ function compileMatchPattern(spec) {
68
68
  const bounded = spec.wholeWord ? `(?<![\\p{L}\\p{N}_])(?:${source})(?![\\p{L}\\p{N}_])` : source;
69
69
  return spec.wholeWord ? new RegExp(bounded, flags) : rawPattern;
70
70
  }
71
- function matchDocumentList(documents, spec, maxSnippet, maxRanges, fullDocument) {
72
- const pattern = compileMatchPattern(spec);
71
+ function matchDocumentList(documents, spec, maxSnippet, maxRanges, fullDocument, pattern) {
73
72
  const hits = [];
74
73
  let totalRanges = 0, truncated = false;
75
74
  for (const doc of documents) {
@@ -121,36 +120,18 @@ function matchDocumentList(documents, spec, maxSnippet, maxRanges, fullDocument)
121
120
  // and snippet work run within the SAME per-session VM deadline; none run in the
122
121
  // service/catalog loop. Literal presence is independent of fuzzy candidates:
123
122
  // exact substrings can exist even when a whole candidate word is not a fuzzy hit.
124
- function matchFuzzyDocumentList(documents, spec, maxSnippet, maxRanges, fullDocument) {
125
- const exactPattern = compileMatchPattern(spec);
123
+ // The per-QUERY compilation (exact pattern, token stream, case-folding table)
124
+ // is hoisted to compileFuzzyTable and built once per search, not once per
125
+ // session; only per-session matching work runs under the VM deadline.
126
+ function matchFuzzyDocumentList(documents, spec, maxSnippet, maxRanges, fullDocument, fuzzy) {
127
+ const exactPattern = fuzzy.exactPattern;
126
128
  let hasExactMatches = false;
127
129
  if (!fullDocument) for (const doc of documents) {
128
130
  exactPattern.lastIndex = 0;
129
131
  if (exactPattern.test(doc.text)) {hasExactMatches = true; break;}
130
132
  }
131
- const wordCharacter = /[\p{L}\p{N}_]/u;
132
- const wordAt = /[\p{L}\p{N}_]+/uy;
133
- const spaceAt = /\s+/uy;
134
- const characters = new Map();
135
- function comparable(character) {
136
- if (spec.matchCase) return candidate => candidate === character;
137
- // Unicode simple case folding, like the exact /iu branch. Folding a whole
138
- // string with toLowerCase() can expand İ and corrupt both matching/offsets.
139
- if (!characters.has(character)) {
140
- const escaped = character.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
141
- const pattern = new RegExp(`^(?:${escaped})$`, 'iu');
142
- characters.set(character, candidate => pattern.test(candidate));
143
- }
144
- return characters.get(character);
145
- }
146
- const tokens = Array.from(spec.query.trim().matchAll(/[\p{L}\p{N}_]+|\s+|[^\p{L}\p{N}_\s]/gu), match => {
147
- const value = match[0];
148
- if (/^\s/u.test(value)) return {kind: 'space'};
149
- if (!wordCharacter.test(value)) return {kind: 'literal', value};
150
- const points = Array.from(value);
151
- return {kind: 'word', length: points.length, equal: points.map(comparable),
152
- edits: points.length <= 3 ? 0 : points.length <= 7 ? 1 : 2};
153
- });
133
+ const wordCharacter = fuzzy.wordCharacter, wordAt = fuzzy.wordAt, spaceAt = fuzzy.spaceAt;
134
+ const characters = fuzzy.characters, comparable = fuzzy.comparable, tokens = fuzzy.tokens;
154
135
  function wordMatch(token, value, offset) {
155
136
  const limit = spec.fuzzy === 'typo' ? token.length + token.edits : 4 * token.length + 8;
156
137
  // Reject enormous candidate words before allocating a code-point array.
@@ -293,14 +274,60 @@ function matchFuzzyDocumentList(documents, spec, maxSnippet, maxRanges, fullDocu
293
274
  }
294
275
  return JSON.stringify(fullDocument ? {items: hits, ...(truncated ? {truncated: true} : {})} : {hits, hasExactMatches});
295
276
  }
296
- const matchingScript = new Script(`${compileMatchPattern.toString()}\n(${matchDocumentList.toString()})(documents, spec, maxSnippet, maxRanges, fullDocument)`, {filename: 'session-search-matcher-v1.7.vm.js'});
297
- const fuzzyMatchingScript = new Script(`${compileMatchPattern.toString()}\n(${matchFuzzyDocumentList.toString()})(documents, spec, maxSnippet, maxRanges, fullDocument)`, {filename: 'session-search-fuzzy-v1.7.vm.js'});
298
- function matchDocuments(documents, spec, fullDocument) {
277
+ // Per-QUERY fuzzy compilation: exact pattern, token stream, and the Unicode
278
+ // case-folding table depend only on `spec`, never on a session's documents, so
279
+ // they are built once per search in the host context instead of once per
280
+ // session inside the VM. The closures are shared read-only; sticky regexes are
281
+ // always re-anchored before use, and VM runs are synchronous, so interleaved
282
+ // concurrent searches cannot observe torn state.
283
+ function compileFuzzyTable(spec, exactPattern) {
284
+ const wordCharacter = /[\p{L}\p{N}_]/u;
285
+ const wordAt = /[\p{L}\p{N}_]+/uy;
286
+ const spaceAt = /\s+/uy;
287
+ const characters = new Map();
288
+ function comparable(character) {
289
+ if (spec.matchCase) return candidate => candidate === character;
290
+ // Unicode simple case folding, like the exact /iu branch. Folding a whole
291
+ // string with toLowerCase() can expand İ and corrupt both matching/offsets.
292
+ if (!characters.has(character)) {
293
+ const escaped = character.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
294
+ const pattern = new RegExp(`^(?:${escaped})$`, 'iu');
295
+ characters.set(character, candidate => pattern.test(candidate));
296
+ }
297
+ return characters.get(character);
298
+ }
299
+ const tokens = Array.from(spec.query.trim().matchAll(/[\p{L}\p{N}_]+|\s+|[^\p{L}\p{N}_\s]/gu), match => {
300
+ const value = match[0];
301
+ if (/^\s/u.test(value)) return {kind: 'space'};
302
+ if (!wordCharacter.test(value)) return {kind: 'literal', value};
303
+ const points = Array.from(value);
304
+ return {kind: 'word', length: points.length, equal: points.map(comparable),
305
+ edits: points.length <= 3 ? 0 : points.length <= 7 ? 1 : 2};
306
+ });
307
+ return {exactPattern, wordCharacter, wordAt, spaceAt, characters, comparable, tokens};
308
+ }
309
+ // Compile everything a query needs before any service read, so an invalid
310
+ // expression fails fast with the same error either path would have produced,
311
+ // and per-session VM work never recompiles query-wide state.
312
+ function compileMatchers(spec) {
313
+ try {
314
+ const pattern = compileMatchPattern(spec);
315
+ return {pattern, fuzzy: spec.fuzzy === 'off' ? undefined : compileFuzzyTable(spec, pattern)};
316
+ } catch (error) {
317
+ if (error instanceof SyntaxError) throw new SearchMatchError(`Invalid regular expression: ${error.message}`, 'INVALID_REGEX');
318
+ throw error;
319
+ }
320
+ }
321
+ const matchingScript = new Script(`(${matchDocumentList.toString()})(documents, spec, maxSnippet, maxRanges, fullDocument, pattern)`, {filename: 'session-search-matcher-v1.8.vm.js'});
322
+ const fuzzyMatchingScript = new Script(`(${matchFuzzyDocumentList.toString()})(documents, spec, maxSnippet, maxRanges, fullDocument, fuzzy)`, {filename: 'session-search-fuzzy-v1.8.vm.js'});
323
+ function matchDocuments(documents, spec, fullDocument, pattern, fuzzy) {
299
324
  try {
300
325
  // One deadline covers the complete per-session document list, NOT one
301
326
  // budget per candidate or exec call. No regex executes outside this VM.
302
327
  const script = spec.fuzzy === 'off' ? matchingScript : fuzzyMatchingScript;
303
- return JSON.parse(script.runInNewContext({documents, spec, fullDocument, maxSnippet: MAX_SNIPPET,
328
+ return JSON.parse(script.runInNewContext({documents, spec, fullDocument,
329
+ ...(spec.fuzzy === 'off' ? {pattern} : {fuzzy}),
330
+ maxSnippet: MAX_SNIPPET,
304
331
  maxRanges: fullDocument ? MAX_HIGHLIGHT_RANGES : MAX_RANGES},
305
332
  {timeout: MATCH_TIMEOUT_MS, contextCodeGeneration: {strings: false, wasm: false}}));
306
333
  } catch (error) {
@@ -312,6 +339,69 @@ function matchDocuments(documents, spec, fullDocument) {
312
339
  }
313
340
  }
314
341
 
342
+ // Cross-query cache of PROJECTED documents, never of raw events. The DSH Host
343
+ // retains only a handful of cold prepared observations (SESSION_QUERY_DEFAULT_
344
+ // PREPARED_SESSION_CACHE_SIZE = 5), so sequential content scans over hundreds
345
+ // of sessions re-read and re-parse nearly every log on EVERY query — with the
346
+ // 300ms live-search debounce this multiplies transient heap until the Host
347
+ // process dies. Caching the projection (text documents, one entry per session
348
+ // and content mode) keyed by the catalog's cheap row stamp keeps repeat scans
349
+ // off the engine entirely.
350
+ //
351
+ // Freshness contract, verified against dsh-session-query@0.2.2-rc.3:
352
+ // - `SessionObservation.revision` (the durable persistence revision) exists
353
+ // ONLY on cold prepared observations, and no cheap per-session probe exposes
354
+ // it without paying for the read, so it cannot validate entries between
355
+ // queries. It is recorded for drift detection whenever an observation
356
+ // happens anyway.
357
+ // - Rows currently live in the Host (`running`/`agentAvailable`) are never
358
+ // served from cache and always observed; a live observation drops the
359
+ // session's entry. DSH appends (replies, compaction, pruning) happen on live
360
+ // sessions, so a COLD session's log cannot change underneath the stamp.
361
+ // - The stamp pairs the catalog row's `updatedAt` (latest prompt time) with
362
+ // `formatStatus`; any new prompt or persistence migration changes it.
363
+ // - Residual gap: hand-edited session files on disk while cold. DSH's own FTS
364
+ // catches that via stat revisions; we accept and document it.
365
+ export const CACHE_MAX_CHARS = 32 * 1024 * 1024;
366
+ export const SCAN_MAX_CHARS = 256 * 1024 * 1024;
367
+ const CACHE_ENTRY_MAX_CHARS = CACHE_MAX_CHARS >> 2;
368
+ const yieldToEventLoop = () => new Promise(resolve => setImmediate(resolve));
369
+ const documentsChars = documents => {let chars = 0; for (const document of documents) chars += document.text.length; return chars;};
370
+ export function createSessionCache(maxChars = CACHE_MAX_CHARS) {
371
+ const entries = new Map(); // `${sessionId}\u0000${content}` → entry, LRU-ordered
372
+ let total = 0;
373
+ const stamp = row => `${row.updatedAt ?? 0}|${row.formatStatus ?? ''}`;
374
+ const forget = key => {const entry = entries.get(key); if (entry) {total -= entry.chars; entries.delete(key);}};
375
+ return {
376
+ get(row, content) {
377
+ const key = `${row.sessionId}\u0000${content}`;
378
+ const entry = entries.get(key);
379
+ // Reuse only while the cheap catalog stamp is unchanged; any mismatch
380
+ // falls through to a fresh observation, which repairs or drops the entry.
381
+ if (!entry || entry.stamp !== stamp(row)) return undefined;
382
+ entries.delete(key); entries.set(key, entry); // most-recently-used last
383
+ return entry.documents;
384
+ },
385
+ record(row, content, observation, documents) {
386
+ const key = `${row.sessionId}\u0000${content}`;
387
+ if (observation.source !== 'prepared' || observation.revision === undefined) {forget(key); return;}
388
+ forget(key); // replace on drift; the stamp below revalidates future reuse
389
+ const chars = documentsChars(documents);
390
+ // One oversized session must not flush the whole working set.
391
+ if (chars > Math.min(CACHE_ENTRY_MAX_CHARS, maxChars)) return;
392
+ entries.set(key, {documents, chars, stamp: stamp(row), revision: observation.revision,
393
+ cursor: observation.cursor, inheritedEventCount: observation.inheritedEventCount});
394
+ total += chars;
395
+ while (total > maxChars && entries.size > 1) {
396
+ const oldest = entries.keys().next().value;
397
+ forget(oldest);
398
+ }
399
+ },
400
+ clear() {entries.clear(); total = 0;},
401
+ stats() {return {entries: entries.size, chars: total};},
402
+ };
403
+ }
404
+
315
405
  function matchOptions(input) {
316
406
  const matchCase = flag(input.matchCase, 'matchCase');
317
407
  const wholeWord = flag(input.wholeWord, 'wholeWord');
@@ -343,10 +433,11 @@ export function highlightText(input) {
343
433
  if (size > MAX_HIGHLIGHT_TEXT) throw new Error(`texts exceed ${MAX_HIGHLIGHT_TEXT} UTF-16 code units`);
344
434
  return {text, index};
345
435
  });
346
- return matchDocuments(documents, {query, ...options}, {maxTotalRanges: MAX_HIGHLIGHT_TOTAL_RANGES});
436
+ const matchers = compileMatchers({query, ...options});
437
+ return matchDocuments(documents, {query, ...options}, {maxTotalRanges: MAX_HIGHLIGHT_TOTAL_RANGES}, matchers.pattern, matchers.fuzzy);
347
438
  }
348
439
 
349
- export async function search(services, input, signal = new AbortController().signal) {
440
+ export async function search(services, input, signal = new AbortController().signal, cache = undefined, limits = {}) {
350
441
  if (!input || typeof input !== 'object' || Array.isArray(input)) throw new Error('Expected a search request');
351
442
  signal.throwIfAborted();
352
443
  const action = choice(input.action, 'search', ['sessions', 'search', 'groups'], 'action');
@@ -371,8 +462,11 @@ export async function search(services, input, signal = new AbortController().sig
371
462
  } catch {throw new Error('Invalid search cursor. Search again.');}
372
463
  }
373
464
  const spec = {query, matchCase, wholeWord, useRegex, fuzzy};
374
- // Validate even with an empty/excluded corpus, before any service reads.
375
- if (action !== 'sessions') matchDocuments([], spec);
465
+ // Compile the whole query (exact pattern plus fuzzy tables) once per
466
+ // search, before any service reads and before any per-session VM run.
467
+ // Sessions actions never match documents, so they skip compilation.
468
+ const matchers = action === 'sessions' ? undefined : compileMatchers(spec);
469
+ if (matchers) matchDocuments([], spec, undefined, matchers.pattern, matchers.fuzzy);
376
470
  const {items: rows, workspaces} = await catalog(services, options, signal);
377
471
  const workspaceSelection = new Set(workspaceIds), sessionSelection = new Set(sessionIds);
378
472
  const byWorkspace = new Map(workspaces.map(item => [item.workspaceId, item]));
@@ -393,25 +487,50 @@ export async function search(services, input, signal = new AbortController().sig
393
487
  sessionIds, workspaceIds, after: after ?? null, before: before ?? null};
394
488
  if (action === 'sessions') return {...scope, ...options, items: eligible};
395
489
  const items = [], warnings = [];
396
- let hasExactMatches = false;
490
+ let hasExactMatches = false, scanned = 0, scanTruncated = false;
491
+ // Fresh reads materialize and parse whole session logs before projection;
492
+ // budget them per scan so a pathological profile truncates instead of
493
+ // exhausting Host heap. Cached projections cost nothing against the budget.
494
+ const {scanMaxChars = SCAN_MAX_CHARS} = limits;
495
+ let scannedChars = 0;
496
+ const applyProjection = (row, projected) => {
497
+ // Project full turns first: cutting events can erase a turn's completion
498
+ // or wrongly promote an earlier assistant message into its final reply.
499
+ // Filter before BOTH fuzzy exact-presence scans and hit aggregation.
500
+ const documents = bounded ? projected.filter(doc => Number.isSafeInteger(doc.time) && Math.abs(doc.time) <= MAX_DATE_MS &&
501
+ (after === undefined || doc.time >= after) && (before === undefined || doc.time < before)) : projected;
502
+ const matched = matchDocuments(documents, spec, undefined, matchers.pattern, matchers.fuzzy);
503
+ const hits = fuzzy === 'off' ? matched : matched.hits;
504
+ if (fuzzy !== 'off' && matched.hasExactMatches) hasExactMatches = true;
505
+ for (const hit of hits) items.push({...row, ...hit});
506
+ };
397
507
  for (const row of eligible) {
398
508
  signal.throwIfAborted();
399
509
  try {
400
- const observation = await services.sessionQuery.observeSession(row.sessionId, {signal, projectionMode: 'none'});
401
- try {
402
- const project = content === 'final' ? finalDocuments : processDocuments;
403
- const projected = project(observation.events, row.sessionId);
404
- // Project full turns first: cutting events can erase a turn's completion
405
- // or wrongly promote an earlier assistant message into its final reply.
406
- // Filter before BOTH fuzzy exact-presence scans and hit aggregation.
407
- const documents = bounded ? projected.filter(doc => Number.isSafeInteger(doc.time) && Math.abs(doc.time) <= MAX_DATE_MS &&
408
- (after === undefined || doc.time >= after) && (before === undefined || doc.time < before)) : projected;
409
- const matched = matchDocuments(documents, spec);
410
- const hits = fuzzy === 'off' ? matched : matched.hits;
411
- if (fuzzy !== 'off' && matched.hasExactMatches) hasExactMatches = true;
412
- for (const hit of hits) items.push({...row, ...hit});
413
- } finally {observation[Symbol.dispose]();}
414
- signal.throwIfAborted();
510
+ // Live sessions are never served from cache and always observed fresh:
511
+ // DSH appends land on live sessions, so any cached projection of one is
512
+ // stale by definition.
513
+ const live = row.running === true || row.agentAvailable === true;
514
+ const cachedDocuments = live || !cache ? undefined : cache.get(row, content);
515
+ let projected = cachedDocuments;
516
+ if (projected) {
517
+ // Cache hit: the projection was validated against the catalog stamp.
518
+ } else {
519
+ if (scannedChars > scanMaxChars) {scanTruncated = true; break;}
520
+ const observation = await services.sessionQuery.observeSession(row.sessionId, {signal, projectionMode: 'none'});
521
+ try {
522
+ projected = (content === 'final' ? finalDocuments : processDocuments)(observation.events, row.sessionId);
523
+ // Cached documents are independent of the observation's lifetime.
524
+ if (cache) cache.record(row, content, observation, projected);
525
+ } finally {observation[Symbol.dispose]();}
526
+ scannedChars += documentsChars(projected);
527
+ // Breathing room between heavy reads keeps one long scan from
528
+ // starving the Host's event loop and lets aborts land promptly.
529
+ await yieldToEventLoop();
530
+ signal.throwIfAborted();
531
+ }
532
+ applyProjection(row, projected);
533
+ scanned++;
415
534
  } catch (error) {
416
535
  signal.throwIfAborted();
417
536
  // A pathological expression must stop ALL scopes, never silently hide
@@ -426,8 +545,11 @@ export async function search(services, input, signal = new AbortController().sig
426
545
  // Bind group cursors to the full hits too: changing a non-extremal matching
427
546
  // reply must invalidate a page even when its group's count/time is unchanged.
428
547
  const exactMetadata = fuzzy === 'off' ? {} : {hasExactMatches};
429
- const version = digest(fuzzy === 'off' ? [items, warnings] : [items, warnings, exactMetadata]);
430
- if (cursor && cursor.version !== version) throw new Error('History changed. Search again to refresh results.');
548
+ // The version digest hashes the complete hit set and only exists to bind
549
+ // page cursors; compute it lazily so cursor-less result pages never pay.
550
+ let versionDigest;
551
+ const version = () => versionDigest ??= digest(fuzzy === 'off' ? [items, warnings] : [items, warnings, exactMetadata]);
552
+ if (cursor && cursor.version !== version()) throw new Error('History changed. Search again to refresh results.');
431
553
  let results = items;
432
554
  if (action === 'groups') {
433
555
  const metadata = new Map(eligible.map(row => [row.sessionId, row]));
@@ -447,8 +569,9 @@ export async function search(services, input, signal = new AbortController().sig
447
569
  const offset = cursor?.offset ?? 0;
448
570
  if (offset > results.length) throw new Error('Invalid search cursor. Search again.');
449
571
  const end = offset + LIMIT;
450
- return {...scope, query, ...options, ...exactMetadata, items: results.slice(offset, end), total: results.length, scanned: eligible.length,
572
+ return {...scope, query, ...options, ...exactMetadata, items: results.slice(offset, end), total: results.length, scanned,
573
+ ...(scanTruncated ? {scanTruncated: true} : {}),
451
574
  ...(action === 'groups' ? {totalMatches: items.length} : {}),
452
575
  ...(warnings.length ? {warnings} : {}),
453
- ...(end < results.length ? {nextCursor: Buffer.from(JSON.stringify({key, version, offset: end})).toString('base64url')} : {})};
576
+ ...(end < results.length ? {nextCursor: Buffer.from(JSON.stringify({key, version: version(), offset: end})).toString('base64url')} : {})};
454
577
  }
@@ -90,6 +90,12 @@ export async function catalog(services, input, signal) {
90
90
  return {sessionId: row.sessionId, title: (row.projections?.values?.title ?? titles.get(row.sessionId))?.trim() || '', updatedAt: row.updatedAt,
91
91
  cwd: row.cwd || '', archived: archived.has(row.sessionId), subagent: row.origin === 'subagent',
92
92
  workspaceId: workspace.workspaceId, workspaceTitle: workspace.title, workspacePath: workspace.path,
93
+ // Pass through the Host's liveness and persistence-migration signals:
94
+ // the search cache refuses to serve sessions the Host reports live and
95
+ // revalidates entries when a persistence migration changes formatStatus.
96
+ ...(row.running !== undefined ? {running: row.running} : {}),
97
+ ...(row.agentAvailable !== undefined ? {agentAvailable: row.agentAvailable} : {}),
98
+ ...(row.formatStatus !== undefined ? {formatStatus: row.formatStatus} : {}),
93
99
  ...(row.parentSessionId ? {parentSession: row.parentSessionId} : {})};
94
100
  });
95
101
  return {items, workspaces};