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
package/dist/shape.js
ADDED
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
// A Shape accumulates statistics about every value seen at one position in a tool's output.
|
|
2
|
+
// Shapes are plain JSON so they can be stored, merged across sessions and shared.
|
|
3
|
+
export const DEFAULT_OPTIONS = {
|
|
4
|
+
examples: "none",
|
|
5
|
+
maxDistinct: 20,
|
|
6
|
+
maxExamples: 3,
|
|
7
|
+
maxExampleLength: 80,
|
|
8
|
+
maxArraySample: 100,
|
|
9
|
+
maxDepth: 24,
|
|
10
|
+
maxProps: 300,
|
|
11
|
+
maxNodes: 10_000,
|
|
12
|
+
maxRetainedNodes: 10_000,
|
|
13
|
+
maxJsonStringLength: 1_000_000,
|
|
14
|
+
};
|
|
15
|
+
/** Short values that look like enum members or dates rather than personal data, paths or secrets. */
|
|
16
|
+
export function isSafeValue(value) {
|
|
17
|
+
if (value.length > 40)
|
|
18
|
+
return false;
|
|
19
|
+
if (/[/\\@]/.test(value))
|
|
20
|
+
return false; // paths, URLs, emails
|
|
21
|
+
if (/\d{6,}/.test(value))
|
|
22
|
+
return false; // phone, account and id numbers
|
|
23
|
+
if (/[0-9a-f]{8}-[0-9a-f]{4}/i.test(value))
|
|
24
|
+
return false; // uuids
|
|
25
|
+
if (/[A-Za-z0-9+_=-]{20,}/.test(value))
|
|
26
|
+
return false; // tokens, hashes, base64
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
function mayStore(value, o) {
|
|
30
|
+
if (o.examples === "none")
|
|
31
|
+
return false;
|
|
32
|
+
if (o.examples === "all")
|
|
33
|
+
return value.length <= o.maxExampleLength;
|
|
34
|
+
return value.length <= o.maxExampleLength && isSafeValue(value);
|
|
35
|
+
}
|
|
36
|
+
const SENSITIVE_SUFFIXES = [
|
|
37
|
+
"password", "passwd", "secret", "token", "apikey", "privatekey", "authorization", "credential", "credentials",
|
|
38
|
+
"cookie", "sessionid", "ssn", "creditcard", "cardnumber", "cvv",
|
|
39
|
+
];
|
|
40
|
+
/** Keys like `apiKey`, `access_token`, `X-Authorization` suppress examples and enum candidates. */
|
|
41
|
+
export function isSensitiveKey(key) {
|
|
42
|
+
const k = key.toLowerCase().replace(/[-_\s]/g, "");
|
|
43
|
+
return SENSITIVE_SUFFIXES.some((suffix) => k.endsWith(suffix));
|
|
44
|
+
}
|
|
45
|
+
const FORMAT_TESTS = [
|
|
46
|
+
["date-time", /^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:?\d{2})?$/],
|
|
47
|
+
["date", /^\d{4}-\d{2}-\d{2}$/],
|
|
48
|
+
["uuid", /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i],
|
|
49
|
+
["email", /^[^\s@]+@[^\s@]+\.[^\s@]+$/],
|
|
50
|
+
["uri", /^[a-z][a-z0-9+.-]*:\/\/[^\s]+$/i],
|
|
51
|
+
];
|
|
52
|
+
export function emptyShape() {
|
|
53
|
+
return { count: 0, null: 0, boolean: 0 };
|
|
54
|
+
}
|
|
55
|
+
export function observe(shape, value, options = DEFAULT_OPTIONS) {
|
|
56
|
+
const retainedRemaining = prepareRetainedBudget(shape, options);
|
|
57
|
+
const remaining = Math.min(100_000, Math.max(0, Math.floor(options.maxNodes ?? 10_000))) || 0;
|
|
58
|
+
observeAt(shape, value, options, 0, false, { remaining, retainedRemaining });
|
|
59
|
+
return shape;
|
|
60
|
+
}
|
|
61
|
+
function observeAt(shape, value, o, depth, sensitive, budget) {
|
|
62
|
+
shape.count++;
|
|
63
|
+
if (budget.remaining-- <= 0)
|
|
64
|
+
return;
|
|
65
|
+
if (value === null || value === undefined) {
|
|
66
|
+
shape.null++;
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
switch (typeof value) {
|
|
70
|
+
case "boolean":
|
|
71
|
+
shape.boolean++;
|
|
72
|
+
return;
|
|
73
|
+
case "number":
|
|
74
|
+
observeNumber(shape, value, o, sensitive);
|
|
75
|
+
return;
|
|
76
|
+
case "string":
|
|
77
|
+
observeString(shape, value, o, depth, sensitive, budget);
|
|
78
|
+
return;
|
|
79
|
+
case "object":
|
|
80
|
+
if (depth >= Math.min(o.maxDepth, 64))
|
|
81
|
+
return;
|
|
82
|
+
if (Array.isArray(value))
|
|
83
|
+
observeArray(shape, value, o, depth, sensitive, budget);
|
|
84
|
+
else
|
|
85
|
+
observeObject(shape, value, o, depth, sensitive, budget);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function observeNumber(shape, value, o, sensitive) {
|
|
90
|
+
if (!Number.isFinite(value))
|
|
91
|
+
return;
|
|
92
|
+
const n = (shape.number ??= { count: 0, integers: 0, min: value, max: value, examples: [] });
|
|
93
|
+
n.count++;
|
|
94
|
+
if (Number.isInteger(value))
|
|
95
|
+
n.integers++;
|
|
96
|
+
if (sensitive)
|
|
97
|
+
n.redacted = true;
|
|
98
|
+
n.min = n.redacted ? 0 : Math.min(n.min, value);
|
|
99
|
+
n.max = n.redacted ? 0 : Math.max(n.max, value);
|
|
100
|
+
if (n.redacted || o.examples === "none")
|
|
101
|
+
n.examples = [];
|
|
102
|
+
else if (n.examples.length < o.maxExamples && !n.examples.includes(value))
|
|
103
|
+
n.examples.push(value);
|
|
104
|
+
}
|
|
105
|
+
function observeString(shape, value, o, depth, sensitive, budget) {
|
|
106
|
+
const s = (shape.string ??= {
|
|
107
|
+
count: 0,
|
|
108
|
+
values: {},
|
|
109
|
+
formats: {},
|
|
110
|
+
minLength: value.length,
|
|
111
|
+
maxLength: value.length,
|
|
112
|
+
examples: [],
|
|
113
|
+
});
|
|
114
|
+
s.count++;
|
|
115
|
+
s.minLength = Math.min(s.minLength, value.length);
|
|
116
|
+
s.maxLength = Math.max(s.maxLength, value.length);
|
|
117
|
+
// Servers often put a JSON document inside a string field (`{ content: "[{...}]" }`).
|
|
118
|
+
if (sensitive || o.examples === "none") {
|
|
119
|
+
s.values = null;
|
|
120
|
+
s.examples = [];
|
|
121
|
+
}
|
|
122
|
+
if (sensitive || s.redacted) {
|
|
123
|
+
s.redacted = true;
|
|
124
|
+
s.values = null;
|
|
125
|
+
s.examples = [];
|
|
126
|
+
delete s.json;
|
|
127
|
+
delete s.jsonCount;
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const embedded = depth < Math.min(o.maxDepth, 64) && budget.remaining > 0 ? parseEmbeddedJson(value, o) : undefined;
|
|
131
|
+
if (embedded !== undefined) {
|
|
132
|
+
s.values = null;
|
|
133
|
+
const json = s.json ?? allocateShape(budget);
|
|
134
|
+
if (json) {
|
|
135
|
+
s.json = json;
|
|
136
|
+
s.jsonCount = (s.jsonCount ?? 0) + 1;
|
|
137
|
+
observeAt(json, embedded, o, depth + 1, sensitive, budget);
|
|
138
|
+
}
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
for (const [format, test] of FORMAT_TESTS) {
|
|
142
|
+
if (test.test(value)) {
|
|
143
|
+
s.formats[format] = (s.formats[format] ?? 0) + 1;
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (sensitive) {
|
|
148
|
+
s.redacted = true;
|
|
149
|
+
s.values = null;
|
|
150
|
+
s.examples = [];
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const storable = mayStore(value, o);
|
|
154
|
+
if (s.values) {
|
|
155
|
+
// One value we may not store means this isn't a small fixed set of tokens.
|
|
156
|
+
if (storable && (Object.hasOwn(s.values, value) || Object.keys(s.values).length < o.maxDistinct)) {
|
|
157
|
+
setOwn(s.values, value, (Object.hasOwn(s.values, value) ? s.values[value] : 0) + 1);
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
s.values = null;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (storable && s.examples.length < o.maxExamples && !s.examples.includes(value))
|
|
164
|
+
s.examples.push(value);
|
|
165
|
+
}
|
|
166
|
+
function parseEmbeddedJson(value, o) {
|
|
167
|
+
if (value.length < 2 || value.length > o.maxJsonStringLength)
|
|
168
|
+
return undefined;
|
|
169
|
+
const first = value.trimStart()[0];
|
|
170
|
+
if (first !== "{" && first !== "[")
|
|
171
|
+
return undefined;
|
|
172
|
+
try {
|
|
173
|
+
const parsed = JSON.parse(value);
|
|
174
|
+
return typeof parsed === "object" && parsed !== null ? parsed : undefined;
|
|
175
|
+
}
|
|
176
|
+
catch {
|
|
177
|
+
return undefined;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
function observeArray(shape, value, o, depth, sensitive, budget) {
|
|
181
|
+
let a = shape.array;
|
|
182
|
+
if (!a) {
|
|
183
|
+
const items = allocateShape(budget);
|
|
184
|
+
// The unaccounted observation makes this position unknown instead of narrowing its type.
|
|
185
|
+
if (!items)
|
|
186
|
+
return;
|
|
187
|
+
a = shape.array = { count: 0, items, minItems: value.length, maxItems: value.length };
|
|
188
|
+
}
|
|
189
|
+
a.count++;
|
|
190
|
+
a.minItems = Math.min(a.minItems, value.length);
|
|
191
|
+
a.maxItems = Math.max(a.maxItems, value.length);
|
|
192
|
+
if (sensitive || a.redacted) {
|
|
193
|
+
a.redacted = true;
|
|
194
|
+
a.truncated = true;
|
|
195
|
+
a.items = emptyShape();
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const limit = Math.min(value.length, Math.max(0, o.maxArraySample));
|
|
199
|
+
let i = 0;
|
|
200
|
+
for (; i < limit && budget.remaining > 0; i++)
|
|
201
|
+
observeAt(a.items, value[i], o, depth + 1, sensitive, budget);
|
|
202
|
+
if (i < value.length)
|
|
203
|
+
a.truncated = true;
|
|
204
|
+
}
|
|
205
|
+
function observeObject(shape, value, o, depth, sensitive, budget) {
|
|
206
|
+
const obj = (shape.object ??= { count: 0, props: {} });
|
|
207
|
+
obj.count++;
|
|
208
|
+
if (sensitive || obj.redacted) {
|
|
209
|
+
obj.redacted = true;
|
|
210
|
+
obj.truncated = true;
|
|
211
|
+
obj.props = {};
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
let tracked = Object.keys(obj.props).length;
|
|
215
|
+
for (const key in value) {
|
|
216
|
+
if (!Object.hasOwn(value, key))
|
|
217
|
+
continue;
|
|
218
|
+
if (budget.remaining <= 0) {
|
|
219
|
+
obj.truncated = true;
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
let prop = Object.hasOwn(obj.props, key) ? obj.props[key] : undefined;
|
|
223
|
+
if (!prop) {
|
|
224
|
+
if (tracked >= o.maxProps || budget.retainedRemaining <= 0) {
|
|
225
|
+
obj.truncated = true;
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
prop = allocateShape(budget);
|
|
229
|
+
tracked++;
|
|
230
|
+
setOwn(obj.props, key, prop);
|
|
231
|
+
}
|
|
232
|
+
observeAt(prop, value[key], o, depth + 1, sensitive || isSensitiveKey(key), budget);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
/** Combines two shapes as if every value behind `source` had been observed into `target`. */
|
|
236
|
+
export function mergeInto(target, source, o = DEFAULT_OPTIONS) {
|
|
237
|
+
// Reapply policy to retained target fields too, including fields absent from source.
|
|
238
|
+
const retainedRemaining = prepareRetainedBudget(target, o);
|
|
239
|
+
return mergeAt(target, source, o, { retainedRemaining });
|
|
240
|
+
}
|
|
241
|
+
function mergeAt(target, source, o, budget, sensitive = false) {
|
|
242
|
+
if (sensitive)
|
|
243
|
+
applyExamplePolicy(target, o, true);
|
|
244
|
+
target.count += source.count;
|
|
245
|
+
target.null += source.null;
|
|
246
|
+
target.boolean += source.boolean;
|
|
247
|
+
if (source.number) {
|
|
248
|
+
const s = source.number;
|
|
249
|
+
const t = (target.number ??= { count: 0, integers: 0, min: s.min, max: s.max, examples: [] });
|
|
250
|
+
t.count += s.count;
|
|
251
|
+
t.integers += s.integers;
|
|
252
|
+
if (sensitive || s.redacted)
|
|
253
|
+
t.redacted = true;
|
|
254
|
+
t.min = t.redacted ? 0 : Math.min(t.min, s.min);
|
|
255
|
+
t.max = t.redacted ? 0 : Math.max(t.max, s.max);
|
|
256
|
+
if (t.redacted || o.examples === "none")
|
|
257
|
+
t.examples = [];
|
|
258
|
+
else
|
|
259
|
+
for (const e of s.examples)
|
|
260
|
+
if (t.examples.length < o.maxExamples && !t.examples.includes(e))
|
|
261
|
+
t.examples.push(e);
|
|
262
|
+
}
|
|
263
|
+
if (source.string) {
|
|
264
|
+
const s = source.string;
|
|
265
|
+
const t = (target.string ??= { count: 0, values: {}, formats: {}, minLength: s.minLength, maxLength: s.maxLength, examples: [] });
|
|
266
|
+
t.count += s.count;
|
|
267
|
+
t.minLength = Math.min(t.minLength, s.minLength);
|
|
268
|
+
t.maxLength = Math.max(t.maxLength, s.maxLength);
|
|
269
|
+
for (const [f, n] of Object.entries(s.formats)) {
|
|
270
|
+
if (FORMAT_TESTS.some(([format]) => format === f))
|
|
271
|
+
setOwn(t.formats, f, (Object.hasOwn(t.formats, f) ? t.formats[f] : 0) + n);
|
|
272
|
+
}
|
|
273
|
+
if (sensitive || s.redacted)
|
|
274
|
+
t.redacted = true;
|
|
275
|
+
if (t.redacted) {
|
|
276
|
+
delete t.json;
|
|
277
|
+
delete t.jsonCount;
|
|
278
|
+
}
|
|
279
|
+
if (!t.redacted && s.jsonCount)
|
|
280
|
+
t.jsonCount = (t.jsonCount ?? 0) + s.jsonCount;
|
|
281
|
+
if (!t.redacted && s.json) {
|
|
282
|
+
const json = t.json ?? allocateShape(budget);
|
|
283
|
+
if (json) {
|
|
284
|
+
t.json = json;
|
|
285
|
+
mergeAt(json, s.json, o, budget);
|
|
286
|
+
}
|
|
287
|
+
else
|
|
288
|
+
delete t.jsonCount;
|
|
289
|
+
}
|
|
290
|
+
if (t.json && t.redacted)
|
|
291
|
+
applyExamplePolicy(t.json, o, true);
|
|
292
|
+
if (t.values && s.values && !t.redacted && o.examples !== "none") {
|
|
293
|
+
for (const [v, n] of Object.entries(s.values)) {
|
|
294
|
+
if (!mayStore(v, o)) {
|
|
295
|
+
t.values = null;
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
setOwn(t.values, v, (Object.hasOwn(t.values, v) ? t.values[v] : 0) + n);
|
|
299
|
+
}
|
|
300
|
+
if (t.values && Object.keys(t.values).length > o.maxDistinct)
|
|
301
|
+
t.values = null;
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
t.values = null;
|
|
305
|
+
}
|
|
306
|
+
if (t.redacted || o.examples === "none")
|
|
307
|
+
t.examples = [];
|
|
308
|
+
else
|
|
309
|
+
for (const e of s.examples.filter((v) => mayStore(v, o)))
|
|
310
|
+
if (t.examples.length < o.maxExamples && !t.examples.includes(e))
|
|
311
|
+
t.examples.push(e);
|
|
312
|
+
}
|
|
313
|
+
if (source.array) {
|
|
314
|
+
const s = source.array;
|
|
315
|
+
let t = target.array;
|
|
316
|
+
if (!t) {
|
|
317
|
+
const items = allocateShape(budget);
|
|
318
|
+
if (items)
|
|
319
|
+
t = target.array = { count: 0, items, minItems: s.minItems, maxItems: s.maxItems };
|
|
320
|
+
}
|
|
321
|
+
if (t) {
|
|
322
|
+
t.count += s.count;
|
|
323
|
+
t.minItems = Math.min(t.minItems, s.minItems);
|
|
324
|
+
t.maxItems = Math.max(t.maxItems, s.maxItems);
|
|
325
|
+
if (s.truncated)
|
|
326
|
+
t.truncated = true;
|
|
327
|
+
if (sensitive || s.redacted || t.redacted) {
|
|
328
|
+
t.redacted = true;
|
|
329
|
+
t.truncated = true;
|
|
330
|
+
t.items = emptyShape();
|
|
331
|
+
}
|
|
332
|
+
else
|
|
333
|
+
mergeAt(t.items, s.items, o, budget);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
if (source.object) {
|
|
337
|
+
const s = source.object;
|
|
338
|
+
const t = (target.object ??= { count: 0, props: {} });
|
|
339
|
+
t.count += s.count;
|
|
340
|
+
if (sensitive || s.redacted || t.redacted) {
|
|
341
|
+
t.redacted = true;
|
|
342
|
+
t.truncated = true;
|
|
343
|
+
t.props = {};
|
|
344
|
+
return target;
|
|
345
|
+
}
|
|
346
|
+
if (s.truncated)
|
|
347
|
+
t.truncated = true;
|
|
348
|
+
let tracked = Object.keys(t.props).length;
|
|
349
|
+
for (const [key, prop] of Object.entries(s.props)) {
|
|
350
|
+
const existing = Object.hasOwn(t.props, key) ? t.props[key] : undefined;
|
|
351
|
+
if (existing)
|
|
352
|
+
mergeAt(existing, prop, o, budget, isSensitiveKey(key));
|
|
353
|
+
else if (tracked < o.maxProps && budget.retainedRemaining > 0) {
|
|
354
|
+
const child = allocateShape(budget);
|
|
355
|
+
tracked++;
|
|
356
|
+
setOwn(t.props, key, mergeAt(child, prop, o, budget, isSensitiveKey(key)));
|
|
357
|
+
}
|
|
358
|
+
else
|
|
359
|
+
t.truncated = true;
|
|
360
|
+
if (isSensitiveKey(key) && Object.hasOwn(t.props, key))
|
|
361
|
+
applyExamplePolicy(t.props[key], o, true);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return target;
|
|
365
|
+
}
|
|
366
|
+
/** True if some string field consistently holds a JSON document (every value seen there was JSON). */
|
|
367
|
+
export function hasEmbeddedJson(shape) {
|
|
368
|
+
const s = shape.string;
|
|
369
|
+
if (s?.json && s.jsonCount === s.count)
|
|
370
|
+
return true;
|
|
371
|
+
if (shape.array && hasEmbeddedJson(shape.array.items))
|
|
372
|
+
return true;
|
|
373
|
+
if (shape.object)
|
|
374
|
+
return Object.values(shape.object.props).some(hasEmbeddedJson);
|
|
375
|
+
return false;
|
|
376
|
+
}
|
|
377
|
+
export function shapeFromValues(values, options = DEFAULT_OPTIONS) {
|
|
378
|
+
const shape = emptyShape();
|
|
379
|
+
for (const v of values)
|
|
380
|
+
observe(shape, v, options);
|
|
381
|
+
return shape;
|
|
382
|
+
}
|
|
383
|
+
/** Define data keys without invoking Object.prototype setters, including after JSON reload. */
|
|
384
|
+
function setOwn(target, key, value) {
|
|
385
|
+
Object.defineProperty(target, key, { value, enumerable: true, writable: true, configurable: true });
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Reapply an examples policy in place to a persisted shape, without observing values or
|
|
389
|
+
* changing counts. Field names and nonsensitive numeric bounds remain; this is not PII removal.
|
|
390
|
+
*/
|
|
391
|
+
export function applyShapePolicy(shape, options = DEFAULT_OPTIONS) {
|
|
392
|
+
applyExamplePolicy(shape, options);
|
|
393
|
+
return shape;
|
|
394
|
+
}
|
|
395
|
+
/** Reapply privacy choices when merging persisted shapes or changing policy. */
|
|
396
|
+
function applyExamplePolicy(shape, o, sensitive = false) {
|
|
397
|
+
let nodes = 1;
|
|
398
|
+
if (shape.number) {
|
|
399
|
+
const n = shape.number;
|
|
400
|
+
if (sensitive) {
|
|
401
|
+
n.redacted = true;
|
|
402
|
+
n.min = n.max = 0;
|
|
403
|
+
}
|
|
404
|
+
if (sensitive || n.redacted || o.examples === "none")
|
|
405
|
+
n.examples = [];
|
|
406
|
+
else
|
|
407
|
+
n.examples = n.examples.slice(0, o.maxExamples);
|
|
408
|
+
}
|
|
409
|
+
if (shape.string) {
|
|
410
|
+
const s = shape.string;
|
|
411
|
+
if (sensitive)
|
|
412
|
+
s.redacted = true;
|
|
413
|
+
if (s.redacted) {
|
|
414
|
+
delete s.json;
|
|
415
|
+
delete s.jsonCount;
|
|
416
|
+
}
|
|
417
|
+
if (s.redacted || o.examples === "none") {
|
|
418
|
+
s.values = null;
|
|
419
|
+
s.examples = [];
|
|
420
|
+
}
|
|
421
|
+
else {
|
|
422
|
+
s.examples = s.examples.filter((v) => mayStore(v, o)).slice(0, o.maxExamples);
|
|
423
|
+
if (s.values && Object.keys(s.values).some((v) => !mayStore(v, o)))
|
|
424
|
+
s.values = null;
|
|
425
|
+
}
|
|
426
|
+
if (s.json)
|
|
427
|
+
nodes += applyExamplePolicy(s.json, o, sensitive || !!s.redacted);
|
|
428
|
+
}
|
|
429
|
+
if (shape.array) {
|
|
430
|
+
if (sensitive || shape.array.redacted) {
|
|
431
|
+
shape.array.redacted = true;
|
|
432
|
+
shape.array.truncated = true;
|
|
433
|
+
shape.array.items = emptyShape();
|
|
434
|
+
nodes++;
|
|
435
|
+
}
|
|
436
|
+
else
|
|
437
|
+
nodes += applyExamplePolicy(shape.array.items, o);
|
|
438
|
+
}
|
|
439
|
+
if (shape.object) {
|
|
440
|
+
if (sensitive || shape.object.redacted) {
|
|
441
|
+
shape.object.redacted = true;
|
|
442
|
+
shape.object.truncated = true;
|
|
443
|
+
shape.object.props = {};
|
|
444
|
+
}
|
|
445
|
+
else
|
|
446
|
+
for (const [key, prop] of Object.entries(shape.object.props))
|
|
447
|
+
nodes += applyExamplePolicy(prop, o, isSensitiveKey(key));
|
|
448
|
+
}
|
|
449
|
+
return nodes;
|
|
450
|
+
}
|
|
451
|
+
function allocateShape(budget) {
|
|
452
|
+
if (budget.retainedRemaining <= 0)
|
|
453
|
+
return undefined;
|
|
454
|
+
budget.retainedRemaining--;
|
|
455
|
+
return emptyShape();
|
|
456
|
+
}
|
|
457
|
+
/** Count during the policy pass; old oversized records are conservatively pruned once. */
|
|
458
|
+
function prepareRetainedBudget(shape, options) {
|
|
459
|
+
const requested = options.maxRetainedNodes ?? 10_000;
|
|
460
|
+
const limit = Number.isFinite(requested) ? Math.max(1, Math.floor(requested)) : 10_000;
|
|
461
|
+
const existing = applyExamplePolicy(shape, options);
|
|
462
|
+
if (existing <= limit)
|
|
463
|
+
return limit - existing;
|
|
464
|
+
const budget = { retainedRemaining: limit - 1 };
|
|
465
|
+
pruneRetainedChildren(shape, budget);
|
|
466
|
+
return budget.retainedRemaining;
|
|
467
|
+
}
|
|
468
|
+
function pruneRetainedChildren(shape, budget) {
|
|
469
|
+
if (shape.string?.json) {
|
|
470
|
+
if (budget.retainedRemaining > 0) {
|
|
471
|
+
budget.retainedRemaining--;
|
|
472
|
+
pruneRetainedChildren(shape.string.json, budget);
|
|
473
|
+
}
|
|
474
|
+
else {
|
|
475
|
+
delete shape.string.json;
|
|
476
|
+
delete shape.string.jsonCount;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
if (shape.array) {
|
|
480
|
+
if (budget.retainedRemaining > 0) {
|
|
481
|
+
budget.retainedRemaining--;
|
|
482
|
+
pruneRetainedChildren(shape.array.items, budget);
|
|
483
|
+
}
|
|
484
|
+
else
|
|
485
|
+
delete shape.array;
|
|
486
|
+
}
|
|
487
|
+
if (shape.object) {
|
|
488
|
+
for (const key of Object.keys(shape.object.props)) {
|
|
489
|
+
if (budget.retainedRemaining > 0) {
|
|
490
|
+
budget.retainedRemaining--;
|
|
491
|
+
pruneRetainedChildren(shape.object.props[key], budget);
|
|
492
|
+
}
|
|
493
|
+
else {
|
|
494
|
+
delete shape.object.props[key];
|
|
495
|
+
shape.object.truncated = true;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { SpliceError, type SpliceSpec } from "./splice.js";
|
|
2
|
+
export interface SpliceFieldOrigin {
|
|
3
|
+
bindingIndex: number;
|
|
4
|
+
targetPointer: string;
|
|
5
|
+
origin: "source" | "context" | "literal";
|
|
6
|
+
pointer?: string;
|
|
7
|
+
selectionPointer?: string;
|
|
8
|
+
parsedJson?: boolean;
|
|
9
|
+
rowIndex?: number;
|
|
10
|
+
}
|
|
11
|
+
export interface SpliceExplanation {
|
|
12
|
+
stage: SpliceError["stage"];
|
|
13
|
+
completedTargets: number;
|
|
14
|
+
endpoint: {
|
|
15
|
+
server: string;
|
|
16
|
+
tool: string;
|
|
17
|
+
};
|
|
18
|
+
source: {
|
|
19
|
+
server: string;
|
|
20
|
+
tool: string;
|
|
21
|
+
};
|
|
22
|
+
target: {
|
|
23
|
+
server: string;
|
|
24
|
+
tool: string;
|
|
25
|
+
};
|
|
26
|
+
sourceSelection: {
|
|
27
|
+
pointer: string;
|
|
28
|
+
parsedJson: boolean;
|
|
29
|
+
forEach: boolean;
|
|
30
|
+
};
|
|
31
|
+
summary: string;
|
|
32
|
+
issues: {
|
|
33
|
+
code: string;
|
|
34
|
+
path: string;
|
|
35
|
+
fields: SpliceFieldOrigin[];
|
|
36
|
+
}[];
|
|
37
|
+
sideEffects: "no-destination-calls" | "destination-effects-may-have-occurred";
|
|
38
|
+
}
|
|
39
|
+
/** Path-only explanation. Never traverses causes or includes inputs, results or literal values. */
|
|
40
|
+
export declare function explainSpliceError(plan: SpliceSpec, error: SpliceError): SpliceExplanation;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { parseSpliceSpec, SpliceError } from "./splice.js";
|
|
2
|
+
import { pointerParts } from "./workflow-json.js";
|
|
3
|
+
const overlaps = (a, b) => {
|
|
4
|
+
const x = pointerParts(a), y = pointerParts(b);
|
|
5
|
+
return x.slice(0, Math.min(x.length, y.length)).every((p, i) => p === y[i]);
|
|
6
|
+
};
|
|
7
|
+
/** Path-only explanation. Never traverses causes or includes inputs, results or literal values. */
|
|
8
|
+
export function explainSpliceError(plan, error) {
|
|
9
|
+
const spec = parseSpliceSpec(plan);
|
|
10
|
+
if (!(error instanceof SpliceError))
|
|
11
|
+
throw new Error("Expected a SpliceError.");
|
|
12
|
+
const issues = error.issues.map((issue) => {
|
|
13
|
+
const match = /^\/rows\/(\d+)(.*)$/.exec(issue.path);
|
|
14
|
+
const rowIndex = match ? Number(match[1]) : undefined;
|
|
15
|
+
const path = match?.[2] ?? issue.path;
|
|
16
|
+
const fields = [];
|
|
17
|
+
for (const [bindingIndex, binding] of spec.bindings.entries()) {
|
|
18
|
+
const relevant = (issue.code === "source-field-missing" &&
|
|
19
|
+
"from" in binding &&
|
|
20
|
+
path === binding.from) ||
|
|
21
|
+
(issue.code === "context-field-missing" &&
|
|
22
|
+
"fromContext" in binding &&
|
|
23
|
+
path === binding.fromContext) ||
|
|
24
|
+
(error.stage === "context" &&
|
|
25
|
+
issue.code.startsWith("validation-") &&
|
|
26
|
+
"fromContext" in binding &&
|
|
27
|
+
overlaps(path, binding.fromContext)) ||
|
|
28
|
+
(error.stage === "mapping" &&
|
|
29
|
+
issue.code.startsWith("validation-") &&
|
|
30
|
+
!!match &&
|
|
31
|
+
overlaps(path, binding.to)) ||
|
|
32
|
+
(issue.code === "duplicate-target-key" &&
|
|
33
|
+
spec.target.uniqueBy?.some((key) => overlaps(key, binding.to))) ||
|
|
34
|
+
(issue.code === "unique-key-missing" &&
|
|
35
|
+
!!match &&
|
|
36
|
+
overlaps(path, binding.to)) ||
|
|
37
|
+
(error.stage === "preflight" &&
|
|
38
|
+
(issue.path === `/bindings${binding.to}` ||
|
|
39
|
+
(issue.path.startsWith("/arguments") &&
|
|
40
|
+
overlaps(issue.path.slice(10), binding.to))));
|
|
41
|
+
if (!relevant)
|
|
42
|
+
continue;
|
|
43
|
+
fields.push({
|
|
44
|
+
bindingIndex,
|
|
45
|
+
targetPointer: binding.to,
|
|
46
|
+
...("from" in binding
|
|
47
|
+
? {
|
|
48
|
+
origin: "source",
|
|
49
|
+
pointer: binding.from,
|
|
50
|
+
selectionPointer: spec.source.path ?? "",
|
|
51
|
+
parsedJson: spec.source.parseJson ?? false,
|
|
52
|
+
}
|
|
53
|
+
: "fromContext" in binding
|
|
54
|
+
? { origin: "context", pointer: binding.fromContext }
|
|
55
|
+
: { origin: "literal" }),
|
|
56
|
+
...(rowIndex === undefined ? {} : { rowIndex }),
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return { code: issue.code, path: issue.path, fields };
|
|
60
|
+
});
|
|
61
|
+
return {
|
|
62
|
+
stage: error.stage,
|
|
63
|
+
completedTargets: error.completedTargets,
|
|
64
|
+
endpoint: error.stage === "target"
|
|
65
|
+
? { server: spec.target.server, tool: spec.target.tool }
|
|
66
|
+
: { server: spec.source.server, tool: spec.source.tool },
|
|
67
|
+
source: { server: spec.source.server, tool: spec.source.tool },
|
|
68
|
+
target: { server: spec.target.server, tool: spec.target.tool },
|
|
69
|
+
sourceSelection: {
|
|
70
|
+
pointer: spec.source.path ?? "",
|
|
71
|
+
parsedJson: spec.source.parseJson ?? false,
|
|
72
|
+
forEach: spec.forEach ?? false,
|
|
73
|
+
},
|
|
74
|
+
summary: `${error.stage} validation or execution failed; ${error.completedTargets} destination result(s) were acknowledged and collected.`,
|
|
75
|
+
issues,
|
|
76
|
+
sideEffects: error.stage === "target"
|
|
77
|
+
? "destination-effects-may-have-occurred"
|
|
78
|
+
: "no-destination-calls",
|
|
79
|
+
};
|
|
80
|
+
}
|