@cappa/cli 0.13.3 → 0.15.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/dist/index.cjs +453 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +457 -21
- package/dist/index.mjs.map +1 -1
- package/integrations/claude/agents/cappa-judge.md +54 -0
- package/integrations/opencode/agents/cappa-judge.md +64 -0
- package/integrations/opencode/commands/cappa-approve.md +21 -0
- package/integrations/opencode/commands/cappa-judge.md +14 -0
- package/integrations/opencode/commands/cappa-review.md +34 -0
- package/integrations/skills/cappa/JUDGING.md +68 -0
- package/integrations/skills/cappa/MASKING.md +40 -0
- package/integrations/skills/cappa/SKILL.md +120 -0
- package/package.json +7 -5
package/dist/index.mjs
CHANGED
|
@@ -1,16 +1,344 @@
|
|
|
1
1
|
import { getLogger, initLogger } from "@cappa/logger";
|
|
2
2
|
import { Command, InvalidArgumentError } from "commander";
|
|
3
3
|
import { configToEngineOptions, defaultConfig, getConfig } from "@cappa/config";
|
|
4
|
-
import { LocalEngine, ScreenshotFileSystem, collectScreenshots, groupScreenshots } from "@cappa/core";
|
|
5
|
-
import path from "node:path";
|
|
4
|
+
import { JUDGED_LABELS, JUDGE_MODES, LocalEngine, ScreenshotFileSystem, clearJudgment, collectScreenshots, groupScreenshots, isJudgedLabel, normalizeChangeType, readAgentReport, runAgentCheck, toSlimAgentReport, writeAgentVerdict } from "@cappa/core";
|
|
6
5
|
import chalk from "chalk";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
import fs from "node:fs";
|
|
9
|
+
import fsp from "node:fs/promises";
|
|
7
10
|
import { z } from "zod";
|
|
8
11
|
import { ProtocolMismatchError, createClient } from "@cappa/client";
|
|
9
12
|
import { createServer, isLoopbackHost } from "@cappa/server";
|
|
10
|
-
import fs from "node:fs";
|
|
11
13
|
import { randomBytes } from "node:crypto";
|
|
12
14
|
//#region package.json
|
|
13
|
-
var version = "0.
|
|
15
|
+
var version = "0.15.0";
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region ../../node_modules/.pnpm/tsdown@0.23.0_@arethetypeswrong+core@0.18.5_oxc-resolver@11.24.2_tsx@4.23.1_typescript@7.0.2/node_modules/tsdown/esm-shims.js
|
|
18
|
+
const getFilename = () => fileURLToPath(import.meta.url);
|
|
19
|
+
const getDirname = () => path.dirname(getFilename());
|
|
20
|
+
const __dirname = /* @__PURE__ */ getDirname();
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/utils/agentSkills.ts
|
|
23
|
+
/**
|
|
24
|
+
* Agent integrations `cappa agent init` can install, and the files each one
|
|
25
|
+
* gets.
|
|
26
|
+
*
|
|
27
|
+
* - `claude` — Claude Code: the skill in `.claude/skills/cappa` plus a
|
|
28
|
+
* `cappa-judge` subagent that judges screenshots in its own context.
|
|
29
|
+
* - `agents` — the vendor-neutral Agent Skills directory `.agents/skills`,
|
|
30
|
+
* read by Codex, OpenCode and other agents that adopted it.
|
|
31
|
+
* - `opencode` — OpenCode: the `.agents` skill plus slash commands that run
|
|
32
|
+
* the review flow and a sandboxed `cappa-judge` subagent that judges
|
|
33
|
+
* screenshots in its own context.
|
|
34
|
+
*
|
|
35
|
+
* Sources are relative to the package's `integrations/` directory; targets
|
|
36
|
+
* are relative to the project.
|
|
37
|
+
*/
|
|
38
|
+
const SKILL = [
|
|
39
|
+
"skills/cappa/SKILL.md",
|
|
40
|
+
"skills/cappa/JUDGING.md",
|
|
41
|
+
"skills/cappa/MASKING.md"
|
|
42
|
+
];
|
|
43
|
+
const skillInto = (dir) => SKILL.map((source) => ({
|
|
44
|
+
source,
|
|
45
|
+
target: path.join(dir, path.basename(source))
|
|
46
|
+
}));
|
|
47
|
+
const STACK_FILES = {
|
|
48
|
+
claude: [...skillInto(".claude/skills/cappa"), {
|
|
49
|
+
source: "claude/agents/cappa-judge.md",
|
|
50
|
+
target: path.join(".claude", "agents", "cappa-judge.md")
|
|
51
|
+
}],
|
|
52
|
+
agents: skillInto(".agents/skills/cappa"),
|
|
53
|
+
opencode: [...skillInto(".agents/skills/cappa"), ...[
|
|
54
|
+
"commands/cappa-review.md",
|
|
55
|
+
"commands/cappa-judge.md",
|
|
56
|
+
"commands/cappa-approve.md",
|
|
57
|
+
"agents/cappa-judge.md"
|
|
58
|
+
].map((file) => ({
|
|
59
|
+
source: `opencode/${file}`,
|
|
60
|
+
target: path.join(".opencode", file)
|
|
61
|
+
}))]
|
|
62
|
+
};
|
|
63
|
+
const SKILL_STACK_NAMES = Object.keys(STACK_FILES);
|
|
64
|
+
/**
|
|
65
|
+
* Find the integration sources shipped with the package.
|
|
66
|
+
*
|
|
67
|
+
* They live in `integrations/` at the package root, which is one level above
|
|
68
|
+
* `dist/` when installed and two above `src/utils/` in development — so walk
|
|
69
|
+
* up instead of hard-coding either depth.
|
|
70
|
+
*/
|
|
71
|
+
const findIntegrationsDir = (from = __dirname) => {
|
|
72
|
+
let current = from;
|
|
73
|
+
while (true) {
|
|
74
|
+
const candidate = path.join(current, "integrations");
|
|
75
|
+
if (fs.existsSync(path.join(candidate, "skills", "cappa", "SKILL.md"))) return candidate;
|
|
76
|
+
const parent = path.dirname(current);
|
|
77
|
+
if (parent === current) throw new Error("Could not locate the bundled cappa agent skill files");
|
|
78
|
+
current = parent;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Pick the stacks to install when none was named: whichever agents the
|
|
83
|
+
* project already shows signs of, falling back to Claude Code.
|
|
84
|
+
*
|
|
85
|
+
* OpenCode wins over plain `agents` — its stack already includes the
|
|
86
|
+
* `.agents` skill, and adds the commands on top.
|
|
87
|
+
*/
|
|
88
|
+
const detectSkillStacks = (cwd) => {
|
|
89
|
+
const has = (entry) => fs.existsSync(path.join(cwd, entry));
|
|
90
|
+
const stacks = [];
|
|
91
|
+
if (has(".claude") || has("CLAUDE.md")) stacks.push("claude");
|
|
92
|
+
if (has(".opencode") || has("opencode.json") || has("opencode.jsonc")) stacks.push("opencode");
|
|
93
|
+
else if (has(".agents") || has("AGENTS.md")) stacks.push("agents");
|
|
94
|
+
return stacks.length > 0 ? stacks : ["claude"];
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* How the project runs its own `cappa` binary, from its lockfile.
|
|
98
|
+
*
|
|
99
|
+
* Written into the installed files because OpenCode commands run shell
|
|
100
|
+
* commands *before* the model sees anything, so a bare `cappa` that is not on
|
|
101
|
+
* `PATH` would inject an error instead of the review state; and agent
|
|
102
|
+
* permission rules match the command text literally.
|
|
103
|
+
*/
|
|
104
|
+
const detectCappaRunner = (cwd) => {
|
|
105
|
+
const has = (entry) => fs.existsSync(path.join(cwd, entry));
|
|
106
|
+
if (has("pnpm-lock.yaml")) return "pnpm exec cappa";
|
|
107
|
+
if (has("yarn.lock")) return "yarn cappa";
|
|
108
|
+
if (has("bun.lock") || has("bun.lockb")) return "bunx cappa";
|
|
109
|
+
return "npx cappa";
|
|
110
|
+
};
|
|
111
|
+
/** Fill the `{{cappa}}` placeholder in a source file. */
|
|
112
|
+
const renderIntegrationFile = (source, runner) => source.replaceAll("{{cappa}}", runner);
|
|
113
|
+
/**
|
|
114
|
+
* Copy each stack's files into the project.
|
|
115
|
+
*
|
|
116
|
+
* A file that exists with different content is left alone unless `force` is
|
|
117
|
+
* set: users are expected to tailor these to their project, and an upgrade
|
|
118
|
+
* must not silently discard that. A target shared by two stacks (the
|
|
119
|
+
* `.agents` skill) is written once.
|
|
120
|
+
*/
|
|
121
|
+
const installAgentSkills = async (options) => {
|
|
122
|
+
const sourceDir = options.sourceDir ?? findIntegrationsDir();
|
|
123
|
+
const runner = options.runner ?? detectCappaRunner(options.cwd);
|
|
124
|
+
const files = [];
|
|
125
|
+
const seen = /* @__PURE__ */ new Set();
|
|
126
|
+
for (const stack of options.stacks) for (const mapping of STACK_FILES[stack]) {
|
|
127
|
+
const target = path.resolve(options.cwd, mapping.target);
|
|
128
|
+
if (seen.has(target)) continue;
|
|
129
|
+
seen.add(target);
|
|
130
|
+
const source = renderIntegrationFile(await fsp.readFile(path.join(sourceDir, mapping.source), "utf8"), runner);
|
|
131
|
+
const existing = await fsp.readFile(target, "utf8").catch(() => void 0);
|
|
132
|
+
if (existing === source) {
|
|
133
|
+
files.push({
|
|
134
|
+
stack,
|
|
135
|
+
path: target,
|
|
136
|
+
status: "unchanged"
|
|
137
|
+
});
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (existing !== void 0 && !options.force) {
|
|
141
|
+
files.push({
|
|
142
|
+
stack,
|
|
143
|
+
path: target,
|
|
144
|
+
status: "skipped"
|
|
145
|
+
});
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
await fsp.mkdir(path.dirname(target), { recursive: true });
|
|
149
|
+
await fsp.writeFile(target, source, "utf8");
|
|
150
|
+
files.push({
|
|
151
|
+
stack,
|
|
152
|
+
path: target,
|
|
153
|
+
status: "written"
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
const warnings = [];
|
|
157
|
+
const claudeSkill = fs.existsSync(path.resolve(options.cwd, ".claude/skills/cappa/SKILL.md"));
|
|
158
|
+
if (options.stacks.includes("opencode") && claudeSkill) warnings.push("OpenCode reads .claude/skills too, and uses that copy of the cappa skill over the one in .agents/skills — keep the two in sync if you tailor them.");
|
|
159
|
+
return {
|
|
160
|
+
runner,
|
|
161
|
+
files,
|
|
162
|
+
warnings
|
|
163
|
+
};
|
|
164
|
+
};
|
|
165
|
+
//#endregion
|
|
166
|
+
//#region src/commands/agent.ts
|
|
167
|
+
const printJson = (value) => {
|
|
168
|
+
process.stdout.write(`${JSON.stringify(value, null, 2)}\n`);
|
|
169
|
+
};
|
|
170
|
+
/** `CI=true`/`CI=1` — but not `CI=false`, which some tools set explicitly. */
|
|
171
|
+
const isCi = (env = process.env) => {
|
|
172
|
+
const value = env.CI?.trim().toLowerCase();
|
|
173
|
+
return !!value && value !== "false" && value !== "0";
|
|
174
|
+
};
|
|
175
|
+
/**
|
|
176
|
+
* Run `action`, reporting a failure as `{ error }` under `--json` instead of
|
|
177
|
+
* a log line an agent would have to find.
|
|
178
|
+
*/
|
|
179
|
+
const run$1 = async (options, action) => {
|
|
180
|
+
try {
|
|
181
|
+
await action();
|
|
182
|
+
} catch (error) {
|
|
183
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
184
|
+
if (options.json) printJson({ error: message });
|
|
185
|
+
else getLogger().error(message);
|
|
186
|
+
process.exitCode = 1;
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
const LABEL_COLORS = {
|
|
190
|
+
"regression-likely": chalk.red,
|
|
191
|
+
"intentional-likely": chalk.green,
|
|
192
|
+
"noise-likely": chalk.gray,
|
|
193
|
+
ambiguous: chalk.yellow
|
|
194
|
+
};
|
|
195
|
+
/** Human-readable lines for a report — what `--json` prints, for people. */
|
|
196
|
+
const describeAgentReport = (report) => {
|
|
197
|
+
const { totals } = report;
|
|
198
|
+
const lines = [`${totals.passed}/${totals.total} passed · ${totals.changed} changed · ${totals.new} new · ${totals.deleted} deleted`];
|
|
199
|
+
if (totals.pendingJudgments > 0) lines.push(chalk.yellow(`${totals.pendingJudgments} awaiting judgment in ${report.outputDir}/.cappa-agent/judgments`));
|
|
200
|
+
for (const entry of report.entries) {
|
|
201
|
+
if (entry.category !== "changed" || !entry.verdict) {
|
|
202
|
+
lines.push(`${chalk.bold(entry.name)} ${chalk.dim(entry.category)}`);
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
const { label, headline, source } = entry.verdict;
|
|
206
|
+
const tags = [source === "agent" ? "judged" : void 0, entry.pending ? "pending" : void 0].filter(Boolean);
|
|
207
|
+
lines.push(`${chalk.bold(entry.name)} ${LABEL_COLORS[label](label)} — ${headline}${tags.length > 0 ? chalk.dim(` (${tags.join(", ")})`) : ""}`);
|
|
208
|
+
if (entry.verdictError) lines.push(chalk.red(` verdict.json ignored: ${entry.verdictError}`));
|
|
209
|
+
}
|
|
210
|
+
return lines;
|
|
211
|
+
};
|
|
212
|
+
const agentCheck = (options) => run$1(options, async () => {
|
|
213
|
+
const config = await getConfig();
|
|
214
|
+
const report = await runAgentCheck({
|
|
215
|
+
outputDir: config.outputDir,
|
|
216
|
+
diff: config.diff,
|
|
217
|
+
judge: options.judge,
|
|
218
|
+
maxTileRegions: options.maxTileRegions
|
|
219
|
+
});
|
|
220
|
+
if (options.json) printJson(toSlimAgentReport(report));
|
|
221
|
+
else getLogger().box({
|
|
222
|
+
title: "Agent Review",
|
|
223
|
+
message: describeAgentReport(report).join("\n")
|
|
224
|
+
});
|
|
225
|
+
if (report.entries.length > 0) process.exitCode = 1;
|
|
226
|
+
});
|
|
227
|
+
const agentJudge = (names, options) => run$1(options, async () => {
|
|
228
|
+
const config = await getConfig();
|
|
229
|
+
const written = [];
|
|
230
|
+
for (const name of names) {
|
|
231
|
+
const verdict = await writeAgentVerdict(config.outputDir, name, {
|
|
232
|
+
label: options.label,
|
|
233
|
+
headline: options.headline,
|
|
234
|
+
rationale: options.rationale,
|
|
235
|
+
confidence: options.confidence
|
|
236
|
+
});
|
|
237
|
+
written.push({
|
|
238
|
+
name,
|
|
239
|
+
verdict
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
if (options.json) printJson({ written });
|
|
243
|
+
else getLogger().success(`Recorded ${options.label} for ${written.length} screenshot(s) — run \`cappa agent check\` to apply`);
|
|
244
|
+
});
|
|
245
|
+
const agentApprove = (names, options) => run$1(options, async () => {
|
|
246
|
+
if (isCi()) throw new Error("Refusing to approve in CI — baselines are only changed on developer machines.");
|
|
247
|
+
if (names.length === 0 && !options.label?.length) throw new Error("Name the screenshots to approve, or pass --label.");
|
|
248
|
+
const config = await getConfig();
|
|
249
|
+
const screenshots = await collectScreenshots(config.outputDir);
|
|
250
|
+
const byName = new Map(screenshots.map((s) => [s.name, s]));
|
|
251
|
+
const wanted = new Set(names);
|
|
252
|
+
const unknown = names.filter((name) => {
|
|
253
|
+
const screenshot = byName.get(name);
|
|
254
|
+
return !screenshot || screenshot.category === "passed";
|
|
255
|
+
});
|
|
256
|
+
if (unknown.length > 0) throw new Error(`Nothing to approve for: ${unknown.join(", ")} — names must match exactly (see \`cappa agent check --json\`).`);
|
|
257
|
+
const verdicts = /* @__PURE__ */ new Map();
|
|
258
|
+
if (!!options.label?.length || names.some((name) => byName.get(name)?.category === "changed")) {
|
|
259
|
+
const previous = await readAgentReport(config.outputDir);
|
|
260
|
+
const report = await runAgentCheck({
|
|
261
|
+
outputDir: config.outputDir,
|
|
262
|
+
diff: config.diff,
|
|
263
|
+
judge: previous?.judge ?? "ambiguous"
|
|
264
|
+
});
|
|
265
|
+
for (const entry of report.entries) if (entry.category === "changed" && entry.verdict) verdicts.set(entry.name, entry.verdict.label);
|
|
266
|
+
}
|
|
267
|
+
const labels = new Set(options.label);
|
|
268
|
+
for (const [name, label] of verdicts) if (labels.has(label)) wanted.add(name);
|
|
269
|
+
const toApprove = [...wanted].flatMap((name) => {
|
|
270
|
+
const screenshot = byName.get(name);
|
|
271
|
+
return screenshot && screenshot.category !== "passed" ? [screenshot] : [];
|
|
272
|
+
});
|
|
273
|
+
const regressions = toApprove.filter((s) => verdicts.get(s.name) === "regression-likely").map((s) => s.name);
|
|
274
|
+
if (regressions.length > 0 && !options.allowRegression) throw new Error(`Refusing to approve regression-likely: ${regressions.join(", ")} — if the change is intended, judge it (\`cappa agent check --judge all\`, then \`cappa agent judge <name> --label intentional-likely\`), or pass --allow-regression.`);
|
|
275
|
+
await new ScreenshotFileSystem(config.outputDir).approveScreenshots(toApprove, config.diff);
|
|
276
|
+
await Promise.all(toApprove.map((screenshot) => clearJudgment(config.outputDir, screenshot.name)));
|
|
277
|
+
const approved = toApprove.map((s) => ({
|
|
278
|
+
name: s.name,
|
|
279
|
+
category: s.category
|
|
280
|
+
}));
|
|
281
|
+
if (options.json) printJson({ approved });
|
|
282
|
+
else if (approved.length === 0) getLogger().warn("No screenshots matched");
|
|
283
|
+
else getLogger().success(`Approved ${approved.length} screenshot(s): ${approved.map((s) => s.name).join(", ")}`);
|
|
284
|
+
});
|
|
285
|
+
const agentInit = (options) => run$1(options, async () => {
|
|
286
|
+
const cwd = process.cwd();
|
|
287
|
+
const stacks = options.stack === "all" ? SKILL_STACK_NAMES : options.stack ? [options.stack] : detectSkillStacks(cwd);
|
|
288
|
+
const { runner, files, warnings } = await installAgentSkills({
|
|
289
|
+
cwd,
|
|
290
|
+
stacks,
|
|
291
|
+
force: options.force,
|
|
292
|
+
runner: options.runner
|
|
293
|
+
});
|
|
294
|
+
if (options.json) {
|
|
295
|
+
printJson({
|
|
296
|
+
stacks,
|
|
297
|
+
runner,
|
|
298
|
+
files,
|
|
299
|
+
warnings
|
|
300
|
+
});
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
const logger = getLogger();
|
|
304
|
+
for (const file of files) {
|
|
305
|
+
const verb = {
|
|
306
|
+
written: chalk.green("wrote"),
|
|
307
|
+
unchanged: chalk.dim("unchanged"),
|
|
308
|
+
skipped: chalk.yellow("skipped (modified — use --force)")
|
|
309
|
+
}[file.status];
|
|
310
|
+
logger.log(`${verb} ${file.path}`);
|
|
311
|
+
}
|
|
312
|
+
logger.log(chalk.dim(`commands run as \`${runner}\` (--runner to change)`));
|
|
313
|
+
for (const warning of warnings) logger.warn(warning);
|
|
314
|
+
logger.success(stacks.includes("opencode") ? "Agent integration installed — run /cappa-review in OpenCode, or /cappa in Claude Code" : "Agent skill installed — ask your agent to run a visual review, or use /cappa in Claude Code");
|
|
315
|
+
});
|
|
316
|
+
const parseChoice = (choices, what) => (value) => {
|
|
317
|
+
if (!choices.includes(value)) throw new InvalidArgumentError(`${what} must be one of: ${choices.join(", ")}`);
|
|
318
|
+
return value;
|
|
319
|
+
};
|
|
320
|
+
const parseLabel = (value) => {
|
|
321
|
+
if (!isJudgedLabel(value)) throw new InvalidArgumentError(`label must be one of: ${JUDGED_LABELS.join(", ")}`);
|
|
322
|
+
return value;
|
|
323
|
+
};
|
|
324
|
+
const collectLabel = (value, previous = []) => [...previous, parseLabel(value)];
|
|
325
|
+
const parseConfidence = (value) => {
|
|
326
|
+
const parsed = Number(value);
|
|
327
|
+
if (!Number.isFinite(parsed) || parsed < 0 || parsed > 1) throw new InvalidArgumentError("confidence must be a number from 0 to 1");
|
|
328
|
+
return parsed;
|
|
329
|
+
};
|
|
330
|
+
const parsePositiveInt = (value) => {
|
|
331
|
+
const parsed = Number(value);
|
|
332
|
+
if (!Number.isInteger(parsed) || parsed < 1) throw new InvalidArgumentError("must be a positive integer");
|
|
333
|
+
return parsed;
|
|
334
|
+
};
|
|
335
|
+
const registerAgentCommand = (program) => {
|
|
336
|
+
const agent = program.command("agent").description("Visual review for coding agents: label changes, hand ambiguous ones to an agent, approve");
|
|
337
|
+
agent.command("check").description("Label every changed screenshot and write judgment requests for an agent (does not capture)").option("--judge <mode>", `which changes get a judgment request: ${JUDGE_MODES.join(", ")}`, parseChoice(JUDGE_MODES, "--judge"), "ambiguous").option("--max-tile-regions <count>", "regions rendered into each regions.png", parsePositiveInt).option("--json", "print a JSON summary to stdout").action(agentCheck);
|
|
338
|
+
agent.command("judge").description("Record a verdict for screenshots with a judgment request").argument("<names...>", "screenshot names, exactly as reported").requiredOption("--label <label>", JUDGED_LABELS.join(" | "), parseLabel).requiredOption("--headline <text>", "one line describing the change").option("--rationale <text...>", "reasons for the verdict").option("--confidence <number>", "0 to 1", parseConfidence).option("--json", "print a JSON summary to stdout").action(agentJudge);
|
|
339
|
+
agent.command("approve").description("Approve screenshots by exact name or by verdict label (refused in CI)").argument("[names...]", "screenshot names, exactly as reported").option("--label <label>", "also approve every changed screenshot with this verdict label (repeatable)", collectLabel).option("--allow-regression", "approve screenshots whose current verdict is regression-likely").option("--json", "print a JSON summary to stdout").action(agentApprove);
|
|
340
|
+
agent.command("init").description("Install the cappa skill for coding agents in this project").option("--stack <stack>", `${SKILL_STACK_NAMES.join(", ")} or all (default: detected)`, parseChoice([...SKILL_STACK_NAMES, "all"], "--stack")).option("--force", "overwrite skill files that were modified").option("--runner <command>", "how the project runs cappa, e.g. `pnpm exec cappa` (default: from the lockfile)").option("--json", "print a JSON summary to stdout").action(agentInit);
|
|
341
|
+
};
|
|
14
342
|
//#endregion
|
|
15
343
|
//#region src/commands/approve.ts
|
|
16
344
|
const approve = async (options) => {
|
|
@@ -47,6 +375,75 @@ const filterScreenshots = (screenshots, filters) => {
|
|
|
47
375
|
});
|
|
48
376
|
};
|
|
49
377
|
//#endregion
|
|
378
|
+
//#region src/utils/captureReport.ts
|
|
379
|
+
const errorMessage = (error) => {
|
|
380
|
+
if (typeof error === "string") return error;
|
|
381
|
+
if (error instanceof Error) return error.message;
|
|
382
|
+
if (typeof error === "object" && error !== null && typeof error.message === "string") return error.message;
|
|
383
|
+
return String(error);
|
|
384
|
+
};
|
|
385
|
+
/**
|
|
386
|
+
* A task failure that is a broken capture rather than a visual difference.
|
|
387
|
+
*
|
|
388
|
+
* A changed or new screenshot is reported as a failed task too; those are
|
|
389
|
+
* already in `results` with their category, so only the failures that never
|
|
390
|
+
* produced a comparable image belong here.
|
|
391
|
+
*/
|
|
392
|
+
const toCaptureError = (failure) => {
|
|
393
|
+
const { result } = failure;
|
|
394
|
+
if (result.error !== void 0 && result.error !== null) return {
|
|
395
|
+
taskId: failure.taskId,
|
|
396
|
+
plugin: failure.pluginName,
|
|
397
|
+
error: errorMessage(result.error)
|
|
398
|
+
};
|
|
399
|
+
if (!result.filepath && result.skipped !== true) return {
|
|
400
|
+
taskId: failure.taskId,
|
|
401
|
+
plugin: failure.pluginName,
|
|
402
|
+
error: "no screenshot was produced"
|
|
403
|
+
};
|
|
404
|
+
};
|
|
405
|
+
const buildCaptureJsonReport = ({ detail, screenshots, durationMs }) => {
|
|
406
|
+
const failures = detail?.failures ?? [];
|
|
407
|
+
const deleted = detail?.deletedScreenshots ?? [];
|
|
408
|
+
const count = (category) => screenshots.filter((s) => s.category === category).length;
|
|
409
|
+
const results = screenshots.filter((s) => s.category !== "passed").map((s) => {
|
|
410
|
+
const origin = s;
|
|
411
|
+
return {
|
|
412
|
+
name: s.name,
|
|
413
|
+
category: s.category,
|
|
414
|
+
...origin.taskId ? {
|
|
415
|
+
taskId: origin.taskId,
|
|
416
|
+
plugin: origin.plugin
|
|
417
|
+
} : {},
|
|
418
|
+
...s.diffMeta?.percentDifference !== void 0 ? { percentDifference: s.diffMeta.percentDifference } : {}
|
|
419
|
+
};
|
|
420
|
+
});
|
|
421
|
+
const errors = failures.flatMap((failure) => {
|
|
422
|
+
const error = toCaptureError(failure);
|
|
423
|
+
return error ? [error] : [];
|
|
424
|
+
});
|
|
425
|
+
return {
|
|
426
|
+
ok: failures.length === 0 && deleted.length === 0,
|
|
427
|
+
...detail ? { runId: detail.id } : {},
|
|
428
|
+
durationMs: Math.round(durationMs),
|
|
429
|
+
tasks: {
|
|
430
|
+
total: detail?.totalTasks ?? 0,
|
|
431
|
+
completed: detail?.completedTasks ?? 0,
|
|
432
|
+
failed: detail?.failedTasks ?? 0
|
|
433
|
+
},
|
|
434
|
+
screenshots: {
|
|
435
|
+
total: screenshots.length,
|
|
436
|
+
passed: count("passed"),
|
|
437
|
+
changed: count("changed"),
|
|
438
|
+
new: count("new"),
|
|
439
|
+
deleted: count("deleted")
|
|
440
|
+
},
|
|
441
|
+
errors,
|
|
442
|
+
results,
|
|
443
|
+
...results.length > 0 ? { next: "cappa agent check --json" } : {}
|
|
444
|
+
};
|
|
445
|
+
};
|
|
446
|
+
//#endregion
|
|
50
447
|
//#region src/utils/describeChanges.ts
|
|
51
448
|
/**
|
|
52
449
|
* The interpretation fields this reporter actually reads.
|
|
@@ -78,23 +475,27 @@ const interpretationSchema = z.object({
|
|
|
78
475
|
regions: z.array(regionSchema).catch([]).optional()
|
|
79
476
|
});
|
|
80
477
|
const severityColor = (severity) => {
|
|
81
|
-
switch (severity) {
|
|
82
|
-
case "
|
|
83
|
-
case "
|
|
84
|
-
case "
|
|
478
|
+
switch (severity.toLowerCase()) {
|
|
479
|
+
case "high": return chalk.red;
|
|
480
|
+
case "medium": return chalk.yellow;
|
|
481
|
+
case "low": return chalk.green;
|
|
85
482
|
default: return chalk.gray;
|
|
86
483
|
}
|
|
87
484
|
};
|
|
88
|
-
/**
|
|
485
|
+
/**
|
|
486
|
+
* Human-readable labels for the `changeType` values reported by blazediff,
|
|
487
|
+
* keyed by their normalised (kebab-case) form so both `color-change` and the
|
|
488
|
+
* older `ColorChange` resolve.
|
|
489
|
+
*/
|
|
89
490
|
const CHANGE_TYPE_LABELS = {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
};
|
|
97
|
-
const changeTypeLabel = (changeType) => CHANGE_TYPE_LABELS[changeType] ?? changeType;
|
|
491
|
+
addition: "added",
|
|
492
|
+
deletion: "removed",
|
|
493
|
+
"color-change": "color",
|
|
494
|
+
"content-change": "content",
|
|
495
|
+
shift: "shifted",
|
|
496
|
+
"rendering-noise": "noise"
|
|
497
|
+
};
|
|
498
|
+
const changeTypeLabel = (changeType) => CHANGE_TYPE_LABELS[normalizeChangeType(changeType)] ?? changeType;
|
|
98
499
|
/** `content at center · 0.90% · 360x200 at (420, 300)` */
|
|
99
500
|
const describeRegion = (region) => {
|
|
100
501
|
const { bbox } = region;
|
|
@@ -607,11 +1008,24 @@ async function renderRunToCompletion(engine, runId, onEvent) {
|
|
|
607
1008
|
unsubscribe?.();
|
|
608
1009
|
}
|
|
609
1010
|
}
|
|
1011
|
+
/**
|
|
1012
|
+
* Print a JSON document and wait until it is flushed: `process.exit` right
|
|
1013
|
+
* after a plain `write` can cut off output that is still queued for a pipe.
|
|
1014
|
+
*/
|
|
1015
|
+
const writeJson = (value) => new Promise((resolve) => {
|
|
1016
|
+
process.stdout.write(`${JSON.stringify(value, null, 2)}\n`, () => resolve());
|
|
1017
|
+
});
|
|
610
1018
|
/** How long a `SIGINT` waits for a remote host to acknowledge cancellation. */
|
|
611
1019
|
const REMOTE_CANCEL_TIMEOUT_MS = 5e3;
|
|
612
1020
|
const runCapture = async (options = {}) => {
|
|
613
1021
|
const logger = getLogger();
|
|
614
1022
|
const captureStart = performance.now();
|
|
1023
|
+
if (options.json && options.watch) {
|
|
1024
|
+
logger.error("--json cannot be combined with --watch: watch never ends.");
|
|
1025
|
+
process.exit(1);
|
|
1026
|
+
return;
|
|
1027
|
+
}
|
|
1028
|
+
if (options.json) logger.level = Math.min(logger.level, 1);
|
|
615
1029
|
if (options.watch && options.server) {
|
|
616
1030
|
logger.error("--watch cannot be combined with --server: a remote host cannot see the files on this machine.");
|
|
617
1031
|
process.exit(1);
|
|
@@ -672,7 +1086,7 @@ const runCapture = async (options = {}) => {
|
|
|
672
1086
|
try {
|
|
673
1087
|
run = await engine.startRun({
|
|
674
1088
|
filter: options.filter,
|
|
675
|
-
clearActual:
|
|
1089
|
+
clearActual: !options.filter
|
|
676
1090
|
});
|
|
677
1091
|
} catch (error) {
|
|
678
1092
|
if (isRunInProgress(error)) {
|
|
@@ -689,7 +1103,7 @@ const runCapture = async (options = {}) => {
|
|
|
689
1103
|
detail = await engine.getRun(run.id);
|
|
690
1104
|
const failedDuringRun = detail?.error !== void 0;
|
|
691
1105
|
const hasFailure = (detail?.failures.length ?? 0) > 0 || (detail?.deletedScreenshots.length ?? 0) > 0;
|
|
692
|
-
if (!failedDuringRun && (isCi || hasFailure)) try {
|
|
1106
|
+
if (!failedDuringRun && (isCi || hasFailure || options.json)) try {
|
|
693
1107
|
groupedScreenshots = await engine.listScreenshots();
|
|
694
1108
|
} catch (err) {
|
|
695
1109
|
logger.warn("Could not collect screenshot results:", err);
|
|
@@ -698,11 +1112,32 @@ const runCapture = async (options = {}) => {
|
|
|
698
1112
|
unregisterSignalHandlers();
|
|
699
1113
|
if (!options.watch) await engine.close();
|
|
700
1114
|
}
|
|
701
|
-
if (detail?.error)
|
|
1115
|
+
if (detail?.error) {
|
|
1116
|
+
if (options.json) {
|
|
1117
|
+
await writeJson({
|
|
1118
|
+
ok: false,
|
|
1119
|
+
runId: detail.id,
|
|
1120
|
+
error: detail.error.message
|
|
1121
|
+
});
|
|
1122
|
+
process.exit(1);
|
|
1123
|
+
return;
|
|
1124
|
+
}
|
|
1125
|
+
throw toThrowable(detail.error);
|
|
1126
|
+
}
|
|
702
1127
|
const failures = detail?.failures ?? [];
|
|
703
1128
|
const deletedScreenshots = detail?.deletedScreenshots ?? [];
|
|
704
1129
|
const hasScreenshotFailure = failures.length > 0 || deletedScreenshots.length > 0;
|
|
705
1130
|
if (isCi) await executeOnFailCallback(config, groupedScreenshots, { remote });
|
|
1131
|
+
if (options.json) {
|
|
1132
|
+
const report = buildCaptureJsonReport({
|
|
1133
|
+
detail,
|
|
1134
|
+
screenshots: groupedScreenshots,
|
|
1135
|
+
durationMs: performance.now() - captureStart
|
|
1136
|
+
});
|
|
1137
|
+
await writeJson(report);
|
|
1138
|
+
if (!report.ok) process.exit(1);
|
|
1139
|
+
return;
|
|
1140
|
+
}
|
|
706
1141
|
const duration = formatDuration(performance.now() - captureStart);
|
|
707
1142
|
if (hasScreenshotFailure) {
|
|
708
1143
|
const reportMessage = generateFailureReportMessage(failures, deletedScreenshots);
|
|
@@ -732,7 +1167,7 @@ const runCapture = async (options = {}) => {
|
|
|
732
1167
|
}
|
|
733
1168
|
};
|
|
734
1169
|
const registerCaptureCommand = (program) => {
|
|
735
|
-
program.command("capture").description("Capture screenshots").option("--ci", "run capture in CI mode and execute onFail callback").option("-f, --filter <pattern>", "only capture tasks whose id matches the given glob pattern").option("--max-regions <count>", "maximum number of interpreted diff regions listed per changed screenshot (0 to disable)", parseMaxRegions, 5).option("--server <url>", "capture against a `cappa serve` host instead of a local browser").option("--token <token>", "access token for --server (falls back to CAPPA_TOKEN)").option("-w, --watch", "capture once, then re-capture affected tasks whenever files change").action(async (options) => {
|
|
1170
|
+
program.command("capture").description("Capture screenshots").option("--ci", "run capture in CI mode and execute onFail callback").option("--json", "print a JSON summary to stdout instead of progress and reports").option("-f, --filter <pattern>", "only capture tasks whose id matches the given glob pattern").option("--max-regions <count>", "maximum number of interpreted diff regions listed per changed screenshot (0 to disable)", parseMaxRegions, 5).option("--server <url>", "capture against a `cappa serve` host instead of a local browser").option("--token <token>", "access token for --server (falls back to CAPPA_TOKEN)").option("-w, --watch", "capture once, then re-capture affected tasks whenever files change").action(async (options) => {
|
|
736
1171
|
await runCapture(options);
|
|
737
1172
|
});
|
|
738
1173
|
};
|
|
@@ -860,6 +1295,7 @@ program.command("review").description("Open the review and capture UI").option("
|
|
|
860
1295
|
program.command("serve").description("Host a capture engine for a remote `cappa capture --server`").option("-p, --port <port>", "port to listen on", (v) => Number.parseInt(v, 10)).option("--host <host>", "host to bind to (a non-loopback host requires an access token)").option("--read-only", "serve without capture, approval or any other mutation").option("--token <token>", "require this access token on every API request (falls back to CAPPA_TOKEN)").option("--no-ui", "serve the API only, without the review UI").action(serve);
|
|
861
1296
|
program.command("approve").description("Approve screenshots").option("-f, --filter <filter...>", "only approve screenshots whose name includes the provided filter value(s)").action(approve);
|
|
862
1297
|
program.command("status").description("Get status of screenshots").option("--max-regions <count>", "maximum number of interpreted diff regions listed per changed screenshot (0 to disable)", parseMaxRegions, 5).action(status);
|
|
1298
|
+
registerAgentCommand(program);
|
|
863
1299
|
program.command("init").description("Initialize Cappa in the current directory").action(init);
|
|
864
1300
|
async function run() {
|
|
865
1301
|
await program.parseAsync();
|