@tekyzinc/gsd-t 3.23.11 → 3.25.10

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 (74) hide show
  1. package/CHANGELOG.md +48 -0
  2. package/README.md +7 -0
  3. package/bin/cli-preflight-checks/branch-guard.cjs +110 -0
  4. package/bin/cli-preflight-checks/contracts-stable.cjs +128 -0
  5. package/bin/cli-preflight-checks/deps-installed.cjs +89 -0
  6. package/bin/cli-preflight-checks/manifest-fresh.cjs +98 -0
  7. package/bin/cli-preflight-checks/ports-free.cjs +110 -0
  8. package/bin/cli-preflight-checks/working-tree-state.cjs +149 -0
  9. package/bin/cli-preflight.cjs +265 -0
  10. package/bin/gsd-t-context-brief-kinds/design-verify.cjs +139 -0
  11. package/bin/gsd-t-context-brief-kinds/execute.cjs +205 -0
  12. package/bin/gsd-t-context-brief-kinds/qa.cjs +130 -0
  13. package/bin/gsd-t-context-brief-kinds/red-team.cjs +131 -0
  14. package/bin/gsd-t-context-brief-kinds/scan.cjs +118 -0
  15. package/bin/gsd-t-context-brief-kinds/verify.cjs +157 -0
  16. package/bin/gsd-t-context-brief.cjs +395 -0
  17. package/bin/gsd-t-ratelimit-probe-worker.cjs +236 -0
  18. package/bin/gsd-t-ratelimit-probe.cjs +648 -0
  19. package/bin/gsd-t-verify-gate-judge.cjs +224 -0
  20. package/bin/gsd-t-verify-gate.cjs +612 -0
  21. package/bin/gsd-t.js +45 -1
  22. package/bin/live-activity-report.cjs +615 -0
  23. package/bin/m55-substrate-proof.cjs +134 -0
  24. package/bin/parallel-cli-tee.cjs +206 -0
  25. package/bin/parallel-cli.cjs +478 -0
  26. package/commands/gsd-t-execute.md +31 -0
  27. package/commands/gsd-t-help.md +21 -0
  28. package/commands/gsd-t-verify.md +38 -0
  29. package/docs/architecture.md +194 -0
  30. package/docs/diagrams/.gsd-t/.context-meter-state.json +10 -0
  31. package/docs/diagrams/.gsd-t/context-meter.log +9 -0
  32. package/docs/diagrams/.gsd-t/events/2026-05-08.jsonl +45 -0
  33. package/docs/diagrams/.gsd-t/events/2026-05-09.jsonl +1 -0
  34. package/docs/diagrams/.gsd-t/heartbeat-cd9e7f59-ba5b-406a-9ed6-16762f039e81.jsonl +48 -0
  35. package/docs/diagrams/01-top-level-map-d2.png +0 -0
  36. package/docs/diagrams/01-top-level-map.d2 +77 -0
  37. package/docs/diagrams/01-top-level-map.mmd +48 -0
  38. package/docs/diagrams/01-top-level-map.png +0 -0
  39. package/docs/diagrams/01-top-level-map.svg +126 -0
  40. package/docs/diagrams/02-milestone-lifecycle-d2.png +0 -0
  41. package/docs/diagrams/02-milestone-lifecycle.d2 +38 -0
  42. package/docs/diagrams/02-milestone-lifecycle.mmd +36 -0
  43. package/docs/diagrams/02-milestone-lifecycle.png +0 -0
  44. package/docs/diagrams/02-milestone-lifecycle.svg +114 -0
  45. package/docs/diagrams/03-wave-mode-d2.png +0 -0
  46. package/docs/diagrams/03-wave-mode.d2 +33 -0
  47. package/docs/diagrams/03-wave-mode.mmd +21 -0
  48. package/docs/diagrams/03-wave-mode.png +0 -0
  49. package/docs/diagrams/03-wave-mode.svg +113 -0
  50. package/docs/diagrams/04-design-to-code-d2.png +0 -0
  51. package/docs/diagrams/04-design-to-code.d2 +35 -0
  52. package/docs/diagrams/04-design-to-code.mmd +29 -0
  53. package/docs/diagrams/04-design-to-code.png +0 -0
  54. package/docs/diagrams/04-design-to-code.svg +115 -0
  55. package/docs/diagrams/05-backlog-d2.png +0 -0
  56. package/docs/diagrams/05-backlog.d2 +40 -0
  57. package/docs/diagrams/05-backlog.mmd +20 -0
  58. package/docs/diagrams/05-backlog.png +0 -0
  59. package/docs/diagrams/05-backlog.svg +113 -0
  60. package/docs/diagrams/06-automation-utilities-d2.png +0 -0
  61. package/docs/diagrams/06-automation-utilities.d2 +48 -0
  62. package/docs/diagrams/06-automation-utilities.mmd +47 -0
  63. package/docs/diagrams/06-automation-utilities.png +0 -0
  64. package/docs/diagrams/06-automation-utilities.svg +110 -0
  65. package/docs/diagrams/_theme.d2 +86 -0
  66. package/docs/requirements.md +48 -0
  67. package/docs/workflow-diagram.md +338 -0
  68. package/package.json +1 -1
  69. package/scripts/gsd-t-dashboard-server.js +190 -0
  70. package/scripts/gsd-t-transcript.html +200 -0
  71. package/templates/CLAUDE-global.md +46 -0
  72. package/templates/prompts/design-verify-subagent.md +3 -0
  73. package/templates/prompts/qa-subagent.md +3 -0
  74. package/templates/prompts/red-team-subagent.md +3 -0
@@ -0,0 +1,615 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * GSD-T Live Activity Report (M54 D1 T1)
5
+ *
6
+ * Pure read-only observer that answers:
7
+ *
8
+ * "What subprocesses, tool calls, and watches is the orchestrator
9
+ * currently running RIGHT NOW?"
10
+ *
11
+ * Contract: .gsd-t/contracts/live-activity-contract.md v1.0.0
12
+ *
13
+ * Hard rules:
14
+ * 1. NEVER writes a file.
15
+ * 2. NEVER calls an LLM or spawns a subprocess.
16
+ * 3. Silent-fail on malformed inputs — skip the bad line, note it, continue.
17
+ * 4. Zero external deps. `.cjs` so it loads in both ESM and CJS projects.
18
+ */
19
+
20
+ const fs = require('fs');
21
+ const path = require('path');
22
+ const os = require('os');
23
+ const crypto = require('crypto');
24
+
25
+ const SCHEMA_VERSION = 1;
26
+
27
+ // Tool-use threshold for `tool` kind detection (30 seconds)
28
+ const TOOL_THRESHOLD_MS = 30_000;
29
+
30
+ // Source-file mtime threshold — files older than this are considered stale (60 seconds)
31
+ const MTIME_STALE_MS = 60_000;
32
+
33
+ // ── Public API ──────────────────────────────────────────────────────────────
34
+
35
+ /**
36
+ * Compute live activities for the given project directory.
37
+ *
38
+ * @param {object} [opts]
39
+ * @param {string} [opts.projectDir='.']
40
+ * @param {Date} [opts.now] injection for tests
41
+ * @returns {{ schemaVersion: number, generatedAt: string, activities: object[], notes: string[] }}
42
+ */
43
+ function computeLiveActivities(opts) {
44
+ opts = opts || {};
45
+ const projectDir = opts.projectDir || '.';
46
+ const now = opts.now instanceof Date ? opts.now : new Date();
47
+ const notes = [];
48
+
49
+ // Source 1: events JSONL (today only)
50
+ const today = now.toISOString().slice(0, 10);
51
+ const eventsFile = path.join(projectDir, '.gsd-t', 'events', today + '.jsonl');
52
+ const eventsActivities = _readEventsActivities(eventsFile, now, notes);
53
+ const eventsMtime = _safeFileMtime(eventsFile);
54
+
55
+ // Source 2: orchestrator JSONL (~/.claude/projects/<slug>/<sid>.jsonl)
56
+ const orchestratorActivities = _readOrchestratorActivities(projectDir, now, notes);
57
+
58
+ // UNION + dedup
59
+ const allActivities = _dedup([...eventsActivities, ...orchestratorActivities]);
60
+
61
+ // Source 3: spawn plan files (.gsd-t/spawns/*.json)
62
+ const spawnActivities = _readSpawnActivities(projectDir, now, notes);
63
+ const allWithSpawns = _dedup([...allActivities, ...spawnActivities]);
64
+
65
+ // Apply liveness falsifiers
66
+ const live = allWithSpawns.filter((a) => _isLive(a, eventsFile, eventsMtime, now, notes));
67
+
68
+ return {
69
+ schemaVersion: SCHEMA_VERSION,
70
+ generatedAt: now.toISOString(),
71
+ activities: live,
72
+ notes,
73
+ };
74
+ }
75
+
76
+ // ── Internal helpers ────────────────────────────────────────────────────────
77
+
78
+ /**
79
+ * Read activities from .gsd-t/events/<today>.jsonl.
80
+ * Detects bash (run_in_background) and monitor (Monitor tool_use without stop).
81
+ * Also detects tool kind (any tool_use > 30s without tool_result).
82
+ */
83
+ function _readEventsActivities(eventsFile, now, notes) {
84
+ const activities = [];
85
+ let content;
86
+ try {
87
+ if (!fs.existsSync(eventsFile)) {
88
+ notes.push('no events file for today');
89
+ return activities;
90
+ }
91
+ content = fs.readFileSync(eventsFile, 'utf8');
92
+ } catch (err) {
93
+ notes.push('could not read events file: ' + (err && err.message || String(err)));
94
+ return activities;
95
+ }
96
+
97
+ const lines = content.split(/\r?\n/);
98
+ const toolUseById = new Map(); // tool_use_id -> event object
99
+ const terminatedIds = new Set(); // tool_use_ids with tool_result or monitor_stopped
100
+ const monitorStoppedIds = new Set();
101
+
102
+ // First pass: collect all events
103
+ const allEvents = [];
104
+ for (let i = 0; i < lines.length; i++) {
105
+ const line = lines[i].trim();
106
+ if (!line) continue;
107
+ let obj;
108
+ try {
109
+ obj = JSON.parse(line);
110
+ } catch (_) {
111
+ notes.push('skipped malformed JSONL line at ' + eventsFile + ':' + (i + 1));
112
+ continue;
113
+ }
114
+ if (!obj || typeof obj !== 'object') continue;
115
+ allEvents.push({ obj, lineno: i + 1 });
116
+ }
117
+
118
+ // Second pass: build lookup maps
119
+ for (const { obj } of allEvents) {
120
+ const type = obj.type || obj.event_type;
121
+
122
+ // tool_result terminates a tool_use
123
+ if (type === 'tool_result' && obj.tool_use_id) {
124
+ terminatedIds.add(obj.tool_use_id);
125
+ }
126
+ // monitor_stopped terminates a monitor
127
+ if (type === 'monitor_stopped' && obj.tool_use_id) {
128
+ terminatedIds.add(obj.tool_use_id);
129
+ monitorStoppedIds.add(obj.tool_use_id);
130
+ }
131
+ // spawn_completed terminates a spawn
132
+ if (type === 'spawn_completed' && obj.tool_use_id) {
133
+ terminatedIds.add(obj.tool_use_id);
134
+ }
135
+ }
136
+
137
+ // Third pass: detect activities
138
+ for (const { obj } of allEvents) {
139
+ const type = obj.type || obj.event_type;
140
+
141
+ // bash: run_in_background sentinel
142
+ if (obj.run_in_background === true) {
143
+ const id = obj.tool_use_id || _makeId('bash', obj.command || obj.label || '', obj.startedAt || obj.ts || now.toISOString());
144
+ if (!terminatedIds.has(id)) {
145
+ activities.push(_makeActivity({
146
+ id,
147
+ kind: 'bash',
148
+ label: obj.command || obj.label || 'bash',
149
+ startedAt: obj.startedAt || obj.ts || now.toISOString(),
150
+ now,
151
+ pid: obj.pid,
152
+ toolUseId: obj.tool_use_id,
153
+ }));
154
+ }
155
+ continue;
156
+ }
157
+
158
+ // Monitor and tool: tool_use events
159
+ const toolName = obj.name || (obj.type === 'tool_use' && obj.name) || (obj.event_type === 'tool_use_started' && obj.name);
160
+ if (type === 'tool_use' || type === 'tool_use_started') {
161
+ const toolUseId = obj.tool_use_id || obj.id;
162
+ if (!toolUseId) continue;
163
+ if (terminatedIds.has(toolUseId)) continue;
164
+
165
+ const name = obj.name || toolName || '';
166
+ const startedAt = obj.startedAt || obj.start_time || obj.ts || now.toISOString();
167
+ const startMs = _safeDate(startedAt);
168
+ const ageMs = startMs ? now.getTime() - startMs.getTime() : 0;
169
+
170
+ if (name === 'Monitor') {
171
+ // Monitor kind
172
+ activities.push(_makeActivity({
173
+ id: toolUseId,
174
+ kind: 'monitor',
175
+ label: (obj.input && (obj.input.command || obj.input.path)) || name,
176
+ startedAt,
177
+ now,
178
+ pid: obj.pid,
179
+ toolUseId,
180
+ }));
181
+ } else if (ageMs > TOOL_THRESHOLD_MS) {
182
+ // Tool kind: any tool_use > 30s without result
183
+ activities.push(_makeActivity({
184
+ id: toolUseId,
185
+ kind: 'tool',
186
+ label: name || 'tool',
187
+ startedAt,
188
+ now,
189
+ pid: obj.pid,
190
+ toolUseId,
191
+ }));
192
+ }
193
+ }
194
+ }
195
+
196
+ return activities;
197
+ }
198
+
199
+ /**
200
+ * Read activities from the orchestrator JSONL (~/.claude/projects/<slug>/<sid>.jsonl).
201
+ * Uses slug discovery via _slugFromTranscriptPath / _slugToProjectDir from
202
+ * scripts/hooks/gsd-t-conversation-capture.js.
203
+ */
204
+ function _readOrchestratorActivities(projectDir, now, notes) {
205
+ const activities = [];
206
+
207
+ // Lazy-load the slug helpers from conversation-capture.js
208
+ let slugHelpers;
209
+ try {
210
+ slugHelpers = require(path.join(__dirname, '..', 'scripts', 'hooks', 'gsd-t-conversation-capture.js'));
211
+ } catch (err) {
212
+ notes.push('could not load slug helpers: ' + (err && err.message || String(err)));
213
+ return activities;
214
+ }
215
+
216
+ const { _slugFromTranscriptPath, _slugToProjectDir } = slugHelpers._internal || slugHelpers;
217
+
218
+ // Derive slug from project dir: reverse the _slugToProjectDir logic
219
+ // by looking for a slug in ~/.claude/projects/ that decodes to projectDir
220
+ const home = process.env.HOME || os.homedir();
221
+ if (!home) {
222
+ notes.push('could not determine HOME directory for slug discovery');
223
+ return activities;
224
+ }
225
+
226
+ const projectsDir = path.join(home, '.claude', 'projects');
227
+ let slugDir;
228
+ try {
229
+ if (!fs.existsSync(projectsDir)) {
230
+ notes.push('no ~/.claude/projects/ directory found');
231
+ return activities;
232
+ }
233
+ // Enumerate slugs and find one that maps to projectDir
234
+ const slugs = fs.readdirSync(projectsDir, { withFileTypes: true })
235
+ .filter((e) => e.isDirectory())
236
+ .map((e) => e.name);
237
+
238
+ const resolvedProject = path.resolve(projectDir);
239
+ for (const slug of slugs) {
240
+ const decoded = _slugToProjectDir(slug);
241
+ if (decoded && path.resolve(decoded) === resolvedProject) {
242
+ slugDir = path.join(projectsDir, slug);
243
+ break;
244
+ }
245
+ }
246
+ } catch (err) {
247
+ notes.push('slug discovery error: ' + (err && err.message || String(err)));
248
+ return activities;
249
+ }
250
+
251
+ if (!slugDir) {
252
+ notes.push('orchestrator slug unresolvable for cwd=' + projectDir);
253
+ return activities;
254
+ }
255
+
256
+ // Find most recent JSONL file in the slug directory
257
+ let jsonlFile;
258
+ try {
259
+ const files = fs.readdirSync(slugDir)
260
+ .filter((f) => f.endsWith('.jsonl'))
261
+ .map((f) => {
262
+ const full = path.join(slugDir, f);
263
+ let mtime = 0;
264
+ try { mtime = fs.statSync(full).mtimeMs; } catch (_) { /* noop */ }
265
+ return { f, full, mtime };
266
+ })
267
+ .sort((a, b) => b.mtime - a.mtime);
268
+ if (files.length > 0) jsonlFile = files[0].full;
269
+ } catch (err) {
270
+ notes.push('could not list slug dir ' + slugDir + ': ' + (err && err.message || String(err)));
271
+ return activities;
272
+ }
273
+
274
+ if (!jsonlFile) {
275
+ notes.push('no JSONL files in slug dir ' + slugDir);
276
+ return activities;
277
+ }
278
+
279
+ // Parse orchestrator JSONL
280
+ let content;
281
+ try {
282
+ content = fs.readFileSync(jsonlFile, 'utf8');
283
+ } catch (err) {
284
+ notes.push('could not read orchestrator JSONL ' + jsonlFile + ': ' + (err && err.message || String(err)));
285
+ return activities;
286
+ }
287
+
288
+ const lines = content.split(/\r?\n/);
289
+ const terminatedIds = new Set();
290
+ const allEvents = [];
291
+
292
+ // First pass: collect
293
+ for (let i = 0; i < lines.length; i++) {
294
+ const line = lines[i].trim();
295
+ if (!line) continue;
296
+ let obj;
297
+ try {
298
+ obj = JSON.parse(line);
299
+ } catch (_) {
300
+ // Orchestrator JSONL lines can be complex; skip silently
301
+ continue;
302
+ }
303
+ if (!obj || typeof obj !== 'object') continue;
304
+ allEvents.push(obj);
305
+ }
306
+
307
+ // The orchestrator JSONL stores conversation turns. Each line is an object with
308
+ // a `message` field containing a Claude API message (role: "assistant" | "user").
309
+ // Tool uses are in assistant messages: message.content[].type === "tool_use".
310
+ // Tool results are in user messages: message.content[].type === "tool_result".
311
+
312
+ // First pass: collect all termination signals from tool_result blocks
313
+ for (const obj of allEvents) {
314
+ // Direct termination events (from GSD-T event hooks)
315
+ if (obj.type === 'monitor_stopped' && obj.tool_use_id) {
316
+ terminatedIds.add(obj.tool_use_id);
317
+ }
318
+ if (obj.type === 'spawn_completed' && obj.tool_use_id) {
319
+ terminatedIds.add(obj.tool_use_id);
320
+ }
321
+
322
+ // Claude API user turns: message.content[].type === "tool_result"
323
+ const msg = obj.message;
324
+ if (msg && msg.role === 'user' && Array.isArray(msg.content)) {
325
+ for (const block of msg.content) {
326
+ if (block && block.type === 'tool_result' && block.tool_use_id) {
327
+ terminatedIds.add(block.tool_use_id);
328
+ }
329
+ }
330
+ }
331
+ }
332
+
333
+ // Second pass: collect tool_use blocks from assistant messages
334
+ const toolUseBlocks = []; // { block, timestamp }
335
+ for (const obj of allEvents) {
336
+ const msg = obj.message;
337
+ if (!msg || msg.role !== 'assistant' || !Array.isArray(msg.content)) continue;
338
+ // Timestamp: prefer the message-level timestamp if available
339
+ const timestamp = obj.timestamp || (msg && msg.created_at) || null;
340
+ for (const block of msg.content) {
341
+ if (block && block.type === 'tool_use' && block.id) {
342
+ toolUseBlocks.push({ block, timestamp });
343
+ }
344
+ }
345
+ }
346
+
347
+ // Third pass: emit activities for non-terminated tool uses
348
+ for (const { block, timestamp } of toolUseBlocks) {
349
+ const toolUseId = block.id;
350
+ if (terminatedIds.has(toolUseId)) continue;
351
+
352
+ const name = block.name || '';
353
+ const startedAt = timestamp || now.toISOString();
354
+ const startMs = _safeDate(startedAt);
355
+ const ageMs = startMs ? now.getTime() - startMs.getTime() : 0;
356
+
357
+ if (name === 'Bash') {
358
+ const isBg = block.input && block.input.run_in_background === true;
359
+ if (isBg) {
360
+ const cmd = (block.input && block.input.command) || name;
361
+ activities.push(_makeActivity({
362
+ id: toolUseId,
363
+ kind: 'bash',
364
+ label: cmd,
365
+ startedAt,
366
+ now,
367
+ toolUseId,
368
+ }));
369
+ }
370
+ } else if (name === 'Monitor') {
371
+ activities.push(_makeActivity({
372
+ id: toolUseId,
373
+ kind: 'monitor',
374
+ label: (block.input && (block.input.command || block.input.path)) || name,
375
+ startedAt,
376
+ now,
377
+ toolUseId,
378
+ }));
379
+ } else if (ageMs > TOOL_THRESHOLD_MS) {
380
+ activities.push(_makeActivity({
381
+ id: toolUseId,
382
+ kind: 'tool',
383
+ label: name || 'tool',
384
+ startedAt,
385
+ now,
386
+ toolUseId,
387
+ }));
388
+ }
389
+ }
390
+
391
+ return activities;
392
+ }
393
+
394
+ /**
395
+ * Read spawn kind activities from .gsd-t/spawns/*.json plan files.
396
+ * Delegates to the same plan-file reader shape as parallelism-report.cjs.
397
+ */
398
+ function _readSpawnActivities(projectDir, now, notes) {
399
+ const activities = [];
400
+ const spawnPlans = _readSpawnPlans(projectDir, notes);
401
+
402
+ for (const plan of spawnPlans) {
403
+ // Only include active (not ended) plans
404
+ if (plan.endedAt !== null && plan.endedAt !== undefined) continue;
405
+
406
+ const startedAt = plan.startedAt || now.toISOString();
407
+ const label = (plan.spawnId || plan.kind || 'spawn') + (plan.command ? ' ' + String(plan.command).slice(0, 30) : '');
408
+ const id = plan.spawnId || _makeId('spawn', label, startedAt);
409
+
410
+ activities.push(_makeActivity({
411
+ id,
412
+ kind: 'spawn',
413
+ label,
414
+ startedAt,
415
+ now,
416
+ pid: plan.pid || plan.workerPid,
417
+ toolUseId: plan.tool_use_id,
418
+ }));
419
+ }
420
+
421
+ return activities;
422
+ }
423
+
424
+ /**
425
+ * Read .gsd-t/spawns/*.json plan files. Mirrors parallelism-report.cjs's _readSpawnPlans.
426
+ */
427
+ function _readSpawnPlans(projectDir, notes) {
428
+ const dir = path.join(projectDir, '.gsd-t', 'spawns');
429
+ let files;
430
+ try {
431
+ if (!fs.existsSync(dir)) return [];
432
+ files = fs.readdirSync(dir).filter((n) => n.endsWith('.json'));
433
+ } catch (err) {
434
+ notes.push('spawns dir unreadable: ' + (err && err.message || err));
435
+ return [];
436
+ }
437
+ const plans = [];
438
+ for (const f of files) {
439
+ const full = path.join(dir, f);
440
+ let raw;
441
+ try {
442
+ raw = fs.readFileSync(full, 'utf8');
443
+ } catch (err) {
444
+ notes.push('could not read spawn plan ' + full + ': ' + (err && err.message || err));
445
+ continue;
446
+ }
447
+ let parsed;
448
+ try {
449
+ parsed = JSON.parse(raw);
450
+ } catch (_) {
451
+ notes.push('spawn-plan malformed JSON: ' + f);
452
+ continue;
453
+ }
454
+ if (!parsed || typeof parsed !== 'object') {
455
+ notes.push('spawn-plan not an object: ' + f);
456
+ continue;
457
+ }
458
+ plans.push(parsed);
459
+ }
460
+ return plans;
461
+ }
462
+
463
+ /**
464
+ * Deduplicate activities by tool_use_id (priority 1) then (kind, label, startedAt) tuple (priority 2).
465
+ */
466
+ function _dedup(activities) {
467
+ const byToolUseId = new Map();
468
+ const byTuple = new Map();
469
+ const result = [];
470
+
471
+ for (const a of activities) {
472
+ // Priority 1: deduplicate by tool_use_id
473
+ if (a.toolUseId) {
474
+ if (byToolUseId.has(a.toolUseId)) {
475
+ // Merge: prefer orchestrator pid when present
476
+ const existing = byToolUseId.get(a.toolUseId);
477
+ if (a.pid && !existing.pid) existing.pid = a.pid;
478
+ continue;
479
+ }
480
+ byToolUseId.set(a.toolUseId, a);
481
+ result.push(a);
482
+ continue;
483
+ }
484
+
485
+ // Priority 2: deduplicate by (kind, label, startedAt) tuple
486
+ const tupleKey = a.kind + '|' + a.label + '|' + a.startedAt;
487
+ if (byTuple.has(tupleKey)) {
488
+ const existing = byTuple.get(tupleKey);
489
+ if (a.pid && !existing.pid) existing.pid = a.pid;
490
+ continue;
491
+ }
492
+ byTuple.set(tupleKey, a);
493
+ result.push(a);
494
+ }
495
+
496
+ return result;
497
+ }
498
+
499
+ /**
500
+ * Apply all 3 liveness falsifiers to an activity.
501
+ * Returns true if the activity should remain in activities[].
502
+ * Returns false if any falsifier fires.
503
+ */
504
+ function _isLive(activity, eventsFile, eventsMtime, now, notes) {
505
+ // F1: explicit terminating event (already handled during parsing — entries with
506
+ // matching tool_result/monitor_stopped/spawn_completed were never added)
507
+ // This is a double-check for spawn kind which reads from a different source.
508
+ if (activity.kind === 'spawn') {
509
+ // For spawn kind, F1 is already handled in _readSpawnActivities (endedAt check).
510
+ // No additional F1 check needed here for spawns.
511
+ }
512
+
513
+ // F2: PID check — process.kill(pid, 0) throws ESRCH
514
+ if (activity.pid) {
515
+ try {
516
+ process.kill(activity.pid, 0);
517
+ // Process is alive (no error thrown)
518
+ } catch (err) {
519
+ if (err && err.code === 'ESRCH') {
520
+ // Process not found — remove entry
521
+ return false;
522
+ }
523
+ // EPERM or other: conservative, treat as dead
524
+ notes.push('PID check error for pid ' + activity.pid + ': ' + (err && err.code || String(err)));
525
+ return false;
526
+ }
527
+ }
528
+
529
+ // F3: Source-file mtime > 60s old
530
+ if (activity.kind === 'spawn') {
531
+ // Spawn activities use spawn plan file mtime, not events file
532
+ // Spawn plan mtime > 60s → stale
533
+ const spawnMtime = activity._spawnPlanMtime;
534
+ if (spawnMtime) {
535
+ const ageMs = now.getTime() - spawnMtime;
536
+ if (ageMs > MTIME_STALE_MS) return false;
537
+ }
538
+ } else {
539
+ // Events-sourced activities: check events file mtime
540
+ if (eventsMtime) {
541
+ const ageMs = now.getTime() - eventsMtime;
542
+ if (ageMs > MTIME_STALE_MS) return false;
543
+ }
544
+ }
545
+
546
+ return true;
547
+ }
548
+
549
+ /**
550
+ * Safely stat a file and return its mtime in milliseconds, or null on error.
551
+ */
552
+ function _safeFileMtime(filePath) {
553
+ try {
554
+ return fs.statSync(filePath).mtimeMs;
555
+ } catch (_) {
556
+ return null;
557
+ }
558
+ }
559
+
560
+ /**
561
+ * Construct an Activity object.
562
+ */
563
+ function _makeActivity({ id, kind, label, startedAt, now, pid, toolUseId, _spawnPlanMtime }) {
564
+ const startMs = _safeDate(startedAt);
565
+ const durationMs = startMs ? Math.max(0, now.getTime() - startMs.getTime()) : 0;
566
+ const activity = {
567
+ id: String(id),
568
+ kind,
569
+ label: String(label),
570
+ startedAt: startMs ? startMs.toISOString() : new Date(now).toISOString(),
571
+ durationMs,
572
+ tailUrl: '/api/live-activity/' + encodeURIComponent(String(id)) + '/tail',
573
+ alive: true,
574
+ };
575
+ if (pid != null) activity.pid = pid;
576
+ if (toolUseId) activity.toolUseId = toolUseId;
577
+ if (_spawnPlanMtime != null) activity._spawnPlanMtime = _spawnPlanMtime;
578
+ return activity;
579
+ }
580
+
581
+ /**
582
+ * Generate a deterministic ID from kind + label + startedAt when no tool_use_id is available.
583
+ */
584
+ function _makeId(kind, label, startedAt) {
585
+ const hash = crypto.createHash('sha1').update(label + startedAt).digest('hex').slice(0, 12);
586
+ return kind + ':' + hash;
587
+ }
588
+
589
+ /**
590
+ * Safely parse a date string. Returns null for invalid inputs.
591
+ */
592
+ function _safeDate(v) {
593
+ if (!v || typeof v !== 'string') return null;
594
+ const d = new Date(v);
595
+ if (isNaN(d.getTime())) return null;
596
+ return d;
597
+ }
598
+
599
+ // ── Module exports ──────────────────────────────────────────────────────────
600
+
601
+ module.exports = {
602
+ computeLiveActivities,
603
+ SCHEMA_VERSION,
604
+ // Exposed for unit tests only; not part of the public contract.
605
+ _readEventsActivities,
606
+ _readOrchestratorActivities,
607
+ _readSpawnActivities,
608
+ _readSpawnPlans,
609
+ _dedup,
610
+ _isLive,
611
+ _makeActivity,
612
+ _makeId,
613
+ _safeDate,
614
+ _safeFileMtime,
615
+ };