wachiman 1.14.0 → 1.16.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 (72) hide show
  1. package/README.md +2 -1
  2. package/package.json +4 -2
  3. package/src/accounts/commands.ts +3 -2
  4. package/src/accounts/confirm.ts +63 -4
  5. package/src/accounts/profile-list.ts +24 -0
  6. package/src/cli.ts +161 -142
  7. package/src/config.ts +19 -6
  8. package/src/connectors/local-git.ts +47 -0
  9. package/src/doctor/checks.ts +106 -1
  10. package/src/doctor/run.ts +9 -1
  11. package/src/engine.ts +294 -97
  12. package/src/help.ts +637 -0
  13. package/src/init/args.ts +5 -2
  14. package/src/init/github-login.ts +13 -0
  15. package/src/init/options.ts +31 -3
  16. package/src/init/prompt.ts +46 -15
  17. package/src/init/run.ts +238 -32
  18. package/src/init/settings.ts +127 -0
  19. package/src/init/write.ts +16 -102
  20. package/src/ledger-read/command.ts +106 -0
  21. package/src/ledger-read/credentials.ts +103 -0
  22. package/src/ledger-read/files.ts +141 -0
  23. package/src/ledger-read/pull.ts +69 -0
  24. package/src/ledger-read/rows.ts +60 -0
  25. package/src/ledger-read/types.ts +127 -0
  26. package/src/ledger.ts +34 -18
  27. package/src/local-ask.ts +5 -4
  28. package/src/local.ts +55 -10
  29. package/src/loop/command.ts +5 -5
  30. package/src/pr-template/adopt.ts +434 -0
  31. package/src/pr-template/catalog.ts +429 -0
  32. package/src/pr-template/check-run.ts +328 -0
  33. package/src/pr-template/check.ts +338 -0
  34. package/src/pr-template/describe.ts +368 -0
  35. package/src/pr-template/format.ts +399 -0
  36. package/src/pr-template/gh.ts +159 -0
  37. package/src/pr-template/install.ts +288 -0
  38. package/src/pr-template/legacy-guides.ts +50 -0
  39. package/src/pr-template/pr-risk.ts +426 -0
  40. package/src/pr-template/render.ts +302 -0
  41. package/src/pr-template/resolve.ts +65 -0
  42. package/src/pr-template/run.ts +88 -0
  43. package/src/pr-template/strip.ts +171 -0
  44. package/src/pr-template/write.ts +139 -0
  45. package/src/providers/claude-code.ts +7 -8
  46. package/src/providers/codex.ts +4 -4
  47. package/src/providers/spawn.ts +95 -0
  48. package/src/providers/types.ts +5 -2
  49. package/src/repo-settings.ts +159 -0
  50. package/src/setup/prompt.ts +49 -33
  51. package/src/setup/run.ts +58 -16
  52. package/src/sinks/common.ts +10 -3
  53. package/src/sinks/markdown.ts +3 -2
  54. package/src/sinks/terminal.ts +3 -2
  55. package/src/spend/report.ts +349 -0
  56. package/src/status/command.ts +150 -36
  57. package/src/store/command.ts +20 -3
  58. package/src/store/html.ts +205 -8
  59. package/src/store/index.ts +64 -5
  60. package/src/store/rows.ts +103 -0
  61. package/src/upgrade/command.ts +286 -0
  62. package/src/upgrade/pin.ts +112 -0
  63. package/src/wizard/interactive.ts +11 -0
  64. package/src/wizard/launcher.ts +340 -0
  65. package/src/wizard/load.ts +30 -0
  66. package/src/wizard/mascot.ts +228 -0
  67. package/src/wizard/prompt-fancy.ts +135 -0
  68. package/src/wizard/prompt.ts +108 -61
  69. package/src/wizard/style.ts +93 -13
  70. package/src/wizard/tone.ts +11 -0
  71. package/src/wizard/ui.ts +80 -0
  72. package/templates/pr/sections.json +163 -0
package/src/engine.ts CHANGED
@@ -33,7 +33,8 @@ import { closeSync, mkdtempSync, openSync, readFileSync, readSync, realpathSync,
33
33
  import { tmpdir } from "node:os";
34
34
  import { join } from "node:path";
35
35
  import { estimateReview, sizeGate, suggestedBudget, type ReviewEstimate } from "./estimate";
36
- import { isAutomatic, operatorProblem, type EngineConfig, type LimitMode, type Trigger } from "./config";
36
+ import { UsageError, isAutomatic, operatorProblem, type EngineConfig, type LimitMode, type Trigger } from "./config";
37
+ import { SETTINGS_PATH, applyRepoSettings, fileSettingsNote, ignoredSettingsNote, parseRepoSettings, wantsRepoSettings, type RepoSettings } from "./repo-settings";
37
38
  import {
38
39
  LedgerBadAnswer,
39
40
  LedgerNotPermitted,
@@ -1676,6 +1677,54 @@ export function readBaseInstructions(
1676
1677
  return instructionsFrom(sha, (path) => fileAtBase(gh, repo, path, sha), warn);
1677
1678
  }
1678
1679
 
1680
+ /**
1681
+ * `.wachiman.yml` as it is at the pull request's base commit, parsed, or undefined when the base has
1682
+ * none. Like the focus file, never the head's copy: a pull request that edits `model` or `effort`
1683
+ * is reviewed, and paid for, under the base's values. A file the contents API cannot return whole
1684
+ * (over 64 KB) is a usage error rather than a guess. An invalid file is a usage error naming it;
1685
+ * `withBaseSettings` turns that into a warning, so a broken file never blocks a review.
1686
+ */
1687
+ export function readBaseSettings(gh: GhFn, repo: string, sha: string): RepoSettings | undefined {
1688
+ const file = fileAtBase(gh, repo, SETTINGS_PATH, sha);
1689
+ if (!file) return undefined;
1690
+ if (file.truncated) throw new UsageError(`${SETTINGS_PATH} at the base commit is over ${MAX_INSTRUCTION_BYTES / 1024} KB, too large for a settings file.`);
1691
+ return parseRepoSettings(file.content);
1692
+ }
1693
+
1694
+ /**
1695
+ * `config` with the model and the effort the base commit's `.wachiman.yml` sets, wherever no
1696
+ * explicit `WACHIMAN_MODEL` / `WACHIMAN_EFFORT` (a workflow input) did. Reads nothing when both
1697
+ * were explicit. Logs one line when the file decided something, so the job log says so. A broken
1698
+ * file at the base (bad YAML, a bad `effort`, too large) is ignored with a warning and the run
1699
+ * continues on the environment and the defaults: the pull request that fixes the file must be
1700
+ * reviewable. Errors from GitHub itself still propagate.
1701
+ */
1702
+ export function withBaseSettings(
1703
+ config: EngineConfig,
1704
+ gh: GhFn,
1705
+ repo: string,
1706
+ pr: number,
1707
+ log: (line: string) => void,
1708
+ warn: (line: string) => void = log,
1709
+ ): EngineConfig {
1710
+ if (!wantsRepoSettings(config)) return config;
1711
+ const sha = baseSha(gh, repo, pr);
1712
+ let settings: RepoSettings | undefined;
1713
+ try {
1714
+ settings = readBaseSettings(gh, repo, sha);
1715
+ } catch (error) {
1716
+ if (!(error instanceof UsageError)) throw error;
1717
+ warn(ignoredSettingsNote(error.message, `the base commit ${sha.slice(0, 7)}`));
1718
+ return config;
1719
+ }
1720
+ if (!settings) return config;
1721
+ const where = `the base commit ${sha.slice(0, 7)}`;
1722
+ const next = applyRepoSettings(config, settings, { where, warn });
1723
+ const note = fileSettingsNote(next, where);
1724
+ if (note) log(`settings: ${note}`);
1725
+ return next;
1726
+ }
1727
+
1679
1728
  /**
1680
1729
  * The instruction files at `sha`, read one path at a time through `fileAt` (null when absent):
1681
1730
  * the lookup order, the old focus path's fallback and the warnings of every connector.
@@ -2338,6 +2387,13 @@ export function pendingThreads(threads: RawThread[]) {
2338
2387
  /** Below this a batch cannot read its prompt, so none is started. */
2339
2388
  export const MIN_BATCH_USD = 0.05;
2340
2389
 
2390
+ /**
2391
+ * How many batches of one review run at the same time. Start at 4 and measure 4 against 6 before
2392
+ * raising it: the subscription documents no concurrency limit per account, and a short-lived
2393
+ * "temporarily limiting requests" throttle exists that Claude Code retries by itself.
2394
+ */
2395
+ export const BATCH_CONCURRENCY = 4;
2396
+
2341
2397
  /**
2342
2398
  * What the pull request's automatic runs have spent, and the ceilings the engine already told
2343
2399
  * the owner about. Both come only from the engine's own signed footers (`readAutoCost`,
@@ -2544,6 +2600,9 @@ const short200 = (error: unknown) => (error instanceof Error ? error.message : S
2544
2600
  /** The dollars the calls since `grant` cost, for a run that ended before it could say what it spent. */
2545
2601
  export const spentSince = (deps: Deps, grant: SpendGrant) => (deps.costs ?? []).slice(grant.costsFrom).reduce((n, c) => n + c.costUsd, 0);
2546
2602
 
2603
+ /** Runs that already said the ledger is skipped, so the line appears once. */
2604
+ const skippedLedger = new WeakSet<Deps>();
2605
+
2547
2606
  /**
2548
2607
  * Asks the ledger how much this run may spend, before any model call. With no `ledger_url` there is
2549
2608
  * nothing to ask and the room is unlimited; `limit_bypass` skips the ledger the same way, and the
@@ -2552,11 +2611,22 @@ export const spentSince = (deps: Deps, grant: SpendGrant) => (deps.costs ?? []).
2552
2611
  * accept and a job without an OIDC token are configuration errors: `LedgerStopped`, whatever
2553
2612
  * `ledger_unavailable` says. A ledger that cannot be reached lets the run go on under its normal
2554
2613
  * budgets (`unreached`), or stops it with `LedgerStopped` when `ledger_unavailable` is `stop`.
2614
+ * Outside GitHub Actions (`GITHUB_ACTIONS` is not `true`) nothing is asked: the run goes on and a
2615
+ * line says so.
2555
2616
  */
2556
2617
  export function spendReserve(deps: Deps, repo: string, pr: number, headSha: string, wantedUsd: number, forced: boolean): SpendGrant {
2557
2618
  const { config } = deps;
2558
2619
  const open = { reservationId: null, roomUsd: Number.POSITIVE_INFINITY, costsFrom: deps.costs?.length ?? 0, trigger: config.trigger ?? "cli", pr };
2559
2620
  if (!config.ledgerUrl) return { kind: "none", ...open };
2621
+ // Laptop runs are not counted against the limit: `WACHIMAN_LEDGER_URL` there is the read side's
2622
+ // setting (`wachiman ledger`), not a request to reserve. Only a GitHub Actions job reserves.
2623
+ if ((deps.ledgerEnv ?? process.env).GITHUB_ACTIONS !== "true") {
2624
+ if (!skippedLedger.has(deps)) {
2625
+ skippedLedger.add(deps);
2626
+ deps.warn("The spend ledger is skipped: this is not a GitHub Actions run, and laptop runs are not counted against the limit.");
2627
+ }
2628
+ return { kind: "none", ...open };
2629
+ }
2560
2630
  if (config.limitBypass) {
2561
2631
  deps.log("ledger bypassed");
2562
2632
  ledgerNote(deps, "The spend limit was bypassed for this run (`limit_bypass`); nothing was reserved or recorded.");
@@ -2673,9 +2743,9 @@ function grantTooSmall(deps: Deps, grant: SpendGrant, what: string): boolean {
2673
2743
  }
2674
2744
 
2675
2745
  /** Runs a paid step; if it throws, the ledger still hears what the calls so far cost, then the error goes on. */
2676
- function settledOnError<T>(deps: Deps, grant: SpendGrant, run: () => T): T {
2746
+ async function settledOnError<T>(deps: Deps, grant: SpendGrant, run: () => Promise<T>): Promise<T> {
2677
2747
  try {
2678
- return run();
2748
+ return await run();
2679
2749
  } catch (error) {
2680
2750
  spendSettle(deps, grant, spentSince(deps, grant));
2681
2751
  throw error;
@@ -2860,12 +2930,12 @@ export function postReview(deps: Deps, repo: string, pr: number, headSha: string
2860
2930
  * Runs one call so that its failure does not end the review: the batches that did run still
2861
2931
  * post. A call that died without a result is counted at its cap, since what it spent is unknown.
2862
2932
  */
2863
- export function attempt<T>(
2864
- run: () => T,
2933
+ export async function attempt<T>(
2934
+ run: () => Promise<T> | T,
2865
2935
  capUsd: number,
2866
- ): { ok: true; value: T } | { ok: false; error: string; costUsd: number } {
2936
+ ): Promise<{ ok: true; value: T } | { ok: false; error: string; costUsd: number }> {
2867
2937
  try {
2868
- return { ok: true, value: run() };
2938
+ return { ok: true, value: await run() };
2869
2939
  } catch (error) {
2870
2940
  return { ok: false, error: String(error).slice(0, 300), costUsd: capUsd };
2871
2941
  }
@@ -2930,7 +3000,7 @@ export function reviewOptionsOf(config: EngineConfig, totalBudgetUsd: number, pr
2930
3000
  * checks, coverage, the one retry, budget arithmetic, merging, the confidence rule, dedupe, the
2931
3001
  * ADR notices, the anchors and the "not reached" skips are here. Posting is not.
2932
3002
  */
2933
- export function reviewChange(input: ReviewInput, options: ReviewOptions, deps: CoreDeps): ReviewResult {
3003
+ export async function reviewChange(input: ReviewInput, options: ReviewOptions, deps: CoreDeps): Promise<ReviewResult> {
2934
3004
  assertReviewInputFormat(input);
2935
3005
  const started = Date.now();
2936
3006
  // Every warning the core logs is also a notice of the result; `runProvider` appends one entry
@@ -2994,7 +3064,7 @@ export function reviewChange(input: ReviewInput, options: ReviewOptions, deps: C
2994
3064
  // is spent before the batches, so they cannot overspend the pull request's budget.
2995
3065
  const confidence = change.kind === "pull-request" ? options.confidence : undefined;
2996
3066
  const claimsStep = confidence
2997
- ? listClaims(core, options, context, Math.min(confidence.budgetUsd, batchTotal / 4))
3067
+ ? await listClaims(core, options, context, Math.min(confidence.budgetUsd, batchTotal / 4))
2998
3068
  : undefined;
2999
3069
  if (claimsStep) {
3000
3070
  spent += claimsStep.costUsd;
@@ -3021,51 +3091,88 @@ export function reviewChange(input: ReviewInput, options: ReviewOptions, deps: C
3021
3091
  else rejection ??= { kind: r.kind, text: r.text };
3022
3092
  };
3023
3093
 
3024
- batches.forEach((batch, i) => {
3025
- const label = `Batch ${i + 1} of ${batches.length}`;
3026
- const remaining = () => batchTotal - spent;
3027
- if (rejection) {
3028
- rejectedFiles.push(...batch.files.map((f) => f.filename));
3029
- reports.push({
3030
- label,
3031
- files: batch.files.length,
3032
- lines: batch.lines,
3033
- maxRisk: batch.maxRisk,
3034
- status: "not run: provider rejected the call",
3035
- turns: 0,
3036
- costUsd: 0,
3037
- });
3038
- return;
3039
- }
3040
- if (remaining() < MIN_BATCH_USD) {
3041
- notRun.push(...batch.files.map((f) => f.filename));
3042
- reports.push({
3043
- label,
3044
- files: batch.files.length,
3045
- lines: batch.lines,
3046
- maxRisk: batch.maxRisk,
3047
- status: "not run: budget spent",
3048
- turns: 0,
3049
- costUsd: 0,
3050
- });
3051
- return;
3094
+ // Batches run side by side, at most `BATCH_CONCURRENCY` at a time, launched in risk order. Each
3095
+ // one ends in its own outcome; the report, the parts and the file lists are put together in
3096
+ // batch order afterwards, so the finish order does not decide them. Warnings and the cost rows
3097
+ // are recorded as the calls end, and name their batch.
3098
+ interface BatchOutcome {
3099
+ report: BatchReport;
3100
+ parts: { label: string; output: ReviewOutput }[];
3101
+ notRun: string[];
3102
+ rejectedFiles: string[];
3103
+ budgetCut: string[];
3104
+ usage: TokenUsage;
3105
+ budgetError: boolean;
3106
+ stopped?: "turns" | "timeout";
3107
+ anyLimit: boolean;
3108
+ /** What the provider refused or failed with, to be reported in batch order, not in finish order. */
3109
+ rejections: NonNullable<BatchResult["rejection"]>[];
3110
+ }
3111
+ const outcomes: (BatchOutcome | undefined)[] = new Array(batches.length).fill(undefined);
3112
+ // Money the batches in flight may still spend: what each reserved, until its cost is known.
3113
+ const reserved = new Map<number, number>();
3114
+ let inFlight = 0;
3115
+ let fatal: { error: unknown } | undefined;
3116
+ const waiters: (() => void)[] = [];
3117
+ const changed = () => new Promise<void>((resolve) => waiters.push(resolve));
3118
+ const notify = () => waiters.splice(0).forEach((resolve) => resolve());
3119
+ const room = () => batchTotal - spent - [...reserved.values()].reduce((sum, usd) => sum + usd, 0);
3120
+ /**
3121
+ * Reserves `min(batchBudget, room)` for batch `id`, or says why it cannot run. While the room is
3122
+ * below the minimum but money is reserved by batches in flight, it waits: what they do not spend
3123
+ * comes back, and giving up now would skip a batch that a sequential run would have afforded.
3124
+ */
3125
+ const acquire = async (id: number, launching: boolean): Promise<{ usd: number } | { why: "rejected" | "budget" | "failed" }> => {
3126
+ for (;;) {
3127
+ if (fatal) return { why: "failed" };
3128
+ if (rejection) return { why: "rejected" };
3129
+ if (!launching || inFlight < BATCH_CONCURRENCY) {
3130
+ const left = room();
3131
+ if (left >= MIN_BATCH_USD) {
3132
+ const usd = Math.min(batchBudget, left);
3133
+ reserved.set(id, usd);
3134
+ return { usd };
3135
+ }
3136
+ if (reserved.size === 0) return { why: "budget" };
3137
+ }
3138
+ await changed();
3052
3139
  }
3053
- const first = reviewBatchBackingOff(
3140
+ };
3141
+
3142
+ const runBatch = async (i: number, batch: Batch, firstBudget: number): Promise<BatchOutcome> => {
3143
+ const label = `Batch ${i + 1} of ${batches.length}`;
3144
+ const out: BatchOutcome = {
3145
+ report: { label, files: batch.files.length, lines: batch.lines, maxRisk: batch.maxRisk, status: "", turns: 0, costUsd: 0 },
3146
+ parts: [],
3147
+ notRun: [],
3148
+ rejectedFiles: [],
3149
+ budgetCut: [],
3150
+ usage: NO_USAGE,
3151
+ budgetError: false,
3152
+ anyLimit: false,
3153
+ rejections: [],
3154
+ };
3155
+ core.log(`batch ${i + 1}/${batches.length}: started (${inFlight} of ${BATCH_CONCURRENCY} running, $${firstBudget.toFixed(2)} reserved)`);
3156
+ const first = await reviewBatchBackingOff(
3054
3157
  core,
3055
3158
  options,
3056
3159
  context,
3057
3160
  batch.files,
3058
3161
  threadsByBatch[i],
3059
3162
  { index: i + 1, total: batches.length },
3060
- Math.min(batchBudget, remaining()),
3163
+ firstBudget,
3061
3164
  );
3165
+ // The reservation gives way to what the call really cost.
3062
3166
  spent += first.costUsd;
3063
- usage = addUsage(usage, first.usage);
3167
+ reserved.delete(i);
3168
+ notify();
3169
+ out.usage = addUsage(out.usage, first.usage);
3064
3170
  noteRejection(first.rejection);
3065
- if (first.output) parts.push({ label, output: first.output });
3066
- if (first.budget) budgetError = true;
3067
- if (first.stopped) limitError = first.stopped;
3068
- if (first.budget || first.stopped) anyLimit = true;
3171
+ if (first.rejection) out.rejections.push(first.rejection);
3172
+ if (first.output) out.parts.push({ label, output: first.output });
3173
+ if (first.budget) out.budgetError = true;
3174
+ if (first.stopped) out.stopped = first.stopped;
3175
+ if (first.budget || first.stopped) out.anyLimit = true;
3069
3176
  let status = first.output
3070
3177
  ? "reviewed"
3071
3178
  : first.budget
@@ -3082,53 +3189,127 @@ export function reviewChange(input: ReviewInput, options: ReviewOptions, deps: C
3082
3189
  let turns = first.turns;
3083
3190
  let cost = first.costUsd;
3084
3191
 
3085
- // One retry with only the files the first pass left unaccounted for.
3192
+ // One retry with only the files the first pass left unaccounted for, in the same slot.
3086
3193
  const gaps = batchGaps(batch.files, first.output);
3087
3194
  // After a wall-clock kill the same files would likely run into the same limit, and another
3088
3195
  // full timeout could exhaust the job's own limit: they are reported as not reviewed instead.
3089
3196
  // A refused call (no credit, bad key) would be refused again: no retry.
3090
- if (gaps.length > 0 && first.stopped !== "timeout" && !rejection && remaining() >= MIN_BATCH_USD) {
3091
- const retry = reviewBatchBackingOff(
3197
+ // Only this batch's own refusal explains its gaps; another batch's refusal only stops new calls.
3198
+ const ownRefusal = first.rejection !== undefined && first.rejection.kind !== "other";
3199
+ const grant = gaps.length > 0 && first.stopped !== "timeout" && !ownRefusal ? await acquire(i, false) : undefined;
3200
+ if (gaps.length > 0 && grant && "usd" in grant) {
3201
+ const retry = await reviewBatchBackingOff(
3092
3202
  core,
3093
3203
  options,
3094
3204
  context,
3095
3205
  gaps,
3096
3206
  [],
3097
3207
  { index: i + 1, total: batches.length, retry: true },
3098
- Math.min(batchBudget, remaining()),
3208
+ grant.usd,
3099
3209
  );
3100
3210
  spent += retry.costUsd;
3101
- usage = addUsage(usage, retry.usage);
3211
+ reserved.delete(i);
3212
+ notify();
3213
+ out.usage = addUsage(out.usage, retry.usage);
3102
3214
  turns += retry.turns;
3103
3215
  cost += retry.costUsd;
3104
3216
  noteRejection(retry.rejection);
3105
- if (retry.budget || retry.stopped) anyLimit = true;
3106
- if (retry.output) parts.push({ label: `${label}, retry`, output: retry.output });
3217
+ if (retry.rejection) out.rejections.push(retry.rejection);
3218
+ if (retry.budget || retry.stopped) out.anyLimit = true;
3219
+ if (retry.output) out.parts.push({ label: `${label}, retry`, output: retry.output });
3107
3220
  const left = batchGaps(gaps, retry.output);
3108
- if (retry.rejection && retry.rejection.kind !== "other") rejectedFiles.push(...left.map((f) => f.filename));
3109
- else if (retry.budget) budgetCut.push(...left.map((f) => f.filename));
3221
+ if (retry.rejection && retry.rejection.kind !== "other") out.rejectedFiles.push(...left.map((f) => f.filename));
3222
+ else if (retry.budget) out.budgetCut.push(...left.map((f) => f.filename));
3110
3223
  status =
3111
3224
  left.length === 0 ? "reviewed after a retry" : `${left.length} file(s) left after a retry`;
3112
3225
  } else if (gaps.length > 0) {
3113
- status = rejection
3226
+ // The files are blamed on a refusal when this batch's own call was refused, or when another
3227
+ // batch's refusal stopped the retry of a call that had no cause of its own (a budget or a limit).
3228
+ const refused =
3229
+ ownRefusal ||
3230
+ (grant !== undefined && "why" in grant && grant.why === "rejected" && !first.budget && !first.stopped);
3231
+ status = refused
3114
3232
  ? `${gaps.length} file(s) left: the provider rejected the call`
3115
3233
  : first.stopped === "timeout"
3116
3234
  ? `${gaps.length} file(s) left: the call timed out`
3117
3235
  : `${gaps.length} file(s) left: budget spent`;
3118
3236
  // A refused call or a spent budget is not the files' doing; a timeout may be, so it stays unexplained.
3119
- if (rejection) rejectedFiles.push(...gaps.map((f) => f.filename));
3120
- else if (first.stopped !== "timeout") budgetCut.push(...gaps.map((f) => f.filename));
3237
+ if (refused) out.rejectedFiles.push(...gaps.map((f) => f.filename));
3238
+ else if (first.stopped !== "timeout") out.budgetCut.push(...gaps.map((f) => f.filename));
3121
3239
  }
3122
- reports.push({
3123
- label,
3124
- files: batch.files.length,
3125
- lines: batch.lines,
3126
- maxRisk: batch.maxRisk,
3127
- status,
3128
- turns,
3129
- costUsd: cost,
3240
+ out.report = { ...out.report, status, turns, costUsd: cost };
3241
+ return out;
3242
+ };
3243
+
3244
+ const running = new Set<Promise<void>>();
3245
+ for (const [i, batch] of batches.entries()) {
3246
+ const label = `Batch ${i + 1} of ${batches.length}`;
3247
+ const skipped = (status: string, files: string[]): BatchOutcome => ({
3248
+ report: { label, files: batch.files.length, lines: batch.lines, maxRisk: batch.maxRisk, status, turns: 0, costUsd: 0 },
3249
+ parts: [],
3250
+ notRun: status === "not run: budget spent" ? files : [],
3251
+ rejectedFiles: status === "not run: budget spent" ? [] : files,
3252
+ budgetCut: [],
3253
+ usage: NO_USAGE,
3254
+ budgetError: false,
3255
+ anyLimit: false,
3256
+ rejections: [],
3130
3257
  });
3131
- });
3258
+ const granted = await acquire(i, true);
3259
+ if ("why" in granted) {
3260
+ if (granted.why === "failed") break;
3261
+ const files = batch.files.map((f) => f.filename);
3262
+ outcomes[i] =
3263
+ granted.why === "rejected"
3264
+ ? skipped("not run: provider rejected the call", files)
3265
+ : skipped("not run: budget spent", files);
3266
+ continue;
3267
+ }
3268
+ // A batch that threw while this one waited for its slot: give the money back and start nothing.
3269
+ if (fatal) {
3270
+ reserved.delete(i);
3271
+ break;
3272
+ }
3273
+ inFlight++;
3274
+ const task: Promise<void> = runBatch(i, batch, granted.usd)
3275
+ .then(
3276
+ (outcome) => void (outcomes[i] = outcome),
3277
+ (error) => void (fatal ??= { error }),
3278
+ )
3279
+ .finally(() => {
3280
+ inFlight--;
3281
+ reserved.delete(i);
3282
+ running.delete(task);
3283
+ notify();
3284
+ });
3285
+ running.add(task);
3286
+ }
3287
+ await Promise.all(running);
3288
+ // A batch that threw (not a failed call: those are outcomes) ends the review as it always did,
3289
+ // after the ones in flight finished and were counted.
3290
+ if (fatal) throw fatal.error;
3291
+
3292
+ // In batch order, whatever order they finished in. The refusal that stopped new batches was noted
3293
+ // as it came; the one that is reported is the first in batch order, as in a sequential run.
3294
+ rejection = undefined;
3295
+ otherError = undefined;
3296
+ for (const outcome of outcomes) {
3297
+ if (!outcome) continue;
3298
+ outcome.rejections.forEach(noteRejection);
3299
+ reports.push(outcome.report);
3300
+ parts.push(...outcome.parts);
3301
+ notRun.push(...outcome.notRun);
3302
+ rejectedFiles.push(...outcome.rejectedFiles);
3303
+ budgetCut.push(...outcome.budgetCut);
3304
+ usage = addUsage(usage, outcome.usage);
3305
+ if (outcome.budgetError) budgetError = true;
3306
+ if (outcome.stopped) limitError = outcome.stopped;
3307
+ if (outcome.anyLimit) anyLimit = true;
3308
+ }
3309
+
3310
+ // Set by the callbacks above, which the compiler does not follow.
3311
+ const refused = rejection as { kind: Exclude<ProviderErrorClass, "other">; text: string } | undefined;
3312
+ const other = otherError as string | undefined;
3132
3313
 
3133
3314
  const merged: ReviewOutput =
3134
3315
  parts.length > 0
@@ -3152,7 +3333,7 @@ export function reviewChange(input: ReviewInput, options: ReviewOptions, deps: C
3152
3333
  let verification: ResultVerificationV2 | undefined;
3153
3334
  let checked: VerifiedFinding[];
3154
3335
  if (options.verify && parts.length > 0) {
3155
- const verified = verifyFindings(core, options, context, merged.findings, kept, Math.max(totalBudget - spent, 0), rejection?.text);
3336
+ const verified = await verifyFindings(core, options, context, merged.findings, kept, Math.max(totalBudget - spent, 0), refused?.text);
3156
3337
  spent += verified.record.costUsd;
3157
3338
  usage = addUsage(usage, verified.usage);
3158
3339
  checked = verified.findings;
@@ -3181,9 +3362,6 @@ export function reviewChange(input: ReviewInput, options: ReviewOptions, deps: C
3181
3362
  const known = new Set(threads.map((t) => t.comment_id));
3182
3363
  const threadReplies = merged.thread_replies.filter((r) => known.has(r.comment_id));
3183
3364
 
3184
- // Assigned inside the loop's callbacks, which the compiler does not follow.
3185
- const refused = rejection as { kind: Exclude<ProviderErrorClass, "other">; text: string } | undefined;
3186
- const other = otherError as string | undefined;
3187
3365
  const firstRejection = refused ?? (other !== undefined ? { kind: "other" as const, text: other } : undefined);
3188
3366
  // In today's precedence: a refused account stops everything; a review with output, or nothing
3189
3367
  // to review, is complete (coverage says whether it is partial); otherwise the limit that ended
@@ -3296,12 +3474,12 @@ export function reviewChange(input: ReviewInput, options: ReviewOptions, deps: C
3296
3474
  * from the author's text alone (Wachiman's own block stripped). It fails closed: with no list the
3297
3475
  * review still runs, and the grade is `cannot say`. A call that cannot be afforded is not made.
3298
3476
  */
3299
- function listClaims(
3477
+ async function listClaims(
3300
3478
  deps: CoreDeps,
3301
3479
  options: ReviewOptions,
3302
3480
  ctx: ReviewContext,
3303
3481
  budgetUsd: number,
3304
- ): { claims?: ProposedClaim[]; costUsd: number; usage: TokenUsage; budget: boolean; truncated: boolean } {
3482
+ ): Promise<{ claims?: ProposedClaim[]; costUsd: number; usage: TokenUsage; budget: boolean; truncated: boolean }> {
3305
3483
  const input = claimsInput(ctx.meta.title ?? "", ctx.meta.body);
3306
3484
  const truncated = claimsTextTruncated(input);
3307
3485
  const none = { costUsd: 0, usage: NO_USAGE, budget: false, truncated };
@@ -3310,7 +3488,7 @@ function listClaims(
3310
3488
  return { ...none, budget: true };
3311
3489
  }
3312
3490
  const prompt = buildClaimsPrompt(input);
3313
- const call = attempt(
3491
+ const call = await attempt(
3314
3492
  () => runProvider(deps, options, ctx.headDir, { operation: "review", label: "claims", ...ctx.costTags }, prompt, ctx.headDir, budgetUsd, CLAIMS_SCHEMA),
3315
3493
  budgetUsd,
3316
3494
  );
@@ -3356,7 +3534,7 @@ function verdictsOf(structured: unknown): VerifierVerdict[] | undefined {
3356
3534
  * `inconclusive` with the stop as their reason. Findings it left at low confidence go through the
3357
3535
  * confidence rule again.
3358
3536
  */
3359
- function verifyFindings(
3537
+ async function verifyFindings(
3360
3538
  deps: CoreDeps,
3361
3539
  options: ReviewOptions & { verify: { model: string; budgetUsd: number } },
3362
3540
  ctx: ReviewContext,
@@ -3364,7 +3542,7 @@ function verifyFindings(
3364
3542
  files: ReviewFile[],
3365
3543
  budgetUsd: number,
3366
3544
  refused?: string,
3367
- ): { findings: VerifiedFinding[]; record: ResultVerificationV2; usage: TokenUsage } {
3545
+ ): Promise<{ findings: VerifiedFinding[]; record: ResultVerificationV2; usage: TokenUsage }> {
3368
3546
  const verifier = deps.verifier ?? deps.provider;
3369
3547
  // Without its own adapter the call runs on the reviewer's, whose model and effort are fixed at construction.
3370
3548
  const ran = deps.verifier ? { model: options.verify.model, effort: "high" } : { model: options.model, effort: options.effort };
@@ -3405,7 +3583,7 @@ function verifyFindings(
3405
3583
  const prompt = buildVerifyPrompt({ describe: ctx.describe, headSha: ctx.headSha, headDir: ctx.headDir, baseDir: baseSnap?.dir, candidates, files });
3406
3584
  dir = mkdtempSync(join(tmpdir(), "agent-verify-"));
3407
3585
  const runDir = dir;
3408
- const call = attempt(
3586
+ const call = await attempt(
3409
3587
  () =>
3410
3588
  runProvider(
3411
3589
  { ...deps, provider: verifier },
@@ -3570,7 +3748,7 @@ export function githubReview(
3570
3748
  }
3571
3749
 
3572
3750
  /** `force` (`--force`) counts only for the operator's own run, never an automatic trigger. */
3573
- export function review(deps: Deps, repo: string, pr: number, words: string[], force = false) {
3751
+ export async function review(deps: Deps, repo: string, pr: number, words: string[], force = false) {
3574
3752
  const { gh, config } = deps;
3575
3753
  const meta = JSON.parse(
3576
3754
  gh(["pr", "view", String(pr), "-R", repo, "--json", "headRefOid,title,body,author"]),
@@ -3812,7 +3990,7 @@ export function review(deps: Deps, repo: string, pr: number, words: string[], fo
3812
3990
  ...reviewOptionsOf(config, totalBudget),
3813
3991
  ...(config.confidence && fullReview ? { confidence: { budgetUsd: CLAIMS_BUDGET_USD } } : {}),
3814
3992
  };
3815
- const result = settledOnError(deps, grant, () => reviewChange(input, options, deps));
3993
+ const result = await settledOnError(deps, grant, () => reviewChange(input, options, deps));
3816
3994
  const { usage, costUsdEstimate: spent } = result.execution;
3817
3995
  const operation: Operation = since ? "re-review" : "review";
3818
3996
  const receiptLine = spendSettle(deps, grant, spent) ?? undefined;
@@ -4038,7 +4216,7 @@ Answer every thread once. Answer no thread that is not in the file.`;
4038
4216
  * It is an automatic run: it counts against the pull request's ceiling. It follows ADR 0008: it
4039
4217
  * reports, it never resolves.
4040
4218
  */
4041
- export function threadReplies(deps: Deps, repo: string, pr: number, author?: string) {
4219
+ export async function threadReplies(deps: Deps, repo: string, pr: number, author?: string) {
4042
4220
  const { gh, config } = deps;
4043
4221
  // As in `comment`: before anything is read, so a stranger's reply on a public repository (or a
4044
4222
  // placeholder operator) starts no paid step. The workflow's `if:` checks the same login.
@@ -4083,7 +4261,7 @@ export function threadReplies(deps: Deps, repo: string, pr: number, author?: str
4083
4261
  const grant = spendReserve(deps, repo, pr, headSha, Math.min(config.threadsBudgetUsd, ceiling.room), false);
4084
4262
  const budgetUsd = Math.min(config.threadsBudgetUsd, ceiling.room, grant.roomUsd);
4085
4263
  if (grantTooSmall(deps, grant, "thread replies")) return;
4086
- const call = settledOnError(deps, grant, () =>
4264
+ const call = await settledOnError(deps, grant, () =>
4087
4265
  runProvider(
4088
4266
  deps,
4089
4267
  config,
@@ -4401,8 +4579,8 @@ function gatherEvidence(deps: CoreDeps, headDir: string, files: ReviewFile[], di
4401
4579
  type CallLimits = Pick<EngineConfig, "maxTurns" | "callTimeoutS" | "model" | "trigger">;
4402
4580
 
4403
4581
  /** One provider call over one batch, with the batch's diff inside the prompt. */
4404
- function reviewBatch(
4405
- deps: CoreDeps,
4582
+ async function reviewBatch(
4583
+ core: CoreDeps,
4406
4584
  limits: CallLimits,
4407
4585
  ctx: ReviewContext,
4408
4586
  files: ReviewFile[],
@@ -4410,6 +4588,8 @@ function reviewBatch(
4410
4588
  part: { index: number; total: number; retry?: boolean },
4411
4589
  budgetUsd: number,
4412
4590
  ) {
4591
+ // Batches run side by side, so every line this one prints names it.
4592
+ const deps = attributed(core, part);
4413
4593
  const dir = mkdtempSync(join(tmpdir(), "wachiman-review-"));
4414
4594
  // The run dir holds the change's text (title, body, threads, evidence): removed once the call
4415
4595
  // is over, whatever happened. Nothing reads it after the provider returns.
@@ -4440,7 +4620,7 @@ function reviewBatch(
4440
4620
  );
4441
4621
 
4442
4622
  const label = `batch ${part.index}/${part.total}${part.retry ? " retry" : ""}`;
4443
- const call = attempt(
4623
+ const call = await attempt(
4444
4624
  () =>
4445
4625
  runProvider(
4446
4626
  deps,
@@ -4491,17 +4671,33 @@ function reviewBatch(
4491
4671
  /** Where a rate-limited call waits before its one retry, in ms. Overridable through `Deps` so tests do not sleep. */
4492
4672
  const RATE_LIMIT_BACKOFF_MS = 20_000;
4493
4673
 
4494
- type BatchResult = ReturnType<typeof reviewBatch>;
4674
+ type BatchResult = Awaited<ReturnType<typeof reviewBatch>>;
4675
+
4676
+ /**
4677
+ * `core` whose log and warning lines each name the batch that printed them (`batch 2/7: ...`),
4678
+ * unless the line already does (the call summary and the cost line carry the label themselves).
4679
+ */
4680
+ function attributed(core: CoreDeps, part: { index: number; total: number }): CoreDeps {
4681
+ const mentions = new RegExp(`\\b(batch ${part.index}/${part.total}|batch ${part.index} of ${part.total}|part ${part.index} of ${part.total})\\b`, "i");
4682
+ const tag = (line: string) => (mentions.test(line) ? line : `batch ${part.index}/${part.total}: ${line}`);
4683
+ return { ...core, log: (line) => core.log(tag(line)), warn: (line) => core.warn(tag(line)) };
4684
+ }
4685
+
4686
+ /** Waits `ms` without blocking the other batches. A test's own `sleep` replaces the wait. */
4687
+ async function pause(deps: CoreDeps, ms: number) {
4688
+ if (deps.sleep) deps.sleep(ms);
4689
+ else await new Promise<void>((resolve) => setTimeout(resolve, ms));
4690
+ }
4495
4691
 
4496
4692
  /** `reviewBatch`, with one retry of the same batch after a backoff when the API rate-limited the call. */
4497
- function reviewBatchBackingOff(...args: Parameters<typeof reviewBatch>): BatchResult {
4693
+ async function reviewBatchBackingOff(...args: Parameters<typeof reviewBatch>): Promise<BatchResult> {
4498
4694
  const [deps, , , , , part] = args;
4499
- const first = reviewBatch(...args);
4695
+ const first = await reviewBatch(...args);
4500
4696
  if (first.rejection?.kind !== "rate_limit") return first;
4501
4697
  const wait = deps.rateLimitBackoffMs ?? RATE_LIMIT_BACKOFF_MS;
4502
4698
  deps.warn(`Batch ${part.index} of ${part.total} was rate-limited; retrying once in ${wait / 1000} s.`);
4503
- (deps.sleep ?? blockingSleep)(wait);
4504
- const again = reviewBatch(...args);
4699
+ await pause(deps, wait);
4700
+ const again = await reviewBatch(...args);
4505
4701
  return {
4506
4702
  ...again,
4507
4703
  turns: first.turns + again.turns,
@@ -4511,7 +4707,7 @@ function reviewBatchBackingOff(...args: Parameters<typeof reviewBatch>): BatchRe
4511
4707
  }
4512
4708
 
4513
4709
  /** The prompt goes through stdin: a batch's diff in an argument would hit Linux's 128 KB limit. */
4514
- export function runProvider(
4710
+ export async function runProvider(
4515
4711
  deps: CoreDeps,
4516
4712
  limits: CallLimits,
4517
4713
  cwd: string,
@@ -4524,9 +4720,9 @@ export function runProvider(
4524
4720
  extraDirs: string[] = [],
4525
4721
  ) {
4526
4722
  const started = Date.now();
4527
- let result: ReturnType<ProviderAdapter["run"]>;
4723
+ let result: Awaited<ReturnType<ProviderAdapter["run"]>>;
4528
4724
  try {
4529
- result = deps.provider.run({
4725
+ result = await deps.provider.run({
4530
4726
  prompt,
4531
4727
  cwd,
4532
4728
  addDirs: [dir, ...extraDirs],
@@ -4560,6 +4756,7 @@ export function runProvider(
4560
4756
  if (result.costKnown === false) result = { ...result, costUsd: budgetUsd };
4561
4757
  deps.log(
4562
4758
  JSON.stringify({
4759
+ label: what.label,
4563
4760
  is_error: result.isError,
4564
4761
  terminal_reason: result.terminalReason,
4565
4762
  turns: result.turns,
@@ -4703,7 +4900,7 @@ function postAnswer(deps: Deps, repo: string, pr: number, body: string, line?: R
4703
4900
  * For someone but the operator, the answer is marked with its cost (`readAskCost`), and a call that
4704
4901
  * fails still posts a note with its cost, so the question ceiling counts what was spent.
4705
4902
  */
4706
- export function answer(deps: Deps, repo: string, pr: number, question: string, options: AnswerOptions = {}) {
4903
+ export async function answer(deps: Deps, repo: string, pr: number, question: string, options: AnswerOptions = {}) {
4707
4904
  const { asker, line } = options;
4708
4905
  const dir = mkdtempSync(join(tmpdir(), "agent-answer-"));
4709
4906
  const meta = deps.gh(["pr", "view", String(pr), "-R", repo, "--json", "title,body,files,headRefOid"]);
@@ -4740,10 +4937,10 @@ export function answer(deps: Deps, repo: string, pr: number, question: string, o
4740
4937
  else postAnswer(deps, repo, pr, body, line);
4741
4938
  return;
4742
4939
  }
4743
- let result: ReturnType<typeof runProvider> | undefined;
4940
+ let result: Awaited<ReturnType<typeof runProvider>> | undefined;
4744
4941
  let failure = "";
4745
4942
  try {
4746
- result = runProvider(
4943
+ result = await runProvider(
4747
4944
  deps,
4748
4945
  deps.config,
4749
4946
  cwd,
@@ -4834,7 +5031,7 @@ function postAskLimit(deps: Deps, repo: string, pr: number, spent: number) {
4834
5031
  * write access, and while the pull request's question ceiling has room (ADR 0032 decision 9).
4835
5032
  * An empty or placeholder operator is an error.
4836
5033
  */
4837
- export function comment(deps: Deps, repo: string, pr: number, request: string, author?: string, line?: ReviewLineComment) {
5034
+ export async function comment(deps: Deps, repo: string, pr: number, request: string, author?: string, line?: ReviewLineComment) {
4838
5035
  const { operator, askers } = deps.config;
4839
5036
  // Before anything else: an unset or placeholder operator would let a stranger who registered
4840
5037
  // that login command the tool.
@@ -4849,9 +5046,9 @@ export function comment(deps: Deps, repo: string, pr: number, request: string, a
4849
5046
  if (command?.kind === "review") {
4850
5047
  // With no ledger `--force` means nothing: it stays a word that names no file, as it always did.
4851
5048
  const words = deps.config.ledgerUrl ? command.paths.filter((w) => w !== "--force") : command.paths;
4852
- review(deps, repo, pr, words, command.force);
5049
+ await review(deps, repo, pr, words, command.force);
4853
5050
  }
4854
- else if (command) answer(deps, repo, pr, request, { line });
5051
+ else if (command) await answer(deps, repo, pr, request, { line });
4855
5052
  return;
4856
5053
  }
4857
5054
  if (askers !== "write") {
@@ -4877,5 +5074,5 @@ export function comment(deps: Deps, repo: string, pr: number, request: string, a
4877
5074
  if (!ledger.latestIsLimit) postAskLimit(deps, repo, pr, ledger.spent);
4878
5075
  return;
4879
5076
  }
4880
- answer(deps, repo, pr, request, { asker: author, line });
5077
+ await answer(deps, repo, pr, request, { asker: author, line });
4881
5078
  }