wachiman 1.12.0 → 1.13.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 (41) hide show
  1. package/README.md +132 -101
  2. package/package.json +1 -1
  3. package/skills/wachiman-loop/SKILL.md +91 -0
  4. package/src/call-message.ts +26 -3
  5. package/src/cli.ts +81 -5
  6. package/src/config.ts +19 -0
  7. package/src/connectors/local-git.ts +101 -13
  8. package/src/cost.ts +20 -0
  9. package/src/doctor/checks.ts +1 -1
  10. package/src/engine.ts +532 -55
  11. package/src/estimate.ts +64 -0
  12. package/src/fidelity/block.ts +263 -0
  13. package/src/fidelity/claims-call.ts +172 -0
  14. package/src/fidelity/claims.ts +69 -0
  15. package/src/fidelity/digest.ts +80 -0
  16. package/src/fidelity/grade.ts +242 -0
  17. package/src/fidelity/inert.ts +25 -0
  18. package/src/fidelity/rules.ts +166 -0
  19. package/src/fidelity/sink.ts +135 -0
  20. package/src/fidelity/summary.ts +16 -0
  21. package/src/fidelity/types.ts +93 -0
  22. package/src/formats.ts +16 -3
  23. package/src/history/fingerprint.ts +1 -1
  24. package/src/history/store.ts +39 -1
  25. package/src/init/options.ts +1 -1
  26. package/src/local.ts +20 -2
  27. package/src/loop/command.ts +441 -0
  28. package/src/loop/state.ts +88 -0
  29. package/src/loop/stop-rule.ts +98 -0
  30. package/src/providers/claude-code.ts +47 -12
  31. package/src/providers/codex.ts +3 -2
  32. package/src/providers/index.ts +5 -2
  33. package/src/providers/types.ts +10 -0
  34. package/src/sinks/json.ts +13 -2
  35. package/src/skills/install.ts +4 -4
  36. package/src/store/command.ts +109 -0
  37. package/src/store/figures.ts +153 -0
  38. package/src/store/html.ts +138 -0
  39. package/src/store/index.ts +298 -0
  40. package/templates/caller-workflow.yml +45 -15
  41. package/templates/wachiman-review-caller.yml +8 -0
package/src/config.ts CHANGED
@@ -23,6 +23,12 @@ export interface EngineConfig {
23
23
  * footers. The operator's `comment` is not limited by it.
24
24
  */
25
25
  autoBudgetUsd: number;
26
+ /**
27
+ * `AGENT_REVIEW_SIZE_GATE_USD`: the most a review's estimate may be before a GitHub run skips it
28
+ * and posts one comment instead. The effective gate is never below what the run may spend
29
+ * (`sizeGate` in estimate.ts), so raising the budgets lifts it. Local runs only warn.
30
+ */
31
+ sizeGateUsd: number;
26
32
  /** `AGENT_REVIEW_THREADS_BUDGET_USD`: the one provider call of a thread-only run. */
27
33
  threadsBudgetUsd: number;
28
34
  /**
@@ -91,6 +97,13 @@ export interface EngineConfig {
91
97
  verifyBudgetUsd: number;
92
98
  /** `AGENT_REVIEW_VERIFIER_MODEL`: the verifier's model; it always runs at effort `high`. */
93
99
  verifierModel: string;
100
+ /**
101
+ * `AGENT_REVIEW_CONFIDENCE`: PR Confidence (ADR 0033). A full review of a pull request lists the
102
+ * claims in its title and body with one more small call, grades the change against them and
103
+ * writes the grade into the description. Off by default; enabled only by `1` or `true`. Read by
104
+ * the pull request review only: `wachiman local` never grades.
105
+ */
106
+ confidence: boolean;
94
107
  }
95
108
 
96
109
  /** The verifier's model when `AGENT_REVIEW_VERIFIER_MODEL` is unset (Claude Code only). */
@@ -171,6 +184,10 @@ export function loadConfig(env: Env, warn: (line: string) => void = console.erro
171
184
  if (verify && verifierModel === undefined) {
172
185
  throw new UsageError(`AGENT_REVIEW_VERIFY needs AGENT_REVIEW_VERIFIER_MODEL with the ${provider} provider: the default verifier is a Claude model.`);
173
186
  }
187
+ const confidenceRaw = get("CONFIDENCE");
188
+ if (confidenceRaw !== undefined && !["0", "1", "true", "false"].includes(confidenceRaw)) {
189
+ throw new UsageError(`AGENT_REVIEW_CONFIDENCE must be 1, true, 0 or false (unset means off), got "${confidenceRaw}".`);
190
+ }
174
191
  const askers = get("ASKERS") ?? "operator";
175
192
  if (askers !== "operator" && askers !== "write") {
176
193
  throw new UsageError(`AGENT_REVIEW_ASKERS must be operator or write, got "${askers}".`);
@@ -182,6 +199,7 @@ export function loadConfig(env: Env, warn: (line: string) => void = console.erro
182
199
  return {
183
200
  budgetUsd: number("BUDGET_USD", 3),
184
201
  autoBudgetUsd: number("AUTO_BUDGET_USD", 6),
202
+ sizeGateUsd: number("SIZE_GATE_USD", 6),
185
203
  threadsBudgetUsd: number("THREADS_BUDGET_USD", 0.25),
186
204
  trigger: trigger || undefined,
187
205
  batchBudgetUsd: number("BATCH_BUDGET_USD", 0.75),
@@ -205,6 +223,7 @@ export function loadConfig(env: Env, warn: (line: string) => void = console.erro
205
223
  verify,
206
224
  verifyBudgetUsd: number("VERIFY_BUDGET_USD", 1),
207
225
  verifierModel: verifierModel ?? DEFAULT_VERIFIER_MODEL,
226
+ confidence: confidenceRaw === "1" || confidenceRaw === "true",
208
227
  };
209
228
  }
210
229
 
@@ -5,13 +5,14 @@
5
5
  * head the agent reads is a snapshot, so later edits of the working tree change nothing.
6
6
  *
7
7
  * The snapshot is a `git archive` of the head extracted into a temporary directory, with an index
8
- * of its own (`git init`, `git add -A -f`, no commit): the evidence step runs `git grep` in the
9
- * head, and in a plain directory that fails (spec correction 1 of the plan).
8
+ * of its own (`git init`, the clone's objects as alternates, `git read-tree`, trimmed to the
9
+ * extracted files, no commit): the evidence step runs `git grep` in the head, and in a plain
10
+ * directory that fails (spec correction 1 of the plan).
10
11
  */
11
12
  import { spawnSync } from "node:child_process";
12
- import { existsSync, mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
13
+ import { existsSync, lstatSync, mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
13
14
  import { tmpdir } from "node:os";
14
- import { basename, join } from "node:path";
15
+ import { basename, dirname, join, resolve } from "node:path";
15
16
  import { UsageError } from "../config";
16
17
  import {
17
18
  instructionFileOf,
@@ -367,29 +368,80 @@ export function tarCommand(platform: string = process.platform, env: Record<stri
367
368
  }
368
369
 
369
370
  /**
370
- * The committed `head` as a directory: `git archive` extracted with the system `tar`, then an
371
- * index of its own so `git grep` sees exactly the archived files. `missing` and `extra` compare the
372
- * snapshot with `git ls-tree` of the head (export-ignore and submodules make them differ).
371
+ * Time allowed to each snapshot step (archive, tar, read-tree, ls-files, update-index) for a head of
372
+ * `fileCount` files: the old 60 s floor plus 3 ms per file. `tar -xf` was measured at 9 s for 31.9k
373
+ * files / 660 MB (about 0.3 ms per file) on a loaded laptop; 3 ms per file is 10x that rate, so 31.9k
374
+ * files get about 156 s.
375
+ */
376
+ export const snapshotTimeoutMs = (fileCount: number) => 60_000 + fileCount * 3;
377
+
378
+ /** The error for a snapshot step that ran out of time: which step, on how many files, what to do. */
379
+ function snapshotTimeout(step: string, ms: number, fileCount: number) {
380
+ return new Error(
381
+ `Snapshot step "${step}" timed out after ${Math.round(ms / 1000)} s on a head of ${fileCount.toLocaleString("en-US")} files. ` +
382
+ `The machine may be busy or the disk slow; retry when it is less loaded, or free space in the temporary directory (${tmpdir()}).`,
383
+ );
384
+ }
385
+
386
+ /**
387
+ * The committed `head` as a directory: `git archive` extracted with the system `tar`, then an index
388
+ * of its own so `git grep` sees exactly the archived files. The index is not built by hashing the
389
+ * files (`git add -A -f` took ~50 s on 32k files): the snapshot borrows the clone's objects through
390
+ * `objects/info/alternates` (read-only; nothing is written to the clone), `git read-tree <head>`
391
+ * fills the index from the tree, and the index is trimmed to what `tar` extracted (read-tree also
392
+ * lists submodule gitlinks and export-ignore paths). If read-tree fails (a partial clone lacks tree
393
+ * objects) the old `git add -A -f` runs instead: slower, same result. `missing` and `extra` compare
394
+ * the snapshot with `git ls-tree` of the head (export-ignore and submodules make them differ).
395
+ * Steps get `snapshotTimeoutMs` of the head's file count.
373
396
  */
374
397
  export function snapshotHead(repoDir: string, head: string, git: GitRunner = spawnGit): { dir: string; missing: string[]; extra: string[] } {
375
398
  const dir = realpathSync(mkdtempSync(join(tmpdir(), "wachiman-snapshot-")));
376
399
  // Beside the snapshot, not inside it: a file of the same name in the head would collide.
377
400
  const tar = `${dir}.tar`;
378
401
  try {
402
+ const tree = new Set(names(run(git, repoDir, ["ls-tree", "-r", "-z", "--name-only", head], DIFF_MAX_BYTES)));
403
+ const files = tree.size;
404
+ const limit = snapshotTimeoutMs(files);
405
+ const step = (label: string, cwd: string, args: string[]) => {
406
+ const out = git(args, { cwd, timeoutMs: limit, maxBytes: DIFF_MAX_BYTES });
407
+ if (out.timedOut) throw snapshotTimeout(label, limit, files);
408
+ if (out.status !== 0 || out.error || out.outputCut) {
409
+ const why = out.outputCut ? "output too large" : (out.error ?? out.stderr?.trim() ?? `status ${out.status}`);
410
+ throw new Error(`git ${args.join(" ")} failed in ${cwd}: ${why}`);
411
+ }
412
+ return out.stdout;
413
+ };
379
414
  // core.autocrlf=true (the Git for Windows default) and core.eol=native with a `text=auto` attribute
380
415
  // would both write CRLF into the archive. The snapshot should hold the committed bytes, so both are
381
416
  // switched off for this one command (a file with an explicit `eol=crlf` attribute is still converted).
382
- run(git, repoDir, ["-c", "core.autocrlf=false", "-c", "core.eol=lf", "archive", "--format=tar", `--output=${tar}`, head]);
383
- const untar = spawnSync(tarCommand(), ["-xf", tar], { cwd: dir, encoding: "utf8", timeout: TIMEOUT_MS });
417
+ step("git archive", repoDir, ["-c", "core.autocrlf=false", "-c", "core.eol=lf", "archive", "--format=tar", `--output=${tar}`, head]);
418
+ const untar = spawnSync(tarCommand(), ["-xf", tar], { cwd: dir, encoding: "utf8", timeout: limit });
419
+ if ((untar.error as NodeJS.ErrnoException | undefined)?.code === "ETIMEDOUT") throw snapshotTimeout("tar -xf", limit, files);
384
420
  if (untar.status !== 0) throw new Error(`tar -xf failed: ${untar.stderr || untar.error}`);
385
421
  rmSync(tar, { force: true });
386
- run(git, dir, ["init", "-q", "--template="]);
422
+ step("git init", dir, ["init", "-q", "--template="]);
387
423
  // No clean or smudge filter (LFS, git-crypt) runs on the copy: the index only lists the files.
388
424
  mkdirSync(join(dir, ".git", "info"), { recursive: true });
389
425
  writeFileSync(join(dir, ".git", "info", "attributes"), "* -filter\n");
390
- run(git, dir, ["add", "-A", "-f"]);
391
- const tree = new Set(names(run(git, repoDir, ["ls-tree", "-r", "-z", "--name-only", head], DIFF_MAX_BYTES)));
392
- const copied = new Set(names(run(git, dir, ["ls-files", "-z"], DIFF_MAX_BYTES)));
426
+ // `--git-path objects` is the common dir's objects for a linked worktree, and may be relative to `repoDir`.
427
+ const objects = resolve(repoDir, run(git, repoDir, ["rev-parse", "--git-path", "objects"]).trim());
428
+ const alternates = join(dir, ".git", "objects", "info", "alternates");
429
+ mkdirSync(dirname(alternates), { recursive: true });
430
+ writeFileSync(alternates, `${objects}\n`);
431
+ const read = git(["read-tree", head], { cwd: dir, timeoutMs: limit });
432
+ if (read.timedOut) throw snapshotTimeout("git read-tree", limit, files);
433
+ if (read.status !== 0 || read.error) {
434
+ // Tree objects missing here (a partial clone): hash the files instead. Never fetch.
435
+ rmSync(alternates, { force: true });
436
+ rmSync(join(dir, ".git", "index"), { force: true });
437
+ rmSync(join(dir, ".git", "index.lock"), { force: true });
438
+ step("git add", dir, ["add", "-A", "-f"]);
439
+ } else {
440
+ trimIndex(dir, git, limit, files);
441
+ }
442
+ const copied = new Set(names(step("git ls-files", dir, ["ls-files", "-z"])));
443
+ // Files on disk that the head does not have (what `git add -A -f` used to add): a directory walk, no hashing.
444
+ for (const f of names(step("git ls-files --others", dir, ["ls-files", "-z", "--others"]))) copied.add(f);
393
445
  return {
394
446
  dir,
395
447
  missing: [...tree].filter((f) => !copied.has(f)),
@@ -401,6 +453,42 @@ export function snapshotHead(repoDir: string, head: string, git: GitRunner = spa
401
453
  }
402
454
  }
403
455
 
456
+ /**
457
+ * Drops from the snapshot's index what `tar` did not extract as a file or a symlink: submodule
458
+ * gitlinks (mode 160000), export-ignore paths and anything else not on disk.
459
+ */
460
+ function trimIndex(dir: string, git: GitRunner, limit: number, files: number) {
461
+ const listing = git(["ls-files", "-s", "-z"], { cwd: dir, timeoutMs: limit, maxBytes: DIFF_MAX_BYTES });
462
+ if (listing.timedOut) throw snapshotTimeout("git ls-files -s", limit, files);
463
+ if (listing.status !== 0 || listing.error || listing.outputCut) throw new Error(`git ls-files -s failed in ${dir}: ${listing.error ?? listing.stderr?.trim() ?? `status ${listing.status}`}`);
464
+ const drop: string[] = [];
465
+ for (const entry of names(listing.stdout)) {
466
+ // `<mode> <sha> <stage>\t<path>`
467
+ const tab = entry.indexOf("\t");
468
+ const path = entry.slice(tab + 1);
469
+ if (entry.startsWith("160000 ")) {
470
+ drop.push(path);
471
+ continue;
472
+ }
473
+ try {
474
+ const stat = lstatSync(join(dir, path));
475
+ if (!stat.isFile() && !stat.isSymbolicLink()) drop.push(path);
476
+ } catch {
477
+ drop.push(path);
478
+ }
479
+ }
480
+ if (drop.length === 0) return;
481
+ const out = spawnSync("git", [...GIT_BASE_ARGS, "update-index", "--force-remove", "-z", "--stdin"], {
482
+ cwd: dir,
483
+ env: gitEnv(),
484
+ input: `${drop.join("\0")}\0`,
485
+ timeout: limit,
486
+ maxBuffer: DIFF_MAX_BYTES,
487
+ });
488
+ if ((out.error as NodeJS.ErrnoException | undefined)?.code === "ETIMEDOUT") throw snapshotTimeout("git update-index", limit, files);
489
+ if (out.status !== 0) throw new Error(`git update-index --force-remove failed in ${dir}: ${out.stderr?.toString() || out.error}`);
490
+ }
491
+
404
492
  export function removeSnapshot(dir: string) {
405
493
  rmSync(dir, { recursive: true, force: true });
406
494
  rmSync(`${dir}.tar`, { force: true });
package/src/cost.ts CHANGED
@@ -7,6 +7,7 @@
7
7
  * be grepped and summed across runs), and a table in the GitHub Actions job summary.
8
8
  */
9
9
  import { appendFileSync } from "node:fs";
10
+ import type { ProviderAuth } from "./providers/types";
10
11
 
11
12
  /**
12
13
  * What a call was for.
@@ -46,6 +47,15 @@ export function addUsage(a: TokenUsage, b: TokenUsage): TokenUsage {
46
47
 
47
48
  export const hasUsage = (u: TokenUsage) => u.input + u.output + u.cacheRead + u.cacheWrite > 0;
48
49
 
50
+ /**
51
+ * True when the dollar figure of a call is an API-price equivalent, not a charge: a run on a
52
+ * Claude subscription, locally or with a CI token (ADR 0019 decision 6, ADR 0037 decision 5).
53
+ */
54
+ export const isEstimate = (auth: ProviderAuth | undefined) => auth === "oauth-token" || auth === "subscription";
55
+
56
+ /** The footer's wording for an estimate; the same idea as the local mode's "API-equivalent estimate … not a bill". */
57
+ export const ESTIMATE_NOTE = "Claude subscription: API-equivalent estimate, not a bill";
58
+
49
59
  /** One model call, as reported by the provider and tagged by the engine. */
50
60
  export interface CallCost {
51
61
  operation: Operation;
@@ -60,6 +70,8 @@ export interface CallCost {
60
70
  /** False when the call died without a result: its real cost is unknown and `costUsd` is its cap. Default true. */
61
71
  costKnown?: boolean;
62
72
  terminalReason: string;
73
+ /** What paid for the call; absent means `api-key`. */
74
+ auth?: ProviderAuth;
63
75
  repo?: string;
64
76
  pr?: number;
65
77
  }
@@ -84,6 +96,8 @@ export function costLine(parts: {
84
96
  /** Anything else for the line, e.g. `2 batches`, `answers 2 thread(s)` or `PR so far $0.77 over 3 automatic runs`. */
85
97
  extra?: string[];
86
98
  usage?: TokenUsage;
99
+ /** The amount is an API-price estimate on a Claude subscription: said before the figure, which stays last. */
100
+ estimate?: boolean;
87
101
  costUsd: number;
88
102
  }) {
89
103
  const { usage } = parts;
@@ -99,6 +113,7 @@ export function costLine(parts: {
99
113
  ...(parts.operation ? [parts.operation] : []),
100
114
  ...(parts.extra ?? []),
101
115
  ...tokens,
116
+ ...(parts.estimate ? [ESTIMATE_NOTE] : []),
102
117
  `$${parts.costUsd.toFixed(2)}`,
103
118
  ];
104
119
  return `<sub>${segments.join(" · ")}</sub>`;
@@ -126,6 +141,8 @@ export function costLogLine(call: CallCost, extra: { trigger?: string } = {}) {
126
141
  cost_usd: call.costKnown === false ? null : Number(call.costUsd.toFixed(6)),
127
142
  ...(call.costKnown === false ? { countedUsd: Number(call.costUsd.toFixed(6)) } : {}),
128
143
  terminal_reason: call.terminalReason,
144
+ // `cost_usd` on `oauth-token` or `subscription` is an API-price estimate, not a charge.
145
+ auth: call.auth ?? "api-key",
129
146
  })}`;
130
147
  }
131
148
 
@@ -149,6 +166,9 @@ export function renderCostSummary(calls: CallCost[]) {
149
166
  "| --- | --- | ---: | ---: | ---: | ---: | ---: | ---: |",
150
167
  ...rows,
151
168
  `| **Total** | | ${turns} | ${total.input} | ${total.output} | ${total.cacheRead} | ${total.cacheWrite} | **$${usd.toFixed(4)}** |`,
169
+ ...(calls.some((c) => isEstimate(c.auth))
170
+ ? ["", "These runs used a Claude subscription: the dollars above are API-price estimates (what the calls would cost at API prices), not a bill."]
171
+ : []),
152
172
  ...(unknown.length
153
173
  ? [
154
174
  "",
@@ -382,7 +382,7 @@ export const CHECKS: DoctorCheck[] = [
382
382
  id: "skills",
383
383
  level: "optional",
384
384
  run: ({ userHome, fs }) => {
385
- const names = ["wachiman-review", "wachiman-audit"] as const;
385
+ const names = ["wachiman-review", "wachiman-loop", "wachiman-audit"] as const;
386
386
  const found = Object.fromEntries(names.map((n) => [n, fs.isDirectory(join(userHome, ".claude", "skills", n))])) as Record<(typeof names)[number], boolean>;
387
387
  const where = join(userHome, ".claude", "skills");
388
388
  if (!names.some((n) => found[n])) return { id: "skills", level: "optional", status: "skip", evidence: `no Wachiman skill in ${where}`, detail: found };