-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
369 lines (349 loc) · 11.7 KB
/
Copy pathcli.ts
File metadata and controls
369 lines (349 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/**
* `headlesscode watch` subcommand — Phase 5 GitHub issue watcher.
*
* npx tsx src/cli.ts watch --owner <o> --repo <local-clone-path> --label <target-label> \
* [--gh-repo <name>] [--poll-interval-ms <n>] [--run-once] [--max-per-sweep <n>] \
* [--state-file <path>] [--mode <slug>] [--qa] [--deploy] [--memory-dir <path>] \
* [--dry-run] [--retry-failed]
*
* Thin dispatch to src/watcher/watch.ts — no logic duplicated here. The
* GitHub repo name defaults to the basename of `--repo` (the local clone),
* which is the common case; override with `--gh-repo` when the directory
* name differs.
*
* `--dry-run` sweeps + reports what would be spawned (list issues + split
* plan per issue) WITHOUT spawning or writing state. It still needs a GitHub
* token for listIssues — a missing token is a clear error.
*
* `--run-once` performs one sweep and exits 0 (or 1 if any spawn failed) —
* the cron/CI-friendly form. Continuous mode (default) logs each sweep and
* runs until SIGINT/SIGTERM (clean shutdown via AbortSignal; state saved).
*
* Pass-through (recorded per batch / forwarded to the spawner): --mode and
* --memory-dir reach the spawner env (ORCHESTRATOR_MODE /
* HEADLESSCODE_MEMORY_DIR); --qa and --deploy are stored on the batch's
* state entry for the follow-up `orchestrate` completion run (see
* docs/phase5-issue-watcher.md — the watcher itself only spawns; completion
* watching / review / QA / deploy stays the orchestrator's job).
*/
import { execFileSync } from "node:child_process"
import * as path from "node:path"
import { Logger } from "../engine/logger.js"
import { watchIssues, type SweepResult } from "./watch.js"
const WATCH_USAGE = `headlesscode watch — Phase 5 GitHub issue watcher
Usage:
headlesscode watch --owner <o> --repo <path> --label <target-label> [options]
Options:
--owner <o> GitHub owner (required)
--repo <path> Local clone of the target repo (required; worktrees
are spawned under <path>/.worktrees/). The GitHub
repo name defaults to the directory basename.
--gh-repo <name> GitHub repo name override (default: basename of --repo)
--label <name> The label that triggers processing, e.g. needs-agent
--poll-interval-ms <n> Sweep interval in continuous mode (default: 60000)
--run-once One sweep then exit 0 (or 1 if any spawn failed);
the cron/CI form of the watcher
--max-per-sweep <n> Max NEW issues spawned per sweep (default: 5); the
rest stay in state as 'pending' and are picked up
next sweep (bounds label storms)
--max-concurrent-sessions <n> Phase 6 GLOBAL cap on concurrent sessions
across processes (default: $HEADLESSCODE_MAX_
CONCURRENT_SESSIONS or 3). Interplay: maxPerSweep
bounds one sweep's burst; this bounds the total
running/spawned fleet — issues beyond it stay
'pending' until slots free up
--state-file <path> Durable idempotency state (default:
<repo>/.worktrees/.watcher-state.json)
--mode <slug> Harness mode for workers (default: code; forwarded
to the spawner as ORCHESTRATOR_MODE)
--qa Pass-through: record that a QA round should follow
each batch (see docs/phase5-issue-watcher.md)
--deploy Pass-through: record that a deploy gate should follow
each batch (see docs/phase5-issue-watcher.md)
--memory-dir <path> Memory dir for workers (forwarded to the spawner as
HEADLESSCODE_MEMORY_DIR)
--dry-run Sweep + print the spawn plan per new issue, spawn
nothing, write no state (still needs a GH token)
--retry-failed Retry previously-failed spawns on the next sweep
(default: failed entries are NOT auto-retried)
--help Show this help and exit
Environment:
GH_TOKEN GitHub token (or GITHUB_TOKEN) — required; never logged
GITHUB_API_BASE_URL API base URL override (tests/mocks)
HEADLESSCODE_SPAWN_SCRIPT Override the spawner script path (e2e stubbing)
HEADLESSCODE_MAX_CONCURRENT_SESSIONS Global concurrency cap (default 3)
`
export interface WatchCliOptions {
owner: string
repo: string
ghRepo?: string
label: string
pollIntervalMs: number
runOnce: boolean
maxPerSweep: number
/** Phase 6: global concurrent-session cap (default env or 3). */
maxConcurrentSessions: number
stateFile?: string
mode: string
qa: boolean
deploy: boolean
memoryDir?: string
dryRun: boolean
retryFailed: boolean
help: boolean
}
export function parseWatchArgs(argv: string[]): { options: WatchCliOptions; error?: string } {
const options: WatchCliOptions = {
owner: "",
repo: "",
label: "",
pollIntervalMs: 60_000,
runOnce: false,
maxPerSweep: 5,
maxConcurrentSessions: 3,
mode: "code",
qa: false,
deploy: false,
dryRun: false,
retryFailed: false,
help: false,
}
for (let i = 0; i < argv.length; i++) {
const arg = argv[i]
const eq = arg.indexOf("=")
const flag = eq === -1 ? arg : arg.slice(0, eq)
const inlineValue = eq === -1 ? undefined : arg.slice(eq + 1)
const next = (): string | undefined => {
if (inlineValue !== undefined) {
return inlineValue
}
const v = argv[i + 1]
if (v === undefined || v.startsWith("--")) {
return undefined
}
i++
return v
}
switch (flag) {
case "--owner": {
const v = next()
if (v === undefined) {
return { options, error: "Missing value for --owner" }
}
options.owner = v
break
}
case "--repo": {
const v = next()
if (v === undefined) {
return { options, error: "Missing value for --repo" }
}
options.repo = v
break
}
case "--gh-repo": {
const v = next()
if (v === undefined) {
return { options, error: "Missing value for --gh-repo" }
}
options.ghRepo = v
break
}
case "--label": {
const v = next()
if (v === undefined) {
return { options, error: "Missing value for --label" }
}
options.label = v
break
}
case "--poll-interval-ms": {
const v = next()
const n = v === undefined ? Number.NaN : Number(v)
if (!Number.isInteger(n) || n <= 0) {
return { options, error: "--poll-interval-ms requires a positive integer" }
}
options.pollIntervalMs = n
break
}
case "--max-per-sweep": {
const v = next()
const n = v === undefined ? Number.NaN : Number(v)
if (!Number.isInteger(n) || n <= 0) {
return { options, error: "--max-per-sweep requires a positive integer" }
}
options.maxPerSweep = n
break
}
case "--max-concurrent-sessions": {
const v = next()
const n = v === undefined ? Number.NaN : Number(v)
if (!Number.isInteger(n) || n <= 0) {
return { options, error: "--max-concurrent-sessions requires a positive integer" }
}
options.maxConcurrentSessions = n
break
}
case "--state-file": {
const v = next()
if (v === undefined) {
return { options, error: "Missing value for --state-file" }
}
options.stateFile = v
break
}
case "--mode": {
const v = next()
if (v === undefined) {
return { options, error: "Missing value for --mode" }
}
options.mode = v
break
}
case "--memory-dir": {
const v = next()
if (v === undefined) {
return { options, error: "Missing value for --memory-dir" }
}
options.memoryDir = v
break
}
case "--run-once":
options.runOnce = true
break
case "--qa":
options.qa = true
break
case "--deploy":
options.deploy = true
break
case "--dry-run":
options.dryRun = true
break
case "--retry-failed":
options.retryFailed = true
break
case "--help":
case "-h":
options.help = true
break
default:
return { options, error: `Unknown watch argument: ${arg}` }
}
}
return { options }
}
/** Print the dry-run spawn plan for the sweeps that were simulated. */
function printDryRunPlan(sweeps: SweepResult[]): void {
process.stdout.write("── Watch plan (dry run) ────────────────────────────────────\n")
for (const sweep of sweeps) {
for (const plan of sweep.dryRunPlan ?? []) {
const { issue, specs } = plan
process.stdout.write(` issue #${issue.number}: ${issue.title}\n`)
for (const spec of specs) {
process.stdout.write(
` -> ${spec.name} issues [${spec.issues.join(", ")}] plans/parallel-tasks/${spec.taskFile}\n`,
)
}
}
if ((sweep.dryRunPlan ?? []).length === 0) {
process.stdout.write(" (no new issues to spawn)\n")
}
}
}
export async function watchMain(argv: string[]): Promise<number> {
const { options, error } = parseWatchArgs(argv)
if (error) {
process.stderr.write(`headlesscode watch: ${error}\n\n${WATCH_USAGE}`)
return 2
}
if (options.help) {
process.stdout.write(WATCH_USAGE)
return 0
}
if (!options.owner || !options.repo || !options.label) {
process.stderr.write(
`headlesscode watch: --owner <o>, --repo <path> and --label <name> are required\n\n${WATCH_USAGE}`,
)
return 2
}
const token = process.env.GH_TOKEN ?? process.env.GITHUB_TOKEN
if (!token) {
process.stderr.write(
"headlesscode watch: no GitHub token. Set GH_TOKEN (or GITHUB_TOKEN) — the watcher\n" +
" needs it for listIssues, even with --dry-run. The token is never logged.\n",
)
return 2
}
const repoRoot = path.resolve(options.repo)
// Dry-run never touches the repo, so git validation only applies to real runs.
if (!options.dryRun) {
try {
execFileSync("git", ["-C", repoRoot, "rev-parse", "--git-dir"], { stdio: "ignore", timeout: 5000 })
} catch {
process.stderr.write(`headlesscode watch: not a git repo: ${repoRoot}\n`)
return 2
}
}
const ghRepo = options.ghRepo ?? path.basename(repoRoot)
const logger = new Logger({ level: "info" })
// Continuous mode: clean shutdown on SIGINT/SIGTERM (state saved by
// watchIssues before returning).
const controller = new AbortController()
const onSignal = (): void => controller.abort()
process.on("SIGINT", onSignal)
process.on("SIGTERM", onSignal)
let result
try {
result = await watchIssues({
owner: options.owner,
repo: ghRepo,
targetLabel: options.label,
token,
ghBaseUrl: process.env.GITHUB_API_BASE_URL,
stateFile: options.stateFile,
repoRoot,
pollIntervalMs: options.pollIntervalMs,
runOnce: options.runOnce,
maxPerSweep: options.maxPerSweep,
maxConcurrentSessions: options.maxConcurrentSessions,
retryFailed: options.retryFailed,
dryRun: options.dryRun,
mode: options.mode,
memoryDir: options.memoryDir,
qa: options.qa,
deploy: options.deploy,
logger,
signal: controller.signal,
})
} finally {
process.off("SIGINT", onSignal)
process.off("SIGTERM", onSignal)
}
for (const sweep of result.sweeps) {
if (sweep.error) {
process.stderr.write(`[watch] ${sweep.error}\n`)
}
}
if (options.dryRun) {
printDryRunPlan(result.sweeps)
} else {
for (const sweep of result.sweeps) {
process.stdout.write(
`[watch] sweep: ${sweep.issuesFound} found, ${sweep.newIssues} new, ${sweep.skipped} skipped, ` +
`${sweep.spawned} spawned, ${sweep.deferred} deferred, ${sweep.failures} failed\n`,
)
}
}
if (options.dryRun) {
return 0
}
if (result.exitCode !== 0) {
process.stderr.write(
`headlesscode watch: ${result.spawned} batch(es) spawned, but one or more sweeps reported errors\n`,
)
return 1
}
process.stdout.write(`[watch] done: ${result.spawned} batch(es) spawned\n`)
return 0
}