@tekyzinc/gsd-t 2.74.13 → 3.10.10
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 +165 -0
- package/README.md +117 -1
- package/bin/advisor-integration.js +93 -0
- package/bin/check-headless-sessions.js +140 -0
- package/bin/context-meter-config.cjs +101 -0
- package/bin/context-meter-config.test.cjs +101 -0
- package/bin/gsd-t-unattended-platform.js +381 -0
- package/bin/gsd-t-unattended-safety.js +766 -0
- package/bin/gsd-t-unattended.js +1259 -0
- package/bin/gsd-t.js +723 -19
- package/bin/handoff-lock.js +249 -0
- package/bin/headless-auto-spawn.js +328 -0
- package/bin/model-selector.js +224 -0
- package/bin/runway-estimator.js +242 -0
- package/bin/token-budget.js +96 -89
- package/bin/token-optimizer.js +471 -0
- package/bin/token-telemetry.js +246 -0
- package/commands/gsd-t-audit.md +3 -3
- package/commands/gsd-t-backlog-list.md +38 -0
- package/commands/gsd-t-brainstorm.md +3 -3
- package/commands/gsd-t-complete-milestone.md +24 -0
- package/commands/gsd-t-debug.md +124 -7
- package/commands/gsd-t-discuss.md +10 -3
- package/commands/gsd-t-doc-ripple.md +32 -4
- package/commands/gsd-t-execute.md +107 -52
- package/commands/gsd-t-help.md +22 -0
- package/commands/gsd-t-integrate.md +67 -4
- package/commands/gsd-t-optimization-apply.md +91 -0
- package/commands/gsd-t-optimization-reject.md +94 -0
- package/commands/gsd-t-partition.md +7 -0
- package/commands/gsd-t-pause.md +3 -0
- package/commands/gsd-t-plan.md +10 -3
- package/commands/gsd-t-prd.md +3 -3
- package/commands/gsd-t-quick.md +71 -9
- package/commands/gsd-t-reflect.md +3 -7
- package/commands/gsd-t-resume.md +86 -1
- package/commands/gsd-t-status.md +31 -0
- package/commands/gsd-t-test-sync.md +7 -0
- package/commands/gsd-t-unattended-stop.md +83 -0
- package/commands/gsd-t-unattended-watch.md +290 -0
- package/commands/gsd-t-unattended.md +414 -0
- package/commands/gsd-t-verify.md +12 -5
- package/commands/gsd-t-visualize.md +3 -7
- package/commands/gsd-t-wave.md +82 -18
- package/docs/GSD-T-README.md +69 -0
- package/docs/architecture.md +176 -4
- package/docs/infrastructure.md +221 -0
- package/docs/methodology.md +44 -0
- package/docs/prd-harness-evolution.md +51 -37
- package/docs/requirements.md +95 -0
- package/docs/unattended-windows-caveats.md +245 -0
- package/package.json +2 -2
- package/scripts/context-meter/count-tokens-client.js +221 -0
- package/scripts/context-meter/count-tokens-client.test.js +308 -0
- package/scripts/context-meter/test-injector.js +55 -0
- package/scripts/context-meter/threshold.js +88 -0
- package/scripts/context-meter/threshold.test.js +255 -0
- package/scripts/context-meter/transcript-parser.js +252 -0
- package/scripts/context-meter/transcript-parser.test.js +320 -0
- package/scripts/gsd-t-context-meter.e2e.test.js +415 -0
- package/scripts/gsd-t-context-meter.js +350 -0
- package/scripts/gsd-t-context-meter.test.js +417 -0
- package/scripts/gsd-t-heartbeat.js +2 -2
- package/scripts/gsd-t-statusline.js +23 -8
- package/templates/CLAUDE-global.md +17 -1
- package/templates/CLAUDE-project.md +26 -6
- package/templates/context-meter-config.json +10 -0
- package/templates/prompts/README.md +1 -1
- package/bin/task-counter.cjs +0 -161
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context Meter config loader (M34).
|
|
3
|
+
*
|
|
4
|
+
* Reads .gsd-t/context-meter-config.json, merges over defaults, validates,
|
|
5
|
+
* and returns the resolved config. Missing file → defaults. Unknown schema
|
|
6
|
+
* version or API-key leak → throws with a clear message.
|
|
7
|
+
*
|
|
8
|
+
* See .gsd-t/contracts/context-meter-contract.md for the schema, validation
|
|
9
|
+
* rules, and the API-key-never-stored invariant.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const fs = require("fs");
|
|
13
|
+
const path = require("path");
|
|
14
|
+
|
|
15
|
+
const DEFAULTS = Object.freeze({
|
|
16
|
+
version: 1,
|
|
17
|
+
thresholdPct: 75,
|
|
18
|
+
modelWindowSize: 200000,
|
|
19
|
+
checkFrequency: 5,
|
|
20
|
+
apiKeyEnvVar: "ANTHROPIC_API_KEY",
|
|
21
|
+
statePath: ".gsd-t/.context-meter-state.json",
|
|
22
|
+
logPath: ".gsd-t/context-meter.log",
|
|
23
|
+
timeoutMs: 2000,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const SUPPORTED_VERSION = 1;
|
|
27
|
+
const API_KEY_FIELD_RE = /api.?key/i;
|
|
28
|
+
const HEX_LOOKALIKE_RE = /^[a-zA-Z0-9_-]{64,}$/;
|
|
29
|
+
|
|
30
|
+
function loadConfig(projectRoot) {
|
|
31
|
+
const root = projectRoot || process.cwd();
|
|
32
|
+
const configPath = path.join(root, ".gsd-t", "context-meter-config.json");
|
|
33
|
+
|
|
34
|
+
let userConfig = {};
|
|
35
|
+
if (fs.existsSync(configPath)) {
|
|
36
|
+
const raw = fs.readFileSync(configPath, "utf8");
|
|
37
|
+
try {
|
|
38
|
+
userConfig = JSON.parse(raw);
|
|
39
|
+
} catch (e) {
|
|
40
|
+
throw new Error(
|
|
41
|
+
`context-meter-config: invalid JSON in ${configPath}: ${e.message}`
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
if (!userConfig || typeof userConfig !== "object" || Array.isArray(userConfig)) {
|
|
45
|
+
throw new Error(`context-meter-config: ${configPath} must contain a JSON object`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
validateNoKeyLeak(userConfig);
|
|
50
|
+
|
|
51
|
+
if (userConfig.version !== undefined && userConfig.version !== SUPPORTED_VERSION) {
|
|
52
|
+
throw new Error(
|
|
53
|
+
`context-meter-config: unsupported schema version ${userConfig.version} ` +
|
|
54
|
+
`(expected ${SUPPORTED_VERSION}). See .gsd-t/contracts/context-meter-contract.md#breaking-changes for migration.`
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const merged = { ...DEFAULTS, ...userConfig, version: SUPPORTED_VERSION };
|
|
59
|
+
validateRanges(merged);
|
|
60
|
+
return merged;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function validateNoKeyLeak(obj) {
|
|
64
|
+
for (const key of Object.keys(obj)) {
|
|
65
|
+
if (key === "apiKeyEnvVar") continue;
|
|
66
|
+
if (API_KEY_FIELD_RE.test(key)) {
|
|
67
|
+
throw new Error(
|
|
68
|
+
`context-meter-config: field "${key}" looks like an API key storage field. ` +
|
|
69
|
+
`API keys must only be read from the env var named in apiKeyEnvVar.`
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
const val = obj[key];
|
|
73
|
+
if (typeof val === "string" && val.length > 100 && HEX_LOOKALIKE_RE.test(val)) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
`context-meter-config: field "${key}" contains a long token-like string. ` +
|
|
76
|
+
`Do not store API keys in config — use apiKeyEnvVar to name the env var.`
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function validateRanges(c) {
|
|
83
|
+
const assert = (cond, msg) => { if (!cond) throw new Error(`context-meter-config: ${msg}`); };
|
|
84
|
+
|
|
85
|
+
assert(Number.isFinite(c.thresholdPct) && c.thresholdPct > 0 && c.thresholdPct < 100,
|
|
86
|
+
`thresholdPct must be a number in (0, 100), got ${c.thresholdPct}`);
|
|
87
|
+
assert(Number.isInteger(c.modelWindowSize) && c.modelWindowSize > 0,
|
|
88
|
+
`modelWindowSize must be a positive integer, got ${c.modelWindowSize}`);
|
|
89
|
+
assert(Number.isInteger(c.checkFrequency) && c.checkFrequency >= 1,
|
|
90
|
+
`checkFrequency must be an integer >= 1, got ${c.checkFrequency}`);
|
|
91
|
+
assert(typeof c.apiKeyEnvVar === "string" && c.apiKeyEnvVar.length > 0,
|
|
92
|
+
`apiKeyEnvVar must be a non-empty string, got ${JSON.stringify(c.apiKeyEnvVar)}`);
|
|
93
|
+
assert(typeof c.statePath === "string" && c.statePath.length > 0,
|
|
94
|
+
`statePath must be a non-empty string`);
|
|
95
|
+
assert(typeof c.logPath === "string" && c.logPath.length > 0,
|
|
96
|
+
`logPath must be a non-empty string`);
|
|
97
|
+
assert(Number.isInteger(c.timeoutMs) && c.timeoutMs > 0,
|
|
98
|
+
`timeoutMs must be a positive integer, got ${c.timeoutMs}`);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
module.exports = { loadConfig, DEFAULTS };
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
const { test } = require("node:test");
|
|
2
|
+
const assert = require("node:assert/strict");
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const os = require("os");
|
|
6
|
+
|
|
7
|
+
const { loadConfig, DEFAULTS } = require("./context-meter-config.cjs");
|
|
8
|
+
|
|
9
|
+
function makeProject(config) {
|
|
10
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), "cmcfg-"));
|
|
11
|
+
fs.mkdirSync(path.join(root, ".gsd-t"), { recursive: true });
|
|
12
|
+
if (config !== undefined) {
|
|
13
|
+
fs.writeFileSync(
|
|
14
|
+
path.join(root, ".gsd-t", "context-meter-config.json"),
|
|
15
|
+
typeof config === "string" ? config : JSON.stringify(config)
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
return root;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
test("missing config file → returns defaults", () => {
|
|
22
|
+
const root = makeProject();
|
|
23
|
+
assert.deepEqual(loadConfig(root), DEFAULTS);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("valid full config → returns exact values", () => {
|
|
27
|
+
const custom = {
|
|
28
|
+
version: 1,
|
|
29
|
+
thresholdPct: 80,
|
|
30
|
+
modelWindowSize: 400000,
|
|
31
|
+
checkFrequency: 10,
|
|
32
|
+
apiKeyEnvVar: "CLAUDE_KEY",
|
|
33
|
+
statePath: ".gsd-t/my-state.json",
|
|
34
|
+
logPath: ".gsd-t/my.log",
|
|
35
|
+
timeoutMs: 5000,
|
|
36
|
+
};
|
|
37
|
+
const root = makeProject(custom);
|
|
38
|
+
assert.deepEqual(loadConfig(root), custom);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("partial config → missing fields filled with defaults", () => {
|
|
42
|
+
const root = makeProject({ thresholdPct: 60, timeoutMs: 1000 });
|
|
43
|
+
const cfg = loadConfig(root);
|
|
44
|
+
assert.equal(cfg.thresholdPct, 60);
|
|
45
|
+
assert.equal(cfg.timeoutMs, 1000);
|
|
46
|
+
assert.equal(cfg.modelWindowSize, DEFAULTS.modelWindowSize);
|
|
47
|
+
assert.equal(cfg.checkFrequency, DEFAULTS.checkFrequency);
|
|
48
|
+
assert.equal(cfg.apiKeyEnvVar, DEFAULTS.apiKeyEnvVar);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("thresholdPct out of range throws", () => {
|
|
52
|
+
for (const bad of [0, 100, -5, 150, "80"]) {
|
|
53
|
+
const root = makeProject({ thresholdPct: bad });
|
|
54
|
+
assert.throws(() => loadConfig(root), /thresholdPct/);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("modelWindowSize <= 0 throws", () => {
|
|
59
|
+
for (const bad of [0, -1, 1.5, "100"]) {
|
|
60
|
+
const root = makeProject({ modelWindowSize: bad });
|
|
61
|
+
assert.throws(() => loadConfig(root), /modelWindowSize/);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("checkFrequency < 1 throws", () => {
|
|
66
|
+
for (const bad of [0, -1, 0.5]) {
|
|
67
|
+
const root = makeProject({ checkFrequency: bad });
|
|
68
|
+
assert.throws(() => loadConfig(root), /checkFrequency/);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("empty apiKeyEnvVar throws", () => {
|
|
73
|
+
const root = makeProject({ apiKeyEnvVar: "" });
|
|
74
|
+
assert.throws(() => loadConfig(root), /apiKeyEnvVar/);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("unknown version throws with migration pointer", () => {
|
|
78
|
+
const root = makeProject({ version: 2, thresholdPct: 75 });
|
|
79
|
+
assert.throws(() => loadConfig(root), /version 2|migration/i);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test("config containing an apiKey field is rejected as leak", () => {
|
|
83
|
+
const root = makeProject({ apiKey: "sk-ant-abc123" });
|
|
84
|
+
assert.throws(() => loadConfig(root), /api.?key/i);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("config containing a long hex-like string value is rejected as leak", () => {
|
|
88
|
+
const longHex = "a".repeat(120);
|
|
89
|
+
const root = makeProject({ customField: longHex });
|
|
90
|
+
assert.throws(() => loadConfig(root), /api.?key|token-like/i);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test("invalid JSON throws with clear message", () => {
|
|
94
|
+
const root = makeProject("{ not valid json");
|
|
95
|
+
assert.throws(() => loadConfig(root), /invalid JSON/);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("non-object JSON throws", () => {
|
|
99
|
+
const root = makeProject("[1, 2, 3]");
|
|
100
|
+
assert.throws(() => loadConfig(root), /JSON object/);
|
|
101
|
+
});
|
|
@@ -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
|
+
};
|