toolsplice 0.0.0-stage → 0.0.1
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 +372 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +923 -0
- package/dist/codegen.d.ts +44 -0
- package/dist/codegen.js +479 -0
- package/dist/contract-caller.d.ts +22 -0
- package/dist/contract-caller.js +307 -0
- package/dist/contract-corrections.d.ts +35 -0
- package/dist/contract-corrections.js +159 -0
- package/dist/contract-schema.d.ts +5 -0
- package/dist/contract-schema.js +126 -0
- package/dist/contracts.d.ts +71 -0
- package/dist/contracts.js +366 -0
- package/dist/core.d.ts +14 -0
- package/dist/core.js +16 -0
- package/dist/doctor.d.ts +22 -0
- package/dist/doctor.js +91 -0
- package/dist/drift.d.ts +23 -0
- package/dist/drift.js +111 -0
- package/dist/execution.d.ts +38 -0
- package/dist/execution.js +66 -0
- package/dist/extract.d.ts +34 -0
- package/dist/extract.js +73 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +19 -0
- package/dist/ordered-transport.d.ts +12 -0
- package/dist/ordered-transport.js +106 -0
- package/dist/pattern.d.ts +2 -0
- package/dist/pattern.js +32 -0
- package/dist/postgres.d.ts +41 -0
- package/dist/postgres.js +264 -0
- package/dist/probe.d.ts +29 -0
- package/dist/probe.js +134 -0
- package/dist/proxy.d.ts +40 -0
- package/dist/proxy.js +436 -0
- package/dist/runtime.d.ts +6 -0
- package/dist/runtime.js +26 -0
- package/dist/schema.d.ts +17 -0
- package/dist/schema.js +299 -0
- package/dist/shape.d.ts +91 -0
- package/dist/shape.js +499 -0
- package/dist/splice-diagnostics.d.ts +40 -0
- package/dist/splice-diagnostics.js +80 -0
- package/dist/splice-project.d.ts +119 -0
- package/dist/splice-project.js +296 -0
- package/dist/splice-recording.d.ts +105 -0
- package/dist/splice-recording.js +464 -0
- package/dist/splice.d.ts +84 -0
- package/dist/splice.js +1065 -0
- package/dist/storage.d.ts +17 -0
- package/dist/storage.js +1 -0
- package/dist/store.d.ts +151 -0
- package/dist/store.js +735 -0
- package/dist/v2.d.ts +49 -0
- package/dist/v2.js +396 -0
- package/dist/validate-output.d.ts +3 -0
- package/dist/validate-output.js +73 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +1 -0
- package/dist/workflow-json.d.ts +8 -0
- package/dist/workflow-json.js +74 -0
- package/docs/PROJECTS-AND-UPGRADES.md +115 -0
- package/docs/RECORDING-AND-CORRECTIONS.md +176 -0
- package/package.json +95 -4
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
// Opt-in guards for reviewed tool contracts. The default proxy/bindings stay pass-through.
|
|
2
|
+
import { Ajv } from "ajv";
|
|
3
|
+
import { RE2JS } from "re2js";
|
|
4
|
+
import { compilePattern } from "./pattern.js";
|
|
5
|
+
import { Ajv2020 } from "ajv/dist/2020.js";
|
|
6
|
+
import { assertContractJson } from "./contract-schema.js";
|
|
7
|
+
import { parseContractSnapshot, } from "./contracts.js";
|
|
8
|
+
import { decode } from "./execution.js";
|
|
9
|
+
export class ContractValidationError extends Error {
|
|
10
|
+
tool;
|
|
11
|
+
stage;
|
|
12
|
+
issues;
|
|
13
|
+
constructor(tool, stage, issues) {
|
|
14
|
+
super(`Contract ${stage} check failed for ${JSON.stringify(tool)} at ${issues.map((i) => `${JSON.stringify(i.path)} (${i.keyword})`).join(", ")}.`);
|
|
15
|
+
this.tool = tool;
|
|
16
|
+
this.stage = stage;
|
|
17
|
+
this.issues = issues;
|
|
18
|
+
this.name = "ContractValidationError";
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const SAFE = new Set([
|
|
22
|
+
"type",
|
|
23
|
+
"pattern",
|
|
24
|
+
"propertyNames",
|
|
25
|
+
"properties",
|
|
26
|
+
"required",
|
|
27
|
+
"items",
|
|
28
|
+
"prefixItems",
|
|
29
|
+
"additionalProperties",
|
|
30
|
+
"anyOf",
|
|
31
|
+
"oneOf",
|
|
32
|
+
"allOf",
|
|
33
|
+
"not",
|
|
34
|
+
"$ref",
|
|
35
|
+
"$defs",
|
|
36
|
+
"definitions",
|
|
37
|
+
"enum",
|
|
38
|
+
"const",
|
|
39
|
+
"minItems",
|
|
40
|
+
"maxItems",
|
|
41
|
+
"uniqueItems",
|
|
42
|
+
"minProperties",
|
|
43
|
+
"maxProperties",
|
|
44
|
+
"minimum",
|
|
45
|
+
"maximum",
|
|
46
|
+
"exclusiveMinimum",
|
|
47
|
+
"exclusiveMaximum",
|
|
48
|
+
"multipleOf",
|
|
49
|
+
"minLength",
|
|
50
|
+
"maxLength",
|
|
51
|
+
"contains",
|
|
52
|
+
"additionalItems",
|
|
53
|
+
"minContains",
|
|
54
|
+
"maxContains",
|
|
55
|
+
"dependentRequired",
|
|
56
|
+
"dependentSchemas",
|
|
57
|
+
"if",
|
|
58
|
+
"then",
|
|
59
|
+
"else",
|
|
60
|
+
"unevaluatedProperties",
|
|
61
|
+
"unevaluatedItems",
|
|
62
|
+
]);
|
|
63
|
+
const IGNORE = new Set([
|
|
64
|
+
"$schema",
|
|
65
|
+
"$id",
|
|
66
|
+
"title",
|
|
67
|
+
"description",
|
|
68
|
+
"$comment",
|
|
69
|
+
"examples",
|
|
70
|
+
"default",
|
|
71
|
+
"deprecated",
|
|
72
|
+
"readOnly",
|
|
73
|
+
"writeOnly",
|
|
74
|
+
"format",
|
|
75
|
+
"contentMediaType",
|
|
76
|
+
"contentEncoding",
|
|
77
|
+
"contentSchema",
|
|
78
|
+
]);
|
|
79
|
+
const MAPS = new Set([
|
|
80
|
+
"properties",
|
|
81
|
+
"$defs",
|
|
82
|
+
"definitions",
|
|
83
|
+
"dependentSchemas",
|
|
84
|
+
]);
|
|
85
|
+
const ARRAYS = new Set(["anyOf", "oneOf", "allOf", "prefixItems"]);
|
|
86
|
+
const VALUES = new Set([
|
|
87
|
+
"propertyNames",
|
|
88
|
+
"items",
|
|
89
|
+
"additionalItems",
|
|
90
|
+
"additionalProperties",
|
|
91
|
+
"not",
|
|
92
|
+
"contains",
|
|
93
|
+
"if",
|
|
94
|
+
"then",
|
|
95
|
+
"else",
|
|
96
|
+
"unevaluatedProperties",
|
|
97
|
+
"unevaluatedItems",
|
|
98
|
+
]);
|
|
99
|
+
const isObject = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
100
|
+
const pointer = (v) => v.replace(/~/g, "~0").replace(/\//g, "~1");
|
|
101
|
+
/** No remote refs, backtracking regex evaluation, coercion, defaults, property removal, or schema downloads.
|
|
102
|
+
* format/content* are annotations here. Unsupported assertion keywords fail at setup. */
|
|
103
|
+
const DRAFT7 = new Set([
|
|
104
|
+
"http://json-schema.org/draft-07/schema",
|
|
105
|
+
"http://json-schema.org/draft-07/schema#",
|
|
106
|
+
"https://json-schema.org/draft-07/schema",
|
|
107
|
+
"https://json-schema.org/draft-07/schema#",
|
|
108
|
+
]);
|
|
109
|
+
const DRAFT2020 = new Set([
|
|
110
|
+
"https://json-schema.org/draft/2020-12/schema",
|
|
111
|
+
"https://json-schema.org/draft/2020-12/schema#",
|
|
112
|
+
]);
|
|
113
|
+
const MODERN = new Set([
|
|
114
|
+
"prefixItems",
|
|
115
|
+
"minContains",
|
|
116
|
+
"maxContains",
|
|
117
|
+
"dependentRequired",
|
|
118
|
+
"dependentSchemas",
|
|
119
|
+
"unevaluatedProperties",
|
|
120
|
+
"unevaluatedItems",
|
|
121
|
+
]);
|
|
122
|
+
function guardedSchema(schema, tool, path = "", draft7 = false, budget = { patterns: 0 }) {
|
|
123
|
+
if (typeof schema === "boolean")
|
|
124
|
+
return schema;
|
|
125
|
+
if (!isObject(schema))
|
|
126
|
+
throw new ContractValidationError(tool, "contract", [
|
|
127
|
+
{ path: path || "/", keyword: "invalid-schema" },
|
|
128
|
+
]);
|
|
129
|
+
const root = ["/input", "/output"].includes(path);
|
|
130
|
+
if ((schema.$schema !== undefined &&
|
|
131
|
+
(!root || !(draft7 ? DRAFT7 : DRAFT2020).has(String(schema.$schema)))) ||
|
|
132
|
+
(schema.$id !== undefined && !root))
|
|
133
|
+
throw new ContractValidationError(tool, "contract", [
|
|
134
|
+
{ path: path || "/", keyword: "unsupported-dialect-or-resource" },
|
|
135
|
+
]);
|
|
136
|
+
// Ajv's default $ref-sibling behavior is modern. Refuse the ambiguous Draft 7 case
|
|
137
|
+
// rather than silently enforce the wrong dialect's semantics.
|
|
138
|
+
if (draft7 &&
|
|
139
|
+
schema.$ref !== undefined &&
|
|
140
|
+
Object.keys(schema).some((k) => !IGNORE.has(k) &&
|
|
141
|
+
!k.startsWith("x-") &&
|
|
142
|
+
!["$ref", "$defs", "definitions"].includes(k)))
|
|
143
|
+
throw new ContractValidationError(tool, "contract", [
|
|
144
|
+
{ path: path || "/", keyword: "unsupported-draft7-ref-siblings" },
|
|
145
|
+
]);
|
|
146
|
+
if (schema.uniqueItems === true &&
|
|
147
|
+
!(typeof schema.maxItems === "number" &&
|
|
148
|
+
Number.isSafeInteger(schema.maxItems) &&
|
|
149
|
+
schema.maxItems >= 0 &&
|
|
150
|
+
schema.maxItems <= 1000))
|
|
151
|
+
throw new ContractValidationError(tool, "contract", [
|
|
152
|
+
{ path: `${path}/uniqueItems`, keyword: "requires-bounded-maxItems" },
|
|
153
|
+
]);
|
|
154
|
+
if (schema.pattern !== undefined) {
|
|
155
|
+
if (typeof schema.pattern !== "string" ||
|
|
156
|
+
schema.pattern.length > 1024 ||
|
|
157
|
+
++budget.patterns > 64)
|
|
158
|
+
throw new ContractValidationError(tool, "contract", [
|
|
159
|
+
{ path: `${path}/pattern`, keyword: "pattern-limits" },
|
|
160
|
+
]);
|
|
161
|
+
try {
|
|
162
|
+
compilePattern(schema.pattern);
|
|
163
|
+
}
|
|
164
|
+
catch {
|
|
165
|
+
throw new ContractValidationError(tool, "contract", [
|
|
166
|
+
{ path: `${path}/pattern`, keyword: "unsupported-pattern" },
|
|
167
|
+
]);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return Object.fromEntries(Object.entries(schema)
|
|
171
|
+
.filter(([key]) => !IGNORE.has(key) && !key.startsWith("x-"))
|
|
172
|
+
.map(([key, value]) => {
|
|
173
|
+
const p = `${path}/${pointer(key)}`;
|
|
174
|
+
if (!SAFE.has(key) ||
|
|
175
|
+
(draft7 && MODERN.has(key)) ||
|
|
176
|
+
(!draft7 && key === "additionalItems") ||
|
|
177
|
+
(key === "$ref" &&
|
|
178
|
+
(typeof value !== "string" ||
|
|
179
|
+
(value !== "#" && !value.startsWith("#/")))))
|
|
180
|
+
throw new ContractValidationError(tool, "contract", [
|
|
181
|
+
{ path: p, keyword: "unsupported-schema" },
|
|
182
|
+
]);
|
|
183
|
+
if ((MAPS.has(key) || key === "dependentRequired") &&
|
|
184
|
+
isObject(value) &&
|
|
185
|
+
Object.hasOwn(value, "__proto__"))
|
|
186
|
+
throw new ContractValidationError(tool, "contract", [
|
|
187
|
+
{ path: p, keyword: "unsupported-property-name" },
|
|
188
|
+
]);
|
|
189
|
+
if (MAPS.has(key) && isObject(value))
|
|
190
|
+
return [
|
|
191
|
+
key,
|
|
192
|
+
Object.fromEntries(Object.entries(value).map(([n, s]) => [
|
|
193
|
+
n,
|
|
194
|
+
guardedSchema(s, tool, `${p}/${pointer(n)}`, draft7, budget),
|
|
195
|
+
])),
|
|
196
|
+
];
|
|
197
|
+
if (ARRAYS.has(key) && Array.isArray(value))
|
|
198
|
+
return [
|
|
199
|
+
key,
|
|
200
|
+
value.map((s, i) => guardedSchema(s, tool, `${p}/${i}`, draft7, budget)),
|
|
201
|
+
];
|
|
202
|
+
if (key === "items" && draft7 && Array.isArray(value))
|
|
203
|
+
return [
|
|
204
|
+
key,
|
|
205
|
+
value.map((s, i) => guardedSchema(s, tool, `${p}/${i}`, draft7, budget)),
|
|
206
|
+
];
|
|
207
|
+
if (VALUES.has(key))
|
|
208
|
+
return [
|
|
209
|
+
key,
|
|
210
|
+
guardedSchema(value, tool, p, draft7, budget),
|
|
211
|
+
];
|
|
212
|
+
return [key, value];
|
|
213
|
+
}));
|
|
214
|
+
}
|
|
215
|
+
/** Compile once, then enforce a detached snapshot before inputs and after decoded outputs.
|
|
216
|
+
* Raw successful results are returned unchanged for generated bindings or SDK consumers.
|
|
217
|
+
* MCP error and unfinished results pass through for normal SDK/binding error handling.
|
|
218
|
+
* Reviewed schemas are trusted configuration, not an arbitrary-schema execution sandbox. */
|
|
219
|
+
export function createContractCaller(caller, source, options = {}) {
|
|
220
|
+
const snapshot = parseContractSnapshot(source);
|
|
221
|
+
const inputs = options.inputs ?? true, outputs = options.outputs ?? true;
|
|
222
|
+
const validatorOptions = {
|
|
223
|
+
strict: false,
|
|
224
|
+
validateFormats: false,
|
|
225
|
+
allErrors: false,
|
|
226
|
+
addUsedSchema: false,
|
|
227
|
+
ownProperties: true,
|
|
228
|
+
logger: false,
|
|
229
|
+
code: {
|
|
230
|
+
regExp: Object.assign((pattern, _flags) => RE2JS.compile(pattern), { code: "toolspliceRE2" }),
|
|
231
|
+
},
|
|
232
|
+
};
|
|
233
|
+
const ajv = new Ajv2020(validatorOptions), ajv7 = new Ajv(validatorOptions);
|
|
234
|
+
const validators = new Map();
|
|
235
|
+
const compile = (schema, tool, stage) => {
|
|
236
|
+
if (schema === null)
|
|
237
|
+
throw new ContractValidationError(tool, "contract", [
|
|
238
|
+
{ path: `/${stage}`, keyword: "unobserved-output" },
|
|
239
|
+
]);
|
|
240
|
+
try {
|
|
241
|
+
assertContractJson(schema, 5000, 32);
|
|
242
|
+
}
|
|
243
|
+
catch {
|
|
244
|
+
throw new ContractValidationError(tool, "contract", [
|
|
245
|
+
{ path: `/${stage}`, keyword: "schema-limits" },
|
|
246
|
+
]);
|
|
247
|
+
}
|
|
248
|
+
const draft7 = isObject(schema) && DRAFT7.has(String(schema.$schema));
|
|
249
|
+
const safe = guardedSchema(schema, tool, `/${stage}`, draft7);
|
|
250
|
+
try {
|
|
251
|
+
return (draft7 ? ajv7 : ajv).compile(safe);
|
|
252
|
+
}
|
|
253
|
+
catch {
|
|
254
|
+
throw new ContractValidationError(tool, "contract", [
|
|
255
|
+
{ path: `/${stage}`, keyword: "invalid-or-unresolved-schema" },
|
|
256
|
+
]);
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
for (const tool of snapshot.tools)
|
|
260
|
+
validators.set(tool.name, {
|
|
261
|
+
input: inputs ? compile(tool.input, tool.name, "input") : undefined,
|
|
262
|
+
output: outputs ? compile(tool.output, tool.name, "output") : undefined,
|
|
263
|
+
});
|
|
264
|
+
const check = (fn, value, tool, stage) => {
|
|
265
|
+
if (!fn)
|
|
266
|
+
return;
|
|
267
|
+
try {
|
|
268
|
+
assertContractJson(value, 50_000, 64);
|
|
269
|
+
}
|
|
270
|
+
catch {
|
|
271
|
+
throw new ContractValidationError(tool, stage, [
|
|
272
|
+
{ path: "/", keyword: "data-limits" },
|
|
273
|
+
]);
|
|
274
|
+
}
|
|
275
|
+
let valid;
|
|
276
|
+
try {
|
|
277
|
+
valid = !!fn(value);
|
|
278
|
+
}
|
|
279
|
+
catch {
|
|
280
|
+
throw new ContractValidationError(tool, stage, [
|
|
281
|
+
{ path: "/", keyword: "validation-failed" },
|
|
282
|
+
]);
|
|
283
|
+
}
|
|
284
|
+
if (!valid)
|
|
285
|
+
throw new ContractValidationError(tool, stage, (fn.errors ?? [])
|
|
286
|
+
.slice(0, 4)
|
|
287
|
+
.map((e) => ({ path: e.instancePath || "/", keyword: e.keyword })));
|
|
288
|
+
};
|
|
289
|
+
return async (request, callOptions) => {
|
|
290
|
+
callOptions?.signal?.throwIfAborted();
|
|
291
|
+
const validator = validators.get(request.name);
|
|
292
|
+
if (!validator)
|
|
293
|
+
throw new ContractValidationError(request.name, "contract", [
|
|
294
|
+
{ path: "/tools", keyword: "unknown-tool" },
|
|
295
|
+
]);
|
|
296
|
+
check(validator.input, request.arguments, request.name, "input");
|
|
297
|
+
const raw = await caller(request, callOptions);
|
|
298
|
+
callOptions?.signal?.throwIfAborted();
|
|
299
|
+
if (raw.isError ||
|
|
300
|
+
("resultType" in raw &&
|
|
301
|
+
raw.resultType !== undefined &&
|
|
302
|
+
raw.resultType !== "complete"))
|
|
303
|
+
return raw;
|
|
304
|
+
check(validator.output, decode(raw), request.name, "output");
|
|
305
|
+
return raw;
|
|
306
|
+
};
|
|
307
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type ContractSchema, type ContractSnapshot } from "./contracts.js";
|
|
2
|
+
export interface ContractCorrection {
|
|
3
|
+
format: "toolsplice-correction";
|
|
4
|
+
formatVersion: 1;
|
|
5
|
+
baseFingerprint: string;
|
|
6
|
+
reason: string;
|
|
7
|
+
changes: {
|
|
8
|
+
tool: string;
|
|
9
|
+
side: "input" | "output";
|
|
10
|
+
schema: ContractSchema;
|
|
11
|
+
fixtures: {
|
|
12
|
+
name: string;
|
|
13
|
+
valid: boolean;
|
|
14
|
+
value: unknown;
|
|
15
|
+
}[];
|
|
16
|
+
}[];
|
|
17
|
+
}
|
|
18
|
+
export interface CorrectionFixtureResult {
|
|
19
|
+
tool: string;
|
|
20
|
+
side: "input" | "output";
|
|
21
|
+
name: string;
|
|
22
|
+
expectedValid: boolean;
|
|
23
|
+
originalValid: boolean | null;
|
|
24
|
+
correctedValid: boolean;
|
|
25
|
+
}
|
|
26
|
+
/** Whole-schema replacements are deliberately explicit; no automatic permissive fallback. */
|
|
27
|
+
export declare function parseContractCorrection(input: unknown): ContractCorrection;
|
|
28
|
+
/** Offline fixture checks only. Returns detached original, manifest and corrected artifacts. */
|
|
29
|
+
export declare function applyContractCorrection(baseline: ContractSnapshot, input: ContractCorrection): Promise<{
|
|
30
|
+
original: ContractSnapshot;
|
|
31
|
+
manifest: ContractCorrection;
|
|
32
|
+
corrected: ContractSnapshot;
|
|
33
|
+
fixtures: CorrectionFixtureResult[];
|
|
34
|
+
comparison: import("./contracts.js").ContractComparison;
|
|
35
|
+
}>;
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { parseContractSnapshot, selectContractTools, contractSnapshotFingerprint, compareContractSnapshots, definitionContract, } from "./contracts.js";
|
|
2
|
+
import { createContractCaller, ContractValidationError, } from "./contract-caller.js";
|
|
3
|
+
import { copyJson, jsonFingerprint, jsonObject, parseWorkflowJson, } from "./workflow-json.js";
|
|
4
|
+
const keys = (value, names) => Object.keys(value).every((k) => names.includes(k));
|
|
5
|
+
/** Whole-schema replacements are deliberately explicit; no automatic permissive fallback. */
|
|
6
|
+
export function parseContractCorrection(input) {
|
|
7
|
+
const value = parseWorkflowJson(input);
|
|
8
|
+
const bad = () => {
|
|
9
|
+
throw new Error("Invalid or unsupported contract correction manifest.");
|
|
10
|
+
};
|
|
11
|
+
if (!jsonObject(value) ||
|
|
12
|
+
!keys(value, [
|
|
13
|
+
"format",
|
|
14
|
+
"formatVersion",
|
|
15
|
+
"baseFingerprint",
|
|
16
|
+
"reason",
|
|
17
|
+
"changes",
|
|
18
|
+
]) ||
|
|
19
|
+
value.format !== "toolsplice-correction" ||
|
|
20
|
+
value.formatVersion !== 1 ||
|
|
21
|
+
typeof value.baseFingerprint !== "string" ||
|
|
22
|
+
!/^[a-f0-9]{64}$/.test(value.baseFingerprint) ||
|
|
23
|
+
typeof value.reason !== "string" ||
|
|
24
|
+
!value.reason.trim() ||
|
|
25
|
+
value.reason.length > 2048 ||
|
|
26
|
+
!Array.isArray(value.changes) ||
|
|
27
|
+
!value.changes.length ||
|
|
28
|
+
value.changes.length > 32)
|
|
29
|
+
return bad();
|
|
30
|
+
const changed = new Set();
|
|
31
|
+
for (const c of value.changes) {
|
|
32
|
+
if (!jsonObject(c) ||
|
|
33
|
+
!keys(c, ["tool", "side", "schema", "fixtures"]) ||
|
|
34
|
+
typeof c.tool !== "string" ||
|
|
35
|
+
!c.tool ||
|
|
36
|
+
!["input", "output"].includes(String(c.side)) ||
|
|
37
|
+
!(typeof c.schema === "boolean" || jsonObject(c.schema)) ||
|
|
38
|
+
!Array.isArray(c.fixtures) ||
|
|
39
|
+
c.fixtures.length < 2 ||
|
|
40
|
+
c.fixtures.length > 32)
|
|
41
|
+
return bad();
|
|
42
|
+
const id = JSON.stringify([c.tool, c.side]);
|
|
43
|
+
if (changed.has(id))
|
|
44
|
+
return bad();
|
|
45
|
+
changed.add(id);
|
|
46
|
+
const names = new Set();
|
|
47
|
+
let positive = false, negative = false;
|
|
48
|
+
for (const f of c.fixtures) {
|
|
49
|
+
if (!jsonObject(f) ||
|
|
50
|
+
!keys(f, ["name", "valid", "value"]) ||
|
|
51
|
+
typeof f.name !== "string" ||
|
|
52
|
+
!f.name ||
|
|
53
|
+
f.name.length > 128 ||
|
|
54
|
+
names.has(f.name) ||
|
|
55
|
+
typeof f.valid !== "boolean" ||
|
|
56
|
+
!Object.hasOwn(f, "value"))
|
|
57
|
+
return bad();
|
|
58
|
+
names.add(f.name);
|
|
59
|
+
positive ||= f.valid;
|
|
60
|
+
negative ||= !f.valid;
|
|
61
|
+
}
|
|
62
|
+
if (!positive || !negative)
|
|
63
|
+
return bad();
|
|
64
|
+
}
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
function fixtureCheck(snapshot, tool, side) {
|
|
68
|
+
const single = selectContractTools(snapshot, [tool]);
|
|
69
|
+
const guard = createContractCaller(async () => ({ content: [] }), single, {
|
|
70
|
+
inputs: side === "input",
|
|
71
|
+
outputs: false,
|
|
72
|
+
});
|
|
73
|
+
if (side === "input")
|
|
74
|
+
return async (value) => {
|
|
75
|
+
try {
|
|
76
|
+
await guard({
|
|
77
|
+
name: tool,
|
|
78
|
+
arguments: value,
|
|
79
|
+
});
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
catch (e) {
|
|
83
|
+
if (e instanceof ContractValidationError && e.stage === "input")
|
|
84
|
+
return false;
|
|
85
|
+
throw e;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
const outputGuard = createContractCaller(async (request) => ({ structuredContent: request.arguments.value }), single, { inputs: false });
|
|
89
|
+
return async (value) => {
|
|
90
|
+
try {
|
|
91
|
+
await outputGuard({ name: tool, arguments: { value } });
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
catch (e) {
|
|
95
|
+
if (e instanceof ContractValidationError && e.stage === "output")
|
|
96
|
+
return false;
|
|
97
|
+
throw e;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/** Offline fixture checks only. Returns detached original, manifest and corrected artifacts. */
|
|
102
|
+
export async function applyContractCorrection(baseline, input) {
|
|
103
|
+
const original = parseContractSnapshot(baseline), manifest = parseContractCorrection(input);
|
|
104
|
+
if (manifest.baseFingerprint !== original.fingerprint)
|
|
105
|
+
throw new Error("Correction baseline fingerprint changed; review a new manifest.");
|
|
106
|
+
const corrected = copyJson(original);
|
|
107
|
+
for (const c of manifest.changes) {
|
|
108
|
+
const tool = corrected.tools.find((t) => t.name === c.tool);
|
|
109
|
+
if (!tool)
|
|
110
|
+
throw new Error("Correction names a tool absent from the baseline.");
|
|
111
|
+
const replacement = definitionContract(c.schema);
|
|
112
|
+
if (jsonFingerprint(tool[c.side]) === jsonFingerprint(replacement))
|
|
113
|
+
throw new Error("Correction does not change the selected schema.");
|
|
114
|
+
tool[c.side] = replacement;
|
|
115
|
+
if (c.side === "output")
|
|
116
|
+
tool.outputSource = "corrected";
|
|
117
|
+
}
|
|
118
|
+
corrected.correction = {
|
|
119
|
+
baseFingerprint: original.fingerprint,
|
|
120
|
+
manifestFingerprint: jsonFingerprint(manifest),
|
|
121
|
+
};
|
|
122
|
+
corrected.createdAt = new Date().toISOString();
|
|
123
|
+
corrected.fingerprint = contractSnapshotFingerprint(corrected);
|
|
124
|
+
const validated = parseContractSnapshot(corrected), fixtures = [];
|
|
125
|
+
for (const c of manifest.changes) {
|
|
126
|
+
const check = fixtureCheck(validated, c.tool, c.side);
|
|
127
|
+
const oldTool = original.tools.find((t) => t.name === c.tool);
|
|
128
|
+
let before;
|
|
129
|
+
// A bad original declaration may itself be unsupported. That is unknown, not accepted.
|
|
130
|
+
if (c.side !== "output" || oldTool.output !== null) {
|
|
131
|
+
try {
|
|
132
|
+
before = fixtureCheck(original, c.tool, c.side);
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
/* report unknown */
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
for (const f of c.fixtures) {
|
|
139
|
+
const accepted = await check(f.value);
|
|
140
|
+
if (accepted !== f.valid)
|
|
141
|
+
throw new Error("Correction fixture expectation failed; corrected contract was not produced.");
|
|
142
|
+
fixtures.push({
|
|
143
|
+
tool: c.tool,
|
|
144
|
+
side: c.side,
|
|
145
|
+
name: f.name,
|
|
146
|
+
expectedValid: f.valid,
|
|
147
|
+
originalValid: before ? await before(f.value) : null,
|
|
148
|
+
correctedValid: accepted,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
original,
|
|
154
|
+
manifest,
|
|
155
|
+
corrected: validated,
|
|
156
|
+
fixtures,
|
|
157
|
+
comparison: compareContractSnapshots(original, validated),
|
|
158
|
+
};
|
|
159
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** Reject cycles, excessive work, and non-JSON library inputs before hashing or compiling. */
|
|
2
|
+
export declare function assertContractJson(value: unknown, maxNodes?: number, maxDepth?: number): void;
|
|
3
|
+
/** Declared constraints matter even when inference deliberately omits observed numeric ranges.
|
|
4
|
+
* Annotation keywords are removed only at schema positions, never from literal values/field names. */
|
|
5
|
+
export declare function definitionContract(schema: unknown): unknown;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// Bounded JSON and schema canonicalization shared by snapshots and definition fingerprints.
|
|
2
|
+
const ANNOTATIONS = new Set([
|
|
3
|
+
"title",
|
|
4
|
+
"description",
|
|
5
|
+
"$comment",
|
|
6
|
+
"examples",
|
|
7
|
+
"default",
|
|
8
|
+
"deprecated",
|
|
9
|
+
"readOnly",
|
|
10
|
+
"writeOnly",
|
|
11
|
+
]);
|
|
12
|
+
const SCHEMA_MAPS = new Set([
|
|
13
|
+
"properties",
|
|
14
|
+
"patternProperties",
|
|
15
|
+
"$defs",
|
|
16
|
+
"definitions",
|
|
17
|
+
"dependentSchemas",
|
|
18
|
+
]);
|
|
19
|
+
const SCHEMA_ARRAYS = new Set(["allOf", "anyOf", "oneOf", "prefixItems"]);
|
|
20
|
+
const SCHEMA_VALUES = new Set([
|
|
21
|
+
"items",
|
|
22
|
+
"additionalItems",
|
|
23
|
+
"additionalProperties",
|
|
24
|
+
"unevaluatedItems",
|
|
25
|
+
"unevaluatedProperties",
|
|
26
|
+
"contains",
|
|
27
|
+
"propertyNames",
|
|
28
|
+
"not",
|
|
29
|
+
"if",
|
|
30
|
+
"then",
|
|
31
|
+
"else",
|
|
32
|
+
"contentSchema",
|
|
33
|
+
]);
|
|
34
|
+
const SET_ARRAYS = new Set(["required", "type", "enum"]);
|
|
35
|
+
const cmp = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
36
|
+
const isObject = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
37
|
+
/** Reject cycles, excessive work, and non-JSON library inputs before hashing or compiling. */
|
|
38
|
+
export function assertContractJson(value, maxNodes = 50_000, maxDepth = 64) {
|
|
39
|
+
const active = new WeakSet();
|
|
40
|
+
let remaining = maxNodes, bytes = 5 * 1024 * 1024;
|
|
41
|
+
const walk = (v, depth) => {
|
|
42
|
+
if (--remaining < 0 || depth > maxDepth)
|
|
43
|
+
throw new Error("Contract data exceeds size or depth limits.");
|
|
44
|
+
if (typeof v === "string") {
|
|
45
|
+
bytes -= Buffer.byteLength(v);
|
|
46
|
+
if (bytes < 0)
|
|
47
|
+
throw new Error("Contract data exceeds the 5 MiB string budget.");
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (v === null ||
|
|
51
|
+
typeof v === "boolean" ||
|
|
52
|
+
(typeof v === "number" && Number.isFinite(v)))
|
|
53
|
+
return;
|
|
54
|
+
if (typeof v !== "object" || !v || active.has(v))
|
|
55
|
+
throw new Error("Contract data must be finite, acyclic JSON.");
|
|
56
|
+
if (!Array.isArray(v) &&
|
|
57
|
+
![Object.prototype, null].includes(Object.getPrototypeOf(v)))
|
|
58
|
+
throw new Error("Contract data must contain only plain JSON objects.");
|
|
59
|
+
active.add(v);
|
|
60
|
+
if (Array.isArray(v)) {
|
|
61
|
+
for (const item of v)
|
|
62
|
+
walk(item, depth + 1);
|
|
63
|
+
}
|
|
64
|
+
else
|
|
65
|
+
for (const [key, x] of Object.entries(v)) {
|
|
66
|
+
bytes -= Buffer.byteLength(key);
|
|
67
|
+
if (bytes < 0)
|
|
68
|
+
throw new Error("Contract data exceeds the 5 MiB string budget.");
|
|
69
|
+
walk(x, depth + 1);
|
|
70
|
+
}
|
|
71
|
+
active.delete(v);
|
|
72
|
+
};
|
|
73
|
+
walk(value, 0);
|
|
74
|
+
}
|
|
75
|
+
function canonicalLiteral(v) {
|
|
76
|
+
if (Array.isArray(v))
|
|
77
|
+
return v.map(canonicalLiteral);
|
|
78
|
+
if (!isObject(v))
|
|
79
|
+
return v;
|
|
80
|
+
return Object.fromEntries(Object.keys(v)
|
|
81
|
+
.sort(cmp)
|
|
82
|
+
.map((k) => [k, canonicalLiteral(v[k])]));
|
|
83
|
+
}
|
|
84
|
+
/** Declared constraints matter even when inference deliberately omits observed numeric ranges.
|
|
85
|
+
* Annotation keywords are removed only at schema positions, never from literal values/field names. */
|
|
86
|
+
export function definitionContract(schema) {
|
|
87
|
+
assertContractJson(schema);
|
|
88
|
+
const walk = (s) => {
|
|
89
|
+
if (!isObject(s))
|
|
90
|
+
return canonicalLiteral(s);
|
|
91
|
+
return Object.fromEntries(Object.keys(s)
|
|
92
|
+
.filter((k) => !ANNOTATIONS.has(k))
|
|
93
|
+
.sort(cmp)
|
|
94
|
+
.map((k) => {
|
|
95
|
+
const v = s[k];
|
|
96
|
+
if (SCHEMA_MAPS.has(k) && isObject(v))
|
|
97
|
+
return [
|
|
98
|
+
k,
|
|
99
|
+
Object.fromEntries(Object.keys(v)
|
|
100
|
+
.sort(cmp)
|
|
101
|
+
.map((n) => [n, walk(v[n])])),
|
|
102
|
+
];
|
|
103
|
+
if (SCHEMA_ARRAYS.has(k) && Array.isArray(v)) {
|
|
104
|
+
const values = v.map(walk);
|
|
105
|
+
return [
|
|
106
|
+
k,
|
|
107
|
+
k === "prefixItems"
|
|
108
|
+
? values
|
|
109
|
+
: values.sort((a, b) => cmp(JSON.stringify(a), JSON.stringify(b))),
|
|
110
|
+
];
|
|
111
|
+
}
|
|
112
|
+
if (SCHEMA_VALUES.has(k))
|
|
113
|
+
return [k, Array.isArray(v) ? v.map(walk) : walk(v)];
|
|
114
|
+
const value = canonicalLiteral(v);
|
|
115
|
+
if (k === "type" && Array.isArray(value) && value.length === 1)
|
|
116
|
+
return [k, value[0]];
|
|
117
|
+
return [
|
|
118
|
+
k,
|
|
119
|
+
SET_ARRAYS.has(k) && Array.isArray(value)
|
|
120
|
+
? [...value].sort((a, b) => cmp(JSON.stringify(a), JSON.stringify(b)))
|
|
121
|
+
: value,
|
|
122
|
+
];
|
|
123
|
+
}));
|
|
124
|
+
};
|
|
125
|
+
return walk(schema);
|
|
126
|
+
}
|