vibecode-anything 0.0.0-stage → 0.3.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.
Files changed (62) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +235 -2
  3. package/dist/cli.d.ts +2 -0
  4. package/dist/cli.js +452 -0
  5. package/dist/core/adapters.d.ts +8 -0
  6. package/dist/core/adapters.js +92 -0
  7. package/dist/core/diff.d.ts +10 -0
  8. package/dist/core/diff.js +207 -0
  9. package/dist/core/doctor.d.ts +50 -0
  10. package/dist/core/doctor.js +312 -0
  11. package/dist/core/errors.d.ts +32 -0
  12. package/dist/core/errors.js +81 -0
  13. package/dist/core/exec.d.ts +14 -0
  14. package/dist/core/exec.js +109 -0
  15. package/dist/core/parity.d.ts +9 -0
  16. package/dist/core/parity.js +138 -0
  17. package/dist/core/png.d.ts +7 -0
  18. package/dist/core/png.js +303 -0
  19. package/dist/core/schema.d.ts +17 -0
  20. package/dist/core/schema.js +614 -0
  21. package/dist/core/slices.d.ts +24 -0
  22. package/dist/core/slices.js +294 -0
  23. package/dist/core/store.d.ts +17 -0
  24. package/dist/core/store.js +97 -0
  25. package/dist/core/types.d.ts +751 -0
  26. package/dist/core/types.js +4 -0
  27. package/dist/core/visual.d.ts +34 -0
  28. package/dist/core/visual.js +459 -0
  29. package/dist/core/watch.d.ts +53 -0
  30. package/dist/core/watch.js +285 -0
  31. package/dist/ingestors/api-repo.d.ts +5 -0
  32. package/dist/ingestors/api-repo.js +722 -0
  33. package/dist/ingestors/bridges.d.ts +3 -0
  34. package/dist/ingestors/bridges.js +562 -0
  35. package/dist/ingestors/cli.d.ts +9 -0
  36. package/dist/ingestors/cli.js +266 -0
  37. package/dist/ingestors/image.d.ts +2 -0
  38. package/dist/ingestors/image.js +147 -0
  39. package/dist/ingestors/index.d.ts +5 -0
  40. package/dist/ingestors/index.js +97 -0
  41. package/dist/ingestors/video.d.ts +47 -0
  42. package/dist/ingestors/video.js +605 -0
  43. package/dist/ingestors/web.d.ts +25 -0
  44. package/dist/ingestors/web.js +397 -0
  45. package/dist/mcp/server.d.ts +2 -0
  46. package/dist/mcp/server.js +504 -0
  47. package/dist/oracles/api-repo.d.ts +2 -0
  48. package/dist/oracles/api-repo.js +214 -0
  49. package/dist/oracles/cli.d.ts +2 -0
  50. package/dist/oracles/cli.js +157 -0
  51. package/dist/oracles/image.d.ts +2 -0
  52. package/dist/oracles/image.js +279 -0
  53. package/dist/oracles/index.d.ts +4 -0
  54. package/dist/oracles/index.js +91 -0
  55. package/dist/oracles/video.d.ts +21 -0
  56. package/dist/oracles/video.js +411 -0
  57. package/dist/oracles/web.d.ts +2 -0
  58. package/dist/oracles/web.js +480 -0
  59. package/dist/setup/index.d.ts +7 -0
  60. package/dist/setup/index.js +186 -0
  61. package/package.json +47 -6
  62. package/skills/vibecode-anything/SKILL.md +52 -0
@@ -0,0 +1,614 @@
1
+ import { VibecodeError } from "./errors.js";
2
+ export const SCHEMA_VERSION = "0.2.0";
3
+ export const VALID_SLICE_STATUSES = new Set(["pending", "in_progress", "passed", "failed", "skipped"]);
4
+ const VALID_DOMAINS = new Set(["cli", "web", "image", "video", "api", "repo"]);
5
+ const VALID_SEVERITIES = new Set(["hard", "soft"]);
6
+ const VALID_TYPES = new Set([
7
+ "command",
8
+ "web_route",
9
+ "web_flow",
10
+ "web_network",
11
+ "image_layout",
12
+ "image_perceptual",
13
+ "video_timeline",
14
+ "http",
15
+ "repo_exports"
16
+ ]);
17
+ function isObject(v) {
18
+ return v !== null && typeof v === "object" && !Array.isArray(v);
19
+ }
20
+ function clone(v) {
21
+ return JSON.parse(JSON.stringify(v));
22
+ }
23
+ function defaultThresholds(d = {}) {
24
+ return {
25
+ minSSIM: typeof d.minSSIM === "number" ? d.minSSIM : 0.85,
26
+ maxPixelDiffRatio: typeof d.maxPixelDiffRatio === "number" ? d.maxPixelDiffRatio : 0.1,
27
+ colorDelta: typeof d.colorDelta === "number" ? d.colorDelta : 0.1,
28
+ minLayoutIoU: typeof d.minLayoutIoU === "number" ? d.minLayoutIoU : 0.75,
29
+ minPaletteSimilarity: typeof d.minPaletteSimilarity === "number" ? d.minPaletteSimilarity : 0.8,
30
+ stateMatchFloor: typeof d.stateMatchFloor === "number" ? d.stateMatchFloor : 0.7,
31
+ minMotionRegionIoU: typeof d.minMotionRegionIoU === "number" ? d.minMotionRegionIoU : 0.55
32
+ };
33
+ }
34
+ function normalizeSlices(rawSlices) {
35
+ return rawSlices.map((entry) => {
36
+ const s = isObject(entry) ? entry : {};
37
+ const slice = {
38
+ id: typeof s.id === "string" ? s.id : "",
39
+ title: typeof s.title === "string" ? s.title : "",
40
+ invariantIds: Array.isArray(s.invariantIds) ? s.invariantIds.map(String) : [],
41
+ status: typeof s.status === "string" && VALID_SLICE_STATUSES.has(s.status)
42
+ ? s.status
43
+ : "pending"
44
+ };
45
+ if (typeof s.description === "string")
46
+ slice.description = s.description;
47
+ if (Array.isArray(s.dependsOn))
48
+ slice.dependsOn = s.dependsOn.map(String);
49
+ if (typeof s.lastVerifiedAt === "string")
50
+ slice.lastVerifiedAt = s.lastVerifiedAt;
51
+ if (typeof s.lastScore === "number" && Number.isFinite(s.lastScore))
52
+ slice.lastScore = s.lastScore;
53
+ if (typeof s.notes === "string")
54
+ slice.notes = s.notes;
55
+ return slice;
56
+ });
57
+ }
58
+ function normalizeV01(raw) {
59
+ const rawTarget = isObject(raw.target) ? raw.target : {};
60
+ const rawDomain = typeof rawTarget.domain === "string" && rawTarget.domain ? rawTarget.domain : "cli";
61
+ const rawThresholds = isObject(raw.thresholds) ? raw.thresholds : {};
62
+ const contract = {
63
+ schemaVersion: SCHEMA_VERSION,
64
+ createdAt: new Date().toISOString(),
65
+ generator: {
66
+ name: "vibecode-anything",
67
+ version: "0.1.0-migrated",
68
+ adapter: "manual"
69
+ },
70
+ target: {
71
+ name: typeof rawTarget.name === "string" && rawTarget.name ? rawTarget.name : "unknown",
72
+ domains: [rawDomain],
73
+ sources: [
74
+ {
75
+ id: "src-1",
76
+ kind: rawDomain,
77
+ ref: typeof rawTarget.source === "string" && rawTarget.source ? rawTarget.source : "unknown",
78
+ meta: {}
79
+ }
80
+ ]
81
+ },
82
+ artifacts: { root: ".vibecode/artifacts", files: {} },
83
+ thresholds: {
84
+ hardBehavioralParity: 1.0,
85
+ softPerceptualParity: 0.85,
86
+ softGateMode: "warn",
87
+ defaults: defaultThresholds()
88
+ },
89
+ invariants: [],
90
+ ...(Array.isArray(raw.slices) ? { slices: normalizeSlices(raw.slices) } : {})
91
+ };
92
+ // v0.1 thresholds.behavioralParity maps to hardBehavioralParity
93
+ if (typeof rawThresholds.behavioralParity === "number") {
94
+ contract.thresholds.hardBehavioralParity = rawThresholds.behavioralParity;
95
+ }
96
+ if (typeof rawThresholds.visualSimilarity === "number") {
97
+ contract.thresholds.softPerceptualParity = rawThresholds.visualSimilarity;
98
+ }
99
+ const invs = Array.isArray(raw.invariants) ? raw.invariants : [];
100
+ contract.invariants = invs.map((item, i) => {
101
+ const inv = isObject(item) ? item : {};
102
+ const domain = (typeof inv.domain === "string" && inv.domain ? inv.domain : rawDomain);
103
+ const type = (typeof inv.type === "string" && inv.type
104
+ ? inv.type
105
+ : domain === "cli"
106
+ ? "command"
107
+ : domain === "api"
108
+ ? "http"
109
+ : "command");
110
+ const v2 = {
111
+ id: typeof inv.id === "string" && inv.id ? inv.id : `inv-${i + 1}`,
112
+ domain,
113
+ type,
114
+ severity: (typeof inv.severity === "string" && inv.severity ? inv.severity : "hard"),
115
+ description: typeof inv.description === "string" ? inv.description : "",
116
+ tags: Array.isArray(inv.tags) ? inv.tags.map(String) : [],
117
+ sourceId: typeof inv.sourceId === "string" && inv.sourceId ? inv.sourceId : "src-1",
118
+ input: isObject(inv.input) ? clone(inv.input) : {},
119
+ expected: isObject(inv.expected) ? clone(inv.expected) : {},
120
+ tolerance: isObject(inv.tolerance) ? clone(inv.tolerance) : {}
121
+ };
122
+ // v0.1 top-level args -> input.args
123
+ if (Array.isArray(inv.args) && !Array.isArray(v2.input.args)) {
124
+ v2.input.args = clone(inv.args);
125
+ }
126
+ return v2;
127
+ });
128
+ return contract;
129
+ }
130
+ export function normalizeContract(raw) {
131
+ if (!isObject(raw)) {
132
+ throw new VibecodeError("CONTRACT_INVALID", "Contract must be a JSON object");
133
+ }
134
+ // Detect v0.1
135
+ if (raw.version === "0.1.0" || (!raw.schemaVersion && raw.version && String(raw.version).startsWith("0.1"))) {
136
+ return normalizeV01(raw);
137
+ }
138
+ // v0.2+: normalize in-memory without mutating raw
139
+ const c = clone(raw);
140
+ const rawGen = isObject(c.generator) ? c.generator : {};
141
+ const generator = {
142
+ name: typeof rawGen.name === "string" && rawGen.name ? rawGen.name : "vibecode-anything",
143
+ version: typeof rawGen.version === "string" && rawGen.version ? rawGen.version : "0.2.0",
144
+ adapter: typeof rawGen.adapter === "string" && rawGen.adapter ? rawGen.adapter : "manual"
145
+ };
146
+ const rawTarget = isObject(c.target) ? c.target : {};
147
+ const targetName = typeof rawTarget.name === "string" && rawTarget.name ? rawTarget.name : "unknown";
148
+ let domains;
149
+ if (Array.isArray(rawTarget.domains)) {
150
+ domains = rawTarget.domains;
151
+ }
152
+ else if (typeof rawTarget.domain === "string") {
153
+ domains = [rawTarget.domain];
154
+ }
155
+ else {
156
+ domains = ["cli"];
157
+ }
158
+ let rawSources = Array.isArray(rawTarget.sources) ? rawTarget.sources : [];
159
+ // v0.1 compat: top-level source string
160
+ if (rawSources.length === 0 && typeof rawTarget.source === "string") {
161
+ rawSources = [{ id: "src-1", kind: domains[0] || "cli", ref: rawTarget.source, meta: {} }];
162
+ }
163
+ const sources = rawSources.map((entry, i) => {
164
+ const s = isObject(entry) ? entry : {};
165
+ return {
166
+ id: typeof s.id === "string" && s.id ? s.id : `src-${i + 1}`,
167
+ kind: typeof s.kind === "string" && s.kind ? s.kind : domains[0] || "cli",
168
+ ref: typeof s.ref === "string" ? s.ref : "",
169
+ meta: isObject(s.meta) ? s.meta : {}
170
+ };
171
+ });
172
+ const rawArtifacts = isObject(c.artifacts) ? c.artifacts : {};
173
+ const artifactsFiles = {};
174
+ if (isObject(rawArtifacts.files)) {
175
+ for (const [k, v] of Object.entries(rawArtifacts.files)) {
176
+ if (typeof v === "string")
177
+ artifactsFiles[k] = v;
178
+ }
179
+ }
180
+ const artifacts = {
181
+ root: typeof rawArtifacts.root === "string" && rawArtifacts.root ? rawArtifacts.root : ".vibecode/artifacts",
182
+ files: artifactsFiles
183
+ };
184
+ const t = isObject(c.thresholds) ? c.thresholds : {};
185
+ const d = isObject(t.defaults) ? t.defaults : {};
186
+ const thresholds = {
187
+ hardBehavioralParity: t.hardBehavioralParity === undefined ? 1.0 : t.hardBehavioralParity,
188
+ softPerceptualParity: t.softPerceptualParity === undefined ? 0.85 : t.softPerceptualParity,
189
+ softGateMode: (t.softGateMode === undefined ? "warn" : t.softGateMode),
190
+ defaults: defaultThresholds(d)
191
+ };
192
+ const defaultTypeByDomain = {
193
+ cli: "command",
194
+ web: "web_route",
195
+ image: "image_layout",
196
+ video: "video_timeline",
197
+ api: "http",
198
+ repo: "repo_exports"
199
+ };
200
+ const rawInvs = Array.isArray(c.invariants) ? c.invariants : [];
201
+ const invariants = rawInvs.map((inv, i) => {
202
+ const n = isObject(inv) ? clone(inv) : {};
203
+ const domain = (typeof n.domain === "string" && n.domain ? n.domain : domains[0] || "cli");
204
+ const type = (typeof n.type === "string" && n.type ? n.type : defaultTypeByDomain[domain] || "command");
205
+ const input = isObject(n.input) ? n.input : {};
206
+ // v0.1 compat: top-level args
207
+ if (Array.isArray(n.args) && !Array.isArray(input.args)) {
208
+ input.args = clone(n.args);
209
+ }
210
+ const out = {
211
+ id: typeof n.id === "string" && n.id ? n.id : `inv-${i + 1}`,
212
+ domain,
213
+ type,
214
+ severity: (typeof n.severity === "string" && n.severity ? n.severity : "hard"),
215
+ description: typeof n.description === "string" ? n.description : "",
216
+ tags: Array.isArray(n.tags) ? n.tags.map(String) : [],
217
+ ...(typeof n.sourceId === "string" && n.sourceId ? { sourceId: n.sourceId } : {}),
218
+ input,
219
+ expected: isObject(n.expected) ? n.expected : {},
220
+ tolerance: isObject(n.tolerance) ? n.tolerance : {}
221
+ };
222
+ return out;
223
+ });
224
+ return {
225
+ schemaVersion: typeof c.schemaVersion === "string" && c.schemaVersion ? c.schemaVersion : SCHEMA_VERSION,
226
+ createdAt: typeof c.createdAt === "string" && c.createdAt ? c.createdAt : new Date().toISOString(),
227
+ generator,
228
+ target: {
229
+ name: targetName,
230
+ domains,
231
+ sources
232
+ },
233
+ artifacts,
234
+ thresholds,
235
+ invariants,
236
+ ...(Array.isArray(c.slices) ? { slices: normalizeSlices(c.slices) } : {})
237
+ };
238
+ }
239
+ export function validateContract(contract) {
240
+ const errors = [];
241
+ if (!isObject(contract)) {
242
+ return { ok: false, errors: [{ path: "$", message: "Contract must be an object" }] };
243
+ }
244
+ if (typeof contract.schemaVersion !== "string") {
245
+ errors.push({ path: "$.schemaVersion", message: "schemaVersion must be a string" });
246
+ }
247
+ if (!isObject(contract.target)) {
248
+ errors.push({ path: "$.target", message: "target must be an object" });
249
+ }
250
+ else {
251
+ if (typeof contract.target.name !== "string")
252
+ errors.push({ path: "$.target.name", message: "target.name must be a string" });
253
+ if (!Array.isArray(contract.target.domains) || contract.target.domains.length === 0) {
254
+ errors.push({ path: "$.target.domains", message: "target.domains must be a non-empty array" });
255
+ }
256
+ else {
257
+ contract.target.domains.forEach((d, i) => {
258
+ if (typeof d !== "string" || !VALID_DOMAINS.has(d)) {
259
+ errors.push({ path: `$.target.domains[${i}]`, message: `Invalid domain: ${String(d)}` });
260
+ }
261
+ });
262
+ }
263
+ }
264
+ if (!isObject(contract.thresholds)) {
265
+ errors.push({ path: "$.thresholds", message: "thresholds must be an object" });
266
+ }
267
+ else {
268
+ for (const k of ["hardBehavioralParity", "softPerceptualParity"]) {
269
+ const v = contract.thresholds[k];
270
+ if (typeof v !== "number" || !Number.isFinite(v) || v < 0 || v > 1) {
271
+ errors.push({ path: `$.thresholds.${k}`, message: `${k} must be a number in [0,1]` });
272
+ }
273
+ }
274
+ const mode = contract.thresholds.softGateMode;
275
+ if (mode !== undefined && mode !== null && (typeof mode !== "string" || !["warn", "fail"].includes(mode))) {
276
+ errors.push({ path: "$.thresholds.softGateMode", message: "softGateMode must be 'warn' or 'fail'" });
277
+ }
278
+ }
279
+ if (!Array.isArray(contract.invariants)) {
280
+ errors.push({ path: "$.invariants", message: "invariants must be an array" });
281
+ }
282
+ else {
283
+ const seen = new Set();
284
+ contract.invariants.forEach((inv, i) => {
285
+ const p = `$.invariants[${i}]`;
286
+ if (!isObject(inv)) {
287
+ errors.push({ path: p, message: "invariant must be an object" });
288
+ return;
289
+ }
290
+ if (typeof inv.id !== "string" || !inv.id) {
291
+ errors.push({ path: `${p}.id`, message: "id must be a non-empty string" });
292
+ }
293
+ else if (seen.has(inv.id)) {
294
+ errors.push({ path: `${p}.id`, message: `Duplicate invariant id: ${inv.id}` });
295
+ }
296
+ else {
297
+ seen.add(inv.id);
298
+ }
299
+ if (typeof inv.domain !== "string" || !VALID_DOMAINS.has(inv.domain)) {
300
+ errors.push({ path: `${p}.domain`, message: `Invalid domain: ${String(inv.domain)}` });
301
+ }
302
+ if (typeof inv.type !== "string" || !VALID_TYPES.has(inv.type)) {
303
+ errors.push({ path: `${p}.type`, message: `Invalid type: ${String(inv.type)}` });
304
+ }
305
+ if (typeof inv.severity !== "string" || !VALID_SEVERITIES.has(inv.severity)) {
306
+ errors.push({ path: `${p}.severity`, message: `Invalid severity: ${String(inv.severity)}` });
307
+ }
308
+ if (!isObject(inv.input))
309
+ errors.push({ path: `${p}.input`, message: "input must be an object" });
310
+ if (!isObject(inv.expected))
311
+ errors.push({ path: `${p}.expected`, message: "expected must be an object" });
312
+ });
313
+ }
314
+ if (contract.slices !== undefined) {
315
+ if (!Array.isArray(contract.slices)) {
316
+ errors.push({ path: "$.slices", message: "slices must be an array" });
317
+ }
318
+ else {
319
+ const slices = contract.slices;
320
+ const knownInvariantIds = new Set();
321
+ if (Array.isArray(contract.invariants)) {
322
+ for (const inv of contract.invariants) {
323
+ if (isObject(inv) && typeof inv.id === "string" && inv.id)
324
+ knownInvariantIds.add(inv.id);
325
+ }
326
+ }
327
+ const seenSliceIds = new Set();
328
+ const sliceIndexById = new Map();
329
+ const dependencyLists = [];
330
+ slices.forEach((entry, i) => {
331
+ const p = `$.slices[${i}]`;
332
+ const dependencies = [];
333
+ dependencyLists.push(dependencies);
334
+ if (!isObject(entry)) {
335
+ errors.push({ path: p, message: "slice must be an object" });
336
+ return;
337
+ }
338
+ if (typeof entry.id !== "string" || !entry.id) {
339
+ errors.push({ path: `${p}.id`, message: "id must be a non-empty string" });
340
+ }
341
+ else if (seenSliceIds.has(entry.id)) {
342
+ errors.push({ path: `${p}.id`, message: `Duplicate slice id: ${entry.id}` });
343
+ }
344
+ else {
345
+ seenSliceIds.add(entry.id);
346
+ sliceIndexById.set(entry.id, i);
347
+ }
348
+ if (typeof entry.title !== "string" || !entry.title) {
349
+ errors.push({ path: `${p}.title`, message: "title must be a non-empty string" });
350
+ }
351
+ if (!Array.isArray(entry.invariantIds)) {
352
+ errors.push({ path: `${p}.invariantIds`, message: "invariantIds must be an array of strings" });
353
+ }
354
+ else {
355
+ entry.invariantIds.forEach((id, j) => {
356
+ if (typeof id !== "string" || !id) {
357
+ errors.push({ path: `${p}.invariantIds[${j}]`, message: "invariant id must be a non-empty string" });
358
+ return;
359
+ }
360
+ if (Array.isArray(contract.invariants) && !knownInvariantIds.has(id)) {
361
+ errors.push({ path: `${p}.invariantIds[${j}]`, message: `Unknown invariant id: ${id}` });
362
+ }
363
+ });
364
+ }
365
+ if (entry.status === undefined) {
366
+ // Missing status is allowed; normalization supplies "pending".
367
+ }
368
+ else if (typeof entry.status !== "string" || !VALID_SLICE_STATUSES.has(entry.status)) {
369
+ errors.push({ path: `${p}.status`, message: `Invalid slice status: ${String(entry.status)}` });
370
+ }
371
+ if (entry.lastScore !== undefined) {
372
+ const score = entry.lastScore;
373
+ if (typeof score !== "number" || !Number.isFinite(score) || score < 0 || score > 1) {
374
+ errors.push({ path: `${p}.lastScore`, message: "lastScore must be a finite number in [0,1]" });
375
+ }
376
+ }
377
+ if (entry.dependsOn !== undefined) {
378
+ if (!Array.isArray(entry.dependsOn)) {
379
+ errors.push({ path: `${p}.dependsOn`, message: "dependsOn must be an array of strings" });
380
+ }
381
+ else {
382
+ entry.dependsOn.forEach((dep, j) => {
383
+ if (typeof dep !== "string" || !dep) {
384
+ errors.push({ path: `${p}.dependsOn[${j}]`, message: "dependency id must be a non-empty string" });
385
+ return;
386
+ }
387
+ dependencies.push(dep);
388
+ });
389
+ }
390
+ }
391
+ });
392
+ dependencyLists.forEach((dependencies, i) => {
393
+ const entry = slices[i];
394
+ if (!isObject(entry) || !Array.isArray(entry.dependsOn))
395
+ return;
396
+ for (const dep of dependencies) {
397
+ if (!sliceIndexById.has(dep)) {
398
+ errors.push({ path: `$.slices[${i}].dependsOn`, message: `Unknown slice dependency: ${dep}` });
399
+ }
400
+ }
401
+ });
402
+ const visiting = new Map();
403
+ const stack = [];
404
+ const reportedCycles = new Set();
405
+ const visit = (id) => {
406
+ visiting.set(id, 1);
407
+ stack.push(id);
408
+ const index = sliceIndexById.get(id);
409
+ for (const dep of index === undefined ? [] : (dependencyLists[index] || [])) {
410
+ if (!sliceIndexById.has(dep))
411
+ continue;
412
+ const state = visiting.get(dep) ?? 0;
413
+ if (state === 0) {
414
+ visit(dep);
415
+ }
416
+ else if (state === 1) {
417
+ const start = stack.indexOf(dep);
418
+ const cycle = start >= 0 ? [...stack.slice(start), dep] : [dep, dep];
419
+ const key = [...new Set(cycle)].sort().join("|");
420
+ if (!reportedCycles.has(key)) {
421
+ reportedCycles.add(key);
422
+ const at = sliceIndexById.get(id) ?? 0;
423
+ errors.push({
424
+ path: `$.slices[${at}].dependsOn`,
425
+ message: `Dependency cycle detected: ${cycle.join(" -> ")}`
426
+ });
427
+ }
428
+ }
429
+ }
430
+ stack.pop();
431
+ visiting.set(id, 2);
432
+ };
433
+ for (const id of sliceIndexById.keys()) {
434
+ if ((visiting.get(id) ?? 0) === 0)
435
+ visit(id);
436
+ }
437
+ }
438
+ }
439
+ return { ok: errors.length === 0, errors };
440
+ }
441
+ export function assertValidContract(contract) {
442
+ const res = validateContract(contract);
443
+ if (!res.ok) {
444
+ throw new VibecodeError("CONTRACT_INVALID", `Contract invalid: ${res.errors[0].path} ${res.errors[0].message}`, { details: res.errors });
445
+ }
446
+ return contract;
447
+ }
448
+ export function validateJsonSchema(value, schema, path = "$") {
449
+ const errors = [];
450
+ if (!isObject(schema))
451
+ return errors;
452
+ const s = schema;
453
+ const p = path;
454
+ if (s.const !== undefined) {
455
+ if (JSON.stringify(value) !== JSON.stringify(s.const)) {
456
+ errors.push({ path: p, message: `Expected const ${JSON.stringify(s.const)}` });
457
+ }
458
+ return errors;
459
+ }
460
+ if (Array.isArray(s.enum)) {
461
+ const ok = s.enum.some((e) => JSON.stringify(e) === JSON.stringify(value));
462
+ if (!ok)
463
+ errors.push({ path: p, message: `Expected one of ${JSON.stringify(s.enum)}` });
464
+ }
465
+ if (s.type !== undefined) {
466
+ const types = Array.isArray(s.type) ? s.type : [s.type];
467
+ const matches = types.some((t) => typeMatches(value, t));
468
+ if (!matches) {
469
+ errors.push({ path: p, message: `Expected type ${types.join("|")}, got ${jsType(value)}` });
470
+ return errors; // further checks likely meaningless
471
+ }
472
+ }
473
+ if (typeof value === "string") {
474
+ if (typeof s.minLength === "number" && value.length < s.minLength)
475
+ errors.push({ path: p, message: `minLength ${s.minLength}` });
476
+ if (typeof s.maxLength === "number" && value.length > s.maxLength)
477
+ errors.push({ path: p, message: `maxLength ${s.maxLength}` });
478
+ if (typeof s.pattern === "string") {
479
+ try {
480
+ if (!new RegExp(s.pattern).test(value))
481
+ errors.push({ path: p, message: `pattern ${s.pattern}` });
482
+ }
483
+ catch {
484
+ errors.push({ path: p, message: `invalid pattern ${s.pattern}` });
485
+ }
486
+ }
487
+ }
488
+ if (typeof value === "number") {
489
+ if (typeof s.minimum === "number" && value < s.minimum)
490
+ errors.push({ path: p, message: `minimum ${s.minimum}` });
491
+ if (typeof s.maximum === "number" && value > s.maximum)
492
+ errors.push({ path: p, message: `maximum ${s.maximum}` });
493
+ }
494
+ if (Array.isArray(value)) {
495
+ if (typeof s.minItems === "number" && value.length < s.minItems)
496
+ errors.push({ path: p, message: `minItems ${s.minItems}` });
497
+ if (isObject(s.items)) {
498
+ const itemSchema = s.items;
499
+ value.forEach((item, i) => {
500
+ errors.push(...validateJsonSchema(item, itemSchema, `${p}[${i}]`));
501
+ });
502
+ }
503
+ }
504
+ if (isObject(value)) {
505
+ if (Array.isArray(s.required)) {
506
+ for (const k of s.required) {
507
+ if (!(k in value))
508
+ errors.push({ path: `${p}.${k}`, message: "required" });
509
+ }
510
+ }
511
+ if (isObject(s.properties)) {
512
+ for (const [k, subSchema] of Object.entries(s.properties)) {
513
+ if (k in value)
514
+ errors.push(...validateJsonSchema(value[k], subSchema, `${p}.${k}`));
515
+ }
516
+ }
517
+ }
518
+ return errors;
519
+ }
520
+ function jsType(v) {
521
+ if (v === null)
522
+ return "null";
523
+ if (Array.isArray(v))
524
+ return "array";
525
+ return typeof v;
526
+ }
527
+ function typeMatches(v, t) {
528
+ switch (t) {
529
+ case "null": return v === null;
530
+ case "array": return Array.isArray(v);
531
+ case "object": return isObject(v);
532
+ case "integer": return typeof v === "number" && Number.isInteger(v);
533
+ case "number": return typeof v === "number" && Number.isFinite(v);
534
+ case "string": return typeof v === "string";
535
+ case "boolean": return typeof v === "boolean";
536
+ default: return false;
537
+ }
538
+ }
539
+ export function queryJsonPath(obj, expr) {
540
+ if (typeof expr !== "string" || !expr.startsWith("$"))
541
+ return { found: false, value: undefined };
542
+ let cur = obj;
543
+ // Tokenize: .key, ["key"], [0]
544
+ const tokens = [];
545
+ const re = /\.([A-Za-z_$][A-Za-z0-9_$-]*)|\[(\d+)\]|\[(?:"([^"]*)"|'([^']*)')\]/g;
546
+ let i = 1; // skip $
547
+ while (i < expr.length) {
548
+ re.lastIndex = i;
549
+ const m = re.exec(expr);
550
+ if (!m || m.index !== i) {
551
+ // fallback: unsupported path syntax
552
+ return { found: false, value: undefined };
553
+ }
554
+ if (m[1] !== undefined)
555
+ tokens.push(m[1]);
556
+ else if (m[2] !== undefined)
557
+ tokens.push(Number(m[2]));
558
+ else if (m[3] !== undefined)
559
+ tokens.push(m[3]);
560
+ else if (m[4] !== undefined)
561
+ tokens.push(m[4]);
562
+ i = re.lastIndex;
563
+ }
564
+ for (const t of tokens) {
565
+ if (cur === null || cur === undefined)
566
+ return { found: false, value: undefined };
567
+ if (typeof t === "number") {
568
+ if (!Array.isArray(cur) || t < 0 || t >= cur.length)
569
+ return { found: false, value: undefined };
570
+ cur = cur[t];
571
+ }
572
+ else {
573
+ if (typeof cur !== "object" || !(t in cur))
574
+ return { found: false, value: undefined };
575
+ cur = cur[t];
576
+ }
577
+ }
578
+ return { found: true, value: cur };
579
+ }
580
+ export function makeDefaultContract({ name, domain = "cli", source = "unknown" } = {}) {
581
+ const dom = (VALID_DOMAINS.has(domain) ? domain : "cli");
582
+ return {
583
+ schemaVersion: SCHEMA_VERSION,
584
+ createdAt: new Date().toISOString(),
585
+ generator: { name: "vibecode-anything", version: "0.2.0", adapter: dom },
586
+ target: {
587
+ name: name || "unknown",
588
+ domains: [dom],
589
+ sources: [{ id: "src-1", kind: dom, ref: source, meta: {} }]
590
+ },
591
+ artifacts: { root: ".vibecode/artifacts", files: {} },
592
+ thresholds: {
593
+ hardBehavioralParity: 1.0,
594
+ softPerceptualParity: 0.85,
595
+ softGateMode: "warn",
596
+ defaults: defaultThresholds()
597
+ },
598
+ invariants: [
599
+ {
600
+ id: "inv-help",
601
+ domain: "cli",
602
+ type: "command",
603
+ severity: "hard",
604
+ description: "Target responds cleanly to --help",
605
+ tags: [],
606
+ sourceId: "src-1",
607
+ input: { args: ["--help"] },
608
+ expected: { exitCode: 0, stdoutContains: [] },
609
+ tolerance: {}
610
+ }
611
+ ]
612
+ };
613
+ }
614
+ export { VALID_DOMAINS, VALID_SEVERITIES, VALID_TYPES };
@@ -0,0 +1,24 @@
1
+ import type { ContractSlice, ContractV02, ParityReport, SliceStatus, SliceSummary } from "./types.js";
2
+ /** Build deterministic slices from the invariant domains/types in a contract. */
3
+ export declare function deriveDefaultSlices(contract: ContractV02): ContractSlice[];
4
+ /** Return persisted slices when present, otherwise derived defaults. */
5
+ export declare function getContractSlices(contract: ContractV02): ContractSlice[];
6
+ /** Resolve slice IDs (single, comma-separated, or array) to slices and invariant IDs. */
7
+ export declare function resolveSliceInvariants(contract: ContractV02, sliceIds: string | string[]): {
8
+ slices: ContractSlice[];
9
+ invariantIds: string[];
10
+ };
11
+ /**
12
+ * Apply a parity report to slice progress. Untargeted slices are only touched when
13
+ * one of their invariants was actually evaluated in the report.
14
+ */
15
+ export declare function updateSliceProgress(contract: ContractV02, report: ParityReport, options?: {
16
+ sliceIds?: string[];
17
+ timestamp?: string;
18
+ }): ContractV02;
19
+ /** Summarize slice progress and identify the next actionable slice. */
20
+ export declare function summarizeSlices(contract: ContractV02): SliceSummary;
21
+ /** Insert or replace a slice, validating references and dependencies. */
22
+ export declare function addOrUpdateSlice(contract: ContractV02, slice: Omit<ContractSlice, "status"> & {
23
+ status?: SliceStatus;
24
+ }): ContractV02;