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.
Files changed (65) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +372 -2
  3. package/dist/cli.d.ts +2 -0
  4. package/dist/cli.js +923 -0
  5. package/dist/codegen.d.ts +44 -0
  6. package/dist/codegen.js +479 -0
  7. package/dist/contract-caller.d.ts +22 -0
  8. package/dist/contract-caller.js +307 -0
  9. package/dist/contract-corrections.d.ts +35 -0
  10. package/dist/contract-corrections.js +159 -0
  11. package/dist/contract-schema.d.ts +5 -0
  12. package/dist/contract-schema.js +126 -0
  13. package/dist/contracts.d.ts +71 -0
  14. package/dist/contracts.js +366 -0
  15. package/dist/core.d.ts +14 -0
  16. package/dist/core.js +16 -0
  17. package/dist/doctor.d.ts +22 -0
  18. package/dist/doctor.js +91 -0
  19. package/dist/drift.d.ts +23 -0
  20. package/dist/drift.js +111 -0
  21. package/dist/execution.d.ts +38 -0
  22. package/dist/execution.js +66 -0
  23. package/dist/extract.d.ts +34 -0
  24. package/dist/extract.js +73 -0
  25. package/dist/index.d.ts +20 -0
  26. package/dist/index.js +19 -0
  27. package/dist/ordered-transport.d.ts +12 -0
  28. package/dist/ordered-transport.js +106 -0
  29. package/dist/pattern.d.ts +2 -0
  30. package/dist/pattern.js +32 -0
  31. package/dist/postgres.d.ts +41 -0
  32. package/dist/postgres.js +264 -0
  33. package/dist/probe.d.ts +29 -0
  34. package/dist/probe.js +134 -0
  35. package/dist/proxy.d.ts +40 -0
  36. package/dist/proxy.js +436 -0
  37. package/dist/runtime.d.ts +6 -0
  38. package/dist/runtime.js +26 -0
  39. package/dist/schema.d.ts +17 -0
  40. package/dist/schema.js +299 -0
  41. package/dist/shape.d.ts +91 -0
  42. package/dist/shape.js +499 -0
  43. package/dist/splice-diagnostics.d.ts +40 -0
  44. package/dist/splice-diagnostics.js +80 -0
  45. package/dist/splice-project.d.ts +119 -0
  46. package/dist/splice-project.js +296 -0
  47. package/dist/splice-recording.d.ts +105 -0
  48. package/dist/splice-recording.js +464 -0
  49. package/dist/splice.d.ts +84 -0
  50. package/dist/splice.js +1065 -0
  51. package/dist/storage.d.ts +17 -0
  52. package/dist/storage.js +1 -0
  53. package/dist/store.d.ts +151 -0
  54. package/dist/store.js +735 -0
  55. package/dist/v2.d.ts +49 -0
  56. package/dist/v2.js +396 -0
  57. package/dist/validate-output.d.ts +3 -0
  58. package/dist/validate-output.js +73 -0
  59. package/dist/version.d.ts +1 -0
  60. package/dist/version.js +1 -0
  61. package/dist/workflow-json.d.ts +8 -0
  62. package/dist/workflow-json.js +74 -0
  63. package/docs/PROJECTS-AND-UPGRADES.md +115 -0
  64. package/docs/RECORDING-AND-CORRECTIONS.md +176 -0
  65. package/package.json +95 -4
package/dist/schema.js ADDED
@@ -0,0 +1,299 @@
1
+ // Turns a learned Shape into JSON Schema (2020-12).
2
+ // "docs" mode includes observed hints; "lenient" relaxes presence, nullability and numeric
3
+ // constraints. Both preserve observed types, so future type changes can still fail validation.
4
+ import { DEFAULT_OPTIONS, emptyShape, mergeInto } from "./shape.js";
5
+ /** Minimum string observations before values that repeat are reported as likely enum members. */
6
+ const ENUM_MIN_COUNT = 4;
7
+ const ENUM_MAX_VALUES = 12;
8
+ const UNIX_SECONDS = [946_684_800, 4_102_444_800]; // 2000-01-01 .. 2100-01-01
9
+ const UNIX_MILLIS = [UNIX_SECONDS[0] * 1000, UNIX_SECONDS[1] * 1000];
10
+ export function toJsonSchema(shape, options = { mode: "docs" }) {
11
+ const ctx = { defs: {}, remaining: 10_000, depth: 0, active: new WeakSet() };
12
+ const schema = buildSchema(shape, options, ctx);
13
+ if (options.mode === "lenient")
14
+ return loosen(schema, true);
15
+ return Object.keys(ctx.defs).length ? { ...schema, $defs: ctx.defs } : schema;
16
+ }
17
+ /**
18
+ * Lenient schemas relax common variations: nested values may be null, integers may be
19
+ * fractional, and string formats are not enforced. They do not predict every future type.
20
+ */
21
+ function loosen(schema, root) {
22
+ const out = { ...schema };
23
+ delete out.format;
24
+ if (out.type === "integer")
25
+ out.type = "number";
26
+ if (Array.isArray(out.type))
27
+ out.type = [...new Set(out.type.map((t) => (t === "integer" ? "number" : t)))];
28
+ if (out.properties) {
29
+ out.properties = Object.fromEntries(Object.entries(out.properties).map(([k, v]) => [k, loosen(v, false)]));
30
+ }
31
+ if (out.items)
32
+ out.items = loosen(out.items, false);
33
+ if (out.additionalProperties && typeof out.additionalProperties === "object") {
34
+ out.additionalProperties = loosen(out.additionalProperties, false);
35
+ }
36
+ if (Array.isArray(out.anyOf))
37
+ out.anyOf = out.anyOf.map((v) => loosen(v, true));
38
+ return root ? out : nullable(out);
39
+ }
40
+ function nullable(schema) {
41
+ if (Object.keys(schema).length === 0)
42
+ return schema;
43
+ if (typeof schema.type === "string")
44
+ return schema.type === "null" ? schema : { ...schema, type: [schema.type, "null"] };
45
+ if (Array.isArray(schema.type))
46
+ return schema.type.includes("null") ? schema : { ...schema, type: [...schema.type, "null"] };
47
+ if (Array.isArray(schema.anyOf)) {
48
+ const anyOf = schema.anyOf;
49
+ return anyOf.some((v) => v.type === "null") ? schema : { ...schema, anyOf: [...anyOf, { type: "null" }] };
50
+ }
51
+ return schema;
52
+ }
53
+ function buildSchema(shape, options, ctx) {
54
+ if (ctx.remaining-- <= 0 || ctx.depth >= 64 || ctx.active.has(shape))
55
+ return {};
56
+ ctx.depth++;
57
+ ctx.active.add(shape);
58
+ try {
59
+ return buildSchemaBody(shape, options, ctx);
60
+ }
61
+ finally {
62
+ ctx.depth--;
63
+ ctx.active.delete(shape);
64
+ }
65
+ }
66
+ function buildSchemaBody(shape, options, ctx) {
67
+ if (shape.count === 0)
68
+ return {};
69
+ // Never seen with a value: saying "always null" would mislead the reader and reject real values.
70
+ if (shape.null === shape.count)
71
+ return options.mode === "docs" ? { "x-hint": "only seen as null" } : {};
72
+ const variants = [];
73
+ if (shape.null > 0)
74
+ variants.push({ type: "null" });
75
+ if (shape.boolean > 0)
76
+ variants.push({ type: "boolean" });
77
+ if (shape.number)
78
+ variants.push(numberSchema(shape, options));
79
+ if (shape.string)
80
+ variants.push(stringSchema(shape, options, ctx));
81
+ if (shape.array)
82
+ variants.push(arraySchema(shape, options, ctx));
83
+ if (shape.object)
84
+ variants.push(objectSchema(shape, options, ctx));
85
+ const accounted = shape.null + shape.boolean + (shape.number?.count ?? 0) + (shape.string?.count ?? 0) + (shape.array?.count ?? 0) + (shape.object?.count ?? 0);
86
+ // Depth limits and non-JSON values carry observations without a known type.
87
+ if (accounted < shape.count || variants.length === 0)
88
+ return {};
89
+ if (variants.length === 1)
90
+ return variants[0];
91
+ const simple = variants.every((v) => Object.keys(v).length === 1 && typeof v.type === "string");
92
+ if (simple)
93
+ return { type: variants.map((v) => v.type) };
94
+ return { anyOf: variants };
95
+ }
96
+ function numberSchema(shape, options) {
97
+ const n = shape.number;
98
+ const isInt = n.integers === n.count;
99
+ const schema = { type: isInt ? "integer" : "number" };
100
+ if (options.mode === "lenient")
101
+ return schema;
102
+ if (isInt && n.min >= UNIX_SECONDS[0] && n.max <= UNIX_SECONDS[1])
103
+ schema["x-hint"] = "unix-seconds";
104
+ else if (isInt && n.min >= UNIX_MILLIS[0] && n.max <= UNIX_MILLIS[1])
105
+ schema["x-hint"] = "unix-millis";
106
+ if (n.redacted)
107
+ schema["x-redacted"] = true;
108
+ else if (n.examples.length)
109
+ schema.examples = n.examples;
110
+ return schema;
111
+ }
112
+ function stringSchema(shape, options, ctx) {
113
+ const s = shape.string;
114
+ const schema = { type: "string" };
115
+ // JSON Schema's own way of saying "this string holds a JSON document shaped like X".
116
+ if (s.json && s.jsonCount === s.count) {
117
+ if (options.mode === "lenient")
118
+ return schema;
119
+ return { ...schema, contentMediaType: "application/json", contentSchema: buildSchema(s.json, options, ctx) };
120
+ }
121
+ const format = dominantFormat(s.formats, s.count);
122
+ if (format)
123
+ schema.format = format;
124
+ if (options.mode === "lenient")
125
+ return schema;
126
+ const observed = observedValues(shape);
127
+ if (observed)
128
+ schema["x-observed-values"] = observed;
129
+ else if (s.examples.length && !format)
130
+ schema.examples = s.examples;
131
+ if (s.redacted)
132
+ schema["x-redacted"] = true;
133
+ return schema;
134
+ }
135
+ /** Values that repeat often enough to look like a fixed set (status, kind, priority...). */
136
+ export function observedValues(shape) {
137
+ const s = shape.string;
138
+ if (!s || !s.values || s.count < ENUM_MIN_COUNT)
139
+ return undefined;
140
+ const values = Object.keys(s.values);
141
+ if (values.length === 0 || values.length > ENUM_MAX_VALUES)
142
+ return undefined;
143
+ // Require repetition: unique-per-row strings (names, titles) are not an enum.
144
+ if (values.length > s.count / 2)
145
+ return undefined;
146
+ return values.sort((a, b) => s.values[b] - s.values[a]);
147
+ }
148
+ function dominantFormat(formats, count) {
149
+ for (const [format, n] of Object.entries(formats)) {
150
+ if (n === count)
151
+ return format;
152
+ }
153
+ return undefined;
154
+ }
155
+ function arraySchema(shape, options, ctx) {
156
+ const a = shape.array;
157
+ if (a.redacted)
158
+ return options.mode === "docs" ? { type: "array", items: {}, "x-redacted": true } : { type: "array", items: {} };
159
+ const schema = { type: "array" };
160
+ if (a.truncated) {
161
+ schema.items = {};
162
+ if (options.mode === "docs") {
163
+ schema["x-sampled"] = true;
164
+ schema["x-hint"] = "items sampled; additional item types unknown";
165
+ if (a.items.count > 0)
166
+ schema["x-observed-items"] = buildSchema(a.items, options, ctx);
167
+ }
168
+ }
169
+ else if (a.items.count > 0)
170
+ schema.items = buildSchema(a.items, options, ctx);
171
+ else if (options.mode === "docs")
172
+ schema["x-hint"] = "always empty when observed";
173
+ return schema;
174
+ }
175
+ function objectSchema(shape, options, ctx) {
176
+ const obj = shape.object;
177
+ if (obj.redacted)
178
+ return options.mode === "docs" ? { type: "object", additionalProperties: {}, "x-redacted": true } : { type: "object", additionalProperties: {} };
179
+ const keys = Object.keys(obj.props);
180
+ if (obj.truncated || keys.length > 10_000) {
181
+ return options.mode === "docs" ? { type: "object", additionalProperties: {}, "x-hint": "keys sampled; additional value types unknown" } : { type: "object", additionalProperties: {} };
182
+ }
183
+ if (looksLikeMap(shape)) {
184
+ const merged = emptyShape();
185
+ for (const key of keys)
186
+ mergeInto(merged, obj.props[key], { ...DEFAULT_OPTIONS, examples: "all" });
187
+ const schema = { type: "object", additionalProperties: buildSchema(merged, options, ctx) };
188
+ if (options.mode === "docs")
189
+ schema["x-hint"] = `map keyed by ${describeKeys(keys)}`;
190
+ return schema;
191
+ }
192
+ // Trees (file trees, comment threads, nested categories) become one recursive type instead of
193
+ // a different type per observed depth. Only in docs mode: lenient schemas stay ref-free.
194
+ const recursiveKey = options.mode === "docs" ? findRecursiveKey(shape) : undefined;
195
+ if (recursiveKey !== undefined) {
196
+ const name = uniqueDefName(ctx, "Node");
197
+ const ref = { $ref: `#/$defs/${name}` };
198
+ ctx.defs[name] = {};
199
+ ctx.defs[name] = objectBody(foldTree(shape, recursiveKey), options, ctx, { key: recursiveKey, ref });
200
+ return ref;
201
+ }
202
+ return objectBody(shape, options, ctx);
203
+ }
204
+ function objectBody(shape, options, ctx, recursion) {
205
+ const obj = shape.object;
206
+ const keys = Object.keys(obj.props);
207
+ const properties = Object.create(null);
208
+ const required = [];
209
+ for (const key of keys) {
210
+ const prop = obj.props[key];
211
+ const child = recursion?.key === key ? recursiveProperty(prop, options, ctx, recursion.ref) : buildSchema(prop, options, ctx);
212
+ const presence = prop.count / obj.count;
213
+ if (presence >= 1)
214
+ required.push(key);
215
+ else if (options.mode === "docs")
216
+ child["x-presence"] = Math.round(presence * 100) / 100;
217
+ properties[key] = child;
218
+ }
219
+ const schema = { type: "object", properties };
220
+ if (options.mode === "docs" && required.length)
221
+ schema.required = required;
222
+ return schema;
223
+ }
224
+ /**
225
+ * The key whose array items look like the parent itself: every key of the items also appears on
226
+ * the parent, including the key itself (so the nesting continues at least one more level).
227
+ */
228
+ export function findRecursiveKey(shape) {
229
+ const obj = shape.object;
230
+ if (!obj)
231
+ return undefined;
232
+ for (const [key, prop] of Object.entries(obj.props)) {
233
+ const items = prop.array?.items.object;
234
+ if (!items || prop.object || prop.string)
235
+ continue;
236
+ const itemKeys = Object.keys(items.props);
237
+ if (itemKeys.length >= 2 && Object.hasOwn(items.props, key) && itemKeys.every((k) => Object.hasOwn(obj.props, k)))
238
+ return key;
239
+ }
240
+ return undefined;
241
+ }
242
+ /** Merges every level of a tree into one node shape, so the node's stats cover all depths. */
243
+ function foldTree(shape, key) {
244
+ const node = mergeInto(emptyShape(), shape, { ...DEFAULT_OPTIONS, examples: "all" });
245
+ let level = shape.object.props[key].array.items;
246
+ for (let depth = 0; level.object && depth < 64; depth++) {
247
+ mergeInto(node, level, { ...DEFAULT_OPTIONS, examples: "all" });
248
+ level = level.object.props[key]?.array?.items ?? emptyShape();
249
+ }
250
+ return node;
251
+ }
252
+ /** Preserve scalar/null alternatives on the edge and heterogeneous array items while folding. */
253
+ function recursiveProperty(prop, options, ctx, ref) {
254
+ const items = prop.array.items;
255
+ const restItems = { ...items, object: undefined, count: items.count - (items.object?.count ?? 0) };
256
+ const observedItems = restItems.count > 0
257
+ ? { anyOf: [ref, buildSchema(restItems, options, ctx)] }
258
+ : ref;
259
+ const array = { type: "array", items: prop.array.truncated ? {} : observedItems };
260
+ if (prop.array.truncated && options.mode === "docs") {
261
+ array["x-sampled"] = true;
262
+ array["x-observed-items"] = observedItems;
263
+ }
264
+ const rest = { ...prop, array: undefined, count: prop.count - prop.array.count };
265
+ return rest.count > 0 ? { anyOf: [buildSchema(rest, options, ctx), array] } : array;
266
+ }
267
+ function uniqueDefName(ctx, base) {
268
+ let name = base;
269
+ for (let i = 2; Object.hasOwn(ctx.defs, name); i++)
270
+ name = `${base}${i}`;
271
+ return name;
272
+ }
273
+ const ID_LIKE = /^(\d+|[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}|\d{4}-\d{2}-\d{2}.*|[0-9a-f]{24,})$/i;
274
+ /** Objects used as dictionaries (keyed by id or date) should be typed as maps, not as hundreds of fields. */
275
+ export function looksLikeMap(shape) {
276
+ const obj = shape.object;
277
+ if (!obj)
278
+ return false;
279
+ if (obj.truncated)
280
+ return true;
281
+ const keys = Object.keys(obj.props);
282
+ if (keys.length >= 3 && keys.every((k) => ID_LIKE.test(k)))
283
+ return true;
284
+ // Many keys that each show up in only a few objects: the keys are data, not field names.
285
+ if (keys.length >= 40) {
286
+ const rare = keys.filter((k) => obj.props[k].count / obj.count < 0.2).length;
287
+ return rare / keys.length > 0.8;
288
+ }
289
+ return false;
290
+ }
291
+ function describeKeys(keys) {
292
+ if (keys.every((k) => /^\d+$/.test(k)))
293
+ return "numeric id";
294
+ if (keys.every((k) => /^\d{4}-\d{2}-\d{2}/.test(k)))
295
+ return "date";
296
+ if (keys.every((k) => /^[0-9a-f]{8}-/i.test(k)))
297
+ return "uuid";
298
+ return "string";
299
+ }
@@ -0,0 +1,91 @@
1
+ export type StringFormat = "date-time" | "date" | "email" | "uri" | "uuid";
2
+ export interface StringStats {
3
+ count: number;
4
+ /** Distinct values and how often each was seen; null once there were too many to track. */
5
+ values: Record<string, number> | null;
6
+ formats: Partial<Record<StringFormat, number>>;
7
+ minLength: number;
8
+ maxLength: number;
9
+ examples: string[];
10
+ /** Values were not recorded because the key looked like a secret. */
11
+ redacted?: boolean;
12
+ /** How many of the strings were themselves JSON objects or arrays. */
13
+ jsonCount?: number;
14
+ /** Shape of the JSON found inside the strings. */
15
+ json?: Shape;
16
+ }
17
+ export interface NumberStats {
18
+ count: number;
19
+ integers: number;
20
+ min: number;
21
+ max: number;
22
+ examples: number[];
23
+ /** Numeric values under sensitive keys must not survive in bounds or examples. */
24
+ redacted?: boolean;
25
+ }
26
+ export interface ArrayStats {
27
+ count: number;
28
+ items: Shape;
29
+ minItems: number;
30
+ maxItems: number;
31
+ /** Some elements were not observed; their types are unknown. */
32
+ truncated?: boolean;
33
+ /** Known-sensitive containers retain no child structure. */
34
+ redacted?: boolean;
35
+ }
36
+ export interface ObjectStats {
37
+ count: number;
38
+ props: Record<string, Shape>;
39
+ /** Stopped tracking new keys after hitting the limit; the object is probably a map. */
40
+ truncated?: boolean;
41
+ /** Known-sensitive containers retain no child structure. */
42
+ redacted?: boolean;
43
+ }
44
+ export interface Shape {
45
+ count: number;
46
+ null: number;
47
+ boolean: number;
48
+ number?: NumberStats;
49
+ string?: StringStats;
50
+ array?: ArrayStats;
51
+ object?: ObjectStats;
52
+ }
53
+ /**
54
+ * Which real values may be stored as examples, and strings as enum candidates.
55
+ * safe: a heuristic for short token-like values (statuses, kinds, dates); it cannot guarantee privacy.
56
+ * all: anything short enough. none: no examples or enum candidates.
57
+ * Keys and statistics (including numeric bounds) remain; this is not anonymization.
58
+ */
59
+ export type ExamplePolicy = "safe" | "all" | "none";
60
+ export interface ObserveOptions {
61
+ examples: ExamplePolicy;
62
+ maxDistinct: number;
63
+ maxExamples: number;
64
+ maxExampleLength: number;
65
+ maxArraySample: number;
66
+ maxDepth: number;
67
+ maxProps: number;
68
+ /** Per-observation node budget, defaults to 10,000 (hard ceiling 100,000). */
69
+ maxNodes?: number;
70
+ /** Maximum retained Shape nodes across calls/merges, including the root (minimum 1). */
71
+ maxRetainedNodes?: number;
72
+ /** Largest string we'll try to parse as embedded JSON. */
73
+ maxJsonStringLength: number;
74
+ }
75
+ export declare const DEFAULT_OPTIONS: ObserveOptions;
76
+ /** Short values that look like enum members or dates rather than personal data, paths or secrets. */
77
+ export declare function isSafeValue(value: string): boolean;
78
+ /** Keys like `apiKey`, `access_token`, `X-Authorization` suppress examples and enum candidates. */
79
+ export declare function isSensitiveKey(key: string): boolean;
80
+ export declare function emptyShape(): Shape;
81
+ export declare function observe(shape: Shape, value: unknown, options?: ObserveOptions): Shape;
82
+ /** Combines two shapes as if every value behind `source` had been observed into `target`. */
83
+ export declare function mergeInto(target: Shape, source: Shape, o?: ObserveOptions): Shape;
84
+ /** True if some string field consistently holds a JSON document (every value seen there was JSON). */
85
+ export declare function hasEmbeddedJson(shape: Shape): boolean;
86
+ export declare function shapeFromValues(values: unknown[], options?: ObserveOptions): Shape;
87
+ /**
88
+ * Reapply an examples policy in place to a persisted shape, without observing values or
89
+ * changing counts. Field names and nonsensitive numeric bounds remain; this is not PII removal.
90
+ */
91
+ export declare function applyShapePolicy(shape: Shape, options?: ObserveOptions): Shape;