@tekyzinc/gsd-t 2.76.10 → 3.10.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 CHANGED
@@ -2,6 +2,69 @@
2
2
 
3
3
  All notable changes to GSD-T are documented here. Updated with each release.
4
4
 
5
+ ## [3.10.11] - 2026-04-15
6
+
7
+ ### Added
8
+ - `docs/unattended-config.md` — full schema and recipe reference for `.gsd-t/.unattended/config.json`. The supervisor has always loaded this file (M36 safety rails), but there was no user-facing doc explaining the schema, precedence, or common overrides.
9
+ - `commands/gsd-t-unattended.md` Step 1c cross-references the new config doc and calls out the solo-project recipe (`{"protectedBranches": []}` to disable the main/master guard).
10
+
11
+ ### Fixed
12
+ - Flaky test: `scripts/gsd-t-context-meter.e2e.test.js` `HARD_TIMEOUT_MS` bumped 6000 → 12000ms. The hook child process runs fine in 30ms in isolation but was timing out under full-suite parallelism load on some machines. No behavioral change — just a more forgiving outer cap.
13
+ - `commands/gsd-t-unattended.md` gained Step 1e: pre-flight software check that hard-fails the launch if `node`, `claude`, or `git` are missing, and prints soft warnings for missing platform helpers (`caffeinate` on darwin; `systemd-inhibit`/`notify-send` on linux; BurntToast advisory on win32). Replaces the previous "crash mid-run when a helper is missing" behavior with fail-fast + actionable install instructions.
14
+ - `docs/unattended-windows-caveats.md` added §0 "Required Software" matrix listing hard-required and soft-recommended tools per platform.
15
+
16
+ ### Notes
17
+ - No API or contract changes. `.gsd-t/.unattended/config.json` loader and precedence (CLI > env > config > defaults) were already built into M36 safety rails — this release only surfaces them in documentation.
18
+
19
+ ## [3.10.10] - 2026-04-15
20
+
21
+ ### Major version bump: 2.x → 3.x
22
+
23
+ M36 ships the third pillar of the context/runway/autonomy arc (M34 context meter → M35 no-silent-degradation → M36 unattended supervisor). Cumulatively these three milestones are substantial enough to mark a new major version. No breaking API changes — existing commands and contracts continue to work — but v3.x establishes "unattended-capable" as the default expectation for the harness. Semver major bump also aligns with the "always 2-digit minor and patch" display convention (Minor and Patch start at 10 after a major reset).
24
+
25
+ ### M36: Unattended Supervisor — Zero-Human-Intervention Milestone Execution
26
+
27
+ **Background**: M35 introduced headless auto-spawn to continue a single runway-exhausted session in a fresh context. M36 generalizes this into a first-class long-running supervisor: a detached OS-level process that drives the active GSD-T milestone to completion over hours or days via a `claude -p` worker relay. Each worker runs in its own fresh context window; the supervisor survives terminal close, `/clear`, and sleep/wake cycles. A 270-second ScheduleWakeup watch loop in the interactive session provides live status without blocking the user.
28
+
29
+ ### Added
30
+
31
+ - **`bin/gsd-t-unattended.js`** — detached supervisor process. Spawns `claude -p` workers in a relay, writes atomic `state.json` between iterations, manages `supervisor.pid` lifecycle, invokes safety rails at hook points, sends OS notifications on terminal transitions (macOS `osascript`; silent no-op on other platforms), and removes its own PID file on any exit. Singleton: a second launch with a live PID refuses.
32
+ - **`bin/gsd-t-unattended-safety.js`** — safety rails module. Exports: `checkGitBranch` (protected branch list; configurable), `checkWorktreeCleanliness` (dirty-tree guard with whitelist), `checkIterationCap`, `checkWallClockCap`, `validateState`, `detectBlockerSentinel` (scan run.log tail for unrecoverable/dispatch-failed patterns), `detectGutter` (repeated-error / file-thrash / no-progress stall detection). Each check returns `{ ok, reason?, code? }`.
33
+ - **`bin/gsd-t-unattended-platform.js`** — platform abstraction. Exports: `spawnSupervisor` (detached spawn with `windowsHide`), `preventSleep` / `releaseSleep` (`caffeinate -i` on darwin; no-op on linux/win32), `sendNotification` (osascript on darwin; `notify-send` on linux; toast via PowerShell on win32 — all graceful no-op on failure), `resolveClaudeBin` (`claude.cmd` on win32; `claude` elsewhere + PATH search), `getPlatform`.
34
+ - **`bin/handoff-lock.js`** — parent/child race guard for headless-auto-spawn. Writes `.gsd-t/.handoff/{session-id}.lock` before detaching; child removes on first iteration. Prevents the parent from reporting "failed" while the child is still starting. Exports: `acquireLock`, `releaseLock`, `waitForRelease`, `isLocked`.
35
+ - **`commands/gsd-t-unattended.md`** — `/user:gsd-t-unattended` launch command. Pre-flights (singleton check, safety rails, active milestone), spawns the supervisor via `bin/gsd-t-unattended-platform.js`, polls for `supervisor.pid` + `status=running` (up to 5s), prints the initial watch block, calls `ScheduleWakeup(270, '/user:gsd-t-unattended-watch')`.
36
+ - **`commands/gsd-t-unattended-watch.md`** — `/user:gsd-t-unattended-watch` watch tick. Stateless; reads `supervisor.pid` + `state.json`; renders progress or final summary; reschedules via `ScheduleWakeup(270, ...)` on non-terminal status; stops on terminal status or missing PID file.
37
+ - **`commands/gsd-t-unattended-stop.md`** — `/user:gsd-t-unattended-stop` stop command. Touches `.gsd-t/.unattended/stop` sentinel; prints reassurance; returns immediately (no kill, no wait).
38
+ - **`.gsd-t/contracts/unattended-supervisor-contract.md` v1.0.0 ACTIVE** — authoritative interface for state file schema (18 fields), PID file lifecycle, sentinel semantics, exit codes 0–8+124, launch handshake, watch tick decision tree, resume auto-reattach handshake, stop mechanism, notification levels, safety rails hook points, and configuration file schema.
39
+ - **`docs/unattended-windows-caveats.md`** — known Windows limitations: sleep-prevention not supported (no `caffeinate` equivalent wired; `powercfg /requests` path is v2), `claude.cmd` wrapper adds ~500ms per spawn, Windows Defender may scan each worker spawn, notification via PowerShell toast requires non-interactive shell workaround.
40
+ - **`.claude/settings.json`** (project-shared) — SessionStart hook registered: `node bin/check-headless-sessions.js . 2>/dev/null || true` surfaces completed headless session banners on session start.
41
+
42
+ ### Changed
43
+
44
+ - **`commands/gsd-t-wave.md`** — "Run /clear" STOP block removed from the runway-exceeded handoff path. The command now calls `autoSpawnHeadless()` seamlessly; user never sees a manual-intervention prompt under normal runway overflow.
45
+ - **`commands/gsd-t-execute.md`**, **`gsd-t-quick.md`**, **`gsd-t-integrate.md`**, **`gsd-t-debug.md`** — same "Run /clear" prompt removal; headless auto-spawn wired in.
46
+ - **`commands/gsd-t-resume.md`** — new Step 0 "Unattended Supervisor Auto-Reattach": checks `supervisor.pid`; if live and non-terminal, skips normal resume and re-starts the watch loop. New Step 0.2 "Handoff Lock Wait": polls until `.gsd-t/.handoff/*.lock` is released (headless child has taken ownership) before proceeding.
47
+
48
+ ### Fixed
49
+
50
+ - **`bin/gsd-t.js` headless dispatch** (Phase 0 P0, committed prior milestone): `mapHeadlessExitCode` now maps `"Unknown command:"` in worker stdout → exit code 5 (`command-dispatch-failed`). Worker invocation no longer prepends `/user:` to command names, preventing "Unknown command:" failures in non-interactive `claude -p` sessions.
51
+
52
+ ### Tests
53
+
54
+ - `test/unattended-supervisor.test.js` — 42 tests: happy-path relay, gutter halt, stop sentinel, dispatch-failure halt, crash detection, dirty-tree pre-flight refusal.
55
+ - `test/unattended-safety.test.js` — 18 tests: each check function, combined pre-flight, gutter threshold config.
56
+ - `test/unattended-platform.test.js` — 14 tests: platform detection, spawn flags, sleep-prevention no-op on linux, claude binary resolution.
57
+ - `test/handoff-lock.test.js` — 16 tests: acquire/release, race prevention, waitForRelease timeout, stale lock cleanup.
58
+ - `test/headless-auto-spawn.test.js` — +9 tests (new handoff-lock integration cases added to existing suite).
59
+ - `test/filesystem.test.js` — counts updated to reflect new files (+6 bin files, +3 command files, +1 docs file).
60
+ - **Total**: 1146 → 1226 (+80 new tests).
61
+
62
+ ### Migration
63
+
64
+ After `npm install @tekyzinc/gsd-t@3.10.10`, run `/user:gsd-t-version-update-all` to propagate v3.10.10 to all registered projects. The new command files, `bin/` modules, and contract are written into each project automatically. No existing `.gsd-t/` state is modified.
65
+
66
+ ---
67
+
5
68
  ## [2.76.10] - 2026-04-15
6
69
 
7
70
  ### M35: Runway-Protected Execution — Aggressive Pause-Resume Replaces Graduated Degradation
package/README.md CHANGED
@@ -10,6 +10,7 @@ A methodology for reliable, parallelizable development using Claude Code with op
10
10
  **Protects existing work** — destructive action guard prevents schema drops, architecture replacements, and data loss without explicit approval.
11
11
  **Visualizes execution in real time** — live browser dashboard renders agent hierarchy, tool activity, and phase progression from the event stream.
12
12
  **Generates visual scan reports** — every `/gsd-t-scan` produces a self-contained HTML report with 6 live architectural diagrams, a tech debt register, and domain health scores; optional DOCX/PDF export via `--export docx|pdf`.
13
+ **Unattended execution** — `gsd-t unattended --hours=N` spawns a detached OS-process supervisor that drives the active milestone to completion over hours or days with zero human intervention. A ScheduleWakeup watch loop ticks every 270 seconds; `/clear` + resume transparently re-attaches to the running supervisor.
13
14
  **Self-learning rule engine** — declarative rules in rules.jsonl detect failure patterns from task metrics. Candidate patches progress through a 5-stage lifecycle (candidate, applied, measured, promoted, graduated) with >55% improvement gates before becoming permanent methodology artifacts.
14
15
  **Cross-project learning** — proven rules propagate to `~/.claude/metrics/` and sync across all registered projects via `update-all`. Rules validated in 3+ projects become universal; 5+ projects qualify for npm distribution. Cross-project signal comparison and global ELO rankings available via `gsd-t-metrics --cross-project` and `gsd-t-status`.
15
16
  **Stack Rules Engine** — auto-detects project tech stack (React, TypeScript, Node API, Python, Go, Rust) from manifest files and injects mandatory best-practice rules into subagent prompts at execute-time. Universal security rules always apply; stack-specific rules layer on top. Includes **design-to-code** rules for pixel-perfect frontend implementation from Figma, screenshots, or design images — with Figma MCP integration, design token extraction, stack capability evaluation, and mandatory visual verification: every screen is rendered in a real browser, screenshotted at mobile/tablet/desktop, and compared pixel-by-pixel against the Figma design. Auto-bootstraps during partition when design references are detected. Extensible: drop a `.md` file in `templates/stacks/` to add a new stack.
@@ -175,6 +176,14 @@ This will replace changed command files, back up your CLAUDE.md if customized, a
175
176
  | `/user:gsd-t-verify` | Run quality gates + goal-backward behavior verification | In wave |
176
177
  | `/user:gsd-t-complete-milestone` | Archive + git tag (goal-backward gate required) | In wave |
177
178
 
179
+ ### Unattended Execution
180
+
181
+ | Command | Purpose | Auto |
182
+ |---------|---------|------|
183
+ | `/user:gsd-t-unattended` | Launch detached supervisor — runs active milestone to completion with zero human intervention | Manual |
184
+ | `/user:gsd-t-unattended-watch` | Watch tick — fires every 270s via ScheduleWakeup, reports supervisor status | Auto |
185
+ | `/user:gsd-t-unattended-stop` | Touch stop sentinel — supervisor halts after current worker finishes | Manual |
186
+
178
187
  ### Automation & Utilities
179
188
 
180
189
  | Command | Purpose | Auto |
@@ -311,6 +320,43 @@ your-project/
311
320
 
312
321
  ---
313
322
 
323
+ ## Unattended Execution (M36 — v3.10.10+)
324
+
325
+ Run the active milestone to completion over hours or days — no human in the loop.
326
+
327
+ ```bash
328
+ # Launch from the CLI (detached OS process)
329
+ gsd-t unattended --hours=24
330
+
331
+ # Or from within Claude Code (starts a 270s watch loop)
332
+ /user:gsd-t-unattended
333
+
334
+ # Stop (graceful — supervisor halts after the current worker finishes)
335
+ /user:gsd-t-unattended-stop
336
+ ```
337
+
338
+ **How it works:**
339
+
340
+ - `gsd-t unattended` spawns `bin/gsd-t-unattended.js` as a fully detached OS process. The supervisor runs `claude -p` workers in a relay — one worker per iteration — each in a fresh context window. State is written atomically to `.gsd-t/.unattended/state.json` between iterations.
341
+ - `/user:gsd-t-unattended` does the same from inside Claude Code, then calls `ScheduleWakeup(270, '/user:gsd-t-unattended-watch')` to start an in-session watch loop that ticks every 270 seconds and prints progress.
342
+ - If you run `/clear` + `/user:gsd-t-resume` during a live run, the resume command auto-detects the running supervisor and re-attaches the watch loop — no re-launch needed.
343
+ - The supervisor halts automatically when: the milestone reaches COMPLETED status, the `--hours` wall-clock cap expires, `--max-iterations` is reached, safety rails detect a stall or unrecoverable error, or the stop sentinel is touched.
344
+
345
+ **Platform support:** macOS and Linux fully supported (including sleep-prevention via `caffeinate` on macOS). Windows is supported except sleep-prevention. See `docs/unattended-windows-caveats.md` for known Windows limitations.
346
+
347
+ **Key flags:**
348
+
349
+ | Flag | Default | Description |
350
+ |------|---------|-------------|
351
+ | `--hours=N` | 24 | Wall-clock cap |
352
+ | `--max-iterations=N` | 200 | Iteration cap |
353
+ | `--milestone=NAME` | (current) | Override active milestone |
354
+ | `--dry-run` | false | Preflight only — no spawn |
355
+
356
+ **State files** live under `.gsd-t/.unattended/`: `supervisor.pid`, `state.json`, `run.log`, `stop` (sentinel). Authoritative field definitions: `.gsd-t/contracts/unattended-supervisor-contract.md` v1.0.0.
357
+
358
+ ---
359
+
314
360
  ## Context Meter Setup (M34 — v2.75.10+)
315
361
 
316
362
  The Context Meter replaces the v2.74.12 task-counter proxy with real context-window measurement via the Anthropic `count_tokens` API. This is the authoritative signal for session-stop gates in `gsd-t-execute`, `gsd-t-wave`, `gsd-t-quick`, `gsd-t-integrate`, and `gsd-t-debug`.
@@ -0,0 +1,381 @@
1
+ /**
2
+ * gsd-t-unattended-platform.js
3
+ *
4
+ * Cross-platform helpers for the unattended supervisor (M36).
5
+ *
6
+ * This module is the SINGLE place where `process.platform` branches live.
7
+ * Supervisor-core, watch-loop, and safety-rails import from here so that
8
+ * the rest of the supervisor can stay platform-agnostic.
9
+ *
10
+ * Contract: .gsd-t/contracts/unattended-supervisor-contract.md v1.0.0
11
+ * §5 Exit Code Table (timeout = 3, OS process-timeout = 124)
12
+ * §7 Launch Handshake (spawn semantics)
13
+ *
14
+ * Task 1 of m36-cross-platform delivers:
15
+ * - resolveClaudePath()
16
+ * - isAlive(pid)
17
+ * - spawnWorker(projectDir, timeoutMs)
18
+ *
19
+ * Cross-platform notes:
20
+ * - darwin / linux paths are runtime-tested.
21
+ * - win32 paths are implementation-complete but NOT runtime-tested on the
22
+ * dev host (macOS). Spike C and the full Windows caveats matrix ship in
23
+ * Task 3 (`docs/unattended-windows-caveats.md`).
24
+ *
25
+ * Zero external dependencies — Node built-ins only.
26
+ */
27
+
28
+ "use strict";
29
+
30
+ const { spawnSync, spawn } = require("node:child_process");
31
+
32
+ // ─── resolveClaudePath ───────────────────────────────────────────────────────
33
+
34
+ /**
35
+ * Resolve the executable name for the `claude` CLI on the current platform.
36
+ *
37
+ * Returns `'claude.cmd'` on win32 and `'claude'` everywhere else.
38
+ *
39
+ * This is intentionally a simple platform branch — it does NOT shell out to
40
+ * `which` / `where`. The resolver assumes `claude` is on PATH; PATH lookup is
41
+ * delegated to `spawnSync`, which is cross-platform and quoting-safe.
42
+ *
43
+ * Cross-platform:
44
+ * - darwin / linux: returns `'claude'`. The macOS / Linux installer puts
45
+ * `claude` on PATH via `/usr/local/bin` or `/opt/homebrew/bin`.
46
+ * - win32: returns `'claude.cmd'`. The Anthropic Windows installer ships a
47
+ * `.cmd` shim. Using the `.cmd` filename explicitly (instead of bare
48
+ * `claude`) avoids `spawnSync` falling through to `cmd.exe /c claude`,
49
+ * which would re-introduce the Spike C PowerShell quoting hazard.
50
+ *
51
+ * @returns {string} `'claude'` or `'claude.cmd'`
52
+ */
53
+ function resolveClaudePath() {
54
+ return process.platform === "win32" ? "claude.cmd" : "claude";
55
+ }
56
+
57
+ // ─── isAlive ─────────────────────────────────────────────────────────────────
58
+
59
+ /**
60
+ * Cross-platform liveness check for a PID.
61
+ *
62
+ * Uses the POSIX trick `kill(pid, 0)` — sends signal 0, which performs all
63
+ * permission and existence checks but delivers no signal. Node's
64
+ * `process.kill` implements the same semantics on Windows.
65
+ *
66
+ * Errors:
67
+ * - `ESRCH` → no such process. Returns `false`.
68
+ * - `EPERM` → process exists but we don't own it. Returns `true` (we got
69
+ * permission feedback, which proves the PID is live).
70
+ * - other → unexpected; rethrown.
71
+ *
72
+ * @param {number} pid
73
+ * @returns {boolean}
74
+ */
75
+ function isAlive(pid) {
76
+ if (typeof pid !== "number" || !Number.isInteger(pid) || pid <= 0) {
77
+ return false;
78
+ }
79
+ try {
80
+ process.kill(pid, 0);
81
+ return true;
82
+ } catch (err) {
83
+ if (err && err.code === "ESRCH") return false;
84
+ if (err && err.code === "EPERM") return true; // exists, not ours
85
+ throw err;
86
+ }
87
+ }
88
+
89
+ // ─── spawnWorker ─────────────────────────────────────────────────────────────
90
+
91
+ /**
92
+ * Spawn a synchronous `claude -p '/gsd-t-resume'` worker iteration for the
93
+ * unattended supervisor.
94
+ *
95
+ * Returns a normalized result object: `{ status, stdout, stderr, signal,
96
+ * timedOut, error }`. Never throws — spawn errors are returned in `error`.
97
+ *
98
+ * Timeout semantics: when `spawnSync`'s `timeout` fires, the child is sent
99
+ * SIGTERM (or the equivalent on win32), `status` is `null`, and `signal` is
100
+ * non-null. We surface this as `timedOut: true` so callers can map to exit
101
+ * code 3 per contract §5.
102
+ *
103
+ * Spawn recipe (uniform across platforms):
104
+ * - `shell: false` → no shell quoting hazards
105
+ * - `windowsHide: true` → no flashed window on win32
106
+ * - explicit `claude.cmd` filename on win32 (see resolveClaudePath JSDoc)
107
+ *
108
+ * @todo Spike C: verify `claude.cmd -p "/gsd-t-resume"` dispatches correctly
109
+ * under PowerShell + cmd.exe + Git Bash. See
110
+ * `docs/unattended-windows-caveats.md` (Task 3 of m36-cross-platform).
111
+ *
112
+ * @param {string} projectDir Absolute path to the project directory (cwd).
113
+ * @param {number} timeoutMs Wall-clock cap per worker iteration in ms.
114
+ * @param {object} [opts] Optional overrides (test-mode hooks).
115
+ * @param {string} [opts.bin] Override the resolved binary (test-mode only).
116
+ * @param {string[]} [opts.args] Override args (defaults to `['-p', '/gsd-t-resume']`).
117
+ * @param {object} [opts.env] Override env (defaults to `process.env`).
118
+ * @returns {{
119
+ * status: number|null,
120
+ * stdout: string,
121
+ * stderr: string,
122
+ * signal: string|null,
123
+ * timedOut: boolean,
124
+ * error: Error|null
125
+ * }}
126
+ */
127
+ function spawnWorker(projectDir, timeoutMs, opts = {}) {
128
+ const bin = opts.bin || resolveClaudePath();
129
+ const args = opts.args || ["-p", "/gsd-t-resume"];
130
+ const env = opts.env || process.env;
131
+
132
+ const result = spawnSync(bin, args, {
133
+ cwd: projectDir,
134
+ encoding: "utf8",
135
+ timeout: timeoutMs,
136
+ env,
137
+ stdio: ["ignore", "pipe", "pipe"],
138
+ shell: false,
139
+ windowsHide: true,
140
+ });
141
+
142
+ // Normalize. spawnSync may return error if the binary cannot be launched
143
+ // (ENOENT etc.) — surface it instead of throwing.
144
+ const stdout = typeof result.stdout === "string" ? result.stdout : "";
145
+ const stderr = typeof result.stderr === "string" ? result.stderr : "";
146
+ const signal = result.signal || null;
147
+ const status = typeof result.status === "number" ? result.status : null;
148
+
149
+ // Timeout detection: when spawnSync's `timeout` option fires it sets
150
+ // - status === null
151
+ // - signal !== null (SIGTERM on POSIX, equivalent on win32)
152
+ // - error.code === 'ETIMEDOUT' (Node surfaces it as a synthetic Error)
153
+ // The ETIMEDOUT code is the authoritative signal — checking it
154
+ // discriminates a genuine timeout from an ENOENT/spawn failure.
155
+ const errCode = result.error && result.error.code;
156
+ const timedOut =
157
+ errCode === "ETIMEDOUT" || (status === null && signal !== null && !result.error);
158
+
159
+ return {
160
+ status,
161
+ stdout,
162
+ stderr,
163
+ signal,
164
+ timedOut,
165
+ // Suppress the synthetic ETIMEDOUT error so callers can rely on
166
+ // `timedOut` for the timeout case and `error` for genuine spawn failures.
167
+ error: errCode === "ETIMEDOUT" ? null : result.error || null,
168
+ };
169
+ }
170
+
171
+ // ─── spawnSupervisor ─────────────────────────────────────────────────────────
172
+
173
+ /**
174
+ * Spawn a detached unattended supervisor process.
175
+ *
176
+ * Implements the Launch Handshake from contract §7: the interactive launch
177
+ * command forks a long-lived supervisor that outlives the parent, reads the
178
+ * state file, and relays `claude -p` workers until the milestone terminates.
179
+ *
180
+ * Spawn recipe:
181
+ * - `node {binPath} unattended {...args}` — the `unattended` subcommand is
182
+ * prepended automatically so callers pass only user-facing args.
183
+ * - `detached: true` — the child becomes a process-group leader on POSIX
184
+ * (darwin/linux) so it survives the parent closing its terminal. On win32
185
+ * the equivalent flag produces a separate process tree.
186
+ * - `stdio: 'ignore'` — no pipes held open that would block the parent
187
+ * from exiting.
188
+ * - `windowsHide: true` (win32 only) — no flashed console window.
189
+ * - `child.unref()` — the parent event loop will not wait on the child.
190
+ *
191
+ * Cross-platform notes:
192
+ * - darwin / linux: runtime-tested.
193
+ * - win32: implementation-complete; documented in
194
+ * `docs/unattended-windows-caveats.md` (Task 3).
195
+ *
196
+ * @param {object} params
197
+ * @param {string} params.binPath Absolute path to `bin/gsd-t.js`.
198
+ * @param {string[]} params.args Extra args appended after `unattended`.
199
+ * @param {string} params.cwd Project directory (supervisor's cwd).
200
+ * @returns {{ pid: number }} The detached child's PID.
201
+ */
202
+ function spawnSupervisor({ binPath, args, cwd }) {
203
+ const spawnArgs = [binPath, "unattended", ...(args || [])];
204
+ const opts = {
205
+ cwd,
206
+ detached: true,
207
+ stdio: "ignore",
208
+ };
209
+ if (process.platform === "win32") {
210
+ opts.windowsHide = true;
211
+ }
212
+ const child = spawn("node", spawnArgs, opts);
213
+ child.unref();
214
+ return { pid: child.pid };
215
+ }
216
+
217
+ // ─── preventSleep ────────────────────────────────────────────────────────────
218
+
219
+ /**
220
+ * Prevent the OS from going to sleep while the supervisor is running.
221
+ *
222
+ * Returns a handle that must be passed to `releaseSleep` when the supervisor
223
+ * terminates.
224
+ *
225
+ * Cross-platform:
226
+ * - darwin: `caffeinate -i -w <supervisor-pid>` — the `-w` flag ties the
227
+ * caffeinate lifetime to the supervisor's PID. Even if the supervisor
228
+ * forgets to call `releaseSleep`, caffeinate will self-exit when the
229
+ * supervisor dies. Returns the caffeinate child PID as the handle.
230
+ * - linux: returns `null`. Reliable sleep prevention requires
231
+ * `systemd-inhibit`, which only works under a user session bus and is
232
+ * not universally available. v1 documents the gap; v2 may add opt-in
233
+ * systemd-inhibit. Prints a one-line notice to stderr.
234
+ * - win32: returns `null`. `SetThreadExecutionState` is the native API but
235
+ * requires a C binding. v1 documents the gap; see
236
+ * `docs/unattended-windows-caveats.md` (Task 3).
237
+ *
238
+ * @param {string} [reason] Informational label (reserved; not currently used
239
+ * — darwin's caffeinate has no reason field, and
240
+ * linux/win32 don't have sleep prevention yet).
241
+ * @returns {number|null} PID handle on darwin, `null` elsewhere.
242
+ */
243
+ function preventSleep(reason) {
244
+ void reason; // reserved for future implementations
245
+ if (process.platform === "darwin") {
246
+ try {
247
+ const child = spawn("caffeinate", ["-i", "-w", String(process.pid)], {
248
+ detached: true,
249
+ stdio: "ignore",
250
+ });
251
+ child.unref();
252
+ return typeof child.pid === "number" ? child.pid : null;
253
+ } catch (err) {
254
+ process.stderr.write(
255
+ `[platform] caffeinate failed to spawn: ${err && err.message}\n`,
256
+ );
257
+ return null;
258
+ }
259
+ }
260
+ if (process.platform === "linux") {
261
+ process.stderr.write(
262
+ "[platform] sleep prevention not implemented on linux\n",
263
+ );
264
+ return null;
265
+ }
266
+ process.stderr.write(
267
+ "[platform] sleep prevention not implemented on win32 (see docs/unattended-windows-caveats.md)\n",
268
+ );
269
+ return null;
270
+ }
271
+
272
+ // ─── releaseSleep ────────────────────────────────────────────────────────────
273
+
274
+ /**
275
+ * Release a sleep-prevention handle obtained from `preventSleep`.
276
+ *
277
+ * Idempotent and tolerant:
278
+ * - `null` / non-number handle → no-op.
279
+ * - handle is a dead PID → no-op (ESRCH is swallowed).
280
+ * - handle is a live PID → `SIGTERM` is delivered (EPERM and other unusual
281
+ * errors are swallowed — caller cannot reasonably act on them).
282
+ *
283
+ * @param {number|null} handle
284
+ * @returns {void}
285
+ */
286
+ function releaseSleep(handle) {
287
+ if (handle == null) return;
288
+ if (typeof handle !== "number" || !Number.isInteger(handle) || handle <= 0) {
289
+ return;
290
+ }
291
+ if (!isAlive(handle)) return;
292
+ try {
293
+ process.kill(handle, "SIGTERM");
294
+ } catch (_err) {
295
+ // Swallow — releaseSleep must never throw. A dead/gone process is fine;
296
+ // an EPERM on an adopted PID is also fine (not ours to reap).
297
+ }
298
+ }
299
+
300
+ // ─── notify ──────────────────────────────────────────────────────────────────
301
+
302
+ /**
303
+ * Emit an OS-level desktop notification. Fire-and-forget — never throws.
304
+ *
305
+ * Cross-platform:
306
+ * - darwin: `osascript -e 'display notification "msg" with title "title"'`.
307
+ * - linux: `notify-send "title" "message"` (requires libnotify).
308
+ * - win32: `msg.exe * "title: message"` (console msg; ships with Windows).
309
+ *
310
+ * All platform helpers are fire-and-forget `spawn` calls. Errors are caught
311
+ * and logged to stderr so a missing binary (e.g., no libnotify installed on a
312
+ * headless Linux box) does NOT break the supervisor.
313
+ *
314
+ * @param {string} title
315
+ * @param {string} message
316
+ * @param {string} [level] `info` | `warn` | `done` | `failed` — accepted but
317
+ * currently unused. Reserved for future formatting.
318
+ * @returns {void}
319
+ */
320
+ function notify(title, message, level) {
321
+ void level; // reserved for future formatting
322
+ const safeTitle = String(title || "");
323
+ const safeMessage = String(message || "");
324
+ try {
325
+ if (process.platform === "darwin") {
326
+ // Escape double-quotes and backslashes for the AppleScript string
327
+ // literal. osascript uses double-quoted string syntax.
328
+ const esc = (s) => s.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
329
+ const script =
330
+ `display notification "${esc(safeMessage)}" ` +
331
+ `with title "${esc(safeTitle)}"`;
332
+ const child = spawn("osascript", ["-e", script], { stdio: "ignore" });
333
+ child.on("error", (err) => {
334
+ process.stderr.write(
335
+ `[platform] notify(osascript) failed: ${err && err.message}\n`,
336
+ );
337
+ });
338
+ child.unref && child.unref();
339
+ return;
340
+ }
341
+ if (process.platform === "linux") {
342
+ const child = spawn("notify-send", [safeTitle, safeMessage], {
343
+ stdio: "ignore",
344
+ });
345
+ child.on("error", (err) => {
346
+ process.stderr.write(
347
+ `[platform] notify(notify-send) failed: ${err && err.message}\n`,
348
+ );
349
+ });
350
+ child.unref && child.unref();
351
+ return;
352
+ }
353
+ // win32
354
+ const child = spawn("msg.exe", ["*", `${safeTitle}: ${safeMessage}`], {
355
+ stdio: "ignore",
356
+ windowsHide: true,
357
+ });
358
+ child.on("error", (err) => {
359
+ process.stderr.write(
360
+ `[platform] notify(msg.exe) failed: ${err && err.message}\n`,
361
+ );
362
+ });
363
+ child.unref && child.unref();
364
+ } catch (err) {
365
+ // Defense in depth — spawn() itself can throw synchronously on certain
366
+ // argument errors. Never propagate.
367
+ process.stderr.write(
368
+ `[platform] notify synchronous error: ${err && err.message}\n`,
369
+ );
370
+ }
371
+ }
372
+
373
+ module.exports = {
374
+ resolveClaudePath,
375
+ isAlive,
376
+ spawnWorker,
377
+ spawnSupervisor,
378
+ preventSleep,
379
+ releaseSleep,
380
+ notify,
381
+ };