@tekyzinc/gsd-t 3.10.14 → 3.10.16

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.
@@ -0,0 +1,788 @@
1
+ /**
2
+ * gsd-t-unattended-safety.js
3
+ *
4
+ * Pure-function safety rails for the unattended supervisor.
5
+ *
6
+ * Contract: .gsd-t/contracts/unattended-supervisor-contract.md v1.0.0
7
+ * §5 — Exit code table (2 = preflight-failure, 7 = protected-branch-refusal,
8
+ * 8 = dirty-tree-refusal)
9
+ * §12 — Safety Rails Hook Points
10
+ * §13 — Configuration File schema (DEFAULTS authoritative source)
11
+ *
12
+ * This module exports synchronous, side-effect-light check functions called
13
+ * by the supervisor between worker spawns. Each check returns
14
+ * { ok: true } on allow
15
+ * { ok: false, reason: string, code: number } on refuse
16
+ *
17
+ * The only permitted side effects are:
18
+ * - reading git state via `git branch --show-current` and `git status --porcelain`
19
+ * - reading the optional config file at `.gsd-t/.unattended/config.json`
20
+ *
21
+ * Zero external dependencies — Node built-ins only.
22
+ *
23
+ * Owner: m36-safety-rails
24
+ */
25
+
26
+ const fs = require("node:fs");
27
+ const path = require("node:path");
28
+ const { spawnSync } = require("node:child_process");
29
+
30
+ // ── DEFAULTS ────────────────────────────────────────────────────────────────
31
+ //
32
+ // Source of truth: unattended-supervisor-contract.md §13.
33
+ // Any drift here from the contract is a contract violation.
34
+
35
+ const DEFAULTS = Object.freeze({
36
+ protectedBranches: [
37
+ "main",
38
+ "master",
39
+ "develop",
40
+ "trunk",
41
+ "release/*",
42
+ "hotfix/*",
43
+ ],
44
+ dirtyTreeWhitelist: [
45
+ ".gsd-t/heartbeat-*.jsonl",
46
+ ".gsd-t/.context-meter-state.json",
47
+ ".gsd-t/events/*.jsonl",
48
+ ".gsd-t/token-metrics.jsonl",
49
+ ".gsd-t/token-log.md",
50
+ ".gsd-t/.unattended/*",
51
+ ".gsd-t/.handoff/*",
52
+ ".claude/settings.local.json",
53
+ ".claude/settings.local.json.bak*",
54
+ ],
55
+ maxIterations: 200,
56
+ hours: 24,
57
+ gutterNoProgressIters: 5,
58
+ workerTimeoutMs: 3600000,
59
+ });
60
+
61
+ // ── Glob → regex helper ─────────────────────────────────────────────────────
62
+ //
63
+ // Minimal glob matcher: `*` matches any run of characters except `/`,
64
+ // `**` matches across path separators, everything else is a literal.
65
+ // No external dependency. Sufficient for the whitelist patterns in §13.
66
+
67
+ function globToRegex(glob) {
68
+ let re = "^";
69
+ for (let i = 0; i < glob.length; i++) {
70
+ const c = glob[i];
71
+ if (c === "*") {
72
+ if (glob[i + 1] === "*") {
73
+ re += ".*";
74
+ i++;
75
+ } else {
76
+ re += "[^/]*";
77
+ }
78
+ } else if (c === "?") {
79
+ re += "[^/]";
80
+ } else if ("\\^$+.()|{}[]".includes(c)) {
81
+ re += "\\" + c;
82
+ } else {
83
+ re += c;
84
+ }
85
+ }
86
+ re += "$";
87
+ return new RegExp(re);
88
+ }
89
+
90
+ function matchesAnyGlob(value, patterns) {
91
+ for (const pattern of patterns) {
92
+ if (globToRegex(pattern).test(value)) return true;
93
+ }
94
+ return false;
95
+ }
96
+
97
+ // ── loadConfig ──────────────────────────────────────────────────────────────
98
+ //
99
+ // Reads `.gsd-t/.unattended/config.json` if present, merges field-by-field
100
+ // over DEFAULTS, returns a plain object. Missing file → return a deep copy
101
+ // of DEFAULTS unchanged. Malformed JSON → throws a clear Error.
102
+
103
+ function cloneDefaults() {
104
+ // Defensive deep copy so callers can mutate the result without poisoning
105
+ // the frozen DEFAULTS singleton.
106
+ return {
107
+ protectedBranches: DEFAULTS.protectedBranches.slice(),
108
+ dirtyTreeWhitelist: DEFAULTS.dirtyTreeWhitelist.slice(),
109
+ maxIterations: DEFAULTS.maxIterations,
110
+ hours: DEFAULTS.hours,
111
+ gutterNoProgressIters: DEFAULTS.gutterNoProgressIters,
112
+ workerTimeoutMs: DEFAULTS.workerTimeoutMs,
113
+ };
114
+ }
115
+
116
+ function loadConfig(projectDir) {
117
+ const merged = cloneDefaults();
118
+ const configPath = path.join(
119
+ projectDir,
120
+ ".gsd-t",
121
+ ".unattended",
122
+ "config.json",
123
+ );
124
+ if (!fs.existsSync(configPath)) return merged;
125
+
126
+ let raw;
127
+ try {
128
+ raw = fs.readFileSync(configPath, "utf8");
129
+ } catch (err) {
130
+ throw new Error(
131
+ `safety-rails: failed to read config at ${configPath}: ${err.message}`,
132
+ );
133
+ }
134
+
135
+ let parsed;
136
+ try {
137
+ parsed = JSON.parse(raw);
138
+ } catch (err) {
139
+ throw new Error(
140
+ `safety-rails: malformed JSON in ${configPath}: ${err.message}`,
141
+ );
142
+ }
143
+
144
+ if (parsed && typeof parsed === "object") {
145
+ for (const key of Object.keys(merged)) {
146
+ if (parsed[key] !== undefined) {
147
+ merged[key] = parsed[key];
148
+ }
149
+ }
150
+ }
151
+ return merged;
152
+ }
153
+
154
+ // ── saveConfig ─────────────────────────────────────────────────────────────
155
+ //
156
+ // Persists the given config object back to `.gsd-t/.unattended/config.json`.
157
+ // Creates the directory if missing. Used by auto-whitelist to remember newly
158
+ // whitelisted dirty-tree entries so subsequent launches don't re-warn.
159
+
160
+ function saveConfig(projectDir, config) {
161
+ const configDir = path.join(projectDir, ".gsd-t", ".unattended");
162
+ const configPath = path.join(configDir, "config.json");
163
+ if (!fs.existsSync(configDir)) {
164
+ fs.mkdirSync(configDir, { recursive: true });
165
+ }
166
+ const serializable = {};
167
+ for (const key of Object.keys(DEFAULTS)) {
168
+ if (config[key] !== undefined) {
169
+ serializable[key] = config[key];
170
+ }
171
+ }
172
+ fs.writeFileSync(configPath, JSON.stringify(serializable, null, 2) + "\n");
173
+ }
174
+
175
+ // ── checkGitBranch ──────────────────────────────────────────────────────────
176
+ //
177
+ // Runs `git branch --show-current` in projectDir. An empty result indicates
178
+ // detached HEAD, which is treated as a protected-branch refusal (you cannot
179
+ // safely run unattended on a detached HEAD — there's no branch to push to).
180
+ // Otherwise, the current branch is matched against the protectedBranches
181
+ // list using glob semantics. Match → refuse with code 7.
182
+
183
+ function checkGitBranch(projectDir, config) {
184
+ const cfg = config || loadConfig(projectDir);
185
+ const result = spawnSync("git", ["branch", "--show-current"], {
186
+ cwd: projectDir,
187
+ encoding: "utf8",
188
+ });
189
+
190
+ if (result.error) {
191
+ return {
192
+ ok: false,
193
+ reason: `git branch --show-current failed: ${result.error.message}`,
194
+ code: 2,
195
+ };
196
+ }
197
+ if (result.status !== 0) {
198
+ return {
199
+ ok: false,
200
+ reason:
201
+ `git branch --show-current exited ${result.status}: ` +
202
+ (result.stderr || "").trim(),
203
+ code: 2,
204
+ };
205
+ }
206
+
207
+ const branch = (result.stdout || "").trim();
208
+ if (!branch) {
209
+ // Detached HEAD: refuse as protected.
210
+ return {
211
+ ok: false,
212
+ reason: "detached HEAD: refusing to run unattended without a branch",
213
+ code: 7,
214
+ branch: "",
215
+ };
216
+ }
217
+
218
+ if (matchesAnyGlob(branch, cfg.protectedBranches)) {
219
+ return {
220
+ ok: false,
221
+ reason: `branch '${branch}' is protected (matches one of: ${cfg.protectedBranches.join(", ")})`,
222
+ code: 7,
223
+ branch,
224
+ };
225
+ }
226
+
227
+ return { ok: true, branch };
228
+ }
229
+
230
+ // ── checkWorktreeCleanliness ────────────────────────────────────────────────
231
+ //
232
+ // Runs `git status --porcelain`, parses each line, filters whitelisted files
233
+ // per dirtyTreeWhitelist (glob-aware). If any non-whitelisted dirty file
234
+ // remains, refuse with code 8 and report the offenders.
235
+ //
236
+ // Fail-closed: any git failure → code 2 preflight-failure.
237
+
238
+ function parsePorcelainLine(line) {
239
+ // Porcelain v1 line shape: "XY path" (XY are 2 status chars, then a space).
240
+ // Renames look like "R old -> new" — return the destination path.
241
+ if (line.length < 4) return null;
242
+ let payload = line.slice(3);
243
+ const arrow = payload.indexOf(" -> ");
244
+ if (arrow !== -1) payload = payload.slice(arrow + 4);
245
+ // Strip optional surrounding quotes that git uses for special chars.
246
+ if (payload.startsWith('"') && payload.endsWith('"')) {
247
+ payload = payload.slice(1, -1);
248
+ }
249
+ return payload;
250
+ }
251
+
252
+ function checkWorktreeCleanliness(projectDir, config) {
253
+ const cfg = config || loadConfig(projectDir);
254
+ // `--untracked-files=all` expands untracked directories to individual file
255
+ // paths. Without this, git would summarize new dirs as ".gsd-t/" and we'd
256
+ // refuse a tree even when every file inside is whitelisted.
257
+ const result = spawnSync(
258
+ "git",
259
+ ["status", "--porcelain", "--untracked-files=all"],
260
+ { cwd: projectDir, encoding: "utf8" },
261
+ );
262
+
263
+ if (result.error) {
264
+ return {
265
+ ok: false,
266
+ reason: `git status --porcelain failed: ${result.error.message}`,
267
+ code: 2,
268
+ };
269
+ }
270
+ if (result.status !== 0) {
271
+ return {
272
+ ok: false,
273
+ reason:
274
+ `git status --porcelain exited ${result.status}: ` +
275
+ (result.stderr || "").trim(),
276
+ code: 2,
277
+ };
278
+ }
279
+
280
+ const stdout = (result.stdout || "").replace(/\r\n/g, "\n");
281
+ const lines = stdout.split("\n").filter((l) => l.length > 0);
282
+
283
+ const dirtyFiles = [];
284
+ for (const line of lines) {
285
+ const file = parsePorcelainLine(line);
286
+ if (!file) continue;
287
+ if (!matchesAnyGlob(file, cfg.dirtyTreeWhitelist)) {
288
+ dirtyFiles.push(file);
289
+ }
290
+ }
291
+
292
+ if (dirtyFiles.length > 0) {
293
+ return {
294
+ ok: false,
295
+ reason:
296
+ `worktree has ${dirtyFiles.length} non-whitelisted dirty file(s): ` +
297
+ dirtyFiles.slice(0, 5).join(", ") +
298
+ (dirtyFiles.length > 5 ? ", …" : ""),
299
+ code: 8,
300
+ dirtyFiles,
301
+ };
302
+ }
303
+
304
+ return { ok: true };
305
+ }
306
+
307
+ // ── checkIterationCap ───────────────────────────────────────────────────────
308
+ //
309
+ // Pre-worker hook: refuses to spawn another worker if the iteration count has
310
+ // reached the configured cap. Contract §12 lists this under the pre-worker
311
+ // hook. The cap is resolved with the precedence:
312
+ // 1. config.maxIterations (explicit override)
313
+ // 2. DEFAULTS.maxIterations (hardcoded contract default)
314
+ // 3. state.maxIterations (fallback — what the supervisor was launched with)
315
+ //
316
+ // Iteration cap is a soft gutter (not a crash), so it surfaces as code 6
317
+ // per contract §5.
318
+
319
+ function checkIterationCap(state, config) {
320
+ const cap =
321
+ (config && typeof config.maxIterations === "number"
322
+ ? config.maxIterations
323
+ : undefined) ??
324
+ DEFAULTS.maxIterations ??
325
+ (state && state.maxIterations);
326
+
327
+ if (typeof cap !== "number" || !Number.isFinite(cap)) {
328
+ return {
329
+ ok: false,
330
+ reason: "iteration cap is not a finite number",
331
+ code: 2,
332
+ };
333
+ }
334
+
335
+ const iter = state && typeof state.iter === "number" ? state.iter : 0;
336
+ if (iter < cap) {
337
+ return { ok: true };
338
+ }
339
+ return {
340
+ ok: false,
341
+ reason: `iteration cap exceeded: iter=${iter} >= maxIterations=${cap}`,
342
+ code: 6,
343
+ iter,
344
+ maxIterations: cap,
345
+ };
346
+ }
347
+
348
+ // ── checkWallClockCap ───────────────────────────────────────────────────────
349
+ //
350
+ // Pre-worker hook: refuses to spawn another worker if the total elapsed wall
351
+ // clock time has reached the configured hours cap. The cap is resolved with
352
+ // the precedence:
353
+ // 1. config.hours (explicit override)
354
+ // 2. DEFAULTS.hours (hardcoded contract default — 24)
355
+ //
356
+ // wallClockElapsedMs is an integer on state.json (§3). The cap is converted
357
+ // to milliseconds for comparison. Wall-clock cap is a soft gutter → code 6.
358
+
359
+ function checkWallClockCap(state, config) {
360
+ const hours =
361
+ config && typeof config.hours === "number"
362
+ ? config.hours
363
+ : DEFAULTS.hours;
364
+
365
+ if (typeof hours !== "number" || !Number.isFinite(hours) || hours <= 0) {
366
+ return {
367
+ ok: false,
368
+ reason: "wall-clock cap (hours) is not a positive finite number",
369
+ code: 2,
370
+ };
371
+ }
372
+
373
+ const capMs = hours * 3600 * 1000;
374
+ const elapsedMs =
375
+ state && typeof state.wallClockElapsedMs === "number"
376
+ ? state.wallClockElapsedMs
377
+ : 0;
378
+
379
+ if (elapsedMs < capMs) {
380
+ return { ok: true };
381
+ }
382
+ return {
383
+ ok: false,
384
+ reason:
385
+ `wall-clock cap exceeded: elapsedMs=${elapsedMs} >= capMs=${capMs} ` +
386
+ `(hours=${hours})`,
387
+ code: 6,
388
+ elapsedMs,
389
+ capMs,
390
+ };
391
+ }
392
+
393
+ // ── validateState ───────────────────────────────────────────────────────────
394
+ //
395
+ // Pure schema validator for state.json. Checks every REQUIRED field from
396
+ // contract §3, verifies types, and validates the status enum from §4.
397
+ //
398
+ // Aggregates errors (does NOT fail-fast) so the caller can surface every
399
+ // problem in a single preflight refusal. Returns code 2 (preflight-failure)
400
+ // per contract §5 on any failure.
401
+
402
+ const STATUS_ENUM = Object.freeze([
403
+ "initializing",
404
+ "running",
405
+ "done",
406
+ "failed",
407
+ "stopped",
408
+ "crashed",
409
+ ]);
410
+
411
+ const PLATFORM_ENUM = Object.freeze(["darwin", "linux", "win32"]);
412
+
413
+ // Required fields per contract §3, with an expected-type tag. We check the
414
+ // supervisor-critical fields listed in the Task 2 acceptance criteria.
415
+ const REQUIRED_STATE_FIELDS = Object.freeze([
416
+ { name: "version", type: "string" },
417
+ { name: "sessionId", type: "string" },
418
+ { name: "projectDir", type: "string" },
419
+ { name: "status", type: "string" }, // enum-validated below
420
+ { name: "milestone", type: "string" },
421
+ { name: "iter", type: "integer" },
422
+ { name: "maxIterations", type: "integer" },
423
+ { name: "startedAt", type: "string" },
424
+ { name: "lastTick", type: "string" },
425
+ { name: "hours", type: "number" },
426
+ { name: "wallClockElapsedMs", type: "integer" },
427
+ { name: "supervisorPid", type: "integer" },
428
+ { name: "logPath", type: "string" },
429
+ { name: "platform", type: "string" }, // enum-validated below
430
+ { name: "claudeBin", type: "string" },
431
+ ]);
432
+
433
+ function typeMatches(value, expected) {
434
+ if (value === undefined || value === null) return false;
435
+ switch (expected) {
436
+ case "string":
437
+ return typeof value === "string";
438
+ case "number":
439
+ return typeof value === "number" && Number.isFinite(value);
440
+ case "integer":
441
+ return (
442
+ typeof value === "number" &&
443
+ Number.isFinite(value) &&
444
+ Number.isInteger(value)
445
+ );
446
+ default:
447
+ return false;
448
+ }
449
+ }
450
+
451
+ function validateState(state) {
452
+ const errors = [];
453
+
454
+ if (!state || typeof state !== "object" || Array.isArray(state)) {
455
+ return {
456
+ ok: false,
457
+ code: 2,
458
+ reason: "state-validation-failed",
459
+ errors: ["state must be a non-null object"],
460
+ };
461
+ }
462
+
463
+ for (const field of REQUIRED_STATE_FIELDS) {
464
+ if (!(field.name in state)) {
465
+ errors.push(`${field.name}: missing required field`);
466
+ continue;
467
+ }
468
+ const value = state[field.name];
469
+ if (!typeMatches(value, field.type)) {
470
+ errors.push(
471
+ `${field.name}: expected ${field.type}, got ${
472
+ value === null ? "null" : typeof value
473
+ }`,
474
+ );
475
+ }
476
+ }
477
+
478
+ // Status enum check — only if the field was present and a string. Skip if
479
+ // we already flagged it as a type error to avoid double reporting.
480
+ if (typeof state.status === "string" && !STATUS_ENUM.includes(state.status)) {
481
+ errors.push(
482
+ `status: invalid enum value '${state.status}' (expected one of: ${STATUS_ENUM.join(", ")})`,
483
+ );
484
+ }
485
+
486
+ // Platform enum check.
487
+ if (
488
+ typeof state.platform === "string" &&
489
+ !PLATFORM_ENUM.includes(state.platform)
490
+ ) {
491
+ errors.push(
492
+ `platform: invalid enum value '${state.platform}' (expected one of: ${PLATFORM_ENUM.join(", ")})`,
493
+ );
494
+ }
495
+
496
+ // Value sanity — only if the field was present and numeric.
497
+ if (typeof state.iter === "number" && Number.isInteger(state.iter) && state.iter < 0) {
498
+ errors.push(`iter: must be >= 0, got ${state.iter}`);
499
+ }
500
+ if (
501
+ typeof state.maxIterations === "number" &&
502
+ Number.isInteger(state.maxIterations) &&
503
+ state.maxIterations <= 0
504
+ ) {
505
+ errors.push(
506
+ `maxIterations: must be > 0, got ${state.maxIterations}`,
507
+ );
508
+ }
509
+ if (
510
+ typeof state.wallClockElapsedMs === "number" &&
511
+ Number.isInteger(state.wallClockElapsedMs) &&
512
+ state.wallClockElapsedMs < 0
513
+ ) {
514
+ errors.push(
515
+ `wallClockElapsedMs: must be >= 0, got ${state.wallClockElapsedMs}`,
516
+ );
517
+ }
518
+
519
+ if (errors.length > 0) {
520
+ return {
521
+ ok: false,
522
+ code: 2,
523
+ reason: "state-validation-failed",
524
+ errors,
525
+ };
526
+ }
527
+
528
+ return { ok: true };
529
+ }
530
+
531
+ // ── detectGutter ────────────────────────────────────────────────────────────
532
+ //
533
+ // Post-worker hook: scans the tail of run.log plus the supervisor state for
534
+ // three stall patterns. A positive detection returns code 6 (gutter-detected)
535
+ // per contract §5. Pure function — NO filesystem reads. The caller is
536
+ // responsible for passing the run-log tail as a string (typically last ~200
537
+ // lines) and the current state object.
538
+ //
539
+ // The three patterns:
540
+ //
541
+ // 1. repeated-error
542
+ // Extract error lines (regex /error[:\s].*$/im) grouped by iteration
543
+ // block (headers of the form "--- ITER N ---"). If the same error
544
+ // signature appears in the last `gutterThreshold` (default 3) consecutive
545
+ // iteration blocks, flag it.
546
+ //
547
+ // 2. file-thrash
548
+ // Count `Edit(`/`Write(` tool operations per file across iteration
549
+ // blocks. Heuristic: if the top file appears in >= `gutterThreshold`
550
+ // iterations AND accounts for a dominant share of edits, flag it. This
551
+ // is intentionally a cheap approximation — see §12 of the contract which
552
+ // lists gutter detection as implementation-owned.
553
+ //
554
+ // 3. no-progress
555
+ // If the caller passes `state.progressHash` and `state.progressHashHistory`
556
+ // (an array of the last N hashes, one per iter), and the last
557
+ // `gutterWindow` (default 5) hashes are all identical AND state.iter has
558
+ // advanced by at least `gutterWindow`, flag it. Callers without history
559
+ // can omit this signal and the function will skip the no-progress check
560
+ // (low false-positive design).
561
+ //
562
+ // Config fields consumed (all optional):
563
+ // - gutterThreshold (default 3) — min consecutive iters for pattern
564
+ // - gutterWindow (default 5) — lookback window for no-progress
565
+ // - gutterNoProgressIters (default 5) — alias for gutterWindow from §13
566
+
567
+ const ITER_HEADER_RE = /^---\s*ITER\s+(\d+)\s*---/im;
568
+
569
+ function splitIterBlocks(runLogTail) {
570
+ // Split the tail into blocks keyed by the "--- ITER N ---" header. Content
571
+ // before the first header is discarded (it belongs to an iteration we don't
572
+ // have full visibility into).
573
+ if (typeof runLogTail !== "string" || runLogTail.length === 0) return [];
574
+ const lines = runLogTail.split(/\r?\n/);
575
+ const blocks = [];
576
+ let current = null;
577
+ for (const line of lines) {
578
+ const m = line.match(ITER_HEADER_RE);
579
+ if (m) {
580
+ if (current) blocks.push(current);
581
+ current = { iter: Number(m[1]), lines: [] };
582
+ } else if (current) {
583
+ current.lines.push(line);
584
+ }
585
+ }
586
+ if (current) blocks.push(current);
587
+ return blocks;
588
+ }
589
+
590
+ // Extract the first error-looking line from a block and normalize it into a
591
+ // signature for equality comparison. Returns null if no error line found.
592
+ function extractErrorSignature(block) {
593
+ for (const line of block.lines) {
594
+ const m = line.match(/error[:\s]+(.+)$/i);
595
+ if (m) {
596
+ // Normalize whitespace and strip volatile numeric/path suffixes so that
597
+ // two errors that differ only by line number still match.
598
+ return m[1]
599
+ .replace(/\s+/g, " ")
600
+ .replace(/\b\d+\b/g, "N")
601
+ .replace(/[/\\][\w./\\-]+/g, "PATH")
602
+ .trim()
603
+ .slice(0, 200);
604
+ }
605
+ }
606
+ return null;
607
+ }
608
+
609
+ // Count `Edit(path=...)` / `Write(file_path=...)` mentions per file in a
610
+ // block. The exact tool-call serialization isn't standardized, so we fall back
611
+ // to a loose match: `(Edit|Write)\s*\(\s*(?:file_path|path)?\s*=?\s*['"]([^'"]+)['"]`
612
+ const TOOL_CALL_RE =
613
+ /(?:Edit|Write)\s*\(\s*(?:file_path|path)?\s*=?\s*['"]([^'"]+)['"]/gi;
614
+
615
+ function extractEditedFiles(block) {
616
+ const files = new Set();
617
+ const text = block.lines.join("\n");
618
+ let m;
619
+ while ((m = TOOL_CALL_RE.exec(text)) !== null) {
620
+ files.add(m[1]);
621
+ }
622
+ return files;
623
+ }
624
+
625
+ function detectGutter(state, runLogTail, config) {
626
+ const cfg = config || {};
627
+ const threshold =
628
+ typeof cfg.gutterThreshold === "number" && cfg.gutterThreshold > 0
629
+ ? cfg.gutterThreshold
630
+ : 3;
631
+ const window =
632
+ typeof cfg.gutterWindow === "number" && cfg.gutterWindow > 0
633
+ ? cfg.gutterWindow
634
+ : typeof cfg.gutterNoProgressIters === "number" &&
635
+ cfg.gutterNoProgressIters > 0
636
+ ? cfg.gutterNoProgressIters
637
+ : DEFAULTS.gutterNoProgressIters;
638
+
639
+ const blocks = splitIterBlocks(runLogTail || "");
640
+
641
+ // ── Pattern 1: repeated-error ─────────────────────────────────────────────
642
+ if (blocks.length >= threshold) {
643
+ const recent = blocks.slice(-threshold);
644
+ const sigs = recent.map(extractErrorSignature);
645
+ if (sigs.every((s) => s !== null) && sigs.every((s) => s === sigs[0])) {
646
+ return {
647
+ ok: false,
648
+ code: 6,
649
+ reason: "gutter-detected",
650
+ pattern: "repeated-error",
651
+ details: {
652
+ signature: sigs[0],
653
+ consecutiveIters: threshold,
654
+ iters: recent.map((b) => b.iter),
655
+ },
656
+ };
657
+ }
658
+ }
659
+
660
+ // ── Pattern 2: file-thrash ────────────────────────────────────────────────
661
+ // Heuristic: for each file, count how many of the last `threshold` blocks
662
+ // it was edited in. If any file appears in ALL `threshold` recent blocks AND
663
+ // the top file accounts for a dominant share of edits (>= 50% of total
664
+ // file-block pairs), flag it. This catches "keeps editing the same 2-3
665
+ // files over and over" stalls without firing on normal healthy multi-file
666
+ // churn.
667
+ if (blocks.length >= threshold) {
668
+ const recent = blocks.slice(-threshold);
669
+ const fileCounts = new Map(); // file -> number of blocks it appears in
670
+ let totalPairs = 0;
671
+ for (const block of recent) {
672
+ const files = extractEditedFiles(block);
673
+ for (const f of files) {
674
+ fileCounts.set(f, (fileCounts.get(f) || 0) + 1);
675
+ totalPairs += 1;
676
+ }
677
+ }
678
+ // Find files that appear in every block.
679
+ const persistent = Array.from(fileCounts.entries())
680
+ .filter(([, count]) => count >= threshold)
681
+ .sort((a, b) => b[1] - a[1]);
682
+ if (persistent.length > 0 && totalPairs > 0) {
683
+ const topShare =
684
+ persistent.reduce((sum, [, c]) => sum + c, 0) / totalPairs;
685
+ // Dominant = the persistent files account for >= 50% of all edit
686
+ // activity in the window. Tuned for low false-positive rate.
687
+ if (topShare >= 0.5) {
688
+ return {
689
+ ok: false,
690
+ code: 6,
691
+ reason: "gutter-detected",
692
+ pattern: "file-thrash",
693
+ details: {
694
+ files: persistent.map(([f]) => f),
695
+ window: threshold,
696
+ dominantShare: Number(topShare.toFixed(2)),
697
+ },
698
+ };
699
+ }
700
+ }
701
+ }
702
+
703
+ // ── Pattern 3: no-progress ────────────────────────────────────────────────
704
+ // Only runs when caller supplies progressHashHistory. Absence = skip
705
+ // (low-false-positive design — we'd rather miss a stall than flag a
706
+ // healthy run as stalled).
707
+ if (
708
+ state &&
709
+ Array.isArray(state.progressHashHistory) &&
710
+ state.progressHashHistory.length >= window &&
711
+ typeof state.iter === "number"
712
+ ) {
713
+ const history = state.progressHashHistory.slice(-window);
714
+ const first = history[0];
715
+ if (first && history.every((h) => h === first)) {
716
+ return {
717
+ ok: false,
718
+ code: 6,
719
+ reason: "gutter-detected",
720
+ pattern: "no-progress",
721
+ details: {
722
+ unchangedHash: first,
723
+ window,
724
+ iter: state.iter,
725
+ },
726
+ };
727
+ }
728
+ }
729
+
730
+ return { ok: true };
731
+ }
732
+
733
+ // ── detectBlockerSentinel ───────────────────────────────────────────────────
734
+ //
735
+ // Post-worker hook: scans the run.log tail for sentinel strings the worker
736
+ // emits when it hits a human-gated blocker (destructive action guard, waiting
737
+ // for user input, etc.). A match halts the supervisor with code 6 and the
738
+ // matched pattern string so the watch loop can surface it to the user.
739
+
740
+ const BLOCKER_SENTINEL_PATTERNS = Object.freeze([
741
+ /\bblocked\s+needs\s+human\b/i,
742
+ /\bblocker:\s+.+$/im,
743
+ /\bdestructive\s+action\s+guard\b/i,
744
+ /\bwaiting\s+for\s+user\b/i,
745
+ ]);
746
+
747
+ function detectBlockerSentinel(runLogTail) {
748
+ if (typeof runLogTail !== "string" || runLogTail.length === 0) {
749
+ return { ok: true };
750
+ }
751
+ for (const re of BLOCKER_SENTINEL_PATTERNS) {
752
+ const m = runLogTail.match(re);
753
+ if (m) {
754
+ return {
755
+ ok: false,
756
+ code: 6,
757
+ reason: "blocker-sentinel-detected",
758
+ pattern: re.source,
759
+ matchedText: m[0].slice(0, 200),
760
+ };
761
+ }
762
+ }
763
+ return { ok: true };
764
+ }
765
+
766
+ module.exports = {
767
+ DEFAULTS,
768
+ loadConfig,
769
+ saveConfig,
770
+ checkGitBranch,
771
+ checkWorktreeCleanliness,
772
+ checkIterationCap,
773
+ checkWallClockCap,
774
+ validateState,
775
+ detectGutter,
776
+ detectBlockerSentinel,
777
+ // Internal helpers exported for unit tests.
778
+ _globToRegex: globToRegex,
779
+ _matchesAnyGlob: matchesAnyGlob,
780
+ _parsePorcelainLine: parsePorcelainLine,
781
+ _splitIterBlocks: splitIterBlocks,
782
+ _extractErrorSignature: extractErrorSignature,
783
+ _extractEditedFiles: extractEditedFiles,
784
+ _BLOCKER_SENTINEL_PATTERNS: BLOCKER_SENTINEL_PATTERNS,
785
+ _STATUS_ENUM: STATUS_ENUM,
786
+ _PLATFORM_ENUM: PLATFORM_ENUM,
787
+ _REQUIRED_STATE_FIELDS: REQUIRED_STATE_FIELDS,
788
+ };