@tekyzinc/gsd-t 3.13.16 → 3.16.11
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/CHANGELOG.md +44 -0
- package/README.md +1 -0
- package/bin/gsd-t-benchmark-orchestrator.js +437 -0
- package/bin/gsd-t-capture-lint.cjs +276 -0
- package/bin/gsd-t-completion-check.cjs +106 -0
- package/bin/gsd-t-orchestrator-config.cjs +64 -0
- package/bin/gsd-t-orchestrator-queue.cjs +180 -0
- package/bin/gsd-t-orchestrator-recover.cjs +231 -0
- package/bin/gsd-t-orchestrator-worker.cjs +219 -0
- package/bin/gsd-t-orchestrator.js +534 -0
- package/bin/gsd-t-stream-feed-client.cjs +151 -0
- package/bin/gsd-t-task-brief-compactor.cjs +89 -0
- package/bin/gsd-t-task-brief-template.cjs +96 -0
- package/bin/gsd-t-task-brief.js +249 -0
- package/bin/gsd-t-token-backfill.cjs +366 -0
- package/bin/gsd-t-token-capture.cjs +306 -0
- package/bin/gsd-t-token-dashboard.cjs +318 -0
- package/bin/gsd-t-token-regenerate-log.cjs +129 -0
- package/bin/gsd-t-transcript-tee.cjs +246 -0
- package/bin/gsd-t-unattended-heartbeat.cjs +188 -0
- package/bin/gsd-t-unattended-platform.cjs +191 -27
- package/bin/gsd-t-unattended-safety.cjs +8 -1
- package/bin/gsd-t-unattended.cjs +192 -31
- package/bin/gsd-t.js +329 -2
- package/bin/supervisor-pid-fingerprint.cjs +126 -0
- package/commands/gsd-t-debug.md +63 -51
- package/commands/gsd-t-design-decompose.md +2 -7
- package/commands/gsd-t-doc-ripple.md +20 -11
- package/commands/gsd-t-execute.md +82 -50
- package/commands/gsd-t-integrate.md +43 -16
- package/commands/gsd-t-plan.md +20 -7
- package/commands/gsd-t-prd.md +19 -12
- package/commands/gsd-t-quick.md +64 -29
- package/commands/gsd-t-resume.md +51 -4
- package/commands/gsd-t-unattended.md +19 -20
- package/commands/gsd-t-verify.md +48 -32
- package/commands/gsd-t-visualize.md +19 -17
- package/commands/gsd-t-wave.md +29 -27
- package/docs/architecture.md +16 -0
- package/docs/m40-benchmark-report.md +35 -0
- package/docs/requirements.md +20 -0
- package/package.json +1 -1
- package/scripts/gsd-t-dashboard-server.js +291 -4
- package/scripts/gsd-t-dashboard.html +31 -1
- package/scripts/gsd-t-design-review-server.js +3 -1
- package/scripts/gsd-t-stream-feed-server.js +428 -0
- package/scripts/gsd-t-stream-feed.html +1168 -0
- package/scripts/gsd-t-token-aggregator.js +373 -0
- package/scripts/gsd-t-transcript.html +422 -0
- package/scripts/hooks/gsd-t-in-session-probe.js +62 -0
- package/scripts/hooks/pre-commit-capture-lint +26 -0
- package/templates/CLAUDE-global.md +69 -0
- package/scripts/gsd-t-agent-dashboard-server.js +0 -424
- package/scripts/gsd-t-agent-dashboard.html +0 -1043
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* GSD-T Historical Token Backfill (M41 D3)
|
|
4
|
+
*
|
|
5
|
+
* Walks past headless stream-json logs + event-stream JSONL and recovers
|
|
6
|
+
* `usage` envelopes for spawns that ran before M41 (when pre-M41 rows
|
|
7
|
+
* wrote `N/A` or `0` because no caller parsed `usage`).
|
|
8
|
+
*
|
|
9
|
+
* Idempotent: re-running produces the same JSONL line count as running
|
|
10
|
+
* once; backfill records are tagged `source: "backfill"` per schema v1.
|
|
11
|
+
*
|
|
12
|
+
* Zero external deps. `.cjs` for ESM/CJS compat.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const fs = require('fs');
|
|
16
|
+
const path = require('path');
|
|
17
|
+
|
|
18
|
+
const capture = require('./gsd-t-token-capture.cjs');
|
|
19
|
+
|
|
20
|
+
// ── Envelope parsing (assistant-vs-result precedence; inline fallback) ──
|
|
21
|
+
|
|
22
|
+
function _parseJsonLine(line) {
|
|
23
|
+
const s = String(line || '').trim();
|
|
24
|
+
if (!s || s[0] !== '{') return null;
|
|
25
|
+
try { return JSON.parse(s); } catch (_) { return null; }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function _pickUsageFromFrame(frame) {
|
|
29
|
+
if (!frame || typeof frame !== 'object') return undefined;
|
|
30
|
+
if (frame.usage && typeof frame.usage === 'object') return frame.usage;
|
|
31
|
+
if (frame.message && typeof frame.message === 'object' && frame.message.usage && typeof frame.message.usage === 'object') {
|
|
32
|
+
return frame.message.usage;
|
|
33
|
+
}
|
|
34
|
+
if (frame.result && typeof frame.result === 'object' && frame.result.usage && typeof frame.result.usage === 'object') {
|
|
35
|
+
return frame.result.usage;
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// ── Async log walker ──────────────────────────────────────────────────
|
|
41
|
+
|
|
42
|
+
function* _walkDir(dir) {
|
|
43
|
+
if (!fs.existsSync(dir)) return;
|
|
44
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
45
|
+
for (const e of entries) {
|
|
46
|
+
const p = path.join(dir, e.name);
|
|
47
|
+
if (e.isFile()) yield p;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function _listCandidateFiles(projectDir) {
|
|
52
|
+
const files = [];
|
|
53
|
+
const eventsDir = path.join(projectDir, '.gsd-t', 'events');
|
|
54
|
+
for (const f of _walkDir(eventsDir)) if (f.endsWith('.jsonl')) files.push(f);
|
|
55
|
+
const streamFeedDir = path.join(projectDir, '.gsd-t', 'stream-feed');
|
|
56
|
+
for (const f of _walkDir(streamFeedDir)) if (f.endsWith('.jsonl')) files.push(f);
|
|
57
|
+
const gsdDir = path.join(projectDir, '.gsd-t');
|
|
58
|
+
for (const f of _walkDir(gsdDir)) {
|
|
59
|
+
const base = path.basename(f);
|
|
60
|
+
if (base.startsWith('headless-') && (base.endsWith('.log') || base.endsWith('.jsonl'))) files.push(f);
|
|
61
|
+
}
|
|
62
|
+
return files;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function _mtime(filePath) {
|
|
66
|
+
try { return fs.statSync(filePath).mtimeMs; } catch (_) { return 0; }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function _parseSinceFilter(since) {
|
|
70
|
+
if (!since) return 0;
|
|
71
|
+
if (since instanceof Date) return since.getTime();
|
|
72
|
+
const ms = Date.parse(since);
|
|
73
|
+
return Number.isFinite(ms) ? ms : 0;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function pad2(n) { return String(n).padStart(2, '0'); }
|
|
77
|
+
function _fmtDateTime(ms) {
|
|
78
|
+
const d = new Date(ms);
|
|
79
|
+
if (isNaN(d.getTime())) return null;
|
|
80
|
+
return `${d.getFullYear()}-${pad2(d.getMonth()+1)}-${pad2(d.getDate())} ${pad2(d.getHours())}:${pad2(d.getMinutes())}`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Scan log files for spawn envelopes.
|
|
85
|
+
*
|
|
86
|
+
* @param {object} opts
|
|
87
|
+
* @param {string} opts.projectDir
|
|
88
|
+
* @param {string|Date} [opts.since]
|
|
89
|
+
* @returns {AsyncIterable<{envelope, sourceFile, startedAt, endedAt, command, step, model, raw}>}
|
|
90
|
+
*/
|
|
91
|
+
async function* scanLogs(opts) {
|
|
92
|
+
const projectDir = opts.projectDir || '.';
|
|
93
|
+
const sinceMs = _parseSinceFilter(opts.since);
|
|
94
|
+
const files = _listCandidateFiles(projectDir).filter((f) => _mtime(f) >= sinceMs);
|
|
95
|
+
|
|
96
|
+
for (const file of files) {
|
|
97
|
+
let text;
|
|
98
|
+
try { text = fs.readFileSync(file, 'utf8'); } catch (_) { continue; }
|
|
99
|
+
const lines = text.split('\n');
|
|
100
|
+
|
|
101
|
+
// Per-file scan state: track the most recent `init` frame so `result`
|
|
102
|
+
// frames inherit command/step/model/session context.
|
|
103
|
+
let ctx = { command: null, step: null, model: null, startedAt: null };
|
|
104
|
+
let firstSeenMs = null;
|
|
105
|
+
|
|
106
|
+
for (const line of lines) {
|
|
107
|
+
const frame = _parseJsonLine(line);
|
|
108
|
+
if (!frame) continue;
|
|
109
|
+
|
|
110
|
+
const frameTsMs = (typeof frame.ts === 'string' && Date.parse(frame.ts)) || null;
|
|
111
|
+
if (frameTsMs && firstSeenMs == null) firstSeenMs = frameTsMs;
|
|
112
|
+
|
|
113
|
+
// Event-stream frames (UserPromptSubmit, command_invoked, etc.)
|
|
114
|
+
if (frame.type === 'command_invoked' && frame.command) {
|
|
115
|
+
ctx.command = frame.command;
|
|
116
|
+
if (frameTsMs) ctx.startedAt = _fmtDateTime(frameTsMs);
|
|
117
|
+
}
|
|
118
|
+
if (frame.type === 'spawn' && typeof frame.data === 'object' && frame.data) {
|
|
119
|
+
ctx.command = frame.data.command || ctx.command;
|
|
120
|
+
ctx.step = frame.data.step || ctx.step;
|
|
121
|
+
ctx.model = frame.data.model || ctx.model;
|
|
122
|
+
if (frameTsMs) ctx.startedAt = _fmtDateTime(frameTsMs);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Stream-json init (headless)
|
|
126
|
+
if (frame.type === 'system' && frame.subtype === 'init') {
|
|
127
|
+
if (frame.model) ctx.model = frame.model;
|
|
128
|
+
if (frame.session_id && !ctx.step) ctx.step = '-';
|
|
129
|
+
if (frameTsMs && !ctx.startedAt) ctx.startedAt = _fmtDateTime(frameTsMs);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Result frame with usage → yield envelope
|
|
133
|
+
if (frame.type === 'result') {
|
|
134
|
+
const usage = _pickUsageFromFrame(frame);
|
|
135
|
+
if (!usage) continue;
|
|
136
|
+
const endedAtMs = frameTsMs || _mtime(file);
|
|
137
|
+
const startedAtFinal = ctx.startedAt || _fmtDateTime(firstSeenMs || endedAtMs) || '';
|
|
138
|
+
const endedAt = _fmtDateTime(endedAtMs) || startedAtFinal;
|
|
139
|
+
yield {
|
|
140
|
+
envelope: usage,
|
|
141
|
+
sourceFile: file,
|
|
142
|
+
startedAt: startedAtFinal,
|
|
143
|
+
endedAt,
|
|
144
|
+
command: ctx.command || _inferCommandFromPath(file),
|
|
145
|
+
step: ctx.step || '-',
|
|
146
|
+
model: ctx.model || frame.model || 'sonnet',
|
|
147
|
+
raw: frame,
|
|
148
|
+
};
|
|
149
|
+
// Reset per-result context so multi-spawn logs don't cross-contaminate
|
|
150
|
+
ctx = { command: ctx.command, step: null, model: ctx.model, startedAt: null };
|
|
151
|
+
firstSeenMs = null;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Spawn-result event-stream frames carry the envelope directly
|
|
155
|
+
if (frame.type === 'spawn_result' && frame.data && frame.data.usage) {
|
|
156
|
+
const endedAtMs = frameTsMs || _mtime(file);
|
|
157
|
+
const startedAtFinal = (frame.data.startedAt) || _fmtDateTime(endedAtMs) || '';
|
|
158
|
+
const endedAt = (frame.data.endedAt) || _fmtDateTime(endedAtMs) || startedAtFinal;
|
|
159
|
+
yield {
|
|
160
|
+
envelope: frame.data.usage,
|
|
161
|
+
sourceFile: file,
|
|
162
|
+
startedAt: startedAtFinal,
|
|
163
|
+
endedAt,
|
|
164
|
+
command: frame.data.command || ctx.command || _inferCommandFromPath(file),
|
|
165
|
+
step: frame.data.step || ctx.step || '-',
|
|
166
|
+
model: frame.data.model || ctx.model || 'sonnet',
|
|
167
|
+
raw: frame,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function _inferCommandFromPath(file) {
|
|
175
|
+
const base = path.basename(file);
|
|
176
|
+
const m = /^headless-(gsd-t-[a-z-]+)-/.exec(base);
|
|
177
|
+
return m ? m[1] : 'gsd-t-unknown';
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// ── Matcher + writer ─────────────────────────────────────────────────
|
|
181
|
+
|
|
182
|
+
function _loadExistingJsonl(jsonlPath) {
|
|
183
|
+
if (!fs.existsSync(jsonlPath)) return [];
|
|
184
|
+
const text = fs.readFileSync(jsonlPath, 'utf8');
|
|
185
|
+
return text.split('\n').filter(Boolean).map((l) => {
|
|
186
|
+
try { return JSON.parse(l); } catch (_) { return null; }
|
|
187
|
+
}).filter(Boolean);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function _indexKey(startedAt, command, step, model) {
|
|
191
|
+
return `${startedAt}|${command}|${step}|${model}`;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function _parseTokenLogRows(text) {
|
|
195
|
+
// Returns array of {line, idx, startedAt, endedAt, command, step, model, tokensCell}
|
|
196
|
+
const lines = text.split('\n');
|
|
197
|
+
const out = [];
|
|
198
|
+
for (let i = 0; i < lines.length; i++) {
|
|
199
|
+
const l = lines[i];
|
|
200
|
+
if (!l.startsWith('| ')) continue;
|
|
201
|
+
if (/^\|\s*Datetime-start\s*\|/.test(l)) continue;
|
|
202
|
+
if (/^\|[\s\-|]+\|\s*$/.test(l)) continue;
|
|
203
|
+
const cols = l.split('|').slice(1, -1).map((c) => c.trim());
|
|
204
|
+
if (cols.length < 7) continue;
|
|
205
|
+
out.push({
|
|
206
|
+
line: l,
|
|
207
|
+
idx: i,
|
|
208
|
+
startedAt: cols[0],
|
|
209
|
+
endedAt: cols[1],
|
|
210
|
+
command: cols[2],
|
|
211
|
+
step: cols[3],
|
|
212
|
+
model: cols[4],
|
|
213
|
+
duration: cols[5],
|
|
214
|
+
tokensCell: cols[6],
|
|
215
|
+
notes: cols[7] || '-',
|
|
216
|
+
domain: cols[8] || '-',
|
|
217
|
+
task: cols[9] || '-',
|
|
218
|
+
ctxPct: cols[10] || 'N/A',
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
return { lines, rows: out };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function _writeAtomic(filePath, content) {
|
|
225
|
+
const tmp = `${filePath}.tmp-${process.pid}-${Date.now()}`;
|
|
226
|
+
fs.writeFileSync(tmp, content);
|
|
227
|
+
fs.renameSync(tmp, filePath);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Match envelopes against token-log.md + token-usage.jsonl.
|
|
232
|
+
*
|
|
233
|
+
* @param {object} opts
|
|
234
|
+
* @param {string} opts.projectDir
|
|
235
|
+
* @param {AsyncIterable|Array} opts.envelopes
|
|
236
|
+
* @param {boolean} [opts.patchLog]
|
|
237
|
+
* @param {boolean} [opts.dryRun]
|
|
238
|
+
* @returns {Promise<{scanned, parsed, matched, patched, new: number, unmatched}>}
|
|
239
|
+
*/
|
|
240
|
+
async function matchAndWrite(opts) {
|
|
241
|
+
const projectDir = opts.projectDir || '.';
|
|
242
|
+
const tokenLogPath = path.join(projectDir, '.gsd-t', 'token-log.md');
|
|
243
|
+
const jsonlPath = path.join(projectDir, '.gsd-t', 'metrics', 'token-usage.jsonl');
|
|
244
|
+
const patchLog = !!opts.patchLog;
|
|
245
|
+
const dryRun = !!opts.dryRun;
|
|
246
|
+
|
|
247
|
+
const counters = { scanned: 0, parsed: 0, matched: 0, patched: 0, new: 0, unmatched: 0 };
|
|
248
|
+
|
|
249
|
+
const existing = _loadExistingJsonl(jsonlPath);
|
|
250
|
+
const existingKeys = new Set();
|
|
251
|
+
for (const r of existing) {
|
|
252
|
+
if (r && r.source === 'backfill') {
|
|
253
|
+
existingKeys.add(_indexKey(r.startedAt, r.command, r.step, r.model));
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
let tokenLogText = '';
|
|
258
|
+
let parsedLog = { lines: [], rows: [] };
|
|
259
|
+
if (fs.existsSync(tokenLogPath)) {
|
|
260
|
+
tokenLogText = fs.readFileSync(tokenLogPath, 'utf8');
|
|
261
|
+
parsedLog = _parseTokenLogRows(tokenLogText);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Iterate envelopes
|
|
265
|
+
for await (const env of opts.envelopes) {
|
|
266
|
+
counters.parsed += 1;
|
|
267
|
+
const key = _indexKey(env.startedAt, env.command, env.step, env.model);
|
|
268
|
+
|
|
269
|
+
if (existingKeys.has(key)) continue;
|
|
270
|
+
|
|
271
|
+
// Try to match against a token-log.md row by (startedAt, command, step, model)
|
|
272
|
+
const matchRow = parsedLog.rows.find((r) =>
|
|
273
|
+
r.startedAt === env.startedAt &&
|
|
274
|
+
r.command === env.command &&
|
|
275
|
+
r.step === env.step &&
|
|
276
|
+
r.model === env.model
|
|
277
|
+
);
|
|
278
|
+
|
|
279
|
+
if (matchRow) {
|
|
280
|
+
counters.matched += 1;
|
|
281
|
+
const cellIsEmpty = !matchRow.tokensCell || matchRow.tokensCell === 'N/A' || matchRow.tokensCell === '0' || matchRow.tokensCell === '—';
|
|
282
|
+
if (cellIsEmpty && patchLog && !dryRun) {
|
|
283
|
+
const newCell = capture._formatTokensCell(env.envelope);
|
|
284
|
+
const cols = matchRow.line.split('|');
|
|
285
|
+
// Columns indexing: [empty, startedAt, endedAt, command, step, model, duration, tokens, ...]
|
|
286
|
+
cols[7] = ` ${newCell} `;
|
|
287
|
+
parsedLog.lines[matchRow.idx] = cols.join('|');
|
|
288
|
+
counters.patched += 1;
|
|
289
|
+
}
|
|
290
|
+
} else {
|
|
291
|
+
counters.unmatched += 1;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// Write JSONL record (backfill source) whether matched or unmatched
|
|
295
|
+
if (!dryRun) {
|
|
296
|
+
const durationMs = Math.max(0, (Date.parse(env.endedAt.replace(' ', 'T') + ':00') || 0) - (Date.parse(env.startedAt.replace(' ', 'T') + ':00') || 0));
|
|
297
|
+
const record = capture._buildJsonlRecord({
|
|
298
|
+
command: env.command,
|
|
299
|
+
step: env.step,
|
|
300
|
+
model: env.model,
|
|
301
|
+
startedAt: env.startedAt,
|
|
302
|
+
endedAt: env.endedAt,
|
|
303
|
+
durationSec: Math.round(durationMs / 1000),
|
|
304
|
+
usage: env.envelope,
|
|
305
|
+
domain: null,
|
|
306
|
+
task: null,
|
|
307
|
+
notes: matchRow ? 'backfill: matched original row' : 'backfill: no original row',
|
|
308
|
+
ctxPct: null,
|
|
309
|
+
milestone: null,
|
|
310
|
+
source: 'backfill',
|
|
311
|
+
});
|
|
312
|
+
if (!fs.existsSync(path.dirname(jsonlPath))) fs.mkdirSync(path.dirname(jsonlPath), { recursive: true });
|
|
313
|
+
fs.appendFileSync(jsonlPath, JSON.stringify(record) + '\n');
|
|
314
|
+
counters.new += 1;
|
|
315
|
+
existingKeys.add(key);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
counters.scanned = _listCandidateFiles(projectDir).length;
|
|
320
|
+
|
|
321
|
+
if (patchLog && !dryRun && counters.patched > 0) {
|
|
322
|
+
_writeAtomic(tokenLogPath, parsedLog.lines.join('\n'));
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
return counters;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function _printSummary(counters) {
|
|
329
|
+
const msg = `Scanned: ${counters.scanned} files | Parsed: ${counters.parsed} envelopes | Matched: ${counters.matched} | Patched: ${counters.patched} | New JSONL: ${counters.new} | Unmatched: ${counters.unmatched}`;
|
|
330
|
+
process.stdout.write(msg + '\n');
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* CLI entrypoint.
|
|
335
|
+
*
|
|
336
|
+
* @param {object} opts
|
|
337
|
+
* @returns {Promise<{counters, exitCode}>}
|
|
338
|
+
*/
|
|
339
|
+
async function main(opts) {
|
|
340
|
+
const projectDir = opts.projectDir || '.';
|
|
341
|
+
if (!fs.existsSync(projectDir)) {
|
|
342
|
+
process.stderr.write(`gsd-t backfill-tokens: project dir not found: ${projectDir}\n`);
|
|
343
|
+
return { counters: null, exitCode: 3 };
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const envelopes = scanLogs({ projectDir, since: opts.since });
|
|
347
|
+
const counters = await matchAndWrite({
|
|
348
|
+
projectDir,
|
|
349
|
+
envelopes,
|
|
350
|
+
patchLog: !!opts.patchLog,
|
|
351
|
+
dryRun: !!opts.dryRun,
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
_printSummary(counters);
|
|
355
|
+
return { counters, exitCode: 0 };
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
module.exports = {
|
|
359
|
+
scanLogs,
|
|
360
|
+
matchAndWrite,
|
|
361
|
+
main,
|
|
362
|
+
_parseJsonLine,
|
|
363
|
+
_pickUsageFromFrame,
|
|
364
|
+
_listCandidateFiles,
|
|
365
|
+
_parseTokenLogRows,
|
|
366
|
+
};
|
|
@@ -0,0 +1,306 @@
|
|
|
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 }) {
|
|
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
|
+
return rec;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function _inferMilestone(projectDir) {
|
|
131
|
+
try {
|
|
132
|
+
const progress = fs.readFileSync(path.join(projectDir, '.gsd-t', 'progress.md'), 'utf8');
|
|
133
|
+
const m = /\*\*(M\d+)\b/.exec(progress) || /## Status:\s*(M\d+)/.exec(progress);
|
|
134
|
+
return m ? m[1] : null;
|
|
135
|
+
} catch (_) { return null; }
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function _resolveCtxPct(projectDir) {
|
|
139
|
+
try {
|
|
140
|
+
const tb = require('./token-budget.cjs');
|
|
141
|
+
const s = tb.getSessionStatus(projectDir);
|
|
142
|
+
return (s && typeof s.pct === 'number') ? s.pct : (s && s.pct) || 'N/A';
|
|
143
|
+
} catch (_) { return 'N/A'; }
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function _parseStartedAt(s) {
|
|
147
|
+
if (!s) return Date.now();
|
|
148
|
+
if (/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/.test(s)) {
|
|
149
|
+
const [d, t] = s.split(' ');
|
|
150
|
+
return new Date(`${d}T${t}:00`).getTime();
|
|
151
|
+
}
|
|
152
|
+
const parsed = Date.parse(s);
|
|
153
|
+
return Number.isFinite(parsed) ? parsed : Date.now();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Record a single spawn row to both token-log.md and token-usage.jsonl.
|
|
158
|
+
*
|
|
159
|
+
* @param {object} opts
|
|
160
|
+
* @param {string} opts.projectDir
|
|
161
|
+
* @param {string} opts.command e.g. 'gsd-t-execute'
|
|
162
|
+
* @param {string} opts.step e.g. 'Step 4'
|
|
163
|
+
* @param {string} opts.model e.g. 'sonnet'
|
|
164
|
+
* @param {string} opts.startedAt 'YYYY-MM-DD HH:MM'
|
|
165
|
+
* @param {string} opts.endedAt 'YYYY-MM-DD HH:MM'
|
|
166
|
+
* @param {object} [opts.usage] Claude usage envelope; undefined → '—'
|
|
167
|
+
* @param {string} [opts.domain]
|
|
168
|
+
* @param {string} [opts.task]
|
|
169
|
+
* @param {string|number} [opts.ctxPct]
|
|
170
|
+
* @param {string} [opts.notes]
|
|
171
|
+
* @param {'live'|'backfill'} [opts.source]
|
|
172
|
+
* @param {string} [opts.sessionId] v2 — stable session identifier
|
|
173
|
+
* @param {string|number} [opts.turnId] v2 — per-turn identifier within sessionId
|
|
174
|
+
* @param {'in-session'|'headless'} [opts.sessionType] v2 — channel classifier
|
|
175
|
+
* @param {Array} [opts.toolAttribution] v2 — D2 joiner output; usually omitted by spawn callers
|
|
176
|
+
* @param {object} [opts.compactionPressure] v2 — D5 runway snapshot; usually omitted by spawn callers
|
|
177
|
+
* @returns {{tokenLogPath: string, jsonlPath: string}}
|
|
178
|
+
*/
|
|
179
|
+
function recordSpawnRow(opts) {
|
|
180
|
+
const projectDir = opts.projectDir || '.';
|
|
181
|
+
const tokenLogPath = opts.tokenLogPath || path.join(projectDir, '.gsd-t', 'token-log.md');
|
|
182
|
+
const jsonlPath = opts.jsonlPath || path.join(projectDir, '.gsd-t', 'metrics', 'token-usage.jsonl');
|
|
183
|
+
|
|
184
|
+
const startMs = _parseStartedAt(opts.startedAt);
|
|
185
|
+
const endMs = _parseStartedAt(opts.endedAt);
|
|
186
|
+
const durationSec = Math.max(0, Math.round((endMs - startMs) / 1000));
|
|
187
|
+
|
|
188
|
+
const tokensCell = _formatTokensCell(opts.usage);
|
|
189
|
+
const notes = (opts.notes == null || opts.notes === '') ? '-' : String(opts.notes).replace(/\|/g, '\\|');
|
|
190
|
+
const domain = (opts.domain == null || opts.domain === '') ? '-' : String(opts.domain);
|
|
191
|
+
const task = (opts.task == null || opts.task === '') ? '-' : String(opts.task);
|
|
192
|
+
const ctxPct = (opts.ctxPct == null) ? 'N/A' : String(opts.ctxPct);
|
|
193
|
+
|
|
194
|
+
_appendTokenLogRow(tokenLogPath, {
|
|
195
|
+
startedAt: opts.startedAt,
|
|
196
|
+
endedAt: opts.endedAt,
|
|
197
|
+
command: opts.command,
|
|
198
|
+
step: opts.step,
|
|
199
|
+
model: opts.model,
|
|
200
|
+
durationSec,
|
|
201
|
+
tokensCell,
|
|
202
|
+
notes,
|
|
203
|
+
domain,
|
|
204
|
+
task,
|
|
205
|
+
ctxPct,
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
const milestone = opts.milestone || _inferMilestone(projectDir);
|
|
209
|
+
_appendJsonlRecord(jsonlPath, _buildJsonlRecord({
|
|
210
|
+
command: opts.command,
|
|
211
|
+
step: opts.step,
|
|
212
|
+
model: opts.model,
|
|
213
|
+
startedAt: opts.startedAt,
|
|
214
|
+
endedAt: opts.endedAt,
|
|
215
|
+
durationSec,
|
|
216
|
+
usage: opts.usage,
|
|
217
|
+
domain: opts.domain || null,
|
|
218
|
+
task: opts.task || null,
|
|
219
|
+
notes: opts.notes || null,
|
|
220
|
+
ctxPct: opts.ctxPct == null ? null : opts.ctxPct,
|
|
221
|
+
milestone,
|
|
222
|
+
source: opts.source || 'live',
|
|
223
|
+
sessionId: opts.sessionId,
|
|
224
|
+
turnId: opts.turnId,
|
|
225
|
+
sessionType: opts.sessionType,
|
|
226
|
+
toolAttribution: opts.toolAttribution,
|
|
227
|
+
compactionPressure: opts.compactionPressure,
|
|
228
|
+
}));
|
|
229
|
+
|
|
230
|
+
return { tokenLogPath, jsonlPath };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Wrap an async spawn callable; capture timing + usage + write the row.
|
|
235
|
+
*
|
|
236
|
+
* @param {object} opts
|
|
237
|
+
* @param {string} opts.command
|
|
238
|
+
* @param {string} opts.step
|
|
239
|
+
* @param {string} opts.model
|
|
240
|
+
* @param {string} opts.description
|
|
241
|
+
* @param {string} [opts.projectDir='.']
|
|
242
|
+
* @param {() => Promise<any>} opts.spawnFn
|
|
243
|
+
* @param {string} [opts.domain]
|
|
244
|
+
* @param {string} [opts.task]
|
|
245
|
+
* @returns {Promise<{result: any, usage: any|undefined, rowWritten: {tokenLogPath, jsonlPath}}>}
|
|
246
|
+
*/
|
|
247
|
+
async function captureSpawn(opts) {
|
|
248
|
+
if (!opts || typeof opts.spawnFn !== 'function') {
|
|
249
|
+
throw new Error('captureSpawn: spawnFn is required and must be a function');
|
|
250
|
+
}
|
|
251
|
+
const projectDir = opts.projectDir || '.';
|
|
252
|
+
const startDate = new Date();
|
|
253
|
+
const startedAt = fmtDateTime(startDate);
|
|
254
|
+
const startMs = startDate.getTime();
|
|
255
|
+
|
|
256
|
+
// Visible banner before the spawn fires
|
|
257
|
+
process.stdout.write(`⚙ [${opts.model}] ${opts.command} → ${opts.description}\n`);
|
|
258
|
+
|
|
259
|
+
let result, caught;
|
|
260
|
+
try {
|
|
261
|
+
result = await opts.spawnFn();
|
|
262
|
+
} catch (err) {
|
|
263
|
+
caught = err;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const endDate = new Date();
|
|
267
|
+
const endedAt = fmtDateTime(endDate);
|
|
268
|
+
|
|
269
|
+
const usage = caught ? undefined : _parseUsageFromResult(result);
|
|
270
|
+
const ctxPct = _resolveCtxPct(projectDir);
|
|
271
|
+
const notes = caught ? `spawn_error: ${String(caught && caught.message || caught).slice(0, 200)}` : (opts.notes || '-');
|
|
272
|
+
|
|
273
|
+
const rowWritten = recordSpawnRow({
|
|
274
|
+
projectDir,
|
|
275
|
+
command: opts.command,
|
|
276
|
+
step: opts.step,
|
|
277
|
+
model: opts.model,
|
|
278
|
+
startedAt,
|
|
279
|
+
endedAt,
|
|
280
|
+
usage,
|
|
281
|
+
domain: opts.domain,
|
|
282
|
+
task: opts.task,
|
|
283
|
+
ctxPct,
|
|
284
|
+
notes,
|
|
285
|
+
sessionId: opts.sessionId,
|
|
286
|
+
turnId: opts.turnId,
|
|
287
|
+
sessionType: opts.sessionType,
|
|
288
|
+
toolAttribution: opts.toolAttribution,
|
|
289
|
+
compactionPressure: opts.compactionPressure,
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
if (caught) throw caught;
|
|
293
|
+
return { result, usage, rowWritten };
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
module.exports = {
|
|
297
|
+
captureSpawn,
|
|
298
|
+
recordSpawnRow,
|
|
299
|
+
_parseUsageFromResult,
|
|
300
|
+
_formatTokensCell,
|
|
301
|
+
_ensureTokenLogHeader,
|
|
302
|
+
_buildJsonlRecord,
|
|
303
|
+
SCHEMA_VERSION,
|
|
304
|
+
NEW_HEADER,
|
|
305
|
+
NEW_SEP,
|
|
306
|
+
};
|