wachiman 1.15.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 (54) hide show
  1. package/README.md +1 -0
  2. package/package.json +1 -1
  3. package/src/accounts/commands.ts +3 -2
  4. package/src/accounts/confirm.ts +1 -1
  5. package/src/accounts/profile-list.ts +24 -0
  6. package/src/cli.ts +54 -20
  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 +280 -97
  12. package/src/help.ts +85 -3
  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 +204 -24
  18. package/src/init/settings.ts +127 -0
  19. package/src/init/write.ts +16 -102
  20. package/src/local-ask.ts +5 -4
  21. package/src/local.ts +55 -10
  22. package/src/loop/command.ts +5 -5
  23. package/src/pr-template/adopt.ts +434 -0
  24. package/src/pr-template/catalog.ts +429 -0
  25. package/src/pr-template/check-run.ts +328 -0
  26. package/src/pr-template/check.ts +338 -0
  27. package/src/pr-template/describe.ts +368 -0
  28. package/src/pr-template/format.ts +399 -0
  29. package/src/pr-template/gh.ts +159 -0
  30. package/src/pr-template/install.ts +288 -0
  31. package/src/pr-template/legacy-guides.ts +50 -0
  32. package/src/pr-template/pr-risk.ts +426 -0
  33. package/src/pr-template/render.ts +302 -0
  34. package/src/pr-template/resolve.ts +65 -0
  35. package/src/pr-template/run.ts +88 -0
  36. package/src/pr-template/strip.ts +171 -0
  37. package/src/pr-template/write.ts +139 -0
  38. package/src/providers/claude-code.ts +7 -8
  39. package/src/providers/codex.ts +4 -4
  40. package/src/providers/spawn.ts +95 -0
  41. package/src/providers/types.ts +5 -2
  42. package/src/repo-settings.ts +159 -0
  43. package/src/setup/prompt.ts +49 -33
  44. package/src/setup/run.ts +31 -6
  45. package/src/sinks/common.ts +10 -3
  46. package/src/sinks/markdown.ts +3 -2
  47. package/src/sinks/terminal.ts +3 -2
  48. package/src/upgrade/command.ts +286 -0
  49. package/src/upgrade/pin.ts +112 -0
  50. package/src/wizard/mascot.ts +56 -51
  51. package/src/wizard/prompt-fancy.ts +10 -2
  52. package/src/wizard/prompt.ts +17 -4
  53. package/src/wizard/ui.ts +2 -0
  54. 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`,
@@ -2687,9 +2743,9 @@ function grantTooSmall(deps: Deps, grant: SpendGrant, what: string): boolean {
2687
2743
  }
2688
2744
 
2689
2745
  /** Runs a paid step; if it throws, the ledger still hears what the calls so far cost, then the error goes on. */
2690
- function settledOnError<T>(deps: Deps, grant: SpendGrant, run: () => T): T {
2746
+ async function settledOnError<T>(deps: Deps, grant: SpendGrant, run: () => Promise<T>): Promise<T> {
2691
2747
  try {
2692
- return run();
2748
+ return await run();
2693
2749
  } catch (error) {
2694
2750
  spendSettle(deps, grant, spentSince(deps, grant));
2695
2751
  throw error;
@@ -2874,12 +2930,12 @@ export function postReview(deps: Deps, repo: string, pr: number, headSha: string
2874
2930
  * Runs one call so that its failure does not end the review: the batches that did run still
2875
2931
  * post. A call that died without a result is counted at its cap, since what it spent is unknown.
2876
2932
  */
2877
- export function attempt<T>(
2878
- run: () => T,
2933
+ export async function attempt<T>(
2934
+ run: () => Promise<T> | T,
2879
2935
  capUsd: number,
2880
- ): { ok: true; value: T } | { ok: false; error: string; costUsd: number } {
2936
+ ): Promise<{ ok: true; value: T } | { ok: false; error: string; costUsd: number }> {
2881
2937
  try {
2882
- return { ok: true, value: run() };
2938
+ return { ok: true, value: await run() };
2883
2939
  } catch (error) {
2884
2940
  return { ok: false, error: String(error).slice(0, 300), costUsd: capUsd };
2885
2941
  }
@@ -2944,7 +3000,7 @@ export function reviewOptionsOf(config: EngineConfig, totalBudgetUsd: number, pr
2944
3000
  * checks, coverage, the one retry, budget arithmetic, merging, the confidence rule, dedupe, the
2945
3001
  * ADR notices, the anchors and the "not reached" skips are here. Posting is not.
2946
3002
  */
2947
- export function reviewChange(input: ReviewInput, options: ReviewOptions, deps: CoreDeps): ReviewResult {
3003
+ export async function reviewChange(input: ReviewInput, options: ReviewOptions, deps: CoreDeps): Promise<ReviewResult> {
2948
3004
  assertReviewInputFormat(input);
2949
3005
  const started = Date.now();
2950
3006
  // Every warning the core logs is also a notice of the result; `runProvider` appends one entry
@@ -3008,7 +3064,7 @@ export function reviewChange(input: ReviewInput, options: ReviewOptions, deps: C
3008
3064
  // is spent before the batches, so they cannot overspend the pull request's budget.
3009
3065
  const confidence = change.kind === "pull-request" ? options.confidence : undefined;
3010
3066
  const claimsStep = confidence
3011
- ? listClaims(core, options, context, Math.min(confidence.budgetUsd, batchTotal / 4))
3067
+ ? await listClaims(core, options, context, Math.min(confidence.budgetUsd, batchTotal / 4))
3012
3068
  : undefined;
3013
3069
  if (claimsStep) {
3014
3070
  spent += claimsStep.costUsd;
@@ -3035,51 +3091,88 @@ export function reviewChange(input: ReviewInput, options: ReviewOptions, deps: C
3035
3091
  else rejection ??= { kind: r.kind, text: r.text };
3036
3092
  };
3037
3093
 
3038
- batches.forEach((batch, i) => {
3039
- const label = `Batch ${i + 1} of ${batches.length}`;
3040
- const remaining = () => batchTotal - spent;
3041
- if (rejection) {
3042
- rejectedFiles.push(...batch.files.map((f) => f.filename));
3043
- reports.push({
3044
- label,
3045
- files: batch.files.length,
3046
- lines: batch.lines,
3047
- maxRisk: batch.maxRisk,
3048
- status: "not run: provider rejected the call",
3049
- turns: 0,
3050
- costUsd: 0,
3051
- });
3052
- return;
3053
- }
3054
- if (remaining() < MIN_BATCH_USD) {
3055
- notRun.push(...batch.files.map((f) => f.filename));
3056
- reports.push({
3057
- label,
3058
- files: batch.files.length,
3059
- lines: batch.lines,
3060
- maxRisk: batch.maxRisk,
3061
- status: "not run: budget spent",
3062
- turns: 0,
3063
- costUsd: 0,
3064
- });
3065
- 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();
3066
3139
  }
3067
- 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(
3068
3157
  core,
3069
3158
  options,
3070
3159
  context,
3071
3160
  batch.files,
3072
3161
  threadsByBatch[i],
3073
3162
  { index: i + 1, total: batches.length },
3074
- Math.min(batchBudget, remaining()),
3163
+ firstBudget,
3075
3164
  );
3165
+ // The reservation gives way to what the call really cost.
3076
3166
  spent += first.costUsd;
3077
- usage = addUsage(usage, first.usage);
3167
+ reserved.delete(i);
3168
+ notify();
3169
+ out.usage = addUsage(out.usage, first.usage);
3078
3170
  noteRejection(first.rejection);
3079
- if (first.output) parts.push({ label, output: first.output });
3080
- if (first.budget) budgetError = true;
3081
- if (first.stopped) limitError = first.stopped;
3082
- 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;
3083
3176
  let status = first.output
3084
3177
  ? "reviewed"
3085
3178
  : first.budget
@@ -3096,53 +3189,127 @@ export function reviewChange(input: ReviewInput, options: ReviewOptions, deps: C
3096
3189
  let turns = first.turns;
3097
3190
  let cost = first.costUsd;
3098
3191
 
3099
- // 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.
3100
3193
  const gaps = batchGaps(batch.files, first.output);
3101
3194
  // After a wall-clock kill the same files would likely run into the same limit, and another
3102
3195
  // full timeout could exhaust the job's own limit: they are reported as not reviewed instead.
3103
3196
  // A refused call (no credit, bad key) would be refused again: no retry.
3104
- if (gaps.length > 0 && first.stopped !== "timeout" && !rejection && remaining() >= MIN_BATCH_USD) {
3105
- 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(
3106
3202
  core,
3107
3203
  options,
3108
3204
  context,
3109
3205
  gaps,
3110
3206
  [],
3111
3207
  { index: i + 1, total: batches.length, retry: true },
3112
- Math.min(batchBudget, remaining()),
3208
+ grant.usd,
3113
3209
  );
3114
3210
  spent += retry.costUsd;
3115
- usage = addUsage(usage, retry.usage);
3211
+ reserved.delete(i);
3212
+ notify();
3213
+ out.usage = addUsage(out.usage, retry.usage);
3116
3214
  turns += retry.turns;
3117
3215
  cost += retry.costUsd;
3118
3216
  noteRejection(retry.rejection);
3119
- if (retry.budget || retry.stopped) anyLimit = true;
3120
- 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 });
3121
3220
  const left = batchGaps(gaps, retry.output);
3122
- if (retry.rejection && retry.rejection.kind !== "other") rejectedFiles.push(...left.map((f) => f.filename));
3123
- 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));
3124
3223
  status =
3125
3224
  left.length === 0 ? "reviewed after a retry" : `${left.length} file(s) left after a retry`;
3126
3225
  } else if (gaps.length > 0) {
3127
- 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
3128
3232
  ? `${gaps.length} file(s) left: the provider rejected the call`
3129
3233
  : first.stopped === "timeout"
3130
3234
  ? `${gaps.length} file(s) left: the call timed out`
3131
3235
  : `${gaps.length} file(s) left: budget spent`;
3132
3236
  // A refused call or a spent budget is not the files' doing; a timeout may be, so it stays unexplained.
3133
- if (rejection) rejectedFiles.push(...gaps.map((f) => f.filename));
3134
- 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));
3135
3239
  }
3136
- reports.push({
3137
- label,
3138
- files: batch.files.length,
3139
- lines: batch.lines,
3140
- maxRisk: batch.maxRisk,
3141
- status,
3142
- turns,
3143
- 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: [],
3144
3257
  });
3145
- });
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;
3146
3313
 
3147
3314
  const merged: ReviewOutput =
3148
3315
  parts.length > 0
@@ -3166,7 +3333,7 @@ export function reviewChange(input: ReviewInput, options: ReviewOptions, deps: C
3166
3333
  let verification: ResultVerificationV2 | undefined;
3167
3334
  let checked: VerifiedFinding[];
3168
3335
  if (options.verify && parts.length > 0) {
3169
- 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);
3170
3337
  spent += verified.record.costUsd;
3171
3338
  usage = addUsage(usage, verified.usage);
3172
3339
  checked = verified.findings;
@@ -3195,9 +3362,6 @@ export function reviewChange(input: ReviewInput, options: ReviewOptions, deps: C
3195
3362
  const known = new Set(threads.map((t) => t.comment_id));
3196
3363
  const threadReplies = merged.thread_replies.filter((r) => known.has(r.comment_id));
3197
3364
 
3198
- // Assigned inside the loop's callbacks, which the compiler does not follow.
3199
- const refused = rejection as { kind: Exclude<ProviderErrorClass, "other">; text: string } | undefined;
3200
- const other = otherError as string | undefined;
3201
3365
  const firstRejection = refused ?? (other !== undefined ? { kind: "other" as const, text: other } : undefined);
3202
3366
  // In today's precedence: a refused account stops everything; a review with output, or nothing
3203
3367
  // to review, is complete (coverage says whether it is partial); otherwise the limit that ended
@@ -3310,12 +3474,12 @@ export function reviewChange(input: ReviewInput, options: ReviewOptions, deps: C
3310
3474
  * from the author's text alone (Wachiman's own block stripped). It fails closed: with no list the
3311
3475
  * review still runs, and the grade is `cannot say`. A call that cannot be afforded is not made.
3312
3476
  */
3313
- function listClaims(
3477
+ async function listClaims(
3314
3478
  deps: CoreDeps,
3315
3479
  options: ReviewOptions,
3316
3480
  ctx: ReviewContext,
3317
3481
  budgetUsd: number,
3318
- ): { claims?: ProposedClaim[]; costUsd: number; usage: TokenUsage; budget: boolean; truncated: boolean } {
3482
+ ): Promise<{ claims?: ProposedClaim[]; costUsd: number; usage: TokenUsage; budget: boolean; truncated: boolean }> {
3319
3483
  const input = claimsInput(ctx.meta.title ?? "", ctx.meta.body);
3320
3484
  const truncated = claimsTextTruncated(input);
3321
3485
  const none = { costUsd: 0, usage: NO_USAGE, budget: false, truncated };
@@ -3324,7 +3488,7 @@ function listClaims(
3324
3488
  return { ...none, budget: true };
3325
3489
  }
3326
3490
  const prompt = buildClaimsPrompt(input);
3327
- const call = attempt(
3491
+ const call = await attempt(
3328
3492
  () => runProvider(deps, options, ctx.headDir, { operation: "review", label: "claims", ...ctx.costTags }, prompt, ctx.headDir, budgetUsd, CLAIMS_SCHEMA),
3329
3493
  budgetUsd,
3330
3494
  );
@@ -3370,7 +3534,7 @@ function verdictsOf(structured: unknown): VerifierVerdict[] | undefined {
3370
3534
  * `inconclusive` with the stop as their reason. Findings it left at low confidence go through the
3371
3535
  * confidence rule again.
3372
3536
  */
3373
- function verifyFindings(
3537
+ async function verifyFindings(
3374
3538
  deps: CoreDeps,
3375
3539
  options: ReviewOptions & { verify: { model: string; budgetUsd: number } },
3376
3540
  ctx: ReviewContext,
@@ -3378,7 +3542,7 @@ function verifyFindings(
3378
3542
  files: ReviewFile[],
3379
3543
  budgetUsd: number,
3380
3544
  refused?: string,
3381
- ): { findings: VerifiedFinding[]; record: ResultVerificationV2; usage: TokenUsage } {
3545
+ ): Promise<{ findings: VerifiedFinding[]; record: ResultVerificationV2; usage: TokenUsage }> {
3382
3546
  const verifier = deps.verifier ?? deps.provider;
3383
3547
  // Without its own adapter the call runs on the reviewer's, whose model and effort are fixed at construction.
3384
3548
  const ran = deps.verifier ? { model: options.verify.model, effort: "high" } : { model: options.model, effort: options.effort };
@@ -3419,7 +3583,7 @@ function verifyFindings(
3419
3583
  const prompt = buildVerifyPrompt({ describe: ctx.describe, headSha: ctx.headSha, headDir: ctx.headDir, baseDir: baseSnap?.dir, candidates, files });
3420
3584
  dir = mkdtempSync(join(tmpdir(), "agent-verify-"));
3421
3585
  const runDir = dir;
3422
- const call = attempt(
3586
+ const call = await attempt(
3423
3587
  () =>
3424
3588
  runProvider(
3425
3589
  { ...deps, provider: verifier },
@@ -3584,7 +3748,7 @@ export function githubReview(
3584
3748
  }
3585
3749
 
3586
3750
  /** `force` (`--force`) counts only for the operator's own run, never an automatic trigger. */
3587
- 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) {
3588
3752
  const { gh, config } = deps;
3589
3753
  const meta = JSON.parse(
3590
3754
  gh(["pr", "view", String(pr), "-R", repo, "--json", "headRefOid,title,body,author"]),
@@ -3826,7 +3990,7 @@ export function review(deps: Deps, repo: string, pr: number, words: string[], fo
3826
3990
  ...reviewOptionsOf(config, totalBudget),
3827
3991
  ...(config.confidence && fullReview ? { confidence: { budgetUsd: CLAIMS_BUDGET_USD } } : {}),
3828
3992
  };
3829
- const result = settledOnError(deps, grant, () => reviewChange(input, options, deps));
3993
+ const result = await settledOnError(deps, grant, () => reviewChange(input, options, deps));
3830
3994
  const { usage, costUsdEstimate: spent } = result.execution;
3831
3995
  const operation: Operation = since ? "re-review" : "review";
3832
3996
  const receiptLine = spendSettle(deps, grant, spent) ?? undefined;
@@ -4052,7 +4216,7 @@ Answer every thread once. Answer no thread that is not in the file.`;
4052
4216
  * It is an automatic run: it counts against the pull request's ceiling. It follows ADR 0008: it
4053
4217
  * reports, it never resolves.
4054
4218
  */
4055
- export function threadReplies(deps: Deps, repo: string, pr: number, author?: string) {
4219
+ export async function threadReplies(deps: Deps, repo: string, pr: number, author?: string) {
4056
4220
  const { gh, config } = deps;
4057
4221
  // As in `comment`: before anything is read, so a stranger's reply on a public repository (or a
4058
4222
  // placeholder operator) starts no paid step. The workflow's `if:` checks the same login.
@@ -4097,7 +4261,7 @@ export function threadReplies(deps: Deps, repo: string, pr: number, author?: str
4097
4261
  const grant = spendReserve(deps, repo, pr, headSha, Math.min(config.threadsBudgetUsd, ceiling.room), false);
4098
4262
  const budgetUsd = Math.min(config.threadsBudgetUsd, ceiling.room, grant.roomUsd);
4099
4263
  if (grantTooSmall(deps, grant, "thread replies")) return;
4100
- const call = settledOnError(deps, grant, () =>
4264
+ const call = await settledOnError(deps, grant, () =>
4101
4265
  runProvider(
4102
4266
  deps,
4103
4267
  config,
@@ -4415,8 +4579,8 @@ function gatherEvidence(deps: CoreDeps, headDir: string, files: ReviewFile[], di
4415
4579
  type CallLimits = Pick<EngineConfig, "maxTurns" | "callTimeoutS" | "model" | "trigger">;
4416
4580
 
4417
4581
  /** One provider call over one batch, with the batch's diff inside the prompt. */
4418
- function reviewBatch(
4419
- deps: CoreDeps,
4582
+ async function reviewBatch(
4583
+ core: CoreDeps,
4420
4584
  limits: CallLimits,
4421
4585
  ctx: ReviewContext,
4422
4586
  files: ReviewFile[],
@@ -4424,6 +4588,8 @@ function reviewBatch(
4424
4588
  part: { index: number; total: number; retry?: boolean },
4425
4589
  budgetUsd: number,
4426
4590
  ) {
4591
+ // Batches run side by side, so every line this one prints names it.
4592
+ const deps = attributed(core, part);
4427
4593
  const dir = mkdtempSync(join(tmpdir(), "wachiman-review-"));
4428
4594
  // The run dir holds the change's text (title, body, threads, evidence): removed once the call
4429
4595
  // is over, whatever happened. Nothing reads it after the provider returns.
@@ -4454,7 +4620,7 @@ function reviewBatch(
4454
4620
  );
4455
4621
 
4456
4622
  const label = `batch ${part.index}/${part.total}${part.retry ? " retry" : ""}`;
4457
- const call = attempt(
4623
+ const call = await attempt(
4458
4624
  () =>
4459
4625
  runProvider(
4460
4626
  deps,
@@ -4505,17 +4671,33 @@ function reviewBatch(
4505
4671
  /** Where a rate-limited call waits before its one retry, in ms. Overridable through `Deps` so tests do not sleep. */
4506
4672
  const RATE_LIMIT_BACKOFF_MS = 20_000;
4507
4673
 
4508
- 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
+ }
4509
4691
 
4510
4692
  /** `reviewBatch`, with one retry of the same batch after a backoff when the API rate-limited the call. */
4511
- function reviewBatchBackingOff(...args: Parameters<typeof reviewBatch>): BatchResult {
4693
+ async function reviewBatchBackingOff(...args: Parameters<typeof reviewBatch>): Promise<BatchResult> {
4512
4694
  const [deps, , , , , part] = args;
4513
- const first = reviewBatch(...args);
4695
+ const first = await reviewBatch(...args);
4514
4696
  if (first.rejection?.kind !== "rate_limit") return first;
4515
4697
  const wait = deps.rateLimitBackoffMs ?? RATE_LIMIT_BACKOFF_MS;
4516
4698
  deps.warn(`Batch ${part.index} of ${part.total} was rate-limited; retrying once in ${wait / 1000} s.`);
4517
- (deps.sleep ?? blockingSleep)(wait);
4518
- const again = reviewBatch(...args);
4699
+ await pause(deps, wait);
4700
+ const again = await reviewBatch(...args);
4519
4701
  return {
4520
4702
  ...again,
4521
4703
  turns: first.turns + again.turns,
@@ -4525,7 +4707,7 @@ function reviewBatchBackingOff(...args: Parameters<typeof reviewBatch>): BatchRe
4525
4707
  }
4526
4708
 
4527
4709
  /** The prompt goes through stdin: a batch's diff in an argument would hit Linux's 128 KB limit. */
4528
- export function runProvider(
4710
+ export async function runProvider(
4529
4711
  deps: CoreDeps,
4530
4712
  limits: CallLimits,
4531
4713
  cwd: string,
@@ -4538,9 +4720,9 @@ export function runProvider(
4538
4720
  extraDirs: string[] = [],
4539
4721
  ) {
4540
4722
  const started = Date.now();
4541
- let result: ReturnType<ProviderAdapter["run"]>;
4723
+ let result: Awaited<ReturnType<ProviderAdapter["run"]>>;
4542
4724
  try {
4543
- result = deps.provider.run({
4725
+ result = await deps.provider.run({
4544
4726
  prompt,
4545
4727
  cwd,
4546
4728
  addDirs: [dir, ...extraDirs],
@@ -4574,6 +4756,7 @@ export function runProvider(
4574
4756
  if (result.costKnown === false) result = { ...result, costUsd: budgetUsd };
4575
4757
  deps.log(
4576
4758
  JSON.stringify({
4759
+ label: what.label,
4577
4760
  is_error: result.isError,
4578
4761
  terminal_reason: result.terminalReason,
4579
4762
  turns: result.turns,
@@ -4717,7 +4900,7 @@ function postAnswer(deps: Deps, repo: string, pr: number, body: string, line?: R
4717
4900
  * For someone but the operator, the answer is marked with its cost (`readAskCost`), and a call that
4718
4901
  * fails still posts a note with its cost, so the question ceiling counts what was spent.
4719
4902
  */
4720
- 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 = {}) {
4721
4904
  const { asker, line } = options;
4722
4905
  const dir = mkdtempSync(join(tmpdir(), "agent-answer-"));
4723
4906
  const meta = deps.gh(["pr", "view", String(pr), "-R", repo, "--json", "title,body,files,headRefOid"]);
@@ -4754,10 +4937,10 @@ export function answer(deps: Deps, repo: string, pr: number, question: string, o
4754
4937
  else postAnswer(deps, repo, pr, body, line);
4755
4938
  return;
4756
4939
  }
4757
- let result: ReturnType<typeof runProvider> | undefined;
4940
+ let result: Awaited<ReturnType<typeof runProvider>> | undefined;
4758
4941
  let failure = "";
4759
4942
  try {
4760
- result = runProvider(
4943
+ result = await runProvider(
4761
4944
  deps,
4762
4945
  deps.config,
4763
4946
  cwd,
@@ -4848,7 +5031,7 @@ function postAskLimit(deps: Deps, repo: string, pr: number, spent: number) {
4848
5031
  * write access, and while the pull request's question ceiling has room (ADR 0032 decision 9).
4849
5032
  * An empty or placeholder operator is an error.
4850
5033
  */
4851
- 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) {
4852
5035
  const { operator, askers } = deps.config;
4853
5036
  // Before anything else: an unset or placeholder operator would let a stranger who registered
4854
5037
  // that login command the tool.
@@ -4863,9 +5046,9 @@ export function comment(deps: Deps, repo: string, pr: number, request: string, a
4863
5046
  if (command?.kind === "review") {
4864
5047
  // With no ledger `--force` means nothing: it stays a word that names no file, as it always did.
4865
5048
  const words = deps.config.ledgerUrl ? command.paths.filter((w) => w !== "--force") : command.paths;
4866
- review(deps, repo, pr, words, command.force);
5049
+ await review(deps, repo, pr, words, command.force);
4867
5050
  }
4868
- else if (command) answer(deps, repo, pr, request, { line });
5051
+ else if (command) await answer(deps, repo, pr, request, { line });
4869
5052
  return;
4870
5053
  }
4871
5054
  if (askers !== "write") {
@@ -4891,5 +5074,5 @@ export function comment(deps: Deps, repo: string, pr: number, request: string, a
4891
5074
  if (!ledger.latestIsLimit) postAskLimit(deps, repo, pr, ledger.spent);
4892
5075
  return;
4893
5076
  }
4894
- answer(deps, repo, pr, request, { asker: author, line });
5077
+ await answer(deps, repo, pr, request, { asker: author, line });
4895
5078
  }