@soleone/factory 0.1.0 → 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/README.md +65 -27
- package/dist/cli.mjs +1765 -341
- package/dist/web/assets/geist-cyrillic-ext-wght-normal-DjL33-gN.woff2 +0 -0
- package/dist/web/assets/geist-cyrillic-wght-normal-BEAKL7Jp.woff2 +0 -0
- package/dist/web/assets/geist-latin-ext-wght-normal-DC-KSUi6.woff2 +0 -0
- package/dist/web/assets/geist-latin-wght-normal-BgDaEnEv.woff2 +0 -0
- package/dist/web/assets/geist-vietnamese-wght-normal-6IgcOCM7.woff2 +0 -0
- package/dist/web/assets/index-CJEcQ_Xz.js +79 -0
- package/dist/web/assets/index-CP8jYtPj.css +2 -0
- package/dist/web/favicon.svg +7 -0
- package/dist/web/index.html +44 -0
- package/package.json +24 -2
package/dist/cli.mjs
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { basename, join } from "node:path";
|
|
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";
|
|
5
4
|
import { parseArgs, promisify } from "node:util";
|
|
6
|
-
import {
|
|
7
|
-
|
|
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
|
|
8
13
|
const LIMITS = {
|
|
9
14
|
claude: /usage limit|You've hit your [\w' ]*limit|You've reached your [\w ]*limit|rate_limit_error/i,
|
|
10
15
|
pi: /usage limit|usage_limit_reached|usage_not_included|rate_limit_exceeded/i
|
|
11
16
|
};
|
|
12
17
|
const AGENT_NAMES = Object.keys(LIMITS);
|
|
13
|
-
function agent(name, model) {
|
|
18
|
+
function agent(name, { model, effort } = {}) {
|
|
14
19
|
const pick = model ? ["--model", model] : [];
|
|
15
20
|
if (name === "claude") return {
|
|
16
21
|
name,
|
|
@@ -21,18 +26,73 @@ function agent(name, model) {
|
|
|
21
26
|
"bypassPermissions",
|
|
22
27
|
"--output-format",
|
|
23
28
|
"json",
|
|
24
|
-
...pick
|
|
29
|
+
...pick,
|
|
30
|
+
...effort ? ["--effort", effort] : []
|
|
25
31
|
],
|
|
26
32
|
limit: LIMITS.claude
|
|
27
33
|
};
|
|
28
34
|
if (name === "pi") return {
|
|
29
35
|
name,
|
|
30
36
|
command: "pi",
|
|
31
|
-
args: [
|
|
37
|
+
args: [
|
|
38
|
+
"-p",
|
|
39
|
+
...pick,
|
|
40
|
+
...effort ? ["--thinking", effort] : []
|
|
41
|
+
],
|
|
32
42
|
limit: LIMITS.pi
|
|
33
43
|
};
|
|
34
44
|
throw new Error(`unknown agent ${name} (known: ${AGENT_NAMES.join(", ")})`);
|
|
35
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
|
+
}
|
|
36
96
|
/** Only the end of the log decides: the agent's own work earlier on may mention limits too. */
|
|
37
97
|
function classify(agent, code, log) {
|
|
38
98
|
const tail = log.slice(-8e3);
|
|
@@ -116,38 +176,294 @@ function wallClock(time, zone) {
|
|
|
116
176
|
};
|
|
117
177
|
}
|
|
118
178
|
//#endregion
|
|
119
|
-
//#region src/state.ts
|
|
179
|
+
//#region src/backend/state.ts
|
|
120
180
|
const STATE = process.env.DATA ? join(process.env.DATA, "apps", "factory") : join(homedir(), ".local", "share", "factory");
|
|
121
|
-
const
|
|
122
|
-
function
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
return
|
|
126
|
-
}
|
|
127
|
-
function
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
for (const entry of ledger) {
|
|
135
|
-
if (entry.outcome === "done") finished.add(key(entry));
|
|
136
|
-
if (entry.outcome !== "failed" && entry.outcome !== "timeout") continue;
|
|
137
|
-
const agents = [...failedBy.get(key(entry)) ?? [], entry.agent];
|
|
138
|
-
failedBy.set(key(entry), agents);
|
|
139
|
-
if (agents.length >= 2) finished.add(key(entry));
|
|
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;
|
|
140
194
|
}
|
|
141
|
-
return {
|
|
142
|
-
finished,
|
|
143
|
-
failedBy
|
|
144
|
-
};
|
|
145
195
|
}
|
|
146
196
|
const LEVELS = [
|
|
147
197
|
"auto",
|
|
148
198
|
"skim",
|
|
149
199
|
"decide"
|
|
150
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
|
+
}
|
|
151
467
|
/** Cheapest attention first, and within a level one item per repo in turn so no repo takes the night. */
|
|
152
468
|
function order(items, levels) {
|
|
153
469
|
return LEVELS.filter((level) => levels.includes(level)).flatMap((level) => {
|
|
@@ -159,65 +475,239 @@ function order(items, levels) {
|
|
|
159
475
|
return turns;
|
|
160
476
|
});
|
|
161
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
|
+
}
|
|
162
648
|
//#endregion
|
|
163
|
-
//#region src/loop.ts
|
|
649
|
+
//#region src/backend/loop.ts
|
|
164
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`);
|
|
165
655
|
const URL$1 = /https:\/\/github\.com\/[\w.-]+\/[\w.-]+\/(?:pull|issues)\/\d+/g;
|
|
166
|
-
/**
|
|
656
|
+
/** Works the queue until every agent is out of limits, until passes, or it's stopped; returns the run id. */
|
|
167
657
|
async function run(options) {
|
|
658
|
+
const { state } = options;
|
|
168
659
|
const id = (/* @__PURE__ */ new Date()).toISOString().slice(0, 19).replaceAll(":", "");
|
|
169
|
-
const runDir = join(
|
|
660
|
+
const runDir = join(state, "runs", id);
|
|
170
661
|
mkdirSync(runDir, { recursive: true });
|
|
171
|
-
|
|
172
|
-
const
|
|
173
|
-
const children = /* @__PURE__ */ new Set();
|
|
662
|
+
mkdirSync(join(state, "running"), { recursive: true });
|
|
663
|
+
const agents = /* @__PURE__ */ new Set();
|
|
174
664
|
const wakers = /* @__PURE__ */ new Set();
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
limited: 0,
|
|
179
|
-
timeout: 0
|
|
180
|
-
};
|
|
181
|
-
const workers = options.agents.map((agent) => ({
|
|
182
|
-
agent: agent.name,
|
|
665
|
+
const workers = Object.entries(options.agents).map(([agent, tuning]) => ({
|
|
666
|
+
agent,
|
|
667
|
+
...tuning,
|
|
183
668
|
state: "idle",
|
|
184
669
|
since: (/* @__PURE__ */ new Date()).toISOString(),
|
|
185
670
|
limits: 0
|
|
186
671
|
}));
|
|
187
|
-
let started = 0;
|
|
188
672
|
let stopping = false;
|
|
673
|
+
let killing = false;
|
|
674
|
+
let topping;
|
|
189
675
|
function writeStatus() {
|
|
190
|
-
const until = options.until
|
|
676
|
+
const until = options.until ? new Date(options.until).toISOString() : void 0;
|
|
191
677
|
const status = {
|
|
192
678
|
pid: process.pid,
|
|
193
679
|
run: id,
|
|
194
680
|
until,
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
queued: queue.length
|
|
681
|
+
limit: options.limit,
|
|
682
|
+
workers
|
|
198
683
|
};
|
|
199
|
-
writeFileSync(join(
|
|
684
|
+
writeFileSync(join(state, "status.json"), `${JSON.stringify(status, null, 2)}\n`);
|
|
200
685
|
}
|
|
201
686
|
function set(worker, change) {
|
|
202
687
|
Object.assign(worker, {
|
|
203
|
-
|
|
204
|
-
until: void 0
|
|
688
|
+
job: void 0,
|
|
689
|
+
until: void 0,
|
|
690
|
+
reason: void 0
|
|
205
691
|
}, change);
|
|
206
692
|
worker.since = (/* @__PURE__ */ new Date()).toISOString();
|
|
207
693
|
writeStatus();
|
|
208
694
|
}
|
|
209
|
-
|
|
695
|
+
/** Starts nothing new; agents keep working for the next run to finish, unless killed. */
|
|
696
|
+
function stop(kill) {
|
|
210
697
|
stopping = true;
|
|
211
|
-
|
|
698
|
+
killing ||= kill;
|
|
699
|
+
if (kill) for (const pid of agents) killGroup(pid);
|
|
212
700
|
for (const wake of wakers) wake();
|
|
213
701
|
}
|
|
702
|
+
const handOff = () => stop(false);
|
|
703
|
+
const kill = () => stop(true);
|
|
704
|
+
const aborted = () => stop(options.signal?.reason === "kill");
|
|
214
705
|
function ended() {
|
|
215
|
-
if (stopping) return "
|
|
216
|
-
if (options.until && Date.now() >= options.until) return "
|
|
217
|
-
if (options.maxItems && started >= options.maxItems) return "--max-items reached";
|
|
706
|
+
if (stopping) return "turned off";
|
|
707
|
+
if (options.until && Date.now() >= options.until) return "until passed";
|
|
218
708
|
}
|
|
709
|
+
/** A wait that stopping cuts short. */
|
|
219
710
|
function nap(ms) {
|
|
220
|
-
if (options.sleep) return options.sleep(ms);
|
|
221
711
|
return new Promise((resolve) => {
|
|
222
712
|
const wake = () => {
|
|
223
713
|
clearTimeout(timer);
|
|
@@ -228,205 +718,301 @@ async function run(options) {
|
|
|
228
718
|
wakers.add(wake);
|
|
229
719
|
});
|
|
230
720
|
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
return index < 0 ? void 0 : queue.splice(index, 1)[0];
|
|
235
|
-
}
|
|
236
|
-
async function refill() {
|
|
237
|
-
const candidates = JSON.parse(await output(options.workwork, ["--all", "--json"], process.cwd()));
|
|
238
|
-
const { finished, failedBy } = replay(readLedger(options.state));
|
|
239
|
-
const agents = workers.filter((worker) => worker.state !== "stopped").map((worker) => worker.agent);
|
|
240
|
-
const known = /* @__PURE__ */ new Set([...queue.map(key), ...claimed]);
|
|
241
|
-
const fresh = candidates.filter((item) => !finished.has(key(item)) && !known.has(key(item)) && agents.some((agent) => !failedBy.get(key(item))?.includes(agent)));
|
|
242
|
-
queue.push(...order(fresh, options.levels));
|
|
243
|
-
writeStatus();
|
|
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);
|
|
244
724
|
}
|
|
245
|
-
async function
|
|
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) {
|
|
246
738
|
let quickFailures = 0;
|
|
247
739
|
while (!ended()) {
|
|
248
|
-
|
|
249
|
-
|
|
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, {
|
|
250
777
|
state: "stopped",
|
|
251
|
-
reason: "
|
|
252
|
-
});
|
|
253
|
-
started++;
|
|
254
|
-
claimed.add(key(item));
|
|
255
|
-
set(worker, {
|
|
256
|
-
state: "working",
|
|
257
|
-
item: `${item.label} ${item.id}`
|
|
778
|
+
reason: `3 failures in a row under a minute; last: ${attempt.error?.split("\n")[0] ?? "see its log"}`
|
|
258
779
|
});
|
|
259
|
-
|
|
260
|
-
const entry = await attempt(agent, item);
|
|
261
|
-
claimed.delete(key(item));
|
|
262
|
-
if (!entry) break;
|
|
263
|
-
appendLedger(options.state, entry);
|
|
264
|
-
counts[entry.outcome]++;
|
|
265
|
-
log(`${agent.name} ${entry.outcome} ${item.label} ${item.id} ${entry.url ?? entry.error ?? ""}`);
|
|
266
|
-
const seconds = (Date.parse(entry.end) - Date.parse(entry.start)) / 1e3;
|
|
267
|
-
quickFailures = entry.outcome === "failed" && !entry.error && seconds < 60 ? quickFailures + 1 : 0;
|
|
268
|
-
if (entry.outcome === "limited") {
|
|
269
|
-
started--;
|
|
270
|
-
queue.unshift(item);
|
|
271
|
-
}
|
|
272
|
-
if (entry.outcome !== "limited" && quickFailures < 3) {
|
|
780
|
+
if (attempt.outcome !== "limited") {
|
|
273
781
|
set(worker, { state: "idle" });
|
|
274
782
|
continue;
|
|
275
783
|
}
|
|
276
|
-
quickFailures = 0;
|
|
277
784
|
worker.limits++;
|
|
278
|
-
if (worker.limits >= options.
|
|
785
|
+
if (options.limit !== null && worker.limits >= options.limit) return set(worker, {
|
|
279
786
|
state: "stopped",
|
|
280
|
-
reason: `limit ${worker.limits} of ${options.
|
|
787
|
+
reason: `limit ${worker.limits} of ${options.limit}`
|
|
281
788
|
});
|
|
282
|
-
const reset =
|
|
789
|
+
const reset = resetTime(readFileSync(attempt.log, "utf8"), Date.now());
|
|
283
790
|
const wake = Math.min(reset === void 0 ? Date.now() + 30 * MINUTE : reset + 2 * MINUTE, options.until ?? Infinity);
|
|
284
791
|
set(worker, {
|
|
285
792
|
state: "sleeping",
|
|
286
793
|
until: new Date(wake).toISOString()
|
|
287
794
|
});
|
|
288
|
-
log(`${agent.
|
|
289
|
-
await nap(wake - Date.now());
|
|
795
|
+
log(`${worker.agent} hit limit ${worker.limits}, sleeps until ${new Date(wake).toISOString()}`);
|
|
796
|
+
await (options.sleep ?? nap)(wake - Date.now());
|
|
290
797
|
}
|
|
291
798
|
set(worker, {
|
|
292
799
|
state: "stopped",
|
|
293
800
|
reason: ended()
|
|
294
801
|
});
|
|
295
802
|
}
|
|
296
|
-
/** One attempt
|
|
297
|
-
async function
|
|
298
|
-
const
|
|
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}`;
|
|
299
811
|
const dir = join(runDir, slug);
|
|
300
812
|
mkdirSync(dir, { recursive: true });
|
|
301
813
|
const logPath = join(dir, "agent.log");
|
|
302
|
-
const
|
|
303
|
-
const
|
|
304
|
-
const
|
|
814
|
+
const repoKey = createHash("sha256").update(job.item.repo).digest("hex");
|
|
815
|
+
const worktree = join(state, "worktrees", agent$1.name, repoKey);
|
|
816
|
+
const begun = {
|
|
305
817
|
run: id,
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
start,
|
|
312
|
-
end: (/* @__PURE__ */ new Date()).toISOString(),
|
|
313
|
-
outcome,
|
|
314
|
-
log: logPath,
|
|
315
|
-
...rest
|
|
316
|
-
});
|
|
818
|
+
agent: agent$1.name,
|
|
819
|
+
...tuning,
|
|
820
|
+
start: (/* @__PURE__ */ new Date()).toISOString(),
|
|
821
|
+
log: logPath
|
|
822
|
+
};
|
|
317
823
|
let prompt;
|
|
318
824
|
try {
|
|
319
|
-
await
|
|
320
|
-
|
|
321
|
-
"--quiet",
|
|
322
|
-
"origin"
|
|
323
|
-
], item.repo);
|
|
324
|
-
await output("git", [
|
|
325
|
-
"worktree",
|
|
326
|
-
"add",
|
|
327
|
-
"--detach",
|
|
328
|
-
worktree,
|
|
329
|
-
(await output("git", [
|
|
330
|
-
"symbolic-ref",
|
|
331
|
-
"--short",
|
|
332
|
-
"refs/remotes/origin/HEAD"
|
|
333
|
-
], item.repo)).trim()
|
|
334
|
-
], item.repo);
|
|
335
|
-
prompt = await output(options.workwork, ["prompt", item.id], item.repo);
|
|
825
|
+
await checkout(worktree, job.item.repo);
|
|
826
|
+
prompt = job.prompt ?? await output(options.workwork, ["prompt", job.item.id], job.item.repo);
|
|
336
827
|
} catch (error) {
|
|
337
|
-
const
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
worktree: kept ? worktree : void 0
|
|
341
|
-
});
|
|
828
|
+
const message = error.message.trim();
|
|
829
|
+
writeFileSync(logPath, `${message}\n`);
|
|
830
|
+
return attempt(begun, "failed", { error: message.slice(0, 2e3) });
|
|
342
831
|
}
|
|
343
832
|
writeFileSync(join(dir, "prompt.md"), prompt);
|
|
344
|
-
const { code, timedOut } = await spawnAgent(agent, prompt, worktree, logPath);
|
|
345
|
-
const kept = await settle(worktree, item.repo);
|
|
346
833
|
if (stopping) return void 0;
|
|
347
|
-
const
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
worktree
|
|
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
|
|
351
871
|
});
|
|
352
872
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
const timer = setTimeout(() => {
|
|
369
|
-
timedOut = true;
|
|
370
|
-
killGroup(child);
|
|
371
|
-
}, options.timeoutMs);
|
|
372
|
-
const done = (code) => {
|
|
373
|
-
clearTimeout(timer);
|
|
374
|
-
children.delete(child);
|
|
375
|
-
resolve({
|
|
376
|
-
code,
|
|
377
|
-
timedOut
|
|
378
|
-
});
|
|
379
|
-
};
|
|
380
|
-
child.on("error", (error) => {
|
|
381
|
-
appendFileSync(logPath, `factory: could not start ${agent.command}: ${error.message}\n`);
|
|
382
|
-
done(127);
|
|
383
|
-
});
|
|
384
|
-
child.on("close", (code) => {
|
|
385
|
-
killGroup(child);
|
|
386
|
-
done(code ?? 1);
|
|
387
|
-
});
|
|
388
|
-
child.stdin.on("error", () => {});
|
|
389
|
-
child.stdin.end(prompt);
|
|
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
|
|
390
888
|
});
|
|
391
889
|
}
|
|
392
|
-
process.on("SIGTERM",
|
|
393
|
-
process.on("SIGINT",
|
|
890
|
+
process.on("SIGTERM", handOff);
|
|
891
|
+
process.on("SIGINT", kill);
|
|
892
|
+
options.signal?.addEventListener("abort", aborted);
|
|
394
893
|
writeStatus();
|
|
395
894
|
try {
|
|
396
|
-
await Promise.all(
|
|
895
|
+
await Promise.all(workers.map((worker) => work(worker, handed.get(worker.agent))));
|
|
397
896
|
} catch (error) {
|
|
398
|
-
|
|
897
|
+
kill();
|
|
399
898
|
throw error;
|
|
400
899
|
} finally {
|
|
401
|
-
process.off("SIGTERM",
|
|
402
|
-
process.off("SIGINT",
|
|
900
|
+
process.off("SIGTERM", handOff);
|
|
901
|
+
process.off("SIGINT", kill);
|
|
902
|
+
options.signal?.removeEventListener("abort", aborted);
|
|
903
|
+
await topping;
|
|
403
904
|
writeStatus();
|
|
404
905
|
}
|
|
405
906
|
return id;
|
|
406
907
|
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
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", [
|
|
411
1001
|
"log",
|
|
412
1002
|
"--oneline",
|
|
413
1003
|
"HEAD",
|
|
414
1004
|
"--not",
|
|
415
|
-
|
|
416
|
-
], worktree);
|
|
417
|
-
const changes = await output("git", ["status", "--porcelain"], worktree);
|
|
418
|
-
if (unpushed.trim() || changes.trim()) return true;
|
|
1005
|
+
...saved
|
|
1006
|
+
], worktree)).trim()) return void 0;
|
|
419
1007
|
await output("git", [
|
|
420
|
-
"
|
|
421
|
-
"remove",
|
|
1008
|
+
"branch",
|
|
422
1009
|
"--force",
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
1010
|
+
branch,
|
|
1011
|
+
"HEAD"
|
|
1012
|
+
], worktree);
|
|
1013
|
+
return branch;
|
|
426
1014
|
}
|
|
427
|
-
function killGroup(
|
|
428
|
-
const pid = child.pid;
|
|
429
|
-
if (pid === void 0) return;
|
|
1015
|
+
function killGroup(pid) {
|
|
430
1016
|
for (const [signal, delay] of [["SIGTERM", 0], ["SIGKILL", 1e4]]) setTimeout(() => {
|
|
431
1017
|
try {
|
|
432
1018
|
process.kill(-pid, signal);
|
|
@@ -435,64 +1021,891 @@ function killGroup(child) {
|
|
|
435
1021
|
}
|
|
436
1022
|
}, delay).unref();
|
|
437
1023
|
}
|
|
438
|
-
async function output(command, args, cwd) {
|
|
439
|
-
const { stdout, stderr } = await promisify(execFile)(command, args, {
|
|
440
|
-
cwd,
|
|
441
|
-
maxBuffer: 67108864
|
|
442
|
-
});
|
|
443
|
-
process.stderr.write(stderr);
|
|
444
|
-
return stdout;
|
|
445
|
-
}
|
|
446
1024
|
function log(message) {
|
|
447
1025
|
console.log(`${(/* @__PURE__ */ new Date()).toISOString()} ${message}`);
|
|
448
1026
|
}
|
|
449
1027
|
//#endregion
|
|
450
|
-
//#region src/
|
|
451
|
-
const
|
|
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.
|
|
452
1399
|
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
factory report [--all] the last run (or every run): outcome, agent, item, PR/issue link or log
|
|
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.
|
|
458
1404
|
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
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
|
|
467
1879
|
|
|
468
1880
|
State: ${STATE}`;
|
|
469
|
-
const PID = join(STATE, "factory.pid");
|
|
470
|
-
const LOG = join(STATE, "factory.log");
|
|
471
1881
|
async function main() {
|
|
472
1882
|
const { values, positionals } = parseArgs({
|
|
473
1883
|
allowPositionals: true,
|
|
474
1884
|
options: {
|
|
1885
|
+
agents: { type: "string" },
|
|
1886
|
+
limit: { type: "string" },
|
|
475
1887
|
until: { type: "string" },
|
|
476
|
-
|
|
1888
|
+
model: {
|
|
477
1889
|
type: "string",
|
|
478
|
-
|
|
1890
|
+
multiple: true
|
|
479
1891
|
},
|
|
480
|
-
|
|
1892
|
+
effort: {
|
|
481
1893
|
type: "string",
|
|
482
|
-
|
|
1894
|
+
multiple: true
|
|
483
1895
|
},
|
|
484
|
-
|
|
1896
|
+
timeout: { type: "string" },
|
|
1897
|
+
repo: { type: "string" },
|
|
1898
|
+
agent: { type: "string" },
|
|
1899
|
+
all: { type: "boolean" },
|
|
1900
|
+
kill: { type: "boolean" },
|
|
1901
|
+
port: {
|
|
485
1902
|
type: "string",
|
|
486
|
-
default: "
|
|
1903
|
+
default: "7040"
|
|
487
1904
|
},
|
|
488
|
-
|
|
489
|
-
timeout: {
|
|
1905
|
+
host: {
|
|
490
1906
|
type: "string",
|
|
491
|
-
default: "
|
|
1907
|
+
default: "127.0.0.1"
|
|
492
1908
|
},
|
|
493
|
-
"claude-model": { type: "string" },
|
|
494
|
-
"pi-model": { type: "string" },
|
|
495
|
-
all: { type: "boolean" },
|
|
496
1909
|
help: {
|
|
497
1910
|
type: "boolean",
|
|
498
1911
|
short: "h"
|
|
@@ -503,122 +1916,136 @@ async function main() {
|
|
|
503
1916
|
}
|
|
504
1917
|
}
|
|
505
1918
|
});
|
|
506
|
-
const [command] = positionals;
|
|
1919
|
+
const [command, ...args] = positionals;
|
|
507
1920
|
if (values.help || command === void 0) return console.log(USAGE);
|
|
508
|
-
if (values.version) return console.log(version
|
|
1921
|
+
if (values.version) return console.log(version);
|
|
509
1922
|
mkdirSync(STATE, { recursive: true });
|
|
510
1923
|
if (command === "status") return status();
|
|
511
|
-
if (command === "stop") return stop();
|
|
512
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
|
+
}
|
|
513
1937
|
if (command !== "start" && command !== "run") throw new Error(`unknown command ${command}\n\n${USAGE}`);
|
|
514
|
-
const
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
"decide"
|
|
523
|
-
].includes(level)) throw new Error(`unknown level ${level}`);
|
|
524
|
-
const options = {
|
|
525
|
-
agents: values.agents.split(",").map((name) => agent(name, models[name])),
|
|
526
|
-
levels,
|
|
527
|
-
windows: count(values.windows, "--windows"),
|
|
528
|
-
until: values.until === void 0 ? void 0 : nextTime(values.until),
|
|
529
|
-
maxItems: values["max-items"] === void 0 ? void 0 : count(values["max-items"], "--max-items"),
|
|
530
|
-
timeoutMs: count(values.timeout, "--timeout") * 6e4,
|
|
531
|
-
state: STATE,
|
|
532
|
-
workwork: "workwork"
|
|
533
|
-
};
|
|
534
|
-
const pid = runningPid();
|
|
535
|
-
if (pid && pid !== process.pid) throw new Error(`already running (pid ${pid}); factory status, or factory stop`);
|
|
536
|
-
if (command === "start") return start();
|
|
537
|
-
writeFileSync(PID, `${process.pid}\n`);
|
|
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`);
|
|
538
1946
|
try {
|
|
539
1947
|
const id = await run(options);
|
|
540
1948
|
console.log(report(false, id));
|
|
541
1949
|
} finally {
|
|
542
|
-
rmSync(
|
|
1950
|
+
rmSync(pidPath(STATE), { force: true });
|
|
543
1951
|
}
|
|
544
1952
|
}
|
|
545
|
-
/**
|
|
546
|
-
function
|
|
547
|
-
const
|
|
548
|
-
const
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
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;
|
|
559
1986
|
});
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
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}`);
|
|
563
2006
|
}
|
|
564
2007
|
function status() {
|
|
565
|
-
const pid = runningPid();
|
|
2008
|
+
const pid = runningPid(STATE);
|
|
566
2009
|
const path = join(STATE, "status.json");
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
}
|
|
578
|
-
async function stop() {
|
|
579
|
-
const pid = runningPid();
|
|
580
|
-
if (!pid) return console.log("not running");
|
|
581
|
-
process.kill(pid, "SIGTERM");
|
|
582
|
-
for (let waited = 0; alive(pid); waited++) {
|
|
583
|
-
if (waited === 60) throw new Error(`pid ${pid} is still running after 30 seconds`);
|
|
584
|
-
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
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
|
+
}
|
|
585
2020
|
}
|
|
586
|
-
|
|
587
|
-
console.log(`
|
|
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}`;
|
|
588
2029
|
}
|
|
589
2030
|
function report(all, run) {
|
|
590
|
-
const
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
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";
|
|
594
2038
|
const lines = [];
|
|
595
|
-
for (const [i,
|
|
596
|
-
if (
|
|
597
|
-
lines.push(` ${
|
|
598
|
-
|
|
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}`));
|
|
599
2046
|
}
|
|
600
2047
|
return lines.join("\n");
|
|
601
2048
|
}
|
|
602
|
-
function detail(entry) {
|
|
603
|
-
const parts = [entry.url ?? entry.log];
|
|
604
|
-
if (entry.error) parts.push(entry.error.split("\n")[0]);
|
|
605
|
-
if (entry.worktree) parts.push(`unpushed work kept in ${entry.worktree}`);
|
|
606
|
-
return parts.join("\n ");
|
|
607
|
-
}
|
|
608
|
-
function runningPid() {
|
|
609
|
-
if (!existsSync(PID)) return void 0;
|
|
610
|
-
const pid = Number(readFileSync(PID, "utf8"));
|
|
611
|
-
return alive(pid) ? pid : void 0;
|
|
612
|
-
}
|
|
613
|
-
function alive(pid) {
|
|
614
|
-
try {
|
|
615
|
-
process.kill(pid, 0);
|
|
616
|
-
return true;
|
|
617
|
-
} catch (error) {
|
|
618
|
-
if (error.code === "ESRCH") return false;
|
|
619
|
-
throw error;
|
|
620
|
-
}
|
|
621
|
-
}
|
|
622
2049
|
/** The next time the local clock shows HH:MM. */
|
|
623
2050
|
function nextTime(text) {
|
|
624
2051
|
const match = /^(\d{1,2}):(\d{2})$/.exec(text);
|
|
@@ -639,9 +2066,6 @@ function clock(iso) {
|
|
|
639
2066
|
minute: "2-digit"
|
|
640
2067
|
});
|
|
641
2068
|
}
|
|
642
|
-
function version() {
|
|
643
|
-
return JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
|
|
644
|
-
}
|
|
645
2069
|
main().catch((error) => {
|
|
646
2070
|
console.error(`factory: ${error.message}`);
|
|
647
2071
|
process.exitCode = 1;
|