@operad-io/dev 0.1.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +1 -1
  2. package/dev.schema.json +53 -31
  3. package/package.json +9 -6
  4. package/skills/git/SKILL.md +70 -0
  5. package/skills/setup/SKILL.md +74 -0
  6. package/src/access-grants.ts +59 -0
  7. package/src/actions.ts +81 -17
  8. package/src/announce.ts +134 -17
  9. package/src/chat.ts +586 -36
  10. package/src/check-host.ts +111 -0
  11. package/src/check-load.ts +67 -0
  12. package/src/check.ts +45 -22
  13. package/src/clock.ts +29 -0
  14. package/src/clone-chat.ts +32 -0
  15. package/src/commands.ts +103 -34
  16. package/src/create-actions.ts +210 -0
  17. package/src/flaky.ts +45 -29
  18. package/src/flow-compiler.ts +156 -69
  19. package/src/followups.ts +20 -22
  20. package/src/gate.ts +254 -88
  21. package/src/git-state.ts +155 -0
  22. package/src/git.ts +92 -4
  23. package/src/index.ts +52 -6
  24. package/src/integrate.ts +162 -62
  25. package/src/integration-queue.ts +23 -10
  26. package/src/lock.ts +101 -5
  27. package/src/mainclone.ts +61 -5
  28. package/src/model-error.ts +70 -0
  29. package/src/model.ts +61 -35
  30. package/src/notcode.ts +51 -15
  31. package/src/places.ts +2 -2
  32. package/src/prepare.ts +7 -2
  33. package/src/project-flows.ts +135 -4
  34. package/src/project-settings.ts +110 -3
  35. package/src/register-requests.ts +121 -0
  36. package/src/repository-key.ts +4 -0
  37. package/src/rerun.ts +23 -7
  38. package/src/retry.ts +2 -12
  39. package/src/rework.ts +8 -0
  40. package/src/schedule.ts +342 -93
  41. package/src/service.ts +241 -185
  42. package/src/settings.ts +409 -65
  43. package/src/setup-git.ts +366 -0
  44. package/src/standing.ts +23 -6
  45. package/src/step-edge.ts +22 -0
  46. package/src/steps.ts +945 -179
  47. package/src/throughput.ts +4 -21
  48. package/src/timeouts.ts +10 -2
  49. package/src/views.ts +325 -86
  50. package/src/waits.ts +59 -0
package/src/gate.ts CHANGED
@@ -18,6 +18,7 @@ import {
18
18
  writeFileSync,
19
19
  } from "node:fs";
20
20
  import { dirname, join, resolve } from "node:path";
21
+ import { checkEnvironment } from "./check-host.ts";
21
22
  import { checkPlaces } from "./places.ts";
22
23
  import { runPrepare, stepOptions } from "./prepare.ts";
23
24
  import { runCheckCommands } from "./rerun.ts";
@@ -30,7 +31,7 @@ import {
30
31
  strictestFixLevel,
31
32
  type Grading,
32
33
  } from "@operad-io/review-grading";
33
- import { isRecord, messageOf, type PluginStepResult, type StepContext, type StepRunOptions } from "@operad-io/engine";
34
+ import { isRecord, messageOf, type PluginStepResult, type StepContext } from "@operad-io/engine";
34
35
  import { appendRework, DevError, type PendingReviewRun, type ReviewFinding, type ReviewNote, type ReviewNotes, type ReviewRecord, type ReviewResult, type ReviewRunRecord, type ReviewRunReason, type ReviewRunRole, type ReworkRecord, type TaskAttempt, type TaskData, type VerifyRecord, type VerifyResult } from "./model.ts";
35
36
  import {
36
37
  assertInsideRepository,
@@ -43,12 +44,14 @@ import {
43
44
  } from "./settings.ts";
44
45
  import { reviewWaitKind } from "./waits.ts";
45
46
  import { appendChange } from "./model.ts";
47
+ import { reviewTestHooks } from "./clock.ts";
48
+ export { reviewTestHooks };
49
+ import { modelErrorIn, modelWaitsForPerson, type ModelErrorInfo } from "./model-error.ts";
50
+ import { baseName, commitOf, fetchRemote, git } from "./git.ts";
46
51
  import {
47
52
  briefText,
48
53
  cancelledStep,
49
54
  claimAllows,
50
- git,
51
- headOf,
52
55
  isRegistered,
53
56
  saveTaskGuarded,
54
57
  taskContext,
@@ -57,7 +60,6 @@ import {
57
60
  } from "./steps.ts";
58
61
  import {
59
62
  addedLinesOf,
60
- filePatches,
61
63
  incrementOf,
62
64
  type Increment,
63
65
  fixedCount,
@@ -67,6 +69,7 @@ import {
67
69
  reviewSettingsOf,
68
70
  previousLine,
69
71
  previousReview,
72
+ severityRank,
70
73
  type LineMap,
71
74
  type PreviousFinding,
72
75
  type ReviewHistoryEntry,
@@ -96,20 +99,10 @@ export function branchHead(
96
99
  workName: string,
97
100
  ): string | undefined {
98
101
  const branch = workBranch(workName);
99
- const local = git(repositoryPath, [
100
- "rev-parse",
101
- "--verify",
102
- "--quiet",
103
- `${branch}^{commit}`,
104
- ]);
105
- if (local.ok && local.out.length > 0) return local.out;
106
- const remote = git(repositoryPath, [
107
- "rev-parse",
108
- "--verify",
109
- "--quiet",
110
- `refs/remotes/${settings.remote}/${branch}^{commit}`,
111
- ]);
112
- return remote.ok && remote.out.length > 0 ? remote.out : undefined;
102
+ const local = commitOf(repositoryPath, branch);
103
+ if (local !== undefined) return local;
104
+ if (settings.remote === undefined) return undefined;
105
+ return commitOf(repositoryPath, `refs/remotes/${settings.remote}/${branch}`);
113
106
  }
114
107
 
115
108
  /** The commit the task branched from: the merge base of main and the commit. */
@@ -118,8 +111,7 @@ function mergeBase(
118
111
  settings: DevSettings,
119
112
  sha: string,
120
113
  ): string | undefined {
121
- const main = `${settings.remote}/${settings.mainBranch}`;
122
- const result = git(repositoryPath, ["merge-base", main, sha]);
114
+ const result = git(repositoryPath, ["merge-base", baseName(settings), sha]);
123
115
  return result.ok && result.out.length > 0 ? result.out : undefined;
124
116
  }
125
117
 
@@ -188,11 +180,7 @@ export async function runVerify(
188
180
  const task = context.task;
189
181
  const attempt = attemptOf(ctx, task);
190
182
  const previous = attempt.verify;
191
- git(context.repository.path, [
192
- "fetch",
193
- "--prune",
194
- context.settings.remote,
195
- ]);
183
+ fetchRemote(context.repository.path, context.settings);
196
184
  const sha = branchHead(
197
185
  context.repository.path,
198
186
  context.settings,
@@ -262,7 +250,9 @@ export async function runVerify(
262
250
 
263
251
  if (sha === undefined) {
264
252
  return failed(
265
- `no branch "${workBranch(context.workName)}" or "${context.settings.remote}/${workBranch(context.workName)}" to verify`,
253
+ context.settings.remote === undefined
254
+ ? `no branch "${workBranch(context.workName)}" to verify`
255
+ : `no branch "${workBranch(context.workName)}" or "${context.settings.remote}/${workBranch(context.workName)}" to verify`,
266
256
  );
267
257
  }
268
258
  const prepared = recreateGateWorktree(
@@ -310,6 +300,7 @@ export async function runVerify(
310
300
  ...(context.settings.gate.stallSecs === undefined
311
301
  ? {}
312
302
  : { stallSecs: context.settings.gate.stallSecs }),
303
+ env: checkEnvironment(ctx.settings),
313
304
  },
314
305
  commands,
315
306
  stepOptions(ctx, path),
@@ -320,6 +311,7 @@ export async function runVerify(
320
311
  flakyLimit: context.settings.check.flakyLoadLimit,
321
312
  signal: ctx.signal,
322
313
  log: (text) => ctx.logger.write(text),
314
+ logPath: ctx.logger.path,
323
315
  });
324
316
  if (steady.cancelled === true) return cancelledStep();
325
317
  const { checked } = steady;
@@ -433,6 +425,8 @@ interface FileDiff {
433
425
  /** Why the file is left out of the prompt, or undefined when it is in it. */
434
426
  readonly leftOut: string | undefined;
435
427
  readonly binary: boolean;
428
+ /** True when the task deletes the whole file. */
429
+ readonly deleted: boolean;
436
430
  }
437
431
 
438
432
  interface Attributes {
@@ -608,6 +602,7 @@ function diffsOf(
608
602
  bytes: Buffer.byteLength(patch, "utf8"),
609
603
  leftOut: undefined,
610
604
  binary,
605
+ deleted: change?.status === "deleted",
611
606
  });
612
607
  }
613
608
  return files;
@@ -708,13 +703,6 @@ export function leftOutLine(file: FileDiff): string {
708
703
  return `- ${file.path} (+${file.added}/-${file.removed}) — ${file.leftOut ?? "left out"}`;
709
704
  }
710
705
 
711
- function severityRank(severity: string): number {
712
- if (severity === "critical") return 3;
713
- if (severity === "major") return 2;
714
- if (severity === "minor") return 1;
715
- return 0;
716
- }
717
-
718
706
  function levelRank(level: FixLevel): number {
719
707
  return severityRank(level);
720
708
  }
@@ -723,6 +711,85 @@ function atOrAbove(severity: string, level: FixLevel): boolean {
723
711
  return severityRank(severity) >= levelRank(level);
724
712
  }
725
713
 
714
+ /** The line `dev/work` writes into the worker's answer for a main-checkout change. */
715
+ const MAIN_CHECKOUT_CHANGE = "changed the main checkout:";
716
+
717
+ /** An entry of that line that names the main checkout's HEAD moving, not a file. */
718
+ const MOVED_HEAD = / \((?:moved|switched) [^)]*\)$/;
719
+
720
+ /**
721
+ * The paths an attempt's work changed outside its worktree, read from the line
722
+ * the work step wrote into the worker's answer (T-0646).
723
+ */
724
+ function mainCheckoutChanges(attempt: TaskAttempt): readonly string[] {
725
+ // An attempt that works in the main clone changes it by design.
726
+ if (attempt.inPlace === true) return [];
727
+ const message = attempt.finalMessage;
728
+ if (typeof message !== "string") return [];
729
+ const paths: string[] = [];
730
+ for (const line of message.split("\n")) {
731
+ const said = line.trim();
732
+ if (!said.startsWith(`${MAIN_CHECKOUT_CHANGE} `)) continue;
733
+ for (const path of said.slice(MAIN_CHECKOUT_CHANGE.length + 1).split(", ")) {
734
+ const named = path.trim();
735
+ if (named.length > 0 && !paths.includes(named)) paths.push(named);
736
+ }
737
+ }
738
+ return paths;
739
+ }
740
+
741
+ /**
742
+ * The major findings dev adds itself for an attempt whose work changed the main
743
+ * checkout: one per named file that no report already blocks on. They are
744
+ * added whatever the reviewer reported, so such an attempt carries the major
745
+ * finding without asking the reviewer for it; the work must make those changes
746
+ * in its own worktree instead.
747
+ */
748
+ function mainCheckoutFindings(
749
+ paths: readonly string[],
750
+ reported: readonly ReviewFinding[],
751
+ ): ReviewFinding[] {
752
+ const blocked = new Set(
753
+ reported
754
+ .filter(
755
+ (finding) =>
756
+ finding.preExisting !== true &&
757
+ finding.pre_existing !== true &&
758
+ atOrAbove(finding.severity, "major"),
759
+ )
760
+ .map((finding) => normalizePath(finding.file)),
761
+ );
762
+ return paths
763
+ .filter((path) => !blocked.has(normalizePath(path)))
764
+ .map((path) =>
765
+ MOVED_HEAD.test(path)
766
+ ? {
767
+ file: path,
768
+ line: 0,
769
+ severity: "major",
770
+ summary: "the work committed in the main checkout",
771
+ trigger: "the work must commit on its own branch in its own worktree instead",
772
+ }
773
+ : {
774
+ file: path,
775
+ line: 0,
776
+ severity: "major",
777
+ summary: "the work changed this file in the main checkout",
778
+ trigger: "the work must make this change in its own worktree instead",
779
+ },
780
+ );
781
+ }
782
+
783
+ /** The counted findings of a failed review, as the task's history and brief show them. */
784
+ function describeFindings(findings: readonly ReviewFinding[]): string {
785
+ return findings
786
+ .map(
787
+ (finding) =>
788
+ `${finding.file}:${finding.line} ${finding.severity}: ${finding.summary}`,
789
+ )
790
+ .join("; ");
791
+ }
792
+
726
793
  interface FindingShape {
727
794
  readonly file: string;
728
795
  readonly line: number;
@@ -885,12 +952,22 @@ function reviewPrompt(
885
952
  prepare: readonly string[] = [],
886
953
  rework?: ReworkPrompt,
887
954
  checks: readonly string[] = [],
955
+ mainCheckout: readonly string[] = [],
888
956
  ): string {
889
957
  const pieces: string[] = [
890
958
  `Review the changes of task ${task.title}.`,
891
959
  "## Brief",
892
960
  briefText(task).trim(),
893
961
  ];
962
+ if (mainCheckout.length > 0) {
963
+ pieces.push(
964
+ "## The main checkout",
965
+ [
966
+ `The work of this attempt left this line in its answer: \`${MAIN_CHECKOUT_CHANGE} ${mainCheckout.join(", ")}\`. It means the work changed files outside its worktree, in the main checkout, and must make those changes in its own worktree instead.`,
967
+ "This is a major finding: report it as a major finding on each of those files.",
968
+ ].join("\n"),
969
+ );
970
+ }
894
971
  const graded = gradingLines(grading);
895
972
  if (graded.length > 0) pieces.push("## Grading", graded.join("\n"));
896
973
  if (rework !== undefined) {
@@ -924,6 +1001,25 @@ function reviewPrompt(
924
1001
  prepared.included.map((file) => file.patch).join(""),
925
1002
  );
926
1003
  }
1004
+ const deleted = prepared.files.filter((file) => file.deleted);
1005
+ if (deleted.length > 0) {
1006
+ if (rework === undefined) {
1007
+ pieces.push(
1008
+ "## Deleted files",
1009
+ `A file this change deletes whole is listed by name with its line count, not by its removed lines. Read it from the base commit with \`git show ${base}:<path>\`.`,
1010
+ );
1011
+ } else {
1012
+ // The increment holds hunks, and a whole deletion has none: list the
1013
+ // deleted files so a rework round still learns what went.
1014
+ pieces.push(
1015
+ "## Deleted files",
1016
+ [
1017
+ `These files are deleted whole. Each is listed by name with its line count, not by its removed lines; read one from the base commit with \`git show ${base}:<path>\`:`,
1018
+ ...deleted.map((file) => `- ${file.path} (${file.removed} lines)`),
1019
+ ].join("\n"),
1020
+ );
1021
+ }
1022
+ }
927
1023
  if (prepared.leftOut.length > 0) {
928
1024
  pieces.push(
929
1025
  "## Files left out of the diff",
@@ -977,6 +1073,8 @@ interface ReviewerCall {
977
1073
  | "failed";
978
1074
  readonly report?: Report | undefined;
979
1075
  readonly message: string;
1076
+ /** What the model error said, when the run ended on one. */
1077
+ readonly modelError?: ModelErrorInfo | undefined;
980
1078
  }
981
1079
 
982
1080
  /** The largest review report the gate reads; anything bigger is not a report. */
@@ -1144,6 +1242,7 @@ function writeReviewOutcome(path: string, call: ReviewerCall): void {
1144
1242
  outcome: call.outcome,
1145
1243
  message: call.message,
1146
1244
  ...(call.report === undefined ? {} : { report: call.report }),
1245
+ ...(call.modelError === undefined ? {} : { modelError: call.modelError }),
1147
1246
  }),
1148
1247
  );
1149
1248
  } catch {
@@ -1180,8 +1279,13 @@ function readReviewOutcome(resultPath: string): ReviewerCall | undefined {
1180
1279
  return undefined;
1181
1280
  }
1182
1281
  const message = typeof value.message === "string" ? value.message : "";
1282
+ const modelError = modelErrorIn(value);
1183
1283
  if (outcome !== "report") {
1184
- return { outcome: outcome as ReviewerCall["outcome"], message };
1284
+ return {
1285
+ outcome: outcome as ReviewerCall["outcome"],
1286
+ message,
1287
+ ...(modelError === undefined ? {} : { modelError }),
1288
+ };
1185
1289
  }
1186
1290
  const report = parseReport(JSON.stringify(value.report));
1187
1291
  if (report === undefined) return undefined;
@@ -1447,7 +1551,12 @@ async function callReviewerOnce(
1447
1551
  : `the reviewer ended "${outcome.result}"`;
1448
1552
  const kind = failureOutcome(data, message);
1449
1553
  if (kind === "timed_out" || kind === "unavailable") {
1450
- return { outcome: kind, message };
1554
+ const modelError = modelErrorIn(data);
1555
+ return {
1556
+ outcome: kind,
1557
+ message,
1558
+ ...(modelError === undefined ? {} : { modelError }),
1559
+ };
1451
1560
  }
1452
1561
  }
1453
1562
  return { outcome: "no_report", message: lastMessageOf(outcome) };
@@ -1653,27 +1762,6 @@ function reviewMessage(
1653
1762
  return parts.join("; ");
1654
1763
  }
1655
1764
 
1656
- /** Test-only hooks: a child process crashes at a named point in a review. */
1657
- export const reviewTestHooks: {
1658
- now?: (() => number) | undefined;
1659
- /** Waits instead of the real timer, so a test can move the clock. */
1660
- wait?:
1661
- | ((ms: number, signal: AbortSignal | undefined) => Promise<boolean>)
1662
- | undefined;
1663
- beforeRun?:
1664
- | ((run: { readonly role: ReviewRunRole; readonly reason: ReviewRunReason }) => void)
1665
- | undefined;
1666
- /** Runs after the reviewer answered and before its run is written. */
1667
- afterCall?:
1668
- | ((run: { readonly role: ReviewRunRole; readonly reason: ReviewRunReason }) => void)
1669
- | undefined;
1670
- afterRunSaved?: ((run: ReviewRunRecord) => void) | undefined;
1671
- /** Runs after the reviewer wrote a valid report, before the store copy. */
1672
- beforeCopy?: (() => void) | undefined;
1673
- /** Runs while a workspace report is open, before the folders are checked again. */
1674
- afterOpen?: (() => void) | undefined;
1675
- } = {};
1676
-
1677
1765
  /** The task's notes with this round's findings that did not fail it. */
1678
1766
  function notesOf(
1679
1767
  task: TaskData,
@@ -1840,6 +1928,9 @@ export async function runReview(
1840
1928
  if (!claimAllows(ctx, context.task)) return cancelledStep();
1841
1929
  const task = context.task;
1842
1930
  const attempt = attemptOf(ctx, task);
1931
+ // The attempt being reviewed changed the main checkout: the work must make
1932
+ // those changes in its own worktree instead, and dev holds the review to it.
1933
+ const mainPaths = mainCheckoutChanges(attempt);
1843
1934
  const reviews =
1844
1935
  typeof attempt.reviews === "number" ? attempt.reviews : 0;
1845
1936
  const round = reviews + 1;
@@ -1924,8 +2015,14 @@ export async function runReview(
1924
2015
  typeof sha === "string" && recorded.sha === undefined
1925
2016
  ? { ...recorded, sha }
1926
2017
  : recorded;
1927
- const record: ReviewRecord =
2018
+ const withSettings: ReviewRecord =
1928
2019
  used === undefined ? stamped : { ...stamped, reviewSettings: used };
2020
+ // The fix level in force for this round, so a later view shows it and not
2021
+ // whatever dev.json says by then.
2022
+ const record: ReviewRecord = {
2023
+ ...withSettings,
2024
+ fixLevel: context.settings.reviewer.fixLevel,
2025
+ };
1929
2026
  const waiting =
1930
2027
  record.decision !== "findings" && record.result !== "fail";
1931
2028
  const written = await saveTaskGuarded(
@@ -2006,11 +2103,7 @@ export async function runReview(
2006
2103
  });
2007
2104
  }
2008
2105
 
2009
- git(context.repository.path, [
2010
- "fetch",
2011
- "--prune",
2012
- context.settings.remote,
2013
- ]);
2106
+ fetchRemote(context.repository.path, context.settings);
2014
2107
  const base = mergeBase(context.repository.path, context.settings, sha);
2015
2108
  if (base === undefined) {
2016
2109
  const record: ReviewRecord = {
@@ -2019,7 +2112,7 @@ export async function runReview(
2019
2112
  sha,
2020
2113
  round,
2021
2114
  reason: "no_merge_base",
2022
- message: `cannot find the merge base of ${context.settings.remote}/${context.settings.mainBranch} and ${sha}`,
2115
+ message: `cannot find the merge base of ${baseName(context.settings)} and ${sha}`,
2023
2116
  };
2024
2117
  return finish(record, "failed", {
2025
2118
  reason: "no_merge_base",
@@ -2228,6 +2321,65 @@ export async function runReview(
2228
2321
  // review to ask a model for. A change made only of left-out files still runs
2229
2322
  // the reviewer: the list is what tells it those files exist.
2230
2323
  if (prepared.files.length === 0) {
2324
+ // The work never reported done and the branch has no change: the empty
2325
+ // diff is the failed work, not a finished task with nothing to add.
2326
+ if (attempt.workDone !== true) {
2327
+ const findings: ReviewFinding[] = [
2328
+ {
2329
+ file: "(branch)",
2330
+ line: 0,
2331
+ severity: "major",
2332
+ summary: "the branch has no change against main and the work did not finish",
2333
+ trigger: "the work stopped before it committed anything; do the work again",
2334
+ },
2335
+ ];
2336
+ const message = describeFindings(findings);
2337
+ const record: ReviewRecord = {
2338
+ result: "findings",
2339
+ decision: "findings",
2340
+ run: ctx.runId,
2341
+ sha,
2342
+ round,
2343
+ runs: [],
2344
+ findings,
2345
+ verified: [],
2346
+ reason: "findings",
2347
+ message,
2348
+ };
2349
+ return finish(record, "fail", {
2350
+ reason: "findings",
2351
+ counted: 1,
2352
+ message,
2353
+ leftOut: prepared.leftOut.map(leftOutLine),
2354
+ });
2355
+ }
2356
+ // A main-checkout change fails even without a diff: no reviewer is asked,
2357
+ // and dev adds the major finding itself.
2358
+ const findings = mainCheckoutFindings(mainPaths, []);
2359
+ if (findings.length > 0) {
2360
+ const counted = findings.filter((finding) =>
2361
+ atOrAbove(finding.severity, context.settings.reviewer.fixLevel),
2362
+ ).length;
2363
+ const message = describeFindings(findings);
2364
+ const record: ReviewRecord = {
2365
+ result: "findings",
2366
+ decision: "findings",
2367
+ run: ctx.runId,
2368
+ sha,
2369
+ round,
2370
+ runs: [],
2371
+ findings,
2372
+ verified: [],
2373
+ reason: "findings",
2374
+ message,
2375
+ };
2376
+ return finish(record, "fail", {
2377
+ reason: "findings",
2378
+ counted,
2379
+ message,
2380
+ leftOut: prepared.leftOut.map(leftOutLine),
2381
+ });
2382
+ }
2231
2383
  const record: ReviewRecord = {
2232
2384
  result: "passed",
2233
2385
  decision: "passed",
@@ -2350,6 +2502,7 @@ export async function runReview(
2350
2502
  context.settings.prepare,
2351
2503
  reworkPrompt,
2352
2504
  verifiedCommands(attempt.verify),
2505
+ mainPaths,
2353
2506
  );
2354
2507
  // A resumed call asks with the prompt it recorded, not one built again.
2355
2508
  const recorded =
@@ -2489,6 +2642,7 @@ export async function runReview(
2489
2642
  outcome: call.outcome,
2490
2643
  run: ctx.runId,
2491
2644
  ...(call.message.length === 0 ? {} : { message: call.message }),
2645
+ ...(call.modelError === undefined ? {} : { modelError: call.modelError }),
2492
2646
  };
2493
2647
  }
2494
2648
  runs.push(runRecord);
@@ -2512,31 +2666,42 @@ export async function runReview(
2512
2666
  if (!written) return cancelledStep();
2513
2667
  reviewTestHooks.afterRunSaved?.(runRecord);
2514
2668
  next = nextReviewAction(runs, settings.fallback);
2669
+ if (
2670
+ next === "wait-unavailable" &&
2671
+ runRecord.modelError !== undefined &&
2672
+ runRecord.modelError !== null &&
2673
+ modelWaitsForPerson(runRecord.modelError)
2674
+ ) {
2675
+ // A usage limit, or a wait past half an hour, is not an outage dev can
2676
+ // wait out: the task goes to a person who can change the worker.
2677
+ next = "wait-exhausted";
2678
+ }
2515
2679
  }
2516
2680
 
2517
2681
  const last = runs[runs.length - 1];
2518
2682
  if (next === "done" && last !== undefined) {
2519
- const counted = last.counted ?? 0;
2520
- const decision = counted === 0 ? "passed" : "findings";
2521
- const stepResult = decision === "passed" ? "pass" : "fail";
2522
- const result: ReviewResult = decision;
2523
- const findings = last.findings ?? [];
2683
+ // A reviewer that passed an attempt with a main-checkout change cannot let
2684
+ // it through: dev adds the major finding itself.
2685
+ const reported = last.findings ?? [];
2686
+ const findings = [
2687
+ ...reported,
2688
+ ...mainCheckoutFindings(mainPaths, reported),
2689
+ ];
2524
2690
  const countedFindings = findings.filter(
2525
2691
  (finding) =>
2526
2692
  finding.preExisting !== true &&
2527
2693
  finding.pre_existing !== true &&
2528
2694
  atOrAbove(finding.severity, settings.fixLevel),
2529
2695
  );
2696
+ const counted = countedFindings.length;
2697
+ const decision = counted === 0 ? "passed" : "findings";
2698
+ const stepResult = decision === "passed" ? "pass" : "fail";
2699
+ const result: ReviewResult = decision;
2530
2700
  const preExistingCount = findings.filter(
2531
2701
  (finding) =>
2532
2702
  finding.preExisting === true || finding.pre_existing === true,
2533
2703
  ).length;
2534
- const findingsMessage = countedFindings
2535
- .map(
2536
- (finding) =>
2537
- `${finding.file}:${finding.line} ${finding.severity}: ${finding.summary}`,
2538
- )
2539
- .join("; ");
2704
+ const findingsMessage = describeFindings(countedFindings);
2540
2705
  const reworkLine =
2541
2706
  rework === undefined
2542
2707
  ? ""
@@ -2594,7 +2759,14 @@ export async function runReview(
2594
2759
  const why = exhausted
2595
2760
  ? "review did not finish (model unavailable)"
2596
2761
  : "review did not finish (out of time)";
2597
- const message = [reviewMessage("passed", runs), why]
2762
+ const reset =
2763
+ exhausted && last?.modelError?.retryAfterSecs !== undefined
2764
+ ? `; the model can serve again at ${new Date(
2765
+ (last.modelError.reportedAt ?? clock()) +
2766
+ last.modelError.retryAfterSecs * 1000,
2767
+ ).toISOString()}`
2768
+ : "";
2769
+ const message = [reviewMessage("passed", runs), why + reset]
2598
2770
  .filter((piece) => piece.length > 0)
2599
2771
  .join("; ");
2600
2772
  const timedOut = !exhausted && last?.outcome === "timed_out";
@@ -2622,9 +2794,14 @@ export async function runReview(
2622
2794
  const waits = runs.filter(
2623
2795
  (run) => run.role === "primary" && run.outcome === "unavailable",
2624
2796
  ).length;
2797
+ const retryAfterSecs = last?.modelError?.retryAfterSecs;
2625
2798
  const retryAt =
2626
- (reviewTestHooks.now?.() ?? Date.now()) +
2627
- reviewWaitMs(settings.retrySecs, waits);
2799
+ (retryAfterSecs === undefined
2800
+ ? clock()
2801
+ : last?.modelError?.reportedAt ?? clock()) +
2802
+ (retryAfterSecs === undefined
2803
+ ? reviewWaitMs(settings.retrySecs, waits)
2804
+ : retryAfterSecs * 1000);
2628
2805
  const message = reviewMessage("unavailable", runs, retryAt);
2629
2806
  const record: ReviewRecord = {
2630
2807
  result: "unavailable",
@@ -2937,14 +3114,3 @@ export async function runRework(
2937
3114
  };
2938
3115
  }
2939
3116
 
2940
- /** The recorded verified commit of a task, for a caller that wants to look. */
2941
- export function verifiedCommitOf(task: TaskData): string | undefined {
2942
- const sha = task.attempt?.verified;
2943
- return typeof sha === "string" && sha.length > 0 ? sha : undefined;
2944
- }
2945
-
2946
- /** The branch head a task's worktree is on, for tests and views. */
2947
- export function taskHead(path: string): string | undefined {
2948
- return headOf(path);
2949
- }
2950
-