@threadbase-sh/scanner 0.21.0 → 0.22.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.
Files changed (39) hide show
  1. package/README.md +3 -1
  2. package/dist/cli.js +496 -128
  3. package/dist/cli.js.map +1 -1
  4. package/dist/index.cjs +579 -152
  5. package/dist/index.cjs.map +1 -1
  6. package/dist/index.d.ts +3 -0
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +551 -133
  9. package/dist/index.js.map +1 -1
  10. package/dist/name-stores.d.ts +13 -0
  11. package/dist/name-stores.d.ts.map +1 -0
  12. package/dist/parser.d.ts.map +1 -1
  13. package/dist/persistent/claude-name-slots.d.ts +3 -0
  14. package/dist/persistent/claude-name-slots.d.ts.map +1 -0
  15. package/dist/persistent/conversation-reducer.d.ts +2 -1
  16. package/dist/persistent/conversation-reducer.d.ts.map +1 -1
  17. package/dist/persistent/conversation-stream.d.ts.map +1 -1
  18. package/dist/persistent/index-engine.d.ts.map +1 -1
  19. package/dist/persistent/metadata-reducer.d.ts +2 -4
  20. package/dist/persistent/metadata-reducer.d.ts.map +1 -1
  21. package/dist/persistent/migrations.d.ts.map +1 -1
  22. package/dist/persistent/repositories/conversations.repo.d.ts +2 -1
  23. package/dist/persistent/repositories/conversations.repo.d.ts.map +1 -1
  24. package/dist/persistent/schema.d.ts +2 -2
  25. package/dist/persistent/schema.d.ts.map +1 -1
  26. package/dist/providers/codex-cli.d.ts +3 -6
  27. package/dist/providers/codex-cli.d.ts.map +1 -1
  28. package/dist/providers/copilot.d.ts +2 -0
  29. package/dist/providers/copilot.d.ts.map +1 -1
  30. package/dist/providers/cursor.d.ts +2 -0
  31. package/dist/providers/cursor.d.ts.map +1 -1
  32. package/dist/providers/parse.d.ts +1 -1
  33. package/dist/providers/parse.d.ts.map +1 -1
  34. package/dist/scanner.d.ts.map +1 -1
  35. package/dist/session-name.d.ts +24 -0
  36. package/dist/session-name.d.ts.map +1 -0
  37. package/dist/types.d.ts +1 -0
  38. package/dist/types.d.ts.map +1 -1
  39. package/package.json +1 -1
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.21.0";
8
+ var version = "0.22.0";
9
9
 
10
10
  // src/logger.ts
11
11
  import pino from "pino";
@@ -60,9 +60,9 @@ async function saveProfiles(profiles, configPath) {
60
60
 
61
61
  // src/scanner.ts
62
62
  import { EventEmitter } from "events";
63
- import { closeSync as closeSync2, openSync as openSync2, readSync as readSync2, statSync as statSync5 } from "fs";
63
+ import { closeSync as closeSync3, openSync as openSync3, readSync as readSync3, statSync as statSync6 } from "fs";
64
64
  import { homedir as homedir2 } from "os";
65
- import { join as join6 } from "path";
65
+ import { join as join7 } from "path";
66
66
 
67
67
  // src/cache.ts
68
68
  var LRUCache = class {
@@ -506,17 +506,317 @@ function toIndexDoc(meta, searchContent) {
506
506
  };
507
507
  }
508
508
 
509
+ // src/name-stores.ts
510
+ import Database from "better-sqlite3";
511
+ import { closeSync, existsSync, openSync, readdirSync, readFileSync as readFileSync2, readSync, statSync } from "fs";
512
+ import { basename, dirname as dirname2, join as join3 } from "path";
513
+
514
+ // src/tags.ts
515
+ var SYSTEM_TAGS = [
516
+ "system-reminder",
517
+ "command-name",
518
+ "command-message",
519
+ "command-args",
520
+ "ide_selection",
521
+ "ide_opened_file",
522
+ "local-command-stdout",
523
+ "local-command-caveat",
524
+ "retrieval_status",
525
+ "task_id",
526
+ "task_type",
527
+ "task-id",
528
+ "task-notification",
529
+ "fast_mode_info",
530
+ "persisted-output",
531
+ "tool_use_error",
532
+ "user-prompt-submit-hook",
533
+ "thinking",
534
+ "ask_user",
535
+ "teammate-message"
536
+ ];
537
+ var SYSTEM_TAG_RE = new RegExp(`<(${SYSTEM_TAGS.join("|")})[^>]*>[\\s\\S]*?<\\/\\1>`, "g");
538
+ var PASTED_CONTENT_RE = /<pasted_content id="[0-9a-f]{4}">([\s\S]*?)<\/pasted_content(?: id="[0-9a-f]{4}")?>/g;
539
+ function cleanSystemTags(text) {
540
+ return text.replace(SYSTEM_TAG_RE, "").replace(PASTED_CONTENT_RE, "$1").replace(/[^\S\n]+/g, " ").replace(/\n{3,}/g, "\n\n").trim();
541
+ }
542
+
543
+ // src/session-name.ts
544
+ var NAME_PRECEDENCE = [
545
+ "userTitle",
546
+ "toolTitle",
547
+ "lastPrompt",
548
+ "firstUserMessage",
549
+ "slug"
550
+ ];
551
+ var CLAUDE_SYSTEM_PREFIXES = [
552
+ "<command-name>",
553
+ "<command-message>",
554
+ "<command-args>",
555
+ "<local-command-stdout>",
556
+ "<local-command-stderr>",
557
+ "<local-command-caveat>",
558
+ "<bash-input>",
559
+ "<bash-stdout>",
560
+ "<bash-stderr>",
561
+ "<system-reminder>",
562
+ "<task-notification>",
563
+ "<teammate-message",
564
+ "Another Claude session sent a message:",
565
+ "[Request interrupted by user"
566
+ ];
567
+ var CODEX_SYSTEM_PREFIXES = [
568
+ "<environment_context>",
569
+ "# AGENTS.md instructions",
570
+ "<user_instructions>",
571
+ "<INSTRUCTIONS>",
572
+ "<external_codex_apps_open",
573
+ "<user_shell_command>",
574
+ "<turn_aborted>"
575
+ ];
576
+ function isCodexInjectedContext(text) {
577
+ if (text.startsWith("# AGENTS.md") || text.includes("<INSTRUCTIONS>")) return true;
578
+ return text.startsWith("<permissions instructions>") || text.includes("Filesystem sandboxing defines");
579
+ }
580
+ var CODEX_IMAGE_TAG_RE = /<\/?image\b[^>]*>/g;
581
+ function normalizeCandidate(text, provider) {
582
+ let t = text.trim();
583
+ if (provider === "codex-cli") t = t.replace(CODEX_IMAGE_TAG_RE, "").trim();
584
+ const prefixes = provider === "claude-code" ? CLAUDE_SYSTEM_PREFIXES : provider === "codex-cli" ? CODEX_SYSTEM_PREFIXES : [];
585
+ if (prefixes.some((p) => t.startsWith(p))) return null;
586
+ if (provider === "codex-cli" && isCodexInjectedContext(t)) return null;
587
+ return cleanSystemTags(t) || null;
588
+ }
589
+ function resolveSessionName(slots) {
590
+ for (const slot of NAME_PRECEDENCE) {
591
+ const value = slots[slot];
592
+ if (value?.trim()) return { name: value, source: slot };
593
+ }
594
+ return { name: null, source: null };
595
+ }
596
+ function applyNameCandidate(slots, slot, raw, provider) {
597
+ if (typeof raw !== "string") return slots;
598
+ if (slot === "firstUserMessage" && slots.firstUserMessage !== void 0) return slots;
599
+ const text = normalizeCandidate(raw, provider);
600
+ return text === null ? slots : { ...slots, [slot]: text };
601
+ }
602
+
603
+ // src/name-stores.ts
604
+ function slotsFrom(provider, candidates) {
605
+ return candidates.reduce(
606
+ (slots, [slot, raw]) => applyNameCandidate(slots, slot, typeof raw === "string" ? raw : void 0, provider),
607
+ {}
608
+ );
609
+ }
610
+ function readJson(path) {
611
+ try {
612
+ return JSON.parse(readFileSync2(path, "utf8"));
613
+ } catch {
614
+ return void 0;
615
+ }
616
+ }
617
+ function queryOne(dbPath, sql, ...params) {
618
+ let db;
619
+ try {
620
+ db = new Database(dbPath, { readonly: true, fileMustExist: true, timeout: 250 });
621
+ return db.prepare(sql).get(...params);
622
+ } catch (err) {
623
+ getLogger().debug({ dbPath, err }, "name store: query failed");
624
+ return void 0;
625
+ } finally {
626
+ db?.close();
627
+ }
628
+ }
629
+ var lastByIdCache = /* @__PURE__ */ new Map();
630
+ function lastValueById(path, idField, valueField) {
631
+ let sig;
632
+ try {
633
+ const s = statSync(path);
634
+ sig = `${s.mtimeMs}:${s.size}`;
635
+ } catch {
636
+ return /* @__PURE__ */ new Map();
637
+ }
638
+ const key = `${path}\0${idField}\0${valueField}`;
639
+ const cached = lastByIdCache.get(key);
640
+ if (cached?.sig === sig) return cached.map;
641
+ const map = /* @__PURE__ */ new Map();
642
+ try {
643
+ for (const line of readFileSync2(path, "utf8").split("\n")) {
644
+ if (!line.trim()) continue;
645
+ try {
646
+ const row = JSON.parse(line);
647
+ const id = row[idField];
648
+ const value = row[valueField];
649
+ if (typeof id === "string" && typeof value === "string") map.set(id, value);
650
+ } catch {
651
+ }
652
+ }
653
+ } catch {
654
+ }
655
+ lastByIdCache.set(key, { sig, map });
656
+ return map;
657
+ }
658
+ function readCodexNameSlots(codexHome, id) {
659
+ const index = lastValueById(join3(codexHome, "session_index.jsonl"), "id", "thread_name");
660
+ const history = lastValueById(join3(codexHome, "history.jsonl"), "session_id", "text");
661
+ let title = index.get(id);
662
+ if (!index.has(id)) {
663
+ title = queryOne(
664
+ join3(codexHome, "state_5.sqlite"),
665
+ "SELECT name FROM threads WHERE id = ?",
666
+ id
667
+ )?.name;
668
+ }
669
+ return slotsFrom("codex-cli", [
670
+ ["toolTitle", title],
671
+ ["lastPrompt", history.get(id)]
672
+ ]);
673
+ }
674
+ function readCursorCliNameSlots(chatsDir, id) {
675
+ let chat;
676
+ try {
677
+ chat = readdirSync(chatsDir).map((hash) => join3(chatsDir, hash, id)).find((p) => existsSync(p));
678
+ } catch {
679
+ }
680
+ if (!chat) return {};
681
+ const meta = readJson(join3(chat, "meta.json"));
682
+ const history = readJson(join3(chat, "prompt_history.json"));
683
+ let title = meta?.title;
684
+ if (typeof title !== "string" || !title.trim()) title = storeDbName(join3(chat, "store.db"));
685
+ return slotsFrom("cursor", [
686
+ ["toolTitle", title],
687
+ ["lastPrompt", Array.isArray(history) ? history[0] : void 0]
688
+ ]);
689
+ }
690
+ function storeDbName(dbPath) {
691
+ const row = queryOne(
692
+ dbPath,
693
+ "SELECT value FROM meta WHERE key = '0'"
694
+ );
695
+ if (!row) return void 0;
696
+ try {
697
+ const text = row.value.toString();
698
+ const json = /^(?:[0-9a-f]{2})+$/i.test(text) ? Buffer.from(text, "hex").toString("utf8") : text;
699
+ const name = JSON.parse(json).name;
700
+ return typeof name === "string" ? name : void 0;
701
+ } catch {
702
+ return void 0;
703
+ }
704
+ }
705
+ function readCursorIdeNameSlots(stateDbPath, id) {
706
+ const row = queryOne(
707
+ stateDbPath,
708
+ "SELECT json_extract(CAST(value AS TEXT), '$.name') AS name FROM cursorDiskKV WHERE key = ?",
709
+ `composerData:${id}`
710
+ );
711
+ return slotsFrom("cursor", [["toolTitle", row?.name]]);
712
+ }
713
+ function yamlScalar(text, key) {
714
+ const m = text.match(new RegExp(`^${key}:[ \\t]*(.*)$`, "m"));
715
+ const v = m?.[1].trim().replace(/^(["'])(.*)\1$/, "$2");
716
+ return v || void 0;
717
+ }
718
+ function readCopilotTitleSlots(sessionDir) {
719
+ const id = basename(sessionDir);
720
+ let yaml = "";
721
+ try {
722
+ yaml = readFileSync2(join3(sessionDir, "workspace.yaml"), "utf8");
723
+ } catch {
724
+ }
725
+ const name = yamlScalar(yaml, "name");
726
+ if (name) {
727
+ return slotsFrom("copilot", [
728
+ [yamlScalar(yaml, "user_named") === "true" ? "userTitle" : "toolTitle", name]
729
+ ]);
730
+ }
731
+ const row = queryOne(
732
+ join3(dirname2(dirname2(sessionDir)), "session-store.db"),
733
+ "SELECT summary FROM sessions WHERE id = ?",
734
+ id
735
+ );
736
+ return slotsFrom("copilot", [["toolTitle", row?.summary]]);
737
+ }
738
+ function findUp(start, hasStore, levels) {
739
+ let dir = start;
740
+ for (let i = 0; i < levels; i++) {
741
+ if (hasStore(dir)) return dir;
742
+ const parent = dirname2(dir);
743
+ if (parent === dir) break;
744
+ dir = parent;
745
+ }
746
+ return null;
747
+ }
748
+ function codexNameSlotsFor(rolloutPath, id) {
749
+ const home = findUp(
750
+ dirname2(rolloutPath),
751
+ (d) => ["session_index.jsonl", "history.jsonl", "state_5.sqlite"].some(
752
+ (f) => existsSync(join3(d, f))
753
+ ),
754
+ 6
755
+ );
756
+ return home ? readCodexNameSlots(home, id) : {};
757
+ }
758
+ var cursorStateDb;
759
+ function setCursorStateDb(path) {
760
+ cursorStateDb = path;
761
+ }
762
+ function cursorNameSlotsFor(transcriptPath, id) {
763
+ const home = findUp(dirname2(transcriptPath), (d) => existsSync(join3(d, "chats")), 6);
764
+ const cli = home ? readCursorCliNameSlots(join3(home, "chats"), id) : {};
765
+ if (cli.toolTitle || !cursorStateDb) return cli;
766
+ return { ...cli, ...readCursorIdeNameSlots(cursorStateDb, id) };
767
+ }
768
+ function copilotNameSlotsFor(eventsPath) {
769
+ return readCopilotTitleSlots(dirname2(eventsPath));
770
+ }
771
+ function nameStoreSlotsFor(provider, filePath, sessionId) {
772
+ switch (provider) {
773
+ case "codex-cli":
774
+ return codexNameSlotsFor(filePath, sessionId);
775
+ case "cursor":
776
+ return cursorNameSlotsFor(filePath, basename(filePath, ".jsonl"));
777
+ case "copilot":
778
+ return copilotNameSlotsFor(filePath);
779
+ default:
780
+ return {};
781
+ }
782
+ }
783
+
509
784
  // src/parser.ts
510
785
  import { createReadStream } from "fs";
511
- import { basename as basename2 } from "path";
786
+ import { basename as basename3 } from "path";
512
787
  import { createInterface } from "readline";
513
788
 
789
+ // src/persistent/claude-name-slots.ts
790
+ function userPromptText(entry) {
791
+ if (entry.isMeta || entry.isCompactSummary === true) return "";
792
+ const content = entry.message?.content;
793
+ if (typeof content === "string") return content;
794
+ if (!Array.isArray(content)) return "";
795
+ return content.map((b) => typeof b === "string" ? b : b?.type === "text" ? b.text : "").filter((t) => typeof t === "string" && t !== "").join(" ");
796
+ }
797
+ function applyClaudeNameEntry(slots, entry) {
798
+ const apply = (slot, raw) => applyNameCandidate(slots, slot, typeof raw === "string" ? raw : void 0, "claude-code");
799
+ if (entry.slug !== slots.slug) slots = apply("slug", entry.slug);
800
+ switch (entry.type) {
801
+ case "custom-title":
802
+ return apply("userTitle", entry.customTitle);
803
+ case "ai-title":
804
+ return apply("toolTitle", entry.aiTitle);
805
+ case "last-prompt":
806
+ return apply("lastPrompt", entry.lastPrompt);
807
+ case "user":
808
+ return slots.firstUserMessage === void 0 ? apply("firstUserMessage", userPromptText(entry)) : slots;
809
+ default:
810
+ return slots;
811
+ }
812
+ }
813
+
514
814
  // src/persistent/conversation-reducer.ts
515
815
  function initialConvState() {
516
816
  return {
517
817
  cwd: "",
518
818
  sessionId: "",
519
- sessionName: "",
819
+ nameSlots: {},
520
820
  latestTimestamp: "",
521
821
  lastPrompt: "",
522
822
  pendingToolUses: {},
@@ -526,7 +826,7 @@ function initialConvState() {
526
826
  function reduceConvLine(state, entry) {
527
827
  if (entry.cwd && !state.cwd) state.cwd = entry.cwd;
528
828
  if (entry.sessionId && !state.sessionId) state.sessionId = entry.sessionId;
529
- if (entry.slug && !state.sessionName) state.sessionName = entry.slug;
829
+ state.nameSlots = applyClaudeNameEntry(state.nameSlots, entry);
530
830
  if (entry.timestamp) {
531
831
  const ts = entry.timestamp;
532
832
  if (!state.latestTimestamp || ts > state.latestTimestamp) state.latestTimestamp = ts;
@@ -623,7 +923,7 @@ function applyTeamInfo(messages, state) {
623
923
  }
624
924
 
625
925
  // src/persistent/metadata-reducer.ts
626
- import { basename, dirname as dirname2, join as join3 } from "path";
926
+ import { basename as basename2, dirname as dirname3, join as join4 } from "path";
627
927
 
628
928
  // src/providers/provider.ts
629
929
  var CLAUDE_CODE_PROVIDER = "claude-code";
@@ -657,7 +957,7 @@ function providerMatches(stored, wanted) {
657
957
  function initialReducerState() {
658
958
  return {
659
959
  sessionId: "",
660
- sessionName: "",
960
+ nameSlots: {},
661
961
  latestTimestamp: "",
662
962
  cwd: "",
663
963
  teamName: "",
@@ -683,7 +983,7 @@ function initialReducerState() {
683
983
  function reduceLine(state, entry, tier) {
684
984
  if (entry.cwd && !state.cwd) state.cwd = entry.cwd;
685
985
  if (entry.sessionId && !state.sessionId) state.sessionId = entry.sessionId;
686
- if (entry.slug && !state.sessionName) state.sessionName = entry.slug;
986
+ state.nameSlots = applyClaudeNameEntry(state.nameSlots, entry);
687
987
  if (entry.teamName && !state.teamName) state.teamName = entry.teamName;
688
988
  if (entry.agentId && !state.agentId) state.agentId = entry.agentId;
689
989
  if (entry.isSidechain === true) state.isSidechain = true;
@@ -747,16 +1047,16 @@ function finalizeMeta(state, filePath, account, tier) {
747
1047
  const isSubagent = isSidechain || isSubagentPath;
748
1048
  let parentSessionId = null;
749
1049
  if (isSubagentPath) {
750
- const uuidDir = dirname2(dirname2(filePath));
751
- parentSessionId = join3(dirname2(uuidDir), `${basename(uuidDir)}.jsonl`);
1050
+ const uuidDir = dirname3(dirname3(filePath));
1051
+ parentSessionId = join4(dirname3(uuidDir), `${basename2(uuidDir)}.jsonl`);
752
1052
  }
753
1053
  const projectPath = state.cwd;
754
1054
  return {
755
1055
  id: filePath,
756
1056
  filePath,
757
1057
  provider: CLAUDE_CODE_PROVIDER,
758
- sessionId: state.sessionId || basename(filePath, ".jsonl"),
759
- sessionName: state.sessionName || deriveSessionNameFromFirstMessage(state.firstMessage),
1058
+ sessionId: state.sessionId || basename2(filePath, ".jsonl"),
1059
+ sessionName: resolveSessionName(state.nameSlots).name ?? "",
760
1060
  projectPath,
761
1061
  projectName: getShortProjectName(projectPath),
762
1062
  account,
@@ -782,44 +1082,11 @@ function finalizeMeta(state, filePath, account, tier) {
782
1082
  };
783
1083
  }
784
1084
  var SUBAGENT_PATH_RE = /[\\/]subagents[\\/]/;
785
- function deriveSessionNameFromFirstMessage(firstMessage) {
786
- const firstLine = firstMessage?.text.split("\n", 1)[0]?.trim() ?? "";
787
- return firstLine.slice(0, 80);
788
- }
789
1085
  function getShortProjectName(fullPath) {
790
1086
  const parts = fullPath.split("/").filter(Boolean);
791
1087
  return parts.slice(-3).join("/");
792
1088
  }
793
1089
 
794
- // src/tags.ts
795
- var SYSTEM_TAGS = [
796
- "system-reminder",
797
- "command-name",
798
- "command-message",
799
- "command-args",
800
- "ide_selection",
801
- "ide_opened_file",
802
- "local-command-stdout",
803
- "local-command-caveat",
804
- "retrieval_status",
805
- "task_id",
806
- "task_type",
807
- "task-id",
808
- "task-notification",
809
- "fast_mode_info",
810
- "persisted-output",
811
- "tool_use_error",
812
- "user-prompt-submit-hook",
813
- "thinking",
814
- "ask_user",
815
- "teammate-message"
816
- ];
817
- var SYSTEM_TAG_RE = new RegExp(`<(${SYSTEM_TAGS.join("|")})[^>]*>[\\s\\S]*?<\\/\\1>`, "g");
818
- var PASTED_CONTENT_RE = /<pasted_content id="[0-9a-f]{4}">([\s\S]*?)<\/pasted_content(?: id="[0-9a-f]{4}")?>/g;
819
- function cleanSystemTags(text) {
820
- return text.replace(SYSTEM_TAG_RE, "").replace(PASTED_CONTENT_RE, "$1").replace(/[^\S\n]+/g, " ").replace(/\n{3,}/g, "\n\n").trim();
821
- }
822
-
823
1090
  // src/parser.ts
824
1091
  async function parseMeta(filePath, account, tier, onEntry) {
825
1092
  const log = getLogger();
@@ -885,7 +1152,7 @@ async function parseConversation(filePath, account) {
885
1152
  const message = reduceConvLine(state, entry);
886
1153
  if (message) {
887
1154
  messages.push(message);
888
- if (message.text) textParts.push(message.text);
1155
+ if (message.text && !message.isCompactSummary) textParts.push(message.text);
889
1156
  }
890
1157
  }
891
1158
  } catch (err) {
@@ -906,8 +1173,8 @@ async function parseConversation(filePath, account) {
906
1173
  filePath,
907
1174
  projectPath: state.cwd,
908
1175
  projectName: getShortProjectName2(state.cwd),
909
- sessionId: state.sessionId || basename2(filePath, ".jsonl"),
910
- sessionName: state.sessionName || deriveSessionNameFromFirstMessage(messages.find((m) => m.role === "user" && m.text) ?? null),
1176
+ sessionId: state.sessionId || basename3(filePath, ".jsonl"),
1177
+ sessionName: resolveSessionName(state.nameSlots).name ?? "",
911
1178
  messages,
912
1179
  fullText: textParts.join(" "),
913
1180
  timestamp: state.latestTimestamp || (/* @__PURE__ */ new Date()).toISOString(),
@@ -1013,7 +1280,7 @@ function getShortProjectName2(fullPath) {
1013
1280
  }
1014
1281
 
1015
1282
  // src/persistent/conversation-stream.ts
1016
- import { basename as basename3 } from "path";
1283
+ import { basename as basename4 } from "path";
1017
1284
 
1018
1285
  // src/persistent/paged-reader.ts
1019
1286
  import { createReadStream as createReadStream3 } from "fs";
@@ -1150,7 +1417,7 @@ async function foldTail(filePath, resume) {
1150
1417
  resume.state,
1151
1418
  (message) => {
1152
1419
  messages.push(message);
1153
- if (message.text) textParts.push(message.text);
1420
+ if (message.text && !message.isCompactSummary) textParts.push(message.text);
1154
1421
  return false;
1155
1422
  },
1156
1423
  (entry) => {
@@ -1173,8 +1440,8 @@ function assemble(filePath, account, messages, fullText, turnDurations, state) {
1173
1440
  filePath,
1174
1441
  projectPath: state.cwd,
1175
1442
  projectName: getShortProjectName2(state.cwd),
1176
- sessionId: state.sessionId || basename3(filePath, ".jsonl"),
1177
- sessionName: state.sessionName || deriveSessionNameFromFirstMessage(messages.find((m) => m.role === "user" && m.text) ?? null),
1443
+ sessionId: state.sessionId || basename4(filePath, ".jsonl"),
1444
+ sessionName: resolveSessionName(state.nameSlots).name ?? "",
1178
1445
  messages,
1179
1446
  fullText,
1180
1447
  timestamp: state.latestTimestamp || (/* @__PURE__ */ new Date()).toISOString(),
@@ -1218,33 +1485,33 @@ async function extendConversation(previous, resume, filePath, account) {
1218
1485
 
1219
1486
  // src/persistent/cursor.ts
1220
1487
  import { createHash } from "crypto";
1221
- import { closeSync, openSync, readSync, statSync } from "fs";
1488
+ import { closeSync as closeSync2, openSync as openSync2, readSync as readSync2, statSync as statSync2 } from "fs";
1222
1489
  var FP_BYTES = 4096;
1223
1490
  function fingerprint(filePath, size) {
1224
1491
  const hash = createHash("sha1");
1225
1492
  hash.update(String(size));
1226
- const fd = openSync(filePath, "r");
1493
+ const fd = openSync2(filePath, "r");
1227
1494
  try {
1228
1495
  const head = Buffer.alloc(Math.min(FP_BYTES, size));
1229
1496
  if (head.length > 0) {
1230
- readSync(fd, head, 0, head.length, 0);
1497
+ readSync2(fd, head, 0, head.length, 0);
1231
1498
  hash.update(head);
1232
1499
  }
1233
1500
  if (size > FP_BYTES) {
1234
1501
  const tailLen = Math.min(FP_BYTES, size);
1235
1502
  const tail = Buffer.alloc(tailLen);
1236
- readSync(fd, tail, 0, tailLen, size - tailLen);
1503
+ readSync2(fd, tail, 0, tailLen, size - tailLen);
1237
1504
  hash.update(tail);
1238
1505
  }
1239
1506
  } finally {
1240
- closeSync(fd);
1507
+ closeSync2(fd);
1241
1508
  }
1242
1509
  return hash.digest("hex");
1243
1510
  }
1244
1511
  function classify(filePath, existing) {
1245
1512
  let stat6;
1246
1513
  try {
1247
- const s = statSync(filePath);
1514
+ const s = statSync2(filePath);
1248
1515
  stat6 = { size: s.size, mtimeMs: s.mtimeMs };
1249
1516
  } catch {
1250
1517
  return { change: "vanished" };
@@ -1272,13 +1539,13 @@ function classify(filePath, existing) {
1272
1539
  }
1273
1540
 
1274
1541
  // src/persistent/index-engine.ts
1275
- import { statSync as statSync4 } from "fs";
1542
+ import { statSync as statSync5 } from "fs";
1276
1543
 
1277
1544
  // src/providers/codex-cli.ts
1278
1545
  import fg2 from "fast-glob";
1279
1546
  import { createReadStream as createReadStream4 } from "fs";
1280
1547
  import { stat as stat2 } from "fs/promises";
1281
- import { basename as basename4 } from "path";
1548
+ import { basename as basename5 } from "path";
1282
1549
  import { createInterface as createInterface2 } from "readline";
1283
1550
  var CodexCliProvider = class {
1284
1551
  name = CODEX_CLI_PROVIDER;
@@ -1336,6 +1603,7 @@ var CodexCliProvider = class {
1336
1603
  isSubagent: false,
1337
1604
  parentSessionUuid: null,
1338
1605
  firstUser: null,
1606
+ nameSlots: {},
1339
1607
  lastUser: null,
1340
1608
  lastAssistant: null,
1341
1609
  toolNames: [],
@@ -1498,6 +1766,7 @@ function reduceCodexEntry(acc, entry, tier) {
1498
1766
  const snapshot = { text: text.slice(0, 200), timestamp: ts };
1499
1767
  if (sender === "user") {
1500
1768
  if (!acc.firstUser) acc.firstUser = snapshot;
1769
+ acc.nameSlots = applyNameCandidate(acc.nameSlots, "firstUserMessage", text, CODEX_CLI_PROVIDER);
1501
1770
  acc.lastUser = snapshot;
1502
1771
  } else {
1503
1772
  acc.lastAssistant = snapshot;
@@ -1515,7 +1784,7 @@ function reduceCodexEntry(acc, entry, tier) {
1515
1784
  }
1516
1785
  function finalizeCodexMeta(acc, filePath, account, tier) {
1517
1786
  if (acc.messageCount === 0) return null;
1518
- const sessionId = acc.sessionId || basename4(filePath, ".jsonl");
1787
+ const sessionId = acc.sessionId || basename5(filePath, ".jsonl");
1519
1788
  const projectPath = acc.cwd;
1520
1789
  const kind = acc.lastAssistant === null && acc.toolNames.length > 0 ? "task" : "conversation";
1521
1790
  return {
@@ -1525,10 +1794,7 @@ function finalizeCodexMeta(acc, filePath, account, tier) {
1525
1794
  kind,
1526
1795
  externalSessionId: acc.sessionId || void 0,
1527
1796
  sessionId,
1528
- // Mirrors the Claude reducer: a rollout carries no slug, so the opening turn
1529
- // is the only human-readable name source. Only useful because the injected
1530
- // AGENTS.md turn is skipped above — otherwise every title would be that blob.
1531
- sessionName: deriveSessionNameFromFirstMessage(acc.firstUser),
1797
+ sessionName: resolveSessionName({ ...acc.nameSlots, ...codexNameSlotsFor(filePath, acc.sessionId) }).name ?? "",
1532
1798
  projectPath,
1533
1799
  projectName: getShortProjectName3(projectPath),
1534
1800
  account,
@@ -1551,10 +1817,6 @@ function finalizeCodexMeta(acc, filePath, account, tier) {
1551
1817
  lastPrompt: acc.lastUser?.text || void 0
1552
1818
  };
1553
1819
  }
1554
- function isCodexInjectedContext(text) {
1555
- if (text.startsWith("# AGENTS.md") || text.includes("<INSTRUCTIONS>")) return true;
1556
- return text.startsWith("<permissions instructions>") || text.includes("Filesystem sandboxing defines");
1557
- }
1558
1820
  function getShortProjectName3(fullPath) {
1559
1821
  return fullPath.split("/").filter(Boolean).slice(-3).join("/");
1560
1822
  }
@@ -1577,6 +1839,7 @@ async function parseCodexConversation(filePath, account) {
1577
1839
  let cwd = "";
1578
1840
  let latestTimestamp = "";
1579
1841
  let lastUserText = "";
1842
+ let nameSlots = {};
1580
1843
  const rl = createInterface2({ input: createReadStream4(filePath), crlfDelay: Infinity });
1581
1844
  try {
1582
1845
  for await (const line of rl) {
@@ -1601,7 +1864,15 @@ async function parseCodexConversation(filePath, account) {
1601
1864
  messages.push(message);
1602
1865
  if (!message.text) continue;
1603
1866
  textParts.push(message.text);
1604
- if (message.role === "user") lastUserText = message.text;
1867
+ if (message.role === "user") {
1868
+ lastUserText = message.text;
1869
+ nameSlots = applyNameCandidate(
1870
+ nameSlots,
1871
+ "firstUserMessage",
1872
+ message.text,
1873
+ CODEX_CLI_PROVIDER
1874
+ );
1875
+ }
1605
1876
  }
1606
1877
  } catch (err) {
1607
1878
  log.warn({ filePath, err }, "parseCodexConversation: read failed");
@@ -1613,8 +1884,8 @@ async function parseCodexConversation(filePath, account) {
1613
1884
  filePath,
1614
1885
  projectPath: cwd,
1615
1886
  projectName: getShortProjectName3(cwd),
1616
- sessionId: sessionId || basename4(filePath, ".jsonl"),
1617
- sessionName: "",
1887
+ sessionId: sessionId || basename5(filePath, ".jsonl"),
1888
+ sessionName: resolveSessionName({ ...nameSlots, ...codexNameSlotsFor(filePath, sessionId) }).name ?? "",
1618
1889
  messages,
1619
1890
  fullText: textParts.join(" "),
1620
1891
  timestamp: latestTimestamp || (/* @__PURE__ */ new Date()).toISOString(),
@@ -1626,9 +1897,9 @@ async function parseCodexConversation(filePath, account) {
1626
1897
 
1627
1898
  // src/providers/copilot.ts
1628
1899
  import fg3 from "fast-glob";
1629
- import { createReadStream as createReadStream5, statSync as statSync2 } from "fs";
1900
+ import { createReadStream as createReadStream5, statSync as statSync3 } from "fs";
1630
1901
  import { stat as stat3 } from "fs/promises";
1631
- import { basename as basename5, dirname as dirname3 } from "path";
1902
+ import { basename as basename6, dirname as dirname4 } from "path";
1632
1903
  import { createInterface as createInterface3 } from "readline";
1633
1904
  var SESSION_START = "session.start";
1634
1905
  var USER_MESSAGE = "user.message";
@@ -1682,6 +1953,7 @@ var CopilotProvider = class {
1682
1953
  messageCount: 0,
1683
1954
  lastMessageSender: "user",
1684
1955
  firstUser: null,
1956
+ nameSlots: {},
1685
1957
  lastUser: null,
1686
1958
  lastAssistant: null,
1687
1959
  toolNames: [],
@@ -1789,6 +2061,7 @@ function reduceCopilotEntry(acc, entry, tier) {
1789
2061
  const snapshot = { text: text.slice(0, 200), timestamp };
1790
2062
  if (role === "user") {
1791
2063
  if (!acc.firstUser) acc.firstUser = snapshot;
2064
+ acc.nameSlots = applyNameCandidate(acc.nameSlots, "firstUserMessage", text, COPILOT_PROVIDER);
1792
2065
  acc.lastUser = snapshot;
1793
2066
  } else {
1794
2067
  acc.lastAssistant = snapshot;
@@ -1815,7 +2088,7 @@ function finalizeCopilotMeta(acc, filePath, account, tier) {
1815
2088
  kind,
1816
2089
  externalSessionId: sessionId,
1817
2090
  sessionId,
1818
- sessionName: deriveSessionNameFromFirstMessage(acc.firstUser),
2091
+ sessionName: resolveSessionName({ ...acc.nameSlots, ...copilotNameSlotsFor(filePath) }).name ?? "",
1819
2092
  projectPath: acc.cwd,
1820
2093
  projectName: getShortProjectName4(acc.cwd),
1821
2094
  account,
@@ -1837,14 +2110,14 @@ function finalizeCopilotMeta(acc, filePath, account, tier) {
1837
2110
  };
1838
2111
  }
1839
2112
  function sessionIdFromPath(filePath) {
1840
- return basename5(dirname3(filePath));
2113
+ return basename6(dirname4(filePath));
1841
2114
  }
1842
2115
  function getShortProjectName4(fullPath) {
1843
2116
  return fullPath.split(/[/\\]/).filter(Boolean).slice(-3).join("/");
1844
2117
  }
1845
2118
  function fileMtimeIso(filePath) {
1846
2119
  try {
1847
- return new Date(statSync2(filePath).mtimeMs).toISOString();
2120
+ return new Date(statSync3(filePath).mtimeMs).toISOString();
1848
2121
  } catch {
1849
2122
  return (/* @__PURE__ */ new Date()).toISOString();
1850
2123
  }
@@ -1857,6 +2130,7 @@ async function parseCopilotConversation(filePath, account) {
1857
2130
  let cwd = "";
1858
2131
  let latestTimestamp = "";
1859
2132
  let lastUserText = "";
2133
+ let nameSlots = {};
1860
2134
  const rl = createInterface3({ input: createReadStream5(filePath), crlfDelay: Infinity });
1861
2135
  try {
1862
2136
  for await (const line of rl) {
@@ -1881,7 +2155,15 @@ async function parseCopilotConversation(filePath, account) {
1881
2155
  messages.push(message);
1882
2156
  if (!message.text) continue;
1883
2157
  textParts.push(message.text);
1884
- if (message.role === "user") lastUserText = message.text;
2158
+ if (message.role === "user") {
2159
+ lastUserText = message.text;
2160
+ nameSlots = applyNameCandidate(
2161
+ nameSlots,
2162
+ "firstUserMessage",
2163
+ message.text,
2164
+ COPILOT_PROVIDER
2165
+ );
2166
+ }
1885
2167
  }
1886
2168
  } catch (err) {
1887
2169
  log.warn({ filePath, err }, "parseCopilotConversation: read failed");
@@ -1894,7 +2176,7 @@ async function parseCopilotConversation(filePath, account) {
1894
2176
  projectPath: cwd,
1895
2177
  projectName: getShortProjectName4(cwd),
1896
2178
  sessionId: sessionId || sessionIdFromPath(filePath),
1897
- sessionName: "",
2179
+ sessionName: resolveSessionName({ ...nameSlots, ...copilotNameSlotsFor(filePath) }).name ?? "",
1898
2180
  messages,
1899
2181
  fullText: textParts.join(" "),
1900
2182
  timestamp: latestTimestamp || fileMtimeIso(filePath),
@@ -1907,9 +2189,9 @@ async function parseCopilotConversation(filePath, account) {
1907
2189
  // src/providers/cursor.ts
1908
2190
  import { createHash as createHash2 } from "crypto";
1909
2191
  import fg4 from "fast-glob";
1910
- import { createReadStream as createReadStream6, existsSync, statSync as statSync3 } from "fs";
2192
+ import { createReadStream as createReadStream6, existsSync as existsSync2, statSync as statSync4 } from "fs";
1911
2193
  import { stat as stat4 } from "fs/promises";
1912
- import { basename as basename6, dirname as dirname4, join as join4, sep as sep2 } from "path";
2194
+ import { basename as basename7, dirname as dirname5, join as join5, sep as sep2 } from "path";
1913
2195
  import { createInterface as createInterface4 } from "readline";
1914
2196
  var TIMESTAMP_RE = /<timestamp>\s*([\s\S]*?)\s*<\/timestamp>/;
1915
2197
  var USER_QUERY_RE = /<user_query>\s*([\s\S]*?)\s*<\/user_query>/;
@@ -1968,6 +2250,7 @@ var CursorProvider = class {
1968
2250
  parentSessionUuid: null,
1969
2251
  subagentId: null,
1970
2252
  firstUser: null,
2253
+ nameSlots: {},
1971
2254
  lastUser: null,
1972
2255
  lastAssistant: null,
1973
2256
  isImportedFromClaude: false,
@@ -2034,6 +2317,15 @@ function noteImportProvenance(acc, entry, text) {
2034
2317
  acc.isImportedFromCodex = true;
2035
2318
  }
2036
2319
  }
2320
+ function cursorUserQuery(content) {
2321
+ const texts = typeof content === "string" ? [content] : Array.isArray(content) ? content : [];
2322
+ for (const item of texts) {
2323
+ const text = typeof item === "string" ? item : item?.text;
2324
+ const query = typeof text === "string" ? text.match(USER_QUERY_RE) : null;
2325
+ if (query) return query[1];
2326
+ }
2327
+ return void 0;
2328
+ }
2037
2329
  function unwrapCursorUserText(text) {
2038
2330
  const query = text.match(USER_QUERY_RE);
2039
2331
  const body = query ? query[1] : text.replace(TIMESTAMP_RE, "");
@@ -2171,6 +2463,14 @@ function reduceCursorEntry(acc, entry, tier) {
2171
2463
  collectCursorToolNames(content, acc.toolNames);
2172
2464
  const text = textHint;
2173
2465
  if (!text) return;
2466
+ if (entry.role === "user") {
2467
+ acc.nameSlots = applyNameCandidate(
2468
+ acc.nameSlots,
2469
+ "firstUserMessage",
2470
+ cursorUserQuery(content),
2471
+ CURSOR_PROVIDER
2472
+ );
2473
+ }
2174
2474
  foldCursorMessage(acc, entry.role, text, firstTimestampHint(content), tier);
2175
2475
  return;
2176
2476
  }
@@ -2189,7 +2489,7 @@ function reduceCursorEntry(acc, entry, tier) {
2189
2489
  }
2190
2490
  function finalizeCursorMeta(acc, filePath, account, tier) {
2191
2491
  if (acc.messageCount === 0) return null;
2192
- const sessionId = basename6(filePath, ".jsonl");
2492
+ const sessionId = basename7(filePath, ".jsonl");
2193
2493
  const isSubagent = SUBAGENT_PATH_RE2.test(filePath);
2194
2494
  const parent = isSubagent ? parentFromSubagentPath(filePath) : null;
2195
2495
  const { projectPath, projectName } = projectFromCursorPath(filePath);
@@ -2202,7 +2502,7 @@ function finalizeCursorMeta(acc, filePath, account, tier) {
2202
2502
  kind,
2203
2503
  externalSessionId: sessionId,
2204
2504
  sessionId,
2205
- sessionName: deriveSessionNameFromFirstMessage(acc.firstUser),
2505
+ sessionName: resolveSessionName({ ...acc.nameSlots, ...cursorNameSlotsFor(filePath, sessionId) }).name ?? "",
2206
2506
  projectPath,
2207
2507
  projectName,
2208
2508
  account,
@@ -2228,20 +2528,20 @@ function finalizeCursorMeta(acc, filePath, account, tier) {
2228
2528
  };
2229
2529
  }
2230
2530
  function parentFromSubagentPath(filePath) {
2231
- const subagentsDir = dirname4(filePath);
2232
- const parentDir = dirname4(subagentsDir);
2233
- const parentSessionUuid = basename6(parentDir);
2531
+ const subagentsDir = dirname5(filePath);
2532
+ const parentDir = dirname5(subagentsDir);
2533
+ const parentSessionUuid = basename7(parentDir);
2234
2534
  if (!parentSessionUuid) return null;
2235
2535
  return {
2236
2536
  parentSessionUuid,
2237
- parentFilePath: canonicalPath(join4(parentDir, `${parentSessionUuid}.jsonl`))
2537
+ parentFilePath: canonicalPath(join5(parentDir, `${parentSessionUuid}.jsonl`))
2238
2538
  };
2239
2539
  }
2240
2540
  function projectFromCursorPath(filePath) {
2241
2541
  const parts = filePath.split(/[/\\]/);
2242
2542
  const idx = parts.lastIndexOf("agent-transcripts");
2243
- const cursorProjectDir = idx > 0 ? parts.slice(0, idx).join(sep2) : dirname4(dirname4(filePath));
2244
- const slug = basename6(cursorProjectDir);
2543
+ const cursorProjectDir = idx > 0 ? parts.slice(0, idx).join(sep2) : dirname5(dirname5(filePath));
2544
+ const slug = basename7(cursorProjectDir);
2245
2545
  const decoded = decodeCursorProjectSlug(slug);
2246
2546
  const projectPath = decoded ?? cursorProjectDir;
2247
2547
  return { projectPath, projectName: getShortProjectName5(projectPath) || slug };
@@ -2255,7 +2555,7 @@ function decodeCursorProjectSlug(slug) {
2255
2555
  let i = 0;
2256
2556
  if (rooted && segments[0] === "Users") {
2257
2557
  const users = `${sep2}Users`;
2258
- if (!existsSync(users)) return null;
2558
+ if (!existsSync2(users)) return null;
2259
2559
  acc = users;
2260
2560
  i = 1;
2261
2561
  }
@@ -2263,9 +2563,9 @@ function decodeCursorProjectSlug(slug) {
2263
2563
  let found = null;
2264
2564
  let next = i + 1;
2265
2565
  for (let j = segments.length; j > i; j--) {
2266
- const candidate = join4(acc, segments.slice(i, j).join("-"));
2566
+ const candidate = join5(acc, segments.slice(i, j).join("-"));
2267
2567
  try {
2268
- if (existsSync(candidate) && statSync3(candidate).isDirectory()) {
2568
+ if (existsSync2(candidate) && statSync4(candidate).isDirectory()) {
2269
2569
  found = candidate;
2270
2570
  next = j;
2271
2571
  break;
@@ -2284,17 +2584,26 @@ function getShortProjectName5(fullPath) {
2284
2584
  }
2285
2585
  function fileMtimeIso2(filePath) {
2286
2586
  try {
2287
- return new Date(statSync3(filePath).mtimeMs).toISOString();
2587
+ return new Date(statSync4(filePath).mtimeMs).toISOString();
2288
2588
  } catch {
2289
2589
  return (/* @__PURE__ */ new Date()).toISOString();
2290
2590
  }
2291
2591
  }
2592
+ function cursorLineContent(line) {
2593
+ try {
2594
+ const entry = JSON.parse(line);
2595
+ return entry.message?.content ?? entry.content;
2596
+ } catch {
2597
+ return void 0;
2598
+ }
2599
+ }
2292
2600
  async function parseCursorConversation(filePath, account) {
2293
2601
  const log = getLogger();
2294
2602
  const messages = [];
2295
2603
  const textParts = [];
2296
2604
  let latestTimestamp = "";
2297
2605
  let lastUserText = "";
2606
+ let nameSlots = {};
2298
2607
  const rl = createInterface4({ input: createReadStream6(filePath), crlfDelay: Infinity });
2299
2608
  try {
2300
2609
  for await (const line of rl) {
@@ -2306,7 +2615,17 @@ async function parseCursorConversation(filePath, account) {
2306
2615
  messages.push(message);
2307
2616
  if (!message.text) continue;
2308
2617
  textParts.push(message.text);
2309
- if (message.role === "user") lastUserText = message.text;
2618
+ if (message.role === "user") {
2619
+ lastUserText = message.text;
2620
+ if (nameSlots.firstUserMessage === void 0) {
2621
+ nameSlots = applyNameCandidate(
2622
+ nameSlots,
2623
+ "firstUserMessage",
2624
+ cursorUserQuery(cursorLineContent(line)),
2625
+ CURSOR_PROVIDER
2626
+ );
2627
+ }
2628
+ }
2310
2629
  }
2311
2630
  } catch (err) {
2312
2631
  log.warn({ filePath, err }, "parseCursorConversation: read failed");
@@ -2314,14 +2633,14 @@ async function parseCursorConversation(filePath, account) {
2314
2633
  }
2315
2634
  if (messages.length === 0) return null;
2316
2635
  const { projectPath, projectName } = projectFromCursorPath(filePath);
2317
- const sessionId = basename6(filePath, ".jsonl");
2636
+ const sessionId = basename7(filePath, ".jsonl");
2318
2637
  return {
2319
2638
  id: filePath,
2320
2639
  filePath,
2321
2640
  projectPath,
2322
2641
  projectName,
2323
2642
  sessionId,
2324
- sessionName: "",
2643
+ sessionName: resolveSessionName({ ...nameSlots, ...cursorNameSlotsFor(filePath, sessionId) }).name ?? "",
2325
2644
  messages,
2326
2645
  fullText: textParts.join(" "),
2327
2646
  timestamp: latestTimestamp || fileMtimeIso2(filePath),
@@ -2334,7 +2653,7 @@ async function parseCursorConversation(filePath, account) {
2334
2653
  // src/providers/parse.ts
2335
2654
  import { createReadStream as createReadStream7 } from "fs";
2336
2655
  import { createInterface as createInterface5 } from "readline";
2337
- async function parseMetaWithProvider(provider, filePath, account, tier, onEntry) {
2656
+ async function parseMetaWithProvider(provider, filePath, account, tier, onEntry, onAccumulator) {
2338
2657
  const log = getLogger();
2339
2658
  const acc = provider.createEmptyAccumulator();
2340
2659
  const rl = createInterface5({
@@ -2361,6 +2680,7 @@ async function parseMetaWithProvider(provider, filePath, account, tier, onEntry)
2361
2680
  log.warn({ filePath, provider: provider.name, err }, "provider parse: read failed");
2362
2681
  return null;
2363
2682
  }
2683
+ onAccumulator?.(acc);
2364
2684
  return provider.finalize(acc, filePath, account, tier);
2365
2685
  }
2366
2686
 
@@ -2561,12 +2881,12 @@ function resolveTier(tierName, customTiers) {
2561
2881
  }
2562
2882
 
2563
2883
  // src/persistent/db.ts
2564
- import Database from "better-sqlite3";
2884
+ import Database2 from "better-sqlite3";
2565
2885
  import { mkdirSync } from "fs";
2566
- import { dirname as dirname5 } from "path";
2886
+ import { dirname as dirname6 } from "path";
2567
2887
 
2568
2888
  // src/persistent/schema.ts
2569
- var SCHEMA_VERSION = 10;
2889
+ var SCHEMA_VERSION = 11;
2570
2890
  var SCHEMA_SQL = `
2571
2891
  CREATE TABLE IF NOT EXISTS conversation_files (
2572
2892
  id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -2612,6 +2932,9 @@ CREATE TABLE IF NOT EXISTS conversations (
2612
2932
  external_session_id TEXT,
2613
2933
  session_id TEXT NOT NULL,
2614
2934
  session_name TEXT,
2935
+ -- JSON NameSlots the name was resolved from; lets a side-store change
2936
+ -- re-resolve the name without reparsing the transcript.
2937
+ name_slots TEXT,
2615
2938
 
2616
2939
  project_path TEXT,
2617
2940
  project_name TEXT,
@@ -2874,6 +3197,27 @@ function runMigrations(db) {
2874
3197
  db.exec(`UPDATE conversation_files SET ${assignments.join(", ")}`);
2875
3198
  }
2876
3199
  }
3200
+ if (current >= 1 && current < 11 && tableExists(db, "conversations")) {
3201
+ if (tableExists(db, "message_checkpoints")) db.exec("DELETE FROM message_checkpoints");
3202
+ if (!hasColumn(db, "conversations", "name_slots")) {
3203
+ db.exec("ALTER TABLE conversations ADD COLUMN name_slots TEXT");
3204
+ }
3205
+ if (tableExists(db, "conversation_files")) {
3206
+ const assignments = [];
3207
+ if (hasColumn(db, "conversation_files", "last_indexed_offset")) {
3208
+ assignments.push("last_indexed_offset = 0");
3209
+ }
3210
+ if (hasColumn(db, "conversation_files", "last_indexed_line")) {
3211
+ assignments.push("last_indexed_line = 0");
3212
+ }
3213
+ if (hasColumn(db, "conversation_files", "reducer_state")) {
3214
+ assignments.push("reducer_state = NULL");
3215
+ }
3216
+ if (assignments.length > 0) {
3217
+ db.exec(`UPDATE conversation_files SET ${assignments.join(", ")}`);
3218
+ }
3219
+ }
3220
+ }
2877
3221
  db.exec(SCHEMA_SQL);
2878
3222
  db.pragma(`user_version = ${SCHEMA_VERSION}`);
2879
3223
  }
@@ -2888,11 +3232,11 @@ function hasColumn(db, table, column) {
2888
3232
  // src/persistent/db.ts
2889
3233
  function openDatabase(dbPath) {
2890
3234
  if (dbPath !== ":memory:") {
2891
- mkdirSync(dirname5(dbPath), { recursive: true });
3235
+ mkdirSync(dirname6(dbPath), { recursive: true });
2892
3236
  }
2893
3237
  let db;
2894
3238
  try {
2895
- db = new Database(dbPath);
3239
+ db = new Database2(dbPath);
2896
3240
  } catch (err) {
2897
3241
  if (isNativeBindingFailure(err)) throw nativeBindingError(err, dbPath);
2898
3242
  throw err;
@@ -2933,7 +3277,7 @@ function nativeBindingError(err, dbPath) {
2933
3277
 
2934
3278
  // src/persistent/dir-watermark.ts
2935
3279
  import { readdir, stat as stat5 } from "fs/promises";
2936
- import { dirname as dirname6, join as join5 } from "path";
3280
+ import { dirname as dirname7, join as join6 } from "path";
2937
3281
  var FULL_RECONCILE_EVERY_N_SCANS = 20;
2938
3282
  async function discoverJsonlFilesGated(dirs, files, scannedDirs, options = {}) {
2939
3283
  const log = getLogger();
@@ -3004,10 +3348,10 @@ async function resolveProjectDirs(projectsDir, scannedDirs) {
3004
3348
  return { entries, commitRoot: { mtimeMs: rootStat.mtimeMs } };
3005
3349
  }
3006
3350
  function dirnameOf(filePath) {
3007
- return dirname6(filePath);
3351
+ return dirname7(filePath);
3008
3352
  }
3009
3353
  function joinPath(dir, name) {
3010
- return join5(dir, name);
3354
+ return join6(dir, name);
3011
3355
  }
3012
3356
 
3013
3357
  // src/persistent/repositories/checkpoints.repo.ts
@@ -3087,7 +3431,7 @@ function toCheckpoint(row) {
3087
3431
  }
3088
3432
 
3089
3433
  // src/persistent/repositories/conversation-files.repo.ts
3090
- import { basename as basename7, dirname as dirname7 } from "path";
3434
+ import { basename as basename8, dirname as dirname8 } from "path";
3091
3435
  var ConversationFilesRepo = class {
3092
3436
  constructor(db) {
3093
3437
  this.db = db;
@@ -3110,7 +3454,7 @@ var ConversationFilesRepo = class {
3110
3454
  const info = this.db.prepare(
3111
3455
  `INSERT INTO conversation_files (absolute_path, parent_dir, file_name, account)
3112
3456
  VALUES (?, ?, ?, ?)`
3113
- ).run(canonical, dirname7(canonical), basename7(canonical), account);
3457
+ ).run(canonical, dirname8(canonical), basename8(canonical), account);
3114
3458
  return Number(info.lastInsertRowid);
3115
3459
  }
3116
3460
  // Advance the cursor + persisted reducer state after a successful index pass.
@@ -3218,7 +3562,7 @@ var ConversationsRepo = class {
3218
3562
  db;
3219
3563
  // Upsert by file_id (1 file = 1 conversation). Keyed on the unique file_id so
3220
3564
  // a re-index overwrites the prior summary in place.
3221
- upsert(fileId, meta, pageMessageCount = meta.messageCount) {
3565
+ upsert(fileId, meta, pageMessageCount = meta.messageCount, nameSlots) {
3222
3566
  this.db.prepare(
3223
3567
  `INSERT INTO conversations (
3224
3568
  file_id, source_path, provider, kind, external_session_id,
@@ -3229,7 +3573,7 @@ var ConversationsRepo = class {
3229
3573
  model, is_subagent, parent_session_id, subagent_id, parent_session_uuid,
3230
3574
  is_teammate, team_name, tool_names_json,
3231
3575
  last_prompt, is_imported_from_claude, is_imported_from_codex,
3232
- is_imported_from_cursor, status, updated_at
3576
+ is_imported_from_cursor, name_slots, status, updated_at
3233
3577
  ) VALUES (
3234
3578
  @file_id, @source_path, @provider, @kind, @external_session_id,
3235
3579
  @session_id, @session_name, @project_path, @project_name,
@@ -3241,7 +3585,7 @@ var ConversationsRepo = class {
3241
3585
  @model, @is_subagent, @parent_session_id, @subagent_id, @parent_session_uuid,
3242
3586
  @is_teammate, @team_name, @tool_names_json,
3243
3587
  @last_prompt, @is_imported_from_claude, @is_imported_from_codex,
3244
- @is_imported_from_cursor, 'active', CURRENT_TIMESTAMP
3588
+ @is_imported_from_cursor, @name_slots, 'active', CURRENT_TIMESTAMP
3245
3589
  )
3246
3590
  ON CONFLICT(file_id) DO UPDATE SET
3247
3591
  source_path = excluded.source_path,
@@ -3276,6 +3620,7 @@ var ConversationsRepo = class {
3276
3620
  is_imported_from_claude = excluded.is_imported_from_claude,
3277
3621
  is_imported_from_codex = excluded.is_imported_from_codex,
3278
3622
  is_imported_from_cursor = excluded.is_imported_from_cursor,
3623
+ name_slots = excluded.name_slots,
3279
3624
  status = 'active',
3280
3625
  index_seq = (SELECT COALESCE(MAX(index_seq), 0) + 1 FROM conversations),
3281
3626
  updated_at = CURRENT_TIMESTAMP`
@@ -3315,7 +3660,8 @@ var ConversationsRepo = class {
3315
3660
  last_prompt: meta.lastPrompt ?? null,
3316
3661
  is_imported_from_claude: meta.isImportedFromClaude ? 1 : 0,
3317
3662
  is_imported_from_codex: meta.isImportedFromCodex ? 1 : 0,
3318
- is_imported_from_cursor: meta.isImportedFromCursor ? 1 : 0
3663
+ is_imported_from_cursor: meta.isImportedFromCursor ? 1 : 0,
3664
+ name_slots: nameSlots ? JSON.stringify(nameSlots) : null
3319
3665
  });
3320
3666
  }
3321
3667
  getBySourcePath(sourcePath) {
@@ -3564,7 +3910,7 @@ var ScannedDirsRepo = class {
3564
3910
  };
3565
3911
 
3566
3912
  // src/persistent/sidecar.ts
3567
- import { readFileSync as readFileSync2, writeFileSync } from "fs";
3913
+ import { readFileSync as readFileSync3, writeFileSync } from "fs";
3568
3914
  var SIDECAR_VERSION = 1;
3569
3915
  function sidecarPath(jsonlPath) {
3570
3916
  return `${jsonlPath}.idx.json`;
@@ -3729,13 +4075,18 @@ var PersistentEngine = class {
3729
4075
  const tier = resolveTier(tierName, customTiers);
3730
4076
  const filePath = canonicalPath(rawFilePath);
3731
4077
  const existing = this.files.getByPath(filePath);
3732
- const { change, stat: stat6 } = classify(filePath, existing);
4078
+ const classified = classify(filePath, existing);
4079
+ const stat6 = classified.stat;
4080
+ let change = classified.change;
3733
4081
  if (change === "vanished" || !stat6) {
3734
4082
  this.markDeleted(filePath);
3735
4083
  return { meta: null, change: "vanished" };
3736
4084
  }
3737
4085
  if (change === "unchanged" && !force) {
3738
- return { meta: this.conversations.getBySourcePath(filePath), change };
4086
+ const meta2 = this.conversations.getBySourcePath(filePath);
4087
+ const sideStoresMoved = provider && provider.name !== CLAUDE_CODE_PROVIDER && meta2 && JSON.stringify(nameStoreSlotsFor(provider.name, filePath, meta2.sessionId)) !== existing?.reducer_state;
4088
+ if (!sideStoresMoved) return { meta: meta2, change };
4089
+ change = "reindex";
3739
4090
  }
3740
4091
  if (provider && provider.name !== CLAUDE_CODE_PROVIDER) {
3741
4092
  const meta2 = await this.indexFileWithProvider(
@@ -3774,7 +4125,7 @@ var PersistentEngine = class {
3774
4125
  const base = resume ? this.fts.readDocument(fileId) ?? emptySearchDocument() : emptySearchDocument();
3775
4126
  const searchDoc = appendSearchDelta(base, searchDelta);
3776
4127
  const upsert = this.db.transaction(() => {
3777
- this.conversations.upsert(fileId, meta, state.pageMessageCount);
4128
+ this.conversations.upsert(fileId, meta, state.pageMessageCount, state.nameSlots);
3778
4129
  if (!this.fts.isCurrent(fileId, meta, searchDoc)) {
3779
4130
  this.fts.upsert(fileId, meta, searchDoc);
3780
4131
  }
@@ -3817,13 +4168,23 @@ var PersistentEngine = class {
3817
4168
  // provider's reducer/finalize, then the same upsert + FTS write + cursor bump
3818
4169
  // the Threadbase path uses. The cursor records size/mtime (and offset = size)
3819
4170
  // so the next pass classifies an unchanged file as "unchanged" and skips it;
3820
- // any change reparses from 0 again. No reducer_state is persisted.
4171
+ // any change reparses from 0 again. reducer_state holds the side-store name slots, not a fold.
3821
4172
  async indexFileWithProvider(provider, filePath, account, tier, stat6, resolveGitBranch) {
3822
4173
  const log = getLogger();
3823
4174
  let searchDoc = emptySearchDocument();
3824
- const meta = await parseMetaWithProvider(provider, filePath, account, tier, (entry) => {
3825
- searchDoc = appendSearchDelta(searchDoc, extractSearchDelta(entry));
3826
- });
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
+ );
3827
4188
  if (!meta) {
3828
4189
  this.markDeleted(filePath);
3829
4190
  return null;
@@ -3831,10 +4192,14 @@ var PersistentEngine = class {
3831
4192
  if (meta.gitBranch === null && meta.projectPath) {
3832
4193
  meta.gitBranch = resolveGitBranch(meta.projectPath);
3833
4194
  }
4195
+ const sideSlots = nameStoreSlotsFor(provider.name, filePath, meta.sessionId);
3834
4196
  const fp = stat6.size > 0 ? fingerprint(filePath, stat6.size) : null;
3835
4197
  const fileId = this.files.ensure(filePath, account);
3836
4198
  const upsert = this.db.transaction(() => {
3837
- this.conversations.upsert(fileId, meta, meta.messageCount);
4199
+ this.conversations.upsert(fileId, meta, meta.messageCount, {
4200
+ ...transcriptSlots,
4201
+ ...sideSlots
4202
+ });
3838
4203
  if (!this.fts.isCurrent(fileId, meta, searchDoc)) {
3839
4204
  this.fts.upsert(fileId, meta, searchDoc);
3840
4205
  }
@@ -3846,7 +4211,9 @@ var PersistentEngine = class {
3846
4211
  // can classify it "unchanged"); no resumable reducer state is kept.
3847
4212
  offset: stat6.size,
3848
4213
  line: 0,
3849
- reducerState: null,
4214
+ // Not resumable state: the side-store name slots this parse used, so the
4215
+ // next pass can tell when they moved (see indexFile).
4216
+ reducerState: JSON.stringify(sideSlots),
3850
4217
  fingerprint: fp,
3851
4218
  status: "active"
3852
4219
  });
@@ -3926,7 +4293,7 @@ var PersistentEngine = class {
3926
4293
  let floor = this.checkpoints.floor(filePath, fromIndex);
3927
4294
  if (floor) {
3928
4295
  try {
3929
- if (floor.byteOffset > statSync4(filePath).size) floor = null;
4296
+ if (floor.byteOffset > statSync5(filePath).size) floor = null;
3930
4297
  } catch {
3931
4298
  }
3932
4299
  }
@@ -4101,7 +4468,7 @@ function defaultDbPath() {
4101
4468
  "TB_SCANNER_DB must be set under a test runner. Refusing to open the default index at ~/.config/threadbase-scanner/index.db, because test fixtures written there are indistinguishable from real conversations and corrupt every measurement of the index. Point it at a temp directory in your test setup, or pass persistent.dbPath explicitly."
4102
4469
  );
4103
4470
  }
4104
- return join6(homedir2(), ".config", "threadbase-scanner", "index.db");
4471
+ return join7(homedir2(), ".config", "threadbase-scanner", "index.db");
4105
4472
  }
4106
4473
  function capForMemory(doc, max) {
4107
4474
  const combined = combineSearchContent(doc);
@@ -4194,6 +4561,7 @@ var ConversationScanner = class {
4194
4561
  }
4195
4562
  }
4196
4563
  async runScan(options) {
4564
+ if (options.cursorStateDb !== void 0) setCursorStateDb(options.cursorStateDb);
4197
4565
  const profiles = await this.resolveProfiles(options.profiles);
4198
4566
  const activeProfiles = profiles.filter((p) => p.enabled && p.scanHistory !== false);
4199
4567
  this.lastTier = resolveTier(options.tier ?? "standard", options.tiers);
@@ -4280,7 +4648,7 @@ var ConversationScanner = class {
4280
4648
  const cached = statCache.get(filePath);
4281
4649
  if (cached) {
4282
4650
  try {
4283
- const s = statSync5(filePath);
4651
+ const s = statSync6(filePath);
4284
4652
  if (s.mtimeMs === cached.stat.mtimeMs && s.size === cached.stat.size) {
4285
4653
  return {
4286
4654
  meta: cached.meta,
@@ -4828,13 +5196,13 @@ var ConversationScanner = class {
4828
5196
  if (previous?.provider) return void 0;
4829
5197
  let sample = "";
4830
5198
  try {
4831
- const fd = openSync2(filePath, "r");
5199
+ const fd = openSync3(filePath, "r");
4832
5200
  try {
4833
5201
  const buf = Buffer.alloc(8192);
4834
- const n = readSync2(fd, buf, 0, buf.length, 0);
5202
+ const n = readSync3(fd, buf, 0, buf.length, 0);
4835
5203
  sample = buf.subarray(0, n).toString("utf8");
4836
5204
  } finally {
4837
- closeSync2(fd);
5205
+ closeSync3(fd);
4838
5206
  }
4839
5207
  } catch {
4840
5208
  return void 0;