@tekyzinc/gsd-t 3.18.13 → 3.18.17

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.
@@ -62,6 +62,19 @@ function _emit(projectDir, ev) {
62
62
  try { _esAppendEvent(projectDir, ev); } catch (_) { /* never halt the loop */ }
63
63
  }
64
64
 
65
+ // M44 D9 (v1.5.0) — planner-driven multi-worker fan-out. Lazy-loaded so unit
66
+ // tests can stub via deps._runParallel without touching the real module.
67
+ let _parallelModule = null;
68
+ function _loadRunParallel() {
69
+ if (_parallelModule) return _parallelModule;
70
+ try {
71
+ _parallelModule = require("./gsd-t-parallel.cjs");
72
+ } catch {
73
+ _parallelModule = { runParallel: () => ({ workerCount: 0, parallelTasks: [], plan: [] }) };
74
+ }
75
+ return _parallelModule;
76
+ }
77
+
65
78
  // M42 D1 — transcript tee. Captures each worker's stdout lines to an ndjson
66
79
  // file and registers the spawn so the dashboard sidebar can list + render it.
67
80
  // Best-effort: every call is swallowed so tee failures never halt the loop.
@@ -73,7 +86,7 @@ const { checkHeartbeat: _checkHeartbeat } = require("./gsd-t-unattended-heartbea
73
86
 
74
87
  // ── Constants ───────────────────────────────────────────────────────────────
75
88
 
76
- const CONTRACT_VERSION = "1.4.0";
89
+ const CONTRACT_VERSION = "1.5.0";
77
90
  const UNATTENDED_DIR_REL = path.join(".gsd-t", ".unattended");
78
91
  const PID_FILE = "supervisor.pid";
79
92
  const STATE_FILE = "state.json";
@@ -122,6 +135,8 @@ module.exports = {
122
135
  releaseSleepPrevention,
123
136
  runMainLoop,
124
137
  _spawnWorker,
138
+ _spawnWorkerFanOut,
139
+ _partitionTasks,
125
140
  _appendRunLog,
126
141
  CONTRACT_VERSION,
127
142
  UNATTENDED_DIR_REL,
@@ -927,6 +942,10 @@ async function runMainLoop(state, dir, opts, deps, ctx) {
927
942
  deps._spawnWorker || (useTestStub ? _testModeSpawnWorker : _spawnWorker);
928
943
  const milestoneComplete =
929
944
  deps._isMilestoneComplete || (useTestStub ? () => true : isMilestoneComplete);
945
+ // M44 D9 (v1.5.0) — planner injected for multi-worker iter fan-out.
946
+ // Tests stub via deps._runParallel; production lazy-loads from gsd-t-parallel.cjs.
947
+ const runParallelImpl =
948
+ deps._runParallel || ((o) => _loadRunParallel().runParallel(o));
930
949
  const stopCheck = deps._stopRequested || stopRequested;
931
950
  const workerTimeoutMs = opts.workerTimeoutMs || DEFAULT_WORKER_TIMEOUT_MS;
932
951
  const staleHeartbeatMs =
@@ -1001,15 +1020,61 @@ async function runMainLoop(state, dir, opts, deps, ctx) {
1001
1020
  heartbeatPollMs,
1002
1021
  }
1003
1022
  : {};
1023
+
1024
+ // M44 D9 (v1.5.0) — planner-driven fan-out decision for this iter.
1025
+ // Ask runParallel whether the current task graph supports ≥2 concurrent
1026
+ // workers. Any failure in the planner path MUST fall back to the single-
1027
+ // worker spawn — the parallel path is purely additive.
1028
+ let iterPlan = null;
1004
1029
  try {
1005
- res = spawnWorker(state, {
1006
- cwd: projectDir,
1007
- timeout: workerTimeoutMs,
1008
- verbose: !!opts.verbose,
1009
- ...hbOpts,
1030
+ iterPlan = runParallelImpl({
1031
+ projectDir,
1032
+ mode: "unattended",
1033
+ milestone: state.milestone || null,
1034
+ dryRun: true,
1010
1035
  });
1011
- if (res && typeof res.then === "function") {
1012
- res = await res;
1036
+ } catch (e) {
1037
+ iterPlan = null;
1038
+ _emit(projectDir, {
1039
+ iter: state.iter,
1040
+ type: "parallelism_reduced",
1041
+ source: "supervisor",
1042
+ original_count: null,
1043
+ reduced_count: 1,
1044
+ reason: `planner_error:${(e && e.message) || "unknown"}`,
1045
+ });
1046
+ }
1047
+ const fanOutCount = iterPlan && Number(iterPlan.workerCount) >= 2 ? Number(iterPlan.workerCount) : 1;
1048
+ const parallelTaskIds = iterPlan && Array.isArray(iterPlan.parallelTasks) ? iterPlan.parallelTasks : [];
1049
+ const subsets = fanOutCount >= 2 ? _partitionTasks(parallelTaskIds, fanOutCount) : null;
1050
+ const useFanOut = !!(subsets && subsets.length >= 2);
1051
+
1052
+ try {
1053
+ if (useFanOut) {
1054
+ _emit(projectDir, {
1055
+ ts: workerStart.toISOString(),
1056
+ iter: state.iter,
1057
+ type: "fan_out",
1058
+ source: "supervisor",
1059
+ worker_count: subsets.length,
1060
+ task_ids: parallelTaskIds,
1061
+ });
1062
+ res = await _spawnWorkerFanOut(state, {
1063
+ cwd: projectDir,
1064
+ timeout: workerTimeoutMs,
1065
+ verbose: !!opts.verbose,
1066
+ ...hbOpts,
1067
+ }, spawnWorker, subsets);
1068
+ } else {
1069
+ res = spawnWorker(state, {
1070
+ cwd: projectDir,
1071
+ timeout: workerTimeoutMs,
1072
+ verbose: !!opts.verbose,
1073
+ ...hbOpts,
1074
+ });
1075
+ if (res && typeof res.then === "function") {
1076
+ res = await res;
1077
+ }
1013
1078
  }
1014
1079
  } catch (e) {
1015
1080
  // Defensive: a real spawnSync shouldn't throw, but a shim could.
@@ -1083,6 +1148,26 @@ async function runMainLoop(state, dir, opts, deps, ctx) {
1083
1148
  } else {
1084
1149
  state.lastExitReason = `exit_${exitCode}`;
1085
1150
  }
1151
+ // M44 D9 (v1.5.0) — per-iter multi-worker aggregates. Present only when the
1152
+ // planner selected fan-out; single-worker iters omit these fields so the
1153
+ // state schema stays backward-compatible with v1.4.x readers.
1154
+ if (useFanOut && Array.isArray(res.workerResults)) {
1155
+ state.lastExits = res.workerResults.map((w) => ({
1156
+ idx: w.idx,
1157
+ code: typeof w.status === "number" ? w.status : null,
1158
+ taskIds: w.taskIds || [],
1159
+ elapsedMs: w.elapsedMs,
1160
+ spawnId: w.spawnId || null,
1161
+ }));
1162
+ state.workerPids = res.workerResults.map((w) => w.spawnId || null);
1163
+ state.lastFanOutCount = res.workerResults.length;
1164
+ } else {
1165
+ // Clear stale multi-worker fields on single-worker iters so readers
1166
+ // never see a mix of regimes.
1167
+ if (state.lastExits) delete state.lastExits;
1168
+ if (state.workerPids) delete state.workerPids;
1169
+ if (state.lastFanOutCount) delete state.lastFanOutCount;
1170
+ }
1086
1171
  writeState(state, dir);
1087
1172
 
1088
1173
  // Event-stream: task_complete on success, error on non-zero.
@@ -1285,11 +1370,24 @@ function _spawnWorker(state, opts) {
1285
1370
  // id as parent, so shims inside the worker write state files that the tree
1286
1371
  // builder can attach under the supervisor root.
1287
1372
  workerEnv.GSD_T_AGENT_ID =
1288
- "supervisor-iter-" + (state && state.iter ? state.iter : Date.now());
1373
+ "supervisor-iter-" + (state && state.iter ? state.iter : Date.now()) +
1374
+ (state && typeof state._workerIndex === "number" ? `-w${state._workerIndex}` : "");
1289
1375
  if (process.env.GSD_T_AGENT_ID) {
1290
1376
  workerEnv.GSD_T_PARENT_AGENT_ID = process.env.GSD_T_AGENT_ID;
1291
1377
  }
1292
1378
 
1379
+ // M44 D9 (v1.5.0) — planner-driven fan-out: when the supervisor partitions
1380
+ // the iter's task graph across N workers, each worker carries its disjoint
1381
+ // task-id subset via env var. The worker prompt consumes this to (a) skip
1382
+ // the intra-worker Team Mode block (the fan-out is the team), (b) restrict
1383
+ // itself to its assigned task IDs.
1384
+ const assignedTaskIds = Array.isArray(opts && opts.taskIds) ? opts.taskIds : null;
1385
+ if (assignedTaskIds && assignedTaskIds.length > 0) {
1386
+ workerEnv.GSD_T_WORKER_TASK_IDS = assignedTaskIds.join(",");
1387
+ workerEnv.GSD_T_WORKER_INDEX = String((state && state._workerIndex) || 0);
1388
+ workerEnv.GSD_T_WORKER_TOTAL = String((state && state._workerTotal) || 1);
1389
+ }
1390
+
1293
1391
  // M42 D1 — allocate a spawn-id + open transcript before spawning. parentId
1294
1392
  // is the supervisor's own spawn-id (set once at supervisor start via
1295
1393
  // GSD_T_SPAWN_ID env) so the sidebar can render parent-indented trees.
@@ -1353,6 +1451,18 @@ function _spawnWorker(state, opts) {
1353
1451
  "",
1354
1452
  "# Team Mode (Intra-Wave Parallelism)",
1355
1453
  "",
1454
+ "M44 D9 (v1.5.0+) — check env `GSD_T_WORKER_TASK_IDS` FIRST. If SET, you",
1455
+ "are one of N planner-assigned workers in a supervisor-level fan-out.",
1456
+ "The value is your disjoint task-id subset. DO NOT spawn Task subagents",
1457
+ "to re-fan-out (the supervisor already did). Execute ONLY your assigned",
1458
+ "task IDs sequentially in this worker, then return. Skip the rest of",
1459
+ "this block.",
1460
+ "",
1461
+ "If GSD_T_WORKER_TASK_IDS is UNSET, the supervisor's planner decided",
1462
+ "N=1 for this iter (sequential fallback: gates vetoed, file-disjointness",
1463
+ "unprovable, or est CW% too high). Proceed with the legacy worker-level",
1464
+ "Team Mode below:",
1465
+ "",
1356
1466
  "Before executing tasks for this iteration, read `.gsd-t/partition.md` to",
1357
1467
  "identify the current wave and which domains belong to it.",
1358
1468
  "",
@@ -1427,6 +1537,97 @@ function _spawnWorker(state, opts) {
1427
1537
  return finalize(spawnResult);
1428
1538
  }
1429
1539
 
1540
+ // ── _spawnWorkerFanOut (M44 D9, contract v1.5.0) ────────────────────────────
1541
+
1542
+ /**
1543
+ * Planner-driven multi-worker fan-out. Spawns N concurrent workers via the
1544
+ * injected `spawnWorker` shim, each receiving a disjoint subset of the iter's
1545
+ * parallel task IDs (passed through `opts.taskIds`). Waits on all via
1546
+ * Promise.all before returning a merged result shape compatible with the
1547
+ * single-worker path.
1548
+ *
1549
+ * Merge semantics:
1550
+ * - `status` — 0 if every worker cleanly returned 0, else the first
1551
+ * non-zero status encountered (worst exit wins).
1552
+ * - `stdout` — per-worker blocks joined by `[WORKER i/N tasks=...]` headers.
1553
+ * - `stderr` — concatenated.
1554
+ * - `staleHeartbeat`/`timedOut` — true if any worker triggered them.
1555
+ * - `workerResults` — array of per-worker {status, taskIds, pid, spawnId, elapsedMs}
1556
+ * for state.json aggregation.
1557
+ *
1558
+ * The caller (runMainLoop) treats this result exactly like a single-worker
1559
+ * result for downstream classification. Multi-worker observability lives in
1560
+ * the `workerResults` array, not in new control-flow branches.
1561
+ */
1562
+ async function _spawnWorkerFanOut(state, opts, spawnWorker, subsets) {
1563
+ const launches = subsets.map((taskIds, i) => {
1564
+ const subState = { ...state, _workerIndex: i, _workerTotal: subsets.length, _workerTaskIds: taskIds };
1565
+ const started = Date.now();
1566
+ return Promise.resolve()
1567
+ .then(() => spawnWorker(subState, { ...opts, taskIds }))
1568
+ .then((r) => ({ r: r || {}, taskIds, started, ended: Date.now(), idx: i }))
1569
+ .catch((e) => ({
1570
+ r: { status: 3, stdout: "", stderr: String((e && e.message) || e), signal: null },
1571
+ taskIds, started, ended: Date.now(), idx: i,
1572
+ }));
1573
+ });
1574
+ const outcomes = await Promise.all(launches);
1575
+ outcomes.sort((a, b) => a.idx - b.idx);
1576
+
1577
+ let mergedStatus = 0;
1578
+ let stale = false;
1579
+ let timedOut = false;
1580
+ let heartbeatReason = null;
1581
+ const stdoutBlocks = [];
1582
+ const stderrBlocks = [];
1583
+ const workerResults = [];
1584
+
1585
+ for (const o of outcomes) {
1586
+ const s = typeof o.r.status === "number" ? o.r.status : null;
1587
+ if (mergedStatus === 0 && s !== 0) mergedStatus = s === null ? 1 : s;
1588
+ if (o.r.staleHeartbeat) stale = true;
1589
+ if (o.r.timedOut) timedOut = true;
1590
+ if (!heartbeatReason && o.r.heartbeatReason) heartbeatReason = o.r.heartbeatReason;
1591
+ const tag = `[WORKER ${o.idx + 1}/${outcomes.length} tasks=${(o.taskIds || []).join(",") || "-"}]`;
1592
+ stdoutBlocks.push(`${tag}\n${o.r.stdout || ""}`);
1593
+ if (o.r.stderr) stderrBlocks.push(`${tag}\n${o.r.stderr}`);
1594
+ workerResults.push({
1595
+ idx: o.idx,
1596
+ status: s,
1597
+ taskIds: o.taskIds,
1598
+ spawnId: o.r.spawnId || null,
1599
+ signal: o.r.signal || null,
1600
+ elapsedMs: o.ended - o.started,
1601
+ staleHeartbeat: !!o.r.staleHeartbeat,
1602
+ timedOut: !!o.r.timedOut,
1603
+ });
1604
+ }
1605
+
1606
+ return {
1607
+ status: mergedStatus,
1608
+ stdout: stdoutBlocks.join("\n"),
1609
+ stderr: stderrBlocks.join("\n"),
1610
+ signal: null,
1611
+ timedOut,
1612
+ staleHeartbeat: stale,
1613
+ heartbeatReason,
1614
+ workerResults,
1615
+ fanOutCount: outcomes.length,
1616
+ };
1617
+ }
1618
+
1619
+ /**
1620
+ * Partition a task-id list into `workerCount` roughly-equal subsets. Simple
1621
+ * round-robin — each subset is non-empty as long as `tasks.length >= workerCount`.
1622
+ */
1623
+ function _partitionTasks(tasks, workerCount) {
1624
+ if (!Array.isArray(tasks) || tasks.length === 0 || workerCount < 1) return [];
1625
+ const n = Math.min(workerCount, tasks.length);
1626
+ const subsets = Array.from({ length: n }, () => []);
1627
+ for (let i = 0; i < tasks.length; i++) subsets[i % n].push(tasks[i]);
1628
+ return subsets;
1629
+ }
1630
+
1430
1631
  // ── _testModeSpawnWorker ────────────────────────────────────────────────────
1431
1632
 
1432
1633
  /**
@@ -73,7 +73,8 @@ let _deprecatedWatchWarned = false;
73
73
  * sessionContext?: object,
74
74
  * sessionId?: string,
75
75
  * watch?: boolean,
76
- * spawnType?: 'primary' | 'validation'
76
+ * spawnType?: 'primary' | 'validation',
77
+ * env?: object
77
78
  * }} opts
78
79
  * @returns {{ id: string | null, pid: number | null, logPath: string | null, timestamp: string, mode: 'headless' | 'in-context' }}
79
80
  */
@@ -83,6 +84,12 @@ function autoSpawnHeadless(opts) {
83
84
  const continue_from = opts.continue_from || ".";
84
85
  const projectDir = opts.projectDir || process.cwd();
85
86
  const context = opts.context || opts.sessionContext || null;
87
+ // M44 D9 Step 3 — optional per-call env overrides layered over the inherited
88
+ // process.env. Used by `runDispatch` in gsd-t-parallel.cjs to forward
89
+ // GSD_T_WORKER_TASK_IDS / _WORKER_INDEX / _WORKER_TOTAL to each fan-out
90
+ // child so the child knows which task subset to handle. Purely additive —
91
+ // callers that don't pass `env` get the pre-M44-D9 behavior unchanged.
92
+ const envOverride = (opts.env && typeof opts.env === "object") ? opts.env : null;
86
93
  // M43 D4 — `watch` is accepted for caller backward-compat but IGNORED.
87
94
  // `inSession` was never shipped; accept+ignore for the same reason.
88
95
  // Under headless-default-contract v2.0.0 every spawn goes headless; the
@@ -195,6 +202,15 @@ function autoSpawnHeadless(opts) {
195
202
  if (process.env.GSD_T_AGENT_ID) {
196
203
  workerEnv.GSD_T_PARENT_AGENT_ID = process.env.GSD_T_AGENT_ID;
197
204
  }
205
+ // M44 D9 Step 3 — caller-supplied env overrides (e.g., fan-out task ids).
206
+ // Applied AFTER the canonical GSD_T_* keys so a caller can override any
207
+ // of them if needed; opaque for everything else.
208
+ if (envOverride) {
209
+ for (const [k, v] of Object.entries(envOverride)) {
210
+ if (v == null) continue;
211
+ workerEnv[k] = String(v);
212
+ }
213
+ }
198
214
 
199
215
  const child = spawn("node", childArgs, {
200
216
  cwd: projectDir,
@@ -19,29 +19,47 @@
19
19
 
20
20
  "use strict";
21
21
 
22
+ // Match terminal markers, not narration. A bare "tests failed" substring will
23
+ // appear in healthy output ("0 tests failed", "no tests failed", quoted as an
24
+ // example in prose). Require either a non-zero count prefix or a structured
25
+ // terminal marker (start of line, uppercase-FAIL prefix, Jest-style summary).
26
+ // Bug history: M45 worker output contained "tests failed" 6× in narration,
27
+ // causing the supervisor to map exit 0 → exit 1 and halt a successful run.
28
+
29
+ const NONZERO_FAILURE_COUNT_RE =
30
+ /(?:^|\b)([1-9]\d*)\s+(?:tests?|specs?|assertions?|examples?|suites?)\s+failed\b/i;
31
+ const STRUCTURED_FAIL_RE = /^FAIL[:\s]/m;
32
+ const JEST_SUMMARY_FAIL_RE = /^Tests:\s+\d+\s+failed/im;
33
+
34
+ // Verification-phrase matchers: require the phrase at a line boundary or
35
+ // preceded by a sentence-start punctuation — not mid-prose. Each phrase is
36
+ // distinctive enough that start-of-line / post-punctuation is a reliable
37
+ // terminal-marker signal.
38
+ const VERIFICATION_FAILED_RE =
39
+ /(?:^|[.!?]\s+)(?:verification|verify|quality gate)\s+failed\b/im;
40
+
41
+ // Context-budget phrases — same polarity discipline. Tolerant of surrounding
42
+ // punctuation (— / :) but requires the phrase at a line boundary.
43
+ const CONTEXT_BUDGET_RE =
44
+ /(?:^|[.!?]\s+)(?:context budget exceeded|context window exceeded|budget exceeded|token limit)\b/im;
45
+
46
+ // Blocker compound: "blocked" within 80 chars of a human-gate phrase, both
47
+ // anchored to recognizable boundaries. The 80-char proximity keeps unrelated
48
+ // mentions from compounding.
49
+ const BLOCKED_HUMAN_RE =
50
+ /\bblocked\b[\s\S]{0,80}?\b(?:needs? human|human input|human approval)\b/i;
51
+
22
52
  function mapHeadlessExitCode(processExitCode, output) {
23
53
  if (processExitCode !== 0 && processExitCode !== null) return 3;
24
54
  const raw = output || "";
25
- const lower = raw.toLowerCase();
26
55
  if (/^unknown command:/im.test(raw)) return 5;
56
+ if (CONTEXT_BUDGET_RE.test(raw)) return 2;
57
+ if (BLOCKED_HUMAN_RE.test(raw)) return 4;
27
58
  if (
28
- lower.includes("context budget exceeded") ||
29
- lower.includes("context window exceeded") ||
30
- lower.includes("budget exceeded") ||
31
- lower.includes("token limit")
32
- ) return 2;
33
- if (
34
- lower.includes("blocked") &&
35
- (lower.includes("needs human") ||
36
- lower.includes("need human") ||
37
- lower.includes("human input") ||
38
- lower.includes("human approval"))
39
- ) return 4;
40
- if (
41
- lower.includes("verification failed") ||
42
- lower.includes("verify failed") ||
43
- lower.includes("quality gate failed") ||
44
- lower.includes("tests failed")
59
+ VERIFICATION_FAILED_RE.test(raw) ||
60
+ NONZERO_FAILURE_COUNT_RE.test(raw) ||
61
+ STRUCTURED_FAIL_RE.test(raw) ||
62
+ JEST_SUMMARY_FAIL_RE.test(raw)
45
63
  ) return 1;
46
64
  return 0;
47
65
  }
@@ -0,0 +1,285 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ /**
5
+ * M44 Proof Measurement Driver (backlog #15 — T/2 criterion)
6
+ *
7
+ * Goal: prove v3.19.00's parallel dispatcher actually fans out concurrently.
8
+ *
9
+ * Method:
10
+ * 1. Build a temp project with a 4-task file-disjoint tasks.md fixture.
11
+ * 2. Inject a synthetic spawner into bin/gsd-t-parallel.cjs::runDispatch
12
+ * that launches test/fixtures/m44-proof/worker-sim.js as a detached
13
+ * child (same contract as the real autoSpawnHeadless).
14
+ * 3. Measure:
15
+ * T_seq = sum of per-worker durations when called sequentially.
16
+ * T_par = wall-clock span from first spawn_started to last .done
17
+ * marker when dispatched via runDispatch.
18
+ * 4. Criterion: T_par ≤ T_seq / 2 ( = parallelism_factor ≥ 2 for N=4 )
19
+ *
20
+ * This proves the DISPATCHER. It does NOT prove that 4 Claude workers
21
+ * produce correct code in T/2. That is a separate, API-budget-intensive
22
+ * experiment (backlog #15 follow-up). The dispatcher is the shipped code
23
+ * in v3.19.00, and its mechanics are what the tag certifies.
24
+ */
25
+
26
+ const fs = require('fs');
27
+ const path = require('path');
28
+ const os = require('os');
29
+ const { spawn } = require('child_process');
30
+
31
+ const { runDispatch } = require(path.join(__dirname, 'gsd-t-parallel.cjs'));
32
+ const { writeSpawnPlan } = require(path.join(__dirname, 'spawn-plan-writer.cjs'));
33
+ const { markTaskDone, markSpawnEnded } = require(path.join(__dirname, 'spawn-plan-status-updater.cjs'));
34
+
35
+ // When invoked with --visualize, write spawn-plan files into the REAL project
36
+ // directory (not the temp fixture dir) so the live dashboard at :7455 can
37
+ // render the fan-out in real time. Off by default to keep the measurement
38
+ // directory clean.
39
+ const VISUALIZE = process.argv.includes('--visualize');
40
+ const REAL_PROJECT_DIR = path.resolve(__dirname, '..');
41
+
42
+ // ── fixture setup ───────────────────────────────────────────────────────────
43
+
44
+ function buildFixtureProject(workDurationMs) {
45
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'm44-proof-'));
46
+ fs.mkdirSync(path.join(root, '.gsd-t', 'domains', 'm99-d1-proof'), { recursive: true });
47
+ fs.mkdirSync(path.join(root, '.gsd-t', 'spawns'), { recursive: true });
48
+ fs.mkdirSync(path.join(root, '.gsd-t', 'events'), { recursive: true });
49
+
50
+ const tasksMd = fs.readFileSync(
51
+ path.join(__dirname, '..', 'test', 'fixtures', 'm44-proof', 'fixture.tasks.md'),
52
+ 'utf8',
53
+ );
54
+ fs.writeFileSync(path.join(root, '.gsd-t', 'domains', 'm99-d1-proof', 'tasks.md'), tasksMd);
55
+
56
+ const partitionMd = [
57
+ '# Partition — M99',
58
+ '',
59
+ '## Wave 1',
60
+ '- m99-d1-proof (all tasks disjoint, no deps)',
61
+ '',
62
+ ].join('\n');
63
+ fs.writeFileSync(path.join(root, '.gsd-t', 'partition.md'), partitionMd);
64
+
65
+ return root;
66
+ }
67
+
68
+ function cleanup(root) {
69
+ try { fs.rmSync(root, { recursive: true, force: true }); } catch (_) { /* ignore */ }
70
+ }
71
+
72
+ // ── spawner (injected into runDispatch) ────────────────────────────────────
73
+
74
+ function makeSpawner({ outDir, workerDurationMs }) {
75
+ const launched = [];
76
+ const spawner = ({ env }) => {
77
+ const childEnv = Object.assign({}, process.env, env, {
78
+ OUT_DIR: outDir,
79
+ WORKER_DURATION_MS: String(workerDurationMs),
80
+ });
81
+ const workerPath = path.join(__dirname, '..', 'test', 'fixtures', 'm44-proof', 'worker-sim.js');
82
+ const child = spawn(process.execPath, [workerPath], {
83
+ env: childEnv, detached: true, stdio: 'ignore',
84
+ });
85
+ child.unref();
86
+ const spawnId = 'm44-proof-' + env.GSD_T_WORKER_INDEX + '-' + Date.now();
87
+ const taskIds = env.GSD_T_WORKER_TASK_IDS.split(',');
88
+
89
+ if (VISUALIZE) {
90
+ try {
91
+ writeSpawnPlan({
92
+ spawnId,
93
+ kind: 'headless-detached',
94
+ milestone: 'M99',
95
+ wave: 'wave-1',
96
+ domains: ['m99-d1-proof'],
97
+ tasks: taskIds.map((id) => ({ id, title: 'Proof ' + id, status: 'in_flight' })),
98
+ projectDir: REAL_PROJECT_DIR,
99
+ });
100
+ } catch (e) { process.stderr.write('[visualize] writeSpawnPlan failed: ' + e.message + '\n'); }
101
+ }
102
+
103
+ launched.push({
104
+ spawnId, pid: child.pid,
105
+ taskIds,
106
+ launchedAt: process.hrtime.bigint(),
107
+ });
108
+ return { id: spawnId, pid: child.pid, logPath: null };
109
+ };
110
+ return { spawner, launched };
111
+ }
112
+
113
+ function markLaunchedDone(launched) {
114
+ if (!VISUALIZE) return;
115
+ for (const l of launched) {
116
+ try {
117
+ for (const id of l.taskIds) {
118
+ markTaskDone({ spawnId: l.spawnId, taskId: id, projectDir: REAL_PROJECT_DIR, commit: 'proof-sim', tokens: null });
119
+ }
120
+ markSpawnEnded({ spawnId: l.spawnId, projectDir: REAL_PROJECT_DIR });
121
+ } catch (e) { process.stderr.write('[visualize] mark-done failed: ' + e.message + '\n'); }
122
+ }
123
+ }
124
+
125
+ // ── wait for all .done markers ─────────────────────────────────────────────
126
+
127
+ async function waitForMarkers(outDir, expectedCount, timeoutMs = 60000) {
128
+ const deadline = Date.now() + timeoutMs;
129
+ while (Date.now() < deadline) {
130
+ let present;
131
+ try { present = fs.readdirSync(outDir).filter((n) => n.endsWith('.done')); }
132
+ catch { present = []; }
133
+ if (present.length >= expectedCount) return present;
134
+ await new Promise((r) => setTimeout(r, 100));
135
+ }
136
+ throw new Error('timeout waiting for ' + expectedCount + ' .done markers in ' + outDir);
137
+ }
138
+
139
+ function readMarkers(outDir) {
140
+ return fs.readdirSync(outDir)
141
+ .filter((n) => n.endsWith('.done'))
142
+ .map((n) => JSON.parse(fs.readFileSync(path.join(outDir, n), 'utf8')))
143
+ .sort((a, b) => a.workerIndex - b.workerIndex);
144
+ }
145
+
146
+ // ── runs ────────────────────────────────────────────────────────────────────
147
+
148
+ async function runParallelPass(workerDurationMs) {
149
+ const projectDir = buildFixtureProject(workerDurationMs);
150
+ const outDir = path.join(projectDir, 'worker-out');
151
+ fs.mkdirSync(outDir, { recursive: true });
152
+
153
+ const { spawner, launched } = makeSpawner({ outDir, workerDurationMs });
154
+
155
+ const t0 = process.hrtime.bigint();
156
+ const result = runDispatch({
157
+ projectDir,
158
+ command: 'gsd-t-execute',
159
+ mode: 'unattended',
160
+ env: { GSD_T_UNATTENDED: '1' },
161
+ spawnHeadlessImpl: spawner,
162
+ });
163
+ const dispatchReturnedAt = process.hrtime.bigint();
164
+
165
+ if (result.fanOutCount < 2) {
166
+ cleanup(projectDir);
167
+ throw new Error('parallel dispatch did not fan out (decision=' + result.decision + ', fanOutCount=' + result.fanOutCount + ')');
168
+ }
169
+
170
+ await waitForMarkers(outDir, result.fanOutCount);
171
+ const t1 = process.hrtime.bigint();
172
+
173
+ markLaunchedDone(launched);
174
+
175
+ const markers = readMarkers(outDir);
176
+ const wallClockMs = Number(t1 - t0) / 1e6;
177
+ const dispatchOverheadMs = Number(dispatchReturnedAt - t0) / 1e6;
178
+ const perWorkerDurationMs = markers.map((m) => m.durationMs);
179
+ const sumWorkerDurationMs = perWorkerDurationMs.reduce((a, b) => a + b, 0);
180
+
181
+ cleanup(projectDir);
182
+ return {
183
+ mode: 'parallel',
184
+ fanOutCount: result.fanOutCount,
185
+ launched: launched.length,
186
+ wallClockMs,
187
+ dispatchOverheadMs,
188
+ perWorkerDurationMs,
189
+ sumWorkerDurationMs,
190
+ workerPids: markers.map((m) => m.pid),
191
+ markers,
192
+ };
193
+ }
194
+
195
+ async function runSequentialPass(workerDurationMs, taskCount = 4) {
196
+ // Sequential baseline: run N workers back-to-back, same worker-sim.js.
197
+ // This is what a single-worker supervisor would do before v3.19.00.
198
+ const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'm44-proof-seq-'));
199
+ const outDir = path.join(tmp, 'worker-out');
200
+ fs.mkdirSync(outDir, { recursive: true });
201
+
202
+ const t0 = process.hrtime.bigint();
203
+ for (let i = 0; i < taskCount; i++) {
204
+ await new Promise((resolve, reject) => {
205
+ const child = spawn(process.execPath, [
206
+ path.join(__dirname, '..', 'test', 'fixtures', 'm44-proof', 'worker-sim.js'),
207
+ ], {
208
+ env: Object.assign({}, process.env, {
209
+ OUT_DIR: outDir,
210
+ WORKER_DURATION_MS: String(workerDurationMs),
211
+ GSD_T_WORKER_INDEX: String(i),
212
+ GSD_T_WORKER_TOTAL: String(taskCount),
213
+ GSD_T_WORKER_TASK_IDS: 'M99-D1-T' + (i + 1),
214
+ }),
215
+ stdio: 'ignore',
216
+ });
217
+ child.on('exit', (code) => code === 0 ? resolve() : reject(new Error('worker exit ' + code)));
218
+ child.on('error', reject);
219
+ });
220
+ }
221
+ const t1 = process.hrtime.bigint();
222
+ const markers = readMarkers(outDir);
223
+ const wallClockMs = Number(t1 - t0) / 1e6;
224
+ cleanup(tmp);
225
+
226
+ return {
227
+ mode: 'sequential',
228
+ taskCount,
229
+ wallClockMs,
230
+ perWorkerDurationMs: markers.map((m) => m.durationMs),
231
+ sumWorkerDurationMs: markers.map((m) => m.durationMs).reduce((a, b) => a + b, 0),
232
+ };
233
+ }
234
+
235
+ // ── main ────────────────────────────────────────────────────────────────────
236
+
237
+ async function main() {
238
+ const workerDurationMs = parseInt(process.env.WORKER_DURATION_MS || '8000', 10);
239
+ process.stdout.write('M44 Proof Measurement (worker duration=' + workerDurationMs + 'ms)\n');
240
+ process.stdout.write('─'.repeat(60) + '\n');
241
+
242
+ const seq = await runSequentialPass(workerDurationMs);
243
+ process.stdout.write('Sequential (N=4 workers run back-to-back)\n');
244
+ process.stdout.write(' T_seq (wall-clock): ' + seq.wallClockMs.toFixed(1) + ' ms\n');
245
+ process.stdout.write(' sum(worker durations): ' + seq.sumWorkerDurationMs.toFixed(1) + ' ms\n');
246
+ process.stdout.write('\n');
247
+
248
+ const par = await runParallelPass(workerDurationMs);
249
+ process.stdout.write('Parallel (runDispatch, N=' + par.fanOutCount + ' concurrent workers)\n');
250
+ process.stdout.write(' T_par (wall-clock): ' + par.wallClockMs.toFixed(1) + ' ms\n');
251
+ process.stdout.write(' dispatch overhead: ' + par.dispatchOverheadMs.toFixed(1) + ' ms\n');
252
+ process.stdout.write(' sum(worker durations): ' + par.sumWorkerDurationMs.toFixed(1) + ' ms\n');
253
+ process.stdout.write(' per-worker durations: [' + par.perWorkerDurationMs.map((x) => x.toFixed(0)).join(', ') + '] ms\n');
254
+ process.stdout.write(' worker pids: [' + par.workerPids.join(', ') + ']\n');
255
+ process.stdout.write('\n');
256
+
257
+ const ratio = par.wallClockMs / seq.wallClockMs;
258
+ const speedup = seq.wallClockMs / par.wallClockMs;
259
+ const parallelismFactor = par.sumWorkerDurationMs / par.wallClockMs;
260
+ const criterion = par.wallClockMs <= seq.wallClockMs / 2;
261
+
262
+ process.stdout.write('─'.repeat(60) + '\n');
263
+ process.stdout.write('Result\n');
264
+ process.stdout.write(' T_par / T_seq = ' + ratio.toFixed(3) + '\n');
265
+ process.stdout.write(' speedup = ' + speedup.toFixed(2) + '×\n');
266
+ process.stdout.write(' parallelism_factor = ' + parallelismFactor.toFixed(2) + ' (ideal = ' + par.fanOutCount + ')\n');
267
+ process.stdout.write(' T/2 criterion = ' + (criterion ? 'MET ✓' : 'NOT MET ✗') + ' (T_par ≤ T_seq/2)\n');
268
+
269
+ const report = {
270
+ generatedAt: new Date().toISOString(),
271
+ workerDurationMs,
272
+ sequential: seq,
273
+ parallel: par,
274
+ ratio, speedup, parallelismFactor,
275
+ criterionMet: criterion,
276
+ };
277
+ const reportPath = path.join(process.cwd(), '.gsd-t', 'm44-proof-report.json');
278
+ fs.mkdirSync(path.dirname(reportPath), { recursive: true });
279
+ fs.writeFileSync(reportPath, JSON.stringify(report, null, 2));
280
+ process.stdout.write('\nReport written: ' + reportPath + '\n');
281
+
282
+ process.exit(criterion ? 0 : 1);
283
+ }
284
+
285
+ main().catch((e) => { process.stderr.write('ERROR: ' + (e && e.stack || e) + '\n'); process.exit(2); });