vibecode-anything 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +407 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +2519 -0
- package/dist/core/adapters.d.ts +31 -0
- package/dist/core/adapters.js +478 -0
- package/dist/core/agent-adapter.d.ts +67 -0
- package/dist/core/agent-adapter.js +312 -0
- package/dist/core/anti-cheat.d.ts +6 -0
- package/dist/core/anti-cheat.js +372 -0
- package/dist/core/badge.d.ts +18 -0
- package/dist/core/badge.js +360 -0
- package/dist/core/cache.d.ts +47 -0
- package/dist/core/cache.js +403 -0
- package/dist/core/ci-export.d.ts +35 -0
- package/dist/core/ci-export.js +796 -0
- package/dist/core/ci-formats.d.ts +21 -0
- package/dist/core/ci-formats.js +388 -0
- package/dist/core/client-codegen.d.ts +36 -0
- package/dist/core/client-codegen.js +2538 -0
- package/dist/core/compose.d.ts +71 -0
- package/dist/core/compose.js +1088 -0
- package/dist/core/diff.d.ts +10 -0
- package/dist/core/diff.js +207 -0
- package/dist/core/docs-export.d.ts +37 -0
- package/dist/core/docs-export.js +2706 -0
- package/dist/core/doctor.d.ts +70 -0
- package/dist/core/doctor.js +525 -0
- package/dist/core/errors.d.ts +32 -0
- package/dist/core/errors.js +81 -0
- package/dist/core/exec.d.ts +38 -0
- package/dist/core/exec.js +336 -0
- package/dist/core/fixtures.d.ts +153 -0
- package/dist/core/fixtures.js +2245 -0
- package/dist/core/format.d.ts +77 -0
- package/dist/core/format.js +632 -0
- package/dist/core/fs-snapshot.d.ts +37 -0
- package/dist/core/fs-snapshot.js +216 -0
- package/dist/core/graphql-export.d.ts +34 -0
- package/dist/core/graphql-export.js +2482 -0
- package/dist/core/har-export.d.ts +29 -0
- package/dist/core/har-export.js +988 -0
- package/dist/core/html-report.d.ts +27 -0
- package/dist/core/html-report.js +1535 -0
- package/dist/core/http-export.d.ts +31 -0
- package/dist/core/http-export.js +726 -0
- package/dist/core/impact.d.ts +80 -0
- package/dist/core/impact.js +1412 -0
- package/dist/core/lint.d.ts +38 -0
- package/dist/core/lint.js +1111 -0
- package/dist/core/load-export.d.ts +37 -0
- package/dist/core/load-export.js +1813 -0
- package/dist/core/mcp-export.d.ts +35 -0
- package/dist/core/mcp-export.js +1736 -0
- package/dist/core/mock-server.d.ts +47 -0
- package/dist/core/mock-server.js +1722 -0
- package/dist/core/openapi-export.d.ts +44 -0
- package/dist/core/openapi-export.js +1074 -0
- package/dist/core/parity.d.ts +14 -0
- package/dist/core/parity.js +173 -0
- package/dist/core/playwright-exporter.d.ts +23 -0
- package/dist/core/playwright-exporter.js +1343 -0
- package/dist/core/png.d.ts +7 -0
- package/dist/core/png.js +339 -0
- package/dist/core/regression.d.ts +54 -0
- package/dist/core/regression.js +529 -0
- package/dist/core/responsive.d.ts +86 -0
- package/dist/core/responsive.js +606 -0
- package/dist/core/run.d.ts +74 -0
- package/dist/core/run.js +696 -0
- package/dist/core/scheduler.d.ts +24 -0
- package/dist/core/scheduler.js +162 -0
- package/dist/core/schema-export.d.ts +29 -0
- package/dist/core/schema-export.js +2215 -0
- package/dist/core/schema.d.ts +18 -0
- package/dist/core/schema.js +1057 -0
- package/dist/core/security.d.ts +233 -0
- package/dist/core/security.js +1839 -0
- package/dist/core/server-export.d.ts +38 -0
- package/dist/core/server-export.js +3198 -0
- package/dist/core/slices.d.ts +24 -0
- package/dist/core/slices.js +294 -0
- package/dist/core/spec-writer.d.ts +9 -0
- package/dist/core/spec-writer.js +1202 -0
- package/dist/core/store.d.ts +26 -0
- package/dist/core/store.js +217 -0
- package/dist/core/target-map.d.ts +34 -0
- package/dist/core/target-map.js +262 -0
- package/dist/core/test-exporter.d.ts +25 -0
- package/dist/core/test-exporter.js +1918 -0
- package/dist/core/types.d.ts +919 -0
- package/dist/core/types.js +4 -0
- package/dist/core/visual-semantic.d.ts +67 -0
- package/dist/core/visual-semantic.js +673 -0
- package/dist/core/visual.d.ts +34 -0
- package/dist/core/visual.js +508 -0
- package/dist/core/watch.d.ts +78 -0
- package/dist/core/watch.js +495 -0
- package/dist/core/yaml.d.ts +5 -0
- package/dist/core/yaml.js +1009 -0
- package/dist/diagnostics/diagnose.d.ts +53 -0
- package/dist/diagnostics/diagnose.js +1083 -0
- package/dist/diagnostics/flake.d.ts +71 -0
- package/dist/diagnostics/flake.js +608 -0
- package/dist/diagnostics/mutation-audit.d.ts +69 -0
- package/dist/diagnostics/mutation-audit.js +769 -0
- package/dist/diagnostics/spatial.d.ts +42 -0
- package/dist/diagnostics/spatial.js +346 -0
- package/dist/diagnostics/trends.d.ts +122 -0
- package/dist/diagnostics/trends.js +1190 -0
- package/dist/discovery/discover.d.ts +111 -0
- package/dist/discovery/discover.js +2017 -0
- package/dist/discovery/lock.d.ts +116 -0
- package/dist/discovery/lock.js +1505 -0
- package/dist/discovery/routes.d.ts +36 -0
- package/dist/discovery/routes.js +1494 -0
- package/dist/discovery/test-importer.d.ts +92 -0
- package/dist/discovery/test-importer.js +4952 -0
- package/dist/index.d.ts +123 -0
- package/dist/index.js +99 -0
- package/dist/ingestors/api-collections.d.ts +28 -0
- package/dist/ingestors/api-collections.js +2335 -0
- package/dist/ingestors/api-repo.d.ts +6 -0
- package/dist/ingestors/api-repo.js +1120 -0
- package/dist/ingestors/asyncapi.d.ts +26 -0
- package/dist/ingestors/asyncapi.js +2105 -0
- package/dist/ingestors/bridges.d.ts +5 -0
- package/dist/ingestors/bridges.js +1264 -0
- package/dist/ingestors/cli.d.ts +10 -0
- package/dist/ingestors/cli.js +454 -0
- package/dist/ingestors/curl.d.ts +14 -0
- package/dist/ingestors/curl.js +1024 -0
- package/dist/ingestors/graphql.d.ts +56 -0
- package/dist/ingestors/graphql.js +1852 -0
- package/dist/ingestors/http-file.d.ts +20 -0
- package/dist/ingestors/http-file.js +923 -0
- package/dist/ingestors/image.d.ts +2 -0
- package/dist/ingestors/image.js +147 -0
- package/dist/ingestors/index.d.ts +5 -0
- package/dist/ingestors/index.js +577 -0
- package/dist/ingestors/openrpc.d.ts +29 -0
- package/dist/ingestors/openrpc.js +1807 -0
- package/dist/ingestors/proto.d.ts +47 -0
- package/dist/ingestors/proto.js +2690 -0
- package/dist/ingestors/trace.d.ts +10 -0
- package/dist/ingestors/trace.js +599 -0
- package/dist/ingestors/video.d.ts +50 -0
- package/dist/ingestors/video.js +655 -0
- package/dist/ingestors/web.d.ts +40 -0
- package/dist/ingestors/web.js +839 -0
- package/dist/mcp/server.d.ts +2 -0
- package/dist/mcp/server.js +3969 -0
- package/dist/oracles/api-repo.d.ts +2 -0
- package/dist/oracles/api-repo.js +286 -0
- package/dist/oracles/cli.d.ts +2 -0
- package/dist/oracles/cli.js +205 -0
- package/dist/oracles/http-workflow.d.ts +68 -0
- package/dist/oracles/http-workflow.js +828 -0
- package/dist/oracles/image.d.ts +2 -0
- package/dist/oracles/image.js +429 -0
- package/dist/oracles/index.d.ts +6 -0
- package/dist/oracles/index.js +320 -0
- package/dist/oracles/perf-budget.d.ts +90 -0
- package/dist/oracles/perf-budget.js +1329 -0
- package/dist/oracles/stream.d.ts +94 -0
- package/dist/oracles/stream.js +1268 -0
- package/dist/oracles/ui-states.d.ts +49 -0
- package/dist/oracles/ui-states.js +894 -0
- package/dist/oracles/video.d.ts +21 -0
- package/dist/oracles/video.js +627 -0
- package/dist/oracles/web.d.ts +2 -0
- package/dist/oracles/web.js +871 -0
- package/dist/plugins/builtins.d.ts +12 -0
- package/dist/plugins/builtins.js +1052 -0
- package/dist/plugins/registry.d.ts +86 -0
- package/dist/plugins/registry.js +554 -0
- package/dist/setup/index.d.ts +7 -0
- package/dist/setup/index.js +298 -0
- package/package.json +54 -4
- package/skills/vibecode-anything/SKILL.md +75 -0
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { VibecodeError, errorMessage } from "../core/errors.js";
|
|
4
|
+
import { STRICT_CONTROL_CHARS_RE, isForbiddenObjectKey, isInsideVaultDirectory, isPathWithinDir, parseSafeHttpBaseUrl, redactSensitiveText, resolveSafeTargetUrl } from "../core/security.js";
|
|
5
|
+
import { artifactPath, ensureDir, resolveWorkspace } from "../core/store.js";
|
|
6
|
+
import { readImageDimensions } from "../core/png.js";
|
|
7
|
+
import { decodeImage, computeSSIM, computePixelDiff, computeDHash, hammingDistanceHex, extractPalette, comparePalettes, extractLayoutRegions, compareRegionsIoU, estimateTypographyBands, writeDiffPng } from "../core/visual.js";
|
|
8
|
+
import { auditWcagContrast } from "../core/visual-semantic.js";
|
|
9
|
+
import { loadPlaywright, launchBrowser } from "../core/adapters.js";
|
|
10
|
+
function applyWcagAudit(candImg, level, failures, metrics, diff) {
|
|
11
|
+
const wcag = auditWcagContrast(candImg, { level });
|
|
12
|
+
metrics.wcagAudit = {
|
|
13
|
+
passed: wcag.passed,
|
|
14
|
+
level: wcag.level,
|
|
15
|
+
worstContrastRatio: wcag.worstContrastRatio,
|
|
16
|
+
violationsCount: wcag.violations.length,
|
|
17
|
+
checkedRegions: wcag.checkedRegions,
|
|
18
|
+
minRequiredRatio: wcag.minRequiredRatio,
|
|
19
|
+
designTokens: wcag.designTokens
|
|
20
|
+
};
|
|
21
|
+
if (!wcag.passed) {
|
|
22
|
+
for (const v of wcag.violations.slice(0, 10)) {
|
|
23
|
+
failures.push(`WCAG ${v.level} contrast ratio ${v.contrastRatio.toFixed(2)}:1 < ${v.requiredRatio.toFixed(1)}:1 ` +
|
|
24
|
+
`in region ${v.regionId} (${v.foregroundHex} on ${v.backgroundHex})`);
|
|
25
|
+
}
|
|
26
|
+
if (wcag.violations.length > 10) {
|
|
27
|
+
failures.push(`WCAG ${wcag.level} contrast audit found ${wcag.violations.length} total contrast violations`);
|
|
28
|
+
}
|
|
29
|
+
diff.wcagViolations = wcag.violations;
|
|
30
|
+
}
|
|
31
|
+
return wcag;
|
|
32
|
+
}
|
|
33
|
+
function wcagAuditScore(wcag) {
|
|
34
|
+
if (!wcag || wcag.passed)
|
|
35
|
+
return 1;
|
|
36
|
+
const req = wcag.minRequiredRatio > 0 ? wcag.minRequiredRatio : 4.5;
|
|
37
|
+
return Math.max(0, Math.min(0.99, wcag.worstContrastRatio / req));
|
|
38
|
+
}
|
|
39
|
+
function assertValidPngHeaderIfPng(filePath) {
|
|
40
|
+
if (path.extname(filePath).toLowerCase() !== ".png")
|
|
41
|
+
return;
|
|
42
|
+
const dims = readImageDimensions(fs.readFileSync(filePath));
|
|
43
|
+
if (!dims || dims.format !== "png" || !(dims.width > 0 && dims.height > 0)) {
|
|
44
|
+
throw new VibecodeError("UNSUPPORTED_FORMAT", `Corrupt or invalid PNG image: ${path.basename(filePath)}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function resolveReference(invariant, ctx) {
|
|
48
|
+
const refKey = invariant.input?.referenceArtifact;
|
|
49
|
+
if (!refKey || typeof refKey !== "string" || isForbiddenObjectKey(refKey)) {
|
|
50
|
+
throw new VibecodeError("CONTRACT_INVALID", `Invariant ${invariant.id} missing input.referenceArtifact`);
|
|
51
|
+
}
|
|
52
|
+
const files = ctx.contract?.artifacts?.files || {};
|
|
53
|
+
const rel = Object.prototype.hasOwnProperty.call(files, refKey) ? files[refKey] : undefined;
|
|
54
|
+
if (!rel || typeof rel !== "string") {
|
|
55
|
+
throw new VibecodeError("CONTRACT_INVALID", `Reference artifact ${refKey} not found in contract.artifacts.files`);
|
|
56
|
+
}
|
|
57
|
+
if (STRICT_CONTROL_CHARS_RE.test(rel) ||
|
|
58
|
+
path.isAbsolute(rel) ||
|
|
59
|
+
/^[A-Za-z]:/.test(rel) ||
|
|
60
|
+
isInsideVaultDirectory(rel)) {
|
|
61
|
+
throw new VibecodeError("CONFIG_INVALID", "Reference artifact path must remain inside .vibecode/artifacts and outside vault directories.");
|
|
62
|
+
}
|
|
63
|
+
const { artifactsDir } = resolveWorkspace(ctx.cwd);
|
|
64
|
+
const abs = artifactPath(rel, ctx.cwd);
|
|
65
|
+
if (!isPathWithinDir(abs, artifactsDir, { allowEqual: false }) || isInsideVaultDirectory(abs)) {
|
|
66
|
+
throw new VibecodeError("CONFIG_INVALID", "Reference artifact path must remain inside .vibecode/artifacts and outside vault directories.");
|
|
67
|
+
}
|
|
68
|
+
if (!fs.existsSync(abs) || !fs.statSync(abs).isFile()) {
|
|
69
|
+
throw new VibecodeError("CONTRACT_NOT_FOUND", `Reference artifact missing on disk: ${redactSensitiveText(rel)}`);
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
const realArtifacts = fs.realpathSync(artifactsDir);
|
|
73
|
+
const realAbs = fs.realpathSync(abs);
|
|
74
|
+
if (!isPathWithinDir(realAbs, realArtifacts, { allowEqual: false }) || isInsideVaultDirectory(realAbs)) {
|
|
75
|
+
throw new VibecodeError("CONFIG_INVALID", "Reference artifact symlink must remain inside .vibecode/artifacts and outside vault directories.");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
if (err instanceof VibecodeError)
|
|
80
|
+
throw err;
|
|
81
|
+
throw new VibecodeError("CONFIG_INVALID", "Reference artifact path could not be verified safely.");
|
|
82
|
+
}
|
|
83
|
+
return { refKey, rel, abs };
|
|
84
|
+
}
|
|
85
|
+
async function captureUrlScreenshot(url, ctx, deps) {
|
|
86
|
+
const explicitPw = Object.prototype.hasOwnProperty.call(deps, "playwright") ? deps.playwright : undefined;
|
|
87
|
+
const pw = explicitPw === null ? null : (explicitPw || (await loadPlaywright({ ...deps, cwd: ctx.cwd })));
|
|
88
|
+
if (!pw) {
|
|
89
|
+
throw new VibecodeError("ADAPTER_MISSING", "Cannot capture URL target as image without Playwright", {
|
|
90
|
+
adapter: "playwright",
|
|
91
|
+
installHint: "npm install -D playwright && npx playwright install chromium",
|
|
92
|
+
fallbackHint: "Provide a PNG screenshot path as --target instead of a URL."
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
const browser = await launchBrowser(pw, { featureName: "Image URL capture" });
|
|
96
|
+
try {
|
|
97
|
+
const viewport = ctx.viewport || { width: 1280, height: 720 };
|
|
98
|
+
const page = await browser.newPage({ viewport });
|
|
99
|
+
try {
|
|
100
|
+
await page.goto(url, { waitUntil: "networkidle", timeout: 15000 });
|
|
101
|
+
const outDir = path.join(ctx.cacheDir || path.join(ctx.cwd || process.cwd(), ".vibecode", "cache"), "captures");
|
|
102
|
+
ensureDir(outDir);
|
|
103
|
+
const outPath = path.join(outDir, `${invariantSafeName(ctx.currentInvariantId || "capture")}.png`);
|
|
104
|
+
await page.screenshot({ path: outPath, fullPage: false });
|
|
105
|
+
return outPath;
|
|
106
|
+
}
|
|
107
|
+
finally {
|
|
108
|
+
try {
|
|
109
|
+
await page.close?.();
|
|
110
|
+
}
|
|
111
|
+
catch { /* ignore */ }
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
finally {
|
|
115
|
+
try {
|
|
116
|
+
await browser.close();
|
|
117
|
+
}
|
|
118
|
+
catch { /* ignore */ }
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function invariantSafeName(id) {
|
|
122
|
+
return String(id).replace(/[^A-Za-z0-9_.-]+/g, "-");
|
|
123
|
+
}
|
|
124
|
+
async function resolveCandidatePath(invariant, ctx, deps) {
|
|
125
|
+
const target = ctx.target;
|
|
126
|
+
if (!target)
|
|
127
|
+
throw new VibecodeError("TARGET_MISSING", "Missing --target <image-path-or-url> for image verification.");
|
|
128
|
+
const trimmed = target.trim();
|
|
129
|
+
const looksLikeUriScheme = /^[a-z][a-z0-9+.-]*:/i.test(trimmed) && !/^[a-z]:[\\/]/i.test(trimmed);
|
|
130
|
+
if (/^https?:\/\//i.test(trimmed) || looksLikeUriScheme || trimmed.startsWith("//")) {
|
|
131
|
+
let targetUrl;
|
|
132
|
+
try {
|
|
133
|
+
const baseUrl = parseSafeHttpBaseUrl(trimmed);
|
|
134
|
+
if (typeof invariant.input?.urlPath === "string" && invariant.input.urlPath) {
|
|
135
|
+
targetUrl = resolveSafeTargetUrl(invariant.input.urlPath, baseUrl.toString()).toString();
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
targetUrl = /^https?:\/\//i.test(trimmed) ? trimmed : baseUrl.toString();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
catch (err) {
|
|
142
|
+
throw new VibecodeError("CONFIG_INVALID", redactSensitiveText(`Invalid image target URL: ${errorMessage(err)}`));
|
|
143
|
+
}
|
|
144
|
+
return captureUrlScreenshot(targetUrl, { ...ctx, currentInvariantId: invariant.id }, deps);
|
|
145
|
+
}
|
|
146
|
+
const abs = path.resolve(ctx.cwd || process.cwd(), target);
|
|
147
|
+
if (isInsideVaultDirectory(abs)) {
|
|
148
|
+
throw new VibecodeError("CONFIG_INVALID", "Target image path must not point inside a holdout vault directory.");
|
|
149
|
+
}
|
|
150
|
+
if (!fs.existsSync(abs))
|
|
151
|
+
throw new VibecodeError("TARGET_MISSING", `Target image not found: ${redactSensitiveText(target)}`);
|
|
152
|
+
if (fs.statSync(abs).isDirectory()) {
|
|
153
|
+
throw new VibecodeError("UNSUPPORTED_FORMAT", `Target image path is a directory: ${redactSensitiveText(target)}`);
|
|
154
|
+
}
|
|
155
|
+
return abs;
|
|
156
|
+
}
|
|
157
|
+
function writeDiffArtifact(diffMask, invariant, ctx) {
|
|
158
|
+
const cacheDir = ctx.cacheDir || path.join(ctx.cwd || process.cwd(), ".vibecode", "cache");
|
|
159
|
+
const diffDir = path.join(cacheDir, "diffs");
|
|
160
|
+
ensureDir(diffDir);
|
|
161
|
+
const file = path.join(diffDir, `${invariantSafeName(invariant.id)}.png`);
|
|
162
|
+
writeDiffPng(diffMask, file);
|
|
163
|
+
return path.relative(ctx.cwd || process.cwd(), file).replace(/\\/g, "/");
|
|
164
|
+
}
|
|
165
|
+
function checkDimensions(expectedDims, actualDims) {
|
|
166
|
+
if (!expectedDims)
|
|
167
|
+
return [];
|
|
168
|
+
const failures = [];
|
|
169
|
+
if (!actualDims || !(actualDims.width > 0 && actualDims.height > 0)) {
|
|
170
|
+
failures.push(`invalid image dimensions ${actualDims?.width}x${actualDims?.height}`);
|
|
171
|
+
return failures;
|
|
172
|
+
}
|
|
173
|
+
if (typeof expectedDims.width === "number" && expectedDims.width !== actualDims.width) {
|
|
174
|
+
failures.push(`width expected ${expectedDims.width}, got ${actualDims.width}`);
|
|
175
|
+
}
|
|
176
|
+
if (typeof expectedDims.height === "number" && expectedDims.height !== actualDims.height) {
|
|
177
|
+
failures.push(`height expected ${expectedDims.height}, got ${actualDims.height}`);
|
|
178
|
+
}
|
|
179
|
+
if (typeof expectedDims.aspectRatioTolerance === "number") {
|
|
180
|
+
const expectedRatio = typeof expectedDims.aspectRatio === "number" && expectedDims.aspectRatio > 0
|
|
181
|
+
? expectedDims.aspectRatio
|
|
182
|
+
: typeof expectedDims.width === "number" &&
|
|
183
|
+
expectedDims.width > 0 &&
|
|
184
|
+
typeof expectedDims.height === "number" &&
|
|
185
|
+
expectedDims.height > 0
|
|
186
|
+
? expectedDims.width / expectedDims.height
|
|
187
|
+
: null;
|
|
188
|
+
if (expectedRatio !== null) {
|
|
189
|
+
const actualRatio = actualDims.width / actualDims.height;
|
|
190
|
+
if (Math.abs(expectedRatio - actualRatio) > expectedDims.aspectRatioTolerance) {
|
|
191
|
+
failures.push(`aspectRatio delta ${Math.abs(expectedRatio - actualRatio).toFixed(4)} > ${expectedDims.aspectRatioTolerance}`);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return failures;
|
|
196
|
+
}
|
|
197
|
+
async function decodeCached(filePath, ctx, deps) {
|
|
198
|
+
if (!ctx?.runtimeCache || Object.keys(deps || {}).length > 0) {
|
|
199
|
+
return decodeImage(filePath, deps);
|
|
200
|
+
}
|
|
201
|
+
const st = fs.statSync(filePath);
|
|
202
|
+
const key = `img:${filePath}:${st.size}:${st.mtimeMs}`;
|
|
203
|
+
const cached = ctx.runtimeCache[key];
|
|
204
|
+
if (!cached) {
|
|
205
|
+
const decoded = await decodeImage(filePath, deps);
|
|
206
|
+
ctx.runtimeCache[key] = decoded;
|
|
207
|
+
return decoded;
|
|
208
|
+
}
|
|
209
|
+
return cached;
|
|
210
|
+
}
|
|
211
|
+
async function verifyLayout(invariant, ctx, deps) {
|
|
212
|
+
const { abs: refPath } = resolveReference(invariant, ctx);
|
|
213
|
+
const candPath = await resolveCandidatePath(invariant, ctx, deps);
|
|
214
|
+
assertValidPngHeaderIfPng(refPath);
|
|
215
|
+
assertValidPngHeaderIfPng(candPath);
|
|
216
|
+
const expected = invariant.expected || {};
|
|
217
|
+
const tolerance = invariant.tolerance || {};
|
|
218
|
+
const mode = ctx.mode ?? "exact";
|
|
219
|
+
const skipPalette = mode === "reskin" || mode === "structural";
|
|
220
|
+
const runWcag = mode === "reskin" || expected.wcagLevel !== undefined;
|
|
221
|
+
const expectedPalette = Array.isArray(expected.palette) ? expected.palette : [];
|
|
222
|
+
const expectedRegions = Array.isArray(expected.regions) ? expected.regions : [];
|
|
223
|
+
const expectedTypography = Array.isArray(expected.typographyHierarchy) ? expected.typographyHierarchy : [];
|
|
224
|
+
const hasPalette = expectedPalette.length > 0;
|
|
225
|
+
const hasRegions = expectedRegions.length > 0;
|
|
226
|
+
const hasTypography = expectedTypography.length > 0;
|
|
227
|
+
const needsPixels = Boolean(expected.nonBlank) || (hasPalette && !skipPalette) || hasRegions || runWcag;
|
|
228
|
+
if (!needsPixels) {
|
|
229
|
+
const refDims = readImageDimensions(fs.readFileSync(refPath));
|
|
230
|
+
const candDims = readImageDimensions(fs.readFileSync(candPath));
|
|
231
|
+
if (refDims && candDims && candDims.width > 0 && candDims.height > 0) {
|
|
232
|
+
const failures = checkDimensions(expected.dimensions, { width: candDims.width, height: candDims.height });
|
|
233
|
+
const metrics = {
|
|
234
|
+
referencePath: refPath,
|
|
235
|
+
candidatePath: candPath,
|
|
236
|
+
reference: { width: refDims.width, height: refDims.height },
|
|
237
|
+
candidate: { width: candDims.width, height: candDims.height },
|
|
238
|
+
...(failures.length ? { dimensionMismatch: true } : {})
|
|
239
|
+
};
|
|
240
|
+
return { status: failures.length ? "fail" : "pass", score: failures.length ? 0 : 1, failures, metrics };
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
// Let decode errors (notably ADAPTER_MISSING) propagate so the dispatcher can mark skipped.
|
|
244
|
+
const refImg = await decodeCached(refPath, ctx, deps);
|
|
245
|
+
const candImg = await decodeCached(candPath, ctx, deps);
|
|
246
|
+
const failures = [];
|
|
247
|
+
const diff = {};
|
|
248
|
+
const metrics = {
|
|
249
|
+
referencePath: refPath,
|
|
250
|
+
candidatePath: candPath,
|
|
251
|
+
reference: { width: refImg.width, height: refImg.height },
|
|
252
|
+
candidate: { width: candImg.width, height: candImg.height }
|
|
253
|
+
};
|
|
254
|
+
const dimFailures = checkDimensions(expected.dimensions, { width: candImg.width, height: candImg.height });
|
|
255
|
+
if (dimFailures.length) {
|
|
256
|
+
failures.push(...dimFailures);
|
|
257
|
+
metrics.dimensionMismatch = true;
|
|
258
|
+
}
|
|
259
|
+
if (expected.nonBlank === true) {
|
|
260
|
+
const data = candImg.data;
|
|
261
|
+
if (!data || data.length < 4) {
|
|
262
|
+
failures.push("candidate image is blank (empty pixel buffer)");
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
let uniform = true;
|
|
266
|
+
let allTransparent = data[3] === 0;
|
|
267
|
+
const r0 = data[0], g0 = data[1], b0 = data[2], a0 = data[3];
|
|
268
|
+
for (let i = 4; i < data.length; i += 4) {
|
|
269
|
+
if (data[i + 3] !== 0)
|
|
270
|
+
allTransparent = false;
|
|
271
|
+
if (data[i] !== r0 || data[i + 1] !== g0 || data[i + 2] !== b0 || data[i + 3] !== a0) {
|
|
272
|
+
uniform = false;
|
|
273
|
+
}
|
|
274
|
+
if (!uniform && !allTransparent)
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
if (allTransparent)
|
|
278
|
+
failures.push("candidate image is blank (fully transparent)");
|
|
279
|
+
else if (uniform)
|
|
280
|
+
failures.push("candidate image is blank (uniform color)");
|
|
281
|
+
}
|
|
282
|
+
metrics.nonBlank = !failures.some((f) => f.includes("blank"));
|
|
283
|
+
}
|
|
284
|
+
// Soft metrics: palette/regions/typography/WCAG
|
|
285
|
+
let paletteSimilarity = 1;
|
|
286
|
+
let regionIoU = 1;
|
|
287
|
+
if (hasPalette && !skipPalette) {
|
|
288
|
+
const candPalette = extractPalette(candImg, expectedPalette.length);
|
|
289
|
+
paletteSimilarity = comparePalettes(expectedPalette, candPalette);
|
|
290
|
+
metrics.paletteSimilarity = paletteSimilarity;
|
|
291
|
+
const minPalette = typeof tolerance.minPaletteSimilarity === "number" ? tolerance.minPaletteSimilarity : 0.8;
|
|
292
|
+
if (paletteSimilarity < minPalette)
|
|
293
|
+
failures.push(`paletteSimilarity ${paletteSimilarity.toFixed(3)} < ${minPalette}`);
|
|
294
|
+
}
|
|
295
|
+
if (hasRegions) {
|
|
296
|
+
const candRegions = extractLayoutRegions(candImg, { maxDepth: 3 });
|
|
297
|
+
const cmp = compareRegionsIoU(expectedRegions, candRegions);
|
|
298
|
+
regionIoU = cmp.meanIoU;
|
|
299
|
+
metrics.regionIoU = regionIoU;
|
|
300
|
+
metrics.matchedRegions = cmp.matched;
|
|
301
|
+
const minIoU = typeof tolerance.minLayoutIoU === "number" ? tolerance.minLayoutIoU : 0.75;
|
|
302
|
+
if (regionIoU < minIoU)
|
|
303
|
+
failures.push(`regionIoU ${regionIoU.toFixed(3)} < ${minIoU}`);
|
|
304
|
+
}
|
|
305
|
+
if (mode === "reskin" && hasTypography) {
|
|
306
|
+
const candTypography = estimateTypographyBands(candImg);
|
|
307
|
+
metrics.typographyHierarchy = candTypography;
|
|
308
|
+
if (candTypography.length === 0) {
|
|
309
|
+
failures.push("typography structure missing (expected typography bands, found 0)");
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
const wcag = runWcag
|
|
313
|
+
? applyWcagAudit(candImg, expected.wcagLevel ?? "AA", failures, metrics, diff)
|
|
314
|
+
: null;
|
|
315
|
+
const wcagScore = wcagAuditScore(wcag);
|
|
316
|
+
const hasDiff = Object.keys(diff).length > 0;
|
|
317
|
+
if (failures.length > 0) {
|
|
318
|
+
// Hard dimension mismatch should fail; soft palette/region/WCAG also fail when invoked as soft invariant under strict gate handled by scorer.
|
|
319
|
+
// Status here is pass/fail; scorer applies severity/gate.
|
|
320
|
+
return {
|
|
321
|
+
status: "fail",
|
|
322
|
+
score: invariant.severity === "hard" ? 0 : Math.min(paletteSimilarity, regionIoU, wcagScore),
|
|
323
|
+
failures,
|
|
324
|
+
metrics,
|
|
325
|
+
...(hasDiff ? { diff } : {})
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
const score = invariant.severity === "hard" ? 1 : Math.min(paletteSimilarity, regionIoU, wcagScore);
|
|
329
|
+
return { status: "pass", score, failures: [], metrics };
|
|
330
|
+
}
|
|
331
|
+
async function verifyPerceptual(invariant, ctx, deps) {
|
|
332
|
+
const { abs: refPath } = resolveReference(invariant, ctx);
|
|
333
|
+
const candPath = await resolveCandidatePath(invariant, ctx, deps);
|
|
334
|
+
assertValidPngHeaderIfPng(refPath);
|
|
335
|
+
assertValidPngHeaderIfPng(candPath);
|
|
336
|
+
const expected = invariant.expected || {};
|
|
337
|
+
const tolerance = invariant.tolerance || {};
|
|
338
|
+
const mode = ctx.mode ?? "exact";
|
|
339
|
+
if (mode === "reskin") {
|
|
340
|
+
const candImg = await decodeCached(candPath, ctx, deps);
|
|
341
|
+
const failures = [];
|
|
342
|
+
const diff = {};
|
|
343
|
+
const metrics = {
|
|
344
|
+
referencePath: refPath,
|
|
345
|
+
candidatePath: candPath,
|
|
346
|
+
candidate: { width: candImg.width, height: candImg.height },
|
|
347
|
+
mode: "reskin"
|
|
348
|
+
};
|
|
349
|
+
const dimFailures = checkDimensions(expected.dimensions, { width: candImg.width, height: candImg.height });
|
|
350
|
+
if (dimFailures.length > 0) {
|
|
351
|
+
failures.push(...dimFailures);
|
|
352
|
+
metrics.dimensionMismatch = true;
|
|
353
|
+
}
|
|
354
|
+
const wcag = applyWcagAudit(candImg, expected.wcagLevel ?? "AA", failures, metrics, diff);
|
|
355
|
+
const hasDiff = Object.keys(diff).length > 0;
|
|
356
|
+
const score = failures.length === 0
|
|
357
|
+
? 1
|
|
358
|
+
: dimFailures.length > 0 || invariant.severity === "hard"
|
|
359
|
+
? 0
|
|
360
|
+
: wcagAuditScore(wcag);
|
|
361
|
+
return {
|
|
362
|
+
status: failures.length > 0 ? "fail" : "pass",
|
|
363
|
+
score,
|
|
364
|
+
failures,
|
|
365
|
+
metrics,
|
|
366
|
+
...(hasDiff ? { diff } : {})
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
const refImg = await decodeCached(refPath, ctx, deps);
|
|
370
|
+
const candImg = await decodeCached(candPath, ctx, deps);
|
|
371
|
+
const minSSIM = typeof tolerance.minSSIM === "number" ? tolerance.minSSIM : 0.85;
|
|
372
|
+
const maxPixelDiffRatio = typeof tolerance.maxPixelDiffRatio === "number" ? tolerance.maxPixelDiffRatio : 0.1;
|
|
373
|
+
const colorDelta = typeof tolerance.colorDelta === "number" ? tolerance.colorDelta : 0.1;
|
|
374
|
+
const ssimRes = computeSSIM(refImg, candImg);
|
|
375
|
+
const diffRes = computePixelDiff(refImg, candImg, { colorDelta });
|
|
376
|
+
const refHash = computeDHash(refImg);
|
|
377
|
+
const candHash = computeDHash(candImg);
|
|
378
|
+
const hamming = hammingDistanceHex(refHash, candHash);
|
|
379
|
+
const failures = [];
|
|
380
|
+
if (ssimRes.ssim < minSSIM)
|
|
381
|
+
failures.push(`ssim ${ssimRes.ssim.toFixed(4)} < ${minSSIM}`);
|
|
382
|
+
if (diffRes.diffRatio > maxPixelDiffRatio)
|
|
383
|
+
failures.push(`diffRatio ${diffRes.diffRatio.toFixed(4)} > ${maxPixelDiffRatio}`);
|
|
384
|
+
const metrics = {
|
|
385
|
+
referencePath: refPath,
|
|
386
|
+
candidatePath: candPath,
|
|
387
|
+
ssim: ssimRes.ssim,
|
|
388
|
+
diffRatio: diffRes.diffRatio,
|
|
389
|
+
diffPixels: diffRes.diffPixels,
|
|
390
|
+
totalPixels: diffRes.totalPixels,
|
|
391
|
+
hammingDistance: hamming,
|
|
392
|
+
referenceHash: refHash,
|
|
393
|
+
candidateHash: candHash,
|
|
394
|
+
dimensionMismatch: ssimRes.dimensionMismatch || diffRes.dimensionMismatch
|
|
395
|
+
};
|
|
396
|
+
const diff = {};
|
|
397
|
+
const wcag = expected.wcagLevel !== undefined
|
|
398
|
+
? applyWcagAudit(candImg, expected.wcagLevel, failures, metrics, diff)
|
|
399
|
+
: null;
|
|
400
|
+
let diffArtifact = null;
|
|
401
|
+
if (failures.length > 0) {
|
|
402
|
+
try {
|
|
403
|
+
diffArtifact = writeDiffArtifact(diffRes.diffMask, invariant, ctx);
|
|
404
|
+
metrics.diffPng = diffArtifact;
|
|
405
|
+
diff.diffPng = diffArtifact;
|
|
406
|
+
}
|
|
407
|
+
catch {
|
|
408
|
+
// ignore diff write failures
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
// Continuous score for soft perceptual: blend ssim and pixel match
|
|
412
|
+
const baseScore = Math.max(0, Math.min(1, (ssimRes.ssim * 0.7) + ((1 - diffRes.diffRatio) * 0.3)));
|
|
413
|
+
const score = wcag && !wcag.passed ? Math.min(baseScore, wcagAuditScore(wcag)) : baseScore;
|
|
414
|
+
const hasDiff = Object.keys(diff).length > 0;
|
|
415
|
+
return {
|
|
416
|
+
status: failures.length > 0 ? "fail" : "pass",
|
|
417
|
+
score,
|
|
418
|
+
failures,
|
|
419
|
+
metrics,
|
|
420
|
+
...(hasDiff ? { diff } : {})
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
export async function verifyInvariant(invariant, ctx, deps = {}) {
|
|
424
|
+
if (invariant.type === "image_layout")
|
|
425
|
+
return verifyLayout(invariant, ctx, deps);
|
|
426
|
+
if (invariant.type === "image_perceptual")
|
|
427
|
+
return verifyPerceptual(invariant, ctx, deps);
|
|
428
|
+
throw new VibecodeError("UNSUPPORTED_FORMAT", `Unsupported image invariant type: ${invariant.type}`);
|
|
429
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ContractV02, Invariant, InvariantResult, OracleContext, OracleFn, ParityReport, VerifyDeps } from "../core/types.js";
|
|
2
|
+
export declare function getOracle(type: string): Promise<OracleFn | null>;
|
|
3
|
+
/** Deep, cycle-safe, depth-bounded runtime-var scrub of every string (values and keys) in any JSON-like value. */
|
|
4
|
+
export declare function redactRuntimeVarsDeep<T>(value: T, vars?: Record<string, string>): T;
|
|
5
|
+
export declare function verifyInvariant(invariant: Invariant, ctx: OracleContext, deps?: VerifyDeps): Promise<InvariantResult>;
|
|
6
|
+
export declare function verifyContract(contract: ContractV02, rawCtx: OracleContext, deps?: VerifyDeps): Promise<ParityReport>;
|