@threadbase-sh/scanner 0.14.7 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -5,7 +5,7 @@ import { Command } from "commander";
5
5
  import pino2 from "pino";
6
6
 
7
7
  // package.json
8
- var version = "0.14.7";
8
+ var version = "0.16.0";
9
9
 
10
10
  // src/logger.ts
11
11
  import pino from "pino";
@@ -626,6 +626,8 @@ function initialReducerState() {
626
626
  model: null,
627
627
  messageCount: 0,
628
628
  lastMessageSender: "user",
629
+ agentId: "",
630
+ isSidechain: false,
629
631
  isTeammate: false,
630
632
  firstUserSeen: false,
631
633
  firstMessage: null,
@@ -645,6 +647,8 @@ function reduceLine(state, entry, tier) {
645
647
  if (entry.sessionId && !state.sessionId) state.sessionId = entry.sessionId;
646
648
  if (entry.slug && !state.sessionName) state.sessionName = entry.slug;
647
649
  if (entry.teamName && !state.teamName) state.teamName = entry.teamName;
650
+ if (entry.agentId && !state.agentId) state.agentId = entry.agentId;
651
+ if (entry.isSidechain === true) state.isSidechain = true;
648
652
  if (entry.timestamp) {
649
653
  const ts = entry.timestamp;
650
654
  if (!state.latestTimestamp || ts > state.latestTimestamp) state.latestTimestamp = ts;
@@ -696,9 +700,11 @@ function reduceLine(state, entry, tier) {
696
700
  }
697
701
  function finalizeMeta(state, filePath, account, tier) {
698
702
  if (state.messageCount === 0) return null;
699
- const isSubagent = filePath.includes("/subagents/");
703
+ const isSidechain = state.isSidechain && !!state.agentId;
704
+ const isSubagentPath = SUBAGENT_PATH_RE.test(filePath);
705
+ const isSubagent = isSidechain || isSubagentPath;
700
706
  let parentSessionId = null;
701
- if (isSubagent) {
707
+ if (isSubagentPath) {
702
708
  const uuidDir = dirname2(dirname2(filePath));
703
709
  parentSessionId = join3(dirname2(uuidDir), `${basename(uuidDir)}.jsonl`);
704
710
  }
@@ -721,6 +727,10 @@ function finalizeMeta(state, filePath, account, tier) {
721
727
  model: state.model,
722
728
  isSubagent,
723
729
  parentSessionId,
730
+ subagentId: isSidechain ? state.agentId : void 0,
731
+ // A sidechain line's `sessionId` IS the parent's id — that is exactly the
732
+ // conflation `subagentId` exists to undo, so it is the parent UUID here.
733
+ parentSessionUuid: isSidechain && state.sessionId ? state.sessionId : void 0,
724
734
  isTeammate: state.isTeammate,
725
735
  teamName: state.teamName || null,
726
736
  toolNames: state.toolNames,
@@ -729,6 +739,7 @@ function finalizeMeta(state, filePath, account, tier) {
729
739
  lastPrompt: state.lastPrompt || void 0
730
740
  };
731
741
  }
742
+ var SUBAGENT_PATH_RE = /[\\/]subagents[\\/]/;
732
743
  function deriveSessionNameFromFirstMessage(firstMessage) {
733
744
  const firstLine = firstMessage?.text.split("\n", 1)[0]?.trim() ?? "";
734
745
  return firstLine.slice(0, 80);
@@ -1279,6 +1290,8 @@ var CodexCliProvider = class {
1279
1290
  latestTimestamp: "",
1280
1291
  messageCount: 0,
1281
1292
  lastMessageSender: "user",
1293
+ isSubagent: false,
1294
+ parentSessionUuid: null,
1282
1295
  firstUser: null,
1283
1296
  lastUser: null,
1284
1297
  lastAssistant: null,
@@ -1309,6 +1322,16 @@ function extractCodexText(content) {
1309
1322
  return "";
1310
1323
  }).filter(Boolean).map(cleanSystemTags).join(" ");
1311
1324
  }
1325
+ function codexEntryToMessage(entry) {
1326
+ const payload = entry.payload;
1327
+ if (!payload || typeof payload !== "object") return null;
1328
+ if (entry.type !== "response_item" || payload.type !== "message") return null;
1329
+ const role = payload.role;
1330
+ if (role !== "user" && role !== "assistant") return null;
1331
+ const text = extractCodexText(payload.content);
1332
+ if (!text) return null;
1333
+ return { role, text, timestamp: asString(entry.timestamp) };
1334
+ }
1312
1335
  function reduceCodexEntry(acc, entry, tier) {
1313
1336
  const ts = asString(entry.timestamp);
1314
1337
  if (ts && (!acc.latestTimestamp || ts > acc.latestTimestamp)) acc.latestTimestamp = ts;
@@ -1320,6 +1343,10 @@ function reduceCodexEntry(acc, entry, tier) {
1320
1343
  if (!acc.cwd) acc.cwd = asString(payload.cwd);
1321
1344
  const git = payload.git;
1322
1345
  if (acc.gitBranch === null && git?.branch) acc.gitBranch = asString(git.branch) || null;
1346
+ if (!acc.isSubagent && isSubagentSource(payload.source)) {
1347
+ acc.isSubagent = true;
1348
+ acc.parentSessionUuid = parentThreadId(payload.source);
1349
+ }
1323
1350
  return;
1324
1351
  }
1325
1352
  if (acc.model === null && payload.model) acc.model = asString(payload.model) || null;
@@ -1379,8 +1406,10 @@ function finalizeCodexMeta(acc, filePath, account, tier) {
1379
1406
  contentSnippet: acc.snippetParts.join(" "),
1380
1407
  gitBranch: acc.gitBranch,
1381
1408
  model: acc.model,
1382
- isSubagent: false,
1409
+ isSubagent: acc.isSubagent,
1410
+ // Codex exposes no path-shaped parent, and no per-child id of its own.
1383
1411
  parentSessionId: null,
1412
+ parentSessionUuid: acc.parentSessionUuid ?? void 0,
1384
1413
  isTeammate: false,
1385
1414
  teamName: null,
1386
1415
  toolNames: acc.toolNames,
@@ -1392,6 +1421,17 @@ function finalizeCodexMeta(acc, filePath, account, tier) {
1392
1421
  function getShortProjectName3(fullPath) {
1393
1422
  return fullPath.split("/").filter(Boolean).slice(-3).join("/");
1394
1423
  }
1424
+ function isSubagentSource(source) {
1425
+ return typeof source === "object" && source !== null && "subagent" in source;
1426
+ }
1427
+ function parentThreadId(source) {
1428
+ if (!isSubagentSource(source)) return null;
1429
+ const subagent = source.subagent;
1430
+ if (typeof subagent !== "object" || subagent === null) return null;
1431
+ const spawn = subagent.thread_spawn;
1432
+ if (typeof spawn !== "object" || spawn === null) return null;
1433
+ return asString(spawn.parent_thread_id) || null;
1434
+ }
1395
1435
  async function parseCodexConversation(filePath, account) {
1396
1436
  const log = getLogger();
1397
1437
  const messages = [];
@@ -1419,14 +1459,11 @@ async function parseCodexConversation(filePath, account) {
1419
1459
  if (!cwd) cwd = asString(payload.cwd);
1420
1460
  continue;
1421
1461
  }
1422
- if (entry.type !== "response_item" || payload.type !== "message") continue;
1423
- const role = payload.role;
1424
- if (role !== "user" && role !== "assistant") continue;
1425
- const text = extractCodexText(payload.content);
1426
- if (!text) continue;
1427
- messages.push({ role, text, timestamp: ts });
1428
- textParts.push(text);
1429
- if (role === "user") lastUserText = text;
1462
+ const message = codexEntryToMessage(entry);
1463
+ if (!message) continue;
1464
+ messages.push(message);
1465
+ textParts.push(message.text);
1466
+ if (message.role === "user") lastUserText = message.text;
1430
1467
  }
1431
1468
  } catch (err) {
1432
1469
  log.warn({ filePath, err }, "parseCodexConversation: read failed");
@@ -1648,7 +1685,7 @@ import { mkdirSync } from "fs";
1648
1685
  import { dirname as dirname3 } from "path";
1649
1686
 
1650
1687
  // src/persistent/schema.ts
1651
- var SCHEMA_VERSION = 5;
1688
+ var SCHEMA_VERSION = 6;
1652
1689
  var SCHEMA_SQL = `
1653
1690
  CREATE TABLE IF NOT EXISTS conversation_files (
1654
1691
  id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -1723,7 +1760,11 @@ CREATE TABLE IF NOT EXISTS conversations (
1723
1760
 
1724
1761
  model TEXT,
1725
1762
  is_subagent INTEGER NOT NULL DEFAULT 0,
1763
+ -- The parent's JSONL PATH (matched against source_path), not an id.
1726
1764
  parent_session_id TEXT,
1765
+ -- The subagent's own id (Claude agentId), and the parent's session UUID.
1766
+ subagent_id TEXT,
1767
+ parent_session_uuid TEXT,
1727
1768
  is_teammate INTEGER NOT NULL DEFAULT 0,
1728
1769
  team_name TEXT,
1729
1770
  tool_names_json TEXT,
@@ -1856,6 +1897,29 @@ function runMigrations(db) {
1856
1897
  }
1857
1898
  }
1858
1899
  }
1900
+ if (current >= 1 && current < 6 && tableExists(db, "conversations")) {
1901
+ for (const [col, ddl] of [
1902
+ ["subagent_id", "ALTER TABLE conversations ADD COLUMN subagent_id TEXT"],
1903
+ ["parent_session_uuid", "ALTER TABLE conversations ADD COLUMN parent_session_uuid TEXT"]
1904
+ ]) {
1905
+ if (!hasColumn(db, "conversations", col)) db.exec(ddl);
1906
+ }
1907
+ if (tableExists(db, "conversation_files")) {
1908
+ const assignments = [];
1909
+ if (hasColumn(db, "conversation_files", "last_indexed_offset")) {
1910
+ assignments.push("last_indexed_offset = 0");
1911
+ }
1912
+ if (hasColumn(db, "conversation_files", "last_indexed_line")) {
1913
+ assignments.push("last_indexed_line = 0");
1914
+ }
1915
+ if (hasColumn(db, "conversation_files", "reducer_state")) {
1916
+ assignments.push("reducer_state = NULL");
1917
+ }
1918
+ if (assignments.length > 0) {
1919
+ db.exec(`UPDATE conversation_files SET ${assignments.join(", ")}`);
1920
+ }
1921
+ }
1922
+ }
1859
1923
  db.exec(SCHEMA_SQL);
1860
1924
  db.pragma(`user_version = ${SCHEMA_VERSION}`);
1861
1925
  }
@@ -2180,6 +2244,8 @@ function rowToMeta(row) {
2180
2244
  model: row.model,
2181
2245
  isSubagent: row.is_subagent === 1,
2182
2246
  parentSessionId: row.parent_session_id,
2247
+ subagentId: row.subagent_id ?? void 0,
2248
+ parentSessionUuid: row.parent_session_uuid ?? void 0,
2183
2249
  isTeammate: row.is_teammate === 1,
2184
2250
  teamName: row.team_name,
2185
2251
  toolNames: row.tool_names_json ? JSON.parse(row.tool_names_json) : [],
@@ -2203,7 +2269,8 @@ var ConversationsRepo = class {
2203
2269
  account, branch, preview, content_snippet, message_count, page_message_count,
2204
2270
  last_message_sender,
2205
2271
  timestamp, index_seq, first_sent_at, first_sent_text, last_sent_at, last_sent_text,
2206
- model, is_subagent, parent_session_id, is_teammate, team_name, tool_names_json,
2272
+ model, is_subagent, parent_session_id, subagent_id, parent_session_uuid,
2273
+ is_teammate, team_name, tool_names_json,
2207
2274
  last_prompt, status, updated_at
2208
2275
  ) VALUES (
2209
2276
  @file_id, @source_path, @provider, @kind, @external_session_id,
@@ -2213,7 +2280,8 @@ var ConversationsRepo = class {
2213
2280
  @timestamp,
2214
2281
  (SELECT COALESCE(MAX(index_seq), 0) + 1 FROM conversations),
2215
2282
  @first_sent_at, @first_sent_text, @last_sent_at, @last_sent_text,
2216
- @model, @is_subagent, @parent_session_id, @is_teammate, @team_name, @tool_names_json,
2283
+ @model, @is_subagent, @parent_session_id, @subagent_id, @parent_session_uuid,
2284
+ @is_teammate, @team_name, @tool_names_json,
2217
2285
  @last_prompt, 'active', CURRENT_TIMESTAMP
2218
2286
  )
2219
2287
  ON CONFLICT(file_id) DO UPDATE SET
@@ -2240,6 +2308,8 @@ var ConversationsRepo = class {
2240
2308
  model = excluded.model,
2241
2309
  is_subagent = excluded.is_subagent,
2242
2310
  parent_session_id = excluded.parent_session_id,
2311
+ subagent_id = excluded.subagent_id,
2312
+ parent_session_uuid = excluded.parent_session_uuid,
2243
2313
  is_teammate = excluded.is_teammate,
2244
2314
  team_name = excluded.team_name,
2245
2315
  tool_names_json = excluded.tool_names_json,
@@ -2275,6 +2345,8 @@ var ConversationsRepo = class {
2275
2345
  model: meta.model,
2276
2346
  is_subagent: meta.isSubagent ? 1 : 0,
2277
2347
  parent_session_id: meta.parentSessionId,
2348
+ subagent_id: meta.subagentId ?? null,
2349
+ parent_session_uuid: meta.parentSessionUuid ?? null,
2278
2350
  is_teammate: meta.isTeammate ? 1 : 0,
2279
2351
  team_name: meta.teamName,
2280
2352
  tool_names_json: JSON.stringify(meta.toolNames),