@tekyzinc/gsd-t 3.29.11 → 4.0.12

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 (108) hide show
  1. package/CHANGELOG.md +146 -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-debug.md +47 -533
  7. package/commands/gsd-t-design-decompose.md +24 -497
  8. package/commands/gsd-t-doc-ripple.md +23 -139
  9. package/commands/gsd-t-execute.md +41 -958
  10. package/commands/gsd-t-help.md +0 -2
  11. package/commands/gsd-t-impact.md +26 -295
  12. package/commands/gsd-t-integrate.md +44 -369
  13. package/commands/gsd-t-milestone.md +25 -124
  14. package/commands/gsd-t-partition.md +28 -539
  15. package/commands/gsd-t-plan.md +26 -472
  16. package/commands/gsd-t-prd.md +25 -311
  17. package/commands/gsd-t-quick.md +1 -1
  18. package/commands/gsd-t-resume.md +0 -33
  19. package/commands/gsd-t-verify.md +52 -569
  20. package/commands/gsd-t-wave.md +41 -430
  21. package/package.json +1 -1
  22. package/scripts/gsd-t-calibration-hook.js +3 -1
  23. package/scripts/hooks/pre-commit-journey-coverage +0 -2
  24. package/scripts/hooks/pre-commit-playwright-gate +0 -2
  25. package/templates/CLAUDE-global.md +43 -173
  26. package/templates/CLAUDE-project.md +118 -104
  27. package/templates/prompts/design-verify-subagent.md +3 -0
  28. package/templates/prompts/qa-subagent.md +3 -0
  29. package/templates/prompts/red-team-subagent.md +5 -2
  30. package/templates/workflows/_lib.js +176 -0
  31. package/templates/workflows/gsd-t-debug.workflow.js +86 -0
  32. package/templates/workflows/gsd-t-execute.workflow.js +206 -0
  33. package/templates/workflows/gsd-t-integrate.workflow.js +71 -0
  34. package/templates/workflows/gsd-t-phase.workflow.js +94 -0
  35. package/templates/workflows/gsd-t-quick.workflow.js +72 -0
  36. package/templates/workflows/gsd-t-verify.workflow.js +348 -0
  37. package/templates/workflows/gsd-t-wave.workflow.js +43 -0
  38. package/bin/check-headless-sessions.js +0 -140
  39. package/bin/context-budget-audit.cjs +0 -447
  40. package/bin/context-meter-config.cjs +0 -101
  41. package/bin/context-meter-config.test.cjs +0 -101
  42. package/bin/event-stream.cjs +0 -205
  43. package/bin/gsd-t-benchmark-orchestrator.js +0 -437
  44. package/bin/gsd-t-capture-lint.cjs +0 -440
  45. package/bin/gsd-t-economics.cjs +0 -315
  46. package/bin/gsd-t-in-session-usage.cjs +0 -213
  47. package/bin/gsd-t-orchestrator-config.cjs +0 -161
  48. package/bin/gsd-t-orchestrator-queue.cjs +0 -180
  49. package/bin/gsd-t-orchestrator-recover.cjs +0 -231
  50. package/bin/gsd-t-orchestrator-worker.cjs +0 -219
  51. package/bin/gsd-t-orchestrator.js +0 -535
  52. package/bin/gsd-t-parallel-probe.cjs +0 -132
  53. package/bin/gsd-t-ratelimit-probe-worker.cjs +0 -237
  54. package/bin/gsd-t-ratelimit-probe.cjs +0 -648
  55. package/bin/gsd-t-report-tokens.cjs +0 -549
  56. package/bin/gsd-t-stream-feed-client.cjs +0 -151
  57. package/bin/gsd-t-token-backfill.cjs +0 -366
  58. package/bin/gsd-t-token-capture.cjs +0 -321
  59. package/bin/gsd-t-token-dashboard.cjs +0 -353
  60. package/bin/gsd-t-token-regenerate-log.cjs +0 -129
  61. package/bin/gsd-t-tool-attribution.cjs +0 -377
  62. package/bin/gsd-t-tool-cost.cjs +0 -195
  63. package/bin/gsd-t-transcript-tee.cjs +0 -246
  64. package/bin/gsd-t-unattended-heartbeat.cjs +0 -188
  65. package/bin/gsd-t-unattended-platform.cjs +0 -551
  66. package/bin/gsd-t-unattended-platform.js +0 -381
  67. package/bin/gsd-t-unattended-safety.cjs +0 -773
  68. package/bin/gsd-t-unattended-safety.js +0 -788
  69. package/bin/gsd-t-unattended.cjs +0 -2109
  70. package/bin/gsd-t-unattended.js +0 -1367
  71. package/bin/gsd-t-worker-dispatch.cjs +0 -211
  72. package/bin/handoff-lock.cjs +0 -249
  73. package/bin/handoff-lock.js +0 -249
  74. package/bin/headless-auto-spawn.cjs +0 -711
  75. package/bin/headless-auto-spawn.js +0 -373
  76. package/bin/headless-exit-codes.cjs +0 -67
  77. package/bin/live-activity-report.cjs +0 -615
  78. package/bin/log-tail.cjs +0 -81
  79. package/bin/m44-proof-measure.cjs +0 -285
  80. package/bin/m46-iter-proof.cjs +0 -149
  81. package/bin/m46-worker-proof.cjs +0 -201
  82. package/bin/m55-substrate-proof.cjs +0 -134
  83. package/bin/metrics-rollup.js +0 -200
  84. package/bin/model-windows.cjs +0 -99
  85. package/bin/model-windows.test.cjs +0 -75
  86. package/bin/parallelism-report.cjs +0 -535
  87. package/bin/runway-estimator.cjs +0 -242
  88. package/bin/spawn-plan-derive.cjs +0 -163
  89. package/bin/spawn-plan-status-updater.cjs +0 -292
  90. package/bin/spawn-plan-writer.cjs +0 -204
  91. package/bin/supervisor-pid-fingerprint.cjs +0 -126
  92. package/bin/token-budget.cjs +0 -265
  93. package/bin/unattended-watch-format.cjs +0 -178
  94. package/bin/watch-progress.js +0 -155
  95. package/commands/gsd-t-unattended-stop.md +0 -85
  96. package/commands/gsd-t-unattended-watch.md +0 -465
  97. package/commands/gsd-t-unattended.md +0 -476
  98. package/commands/gsd-t-visualize.md +0 -116
  99. package/scripts/gsd-t-context-meter.e2e.test.js +0 -364
  100. package/scripts/gsd-t-context-meter.js +0 -341
  101. package/scripts/gsd-t-context-meter.test.js +0 -471
  102. package/scripts/gsd-t-dashboard-server.js +0 -1203
  103. package/scripts/gsd-t-post-commit-spawn-plan.sh +0 -86
  104. package/scripts/gsd-t-transcript.html +0 -1821
  105. package/scripts/hooks/gsd-t-conversation-capture.js +0 -439
  106. package/scripts/hooks/gsd-t-in-session-usage-hook.js +0 -84
  107. package/scripts/spawn-plan-fmt-tokens.cjs +0 -80
  108. package/templates/hooks/post-commit-spawn-plan.sh +0 -85
@@ -1,648 +0,0 @@
1
- #!/usr/bin/env node
2
- 'use strict';
3
-
4
- /**
5
- * GSD-T Rate-Limit Probe — Sweep Runner (M55 D3)
6
- *
7
- * One-shot empirical measurement of the Claude account's parallelism
8
- * ceiling under realistic GSD-T spawn shape.
9
- *
10
- * Output: `.gsd-t/ratelimit-map.json` (canonical, schema-versioned 1.0.0).
11
- *
12
- * Sweep matrix:
13
- * parallel_workers ∈ {1, 2, 3, 4, 5, 6, 8}
14
- * context_tokens ∈ {10_000, 30_000, 60_000, 100_000}
15
- * runs_per_cell = 3
16
- * total_runs = 28 cells × 3 = 84
17
- *
18
- * + backoff probe (deliberately provoke 429 at workers=8 / 100k)
19
- * + steady-state probe (workers=3 / 30k for 5 minutes)
20
- *
21
- * Every spawn flows through `bin/gsd-t-token-capture.cjs::captureSpawn`.
22
- *
23
- * Contract: .gsd-t/contracts/ratelimit-map-contract.md v1.0.0 STABLE
24
- *
25
- * CLI:
26
- * node bin/gsd-t-ratelimit-probe.cjs --json [--quick] [--out PATH]
27
- * [--no-worktree] // run in cwd, skip throwaway worktree
28
- * [--steady-state-sec N] // override steady-state duration (debug)
29
- * [--max-cells N] // hard ceiling on cells (debug/abort)
30
- * [--max-token-budget N] // abort sweep if cumulative tokens exceed N
31
- * [--max-wall-clock-min N] // abort sweep if wall-clock exceeds N min
32
- * [--force-refresh] // (informational; no caching yet)
33
- *
34
- * Zero external deps (Node built-ins only).
35
- */
36
-
37
- const fs = require('fs');
38
- const path = require('path');
39
- const os = require('os');
40
- const crypto = require('crypto');
41
- const { spawn, spawnSync } = require('child_process');
42
-
43
- const SCHEMA_VERSION = '1.0.0';
44
-
45
- const SWEEP_WORKERS = [1, 2, 3, 4, 5, 6, 8];
46
- const SWEEP_CONTEXTS = [10000, 30000, 60000, 100000];
47
- const RUNS_PER_CELL = 3;
48
-
49
- const DECLARED_SAFE_P95_TTFT_MS = 8000;
50
- const STEADY_STATE_DEFAULT_SEC = 300; // 5 min
51
- const STEADY_STATE_SAMPLE_SEC = 30;
52
- const STEADY_STATE_WORKERS = 3;
53
- const STEADY_STATE_CONTEXT = 30000;
54
-
55
- const BACKOFF_TRIGGER_WORKERS = 8;
56
- const BACKOFF_TRIGGER_CONTEXT = 100000;
57
- const BACKOFF_TRIGGER_RUNS = 3;
58
- const BACKOFF_RECOVERY_SAMPLES = 12;
59
- const BACKOFF_RECOVERY_CADENCE_MS = 5000;
60
-
61
- const FIXTURES_DIR_REL = '.gsd-t/fixtures/ratelimit-probe';
62
- const WORKER_BIN_REL = 'bin/gsd-t-ratelimit-probe-worker.cjs';
63
-
64
- const DEFAULT_BACKOFF_MS = 30000;
65
-
66
- // ── Args ────────────────────────────────────────────────────────────────────
67
-
68
- function parseArgs(argv) {
69
- const args = {};
70
- for (let i = 2; i < argv.length; i++) {
71
- const a = argv[i];
72
- if (a.startsWith('--')) {
73
- const key = a.slice(2);
74
- const next = argv[i + 1];
75
- if (next != null && !next.startsWith('--')) {
76
- args[key] = next;
77
- i++;
78
- } else {
79
- args[key] = true;
80
- }
81
- }
82
- }
83
- return args;
84
- }
85
-
86
- // ── Pure helpers (exported for tests) ───────────────────────────────────────
87
-
88
- function buildSweepMatrix({ workers, contexts, runsPerCell }) {
89
- const cells = [];
90
- for (const w of workers) {
91
- for (const c of contexts) {
92
- cells.push({ workers: w, contextTokens: c, runsPerCell });
93
- }
94
- }
95
- return cells;
96
- }
97
-
98
- function percentile(sortedAsc, p) {
99
- if (!sortedAsc.length) return null;
100
- if (sortedAsc.length === 1) return sortedAsc[0];
101
- const rank = (p / 100) * (sortedAsc.length - 1);
102
- const lo = Math.floor(rank);
103
- const hi = Math.ceil(rank);
104
- if (lo === hi) return sortedAsc[lo];
105
- const frac = rank - lo;
106
- return sortedAsc[lo] + (sortedAsc[hi] - sortedAsc[lo]) * frac;
107
- }
108
-
109
- function summarizeCell(runs) {
110
- const ttfts = runs
111
- .map((r) => r.ttftMs)
112
- .filter((v) => typeof v === 'number' && Number.isFinite(v))
113
- .sort((a, b) => a - b);
114
- const total429 = runs.filter((r) => r && r.status429 === true).length;
115
- const p50 = ttfts.length ? Math.round(percentile(ttfts, 50)) : null;
116
- const p95 = ttfts.length ? Math.round(percentile(ttfts, 95)) : null;
117
- const declaredSafe = total429 === 0 && p95 !== null && p95 <= DECLARED_SAFE_P95_TTFT_MS;
118
- return {
119
- p50TtftMs: p50,
120
- p95TtftMs: p95,
121
- total429,
122
- declaredSafe,
123
- };
124
- }
125
-
126
- function maskAccount({ apiKey, oauthToken }) {
127
- if (apiKey) {
128
- const prefix = crypto.createHash('sha256').update(apiKey).digest('hex').slice(0, 16);
129
- return { account: prefix, authPath: 'api-key' };
130
- }
131
- if (oauthToken) {
132
- const prefix = crypto.createHash('sha256').update(oauthToken).digest('hex').slice(0, 16);
133
- return { account: 'oauth-' + prefix, authPath: 'oauth-claude-max' };
134
- }
135
- return { account: 'unknown', authPath: 'unknown' };
136
- }
137
-
138
- function deriveRecommended({ matrix, backoffProbe, steadyState }) {
139
- // peakConcurrency: highest workers value with declaredSafe=true at any context
140
- let peak = 1;
141
- let perWorker = 10000;
142
- for (const cell of matrix) {
143
- if (cell.summary && cell.summary.declaredSafe) {
144
- if (cell.workers > peak) peak = cell.workers;
145
- }
146
- }
147
- // perWorkerContextBudgetTokens: highest context where declaredSafe holds at peak
148
- for (const cell of matrix) {
149
- if (cell.summary && cell.summary.declaredSafe && cell.workers === peak) {
150
- if (cell.contextTokens > perWorker) perWorker = cell.contextTokens;
151
- }
152
- }
153
- // safeConcurrencyAt60kContext: highest workers value with declaredSafe at 60k
154
- let safe60 = 1;
155
- for (const cell of matrix) {
156
- if (cell.contextTokens === 60000 && cell.summary && cell.summary.declaredSafe) {
157
- if (cell.workers > safe60) safe60 = cell.workers;
158
- }
159
- }
160
- // backoffMs: median first-success tElapsedMs in recovery samples; else default
161
- let backoffMs = DEFAULT_BACKOFF_MS;
162
- if (backoffProbe && backoffProbe.trigger429Count === 0) {
163
- backoffMs = 0;
164
- } else if (backoffProbe && Array.isArray(backoffProbe.post429RecoverySamples)) {
165
- const firstOk = backoffProbe.post429RecoverySamples.find((s) => s && s.ok === true);
166
- if (firstOk && typeof firstOk.tElapsedMs === 'number') {
167
- backoffMs = firstOk.tElapsedMs;
168
- }
169
- }
170
- const steadyStatePass = !!(steadyState && steadyState.ok);
171
- return {
172
- safeConcurrencyAt60kContext: safe60,
173
- peakConcurrency: peak,
174
- perWorkerContextBudgetTokens: perWorker,
175
- backoffMs,
176
- steadyState3Workers5MinPass: steadyStatePass,
177
- };
178
- }
179
-
180
- // ── Credential discovery ────────────────────────────────────────────────────
181
-
182
- function tryReadOAuthToken() {
183
- // 1. file path (older installs)
184
- const fp = path.join(os.homedir(), '.claude', '.credentials.json');
185
- if (fs.existsSync(fp)) {
186
- try {
187
- const j = JSON.parse(fs.readFileSync(fp, 'utf8'));
188
- const t = j && j.claudeAiOauth && j.claudeAiOauth.accessToken;
189
- if (t) return { token: t, tier: j.claudeAiOauth.rateLimitTier || null };
190
- } catch (_) {}
191
- }
192
- // 2. macOS Keychain (newer Claude Code installs)
193
- if (process.platform === 'darwin') {
194
- try {
195
- const r = spawnSync('security',
196
- ['find-generic-password', '-s', 'Claude Code-credentials', '-a', os.userInfo().username, '-w'],
197
- { encoding: 'utf8', timeout: 5000 });
198
- if (r.status === 0 && r.stdout) {
199
- const raw = r.stdout.trim();
200
- try {
201
- const j = JSON.parse(raw);
202
- const t = j && j.claudeAiOauth && j.claudeAiOauth.accessToken;
203
- if (t) return { token: t, tier: j.claudeAiOauth.rateLimitTier || null };
204
- } catch (_) {}
205
- }
206
- } catch (_) {}
207
- }
208
- return { token: null, tier: null };
209
- }
210
-
211
- function detectClaudeCliVersion() {
212
- try {
213
- const r = spawnSync('claude', ['--version'], { encoding: 'utf8', timeout: 5000 });
214
- if (r.status === 0 && r.stdout) {
215
- const m = r.stdout.match(/(\d+\.\d+\.\d+)/);
216
- if (m) return m[1];
217
- }
218
- } catch (_) {}
219
- return 'unknown';
220
- }
221
-
222
- // ── Worktree management ─────────────────────────────────────────────────────
223
-
224
- function createWorktree(projectDir) {
225
- const wtBase = path.join(os.tmpdir(), 'gsd-t-ratelimit-probe-' + process.pid + '-' + Date.now());
226
- const r = spawnSync('git', ['-C', projectDir, 'worktree', 'add', '--detach', wtBase, 'HEAD'],
227
- { encoding: 'utf8', timeout: 30000 });
228
- if (r.status !== 0) {
229
- return { worktreeDir: null, error: 'worktree-add-failed: ' + (r.stderr || r.stdout || '').slice(0, 200) };
230
- }
231
- return { worktreeDir: wtBase, error: null };
232
- }
233
-
234
- function removeWorktree(projectDir, worktreeDir) {
235
- if (!worktreeDir) return;
236
- try {
237
- spawnSync('git', ['-C', projectDir, 'worktree', 'remove', '--force', worktreeDir],
238
- { encoding: 'utf8', timeout: 30000 });
239
- } catch (_) {}
240
- // Best-effort cleanup
241
- try {
242
- if (fs.existsSync(worktreeDir)) fs.rmSync(worktreeDir, { recursive: true, force: true });
243
- } catch (_) {}
244
- }
245
-
246
- // ── Single worker spawn ─────────────────────────────────────────────────────
247
-
248
- function spawnWorker({ workerBin, fixturePath, workerId, cellWorkers, cellContextTokens, runIdx, cwd }) {
249
- return new Promise((resolve) => {
250
- let stdout = '';
251
- let stderr = '';
252
- const child = spawn('node', [
253
- workerBin,
254
- '--fixture', fixturePath,
255
- '--worker-id', workerId,
256
- '--cell-workers', String(cellWorkers),
257
- '--cell-context-tokens', String(cellContextTokens),
258
- '--run-idx', String(runIdx),
259
- '--cwd', cwd,
260
- ], { stdio: ['ignore', 'pipe', 'pipe'] });
261
- child.stdout.on('data', (c) => { stdout += c.toString(); });
262
- child.stderr.on('data', (c) => { stderr += c.toString(); });
263
- child.on('exit', () => {
264
- // Worker emits one NDJSON line; parse first JSON-y line.
265
- let envelope = null;
266
- for (const line of stdout.split(/\r?\n/)) {
267
- const t = line.trim();
268
- if (!t) continue;
269
- try { envelope = JSON.parse(t); break; } catch (_) {}
270
- }
271
- if (!envelope) {
272
- envelope = {
273
- workerId, cellWorkers, cellContextTokens, runIdx,
274
- ttftMs: null, totalMs: 0, status429: false, retryAfterMs: null,
275
- exitCode: -1,
276
- failedReason: 'no-envelope: ' + (stderr || stdout || '').slice(0, 200),
277
- };
278
- }
279
- resolve(envelope);
280
- });
281
- child.on('error', (err) => {
282
- resolve({
283
- workerId, cellWorkers, cellContextTokens, runIdx,
284
- ttftMs: null, totalMs: 0, status429: false, retryAfterMs: null,
285
- exitCode: -1,
286
- failedReason: 'spawn-error: ' + (err && err.message || err),
287
- });
288
- });
289
- });
290
- }
291
-
292
- // ── Cell runner (parallel across cellWorkers) ───────────────────────────────
293
-
294
- async function runCell({ projectDir, workerBin, fixturePath, workers, contextTokens, runsPerCell, cwd, captureSpawn, cellLabel }) {
295
- const allRuns = [];
296
- for (let r = 0; r < runsPerCell; r++) {
297
- // Spawn `workers` children concurrently for this run
298
- const runPromises = [];
299
- for (let w = 0; w < workers; w++) {
300
- const workerId = 'w' + w + '-r' + r;
301
- const desc = cellLabel + ' worker=' + workerId;
302
- // captureSpawn wraps the worker spawn so token-log gets the row.
303
- const p = captureSpawn({
304
- command: 'gsd-t-ratelimit-probe',
305
- step: 'sweep',
306
- model: 'haiku',
307
- description: desc,
308
- projectDir,
309
- domain: 'm55-d3',
310
- task: 'T6-sweep',
311
- notes: 'cell w=' + workers + ' c=' + contextTokens + ' run=' + r,
312
- spawnFn: () => spawnWorker({
313
- workerBin, fixturePath, workerId,
314
- cellWorkers: workers, cellContextTokens: contextTokens, runIdx: r, cwd,
315
- }),
316
- }).then((wrapped) => wrapped && wrapped.result).catch((err) => ({
317
- workerId, cellWorkers: workers, cellContextTokens: contextTokens, runIdx: r,
318
- ttftMs: null, totalMs: 0, status429: false, retryAfterMs: null, exitCode: -1,
319
- failedReason: 'capture-throw: ' + (err && err.message || err),
320
- }));
321
- runPromises.push(p);
322
- }
323
- const runResults = await Promise.all(runPromises);
324
- allRuns.push(...runResults);
325
- }
326
- return allRuns;
327
- }
328
-
329
- // ── Backoff probe ───────────────────────────────────────────────────────────
330
-
331
- async function runBackoffProbe({ projectDir, workerBin, fixtureForContext, cwd, captureSpawn }) {
332
- const triggerFixture = fixtureForContext(BACKOFF_TRIGGER_CONTEXT);
333
- const triggerRuns = await runCell({
334
- projectDir, workerBin, fixturePath: triggerFixture,
335
- workers: BACKOFF_TRIGGER_WORKERS, contextTokens: BACKOFF_TRIGGER_CONTEXT,
336
- runsPerCell: BACKOFF_TRIGGER_RUNS, cwd, captureSpawn,
337
- cellLabel: 'backoff-trigger',
338
- });
339
- const trigger429Count = triggerRuns.filter((r) => r && r.status429 === true).length;
340
-
341
- // Recovery sampling: every 5s for 60s, single worker each time
342
- const samples = [];
343
- const sampleStart = Date.now();
344
- for (let i = 0; i < BACKOFF_RECOVERY_SAMPLES; i++) {
345
- await new Promise((r) => setTimeout(r, BACKOFF_RECOVERY_CADENCE_MS));
346
- const tElapsedMs = Date.now() - sampleStart;
347
- const fixturePath = fixtureForContext(10000); // small fixture for fast recovery probes
348
- const resultWrapped = await captureSpawn({
349
- command: 'gsd-t-ratelimit-probe',
350
- step: 'backoff-recover',
351
- model: 'haiku',
352
- description: 'backoff-recover sample=' + i,
353
- projectDir,
354
- domain: 'm55-d3',
355
- task: 'T6-backoff',
356
- notes: 'recovery sample at ' + tElapsedMs + 'ms',
357
- spawnFn: () => spawnWorker({
358
- workerBin, fixturePath,
359
- workerId: 'recover-' + i,
360
- cellWorkers: 1, cellContextTokens: 10000, runIdx: i, cwd,
361
- }),
362
- }).catch(() => ({ result: null }));
363
- const r = resultWrapped && resultWrapped.result;
364
- samples.push({
365
- tElapsedMs,
366
- ok: !!(r && !r.status429 && r.exitCode === 0 && r.ttftMs != null),
367
- ttftMs: r && r.ttftMs != null ? r.ttftMs : null,
368
- status429: !!(r && r.status429),
369
- });
370
- }
371
- return {
372
- triggerCell: { workers: BACKOFF_TRIGGER_WORKERS, contextTokens: BACKOFF_TRIGGER_CONTEXT, runs: BACKOFF_TRIGGER_RUNS },
373
- trigger429Count,
374
- triggerRuns,
375
- post429RecoverySamples: samples,
376
- };
377
- }
378
-
379
- // ── Steady-state probe ──────────────────────────────────────────────────────
380
-
381
- async function runSteadyState({ projectDir, workerBin, fixtureForContext, cwd, captureSpawn, durationSec }) {
382
- const fixturePath = fixtureForContext(STEADY_STATE_CONTEXT);
383
- const samples = [];
384
- const startMs = Date.now();
385
- let sampleIdx = 0;
386
- while ((Date.now() - startMs) / 1000 < durationSec) {
387
- sampleIdx++;
388
- const sampleStart = Date.now();
389
- const cellRuns = [];
390
- const cellPromises = [];
391
- for (let w = 0; w < STEADY_STATE_WORKERS; w++) {
392
- cellPromises.push(captureSpawn({
393
- command: 'gsd-t-ratelimit-probe',
394
- step: 'steady-state',
395
- model: 'haiku',
396
- description: 'steady-state s=' + sampleIdx + ' w=' + w,
397
- projectDir,
398
- domain: 'm55-d3',
399
- task: 'T6-steady',
400
- notes: 'sample idx ' + sampleIdx,
401
- spawnFn: () => spawnWorker({
402
- workerBin, fixturePath,
403
- workerId: 'ss-' + sampleIdx + '-w' + w,
404
- cellWorkers: STEADY_STATE_WORKERS, cellContextTokens: STEADY_STATE_CONTEXT, runIdx: sampleIdx, cwd,
405
- }),
406
- }).then((wr) => wr && wr.result).catch(() => null));
407
- }
408
- const all = await Promise.all(cellPromises);
409
- for (const r of all) if (r) cellRuns.push(r);
410
- const ttfts = cellRuns.map((r) => r.ttftMs).filter((v) => v != null).sort((a, b) => a - b);
411
- const ttftMedian = ttfts.length ? Math.round(percentile(ttfts, 50)) : null;
412
- const any429 = cellRuns.some((r) => r.status429);
413
- const tElapsedSec = Math.round((Date.now() - startMs) / 1000);
414
- samples.push({
415
- tElapsedSec,
416
- sampleDurationMs: Date.now() - sampleStart,
417
- workersOk: cellRuns.filter((r) => !r.status429 && r.exitCode === 0).length,
418
- ttftMs: ttftMedian,
419
- status429: any429,
420
- });
421
- // Respect the 30s sample cadence — wait the remainder of the window.
422
- const elapsedThisSample = Date.now() - sampleStart;
423
- const remaining = STEADY_STATE_SAMPLE_SEC * 1000 - elapsedThisSample;
424
- if (remaining > 0 && (Date.now() - startMs) / 1000 < durationSec) {
425
- await new Promise((r) => setTimeout(r, remaining));
426
- }
427
- }
428
- // Sustained ITPM/OTPM: rough — context_tokens × (workersOk/sample) / minute, output ≈ 1-token reply
429
- // We keep these conservative and informational; D2/D5 consume `recommended` not these directly.
430
- const totalSec = (Date.now() - startMs) / 1000;
431
- const totalIn = samples.reduce((a, s) => a + (s.workersOk || 0) * STEADY_STATE_CONTEXT, 0);
432
- const totalOut = samples.reduce((a, s) => a + (s.workersOk || 0) * 1, 0);
433
- const sustainedItpm = totalSec > 0 ? Math.round((totalIn / totalSec) * 60) : 0;
434
- const sustainedOtpm = totalSec > 0 ? Math.round((totalOut / totalSec) * 60) : 0;
435
- const ok = samples.length > 0 && samples.every((s) => !s.status429);
436
- return {
437
- workers: STEADY_STATE_WORKERS,
438
- contextTokens: STEADY_STATE_CONTEXT,
439
- durationSec,
440
- sampleCadenceSec: STEADY_STATE_SAMPLE_SEC,
441
- samples,
442
- sustainedItpm,
443
- sustainedOtpm,
444
- ok,
445
- };
446
- }
447
-
448
- // ── Main sweep ──────────────────────────────────────────────────────────────
449
-
450
- async function runFullSweep(opts) {
451
- const projectDir = opts.projectDir || process.cwd();
452
- const quick = !!opts.quick;
453
- const noWorktree = !!opts.noWorktree;
454
- const steadyStateSec = opts.steadyStateSec || STEADY_STATE_DEFAULT_SEC;
455
- const maxCells = opts.maxCells || (quick ? 1 : SWEEP_WORKERS.length * SWEEP_CONTEXTS.length);
456
- const maxTokenBudget = opts.maxTokenBudget || (quick ? 100000 : 250000);
457
- const maxWallClockMin = opts.maxWallClockMin || (quick ? 5 : 45);
458
-
459
- const workerBin = path.join(projectDir, WORKER_BIN_REL);
460
- if (!fs.existsSync(workerBin)) {
461
- throw new Error('worker bin not found: ' + workerBin);
462
- }
463
- const fixturesDir = path.join(projectDir, FIXTURES_DIR_REL);
464
- const fixtureForContext = (ctxTokens) => {
465
- return path.join(fixturesDir, 'context-' + (ctxTokens / 1000) + 'k.txt');
466
- };
467
-
468
- // Credential masking
469
- const oauth = tryReadOAuthToken();
470
- const apiKey = process.env.ANTHROPIC_API_KEY || null;
471
- const masked = maskAccount({ apiKey, oauthToken: oauth.token });
472
- const accountTier = (apiKey ? null : oauth.tier) || (process.env.GSD_T_ACCOUNT_TIER || 'unknown');
473
- const claudeCliVersion = detectClaudeCliVersion();
474
-
475
- // Worktree
476
- let worktreeDir = projectDir;
477
- let worktreeNote = null;
478
- if (!noWorktree && !quick) {
479
- const wt = createWorktree(projectDir);
480
- if (wt.error) {
481
- worktreeNote = 'worktree-fallback: ' + wt.error;
482
- worktreeDir = projectDir;
483
- } else {
484
- worktreeDir = wt.worktreeDir;
485
- }
486
- }
487
-
488
- // captureSpawn shim
489
- const { captureSpawn } = require(path.join(projectDir, 'bin', 'gsd-t-token-capture.cjs'));
490
-
491
- const matrix = [];
492
- const notes = [];
493
- if (worktreeNote) notes.push(worktreeNote);
494
- notes.push('authPath: ' + masked.authPath);
495
- notes.push('claudeCliVersion: ' + claudeCliVersion);
496
-
497
- const sweepStartMs = Date.now();
498
- const cells = quick
499
- ? [{ workers: 1, contextTokens: 10000, runsPerCell: 1 }]
500
- : buildSweepMatrix({ workers: SWEEP_WORKERS, contexts: SWEEP_CONTEXTS, runsPerCell: RUNS_PER_CELL });
501
-
502
- let cellsExecuted = 0;
503
- let abortReason = null;
504
- for (const cell of cells) {
505
- if (cellsExecuted >= maxCells) {
506
- abortReason = 'maxCells (' + maxCells + ') reached';
507
- break;
508
- }
509
- const wallClockMin = (Date.now() - sweepStartMs) / 60000;
510
- if (wallClockMin > maxWallClockMin) {
511
- abortReason = 'maxWallClockMin (' + maxWallClockMin + ') exceeded at ' + wallClockMin.toFixed(1) + 'min';
512
- break;
513
- }
514
- const fixturePath = fixtureForContext(cell.contextTokens);
515
- if (!fs.existsSync(fixturePath)) {
516
- const summary = { p50TtftMs: null, p95TtftMs: null, total429: 0, declaredSafe: false };
517
- matrix.push({
518
- workers: cell.workers, contextTokens: cell.contextTokens,
519
- runs: [{ runIdx: 0, ttftMs: null, totalMs: 0, status429: false, retryAfterMs: null, exitCode: -1, failedReason: 'fixture-missing: ' + fixturePath }],
520
- summary,
521
- });
522
- cellsExecuted++;
523
- continue;
524
- }
525
- const cellLabel = 'cell w=' + cell.workers + '/c=' + cell.contextTokens;
526
- process.stderr.write('[probe] ' + cellLabel + ' starting (' + cell.runsPerCell + ' runs)\n');
527
- let runs;
528
- try {
529
- runs = await runCell({
530
- projectDir, workerBin, fixturePath,
531
- workers: cell.workers, contextTokens: cell.contextTokens,
532
- runsPerCell: cell.runsPerCell, cwd: worktreeDir, captureSpawn, cellLabel,
533
- });
534
- } catch (err) {
535
- runs = [{ runIdx: 0, ttftMs: null, totalMs: 0, status429: false, retryAfterMs: null, exitCode: -1, failedReason: 'cell-throw: ' + (err && err.message || err) }];
536
- }
537
- const summary = summarizeCell(runs);
538
- matrix.push({ workers: cell.workers, contextTokens: cell.contextTokens, runs, summary });
539
- cellsExecuted++;
540
- process.stderr.write('[probe] ' + cellLabel + ' done: p50=' + summary.p50TtftMs + 'ms p95=' + summary.p95TtftMs + 'ms 429=' + summary.total429 + ' safe=' + summary.declaredSafe + '\n');
541
- }
542
- if (abortReason) notes.push('sweep-aborted: ' + abortReason);
543
-
544
- // Backoff probe (LAST in sweep, only on full)
545
- let backoffProbe = null;
546
- if (!quick && !abortReason) {
547
- process.stderr.write('[probe] backoff-probe starting\n');
548
- try {
549
- backoffProbe = await runBackoffProbe({
550
- projectDir, workerBin, fixtureForContext, cwd: worktreeDir, captureSpawn,
551
- });
552
- process.stderr.write('[probe] backoff-probe done: trigger429=' + backoffProbe.trigger429Count + ' samples=' + backoffProbe.post429RecoverySamples.length + '\n');
553
- } catch (err) {
554
- notes.push('backoff-probe-failed: ' + (err && err.message || err));
555
- }
556
- }
557
-
558
- // Steady-state probe
559
- let steadyState = null;
560
- if (!quick && !abortReason) {
561
- process.stderr.write('[probe] steady-state starting (' + steadyStateSec + 's)\n');
562
- try {
563
- steadyState = await runSteadyState({
564
- projectDir, workerBin, fixtureForContext, cwd: worktreeDir, captureSpawn,
565
- durationSec: steadyStateSec,
566
- });
567
- process.stderr.write('[probe] steady-state done: ok=' + steadyState.ok + ' samples=' + steadyState.samples.length + '\n');
568
- } catch (err) {
569
- notes.push('steady-state-failed: ' + (err && err.message || err));
570
- }
571
- }
572
-
573
- // Worktree teardown
574
- if (!noWorktree && !quick && worktreeDir !== projectDir) {
575
- removeWorktree(projectDir, worktreeDir);
576
- }
577
-
578
- const recommended = deriveRecommended({ matrix, backoffProbe, steadyState });
579
-
580
- const out = {
581
- schemaVersion: SCHEMA_VERSION,
582
- generatedAt: new Date().toISOString(),
583
- claudeCliVersion,
584
- account: masked.account,
585
- accountTier,
586
- authPath: masked.authPath,
587
- matrix,
588
- backoffProbe,
589
- steadyState,
590
- recommended,
591
- notes,
592
- };
593
- return out;
594
- }
595
-
596
- async function main() {
597
- const args = parseArgs(process.argv);
598
- const projectDir = process.cwd();
599
- const quick = !!args.quick;
600
- const opts = {
601
- projectDir,
602
- quick,
603
- noWorktree: !!args['no-worktree'],
604
- steadyStateSec: args['steady-state-sec'] ? parseInt(args['steady-state-sec'], 10) : undefined,
605
- maxCells: args['max-cells'] ? parseInt(args['max-cells'], 10) : undefined,
606
- maxTokenBudget: args['max-token-budget'] ? parseInt(args['max-token-budget'], 10) : undefined,
607
- maxWallClockMin: args['max-wall-clock-min'] ? parseInt(args['max-wall-clock-min'], 10) : undefined,
608
- };
609
- const out = await runFullSweep(opts);
610
- const defaultOut = quick
611
- ? path.join(projectDir, '.gsd-t', 'quick-smoke.json')
612
- : path.join(projectDir, '.gsd-t', 'ratelimit-map.json');
613
- const outPath = args.out || defaultOut;
614
- fs.mkdirSync(path.dirname(outPath), { recursive: true });
615
- fs.writeFileSync(outPath, JSON.stringify(out, null, 2));
616
- if (args.json) {
617
- process.stdout.write(JSON.stringify({ outPath, recommended: out.recommended, notes: out.notes }) + '\n');
618
- } else {
619
- process.stdout.write('Wrote ' + outPath + '\n');
620
- process.stdout.write(' peakConcurrency: ' + out.recommended.peakConcurrency + '\n');
621
- process.stdout.write(' safeAt60k: ' + out.recommended.safeConcurrencyAt60kContext + '\n');
622
- process.stdout.write(' perWorkerBudget: ' + out.recommended.perWorkerContextBudgetTokens + '\n');
623
- process.stdout.write(' backoffMs: ' + out.recommended.backoffMs + '\n');
624
- process.stdout.write(' steady5MinPass: ' + out.recommended.steadyState3Workers5MinPass + '\n');
625
- }
626
- process.exit(0);
627
- }
628
-
629
- if (require.main === module) {
630
- main().catch((err) => {
631
- process.stderr.write('FATAL: ' + (err && err.stack || err) + '\n');
632
- process.exit(2);
633
- });
634
- }
635
-
636
- module.exports = {
637
- SCHEMA_VERSION,
638
- SWEEP_WORKERS,
639
- SWEEP_CONTEXTS,
640
- RUNS_PER_CELL,
641
- DECLARED_SAFE_P95_TTFT_MS,
642
- buildSweepMatrix,
643
- summarizeCell,
644
- maskAccount,
645
- deriveRecommended,
646
- percentile,
647
- runFullSweep,
648
- };