wachiman 1.12.0 → 1.14.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.
- package/README.md +132 -101
- package/bin/wachiman.js +1 -1
- package/package.json +2 -3
- package/skills/wachiman-loop/SKILL.md +91 -0
- package/skills/wachiman-review/SKILL.md +2 -2
- package/src/call-message.ts +26 -3
- package/src/cli.ts +113 -18
- package/src/config.ts +99 -32
- package/src/connectors/local-git.ts +102 -14
- package/src/cost.ts +48 -1
- package/src/doctor/checks.ts +1 -1
- package/src/engine.ts +910 -89
- package/src/estimate.ts +64 -0
- package/src/fidelity/block.ts +263 -0
- package/src/fidelity/claims-call.ts +172 -0
- package/src/fidelity/claims.ts +69 -0
- package/src/fidelity/digest.ts +80 -0
- package/src/fidelity/grade.ts +242 -0
- package/src/fidelity/inert.ts +25 -0
- package/src/fidelity/rules.ts +166 -0
- package/src/fidelity/sink.ts +135 -0
- package/src/fidelity/summary.ts +16 -0
- package/src/fidelity/types.ts +93 -0
- package/src/formats.ts +16 -3
- package/src/history/fingerprint.ts +1 -1
- package/src/history/store.ts +44 -6
- package/src/init/options.ts +1 -1
- package/src/ledger.ts +306 -0
- package/src/local.ts +23 -5
- package/src/loop/command.ts +441 -0
- package/src/loop/state.ts +88 -0
- package/src/loop/stop-rule.ts +98 -0
- package/src/providers/claude-code.ts +48 -13
- package/src/providers/codex.ts +8 -7
- package/src/providers/index.ts +6 -3
- package/src/providers/types.ts +13 -3
- package/src/sinks/common.ts +3 -3
- package/src/sinks/json.ts +13 -2
- package/src/skills/install.ts +4 -4
- package/src/status/command.ts +1 -1
- package/src/store/command.ts +109 -0
- package/src/store/figures.ts +153 -0
- package/src/store/html.ts +138 -0
- package/src/store/index.ts +298 -0
- package/templates/caller-workflow.yml +84 -57
- package/templates/wachiman-review-caller.yml +13 -0
|
@@ -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`,
|
|
9
|
-
*
|
|
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,
|
|
@@ -30,7 +31,7 @@ const DIFF_MAX_BYTES = 512 * 1024 * 1024;
|
|
|
30
31
|
const short = (sha: string) => sha.slice(0, 7);
|
|
31
32
|
|
|
32
33
|
/** The most commits a local review may span, from its start commit to `HEAD`; `--max-depth` changes it for one run. */
|
|
33
|
-
export const DEFAULT_MAX_DEPTH =
|
|
34
|
+
export const DEFAULT_MAX_DEPTH = 250;
|
|
34
35
|
|
|
35
36
|
/** Which commits a local review spans; without one, the branch against its base (`merge-base(base, HEAD) → HEAD`). */
|
|
36
37
|
/** `LocalRefs.baseRef` of a root commit, which has no parent to start from. */
|
|
@@ -367,29 +368,80 @@ export function tarCommand(platform: string = process.platform, env: Record<stri
|
|
|
367
368
|
}
|
|
368
369
|
|
|
369
370
|
/**
|
|
370
|
-
*
|
|
371
|
-
*
|
|
372
|
-
*
|
|
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
|
-
|
|
383
|
-
const untar = spawnSync(tarCommand(), ["-xf", tar], { cwd: dir, encoding: "utf8", timeout:
|
|
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
|
-
|
|
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
|
-
|
|
391
|
-
const
|
|
392
|
-
const
|
|
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,9 +141,35 @@ 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
|
+
/**
|
|
150
|
+
* One call as the spend ledger receives it: the fields of the `wachiman-cost` line that describe
|
|
151
|
+
* the call, nothing about where it ran. `repo` and `trigger` are the ledger's own (it knows the
|
|
152
|
+
* repository from the verified token and gets the trigger when it reserves); `countedUsd` is the
|
|
153
|
+
* spent amount the settle already carries. Same names and values as `costLogLine`.
|
|
154
|
+
*/
|
|
155
|
+
export function ledgerCall(call: CallCost) {
|
|
156
|
+
return {
|
|
157
|
+
operation: call.operation,
|
|
158
|
+
label: call.label,
|
|
159
|
+
model: call.model,
|
|
160
|
+
turns: call.turns,
|
|
161
|
+
duration_ms: call.durationMs,
|
|
162
|
+
input_tokens: call.usage.input,
|
|
163
|
+
output_tokens: call.usage.output,
|
|
164
|
+
cache_read_tokens: call.usage.cacheRead,
|
|
165
|
+
cache_write_tokens: call.usage.cacheWrite,
|
|
166
|
+
cost_usd: call.costKnown === false ? null : Number(call.costUsd.toFixed(6)),
|
|
167
|
+
costKnown: call.costKnown !== false,
|
|
168
|
+
terminal_reason: call.terminalReason,
|
|
169
|
+
auth: call.auth ?? "api-key",
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
132
173
|
/** The job summary: one row per call and a total row. Empty string when no call was made. */
|
|
133
174
|
export function renderCostSummary(calls: CallCost[]) {
|
|
134
175
|
if (calls.length === 0) return "";
|
|
@@ -149,6 +190,9 @@ export function renderCostSummary(calls: CallCost[]) {
|
|
|
149
190
|
"| --- | --- | ---: | ---: | ---: | ---: | ---: | ---: |",
|
|
150
191
|
...rows,
|
|
151
192
|
`| **Total** | | ${turns} | ${total.input} | ${total.output} | ${total.cacheRead} | ${total.cacheWrite} | **$${usd.toFixed(4)}** |`,
|
|
193
|
+
...(calls.some((c) => isEstimate(c.auth))
|
|
194
|
+
? ["", "These runs used a Claude subscription: the dollars above are API-price estimates (what the calls would cost at API prices), not a bill."]
|
|
195
|
+
: []),
|
|
152
196
|
...(unknown.length
|
|
153
197
|
? [
|
|
154
198
|
"",
|
|
@@ -167,9 +211,12 @@ export function writeStepSummary(
|
|
|
167
211
|
calls: CallCost[],
|
|
168
212
|
env: Record<string, string | undefined>,
|
|
169
213
|
append: (path: string, text: string) => void = appendFileSync,
|
|
214
|
+
/** What the spend ledger did or could not do in this run, one line each (`### Wachiman spend limit`). */
|
|
215
|
+
ledgerNotes: string[] = [],
|
|
170
216
|
) {
|
|
171
217
|
const path = env.GITHUB_STEP_SUMMARY;
|
|
172
|
-
const
|
|
218
|
+
const notes = ledgerNotes.length === 0 ? "" : ["### Wachiman spend limit", "", ...ledgerNotes.map((n) => `- ${n}`)].join("\n");
|
|
219
|
+
const text = [renderCostSummary(calls), notes].filter(Boolean).join("\n\n");
|
|
173
220
|
if (!path || !text) return false;
|
|
174
221
|
try {
|
|
175
222
|
append(path, `${text}\n`);
|
package/src/doctor/checks.ts
CHANGED
|
@@ -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 };
|