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,71 @@
|
|
|
1
|
+
import { type ToolListEntry, type ServerRecord, type ToolAnnotations } from "./store.js";
|
|
2
|
+
import type { JsonSchema } from "./schema.js";
|
|
3
|
+
export type ContractSchema = JsonSchema | boolean;
|
|
4
|
+
export interface ContractTool {
|
|
5
|
+
name: string;
|
|
6
|
+
input: ContractSchema;
|
|
7
|
+
output: ContractSchema | null;
|
|
8
|
+
outputSource: "declared" | "learned" | "unknown" | "corrected";
|
|
9
|
+
encodings: ("structured" | "json-text" | "text" | "empty")[];
|
|
10
|
+
observations: number;
|
|
11
|
+
limited: boolean;
|
|
12
|
+
annotations: Omit<ToolAnnotations, "title">;
|
|
13
|
+
}
|
|
14
|
+
export interface ContractSnapshot {
|
|
15
|
+
format: "callshape-contract";
|
|
16
|
+
formatVersion: 1;
|
|
17
|
+
createdAt: string;
|
|
18
|
+
server: {
|
|
19
|
+
name: string;
|
|
20
|
+
version?: string;
|
|
21
|
+
};
|
|
22
|
+
selection: "all" | "subset";
|
|
23
|
+
tools: ContractTool[];
|
|
24
|
+
fingerprint: string;
|
|
25
|
+
/** An explicit application correction, bound into this snapshot's fingerprint. */
|
|
26
|
+
correction?: {
|
|
27
|
+
baseFingerprint: string;
|
|
28
|
+
manifestFingerprint: string;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export type ChangeSeverity = "breaking" | "additive" | "review";
|
|
32
|
+
export interface ContractChange {
|
|
33
|
+
tool?: string;
|
|
34
|
+
path: string;
|
|
35
|
+
severity: ChangeSeverity;
|
|
36
|
+
message: string;
|
|
37
|
+
}
|
|
38
|
+
export interface ContractComparison {
|
|
39
|
+
server: string;
|
|
40
|
+
status: "unchanged" | "changed";
|
|
41
|
+
changes: ContractChange[];
|
|
42
|
+
truncated: boolean;
|
|
43
|
+
counts: Record<ChangeSeverity, number>;
|
|
44
|
+
note: string;
|
|
45
|
+
}
|
|
46
|
+
export { definitionContract } from "./contract-schema.js";
|
|
47
|
+
/** Internal helper for producing explicitly revised snapshots; parsing still verifies identity. */
|
|
48
|
+
export declare function contractSnapshotFingerprint(snapshot: ContractSnapshot): string;
|
|
49
|
+
export declare function createContractSnapshot(server: ServerRecord, options?: {
|
|
50
|
+
tools?: string[];
|
|
51
|
+
}): ContractSnapshot;
|
|
52
|
+
/** Build a portable declared contract directly from one tools/list page.
|
|
53
|
+
* The application owns catalog pagination, endpoint identity and connections.
|
|
54
|
+
* This function never opens a store, calls tools or infers semantic field matches.
|
|
55
|
+
*/
|
|
56
|
+
export declare function createContractFromCatalog(server: {
|
|
57
|
+
name: string;
|
|
58
|
+
version?: string;
|
|
59
|
+
}, catalog: {
|
|
60
|
+
tools: readonly ToolListEntry[];
|
|
61
|
+
}, options?: {
|
|
62
|
+
tools?: string[];
|
|
63
|
+
}): ContractSnapshot;
|
|
64
|
+
/** Parse without executing schemas, fetching refs, connecting to servers, or changing learning. */
|
|
65
|
+
export declare function parseContractSnapshot(source: string | unknown): ContractSnapshot;
|
|
66
|
+
/** Exact canonical changes with conservative labels, not a general JSON Schema subsumption solver.
|
|
67
|
+
* Anything not justified by a simple supported rule requires review. */
|
|
68
|
+
export declare function compareContractSnapshots(baseline: ContractSnapshot, current: ContractSnapshot): ContractComparison;
|
|
69
|
+
export declare function contractCheckFailed(report: ContractComparison, failOn?: "any" | "breaking"): boolean;
|
|
70
|
+
/** Derive a detached subset without changing its schemas or fabricating new observations. */
|
|
71
|
+
export declare function selectContractTools(source: ContractSnapshot, names: string[]): ContractSnapshot;
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
// Portable, reviewable contract baselines. Comparing observations is evidence of change,
|
|
2
|
+
// not proof that an upstream service or every consumer is compatible.
|
|
3
|
+
import { contract, stableHash } from "./drift.js";
|
|
4
|
+
import { toolOutput, newServer, recordToolList, } from "./store.js";
|
|
5
|
+
import { diagnoseTool } from "./doctor.js";
|
|
6
|
+
const cmp = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
7
|
+
const pointer = (value) => value.replace(/~/g, "~0").replace(/\//g, "~1");
|
|
8
|
+
const isObject = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
9
|
+
import { assertContractJson, definitionContract } from "./contract-schema.js";
|
|
10
|
+
export { definitionContract } from "./contract-schema.js";
|
|
11
|
+
function canonicalLiteral(v) {
|
|
12
|
+
if (Array.isArray(v))
|
|
13
|
+
return v.map(canonicalLiteral);
|
|
14
|
+
if (!isObject(v))
|
|
15
|
+
return v;
|
|
16
|
+
return Object.fromEntries(Object.keys(v)
|
|
17
|
+
.sort(cmp)
|
|
18
|
+
.map((k) => [k, canonicalLiteral(v[k])]));
|
|
19
|
+
}
|
|
20
|
+
function identity(snapshot) {
|
|
21
|
+
return {
|
|
22
|
+
server: snapshot.server,
|
|
23
|
+
selection: snapshot.selection,
|
|
24
|
+
tools: snapshot.tools.map(({ observations: _n, ...tool }) => tool),
|
|
25
|
+
...(snapshot.correction === undefined
|
|
26
|
+
? {}
|
|
27
|
+
: { correction: snapshot.correction }),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/** Internal helper for producing explicitly revised snapshots; parsing still verifies identity. */
|
|
31
|
+
export function contractSnapshotFingerprint(snapshot) {
|
|
32
|
+
return stableHash(canonicalLiteral(identity(snapshot)));
|
|
33
|
+
}
|
|
34
|
+
function annotations(v) {
|
|
35
|
+
return Object.fromEntries(["readOnlyHint", "destructiveHint", "idempotentHint", "openWorldHint"]
|
|
36
|
+
.filter((k) => typeof v?.[k] === "boolean")
|
|
37
|
+
.map((k) => [k, v[k]]));
|
|
38
|
+
}
|
|
39
|
+
export function createContractSnapshot(server, options = {}) {
|
|
40
|
+
const names = options.tools ?? Object.keys(server.tools);
|
|
41
|
+
if (new Set(names).size !== names.length ||
|
|
42
|
+
names.some((n) => !Object.hasOwn(server.tools, n)))
|
|
43
|
+
throw new Error("Contract selection must contain distinct, recorded tool names.");
|
|
44
|
+
const tools = [...names].sort(cmp).map((name) => {
|
|
45
|
+
const tool = server.tools[name], out = toolOutput(tool);
|
|
46
|
+
let output = out?.schema
|
|
47
|
+
? out.source === "declared"
|
|
48
|
+
? definitionContract(out.schema)
|
|
49
|
+
: contract(out.schema)
|
|
50
|
+
: out
|
|
51
|
+
? { type: "string" }
|
|
52
|
+
: null;
|
|
53
|
+
if (out?.schema && out.alsoText)
|
|
54
|
+
output = { anyOf: [output, { type: "string" }] };
|
|
55
|
+
if (output !== null && tool.kinds.empty > 0)
|
|
56
|
+
output = { anyOf: [output, { type: "null" }] };
|
|
57
|
+
if (output === null &&
|
|
58
|
+
tool.kinds.empty > 0 &&
|
|
59
|
+
!tool.kinds.structured &&
|
|
60
|
+
!tool.kinds["json-text"] &&
|
|
61
|
+
!tool.kinds.text)
|
|
62
|
+
output = { type: "null" };
|
|
63
|
+
return {
|
|
64
|
+
name,
|
|
65
|
+
input: definitionContract(tool.inputSchema ?? { type: "object" }),
|
|
66
|
+
output: output,
|
|
67
|
+
outputSource: out?.source ?? (output === null ? "unknown" : "learned"),
|
|
68
|
+
encodings: ["structured", "json-text", "text", "empty"].filter((k) => tool.kinds[k] > 0),
|
|
69
|
+
observations: tool.shape.count + tool.kinds.text + tool.kinds.empty,
|
|
70
|
+
limited: diagnoseTool(tool).limited,
|
|
71
|
+
annotations: annotations(tool.annotations),
|
|
72
|
+
};
|
|
73
|
+
});
|
|
74
|
+
const snapshot = {
|
|
75
|
+
format: "callshape-contract",
|
|
76
|
+
formatVersion: 1,
|
|
77
|
+
createdAt: new Date().toISOString(),
|
|
78
|
+
server: {
|
|
79
|
+
name: server.name,
|
|
80
|
+
...(server.version === undefined ? {} : { version: server.version }),
|
|
81
|
+
},
|
|
82
|
+
selection: options.tools ? "subset" : "all",
|
|
83
|
+
tools,
|
|
84
|
+
};
|
|
85
|
+
const result = {
|
|
86
|
+
...snapshot,
|
|
87
|
+
fingerprint: stableHash(canonicalLiteral(identity(snapshot))),
|
|
88
|
+
};
|
|
89
|
+
assertContractJson(result);
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
/** Build a portable declared contract directly from one tools/list page.
|
|
93
|
+
* The application owns catalog pagination, endpoint identity and connections.
|
|
94
|
+
* This function never opens a store, calls tools or infers semantic field matches.
|
|
95
|
+
*/
|
|
96
|
+
export function createContractFromCatalog(server, catalog, options = {}) {
|
|
97
|
+
if (!server ||
|
|
98
|
+
typeof server.name !== "string" ||
|
|
99
|
+
!server.name ||
|
|
100
|
+
(server.version !== undefined && typeof server.version !== "string") ||
|
|
101
|
+
!catalog ||
|
|
102
|
+
!Array.isArray(catalog.tools))
|
|
103
|
+
throw new Error("A server identity and tools/list catalog are required.");
|
|
104
|
+
const names = new Set();
|
|
105
|
+
for (const tool of catalog.tools) {
|
|
106
|
+
if (!tool ||
|
|
107
|
+
typeof tool.name !== "string" ||
|
|
108
|
+
!tool.name ||
|
|
109
|
+
names.has(tool.name) ||
|
|
110
|
+
!isObject(tool.inputSchema) ||
|
|
111
|
+
(tool.outputSchema !== undefined && !isObject(tool.outputSchema)))
|
|
112
|
+
throw new Error("Catalog tools need distinct names and JSON schemas.");
|
|
113
|
+
names.add(tool.name);
|
|
114
|
+
}
|
|
115
|
+
const record = newServer(server.name, server.version);
|
|
116
|
+
recordToolList(record, [...catalog.tools]);
|
|
117
|
+
// Parsing also detaches the result from the caller's catalog and checks limits.
|
|
118
|
+
return parseContractSnapshot(createContractSnapshot(record, options));
|
|
119
|
+
}
|
|
120
|
+
/** Parse without executing schemas, fetching refs, connecting to servers, or changing learning. */
|
|
121
|
+
export function parseContractSnapshot(source) {
|
|
122
|
+
if (typeof source === "string" && Buffer.byteLength(source) > 5 * 1024 * 1024)
|
|
123
|
+
throw new Error("Contract snapshot exceeds the 5 MiB limit.");
|
|
124
|
+
let value;
|
|
125
|
+
try {
|
|
126
|
+
value = typeof source === "string" ? JSON.parse(source) : source;
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
throw new Error("Contract snapshot is not valid JSON.");
|
|
130
|
+
}
|
|
131
|
+
assertContractJson(value);
|
|
132
|
+
const bad = () => {
|
|
133
|
+
throw new Error("Invalid or unsupported Toolsplice contract snapshot.");
|
|
134
|
+
};
|
|
135
|
+
if (!isObject(value) ||
|
|
136
|
+
value.format !== "callshape-contract" ||
|
|
137
|
+
value.formatVersion !== 1 ||
|
|
138
|
+
typeof value.createdAt !== "string" ||
|
|
139
|
+
!Number.isFinite(Date.parse(value.createdAt)) ||
|
|
140
|
+
!isObject(value.server) ||
|
|
141
|
+
typeof value.server.name !== "string" ||
|
|
142
|
+
(value.server.version !== undefined &&
|
|
143
|
+
typeof value.server.version !== "string") ||
|
|
144
|
+
!["all", "subset"].includes(String(value.selection)) ||
|
|
145
|
+
!Array.isArray(value.tools) ||
|
|
146
|
+
typeof value.fingerprint !== "string")
|
|
147
|
+
return bad();
|
|
148
|
+
const names = new Set();
|
|
149
|
+
if (value.correction !== undefined &&
|
|
150
|
+
(!isObject(value.correction) ||
|
|
151
|
+
Object.keys(value.correction).some((k) => !["baseFingerprint", "manifestFingerprint"].includes(k)) ||
|
|
152
|
+
typeof value.correction.baseFingerprint !== "string" ||
|
|
153
|
+
typeof value.correction.manifestFingerprint !== "string" ||
|
|
154
|
+
!/^[a-f0-9]{64}$/.test(String(value.correction.baseFingerprint)) ||
|
|
155
|
+
!/^[a-f0-9]{64}$/.test(String(value.correction.manifestFingerprint))))
|
|
156
|
+
return bad();
|
|
157
|
+
for (const t of value.tools) {
|
|
158
|
+
if (!isObject(t) ||
|
|
159
|
+
typeof t.name !== "string" ||
|
|
160
|
+
names.has(t.name) ||
|
|
161
|
+
!(isObject(t.input) || typeof t.input === "boolean") ||
|
|
162
|
+
!(t.output === null ||
|
|
163
|
+
isObject(t.output) ||
|
|
164
|
+
typeof t.output === "boolean") ||
|
|
165
|
+
!["declared", "learned", "unknown", "corrected"].includes(String(t.outputSource)) ||
|
|
166
|
+
!Number.isSafeInteger(t.observations) ||
|
|
167
|
+
Number(t.observations) < 0 ||
|
|
168
|
+
typeof t.limited !== "boolean" ||
|
|
169
|
+
!isObject(t.annotations) ||
|
|
170
|
+
!Array.isArray(t.encodings) ||
|
|
171
|
+
new Set(t.encodings).size !== t.encodings.length ||
|
|
172
|
+
t.encodings.some((k) => !["structured", "json-text", "text", "empty"].includes(k)))
|
|
173
|
+
return bad();
|
|
174
|
+
if ((t.outputSource === "unknown") !== (t.output === null))
|
|
175
|
+
return bad();
|
|
176
|
+
if (t.outputSource === "corrected" && value.correction === undefined)
|
|
177
|
+
return bad();
|
|
178
|
+
if (Object.entries(t.annotations).some(([k, v]) => ![
|
|
179
|
+
"readOnlyHint",
|
|
180
|
+
"destructiveHint",
|
|
181
|
+
"idempotentHint",
|
|
182
|
+
"openWorldHint",
|
|
183
|
+
].includes(k) || typeof v !== "boolean"))
|
|
184
|
+
return bad();
|
|
185
|
+
names.add(t.name);
|
|
186
|
+
}
|
|
187
|
+
const snapshot = canonicalLiteral(value);
|
|
188
|
+
if (stableHash(canonicalLiteral(identity(snapshot))) !== snapshot.fingerprint)
|
|
189
|
+
throw new Error("Contract snapshot fingerprint does not match its contents. Regenerate and review the baseline.");
|
|
190
|
+
return snapshot;
|
|
191
|
+
}
|
|
192
|
+
/** Exact canonical changes with conservative labels, not a general JSON Schema subsumption solver.
|
|
193
|
+
* Anything not justified by a simple supported rule requires review. */
|
|
194
|
+
export function compareContractSnapshots(baseline, current) {
|
|
195
|
+
baseline = parseContractSnapshot(baseline);
|
|
196
|
+
current = parseContractSnapshot(current);
|
|
197
|
+
if (baseline.server.name !== current.server.name)
|
|
198
|
+
throw new Error("Cannot compare contracts for different server identities.");
|
|
199
|
+
const changes = [];
|
|
200
|
+
let truncated = false;
|
|
201
|
+
const add = (path, severity, message, tool) => {
|
|
202
|
+
if (changes.length >= 500) {
|
|
203
|
+
truncated = true;
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
changes.push({
|
|
207
|
+
...(tool === undefined ? {} : { tool }),
|
|
208
|
+
path,
|
|
209
|
+
severity,
|
|
210
|
+
message,
|
|
211
|
+
});
|
|
212
|
+
};
|
|
213
|
+
if (baseline.server.version !== current.server.version)
|
|
214
|
+
add("/server/version", "review", "Server version changed; verify this installation's contract.");
|
|
215
|
+
if (baseline.selection !== current.selection)
|
|
216
|
+
add("/selection", "review", "Tool selection scope changed.");
|
|
217
|
+
const old = new Map(baseline.tools.map((t) => [t.name, t])), next = new Map(current.tools.map((t) => [t.name, t]));
|
|
218
|
+
for (const name of [...new Set([...old.keys(), ...next.keys()])].sort(cmp)) {
|
|
219
|
+
const a = old.get(name), b = next.get(name), path = `/tools/${pointer(name)}`;
|
|
220
|
+
if (!a) {
|
|
221
|
+
add(path, "additive", "Tool added.", name);
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
if (!b) {
|
|
225
|
+
add(path, "breaking", "Previously reviewed tool is absent.", name);
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
for (const key of ["input", "output"])
|
|
229
|
+
schemaChanges(a[key], b[key], `${path}/${key}`, key, (p, s, m) => add(p, s, m, name));
|
|
230
|
+
for (const key of [
|
|
231
|
+
"outputSource",
|
|
232
|
+
"encodings",
|
|
233
|
+
"limited",
|
|
234
|
+
"annotations",
|
|
235
|
+
]) {
|
|
236
|
+
if (JSON.stringify(a[key]) !== JSON.stringify(b[key]))
|
|
237
|
+
add(`${path}/${key}`, "review", `Tool ${key} changed; check assumptions before continuing.`, name);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
const counts = { breaking: 0, additive: 0, review: 0 };
|
|
241
|
+
for (const c of changes)
|
|
242
|
+
counts[c.severity]++;
|
|
243
|
+
return {
|
|
244
|
+
server: baseline.server.name,
|
|
245
|
+
status: changes.length || truncated ? "changed" : "unchanged",
|
|
246
|
+
changes,
|
|
247
|
+
truncated,
|
|
248
|
+
counts,
|
|
249
|
+
note: "Observed changes are evidence, not upstream guarantees. Review labels are conservative; this is not full consumer-driven contract testing.",
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
export function contractCheckFailed(report, failOn = "any") {
|
|
253
|
+
if (!["any", "breaking"].includes(failOn))
|
|
254
|
+
throw new Error("failOn must be any or breaking.");
|
|
255
|
+
return (report.truncated ||
|
|
256
|
+
(failOn === "any"
|
|
257
|
+
? report.status === "changed"
|
|
258
|
+
: report.counts.breaking > 0 || report.counts.review > 0));
|
|
259
|
+
}
|
|
260
|
+
function schemaChanges(a, b, path, direction, add, complex = false) {
|
|
261
|
+
if (JSON.stringify(a) === JSON.stringify(b))
|
|
262
|
+
return;
|
|
263
|
+
const severity = (narrows) => complex
|
|
264
|
+
? "review"
|
|
265
|
+
: narrows === (direction === "input")
|
|
266
|
+
? "breaking"
|
|
267
|
+
: "additive";
|
|
268
|
+
if (!isObject(a) || !isObject(b)) {
|
|
269
|
+
add(path, "review", "Schema availability, literal, or boolean constraint changed.");
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
complex ||= hasComplex(a) || hasComplex(b);
|
|
273
|
+
for (const key of [...new Set([...Object.keys(a), ...Object.keys(b)])].sort(cmp)) {
|
|
274
|
+
const av = a[key], bv = b[key], p = `${path}/${pointer(key)}`;
|
|
275
|
+
if (JSON.stringify(av) === JSON.stringify(bv))
|
|
276
|
+
continue;
|
|
277
|
+
if (key === "properties" &&
|
|
278
|
+
(av === undefined || isObject(av)) &&
|
|
279
|
+
(bv === undefined || isObject(bv))) {
|
|
280
|
+
const aa = av ?? {}, bb = bv ?? {};
|
|
281
|
+
for (const field of [
|
|
282
|
+
...new Set([...Object.keys(aa), ...Object.keys(bb)]),
|
|
283
|
+
].sort(cmp)) {
|
|
284
|
+
const fp = `${p}/${pointer(field)}`;
|
|
285
|
+
if (!Object.hasOwn(aa, field)) {
|
|
286
|
+
const required = Array.isArray(b.required) && b.required.includes(field);
|
|
287
|
+
const safe = !complex &&
|
|
288
|
+
(direction === "input"
|
|
289
|
+
? !required && a.additionalProperties === false
|
|
290
|
+
: a.additionalProperties === undefined ||
|
|
291
|
+
a.additionalProperties === true);
|
|
292
|
+
add(fp, safe ? "additive" : "review", "Property added; required and closed-object constraints may affect callers.");
|
|
293
|
+
}
|
|
294
|
+
else if (!Object.hasOwn(bb, field)) {
|
|
295
|
+
const required = Array.isArray(a.required) && a.required.includes(field);
|
|
296
|
+
add(fp, !complex && direction === "output" && required
|
|
297
|
+
? "breaking"
|
|
298
|
+
: "review", "Property removed from the schema.");
|
|
299
|
+
}
|
|
300
|
+
else
|
|
301
|
+
schemaChanges(aa[field], bb[field], fp, direction, add, complex || hasComplex(a) || hasComplex(b));
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
else if (["required", "type", "enum"].includes(key)) {
|
|
305
|
+
const list = (v) => Array.isArray(v) ? v : v === undefined ? [] : [v];
|
|
306
|
+
const aa = list(av).map((v) => JSON.stringify(v)), bb = list(bv).map((v) => JSON.stringify(v));
|
|
307
|
+
const added = bb.some((v) => !aa.includes(v)), removed = aa.some((v) => !bb.includes(v));
|
|
308
|
+
if (key === "required" && !(added && removed))
|
|
309
|
+
add(p, severity(added), "Required property set changed.");
|
|
310
|
+
else if (av !== undefined && bv !== undefined && !(added && removed))
|
|
311
|
+
add(p, severity(removed), "Allowed type or enum set changed.");
|
|
312
|
+
else
|
|
313
|
+
add(p, "review", "Type, enum, or required constraint needs review.");
|
|
314
|
+
}
|
|
315
|
+
else if ([
|
|
316
|
+
"minimum",
|
|
317
|
+
"exclusiveMinimum",
|
|
318
|
+
"minLength",
|
|
319
|
+
"minItems",
|
|
320
|
+
"minProperties",
|
|
321
|
+
"maximum",
|
|
322
|
+
"exclusiveMaximum",
|
|
323
|
+
"maxLength",
|
|
324
|
+
"maxItems",
|
|
325
|
+
"maxProperties",
|
|
326
|
+
].includes(key) &&
|
|
327
|
+
typeof av === "number" &&
|
|
328
|
+
typeof bv === "number") {
|
|
329
|
+
const isMin = key.startsWith("min") || key === "exclusiveMinimum";
|
|
330
|
+
add(p, severity(isMin ? bv > av : bv < av), "Declared bound changed.");
|
|
331
|
+
}
|
|
332
|
+
else if (key === "items" && isObject(av) && isObject(bv))
|
|
333
|
+
schemaChanges(av, bv, p, direction, add, complex || hasComplex(a) || hasComplex(b));
|
|
334
|
+
else
|
|
335
|
+
add(p, "review", "Constraint changed; compatibility requires review.");
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
function hasComplex(s) {
|
|
339
|
+
return [
|
|
340
|
+
"$ref",
|
|
341
|
+
"$dynamicRef",
|
|
342
|
+
"anyOf",
|
|
343
|
+
"oneOf",
|
|
344
|
+
"allOf",
|
|
345
|
+
"not",
|
|
346
|
+
"if",
|
|
347
|
+
"then",
|
|
348
|
+
"else",
|
|
349
|
+
"dependentSchemas",
|
|
350
|
+
"dependentRequired",
|
|
351
|
+
"dependencies",
|
|
352
|
+
"patternProperties",
|
|
353
|
+
"unevaluatedProperties",
|
|
354
|
+
].some((k) => Object.hasOwn(s, k));
|
|
355
|
+
}
|
|
356
|
+
/** Derive a detached subset without changing its schemas or fabricating new observations. */
|
|
357
|
+
export function selectContractTools(source, names) {
|
|
358
|
+
const snapshot = parseContractSnapshot(source), wanted = new Set(names);
|
|
359
|
+
if (wanted.size !== names.length ||
|
|
360
|
+
names.some((name) => !snapshot.tools.some((t) => t.name === name)))
|
|
361
|
+
throw new Error("Contract selection must contain distinct, recorded tool names.");
|
|
362
|
+
snapshot.tools = snapshot.tools.filter((t) => wanted.has(t.name));
|
|
363
|
+
snapshot.selection = "subset";
|
|
364
|
+
snapshot.fingerprint = stableHash(canonicalLiteral(identity(snapshot)));
|
|
365
|
+
return snapshot;
|
|
366
|
+
}
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { extract, parseJsonText, type Extracted, type ToolResultLike, } from "./extract.js";
|
|
2
|
+
export { decode, invokeTool, ToolError, type ToolCaller, type BindingCallOptions, type DecodeMode, } from "./execution.js";
|
|
3
|
+
export { createContractFromCatalog, createContractSnapshot, parseContractSnapshot, selectContractTools, compareContractSnapshots, contractCheckFailed, type ContractSnapshot, type ContractTool, type ContractSchema, } from "./contracts.js";
|
|
4
|
+
export { createContractCaller, ContractValidationError, type ContractIssue, type ContractCallerOptions, } from "./contract-caller.js";
|
|
5
|
+
export { parseSpliceSpec, preflightSplice, prepareSplice, SpliceError, type SpliceSpec, type SpliceBinding, type SpliceReport, type SpliceIssue, type SpliceRunOptions, type SpliceMappingOptions, } from "./splice.js";
|
|
6
|
+
export { explainSpliceError, type SpliceExplanation, type SpliceFieldOrigin, } from "./splice-diagnostics.js";
|
|
7
|
+
export { createSpliceRecorder, parseSpliceRecording, replaySpliceRecording, type SpliceRecording, type SpliceRecordingOptions, type SpliceReplayOptions, } from "./splice-recording.js";
|
|
8
|
+
export { parseContractCorrection, applyContractCorrection, type ContractCorrection, type CorrectionFixtureResult, } from "./contract-corrections.js";
|
|
9
|
+
import { type SpliceSpec } from "./splice.js";
|
|
10
|
+
import type { ContractSnapshot } from "./contracts.js";
|
|
11
|
+
/** Generated adapters use the small package unless explicitly overridden. */
|
|
12
|
+
export declare function emitSpliceModule(plan: SpliceSpec, source: ContractSnapshot, target: ContractSnapshot, options?: {
|
|
13
|
+
runtimeImport?: string;
|
|
14
|
+
}): string;
|
package/dist/core.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// The standalone package entry. No SDK, transports, CLI, proxy or PostgreSQL exports.
|
|
2
|
+
export { extract, parseJsonText, } from "./extract.js";
|
|
3
|
+
export { decode, invokeTool, ToolError, } from "./execution.js";
|
|
4
|
+
export { createContractFromCatalog, createContractSnapshot, parseContractSnapshot, selectContractTools, compareContractSnapshots, contractCheckFailed, } from "./contracts.js";
|
|
5
|
+
export { createContractCaller, ContractValidationError, } from "./contract-caller.js";
|
|
6
|
+
export { parseSpliceSpec, preflightSplice, prepareSplice, SpliceError, } from "./splice.js";
|
|
7
|
+
export { explainSpliceError, } from "./splice-diagnostics.js";
|
|
8
|
+
export { createSpliceRecorder, parseSpliceRecording, replaySpliceRecording, } from "./splice-recording.js";
|
|
9
|
+
export { parseContractCorrection, applyContractCorrection, } from "./contract-corrections.js";
|
|
10
|
+
import { emitSpliceModule as emit } from "./splice.js";
|
|
11
|
+
/** Generated adapters use the small package unless explicitly overridden. */
|
|
12
|
+
export function emitSpliceModule(plan, source, target, options = {}) {
|
|
13
|
+
return emit(plan, source, target, {
|
|
14
|
+
runtimeImport: options.runtimeImport ?? "toolsplice-core",
|
|
15
|
+
});
|
|
16
|
+
}
|
package/dist/doctor.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type ServerRecord, type ToolRecord } from "./store.js";
|
|
2
|
+
export type ToolHealth = "declared" | "enriched" | "learning" | "text" | "warning";
|
|
3
|
+
export interface ToolDiagnostic {
|
|
4
|
+
name: string;
|
|
5
|
+
status: ToolHealth;
|
|
6
|
+
observations: number;
|
|
7
|
+
errors: number;
|
|
8
|
+
revision: number;
|
|
9
|
+
limited: boolean;
|
|
10
|
+
detail: string;
|
|
11
|
+
}
|
|
12
|
+
export interface DoctorReport {
|
|
13
|
+
server: string;
|
|
14
|
+
version?: string;
|
|
15
|
+
tools: ToolDiagnostic[];
|
|
16
|
+
totals: Record<ToolHealth, number>;
|
|
17
|
+
export: "not-exported" | "current" | "stale";
|
|
18
|
+
nextSteps: string[];
|
|
19
|
+
}
|
|
20
|
+
export declare function diagnoseTool(tool: ToolRecord, minSamples?: number): ToolDiagnostic;
|
|
21
|
+
export declare function diagnose(server: ServerRecord, minSamples?: number): DoctorReport;
|
|
22
|
+
export declare function formatDoctor(report: DoctorReport): string;
|
package/dist/doctor.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { exportFingerprint } from "./drift.js";
|
|
2
|
+
import { hasEmbeddedJson } from "./shape.js";
|
|
3
|
+
import { toolOutput } from "./store.js";
|
|
4
|
+
function usefulDeclaration(tool) {
|
|
5
|
+
const schema = tool.outputSchema;
|
|
6
|
+
if (!schema)
|
|
7
|
+
return false;
|
|
8
|
+
if (schema.$ref || schema.anyOf || schema.oneOf || schema.allOf)
|
|
9
|
+
return true;
|
|
10
|
+
if (schema.type && schema.type !== "object")
|
|
11
|
+
return true;
|
|
12
|
+
if (schema.additionalProperties && typeof schema.additionalProperties === "object")
|
|
13
|
+
return true;
|
|
14
|
+
const props = schema.properties;
|
|
15
|
+
if (!props || !Object.keys(props).length)
|
|
16
|
+
return false;
|
|
17
|
+
return !(Object.keys(props).length === 1 && props.content?.type === "string");
|
|
18
|
+
}
|
|
19
|
+
function hasLimits(shape) {
|
|
20
|
+
if (shape.object?.truncated || shape.array?.truncated)
|
|
21
|
+
return true;
|
|
22
|
+
if (shape.array && hasLimits(shape.array.items))
|
|
23
|
+
return true;
|
|
24
|
+
if (shape.string?.json && hasLimits(shape.string.json))
|
|
25
|
+
return true;
|
|
26
|
+
return shape.object ? Object.values(shape.object.props).some(hasLimits) : false;
|
|
27
|
+
}
|
|
28
|
+
export function diagnoseTool(tool, minSamples = 1) {
|
|
29
|
+
const observations = tool.shape.count + tool.kinds.text;
|
|
30
|
+
const result = { name: tool.name, status: "learning", observations, errors: tool.kinds.error, revision: tool.drift?.revision ?? 0, limited: hasLimits(tool.shape), detail: "No successful observations yet; use this tool normally to learn its return type." };
|
|
31
|
+
const out = toolOutput(tool);
|
|
32
|
+
if (tool.schemaValidation?.latest === "mismatch") {
|
|
33
|
+
return { ...result, status: "warning", detail: `Observed output does not match the upstream structural schema at ${tool.schemaValidation.paths.join(", ")}. Results are still forwarded unchanged.` };
|
|
34
|
+
}
|
|
35
|
+
if (tool.schemaValidation?.latest === "unverified") {
|
|
36
|
+
return { ...result, status: "warning", detail: "The declared schema could not be structurally checked within diagnostic limits (for example, an unsupported reference). This is not evidence of an invalid tool result." };
|
|
37
|
+
}
|
|
38
|
+
if (out?.warning && !hasEmbeddedJson(tool.shape)) {
|
|
39
|
+
return { ...result, status: "warning", detail: out.warning };
|
|
40
|
+
}
|
|
41
|
+
if (usefulDeclaration(tool)) {
|
|
42
|
+
return { ...result, status: "declared", detail: observations ? "Upstream provides an output schema; observed outputs are recorded." : "Upstream declares an output schema; delivery has not been observed yet." };
|
|
43
|
+
}
|
|
44
|
+
if (observations < minSamples) {
|
|
45
|
+
if (observations)
|
|
46
|
+
result.detail = `${observations}/${minSamples} observations; use this tool more to enable enrichment.`;
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
const wrapper = tool.outputSchema?.properties;
|
|
50
|
+
if ((tool.shape.count === 0 && tool.kinds.text > 0) || (wrapper?.content?.type === "string" && !hasEmbeddedJson(tool.shape))) {
|
|
51
|
+
return { ...result, status: "text", detail: "Returns text with no inferred inner structure; values are passed through unchanged." };
|
|
52
|
+
}
|
|
53
|
+
return { ...result, status: "enriched", detail: hasEmbeddedJson(tool.shape) ? "Learned JSON hidden inside a string; parsing instructions can be added to the description." : "Learned return type is ready for description enrichment and TypeScript export." };
|
|
54
|
+
}
|
|
55
|
+
export function diagnose(server, minSamples = 1) {
|
|
56
|
+
const tools = Object.values(server.tools).sort((a, b) => a.name.localeCompare(b.name)).map((t) => diagnoseTool(t, minSamples));
|
|
57
|
+
const totals = { declared: 0, enriched: 0, learning: 0, text: 0, warning: 0 };
|
|
58
|
+
tools.forEach((t) => totals[t.status]++);
|
|
59
|
+
const exportStatus = !server.exportedFingerprint ? "not-exported" : server.exportedFingerprint === exportFingerprint(server) ? "current" : "stale";
|
|
60
|
+
const nextSteps = [];
|
|
61
|
+
if (totals.learning)
|
|
62
|
+
nextSteps.push("Use the unobserved tools in your agent; the proxy learns without making extra calls.");
|
|
63
|
+
if (totals.enriched)
|
|
64
|
+
nextSteps.push("Keep the proxy enabled; changed descriptions are announced with tools/list_changed. Clients that cache tools may need a reconnect.");
|
|
65
|
+
if (tools.some((t) => t.limited))
|
|
66
|
+
nextSteps.push("Some outputs exceeded sampling or retained-shape limits; affected branches remain unknown rather than claiming an exhaustive type.");
|
|
67
|
+
if (totals.text)
|
|
68
|
+
nextSteps.push("Text-only tools still work; toolsplice does not invent a parser for arbitrary prose.");
|
|
69
|
+
if (totals.warning)
|
|
70
|
+
nextSteps.push("Check tools marked warning: upstream schema declarations do not match observed output encodings.");
|
|
71
|
+
if (exportStatus === "stale")
|
|
72
|
+
nextSteps.push("Output shapes or definitions changed since emit-ts. Regenerate the exported types.");
|
|
73
|
+
return { server: server.name, version: server.version, tools, totals, export: exportStatus, nextSteps };
|
|
74
|
+
}
|
|
75
|
+
export function formatDoctor(report) {
|
|
76
|
+
const { totals: t } = report;
|
|
77
|
+
const lines = [
|
|
78
|
+
`${report.server}${report.version ? ` v${report.version}` : ""}: ${report.tools.length} tools`,
|
|
79
|
+
` ${t.declared} already have useful output schemas`,
|
|
80
|
+
` ${t.enriched} learned return types ready`,
|
|
81
|
+
` ${t.learning} need observations`,
|
|
82
|
+
` ${t.text} return text without inferred structure`,
|
|
83
|
+
` ${t.warning} have schema/encoding warnings`,
|
|
84
|
+
` TypeScript export: ${report.export}`,
|
|
85
|
+
"",
|
|
86
|
+
...report.tools.map((x) => ` ${x.name} [${x.status}; ${x.observations} observation(s)${x.errors ? `; ${x.errors} error(s)` : ""}${x.revision ? `; revision ${x.revision}` : ""}${x.limited ? "; inference limited" : ""}]\n ${x.detail}`),
|
|
87
|
+
];
|
|
88
|
+
if (report.nextSteps.length)
|
|
89
|
+
lines.push("", ...report.nextSteps.map((s) => ` → ${s}`));
|
|
90
|
+
return lines.join("\n") + "\n";
|
|
91
|
+
}
|
package/dist/drift.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Extracted } from "./extract.js";
|
|
2
|
+
import type { ServerRecord, ToolRecord } from "./store.js";
|
|
3
|
+
export interface DriftEvent {
|
|
4
|
+
at: string;
|
|
5
|
+
changes: string[];
|
|
6
|
+
reason: "observed-shape" | "tool-definition" | "server-version";
|
|
7
|
+
}
|
|
8
|
+
export interface DriftState {
|
|
9
|
+
revision: number;
|
|
10
|
+
events: DriftEvent[];
|
|
11
|
+
}
|
|
12
|
+
/** Strip examples, occurrence counts and other annotations. Stable across key order and traffic volume. */
|
|
13
|
+
export declare function contract(schema: unknown): unknown;
|
|
14
|
+
export declare function stableHash(value: unknown): string;
|
|
15
|
+
export declare function learnedContract(tool: ToolRecord): unknown;
|
|
16
|
+
export declare function exportFingerprint(server: ServerRecord): string;
|
|
17
|
+
/** Run after recordCall, using a snapshot taken before it. Only changes after the first result count. */
|
|
18
|
+
export declare function recordDrift(tool: ToolRecord, before: unknown, priorObservations: number, kind: Extracted["kind"]): DriftEvent | undefined;
|
|
19
|
+
export declare function resetLearning(tool: ToolRecord, reason: "tool-definition" | "server-version"): void;
|
|
20
|
+
export declare function definitionFingerprint(tool: {
|
|
21
|
+
inputSchema?: unknown;
|
|
22
|
+
outputSchema?: unknown;
|
|
23
|
+
}): string;
|