@tekyzinc/gsd-t 3.29.11 → 4.0.14

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 (114) hide show
  1. package/CHANGELOG.md +171 -0
  2. package/bin/gsd-t-parallel.cjs +64 -13
  3. package/bin/gsd-t.js +48 -376
  4. package/bin/journey-coverage.cjs +6 -3
  5. package/bin/parallel-cli.cjs +13 -1
  6. package/commands/gsd-t-complete-milestone.md +6 -12
  7. package/commands/gsd-t-debug.md +47 -533
  8. package/commands/gsd-t-design-decompose.md +24 -497
  9. package/commands/gsd-t-doc-ripple.md +23 -139
  10. package/commands/gsd-t-execute.md +41 -958
  11. package/commands/gsd-t-feature.md +2 -6
  12. package/commands/gsd-t-gap-analysis.md +3 -6
  13. package/commands/gsd-t-help.md +2 -4
  14. package/commands/gsd-t-impact.md +26 -295
  15. package/commands/gsd-t-init-scan-setup.md +7 -7
  16. package/commands/gsd-t-integrate.md +44 -369
  17. package/commands/gsd-t-milestone.md +25 -124
  18. package/commands/gsd-t-partition.md +28 -539
  19. package/commands/gsd-t-plan.md +26 -472
  20. package/commands/gsd-t-prd.md +25 -311
  21. package/commands/gsd-t-quick.md +1 -1
  22. package/commands/gsd-t-resume.md +0 -33
  23. package/commands/gsd-t-scan.md +45 -652
  24. package/commands/gsd-t-verify.md +52 -569
  25. package/commands/gsd-t-wave.md +41 -430
  26. package/package.json +1 -1
  27. package/scripts/gsd-t-calibration-hook.js +3 -1
  28. package/scripts/hooks/pre-commit-journey-coverage +0 -2
  29. package/scripts/hooks/pre-commit-playwright-gate +0 -2
  30. package/templates/CLAUDE-global.md +44 -173
  31. package/templates/CLAUDE-project.md +118 -104
  32. package/templates/prompts/design-verify-subagent.md +3 -0
  33. package/templates/prompts/qa-subagent.md +3 -0
  34. package/templates/prompts/red-team-subagent.md +5 -2
  35. package/templates/workflows/_lib.js +176 -0
  36. package/templates/workflows/gsd-t-debug.workflow.js +86 -0
  37. package/templates/workflows/gsd-t-execute.workflow.js +206 -0
  38. package/templates/workflows/gsd-t-integrate.workflow.js +71 -0
  39. package/templates/workflows/gsd-t-phase.workflow.js +94 -0
  40. package/templates/workflows/gsd-t-quick.workflow.js +72 -0
  41. package/templates/workflows/gsd-t-scan.workflow.js +711 -0
  42. package/templates/workflows/gsd-t-verify.workflow.js +348 -0
  43. package/templates/workflows/gsd-t-wave.workflow.js +43 -0
  44. package/bin/check-headless-sessions.js +0 -140
  45. package/bin/context-budget-audit.cjs +0 -447
  46. package/bin/context-meter-config.cjs +0 -101
  47. package/bin/context-meter-config.test.cjs +0 -101
  48. package/bin/event-stream.cjs +0 -205
  49. package/bin/gsd-t-benchmark-orchestrator.js +0 -437
  50. package/bin/gsd-t-capture-lint.cjs +0 -440
  51. package/bin/gsd-t-economics.cjs +0 -315
  52. package/bin/gsd-t-in-session-usage.cjs +0 -213
  53. package/bin/gsd-t-orchestrator-config.cjs +0 -161
  54. package/bin/gsd-t-orchestrator-queue.cjs +0 -180
  55. package/bin/gsd-t-orchestrator-recover.cjs +0 -231
  56. package/bin/gsd-t-orchestrator-worker.cjs +0 -219
  57. package/bin/gsd-t-orchestrator.js +0 -535
  58. package/bin/gsd-t-parallel-probe.cjs +0 -132
  59. package/bin/gsd-t-ratelimit-probe-worker.cjs +0 -237
  60. package/bin/gsd-t-ratelimit-probe.cjs +0 -648
  61. package/bin/gsd-t-report-tokens.cjs +0 -549
  62. package/bin/gsd-t-stream-feed-client.cjs +0 -151
  63. package/bin/gsd-t-token-backfill.cjs +0 -366
  64. package/bin/gsd-t-token-capture.cjs +0 -321
  65. package/bin/gsd-t-token-dashboard.cjs +0 -353
  66. package/bin/gsd-t-token-regenerate-log.cjs +0 -129
  67. package/bin/gsd-t-tool-attribution.cjs +0 -377
  68. package/bin/gsd-t-tool-cost.cjs +0 -195
  69. package/bin/gsd-t-transcript-tee.cjs +0 -246
  70. package/bin/gsd-t-unattended-heartbeat.cjs +0 -188
  71. package/bin/gsd-t-unattended-platform.cjs +0 -551
  72. package/bin/gsd-t-unattended-platform.js +0 -381
  73. package/bin/gsd-t-unattended-safety.cjs +0 -773
  74. package/bin/gsd-t-unattended-safety.js +0 -788
  75. package/bin/gsd-t-unattended.cjs +0 -2109
  76. package/bin/gsd-t-unattended.js +0 -1367
  77. package/bin/gsd-t-worker-dispatch.cjs +0 -211
  78. package/bin/handoff-lock.cjs +0 -249
  79. package/bin/handoff-lock.js +0 -249
  80. package/bin/headless-auto-spawn.cjs +0 -711
  81. package/bin/headless-auto-spawn.js +0 -373
  82. package/bin/headless-exit-codes.cjs +0 -67
  83. package/bin/live-activity-report.cjs +0 -615
  84. package/bin/log-tail.cjs +0 -81
  85. package/bin/m44-proof-measure.cjs +0 -285
  86. package/bin/m46-iter-proof.cjs +0 -149
  87. package/bin/m46-worker-proof.cjs +0 -201
  88. package/bin/m55-substrate-proof.cjs +0 -134
  89. package/bin/metrics-rollup.js +0 -200
  90. package/bin/model-windows.cjs +0 -99
  91. package/bin/model-windows.test.cjs +0 -75
  92. package/bin/parallelism-report.cjs +0 -535
  93. package/bin/runway-estimator.cjs +0 -242
  94. package/bin/spawn-plan-derive.cjs +0 -163
  95. package/bin/spawn-plan-status-updater.cjs +0 -292
  96. package/bin/spawn-plan-writer.cjs +0 -204
  97. package/bin/supervisor-pid-fingerprint.cjs +0 -126
  98. package/bin/token-budget.cjs +0 -265
  99. package/bin/unattended-watch-format.cjs +0 -178
  100. package/bin/watch-progress.js +0 -155
  101. package/commands/gsd-t-unattended-stop.md +0 -85
  102. package/commands/gsd-t-unattended-watch.md +0 -465
  103. package/commands/gsd-t-unattended.md +0 -476
  104. package/commands/gsd-t-visualize.md +0 -116
  105. package/scripts/gsd-t-context-meter.e2e.test.js +0 -364
  106. package/scripts/gsd-t-context-meter.js +0 -341
  107. package/scripts/gsd-t-context-meter.test.js +0 -471
  108. package/scripts/gsd-t-dashboard-server.js +0 -1203
  109. package/scripts/gsd-t-post-commit-spawn-plan.sh +0 -86
  110. package/scripts/gsd-t-transcript.html +0 -1821
  111. package/scripts/hooks/gsd-t-conversation-capture.js +0 -439
  112. package/scripts/hooks/gsd-t-in-session-usage-hook.js +0 -84
  113. package/scripts/spawn-plan-fmt-tokens.cjs +0 -80
  114. package/templates/hooks/post-commit-spawn-plan.sh +0 -85
@@ -1,321 +0,0 @@
1
- 'use strict';
2
- /**
3
- * GSD-T Token Capture Wrapper (M41 D1)
4
- *
5
- * Single reusable module every GSD-T spawn call site uses. Parses the
6
- * `usage` envelope from Claude's result frame, appends a row to
7
- * `.gsd-t/token-log.md`, and appends a schema-v1 JSONL record to
8
- * `.gsd-t/metrics/token-usage.jsonl`.
9
- *
10
- * Zero external deps. `.cjs` so it loads in both ESM-default projects
11
- * and CJS projects without transpilation.
12
- *
13
- * Contracts:
14
- * - .gsd-t/contracts/metrics-schema-contract.md (schema v1)
15
- * - .gsd-t/contracts/stream-json-sink-contract.md v1.1.0 (usage semantics)
16
- *
17
- * Missing `usage` → write `—` in Tokens column. Never `0`, never `N/A`.
18
- */
19
-
20
- const fs = require('fs');
21
- const path = require('path');
22
-
23
- const SCHEMA_VERSION = 2;
24
-
25
- const NEW_HEADER = '| Datetime-start | Datetime-end | Command | Step | Model | Duration(s) | Tokens | Notes | Domain | Task | Ctx% |';
26
- const NEW_SEP = '|---|---|---|---|---|---|---|---|---|---|---|';
27
-
28
- function pad2(n) { return String(n).padStart(2, '0'); }
29
- function fmtDateTime(d) {
30
- return `${d.getFullYear()}-${pad2(d.getMonth()+1)}-${pad2(d.getDate())} ${pad2(d.getHours())}:${pad2(d.getMinutes())}`;
31
- }
32
-
33
- function _parseUsageFromResult(result) {
34
- if (!result || typeof result !== 'object') return undefined;
35
- if (result.usage && typeof result.usage === 'object') return result.usage;
36
- if (result.result && typeof result.result === 'object' && result.result.usage && typeof result.result.usage === 'object') {
37
- return result.result.usage;
38
- }
39
- return undefined;
40
- }
41
-
42
- function _formatTokensCell(usage) {
43
- if (!usage || typeof usage !== 'object') return '—';
44
- const inp = Number(usage.input_tokens || 0);
45
- const out = Number(usage.output_tokens || 0);
46
- const cr = Number(usage.cache_read_input_tokens || 0);
47
- const cc = Number(usage.cache_creation_input_tokens || 0);
48
- const costNum = (typeof usage.total_cost_usd === 'number') ? usage.total_cost_usd : (typeof usage.cost_usd === 'number' ? usage.cost_usd : null);
49
- const cost = (costNum == null) ? '—' : `$${costNum.toFixed(2)}`;
50
- if (!inp && !out && !cr && !cc && cost === '—') return '—';
51
- return `in=${inp} out=${out} cr=${cr} cc=${cc} ${cost}`;
52
- }
53
-
54
- function _ensureDir(p) {
55
- const dir = path.dirname(p);
56
- if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
57
- }
58
-
59
- function _ensureTokenLogHeader(tokenLogPath) {
60
- if (!fs.existsSync(tokenLogPath)) {
61
- _ensureDir(tokenLogPath);
62
- fs.writeFileSync(tokenLogPath, `# GSD-T Token Log\n\n${NEW_HEADER}\n${NEW_SEP}\n`);
63
- return;
64
- }
65
- const text = fs.readFileSync(tokenLogPath, 'utf8');
66
- if (text.includes(NEW_HEADER)) return;
67
- // Old header detection: first header line that starts with `| Datetime-start`
68
- const lines = text.split('\n');
69
- const headerIdx = lines.findIndex(l => /^\|\s*Datetime-start\s*\|/.test(l));
70
- if (headerIdx < 0) {
71
- // No header at all — append new one
72
- fs.writeFileSync(tokenLogPath, text.trimEnd() + `\n\n${NEW_HEADER}\n${NEW_SEP}\n`);
73
- return;
74
- }
75
- // Replace old header + separator with new header + separator, preserve existing rows
76
- const sepIdx = headerIdx + 1;
77
- lines[headerIdx] = NEW_HEADER;
78
- if (lines[sepIdx] && /^\|[\s\-|]+\|$/.test(lines[sepIdx])) {
79
- lines[sepIdx] = NEW_SEP;
80
- } else {
81
- lines.splice(sepIdx, 0, NEW_SEP);
82
- }
83
- fs.writeFileSync(tokenLogPath, lines.join('\n'));
84
- }
85
-
86
- function _appendTokenLogRow(tokenLogPath, row) {
87
- _ensureTokenLogHeader(tokenLogPath);
88
- const line = `| ${row.startedAt} | ${row.endedAt} | ${row.command} | ${row.step} | ${row.model} | ${row.durationSec}s | ${row.tokensCell} | ${row.notes} | ${row.domain} | ${row.task} | ${row.ctxPct} |\n`;
89
- fs.appendFileSync(tokenLogPath, line);
90
- }
91
-
92
- function _appendJsonlRecord(jsonlPath, record) {
93
- _ensureDir(jsonlPath);
94
- fs.appendFileSync(jsonlPath, JSON.stringify(record) + '\n');
95
- }
96
-
97
- function _buildJsonlRecord({ command, step, model, startedAt, endedAt, durationSec, usage, domain, task, notes, ctxPct, milestone, source, sessionId, turnId, sessionType, toolAttribution, compactionPressure, cw_id }) {
98
- const u = usage || {};
99
- const cost = (typeof u.total_cost_usd === 'number') ? u.total_cost_usd : (typeof u.cost_usd === 'number' ? u.cost_usd : null);
100
- const rec = {
101
- schemaVersion: SCHEMA_VERSION,
102
- ts: new Date().toISOString(),
103
- source: source || 'live',
104
- command,
105
- step,
106
- model,
107
- startedAt,
108
- endedAt,
109
- durationMs: durationSec * 1000,
110
- inputTokens: Number(u.input_tokens || 0),
111
- outputTokens: Number(u.output_tokens || 0),
112
- cacheReadInputTokens: Number(u.cache_read_input_tokens || 0),
113
- cacheCreationInputTokens: Number(u.cache_creation_input_tokens || 0),
114
- costUSD: cost,
115
- domain: domain || null,
116
- task: task || null,
117
- milestone: milestone || null,
118
- ctxPct: ctxPct == null ? null : ctxPct,
119
- notes: notes || null,
120
- hasUsage: !!usage,
121
- };
122
- if (sessionId != null) rec.session_id = String(sessionId);
123
- if (turnId != null) rec.turn_id = String(turnId);
124
- if (sessionType != null) rec.sessionType = sessionType;
125
- if (Array.isArray(toolAttribution) && toolAttribution.length) rec.tool_attribution = toolAttribution;
126
- if (compactionPressure && typeof compactionPressure === 'object') rec.compaction_pressure = compactionPressure;
127
- // M44 D7 v2.1.0: optional per-Context-Window attribution key.
128
- // Omitted (not null, not "") when the caller does not supply it, so
129
- // pre-D7 callers continue to produce byte-identical rows.
130
- if (cw_id != null && cw_id !== '') rec.cw_id = String(cw_id);
131
- return rec;
132
- }
133
-
134
- function _inferMilestone(projectDir) {
135
- try {
136
- const progress = fs.readFileSync(path.join(projectDir, '.gsd-t', 'progress.md'), 'utf8');
137
- const m = /\*\*(M\d+)\b/.exec(progress) || /## Status:\s*(M\d+)/.exec(progress);
138
- return m ? m[1] : null;
139
- } catch (_) { return null; }
140
- }
141
-
142
- function _resolveCtxPct(projectDir) {
143
- try {
144
- const tb = require('./token-budget.cjs');
145
- const s = tb.getSessionStatus(projectDir);
146
- return (s && typeof s.pct === 'number') ? s.pct : (s && s.pct) || 'N/A';
147
- } catch (_) { return 'N/A'; }
148
- }
149
-
150
- function _parseStartedAt(s) {
151
- if (!s) return Date.now();
152
- if (/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/.test(s)) {
153
- const [d, t] = s.split(' ');
154
- return new Date(`${d}T${t}:00`).getTime();
155
- }
156
- const parsed = Date.parse(s);
157
- return Number.isFinite(parsed) ? parsed : Date.now();
158
- }
159
-
160
- /**
161
- * Record a single spawn row to both token-log.md and token-usage.jsonl.
162
- *
163
- * @param {object} opts
164
- * @param {string} opts.projectDir
165
- * @param {string} opts.command e.g. 'gsd-t-execute'
166
- * @param {string} opts.step e.g. 'Step 4'
167
- * @param {string} opts.model e.g. 'sonnet'
168
- * @param {string} opts.startedAt 'YYYY-MM-DD HH:MM'
169
- * @param {string} opts.endedAt 'YYYY-MM-DD HH:MM'
170
- * @param {object} [opts.usage] Claude usage envelope; undefined → '—'
171
- * @param {string} [opts.domain]
172
- * @param {string} [opts.task]
173
- * @param {string|number} [opts.ctxPct]
174
- * @param {string} [opts.notes]
175
- * @param {'live'|'backfill'} [opts.source]
176
- * @param {string} [opts.sessionId] v2 — stable session identifier
177
- * @param {string|number} [opts.turnId] v2 — per-turn identifier within sessionId
178
- * @param {'in-session'|'headless'} [opts.sessionType] v2 — channel classifier
179
- * @param {Array} [opts.toolAttribution] v2 — D2 joiner output; usually omitted by spawn callers
180
- * @param {object} [opts.compactionPressure] v2 — D5 runway snapshot; usually omitted by spawn callers
181
- * @param {string} [opts.cw_id] v2.1.0 — per-Context-Window attribution key (M44 D7).
182
- * Pass-through only; the wrapper does not derive it.
183
- * Omitted from the row when absent (NOT null, NOT "").
184
- * @returns {{tokenLogPath: string, jsonlPath: string}}
185
- */
186
- function recordSpawnRow(opts) {
187
- const projectDir = opts.projectDir || '.';
188
- const tokenLogPath = opts.tokenLogPath || path.join(projectDir, '.gsd-t', 'token-log.md');
189
- const jsonlPath = opts.jsonlPath || path.join(projectDir, '.gsd-t', 'metrics', 'token-usage.jsonl');
190
-
191
- const startMs = _parseStartedAt(opts.startedAt);
192
- const endMs = _parseStartedAt(opts.endedAt);
193
- const durationSec = Math.max(0, Math.round((endMs - startMs) / 1000));
194
-
195
- const tokensCell = _formatTokensCell(opts.usage);
196
- const notes = (opts.notes == null || opts.notes === '') ? '-' : String(opts.notes).replace(/\|/g, '\\|');
197
- const domain = (opts.domain == null || opts.domain === '') ? '-' : String(opts.domain);
198
- const task = (opts.task == null || opts.task === '') ? '-' : String(opts.task);
199
- const ctxPct = (opts.ctxPct == null) ? 'N/A' : String(opts.ctxPct);
200
-
201
- // skipMarkdownLog: JSONL is canonical (D3). The markdown log is a legacy view
202
- // kept in sync for human-readable tailing; high-frequency producers (D1
203
- // per-turn in-session rows, D2 joiner) should write JSONL-only and rely on
204
- // `gsd-t tokens --regenerate-log` for the markdown rendering.
205
- if (!opts.skipMarkdownLog) {
206
- _appendTokenLogRow(tokenLogPath, {
207
- startedAt: opts.startedAt,
208
- endedAt: opts.endedAt,
209
- command: opts.command,
210
- step: opts.step,
211
- model: opts.model,
212
- durationSec,
213
- tokensCell,
214
- notes,
215
- domain,
216
- task,
217
- ctxPct,
218
- });
219
- }
220
-
221
- const milestone = opts.milestone || _inferMilestone(projectDir);
222
- _appendJsonlRecord(jsonlPath, _buildJsonlRecord({
223
- command: opts.command,
224
- step: opts.step,
225
- model: opts.model,
226
- startedAt: opts.startedAt,
227
- endedAt: opts.endedAt,
228
- durationSec,
229
- usage: opts.usage,
230
- domain: opts.domain || null,
231
- task: opts.task || null,
232
- notes: opts.notes || null,
233
- ctxPct: opts.ctxPct == null ? null : opts.ctxPct,
234
- milestone,
235
- source: opts.source || 'live',
236
- sessionId: opts.sessionId,
237
- turnId: opts.turnId,
238
- sessionType: opts.sessionType,
239
- toolAttribution: opts.toolAttribution,
240
- compactionPressure: opts.compactionPressure,
241
- cw_id: opts.cw_id,
242
- }));
243
-
244
- return { tokenLogPath, jsonlPath };
245
- }
246
-
247
- /**
248
- * Wrap an async spawn callable; capture timing + usage + write the row.
249
- *
250
- * @param {object} opts
251
- * @param {string} opts.command
252
- * @param {string} opts.step
253
- * @param {string} opts.model
254
- * @param {string} opts.description
255
- * @param {string} [opts.projectDir='.']
256
- * @param {() => Promise<any>} opts.spawnFn
257
- * @param {string} [opts.domain]
258
- * @param {string} [opts.task]
259
- * @returns {Promise<{result: any, usage: any|undefined, rowWritten: {tokenLogPath, jsonlPath}}>}
260
- */
261
- async function captureSpawn(opts) {
262
- if (!opts || typeof opts.spawnFn !== 'function') {
263
- throw new Error('captureSpawn: spawnFn is required and must be a function');
264
- }
265
- const projectDir = opts.projectDir || '.';
266
- const startDate = new Date();
267
- const startedAt = fmtDateTime(startDate);
268
- const startMs = startDate.getTime();
269
-
270
- // Visible banner before the spawn fires
271
- process.stdout.write(`⚙ [${opts.model}] ${opts.command} → ${opts.description}\n`);
272
-
273
- let result, caught;
274
- try {
275
- result = await opts.spawnFn();
276
- } catch (err) {
277
- caught = err;
278
- }
279
-
280
- const endDate = new Date();
281
- const endedAt = fmtDateTime(endDate);
282
-
283
- const usage = caught ? undefined : _parseUsageFromResult(result);
284
- const ctxPct = _resolveCtxPct(projectDir);
285
- const notes = caught ? `spawn_error: ${String(caught && caught.message || caught).slice(0, 200)}` : (opts.notes || '-');
286
-
287
- const rowWritten = recordSpawnRow({
288
- projectDir,
289
- command: opts.command,
290
- step: opts.step,
291
- model: opts.model,
292
- startedAt,
293
- endedAt,
294
- usage,
295
- domain: opts.domain,
296
- task: opts.task,
297
- ctxPct,
298
- notes,
299
- sessionId: opts.sessionId,
300
- turnId: opts.turnId,
301
- sessionType: opts.sessionType,
302
- toolAttribution: opts.toolAttribution,
303
- compactionPressure: opts.compactionPressure,
304
- cw_id: opts.cw_id,
305
- });
306
-
307
- if (caught) throw caught;
308
- return { result, usage, rowWritten };
309
- }
310
-
311
- module.exports = {
312
- captureSpawn,
313
- recordSpawnRow,
314
- _parseUsageFromResult,
315
- _formatTokensCell,
316
- _ensureTokenLogHeader,
317
- _buildJsonlRecord,
318
- SCHEMA_VERSION,
319
- NEW_HEADER,
320
- NEW_SEP,
321
- };
@@ -1,353 +0,0 @@
1
- 'use strict';
2
- /**
3
- * GSD-T Token Dashboard (M41 D4)
4
- *
5
- * Cumulative historical view of `.gsd-t/metrics/token-usage.jsonl`.
6
- * Feeds the `gsd-t tokens` CLI and the token-block tail of `gsd-t status`.
7
- *
8
- * Zero external deps. `.cjs` for ESM/CJS compat.
9
- */
10
-
11
- const fs = require('fs');
12
- const path = require('path');
13
- const readline = require('readline');
14
-
15
- const DEFAULT_JSONL_PATH = (projectDir) => path.join(projectDir, '.gsd-t', 'metrics', 'token-usage.jsonl');
16
-
17
- function _safeParse(line) {
18
- try { return JSON.parse(line); } catch (_) { return null; }
19
- }
20
-
21
- function _day(startedAt) {
22
- if (!startedAt) return 'unknown';
23
- // YYYY-MM-DD slice works for both ISO and our 'YYYY-MM-DD HH:MM' format.
24
- return String(startedAt).slice(0, 10);
25
- }
26
-
27
- function _cost(r) {
28
- return (typeof r.costUSD === 'number' && r.costUSD >= 0) ? r.costUSD : 0;
29
- }
30
-
31
- /**
32
- * Aggregate token-usage records.
33
- *
34
- * @param {object} opts
35
- * @param {string} opts.projectDir
36
- * @param {string} [opts.since] YYYY-MM-DD (inclusive)
37
- * @param {string} [opts.milestone] e.g. 'M41'
38
- * @returns {Promise<object>}
39
- */
40
- async function aggregate(opts) {
41
- const projectDir = opts.projectDir || '.';
42
- const jsonlPath = opts.jsonlPath || DEFAULT_JSONL_PATH(projectDir);
43
- const sinceDay = opts.since || null;
44
- const milestone = opts.milestone || null;
45
-
46
- const agg = {
47
- totalRecords: 0,
48
- totalCostUSD: 0,
49
- totalInputTokens: 0,
50
- totalOutputTokens: 0,
51
- totalCacheReadTokens: 0,
52
- totalCacheCreateTokens: 0,
53
- byDay: {},
54
- byCommand: {},
55
- byModel: {},
56
- topSpawns: [],
57
- rolling7d: { days: 0, totalCostUSD: 0, dailyAvgUSD: 0, monthlyProjectionUSD: 0 },
58
- currentMilestone: milestone,
59
- source: jsonlPath,
60
- };
61
-
62
- if (!fs.existsSync(jsonlPath)) {
63
- return agg;
64
- }
65
-
66
- const rs = fs.createReadStream(jsonlPath, { encoding: 'utf8' });
67
- const rl = readline.createInterface({ input: rs, crlfDelay: Infinity });
68
-
69
- const rawRecords = [];
70
-
71
- for await (const line of rl) {
72
- const trimmed = line && line.trim();
73
- if (!trimmed) continue;
74
- const r = _safeParse(trimmed);
75
- if (!r || typeof r !== 'object') continue;
76
-
77
- const day = _day(r.startedAt);
78
- if (sinceDay && day < sinceDay) continue;
79
- if (milestone && r.milestone && r.milestone !== milestone) continue;
80
- if (milestone && !r.milestone) continue;
81
-
82
- rawRecords.push(r);
83
-
84
- agg.totalRecords += 1;
85
- agg.totalCostUSD += _cost(r);
86
- agg.totalInputTokens += Number(r.inputTokens || 0);
87
- agg.totalOutputTokens += Number(r.outputTokens || 0);
88
- agg.totalCacheReadTokens += Number(r.cacheReadInputTokens || 0);
89
- agg.totalCacheCreateTokens += Number(r.cacheCreationInputTokens || 0);
90
-
91
- // byDay
92
- if (!agg.byDay[day]) agg.byDay[day] = { day, records: 0, costUSD: 0, inputTokens: 0, outputTokens: 0 };
93
- const d = agg.byDay[day];
94
- d.records += 1;
95
- d.costUSD += _cost(r);
96
- d.inputTokens += Number(r.inputTokens || 0);
97
- d.outputTokens += Number(r.outputTokens || 0);
98
-
99
- // byCommand
100
- const cmd = r.command || 'unknown';
101
- if (!agg.byCommand[cmd]) agg.byCommand[cmd] = { command: cmd, records: 0, costUSD: 0, inputTokens: 0, outputTokens: 0 };
102
- const c = agg.byCommand[cmd];
103
- c.records += 1;
104
- c.costUSD += _cost(r);
105
- c.inputTokens += Number(r.inputTokens || 0);
106
- c.outputTokens += Number(r.outputTokens || 0);
107
-
108
- // byModel
109
- const m = r.model || 'unknown';
110
- if (!agg.byModel[m]) agg.byModel[m] = { model: m, records: 0, costUSD: 0, inputTokens: 0, cacheReadTokens: 0, cacheHitRate: 0 };
111
- const mm = agg.byModel[m];
112
- mm.records += 1;
113
- mm.costUSD += _cost(r);
114
- mm.inputTokens += Number(r.inputTokens || 0);
115
- mm.cacheReadTokens += Number(r.cacheReadInputTokens || 0);
116
- }
117
-
118
- // Top 10 spawns by cost desc
119
- agg.topSpawns = rawRecords
120
- .slice()
121
- .sort((a, b) => _cost(b) - _cost(a))
122
- .slice(0, 10)
123
- .map((r) => ({
124
- startedAt: r.startedAt,
125
- command: r.command,
126
- step: r.step,
127
- model: r.model,
128
- costUSD: _cost(r),
129
- inputTokens: r.inputTokens || 0,
130
- outputTokens: r.outputTokens || 0,
131
- }));
132
-
133
- // Cache-hit rate per model
134
- for (const key of Object.keys(agg.byModel)) {
135
- const mm = agg.byModel[key];
136
- const denom = mm.inputTokens + mm.cacheReadTokens;
137
- mm.cacheHitRate = denom > 0 ? mm.cacheReadTokens / denom : 0;
138
- }
139
-
140
- // Rolling 7-day window by calendar day
141
- const now = Date.now();
142
- const sevenDaysMs = 7 * 24 * 60 * 60 * 1000;
143
- const cutoffMs = now - sevenDaysMs;
144
- let rollingCost = 0;
145
- const rollingDays = new Set();
146
- for (const r of rawRecords) {
147
- const ts = Date.parse(String(r.startedAt).replace(' ', 'T') + (r.startedAt && r.startedAt.length === 16 ? ':00Z' : ''));
148
- if (Number.isFinite(ts) && ts >= cutoffMs) {
149
- rollingCost += _cost(r);
150
- rollingDays.add(_day(r.startedAt));
151
- }
152
- }
153
- agg.rolling7d.days = rollingDays.size;
154
- agg.rolling7d.totalCostUSD = rollingCost;
155
- agg.rolling7d.dailyAvgUSD = rollingCost / 7;
156
- agg.rolling7d.monthlyProjectionUSD = agg.rolling7d.dailyAvgUSD * 30;
157
-
158
- return agg;
159
- }
160
-
161
- function _fmtMoney(n) {
162
- if (n == null || !Number.isFinite(n)) return '-';
163
- return `$${n.toFixed(2)}`;
164
- }
165
- function _fmtPct(n) {
166
- if (!Number.isFinite(n)) return '-';
167
- return `${(n * 100).toFixed(1)}%`;
168
- }
169
- function _fmtInt(n) {
170
- return String(Number(n || 0).toLocaleString('en-US'));
171
- }
172
-
173
- function renderTable(agg) {
174
- const lines = [];
175
- lines.push('═══ Token Dashboard ═══');
176
- lines.push(`Records: ${_fmtInt(agg.totalRecords)} | Total cost: ${_fmtMoney(agg.totalCostUSD)} | Input: ${_fmtInt(agg.totalInputTokens)} | Output: ${_fmtInt(agg.totalOutputTokens)}`);
177
- if (agg.currentMilestone) lines.push(`Milestone filter: ${agg.currentMilestone}`);
178
- lines.push('');
179
-
180
- lines.push('── By Day ──');
181
- const days = Object.values(agg.byDay).sort((a, b) => a.day.localeCompare(b.day));
182
- if (days.length === 0) lines.push(' (no data)');
183
- for (const d of days) {
184
- lines.push(` ${d.day} ${String(d.records).padStart(4)} spawns ${_fmtMoney(d.costUSD).padStart(8)} in=${_fmtInt(d.inputTokens)} out=${_fmtInt(d.outputTokens)}`);
185
- }
186
- lines.push('');
187
-
188
- lines.push('── By Command ──');
189
- const cmds = Object.values(agg.byCommand).sort((a, b) => b.costUSD - a.costUSD);
190
- if (cmds.length === 0) lines.push(' (no data)');
191
- for (const c of cmds) {
192
- lines.push(` ${c.command.padEnd(30)} ${String(c.records).padStart(4)} ${_fmtMoney(c.costUSD).padStart(8)}`);
193
- }
194
- lines.push('');
195
-
196
- lines.push('── By Model ──');
197
- const models = Object.values(agg.byModel).sort((a, b) => b.costUSD - a.costUSD);
198
- if (models.length === 0) lines.push(' (no data)');
199
- for (const m of models) {
200
- lines.push(` ${m.model.padEnd(20)} ${String(m.records).padStart(4)} ${_fmtMoney(m.costUSD).padStart(8)} cache-hit: ${_fmtPct(m.cacheHitRate)}`);
201
- }
202
- lines.push('');
203
-
204
- lines.push('── Top 10 Spawns by Cost ──');
205
- if (agg.topSpawns.length === 0) lines.push(' (no data)');
206
- for (let i = 0; i < agg.topSpawns.length; i++) {
207
- const s = agg.topSpawns[i];
208
- lines.push(` ${String(i + 1).padStart(2)}. ${s.startedAt} ${String(s.command || '').padEnd(22)} ${String(s.step || '').padEnd(14)} ${_fmtMoney(s.costUSD)}`);
209
- }
210
- lines.push('');
211
-
212
- lines.push('── Rolling 7-Day Projection ──');
213
- lines.push(` 7-day cost: ${_fmtMoney(agg.rolling7d.totalCostUSD)} across ${agg.rolling7d.days} day(s)`);
214
- lines.push(` Daily avg: ${_fmtMoney(agg.rolling7d.dailyAvgUSD)}`);
215
- lines.push(` Monthly projection (× 30): ${_fmtMoney(agg.rolling7d.monthlyProjectionUSD)}`);
216
-
217
- return lines.join('\n');
218
- }
219
-
220
- function renderJson(agg) {
221
- return JSON.stringify(agg, null, 2);
222
- }
223
-
224
- /**
225
- * Renders exactly 2 content lines + 1 separator = 3 output lines total.
226
- */
227
- function renderStatusBlock(agg) {
228
- const sep = '───';
229
- if (!agg || agg.totalRecords === 0) {
230
- return sep + '\nTokens: no data yet (run a command to populate)\n(run `gsd-t tokens` for full dashboard)';
231
- }
232
- const line1 = `Tokens: ${_fmtInt(agg.totalRecords)} spawns, ${_fmtMoney(agg.totalCostUSD)} total${agg.currentMilestone ? ` (${agg.currentMilestone})` : ''}`;
233
- const line2 = `Rolling 7d: ${_fmtMoney(agg.rolling7d.totalCostUSD)} (${_fmtMoney(agg.rolling7d.dailyAvgUSD)}/day → ${_fmtMoney(agg.rolling7d.monthlyProjectionUSD)}/mo proj.)`;
234
- return `${sep}\n${line1}\n${line2}`;
235
- }
236
-
237
- /**
238
- * Synchronous variant of aggregate() — for callers that need a blocking read
239
- * (e.g. the `gsd-t status` tail where we can't await). Uses fs.readFileSync
240
- * and shares the same record-processing loop. Safe for typical JSONL sizes
241
- * (<100k lines); prefer aggregate() for large files or background work.
242
- */
243
- function aggregateSync(opts) {
244
- const projectDir = opts.projectDir || '.';
245
- const jsonlPath = opts.jsonlPath || DEFAULT_JSONL_PATH(projectDir);
246
- const sinceDay = opts.since || null;
247
- const milestone = opts.milestone || null;
248
-
249
- const agg = {
250
- totalRecords: 0,
251
- totalCostUSD: 0,
252
- totalInputTokens: 0,
253
- totalOutputTokens: 0,
254
- totalCacheReadTokens: 0,
255
- totalCacheCreateTokens: 0,
256
- byDay: {},
257
- byCommand: {},
258
- byModel: {},
259
- topSpawns: [],
260
- rolling7d: { days: 0, totalCostUSD: 0, dailyAvgUSD: 0, monthlyProjectionUSD: 0 },
261
- currentMilestone: milestone,
262
- source: jsonlPath,
263
- };
264
-
265
- if (!fs.existsSync(jsonlPath)) return agg;
266
-
267
- const raw = fs.readFileSync(jsonlPath, 'utf8');
268
- const lines = raw.split('\n');
269
- const rawRecords = [];
270
-
271
- for (const line of lines) {
272
- const trimmed = line && line.trim();
273
- if (!trimmed) continue;
274
- const r = _safeParse(trimmed);
275
- if (!r || typeof r !== 'object') continue;
276
-
277
- const day = _day(r.startedAt);
278
- if (sinceDay && day < sinceDay) continue;
279
- if (milestone && r.milestone && r.milestone !== milestone) continue;
280
- if (milestone && !r.milestone) continue;
281
-
282
- rawRecords.push(r);
283
- agg.totalRecords += 1;
284
- agg.totalCostUSD += _cost(r);
285
- agg.totalInputTokens += Number(r.inputTokens || 0);
286
- agg.totalOutputTokens += Number(r.outputTokens || 0);
287
- agg.totalCacheReadTokens += Number(r.cacheReadInputTokens || 0);
288
- agg.totalCacheCreateTokens += Number(r.cacheCreationInputTokens || 0);
289
- }
290
-
291
- const now = Date.now();
292
- const cutoffMs = now - 7 * 24 * 60 * 60 * 1000;
293
- let rollingCost = 0;
294
- const rollingDays = new Set();
295
- for (const r of rawRecords) {
296
- const ts = Date.parse(String(r.startedAt).replace(' ', 'T') + (r.startedAt && r.startedAt.length === 16 ? ':00Z' : ''));
297
- if (Number.isFinite(ts) && ts >= cutoffMs) {
298
- rollingCost += _cost(r);
299
- rollingDays.add(_day(r.startedAt));
300
- }
301
- }
302
- agg.rolling7d.days = rollingDays.size;
303
- agg.rolling7d.totalCostUSD = rollingCost;
304
- agg.rolling7d.dailyAvgUSD = rollingCost / 7;
305
- agg.rolling7d.monthlyProjectionUSD = agg.rolling7d.dailyAvgUSD * 30;
306
-
307
- return agg;
308
- }
309
-
310
- /**
311
- * Render a "Top 10 tools by cost" section by invoking the D2 attribution
312
- * library and its CLI renderer. Used by `gsd-t tokens --show-tool-costs`.
313
- *
314
- * @param {object} opts
315
- * @param {string} opts.projectDir
316
- * @param {string} [opts.since]
317
- * @param {string} [opts.milestone]
318
- * @param {'table'|'json'} [opts.format]
319
- * @returns {string} rendered section (multi-line string)
320
- */
321
- function renderToolCostsSection(opts) {
322
- const projectDir = opts.projectDir || '.';
323
- const toolCost = require('./gsd-t-tool-cost.cjs');
324
- const attribution = require('./gsd-t-tool-attribution.cjs');
325
- const turnsPath = path.join(projectDir, '.gsd-t', 'metrics', 'token-usage.jsonl');
326
- const eventsGlob = path.join(projectDir, '.gsd-t', 'events');
327
- if (!fs.existsSync(turnsPath)) {
328
- return '── Top 10 tools by cost ──\n (no data)';
329
- }
330
- const joined = attribution.joinTurnsAndEvents({
331
- turnsPath,
332
- eventsGlob,
333
- since: opts.since || undefined,
334
- milestone: opts.milestone || undefined,
335
- });
336
- const agg = attribution.aggregateByTool(joined).slice(0, 10);
337
- if (opts.format === 'json') {
338
- return '\n── Top 10 tools by cost (JSON) ──\n' + agg.map((r) => JSON.stringify(r)).join('\n');
339
- }
340
- return '\n── Top 10 tools by cost ──\n' +
341
- toolCost.renderTable(agg, { groupBy: 'tool', since: opts.since, milestone: opts.milestone });
342
- }
343
-
344
- module.exports = {
345
- aggregate,
346
- aggregateSync,
347
- renderTable,
348
- renderJson,
349
- renderStatusBlock,
350
- renderToolCostsSection,
351
- _safeParse,
352
- _day,
353
- };