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.
- package/README.md +132 -101
- package/package.json +1 -1
- package/skills/wachiman-loop/SKILL.md +91 -0
- package/src/call-message.ts +26 -3
- package/src/cli.ts +81 -5
- package/src/config.ts +19 -0
- package/src/connectors/local-git.ts +101 -13
- package/src/cost.ts +20 -0
- package/src/doctor/checks.ts +1 -1
- package/src/engine.ts +532 -55
- 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 +39 -1
- package/src/init/options.ts +1 -1
- package/src/local.ts +20 -2
- 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 +47 -12
- package/src/providers/codex.ts +3 -2
- package/src/providers/index.ts +5 -2
- package/src/providers/types.ts +10 -0
- package/src/sinks/json.ts +13 -2
- package/src/skills/install.ts +4 -4
- 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 +45 -15
- package/templates/wachiman-review-caller.yml +8 -0
package/src/formats.ts
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
import { UsageError } from "./config";
|
|
9
9
|
import type { Contract } from "./contracts";
|
|
10
10
|
import type { TokenUsage } from "./cost";
|
|
11
|
+
import type { Fidelity } from "./fidelity/types";
|
|
11
12
|
import type {
|
|
12
13
|
BaseInstructions,
|
|
13
14
|
BatchReport,
|
|
@@ -65,9 +66,11 @@ export interface ReviewInput {
|
|
|
65
66
|
scope: string[];
|
|
66
67
|
/**
|
|
67
68
|
* Incremental context: the last reviewed commit, whether the change was rebased since, and
|
|
68
|
-
* (optional) the commits since it, oldest first, for the report's headline.
|
|
69
|
+
* (optional) the commits since it, oldest first, for the report's headline. `carried` names
|
|
70
|
+
* the files of `files` that the previous review did not reach: they carry their whole diff, not
|
|
71
|
+
* only what changed since, and are reviewed whole.
|
|
69
72
|
*/
|
|
70
|
-
since?: { sha: string; rebased: boolean; commits?: string[] };
|
|
73
|
+
since?: { sha: string; rebased: boolean; commits?: string[]; carried?: string[] };
|
|
71
74
|
/** Open threads for the model to answer. */
|
|
72
75
|
conversation?: Thread[];
|
|
73
76
|
}
|
|
@@ -181,6 +184,11 @@ export interface VerifiedFields {
|
|
|
181
184
|
export interface ResultFindingV2 extends Finding, VerifiedFields {
|
|
182
185
|
/** Whether the line is one a diff-anchored comment can target (`commentableLines`). */
|
|
183
186
|
anchor: "diff" | "outside";
|
|
187
|
+
/**
|
|
188
|
+
* The finding's history id, the one `wachiman history --verdict` takes (ADR 0034). Only a
|
|
189
|
+
* `local` JSON `/2` written with history on carries it; the core never sets it.
|
|
190
|
+
*/
|
|
191
|
+
id?: string;
|
|
184
192
|
}
|
|
185
193
|
|
|
186
194
|
export interface VerificationEntryV2 {
|
|
@@ -247,6 +255,11 @@ export interface ReviewResultV2 {
|
|
|
247
255
|
summary: string;
|
|
248
256
|
/** The risk map (ADR 0031). Only `/2` has it: `toResultV1` leaves it out. */
|
|
249
257
|
riskMap?: RiskMap;
|
|
258
|
+
/**
|
|
259
|
+
* PR Confidence (ADR 0033): the claims, the unexplained files and the grade. Only the `/2` result
|
|
260
|
+
* of a GitHub pull request review has it; a local review never does, and `toResultV1` leaves it out.
|
|
261
|
+
*/
|
|
262
|
+
fidelity?: Fidelity;
|
|
250
263
|
batches: BatchReport[];
|
|
251
264
|
threadReplies: ReviewOutput["thread_replies"];
|
|
252
265
|
instructionChanges: { files: string[]; baseSha: string };
|
|
@@ -309,7 +322,7 @@ export function toResultV1(result: ReviewResultV2): ReviewResultV1 {
|
|
|
309
322
|
throw new UsageError("A working-tree review has no result /1: its head is a tree id, not a commit. Use --result-version 2.");
|
|
310
323
|
}
|
|
311
324
|
const { account: _account, ...provider } = result.provider;
|
|
312
|
-
const { verification, riskMap: _riskMap, ...rest } = result;
|
|
325
|
+
const { verification, riskMap: _riskMap, fidelity: _fidelity, ...rest } = result;
|
|
313
326
|
return {
|
|
314
327
|
...rest,
|
|
315
328
|
format: REVIEW_RESULT_V1,
|
|
@@ -11,7 +11,7 @@ const sha256 = (text: string) => createHash("sha256").update(text).digest("hex")
|
|
|
11
11
|
export const stripWhitespace = (s: string) => s.replace(/\s+/g, "");
|
|
12
12
|
|
|
13
13
|
/** A cited line shorter than this, once stripped, is too common to identify anything. */
|
|
14
|
-
const MIN_LINE_CHARS = 8;
|
|
14
|
+
export const MIN_LINE_CHARS = 8;
|
|
15
15
|
|
|
16
16
|
/** The title lowercased, backticks and punctuation removed, every digit run a `#`, whitespace collapsed. */
|
|
17
17
|
export function titleKey(title: string): string {
|
package/src/history/store.ts
CHANGED
|
@@ -208,7 +208,7 @@ function parseRun(path: string): HistoryRun {
|
|
|
208
208
|
return parsed as HistoryRun;
|
|
209
209
|
}
|
|
210
210
|
|
|
211
|
-
const runFiles = (root: string, key: string) => {
|
|
211
|
+
export const runFiles = (root: string, key: string) => {
|
|
212
212
|
const dir = runsDir(root, key);
|
|
213
213
|
if (!existsSync(dir)) return [];
|
|
214
214
|
return readdirSync(dir)
|
|
@@ -316,6 +316,44 @@ export function readVerdicts(root: string, key: string): Map<string, VerdictLine
|
|
|
316
316
|
return out;
|
|
317
317
|
}
|
|
318
318
|
|
|
319
|
+
/**
|
|
320
|
+
* One file per branch under `<root>/<repo-key>/loops/` for `wachiman loop` (ADR 0034): the loop's
|
|
321
|
+
* state lives beside the runs it reads, so `history --forget` removes both. The branch name is only
|
|
322
|
+
* a label in the file name; the hash keeps two names that sanitise alike apart.
|
|
323
|
+
*/
|
|
324
|
+
const loopPath = (root: string, key: string, branch: string) =>
|
|
325
|
+
join(keyDir(root, key), "loops", `${safeName(branch)}-${sha256(branch).slice(0, 8)}.json`);
|
|
326
|
+
|
|
327
|
+
/** Every loop state file of a repository, in name order: what the local index replays. */
|
|
328
|
+
export function loopFiles(root: string, key: string): string[] {
|
|
329
|
+
const dir = join(keyDir(root, key), "loops");
|
|
330
|
+
if (!existsSync(dir)) return [];
|
|
331
|
+
return readdirSync(dir)
|
|
332
|
+
.filter((n) => n.endsWith(".json"))
|
|
333
|
+
.sort()
|
|
334
|
+
.map((n) => join(dir, n));
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** The stored loop state of a branch as parsed JSON, or undefined when there is none. A file that is not JSON is an error naming it. */
|
|
338
|
+
export function readLoopFile(root: string, key: string, branch: string): unknown | undefined {
|
|
339
|
+
const path = loopPath(root, key, branch);
|
|
340
|
+
if (!existsSync(path)) return undefined;
|
|
341
|
+
try {
|
|
342
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
343
|
+
} catch (error) {
|
|
344
|
+
throw new UsageError(`${path} is not a loop state file (${error instanceof Error ? error.message : error}); remove it to start the loop again.`);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
export function writeLoopFile(root: string, key: string, branch: string, state: unknown): string {
|
|
349
|
+
ensureKeyDirs(root, key);
|
|
350
|
+
privateDirs(join(keyDir(root, key), "loops"));
|
|
351
|
+
chmodSync(join(keyDir(root, key), "loops"), 0o700);
|
|
352
|
+
const path = loopPath(root, key, branch);
|
|
353
|
+
writeAtomic(path, `${JSON.stringify(state, null, 2)}\n`);
|
|
354
|
+
return path;
|
|
355
|
+
}
|
|
356
|
+
|
|
319
357
|
/** Removes one repository's history, or all of it; returns what was removed. Never leaves `root`. */
|
|
320
358
|
export function forget(root: string, key?: string): string[] {
|
|
321
359
|
const target = key === undefined ? root : keyDir(root, key);
|
package/src/init/options.ts
CHANGED
|
@@ -34,7 +34,7 @@ export type ResolveResult<T> = { ok: true; options: T } | { ok: false; error: st
|
|
|
34
34
|
* docs/releasing.md); the CLI's own version is never a substitute,
|
|
35
35
|
* because an unreleased or unrelated version would pin a workflow that does not exist.
|
|
36
36
|
*/
|
|
37
|
-
export const DEFAULT_WORKFLOW_REF: string | undefined = "v1.
|
|
37
|
+
export const DEFAULT_WORKFLOW_REF: string | undefined = "v1.13.0"; // x-release-please-version
|
|
38
38
|
|
|
39
39
|
export const DEFAULT_WORKFLOW_PATH = ".github/workflows/wachiman-review.yml";
|
|
40
40
|
const WORKFLOWS_DIR = ".github/workflows/";
|
package/src/local.ts
CHANGED
|
@@ -23,7 +23,8 @@ import {
|
|
|
23
23
|
snapshotHead,
|
|
24
24
|
warnSnapshotDifferences,
|
|
25
25
|
} from "./connectors/local-git";
|
|
26
|
-
import { DEFAULT_EXCLUDE, filterFiles, planBatches, reviewChange, reviewOptionsOf, type ReviewInput, type ReviewResult } from "./engine";
|
|
26
|
+
import { DEFAULT_EXCLUDE, estimateFor, filterFiles, planBatches, reviewChange, reviewOptionsOf, type ReviewInput, type ReviewResult } from "./engine";
|
|
27
|
+
import { sizeGate, type ReviewEstimate } from "./estimate";
|
|
27
28
|
import { spawnGit } from "./evidence";
|
|
28
29
|
import { assertReviewInputFormat, REVIEW_INPUT_V2 } from "./formats";
|
|
29
30
|
import { workingTreeInput, snapshotWorkingTree } from "./connectors/working-tree";
|
|
@@ -243,6 +244,7 @@ export function exitCodeOf(result: ReviewResult) {
|
|
|
243
244
|
export function interruptible(provider: ProviderAdapter, state: { interrupted: boolean }): ProviderAdapter {
|
|
244
245
|
return {
|
|
245
246
|
name: provider.name,
|
|
247
|
+
auth: provider.auth,
|
|
246
248
|
preflight: () => provider.preflight(),
|
|
247
249
|
modelLabel: () => provider.modelLabel(),
|
|
248
250
|
leaksSecret: (texts) => provider.leaksSecret(texts),
|
|
@@ -433,6 +435,15 @@ export function localScopeInput(p: {
|
|
|
433
435
|
return { input: built.input, release: () => removeSnapshot(built.snapshotDir) };
|
|
434
436
|
}
|
|
435
437
|
|
|
438
|
+
/**
|
|
439
|
+
* The one warning a local run prints when its estimate is above the size gate, or null. Local runs
|
|
440
|
+
* are never refused: the run continues and stops at its budget.
|
|
441
|
+
*/
|
|
442
|
+
export function sizeWarning(estimate: ReviewEstimate, gate: number, budgetUsd: number, model: string): string | null {
|
|
443
|
+
if (estimate.usd <= gate) return null;
|
|
444
|
+
return `warning: reviewing all of this would cost about $${estimate.usd.toFixed(2)} at API prices (${estimate.batches} batches × $${estimate.perBatchUsd.toFixed(2)} for ${model}), above the $${gate.toFixed(2)} size gate. Local runs are never refused; this one continues and stops at its budget, $${budgetUsd.toFixed(2)} (AGENT_REVIEW_BUDGET_USD).`;
|
|
445
|
+
}
|
|
446
|
+
|
|
436
447
|
export function runLocal(
|
|
437
448
|
args: LocalArgs,
|
|
438
449
|
repoDir: string,
|
|
@@ -510,6 +521,8 @@ export function runLocal(
|
|
|
510
521
|
});
|
|
511
522
|
io.stderr(`files: ${kept.length} (${dropped.length} excluded)`);
|
|
512
523
|
io.stderr(`batches: ${batches.length}`);
|
|
524
|
+
const warning = sizeWarning(estimateFor(kept, config), sizeGate(config, false), config.budgetUsd, config.model);
|
|
525
|
+
if (warning) io.stderr(warning);
|
|
513
526
|
io.stderr(
|
|
514
527
|
config.verify
|
|
515
528
|
? `verify: on (${config.verifierModel}, reserve $${Math.min(config.verifyBudgetUsd, config.budgetUsd / 2).toFixed(2)})`
|
|
@@ -564,7 +577,12 @@ export function runLocal(
|
|
|
564
577
|
}
|
|
565
578
|
const text =
|
|
566
579
|
args.format === "json"
|
|
567
|
-
? renderJson(
|
|
580
|
+
? renderJson(
|
|
581
|
+
result,
|
|
582
|
+
args.resultVersion ?? (input.change.kind === "working-tree" ? 2 : 1),
|
|
583
|
+
// ADR 0034: with history on, a `/2` finding names itself by the id `history --verdict` takes.
|
|
584
|
+
annotation ? result.findings.map((_, i) => annotation!.byIndex.get(i)?.id) : undefined,
|
|
585
|
+
)
|
|
568
586
|
: args.format === "markdown"
|
|
569
587
|
? renderMarkdown(result, { profileNote, history: annotation })
|
|
570
588
|
: renderTerminal(result, {
|
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `wachiman loop`: one review round per call (ADR 0034). A round is a local review with history on,
|
|
3
|
+
* a comparison with the rounds before it, and an outcome the caller can branch on. The agent fixes
|
|
4
|
+
* between calls; this command never edits code.
|
|
5
|
+
*
|
|
6
|
+
* Exit codes: 0 converged, 10 findings still open, 11 stopped without converging (`round cap`,
|
|
7
|
+
* `review failed`), 2 usage error, 130 interrupted.
|
|
8
|
+
*
|
|
9
|
+
* The review is `runLocal` itself, asked for result `/2` with history on, so the account rules, the
|
|
10
|
+
* prompt and the history are exactly those of `wachiman local`. Round 1 reviews the whole range from
|
|
11
|
+
* `--base`; a later round reviews the commits since the previous round's head, as an ordinary range.
|
|
12
|
+
* A round with no new commit makes no model call: it re-reads the verdicts and reports again.
|
|
13
|
+
*/
|
|
14
|
+
import { UsageError } from "../config";
|
|
15
|
+
import { neutralise, SEVERITY, TOPIC, type ReviewResult } from "../engine";
|
|
16
|
+
import { spawnGit } from "../evidence";
|
|
17
|
+
import { readReviewResultFormat } from "../formats";
|
|
18
|
+
import { wachimanHome } from "../accounts/config";
|
|
19
|
+
import { MIN_LINE_CHARS, stripWhitespace, titleKey } from "../history/fingerprint";
|
|
20
|
+
import { replay, type ReplayedFinding } from "../history/match";
|
|
21
|
+
import { historyRoot, readRuns, readVerdicts, repositoryKey } from "../history/store";
|
|
22
|
+
import { LOCAL_FLAGS, parseFlags, runLocal, type LocalArgs, type LocalDeps } from "../local";
|
|
23
|
+
import { location, sha7 } from "../sinks/common";
|
|
24
|
+
import { LOOP_STATE_FORMAT, readLoop, writeLoop, type LedgerEntry, type LoopState } from "./state";
|
|
25
|
+
import { evaluate, isRefutedBy, roundCapReached, type Decision, type LoopFinding, type LoopStatus, type OpenFinding, type Released } from "./stop-rule";
|
|
26
|
+
|
|
27
|
+
export const LOOP_RESULT_FORMAT = "wachiman.loop-round/1";
|
|
28
|
+
export const DEFAULT_MAX_ROUNDS = 3;
|
|
29
|
+
|
|
30
|
+
export const loopUsage = `wachiman loop [--base <ref>] [--max-rounds <n>] [--max-depth <n>] [--repo-dir <dir>]
|
|
31
|
+
[--format terminal|json] [--profile <name>] [--verify | --no-verify]`;
|
|
32
|
+
|
|
33
|
+
export interface LoopArgs {
|
|
34
|
+
/** Starts a loop (round 1 reviews the whole range from here). Absent: continue the open loop. */
|
|
35
|
+
base?: string;
|
|
36
|
+
maxRounds: number;
|
|
37
|
+
format: "terminal" | "json";
|
|
38
|
+
profile?: string;
|
|
39
|
+
verify: boolean | undefined;
|
|
40
|
+
maxDepth?: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The flags `loop` takes, a subset of `local`'s plus `--max-rounds`. */
|
|
44
|
+
const LOOP_LOCAL_FLAGS = LOCAL_FLAGS.filter((f) => ["--base", "--format", "--profile", "--verify", "--no-verify", "--max-depth"].includes(f));
|
|
45
|
+
|
|
46
|
+
/** The arguments after `loop` (`--repo-dir` is already taken out). Both `--x v` and `--x=v`. */
|
|
47
|
+
export function parseLoopArgs(argv: string[]): LoopArgs {
|
|
48
|
+
let maxRounds = DEFAULT_MAX_ROUNDS;
|
|
49
|
+
let seenMax = false;
|
|
50
|
+
const rest: string[] = [];
|
|
51
|
+
for (let i = 0; i < argv.length; i++) {
|
|
52
|
+
const arg = argv[i];
|
|
53
|
+
if (arg === "--max-rounds" || arg.startsWith("--max-rounds=")) {
|
|
54
|
+
if (seenMax) throw new UsageError("--max-rounds was given twice.");
|
|
55
|
+
seenMax = true;
|
|
56
|
+
const v = arg === "--max-rounds" ? argv[++i] : arg.slice("--max-rounds=".length);
|
|
57
|
+
if (v === undefined || v === "" || v.startsWith("--")) throw new UsageError("--max-rounds needs a value.");
|
|
58
|
+
if (!/^[1-9]\d*$/.test(v) || !Number.isSafeInteger(Number(v))) throw new UsageError(`--max-rounds must be a positive integer, got "${v}".`);
|
|
59
|
+
maxRounds = Number(v);
|
|
60
|
+
} else rest.push(arg);
|
|
61
|
+
}
|
|
62
|
+
const local = parseFlags(rest, "loop", LOOP_LOCAL_FLAGS);
|
|
63
|
+
if (local.paths.length > 0) throw new UsageError(`loop takes no path or argument ("${local.paths[0]}"): it reviews the committed branch.`);
|
|
64
|
+
if (local.format === "markdown") throw new UsageError("--format must be terminal or json for loop.");
|
|
65
|
+
return {
|
|
66
|
+
...(local.base !== undefined ? { base: local.base } : {}),
|
|
67
|
+
maxRounds,
|
|
68
|
+
format: local.format as "terminal" | "json",
|
|
69
|
+
...(local.profile !== undefined ? { profile: local.profile } : {}),
|
|
70
|
+
verify: local.verify,
|
|
71
|
+
...(local.maxDepth !== undefined ? { maxDepth: local.maxDepth } : {}),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface LoopDeps extends LocalDeps {
|
|
76
|
+
/** The review of one round; `runLocal` unless a test says otherwise. */
|
|
77
|
+
review?: typeof runLocal;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
type Why = "round cap" | "review failed";
|
|
81
|
+
|
|
82
|
+
/** What one call reports, in terminal and JSON form. */
|
|
83
|
+
export interface RoundReport {
|
|
84
|
+
branch: string;
|
|
85
|
+
round: number;
|
|
86
|
+
maxRounds: number;
|
|
87
|
+
/** What the round reviewed. */
|
|
88
|
+
range: { kind: "whole"; base: string } | { kind: "since-previous-round"; from: string } | { kind: "none"; head: string };
|
|
89
|
+
head: string;
|
|
90
|
+
open: OpenFinding[];
|
|
91
|
+
released: Released[];
|
|
92
|
+
notes: LoopFinding[];
|
|
93
|
+
outcome: { status: "converged" | "open" | "stopped"; reason?: Why; message: string };
|
|
94
|
+
exitCode: 0 | 10 | 11;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const GIT_TIMEOUT_MS = 30_000;
|
|
98
|
+
const git = (repoDir: string, args: string[]) => spawnGit(args, { cwd: repoDir, timeoutMs: GIT_TIMEOUT_MS });
|
|
99
|
+
|
|
100
|
+
function currentBranch(repoDir: string): string {
|
|
101
|
+
const out = git(repoDir, ["symbolic-ref", "--short", "-q", "HEAD"]);
|
|
102
|
+
if (out.status !== 0 || out.stdout.trim() === "") throw new UsageError("HEAD is detached; the loop is kept per branch, so check out a branch.");
|
|
103
|
+
return out.stdout.trim();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function headOf(repoDir: string): string {
|
|
107
|
+
const out = git(repoDir, ["rev-parse", "--verify", "HEAD^{commit}"]);
|
|
108
|
+
if (out.status !== 0) throw new UsageError("HEAD has no commit yet; commit the change first.");
|
|
109
|
+
return out.stdout.trim();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** `old path -> new path` for the files the range renamed. */
|
|
113
|
+
function renamesIn(repoDir: string, from: string, head: string): Map<string, string> {
|
|
114
|
+
const out = git(repoDir, ["diff", "--name-status", "-M", from, head]);
|
|
115
|
+
const map = new Map<string, string>();
|
|
116
|
+
if (out.status !== 0) return map;
|
|
117
|
+
for (const line of out.stdout.split("\n")) {
|
|
118
|
+
const [kind, oldPath, newPath] = line.split("\t");
|
|
119
|
+
if (kind?.startsWith("R") && oldPath && newPath) map.set(oldPath, newPath);
|
|
120
|
+
}
|
|
121
|
+
return map;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Whether the finding's cited region changed in the file at HEAD, by the rule the history uses when
|
|
126
|
+
* it settles a finding: the cited line is gone from the file; or, with no line to search for, the
|
|
127
|
+
* file's blob differs.
|
|
128
|
+
*/
|
|
129
|
+
function regionChanged(repoDir: string, head: string, path: string, t: Pick<ReplayedFinding, "lineText" | "blobSha">): boolean {
|
|
130
|
+
if (t.lineText !== undefined) {
|
|
131
|
+
const shown = git(repoDir, ["show", `${head}:${path}`]);
|
|
132
|
+
return shown.status !== 0 || !shown.stdout.split("\n").some((l) => stripWhitespace(l) === t.lineText);
|
|
133
|
+
}
|
|
134
|
+
const blob = git(repoDir, ["rev-parse", `${head}:${path}`]);
|
|
135
|
+
return blob.status !== 0 || t.blobSha === undefined || blob.stdout.trim() !== t.blobSha;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* What `regionChanged` compares against later: the cited line without whitespace, or the file's blob
|
|
140
|
+
* when the line is too short, repeated or missing, as the history fingerprints a finding.
|
|
141
|
+
*/
|
|
142
|
+
function fingerprintAt(repoDir: string, head: string, path: string, line: number): { lineText?: string; blobSha?: string } {
|
|
143
|
+
const shown = git(repoDir, ["show", `${head}:${path}`]);
|
|
144
|
+
if (shown.status === 0 && Number.isInteger(line) && line >= 1) {
|
|
145
|
+
const lines = shown.stdout.split("\n");
|
|
146
|
+
const text = line <= lines.length ? stripWhitespace(lines[line - 1]) : "";
|
|
147
|
+
if (text.length >= MIN_LINE_CHARS && lines.filter((l) => stripWhitespace(l) === text).length === 1) return { lineText: text };
|
|
148
|
+
}
|
|
149
|
+
const blob = git(repoDir, ["rev-parse", `${head}:${path}`]);
|
|
150
|
+
return blob.status === 0 ? { blobSha: blob.stdout.trim() } : {};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** A complete `/2` result whose findings all carry their history id. */
|
|
154
|
+
type IdentifiedResult = ReviewResult & { findings: { id: string }[] };
|
|
155
|
+
type Attempt = { ok: true; result: IdentifiedResult } | { ok: false; why: string } | { interrupted: true };
|
|
156
|
+
|
|
157
|
+
/** One review call: succeeds only with a complete `/2` result in which every finding has a history id. */
|
|
158
|
+
function attempt(args: LocalArgs, repoDir: string, env: Record<string, string | undefined>, io: LoopIo, deps: LoopDeps): Attempt {
|
|
159
|
+
let out = "";
|
|
160
|
+
let code: number;
|
|
161
|
+
try {
|
|
162
|
+
code = (deps.review ?? runLocal)(args, repoDir, env, { stdout: (t) => (out += t), stderr: io.stderr, engineVersion: io.engineVersion }, deps);
|
|
163
|
+
} catch (error) {
|
|
164
|
+
if (error instanceof UsageError) throw error;
|
|
165
|
+
return { ok: false, why: error instanceof Error ? error.message : String(error) };
|
|
166
|
+
}
|
|
167
|
+
if (code === 130) return { interrupted: true };
|
|
168
|
+
if (code !== 0) return { ok: false, why: `the review exited ${code}` };
|
|
169
|
+
let parsed: ReviewResult;
|
|
170
|
+
try {
|
|
171
|
+
parsed = JSON.parse(out);
|
|
172
|
+
readReviewResultFormat(parsed);
|
|
173
|
+
} catch {
|
|
174
|
+
return { ok: false, why: "the review did not return a result" };
|
|
175
|
+
}
|
|
176
|
+
if (parsed.execution.stop !== "complete") return { ok: false, why: `the review stopped early (${parsed.execution.stop})` };
|
|
177
|
+
if (parsed.coverage.partial) return { ok: false, why: "the review left files unread" };
|
|
178
|
+
if (parsed.findings.some((f: { id?: string }) => typeof f.id !== "string")) return { ok: false, why: "the review's history entry was not saved, so its findings have no id" };
|
|
179
|
+
return { ok: true, result: parsed as IdentifiedResult };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
interface LoopIo {
|
|
183
|
+
stdout: (text: string) => void;
|
|
184
|
+
stderr: (line: string) => void;
|
|
185
|
+
engineVersion: string;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const withVerdicts = (ledger: LedgerEntry[], verdicts: ReturnType<typeof readVerdicts>): LoopFinding[] =>
|
|
189
|
+
ledger.map((e) => ({ ...e, ...(verdicts.get(e.id) ? { verdict: { verdict: verdicts.get(e.id)!.verdict, reason: verdicts.get(e.id)!.reason } } : {}) }));
|
|
190
|
+
|
|
191
|
+
export function runLoop(args: LoopArgs, repoDir: string, env: Record<string, string | undefined>, io: LoopIo, deps: LoopDeps = {}): number {
|
|
192
|
+
const root = historyRoot(wachimanHome(env));
|
|
193
|
+
const key = repositoryKey(repoDir).key;
|
|
194
|
+
const branch = currentBranch(repoDir);
|
|
195
|
+
const head = headOf(repoDir);
|
|
196
|
+
// With --base the old state is about to be replaced, so one this version cannot read is no obstacle.
|
|
197
|
+
let existing: LoopState | undefined;
|
|
198
|
+
try {
|
|
199
|
+
existing = readLoop(root, key, branch);
|
|
200
|
+
} catch (error) {
|
|
201
|
+
if (args.base === undefined || !(error instanceof UsageError)) throw error;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
let state: LoopState;
|
|
205
|
+
if (args.base !== undefined) {
|
|
206
|
+
if (existing?.status === "open" && existing.rounds.length > 0) io.stderr(`loop: starting over on ${branch}; the open loop had run ${existing.rounds.length} round(s).`);
|
|
207
|
+
state = { format: LOOP_STATE_FORMAT, branch, base: args.base, status: "open", rounds: [], ledger: [] };
|
|
208
|
+
} else if (!existing) {
|
|
209
|
+
throw new UsageError(`No loop is open on ${branch}; start one with \`wachiman loop --base <ref>\`.`);
|
|
210
|
+
} else if (existing.status === "converged") {
|
|
211
|
+
throw new UsageError(`The loop on ${branch} converged at round ${existing.rounds.length}; start a new one with \`wachiman loop --base <ref>\`.`);
|
|
212
|
+
} else state = existing;
|
|
213
|
+
|
|
214
|
+
const verdicts = () => readVerdicts(root, key);
|
|
215
|
+
const roundsRun = state.rounds.length;
|
|
216
|
+
const render = (report: RoundReport) => {
|
|
217
|
+
io.stdout(args.format === "json" ? renderJson(report) : renderTerminal(report));
|
|
218
|
+
return report.exitCode;
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
// Stop before another round when the cap is reached: no model call.
|
|
222
|
+
if (args.base === undefined && roundCapReached(roundsRun, args.maxRounds)) {
|
|
223
|
+
const decision = evaluate(withVerdicts(state.ledger, verdicts()));
|
|
224
|
+
return render(
|
|
225
|
+
report(state, roundsRun, args, head, { kind: "none", head }, decision, 11, {
|
|
226
|
+
status: "stopped",
|
|
227
|
+
reason: "round cap",
|
|
228
|
+
message: `Stopped without converging: round cap. ${roundsRun} round(s) ran (--max-rounds ${args.maxRounds}) and ${plural(decision.open.length, "finding")} still ${decision.open.length === 1 ? "keeps" : "keep"} the loop open. Raise --max-rounds to continue, or start a new loop with --base. A stop is never an approval.`,
|
|
229
|
+
}),
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const previous = state.rounds.at(-1);
|
|
234
|
+
const round = roundsRun + 1;
|
|
235
|
+
const now = () => new Date().toISOString();
|
|
236
|
+
|
|
237
|
+
// No new commit since the last round: nothing to review. The verdicts may have changed.
|
|
238
|
+
if (previous && previous.head === head) {
|
|
239
|
+
const decision = evaluate(withVerdicts(state.ledger, verdicts()));
|
|
240
|
+
state = { ...state, rounds: [...state.rounds, { round, head, at: now() }], status: decision.converged ? "converged" : "open" };
|
|
241
|
+
writeLoop(root, key, state);
|
|
242
|
+
io.stderr(`loop: no commit since round ${previous.round} (${sha7(head)}); nothing was reviewed, the verdicts were read again.`);
|
|
243
|
+
return render(report(state, round, args, head, { kind: "none", head }, decision, decision.converged ? 0 : 10, finish(decision, round, args)));
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const dirty = git(repoDir, ["status", "--porcelain", "--untracked-files=no"]);
|
|
247
|
+
if (dirty.status === 0 && dirty.stdout.trim() !== "") io.stderr("loop: uncommitted changes are not reviewed; commit the fix first.");
|
|
248
|
+
|
|
249
|
+
const from = previous ? previous.head : args.base ?? state.base;
|
|
250
|
+
const localArgs: LocalArgs = {
|
|
251
|
+
paths: [],
|
|
252
|
+
format: "json",
|
|
253
|
+
resultVersion: 2,
|
|
254
|
+
agentPrompt: false,
|
|
255
|
+
history: true,
|
|
256
|
+
verify: args.verify,
|
|
257
|
+
base: from,
|
|
258
|
+
...(args.profile !== undefined ? { profile: args.profile } : {}),
|
|
259
|
+
...(args.maxDepth !== undefined ? { maxDepth: args.maxDepth } : {}),
|
|
260
|
+
};
|
|
261
|
+
let made = attempt(localArgs, repoDir, env, io, deps);
|
|
262
|
+
if ("ok" in made && !made.ok) {
|
|
263
|
+
io.stderr(`loop: the review failed (${made.why}); trying once more.`);
|
|
264
|
+
made = attempt(localArgs, repoDir, env, io, deps);
|
|
265
|
+
}
|
|
266
|
+
if ("interrupted" in made) return 130;
|
|
267
|
+
if (!made.ok) {
|
|
268
|
+
// The round is not recorded; a fresh loop is, so the next call can retry it without --base.
|
|
269
|
+
if (args.base !== undefined) writeLoop(root, key, state);
|
|
270
|
+
const decision = evaluate(withVerdicts(state.ledger, verdicts()));
|
|
271
|
+
return render(
|
|
272
|
+
report(state, round, args, head, { kind: "none", head }, decision, 11, {
|
|
273
|
+
status: "stopped",
|
|
274
|
+
reason: "review failed",
|
|
275
|
+
message: `Stopped without converging: review failed. The review call failed twice (${made.why}). Round ${round} was not recorded; run \`wachiman loop\` again to retry it. A stop is never an approval.`,
|
|
276
|
+
}),
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const { result } = made;
|
|
281
|
+
const decision = settle(state, result, repoDir, key, root, previous?.head ?? from, verdicts());
|
|
282
|
+
state = {
|
|
283
|
+
...state,
|
|
284
|
+
rounds: [...state.rounds, { round, head: result.change.head.sha, at: now() }],
|
|
285
|
+
ledger: decision.ledger,
|
|
286
|
+
status: decision.decision.converged ? "converged" : "open",
|
|
287
|
+
};
|
|
288
|
+
writeLoop(root, key, state);
|
|
289
|
+
const range: RoundReport["range"] = previous ? { kind: "since-previous-round", from: previous.head } : { kind: "whole", base: from };
|
|
290
|
+
return render(report(state, round, args, result.change.head.sha, range, decision.decision, decision.decision.converged ? 0 : 10, finish(decision.decision, round, args)));
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* The new ledger and the decision after a round. The loop decides from the round's own result and the
|
|
295
|
+
* code, never from the history's settled map (a review outside the loop writes to the same history).
|
|
296
|
+
* In a round that reads a finding's file: reported again means open; not reported with its cited
|
|
297
|
+
* line gone means probably fixed; not reported with the line still there means not repeated, which
|
|
298
|
+
* still counts. A round that does not read the file keeps the previous state.
|
|
299
|
+
*/
|
|
300
|
+
function settle(
|
|
301
|
+
state: LoopState,
|
|
302
|
+
result: IdentifiedResult,
|
|
303
|
+
repoDir: string,
|
|
304
|
+
key: string,
|
|
305
|
+
root: string,
|
|
306
|
+
rangeFrom: string,
|
|
307
|
+
verdicts: ReturnType<typeof readVerdicts>,
|
|
308
|
+
): { ledger: LedgerEntry[]; decision: Decision } {
|
|
309
|
+
const head = result.change.head.sha;
|
|
310
|
+
const renames = renamesIn(repoDir, rangeFrom, head);
|
|
311
|
+
const expected = new Set(result.coverage.expected);
|
|
312
|
+
const readThisRound = new Set(result.coverage.reviewed.filter((p) => expected.has(p)));
|
|
313
|
+
const dropped = (result.verification?.entries ?? []).filter((e) => e.outcome === "dropped" && e.status === "refuted");
|
|
314
|
+
const droppedKeys = dropped.map((d) => ({ path: d.path, topic: d.topic, titleKey: titleKey(d.title) }));
|
|
315
|
+
type Reported = { id: string; path: string; line: number; severity: LedgerEntry["severity"]; topic: LedgerEntry["topic"]; title: string };
|
|
316
|
+
const findings = result.findings as unknown as Reported[];
|
|
317
|
+
|
|
318
|
+
const prior = state.ledger.map((e) => ({ ...e, path: renames.get(e.path) ?? e.path }));
|
|
319
|
+
// A reported finding is the ledger entry with its id, or failing that the one in the same file with the same topic and title.
|
|
320
|
+
const reportedBy = new Map<string, Reported>();
|
|
321
|
+
const unmatched: Reported[] = [];
|
|
322
|
+
for (const f of findings) {
|
|
323
|
+
const e = prior.find((x) => x.id === f.id && !reportedBy.has(x.id));
|
|
324
|
+
if (e) reportedBy.set(e.id, f);
|
|
325
|
+
else unmatched.push(f);
|
|
326
|
+
}
|
|
327
|
+
const fresh: Reported[] = [];
|
|
328
|
+
for (const f of unmatched) {
|
|
329
|
+
const e = prior.find((x) => !reportedBy.has(x.id) && x.path === f.path && x.topic === f.topic && titleKey(x.title) === titleKey(f.title));
|
|
330
|
+
if (e) reportedBy.set(e.id, f);
|
|
331
|
+
else fresh.push(f);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Fingerprints of entries written before the loop kept them come from the history, by id: data only, never a status.
|
|
335
|
+
let past: Map<string, ReplayedFinding> | undefined;
|
|
336
|
+
const fingerprintOf = (e: LedgerEntry) => {
|
|
337
|
+
if (e.lineText !== undefined || e.blobSha !== undefined) return e;
|
|
338
|
+
past ??= new Map(replay(readRuns(root, key, () => {}), verdicts).findings.map((f) => [f.firstId, f]));
|
|
339
|
+
return past.get(e.id);
|
|
340
|
+
};
|
|
341
|
+
const seen = (f: Reported): LedgerEntry => ({
|
|
342
|
+
id: f.id,
|
|
343
|
+
path: f.path,
|
|
344
|
+
line: f.line,
|
|
345
|
+
severity: f.severity,
|
|
346
|
+
topic: f.topic,
|
|
347
|
+
title: f.title,
|
|
348
|
+
status: "open",
|
|
349
|
+
refuted: false,
|
|
350
|
+
...fingerprintAt(repoDir, head, f.path, f.line),
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
const ledger: LedgerEntry[] = [
|
|
354
|
+
...prior.map((e): LedgerEntry => {
|
|
355
|
+
const again = reportedBy.get(e.id);
|
|
356
|
+
if (again) return { ...seen(again), id: e.id };
|
|
357
|
+
const refuted =
|
|
358
|
+
e.refuted ||
|
|
359
|
+
droppedKeys.some((d) => isRefutedBy(d, { path: e.path, topic: e.topic, titleKey: titleKey(e.title) }));
|
|
360
|
+
const fp = readThisRound.has(e.path) ? fingerprintOf(e) : undefined;
|
|
361
|
+
if (!fp || (fp.lineText === undefined && fp.blobSha === undefined)) return { ...e, refuted };
|
|
362
|
+
return { ...e, refuted, linesChanged: undefined, lineText: fp.lineText, blobSha: fp.blobSha, status: regionChanged(repoDir, head, e.path, fp) ? "probably-fixed" : "not-repeated" };
|
|
363
|
+
}),
|
|
364
|
+
...fresh.map(seen),
|
|
365
|
+
];
|
|
366
|
+
return { ledger, decision: evaluate(withVerdicts(ledger, verdicts)) };
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function finish(d: Decision, round: number, args: LoopArgs): RoundReport["outcome"] {
|
|
370
|
+
if (d.converged) {
|
|
371
|
+
return {
|
|
372
|
+
status: "converged",
|
|
373
|
+
message:
|
|
374
|
+
round === 1
|
|
375
|
+
? "Converged: nothing keeps the loop open. Round 1 reviewed the whole range. This is not an approval."
|
|
376
|
+
: "Converged on incremental rounds: round 1 reviewed the whole range and later rounds only the commits after the previous round. Interactions between a fix and code outside those commits were not re-read. This is not an approval.",
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
const left = args.maxRounds - round;
|
|
380
|
+
return {
|
|
381
|
+
status: "open",
|
|
382
|
+
message: `${plural(d.open.length, "finding")} ${d.open.length === 1 ? "keeps" : "keep"} the loop open. Fix each one and commit, or judge it wrong with a reason (wachiman history --verdict <id> wrong --reason "<why>"), then run \`wachiman loop\` again. ${left > 0 ? `${plural(left, "round")} left of ${args.maxRounds}.` : `That was round ${round} of ${args.maxRounds}; the next call stops at the round cap.`}`,
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const plural = (n: number, word: string) => `${n} ${word}${n === 1 ? "" : "s"}`;
|
|
387
|
+
|
|
388
|
+
function report(
|
|
389
|
+
state: LoopState,
|
|
390
|
+
round: number,
|
|
391
|
+
args: LoopArgs,
|
|
392
|
+
head: string,
|
|
393
|
+
range: RoundReport["range"],
|
|
394
|
+
d: Decision,
|
|
395
|
+
exitCode: 0 | 10 | 11,
|
|
396
|
+
outcome: RoundReport["outcome"],
|
|
397
|
+
): RoundReport {
|
|
398
|
+
return { branch: state.branch, round, maxRounds: args.maxRounds, range, head, open: d.open, released: d.released, notes: d.notes, outcome, exitCode };
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
const cleanFinding = (f: LoopFinding) => ({ id: f.id, path: f.path, line: f.line, severity: f.severity, topic: f.topic, title: f.title, status: f.status });
|
|
402
|
+
|
|
403
|
+
export function renderJson(r: RoundReport): string {
|
|
404
|
+
const doc = {
|
|
405
|
+
format: LOOP_RESULT_FORMAT,
|
|
406
|
+
branch: r.branch,
|
|
407
|
+
round: r.round,
|
|
408
|
+
maxRounds: r.maxRounds,
|
|
409
|
+
range: r.range,
|
|
410
|
+
head: r.head,
|
|
411
|
+
exitCode: r.exitCode,
|
|
412
|
+
outcome: r.outcome,
|
|
413
|
+
open: r.open.map((f) => ({ ...cleanFinding(f), ...(f.unreasonedWrong ? { unreasonedWrong: true } : {}) })),
|
|
414
|
+
released: r.released.map((x) => ({ ...cleanFinding(x.finding), why: x.why, ...(x.reason ? { reason: x.reason } : {}) })),
|
|
415
|
+
notes: r.notes.map(cleanFinding),
|
|
416
|
+
};
|
|
417
|
+
return `${JSON.stringify(doc, null, 2)}\n`;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const row = (f: LoopFinding) => `${SEVERITY[f.severity].emoji} ${SEVERITY[f.severity].word} · ${TOPIC[f.topic].word} ${location(f)} ${neutralise(f.title).replace(/\s+/g, " ")} (id ${f.id})`;
|
|
421
|
+
|
|
422
|
+
export function renderTerminal(r: RoundReport): string {
|
|
423
|
+
const range =
|
|
424
|
+
r.range.kind === "whole"
|
|
425
|
+
? `the whole range from ${neutralise(r.range.base)}`
|
|
426
|
+
: r.range.kind === "since-previous-round"
|
|
427
|
+
? `the commits since round ${r.round - 1} (${sha7(r.range.from)})`
|
|
428
|
+
: "no review this call";
|
|
429
|
+
const lines = [`wachiman loop · ${neutralise(r.branch)} · round ${r.round} of ${r.maxRounds} · ${range} → ${sha7(r.head)}`, ""];
|
|
430
|
+
lines.push(`Keeps the loop open (${r.open.length}):`);
|
|
431
|
+
for (const f of r.open) lines.push(` ${row(f)}`, ` history: ${f.status}${f.unreasonedWrong ? " · judged wrong without a reason: still counts" : ""}`);
|
|
432
|
+
if (r.open.length === 0) lines.push(" none");
|
|
433
|
+
lines.push("", `No longer open (${r.released.length}):`);
|
|
434
|
+
for (const x of r.released) lines.push(` ${x.why} ${row(x.finding)}${x.reason ? `\n reason: ${neutralise(x.reason)}` : ""}`);
|
|
435
|
+
if (r.released.length === 0) lines.push(" none");
|
|
436
|
+
lines.push("", `Notes, never counted (${r.notes.length}):`);
|
|
437
|
+
for (const f of r.notes) lines.push(` ${row(f)}`);
|
|
438
|
+
if (r.notes.length === 0) lines.push(" none");
|
|
439
|
+
lines.push("", `Outcome (exit ${r.exitCode}): ${r.outcome.message}`);
|
|
440
|
+
return `${lines.join("\n")}\n`;
|
|
441
|
+
}
|