wachiman 1.13.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 +1 -1
- package/bin/wachiman.js +1 -1
- package/package.json +2 -3
- package/skills/wachiman-review/SKILL.md +2 -2
- package/src/cli.ts +35 -16
- package/src/config.ts +83 -35
- package/src/connectors/local-git.ts +1 -1
- package/src/cost.ts +28 -1
- package/src/engine.ts +382 -38
- package/src/history/store.ts +5 -5
- package/src/init/options.ts +1 -1
- package/src/ledger.ts +306 -0
- package/src/local.ts +4 -4
- package/src/providers/claude-code.ts +1 -1
- package/src/providers/codex.ts +5 -5
- package/src/providers/index.ts +1 -1
- package/src/providers/types.ts +3 -3
- package/src/sinks/common.ts +3 -3
- package/src/status/command.ts +1 -1
- package/templates/caller-workflow.yml +44 -47
- package/templates/wachiman-review-caller.yml +5 -0
package/src/history/store.ts
CHANGED
|
@@ -44,16 +44,16 @@ export interface HistorySettings {
|
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
/**
|
|
47
|
-
* Whether the history is on (`--history` or `
|
|
47
|
+
* Whether the history is on (`--history` or `WACHIMAN_HISTORY=1`) and how long it is kept.
|
|
48
48
|
* Read here, not in `loadConfig`: that one rejects `0`, and GitHub mode must not see the variable.
|
|
49
|
-
* A bad `
|
|
49
|
+
* A bad `WACHIMAN_HISTORY_DAYS` is checked only when the history is on.
|
|
50
50
|
*/
|
|
51
51
|
export function historySettings(env: Record<string, string | undefined>, flag: boolean): HistorySettings {
|
|
52
|
-
const enabled = flag || env.
|
|
53
|
-
const raw = env.
|
|
52
|
+
const enabled = flag || env.WACHIMAN_HISTORY === "1";
|
|
53
|
+
const raw = env.WACHIMAN_HISTORY_DAYS;
|
|
54
54
|
if (!enabled || raw === undefined || raw === "") return { enabled, days: DEFAULT_HISTORY_DAYS };
|
|
55
55
|
if (!/^\d+$/.test(raw)) {
|
|
56
|
-
throw new UsageError(`
|
|
56
|
+
throw new UsageError(`WACHIMAN_HISTORY_DAYS must be 0 (keep forever) or a whole number of days, got "${raw}".`);
|
|
57
57
|
}
|
|
58
58
|
return { enabled, days: Number(raw) };
|
|
59
59
|
}
|
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.14.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/ledger.ts
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The client of the spend ledger service: one synchronous call to reserve before a run, one to
|
|
3
|
+
* settle after it (or to record a run that could not reserve), and the signed receipt that settling
|
|
4
|
+
* returns. Like `gh`, the network goes through a binary the runner has (`curl`), so the engine stays
|
|
5
|
+
* synchronous and dependency-free (ADR 0002). Imports only `node:` builtins.
|
|
6
|
+
*
|
|
7
|
+
* What may leave the machine is fixed by this file: a reserve carries amounts, the trigger and the
|
|
8
|
+
* pull request number; a settle or record carries the amount spent and the calls the engine
|
|
9
|
+
* already prints as `wachiman-cost` lines. No code, no finding text, no title, no credential.
|
|
10
|
+
*/
|
|
11
|
+
import { spawnSync } from "node:child_process";
|
|
12
|
+
import { createPublicKey, verify } from "node:crypto";
|
|
13
|
+
|
|
14
|
+
/** `curl`'s exit status and output; the HTTP status is read from the output (see `call`). */
|
|
15
|
+
export type CurlFn = (args: string[]) => { status: number; stdout: string; stderr: string };
|
|
16
|
+
|
|
17
|
+
export const defaultCurl: CurlFn = (args) => {
|
|
18
|
+
const run = spawnSync("curl", ["--silent", "--show-error", "--max-time", "20", ...args], { encoding: "utf8" });
|
|
19
|
+
return { status: run.status ?? 1, stdout: run.stdout ?? "", stderr: run.stderr ?? run.error?.message ?? "" };
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export interface LedgerEnv {
|
|
23
|
+
ACTIONS_ID_TOKEN_REQUEST_URL?: string;
|
|
24
|
+
ACTIONS_ID_TOKEN_REQUEST_TOKEN?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The ledger cannot answer: a network error, a timeout, a 5xx or a 429. Only these are an outage. */
|
|
28
|
+
export class LedgerUnavailable extends Error {}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Something answered, but not the way the ledger does: a redirect, a 400, 404 or 405, a 200 whose body
|
|
32
|
+
* is not the documented JSON, a grant that is not a sane amount. That is a wrong `ledger_url` or a
|
|
33
|
+
* broken service, not an outage, so `ledger_unavailable` does not apply: the run stops. The message is
|
|
34
|
+
* ready to show.
|
|
35
|
+
*/
|
|
36
|
+
export class LedgerBadAnswer extends Error {}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The job cannot ask the ledger at all: it holds no OIDC request token. A configuration error that
|
|
40
|
+
* retrying does not fix, so it is not an outage (`ledger_unavailable` does not apply to it).
|
|
41
|
+
*/
|
|
42
|
+
export class LedgerNotPermitted extends Error {}
|
|
43
|
+
|
|
44
|
+
export type RefusalReason = "limit" | "weekly_limit" | "repository_limit" | "not_linked" | "unauthorized";
|
|
45
|
+
|
|
46
|
+
export interface Refusal {
|
|
47
|
+
reason: RefusalReason;
|
|
48
|
+
period?: "day" | "week";
|
|
49
|
+
accountSpentUsd?: number;
|
|
50
|
+
accountLimitUsd?: number;
|
|
51
|
+
day?: string;
|
|
52
|
+
week?: string;
|
|
53
|
+
resetsAt?: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The ledger answered and said no. */
|
|
57
|
+
export class LedgerRefused extends Error {
|
|
58
|
+
constructor(readonly refusal: Refusal) {
|
|
59
|
+
super(`The ledger refused the run: ${refusal.reason}.`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface Reservation {
|
|
64
|
+
reservationId: string;
|
|
65
|
+
grantedUsd: number;
|
|
66
|
+
accountLeftUsd: number;
|
|
67
|
+
repositoryLeftUsd: number | null;
|
|
68
|
+
day: string;
|
|
69
|
+
week: string;
|
|
70
|
+
weekLeftUsd: number | null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface Receipt {
|
|
74
|
+
account: string;
|
|
75
|
+
repository: string;
|
|
76
|
+
run_id: string;
|
|
77
|
+
usd: number;
|
|
78
|
+
day: string;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface ReserveRequest {
|
|
82
|
+
wantedUsd: number;
|
|
83
|
+
callerLimitUsd?: number;
|
|
84
|
+
callerWeeklyLimitUsd?: number;
|
|
85
|
+
trigger: string;
|
|
86
|
+
forced: boolean;
|
|
87
|
+
pr?: number;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** One model call, with the field names of the `wachiman-cost` line (`ledgerCall` in `cost.ts`). */
|
|
91
|
+
export interface LedgerCall {
|
|
92
|
+
operation: string;
|
|
93
|
+
label: string;
|
|
94
|
+
model: string;
|
|
95
|
+
turns: number;
|
|
96
|
+
duration_ms: number;
|
|
97
|
+
input_tokens: number;
|
|
98
|
+
output_tokens: number;
|
|
99
|
+
cache_read_tokens: number;
|
|
100
|
+
cache_write_tokens: number;
|
|
101
|
+
cost_usd: number | null;
|
|
102
|
+
costKnown: boolean;
|
|
103
|
+
terminal_reason: string;
|
|
104
|
+
auth: string;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const b64url = (text: string) => Buffer.from(text, "utf8").toString("base64url");
|
|
108
|
+
|
|
109
|
+
/** The ledger url as configured, minus any trailing slashes, so `${base}/v1/...` has one slash. */
|
|
110
|
+
const base = (ledgerUrl: string) => ledgerUrl.replace(/\/+$/, "");
|
|
111
|
+
|
|
112
|
+
/** Whether an HTTP status means "try later": a server error or a rate limit. Anything else unexpected is not an outage. */
|
|
113
|
+
const isOutage = (status: number) => status >= 500 || status === 429;
|
|
114
|
+
|
|
115
|
+
/** The error for an answer the client cannot use: an outage for 5xx and 429, a wrong-address problem for the rest. */
|
|
116
|
+
function unusable(route: string, status: number, what: string): Error {
|
|
117
|
+
return isOutage(status)
|
|
118
|
+
? new LedgerUnavailable(`The ledger could not ${what} (HTTP ${status}).`)
|
|
119
|
+
: new LedgerBadAnswer(
|
|
120
|
+
`\`ledger_url\` does not look like the spend ledger: POST ${route} answered HTTP ${status}, not the documented answer. Check that \`ledger_url\` is the ledger's address.`,
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** One request. The HTTP status is the last line of the output (`-w "\n%{http_code}"`). */
|
|
125
|
+
function call(curl: CurlFn, args: string[]): { status: number; body: string } {
|
|
126
|
+
const run = curl(["-o", "-", "-w", "\n%{http_code}", ...args]);
|
|
127
|
+
if (run.status !== 0) throw new LedgerUnavailable(`curl failed (exit ${run.status}): ${run.stderr.trim()}`);
|
|
128
|
+
const cut = run.stdout.lastIndexOf("\n");
|
|
129
|
+
const status = Number(run.stdout.slice(cut + 1).trim());
|
|
130
|
+
if (cut < 0 || !Number.isInteger(status)) throw new LedgerUnavailable("curl returned no HTTP status.");
|
|
131
|
+
return { status, body: run.stdout.slice(0, cut) };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const parseObject = (body: string): Record<string, unknown> | undefined => {
|
|
135
|
+
try {
|
|
136
|
+
const value: unknown = JSON.parse(body);
|
|
137
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? (value as Record<string, unknown>) : undefined;
|
|
138
|
+
} catch {
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const post = (ledgerUrl: string, route: string, token: string, payload: unknown, curl: CurlFn) =>
|
|
144
|
+
call(curl, [
|
|
145
|
+
"-X",
|
|
146
|
+
"POST",
|
|
147
|
+
"-H",
|
|
148
|
+
`Authorization: Bearer ${token}`,
|
|
149
|
+
"-H",
|
|
150
|
+
"Content-Type: application/json",
|
|
151
|
+
"--data",
|
|
152
|
+
JSON.stringify(payload),
|
|
153
|
+
`${base(ledgerUrl)}${route}`,
|
|
154
|
+
]);
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* A GitHub OIDC token for `audience`. Needs `id-token: write` in the job, which only
|
|
158
|
+
* `review-platform.yml` grants; without the request variables there is nothing to retry.
|
|
159
|
+
*/
|
|
160
|
+
export function requestIdToken(env: LedgerEnv, audience: string, curl: CurlFn): string {
|
|
161
|
+
const url = env.ACTIONS_ID_TOKEN_REQUEST_URL;
|
|
162
|
+
const bearer = env.ACTIONS_ID_TOKEN_REQUEST_TOKEN;
|
|
163
|
+
if (!url || !bearer) throw new LedgerNotPermitted("this job has no OIDC token: id-token: write is not granted");
|
|
164
|
+
const { status, body } = call(curl, ["-H", `Authorization: Bearer ${bearer}`, `${url}&audience=${encodeURIComponent(audience)}`]);
|
|
165
|
+
const value = parseObject(body)?.value;
|
|
166
|
+
if (status !== 200 || typeof value !== "string" || value === "") {
|
|
167
|
+
if (isOutage(status)) throw new LedgerUnavailable(`GitHub did not issue an id token (HTTP ${status}).`);
|
|
168
|
+
throw new LedgerBadAnswer(
|
|
169
|
+
`GitHub did not issue an OIDC token for the ledger (HTTP ${status}). Check that the job has \`id-token: write\` and calls review-platform.yml.`,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
return value;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const REASONS: readonly RefusalReason[] = ["limit", "weekly_limit", "repository_limit", "not_linked", "unauthorized"];
|
|
176
|
+
const DEFAULT_REASON: Record<number, RefusalReason> = { 402: "limit", 401: "unauthorized", 403: "not_linked" };
|
|
177
|
+
|
|
178
|
+
/** A refusal from the documented JSON, or from the status alone when the body is something else. */
|
|
179
|
+
function refusalOf(status: number, body: string): Refusal | null {
|
|
180
|
+
const json = parseObject(body) ?? {};
|
|
181
|
+
const reason = REASONS.find((r) => r === json.reason) ?? DEFAULT_REASON[status];
|
|
182
|
+
if (!reason) return null;
|
|
183
|
+
const refusal: Refusal = { reason };
|
|
184
|
+
if (json.period === "day" || json.period === "week") refusal.period = json.period;
|
|
185
|
+
if (typeof json.accountSpentUsd === "number") refusal.accountSpentUsd = json.accountSpentUsd;
|
|
186
|
+
if (typeof json.accountLimitUsd === "number") refusal.accountLimitUsd = json.accountLimitUsd;
|
|
187
|
+
if (typeof json.day === "string") refusal.day = json.day;
|
|
188
|
+
if (typeof json.week === "string") refusal.week = json.week;
|
|
189
|
+
if (typeof json.resetsAt === "string") refusal.resetsAt = json.resetsAt;
|
|
190
|
+
return refusal;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function reserve(ledgerUrl: string, token: string, req: ReserveRequest, curl: CurlFn): Reservation {
|
|
194
|
+
const payload = {
|
|
195
|
+
wantedUsd: req.wantedUsd,
|
|
196
|
+
...(req.callerLimitUsd === undefined ? {} : { callerLimitUsd: req.callerLimitUsd }),
|
|
197
|
+
...(req.callerWeeklyLimitUsd === undefined ? {} : { callerWeeklyLimitUsd: req.callerWeeklyLimitUsd }),
|
|
198
|
+
trigger: req.trigger,
|
|
199
|
+
forced: req.forced,
|
|
200
|
+
...(req.pr === undefined ? {} : { pr: req.pr }),
|
|
201
|
+
};
|
|
202
|
+
const { status, body } = post(ledgerUrl, "/v1/reserve", token, payload, curl);
|
|
203
|
+
if (status >= 400 && status < 500) {
|
|
204
|
+
const refusal = refusalOf(status, body);
|
|
205
|
+
if (refusal) throw new LedgerRefused(refusal);
|
|
206
|
+
}
|
|
207
|
+
const json = parseObject(body);
|
|
208
|
+
if (status !== 200 || !json || typeof json.reservationId !== "string" || typeof json.grantedUsd !== "number") {
|
|
209
|
+
throw unusable("/v1/reserve", status, "reserve");
|
|
210
|
+
}
|
|
211
|
+
// A grant that is not a finite, non-negative amount is a malformed answer: never a budget.
|
|
212
|
+
if (!Number.isFinite(json.grantedUsd) || json.grantedUsd < 0) {
|
|
213
|
+
throw new LedgerBadAnswer(
|
|
214
|
+
`\`ledger_url\` does not look like the spend ledger: POST /v1/reserve granted ${String(json.grantedUsd)}, which is not an amount. Check that \`ledger_url\` is the ledger's address.`,
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
return {
|
|
218
|
+
reservationId: json.reservationId,
|
|
219
|
+
grantedUsd: json.grantedUsd,
|
|
220
|
+
accountLeftUsd: Number(json.accountLeftUsd),
|
|
221
|
+
repositoryLeftUsd: typeof json.repositoryLeftUsd === "number" ? json.repositoryLeftUsd : null,
|
|
222
|
+
day: String(json.day),
|
|
223
|
+
week: String(json.week),
|
|
224
|
+
weekLeftUsd: typeof json.weekLeftUsd === "number" ? json.weekLeftUsd : null,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export function settle(
|
|
229
|
+
ledgerUrl: string,
|
|
230
|
+
token: string,
|
|
231
|
+
reservationId: string,
|
|
232
|
+
spentUsd: number,
|
|
233
|
+
calls: LedgerCall[],
|
|
234
|
+
curl: CurlFn,
|
|
235
|
+
): { receipt: Receipt; signature: string } {
|
|
236
|
+
const { status, body } = post(ledgerUrl, "/v1/settle", token, { reservationId, spentUsd, calls }, curl);
|
|
237
|
+
const json = parseObject(body);
|
|
238
|
+
const receipt = json?.receipt;
|
|
239
|
+
if (status !== 200 || !isReceipt(receipt) || typeof json?.signature !== "string") {
|
|
240
|
+
throw unusable("/v1/settle", status, "settle");
|
|
241
|
+
}
|
|
242
|
+
return { receipt, signature: json.signature };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Records a run that could not reserve because the ledger was unreachable, now that it may be
|
|
247
|
+
* reachable again. Stored apart from reserved runs; idempotent per run on the server.
|
|
248
|
+
*/
|
|
249
|
+
export function record(
|
|
250
|
+
ledgerUrl: string,
|
|
251
|
+
token: string,
|
|
252
|
+
run: { spentUsd: number; calls: LedgerCall[]; trigger: string; pr?: number },
|
|
253
|
+
curl: CurlFn,
|
|
254
|
+
): void {
|
|
255
|
+
const payload = {
|
|
256
|
+
spentUsd: run.spentUsd,
|
|
257
|
+
calls: run.calls,
|
|
258
|
+
trigger: run.trigger,
|
|
259
|
+
...(run.pr === undefined ? {} : { pr: run.pr }),
|
|
260
|
+
reason: "unreserved",
|
|
261
|
+
};
|
|
262
|
+
const { status } = post(ledgerUrl, "/v1/record", token, payload, curl);
|
|
263
|
+
if (status !== 200) throw unusable("/v1/record", status, "record the run");
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function isReceipt(value: unknown): value is Receipt {
|
|
267
|
+
const r = value as Record<string, unknown> | null;
|
|
268
|
+
return (
|
|
269
|
+
typeof r === "object" &&
|
|
270
|
+
r !== null &&
|
|
271
|
+
typeof r.account === "string" &&
|
|
272
|
+
typeof r.repository === "string" &&
|
|
273
|
+
typeof r.run_id === "string" &&
|
|
274
|
+
typeof r.usd === "number" &&
|
|
275
|
+
typeof r.day === "string"
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export const receiptFooter = (receipt: Receipt, signature: string) => `<!-- wachi:receipt ${b64url(JSON.stringify(receipt))}.${signature} -->`;
|
|
280
|
+
|
|
281
|
+
const RECEIPT_LINE = /<!-- wachi:receipt ([A-Za-z0-9_-]+)\.([A-Za-z0-9_-]+) -->/g;
|
|
282
|
+
|
|
283
|
+
/** The last receipt line in `body`, or null when there is none or it is damaged. */
|
|
284
|
+
export function readReceipt(body: string): { receipt: Receipt; signature: string } | null {
|
|
285
|
+
const last = [...body.matchAll(RECEIPT_LINE)].pop();
|
|
286
|
+
if (!last) return null;
|
|
287
|
+
try {
|
|
288
|
+
const receipt: unknown = JSON.parse(Buffer.from(last[1]!, "base64url").toString("utf8"));
|
|
289
|
+
return isReceipt(receipt) ? { receipt, signature: last[2]! } : null;
|
|
290
|
+
} catch {
|
|
291
|
+
return null;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** The bytes the ledger signs: JSON with keys in a fixed order (shared contract with the Worker). */
|
|
296
|
+
export const canonicalReceipt = (r: Receipt): Uint8Array =>
|
|
297
|
+
new TextEncoder().encode(JSON.stringify({ account: r.account, repository: r.repository, run_id: r.run_id, usd: r.usd, day: r.day }));
|
|
298
|
+
|
|
299
|
+
/** Whether `signature` (base64url Ed25519) is the ledger's over `receipt`. A bad key or signature is false. */
|
|
300
|
+
export function verifyReceipt(receipt: Receipt, signature: string, publicKeyPem: string): boolean {
|
|
301
|
+
try {
|
|
302
|
+
return verify(null, canonicalReceipt(receipt), createPublicKey(publicKeyPem), Buffer.from(signature, "base64url"));
|
|
303
|
+
} catch {
|
|
304
|
+
return false;
|
|
305
|
+
}
|
|
306
|
+
}
|
package/src/local.ts
CHANGED
|
@@ -57,7 +57,7 @@ export interface LocalArgs {
|
|
|
57
57
|
profile?: string;
|
|
58
58
|
agentPrompt: boolean;
|
|
59
59
|
history: boolean;
|
|
60
|
-
/** `--verify` forces the verifier on, `--no-verify` off; unset follows `
|
|
60
|
+
/** `--verify` forces the verifier on, `--no-verify` off; unset follows `WACHIMAN_VERIFY` (off by default, ADR 0027). */
|
|
61
61
|
verify: boolean | undefined;
|
|
62
62
|
/** `--result-version`: with `--format json`, which result version to write (default 1 for a committed change). */
|
|
63
63
|
resultVersion?: 1 | 2;
|
|
@@ -297,7 +297,7 @@ export function openLocalSession(p: {
|
|
|
297
297
|
const { env, repoDir, deps } = p;
|
|
298
298
|
const loaded = loadConfig(env, p.stderr);
|
|
299
299
|
if (loaded.provider !== "claude-code") {
|
|
300
|
-
throw new UsageError(`
|
|
300
|
+
throw new UsageError(`WACHIMAN_PROVIDER=${loaded.provider}: the local review runs only on Claude Code, your signed-in \`claude\`.`);
|
|
301
301
|
}
|
|
302
302
|
// A person ran it: no automatic ceiling (decision 14).
|
|
303
303
|
const config = {
|
|
@@ -441,7 +441,7 @@ export function localScopeInput(p: {
|
|
|
441
441
|
*/
|
|
442
442
|
export function sizeWarning(estimate: ReviewEstimate, gate: number, budgetUsd: number, model: string): string | null {
|
|
443
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)} (
|
|
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)} (WACHIMAN_BUDGET_USD).`;
|
|
445
445
|
}
|
|
446
446
|
|
|
447
447
|
export function runLocal(
|
|
@@ -460,7 +460,7 @@ export function runLocal(
|
|
|
460
460
|
throw new UsageError("Working-tree reviews require --result-version 2; a tree is not a commit.");
|
|
461
461
|
}
|
|
462
462
|
// Off unless asked for (ADR 0020): without this, nothing below touches ~/.wachiman/history.
|
|
463
|
-
// A bad
|
|
463
|
+
// A bad WACHIMAN_HISTORY_DAYS stops here, before any call is paid for.
|
|
464
464
|
const history = historySettings(env, args.history);
|
|
465
465
|
const home = wachimanHome(env);
|
|
466
466
|
const { config, profile, profileNote, provider, verifier, state } = openLocalSession({
|
|
@@ -355,7 +355,7 @@ export class ClaudeCodeProvider implements ProviderAdapter {
|
|
|
355
355
|
// The session starts in an empty scratch directory, never in the head: Claude Code discovers
|
|
356
356
|
// `CLAUDE.md` (and `.claude/`) from its working directory and its parents, and a pull request
|
|
357
357
|
// must not supply instructions. The head is only an `--add-dir`, which does not load them.
|
|
358
|
-
const workDir = mkdtempSync(join(tmpdir(), "
|
|
358
|
+
const workDir = mkdtempSync(join(tmpdir(), "wachiman-review-claude-"));
|
|
359
359
|
const prompt = locatePrompt(request.prompt, request.cwd, request.addDirs, { shell: false });
|
|
360
360
|
const args = buildClaudeArgs({
|
|
361
361
|
addDirs: [request.cwd, ...request.addDirs],
|
package/src/providers/codex.ts
CHANGED
|
@@ -81,7 +81,7 @@ export const CACHE_WRITE_MULTIPLIER = 2.5
|
|
|
81
81
|
|
|
82
82
|
/**
|
|
83
83
|
* List prices of the models this adapter has been set up for. A model not listed here needs
|
|
84
|
-
* `
|
|
84
|
+
* `WACHIMAN_PRICE_PER_MTOK`: the engine's budgets are in dollars, and Codex reports tokens.
|
|
85
85
|
*/
|
|
86
86
|
export const PRICES: Record<string, Price> = {
|
|
87
87
|
"gpt-6.1-sol": { input: 2, cachedInput: 0.1, output: 10, cacheWrite: 5 },
|
|
@@ -93,7 +93,7 @@ export function parsePrice(raw: string): Price {
|
|
|
93
93
|
const parts = raw.split(",").map((p) => Number(p.trim()));
|
|
94
94
|
if (parts.length < 3 || parts.length > 4 || parts.some((n) => !Number.isFinite(n) || n < 0)) {
|
|
95
95
|
throw new Error(
|
|
96
|
-
`
|
|
96
|
+
`WACHIMAN_PRICE_PER_MTOK must be "input,cached_input,output[,cache_write]" in USD per million tokens, got "${raw}".`,
|
|
97
97
|
);
|
|
98
98
|
}
|
|
99
99
|
return { input: parts[0], cachedInput: parts[1], output: parts[2], ...(parts.length === 4 ? { cacheWrite: parts[3] } : {}) };
|
|
@@ -270,7 +270,7 @@ export class CodexProvider implements ProviderAdapter {
|
|
|
270
270
|
private readonly opts: {
|
|
271
271
|
model: string;
|
|
272
272
|
requireApiKey: boolean;
|
|
273
|
-
/** `
|
|
273
|
+
/** `WACHIMAN_PRICE_PER_MTOK`, for a model not in `PRICES`. */
|
|
274
274
|
priceOverride?: string;
|
|
275
275
|
reasoningEffort?: string;
|
|
276
276
|
/**
|
|
@@ -292,7 +292,7 @@ export class CodexProvider implements ProviderAdapter {
|
|
|
292
292
|
preflight() {
|
|
293
293
|
if (!this.price) {
|
|
294
294
|
throw new Error(
|
|
295
|
-
`No price is known for model "${this.opts.model}". Set
|
|
295
|
+
`No price is known for model "${this.opts.model}". Set WACHIMAN_PRICE_PER_MTOK="input,cached_input,output[,cache_write]" (USD per million tokens).`,
|
|
296
296
|
);
|
|
297
297
|
}
|
|
298
298
|
const probe = spawnSync(this.binary, ["--version"], { encoding: "utf8", env: { PATH: process.env.PATH ?? "" } });
|
|
@@ -314,7 +314,7 @@ export class CodexProvider implements ProviderAdapter {
|
|
|
314
314
|
run(request: ProviderRequest): ProviderResult {
|
|
315
315
|
const unknown = request.allowedTools.filter((t) => !READ_TOOLS.includes(t));
|
|
316
316
|
if (unknown.length > 0) throw new Error(`The Codex adapter only grants reads; asked for ${unknown.join(", ")}.`);
|
|
317
|
-
const scratch = mkdtempSync(join(tmpdir(), "
|
|
317
|
+
const scratch = mkdtempSync(join(tmpdir(), "wachiman-review-codex-"));
|
|
318
318
|
try {
|
|
319
319
|
const workDir = join(scratch, "work");
|
|
320
320
|
const home = join(scratch, "home");
|
package/src/providers/index.ts
CHANGED
|
@@ -7,7 +7,7 @@ export type { NeutralTool, ProviderAdapter, ProviderRequest, ProviderResult } fr
|
|
|
7
7
|
export const PROVIDER_NAMES = ["claude-code", "codex"] as const;
|
|
8
8
|
export const DEFAULT_PROVIDER = "claude-code";
|
|
9
9
|
|
|
10
|
-
/** The model each provider uses when `
|
|
10
|
+
/** The model each provider uses when `WACHIMAN_MODEL` is not set. */
|
|
11
11
|
export const DEFAULT_MODELS: Record<(typeof PROVIDER_NAMES)[number], string> = {
|
|
12
12
|
"claude-code": "claude-sonnet-5-5",
|
|
13
13
|
codex: "gpt-6.1-sol",
|
package/src/providers/types.ts
CHANGED
|
@@ -24,12 +24,12 @@ export type Effort = (typeof EFFORTS)[number];
|
|
|
24
24
|
export const DEFAULT_EFFORT: Effort = "high";
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
* Default cap on the agent's turns per call (`
|
|
27
|
+
* Default cap on the agent's turns per call (`WACHIMAN_MAX_TURNS`). Wachiman's batches take
|
|
28
28
|
* 5–12 turns and an external agent's whole-PR reviews took 19–46, so 40 never cuts a normal batch;
|
|
29
29
|
* it only stops a run that is looping.
|
|
30
30
|
*/
|
|
31
31
|
export const DEFAULT_MAX_TURNS = 40;
|
|
32
|
-
/** Default wall-clock limit on one provider call, in seconds (`
|
|
32
|
+
/** Default wall-clock limit on one provider call, in seconds (`WACHIMAN_CALL_TIMEOUT_S`). */
|
|
33
33
|
export const DEFAULT_CALL_TIMEOUT_S = 900;
|
|
34
34
|
|
|
35
35
|
/** Tool names the engine may ask for. Each adapter maps them to its own names. */
|
|
@@ -108,7 +108,7 @@ export interface ProviderResult {
|
|
|
108
108
|
export type ProviderAuth = "api-key" | "oauth-token" | "subscription";
|
|
109
109
|
|
|
110
110
|
export interface ProviderAdapter {
|
|
111
|
-
/** The value of `
|
|
111
|
+
/** The value of `WACHIMAN_PROVIDER` that selects it. */
|
|
112
112
|
readonly name: string;
|
|
113
113
|
/** What pays for the calls. Absent means `api-key`. */
|
|
114
114
|
readonly auth?: ProviderAuth;
|
package/src/sinks/common.ts
CHANGED
|
@@ -128,9 +128,9 @@ export function costSentence(e: { costUsdEstimate: number; calls: number; turns:
|
|
|
128
128
|
|
|
129
129
|
const STOP_REASONS: Record<Exclude<ReviewResult["execution"]["stop"], "complete">, string> = {
|
|
130
130
|
quota: "the account's usage limit ended the run",
|
|
131
|
-
budget: "the run's estimate guard (
|
|
132
|
-
turns: "the calls hit the turn limit (
|
|
133
|
-
timeout: "the calls hit the time limit (
|
|
131
|
+
budget: "the run's estimate guard (WACHIMAN_BUDGET_USD) was spent",
|
|
132
|
+
turns: "the calls hit the turn limit (WACHIMAN_MAX_TURNS)",
|
|
133
|
+
timeout: "the calls hit the time limit (WACHIMAN_CALL_TIMEOUT_S)",
|
|
134
134
|
error: "no call returned a review",
|
|
135
135
|
};
|
|
136
136
|
|
package/src/status/command.ts
CHANGED
|
@@ -196,7 +196,7 @@ export function runStatus(args: StatusArgs, repoDir: string, env: Record<string,
|
|
|
196
196
|
lines.push(`${runs.length} run${runs.length === 1 ? "" : "s"}, ${usd(total)} estimated (API-equivalent, not a bill), ${runs[0].at.slice(0, 10)} → ${runs.at(-1)!.at.slice(0, 10)}`, "");
|
|
197
197
|
lines.push(` ${pad(args.by, w)} runs cost`);
|
|
198
198
|
for (const g of groups) lines.push(` ${pad(g.key, w)} ${pad(String(g.runs), 4)} ${usd(g.costUsdEstimate)}`);
|
|
199
|
-
lines.push("", retentionDays === 0 ? "New `--history` runs keep all runs (
|
|
199
|
+
lines.push("", retentionDays === 0 ? "New `--history` runs keep all runs (WACHIMAN_HISTORY_DAYS=0). `--json` lists every run with its attributes." : `A new \`--history\` run deletes runs older than ${retentionDays} days (WACHIMAN_HISTORY_DAYS changes that). \`--json\` lists every run with its attributes.`);
|
|
200
200
|
io.stdout(`${lines.join("\n")}\n`);
|
|
201
201
|
return 0;
|
|
202
202
|
}
|