@soleone/factory 0.2.0 → 0.2.2
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 +11 -4
- package/dist/cli.mjs +509 -242
- package/dist/web/assets/index-1ZYZbwXO.js +80 -0
- package/dist/web/assets/index-BYLnECQ_.css +2 -0
- package/dist/web/index.html +3 -3
- package/package.json +1 -1
- package/dist/web/assets/index-CJEcQ_Xz.js +0 -79
- package/dist/web/assets/index-CP8jYtPj.css +0 -2
package/dist/cli.mjs
CHANGED
|
@@ -3,9 +3,9 @@ import { appendFileSync, closeSync, existsSync, mkdirSync, openSync, readFileSyn
|
|
|
3
3
|
import { basename, dirname, extname, join, relative, resolve, sep } from "node:path";
|
|
4
4
|
import { parseArgs, promisify } from "node:util";
|
|
5
5
|
import { createHash, randomBytes, randomUUID } from "node:crypto";
|
|
6
|
-
import { execFile, spawn } from "node:child_process";
|
|
7
|
-
import { homedir, tmpdir } from "node:os";
|
|
8
6
|
import { mkdtemp, rm } from "node:fs/promises";
|
|
7
|
+
import { homedir, tmpdir } from "node:os";
|
|
8
|
+
import { execFile, spawn } from "node:child_process";
|
|
9
9
|
import { createServer } from "node:http";
|
|
10
10
|
import { fileURLToPath } from "node:url";
|
|
11
11
|
import { createInterface } from "node:readline";
|
|
@@ -242,7 +242,6 @@ function checkSettings(raw) {
|
|
|
242
242
|
for (const level of settings.auto.levels) if (!LEVELS.includes(level)) throw new Error(`unknown level ${level}`);
|
|
243
243
|
return settings;
|
|
244
244
|
}
|
|
245
|
-
const itemKey = (item) => `${item.repo} ${item.id}`;
|
|
246
245
|
const candidatesPath = (state) => join(state, "candidates.json");
|
|
247
246
|
const refreshLock = (state) => join(state, "candidates.lock");
|
|
248
247
|
function readCandidates(state) {
|
|
@@ -284,6 +283,183 @@ async function output(command, args, cwd) {
|
|
|
284
283
|
return stdout;
|
|
285
284
|
}
|
|
286
285
|
//#endregion
|
|
286
|
+
//#region src/backend/review.ts
|
|
287
|
+
const MINUTE$1 = 6e4;
|
|
288
|
+
/** The branch workwork has the agent push a candidate's work to. */
|
|
289
|
+
const workworkBranch = (item) => `workwork/${item.id.replace(/[^\w./@-]+/g, "-")}`;
|
|
290
|
+
/** The PR workwork had the agent open, found by the branch workwork names; scans name theirs per area, so they find none. */
|
|
291
|
+
async function findPr(item) {
|
|
292
|
+
const branch = workworkBranch(item);
|
|
293
|
+
return JSON.parse(await output("gh", [
|
|
294
|
+
"pr",
|
|
295
|
+
"list",
|
|
296
|
+
"--head",
|
|
297
|
+
branch,
|
|
298
|
+
"--state",
|
|
299
|
+
"all",
|
|
300
|
+
"--json",
|
|
301
|
+
"url"
|
|
302
|
+
], item.repo))[0]?.url;
|
|
303
|
+
}
|
|
304
|
+
async function readPr(url) {
|
|
305
|
+
const raw = JSON.parse(await gh([
|
|
306
|
+
"pr",
|
|
307
|
+
"view",
|
|
308
|
+
url,
|
|
309
|
+
"--json",
|
|
310
|
+
"state,isDraft,title,body,additions,deletions,files,mergeable,isCrossRepository,statusCheckRollup"
|
|
311
|
+
]));
|
|
312
|
+
return {
|
|
313
|
+
state: raw.state,
|
|
314
|
+
draft: raw.isDraft,
|
|
315
|
+
title: raw.title,
|
|
316
|
+
level: /^Level: (\w+)\s*$/m.exec(raw.body)?.[1],
|
|
317
|
+
additions: raw.additions,
|
|
318
|
+
deletions: raw.deletions,
|
|
319
|
+
files: raw.files.map((file) => file.path),
|
|
320
|
+
checks: checks(raw.statusCheckRollup),
|
|
321
|
+
conflicts: raw.mergeable === "UNKNOWN" ? null : raw.mergeable === "CONFLICTING",
|
|
322
|
+
fork: raw.isCrossRepository
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
/** Check runs have a conclusion, empty while running; commit statuses have a state. */
|
|
326
|
+
function checks(rollup) {
|
|
327
|
+
if (rollup.length === 0) return "none";
|
|
328
|
+
const results = rollup.map((check) => check.conclusion || check.state || "");
|
|
329
|
+
if (results.some((result) => /FAILURE|ERROR|CANCELLED|TIMED_OUT|ACTION_REQUIRED/.test(result))) return "failing";
|
|
330
|
+
if (results.some((result) => result === "" || result === "PENDING" || result === "EXPECTED")) return "pending";
|
|
331
|
+
return "passing";
|
|
332
|
+
}
|
|
333
|
+
/** What the review popup shows: the whole PR body and its diff, fetched only when opened. */
|
|
334
|
+
async function readPrDetails(url) {
|
|
335
|
+
const [view, diff] = await Promise.all([gh([
|
|
336
|
+
"pr",
|
|
337
|
+
"view",
|
|
338
|
+
url,
|
|
339
|
+
"--json",
|
|
340
|
+
"body"
|
|
341
|
+
]), gh([
|
|
342
|
+
"pr",
|
|
343
|
+
"diff",
|
|
344
|
+
url
|
|
345
|
+
])]);
|
|
346
|
+
return {
|
|
347
|
+
body: JSON.parse(view).body,
|
|
348
|
+
diff
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
const mergePr = (url) => gh([
|
|
352
|
+
"pr",
|
|
353
|
+
"merge",
|
|
354
|
+
url,
|
|
355
|
+
"--squash",
|
|
356
|
+
"--delete-branch"
|
|
357
|
+
]);
|
|
358
|
+
const closePr = (url) => gh([
|
|
359
|
+
"pr",
|
|
360
|
+
"close",
|
|
361
|
+
url,
|
|
362
|
+
"--delete-branch"
|
|
363
|
+
]);
|
|
364
|
+
/** A rebase that stopped on conflicts git can't resolve; nothing was pushed. */
|
|
365
|
+
var Conflicts = class extends Error {
|
|
366
|
+
files;
|
|
367
|
+
constructor(files) {
|
|
368
|
+
super(`Rebase stopped. Resolve these conflicts manually; no changes were pushed:\n${files.join("\n")}`);
|
|
369
|
+
this.files = files;
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
async function rebasePr(url) {
|
|
373
|
+
const pull = /^(https:\/\/github\.com\/[\w.-]+\/[\w.-]+)\/pull\/(\d+)$/.exec(url);
|
|
374
|
+
if (!pull) throw new Error("Rebase requires a github.com pull request URL");
|
|
375
|
+
const [, repo, number] = pull;
|
|
376
|
+
const pr = JSON.parse(await gh([
|
|
377
|
+
"pr",
|
|
378
|
+
"view",
|
|
379
|
+
url,
|
|
380
|
+
"--json",
|
|
381
|
+
"baseRefName,headRefName,isCrossRepository"
|
|
382
|
+
]));
|
|
383
|
+
if (pr.isCrossRepository || pr.baseRefName === pr.headRefName) throw new Error("Rebase requires a separate PR branch in the same repository");
|
|
384
|
+
const dir = await mkdtemp(join(tmpdir(), "factory-rebase-"));
|
|
385
|
+
const git = (...args) => output("git", args, dir);
|
|
386
|
+
const remote = `${repo}.git`;
|
|
387
|
+
const base = "refs/factory/base";
|
|
388
|
+
const head = "refs/factory/head";
|
|
389
|
+
const branch = `refs/heads/${pr.headRefName}`;
|
|
390
|
+
try {
|
|
391
|
+
await git("init", "--quiet");
|
|
392
|
+
await git("config", "--add", "credential.https://github.com.helper", "");
|
|
393
|
+
await git("config", "--add", "credential.https://github.com.helper", "!gh auth git-credential");
|
|
394
|
+
await git("fetch", "--no-tags", remote, `refs/heads/${pr.baseRefName}:${base}`, `refs/pull/${number}/head:${head}`);
|
|
395
|
+
const before = (await git("rev-parse", head)).trim();
|
|
396
|
+
await git("checkout", "--quiet", "--detach", head);
|
|
397
|
+
try {
|
|
398
|
+
await git("rebase", "--rebase-merges", base);
|
|
399
|
+
} catch (error) {
|
|
400
|
+
const conflicts = (await git("diff", "--name-only", "--diff-filter=U")).trim();
|
|
401
|
+
if (!conflicts) throw error;
|
|
402
|
+
throw new Conflicts(conflicts.split("\n"));
|
|
403
|
+
}
|
|
404
|
+
await git("push", `--force-with-lease=${branch}:${before}`, remote, `HEAD:${branch}`);
|
|
405
|
+
} finally {
|
|
406
|
+
await rm(dir, {
|
|
407
|
+
recursive: true,
|
|
408
|
+
force: true
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
/** Run outside any repo, so --delete-branch only touches GitHub, never a local checkout. */
|
|
413
|
+
const gh = (args) => output("gh", args, tmpdir());
|
|
414
|
+
/**
|
|
415
|
+
* PR status for the page. gh takes a second or more per PR, so this answers from what it last
|
|
416
|
+
* fetched and refreshes open PRs in the background. While GitHub calculates mergeability, retry
|
|
417
|
+
* sooner so the page doesn't wait a minute after a rebase.
|
|
418
|
+
*/
|
|
419
|
+
function prCache() {
|
|
420
|
+
const cache = /* @__PURE__ */ new Map();
|
|
421
|
+
function refresh(url) {
|
|
422
|
+
const entry = {
|
|
423
|
+
at: Date.now(),
|
|
424
|
+
pr: cache.get(url)?.pr
|
|
425
|
+
};
|
|
426
|
+
cache.set(url, entry);
|
|
427
|
+
readPr(url).then((pr) => entry.pr = pr, (error) => console.error(`factory: gh pr view ${url} failed: ${error.message}`));
|
|
428
|
+
}
|
|
429
|
+
return {
|
|
430
|
+
get(urls) {
|
|
431
|
+
const prs = {};
|
|
432
|
+
for (const url of urls) {
|
|
433
|
+
const entry = cache.get(url);
|
|
434
|
+
const interval = entry?.pr?.conflicts === null ? 5e3 : MINUTE$1;
|
|
435
|
+
if (!entry || (entry.pr?.state ?? "OPEN") === "OPEN" && Date.now() - entry.at > interval) refresh(url);
|
|
436
|
+
const pr = cache.get(url).pr;
|
|
437
|
+
if (pr) prs[url] = pr;
|
|
438
|
+
}
|
|
439
|
+
return prs;
|
|
440
|
+
},
|
|
441
|
+
/** Asks GitHub now, for an action that must not act on a stale answer. */
|
|
442
|
+
async fresh(url) {
|
|
443
|
+
const pr = await readPr(url);
|
|
444
|
+
cache.set(url, {
|
|
445
|
+
at: Date.now(),
|
|
446
|
+
pr
|
|
447
|
+
});
|
|
448
|
+
return pr;
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
//#endregion
|
|
453
|
+
//#region src/backend/types.ts
|
|
454
|
+
/** A candidate's identity across workwork runs. */
|
|
455
|
+
const itemKey = (item) => `${item.repo} ${item.id}`;
|
|
456
|
+
const onTrain = (car) => car !== void 0 && car.step !== "merged" && car.step !== "skipped";
|
|
457
|
+
/** The PR of the job's latest attempt that linked one. */
|
|
458
|
+
const prUrl = (job) => job.attempts.findLast((attempt) => attempt.url?.includes("/pull/"))?.url;
|
|
459
|
+
/** Merging from the page skips GitHub's own page, so it only goes ahead when nothing argues against it. */
|
|
460
|
+
const canMerge = (pr) => pr.state === "OPEN" && !pr.draft && pr.conflicts === false && (pr.checks === "passing" || pr.checks === "none");
|
|
461
|
+
const canRebase = (pr) => pr.state === "OPEN" && pr.conflicts === true && !pr.fork;
|
|
462
|
+
//#endregion
|
|
287
463
|
//#region src/backend/jobs.ts
|
|
288
464
|
/** An unexpected failure gets one more try by the same agent. */
|
|
289
465
|
const TRIES$1 = 2;
|
|
@@ -306,7 +482,8 @@ function readJobs(state) {
|
|
|
306
482
|
...event.job,
|
|
307
483
|
added: event.at,
|
|
308
484
|
state: "queued",
|
|
309
|
-
attempts: []
|
|
485
|
+
attempts: [],
|
|
486
|
+
notes: []
|
|
310
487
|
};
|
|
311
488
|
jobs.set(job.id, job);
|
|
312
489
|
(job.source === "pick" ? picks : autos).push(job);
|
|
@@ -328,10 +505,21 @@ function readJobs(state) {
|
|
|
328
505
|
job.state = "running";
|
|
329
506
|
job.agent = event.agent;
|
|
330
507
|
}
|
|
508
|
+
if (event.type === "note") {
|
|
509
|
+
const { kind } = event.note;
|
|
510
|
+
const attempt = job.attempts.length + (kind === "answer" || kind === "train" ? 0 : 1);
|
|
511
|
+
job.notes.push({
|
|
512
|
+
...event.note,
|
|
513
|
+
at: event.at,
|
|
514
|
+
attempt
|
|
515
|
+
});
|
|
516
|
+
if (kind === "answer") job.state = "queued";
|
|
517
|
+
}
|
|
331
518
|
if (event.type === "attempt") {
|
|
332
519
|
job.attempts.push(event.attempt);
|
|
333
520
|
const { outcome } = event.attempt;
|
|
334
|
-
if (
|
|
521
|
+
if (job.notes.some((note) => note.kind === "ask" && note.attempt === job.attempts.length)) job.state = "blocked";
|
|
522
|
+
else if (outcome === "done") job.state = "done";
|
|
335
523
|
else if (outcome === "limited" || outcome === "stopped") job.state = "queued";
|
|
336
524
|
else {
|
|
337
525
|
const count = (failures.get(job.id) ?? 0) + 1;
|
|
@@ -340,15 +528,28 @@ function readJobs(state) {
|
|
|
340
528
|
}
|
|
341
529
|
}
|
|
342
530
|
}
|
|
531
|
+
for (const job of jobs.values()) {
|
|
532
|
+
const state = job.after && jobs.get(job.after).state;
|
|
533
|
+
if (job.state === "queued" && (state === "failed" || state === "removed")) job.state = "removed";
|
|
534
|
+
}
|
|
343
535
|
return [...picks, ...autos];
|
|
344
536
|
}
|
|
345
|
-
/** The next job this agent may take. */
|
|
537
|
+
/** The next job this agent may take: queued for it, with the job it comes after done. */
|
|
346
538
|
function nextJob(jobs, agent) {
|
|
347
|
-
|
|
539
|
+
const done = (id) => !id || jobs.find((job) => job.id === id)?.state === "done";
|
|
540
|
+
return jobs.find((job) => job.state === "queued" && (job.agent ?? agent) === agent && done(job.after));
|
|
348
541
|
}
|
|
542
|
+
const ACTIVE = [
|
|
543
|
+
"queued",
|
|
544
|
+
"running",
|
|
545
|
+
"blocked"
|
|
546
|
+
];
|
|
547
|
+
const sameWork = (item) => `${itemKey(item)} ${item.play} ${item.title}`;
|
|
548
|
+
/** Queues the items, skipping those already in the queue; returns the jobs it added. */
|
|
349
549
|
function addJobs(state, items, source, pin = {}) {
|
|
350
550
|
const at = (/* @__PURE__ */ new Date()).toISOString();
|
|
351
|
-
|
|
551
|
+
const active = new Set(readJobs(state).filter((job) => ACTIVE.includes(job.state)).map((job) => sameWork(job.item)));
|
|
552
|
+
return items.filter((item) => !active.has(sameWork(item))).map((item) => {
|
|
352
553
|
const job = {
|
|
353
554
|
id: randomBytes(4).toString("hex"),
|
|
354
555
|
item,
|
|
@@ -364,7 +565,8 @@ function addJobs(state, items, source, pin = {}) {
|
|
|
364
565
|
...job,
|
|
365
566
|
added: at,
|
|
366
567
|
state: "queued",
|
|
367
|
-
attempts: []
|
|
568
|
+
attempts: [],
|
|
569
|
+
notes: []
|
|
368
570
|
};
|
|
369
571
|
});
|
|
370
572
|
}
|
|
@@ -373,9 +575,7 @@ function addJobs(state, items, source, pin = {}) {
|
|
|
373
575
|
* keeps the job's workwork id, so the attempt links the same PR.
|
|
374
576
|
*/
|
|
375
577
|
function queueConflicts(state, id, url, files) {
|
|
376
|
-
const
|
|
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;
|
|
578
|
+
const job = readJobs(state).find((job) => job.id === id);
|
|
379
579
|
addJobs(state, [{
|
|
380
580
|
...job.item,
|
|
381
581
|
play: "conflicts",
|
|
@@ -394,6 +594,56 @@ If a conflict can't be resolved without guessing what someone meant, don't push.
|
|
|
394
594
|
|
|
395
595
|
End your reply with the pull request's URL.
|
|
396
596
|
`;
|
|
597
|
+
/**
|
|
598
|
+
* Queues a review of the job's PR by another agent, once the job is done. Like a conflicts job it
|
|
599
|
+
* keeps the job's workwork id, so it finds and links the same PR.
|
|
600
|
+
*/
|
|
601
|
+
function queueReview(state, id, agent) {
|
|
602
|
+
const job = find(state, id, ...ACTIVE, "done");
|
|
603
|
+
if (job.agent === agent) throw new Error(`job ${id} is ${agent}'s own; pick another agent to review it`);
|
|
604
|
+
const branch = workworkBranch(job.item);
|
|
605
|
+
const item = {
|
|
606
|
+
...job.item,
|
|
607
|
+
play: "review",
|
|
608
|
+
title: `Review: ${job.item.title}`
|
|
609
|
+
};
|
|
610
|
+
const prompt = reviewPrompt(branch, job.agent);
|
|
611
|
+
return addJobs(state, [item], "pick", {
|
|
612
|
+
agent,
|
|
613
|
+
prompt,
|
|
614
|
+
after: id
|
|
615
|
+
})[0];
|
|
616
|
+
}
|
|
617
|
+
const reviewPrompt = (branch, author = "another agent") => `Review the pull request ${author} opened from branch \`${branch}\`: \`gh pr view ${branch}\` and \`gh pr diff ${branch}\`.
|
|
618
|
+
|
|
619
|
+
1. Check it out with \`gh pr checkout ${branch}\`, run the repo's checks and tests, and read the change against what its description says it does.
|
|
620
|
+
2. Look for bugs, missed cases and changes nobody asked for. Skip style the linters already cover.
|
|
621
|
+
3. Post what you found as one comment with \`gh pr comment ${branch} --body-file -\`: each finding with file, line and why it matters, or that you found nothing. Don't push, approve or merge.
|
|
622
|
+
|
|
623
|
+
If there's no such pull request, say so and end.
|
|
624
|
+
|
|
625
|
+
End your reply with the pull request's URL.
|
|
626
|
+
`;
|
|
627
|
+
/**
|
|
628
|
+
* Queues a task an agent reported as a follow-up of its job. Its own id, so the PR of the job it
|
|
629
|
+
* came from isn't taken for its own.
|
|
630
|
+
*/
|
|
631
|
+
function queueFollowup(state, id, task) {
|
|
632
|
+
const job = find(state, id, ...ACTIVE, "done", "failed");
|
|
633
|
+
if (!job.notes.some((note) => note.followups?.includes(task))) throw new Error(`job ${id} reported no follow-up "${task}"`);
|
|
634
|
+
const item = {
|
|
635
|
+
...job.item,
|
|
636
|
+
id: `${job.item.id}+followup`,
|
|
637
|
+
play: "followup",
|
|
638
|
+
title: task
|
|
639
|
+
};
|
|
640
|
+
const url = prUrl(job);
|
|
641
|
+
const prompt = `${task}
|
|
642
|
+
|
|
643
|
+
This came up while an agent worked on "${job.item.title}"${url ? ` (${url})` : ""}. Make it its own change on a new branch, and open a pull request for it.
|
|
644
|
+
`;
|
|
645
|
+
return addJobs(state, [item], "pick", { prompt })[0];
|
|
646
|
+
}
|
|
397
647
|
function find(state, id, ...states) {
|
|
398
648
|
const job = readJobs(state).find((job) => job.id === id);
|
|
399
649
|
if (!job) throw new Error(`no job ${id}`);
|
|
@@ -402,7 +652,7 @@ function find(state, id, ...states) {
|
|
|
402
652
|
}
|
|
403
653
|
/** Takes a job out of the queue or the results, and deletes the branches its attempts kept. */
|
|
404
654
|
async function removeJob(state, id) {
|
|
405
|
-
const job = find(state, id, "queued", "done", "failed");
|
|
655
|
+
const job = find(state, id, "queued", "blocked", "done", "failed");
|
|
406
656
|
append(state, {
|
|
407
657
|
at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
408
658
|
type: "remove",
|
|
@@ -440,6 +690,19 @@ function moveJob(state, id, index) {
|
|
|
440
690
|
index
|
|
441
691
|
});
|
|
442
692
|
}
|
|
693
|
+
/**
|
|
694
|
+
* An agent's note on the job it works, the user's answer to a blocked job, which queues it again,
|
|
695
|
+
* or what the merge train did with the job's PR.
|
|
696
|
+
*/
|
|
697
|
+
function addNote(state, id, note) {
|
|
698
|
+
if (note.kind !== "train") find(state, id, note.kind === "answer" ? "blocked" : "running");
|
|
699
|
+
append(state, {
|
|
700
|
+
at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
701
|
+
type: "note",
|
|
702
|
+
id,
|
|
703
|
+
note
|
|
704
|
+
});
|
|
705
|
+
}
|
|
443
706
|
function startJob(state, id, agent) {
|
|
444
707
|
append(state, {
|
|
445
708
|
at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
@@ -481,171 +744,6 @@ function autoPicks(candidates, jobs, auto, count) {
|
|
|
481
744
|
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
745
|
}
|
|
483
746
|
//#endregion
|
|
484
|
-
//#region src/backend/review.ts
|
|
485
|
-
const MINUTE$1 = 6e4;
|
|
486
|
-
/** The PR workwork had the agent open, found by the branch workwork names; scans name theirs per area, so they find none. */
|
|
487
|
-
async function findPr(item) {
|
|
488
|
-
const branch = `workwork/${item.id.replace(/[^\w./@-]+/g, "-")}`;
|
|
489
|
-
return JSON.parse(await output("gh", [
|
|
490
|
-
"pr",
|
|
491
|
-
"list",
|
|
492
|
-
"--head",
|
|
493
|
-
branch,
|
|
494
|
-
"--state",
|
|
495
|
-
"all",
|
|
496
|
-
"--json",
|
|
497
|
-
"url"
|
|
498
|
-
], item.repo))[0]?.url;
|
|
499
|
-
}
|
|
500
|
-
async function readPr(url) {
|
|
501
|
-
const raw = JSON.parse(await gh([
|
|
502
|
-
"pr",
|
|
503
|
-
"view",
|
|
504
|
-
url,
|
|
505
|
-
"--json",
|
|
506
|
-
"state,isDraft,title,body,additions,deletions,files,mergeable,isCrossRepository,statusCheckRollup"
|
|
507
|
-
]));
|
|
508
|
-
return {
|
|
509
|
-
state: raw.state,
|
|
510
|
-
draft: raw.isDraft,
|
|
511
|
-
title: raw.title,
|
|
512
|
-
level: /^Level: (\w+)\s*$/m.exec(raw.body)?.[1],
|
|
513
|
-
additions: raw.additions,
|
|
514
|
-
deletions: raw.deletions,
|
|
515
|
-
files: raw.files.map((file) => file.path),
|
|
516
|
-
checks: checks(raw.statusCheckRollup),
|
|
517
|
-
conflicts: raw.mergeable === "UNKNOWN" ? null : raw.mergeable === "CONFLICTING",
|
|
518
|
-
fork: raw.isCrossRepository
|
|
519
|
-
};
|
|
520
|
-
}
|
|
521
|
-
/** Check runs have a conclusion, empty while running; commit statuses have a state. */
|
|
522
|
-
function checks(rollup) {
|
|
523
|
-
if (rollup.length === 0) return "none";
|
|
524
|
-
const results = rollup.map((check) => check.conclusion || check.state || "");
|
|
525
|
-
if (results.some((result) => /FAILURE|ERROR|CANCELLED|TIMED_OUT|ACTION_REQUIRED/.test(result))) return "failing";
|
|
526
|
-
if (results.some((result) => result === "" || result === "PENDING" || result === "EXPECTED")) return "pending";
|
|
527
|
-
return "passing";
|
|
528
|
-
}
|
|
529
|
-
/** What the review popup shows: the whole PR body and its diff, fetched only when opened. */
|
|
530
|
-
async function readPrDetails(url) {
|
|
531
|
-
const [view, diff] = await Promise.all([gh([
|
|
532
|
-
"pr",
|
|
533
|
-
"view",
|
|
534
|
-
url,
|
|
535
|
-
"--json",
|
|
536
|
-
"body"
|
|
537
|
-
]), gh([
|
|
538
|
-
"pr",
|
|
539
|
-
"diff",
|
|
540
|
-
url
|
|
541
|
-
])]);
|
|
542
|
-
return {
|
|
543
|
-
body: JSON.parse(view).body,
|
|
544
|
-
diff
|
|
545
|
-
};
|
|
546
|
-
}
|
|
547
|
-
const mergePr = (url) => gh([
|
|
548
|
-
"pr",
|
|
549
|
-
"merge",
|
|
550
|
-
url,
|
|
551
|
-
"--squash",
|
|
552
|
-
"--delete-branch"
|
|
553
|
-
]);
|
|
554
|
-
const closePr = (url) => gh([
|
|
555
|
-
"pr",
|
|
556
|
-
"close",
|
|
557
|
-
url,
|
|
558
|
-
"--delete-branch"
|
|
559
|
-
]);
|
|
560
|
-
/** A rebase that stopped on conflicts git can't resolve; nothing was pushed. */
|
|
561
|
-
var Conflicts = class extends Error {
|
|
562
|
-
files;
|
|
563
|
-
constructor(files) {
|
|
564
|
-
super(`Rebase stopped. Resolve these conflicts manually; no changes were pushed:\n${files.join("\n")}`);
|
|
565
|
-
this.files = files;
|
|
566
|
-
}
|
|
567
|
-
};
|
|
568
|
-
async function rebasePr(url) {
|
|
569
|
-
const pull = /^(https:\/\/github\.com\/[\w.-]+\/[\w.-]+)\/pull\/(\d+)$/.exec(url);
|
|
570
|
-
if (!pull) throw new Error("Rebase requires a github.com pull request URL");
|
|
571
|
-
const [, repo, number] = pull;
|
|
572
|
-
const pr = JSON.parse(await gh([
|
|
573
|
-
"pr",
|
|
574
|
-
"view",
|
|
575
|
-
url,
|
|
576
|
-
"--json",
|
|
577
|
-
"baseRefName,headRefName,isCrossRepository"
|
|
578
|
-
]));
|
|
579
|
-
if (pr.isCrossRepository || pr.baseRefName === pr.headRefName) throw new Error("Rebase requires a separate PR branch in the same repository");
|
|
580
|
-
const dir = await mkdtemp(join(tmpdir(), "factory-rebase-"));
|
|
581
|
-
const git = (...args) => output("git", args, dir);
|
|
582
|
-
const remote = `${repo}.git`;
|
|
583
|
-
const base = "refs/factory/base";
|
|
584
|
-
const head = "refs/factory/head";
|
|
585
|
-
const branch = `refs/heads/${pr.headRefName}`;
|
|
586
|
-
try {
|
|
587
|
-
await git("init", "--quiet");
|
|
588
|
-
await git("config", "--add", "credential.https://github.com.helper", "");
|
|
589
|
-
await git("config", "--add", "credential.https://github.com.helper", "!gh auth git-credential");
|
|
590
|
-
await git("fetch", "--no-tags", remote, `refs/heads/${pr.baseRefName}:${base}`, `refs/pull/${number}/head:${head}`);
|
|
591
|
-
const before = (await git("rev-parse", head)).trim();
|
|
592
|
-
await git("checkout", "--quiet", "--detach", head);
|
|
593
|
-
try {
|
|
594
|
-
await git("rebase", "--rebase-merges", base);
|
|
595
|
-
} catch (error) {
|
|
596
|
-
const conflicts = (await git("diff", "--name-only", "--diff-filter=U")).trim();
|
|
597
|
-
if (!conflicts) throw error;
|
|
598
|
-
throw new Conflicts(conflicts.split("\n"));
|
|
599
|
-
}
|
|
600
|
-
await git("push", `--force-with-lease=${branch}:${before}`, remote, `HEAD:${branch}`);
|
|
601
|
-
} finally {
|
|
602
|
-
await rm(dir, {
|
|
603
|
-
recursive: true,
|
|
604
|
-
force: true
|
|
605
|
-
});
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
|
-
/** Run outside any repo, so --delete-branch only touches GitHub, never a local checkout. */
|
|
609
|
-
const gh = (args) => output("gh", args, tmpdir());
|
|
610
|
-
/**
|
|
611
|
-
* PR status for the page. gh takes a second or more per PR, so this answers from what it last
|
|
612
|
-
* fetched and refreshes open PRs in the background. While GitHub calculates mergeability, retry
|
|
613
|
-
* sooner so the page doesn't wait a minute after a rebase.
|
|
614
|
-
*/
|
|
615
|
-
function prCache() {
|
|
616
|
-
const cache = /* @__PURE__ */ new Map();
|
|
617
|
-
function refresh(url) {
|
|
618
|
-
const entry = {
|
|
619
|
-
at: Date.now(),
|
|
620
|
-
pr: cache.get(url)?.pr
|
|
621
|
-
};
|
|
622
|
-
cache.set(url, entry);
|
|
623
|
-
readPr(url).then((pr) => entry.pr = pr, (error) => console.error(`factory: gh pr view ${url} failed: ${error.message}`));
|
|
624
|
-
}
|
|
625
|
-
return {
|
|
626
|
-
get(urls) {
|
|
627
|
-
const prs = {};
|
|
628
|
-
for (const url of urls) {
|
|
629
|
-
const entry = cache.get(url);
|
|
630
|
-
const interval = entry?.pr?.conflicts === null ? 5e3 : MINUTE$1;
|
|
631
|
-
if (!entry || (entry.pr?.state ?? "OPEN") === "OPEN" && Date.now() - entry.at > interval) refresh(url);
|
|
632
|
-
const pr = cache.get(url).pr;
|
|
633
|
-
if (pr) prs[url] = pr;
|
|
634
|
-
}
|
|
635
|
-
return prs;
|
|
636
|
-
},
|
|
637
|
-
/** Asks GitHub now, for an action that must not act on a stale answer. */
|
|
638
|
-
async fresh(url) {
|
|
639
|
-
const pr = await readPr(url);
|
|
640
|
-
cache.set(url, {
|
|
641
|
-
at: Date.now(),
|
|
642
|
-
pr
|
|
643
|
-
});
|
|
644
|
-
return pr;
|
|
645
|
-
}
|
|
646
|
-
};
|
|
647
|
-
}
|
|
648
|
-
//#endregion
|
|
649
747
|
//#region src/backend/loop.ts
|
|
650
748
|
const MINUTE = 6e4;
|
|
651
749
|
/** Auto mode keeps this many of its jobs queued per agent, so picks never drown in them. */
|
|
@@ -674,11 +772,13 @@ async function run(options) {
|
|
|
674
772
|
let topping;
|
|
675
773
|
function writeStatus() {
|
|
676
774
|
const until = options.until ? new Date(options.until).toISOString() : void 0;
|
|
775
|
+
const timeout = options.timeoutMs / MINUTE;
|
|
677
776
|
const status = {
|
|
678
777
|
pid: process.pid,
|
|
679
778
|
run: id,
|
|
680
779
|
until,
|
|
681
780
|
limit: options.limit,
|
|
781
|
+
timeout,
|
|
682
782
|
workers
|
|
683
783
|
};
|
|
684
784
|
writeFileSync(join(state, "status.json"), `${JSON.stringify(status, null, 2)}\n`);
|
|
@@ -829,11 +929,11 @@ async function run(options) {
|
|
|
829
929
|
writeFileSync(logPath, `${message}\n`);
|
|
830
930
|
return attempt(begun, "failed", { error: message.slice(0, 2e3) });
|
|
831
931
|
}
|
|
832
|
-
writeFileSync(join(dir, "prompt.md"), prompt);
|
|
932
|
+
writeFileSync(join(dir, "prompt.md"), prompt + earlier(job) + reporting(job, begun));
|
|
833
933
|
if (stopping) return void 0;
|
|
834
934
|
const live = {
|
|
835
935
|
begun,
|
|
836
|
-
pid: startAgent(agent$1, worktree, dir),
|
|
936
|
+
pid: startAgent(agent$1, worktree, dir, job.id),
|
|
837
937
|
worktree
|
|
838
938
|
};
|
|
839
939
|
writeFileSync(livePath(state, job.id), JSON.stringify(live));
|
|
@@ -865,8 +965,9 @@ async function run(options) {
|
|
|
865
965
|
const code = existsSync(exit) ? Number(readFileSync(exit, "utf8")) : 1;
|
|
866
966
|
const agent$2 = agent(begun.agent, begun);
|
|
867
967
|
const outcome = timedOut ? "timeout" : classify(agent$2, code, text);
|
|
968
|
+
const result = readJobs(state).find((other) => other.id === job.id).notes.findLast((note) => note.kind === "result" && note.attempt === job.attempts.length + 1);
|
|
868
969
|
return attempt(begun, outcome, {
|
|
869
|
-
url: outcome === "done" && await delivered(job.item) || text.match(URL$1)?.at(-1),
|
|
970
|
+
url: outcome === "done" && await delivered(job.item) || result?.url || text.match(URL$1)?.at(-1),
|
|
870
971
|
branch
|
|
871
972
|
});
|
|
872
973
|
}
|
|
@@ -916,7 +1017,7 @@ const attempt = (begun, outcome, rest = {}) => ({
|
|
|
916
1017
|
* detached, so it outlives a run that hands it off. The shell in between reads the prompt from a file
|
|
917
1018
|
* and writes the exit code to one, both of which outlast this process. Returns the group's pid.
|
|
918
1019
|
*/
|
|
919
|
-
function startAgent(agent, cwd, dir) {
|
|
1020
|
+
function startAgent(agent, cwd, dir, job) {
|
|
920
1021
|
const log = openSync(join(dir, "agent.log"), "w");
|
|
921
1022
|
const child = spawn("sh", [
|
|
922
1023
|
"-c",
|
|
@@ -932,12 +1033,46 @@ function startAgent(agent, cwd, dir) {
|
|
|
932
1033
|
"ignore",
|
|
933
1034
|
log,
|
|
934
1035
|
log
|
|
935
|
-
]
|
|
1036
|
+
],
|
|
1037
|
+
env: {
|
|
1038
|
+
...process.env,
|
|
1039
|
+
FACTORY_JOB: job
|
|
1040
|
+
}
|
|
936
1041
|
});
|
|
937
1042
|
closeSync(log);
|
|
938
1043
|
child.unref();
|
|
939
1044
|
return child.pid;
|
|
940
1045
|
}
|
|
1046
|
+
const NOTE_LABEL = {
|
|
1047
|
+
note: "note",
|
|
1048
|
+
ask: "it asked",
|
|
1049
|
+
answer: "the user answered",
|
|
1050
|
+
result: "result",
|
|
1051
|
+
train: "merge train"
|
|
1052
|
+
};
|
|
1053
|
+
/** What earlier attempts said and kept, so a retry or an answered question picks up where they left off. */
|
|
1054
|
+
function earlier(job) {
|
|
1055
|
+
if (job.attempts.length === 0) return "";
|
|
1056
|
+
return `\n\n## Earlier attempts at this job\n${job.attempts.flatMap((attempt, i) => [`\nAttempt ${i + 1} (${attempt.agent}, ${attempt.outcome})${attempt.branch ? `: its work is kept on branch ${attempt.branch}; start from it with \`git checkout -B ${attempt.branch}\`` : ""}`, ...job.notes.filter((note) => note.attempt === i + 1).map((note) => `- ${NOTE_LABEL[note.kind]}: ${note.text}`)]).join("\n")}\n`;
|
|
1057
|
+
}
|
|
1058
|
+
/** How the agent reports back. Nobody watches a run, so this is all the user learns besides the PR. */
|
|
1059
|
+
function reporting(job, { agent, model, effort }) {
|
|
1060
|
+
const factory = `"${process.execPath}" "${process.argv[1]}"`;
|
|
1061
|
+
const signature = [
|
|
1062
|
+
agent,
|
|
1063
|
+
model,
|
|
1064
|
+
effort
|
|
1065
|
+
].filter(Boolean).join(" · ");
|
|
1066
|
+
return `
|
|
1067
|
+
---
|
|
1068
|
+
You run headless for factory as job ${job.id}, and nobody watches live. Report through its CLI:
|
|
1069
|
+
|
|
1070
|
+
- \`${factory} note "<text>"\` records something worth knowing later: what you found, tried, or what's in the way. A few per job, not a play-by-play.
|
|
1071
|
+
- Decide for yourself wherever a reasonable engineer would, and note the call you made so the user can revisit it. Asking parks the job until the user is back, often the next morning, so it's for the rare decision that's costly to get wrong and that you can't settle from the code, the docs or the task: deleting data, a public API change, two directions that would each waste the work of the other. Then run \`${factory} ask "<question, with the options and your recommendation>"\` and end there. Your work in this folder is kept, and the job goes on later with your notes, the answer and your work.
|
|
1072
|
+
- Before you end, run \`${factory} result "<what changed and why, in a sentence or two>" --confidence low|medium|high\`, adding \`--url <link>\` for the PR or issue you opened and \`--followup "<task>"\` for each task you saw that deserves its own job.
|
|
1073
|
+
- In a PR description you write, add the line \`Agent: ${signature}\`.
|
|
1074
|
+
`;
|
|
1075
|
+
}
|
|
941
1076
|
/** The PR the agent opened, as GitHub knows it; undefined when gh can't tell, and the log's last link stands in. */
|
|
942
1077
|
async function delivered(item) {
|
|
943
1078
|
try {
|
|
@@ -1400,6 +1535,7 @@ function context() {
|
|
|
1400
1535
|
- 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
1536
|
- \`${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
1537
|
- To queue candidates: \`${factory} add <id>... --repo <repo>\`, optionally with \`--agent ${AGENT_NAMES.join("|")}\`. Picked jobs go ahead of auto ones.
|
|
1538
|
+
- \`--after <job>\` holds a job until that one is done. \`${factory} review <job> --agent <name>\` queues a review of a job's PR by another agent than the one that wrote it.
|
|
1403
1539
|
- Levels: auto needs no review, skim a quick look, decide needs the user's judgment. Prefer a few that fit together over many that touch the same files, since those conflict when merged.
|
|
1404
1540
|
|
|
1405
1541
|
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.`;
|
|
@@ -1407,12 +1543,18 @@ Propose what to queue and why, briefly, and add jobs only once the user agrees.
|
|
|
1407
1543
|
//#endregion
|
|
1408
1544
|
//#region src/backend/repos.ts
|
|
1409
1545
|
/** Where workwork --all looks for repos, with the same fallback. */
|
|
1410
|
-
const
|
|
1546
|
+
const sourceRoot = () => resolve(process.env.SRC ?? process.cwd());
|
|
1547
|
+
/** Local repos are available as filters even when workwork finds no candidates in them. */
|
|
1548
|
+
function localRepos() {
|
|
1549
|
+
const root = sourceRoot();
|
|
1550
|
+
return existsSync(root) ? findRepos(root, 4) : [];
|
|
1551
|
+
}
|
|
1411
1552
|
/**
|
|
1412
|
-
* The
|
|
1413
|
-
*
|
|
1553
|
+
* The GitHub user's own repos (not forked or archived) that aren't cloned anywhere under $SRC
|
|
1554
|
+
* yet, newest first.
|
|
1414
1555
|
*/
|
|
1415
1556
|
async function clonableRepos() {
|
|
1557
|
+
const root = sourceRoot();
|
|
1416
1558
|
const listed = JSON.parse(await output("gh", [
|
|
1417
1559
|
"repo",
|
|
1418
1560
|
"list",
|
|
@@ -1422,46 +1564,31 @@ async function clonableRepos() {
|
|
|
1422
1564
|
"5000",
|
|
1423
1565
|
"--json",
|
|
1424
1566
|
"nameWithOwner,description,createdAt"
|
|
1425
|
-
],
|
|
1426
|
-
const
|
|
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];
|
|
1567
|
+
], root));
|
|
1568
|
+
const cloned = new Set(await Promise.all(localRepos().map(originName)));
|
|
1440
1569
|
return {
|
|
1441
|
-
root
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1570
|
+
root,
|
|
1571
|
+
repos: listed.filter((repo) => !cloned.has(repo.nameWithOwner.toLowerCase())).map((repo) => ({
|
|
1572
|
+
name: repo.nameWithOwner,
|
|
1573
|
+
description: repo.description,
|
|
1574
|
+
createdAt: repo.createdAt
|
|
1575
|
+
})).sort((a, b) => b.createdAt.localeCompare(a.createdAt))
|
|
1445
1576
|
};
|
|
1446
1577
|
}
|
|
1447
|
-
/** Clones `name` (owner/repo) to $SRC/<
|
|
1448
|
-
async function cloneRepo(name
|
|
1578
|
+
/** Clones `name` (owner/repo) to $SRC/<repo> and returns that path. */
|
|
1579
|
+
async function cloneRepo(name) {
|
|
1449
1580
|
if (!/^[\w.-]+\/[\w.-]+$/.test(name)) throw new Error(`${name} isn't a GitHub owner/repo`);
|
|
1450
|
-
|
|
1451
|
-
const path = join(
|
|
1581
|
+
const root = sourceRoot();
|
|
1582
|
+
const path = join(root, name.split("/")[1]);
|
|
1452
1583
|
if (existsSync(path)) throw new Error(`${path} already exists`);
|
|
1453
1584
|
await output("gh", [
|
|
1454
1585
|
"repo",
|
|
1455
1586
|
"clone",
|
|
1456
1587
|
name,
|
|
1457
1588
|
path
|
|
1458
|
-
],
|
|
1589
|
+
], root);
|
|
1459
1590
|
return path;
|
|
1460
1591
|
}
|
|
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
1592
|
/** Repos under `dir`, searched the way workwork --all does. */
|
|
1466
1593
|
function findRepos(dir, depth) {
|
|
1467
1594
|
if (existsSync(join(dir, ".git"))) return [dir];
|
|
@@ -1565,14 +1692,6 @@ function ask(command, args, request, answers) {
|
|
|
1565
1692
|
});
|
|
1566
1693
|
}
|
|
1567
1694
|
//#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
1695
|
//#region src/backend/train.ts
|
|
1577
1696
|
const POLL = 1e4;
|
|
1578
1697
|
const PATIENCE = 18e5;
|
|
@@ -1581,9 +1700,10 @@ const TRIES = 3;
|
|
|
1581
1700
|
/**
|
|
1582
1701
|
* Merges PRs one after another per repo, since each merge moves the base under the next one.
|
|
1583
1702
|
* 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.
|
|
1703
|
+
* and when git stopped on conflicts, handed to `onConflicts` to queue for an agent. `onEnd` hears
|
|
1704
|
+
* how each PR's ride ended.
|
|
1585
1705
|
*/
|
|
1586
|
-
function mergeTrain(prs, onConflicts) {
|
|
1706
|
+
function mergeTrain(prs, onConflicts, onEnd) {
|
|
1587
1707
|
const cars = /* @__PURE__ */ new Map();
|
|
1588
1708
|
/** Each repo's last PR on the train; a new one waits for it, even from a later start. */
|
|
1589
1709
|
const tracks = /* @__PURE__ */ new Map();
|
|
@@ -1630,17 +1750,20 @@ function mergeTrain(prs, onConflicts) {
|
|
|
1630
1750
|
for (const { id, url } of boarding.toSorted((a, b) => size(a.url) - size(b.url))) {
|
|
1631
1751
|
cars.set(id, { step: "queued" });
|
|
1632
1752
|
const repo = url.replace(/\/pull\/\d+$/, "");
|
|
1633
|
-
const track = (tracks.get(repo) ?? Promise.resolve()).then(() => land(id, url)).then(() =>
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1753
|
+
const track = (tracks.get(repo) ?? Promise.resolve()).then(() => land(id, url)).then(() => {
|
|
1754
|
+
cars.set(id, { step: "merged" });
|
|
1755
|
+
onEnd(id, "merged");
|
|
1756
|
+
}, (error) => {
|
|
1757
|
+
let reason = error.message;
|
|
1758
|
+
if (error instanceof Conflicts) {
|
|
1759
|
+
onConflicts(id, url, error.files);
|
|
1760
|
+
reason = `conflicts in ${error.files.join(", ")}; a job to resolve them is queued`;
|
|
1761
|
+
}
|
|
1640
1762
|
cars.set(id, {
|
|
1641
1763
|
step: "skipped",
|
|
1642
1764
|
reason
|
|
1643
1765
|
});
|
|
1766
|
+
onEnd(id, `skipped: ${reason}`);
|
|
1644
1767
|
});
|
|
1645
1768
|
tracks.set(repo, track);
|
|
1646
1769
|
}
|
|
@@ -1666,7 +1789,10 @@ const TYPES = {
|
|
|
1666
1789
|
/** The page and the API it uses: the same state and commands as the CLI, nothing more. */
|
|
1667
1790
|
function serve(state, port, host) {
|
|
1668
1791
|
const prs = prCache();
|
|
1669
|
-
const train = mergeTrain(prs, (id, url, files) => queueConflicts(state, id, url, files))
|
|
1792
|
+
const train = mergeTrain(prs, (id, url, files) => queueConflicts(state, id, url, files), (id, text) => addNote(state, id, {
|
|
1793
|
+
kind: "train",
|
|
1794
|
+
text
|
|
1795
|
+
}));
|
|
1670
1796
|
const planner = chat(state);
|
|
1671
1797
|
const models = /* @__PURE__ */ new Map();
|
|
1672
1798
|
function modelsOf(name) {
|
|
@@ -1704,16 +1830,22 @@ function serve(state, port, host) {
|
|
|
1704
1830
|
}
|
|
1705
1831
|
const routes = {
|
|
1706
1832
|
"GET /api/state": () => snapshot(state, (urls) => prs.get(urls), train.cars()),
|
|
1707
|
-
"GET /api/candidates": () =>
|
|
1708
|
-
|
|
1709
|
-
items
|
|
1833
|
+
"GET /api/candidates": () => {
|
|
1834
|
+
const cached = readCandidates(state);
|
|
1835
|
+
const items = cached?.items ?? [];
|
|
1836
|
+
const repos = [.../* @__PURE__ */ new Set([...localRepos(), ...items.map((item) => item.repo)])].sort();
|
|
1837
|
+
return {
|
|
1838
|
+
at: cached?.at ?? null,
|
|
1839
|
+
items,
|
|
1840
|
+
repos
|
|
1841
|
+
};
|
|
1710
1842
|
},
|
|
1711
1843
|
"POST /api/candidates/refresh": () => {
|
|
1712
1844
|
refreshCandidates(state, "workwork").catch((error) => console.error(`factory: workwork failed: ${error.message}`));
|
|
1713
1845
|
},
|
|
1714
1846
|
"GET /api/repos": () => clonableRepos(),
|
|
1715
1847
|
"POST /api/repos/clone": async (body) => {
|
|
1716
|
-
await cloneRepo(body.name
|
|
1848
|
+
await cloneRepo(body.name);
|
|
1717
1849
|
refreshCandidates(state, "workwork").catch((error) => console.error(`factory: workwork failed: ${error.message}`));
|
|
1718
1850
|
},
|
|
1719
1851
|
"GET /api/plays": async () => JSON.parse(await output("workwork", ["plays", "--json"], process.cwd())),
|
|
@@ -1724,6 +1856,12 @@ function serve(state, port, host) {
|
|
|
1724
1856
|
"POST /api/jobs": (body) => addJobs(state, candidates(body.items), "pick", body.pin),
|
|
1725
1857
|
"POST /api/jobs/remove": (_, id) => removeJob(state, id),
|
|
1726
1858
|
"POST /api/jobs/retry": (_, id) => retryJob(state, id),
|
|
1859
|
+
"POST /api/jobs/answer": (body, id) => addNote(state, id, {
|
|
1860
|
+
kind: "answer",
|
|
1861
|
+
text: body.text
|
|
1862
|
+
}),
|
|
1863
|
+
"POST /api/jobs/followup": (body, id) => queueFollowup(state, id, body.task),
|
|
1864
|
+
"POST /api/jobs/review": (body, id) => queueReview(state, id, body.agent),
|
|
1727
1865
|
"POST /api/jobs/move": (body, id) => moveJob(state, id, body.index),
|
|
1728
1866
|
"GET /api/jobs/pr": (_, id) => readPrDetails(jobPr(id)),
|
|
1729
1867
|
"POST /api/jobs/merge": async (_, id) => {
|
|
@@ -1850,7 +1988,7 @@ function send(response, type, body) {
|
|
|
1850
1988
|
}
|
|
1851
1989
|
//#endregion
|
|
1852
1990
|
//#region package.json
|
|
1853
|
-
var version = "0.2.
|
|
1991
|
+
var version = "0.2.2";
|
|
1854
1992
|
//#endregion
|
|
1855
1993
|
//#region src/backend/cli.ts
|
|
1856
1994
|
const USAGE = `factory: works a queue of workwork candidates with headless coding agents
|
|
@@ -1860,15 +1998,28 @@ const USAGE = `factory: works a queue of workwork candidates with headless codin
|
|
|
1860
1998
|
factory stop [--kill] turn it off; agents at work finish, and the next start records them;
|
|
1861
1999
|
--kill stops them too and puts their jobs back in the queue
|
|
1862
2000
|
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
|
|
2001
|
+
factory add <id>... [--repo DIR] [--agent NAME] [--model M] [--effort E] [--after JOB]
|
|
2002
|
+
queue workwork candidates from DIR (default: here), ahead of auto jobs;
|
|
2003
|
+
--after: start once that job is done
|
|
2004
|
+
factory review <job> --agent NAME
|
|
2005
|
+
queue a review of the job's PR by another agent, once the job is done
|
|
2006
|
+
factory wait <job> [--timeout MIN]
|
|
2007
|
+
block until the job is done, failed or blocked; exits 1 at the timeout
|
|
2008
|
+
factory tail follow what happens to every job, one line each
|
|
1865
2009
|
factory remove <job> take a job out of the queue or the results, with its kept branches
|
|
1866
2010
|
factory retry <job> queue a failed job again, with the same agent
|
|
2011
|
+
factory answer <job> "<text>" answer the question a blocked job asked, and queue it again
|
|
1867
2012
|
factory auto on|off let auto mode keep the queue topped up from workwork, or not
|
|
1868
2013
|
factory report [--all] the last run (or every run): outcome, agent, job, PR/issue link or log
|
|
1869
2014
|
factory serve [--port 7040] [--host 127.0.0.1]
|
|
1870
2015
|
a web page to watch, browse candidates, queue them and turn it on or off
|
|
1871
2016
|
|
|
2017
|
+
For the agent working a job (the job is $FACTORY_JOB, or --job ID):
|
|
2018
|
+
factory note "<text>" something worth knowing later
|
|
2019
|
+
factory ask "<question>" a decision for the user; the job waits for the answer once the agent ends
|
|
2020
|
+
factory result "<summary>" [--confidence low|medium|high] [--url LINK] [--followup "<task>"]...
|
|
2021
|
+
what the job delivered, and tasks that deserve their own job
|
|
2022
|
+
|
|
1872
2023
|
Run options (defaults from settings.json):
|
|
1873
2024
|
--agents claude,pi which registered agents work this run
|
|
1874
2025
|
--limit N|none usage-limit hits per agent before it stops; none: wait for each reset
|
|
@@ -1898,6 +2049,14 @@ async function main() {
|
|
|
1898
2049
|
agent: { type: "string" },
|
|
1899
2050
|
all: { type: "boolean" },
|
|
1900
2051
|
kill: { type: "boolean" },
|
|
2052
|
+
job: { type: "string" },
|
|
2053
|
+
after: { type: "string" },
|
|
2054
|
+
confidence: { type: "string" },
|
|
2055
|
+
url: { type: "string" },
|
|
2056
|
+
followup: {
|
|
2057
|
+
type: "string",
|
|
2058
|
+
multiple: true
|
|
2059
|
+
},
|
|
1901
2060
|
port: {
|
|
1902
2061
|
type: "string",
|
|
1903
2062
|
default: "7040"
|
|
@@ -1928,6 +2087,18 @@ async function main() {
|
|
|
1928
2087
|
}
|
|
1929
2088
|
if (command === "retry") return args.forEach((id) => retryJob(STATE, id));
|
|
1930
2089
|
if (command === "auto") return auto(args[0]);
|
|
2090
|
+
if (command === "answer") return addNote(STATE, args[0], {
|
|
2091
|
+
kind: "answer",
|
|
2092
|
+
text: text(args[1])
|
|
2093
|
+
});
|
|
2094
|
+
if (command === "note" || command === "ask") return addNote(STATE, ownJob(values.job), {
|
|
2095
|
+
kind: command,
|
|
2096
|
+
text: text(args[0])
|
|
2097
|
+
});
|
|
2098
|
+
if (command === "result") return result(args[0], values);
|
|
2099
|
+
if (command === "review") return review(args[0], values.agent);
|
|
2100
|
+
if (command === "wait") return wait(args[0], values.timeout);
|
|
2101
|
+
if (command === "tail") return tail();
|
|
1931
2102
|
if (command === "add") return add(args, values);
|
|
1932
2103
|
if (command === "serve") return void serve(STATE, count(values.port, "--port"), values.host);
|
|
1933
2104
|
if (command === "stop") {
|
|
@@ -1985,12 +2156,102 @@ async function add(ids, values) {
|
|
|
1985
2156
|
return item;
|
|
1986
2157
|
});
|
|
1987
2158
|
if (values.agent) agent(values.agent);
|
|
2159
|
+
if (values.after && !readJobs(STATE).some((job) => job.id === values.after)) throw new Error(`--after names no job ${values.after}; factory status lists them`);
|
|
1988
2160
|
const pin = {
|
|
1989
2161
|
agent: values.agent,
|
|
1990
2162
|
model: values.model?.[0],
|
|
1991
|
-
effort: values.effort?.[0]
|
|
2163
|
+
effort: values.effort?.[0],
|
|
2164
|
+
after: values.after
|
|
1992
2165
|
};
|
|
1993
|
-
|
|
2166
|
+
const added = addJobs(STATE, items, "pick", pin);
|
|
2167
|
+
for (const job of added) console.log(`queued ${job.id}: ${job.item.label} ${job.item.id}`);
|
|
2168
|
+
for (const item of items.filter((item) => !added.some((job) => job.item === item))) console.log(`already queued: ${item.label} ${item.id}`);
|
|
2169
|
+
}
|
|
2170
|
+
function review(id, name) {
|
|
2171
|
+
if (!id || !name) throw new Error("factory review wants a job and --agent NAME");
|
|
2172
|
+
agent(name);
|
|
2173
|
+
const job = queueReview(STATE, id, name);
|
|
2174
|
+
console.log(job ? `queued ${job.id}: ${job.item.title}` : `a review of ${id} is already queued`);
|
|
2175
|
+
}
|
|
2176
|
+
const SETTLED = [
|
|
2177
|
+
"done",
|
|
2178
|
+
"failed",
|
|
2179
|
+
"blocked",
|
|
2180
|
+
"removed"
|
|
2181
|
+
];
|
|
2182
|
+
async function wait(id, timeout) {
|
|
2183
|
+
if (!id) throw new Error("factory wait wants a job");
|
|
2184
|
+
const deadline = timeout ? Date.now() + count(timeout, "--timeout") * 6e4 : Infinity;
|
|
2185
|
+
for (;;) {
|
|
2186
|
+
const job = readJobs(STATE).find((job) => job.id === id);
|
|
2187
|
+
if (!job) throw new Error(`no job ${id}; factory status lists them`);
|
|
2188
|
+
if (SETTLED.includes(job.state)) {
|
|
2189
|
+
const said = job.notes.findLast((note) => note.kind === "result" || note.kind === "ask");
|
|
2190
|
+
return console.log([
|
|
2191
|
+
job.state,
|
|
2192
|
+
prUrl(job),
|
|
2193
|
+
said?.text
|
|
2194
|
+
].filter(Boolean).join(" "));
|
|
2195
|
+
}
|
|
2196
|
+
if (Date.now() >= deadline) {
|
|
2197
|
+
console.log(`still ${job.state}`);
|
|
2198
|
+
process.exitCode = 1;
|
|
2199
|
+
return;
|
|
2200
|
+
}
|
|
2201
|
+
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
2202
|
+
}
|
|
2203
|
+
}
|
|
2204
|
+
/** Prints each new event in jobs.jsonl as a line, until interrupted. */
|
|
2205
|
+
async function tail() {
|
|
2206
|
+
const path = join(STATE, "jobs.jsonl");
|
|
2207
|
+
let read = existsSync(path) ? statSync(path).size : 0;
|
|
2208
|
+
for (;;) {
|
|
2209
|
+
const size = existsSync(path) ? statSync(path).size : 0;
|
|
2210
|
+
if (size > read) {
|
|
2211
|
+
const text = readFileSync(path).subarray(read, size).toString("utf8");
|
|
2212
|
+
const end = text.lastIndexOf("\n") + 1;
|
|
2213
|
+
read += Buffer.byteLength(text.slice(0, end));
|
|
2214
|
+
const jobs = new Map(readJobs(STATE).map((job) => [job.id, job]));
|
|
2215
|
+
for (const line of text.slice(0, end).split("\n").filter(Boolean)) console.log(eventLine(JSON.parse(line), jobs));
|
|
2216
|
+
}
|
|
2217
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
2218
|
+
}
|
|
2219
|
+
}
|
|
2220
|
+
const PAST = {
|
|
2221
|
+
remove: "removed",
|
|
2222
|
+
requeue: "back in the queue",
|
|
2223
|
+
retry: "queued for a retry"
|
|
2224
|
+
};
|
|
2225
|
+
function eventLine(event, jobs) {
|
|
2226
|
+
const id = event.type === "add" ? event.job.id : event.id;
|
|
2227
|
+
const job = jobs.get(id);
|
|
2228
|
+
const what = event.type === "add" ? `queued ${event.job.item.title}${event.job.agent ? ` for ${event.job.agent}` : ""}` : event.type === "start" ? `started by ${event.agent}` : event.type === "attempt" ? `${event.attempt.agent} ${event.attempt.outcome} ${event.attempt.url ?? ""}` : event.type === "note" ? `${event.note.kind}: ${event.note.text}` : event.type === "move" ? `moved to ${event.index + 1}` : PAST[event.type];
|
|
2229
|
+
return `${clock(event.at)} ${id} ${job ? basename(job.item.repo) : ""} ${what}`.trimEnd();
|
|
2230
|
+
}
|
|
2231
|
+
/** The job an agent works, which factory hands it in the environment. */
|
|
2232
|
+
function ownJob(job = process.env.FACTORY_JOB) {
|
|
2233
|
+
if (!job) throw new Error("runs inside a job, which sets FACTORY_JOB; elsewhere pass --job ID");
|
|
2234
|
+
return job;
|
|
2235
|
+
}
|
|
2236
|
+
function text(value) {
|
|
2237
|
+
if (!value?.trim()) throw new Error("wants the text as one argument, in quotes: \"…\"");
|
|
2238
|
+
return value;
|
|
2239
|
+
}
|
|
2240
|
+
const CONFIDENCES = [
|
|
2241
|
+
"low",
|
|
2242
|
+
"medium",
|
|
2243
|
+
"high"
|
|
2244
|
+
];
|
|
2245
|
+
function result(summary, values) {
|
|
2246
|
+
const { confidence, url, followup = [] } = values;
|
|
2247
|
+
if (confidence !== void 0 && !CONFIDENCES.includes(confidence)) throw new Error(`--confidence wants ${CONFIDENCES.join(", ")}, got ${confidence}`);
|
|
2248
|
+
addNote(STATE, ownJob(values.job), {
|
|
2249
|
+
kind: "result",
|
|
2250
|
+
text: text(summary),
|
|
2251
|
+
confidence,
|
|
2252
|
+
url,
|
|
2253
|
+
followups: followup
|
|
2254
|
+
});
|
|
1994
2255
|
}
|
|
1995
2256
|
function auto(onOrOff) {
|
|
1996
2257
|
if (onOrOff !== "on" && onOrOff !== "off") throw new Error("factory auto wants on or off");
|
|
@@ -2018,7 +2279,11 @@ function status() {
|
|
|
2018
2279
|
console.log(` ${worker.agent.padEnd(7)} ${worker.state.padEnd(9)} ${what} (since ${clock(worker.since)}, limits hit ${worker.limits})`);
|
|
2019
2280
|
}
|
|
2020
2281
|
}
|
|
2021
|
-
const
|
|
2282
|
+
const jobs = readJobs(STATE);
|
|
2283
|
+
const blocked = jobs.filter((job) => job.state === "blocked");
|
|
2284
|
+
if (blocked.length) console.log(`waiting for your answer: ${blocked.length}`);
|
|
2285
|
+
for (const job of blocked) console.log(` ${line(job)}\n ${job.notes.findLast((note) => note.kind === "ask").text}`);
|
|
2286
|
+
const queued = jobs.filter((job) => job.state === "queued");
|
|
2022
2287
|
console.log(`queue: ${queued.length}`);
|
|
2023
2288
|
for (const job of queued.slice(0, 10)) console.log(` ${line(job)}`);
|
|
2024
2289
|
if (queued.length > 10) console.log(` … ${queued.length - 10} more`);
|
|
@@ -2040,6 +2305,8 @@ function report(all, run) {
|
|
|
2040
2305
|
if (attempt.run !== shown[i - 1]?.attempt.run) lines.push(`run ${attempt.run}`);
|
|
2041
2306
|
lines.push(` ${attempt.outcome.padEnd(7)} ${attempt.agent.padEnd(6)} ${line(job)}`);
|
|
2042
2307
|
const details = [attempt.url ?? attempt.log];
|
|
2308
|
+
const number = job.attempts.indexOf(attempt) + 1;
|
|
2309
|
+
for (const note of job.notes) if (note.attempt === number && (note.kind === "result" || note.kind === "ask")) details.push(`${note.kind === "ask" ? "asks" : "result"}: ${note.text}`);
|
|
2043
2310
|
if (attempt.error) details.push(attempt.error.split("\n")[0]);
|
|
2044
2311
|
if (attempt.branch) details.push(`unpushed work kept on branch ${attempt.branch}`);
|
|
2045
2312
|
lines.push(...details.map((detail) => ` ${detail}`));
|