@threadbase-sh/scanner 0.22.0 → 0.24.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/dist/cli.js CHANGED
@@ -5,7 +5,7 @@ import { Command } from "commander";
5
5
  import pino2 from "pino";
6
6
 
7
7
  // package.json
8
- var version = "0.22.0";
8
+ var version = "0.24.0";
9
9
 
10
10
  // src/logger.ts
11
11
  import pino from "pino";
@@ -977,7 +977,8 @@ function initialReducerState() {
977
977
  snippetParts: [],
978
978
  previewLength: 0,
979
979
  snippetLength: 0,
980
- badJsonLines: 0
980
+ badJsonLines: 0,
981
+ turnDurations: []
981
982
  };
982
983
  }
983
984
  function reduceLine(state, entry, tier) {
@@ -991,6 +992,8 @@ function reduceLine(state, entry, tier) {
991
992
  const ts = entry.timestamp;
992
993
  if (!state.latestTimestamp || ts > state.latestTimestamp) state.latestTimestamp = ts;
993
994
  }
995
+ const turnDuration = turnDurationOf(entry);
996
+ if (turnDuration) state.turnDurations?.push(turnDuration);
994
997
  const type = entry.type;
995
998
  if (type === "last-prompt") {
996
999
  if (entry.lastPrompt && !state.lastPrompt) state.lastPrompt = entry.lastPrompt;
@@ -1057,6 +1060,7 @@ function finalizeMeta(state, filePath, account, tier) {
1057
1060
  provider: CLAUDE_CODE_PROVIDER,
1058
1061
  sessionId: state.sessionId || basename2(filePath, ".jsonl"),
1059
1062
  sessionName: resolveSessionName(state.nameSlots).name ?? "",
1063
+ nameSlots: state.nameSlots,
1060
1064
  projectPath,
1061
1065
  projectName: getShortProjectName(projectPath),
1062
1066
  account,
@@ -1088,6 +1092,16 @@ function getShortProjectName(fullPath) {
1088
1092
  }
1089
1093
 
1090
1094
  // src/parser.ts
1095
+ function turnDurationOf(entry) {
1096
+ if (entry.type !== "system" || entry.subtype !== "turn_duration" || typeof entry.durationMs !== "number") {
1097
+ return null;
1098
+ }
1099
+ return {
1100
+ durationMs: entry.durationMs,
1101
+ messageCount: entry.messageCount || 0,
1102
+ uuid: entry.uuid
1103
+ };
1104
+ }
1091
1105
  async function parseMeta(filePath, account, tier, onEntry) {
1092
1106
  const log = getLogger();
1093
1107
  log.trace({ filePath, account, tier: tier.name }, "parseMeta: start");
@@ -1141,12 +1155,9 @@ async function parseConversation(filePath, account) {
1141
1155
  badJsonLines++;
1142
1156
  continue;
1143
1157
  }
1144
- if (entry.type === "system" && entry.subtype === "turn_duration" && typeof entry.durationMs === "number") {
1145
- turnDurations.push({
1146
- durationMs: entry.durationMs,
1147
- messageCount: entry.messageCount || 0,
1148
- uuid: entry.uuid
1149
- });
1158
+ const turnDuration = turnDurationOf(entry);
1159
+ if (turnDuration) {
1160
+ turnDurations.push(turnDuration);
1150
1161
  continue;
1151
1162
  }
1152
1163
  const message = reduceConvLine(state, entry);
@@ -1363,7 +1374,7 @@ async function streamMessages(filePath, startOffset, startLine, state, onMessage
1363
1374
  }
1364
1375
  return { offset, line };
1365
1376
  }
1366
- async function buildCheckpoints(filePath, interval = CHECKPOINT_INTERVAL, from = null) {
1377
+ async function buildCheckpoints(filePath, interval = CHECKPOINT_INTERVAL, from = null, onEntry) {
1367
1378
  const checkpoints = [];
1368
1379
  const state = from ? from.state : initialConvState();
1369
1380
  let index = from ? from.messageIndex : 0;
@@ -1383,7 +1394,8 @@ async function buildCheckpoints(filePath, interval = CHECKPOINT_INTERVAL, from =
1383
1394
  });
1384
1395
  }
1385
1396
  return false;
1386
- }
1397
+ },
1398
+ onEntry
1387
1399
  );
1388
1400
  return checkpoints;
1389
1401
  }
@@ -1404,6 +1416,41 @@ async function readPage(filePath, total, options, floor) {
1404
1416
  applyTeamInfo(window, state);
1405
1417
  return { messages: window, total, fromIndex };
1406
1418
  }
1419
+ function isInjectedTurn(message, isInjected) {
1420
+ return message.role === "user" && isInjected(message.text ?? "");
1421
+ }
1422
+ async function countLeadingInjected(filePath, isInjected = isCodexInjectedContext) {
1423
+ let skip = 0;
1424
+ await streamMessages(filePath, 0, 0, initialConvState(), (message) => {
1425
+ if (!isInjectedTurn(message, isInjected)) return true;
1426
+ skip += 1;
1427
+ return false;
1428
+ });
1429
+ return skip;
1430
+ }
1431
+ function filteredWindow(rawTotal, skip, options) {
1432
+ const total = Math.max(0, rawTotal - skip);
1433
+ const beforeIndex = options.beforeIndex ?? total;
1434
+ const fromIndex = Math.max(0, beforeIndex - options.limit);
1435
+ return {
1436
+ total,
1437
+ fromIndex,
1438
+ raw: { beforeIndex: beforeIndex + skip, limit: beforeIndex - fromIndex }
1439
+ };
1440
+ }
1441
+ function pageOfConversation(conversation, options) {
1442
+ const { messages } = conversation;
1443
+ const isInjected = options.isInjectedContext ?? isCodexInjectedContext;
1444
+ const firstKept = messages.findIndex((m) => !isInjectedTurn(m, isInjected));
1445
+ const skip = firstKept === -1 ? messages.length : firstKept;
1446
+ const { total, fromIndex, raw } = filteredWindow(messages.length, skip, options);
1447
+ return {
1448
+ messages: messages.slice(raw.beforeIndex - raw.limit, raw.beforeIndex),
1449
+ total,
1450
+ fromIndex,
1451
+ turnDurations: conversation.turnDurations
1452
+ };
1453
+ }
1407
1454
 
1408
1455
  // src/persistent/conversation-stream.ts
1409
1456
  async function foldTail(filePath, resume) {
@@ -1785,6 +1832,7 @@ function reduceCodexEntry(acc, entry, tier) {
1785
1832
  function finalizeCodexMeta(acc, filePath, account, tier) {
1786
1833
  if (acc.messageCount === 0) return null;
1787
1834
  const sessionId = acc.sessionId || basename5(filePath, ".jsonl");
1835
+ const nameSlots = { ...acc.nameSlots, ...codexNameSlotsFor(filePath, acc.sessionId) };
1788
1836
  const projectPath = acc.cwd;
1789
1837
  const kind = acc.lastAssistant === null && acc.toolNames.length > 0 ? "task" : "conversation";
1790
1838
  return {
@@ -1794,7 +1842,8 @@ function finalizeCodexMeta(acc, filePath, account, tier) {
1794
1842
  kind,
1795
1843
  externalSessionId: acc.sessionId || void 0,
1796
1844
  sessionId,
1797
- sessionName: resolveSessionName({ ...acc.nameSlots, ...codexNameSlotsFor(filePath, acc.sessionId) }).name ?? "",
1845
+ sessionName: resolveSessionName(nameSlots).name ?? "",
1846
+ nameSlots,
1798
1847
  projectPath,
1799
1848
  projectName: getShortProjectName3(projectPath),
1800
1849
  account,
@@ -2080,6 +2129,7 @@ function reduceCopilotEntry(acc, entry, tier) {
2080
2129
  function finalizeCopilotMeta(acc, filePath, account, tier) {
2081
2130
  if (acc.messageCount === 0) return null;
2082
2131
  const sessionId = acc.sessionId || sessionIdFromPath(filePath);
2132
+ const nameSlots = { ...acc.nameSlots, ...copilotNameSlotsFor(filePath) };
2083
2133
  const kind = acc.lastAssistant === null && acc.toolNames.length > 0 ? "task" : "conversation";
2084
2134
  return {
2085
2135
  id: filePath,
@@ -2088,7 +2138,8 @@ function finalizeCopilotMeta(acc, filePath, account, tier) {
2088
2138
  kind,
2089
2139
  externalSessionId: sessionId,
2090
2140
  sessionId,
2091
- sessionName: resolveSessionName({ ...acc.nameSlots, ...copilotNameSlotsFor(filePath) }).name ?? "",
2141
+ sessionName: resolveSessionName(nameSlots).name ?? "",
2142
+ nameSlots,
2092
2143
  projectPath: acc.cwd,
2093
2144
  projectName: getShortProjectName4(acc.cwd),
2094
2145
  account,
@@ -2490,6 +2541,7 @@ function reduceCursorEntry(acc, entry, tier) {
2490
2541
  function finalizeCursorMeta(acc, filePath, account, tier) {
2491
2542
  if (acc.messageCount === 0) return null;
2492
2543
  const sessionId = basename7(filePath, ".jsonl");
2544
+ const nameSlots = { ...acc.nameSlots, ...cursorNameSlotsFor(filePath, sessionId) };
2493
2545
  const isSubagent = SUBAGENT_PATH_RE2.test(filePath);
2494
2546
  const parent = isSubagent ? parentFromSubagentPath(filePath) : null;
2495
2547
  const { projectPath, projectName } = projectFromCursorPath(filePath);
@@ -2502,7 +2554,8 @@ function finalizeCursorMeta(acc, filePath, account, tier) {
2502
2554
  kind,
2503
2555
  externalSessionId: sessionId,
2504
2556
  sessionId,
2505
- sessionName: resolveSessionName({ ...acc.nameSlots, ...cursorNameSlotsFor(filePath, sessionId) }).name ?? "",
2557
+ sessionName: resolveSessionName(nameSlots).name ?? "",
2558
+ nameSlots,
2506
2559
  projectPath,
2507
2560
  projectName,
2508
2561
  account,
@@ -2653,7 +2706,7 @@ async function parseCursorConversation(filePath, account) {
2653
2706
  // src/providers/parse.ts
2654
2707
  import { createReadStream as createReadStream7 } from "fs";
2655
2708
  import { createInterface as createInterface5 } from "readline";
2656
- async function parseMetaWithProvider(provider, filePath, account, tier, onEntry, onAccumulator) {
2709
+ async function parseMetaWithProvider(provider, filePath, account, tier, onEntry) {
2657
2710
  const log = getLogger();
2658
2711
  const acc = provider.createEmptyAccumulator();
2659
2712
  const rl = createInterface5({
@@ -2680,7 +2733,6 @@ async function parseMetaWithProvider(provider, filePath, account, tier, onEntry,
2680
2733
  log.warn({ filePath, provider: provider.name, err }, "provider parse: read failed");
2681
2734
  return null;
2682
2735
  }
2683
- onAccumulator?.(acc);
2684
2736
  return provider.finalize(acc, filePath, account, tier);
2685
2737
  }
2686
2738
 
@@ -3530,6 +3582,7 @@ function rowToMeta(row) {
3530
3582
  externalSessionId: row.external_session_id ?? void 0,
3531
3583
  sessionId: row.session_id,
3532
3584
  sessionName: row.session_name ?? "",
3585
+ nameSlots: row.name_slots ? JSON.parse(row.name_slots) : void 0,
3533
3586
  projectPath: row.project_path ?? "",
3534
3587
  projectName: row.project_name ?? "",
3535
3588
  account: row.account,
@@ -3562,7 +3615,7 @@ var ConversationsRepo = class {
3562
3615
  db;
3563
3616
  // Upsert by file_id (1 file = 1 conversation). Keyed on the unique file_id so
3564
3617
  // a re-index overwrites the prior summary in place.
3565
- upsert(fileId, meta, pageMessageCount = meta.messageCount, nameSlots) {
3618
+ upsert(fileId, meta, pageMessageCount = meta.messageCount) {
3566
3619
  this.db.prepare(
3567
3620
  `INSERT INTO conversations (
3568
3621
  file_id, source_path, provider, kind, external_session_id,
@@ -3661,7 +3714,7 @@ var ConversationsRepo = class {
3661
3714
  is_imported_from_claude: meta.isImportedFromClaude ? 1 : 0,
3662
3715
  is_imported_from_codex: meta.isImportedFromCodex ? 1 : 0,
3663
3716
  is_imported_from_cursor: meta.isImportedFromCursor ? 1 : 0,
3664
- name_slots: nameSlots ? JSON.stringify(nameSlots) : null
3717
+ name_slots: meta.nameSlots ? JSON.stringify(meta.nameSlots) : null
3665
3718
  });
3666
3719
  }
3667
3720
  getBySourcePath(sourcePath) {
@@ -4075,6 +4128,7 @@ var PersistentEngine = class {
4075
4128
  const tier = resolveTier(tierName, customTiers);
4076
4129
  const filePath = canonicalPath(rawFilePath);
4077
4130
  const existing = this.files.getByPath(filePath);
4131
+ const refold = force || (!provider || provider.name === CLAUDE_CODE_PROVIDER) && !!existing?.reducer_state && !existing.reducer_state.includes('"turnDurations"');
4078
4132
  const classified = classify(filePath, existing);
4079
4133
  const stat6 = classified.stat;
4080
4134
  let change = classified.change;
@@ -4082,7 +4136,7 @@ var PersistentEngine = class {
4082
4136
  this.markDeleted(filePath);
4083
4137
  return { meta: null, change: "vanished" };
4084
4138
  }
4085
- if (change === "unchanged" && !force) {
4139
+ if (change === "unchanged" && !refold) {
4086
4140
  const meta2 = this.conversations.getBySourcePath(filePath);
4087
4141
  const sideStoresMoved = provider && provider.name !== CLAUDE_CODE_PROVIDER && meta2 && JSON.stringify(nameStoreSlotsFor(provider.name, filePath, meta2.sessionId)) !== existing?.reducer_state;
4088
4142
  if (!sideStoresMoved) return { meta: meta2, change };
@@ -4099,7 +4153,7 @@ var PersistentEngine = class {
4099
4153
  );
4100
4154
  return { meta: meta2, change };
4101
4155
  }
4102
- const resume = change === "appended" && !force && existing?.reducer_state;
4156
+ const resume = change === "appended" && !refold && existing?.reducer_state;
4103
4157
  const state = resume ? JSON.parse(existing.reducer_state) : initialReducerState();
4104
4158
  const startOffset = resume ? existing.last_indexed_offset : 0;
4105
4159
  const startLine = resume ? existing.last_indexed_line : 0;
@@ -4125,7 +4179,7 @@ var PersistentEngine = class {
4125
4179
  const base = resume ? this.fts.readDocument(fileId) ?? emptySearchDocument() : emptySearchDocument();
4126
4180
  const searchDoc = appendSearchDelta(base, searchDelta);
4127
4181
  const upsert = this.db.transaction(() => {
4128
- this.conversations.upsert(fileId, meta, state.pageMessageCount, state.nameSlots);
4182
+ this.conversations.upsert(fileId, meta, state.pageMessageCount);
4129
4183
  if (!this.fts.isCurrent(fileId, meta, searchDoc)) {
4130
4184
  this.fts.upsert(fileId, meta, searchDoc);
4131
4185
  }
@@ -4172,19 +4226,9 @@ var PersistentEngine = class {
4172
4226
  async indexFileWithProvider(provider, filePath, account, tier, stat6, resolveGitBranch) {
4173
4227
  const log = getLogger();
4174
4228
  let searchDoc = emptySearchDocument();
4175
- let transcriptSlots = {};
4176
- const meta = await parseMetaWithProvider(
4177
- provider,
4178
- filePath,
4179
- account,
4180
- tier,
4181
- (entry) => {
4182
- searchDoc = appendSearchDelta(searchDoc, extractSearchDelta(entry));
4183
- },
4184
- (acc) => {
4185
- transcriptSlots = acc.nameSlots ?? {};
4186
- }
4187
- );
4229
+ const meta = await parseMetaWithProvider(provider, filePath, account, tier, (entry) => {
4230
+ searchDoc = appendSearchDelta(searchDoc, extractSearchDelta(entry));
4231
+ });
4188
4232
  if (!meta) {
4189
4233
  this.markDeleted(filePath);
4190
4234
  return null;
@@ -4196,10 +4240,7 @@ var PersistentEngine = class {
4196
4240
  const fp = stat6.size > 0 ? fingerprint(filePath, stat6.size) : null;
4197
4241
  const fileId = this.files.ensure(filePath, account);
4198
4242
  const upsert = this.db.transaction(() => {
4199
- this.conversations.upsert(fileId, meta, meta.messageCount, {
4200
- ...transcriptSlots,
4201
- ...sideSlots
4202
- });
4243
+ this.conversations.upsert(fileId, meta, meta.messageCount);
4203
4244
  if (!this.fts.isCurrent(fileId, meta, searchDoc)) {
4204
4245
  this.fts.upsert(fileId, meta, searchDoc);
4205
4246
  }
@@ -4273,6 +4314,10 @@ var PersistentEngine = class {
4273
4314
  // resolved to an indexed conversation. For conversations large enough to span
4274
4315
  // a checkpoint interval, checkpoints are built lazily on first access and
4275
4316
  // reused thereafter. Smaller conversations read from the start (cheap).
4317
+ //
4318
+ // The window is over the conversation with its leading injected-context run
4319
+ // dropped (options.isInjectedContext, see filteredWindow). The caller keeps the
4320
+ // index current (refreshFile) first; this reads what the index describes.
4276
4321
  async getPage(id, options) {
4277
4322
  const meta = this.conversations.getByIdOrSession(id);
4278
4323
  if (!meta) return null;
@@ -4280,24 +4325,28 @@ var PersistentEngine = class {
4280
4325
  if (meta.provider === CODEX_CLI_PROVIDER || meta.provider === CURSOR_PROVIDER || meta.provider === COPILOT_PROVIDER) {
4281
4326
  const conversation = meta.provider === CODEX_CLI_PROVIDER ? await parseCodexConversation(filePath, meta.account) : meta.provider === CURSOR_PROVIDER ? await parseCursorConversation(filePath, meta.account) : await parseCopilotConversation(filePath, meta.account);
4282
4327
  if (!conversation) return null;
4283
- const { messages } = conversation;
4284
- const total2 = messages.length;
4285
- const beforeIndex2 = options.beforeIndex ?? total2;
4286
- const fromIndex2 = Math.max(0, beforeIndex2 - options.limit);
4287
- return { messages: messages.slice(fromIndex2, beforeIndex2), total: total2, fromIndex: fromIndex2 };
4288
- }
4289
- const total = this.conversations.pageMessageCount(filePath);
4290
- await this.ensureCheckpoints(filePath, total);
4291
- const beforeIndex = options.beforeIndex ?? total;
4292
- const fromIndex = Math.max(0, beforeIndex - options.limit);
4293
- let floor = this.checkpoints.floor(filePath, fromIndex);
4328
+ return pageOfConversation(conversation, options);
4329
+ }
4330
+ const rawTotal = this.conversations.pageMessageCount(filePath);
4331
+ const skip = await countLeadingInjected(filePath, options.isInjectedContext);
4332
+ const { total, fromIndex, raw } = filteredWindow(rawTotal, skip, options);
4333
+ await this.ensureCheckpoints(filePath, rawTotal);
4334
+ let floor = this.checkpoints.floor(filePath, raw.beforeIndex - raw.limit);
4294
4335
  if (floor) {
4295
4336
  try {
4296
4337
  if (floor.byteOffset > statSync5(filePath).size) floor = null;
4297
4338
  } catch {
4298
4339
  }
4299
4340
  }
4300
- return readPage(filePath, total, options, floor);
4341
+ const page = await readPage(filePath, rawTotal, raw, floor);
4342
+ const state = this.files.getByPath(filePath)?.reducer_state;
4343
+ const durations = state ? JSON.parse(state).turnDurations : void 0;
4344
+ return {
4345
+ messages: page.messages,
4346
+ total,
4347
+ fromIndex,
4348
+ turnDurations: durations?.length ? durations : void 0
4349
+ };
4301
4350
  }
4302
4351
  // Build or extend the checkpoint chain so it covers `total` messages. Cold
4303
4352
  // file → full build; a file that grew → extend from the last persisted
@@ -4822,36 +4871,94 @@ var ConversationScanner = class {
4822
4871
  }
4823
4872
  }
4824
4873
  // Return one bounded window of a conversation's messages plus the total
4825
- // message count, so a caller can serve the last page and scroll back without
4826
- // holding the whole conversation itself.
4874
+ // message count, the conversation's current meta and its turn durations, so a
4875
+ // caller can serve the last page and scroll back without holding the whole
4876
+ // conversation itself.
4827
4877
  //
4828
- // The window is `[max(0, beforeIndex - limit), beforeIndex)` in chronological
4878
+ // Messages are those of the full read with the LEADING run of injected user
4879
+ // turns dropped (options.isInjectedContext, default isCodexInjectedContext);
4880
+ // `total`, `fromIndex` and `beforeIndex` all count in that filtered space. The
4881
+ // window is `[max(0, beforeIndex - limit), beforeIndex)` in chronological
4829
4882
  // order; `beforeIndex` defaults to `total` (the newest page). `fromIndex` is
4830
4883
  // the window's start index, so the caller can derive `has_more_older`
4831
4884
  // (fromIndex > 0). Returns null when the id can't be resolved/parsed — the
4832
4885
  // same contract as getConversation.
4833
4886
  //
4834
- // Persistent mode: a true bounded read — the engine seeks from the nearest
4835
- // checkpoint and parses only the requested window (checkpoints are built
4836
- // lazily for large conversations). Windowed message indices are proven
4837
- // identical to parseConversation().messages.slice(...) by the paged-reader
4838
- // equivalence test.
4839
- //
4840
- // Legacy mode: parse-once-then-slice via getConversation (cached in the LRU),
4841
- // which is identical by construction.
4887
+ // Every call reads the file as it is on disk now, never a cached parse:
4888
+ // persistent mode refreshes the index first (O(appended bytes)) and the engine
4889
+ // seeks from the nearest checkpoint. In-memory claude-code files keep a
4890
+ // stat-validated checkpoint index (pageIndexFor). Other providers are parsed
4891
+ // whole per call.
4842
4892
  async getConversationPage(id, options) {
4893
+ const cid = canonicalPath(id);
4894
+ const known = this.persistent ? this.engine().getByIdOrSession(cid) : this.metadataCache.get(cid) ?? this.resolveSessionId(cid);
4895
+ if (!known) return null;
4896
+ const isClaude = known.provider !== CODEX_CLI_PROVIDER && known.provider !== CURSOR_PROVIDER && known.provider !== COPILOT_PROVIDER;
4897
+ if (!this.persistent && isClaude) {
4898
+ const index = await this.pageIndexFor(known.filePath, known.account);
4899
+ if (!index) return null;
4900
+ const skip = await countLeadingInjected(known.filePath, options.isInjectedContext);
4901
+ const { total, fromIndex, raw } = filteredWindow(index.rawTotal, skip, options);
4902
+ const rawFrom = raw.beforeIndex - raw.limit;
4903
+ let floor = null;
4904
+ for (const c of index.checkpoints) if (c.messageIndex <= rawFrom) floor = c;
4905
+ const page = await readPage(known.filePath, index.rawTotal, raw, floor);
4906
+ return {
4907
+ messages: page.messages,
4908
+ total,
4909
+ fromIndex,
4910
+ meta: index.meta,
4911
+ turnDurations: index.turnDurations
4912
+ };
4913
+ }
4914
+ const meta = await this.refreshFile(known.filePath, known.account);
4915
+ if (!meta) return null;
4843
4916
  if (this.persistent) {
4844
- return this.engine().getPage(canonicalPath(id), options);
4917
+ const page = await this.engine().getPage(meta.filePath, options);
4918
+ return page && { ...page, meta };
4919
+ }
4920
+ const conversation = await this.getConversation(meta.filePath);
4921
+ return conversation && { ...pageOfConversation(conversation, options), meta };
4922
+ }
4923
+ // In-memory claude-code paging index: one streamed pass builds the checkpoint
4924
+ // chain and folds the metadata (meta, page total, turn durations) alongside,
4925
+ // reused until the file's size or mtime changes. Memory stays bounded: the
4926
+ // file is never held, and the index LRU is capped.
4927
+ // ponytail: any change rebuilds from byte 0; extending from the last
4928
+ // checkpoint plus a persisted ReducerState (as the engine does) is the
4929
+ // upgrade path if live-appended transcripts make this hot.
4930
+ pageIndexes = new LRUCache(32);
4931
+ async pageIndexFor(filePath, account) {
4932
+ let stat6;
4933
+ try {
4934
+ stat6 = statSync6(filePath);
4935
+ } catch {
4936
+ this.pageIndexes.delete(filePath);
4937
+ return null;
4845
4938
  }
4846
- const conversation = await this.getConversation(id);
4847
- if (!conversation) return null;
4848
- const { messages } = conversation;
4849
- const total = messages.length;
4850
- const { limit } = options;
4851
- const beforeIndex = options.beforeIndex ?? total;
4852
- const fromIndex = Math.max(0, beforeIndex - limit);
4853
- const window = messages.slice(fromIndex, beforeIndex);
4854
- return { messages: window, total, fromIndex };
4939
+ const cached = this.pageIndexes.get(filePath);
4940
+ if (cached && cached.size === stat6.size && cached.mtimeMs === stat6.mtimeMs) return cached;
4941
+ const state = initialReducerState();
4942
+ const checkpoints = await buildCheckpoints(filePath, CHECKPOINT_INTERVAL, null, (entry) => {
4943
+ reduceLine(state, entry, this.lastTier);
4944
+ return false;
4945
+ });
4946
+ const meta = finalizeMeta(state, filePath, account, this.lastTier);
4947
+ if (!meta) {
4948
+ this.pageIndexes.delete(filePath);
4949
+ return null;
4950
+ }
4951
+ meta.gitBranch = readGitBranch(meta.projectPath);
4952
+ const index = {
4953
+ size: stat6.size,
4954
+ mtimeMs: stat6.mtimeMs,
4955
+ checkpoints,
4956
+ meta,
4957
+ rawTotal: state.pageMessageCount,
4958
+ turnDurations: state.turnDurations?.length ? state.turnDurations : void 0
4959
+ };
4960
+ this.pageIndexes.set(filePath, index);
4961
+ return index;
4855
4962
  }
4856
4963
  // Parse one JSONL file directly and slice a page window — without any prior
4857
4964
  // scan() or metadata index. This is the cold-start fast path: a single