@phnx-labs/agents-cli 1.20.50 → 1.20.52

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 (124) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/dist/commands/browser-picker.js +1 -18
  3. package/dist/commands/browser.js +215 -7
  4. package/dist/commands/cloud.js +7 -25
  5. package/dist/commands/computer.d.ts +1 -0
  6. package/dist/commands/computer.js +129 -8
  7. package/dist/commands/events.d.ts +1 -1
  8. package/dist/commands/events.js +2 -3
  9. package/dist/commands/exec.js +66 -8
  10. package/dist/commands/factory.js +9 -4
  11. package/dist/commands/feed.d.ts +9 -0
  12. package/dist/commands/feed.js +69 -0
  13. package/dist/commands/inspect.js +1 -11
  14. package/dist/commands/logs.d.ts +5 -1
  15. package/dist/commands/logs.js +248 -3
  16. package/dist/commands/mcp.js +9 -6
  17. package/dist/commands/message.js +1 -4
  18. package/dist/commands/profiles.js +1 -18
  19. package/dist/commands/repo.js +33 -14
  20. package/dist/commands/resource-view.d.ts +1 -0
  21. package/dist/commands/resource-view.js +5 -17
  22. package/dist/commands/secrets.d.ts +23 -0
  23. package/dist/commands/secrets.js +174 -70
  24. package/dist/commands/sessions-picker.js +1 -18
  25. package/dist/commands/sessions.js +6 -8
  26. package/dist/commands/teams-picker.js +1 -32
  27. package/dist/commands/teams.js +5 -27
  28. package/dist/commands/tmux.js +1 -3
  29. package/dist/commands/view.js +1 -9
  30. package/dist/commands/worktree.js +1 -4
  31. package/dist/index.js +6 -2
  32. package/dist/lib/agents.d.ts +0 -4
  33. package/dist/lib/agents.js +20 -33
  34. package/dist/lib/auto-dispatch-linear.d.ts +18 -0
  35. package/dist/lib/auto-dispatch-linear.js +107 -0
  36. package/dist/lib/auto-dispatch-provider.d.ts +10 -0
  37. package/dist/lib/auto-dispatch-provider.js +25 -0
  38. package/dist/lib/auto-dispatch.d.ts +87 -0
  39. package/dist/lib/auto-dispatch.js +142 -0
  40. package/dist/lib/browser/cdp.js +11 -2
  41. package/dist/lib/browser/drivers/ssh.d.ts +28 -10
  42. package/dist/lib/browser/drivers/ssh.js +57 -18
  43. package/dist/lib/browser/login-detection.d.ts +94 -0
  44. package/dist/lib/browser/login-detection.js +274 -0
  45. package/dist/lib/browser/profiles.d.ts +17 -8
  46. package/dist/lib/browser/profiles.js +27 -8
  47. package/dist/lib/browser/refs.js +1 -5
  48. package/dist/lib/browser/secret-ref.d.ts +10 -0
  49. package/dist/lib/browser/secret-ref.js +14 -0
  50. package/dist/lib/browser/service.js +14 -12
  51. package/dist/lib/cli-resources.d.ts +0 -2
  52. package/dist/lib/cli-resources.js +30 -13
  53. package/dist/lib/cloud/rush.d.ts +15 -24
  54. package/dist/lib/cloud/rush.js +7 -32
  55. package/dist/lib/crabbox/cli.js +4 -1
  56. package/dist/lib/crabbox/lease.d.ts +6 -0
  57. package/dist/lib/crabbox/lease.js +40 -10
  58. package/dist/lib/crabbox/runtimes.d.ts +38 -1
  59. package/dist/lib/crabbox/runtimes.js +98 -5
  60. package/dist/lib/daemon.d.ts +12 -9
  61. package/dist/lib/daemon.js +73 -17
  62. package/dist/lib/events.d.ts +31 -5
  63. package/dist/lib/events.js +288 -101
  64. package/dist/lib/exec.js +32 -14
  65. package/dist/lib/feed.d.ts +56 -0
  66. package/dist/lib/feed.js +251 -0
  67. package/dist/lib/format.d.ts +38 -0
  68. package/dist/lib/format.js +108 -0
  69. package/dist/lib/git.d.ts +21 -0
  70. package/dist/lib/git.js +92 -0
  71. package/dist/lib/hooks/cache.d.ts +9 -2
  72. package/dist/lib/hooks/cache.js +220 -8
  73. package/dist/lib/hooks.js +24 -10
  74. package/dist/lib/hosts/passthrough.js +1 -0
  75. package/dist/lib/platform/exec.d.ts +4 -1
  76. package/dist/lib/platform/exec.js +8 -2
  77. package/dist/lib/resources.d.ts +0 -8
  78. package/dist/lib/resources.js +0 -10
  79. package/dist/lib/rotate.js +2 -0
  80. package/dist/lib/runner.js +10 -2
  81. package/dist/lib/secrets/Agents CLI.app/Contents/CodeResources +0 -0
  82. package/dist/lib/secrets/Agents CLI.app/Contents/MacOS/Agents CLI +0 -0
  83. package/dist/lib/secrets/agent.d.ts +21 -0
  84. package/dist/lib/secrets/agent.js +63 -1
  85. package/dist/lib/secrets/bundles.d.ts +33 -1
  86. package/dist/lib/secrets/bundles.js +38 -8
  87. package/dist/lib/secrets/icloud-import.d.ts +70 -0
  88. package/dist/lib/secrets/icloud-import.js +173 -0
  89. package/dist/lib/secrets/index.d.ts +36 -0
  90. package/dist/lib/secrets/index.js +99 -9
  91. package/dist/lib/secrets/remote.js +1 -1
  92. package/dist/lib/secrets/sync.js +1 -1
  93. package/dist/lib/session/active.d.ts +11 -1
  94. package/dist/lib/session/active.js +3 -0
  95. package/dist/lib/session/db.d.ts +1 -4
  96. package/dist/lib/session/db.js +20 -25
  97. package/dist/lib/session/discover.d.ts +2 -2
  98. package/dist/lib/session/discover.js +60 -48
  99. package/dist/lib/session/parse.js +35 -34
  100. package/dist/lib/session/render.d.ts +7 -3
  101. package/dist/lib/session/render.js +15 -9
  102. package/dist/lib/session/state.d.ts +55 -0
  103. package/dist/lib/session/state.js +100 -11
  104. package/dist/lib/session/types.d.ts +9 -0
  105. package/dist/lib/shims.d.ts +9 -3
  106. package/dist/lib/shims.js +44 -8
  107. package/dist/lib/ssh-tunnel.d.ts +33 -2
  108. package/dist/lib/ssh-tunnel.js +94 -7
  109. package/dist/lib/staleness/types.d.ts +0 -1
  110. package/dist/lib/startup/command-registry.d.ts +1 -0
  111. package/dist/lib/startup/command-registry.js +2 -0
  112. package/dist/lib/state.d.ts +2 -0
  113. package/dist/lib/state.js +25 -8
  114. package/dist/lib/teams/agents.js +6 -3
  115. package/dist/lib/types.d.ts +24 -1
  116. package/dist/lib/versions.d.ts +0 -26
  117. package/dist/lib/versions.js +2 -145
  118. package/dist/lib/warn-unpushed.d.ts +40 -0
  119. package/dist/lib/warn-unpushed.js +128 -0
  120. package/dist/lib/whats-new.d.ts +5 -3
  121. package/dist/lib/whats-new.js +25 -5
  122. package/package.json +3 -1
  123. package/dist/lib/resources/index.d.ts +0 -53
  124. package/dist/lib/resources/index.js +0 -76
@@ -13,7 +13,7 @@ import { getSessionsDir, getSessionsDbPath } from '../state.js';
13
13
  const SESSIONS_DIR = getSessionsDir();
14
14
  const DB_PATH = getSessionsDbPath();
15
15
  /** Current schema version; bumped when migrations are added. */
16
- const SCHEMA_VERSION = 10;
16
+ const SCHEMA_VERSION = 11;
17
17
  /**
18
18
  * Canonicalize a file path for use as a scan_ledger key. The same physical
19
19
  * session file is reachable via multiple aliases — `~/.claude/projects/x.jsonl`
@@ -64,7 +64,8 @@ CREATE TABLE IF NOT EXISTS sessions (
64
64
  pr_url TEXT,
65
65
  pr_number INTEGER,
66
66
  worktree_slug TEXT,
67
- ticket_id TEXT
67
+ ticket_id TEXT,
68
+ plan TEXT
68
69
  );
69
70
  CREATE INDEX IF NOT EXISTS idx_sessions_timestamp ON sessions(timestamp DESC);
70
71
  CREATE INDEX IF NOT EXISTS idx_sessions_cwd ON sessions(cwd);
@@ -213,6 +214,16 @@ function migrateSchema(db, fromVersion) {
213
214
  db.exec(`ALTER TABLE sessions DROP COLUMN name`);
214
215
  }
215
216
  }
217
+ if (fromVersion < 11) {
218
+ // v10 → v11: the Claude scanner now captures the ExitPlanMode plan markdown
219
+ // at scan time so `agents sessions --json` can surface it without forcing
220
+ // consumers (the Factory NEEDS-YOU panel) to re-read raw JSONL. Additive
221
+ // column; rescan to backfill.
222
+ const cols = db.prepare(`PRAGMA table_info(sessions)`).all();
223
+ if (!cols.some(c => c.name === 'plan'))
224
+ db.exec(`ALTER TABLE sessions ADD COLUMN plan TEXT`);
225
+ db.exec(`DELETE FROM scan_ledger;`);
226
+ }
216
227
  }
217
228
  /** Open (or return the cached) sessions database, applying migrations as needed. */
218
229
  export function getDB() {
@@ -429,13 +440,13 @@ const upsertSessionStmt = (db) => db.prepare(`
429
440
  project, cwd, git_branch, topic, label, message_count, token_count,
430
441
  cost_usd, duration_ms,
431
442
  file_path, file_mtime_ms, file_size, scanned_at, is_team_origin,
432
- pr_url, pr_number, worktree_slug, ticket_id
443
+ pr_url, pr_number, worktree_slug, ticket_id, plan
433
444
  ) VALUES (
434
445
  @id, @short_id, @agent, @version, @account, @timestamp, @last_activity,
435
446
  @project, @cwd, @git_branch, @topic, @label, @message_count, @token_count,
436
447
  @cost_usd, @duration_ms,
437
448
  @file_path, @file_mtime_ms, @file_size, @scanned_at, @is_team_origin,
438
- @pr_url, @pr_number, @worktree_slug, @ticket_id
449
+ @pr_url, @pr_number, @worktree_slug, @ticket_id, @plan
439
450
  )
440
451
  ON CONFLICT(id) DO UPDATE SET
441
452
  short_id = excluded.short_id,
@@ -461,7 +472,8 @@ const upsertSessionStmt = (db) => db.prepare(`
461
472
  pr_url = excluded.pr_url,
462
473
  pr_number = excluded.pr_number,
463
474
  worktree_slug = excluded.worktree_slug,
464
- ticket_id = excluded.ticket_id
475
+ ticket_id = excluded.ticket_id,
476
+ plan = excluded.plan
465
477
  `);
466
478
  const deleteTextStmt = (db) => db.prepare(`DELETE FROM session_text WHERE session_id = ?`);
467
479
  const insertTextStmt = (db) => db.prepare(`INSERT INTO session_text (session_id, label, topic, project, content) VALUES (?, ?, ?, ?, ?)`);
@@ -509,6 +521,7 @@ export function upsertSession(meta, content, scan) {
509
521
  pr_number: meta.prNumber ?? null,
510
522
  worktree_slug: meta.worktreeSlug ?? null,
511
523
  ticket_id: meta.ticketId ?? null,
524
+ plan: meta.plan ?? null,
512
525
  };
513
526
  const txn = db.transaction(() => {
514
527
  upsert.run(row);
@@ -597,6 +610,7 @@ export function upsertSessionsBatch(entries) {
597
610
  pr_number: meta.prNumber ?? null,
598
611
  worktree_slug: meta.worktreeSlug ?? null,
599
612
  ticket_id: meta.ticketId ?? null,
613
+ plan: meta.plan ?? null,
600
614
  });
601
615
  delText.run(meta.id);
602
616
  insText.run(meta.id, meta.label ?? '', meta.topic ?? '', meta.project ?? '', content ?? '');
@@ -769,6 +783,7 @@ function rowToMeta(row) {
769
783
  prNumber: row.pr_number ?? undefined,
770
784
  worktreeSlug: row.worktree_slug ?? undefined,
771
785
  ticketId: row.ticket_id ?? undefined,
786
+ plan: row.plan ?? undefined,
772
787
  };
773
788
  }
774
789
  /**
@@ -954,26 +969,6 @@ export function topSessionsByCost(n, options = {}) {
954
969
  durationMs: r.duration_ms ?? 0,
955
970
  }));
956
971
  }
957
- /** Return the set of all file paths currently tracked in the sessions table. */
958
- export function getAllFilePaths() {
959
- const db = getDB();
960
- const rows = db.prepare(`SELECT file_path FROM sessions`).all();
961
- return new Set(rows.map(r => r.file_path));
962
- }
963
- /** Look up sessions by their source file paths. */
964
- export function getSessionsByFilePaths(paths) {
965
- if (paths.length === 0)
966
- return new Map();
967
- const db = getDB();
968
- const placeholders = paths.map(() => '?').join(',');
969
- const rows = db
970
- .prepare(`SELECT * FROM sessions WHERE file_path IN (${placeholders})`)
971
- .all(...paths);
972
- const result = new Map();
973
- for (const row of rows)
974
- result.set(row.file_path, rowToMeta(row));
975
- return result;
976
- }
977
972
  /** Look up a single session by its unique ID. */
978
973
  export function getSessionById(id) {
979
974
  const db = getDB();
@@ -68,6 +68,8 @@ interface ClaudeSessionScan {
68
68
  createdTickets?: string[];
69
69
  /** Team name this session SPAWNED via `agents teams create/add` (not team-of-origin). */
70
70
  spawnedTeam?: string;
71
+ /** Plan markdown from the last ExitPlanMode tool call (Claude sessions only). */
72
+ plan?: string;
71
73
  }
72
74
  /**
73
75
  * Discover sessions. Scans only files whose (mtime, size) have changed since
@@ -152,8 +154,6 @@ export declare function readCodexMeta(filePath: string, resolveAccount?: () => s
152
154
  } | null>;
153
155
  /** Stream a Claude JSONL file and extract scan-level metadata (timestamp, cwd, topic, tokens). */
154
156
  export declare function scanClaudeSession(filePath: string): Promise<ClaudeSessionScan>;
155
- /** Read up to maxLines non-empty lines from the beginning of a file. */
156
- export declare function readFirstLines(filePath: string, maxLines: number): Promise<string[]>;
157
157
  /** Parse a single Kimi session state.json file to extract session metadata. */
158
158
  export declare function readKimiMeta(filePath: string): {
159
159
  meta: SessionMeta;
@@ -13,6 +13,7 @@ import * as crypto from 'crypto';
13
13
  import * as readline from 'readline';
14
14
  import { execFile } from 'child_process';
15
15
  import { promisify } from 'util';
16
+ import Database from '../sqlite.js';
16
17
  import { getAgentsDir, getHistoryDir } from '../state.js';
17
18
  const execFileAsync = promisify(execFile);
18
19
  import { AGENTS, agentConfigDirName, getCliVersion } from '../agents.js';
@@ -485,6 +486,7 @@ async function readClaudeMeta(filePath, sessionId, account, label) {
485
486
  ticketId: scan.ticketId,
486
487
  createdTickets: scan.createdTickets,
487
488
  spawnedTeam: scan.spawnedTeam,
489
+ plan: scan.plan,
488
490
  };
489
491
  }
490
492
  else {
@@ -510,6 +512,7 @@ async function readClaudeMeta(filePath, sessionId, account, label) {
510
512
  ticketId: scan.ticketId,
511
513
  createdTickets: scan.createdTickets,
512
514
  spawnedTeam: scan.spawnedTeam,
515
+ plan: scan.plan,
513
516
  };
514
517
  }
515
518
  return { meta, content: scan.contentText || '' };
@@ -1065,20 +1068,29 @@ let cachedOpenCodeAccount;
1065
1068
  async function getOpenCodeAccount() {
1066
1069
  if (cachedOpenCodeAccount !== undefined)
1067
1070
  return cachedOpenCodeAccount || undefined;
1071
+ // Read through the node/bun SQLite wrapper (not the `sqlite3` CLI) so this
1072
+ // works on every OS — the CLI is absent on Windows.
1073
+ let db;
1068
1074
  try {
1069
1075
  if (fs.existsSync(OPENCODE_DB)) {
1070
- const { stdout } = await execFileAsync('sqlite3', [
1071
- OPENCODE_DB,
1072
- 'SELECT email FROM control_account WHERE active=1 LIMIT 1;',
1073
- ], { encoding: 'utf-8' });
1074
- const out = stdout.trim();
1076
+ db = new Database(OPENCODE_DB);
1077
+ const row = db
1078
+ .prepare('SELECT email FROM control_account WHERE active=1 LIMIT 1;')
1079
+ .get();
1080
+ const out = typeof row?.email === 'string' ? row.email.trim() : '';
1075
1081
  if (out) {
1076
1082
  cachedOpenCodeAccount = out;
1077
1083
  return out;
1078
1084
  }
1079
1085
  }
1080
1086
  }
1081
- catch { /* sqlite3 unavailable or DB locked */ }
1087
+ catch { /* DB not accessible, sqlite module unavailable, or query failed */ }
1088
+ finally {
1089
+ try {
1090
+ db?.close();
1091
+ }
1092
+ catch { /* best-effort close */ }
1093
+ }
1082
1094
  cachedOpenCodeAccount = '';
1083
1095
  return undefined;
1084
1096
  }
@@ -1101,18 +1113,21 @@ async function scanOpenCodeIncremental() {
1101
1113
  }
1102
1114
  const account = await getOpenCodeAccount();
1103
1115
  const currentVersion = await getCurrentAgentVersion('opencode');
1116
+ // Read through the node/bun SQLite wrapper (not the `sqlite3` CLI) so this
1117
+ // works on every OS — the CLI is absent on Windows.
1118
+ let db;
1104
1119
  try {
1105
1120
  const query = `
1106
1121
  SELECT
1107
- s.id,
1108
- s.title,
1109
- s.directory,
1110
- s.version,
1111
- s.time_created,
1112
- s.time_updated,
1113
- COALESCE(stats.message_count, 0),
1114
- stats.token_count,
1115
- COALESCE(stats.has_token_data, 0)
1122
+ s.id AS id,
1123
+ s.title AS title,
1124
+ s.directory AS directory,
1125
+ s.version AS version,
1126
+ s.time_created AS time_created,
1127
+ s.time_updated AS time_updated,
1128
+ COALESCE(stats.message_count, 0) AS message_count,
1129
+ stats.token_count AS token_count,
1130
+ COALESCE(stats.has_token_data, 0) AS has_token_data
1116
1131
  FROM session s
1117
1132
  LEFT JOIN (
1118
1133
  SELECT
@@ -1133,23 +1148,22 @@ async function scanOpenCodeIncremental() {
1133
1148
  ORDER BY time_created DESC
1134
1149
  LIMIT 1000;
1135
1150
  `.replace(/\n/g, ' ');
1136
- const { stdout: out } = await execFileAsync('sqlite3', ['-separator', '|||', OPENCODE_DB, query], {
1137
- encoding: 'utf-8',
1138
- timeout: 5000,
1139
- maxBuffer: 32 * 1024 * 1024,
1140
- });
1151
+ db = new Database(OPENCODE_DB);
1152
+ const rows = db.prepare(query).all();
1141
1153
  const entries = [];
1142
- for (const line of out.split('\n')) {
1143
- if (!line.trim())
1144
- continue;
1145
- const [id, title, directory, version, timeCreatedStr, timeUpdatedStr, messageCountStr, tokenCountStr, hasTokenDataStr] = line.split('|||');
1154
+ for (const row of rows) {
1155
+ const id = typeof row.id === 'string' ? row.id : '';
1146
1156
  if (!id)
1147
1157
  continue;
1148
- const timeCreated = parseInt(timeCreatedStr, 10);
1149
- const timeUpdated = parseInt(timeUpdatedStr, 10);
1150
- const messageCount = parseInt(messageCountStr, 10);
1151
- const tokenCount = parseInt(tokenCountStr, 10);
1152
- const hasTokenData = hasTokenDataStr === '1';
1158
+ const title = typeof row.title === 'string' ? row.title : '';
1159
+ const directory = typeof row.directory === 'string' ? row.directory : '';
1160
+ const version = typeof row.version === 'string' ? row.version : '';
1161
+ const asInt = (v) => typeof v === 'number' ? v : parseInt(String(v), 10);
1162
+ const timeCreated = asInt(row.time_created);
1163
+ const timeUpdated = asInt(row.time_updated);
1164
+ const messageCount = asInt(row.message_count);
1165
+ const tokenCount = asInt(row.token_count);
1166
+ const hasTokenData = asInt(row.has_token_data) === 1;
1153
1167
  const timestamp = isNaN(timeCreated) ? new Date().toISOString() : new Date(timeCreated).toISOString();
1154
1168
  // OpenCode is one shared DB, not one file per session — its row carries a
1155
1169
  // per-session updated time. Set lastActivity explicitly (falling back to
@@ -1182,6 +1196,12 @@ async function scanOpenCodeIncremental() {
1182
1196
  console.error(`Warning: Could not query OpenCode sessions: ${err.message}`);
1183
1197
  }
1184
1198
  }
1199
+ finally {
1200
+ try {
1201
+ db?.close();
1202
+ }
1203
+ catch { /* best-effort close */ }
1204
+ }
1185
1205
  }
1186
1206
  // ---------------------------------------------------------------------------
1187
1207
  // OpenClaw
@@ -1770,6 +1790,9 @@ export async function scanClaudeSession(filePath) {
1770
1790
  const createdTickets = new Set();
1771
1791
  const pendingTicketTools = new Set();
1772
1792
  let spawnedTeam;
1793
+ // The LAST ExitPlanMode plan wins so a re-planned session surfaces its most
1794
+ // recent plan, matching the semantic the extension's re-parser relied on.
1795
+ let plan;
1773
1796
  try {
1774
1797
  for await (const line of rl) {
1775
1798
  if (!line.trim())
@@ -1802,6 +1825,13 @@ export async function scanClaudeSession(filePath) {
1802
1825
  if (typeof b?.id === 'string' && isTicketCreateTool(b?.name, b?.input?.command)) {
1803
1826
  pendingTicketTools.add(b.id);
1804
1827
  }
1828
+ // ExitPlanMode plan markdown — last one wins so a re-planned session
1829
+ // reports its most recent plan.
1830
+ if (b?.name === 'ExitPlanMode' && typeof b?.input?.plan === 'string') {
1831
+ const p = b.input.plan.trim();
1832
+ if (p)
1833
+ plan = b.input.plan;
1834
+ }
1805
1835
  }
1806
1836
  }
1807
1837
  if (pendingTicketTools.size > 0 && parsed.type === 'user' && Array.isArray(parsed.message?.content)) {
@@ -1949,6 +1979,7 @@ export async function scanClaudeSession(filePath) {
1949
1979
  ticketId: ticket?.id,
1950
1980
  createdTickets: createdTickets.size > 0 ? [...createdTickets] : undefined,
1951
1981
  spawnedTeam,
1982
+ plan,
1952
1983
  };
1953
1984
  }
1954
1985
  /** Stream a Codex JSONL file and extract scan-level metadata (session ID, cwd, topic, tokens). */
@@ -2152,25 +2183,6 @@ function getOpenClawWorkspaceMap() {
2152
2183
  // ---------------------------------------------------------------------------
2153
2184
  // Utilities
2154
2185
  // ---------------------------------------------------------------------------
2155
- /** Read up to maxLines non-empty lines from the beginning of a file. */
2156
- export function readFirstLines(filePath, maxLines) {
2157
- return new Promise((resolve) => {
2158
- const lines = [];
2159
- const stream = fs.createReadStream(filePath, { encoding: 'utf-8' });
2160
- const rl = readline.createInterface({ input: stream, crlfDelay: Infinity });
2161
- rl.on('line', (line) => {
2162
- if (line.trim()) {
2163
- lines.push(line);
2164
- }
2165
- if (lines.length >= maxLines) {
2166
- rl.close();
2167
- stream.destroy();
2168
- }
2169
- });
2170
- rl.on('close', () => resolve(lines));
2171
- rl.on('error', () => resolve(lines));
2172
- });
2173
- }
2174
2186
  /** Compute the SHA-256 hex digest of a string. */
2175
2187
  function sha256(input) {
2176
2188
  return crypto.createHash('sha256').update(input).digest('hex');
@@ -6,8 +6,8 @@
6
6
  * objects suitable for rendering, filtering, and summarization.
7
7
  */
8
8
  import * as fs from 'fs';
9
+ import { truncate } from '../format.js';
9
10
  import * as path from 'path';
10
- import { execFileSync } from 'child_process';
11
11
  import Database from '../sqlite.js';
12
12
  /**
13
13
  * Largest session file we will load into memory. Above this we throw a clean
@@ -218,10 +218,6 @@ export function summarizeToolUse(tool, args) {
218
218
  }
219
219
  }
220
220
  }
221
- /** Truncate a string to n characters, appending '...' if shortened. */
222
- function truncate(s, n) {
223
- return s.length <= n ? s : s.slice(0, n - 3) + '...';
224
- }
225
221
  /** Replace the home directory prefix with ~ for display. */
226
222
  function shortenPath(p) {
227
223
  const home = process.env.HOME || '';
@@ -1006,45 +1002,50 @@ export function parseOpenCode(filePath) {
1006
1002
  if (!dbPath || !sessionId)
1007
1003
  return [];
1008
1004
  const events = [];
1005
+ // Read through the node/bun SQLite wrapper (not the `sqlite3` CLI) so this
1006
+ // works on every OS — the CLI is absent on Windows.
1007
+ let rows;
1008
+ let db;
1009
1009
  try {
1010
- // Query messages with their parts, ordered chronologically.
1011
- // Each row: msg_role ||| part_type ||| part_data (truncated for tool output) ||| msg_time
1010
+ // Query messages with their parts, ordered chronologically. Tool parts are
1011
+ // truncated to keep large tool outputs from bloating memory; the session id
1012
+ // is bound as a parameter rather than interpolated.
1012
1013
  const query = `
1013
1014
  SELECT
1014
- json_extract(m.data, '$.role'),
1015
- json_extract(p.data, '$.type'),
1015
+ json_extract(m.data, '$.role') AS role,
1016
+ json_extract(p.data, '$.type') AS part_type,
1016
1017
  CASE
1017
1018
  WHEN json_extract(p.data, '$.type') = 'tool'
1018
1019
  THEN substr(p.data, 1, 2000)
1019
1020
  ELSE p.data
1020
- END,
1021
- m.time_created
1021
+ END AS part_data,
1022
+ m.time_created AS time_created
1022
1023
  FROM message m
1023
1024
  JOIN part p ON p.message_id = m.id AND p.session_id = m.session_id
1024
- WHERE m.session_id = '${sessionId.replace(/'/g, "''")}'
1025
+ WHERE m.session_id = ?
1025
1026
  ORDER BY m.time_created ASC, p.time_created ASC;
1026
1027
  `.replace(/\n/g, ' ');
1027
- const out = execFileSync('sqlite3', ['-separator', '|||', dbPath, query], {
1028
- encoding: 'utf-8',
1029
- timeout: 10000,
1030
- maxBuffer: 32 * 1024 * 1024,
1031
- stdio: ['ignore', 'pipe', 'ignore'],
1032
- });
1033
- for (const line of out.split('\n')) {
1034
- if (!line.trim())
1035
- continue;
1036
- const sepIdx1 = line.indexOf('|||');
1037
- if (sepIdx1 === -1)
1038
- continue;
1039
- const sepIdx2 = line.indexOf('|||', sepIdx1 + 3);
1040
- if (sepIdx2 === -1)
1041
- continue;
1042
- const sepIdx3 = line.lastIndexOf('|||');
1043
- const role = line.slice(0, sepIdx1);
1044
- const partType = line.slice(sepIdx1 + 3, sepIdx2);
1045
- const partDataStr = line.slice(sepIdx2 + 3, sepIdx3);
1046
- const timeStr = line.slice(sepIdx3 + 3);
1047
- const timeMs = parseInt(timeStr, 10);
1028
+ db = new Database(dbPath);
1029
+ rows = db.prepare(query).all(sessionId);
1030
+ }
1031
+ catch {
1032
+ /* DB not accessible, sqlite module unavailable, or query failed */
1033
+ return events;
1034
+ }
1035
+ finally {
1036
+ try {
1037
+ db?.close();
1038
+ }
1039
+ catch {
1040
+ /* best-effort close */
1041
+ }
1042
+ }
1043
+ try {
1044
+ for (const row of rows) {
1045
+ const role = typeof row.role === 'string' ? row.role : '';
1046
+ const partType = typeof row.part_type === 'string' ? row.part_type : '';
1047
+ const partDataStr = typeof row.part_data === 'string' ? row.part_data : '';
1048
+ const timeMs = typeof row.time_created === 'number' ? row.time_created : parseInt(String(row.time_created), 10);
1048
1049
  const timestamp = isNaN(timeMs) ? new Date().toISOString() : new Date(timeMs).toISOString();
1049
1050
  let partData;
1050
1051
  try {
@@ -1112,7 +1113,7 @@ export function parseOpenCode(filePath) {
1112
1113
  }
1113
1114
  }
1114
1115
  catch {
1115
- /* DB not accessible or query failed */
1116
+ /* malformed row payload return what we parsed so far */
1116
1117
  }
1117
1118
  return events;
1118
1119
  }
@@ -6,7 +6,7 @@
6
6
  * errors, final message). The markdown renderer emits a full conversation
7
7
  * transcript. Filtering by role and turn slicing is handled here as well.
8
8
  */
9
- import type { SessionEvent } from './types.js';
9
+ import type { SessionEvent, SessionMeta } from './types.js';
10
10
  /**
11
11
  * Return absPath relative to cwd; fall back to ~/… then absolute.
12
12
  */
@@ -103,7 +103,11 @@ export interface RenderConversationMarkdownOptions {
103
103
  }
104
104
  export declare function renderConversationMarkdown(events: SessionEvent[], opts?: RenderConversationMarkdownOptions): string;
105
105
  /**
106
- * Render session as JSON (normalized events).
106
+ * Render one session's JSON output — the shape `agents sessions <id> --json`
107
+ * emits. When `meta` is provided (the standard path), returns a
108
+ * `{ session, events }` wrapper so top-level fields (`plan`, `prUrl`, …) that
109
+ * live on `SessionMeta` are visible without re-parsing events. Legacy
110
+ * callers that pass no meta still get a bare `SessionEvent[]` array.
107
111
  */
108
- export declare function renderJson(events: SessionEvent[]): string;
112
+ export declare function renderJson(events: SessionEvent[], meta?: SessionMeta): string;
109
113
  export {};
@@ -7,6 +7,7 @@
7
7
  * transcript. Filtering by role and turn slicing is handled here as well.
8
8
  */
9
9
  import chalk from 'chalk';
10
+ import { truncate } from '../format.js';
10
11
  import { summarizeToolUse } from './parse.js';
11
12
  import { cleanSessionPrompt, extractSessionTopic } from './prompt.js';
12
13
  import { renderMarkdown } from '../markdown.js';
@@ -336,14 +337,14 @@ function renderCommandsSection(cmds, lines) {
336
337
  if (catEntry.signal === 'mid') {
337
338
  // Mid signal: display the bucket key (e.g. ssh→openclaw browser) with aggregate count
338
339
  const countSuffix = v.count > 1 ? chalk.gray(` × ${v.count}`) : '';
339
- lines.push(` ${chalk.cyan(truncateCmd(key, 80))}${countSuffix}`);
340
+ lines.push(` ${chalk.cyan(truncate(key, 80))}${countSuffix}`);
340
341
  }
341
342
  else {
342
343
  // High signal: display distinct raw sample commands
343
344
  const distinctSamples = pickDistinct(v.samples, 3);
344
345
  for (const sample of distinctSamples) {
345
346
  const countSuffix = v.count > 1 ? chalk.gray(` × ${v.count}`) : '';
346
- lines.push(` ${chalk.cyan(truncateCmd(sample, 80))}${countSuffix}`);
347
+ lines.push(` ${chalk.cyan(truncate(sample, 80))}${countSuffix}`);
347
348
  }
348
349
  }
349
350
  shown++;
@@ -354,7 +355,7 @@ function renderCommandsSection(cmds, lines) {
354
355
  lines.push(` ${chalk.dim('Other')} ${chalk.gray(`(${otherCount})`)}`);
355
356
  for (const [, v] of Array.from(otherKeys.entries()).slice(0, 5)) {
356
357
  const countSuffix = v.count > 1 ? chalk.gray(` × ${v.count}`) : '';
357
- lines.push(` ${chalk.cyan(truncateCmd(v.samples[0] ?? '', 80))}${countSuffix}`);
358
+ lines.push(` ${chalk.cyan(truncate(v.samples[0] ?? '', 80))}${countSuffix}`);
358
359
  }
359
360
  }
360
361
  lines.push('');
@@ -372,9 +373,6 @@ function pickDistinct(samples, max) {
372
373
  }
373
374
  return result.length > 0 ? result : samples.slice(0, max);
374
375
  }
375
- function truncateCmd(cmd, max) {
376
- return cmd.length <= max ? cmd : cmd.slice(0, max - 1) + '…';
377
- }
378
376
  // ── File grouping ─────────────────────────────────────────────────────────────
379
377
  /** Group file paths by their parent directory, relative to cwd. */
380
378
  function groupByParentDir(paths, cwd) {
@@ -916,10 +914,18 @@ export function renderConversationMarkdown(events, opts = {}) {
916
914
  return parts.join('\n\n');
917
915
  }
918
916
  /**
919
- * Render session as JSON (normalized events).
917
+ * Render one session's JSON output — the shape `agents sessions <id> --json`
918
+ * emits. When `meta` is provided (the standard path), returns a
919
+ * `{ session, events }` wrapper so top-level fields (`plan`, `prUrl`, …) that
920
+ * live on `SessionMeta` are visible without re-parsing events. Legacy
921
+ * callers that pass no meta still get a bare `SessionEvent[]` array.
920
922
  */
921
- export function renderJson(events) {
922
- return JSON.stringify(events, null, 2);
923
+ export function renderJson(events, meta) {
924
+ if (!meta)
925
+ return JSON.stringify(events, null, 2);
926
+ // Strip internal-only bookkeeping fields the listing --json path also strips.
927
+ const { _matchedTerms, _bm25Score, _remote, ...session } = meta;
928
+ return JSON.stringify({ session, events }, null, 2);
923
929
  }
924
930
  /** Replace the home directory prefix with ~ for trace display. */
925
931
  function shortenPathTrace(p) {
@@ -17,6 +17,30 @@
17
17
  import type { SessionEvent } from './types.js';
18
18
  export type SessionActivity = 'working' | 'waiting_input' | 'idle';
19
19
  export type AwaitingReason = 'question' | 'plan_review' | 'permission';
20
+ /** One discrete choice the agent offered the user. */
21
+ export interface QuestionOption {
22
+ /** The choice label — also what a free-text reply channel sends back. */
23
+ label: string;
24
+ /** Optional longer description shown under the label. */
25
+ description?: string;
26
+ /**
27
+ * Selection keystroke for an interactive TUI prompt (AskUserQuestion / plan /
28
+ * permission are select-lists, not text inputs): a digit ('1'), or 'esc' to
29
+ * cancel/deny. Absent for a plain prose question, which takes free text.
30
+ */
31
+ key?: string;
32
+ }
33
+ /**
34
+ * The decision an agent handed back to the user, extracted at the SOURCE so every
35
+ * consumer (Factory panel, teams, cloud) gets the real question + options instead
36
+ * of re-deriving them from a truncated preview line. `reason` mirrors
37
+ * {@link AwaitingReason}; `options` is present when the agent offered discrete choices.
38
+ */
39
+ export interface StructuredQuestion {
40
+ text: string;
41
+ reason: AwaitingReason;
42
+ options?: QuestionOption[];
43
+ }
20
44
  export interface DetectedPr {
21
45
  url: string;
22
46
  number?: number;
@@ -40,6 +64,21 @@ export interface SessionState {
40
64
  lastEventKind?: SessionEvent['type'];
41
65
  /** Single-line description of the latest turn (message text or tool action). */
42
66
  preview?: string;
67
+ /**
68
+ * The structured decision the agent is waiting on (question / plan / permission),
69
+ * with its options when it offered any. Set only when activity is waiting_input.
70
+ */
71
+ question?: StructuredQuestion;
72
+ /**
73
+ * The plan markdown from the most recent `ExitPlanMode` tool call, surfaced
74
+ * when `awaitingReason === 'plan_review'`. The state engine detects the
75
+ * handoff off the same tool event; carrying the plan text alongside it lets
76
+ * consumers (the Factory NEEDS-YOU panel, `agents sessions <id> --json`)
77
+ * render the actual plan without re-parsing the transcript.
78
+ */
79
+ plan?: string;
80
+ /** Last few assistant turns (most-recent last), one line each — panel context. */
81
+ tail?: string[];
43
82
  lastActivityMs?: number;
44
83
  pr?: DetectedPr;
45
84
  worktree?: DetectedWorktree;
@@ -85,6 +124,22 @@ export declare function isTicketCreateTool(name?: string, command?: string): boo
85
124
  * take `#<number>`. Returns undefined when neither shape is present.
86
125
  */
87
126
  export declare function extractCreatedTicket(text?: string): string | undefined;
127
+ /**
128
+ * Pull the plan markdown out of an `ExitPlanMode` tool_use event's args. The
129
+ * Claude tool schema is `{ plan: string }`; the transcript parser already
130
+ * lifts `input` onto `event.args`. Returns undefined for a missing/empty plan
131
+ * so consumers can rely on `plan?: string` truthiness.
132
+ */
133
+ export declare function extractPlanText(args?: Record<string, any>): string | undefined;
134
+ /**
135
+ * Structured question from a Claude `AskUserQuestion` tool call. Its input is
136
+ * `{ questions: [{ question, header, options: [{label, description}] }] }` and the
137
+ * whole thing is already parsed onto `event.args` by the transcript parser — this
138
+ * surfaces the first question + its options (instead of collapsing to a generic
139
+ * "Asked you a question"). The prompt is a select-list, so each option carries its
140
+ * 1-based selection digit as `key`.
141
+ */
142
+ export declare function structuredQuestionFromAsk(args?: Record<string, any>): StructuredQuestion | undefined;
88
143
  /**
89
144
  * Infer live activity + a preview from a chronological event slice. `pr` /
90
145
  * `ticket` / `worktree` are attached by `inferSessionState`; this focuses on the