@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/index.cjs CHANGED
@@ -841,7 +841,8 @@ function initialReducerState() {
841
841
  snippetParts: [],
842
842
  previewLength: 0,
843
843
  snippetLength: 0,
844
- badJsonLines: 0
844
+ badJsonLines: 0,
845
+ turnDurations: []
845
846
  };
846
847
  }
847
848
  function reduceLine(state, entry, tier) {
@@ -855,6 +856,8 @@ function reduceLine(state, entry, tier) {
855
856
  const ts = entry.timestamp;
856
857
  if (!state.latestTimestamp || ts > state.latestTimestamp) state.latestTimestamp = ts;
857
858
  }
859
+ const turnDuration = turnDurationOf(entry);
860
+ if (turnDuration) state.turnDurations?.push(turnDuration);
858
861
  const type = entry.type;
859
862
  if (type === "last-prompt") {
860
863
  if (entry.lastPrompt && !state.lastPrompt) state.lastPrompt = entry.lastPrompt;
@@ -921,6 +924,7 @@ function finalizeMeta(state, filePath, account, tier) {
921
924
  provider: CLAUDE_CODE_PROVIDER,
922
925
  sessionId: state.sessionId || (0, import_path3.basename)(filePath, ".jsonl"),
923
926
  sessionName: resolveSessionName(state.nameSlots).name ?? "",
927
+ nameSlots: state.nameSlots,
924
928
  projectPath,
925
929
  projectName: getShortProjectName(projectPath),
926
930
  account,
@@ -952,6 +956,16 @@ function getShortProjectName(fullPath) {
952
956
  }
953
957
 
954
958
  // src/parser.ts
959
+ function turnDurationOf(entry) {
960
+ if (entry.type !== "system" || entry.subtype !== "turn_duration" || typeof entry.durationMs !== "number") {
961
+ return null;
962
+ }
963
+ return {
964
+ durationMs: entry.durationMs,
965
+ messageCount: entry.messageCount || 0,
966
+ uuid: entry.uuid
967
+ };
968
+ }
955
969
  async function parseMeta(filePath, account, tier, onEntry) {
956
970
  const log = getLogger();
957
971
  log.trace({ filePath, account, tier: tier.name }, "parseMeta: start");
@@ -1005,12 +1019,9 @@ async function parseConversation(filePath, account) {
1005
1019
  badJsonLines++;
1006
1020
  continue;
1007
1021
  }
1008
- if (entry.type === "system" && entry.subtype === "turn_duration" && typeof entry.durationMs === "number") {
1009
- turnDurations.push({
1010
- durationMs: entry.durationMs,
1011
- messageCount: entry.messageCount || 0,
1012
- uuid: entry.uuid
1013
- });
1022
+ const turnDuration = turnDurationOf(entry);
1023
+ if (turnDuration) {
1024
+ turnDurations.push(turnDuration);
1014
1025
  continue;
1015
1026
  }
1016
1027
  const message = reduceConvLine(state, entry);
@@ -1598,6 +1609,7 @@ function reduceCodexEntry(acc, entry, tier) {
1598
1609
  function finalizeCodexMeta(acc, filePath, account, tier) {
1599
1610
  if (acc.messageCount === 0) return null;
1600
1611
  const sessionId = acc.sessionId || (0, import_path7.basename)(filePath, ".jsonl");
1612
+ const nameSlots = { ...acc.nameSlots, ...codexNameSlotsFor(filePath, acc.sessionId) };
1601
1613
  const projectPath = acc.cwd;
1602
1614
  const kind = acc.lastAssistant === null && acc.toolNames.length > 0 ? "task" : "conversation";
1603
1615
  return {
@@ -1607,7 +1619,8 @@ function finalizeCodexMeta(acc, filePath, account, tier) {
1607
1619
  kind,
1608
1620
  externalSessionId: acc.sessionId || void 0,
1609
1621
  sessionId,
1610
- sessionName: resolveSessionName({ ...acc.nameSlots, ...codexNameSlotsFor(filePath, acc.sessionId) }).name ?? "",
1622
+ sessionName: resolveSessionName(nameSlots).name ?? "",
1623
+ nameSlots,
1611
1624
  projectPath,
1612
1625
  projectName: getShortProjectName3(projectPath),
1613
1626
  account,
@@ -1893,6 +1906,7 @@ function reduceCopilotEntry(acc, entry, tier) {
1893
1906
  function finalizeCopilotMeta(acc, filePath, account, tier) {
1894
1907
  if (acc.messageCount === 0) return null;
1895
1908
  const sessionId = acc.sessionId || sessionIdFromPath(filePath);
1909
+ const nameSlots = { ...acc.nameSlots, ...copilotNameSlotsFor(filePath) };
1896
1910
  const kind = acc.lastAssistant === null && acc.toolNames.length > 0 ? "task" : "conversation";
1897
1911
  return {
1898
1912
  id: filePath,
@@ -1901,7 +1915,8 @@ function finalizeCopilotMeta(acc, filePath, account, tier) {
1901
1915
  kind,
1902
1916
  externalSessionId: sessionId,
1903
1917
  sessionId,
1904
- sessionName: resolveSessionName({ ...acc.nameSlots, ...copilotNameSlotsFor(filePath) }).name ?? "",
1918
+ sessionName: resolveSessionName(nameSlots).name ?? "",
1919
+ nameSlots,
1905
1920
  projectPath: acc.cwd,
1906
1921
  projectName: getShortProjectName4(acc.cwd),
1907
1922
  account,
@@ -2303,6 +2318,7 @@ function reduceCursorEntry(acc, entry, tier) {
2303
2318
  function finalizeCursorMeta(acc, filePath, account, tier) {
2304
2319
  if (acc.messageCount === 0) return null;
2305
2320
  const sessionId = (0, import_path9.basename)(filePath, ".jsonl");
2321
+ const nameSlots = { ...acc.nameSlots, ...cursorNameSlotsFor(filePath, sessionId) };
2306
2322
  const isSubagent = SUBAGENT_PATH_RE2.test(filePath);
2307
2323
  const parent = isSubagent ? parentFromSubagentPath(filePath) : null;
2308
2324
  const { projectPath, projectName } = projectFromCursorPath(filePath);
@@ -2315,7 +2331,8 @@ function finalizeCursorMeta(acc, filePath, account, tier) {
2315
2331
  kind,
2316
2332
  externalSessionId: sessionId,
2317
2333
  sessionId,
2318
- sessionName: resolveSessionName({ ...acc.nameSlots, ...cursorNameSlotsFor(filePath, sessionId) }).name ?? "",
2334
+ sessionName: resolveSessionName(nameSlots).name ?? "",
2335
+ nameSlots,
2319
2336
  projectPath,
2320
2337
  projectName,
2321
2338
  account,
@@ -2691,7 +2708,7 @@ async function streamMessages(filePath, startOffset, startLine, state, onMessage
2691
2708
  }
2692
2709
  return { offset, line };
2693
2710
  }
2694
- async function buildCheckpoints(filePath, interval = CHECKPOINT_INTERVAL, from = null) {
2711
+ async function buildCheckpoints(filePath, interval = CHECKPOINT_INTERVAL, from = null, onEntry) {
2695
2712
  const checkpoints = [];
2696
2713
  const state = from ? from.state : initialConvState();
2697
2714
  let index = from ? from.messageIndex : 0;
@@ -2711,7 +2728,8 @@ async function buildCheckpoints(filePath, interval = CHECKPOINT_INTERVAL, from =
2711
2728
  });
2712
2729
  }
2713
2730
  return false;
2714
- }
2731
+ },
2732
+ onEntry
2715
2733
  );
2716
2734
  return checkpoints;
2717
2735
  }
@@ -2732,6 +2750,41 @@ async function readPage(filePath, total, options, floor) {
2732
2750
  applyTeamInfo(window, state);
2733
2751
  return { messages: window, total, fromIndex };
2734
2752
  }
2753
+ function isInjectedTurn(message, isInjected) {
2754
+ return message.role === "user" && isInjected(message.text ?? "");
2755
+ }
2756
+ async function countLeadingInjected(filePath, isInjected = isCodexInjectedContext) {
2757
+ let skip = 0;
2758
+ await streamMessages(filePath, 0, 0, initialConvState(), (message) => {
2759
+ if (!isInjectedTurn(message, isInjected)) return true;
2760
+ skip += 1;
2761
+ return false;
2762
+ });
2763
+ return skip;
2764
+ }
2765
+ function filteredWindow(rawTotal, skip, options) {
2766
+ const total = Math.max(0, rawTotal - skip);
2767
+ const beforeIndex = options.beforeIndex ?? total;
2768
+ const fromIndex = Math.max(0, beforeIndex - options.limit);
2769
+ return {
2770
+ total,
2771
+ fromIndex,
2772
+ raw: { beforeIndex: beforeIndex + skip, limit: beforeIndex - fromIndex }
2773
+ };
2774
+ }
2775
+ function pageOfConversation(conversation, options) {
2776
+ const { messages } = conversation;
2777
+ const isInjected = options.isInjectedContext ?? isCodexInjectedContext;
2778
+ const firstKept = messages.findIndex((m) => !isInjectedTurn(m, isInjected));
2779
+ const skip = firstKept === -1 ? messages.length : firstKept;
2780
+ const { total, fromIndex, raw } = filteredWindow(messages.length, skip, options);
2781
+ return {
2782
+ messages: messages.slice(raw.beforeIndex - raw.limit, raw.beforeIndex),
2783
+ total,
2784
+ fromIndex,
2785
+ turnDurations: conversation.turnDurations
2786
+ };
2787
+ }
2735
2788
 
2736
2789
  // src/persistent/conversation-stream.ts
2737
2790
  async function foldTail(filePath, resume) {
@@ -2872,7 +2925,7 @@ var import_fs13 = require("fs");
2872
2925
  // src/providers/parse.ts
2873
2926
  var import_fs11 = require("fs");
2874
2927
  var import_readline5 = require("readline");
2875
- async function parseMetaWithProvider(provider, filePath, account, tier, onEntry, onAccumulator) {
2928
+ async function parseMetaWithProvider(provider, filePath, account, tier, onEntry) {
2876
2929
  const log = getLogger();
2877
2930
  const acc = provider.createEmptyAccumulator();
2878
2931
  const rl = (0, import_readline5.createInterface)({
@@ -2899,7 +2952,6 @@ async function parseMetaWithProvider(provider, filePath, account, tier, onEntry,
2899
2952
  log.warn({ filePath, provider: provider.name, err }, "provider parse: read failed");
2900
2953
  return null;
2901
2954
  }
2902
- onAccumulator?.(acc);
2903
2955
  return provider.finalize(acc, filePath, account, tier);
2904
2956
  }
2905
2957
 
@@ -3749,6 +3801,7 @@ function rowToMeta(row) {
3749
3801
  externalSessionId: row.external_session_id ?? void 0,
3750
3802
  sessionId: row.session_id,
3751
3803
  sessionName: row.session_name ?? "",
3804
+ nameSlots: row.name_slots ? JSON.parse(row.name_slots) : void 0,
3752
3805
  projectPath: row.project_path ?? "",
3753
3806
  projectName: row.project_name ?? "",
3754
3807
  account: row.account,
@@ -3781,7 +3834,7 @@ var ConversationsRepo = class {
3781
3834
  db;
3782
3835
  // Upsert by file_id (1 file = 1 conversation). Keyed on the unique file_id so
3783
3836
  // a re-index overwrites the prior summary in place.
3784
- upsert(fileId, meta, pageMessageCount = meta.messageCount, nameSlots) {
3837
+ upsert(fileId, meta, pageMessageCount = meta.messageCount) {
3785
3838
  this.db.prepare(
3786
3839
  `INSERT INTO conversations (
3787
3840
  file_id, source_path, provider, kind, external_session_id,
@@ -3880,7 +3933,7 @@ var ConversationsRepo = class {
3880
3933
  is_imported_from_claude: meta.isImportedFromClaude ? 1 : 0,
3881
3934
  is_imported_from_codex: meta.isImportedFromCodex ? 1 : 0,
3882
3935
  is_imported_from_cursor: meta.isImportedFromCursor ? 1 : 0,
3883
- name_slots: nameSlots ? JSON.stringify(nameSlots) : null
3936
+ name_slots: meta.nameSlots ? JSON.stringify(meta.nameSlots) : null
3884
3937
  });
3885
3938
  }
3886
3939
  getBySourcePath(sourcePath) {
@@ -4261,6 +4314,7 @@ var PersistentEngine = class {
4261
4314
  const tier = resolveTier(tierName, customTiers);
4262
4315
  const filePath = canonicalPath(rawFilePath);
4263
4316
  const existing = this.files.getByPath(filePath);
4317
+ const refold = force || (!provider || provider.name === CLAUDE_CODE_PROVIDER) && !!existing?.reducer_state && !existing.reducer_state.includes('"turnDurations"');
4264
4318
  const classified = classify(filePath, existing);
4265
4319
  const stat6 = classified.stat;
4266
4320
  let change = classified.change;
@@ -4268,7 +4322,7 @@ var PersistentEngine = class {
4268
4322
  this.markDeleted(filePath);
4269
4323
  return { meta: null, change: "vanished" };
4270
4324
  }
4271
- if (change === "unchanged" && !force) {
4325
+ if (change === "unchanged" && !refold) {
4272
4326
  const meta2 = this.conversations.getBySourcePath(filePath);
4273
4327
  const sideStoresMoved = provider && provider.name !== CLAUDE_CODE_PROVIDER && meta2 && JSON.stringify(nameStoreSlotsFor(provider.name, filePath, meta2.sessionId)) !== existing?.reducer_state;
4274
4328
  if (!sideStoresMoved) return { meta: meta2, change };
@@ -4285,7 +4339,7 @@ var PersistentEngine = class {
4285
4339
  );
4286
4340
  return { meta: meta2, change };
4287
4341
  }
4288
- const resume = change === "appended" && !force && existing?.reducer_state;
4342
+ const resume = change === "appended" && !refold && existing?.reducer_state;
4289
4343
  const state = resume ? JSON.parse(existing.reducer_state) : initialReducerState();
4290
4344
  const startOffset = resume ? existing.last_indexed_offset : 0;
4291
4345
  const startLine = resume ? existing.last_indexed_line : 0;
@@ -4311,7 +4365,7 @@ var PersistentEngine = class {
4311
4365
  const base = resume ? this.fts.readDocument(fileId) ?? emptySearchDocument() : emptySearchDocument();
4312
4366
  const searchDoc = appendSearchDelta(base, searchDelta);
4313
4367
  const upsert = this.db.transaction(() => {
4314
- this.conversations.upsert(fileId, meta, state.pageMessageCount, state.nameSlots);
4368
+ this.conversations.upsert(fileId, meta, state.pageMessageCount);
4315
4369
  if (!this.fts.isCurrent(fileId, meta, searchDoc)) {
4316
4370
  this.fts.upsert(fileId, meta, searchDoc);
4317
4371
  }
@@ -4358,19 +4412,9 @@ var PersistentEngine = class {
4358
4412
  async indexFileWithProvider(provider, filePath, account, tier, stat6, resolveGitBranch) {
4359
4413
  const log = getLogger();
4360
4414
  let searchDoc = emptySearchDocument();
4361
- let transcriptSlots = {};
4362
- const meta = await parseMetaWithProvider(
4363
- provider,
4364
- filePath,
4365
- account,
4366
- tier,
4367
- (entry) => {
4368
- searchDoc = appendSearchDelta(searchDoc, extractSearchDelta(entry));
4369
- },
4370
- (acc) => {
4371
- transcriptSlots = acc.nameSlots ?? {};
4372
- }
4373
- );
4415
+ const meta = await parseMetaWithProvider(provider, filePath, account, tier, (entry) => {
4416
+ searchDoc = appendSearchDelta(searchDoc, extractSearchDelta(entry));
4417
+ });
4374
4418
  if (!meta) {
4375
4419
  this.markDeleted(filePath);
4376
4420
  return null;
@@ -4382,10 +4426,7 @@ var PersistentEngine = class {
4382
4426
  const fp = stat6.size > 0 ? fingerprint(filePath, stat6.size) : null;
4383
4427
  const fileId = this.files.ensure(filePath, account);
4384
4428
  const upsert = this.db.transaction(() => {
4385
- this.conversations.upsert(fileId, meta, meta.messageCount, {
4386
- ...transcriptSlots,
4387
- ...sideSlots
4388
- });
4429
+ this.conversations.upsert(fileId, meta, meta.messageCount);
4389
4430
  if (!this.fts.isCurrent(fileId, meta, searchDoc)) {
4390
4431
  this.fts.upsert(fileId, meta, searchDoc);
4391
4432
  }
@@ -4459,6 +4500,10 @@ var PersistentEngine = class {
4459
4500
  // resolved to an indexed conversation. For conversations large enough to span
4460
4501
  // a checkpoint interval, checkpoints are built lazily on first access and
4461
4502
  // reused thereafter. Smaller conversations read from the start (cheap).
4503
+ //
4504
+ // The window is over the conversation with its leading injected-context run
4505
+ // dropped (options.isInjectedContext, see filteredWindow). The caller keeps the
4506
+ // index current (refreshFile) first; this reads what the index describes.
4462
4507
  async getPage(id, options) {
4463
4508
  const meta = this.conversations.getByIdOrSession(id);
4464
4509
  if (!meta) return null;
@@ -4466,24 +4511,28 @@ var PersistentEngine = class {
4466
4511
  if (meta.provider === CODEX_CLI_PROVIDER || meta.provider === CURSOR_PROVIDER || meta.provider === COPILOT_PROVIDER) {
4467
4512
  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);
4468
4513
  if (!conversation) return null;
4469
- const { messages } = conversation;
4470
- const total2 = messages.length;
4471
- const beforeIndex2 = options.beforeIndex ?? total2;
4472
- const fromIndex2 = Math.max(0, beforeIndex2 - options.limit);
4473
- return { messages: messages.slice(fromIndex2, beforeIndex2), total: total2, fromIndex: fromIndex2 };
4474
- }
4475
- const total = this.conversations.pageMessageCount(filePath);
4476
- await this.ensureCheckpoints(filePath, total);
4477
- const beforeIndex = options.beforeIndex ?? total;
4478
- const fromIndex = Math.max(0, beforeIndex - options.limit);
4479
- let floor = this.checkpoints.floor(filePath, fromIndex);
4514
+ return pageOfConversation(conversation, options);
4515
+ }
4516
+ const rawTotal = this.conversations.pageMessageCount(filePath);
4517
+ const skip = await countLeadingInjected(filePath, options.isInjectedContext);
4518
+ const { total, fromIndex, raw } = filteredWindow(rawTotal, skip, options);
4519
+ await this.ensureCheckpoints(filePath, rawTotal);
4520
+ let floor = this.checkpoints.floor(filePath, raw.beforeIndex - raw.limit);
4480
4521
  if (floor) {
4481
4522
  try {
4482
4523
  if (floor.byteOffset > (0, import_fs13.statSync)(filePath).size) floor = null;
4483
4524
  } catch {
4484
4525
  }
4485
4526
  }
4486
- return readPage(filePath, total, options, floor);
4527
+ const page = await readPage(filePath, rawTotal, raw, floor);
4528
+ const state = this.files.getByPath(filePath)?.reducer_state;
4529
+ const durations = state ? JSON.parse(state).turnDurations : void 0;
4530
+ return {
4531
+ messages: page.messages,
4532
+ total,
4533
+ fromIndex,
4534
+ turnDurations: durations?.length ? durations : void 0
4535
+ };
4487
4536
  }
4488
4537
  // Build or extend the checkpoint chain so it covers `total` messages. Cold
4489
4538
  // file → full build; a file that grew → extend from the last persisted
@@ -4972,36 +5021,94 @@ var ConversationScanner = class {
4972
5021
  }
4973
5022
  }
4974
5023
  // Return one bounded window of a conversation's messages plus the total
4975
- // message count, so a caller can serve the last page and scroll back without
4976
- // holding the whole conversation itself.
5024
+ // message count, the conversation's current meta and its turn durations, so a
5025
+ // caller can serve the last page and scroll back without holding the whole
5026
+ // conversation itself.
4977
5027
  //
4978
- // The window is `[max(0, beforeIndex - limit), beforeIndex)` in chronological
5028
+ // Messages are those of the full read with the LEADING run of injected user
5029
+ // turns dropped (options.isInjectedContext, default isCodexInjectedContext);
5030
+ // `total`, `fromIndex` and `beforeIndex` all count in that filtered space. The
5031
+ // window is `[max(0, beforeIndex - limit), beforeIndex)` in chronological
4979
5032
  // order; `beforeIndex` defaults to `total` (the newest page). `fromIndex` is
4980
5033
  // the window's start index, so the caller can derive `has_more_older`
4981
5034
  // (fromIndex > 0). Returns null when the id can't be resolved/parsed — the
4982
5035
  // same contract as getConversation.
4983
5036
  //
4984
- // Persistent mode: a true bounded read — the engine seeks from the nearest
4985
- // checkpoint and parses only the requested window (checkpoints are built
4986
- // lazily for large conversations). Windowed message indices are proven
4987
- // identical to parseConversation().messages.slice(...) by the paged-reader
4988
- // equivalence test.
4989
- //
4990
- // Legacy mode: parse-once-then-slice via getConversation (cached in the LRU),
4991
- // which is identical by construction.
5037
+ // Every call reads the file as it is on disk now, never a cached parse:
5038
+ // persistent mode refreshes the index first (O(appended bytes)) and the engine
5039
+ // seeks from the nearest checkpoint. In-memory claude-code files keep a
5040
+ // stat-validated checkpoint index (pageIndexFor). Other providers are parsed
5041
+ // whole per call.
4992
5042
  async getConversationPage(id, options) {
5043
+ const cid = canonicalPath(id);
5044
+ const known = this.persistent ? this.engine().getByIdOrSession(cid) : this.metadataCache.get(cid) ?? this.resolveSessionId(cid);
5045
+ if (!known) return null;
5046
+ const isClaude = known.provider !== CODEX_CLI_PROVIDER && known.provider !== CURSOR_PROVIDER && known.provider !== COPILOT_PROVIDER;
5047
+ if (!this.persistent && isClaude) {
5048
+ const index = await this.pageIndexFor(known.filePath, known.account);
5049
+ if (!index) return null;
5050
+ const skip = await countLeadingInjected(known.filePath, options.isInjectedContext);
5051
+ const { total, fromIndex, raw } = filteredWindow(index.rawTotal, skip, options);
5052
+ const rawFrom = raw.beforeIndex - raw.limit;
5053
+ let floor = null;
5054
+ for (const c of index.checkpoints) if (c.messageIndex <= rawFrom) floor = c;
5055
+ const page = await readPage(known.filePath, index.rawTotal, raw, floor);
5056
+ return {
5057
+ messages: page.messages,
5058
+ total,
5059
+ fromIndex,
5060
+ meta: index.meta,
5061
+ turnDurations: index.turnDurations
5062
+ };
5063
+ }
5064
+ const meta = await this.refreshFile(known.filePath, known.account);
5065
+ if (!meta) return null;
4993
5066
  if (this.persistent) {
4994
- return this.engine().getPage(canonicalPath(id), options);
5067
+ const page = await this.engine().getPage(meta.filePath, options);
5068
+ return page && { ...page, meta };
5069
+ }
5070
+ const conversation = await this.getConversation(meta.filePath);
5071
+ return conversation && { ...pageOfConversation(conversation, options), meta };
5072
+ }
5073
+ // In-memory claude-code paging index: one streamed pass builds the checkpoint
5074
+ // chain and folds the metadata (meta, page total, turn durations) alongside,
5075
+ // reused until the file's size or mtime changes. Memory stays bounded: the
5076
+ // file is never held, and the index LRU is capped.
5077
+ // ponytail: any change rebuilds from byte 0; extending from the last
5078
+ // checkpoint plus a persisted ReducerState (as the engine does) is the
5079
+ // upgrade path if live-appended transcripts make this hot.
5080
+ pageIndexes = new LRUCache(32);
5081
+ async pageIndexFor(filePath, account) {
5082
+ let stat6;
5083
+ try {
5084
+ stat6 = (0, import_fs14.statSync)(filePath);
5085
+ } catch {
5086
+ this.pageIndexes.delete(filePath);
5087
+ return null;
4995
5088
  }
4996
- const conversation = await this.getConversation(id);
4997
- if (!conversation) return null;
4998
- const { messages } = conversation;
4999
- const total = messages.length;
5000
- const { limit } = options;
5001
- const beforeIndex = options.beforeIndex ?? total;
5002
- const fromIndex = Math.max(0, beforeIndex - limit);
5003
- const window = messages.slice(fromIndex, beforeIndex);
5004
- return { messages: window, total, fromIndex };
5089
+ const cached = this.pageIndexes.get(filePath);
5090
+ if (cached && cached.size === stat6.size && cached.mtimeMs === stat6.mtimeMs) return cached;
5091
+ const state = initialReducerState();
5092
+ const checkpoints = await buildCheckpoints(filePath, CHECKPOINT_INTERVAL, null, (entry) => {
5093
+ reduceLine(state, entry, this.lastTier);
5094
+ return false;
5095
+ });
5096
+ const meta = finalizeMeta(state, filePath, account, this.lastTier);
5097
+ if (!meta) {
5098
+ this.pageIndexes.delete(filePath);
5099
+ return null;
5100
+ }
5101
+ meta.gitBranch = readGitBranch(meta.projectPath);
5102
+ const index = {
5103
+ size: stat6.size,
5104
+ mtimeMs: stat6.mtimeMs,
5105
+ checkpoints,
5106
+ meta,
5107
+ rawTotal: state.pageMessageCount,
5108
+ turnDurations: state.turnDurations?.length ? state.turnDurations : void 0
5109
+ };
5110
+ this.pageIndexes.set(filePath, index);
5111
+ return index;
5005
5112
  }
5006
5113
  // Parse one JSONL file directly and slice a page window — without any prior
5007
5114
  // scan() or metadata index. This is the cold-start fast path: a single