@soleone/factory 0.0.0-stage → 0.2.0

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/dist/cli.mjs ADDED
@@ -0,0 +1,2074 @@
1
+ #!/usr/bin/env node
2
+ import { appendFileSync, closeSync, existsSync, mkdirSync, openSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
3
+ import { basename, dirname, extname, join, relative, resolve, sep } from "node:path";
4
+ import { parseArgs, promisify } from "node:util";
5
+ import { createHash, randomBytes, randomUUID } from "node:crypto";
6
+ import { execFile, spawn } from "node:child_process";
7
+ import { homedir, tmpdir } from "node:os";
8
+ import { mkdtemp, rm } from "node:fs/promises";
9
+ import { createServer } from "node:http";
10
+ import { fileURLToPath } from "node:url";
11
+ import { createInterface } from "node:readline";
12
+ //#region src/backend/agents.ts
13
+ const LIMITS = {
14
+ claude: /usage limit|You've hit your [\w' ]*limit|You've reached your [\w ]*limit|rate_limit_error/i,
15
+ pi: /usage limit|usage_limit_reached|usage_not_included|rate_limit_exceeded/i
16
+ };
17
+ const AGENT_NAMES = Object.keys(LIMITS);
18
+ function agent(name, { model, effort } = {}) {
19
+ const pick = model ? ["--model", model] : [];
20
+ if (name === "claude") return {
21
+ name,
22
+ command: "claude",
23
+ args: [
24
+ "-p",
25
+ "--permission-mode",
26
+ "bypassPermissions",
27
+ "--output-format",
28
+ "json",
29
+ ...pick,
30
+ ...effort ? ["--effort", effort] : []
31
+ ],
32
+ limit: LIMITS.claude
33
+ };
34
+ if (name === "pi") return {
35
+ name,
36
+ command: "pi",
37
+ args: [
38
+ "-p",
39
+ ...pick,
40
+ ...effort ? ["--thinking", effort] : []
41
+ ],
42
+ limit: LIMITS.pi
43
+ };
44
+ throw new Error(`unknown agent ${name} (known: ${AGENT_NAMES.join(", ")})`);
45
+ }
46
+ /**
47
+ * How the Plan chat starts an agent: Pi in RPC mode, Claude Code streaming JSON both ways (chat.ts
48
+ * reads each). Only tools for looking and running commands; it changes the queue through the
49
+ * factory CLI. Claude Code's are bypassed like a run's, since nobody is there to answer a prompt.
50
+ * Each keeps the chat as session `id`, continued once `resume`; Pi's session files go in `folder`.
51
+ */
52
+ function chatAgent(name, { model, effort }, context, session) {
53
+ const pick = model ? ["--model", model] : [];
54
+ if (name === "claude") return {
55
+ command: "claude",
56
+ args: [
57
+ "-p",
58
+ "--input-format",
59
+ "stream-json",
60
+ "--output-format",
61
+ "stream-json",
62
+ "--verbose",
63
+ "--include-partial-messages",
64
+ "--replay-user-messages",
65
+ session.resume ? "--resume" : "--session-id",
66
+ session.id,
67
+ "--permission-mode",
68
+ "bypassPermissions",
69
+ "--tools",
70
+ "Read,Bash,Grep,Glob",
71
+ "--append-system-prompt",
72
+ context,
73
+ ...pick,
74
+ ...effort ? ["--effort", effort] : []
75
+ ]
76
+ };
77
+ if (name === "pi") return {
78
+ command: "pi",
79
+ args: [
80
+ "--mode",
81
+ "rpc",
82
+ "--session-id",
83
+ session.id,
84
+ "--session-dir",
85
+ session.folder,
86
+ "--tools",
87
+ "read,bash,grep,find,ls",
88
+ "--append-system-prompt",
89
+ context,
90
+ ...pick,
91
+ ...effort ? ["--thinking", effort] : []
92
+ ]
93
+ };
94
+ throw new Error(`unknown agent ${name} (known: ${AGENT_NAMES.join(", ")})`);
95
+ }
96
+ /** Only the end of the log decides: the agent's own work earlier on may mention limits too. */
97
+ function classify(agent, code, log) {
98
+ const tail = log.slice(-8e3);
99
+ if (code === 0 && !/"is_error":\s*true/.test(tail)) return "done";
100
+ return agent.limit.test(tail) ? "limited" : "failed";
101
+ }
102
+ const MONTHS = [
103
+ "Jan",
104
+ "Feb",
105
+ "Mar",
106
+ "Apr",
107
+ "May",
108
+ "Jun",
109
+ "Jul",
110
+ "Aug",
111
+ "Sep",
112
+ "Oct",
113
+ "Nov",
114
+ "Dec"
115
+ ];
116
+ const UNIT_MS = {
117
+ s: 1e3,
118
+ m: 6e4,
119
+ h: 36e5
120
+ };
121
+ /** When a limit message says the limit resets, in epoch ms; undefined when it doesn't say. */
122
+ function resetTime(text, now) {
123
+ const epoch = /limit reached\|(\d{10})|"resets_at"\s*:\s*(\d{10})/.exec(text);
124
+ if (epoch) return Number(epoch[1] ?? epoch[2]) * 1e3;
125
+ const seconds = /"?resets_in_seconds"?\s*[:=]\s*(\d+)/.exec(text);
126
+ if (seconds) return now + Number(seconds[1]) * 1e3;
127
+ const after = /try again in ~?(\d+)\s*(s|m|h)/i.exec(text);
128
+ if (after) return now + Number(after[1]) * UNIT_MS[after[2].toLowerCase()];
129
+ const clock = /resets (?:at )?(?:([A-Z][a-z]{2}) (\d{1,2}),? )?(\d{1,2})(?::(\d{2}))?\s*([ap]m)(?: \(([^)]+)\))?/i.exec(text);
130
+ if (!clock) return void 0;
131
+ const [, month, day, hour, minute, half, zone] = clock;
132
+ const hour24 = Number(hour) % 12 + (half.toLowerCase() === "pm" ? 12 : 0);
133
+ try {
134
+ return nextWallClock(now, zone, hour24, Number(minute ?? 0), month, day);
135
+ } catch (error) {
136
+ if (error instanceof RangeError) return void 0;
137
+ throw error;
138
+ }
139
+ }
140
+ /** The first instant after now when the clock in zone (default: local) shows hour:minute, on month/day if given. */
141
+ function nextWallClock(now, zone, hour, minute, month, day) {
142
+ const today = wallClock(now, zone);
143
+ const monthIndex = month ? MONTHS.indexOf(month.slice(0, 3)) : today.month;
144
+ const date = day ? Number(day) : today.day;
145
+ for (let step = 0; step < 2; step++) {
146
+ const target = day ? instant(zone, today.year + step, monthIndex, date, hour, minute) : instant(zone, today.year, monthIndex, date + step, hour, minute);
147
+ if (target > now) return target;
148
+ }
149
+ throw new RangeError("no future reset time");
150
+ }
151
+ function instant(zone, ...fields) {
152
+ const guess = Date.UTC(...fields);
153
+ return guess - offset(guess - offset(guess, zone), zone);
154
+ }
155
+ function offset(time, zone) {
156
+ const clock = wallClock(time, zone);
157
+ const wall = Date.UTC(clock.year, clock.month, clock.day, clock.hour, clock.minute);
158
+ return Math.round((wall - time) / 6e4) * 6e4;
159
+ }
160
+ function wallClock(time, zone) {
161
+ const parts = Object.fromEntries(new Intl.DateTimeFormat("en-US", {
162
+ timeZone: zone,
163
+ hourCycle: "h23",
164
+ year: "numeric",
165
+ month: "numeric",
166
+ day: "numeric",
167
+ hour: "numeric",
168
+ minute: "numeric"
169
+ }).formatToParts(new Date(time)).map((part) => [part.type, Number(part.value)]));
170
+ return {
171
+ year: parts.year,
172
+ month: parts.month - 1,
173
+ day: parts.day,
174
+ hour: parts.hour,
175
+ minute: parts.minute
176
+ };
177
+ }
178
+ //#endregion
179
+ //#region src/backend/state.ts
180
+ const STATE = process.env.DATA ? join(process.env.DATA, "apps", "factory") : join(homedir(), ".local", "share", "factory");
181
+ const pidPath = (state) => join(state, "factory.pid");
182
+ function runningPid(state) {
183
+ if (!existsSync(pidPath(state))) return void 0;
184
+ const pid = Number(readFileSync(pidPath(state), "utf8"));
185
+ return alive(pid) ? pid : void 0;
186
+ }
187
+ function alive(pid) {
188
+ try {
189
+ process.kill(pid, 0);
190
+ return true;
191
+ } catch (error) {
192
+ if (error.code === "ESRCH") return false;
193
+ throw error;
194
+ }
195
+ }
196
+ const LEVELS = [
197
+ "auto",
198
+ "skim",
199
+ "decide"
200
+ ];
201
+ const DEFAULTS = {
202
+ agents: {
203
+ claude: {},
204
+ pi: {}
205
+ },
206
+ chat: { agent: "pi" },
207
+ presets: [],
208
+ limit: 1,
209
+ timeout: 60,
210
+ auto: {
211
+ on: false,
212
+ levels: LEVELS,
213
+ plays: [],
214
+ repos: []
215
+ }
216
+ };
217
+ const settingsPath = (state) => join(state, "settings.json");
218
+ function readSettings(state) {
219
+ if (!existsSync(settingsPath(state))) return DEFAULTS;
220
+ return checkSettings(JSON.parse(readFileSync(settingsPath(state), "utf8")));
221
+ }
222
+ function writeSettings(state, settings) {
223
+ writeFileSync(settingsPath(state), `${JSON.stringify(checkSettings(settings), null, 2)}\n`);
224
+ }
225
+ /** Settings come from a file people edit and from the page, so they are checked on the way in. */
226
+ function checkSettings(raw) {
227
+ const settings = {
228
+ ...DEFAULTS,
229
+ ...raw,
230
+ auto: {
231
+ ...DEFAULTS.auto,
232
+ ...raw.auto
233
+ }
234
+ };
235
+ for (const name of Object.keys(settings.agents)) if (!AGENT_NAMES.includes(name)) throw new Error(`unknown agent ${name} (known: ${AGENT_NAMES.join(", ")})`);
236
+ if (!AGENT_NAMES.includes(settings.chat.agent)) throw new Error(`unknown chat agent ${settings.chat.agent} (known: ${AGENT_NAMES.join(", ")})`);
237
+ for (const preset of settings.presets) if (!AGENT_NAMES.includes(preset.agent)) throw new Error(`unknown preset agent ${preset.agent} (known: ${AGENT_NAMES.join(", ")})`);
238
+ if (Object.keys(settings.agents).length === 0) throw new Error("settings need at least one agent");
239
+ if (settings.limit !== null && !(Number.isInteger(settings.limit) && settings.limit > 0)) throw new Error(`limit wants a whole number above 0 or null, got ${settings.limit}`);
240
+ if (settings.until !== void 0 && !/^\d{1,2}:\d{2}$/.test(settings.until)) throw new Error(`until wants HH:MM, got ${settings.until}`);
241
+ if (!(Number.isInteger(settings.timeout) && settings.timeout > 0)) throw new Error(`timeout wants whole minutes above 0, got ${settings.timeout}`);
242
+ for (const level of settings.auto.levels) if (!LEVELS.includes(level)) throw new Error(`unknown level ${level}`);
243
+ return settings;
244
+ }
245
+ const itemKey = (item) => `${item.repo} ${item.id}`;
246
+ const candidatesPath = (state) => join(state, "candidates.json");
247
+ const refreshLock = (state) => join(state, "candidates.lock");
248
+ function readCandidates(state) {
249
+ if (!existsSync(candidatesPath(state))) return void 0;
250
+ return JSON.parse(readFileSync(candidatesPath(state), "utf8"));
251
+ }
252
+ function refreshingCandidates(state) {
253
+ if (!existsSync(refreshLock(state))) return false;
254
+ return alive(Number(readFileSync(refreshLock(state), "utf8")));
255
+ }
256
+ /** Asks workwork again, unless some process (the factory or serve) already is. */
257
+ async function refreshCandidates(state, workwork) {
258
+ if (refreshingCandidates(state)) return;
259
+ rmSync(refreshLock(state), { force: true });
260
+ try {
261
+ closeSync(openSync(refreshLock(state), "wx"));
262
+ } catch (error) {
263
+ if (error.code === "EEXIST") return;
264
+ throw error;
265
+ }
266
+ writeFileSync(refreshLock(state), `${process.pid}\n`);
267
+ try {
268
+ const items = JSON.parse(await output(workwork, ["--all", "--json"], process.cwd()));
269
+ const candidates = {
270
+ at: (/* @__PURE__ */ new Date()).toISOString(),
271
+ items
272
+ };
273
+ writeFileSync(candidatesPath(state), JSON.stringify(candidates));
274
+ } finally {
275
+ rmSync(refreshLock(state), { force: true });
276
+ }
277
+ }
278
+ async function output(command, args, cwd) {
279
+ const { stdout, stderr } = await promisify(execFile)(command, args, {
280
+ cwd,
281
+ maxBuffer: 67108864
282
+ });
283
+ process.stderr.write(stderr);
284
+ return stdout;
285
+ }
286
+ //#endregion
287
+ //#region src/backend/jobs.ts
288
+ /** An unexpected failure gets one more try by the same agent. */
289
+ const TRIES$1 = 2;
290
+ const path = (state) => join(state, "jobs.jsonl");
291
+ function append(state, event) {
292
+ appendFileSync(path(state), `${JSON.stringify(event)}\n`);
293
+ }
294
+ /** Every job ever, the queued ones in the order they will be taken: picks first, then auto. */
295
+ function readJobs(state) {
296
+ if (!existsSync(path(state))) return [];
297
+ const jobs = /* @__PURE__ */ new Map();
298
+ const failures = /* @__PURE__ */ new Map();
299
+ const picks = [];
300
+ const autos = [];
301
+ for (const line of readFileSync(path(state), "utf8").split("\n")) {
302
+ if (!line) continue;
303
+ const event = JSON.parse(line);
304
+ if (event.type === "add") {
305
+ const job = {
306
+ ...event.job,
307
+ added: event.at,
308
+ state: "queued",
309
+ attempts: []
310
+ };
311
+ jobs.set(job.id, job);
312
+ (job.source === "pick" ? picks : autos).push(job);
313
+ continue;
314
+ }
315
+ const job = jobs.get(event.id);
316
+ if (event.type === "remove") job.state = "removed";
317
+ if (event.type === "requeue") job.state = "queued";
318
+ if (event.type === "retry") {
319
+ job.state = "queued";
320
+ failures.set(job.id, 0);
321
+ }
322
+ if (event.type === "move") {
323
+ picks.splice(picks.indexOf(job), 1);
324
+ const before = picks.filter((pick) => pick.state === "queued")[event.index];
325
+ picks.splice(before ? picks.indexOf(before) : picks.length, 0, job);
326
+ }
327
+ if (event.type === "start") {
328
+ job.state = "running";
329
+ job.agent = event.agent;
330
+ }
331
+ if (event.type === "attempt") {
332
+ job.attempts.push(event.attempt);
333
+ const { outcome } = event.attempt;
334
+ if (outcome === "done") job.state = "done";
335
+ else if (outcome === "limited" || outcome === "stopped") job.state = "queued";
336
+ else {
337
+ const count = (failures.get(job.id) ?? 0) + 1;
338
+ failures.set(job.id, count);
339
+ job.state = count >= TRIES$1 ? "failed" : "queued";
340
+ }
341
+ }
342
+ }
343
+ return [...picks, ...autos];
344
+ }
345
+ /** The next job this agent may take. */
346
+ function nextJob(jobs, agent) {
347
+ return jobs.find((job) => job.state === "queued" && (job.agent ?? agent) === agent);
348
+ }
349
+ function addJobs(state, items, source, pin = {}) {
350
+ const at = (/* @__PURE__ */ new Date()).toISOString();
351
+ return items.map((item) => {
352
+ const job = {
353
+ id: randomBytes(4).toString("hex"),
354
+ item,
355
+ source,
356
+ ...pin
357
+ };
358
+ append(state, {
359
+ at,
360
+ type: "add",
361
+ job
362
+ });
363
+ return {
364
+ ...job,
365
+ added: at,
366
+ state: "queued",
367
+ attempts: []
368
+ };
369
+ });
370
+ }
371
+ /**
372
+ * Queues a job to rebase the job's PR and resolve its conflicts, unless one is already waiting. It
373
+ * keeps the job's workwork id, so the attempt links the same PR.
374
+ */
375
+ function queueConflicts(state, id, url, files) {
376
+ const jobs = readJobs(state);
377
+ const job = jobs.find((job) => job.id === id);
378
+ if (jobs.some((other) => other.item.play === "conflicts" && itemKey(other.item) === itemKey(job.item) && (other.state === "queued" || other.state === "running"))) return;
379
+ addJobs(state, [{
380
+ ...job.item,
381
+ play: "conflicts",
382
+ title: `Resolve conflicts: ${job.item.title}`
383
+ }], "pick", { prompt: conflictsPrompt(url, files) });
384
+ }
385
+ const conflictsPrompt = (url, files) => `Pull request ${url} conflicts with its base branch in:
386
+ ${files.map((file) => `- ${file}`).join("\n")}
387
+
388
+ 1. Check out its branch with \`gh pr checkout ${url}\`.
389
+ 2. Rebase it onto the freshly fetched base branch. Resolve each conflict so the intent of both sides survives; read the commits on both sides before choosing.
390
+ 3. Run the repo's checks and tests, and fix what the rebase broke.
391
+ 4. Push with \`git push --force-with-lease\`. Don't open a new pull request.
392
+
393
+ If a conflict can't be resolved without guessing what someone meant, don't push. Comment on the pull request with what's in the way instead.
394
+
395
+ End your reply with the pull request's URL.
396
+ `;
397
+ function find(state, id, ...states) {
398
+ const job = readJobs(state).find((job) => job.id === id);
399
+ if (!job) throw new Error(`no job ${id}`);
400
+ if (!states.includes(job.state)) throw new Error(`job ${id} is ${job.state}`);
401
+ return job;
402
+ }
403
+ /** Takes a job out of the queue or the results, and deletes the branches its attempts kept. */
404
+ async function removeJob(state, id) {
405
+ const job = find(state, id, "queued", "done", "failed");
406
+ append(state, {
407
+ at: (/* @__PURE__ */ new Date()).toISOString(),
408
+ type: "remove",
409
+ id
410
+ });
411
+ const kept = job.attempts.flatMap((attempt) => attempt.branch ?? []);
412
+ if (kept.length === 0) return;
413
+ const branches = await output("git", [
414
+ "for-each-ref",
415
+ "--format=%(refname:short)",
416
+ "refs/heads/factory/"
417
+ ], job.item.repo);
418
+ const left = kept.filter((branch) => branches.split("\n").includes(branch));
419
+ if (left.length) await output("git", [
420
+ "branch",
421
+ "-D",
422
+ ...left
423
+ ], job.item.repo);
424
+ }
425
+ function retryJob(state, id) {
426
+ find(state, id, "failed");
427
+ append(state, {
428
+ at: (/* @__PURE__ */ new Date()).toISOString(),
429
+ type: "retry",
430
+ id
431
+ });
432
+ }
433
+ /** Moves a queued pick to index among the queued picks. */
434
+ function moveJob(state, id, index) {
435
+ if (find(state, id, "queued").source !== "pick") throw new Error(`job ${id} is not a pick`);
436
+ append(state, {
437
+ at: (/* @__PURE__ */ new Date()).toISOString(),
438
+ type: "move",
439
+ id,
440
+ index
441
+ });
442
+ }
443
+ function startJob(state, id, agent) {
444
+ append(state, {
445
+ at: (/* @__PURE__ */ new Date()).toISOString(),
446
+ type: "start",
447
+ id,
448
+ agent
449
+ });
450
+ }
451
+ function endJob(state, id, attempt) {
452
+ append(state, {
453
+ at: (/* @__PURE__ */ new Date()).toISOString(),
454
+ type: "attempt",
455
+ id,
456
+ attempt
457
+ });
458
+ }
459
+ /** A job whose run stopped before its agent started goes back to the queue, its agent kept. */
460
+ function requeueJob(state, id) {
461
+ append(state, {
462
+ at: (/* @__PURE__ */ new Date()).toISOString(),
463
+ type: "requeue",
464
+ id
465
+ });
466
+ }
467
+ /** Cheapest attention first, and within a level one item per repo in turn so no repo takes the night. */
468
+ function order(items, levels) {
469
+ return LEVELS.filter((level) => levels.includes(level)).flatMap((level) => {
470
+ const byRepo = /* @__PURE__ */ new Map();
471
+ for (const item of items.filter((item) => item.level === level)) byRepo.set(item.repo, [...byRepo.get(item.repo) ?? [], item]);
472
+ const queues = [...byRepo.values()];
473
+ const turns = [];
474
+ for (let round = 0; queues.some((queue) => queue.length > round); round++) for (const queue of queues) if (queue[round]) turns.push(queue[round]);
475
+ return turns;
476
+ });
477
+ }
478
+ /** What auto mode would add next: wanted candidates that never had a job. */
479
+ function autoPicks(candidates, jobs, auto, count) {
480
+ const seen = new Set(jobs.map((job) => itemKey(job.item)));
481
+ return order(candidates.filter((item) => !seen.has(itemKey(item)) && (auto.plays.length === 0 || auto.plays.includes(item.play)) && (auto.repos.length === 0 || auto.repos.includes(item.repo))), auto.levels).slice(0, count);
482
+ }
483
+ //#endregion
484
+ //#region src/backend/review.ts
485
+ const MINUTE$1 = 6e4;
486
+ /** The PR workwork had the agent open, found by the branch workwork names; scans name theirs per area, so they find none. */
487
+ async function findPr(item) {
488
+ const branch = `workwork/${item.id.replace(/[^\w./@-]+/g, "-")}`;
489
+ return JSON.parse(await output("gh", [
490
+ "pr",
491
+ "list",
492
+ "--head",
493
+ branch,
494
+ "--state",
495
+ "all",
496
+ "--json",
497
+ "url"
498
+ ], item.repo))[0]?.url;
499
+ }
500
+ async function readPr(url) {
501
+ const raw = JSON.parse(await gh([
502
+ "pr",
503
+ "view",
504
+ url,
505
+ "--json",
506
+ "state,isDraft,title,body,additions,deletions,files,mergeable,isCrossRepository,statusCheckRollup"
507
+ ]));
508
+ return {
509
+ state: raw.state,
510
+ draft: raw.isDraft,
511
+ title: raw.title,
512
+ level: /^Level: (\w+)\s*$/m.exec(raw.body)?.[1],
513
+ additions: raw.additions,
514
+ deletions: raw.deletions,
515
+ files: raw.files.map((file) => file.path),
516
+ checks: checks(raw.statusCheckRollup),
517
+ conflicts: raw.mergeable === "UNKNOWN" ? null : raw.mergeable === "CONFLICTING",
518
+ fork: raw.isCrossRepository
519
+ };
520
+ }
521
+ /** Check runs have a conclusion, empty while running; commit statuses have a state. */
522
+ function checks(rollup) {
523
+ if (rollup.length === 0) return "none";
524
+ const results = rollup.map((check) => check.conclusion || check.state || "");
525
+ if (results.some((result) => /FAILURE|ERROR|CANCELLED|TIMED_OUT|ACTION_REQUIRED/.test(result))) return "failing";
526
+ if (results.some((result) => result === "" || result === "PENDING" || result === "EXPECTED")) return "pending";
527
+ return "passing";
528
+ }
529
+ /** What the review popup shows: the whole PR body and its diff, fetched only when opened. */
530
+ async function readPrDetails(url) {
531
+ const [view, diff] = await Promise.all([gh([
532
+ "pr",
533
+ "view",
534
+ url,
535
+ "--json",
536
+ "body"
537
+ ]), gh([
538
+ "pr",
539
+ "diff",
540
+ url
541
+ ])]);
542
+ return {
543
+ body: JSON.parse(view).body,
544
+ diff
545
+ };
546
+ }
547
+ const mergePr = (url) => gh([
548
+ "pr",
549
+ "merge",
550
+ url,
551
+ "--squash",
552
+ "--delete-branch"
553
+ ]);
554
+ const closePr = (url) => gh([
555
+ "pr",
556
+ "close",
557
+ url,
558
+ "--delete-branch"
559
+ ]);
560
+ /** A rebase that stopped on conflicts git can't resolve; nothing was pushed. */
561
+ var Conflicts = class extends Error {
562
+ files;
563
+ constructor(files) {
564
+ super(`Rebase stopped. Resolve these conflicts manually; no changes were pushed:\n${files.join("\n")}`);
565
+ this.files = files;
566
+ }
567
+ };
568
+ async function rebasePr(url) {
569
+ const pull = /^(https:\/\/github\.com\/[\w.-]+\/[\w.-]+)\/pull\/(\d+)$/.exec(url);
570
+ if (!pull) throw new Error("Rebase requires a github.com pull request URL");
571
+ const [, repo, number] = pull;
572
+ const pr = JSON.parse(await gh([
573
+ "pr",
574
+ "view",
575
+ url,
576
+ "--json",
577
+ "baseRefName,headRefName,isCrossRepository"
578
+ ]));
579
+ if (pr.isCrossRepository || pr.baseRefName === pr.headRefName) throw new Error("Rebase requires a separate PR branch in the same repository");
580
+ const dir = await mkdtemp(join(tmpdir(), "factory-rebase-"));
581
+ const git = (...args) => output("git", args, dir);
582
+ const remote = `${repo}.git`;
583
+ const base = "refs/factory/base";
584
+ const head = "refs/factory/head";
585
+ const branch = `refs/heads/${pr.headRefName}`;
586
+ try {
587
+ await git("init", "--quiet");
588
+ await git("config", "--add", "credential.https://github.com.helper", "");
589
+ await git("config", "--add", "credential.https://github.com.helper", "!gh auth git-credential");
590
+ await git("fetch", "--no-tags", remote, `refs/heads/${pr.baseRefName}:${base}`, `refs/pull/${number}/head:${head}`);
591
+ const before = (await git("rev-parse", head)).trim();
592
+ await git("checkout", "--quiet", "--detach", head);
593
+ try {
594
+ await git("rebase", "--rebase-merges", base);
595
+ } catch (error) {
596
+ const conflicts = (await git("diff", "--name-only", "--diff-filter=U")).trim();
597
+ if (!conflicts) throw error;
598
+ throw new Conflicts(conflicts.split("\n"));
599
+ }
600
+ await git("push", `--force-with-lease=${branch}:${before}`, remote, `HEAD:${branch}`);
601
+ } finally {
602
+ await rm(dir, {
603
+ recursive: true,
604
+ force: true
605
+ });
606
+ }
607
+ }
608
+ /** Run outside any repo, so --delete-branch only touches GitHub, never a local checkout. */
609
+ const gh = (args) => output("gh", args, tmpdir());
610
+ /**
611
+ * PR status for the page. gh takes a second or more per PR, so this answers from what it last
612
+ * fetched and refreshes open PRs in the background. While GitHub calculates mergeability, retry
613
+ * sooner so the page doesn't wait a minute after a rebase.
614
+ */
615
+ function prCache() {
616
+ const cache = /* @__PURE__ */ new Map();
617
+ function refresh(url) {
618
+ const entry = {
619
+ at: Date.now(),
620
+ pr: cache.get(url)?.pr
621
+ };
622
+ cache.set(url, entry);
623
+ readPr(url).then((pr) => entry.pr = pr, (error) => console.error(`factory: gh pr view ${url} failed: ${error.message}`));
624
+ }
625
+ return {
626
+ get(urls) {
627
+ const prs = {};
628
+ for (const url of urls) {
629
+ const entry = cache.get(url);
630
+ const interval = entry?.pr?.conflicts === null ? 5e3 : MINUTE$1;
631
+ if (!entry || (entry.pr?.state ?? "OPEN") === "OPEN" && Date.now() - entry.at > interval) refresh(url);
632
+ const pr = cache.get(url).pr;
633
+ if (pr) prs[url] = pr;
634
+ }
635
+ return prs;
636
+ },
637
+ /** Asks GitHub now, for an action that must not act on a stale answer. */
638
+ async fresh(url) {
639
+ const pr = await readPr(url);
640
+ cache.set(url, {
641
+ at: Date.now(),
642
+ pr
643
+ });
644
+ return pr;
645
+ }
646
+ };
647
+ }
648
+ //#endregion
649
+ //#region src/backend/loop.ts
650
+ const MINUTE = 6e4;
651
+ /** Auto mode keeps this many of its jobs queued per agent, so picks never drown in them. */
652
+ const AUTO_PER_AGENT = 5;
653
+ const STALE_CANDIDATES = 360 * MINUTE;
654
+ const livePath = (state, id) => join(state, "running", `${id}.json`);
655
+ const URL$1 = /https:\/\/github\.com\/[\w.-]+\/[\w.-]+\/(?:pull|issues)\/\d+/g;
656
+ /** Works the queue until every agent is out of limits, until passes, or it's stopped; returns the run id. */
657
+ async function run(options) {
658
+ const { state } = options;
659
+ const id = (/* @__PURE__ */ new Date()).toISOString().slice(0, 19).replaceAll(":", "");
660
+ const runDir = join(state, "runs", id);
661
+ mkdirSync(runDir, { recursive: true });
662
+ mkdirSync(join(state, "running"), { recursive: true });
663
+ const agents = /* @__PURE__ */ new Set();
664
+ const wakers = /* @__PURE__ */ new Set();
665
+ const workers = Object.entries(options.agents).map(([agent, tuning]) => ({
666
+ agent,
667
+ ...tuning,
668
+ state: "idle",
669
+ since: (/* @__PURE__ */ new Date()).toISOString(),
670
+ limits: 0
671
+ }));
672
+ let stopping = false;
673
+ let killing = false;
674
+ let topping;
675
+ function writeStatus() {
676
+ const until = options.until ? new Date(options.until).toISOString() : void 0;
677
+ const status = {
678
+ pid: process.pid,
679
+ run: id,
680
+ until,
681
+ limit: options.limit,
682
+ workers
683
+ };
684
+ writeFileSync(join(state, "status.json"), `${JSON.stringify(status, null, 2)}\n`);
685
+ }
686
+ function set(worker, change) {
687
+ Object.assign(worker, {
688
+ job: void 0,
689
+ until: void 0,
690
+ reason: void 0
691
+ }, change);
692
+ worker.since = (/* @__PURE__ */ new Date()).toISOString();
693
+ writeStatus();
694
+ }
695
+ /** Starts nothing new; agents keep working for the next run to finish, unless killed. */
696
+ function stop(kill) {
697
+ stopping = true;
698
+ killing ||= kill;
699
+ if (kill) for (const pid of agents) killGroup(pid);
700
+ for (const wake of wakers) wake();
701
+ }
702
+ const handOff = () => stop(false);
703
+ const kill = () => stop(true);
704
+ const aborted = () => stop(options.signal?.reason === "kill");
705
+ function ended() {
706
+ if (stopping) return "turned off";
707
+ if (options.until && Date.now() >= options.until) return "until passed";
708
+ }
709
+ /** A wait that stopping cuts short. */
710
+ function nap(ms) {
711
+ return new Promise((resolve) => {
712
+ const wake = () => {
713
+ clearTimeout(timer);
714
+ wakers.delete(wake);
715
+ resolve();
716
+ };
717
+ const timer = setTimeout(wake, ms);
718
+ wakers.add(wake);
719
+ });
720
+ }
721
+ /** Auto mode's part: keep a few of its jobs queued. Reads settings each time, so the toggle applies at once. */
722
+ function topUp() {
723
+ topping ??= fillFromAuto().catch((error) => log(`auto mode could not ask workwork: ${error.message}`)).finally(() => topping = void 0);
724
+ }
725
+ async function fillFromAuto() {
726
+ const { auto } = readSettings(state);
727
+ if (!auto.on) return;
728
+ const wanted = () => AUTO_PER_AGENT * workers.length - readJobs(state).filter((job) => job.source === "auto" && job.state === "queued").length;
729
+ if (wanted() <= 0) return;
730
+ const cached = readCandidates(state);
731
+ if (!cached || Date.now() - Date.parse(cached.at) > STALE_CANDIDATES) await refreshCandidates(state, options.workwork);
732
+ const candidates = readCandidates(state);
733
+ if (!candidates) return;
734
+ const added = addJobs(state, autoPicks(candidates.items, readJobs(state), auto, wanted()), "auto");
735
+ if (added.length) log(`auto mode queued ${added.length}`);
736
+ }
737
+ async function work(worker, handed) {
738
+ let quickFailures = 0;
739
+ while (!ended()) {
740
+ topUp();
741
+ let job;
742
+ let attempt;
743
+ if (handed) {
744
+ ({job} = handed);
745
+ set(worker, {
746
+ state: "working",
747
+ job: job.id
748
+ });
749
+ log(`${worker.agent} takes over ${job.id} from run ${handed.live.begun.run}`);
750
+ attempt = await finish(job, handed.live);
751
+ handed = void 0;
752
+ } else {
753
+ job = nextJob(readJobs(state), worker.agent);
754
+ if (!job) {
755
+ if (worker.state !== "idle") set(worker, { state: "idle" });
756
+ await nap(options.pollMs ?? 3e3);
757
+ continue;
758
+ }
759
+ startJob(state, job.id, worker.agent);
760
+ set(worker, {
761
+ state: "working",
762
+ job: job.id
763
+ });
764
+ log(`${worker.agent} starts ${job.id}: ${job.item.label} ${job.item.id}`);
765
+ attempt = await attemptJob(worker, job);
766
+ }
767
+ if (!attempt) {
768
+ if (!existsSync(livePath(state, job.id))) requeueJob(state, job.id);
769
+ break;
770
+ }
771
+ endJob(state, job.id, attempt);
772
+ rmSync(livePath(state, job.id), { force: true });
773
+ log(`${worker.agent} ${attempt.outcome} ${job.id} ${attempt.url ?? attempt.error ?? ""}`);
774
+ const seconds = (Date.parse(attempt.end) - Date.parse(attempt.start)) / 1e3;
775
+ quickFailures = attempt.outcome === "failed" && seconds < 60 ? quickFailures + 1 : 0;
776
+ if (quickFailures >= 3) return set(worker, {
777
+ state: "stopped",
778
+ reason: `3 failures in a row under a minute; last: ${attempt.error?.split("\n")[0] ?? "see its log"}`
779
+ });
780
+ if (attempt.outcome !== "limited") {
781
+ set(worker, { state: "idle" });
782
+ continue;
783
+ }
784
+ worker.limits++;
785
+ if (options.limit !== null && worker.limits >= options.limit) return set(worker, {
786
+ state: "stopped",
787
+ reason: `limit ${worker.limits} of ${options.limit}`
788
+ });
789
+ const reset = resetTime(readFileSync(attempt.log, "utf8"), Date.now());
790
+ const wake = Math.min(reset === void 0 ? Date.now() + 30 * MINUTE : reset + 2 * MINUTE, options.until ?? Infinity);
791
+ set(worker, {
792
+ state: "sleeping",
793
+ until: new Date(wake).toISOString()
794
+ });
795
+ log(`${worker.agent} hit limit ${worker.limits}, sleeps until ${new Date(wake).toISOString()}`);
796
+ await (options.sleep ?? nap)(wake - Date.now());
797
+ }
798
+ set(worker, {
799
+ state: "stopped",
800
+ reason: ended()
801
+ });
802
+ }
803
+ /** One attempt in the agent's worktree for the repo; undefined when the run was stopped meanwhile. */
804
+ async function attemptJob(worker, job) {
805
+ const tuning = {
806
+ model: job.model ?? worker.model,
807
+ effort: job.effort ?? worker.effort
808
+ };
809
+ const agent$1 = agent(worker.agent, tuning);
810
+ const slug = `${job.id}-${job.attempts.length + 1}`;
811
+ const dir = join(runDir, slug);
812
+ mkdirSync(dir, { recursive: true });
813
+ const logPath = join(dir, "agent.log");
814
+ const repoKey = createHash("sha256").update(job.item.repo).digest("hex");
815
+ const worktree = join(state, "worktrees", agent$1.name, repoKey);
816
+ const begun = {
817
+ run: id,
818
+ agent: agent$1.name,
819
+ ...tuning,
820
+ start: (/* @__PURE__ */ new Date()).toISOString(),
821
+ log: logPath
822
+ };
823
+ let prompt;
824
+ try {
825
+ await checkout(worktree, job.item.repo);
826
+ prompt = job.prompt ?? await output(options.workwork, ["prompt", job.item.id], job.item.repo);
827
+ } catch (error) {
828
+ const message = error.message.trim();
829
+ writeFileSync(logPath, `${message}\n`);
830
+ return attempt(begun, "failed", { error: message.slice(0, 2e3) });
831
+ }
832
+ writeFileSync(join(dir, "prompt.md"), prompt);
833
+ if (stopping) return void 0;
834
+ const live = {
835
+ begun,
836
+ pid: startAgent(agent$1, worktree, dir),
837
+ worktree
838
+ };
839
+ writeFileSync(livePath(state, job.id), JSON.stringify(live));
840
+ return finish(job, live);
841
+ }
842
+ /** Waits for the agent, killing it at the timeout, and records what it did; undefined when the run hands it off. */
843
+ async function finish(job, { begun, pid, worktree }) {
844
+ const dir = dirname(begun.log);
845
+ const exit = join(dir, "exit");
846
+ const deadline = Date.parse(begun.start) + options.timeoutMs;
847
+ let timedOut = false;
848
+ agents.add(pid);
849
+ try {
850
+ while (!existsSync(exit) && alive(pid)) {
851
+ if (stopping && !killing) return void 0;
852
+ if (!timedOut && Date.now() >= deadline) {
853
+ timedOut = true;
854
+ killGroup(pid);
855
+ }
856
+ await nap(Math.max(0, Math.min(options.pollMs ?? 1e3, deadline - Date.now())));
857
+ }
858
+ } finally {
859
+ agents.delete(pid);
860
+ }
861
+ killGroup(pid);
862
+ const branch = await keepWork(worktree, `factory/${basename(dir)}`);
863
+ if (killing) return attempt(begun, "stopped", { branch });
864
+ const text = readFileSync(begun.log, "utf8");
865
+ const code = existsSync(exit) ? Number(readFileSync(exit, "utf8")) : 1;
866
+ const agent$2 = agent(begun.agent, begun);
867
+ const outcome = timedOut ? "timeout" : classify(agent$2, code, text);
868
+ return attempt(begun, outcome, {
869
+ url: outcome === "done" && await delivered(job.item) || text.match(URL$1)?.at(-1),
870
+ branch
871
+ });
872
+ }
873
+ /**
874
+ * Jobs the last run left running: those with a live agent go to this run's worker for that agent;
875
+ * the rest stopped before their agent started, so they go back to the queue.
876
+ */
877
+ const handed = /* @__PURE__ */ new Map();
878
+ for (const job of readJobs(state)) {
879
+ if (job.state !== "running") continue;
880
+ if (!existsSync(livePath(state, job.id))) {
881
+ requeueJob(state, job.id);
882
+ continue;
883
+ }
884
+ const live = JSON.parse(readFileSync(livePath(state, job.id), "utf8"));
885
+ handed.set(live.begun.agent, {
886
+ job,
887
+ live
888
+ });
889
+ }
890
+ process.on("SIGTERM", handOff);
891
+ process.on("SIGINT", kill);
892
+ options.signal?.addEventListener("abort", aborted);
893
+ writeStatus();
894
+ try {
895
+ await Promise.all(workers.map((worker) => work(worker, handed.get(worker.agent))));
896
+ } catch (error) {
897
+ kill();
898
+ throw error;
899
+ } finally {
900
+ process.off("SIGTERM", handOff);
901
+ process.off("SIGINT", kill);
902
+ options.signal?.removeEventListener("abort", aborted);
903
+ await topping;
904
+ writeStatus();
905
+ }
906
+ return id;
907
+ }
908
+ const attempt = (begun, outcome, rest = {}) => ({
909
+ ...begun,
910
+ end: (/* @__PURE__ */ new Date()).toISOString(),
911
+ outcome,
912
+ ...rest
913
+ });
914
+ /**
915
+ * The agent in its own process group, so a timeout or kill takes down everything it started, and
916
+ * detached, so it outlives a run that hands it off. The shell in between reads the prompt from a file
917
+ * and writes the exit code to one, both of which outlast this process. Returns the group's pid.
918
+ */
919
+ function startAgent(agent, cwd, dir) {
920
+ const log = openSync(join(dir, "agent.log"), "w");
921
+ const child = spawn("sh", [
922
+ "-c",
923
+ "dir=$1; shift; \"$@\" <\"$dir/prompt.md\"; echo $? >\"$dir/exit\"",
924
+ "sh",
925
+ dir,
926
+ agent.command,
927
+ ...agent.args
928
+ ], {
929
+ cwd,
930
+ detached: true,
931
+ stdio: [
932
+ "ignore",
933
+ log,
934
+ log
935
+ ]
936
+ });
937
+ closeSync(log);
938
+ child.unref();
939
+ return child.pid;
940
+ }
941
+ /** The PR the agent opened, as GitHub knows it; undefined when gh can't tell, and the log's last link stands in. */
942
+ async function delivered(item) {
943
+ try {
944
+ return await findPr(item);
945
+ } catch (error) {
946
+ log(`gh can't find the PR for ${item.id}: ${error.message.trim().split("\n")[0]}`);
947
+ return;
948
+ }
949
+ }
950
+ /** The worktree at origin's fresh default branch; reused across jobs so installed dependencies survive. */
951
+ async function checkout(worktree, repo) {
952
+ await output("git", [
953
+ "fetch",
954
+ "--quiet",
955
+ "origin"
956
+ ], repo);
957
+ const head = await output("git", [
958
+ "ls-remote",
959
+ "--symref",
960
+ "origin",
961
+ "HEAD"
962
+ ], repo);
963
+ const branch = /^ref: refs\/heads\/(\S+)\tHEAD$/m.exec(head)?.[1];
964
+ if (!branch) throw new Error(`origin of ${repo} names no default branch`);
965
+ const base = `origin/${branch}`;
966
+ if (!existsSync(worktree)) {
967
+ await output("git", ["worktree", "prune"], repo);
968
+ await output("git", [
969
+ "worktree",
970
+ "add",
971
+ "--quiet",
972
+ "--detach",
973
+ worktree,
974
+ base
975
+ ], repo);
976
+ return;
977
+ }
978
+ await keepWork(worktree, `factory/leftover-${Date.now()}`, ["--remotes", "--branches"]);
979
+ await output("git", [
980
+ "checkout",
981
+ "--quiet",
982
+ "--force",
983
+ "--detach",
984
+ base
985
+ ], worktree);
986
+ await output("git", ["clean", "-fdq"], worktree);
987
+ }
988
+ /** Saves commits and changes that aren't on `saved` refs to a branch; returns its name, if there was any. */
989
+ async function keepWork(worktree, branch, saved = ["--remotes"]) {
990
+ if ((await output("git", ["status", "--porcelain"], worktree)).trim()) {
991
+ await output("git", ["add", "-A"], worktree);
992
+ await output("git", [
993
+ "commit",
994
+ "--quiet",
995
+ "--no-verify",
996
+ "-m",
997
+ "factory: changes the agent left uncommitted"
998
+ ], worktree);
999
+ }
1000
+ if (!(await output("git", [
1001
+ "log",
1002
+ "--oneline",
1003
+ "HEAD",
1004
+ "--not",
1005
+ ...saved
1006
+ ], worktree)).trim()) return void 0;
1007
+ await output("git", [
1008
+ "branch",
1009
+ "--force",
1010
+ branch,
1011
+ "HEAD"
1012
+ ], worktree);
1013
+ return branch;
1014
+ }
1015
+ function killGroup(pid) {
1016
+ for (const [signal, delay] of [["SIGTERM", 0], ["SIGKILL", 1e4]]) setTimeout(() => {
1017
+ try {
1018
+ process.kill(-pid, signal);
1019
+ } catch (error) {
1020
+ if (error.code !== "ESRCH") throw error;
1021
+ }
1022
+ }, delay).unref();
1023
+ }
1024
+ function log(message) {
1025
+ console.log(`${(/* @__PURE__ */ new Date()).toISOString()} ${message}`);
1026
+ }
1027
+ //#endregion
1028
+ //#region src/backend/power.ts
1029
+ const logPath = (state) => join(state, "factory.log");
1030
+ /** `factory run <args>` in the background, outliving this process and whoever started it. */
1031
+ function turnOn(state, args) {
1032
+ const pid = runningPid(state);
1033
+ if (pid) throw new Error(`already on (pid ${pid})`);
1034
+ const log = openSync(logPath(state), "a");
1035
+ const child = spawn(process.execPath, [
1036
+ process.argv[1],
1037
+ "run",
1038
+ ...args
1039
+ ], {
1040
+ detached: true,
1041
+ stdio: [
1042
+ "ignore",
1043
+ log,
1044
+ log
1045
+ ]
1046
+ });
1047
+ child.unref();
1048
+ writeFileSync(pidPath(state), `${child.pid}\n`);
1049
+ return child.pid;
1050
+ }
1051
+ /**
1052
+ * Stops the run. Its agents keep working and the next run records what they did; with kill they get
1053
+ * SIGTERM, then SIGKILL after 10 seconds, and their jobs go back to the queue. Returns the pid it
1054
+ * stopped, if one was running.
1055
+ */
1056
+ async function turnOff(state, kill = false) {
1057
+ const pid = runningPid(state);
1058
+ if (!pid) return void 0;
1059
+ process.kill(pid, kill ? "SIGINT" : "SIGTERM");
1060
+ for (let waited = 0; alive(pid); waited++) {
1061
+ if (waited === 60) throw new Error(`pid ${pid} is still running after 30 seconds`);
1062
+ await new Promise((resolve) => setTimeout(resolve, 500));
1063
+ }
1064
+ rmSync(pidPath(state), { force: true });
1065
+ return pid;
1066
+ }
1067
+ //#endregion
1068
+ //#region src/backend/claude.ts
1069
+ const update = (event) => ({
1070
+ type: "message_update",
1071
+ assistantMessageEvent: event
1072
+ });
1073
+ function claudeChat(effort) {
1074
+ /** The reply being streamed, as Pi parts, and the index of the block streaming now. */
1075
+ let parts = [];
1076
+ let block = 0;
1077
+ /** Stop was clicked, so the run ending early isn't an error. */
1078
+ let aborted = false;
1079
+ return {
1080
+ /** What to write to Claude Code for one of Pi's commands, and what the page hears right away. */
1081
+ command(record) {
1082
+ if (record.type === "prompt") {
1083
+ aborted = false;
1084
+ return {
1085
+ write: [{
1086
+ type: "user",
1087
+ message: {
1088
+ role: "user",
1089
+ content: record.message
1090
+ }
1091
+ }],
1092
+ emit: [{ type: "agent_start" }]
1093
+ };
1094
+ }
1095
+ if (record.type === "abort") {
1096
+ aborted = true;
1097
+ return {
1098
+ write: [{
1099
+ type: "control_request",
1100
+ request_id: "abort",
1101
+ request: { subtype: "interrupt" }
1102
+ }],
1103
+ emit: []
1104
+ };
1105
+ }
1106
+ return {
1107
+ write: [],
1108
+ emit: []
1109
+ };
1110
+ },
1111
+ record(record) {
1112
+ if (record.parent_tool_use_id) return [];
1113
+ switch (record.type) {
1114
+ case "system":
1115
+ if (record.subtype !== "init") return [];
1116
+ return [{
1117
+ type: "response",
1118
+ command: "get_state",
1119
+ success: true,
1120
+ data: {
1121
+ model: { name: record.model },
1122
+ thinkingLevel: effort ?? "default"
1123
+ }
1124
+ }];
1125
+ case "stream_event": return stream(record.event);
1126
+ case "assistant": {
1127
+ const out = [];
1128
+ for (const each of record.message.content) {
1129
+ const piece = part(each);
1130
+ if (piece.type === "toolCall") out.push(update({
1131
+ type: "toolcall_end",
1132
+ contentIndex: block,
1133
+ toolCall: piece
1134
+ }));
1135
+ while (parts.length <= block) parts.push({ type: "thinking" });
1136
+ parts[block] = piece;
1137
+ }
1138
+ return out;
1139
+ }
1140
+ case "user": return user(record.message.content).flatMap((message) => [{
1141
+ type: "message_start",
1142
+ message
1143
+ }, {
1144
+ type: "message_end",
1145
+ message
1146
+ }]);
1147
+ case "result": {
1148
+ const settled = { type: "agent_settled" };
1149
+ if (!record.is_error || aborted) return [settled];
1150
+ return [{
1151
+ type: "response",
1152
+ command: "prompt",
1153
+ success: false,
1154
+ error: record.result ?? record.errors?.join("; ") ?? "Claude Code failed"
1155
+ }, settled];
1156
+ }
1157
+ default: return [];
1158
+ }
1159
+ }
1160
+ };
1161
+ /** Pi's stream events for one of the Messages API's. */
1162
+ function stream(event) {
1163
+ const contentIndex = event.index;
1164
+ switch (event.type) {
1165
+ case "message_start":
1166
+ parts = [];
1167
+ block = 0;
1168
+ return [{
1169
+ type: "message_start",
1170
+ message: {
1171
+ role: "assistant",
1172
+ content: []
1173
+ }
1174
+ }];
1175
+ case "content_block_start": {
1176
+ block = contentIndex ?? 0;
1177
+ const started = event.content_block;
1178
+ if (started.type === "text") return [update({
1179
+ type: "text_start",
1180
+ contentIndex
1181
+ })];
1182
+ if (started.type === "thinking") return [update({
1183
+ type: "thinking_start",
1184
+ contentIndex
1185
+ })];
1186
+ if (started.type === "tool_use" || started.type === "server_tool_use") return [update({
1187
+ type: "toolcall_start",
1188
+ contentIndex,
1189
+ id: started.id,
1190
+ toolName: toolName(started.name)
1191
+ })];
1192
+ return [];
1193
+ }
1194
+ case "content_block_delta":
1195
+ if (event.delta?.type !== "text_delta") return [];
1196
+ return [update({
1197
+ type: "text_delta",
1198
+ contentIndex,
1199
+ delta: event.delta.text
1200
+ })];
1201
+ case "message_stop": {
1202
+ const content = parts;
1203
+ parts = [];
1204
+ return [{
1205
+ type: "message_end",
1206
+ message: {
1207
+ role: "assistant",
1208
+ content
1209
+ }
1210
+ }];
1211
+ }
1212
+ default: return [];
1213
+ }
1214
+ }
1215
+ }
1216
+ /** One block of an assistant message as a Pi part. */
1217
+ function part(block) {
1218
+ if (block.type === "text") return {
1219
+ type: "text",
1220
+ text: block.text ?? ""
1221
+ };
1222
+ if (block.type === "tool_use" || block.type === "server_tool_use") {
1223
+ const input = block.input ?? {};
1224
+ const path = input.file_path ?? input.notebook_path;
1225
+ return {
1226
+ type: "toolCall",
1227
+ id: block.id,
1228
+ name: toolName(block.name),
1229
+ arguments: path === void 0 ? input : {
1230
+ ...input,
1231
+ path
1232
+ }
1233
+ };
1234
+ }
1235
+ return { type: "thinking" };
1236
+ }
1237
+ /** Claude Code's tools by the names the page shows steps for. */
1238
+ const TOOLS = {
1239
+ Bash: "bash",
1240
+ Read: "read",
1241
+ Edit: "edit",
1242
+ MultiEdit: "edit",
1243
+ NotebookEdit: "edit",
1244
+ Write: "write",
1245
+ Grep: "grep",
1246
+ Glob: "find"
1247
+ };
1248
+ const toolName = (name) => TOOLS[name] ?? name;
1249
+ /** A user message as Pi messages: the prompt, or the results of the tools the reply called. */
1250
+ function user(content) {
1251
+ if (typeof content === "string") return [{
1252
+ role: "user",
1253
+ content
1254
+ }];
1255
+ return content.filter((block) => block.type === "tool_result").map((result) => ({
1256
+ role: "toolResult",
1257
+ toolCallId: result.tool_use_id,
1258
+ content: [{
1259
+ type: "text",
1260
+ text: typeof result.content === "string" ? result.content : (result.content ?? []).flatMap((block) => block.text ?? []).join("\n")
1261
+ }],
1262
+ isError: result.is_error === true
1263
+ }));
1264
+ }
1265
+ //#endregion
1266
+ //#region src/backend/chat.ts
1267
+ /** Pi speaks the page's protocol already. */
1268
+ const pi = {
1269
+ command: (record) => ({
1270
+ write: [record],
1271
+ emit: []
1272
+ }),
1273
+ record: (record) => [record]
1274
+ };
1275
+ /**
1276
+ * One chat for planning the queue, shared by every open page, with the agent the settings name.
1277
+ * The page reads Pi's RPC records (see Pi's docs/rpc.md), whichever agent replies. The conversation
1278
+ * lasts until reset, and a page that opens later gets it replayed. It survives a restart of serve:
1279
+ * the agent keeps its session (Pi's in chat-sessions/, Claude Code's in its own folder) and the page's
1280
+ * records are in chat.jsonl.
1281
+ */
1282
+ function chat(state) {
1283
+ const savedPath = join(state, "chat.jsonl");
1284
+ const conversationPath = join(state, "chat.json");
1285
+ let session;
1286
+ const pages = /* @__PURE__ */ new Set();
1287
+ /** The agent session this chat continues; `prompted` once the agent has one to resume. */
1288
+ let conversation = existsSync(conversationPath) ? JSON.parse(readFileSync(conversationPath, "utf8")) : void 0;
1289
+ /** Every record of this conversation, minus the deltas of replies that have ended. */
1290
+ let log = existsSync(savedPath) ? readFileSync(savedPath, "utf8").split("\n").filter(Boolean).map((line) => JSON.parse(line)) : [];
1291
+ if (log.length > 0) log.push({ type: "exit" });
1292
+ function emit(record) {
1293
+ if (record.type === "message_end") log = log.filter((each) => each.type !== "message_update");
1294
+ log.push(record);
1295
+ if (record.type !== "message_update") appendFileSync(savedPath, `${JSON.stringify(record)}\n`);
1296
+ for (const page of pages) page.write(`data: ${JSON.stringify(record)}\n\n`);
1297
+ }
1298
+ function save(change) {
1299
+ conversation = change;
1300
+ writeFileSync(conversationPath, `${JSON.stringify(conversation)}\n`);
1301
+ }
1302
+ function start() {
1303
+ const { agent, ...tuning } = readSettings(state).chat;
1304
+ if (conversation?.agent !== agent) save({
1305
+ agent,
1306
+ id: randomUUID(),
1307
+ prompted: false
1308
+ });
1309
+ const { id, prompted } = conversation;
1310
+ const { command, args } = chatAgent(agent, tuning, context(), {
1311
+ id,
1312
+ resume: prompted,
1313
+ folder: join(state, "chat-sessions")
1314
+ });
1315
+ const protocol = agent === "claude" ? claudeChat(tuning.effort) : pi;
1316
+ const child = spawn(command, args, { cwd: state });
1317
+ createInterface({ input: child.stdout }).on("line", (line) => {
1318
+ if (line.startsWith("{") && session?.child === child) for (const record of protocol.record(JSON.parse(line))) emit(record);
1319
+ });
1320
+ child.stdin.on("error", () => {});
1321
+ let stderr = "";
1322
+ child.stderr.on("data", (data) => stderr = (stderr + data).slice(-2e3));
1323
+ child.on("error", (error) => {
1324
+ if (session?.child === child) session = void 0;
1325
+ emit({
1326
+ type: "exit",
1327
+ error: `Can't start ${command}: ${error.message}`
1328
+ });
1329
+ });
1330
+ child.on("exit", (code) => {
1331
+ if (session?.child !== child) return;
1332
+ session = void 0;
1333
+ emit({
1334
+ type: "exit",
1335
+ error: code ? stderr.trim() || `${command} exited with code ${code}` : void 0
1336
+ });
1337
+ });
1338
+ session = {
1339
+ child,
1340
+ protocol
1341
+ };
1342
+ send({ type: "get_state" });
1343
+ }
1344
+ function send(record) {
1345
+ if (!session) start();
1346
+ const { write, emit: heard } = session.protocol.command(record);
1347
+ for (const each of write) session.child.stdin.write(`${JSON.stringify(each)}\n`);
1348
+ for (const each of heard) emit(each);
1349
+ }
1350
+ return {
1351
+ /** Streams records to a page, starting with the conversation so far. */
1352
+ listen(response) {
1353
+ response.writeHead(200, {
1354
+ "content-type": "text/event-stream",
1355
+ "cache-control": "no-store"
1356
+ });
1357
+ response.write(": connected\n\n");
1358
+ for (const record of log) response.write(`data: ${JSON.stringify(record)}\n\n`);
1359
+ pages.add(response);
1360
+ response.on("close", () => pages.delete(response));
1361
+ },
1362
+ prompt(message) {
1363
+ send({
1364
+ type: "prompt",
1365
+ message
1366
+ });
1367
+ if (!conversation.prompted) save({
1368
+ ...conversation,
1369
+ prompted: true
1370
+ });
1371
+ },
1372
+ abort: () => session && send({ type: "abort" }),
1373
+ /** Answers a question from one of Pi's extensions, which Pi waits on; every page closes it. */
1374
+ answer(reply) {
1375
+ if (!session) return;
1376
+ send({
1377
+ type: "extension_ui_response",
1378
+ ...reply
1379
+ });
1380
+ emit({
1381
+ type: "extension_ui_answered",
1382
+ id: reply.id
1383
+ });
1384
+ },
1385
+ reset() {
1386
+ session?.child.stdin.end();
1387
+ session = void 0;
1388
+ for (const page of pages) page.write(`data: ${JSON.stringify({ type: "reset" })}\n\n`);
1389
+ log = [];
1390
+ rmSync(savedPath, { force: true });
1391
+ rmSync(conversationPath, { force: true });
1392
+ conversation = void 0;
1393
+ }
1394
+ };
1395
+ }
1396
+ function context() {
1397
+ const factory = `"${process.execPath}" "${process.argv[1]}"`;
1398
+ return `You run inside the web page of factory, which works a queue of workwork candidates with headless coding agents, often overnight. You help the user decide what to put in the queue.
1399
+
1400
+ - The current folder is factory's state. candidates.json holds every repo's candidates from workwork ({ at, items: [{ repo, label, id, play, level, title }] }); jobs.jsonl is the queue and results, one event per line. runs/ holds each attempt's prompt.md and agent.log.
1401
+ - \`${factory} status\` shows the queue and \`${factory} report --all\` past results. \`workwork prompt <id>\`, run in the candidate's repo, shows the full task.
1402
+ - To queue candidates: \`${factory} add <id>... --repo <repo>\`, optionally with \`--agent ${AGENT_NAMES.join("|")}\`. Picked jobs go ahead of auto ones.
1403
+ - Levels: auto needs no review, skim a quick look, decide needs the user's judgment. Prefer a few that fit together over many that touch the same files, since those conflict when merged.
1404
+
1405
+ Propose what to queue and why, briefly, and add jobs only once the user agrees. Never start, stop or run the factory, never change files or git state, and never merge or close PRs: the user does those from the page.`;
1406
+ }
1407
+ //#endregion
1408
+ //#region src/backend/repos.ts
1409
+ /** Where workwork --all looks for repos, with the same fallback. */
1410
+ const SRC = process.env.SRC ?? process.cwd();
1411
+ /**
1412
+ * The folders under $SRC a repo can be cloned into, and the GitHub user's own repos (not forked or
1413
+ * archived, the ones workwork works in) that aren't cloned anywhere under $SRC yet, newest first.
1414
+ */
1415
+ async function clonableRepos() {
1416
+ const listed = JSON.parse(await output("gh", [
1417
+ "repo",
1418
+ "list",
1419
+ "--source",
1420
+ "--no-archived",
1421
+ "--limit",
1422
+ "5000",
1423
+ "--json",
1424
+ "nameWithOwner,description,createdAt"
1425
+ ], SRC));
1426
+ const local = findRepos(SRC, 4);
1427
+ const cloned = new Set(await Promise.all(local.map(originName)));
1428
+ const repos = listed.filter((repo) => !cloned.has(repo.nameWithOwner.toLowerCase())).map((repo) => ({
1429
+ name: repo.nameWithOwner,
1430
+ description: repo.description,
1431
+ createdAt: repo.createdAt
1432
+ })).sort((a, b) => b.createdAt.localeCompare(a.createdAt));
1433
+ const counts = /* @__PURE__ */ new Map();
1434
+ for (const repo of local) {
1435
+ const folder = relative(SRC, repo).split(sep)[0];
1436
+ counts.set(folder, (counts.get(folder) ?? 0) + 1);
1437
+ }
1438
+ const options = folders();
1439
+ const usual = options.toSorted((a, b) => (counts.get(b) ?? 0) - (counts.get(a) ?? 0))[0];
1440
+ return {
1441
+ root: SRC,
1442
+ folders: options,
1443
+ usual,
1444
+ repos
1445
+ };
1446
+ }
1447
+ /** Clones `name` (owner/repo) to $SRC/<folder>/<repo> and returns that path. */
1448
+ async function cloneRepo(name, folder) {
1449
+ if (!/^[\w.-]+\/[\w.-]+$/.test(name)) throw new Error(`${name} isn't a GitHub owner/repo`);
1450
+ if (!folders().includes(folder)) throw new Error(`${folder} isn't a folder under ${SRC}`);
1451
+ const path = join(SRC, folder, name.split("/")[1]);
1452
+ if (existsSync(path)) throw new Error(`${path} already exists`);
1453
+ await output("gh", [
1454
+ "repo",
1455
+ "clone",
1456
+ name,
1457
+ path
1458
+ ], SRC);
1459
+ return path;
1460
+ }
1461
+ /** $SRC's own subfolders that aren't repos themselves. */
1462
+ function folders() {
1463
+ return readdirSync(SRC, { withFileTypes: true }).filter((entry) => entry.isDirectory() && !entry.name.startsWith(".")).filter((entry) => !existsSync(join(SRC, entry.name, ".git"))).map((entry) => entry.name).sort();
1464
+ }
1465
+ /** Repos under `dir`, searched the way workwork --all does. */
1466
+ function findRepos(dir, depth) {
1467
+ if (existsSync(join(dir, ".git"))) return [dir];
1468
+ if (depth === 0) return [];
1469
+ return readdirSync(dir, { withFileTypes: true }).filter((entry) => entry.isDirectory() && !entry.name.startsWith(".") && entry.name !== "node_modules").flatMap((entry) => findRepos(join(dir, entry.name), depth - 1));
1470
+ }
1471
+ /** owner/repo of the GitHub origin, lowercased; empty when there is none. */
1472
+ async function originName(repo) {
1473
+ const url = await output("git", [
1474
+ "remote",
1475
+ "get-url",
1476
+ "origin"
1477
+ ], repo).catch(() => "");
1478
+ return /github\.com[:/](.+?)(?:\.git)?\/?$/.exec(url.trim())?.[1].toLowerCase() ?? "";
1479
+ }
1480
+ //#endregion
1481
+ //#region src/backend/models.ts
1482
+ /** Every model each agent offers, its short list first. */
1483
+ function agentModels(name) {
1484
+ if (name === "pi") return piModels();
1485
+ if (name === "claude") return claudeModels();
1486
+ throw new Error(`unknown agent ${name}`);
1487
+ }
1488
+ const PI_LEVELS = [
1489
+ "off",
1490
+ "minimal",
1491
+ "low",
1492
+ "medium",
1493
+ "high",
1494
+ "xhigh",
1495
+ "max"
1496
+ ];
1497
+ /** Pi's scoped models (Ctrl+P in Pi, `enabledModels` in its settings) first, then all it knows. */
1498
+ async function piModels() {
1499
+ const settings = join(process.env.PI_CODING_AGENT_DIR ?? join(homedir(), ".pi", "agent"), "settings.json");
1500
+ const scoped = existsSync(settings) ? JSON.parse(readFileSync(settings, "utf8")).enabledModels ?? [] : [];
1501
+ const known = (await ask("pi", [
1502
+ "--mode",
1503
+ "rpc",
1504
+ "--no-session",
1505
+ "--offline"
1506
+ ], { type: "get_available_models" }, (record) => record.type === "response" && record.command === "get_available_models")).data.models;
1507
+ const scope = new Set(scoped.flatMap((each) => matches(known, each)));
1508
+ const rest = known.filter((model) => !scope.has(model));
1509
+ return [...scope, ...rest].map((model) => ({
1510
+ value: `${model.provider}/${model.id}`,
1511
+ label: model.name,
1512
+ efforts: piLevels(model),
1513
+ scoped: scope.has(model)
1514
+ }));
1515
+ }
1516
+ function matches(known, pattern) {
1517
+ const glob = new RegExp(`^${pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replaceAll("*", ".*")}$`, "i");
1518
+ const full = known.filter((model) => glob.test(`${model.provider}/${model.id}`));
1519
+ return full.length > 0 ? full : known.filter((model) => glob.test(model.id));
1520
+ }
1521
+ /** Pi's own rule (pi-ai's getSupportedThinkingLevels): null rules a level out, xhigh and max must be named. */
1522
+ function piLevels({ reasoning, thinkingLevelMap: map = {} }) {
1523
+ if (!reasoning) return ["off"];
1524
+ return PI_LEVELS.filter((level) => map[level] === null ? false : level === "xhigh" || level === "max" ? level in map : true);
1525
+ }
1526
+ /**
1527
+ * Claude Code's current models, as it tells a host that starts it. Its aliases (opus, sonnet, fable,
1528
+ * haiku) are the current ones; full ids like claude-opus-4-8 are older.
1529
+ */
1530
+ async function claudeModels() {
1531
+ const answer = await ask("claude", [
1532
+ "-p",
1533
+ "--input-format",
1534
+ "stream-json",
1535
+ "--output-format",
1536
+ "stream-json",
1537
+ "--verbose"
1538
+ ], {
1539
+ type: "control_request",
1540
+ request_id: "models",
1541
+ request: { subtype: "initialize" }
1542
+ }, (record) => record.type === "control_response");
1543
+ if (answer.response.subtype !== "success") throw new Error(answer.response.error);
1544
+ return answer.response.response.models.filter((model) => model.value !== "default" && !model.value.startsWith("claude-")).map((model) => ({
1545
+ value: model.value,
1546
+ label: model.displayName,
1547
+ efforts: model.supportsEffort ? model.supportedEffortLevels ?? [] : [],
1548
+ scoped: true
1549
+ }));
1550
+ }
1551
+ /** Starts an agent, sends it one request on stdin, and resolves with the record that answers it. */
1552
+ function ask(command, args, request, answers) {
1553
+ const child = spawn(command, args, { cwd: homedir() });
1554
+ child.stdin.end(`${JSON.stringify(request)}\n`);
1555
+ return new Promise((resolve, reject) => {
1556
+ child.on("error", reject);
1557
+ child.on("exit", (code) => reject(/* @__PURE__ */ new Error(`${command} exited with code ${code} before answering`)));
1558
+ createInterface({ input: child.stdout }).on("line", (line) => {
1559
+ if (!line.startsWith("{")) return;
1560
+ const record = JSON.parse(line);
1561
+ if (!answers(record)) return;
1562
+ resolve(record);
1563
+ child.kill();
1564
+ });
1565
+ });
1566
+ }
1567
+ //#endregion
1568
+ //#region src/backend/types.ts
1569
+ const onTrain = (car) => car !== void 0 && car.step !== "merged" && car.step !== "skipped";
1570
+ /** The PR of the job's latest attempt that linked one. */
1571
+ const prUrl = (job) => job.attempts.findLast((attempt) => attempt.url?.includes("/pull/"))?.url;
1572
+ /** Merging from the page skips GitHub's own page, so it only goes ahead when nothing argues against it. */
1573
+ const canMerge = (pr) => pr.state === "OPEN" && !pr.draft && pr.conflicts === false && (pr.checks === "passing" || pr.checks === "none");
1574
+ const canRebase = (pr) => pr.state === "OPEN" && pr.conflicts === true && !pr.fork;
1575
+ //#endregion
1576
+ //#region src/backend/train.ts
1577
+ const POLL = 1e4;
1578
+ const PATIENCE = 18e5;
1579
+ /** A PR gets this many tries at rebase-then-merge before the train gives up on it. */
1580
+ const TRIES = 3;
1581
+ /**
1582
+ * Merges PRs one after another per repo, since each merge moves the base under the next one.
1583
+ * Repos go in parallel. A PR that conflicts is rebased; one that can't be is skipped with the reason,
1584
+ * and when git stopped on conflicts, handed to `onConflicts` to queue for an agent.
1585
+ */
1586
+ function mergeTrain(prs, onConflicts) {
1587
+ const cars = /* @__PURE__ */ new Map();
1588
+ /** Each repo's last PR on the train; a new one waits for it, even from a later start. */
1589
+ const tracks = /* @__PURE__ */ new Map();
1590
+ /** GitHub's answer once mergeability is known and checks have finished. */
1591
+ async function settled(id, url) {
1592
+ const deadline = Date.now() + PATIENCE;
1593
+ for (;;) {
1594
+ const pr = await prs.fresh(url);
1595
+ if (pr.state !== "OPEN" || pr.conflicts !== null && pr.checks !== "pending") return pr;
1596
+ if (Date.now() > deadline) throw new Error("checks still running after 30 minutes");
1597
+ cars.set(id, { step: "waiting" });
1598
+ await new Promise((resolve) => setTimeout(resolve, POLL));
1599
+ }
1600
+ }
1601
+ async function land(id, url) {
1602
+ for (let tries = 1;; tries++) {
1603
+ const pr = await settled(id, url);
1604
+ if (pr.state !== "OPEN") throw new Error(`already ${pr.state.toLowerCase()}`);
1605
+ if (pr.conflicts) {
1606
+ if (pr.fork) throw new Error("conflicts in a fork");
1607
+ } else {
1608
+ if (!canMerge(pr)) throw new Error(blocker(pr));
1609
+ cars.set(id, { step: "merging" });
1610
+ try {
1611
+ await mergePr(url);
1612
+ await prs.fresh(url);
1613
+ return;
1614
+ } catch (error) {
1615
+ if (tries >= TRIES || pr.fork) throw error;
1616
+ }
1617
+ }
1618
+ if (tries >= TRIES) throw new Error(`still conflicting after ${TRIES} rebases`);
1619
+ cars.set(id, { step: "rebasing" });
1620
+ await rebasePr(url);
1621
+ }
1622
+ }
1623
+ return {
1624
+ cars: () => Object.fromEntries(cars),
1625
+ /** Smallest diffs first, so the ones most likely to clash merge last. */
1626
+ start(jobs) {
1627
+ const boarding = jobs.filter(({ id }) => !onTrain(cars.get(id)));
1628
+ const known = prs.get(boarding.map(({ url }) => url));
1629
+ const size = (url) => (known[url]?.additions ?? 0) + (known[url]?.deletions ?? 0);
1630
+ for (const { id, url } of boarding.toSorted((a, b) => size(a.url) - size(b.url))) {
1631
+ cars.set(id, { step: "queued" });
1632
+ const repo = url.replace(/\/pull\/\d+$/, "");
1633
+ const track = (tracks.get(repo) ?? Promise.resolve()).then(() => land(id, url)).then(() => void cars.set(id, { step: "merged" }), (error) => {
1634
+ if (!(error instanceof Conflicts)) return void cars.set(id, {
1635
+ step: "skipped",
1636
+ reason: error.message
1637
+ });
1638
+ onConflicts(id, url, error.files);
1639
+ const reason = `conflicts in ${error.files.join(", ")}; a job to resolve them is queued`;
1640
+ cars.set(id, {
1641
+ step: "skipped",
1642
+ reason
1643
+ });
1644
+ });
1645
+ tracks.set(repo, track);
1646
+ }
1647
+ }
1648
+ };
1649
+ }
1650
+ function blocker(pr) {
1651
+ if (pr.draft) return "draft";
1652
+ if (pr.checks === "failing") return "checks failing";
1653
+ return "not ready to merge";
1654
+ }
1655
+ //#endregion
1656
+ //#region src/backend/web.ts
1657
+ /** The built page; under pn dev, Vite serves it instead and proxies /api and /runs here. */
1658
+ const WEB = fileURLToPath(new URL("./web", import.meta.url));
1659
+ const TYPES = {
1660
+ ".html": "text/html; charset=utf-8",
1661
+ ".js": "text/javascript",
1662
+ ".css": "text/css",
1663
+ ".svg": "image/svg+xml",
1664
+ ".woff2": "font/woff2"
1665
+ };
1666
+ /** The page and the API it uses: the same state and commands as the CLI, nothing more. */
1667
+ function serve(state, port, host) {
1668
+ const prs = prCache();
1669
+ const train = mergeTrain(prs, (id, url, files) => queueConflicts(state, id, url, files));
1670
+ const planner = chat(state);
1671
+ const models = /* @__PURE__ */ new Map();
1672
+ function modelsOf(name) {
1673
+ if (!models.has(name)) models.set(name, agentModels(name).catch((error) => {
1674
+ console.error(`factory: can't list ${name}'s models: ${error.message}`);
1675
+ models.delete(name);
1676
+ return [];
1677
+ }));
1678
+ return models.get(name);
1679
+ }
1680
+ /** Only cached candidates, so the page can't queue or run workwork for anything it names. */
1681
+ function candidates(wanted) {
1682
+ const cached = new Map(readCandidates(state)?.items.map((item) => [itemKey(item), item]));
1683
+ return wanted.map((each) => {
1684
+ const item = cached.get(itemKey(each));
1685
+ if (!item) throw new Error(`no candidate ${each.id} in ${each.repo}`);
1686
+ return item;
1687
+ });
1688
+ }
1689
+ function jobPr(id) {
1690
+ const job = readJobs(state).find((job) => job.id === id);
1691
+ const url = job && prUrl(job);
1692
+ if (!url) throw new Error(`job ${id} has no PR`);
1693
+ return url;
1694
+ }
1695
+ /** The job's PR, checked with GitHub just now. */
1696
+ async function openPr(id) {
1697
+ const url = jobPr(id);
1698
+ const pr = await prs.fresh(url);
1699
+ if (pr.state !== "OPEN") throw new Error(`${url} is already ${pr.state.toLowerCase()}`);
1700
+ return {
1701
+ url,
1702
+ pr
1703
+ };
1704
+ }
1705
+ const routes = {
1706
+ "GET /api/state": () => snapshot(state, (urls) => prs.get(urls), train.cars()),
1707
+ "GET /api/candidates": () => readCandidates(state) ?? {
1708
+ at: null,
1709
+ items: []
1710
+ },
1711
+ "POST /api/candidates/refresh": () => {
1712
+ refreshCandidates(state, "workwork").catch((error) => console.error(`factory: workwork failed: ${error.message}`));
1713
+ },
1714
+ "GET /api/repos": () => clonableRepos(),
1715
+ "POST /api/repos/clone": async (body) => {
1716
+ await cloneRepo(body.name, body.folder);
1717
+ refreshCandidates(state, "workwork").catch((error) => console.error(`factory: workwork failed: ${error.message}`));
1718
+ },
1719
+ "GET /api/plays": async () => JSON.parse(await output("workwork", ["plays", "--json"], process.cwd())),
1720
+ "POST /api/candidates/prompt": (body) => {
1721
+ const [item] = candidates([body]);
1722
+ return output("workwork", ["prompt", item.id], item.repo);
1723
+ },
1724
+ "POST /api/jobs": (body) => addJobs(state, candidates(body.items), "pick", body.pin),
1725
+ "POST /api/jobs/remove": (_, id) => removeJob(state, id),
1726
+ "POST /api/jobs/retry": (_, id) => retryJob(state, id),
1727
+ "POST /api/jobs/move": (body, id) => moveJob(state, id, body.index),
1728
+ "GET /api/jobs/pr": (_, id) => readPrDetails(jobPr(id)),
1729
+ "POST /api/jobs/merge": async (_, id) => {
1730
+ const { url, pr } = await openPr(id);
1731
+ if (!canMerge(pr)) throw new Error(`${url} isn't ready to merge: see it on GitHub`);
1732
+ await mergePr(url);
1733
+ await prs.fresh(url);
1734
+ },
1735
+ "POST /api/jobs/rebase": async (_, id) => {
1736
+ const { url, pr } = await openPr(id);
1737
+ if (!canRebase(pr)) throw new Error(`${url} isn't ready to rebase: see it on GitHub`);
1738
+ await rebasePr(url);
1739
+ await prs.fresh(url);
1740
+ },
1741
+ "POST /api/train": (body) => train.start(body.ids.map((id) => ({
1742
+ id,
1743
+ url: jobPr(id)
1744
+ }))),
1745
+ "POST /api/chat": (body) => planner.prompt(body.message),
1746
+ "POST /api/chat/abort": () => planner.abort(),
1747
+ "POST /api/chat/answer": (body) => planner.answer(body),
1748
+ "POST /api/chat/reset": () => planner.reset(),
1749
+ "POST /api/jobs/close": async (_, id) => {
1750
+ const { url } = await openPr(id);
1751
+ await closePr(url);
1752
+ await prs.fresh(url);
1753
+ },
1754
+ "GET /api/models": async () => Object.fromEntries(await Promise.all(AGENT_NAMES.map(async (name) => [name, await modelsOf(name)]))),
1755
+ "PUT /api/settings": (body) => {
1756
+ const before = JSON.stringify(readSettings(state).chat);
1757
+ writeSettings(state, checkSettings(body));
1758
+ if (JSON.stringify(body.chat) !== before) planner.reset();
1759
+ },
1760
+ "POST /api/on": (body) => {
1761
+ checkSettings({
1762
+ ...readSettings(state),
1763
+ ...body
1764
+ });
1765
+ return turnOn(state, runArgs(body));
1766
+ },
1767
+ "POST /api/off": () => turnOff(state)
1768
+ };
1769
+ return createServer(async (request, response) => {
1770
+ const path = new URL(request.url ?? "/", "http://factory").pathname;
1771
+ const [, jobId, action] = /^\/api\/jobs\/(\w+)\/(\w+)$/.exec(path) ?? [];
1772
+ const route = routes[`${request.method} ${jobId ? `/api/jobs/${action}` : path}`];
1773
+ try {
1774
+ if (`${request.method} ${path}` === "GET /api/chat") return planner.listen(response);
1775
+ if (route) {
1776
+ const result = await route(await body(request), jobId);
1777
+ return send(response, "application/json", JSON.stringify(result ?? null));
1778
+ }
1779
+ const file = resolve(state, `.${path}`);
1780
+ if (file.startsWith(join(state, "runs") + sep) && isFile(file)) return send(response, "text/plain; charset=utf-8", readFileSync(file));
1781
+ const asset = resolve(WEB, `.${path}`);
1782
+ if (asset.startsWith(WEB + sep) && isFile(asset)) return send(response, TYPES[extname(asset)] ?? "application/octet-stream", readFileSync(asset));
1783
+ if (path.startsWith("/api/") || path.startsWith("/runs/")) {
1784
+ response.writeHead(404).end("not found");
1785
+ return;
1786
+ }
1787
+ const page = join(WEB, "index.html");
1788
+ if (!isFile(page)) return send(response, "text/plain", "The page isn't built: run `vp build`, or use `pn dev`.");
1789
+ send(response, TYPES[".html"], readFileSync(page));
1790
+ } catch (error) {
1791
+ response.writeHead(400, { "content-type": "application/json" }).end(JSON.stringify({ error: error.message }));
1792
+ }
1793
+ }).listen(port, host, () => {
1794
+ console.log(`factory: serving http://${host}:${port}`);
1795
+ if (host !== "127.0.0.1" && host !== "localhost") console.log("factory: anyone who reaches this address can run agents on this machine");
1796
+ });
1797
+ }
1798
+ function snapshot(state, prs, train) {
1799
+ const statusPath = join(state, "status.json");
1800
+ const candidates = readCandidates(state);
1801
+ const jobs = readJobs(state);
1802
+ return {
1803
+ on: runningPid(state) !== void 0,
1804
+ status: existsSync(statusPath) ? JSON.parse(readFileSync(statusPath, "utf8")) : null,
1805
+ settings: readSettings(state),
1806
+ jobs: jobs.map((job) => ({
1807
+ ...job,
1808
+ attempts: job.attempts.map((attempt) => ({
1809
+ ...attempt,
1810
+ log: relative(state, attempt.log)
1811
+ }))
1812
+ })),
1813
+ candidates: {
1814
+ at: candidates?.at ?? null,
1815
+ count: candidates?.items.length ?? 0,
1816
+ refreshing: refreshingCandidates(state)
1817
+ },
1818
+ prs: prs(jobs.flatMap((job) => job.state === "removed" ? [] : prUrl(job) ?? [])),
1819
+ train
1820
+ };
1821
+ }
1822
+ /** A run asked for from the page, as the flags `factory run` takes. */
1823
+ function runArgs({ agents, limit, until }) {
1824
+ const args = [
1825
+ "--agents",
1826
+ Object.keys(agents).join(","),
1827
+ "--limit",
1828
+ String(limit ?? "none")
1829
+ ];
1830
+ if (until) args.push("--until", until);
1831
+ for (const [name, { model, effort }] of Object.entries(agents)) {
1832
+ if (model) args.push("--model", `${name}=${model}`);
1833
+ if (effort) args.push("--effort", `${name}=${effort}`);
1834
+ }
1835
+ return args;
1836
+ }
1837
+ async function body(request) {
1838
+ let text = "";
1839
+ for await (const chunk of request) text += chunk;
1840
+ return text ? JSON.parse(text) : {};
1841
+ }
1842
+ function isFile(path) {
1843
+ return existsSync(path) && statSync(path).isFile();
1844
+ }
1845
+ function send(response, type, body) {
1846
+ response.writeHead(200, {
1847
+ "content-type": type,
1848
+ "cache-control": "no-store"
1849
+ }).end(body);
1850
+ }
1851
+ //#endregion
1852
+ //#region package.json
1853
+ var version = "0.2.0";
1854
+ //#endregion
1855
+ //#region src/backend/cli.ts
1856
+ const USAGE = `factory: works a queue of workwork candidates with headless coding agents
1857
+
1858
+ factory start [run options] turn the factory on, in the background
1859
+ factory run [run options] the same, in the foreground
1860
+ factory stop [--kill] turn it off; agents at work finish, and the next start records them;
1861
+ --kill stops them too and puts their jobs back in the queue
1862
+ factory status on or off, what each agent is doing, the queue
1863
+ factory add <id>... [--repo DIR] [--agent NAME] [--model M] [--effort E]
1864
+ queue workwork candidates from DIR (default: here), ahead of auto jobs
1865
+ factory remove <job> take a job out of the queue or the results, with its kept branches
1866
+ factory retry <job> queue a failed job again, with the same agent
1867
+ factory auto on|off let auto mode keep the queue topped up from workwork, or not
1868
+ factory report [--all] the last run (or every run): outcome, agent, job, PR/issue link or log
1869
+ factory serve [--port 7040] [--host 127.0.0.1]
1870
+ a web page to watch, browse candidates, queue them and turn it on or off
1871
+
1872
+ Run options (defaults from settings.json):
1873
+ --agents claude,pi which registered agents work this run
1874
+ --limit N|none usage-limit hits per agent before it stops; none: wait for each reset
1875
+ --until HH:MM start nothing new after this local time
1876
+ --model NAME=MODEL e.g. --model claude=opus; repeat per agent
1877
+ --effort NAME=LEVEL e.g. --effort pi=high; repeat per agent
1878
+ --timeout MIN minutes before an agent is killed
1879
+
1880
+ State: ${STATE}`;
1881
+ async function main() {
1882
+ const { values, positionals } = parseArgs({
1883
+ allowPositionals: true,
1884
+ options: {
1885
+ agents: { type: "string" },
1886
+ limit: { type: "string" },
1887
+ until: { type: "string" },
1888
+ model: {
1889
+ type: "string",
1890
+ multiple: true
1891
+ },
1892
+ effort: {
1893
+ type: "string",
1894
+ multiple: true
1895
+ },
1896
+ timeout: { type: "string" },
1897
+ repo: { type: "string" },
1898
+ agent: { type: "string" },
1899
+ all: { type: "boolean" },
1900
+ kill: { type: "boolean" },
1901
+ port: {
1902
+ type: "string",
1903
+ default: "7040"
1904
+ },
1905
+ host: {
1906
+ type: "string",
1907
+ default: "127.0.0.1"
1908
+ },
1909
+ help: {
1910
+ type: "boolean",
1911
+ short: "h"
1912
+ },
1913
+ version: {
1914
+ type: "boolean",
1915
+ short: "v"
1916
+ }
1917
+ }
1918
+ });
1919
+ const [command, ...args] = positionals;
1920
+ if (values.help || command === void 0) return console.log(USAGE);
1921
+ if (values.version) return console.log(version);
1922
+ mkdirSync(STATE, { recursive: true });
1923
+ if (command === "status") return status();
1924
+ if (command === "report") return console.log(report(values.all ?? false));
1925
+ if (command === "remove") {
1926
+ for (const id of args) await removeJob(STATE, id);
1927
+ return;
1928
+ }
1929
+ if (command === "retry") return args.forEach((id) => retryJob(STATE, id));
1930
+ if (command === "auto") return auto(args[0]);
1931
+ if (command === "add") return add(args, values);
1932
+ if (command === "serve") return void serve(STATE, count(values.port, "--port"), values.host);
1933
+ if (command === "stop") {
1934
+ const pid = await turnOff(STATE, values.kill);
1935
+ return console.log(pid ? `turned off (pid ${pid})` : "already off");
1936
+ }
1937
+ if (command !== "start" && command !== "run") throw new Error(`unknown command ${command}\n\n${USAGE}`);
1938
+ const options = runOptions(values);
1939
+ if (command === "start") {
1940
+ const pid = turnOn(STATE, process.argv.slice(3));
1941
+ return console.log(`factory: on (pid ${pid}); log: ${logPath(STATE)}`);
1942
+ }
1943
+ const pid = runningPid(STATE);
1944
+ if (pid && pid !== process.pid) throw new Error(`already on (pid ${pid}); factory stop first`);
1945
+ writeFileSync(pidPath(STATE), `${process.pid}\n`);
1946
+ try {
1947
+ const id = await run(options);
1948
+ console.log(report(false, id));
1949
+ } finally {
1950
+ rmSync(pidPath(STATE), { force: true });
1951
+ }
1952
+ }
1953
+ /** The run's options: flags where given, settings.json for the rest. */
1954
+ function runOptions(values) {
1955
+ const settings = readSettings(STATE);
1956
+ const names = values.agents?.split(",") ?? Object.keys(settings.agents);
1957
+ const agents = {};
1958
+ for (const name of names) {
1959
+ agent(name);
1960
+ agents[name] = { ...settings.agents[name] };
1961
+ }
1962
+ for (const field of ["model", "effort"]) for (const pair of values[field] ?? []) {
1963
+ const [name, value] = pair.split("=");
1964
+ if (!agents[name] || !value) throw new Error(`--${field} wants NAME=VALUE for an agent of this run, got ${pair}`);
1965
+ agents[name][field] = value;
1966
+ }
1967
+ const limit = values.limit === void 0 ? settings.limit : values.limit === "none" ? null : count(values.limit, "--limit");
1968
+ const until = values.until ?? settings.until;
1969
+ return {
1970
+ agents,
1971
+ limit,
1972
+ until: until === void 0 ? void 0 : nextTime(until),
1973
+ timeoutMs: count(values.timeout ?? String(settings.timeout), "--timeout") * 6e4,
1974
+ state: STATE,
1975
+ workwork: "workwork"
1976
+ };
1977
+ }
1978
+ async function add(ids, values) {
1979
+ if (ids.length === 0) throw new Error("factory add wants at least one workwork id");
1980
+ const repo = resolve(values.repo ?? ".");
1981
+ const candidates = JSON.parse(await output("workwork", ["--json"], repo));
1982
+ const items = ids.map((id) => {
1983
+ const item = candidates.find((candidate) => candidate.id === id);
1984
+ if (!item) throw new Error(`workwork has no candidate ${id} in ${repo}`);
1985
+ return item;
1986
+ });
1987
+ if (values.agent) agent(values.agent);
1988
+ const pin = {
1989
+ agent: values.agent,
1990
+ model: values.model?.[0],
1991
+ effort: values.effort?.[0]
1992
+ };
1993
+ for (const job of addJobs(STATE, items, "pick", pin)) console.log(`queued ${job.id}: ${job.item.label} ${job.item.id}`);
1994
+ }
1995
+ function auto(onOrOff) {
1996
+ if (onOrOff !== "on" && onOrOff !== "off") throw new Error("factory auto wants on or off");
1997
+ const settings = readSettings(STATE);
1998
+ writeSettings(STATE, {
1999
+ ...settings,
2000
+ auto: {
2001
+ ...settings.auto,
2002
+ on: onOrOff === "on"
2003
+ }
2004
+ });
2005
+ console.log(`auto mode ${onOrOff}`);
2006
+ }
2007
+ function status() {
2008
+ const pid = runningPid(STATE);
2009
+ const path = join(STATE, "status.json");
2010
+ const settings = readSettings(STATE);
2011
+ console.log(`${pid ? `on (pid ${pid})` : "off"}, auto mode ${settings.auto.on ? "on" : "off"}`);
2012
+ if (pid && existsSync(path)) {
2013
+ const status = JSON.parse(readFileSync(path, "utf8"));
2014
+ const until = status.until ? `, until ${clock(status.until)}` : "";
2015
+ console.log(`run ${status.run}, limit ${status.limit ?? "none"}${until}`);
2016
+ for (const worker of status.workers) {
2017
+ const what = worker.job ?? (worker.until ? `until ${clock(worker.until)}` : worker.reason ?? "");
2018
+ console.log(` ${worker.agent.padEnd(7)} ${worker.state.padEnd(9)} ${what} (since ${clock(worker.since)}, limits hit ${worker.limits})`);
2019
+ }
2020
+ }
2021
+ const queued = readJobs(STATE).filter((job) => job.state === "queued");
2022
+ console.log(`queue: ${queued.length}`);
2023
+ for (const job of queued.slice(0, 10)) console.log(` ${line(job)}`);
2024
+ if (queued.length > 10) console.log(` … ${queued.length - 10} more`);
2025
+ }
2026
+ function line(job) {
2027
+ const agent = job.agent ? ` [${job.agent}]` : "";
2028
+ return `${job.id} ${job.source.padEnd(4)} ${basename(job.item.repo)} ${job.item.id}${agent}`;
2029
+ }
2030
+ function report(all, run) {
2031
+ const attempts = readJobs(STATE).flatMap((job) => job.attempts.map((attempt) => ({
2032
+ job,
2033
+ attempt
2034
+ }))).sort((a, b) => a.attempt.start.localeCompare(b.attempt.start));
2035
+ const last = run ?? attempts.at(-1)?.attempt.run;
2036
+ const shown = all ? attempts : attempts.filter(({ attempt }) => attempt.run === last);
2037
+ if (shown.length === 0) return "nothing done yet";
2038
+ const lines = [];
2039
+ for (const [i, { job, attempt }] of shown.entries()) {
2040
+ if (attempt.run !== shown[i - 1]?.attempt.run) lines.push(`run ${attempt.run}`);
2041
+ lines.push(` ${attempt.outcome.padEnd(7)} ${attempt.agent.padEnd(6)} ${line(job)}`);
2042
+ const details = [attempt.url ?? attempt.log];
2043
+ if (attempt.error) details.push(attempt.error.split("\n")[0]);
2044
+ if (attempt.branch) details.push(`unpushed work kept on branch ${attempt.branch}`);
2045
+ lines.push(...details.map((detail) => ` ${detail}`));
2046
+ }
2047
+ return lines.join("\n");
2048
+ }
2049
+ /** The next time the local clock shows HH:MM. */
2050
+ function nextTime(text) {
2051
+ const match = /^(\d{1,2}):(\d{2})$/.exec(text);
2052
+ if (!match) throw new Error(`--until wants HH:MM, got ${text}`);
2053
+ const time = /* @__PURE__ */ new Date();
2054
+ time.setHours(Number(match[1]), Number(match[2]), 0, 0);
2055
+ if (time.getTime() <= Date.now()) time.setDate(time.getDate() + 1);
2056
+ return time.getTime();
2057
+ }
2058
+ function count(text, flag) {
2059
+ const value = Number(text);
2060
+ if (!Number.isInteger(value) || value < 1) throw new Error(`${flag} wants a whole number above 0, got ${text}`);
2061
+ return value;
2062
+ }
2063
+ function clock(iso) {
2064
+ return new Date(iso).toLocaleTimeString("en-GB", {
2065
+ hour: "2-digit",
2066
+ minute: "2-digit"
2067
+ });
2068
+ }
2069
+ main().catch((error) => {
2070
+ console.error(`factory: ${error.message}`);
2071
+ process.exitCode = 1;
2072
+ });
2073
+ //#endregion
2074
+ export {};