@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,290 @@
|
|
|
1
|
+
# GSD-T: Unattended Watch — Stateless Watch Tick
|
|
2
|
+
|
|
3
|
+
**Model**: haiku (polling a state file is mechanical — no reasoning needed)
|
|
4
|
+
|
|
5
|
+
You are running one tick of the unattended supervisor watch loop. This command is **stateless**: every firing re-reads `.gsd-t/.unattended/supervisor.pid` + `state.json` + `run.log` from disk and makes a fresh decision. There is no in-memory state between ticks, and a `/clear` followed by `/user:gsd-t-resume` during a live unattended run is a no-op.
|
|
6
|
+
|
|
7
|
+
The watch loop is the user's visibility surface into the running supervisor. It fires every 270 seconds (inside the 5-minute prompt-cache TTL) and either:
|
|
8
|
+
1. Renders a compact status block + reschedules itself, OR
|
|
9
|
+
2. Prints a terminal report and STOPS rescheduling.
|
|
10
|
+
|
|
11
|
+
See `unattended-supervisor-contract.md` §8 (Watch Tick Decision Tree), §4 (Status Enum), §3 (state.json schema).
|
|
12
|
+
|
|
13
|
+
## Decision Tree (Contract §8)
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
1. supervisor.pid absent → finalized cleanly → final report, STOP
|
|
17
|
+
2. kill -0 {pid} fails → crashed → crash diagnostics + log tail, STOP
|
|
18
|
+
3. state.json.status:
|
|
19
|
+
- done → success report, STOP
|
|
20
|
+
- failed → failure summary, STOP
|
|
21
|
+
- stopped → user-stop confirm, STOP
|
|
22
|
+
- initializing | running → render watch block, go to 4
|
|
23
|
+
4. ScheduleWakeup(270, '/user:gsd-t-unattended-watch', reason='unattended tick {iter}')
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
**Critical**: Every branch except `initializing`/`running` is TERMINAL — do NOT reschedule. When you reach a terminal state, print the report and end the turn. Do not add a "Next Up" block — this is a self-rescheduling loop, not a phase workflow.
|
|
27
|
+
|
|
28
|
+
## Step 1: Check State Directory
|
|
29
|
+
|
|
30
|
+
Run via Bash:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
if [ ! -d ".gsd-t/.unattended" ]; then
|
|
34
|
+
echo "No supervisor state directory at .gsd-t/.unattended/ — nothing to watch."
|
|
35
|
+
exit 0
|
|
36
|
+
fi
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
If the directory does not exist, there is no supervisor to watch. Exit 0, do NOT reschedule. End the turn.
|
|
40
|
+
|
|
41
|
+
## Step 2: Read State, PID, and Run.log Tail
|
|
42
|
+
|
|
43
|
+
Run a single `node -e` block that gathers everything needed for the decision tree. This reads the last ~2KB of `run.log` via `fs.open` + `fs.read` with a negative offset from end (not `readFileSync`), so the command stays cheap even when `run.log` is hundreds of MB.
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
node -e "
|
|
47
|
+
const fs = require('fs');
|
|
48
|
+
const path = require('path');
|
|
49
|
+
|
|
50
|
+
const UDIR = '.gsd-t/.unattended';
|
|
51
|
+
const PID_FILE = path.join(UDIR, 'supervisor.pid');
|
|
52
|
+
const STATE_FILE = path.join(UDIR, 'state.json');
|
|
53
|
+
const LOG_FILE = path.join(UDIR, 'run.log');
|
|
54
|
+
|
|
55
|
+
function out(k, v) { console.log(k + '=' + JSON.stringify(v ?? null)); }
|
|
56
|
+
|
|
57
|
+
// --- PID file ---
|
|
58
|
+
let pid = null;
|
|
59
|
+
let pidFileExists = fs.existsSync(PID_FILE);
|
|
60
|
+
if (pidFileExists) {
|
|
61
|
+
try {
|
|
62
|
+
pid = parseInt(fs.readFileSync(PID_FILE, 'utf8').trim(), 10);
|
|
63
|
+
if (!Number.isFinite(pid)) pid = null;
|
|
64
|
+
} catch (_) { pid = null; }
|
|
65
|
+
}
|
|
66
|
+
out('PID_FILE_EXISTS', pidFileExists);
|
|
67
|
+
out('PID', pid);
|
|
68
|
+
|
|
69
|
+
// --- kill -0 liveness ---
|
|
70
|
+
let alive = null;
|
|
71
|
+
if (pid !== null) {
|
|
72
|
+
try { process.kill(pid, 0); alive = true; }
|
|
73
|
+
catch (_) { alive = false; }
|
|
74
|
+
}
|
|
75
|
+
out('ALIVE', alive);
|
|
76
|
+
|
|
77
|
+
// --- state.json ---
|
|
78
|
+
let state = null;
|
|
79
|
+
try { state = JSON.parse(fs.readFileSync(STATE_FILE, 'utf8')); }
|
|
80
|
+
catch (_) {
|
|
81
|
+
// tolerate transient rename race — retry once
|
|
82
|
+
try { state = JSON.parse(fs.readFileSync(STATE_FILE, 'utf8')); }
|
|
83
|
+
catch (_) { state = null; }
|
|
84
|
+
}
|
|
85
|
+
if (state) {
|
|
86
|
+
out('STATUS', state.status);
|
|
87
|
+
out('SESSION', state.sessionId);
|
|
88
|
+
out('MILESTONE', state.milestone);
|
|
89
|
+
out('WAVE', state.wave);
|
|
90
|
+
out('TASK', state.task);
|
|
91
|
+
out('ITER', state.iter);
|
|
92
|
+
out('MAX_ITER', state.maxIterations);
|
|
93
|
+
out('STARTED_AT', state.startedAt);
|
|
94
|
+
out('LAST_TICK', state.lastTick);
|
|
95
|
+
out('LAST_EXIT', state.lastExit);
|
|
96
|
+
out('LAST_ELAPSED_MS', state.lastElapsedMs);
|
|
97
|
+
out('LOG_PATH', state.logPath || LOG_FILE);
|
|
98
|
+
// elapsed
|
|
99
|
+
const started = state.startedAt ? Date.parse(state.startedAt) : null;
|
|
100
|
+
const elapsedMs = started ? (Date.now() - started) : null;
|
|
101
|
+
out('ELAPSED_MS', elapsedMs);
|
|
102
|
+
// last tick age
|
|
103
|
+
const lt = state.lastTick ? Date.parse(state.lastTick) : null;
|
|
104
|
+
const tickAgeMs = lt ? (Date.now() - lt) : null;
|
|
105
|
+
out('TICK_AGE_MS', tickAgeMs);
|
|
106
|
+
} else {
|
|
107
|
+
out('STATUS', null);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// --- run.log tail (last ~2KB via fd seek, not full read) ---
|
|
111
|
+
let tailLines = [];
|
|
112
|
+
try {
|
|
113
|
+
const st = fs.statSync(LOG_FILE);
|
|
114
|
+
const size = st.size;
|
|
115
|
+
const readLen = Math.min(size, 2048);
|
|
116
|
+
const offset = size - readLen;
|
|
117
|
+
const fd = fs.openSync(LOG_FILE, 'r');
|
|
118
|
+
const buf = Buffer.alloc(readLen);
|
|
119
|
+
fs.readSync(fd, buf, 0, readLen, offset);
|
|
120
|
+
fs.closeSync(fd);
|
|
121
|
+
const text = buf.toString('utf8');
|
|
122
|
+
tailLines = text.split('\n').filter(l => l.trim().length > 0);
|
|
123
|
+
} catch (_) { tailLines = []; }
|
|
124
|
+
// last 50 lines for crash path; last 1 non-empty for normal path
|
|
125
|
+
const last50 = tailLines.slice(-50);
|
|
126
|
+
const last1 = tailLines.length > 0 ? tailLines[tailLines.length - 1] : '';
|
|
127
|
+
out('LOG_TAIL_LAST1', last1);
|
|
128
|
+
out('LOG_TAIL_LAST50', last50.join('\n'));
|
|
129
|
+
"
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Step 3: Branch on PID File Absence (Finalized Cleanly)
|
|
133
|
+
|
|
134
|
+
If `PID_FILE_EXISTS=false`:
|
|
135
|
+
|
|
136
|
+
The supervisor has finalized cleanly and removed its own PID file. Read the final `state.json` status and iter, then print the terminal report:
|
|
137
|
+
|
|
138
|
+
```
|
|
139
|
+
✅ Unattended supervisor finalized cleanly.
|
|
140
|
+
|
|
141
|
+
Session: {SESSION}
|
|
142
|
+
Milestone: {MILESTONE}
|
|
143
|
+
Final status: {STATUS}
|
|
144
|
+
Iterations: {ITER} / {MAX_ITER}
|
|
145
|
+
Total elapsed: {Hh Mm — formatted from ELAPSED_MS}
|
|
146
|
+
Log: {LOG_PATH}
|
|
147
|
+
State: .gsd-t/.unattended/state.json
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
If `state.json` was also unreadable (null STATUS), emit:
|
|
151
|
+
|
|
152
|
+
```
|
|
153
|
+
⚠️ Unattended supervisor PID file absent but state.json unreadable.
|
|
154
|
+
Manual inspection required: ls -la .gsd-t/.unattended/
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
**STOP** — do NOT reschedule. End the turn.
|
|
158
|
+
|
|
159
|
+
## Step 4: Branch on Dead PID (Crashed)
|
|
160
|
+
|
|
161
|
+
If `PID_FILE_EXISTS=true` AND `ALIVE=false`:
|
|
162
|
+
|
|
163
|
+
The supervisor process is gone but it did not remove its PID file — this is a crash. Print crash diagnostics including the last 50 lines of `run.log`:
|
|
164
|
+
|
|
165
|
+
```
|
|
166
|
+
💥 Unattended supervisor CRASHED (PID {PID} no longer alive).
|
|
167
|
+
|
|
168
|
+
Last known state:
|
|
169
|
+
Session: {SESSION}
|
|
170
|
+
Status: {STATUS}
|
|
171
|
+
Iter: {ITER} / {MAX_ITER}
|
|
172
|
+
Last tick: {LAST_TICK} (age {tickAgeMs → seconds})
|
|
173
|
+
Last exit: {LAST_EXIT}
|
|
174
|
+
|
|
175
|
+
Last 50 lines of run.log:
|
|
176
|
+
──────────────────────────────────────────────
|
|
177
|
+
{LOG_TAIL_LAST50}
|
|
178
|
+
──────────────────────────────────────────────
|
|
179
|
+
|
|
180
|
+
Recovery: inspect .gsd-t/.unattended/run.log and .gsd-t/.unattended/state.json.
|
|
181
|
+
Remove the stale PID file with: rm .gsd-t/.unattended/supervisor.pid
|
|
182
|
+
Then relaunch with: /user:gsd-t-unattended
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
**STOP** — do NOT reschedule. End the turn.
|
|
186
|
+
|
|
187
|
+
## Step 5: Branch on Terminal Status
|
|
188
|
+
|
|
189
|
+
If `PID_FILE_EXISTS=true` AND `ALIVE=true` AND `STATUS` is terminal (`done` | `failed` | `stopped`):
|
|
190
|
+
|
|
191
|
+
The supervisor is still alive but has transitioned to a terminal status on its last tick. The next supervisor pass will remove the PID file — we just caught it mid-cleanup. Print the matching report and STOP.
|
|
192
|
+
|
|
193
|
+
### 5a: STATUS=done
|
|
194
|
+
|
|
195
|
+
```
|
|
196
|
+
🎉 Unattended supervisor COMPLETED the milestone.
|
|
197
|
+
|
|
198
|
+
Session: {SESSION}
|
|
199
|
+
Milestone: {MILESTONE} — DONE
|
|
200
|
+
Iterations: {ITER} / {MAX_ITER}
|
|
201
|
+
Total elapsed: {Hh Mm}
|
|
202
|
+
Log: {LOG_PATH}
|
|
203
|
+
|
|
204
|
+
The supervisor will clean up its PID file momentarily.
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### 5b: STATUS=failed
|
|
208
|
+
|
|
209
|
+
```
|
|
210
|
+
❌ Unattended supervisor HALTED — status=failed.
|
|
211
|
+
|
|
212
|
+
Session: {SESSION}
|
|
213
|
+
Milestone: {MILESTONE}
|
|
214
|
+
Iterations: {ITER} / {MAX_ITER}
|
|
215
|
+
Last exit: {LAST_EXIT}
|
|
216
|
+
Total elapsed: {Hh Mm}
|
|
217
|
+
Log: {LOG_PATH}
|
|
218
|
+
|
|
219
|
+
See run.log for the halt reason. The supervisor finalized state.json before exiting.
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### 5c: STATUS=stopped
|
|
223
|
+
|
|
224
|
+
```
|
|
225
|
+
🛑 Unattended supervisor STOPPED by user.
|
|
226
|
+
|
|
227
|
+
Session: {SESSION}
|
|
228
|
+
Milestone: {MILESTONE}
|
|
229
|
+
Iterations: {ITER} / {MAX_ITER}
|
|
230
|
+
Total elapsed: {Hh Mm}
|
|
231
|
+
Log: {LOG_PATH}
|
|
232
|
+
|
|
233
|
+
Stop sentinel was honored between worker iterations. Relaunch with /user:gsd-t-unattended.
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
**STOP** — do NOT reschedule. End the turn.
|
|
237
|
+
|
|
238
|
+
## Step 6: Render Live Watch Block (Non-Terminal)
|
|
239
|
+
|
|
240
|
+
If `PID_FILE_EXISTS=true` AND `ALIVE=true` AND `STATUS` is `initializing` or `running`:
|
|
241
|
+
|
|
242
|
+
Render the compact watch block below. Format rules:
|
|
243
|
+
- One extra space after each emoji (per CLAUDE.md markdown table rules — preserves alignment in terminal views).
|
|
244
|
+
- Elapsed formatted as `{H}h{M}m` from `ELAPSED_MS`.
|
|
245
|
+
- Last-tick age formatted as `{S}s` or `{M}m{S}s`. If age > 540s (2× tick cadence), append ` ⚠️ stale` as a soft warning (not a crash — per contract §3 write semantics).
|
|
246
|
+
- `LAST_EXIT` duration from `LAST_ELAPSED_MS` rendered as seconds.
|
|
247
|
+
- Wave/task lines are omitted when absent from state.
|
|
248
|
+
- Last non-empty `run.log` line is truncated to 80 chars.
|
|
249
|
+
|
|
250
|
+
```
|
|
251
|
+
⚙ Unattended — {MILESTONE}{ Wave {WAVE}}{ · Task {TASK}}
|
|
252
|
+
⏱ Iter {ITER} / {MAX_ITER} · elapsed {Hh Mm} · last tick {tickAge}
|
|
253
|
+
📊 Last exit: {LAST_EXIT} ({durationSec}s) · PID {PID} · session {SESSION}
|
|
254
|
+
📝 {truncated last log line or "(no output yet)"}
|
|
255
|
+
⏰ Next tick in 270s · Stop: /user:gsd-t-unattended-stop
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
(5 lines — the contract §8 format allows up to 10. Keep it tight.)
|
|
259
|
+
|
|
260
|
+
## Step 7: Reschedule via ScheduleWakeup (Non-Terminal Only)
|
|
261
|
+
|
|
262
|
+
**This step runs ONLY when Step 6 rendered a live watch block.** All terminal branches (Steps 3, 4, 5) must NOT reach this step — they STOP.
|
|
263
|
+
|
|
264
|
+
Call the harness `ScheduleWakeup` tool with these exact parameters:
|
|
265
|
+
|
|
266
|
+
- `delaySeconds`: `270` (fixed — inside the 5-minute prompt-cache TTL)
|
|
267
|
+
- `prompt`: `/user:gsd-t-unattended-watch`
|
|
268
|
+
- `reason`: `unattended tick {ITER}` — substituting the integer `ITER` from Step 2
|
|
269
|
+
|
|
270
|
+
Tool invocation pattern (make this real tool call, not a bash command):
|
|
271
|
+
|
|
272
|
+
```
|
|
273
|
+
ScheduleWakeup(
|
|
274
|
+
delaySeconds: 270,
|
|
275
|
+
prompt: "/user:gsd-t-unattended-watch",
|
|
276
|
+
reason: "unattended tick {ITER}"
|
|
277
|
+
)
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
After the tool call, end the turn. Do NOT output a "Next Up" hint, do NOT continue with further steps, do NOT summarize. The watch block from Step 6 plus the ScheduleWakeup tool call IS the entire turn output.
|
|
281
|
+
|
|
282
|
+
## Notes
|
|
283
|
+
|
|
284
|
+
- **Stateless**: every tick re-reads state from disk. No memory between firings.
|
|
285
|
+
- **Terminal = STOP**: any terminal branch (3, 4, 5) ends the loop. The user relaunches via `/user:gsd-t-unattended` if needed.
|
|
286
|
+
- **Never spawn subagents**: this is a pure polling command — no Task, no TeamCreate, no observability logging block.
|
|
287
|
+
- **No branch guard, no pre-commit gate, no doc ripple**: this command does not modify any files.
|
|
288
|
+
- **Stale-tick tolerance**: if `lastTick` is >540s old but PID is still alive, warn soft (`⚠️ stale`) but still reschedule — the supervisor may be mid-worker.
|
|
289
|
+
|
|
290
|
+
$ARGUMENTS
|
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
# GSD-T: Unattended — Launch the Background Supervisor
|
|
2
|
+
|
|
3
|
+
**Model**: sonnet (pre-flight reasoning + spawn coordination)
|
|
4
|
+
|
|
5
|
+
You are launching the GSD-T unattended supervisor — a detached background process that drives the active milestone to completion by spawning `claude -p` worker iterations in a relay, without human intervention. The supervisor runs outside the interactive Claude session and survives `/clear`, terminal close, and session expiry.
|
|
6
|
+
|
|
7
|
+
This command performs pre-flight checks, spawns the supervisor via the cross-platform helper, verifies liveness, displays the initial status block, and schedules the first watch tick. It then returns — the watch loop takes over.
|
|
8
|
+
|
|
9
|
+
See `unattended-supervisor-contract.md` §7 (Launch Handshake), §4 (Status Enum), §3 (state.json schema).
|
|
10
|
+
|
|
11
|
+
## Step 1: Pre-Flight Checks
|
|
12
|
+
|
|
13
|
+
### 1a: Verify GSD-T Project
|
|
14
|
+
|
|
15
|
+
Run via Bash:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
if [ ! -f ".gsd-t/progress.md" ]; then
|
|
19
|
+
echo "ERROR: .gsd-t/progress.md not found — not a GSD-T project. Run /user:gsd-t-init first."
|
|
20
|
+
exit 1
|
|
21
|
+
fi
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
If the file is missing, stop immediately with the error above. Do NOT proceed.
|
|
25
|
+
|
|
26
|
+
### 1b: Check for an Already-Running Supervisor
|
|
27
|
+
|
|
28
|
+
Run via Bash:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
node -e "
|
|
32
|
+
const fs = require('fs');
|
|
33
|
+
const PID_FILE = '.gsd-t/.unattended/supervisor.pid';
|
|
34
|
+
|
|
35
|
+
if (!fs.existsSync(PID_FILE)) {
|
|
36
|
+
console.log('PID_RUNNING=false');
|
|
37
|
+
process.exit(0);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let pid = null;
|
|
41
|
+
try {
|
|
42
|
+
pid = parseInt(fs.readFileSync(PID_FILE, 'utf8').trim(), 10);
|
|
43
|
+
} catch (_) {}
|
|
44
|
+
|
|
45
|
+
if (!pid || !Number.isFinite(pid)) {
|
|
46
|
+
console.log('PID_RUNNING=false');
|
|
47
|
+
process.exit(0);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
let alive = false;
|
|
51
|
+
try { process.kill(pid, 0); alive = true; }
|
|
52
|
+
catch (e) { alive = e.code === 'EPERM'; }
|
|
53
|
+
|
|
54
|
+
console.log('PID_RUNNING=' + alive);
|
|
55
|
+
console.log('PID=' + pid);
|
|
56
|
+
"
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
If `PID_RUNNING=true`, **STOP** and print:
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
🔴 Unattended supervisor is already running (PID {PID}).
|
|
63
|
+
|
|
64
|
+
Use /user:gsd-t-unattended-stop to request a clean halt, then wait for the
|
|
65
|
+
watch loop to confirm it has stopped before relaunching.
|
|
66
|
+
|
|
67
|
+
To just watch the current run: /user:gsd-t-unattended-watch
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Do NOT spawn a second supervisor. End the turn.
|
|
71
|
+
|
|
72
|
+
### 1c: Parse Arguments and Resolve Milestone
|
|
73
|
+
|
|
74
|
+
Parse `$ARGUMENTS` for:
|
|
75
|
+
- `--hours=N` — wall-clock cap in hours (default: `24`)
|
|
76
|
+
- `--milestone=LABEL` — milestone to drive (default: read from `.gsd-t/progress.md`)
|
|
77
|
+
- `--max-iterations=N` — iteration cap (default: `200`)
|
|
78
|
+
- `--dry-run` — preflight only; print what would be spawned, do NOT spawn
|
|
79
|
+
|
|
80
|
+
Run via Bash to read the current milestone from progress.md:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
node -e "
|
|
84
|
+
const fs = require('fs');
|
|
85
|
+
let milestone = '';
|
|
86
|
+
try {
|
|
87
|
+
const text = fs.readFileSync('.gsd-t/progress.md', 'utf8');
|
|
88
|
+
// Look for '## Current Milestone: M36' or 'Milestone: M36' patterns
|
|
89
|
+
const m = text.match(/(?:##\s*)?(?:Current\s+)?Milestone[:\s]+(\S+)/i);
|
|
90
|
+
if (m) milestone = m[1].replace(/[^A-Za-z0-9_.-]/g, '');
|
|
91
|
+
} catch (_) {}
|
|
92
|
+
console.log('MILESTONE=' + (milestone || 'unknown'));
|
|
93
|
+
"
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Also read the project name from `package.json` (best-effort):
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
node -e "
|
|
100
|
+
try {
|
|
101
|
+
const p = JSON.parse(require('fs').readFileSync('package.json', 'utf8'));
|
|
102
|
+
console.log('PROJECT_NAME=' + (p.name || ''));
|
|
103
|
+
} catch (_) {
|
|
104
|
+
console.log('PROJECT_NAME=');
|
|
105
|
+
}
|
|
106
|
+
"
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### 1d: Check for Stale Stop Sentinel
|
|
110
|
+
|
|
111
|
+
If `.gsd-t/.unattended/stop` exists from a previous run, remove it before spawning (per contract §10 — the launch command cleans the stale sentinel):
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
if [ -f ".gsd-t/.unattended/stop" ]; then
|
|
115
|
+
rm ".gsd-t/.unattended/stop"
|
|
116
|
+
echo "STALE_SENTINEL=removed"
|
|
117
|
+
else
|
|
118
|
+
echo "STALE_SENTINEL=none"
|
|
119
|
+
fi
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
If the sentinel was removed, print: `ℹ️ Removed stale stop sentinel from previous run.`
|
|
123
|
+
|
|
124
|
+
### 1e: Verify Required Software is Installed
|
|
125
|
+
|
|
126
|
+
The unattended supervisor depends on platform-specific helpers. Check them up front and fail fast with clear install instructions rather than crashing mid-run.
|
|
127
|
+
|
|
128
|
+
Run via Bash:
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
node -e "
|
|
132
|
+
const { execSync } = require('child_process');
|
|
133
|
+
const os = require('os');
|
|
134
|
+
const platform = os.platform();
|
|
135
|
+
|
|
136
|
+
function has(cmd) {
|
|
137
|
+
try {
|
|
138
|
+
const which = platform === 'win32' ? 'where' : 'command -v';
|
|
139
|
+
execSync(which + ' ' + cmd, { stdio: 'ignore' });
|
|
140
|
+
return true;
|
|
141
|
+
} catch (_) { return false; }
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const missing = [];
|
|
145
|
+
const warnings = [];
|
|
146
|
+
|
|
147
|
+
// REQUIRED on all platforms
|
|
148
|
+
if (!has('node')) missing.push({ name: 'node', install: 'https://nodejs.org (>= 16)' });
|
|
149
|
+
if (!has('claude')) {
|
|
150
|
+
missing.push({
|
|
151
|
+
name: 'claude',
|
|
152
|
+
install: platform === 'win32'
|
|
153
|
+
? 'npm install -g @anthropic-ai/claude-code (then ensure claude.cmd is on PATH)'
|
|
154
|
+
: 'npm install -g @anthropic-ai/claude-code'
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
if (!has('git')) missing.push({ name: 'git', install: 'https://git-scm.com/downloads' });
|
|
158
|
+
|
|
159
|
+
// PLATFORM-SPECIFIC (warnings only — supervisor runs without them, just less resilient)
|
|
160
|
+
if (platform === 'darwin') {
|
|
161
|
+
if (!has('caffeinate')) warnings.push('caffeinate (macOS built-in) — missing means sleep may interrupt long runs');
|
|
162
|
+
} else if (platform === 'linux') {
|
|
163
|
+
if (!has('systemd-inhibit') && !has('caffeine')) {
|
|
164
|
+
warnings.push('systemd-inhibit or caffeine — missing means sleep/screen-lock may interrupt long runs');
|
|
165
|
+
}
|
|
166
|
+
if (!has('notify-send')) {
|
|
167
|
+
warnings.push('notify-send (libnotify-bin) — missing means no desktop notifications on milestone events');
|
|
168
|
+
}
|
|
169
|
+
} else if (platform === 'win32') {
|
|
170
|
+
warnings.push('Windows: sleep prevention uses PowerShell SetThreadExecutionState — no external helper required');
|
|
171
|
+
warnings.push('Windows: desktop notifications use BurntToast PowerShell module — install with: Install-Module BurntToast');
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (missing.length > 0) {
|
|
175
|
+
console.log('PREFLIGHT=fail');
|
|
176
|
+
console.log('MISSING=' + JSON.stringify(missing));
|
|
177
|
+
} else {
|
|
178
|
+
console.log('PREFLIGHT=ok');
|
|
179
|
+
}
|
|
180
|
+
if (warnings.length > 0) {
|
|
181
|
+
console.log('WARNINGS=' + JSON.stringify(warnings));
|
|
182
|
+
}
|
|
183
|
+
"
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
If `PREFLIGHT=fail`, **STOP** and print:
|
|
187
|
+
|
|
188
|
+
```
|
|
189
|
+
❌ Required software missing — cannot launch unattended supervisor.
|
|
190
|
+
|
|
191
|
+
Missing:
|
|
192
|
+
{for each item in MISSING:}
|
|
193
|
+
• {name}
|
|
194
|
+
Install: {install}
|
|
195
|
+
|
|
196
|
+
Install the missing software and retry: /user:gsd-t-unattended
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
End the turn. Do NOT spawn.
|
|
200
|
+
|
|
201
|
+
If `WARNINGS` were emitted, print them as a non-blocking advisory before proceeding:
|
|
202
|
+
|
|
203
|
+
```
|
|
204
|
+
⚠️ Platform advisories (non-blocking):
|
|
205
|
+
{for each warning:}
|
|
206
|
+
• {warning}
|
|
207
|
+
|
|
208
|
+
Supervisor will still launch, but consider installing these for best reliability.
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## Step 2: Spawn the Detached Supervisor
|
|
212
|
+
|
|
213
|
+
⚙ [sonnet] gsd-t-unattended → spawning detached supervisor
|
|
214
|
+
|
|
215
|
+
**Before spawn — read starting context tokens (observability bracket):**
|
|
216
|
+
|
|
217
|
+
```bash
|
|
218
|
+
T_START=$(date +%s) && DT_START=$(date +"%Y-%m-%d %H:%M")
|
|
219
|
+
T0_TOKENS=$(node -e "try{const s=require('fs').readFileSync('.gsd-t/.context-meter-state.json','utf8');process.stdout.write(String(JSON.parse(s).inputTokens||0))}catch(_){process.stdout.write('0')}")
|
|
220
|
+
T0_PCT=$(node -e "try{const tb=require('./bin/token-budget.js');process.stdout.write(String(tb.getSessionStatus('.').pct||0))}catch(_){process.stdout.write('0')}")
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
If `--dry-run` was specified, print:
|
|
224
|
+
|
|
225
|
+
```
|
|
226
|
+
🔎 Dry-run mode — would spawn:
|
|
227
|
+
node bin/gsd-t.js unattended --hours={hours} --milestone={milestone} --max-iterations={maxIterations}
|
|
228
|
+
Project: {cwd}
|
|
229
|
+
|
|
230
|
+
No supervisor launched. Remove --dry-run to proceed.
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
Then end the turn.
|
|
234
|
+
|
|
235
|
+
Otherwise, run the actual spawn:
|
|
236
|
+
|
|
237
|
+
```bash
|
|
238
|
+
node -e "
|
|
239
|
+
const path = require('path');
|
|
240
|
+
const { spawnSupervisor } = require('./bin/gsd-t-unattended-platform.js');
|
|
241
|
+
|
|
242
|
+
// Parse CLI args forwarded from the launch command
|
|
243
|
+
const hours = parseInt(process.env.GSD_T_HOURS || '24', 10) || 24;
|
|
244
|
+
const milestone = process.env.GSD_T_MILESTONE || '';
|
|
245
|
+
const maxIterations = parseInt(process.env.GSD_T_MAX_ITERATIONS || '200', 10) || 200;
|
|
246
|
+
|
|
247
|
+
const binPath = path.resolve(__dirname, 'bin', 'gsd-t.js');
|
|
248
|
+
const cwd = process.cwd();
|
|
249
|
+
|
|
250
|
+
const extraArgs = [];
|
|
251
|
+
if (hours !== 24) extraArgs.push('--hours=' + hours);
|
|
252
|
+
if (milestone) extraArgs.push('--milestone=' + milestone);
|
|
253
|
+
if (maxIterations !== 200) extraArgs.push('--max-iterations=' + maxIterations);
|
|
254
|
+
|
|
255
|
+
const result = spawnSupervisor({ binPath, args: extraArgs, cwd });
|
|
256
|
+
console.log('SPAWNED_PID=' + result.pid);
|
|
257
|
+
" \
|
|
258
|
+
GSD_T_HOURS={hours} \
|
|
259
|
+
GSD_T_MILESTONE={milestone} \
|
|
260
|
+
GSD_T_MAX_ITERATIONS={maxIterations}
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
Capture `SPAWNED_PID` from the output.
|
|
264
|
+
|
|
265
|
+
**After spawn — record observability bracket:**
|
|
266
|
+
|
|
267
|
+
```bash
|
|
268
|
+
T_END=$(date +%s) && DT_END=$(date +"%Y-%m-%d %H:%M") && DURATION=$((T_END-T_START))
|
|
269
|
+
T1_TOKENS=$(node -e "try{const s=require('fs').readFileSync('.gsd-t/.context-meter-state.json','utf8');process.stdout.write(String(JSON.parse(s).inputTokens||0))}catch(_){process.stdout.write('0')}")
|
|
270
|
+
T1_PCT=$(node -e "try{const tb=require('./bin/token-budget.js');process.stdout.write(String(tb.getSessionStatus('.').pct||0))}catch(_){process.stdout.write('0')}")
|
|
271
|
+
COUNTER=$(node bin/task-counter.cjs status 2>/dev/null | node -e "let s='';process.stdin.on('data',d=>s+=d).on('end',()=>{try{process.stdout.write(String(JSON.parse(s).count||''))}catch(_){process.stdout.write('')}})")
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
Append to `.gsd-t/token-log.md` (create with header if missing):
|
|
275
|
+
|
|
276
|
+
```bash
|
|
277
|
+
node -e "
|
|
278
|
+
const fs = require('fs');
|
|
279
|
+
const LOG = '.gsd-t/token-log.md';
|
|
280
|
+
const header = '| Datetime-start | Datetime-end | Command | Step | Model | Duration(s) | Notes | Domain | Task | Tasks-Since-Reset |\n|---|---|---|---|---|---|---|---|---|---|\n';
|
|
281
|
+
const row = '| ${DT_START} | ${DT_END} | gsd-t-unattended | Step 2 | sonnet | ${DURATION}s | supervisor spawned PID ${SPAWNED_PID} | | | ${COUNTER} |\n';
|
|
282
|
+
if (!fs.existsSync(LOG)) fs.writeFileSync(LOG, header);
|
|
283
|
+
fs.appendFileSync(LOG, row);
|
|
284
|
+
"
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
## Step 3: Verify Supervisor Liveness
|
|
288
|
+
|
|
289
|
+
Wait up to 2 seconds for the supervisor to write its PID file and transition out of `initializing`:
|
|
290
|
+
|
|
291
|
+
```bash
|
|
292
|
+
node -e "
|
|
293
|
+
const fs = require('fs');
|
|
294
|
+
const path = require('path');
|
|
295
|
+
|
|
296
|
+
const PID_FILE = '.gsd-t/.unattended/supervisor.pid';
|
|
297
|
+
const STATE_FILE = '.gsd-t/.unattended/state.json';
|
|
298
|
+
const LOG_FILE = '.gsd-t/.unattended/run.log';
|
|
299
|
+
const SPAWNED_PID = parseInt(process.env.SPAWNED_PID || '0', 10);
|
|
300
|
+
const WAIT_MS = 2000;
|
|
301
|
+
const POLL_MS = 100;
|
|
302
|
+
|
|
303
|
+
function sleep(ms) { Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms); }
|
|
304
|
+
|
|
305
|
+
let alive = false;
|
|
306
|
+
let pidFromFile = null;
|
|
307
|
+
const deadline = Date.now() + WAIT_MS;
|
|
308
|
+
|
|
309
|
+
while (Date.now() < deadline) {
|
|
310
|
+
// Check process liveness via process.kill(pid, 0)
|
|
311
|
+
if (SPAWNED_PID > 0) {
|
|
312
|
+
try { process.kill(SPAWNED_PID, 0); alive = true; } catch (e) {
|
|
313
|
+
alive = e.code === 'EPERM';
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
// Also try reading the PID file the supervisor writes
|
|
317
|
+
if (fs.existsSync(PID_FILE)) {
|
|
318
|
+
try { pidFromFile = parseInt(fs.readFileSync(PID_FILE, 'utf8').trim(), 10); } catch (_) {}
|
|
319
|
+
}
|
|
320
|
+
if (alive || pidFromFile) break;
|
|
321
|
+
sleep(POLL_MS);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
if (!alive && !pidFromFile) {
|
|
325
|
+
console.log('LIVENESS=dead');
|
|
326
|
+
// Emit last few lines of run.log if it exists
|
|
327
|
+
if (fs.existsSync(LOG_FILE)) {
|
|
328
|
+
try {
|
|
329
|
+
const txt = fs.readFileSync(LOG_FILE, 'utf8');
|
|
330
|
+
const tail = txt.split('\n').filter(l => l.trim()).slice(-20).join('\n');
|
|
331
|
+
console.log('LOG_TAIL=' + JSON.stringify(tail));
|
|
332
|
+
} catch (_) { console.log('LOG_TAIL='); }
|
|
333
|
+
} else {
|
|
334
|
+
console.log('LOG_TAIL=');
|
|
335
|
+
}
|
|
336
|
+
} else {
|
|
337
|
+
console.log('LIVENESS=ok');
|
|
338
|
+
console.log('PID_FROM_FILE=' + (pidFromFile || SPAWNED_PID));
|
|
339
|
+
}
|
|
340
|
+
" SPAWNED_PID=${SPAWNED_PID}
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
If `LIVENESS=dead`, the supervisor crashed at startup. Print diagnostics and **STOP**:
|
|
344
|
+
|
|
345
|
+
```
|
|
346
|
+
💥 Supervisor crashed at startup (PID {SPAWNED_PID} no longer alive after 2s).
|
|
347
|
+
|
|
348
|
+
Startup diagnostics (run.log tail):
|
|
349
|
+
──────────────────────────────────────────────────────────────
|
|
350
|
+
{LOG_TAIL}
|
|
351
|
+
──────────────────────────────────────────────────────────────
|
|
352
|
+
|
|
353
|
+
Common causes:
|
|
354
|
+
• Missing Node.js or claude binary on PATH
|
|
355
|
+
• .gsd-t/progress.md has no active milestone
|
|
356
|
+
• Protected branch or dirty worktree rejected by safety rails
|
|
357
|
+
• Permissions error on .gsd-t/.unattended/ directory
|
|
358
|
+
|
|
359
|
+
Fix the issue above and retry: /user:gsd-t-unattended
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
End the turn without scheduling a watch tick.
|
|
363
|
+
|
|
364
|
+
## Step 4: Display Initial Status Block
|
|
365
|
+
|
|
366
|
+
Print the launch confirmation block:
|
|
367
|
+
|
|
368
|
+
```
|
|
369
|
+
🟢 Unattended supervisor launched.
|
|
370
|
+
|
|
371
|
+
PID: {PID_FROM_FILE}
|
|
372
|
+
Project: {PROJECT_NAME}
|
|
373
|
+
Milestone: {milestone}
|
|
374
|
+
Max hours: {hours}
|
|
375
|
+
Max iterations: {maxIterations}
|
|
376
|
+
Log: .gsd-t/.unattended/run.log
|
|
377
|
+
State: .gsd-t/.unattended/state.json
|
|
378
|
+
Watch: ScheduleWakeup every 270s
|
|
379
|
+
|
|
380
|
+
The supervisor is running detached — it survives /clear and terminal close.
|
|
381
|
+
Stop gracefully: /user:gsd-t-unattended-stop
|
|
382
|
+
Watch manually: /user:gsd-t-unattended-watch
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
## Step 5: Schedule the First Watch Tick
|
|
386
|
+
|
|
387
|
+
Call the harness `ScheduleWakeup` tool with these exact parameters:
|
|
388
|
+
|
|
389
|
+
- `delaySeconds`: `270` (fixed — inside the 5-minute prompt-cache TTL)
|
|
390
|
+
- `prompt`: `/user:gsd-t-unattended-watch`
|
|
391
|
+
- `reason`: `first unattended tick`
|
|
392
|
+
|
|
393
|
+
Tool invocation pattern (make this a real tool call, not a bash command):
|
|
394
|
+
|
|
395
|
+
```
|
|
396
|
+
ScheduleWakeup(
|
|
397
|
+
delaySeconds: 270,
|
|
398
|
+
prompt: "/user:gsd-t-unattended-watch",
|
|
399
|
+
reason: "first unattended tick"
|
|
400
|
+
)
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
After the tool call, end the turn. The in-session watch loop takes over from here. Do NOT output a "Next Up" block — this is a self-rescheduling loop, not a phase workflow.
|
|
404
|
+
|
|
405
|
+
## Notes
|
|
406
|
+
|
|
407
|
+
- **Singleton**: only one supervisor per project at a time. PID collision → refuse with "already running" message.
|
|
408
|
+
- **Stale stop sentinel**: if `.gsd-t/.unattended/stop` exists from a prior run, Step 1d removes it before spawning.
|
|
409
|
+
- **Platform helper**: uses `spawnSupervisor` from `bin/gsd-t-unattended-platform.js` — never hand-rolls `child_process.spawn` directly. This handles macOS/Linux/Windows differences.
|
|
410
|
+
- **Dry-run**: `--dry-run` prints the would-be invocation without spawning. Useful for validating flags before a long overnight run.
|
|
411
|
+
- **No doc ripple, no pre-commit gate**: this command spawns a background process; it does not modify any source files or contracts.
|
|
412
|
+
- **watch command is stateless**: after this command returns, every `/user:gsd-t-unattended-watch` tick re-reads state from disk. There is no in-memory state to preserve.
|
|
413
|
+
|
|
414
|
+
$ARGUMENTS
|