@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.js CHANGED
@@ -751,7 +751,8 @@ function initialReducerState() {
751
751
  snippetParts: [],
752
752
  previewLength: 0,
753
753
  snippetLength: 0,
754
- badJsonLines: 0
754
+ badJsonLines: 0,
755
+ turnDurations: []
755
756
  };
756
757
  }
757
758
  function reduceLine(state, entry, tier) {
@@ -765,6 +766,8 @@ function reduceLine(state, entry, tier) {
765
766
  const ts = entry.timestamp;
766
767
  if (!state.latestTimestamp || ts > state.latestTimestamp) state.latestTimestamp = ts;
767
768
  }
769
+ const turnDuration = turnDurationOf(entry);
770
+ if (turnDuration) state.turnDurations?.push(turnDuration);
768
771
  const type = entry.type;
769
772
  if (type === "last-prompt") {
770
773
  if (entry.lastPrompt && !state.lastPrompt) state.lastPrompt = entry.lastPrompt;
@@ -831,6 +834,7 @@ function finalizeMeta(state, filePath, account, tier) {
831
834
  provider: CLAUDE_CODE_PROVIDER,
832
835
  sessionId: state.sessionId || basename2(filePath, ".jsonl"),
833
836
  sessionName: resolveSessionName(state.nameSlots).name ?? "",
837
+ nameSlots: state.nameSlots,
834
838
  projectPath,
835
839
  projectName: getShortProjectName(projectPath),
836
840
  account,
@@ -862,6 +866,16 @@ function getShortProjectName(fullPath) {
862
866
  }
863
867
 
864
868
  // src/parser.ts
869
+ function turnDurationOf(entry) {
870
+ if (entry.type !== "system" || entry.subtype !== "turn_duration" || typeof entry.durationMs !== "number") {
871
+ return null;
872
+ }
873
+ return {
874
+ durationMs: entry.durationMs,
875
+ messageCount: entry.messageCount || 0,
876
+ uuid: entry.uuid
877
+ };
878
+ }
865
879
  async function parseMeta(filePath, account, tier, onEntry) {
866
880
  const log = getLogger();
867
881
  log.trace({ filePath, account, tier: tier.name }, "parseMeta: start");
@@ -915,12 +929,9 @@ async function parseConversation(filePath, account) {
915
929
  badJsonLines++;
916
930
  continue;
917
931
  }
918
- if (entry.type === "system" && entry.subtype === "turn_duration" && typeof entry.durationMs === "number") {
919
- turnDurations.push({
920
- durationMs: entry.durationMs,
921
- messageCount: entry.messageCount || 0,
922
- uuid: entry.uuid
923
- });
932
+ const turnDuration = turnDurationOf(entry);
933
+ if (turnDuration) {
934
+ turnDurations.push(turnDuration);
924
935
  continue;
925
936
  }
926
937
  const message = reduceConvLine(state, entry);
@@ -1508,6 +1519,7 @@ function reduceCodexEntry(acc, entry, tier) {
1508
1519
  function finalizeCodexMeta(acc, filePath, account, tier) {
1509
1520
  if (acc.messageCount === 0) return null;
1510
1521
  const sessionId = acc.sessionId || basename4(filePath, ".jsonl");
1522
+ const nameSlots = { ...acc.nameSlots, ...codexNameSlotsFor(filePath, acc.sessionId) };
1511
1523
  const projectPath = acc.cwd;
1512
1524
  const kind = acc.lastAssistant === null && acc.toolNames.length > 0 ? "task" : "conversation";
1513
1525
  return {
@@ -1517,7 +1529,8 @@ function finalizeCodexMeta(acc, filePath, account, tier) {
1517
1529
  kind,
1518
1530
  externalSessionId: acc.sessionId || void 0,
1519
1531
  sessionId,
1520
- sessionName: resolveSessionName({ ...acc.nameSlots, ...codexNameSlotsFor(filePath, acc.sessionId) }).name ?? "",
1532
+ sessionName: resolveSessionName(nameSlots).name ?? "",
1533
+ nameSlots,
1521
1534
  projectPath,
1522
1535
  projectName: getShortProjectName3(projectPath),
1523
1536
  account,
@@ -1803,6 +1816,7 @@ function reduceCopilotEntry(acc, entry, tier) {
1803
1816
  function finalizeCopilotMeta(acc, filePath, account, tier) {
1804
1817
  if (acc.messageCount === 0) return null;
1805
1818
  const sessionId = acc.sessionId || sessionIdFromPath(filePath);
1819
+ const nameSlots = { ...acc.nameSlots, ...copilotNameSlotsFor(filePath) };
1806
1820
  const kind = acc.lastAssistant === null && acc.toolNames.length > 0 ? "task" : "conversation";
1807
1821
  return {
1808
1822
  id: filePath,
@@ -1811,7 +1825,8 @@ function finalizeCopilotMeta(acc, filePath, account, tier) {
1811
1825
  kind,
1812
1826
  externalSessionId: sessionId,
1813
1827
  sessionId,
1814
- sessionName: resolveSessionName({ ...acc.nameSlots, ...copilotNameSlotsFor(filePath) }).name ?? "",
1828
+ sessionName: resolveSessionName(nameSlots).name ?? "",
1829
+ nameSlots,
1815
1830
  projectPath: acc.cwd,
1816
1831
  projectName: getShortProjectName4(acc.cwd),
1817
1832
  account,
@@ -2213,6 +2228,7 @@ function reduceCursorEntry(acc, entry, tier) {
2213
2228
  function finalizeCursorMeta(acc, filePath, account, tier) {
2214
2229
  if (acc.messageCount === 0) return null;
2215
2230
  const sessionId = basename6(filePath, ".jsonl");
2231
+ const nameSlots = { ...acc.nameSlots, ...cursorNameSlotsFor(filePath, sessionId) };
2216
2232
  const isSubagent = SUBAGENT_PATH_RE2.test(filePath);
2217
2233
  const parent = isSubagent ? parentFromSubagentPath(filePath) : null;
2218
2234
  const { projectPath, projectName } = projectFromCursorPath(filePath);
@@ -2225,7 +2241,8 @@ function finalizeCursorMeta(acc, filePath, account, tier) {
2225
2241
  kind,
2226
2242
  externalSessionId: sessionId,
2227
2243
  sessionId,
2228
- sessionName: resolveSessionName({ ...acc.nameSlots, ...cursorNameSlotsFor(filePath, sessionId) }).name ?? "",
2244
+ sessionName: resolveSessionName(nameSlots).name ?? "",
2245
+ nameSlots,
2229
2246
  projectPath,
2230
2247
  projectName,
2231
2248
  account,
@@ -2601,7 +2618,7 @@ async function streamMessages(filePath, startOffset, startLine, state, onMessage
2601
2618
  }
2602
2619
  return { offset, line };
2603
2620
  }
2604
- async function buildCheckpoints(filePath, interval = CHECKPOINT_INTERVAL, from = null) {
2621
+ async function buildCheckpoints(filePath, interval = CHECKPOINT_INTERVAL, from = null, onEntry) {
2605
2622
  const checkpoints = [];
2606
2623
  const state = from ? from.state : initialConvState();
2607
2624
  let index = from ? from.messageIndex : 0;
@@ -2621,7 +2638,8 @@ async function buildCheckpoints(filePath, interval = CHECKPOINT_INTERVAL, from =
2621
2638
  });
2622
2639
  }
2623
2640
  return false;
2624
- }
2641
+ },
2642
+ onEntry
2625
2643
  );
2626
2644
  return checkpoints;
2627
2645
  }
@@ -2642,6 +2660,41 @@ async function readPage(filePath, total, options, floor) {
2642
2660
  applyTeamInfo(window, state);
2643
2661
  return { messages: window, total, fromIndex };
2644
2662
  }
2663
+ function isInjectedTurn(message, isInjected) {
2664
+ return message.role === "user" && isInjected(message.text ?? "");
2665
+ }
2666
+ async function countLeadingInjected(filePath, isInjected = isCodexInjectedContext) {
2667
+ let skip = 0;
2668
+ await streamMessages(filePath, 0, 0, initialConvState(), (message) => {
2669
+ if (!isInjectedTurn(message, isInjected)) return true;
2670
+ skip += 1;
2671
+ return false;
2672
+ });
2673
+ return skip;
2674
+ }
2675
+ function filteredWindow(rawTotal, skip, options) {
2676
+ const total = Math.max(0, rawTotal - skip);
2677
+ const beforeIndex = options.beforeIndex ?? total;
2678
+ const fromIndex = Math.max(0, beforeIndex - options.limit);
2679
+ return {
2680
+ total,
2681
+ fromIndex,
2682
+ raw: { beforeIndex: beforeIndex + skip, limit: beforeIndex - fromIndex }
2683
+ };
2684
+ }
2685
+ function pageOfConversation(conversation, options) {
2686
+ const { messages } = conversation;
2687
+ const isInjected = options.isInjectedContext ?? isCodexInjectedContext;
2688
+ const firstKept = messages.findIndex((m) => !isInjectedTurn(m, isInjected));
2689
+ const skip = firstKept === -1 ? messages.length : firstKept;
2690
+ const { total, fromIndex, raw } = filteredWindow(messages.length, skip, options);
2691
+ return {
2692
+ messages: messages.slice(raw.beforeIndex - raw.limit, raw.beforeIndex),
2693
+ total,
2694
+ fromIndex,
2695
+ turnDurations: conversation.turnDurations
2696
+ };
2697
+ }
2645
2698
 
2646
2699
  // src/persistent/conversation-stream.ts
2647
2700
  async function foldTail(filePath, resume) {
@@ -2782,7 +2835,7 @@ import { statSync as statSync5 } from "fs";
2782
2835
  // src/providers/parse.ts
2783
2836
  import { createReadStream as createReadStream7 } from "fs";
2784
2837
  import { createInterface as createInterface5 } from "readline";
2785
- async function parseMetaWithProvider(provider, filePath, account, tier, onEntry, onAccumulator) {
2838
+ async function parseMetaWithProvider(provider, filePath, account, tier, onEntry) {
2786
2839
  const log = getLogger();
2787
2840
  const acc = provider.createEmptyAccumulator();
2788
2841
  const rl = createInterface5({
@@ -2809,7 +2862,6 @@ async function parseMetaWithProvider(provider, filePath, account, tier, onEntry,
2809
2862
  log.warn({ filePath, provider: provider.name, err }, "provider parse: read failed");
2810
2863
  return null;
2811
2864
  }
2812
- onAccumulator?.(acc);
2813
2865
  return provider.finalize(acc, filePath, account, tier);
2814
2866
  }
2815
2867
 
@@ -3659,6 +3711,7 @@ function rowToMeta(row) {
3659
3711
  externalSessionId: row.external_session_id ?? void 0,
3660
3712
  sessionId: row.session_id,
3661
3713
  sessionName: row.session_name ?? "",
3714
+ nameSlots: row.name_slots ? JSON.parse(row.name_slots) : void 0,
3662
3715
  projectPath: row.project_path ?? "",
3663
3716
  projectName: row.project_name ?? "",
3664
3717
  account: row.account,
@@ -3691,7 +3744,7 @@ var ConversationsRepo = class {
3691
3744
  db;
3692
3745
  // Upsert by file_id (1 file = 1 conversation). Keyed on the unique file_id so
3693
3746
  // a re-index overwrites the prior summary in place.
3694
- upsert(fileId, meta, pageMessageCount = meta.messageCount, nameSlots) {
3747
+ upsert(fileId, meta, pageMessageCount = meta.messageCount) {
3695
3748
  this.db.prepare(
3696
3749
  `INSERT INTO conversations (
3697
3750
  file_id, source_path, provider, kind, external_session_id,
@@ -3790,7 +3843,7 @@ var ConversationsRepo = class {
3790
3843
  is_imported_from_claude: meta.isImportedFromClaude ? 1 : 0,
3791
3844
  is_imported_from_codex: meta.isImportedFromCodex ? 1 : 0,
3792
3845
  is_imported_from_cursor: meta.isImportedFromCursor ? 1 : 0,
3793
- name_slots: nameSlots ? JSON.stringify(nameSlots) : null
3846
+ name_slots: meta.nameSlots ? JSON.stringify(meta.nameSlots) : null
3794
3847
  });
3795
3848
  }
3796
3849
  getBySourcePath(sourcePath) {
@@ -4171,6 +4224,7 @@ var PersistentEngine = class {
4171
4224
  const tier = resolveTier(tierName, customTiers);
4172
4225
  const filePath = canonicalPath(rawFilePath);
4173
4226
  const existing = this.files.getByPath(filePath);
4227
+ const refold = force || (!provider || provider.name === CLAUDE_CODE_PROVIDER) && !!existing?.reducer_state && !existing.reducer_state.includes('"turnDurations"');
4174
4228
  const classified = classify(filePath, existing);
4175
4229
  const stat6 = classified.stat;
4176
4230
  let change = classified.change;
@@ -4178,7 +4232,7 @@ var PersistentEngine = class {
4178
4232
  this.markDeleted(filePath);
4179
4233
  return { meta: null, change: "vanished" };
4180
4234
  }
4181
- if (change === "unchanged" && !force) {
4235
+ if (change === "unchanged" && !refold) {
4182
4236
  const meta2 = this.conversations.getBySourcePath(filePath);
4183
4237
  const sideStoresMoved = provider && provider.name !== CLAUDE_CODE_PROVIDER && meta2 && JSON.stringify(nameStoreSlotsFor(provider.name, filePath, meta2.sessionId)) !== existing?.reducer_state;
4184
4238
  if (!sideStoresMoved) return { meta: meta2, change };
@@ -4195,7 +4249,7 @@ var PersistentEngine = class {
4195
4249
  );
4196
4250
  return { meta: meta2, change };
4197
4251
  }
4198
- const resume = change === "appended" && !force && existing?.reducer_state;
4252
+ const resume = change === "appended" && !refold && existing?.reducer_state;
4199
4253
  const state = resume ? JSON.parse(existing.reducer_state) : initialReducerState();
4200
4254
  const startOffset = resume ? existing.last_indexed_offset : 0;
4201
4255
  const startLine = resume ? existing.last_indexed_line : 0;
@@ -4221,7 +4275,7 @@ var PersistentEngine = class {
4221
4275
  const base = resume ? this.fts.readDocument(fileId) ?? emptySearchDocument() : emptySearchDocument();
4222
4276
  const searchDoc = appendSearchDelta(base, searchDelta);
4223
4277
  const upsert = this.db.transaction(() => {
4224
- this.conversations.upsert(fileId, meta, state.pageMessageCount, state.nameSlots);
4278
+ this.conversations.upsert(fileId, meta, state.pageMessageCount);
4225
4279
  if (!this.fts.isCurrent(fileId, meta, searchDoc)) {
4226
4280
  this.fts.upsert(fileId, meta, searchDoc);
4227
4281
  }
@@ -4268,19 +4322,9 @@ var PersistentEngine = class {
4268
4322
  async indexFileWithProvider(provider, filePath, account, tier, stat6, resolveGitBranch) {
4269
4323
  const log = getLogger();
4270
4324
  let searchDoc = emptySearchDocument();
4271
- let transcriptSlots = {};
4272
- const meta = await parseMetaWithProvider(
4273
- provider,
4274
- filePath,
4275
- account,
4276
- tier,
4277
- (entry) => {
4278
- searchDoc = appendSearchDelta(searchDoc, extractSearchDelta(entry));
4279
- },
4280
- (acc) => {
4281
- transcriptSlots = acc.nameSlots ?? {};
4282
- }
4283
- );
4325
+ const meta = await parseMetaWithProvider(provider, filePath, account, tier, (entry) => {
4326
+ searchDoc = appendSearchDelta(searchDoc, extractSearchDelta(entry));
4327
+ });
4284
4328
  if (!meta) {
4285
4329
  this.markDeleted(filePath);
4286
4330
  return null;
@@ -4292,10 +4336,7 @@ var PersistentEngine = class {
4292
4336
  const fp = stat6.size > 0 ? fingerprint(filePath, stat6.size) : null;
4293
4337
  const fileId = this.files.ensure(filePath, account);
4294
4338
  const upsert = this.db.transaction(() => {
4295
- this.conversations.upsert(fileId, meta, meta.messageCount, {
4296
- ...transcriptSlots,
4297
- ...sideSlots
4298
- });
4339
+ this.conversations.upsert(fileId, meta, meta.messageCount);
4299
4340
  if (!this.fts.isCurrent(fileId, meta, searchDoc)) {
4300
4341
  this.fts.upsert(fileId, meta, searchDoc);
4301
4342
  }
@@ -4369,6 +4410,10 @@ var PersistentEngine = class {
4369
4410
  // resolved to an indexed conversation. For conversations large enough to span
4370
4411
  // a checkpoint interval, checkpoints are built lazily on first access and
4371
4412
  // reused thereafter. Smaller conversations read from the start (cheap).
4413
+ //
4414
+ // The window is over the conversation with its leading injected-context run
4415
+ // dropped (options.isInjectedContext, see filteredWindow). The caller keeps the
4416
+ // index current (refreshFile) first; this reads what the index describes.
4372
4417
  async getPage(id, options) {
4373
4418
  const meta = this.conversations.getByIdOrSession(id);
4374
4419
  if (!meta) return null;
@@ -4376,24 +4421,28 @@ var PersistentEngine = class {
4376
4421
  if (meta.provider === CODEX_CLI_PROVIDER || meta.provider === CURSOR_PROVIDER || meta.provider === COPILOT_PROVIDER) {
4377
4422
  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);
4378
4423
  if (!conversation) return null;
4379
- const { messages } = conversation;
4380
- const total2 = messages.length;
4381
- const beforeIndex2 = options.beforeIndex ?? total2;
4382
- const fromIndex2 = Math.max(0, beforeIndex2 - options.limit);
4383
- return { messages: messages.slice(fromIndex2, beforeIndex2), total: total2, fromIndex: fromIndex2 };
4384
- }
4385
- const total = this.conversations.pageMessageCount(filePath);
4386
- await this.ensureCheckpoints(filePath, total);
4387
- const beforeIndex = options.beforeIndex ?? total;
4388
- const fromIndex = Math.max(0, beforeIndex - options.limit);
4389
- let floor = this.checkpoints.floor(filePath, fromIndex);
4424
+ return pageOfConversation(conversation, options);
4425
+ }
4426
+ const rawTotal = this.conversations.pageMessageCount(filePath);
4427
+ const skip = await countLeadingInjected(filePath, options.isInjectedContext);
4428
+ const { total, fromIndex, raw } = filteredWindow(rawTotal, skip, options);
4429
+ await this.ensureCheckpoints(filePath, rawTotal);
4430
+ let floor = this.checkpoints.floor(filePath, raw.beforeIndex - raw.limit);
4390
4431
  if (floor) {
4391
4432
  try {
4392
4433
  if (floor.byteOffset > statSync5(filePath).size) floor = null;
4393
4434
  } catch {
4394
4435
  }
4395
4436
  }
4396
- return readPage(filePath, total, options, floor);
4437
+ const page = await readPage(filePath, rawTotal, raw, floor);
4438
+ const state = this.files.getByPath(filePath)?.reducer_state;
4439
+ const durations = state ? JSON.parse(state).turnDurations : void 0;
4440
+ return {
4441
+ messages: page.messages,
4442
+ total,
4443
+ fromIndex,
4444
+ turnDurations: durations?.length ? durations : void 0
4445
+ };
4397
4446
  }
4398
4447
  // Build or extend the checkpoint chain so it covers `total` messages. Cold
4399
4448
  // file → full build; a file that grew → extend from the last persisted
@@ -4882,36 +4931,94 @@ var ConversationScanner = class {
4882
4931
  }
4883
4932
  }
4884
4933
  // Return one bounded window of a conversation's messages plus the total
4885
- // message count, so a caller can serve the last page and scroll back without
4886
- // holding the whole conversation itself.
4934
+ // message count, the conversation's current meta and its turn durations, so a
4935
+ // caller can serve the last page and scroll back without holding the whole
4936
+ // conversation itself.
4887
4937
  //
4888
- // The window is `[max(0, beforeIndex - limit), beforeIndex)` in chronological
4938
+ // Messages are those of the full read with the LEADING run of injected user
4939
+ // turns dropped (options.isInjectedContext, default isCodexInjectedContext);
4940
+ // `total`, `fromIndex` and `beforeIndex` all count in that filtered space. The
4941
+ // window is `[max(0, beforeIndex - limit), beforeIndex)` in chronological
4889
4942
  // order; `beforeIndex` defaults to `total` (the newest page). `fromIndex` is
4890
4943
  // the window's start index, so the caller can derive `has_more_older`
4891
4944
  // (fromIndex > 0). Returns null when the id can't be resolved/parsed — the
4892
4945
  // same contract as getConversation.
4893
4946
  //
4894
- // Persistent mode: a true bounded read — the engine seeks from the nearest
4895
- // checkpoint and parses only the requested window (checkpoints are built
4896
- // lazily for large conversations). Windowed message indices are proven
4897
- // identical to parseConversation().messages.slice(...) by the paged-reader
4898
- // equivalence test.
4899
- //
4900
- // Legacy mode: parse-once-then-slice via getConversation (cached in the LRU),
4901
- // which is identical by construction.
4947
+ // Every call reads the file as it is on disk now, never a cached parse:
4948
+ // persistent mode refreshes the index first (O(appended bytes)) and the engine
4949
+ // seeks from the nearest checkpoint. In-memory claude-code files keep a
4950
+ // stat-validated checkpoint index (pageIndexFor). Other providers are parsed
4951
+ // whole per call.
4902
4952
  async getConversationPage(id, options) {
4953
+ const cid = canonicalPath(id);
4954
+ const known = this.persistent ? this.engine().getByIdOrSession(cid) : this.metadataCache.get(cid) ?? this.resolveSessionId(cid);
4955
+ if (!known) return null;
4956
+ const isClaude = known.provider !== CODEX_CLI_PROVIDER && known.provider !== CURSOR_PROVIDER && known.provider !== COPILOT_PROVIDER;
4957
+ if (!this.persistent && isClaude) {
4958
+ const index = await this.pageIndexFor(known.filePath, known.account);
4959
+ if (!index) return null;
4960
+ const skip = await countLeadingInjected(known.filePath, options.isInjectedContext);
4961
+ const { total, fromIndex, raw } = filteredWindow(index.rawTotal, skip, options);
4962
+ const rawFrom = raw.beforeIndex - raw.limit;
4963
+ let floor = null;
4964
+ for (const c of index.checkpoints) if (c.messageIndex <= rawFrom) floor = c;
4965
+ const page = await readPage(known.filePath, index.rawTotal, raw, floor);
4966
+ return {
4967
+ messages: page.messages,
4968
+ total,
4969
+ fromIndex,
4970
+ meta: index.meta,
4971
+ turnDurations: index.turnDurations
4972
+ };
4973
+ }
4974
+ const meta = await this.refreshFile(known.filePath, known.account);
4975
+ if (!meta) return null;
4903
4976
  if (this.persistent) {
4904
- return this.engine().getPage(canonicalPath(id), options);
4977
+ const page = await this.engine().getPage(meta.filePath, options);
4978
+ return page && { ...page, meta };
4979
+ }
4980
+ const conversation = await this.getConversation(meta.filePath);
4981
+ return conversation && { ...pageOfConversation(conversation, options), meta };
4982
+ }
4983
+ // In-memory claude-code paging index: one streamed pass builds the checkpoint
4984
+ // chain and folds the metadata (meta, page total, turn durations) alongside,
4985
+ // reused until the file's size or mtime changes. Memory stays bounded: the
4986
+ // file is never held, and the index LRU is capped.
4987
+ // ponytail: any change rebuilds from byte 0; extending from the last
4988
+ // checkpoint plus a persisted ReducerState (as the engine does) is the
4989
+ // upgrade path if live-appended transcripts make this hot.
4990
+ pageIndexes = new LRUCache(32);
4991
+ async pageIndexFor(filePath, account) {
4992
+ let stat6;
4993
+ try {
4994
+ stat6 = statSync6(filePath);
4995
+ } catch {
4996
+ this.pageIndexes.delete(filePath);
4997
+ return null;
4905
4998
  }
4906
- const conversation = await this.getConversation(id);
4907
- if (!conversation) return null;
4908
- const { messages } = conversation;
4909
- const total = messages.length;
4910
- const { limit } = options;
4911
- const beforeIndex = options.beforeIndex ?? total;
4912
- const fromIndex = Math.max(0, beforeIndex - limit);
4913
- const window = messages.slice(fromIndex, beforeIndex);
4914
- return { messages: window, total, fromIndex };
4999
+ const cached = this.pageIndexes.get(filePath);
5000
+ if (cached && cached.size === stat6.size && cached.mtimeMs === stat6.mtimeMs) return cached;
5001
+ const state = initialReducerState();
5002
+ const checkpoints = await buildCheckpoints(filePath, CHECKPOINT_INTERVAL, null, (entry) => {
5003
+ reduceLine(state, entry, this.lastTier);
5004
+ return false;
5005
+ });
5006
+ const meta = finalizeMeta(state, filePath, account, this.lastTier);
5007
+ if (!meta) {
5008
+ this.pageIndexes.delete(filePath);
5009
+ return null;
5010
+ }
5011
+ meta.gitBranch = readGitBranch(meta.projectPath);
5012
+ const index = {
5013
+ size: stat6.size,
5014
+ mtimeMs: stat6.mtimeMs,
5015
+ checkpoints,
5016
+ meta,
5017
+ rawTotal: state.pageMessageCount,
5018
+ turnDurations: state.turnDurations?.length ? state.turnDurations : void 0
5019
+ };
5020
+ this.pageIndexes.set(filePath, index);
5021
+ return index;
4915
5022
  }
4916
5023
  // Parse one JSONL file directly and slice a page window — without any prior
4917
5024
  // scan() or metadata index. This is the cold-start fast path: a single