@soleone/factory 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.mjs 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 (outcome === "done") job.state = "done";
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
- return jobs.find((job) => job.state === "queued" && (job.agent ?? agent) === agent);
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
- return items.map((item) => {
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 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;
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.`;
@@ -1409,8 +1545,8 @@ Propose what to queue and why, briefly, and add jobs only once the user agrees.
1409
1545
  /** Where workwork --all looks for repos, with the same fallback. */
1410
1546
  const SRC = process.env.SRC ?? process.cwd();
1411
1547
  /**
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.
1548
+ * The GitHub user's own repos (not forked or archived) that aren't cloned anywhere under $SRC
1549
+ * yet, newest first.
1414
1550
  */
1415
1551
  async function clonableRepos() {
1416
1552
  const listed = JSON.parse(await output("gh", [
@@ -1430,25 +1566,15 @@ async function clonableRepos() {
1430
1566
  description: repo.description,
1431
1567
  createdAt: repo.createdAt
1432
1568
  })).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
1569
  return {
1441
1570
  root: SRC,
1442
- folders: options,
1443
- usual,
1444
1571
  repos
1445
1572
  };
1446
1573
  }
1447
- /** Clones `name` (owner/repo) to $SRC/<folder>/<repo> and returns that path. */
1448
- async function cloneRepo(name, folder) {
1574
+ /** Clones `name` (owner/repo) to $SRC/<repo> and returns that path. */
1575
+ async function cloneRepo(name) {
1449
1576
  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]);
1577
+ const path = join(SRC, name.split("/")[1]);
1452
1578
  if (existsSync(path)) throw new Error(`${path} already exists`);
1453
1579
  await output("gh", [
1454
1580
  "repo",
@@ -1458,10 +1584,6 @@ async function cloneRepo(name, folder) {
1458
1584
  ], SRC);
1459
1585
  return path;
1460
1586
  }
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
1587
  /** Repos under `dir`, searched the way workwork --all does. */
1466
1588
  function findRepos(dir, depth) {
1467
1589
  if (existsSync(join(dir, ".git"))) return [dir];
@@ -1565,14 +1687,6 @@ function ask(command, args, request, answers) {
1565
1687
  });
1566
1688
  }
1567
1689
  //#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
1690
  //#region src/backend/train.ts
1577
1691
  const POLL = 1e4;
1578
1692
  const PATIENCE = 18e5;
@@ -1581,9 +1695,10 @@ const TRIES = 3;
1581
1695
  /**
1582
1696
  * Merges PRs one after another per repo, since each merge moves the base under the next one.
1583
1697
  * 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.
1698
+ * and when git stopped on conflicts, handed to `onConflicts` to queue for an agent. `onEnd` hears
1699
+ * how each PR's ride ended.
1585
1700
  */
1586
- function mergeTrain(prs, onConflicts) {
1701
+ function mergeTrain(prs, onConflicts, onEnd) {
1587
1702
  const cars = /* @__PURE__ */ new Map();
1588
1703
  /** Each repo's last PR on the train; a new one waits for it, even from a later start. */
1589
1704
  const tracks = /* @__PURE__ */ new Map();
@@ -1630,17 +1745,20 @@ function mergeTrain(prs, onConflicts) {
1630
1745
  for (const { id, url } of boarding.toSorted((a, b) => size(a.url) - size(b.url))) {
1631
1746
  cars.set(id, { step: "queued" });
1632
1747
  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`;
1748
+ const track = (tracks.get(repo) ?? Promise.resolve()).then(() => land(id, url)).then(() => {
1749
+ cars.set(id, { step: "merged" });
1750
+ onEnd(id, "merged");
1751
+ }, (error) => {
1752
+ let reason = error.message;
1753
+ if (error instanceof Conflicts) {
1754
+ onConflicts(id, url, error.files);
1755
+ reason = `conflicts in ${error.files.join(", ")}; a job to resolve them is queued`;
1756
+ }
1640
1757
  cars.set(id, {
1641
1758
  step: "skipped",
1642
1759
  reason
1643
1760
  });
1761
+ onEnd(id, `skipped: ${reason}`);
1644
1762
  });
1645
1763
  tracks.set(repo, track);
1646
1764
  }
@@ -1666,7 +1784,10 @@ const TYPES = {
1666
1784
  /** The page and the API it uses: the same state and commands as the CLI, nothing more. */
1667
1785
  function serve(state, port, host) {
1668
1786
  const prs = prCache();
1669
- const train = mergeTrain(prs, (id, url, files) => queueConflicts(state, id, url, files));
1787
+ const train = mergeTrain(prs, (id, url, files) => queueConflicts(state, id, url, files), (id, text) => addNote(state, id, {
1788
+ kind: "train",
1789
+ text
1790
+ }));
1670
1791
  const planner = chat(state);
1671
1792
  const models = /* @__PURE__ */ new Map();
1672
1793
  function modelsOf(name) {
@@ -1713,7 +1834,7 @@ function serve(state, port, host) {
1713
1834
  },
1714
1835
  "GET /api/repos": () => clonableRepos(),
1715
1836
  "POST /api/repos/clone": async (body) => {
1716
- await cloneRepo(body.name, body.folder);
1837
+ await cloneRepo(body.name);
1717
1838
  refreshCandidates(state, "workwork").catch((error) => console.error(`factory: workwork failed: ${error.message}`));
1718
1839
  },
1719
1840
  "GET /api/plays": async () => JSON.parse(await output("workwork", ["plays", "--json"], process.cwd())),
@@ -1724,6 +1845,12 @@ function serve(state, port, host) {
1724
1845
  "POST /api/jobs": (body) => addJobs(state, candidates(body.items), "pick", body.pin),
1725
1846
  "POST /api/jobs/remove": (_, id) => removeJob(state, id),
1726
1847
  "POST /api/jobs/retry": (_, id) => retryJob(state, id),
1848
+ "POST /api/jobs/answer": (body, id) => addNote(state, id, {
1849
+ kind: "answer",
1850
+ text: body.text
1851
+ }),
1852
+ "POST /api/jobs/followup": (body, id) => queueFollowup(state, id, body.task),
1853
+ "POST /api/jobs/review": (body, id) => queueReview(state, id, body.agent),
1727
1854
  "POST /api/jobs/move": (body, id) => moveJob(state, id, body.index),
1728
1855
  "GET /api/jobs/pr": (_, id) => readPrDetails(jobPr(id)),
1729
1856
  "POST /api/jobs/merge": async (_, id) => {
@@ -1850,7 +1977,7 @@ function send(response, type, body) {
1850
1977
  }
1851
1978
  //#endregion
1852
1979
  //#region package.json
1853
- var version = "0.2.0";
1980
+ var version = "0.2.1";
1854
1981
  //#endregion
1855
1982
  //#region src/backend/cli.ts
1856
1983
  const USAGE = `factory: works a queue of workwork candidates with headless coding agents
@@ -1860,15 +1987,28 @@ const USAGE = `factory: works a queue of workwork candidates with headless codin
1860
1987
  factory stop [--kill] turn it off; agents at work finish, and the next start records them;
1861
1988
  --kill stops them too and puts their jobs back in the queue
1862
1989
  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
1990
+ factory add <id>... [--repo DIR] [--agent NAME] [--model M] [--effort E] [--after JOB]
1991
+ queue workwork candidates from DIR (default: here), ahead of auto jobs;
1992
+ --after: start once that job is done
1993
+ factory review <job> --agent NAME
1994
+ queue a review of the job's PR by another agent, once the job is done
1995
+ factory wait <job> [--timeout MIN]
1996
+ block until the job is done, failed or blocked; exits 1 at the timeout
1997
+ factory tail follow what happens to every job, one line each
1865
1998
  factory remove <job> take a job out of the queue or the results, with its kept branches
1866
1999
  factory retry <job> queue a failed job again, with the same agent
2000
+ factory answer <job> "<text>" answer the question a blocked job asked, and queue it again
1867
2001
  factory auto on|off let auto mode keep the queue topped up from workwork, or not
1868
2002
  factory report [--all] the last run (or every run): outcome, agent, job, PR/issue link or log
1869
2003
  factory serve [--port 7040] [--host 127.0.0.1]
1870
2004
  a web page to watch, browse candidates, queue them and turn it on or off
1871
2005
 
2006
+ For the agent working a job (the job is $FACTORY_JOB, or --job ID):
2007
+ factory note "<text>" something worth knowing later
2008
+ factory ask "<question>" a decision for the user; the job waits for the answer once the agent ends
2009
+ factory result "<summary>" [--confidence low|medium|high] [--url LINK] [--followup "<task>"]...
2010
+ what the job delivered, and tasks that deserve their own job
2011
+
1872
2012
  Run options (defaults from settings.json):
1873
2013
  --agents claude,pi which registered agents work this run
1874
2014
  --limit N|none usage-limit hits per agent before it stops; none: wait for each reset
@@ -1898,6 +2038,14 @@ async function main() {
1898
2038
  agent: { type: "string" },
1899
2039
  all: { type: "boolean" },
1900
2040
  kill: { type: "boolean" },
2041
+ job: { type: "string" },
2042
+ after: { type: "string" },
2043
+ confidence: { type: "string" },
2044
+ url: { type: "string" },
2045
+ followup: {
2046
+ type: "string",
2047
+ multiple: true
2048
+ },
1901
2049
  port: {
1902
2050
  type: "string",
1903
2051
  default: "7040"
@@ -1928,6 +2076,18 @@ async function main() {
1928
2076
  }
1929
2077
  if (command === "retry") return args.forEach((id) => retryJob(STATE, id));
1930
2078
  if (command === "auto") return auto(args[0]);
2079
+ if (command === "answer") return addNote(STATE, args[0], {
2080
+ kind: "answer",
2081
+ text: text(args[1])
2082
+ });
2083
+ if (command === "note" || command === "ask") return addNote(STATE, ownJob(values.job), {
2084
+ kind: command,
2085
+ text: text(args[0])
2086
+ });
2087
+ if (command === "result") return result(args[0], values);
2088
+ if (command === "review") return review(args[0], values.agent);
2089
+ if (command === "wait") return wait(args[0], values.timeout);
2090
+ if (command === "tail") return tail();
1931
2091
  if (command === "add") return add(args, values);
1932
2092
  if (command === "serve") return void serve(STATE, count(values.port, "--port"), values.host);
1933
2093
  if (command === "stop") {
@@ -1985,12 +2145,102 @@ async function add(ids, values) {
1985
2145
  return item;
1986
2146
  });
1987
2147
  if (values.agent) agent(values.agent);
2148
+ 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
2149
  const pin = {
1989
2150
  agent: values.agent,
1990
2151
  model: values.model?.[0],
1991
- effort: values.effort?.[0]
2152
+ effort: values.effort?.[0],
2153
+ after: values.after
1992
2154
  };
1993
- for (const job of addJobs(STATE, items, "pick", pin)) console.log(`queued ${job.id}: ${job.item.label} ${job.item.id}`);
2155
+ const added = addJobs(STATE, items, "pick", pin);
2156
+ for (const job of added) console.log(`queued ${job.id}: ${job.item.label} ${job.item.id}`);
2157
+ for (const item of items.filter((item) => !added.some((job) => job.item === item))) console.log(`already queued: ${item.label} ${item.id}`);
2158
+ }
2159
+ function review(id, name) {
2160
+ if (!id || !name) throw new Error("factory review wants a job and --agent NAME");
2161
+ agent(name);
2162
+ const job = queueReview(STATE, id, name);
2163
+ console.log(job ? `queued ${job.id}: ${job.item.title}` : `a review of ${id} is already queued`);
2164
+ }
2165
+ const SETTLED = [
2166
+ "done",
2167
+ "failed",
2168
+ "blocked",
2169
+ "removed"
2170
+ ];
2171
+ async function wait(id, timeout) {
2172
+ if (!id) throw new Error("factory wait wants a job");
2173
+ const deadline = timeout ? Date.now() + count(timeout, "--timeout") * 6e4 : Infinity;
2174
+ for (;;) {
2175
+ const job = readJobs(STATE).find((job) => job.id === id);
2176
+ if (!job) throw new Error(`no job ${id}; factory status lists them`);
2177
+ if (SETTLED.includes(job.state)) {
2178
+ const said = job.notes.findLast((note) => note.kind === "result" || note.kind === "ask");
2179
+ return console.log([
2180
+ job.state,
2181
+ prUrl(job),
2182
+ said?.text
2183
+ ].filter(Boolean).join(" "));
2184
+ }
2185
+ if (Date.now() >= deadline) {
2186
+ console.log(`still ${job.state}`);
2187
+ process.exitCode = 1;
2188
+ return;
2189
+ }
2190
+ await new Promise((resolve) => setTimeout(resolve, 1e3));
2191
+ }
2192
+ }
2193
+ /** Prints each new event in jobs.jsonl as a line, until interrupted. */
2194
+ async function tail() {
2195
+ const path = join(STATE, "jobs.jsonl");
2196
+ let read = existsSync(path) ? statSync(path).size : 0;
2197
+ for (;;) {
2198
+ const size = existsSync(path) ? statSync(path).size : 0;
2199
+ if (size > read) {
2200
+ const text = readFileSync(path).subarray(read, size).toString("utf8");
2201
+ const end = text.lastIndexOf("\n") + 1;
2202
+ read += Buffer.byteLength(text.slice(0, end));
2203
+ const jobs = new Map(readJobs(STATE).map((job) => [job.id, job]));
2204
+ for (const line of text.slice(0, end).split("\n").filter(Boolean)) console.log(eventLine(JSON.parse(line), jobs));
2205
+ }
2206
+ await new Promise((resolve) => setTimeout(resolve, 500));
2207
+ }
2208
+ }
2209
+ const PAST = {
2210
+ remove: "removed",
2211
+ requeue: "back in the queue",
2212
+ retry: "queued for a retry"
2213
+ };
2214
+ function eventLine(event, jobs) {
2215
+ const id = event.type === "add" ? event.job.id : event.id;
2216
+ const job = jobs.get(id);
2217
+ 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];
2218
+ return `${clock(event.at)} ${id} ${job ? basename(job.item.repo) : ""} ${what}`.trimEnd();
2219
+ }
2220
+ /** The job an agent works, which factory hands it in the environment. */
2221
+ function ownJob(job = process.env.FACTORY_JOB) {
2222
+ if (!job) throw new Error("runs inside a job, which sets FACTORY_JOB; elsewhere pass --job ID");
2223
+ return job;
2224
+ }
2225
+ function text(value) {
2226
+ if (!value?.trim()) throw new Error("wants the text as one argument, in quotes: \"…\"");
2227
+ return value;
2228
+ }
2229
+ const CONFIDENCES = [
2230
+ "low",
2231
+ "medium",
2232
+ "high"
2233
+ ];
2234
+ function result(summary, values) {
2235
+ const { confidence, url, followup = [] } = values;
2236
+ if (confidence !== void 0 && !CONFIDENCES.includes(confidence)) throw new Error(`--confidence wants ${CONFIDENCES.join(", ")}, got ${confidence}`);
2237
+ addNote(STATE, ownJob(values.job), {
2238
+ kind: "result",
2239
+ text: text(summary),
2240
+ confidence,
2241
+ url,
2242
+ followups: followup
2243
+ });
1994
2244
  }
1995
2245
  function auto(onOrOff) {
1996
2246
  if (onOrOff !== "on" && onOrOff !== "off") throw new Error("factory auto wants on or off");
@@ -2018,7 +2268,11 @@ function status() {
2018
2268
  console.log(` ${worker.agent.padEnd(7)} ${worker.state.padEnd(9)} ${what} (since ${clock(worker.since)}, limits hit ${worker.limits})`);
2019
2269
  }
2020
2270
  }
2021
- const queued = readJobs(STATE).filter((job) => job.state === "queued");
2271
+ const jobs = readJobs(STATE);
2272
+ const blocked = jobs.filter((job) => job.state === "blocked");
2273
+ if (blocked.length) console.log(`waiting for your answer: ${blocked.length}`);
2274
+ for (const job of blocked) console.log(` ${line(job)}\n ${job.notes.findLast((note) => note.kind === "ask").text}`);
2275
+ const queued = jobs.filter((job) => job.state === "queued");
2022
2276
  console.log(`queue: ${queued.length}`);
2023
2277
  for (const job of queued.slice(0, 10)) console.log(` ${line(job)}`);
2024
2278
  if (queued.length > 10) console.log(` … ${queued.length - 10} more`);
@@ -2040,6 +2294,8 @@ function report(all, run) {
2040
2294
  if (attempt.run !== shown[i - 1]?.attempt.run) lines.push(`run ${attempt.run}`);
2041
2295
  lines.push(` ${attempt.outcome.padEnd(7)} ${attempt.agent.padEnd(6)} ${line(job)}`);
2042
2296
  const details = [attempt.url ?? attempt.log];
2297
+ const number = job.attempts.indexOf(attempt) + 1;
2298
+ 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
2299
  if (attempt.error) details.push(attempt.error.split("\n")[0]);
2044
2300
  if (attempt.branch) details.push(`unpushed work kept on branch ${attempt.branch}`);
2045
2301
  lines.push(...details.map((detail) => ` ${detail}`));