@orkestrel/contract 0.0.10 → 0.0.12

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.
@@ -363,14 +363,21 @@ var CLONE_NODE_LIMIT = 262144;
363
363
  */
364
364
  var GENERATION_ATTEMPT_LIMIT = 32;
365
365
  /**
366
- * The default maximum object/array nesting depth {@link valueToSchema} walks,
367
- * frozen.
366
+ * The maximum object/array nesting depth {@link valueToSchema} walks, frozen.
368
367
  *
369
368
  * @remarks
370
369
  * Bounds inference against adversarial or cyclic runtime input — once the
371
370
  * remaining depth budget reaches zero, inference stops descending and emits
372
371
  * the empty accept-anything schema `{}` for that branch instead of recursing
373
- * further. Overridable per call via {@link ValueToSchemaOptions.maxDepth}.
372
+ * further. LOWERABLE per call via {@link ValueToSchemaOptions.maxDepth}; a
373
+ * higher value is held here.
374
+ *
375
+ * A ceiling rather than a default, because the walk recurses: what a deeper
376
+ * walk spends is the JavaScript call stack rather than this budget, and that
377
+ * stack is not a fixed quantity. The survivable depth measured on one host rose
378
+ * across repeated calls within a single process as the engine optimized, and
379
+ * fell to roughly this number under a reduced stack size. Any larger constant
380
+ * therefore has a host where it fails, which is why none is published.
374
381
  */
375
382
  var INFER_DEPTH_LIMIT = 32;
376
383
  /**
@@ -2471,6 +2478,61 @@ function readArrayEntries(value) {
2471
2478
  });
2472
2479
  }
2473
2480
  /**
2481
+ * Snapshot a guard shape and its optional-key mode for a shape combinator.
2482
+ *
2483
+ * @remarks
2484
+ * A null-prototype record plus its own key list is used instead of a `Map`.
2485
+ * The declared-key population decides the guard's answer, and
2486
+ * `Map.prototype.has`, `Map.prototype.get`, and map iteration are three
2487
+ * caller-writable members on that path. An own data key read by index
2488
+ * dispatches through nothing.
2489
+ *
2490
+ * @param shape - The guard shape whose own string declarations to snapshot
2491
+ * @param optional - The optional-key list, `true` for every key, or `undefined`
2492
+ * @param reader - The public combinator name used in read refusals
2493
+ * @returns The owned guards and names plus the collected optional-key membership
2494
+ * @throws {ContractError} When the shape or optional-key list cannot be read
2495
+ *
2496
+ * @example
2497
+ * ```ts
2498
+ * readGuardShape({ id: isString }, undefined, 'recordOf')
2499
+ * ```
2500
+ */
2501
+ function readGuardShape(shape, optional, reader) {
2502
+ const declared = readValue(() => {
2503
+ const members = INTRINSICS.members(shape);
2504
+ const guards = INTRINSICS.create(null);
2505
+ const names = [];
2506
+ for (let index = 0; index < members.length; index += 1) {
2507
+ const key = members[index];
2508
+ if (!isString(key)) continue;
2509
+ if (!INTRINSICS.own(guards, key)) names[names.length] = key;
2510
+ guards[key] = shape[key];
2511
+ }
2512
+ return {
2513
+ guards,
2514
+ names,
2515
+ vocabulary: collectMembers(names)
2516
+ };
2517
+ }, reader, { subject: "shape" });
2518
+ const optionalKeys = readValue(() => {
2519
+ if (optional === true) return collectMembers(declared.names);
2520
+ if (!INTRINSICS.array(optional)) return collectMembers([]);
2521
+ const entries = readArrayEntries(optional);
2522
+ if (!entries.success) throw entries.error;
2523
+ if (!entries.value.dense) throw new INTRINSICS.error("Optional key list must be dense");
2524
+ const keys = collectMembers([]);
2525
+ for (let index = 0; index < entries.value.entries.length; index += 1) admitMember(keys, INTRINSICS.text(entries.value.entries[index]));
2526
+ return keys;
2527
+ }, reader, { subject: "optional" });
2528
+ return {
2529
+ guards: declared.guards,
2530
+ names: declared.names,
2531
+ optional: optionalKeys,
2532
+ vocabulary: declared.vocabulary
2533
+ };
2534
+ }
2535
+ /**
2474
2536
  * Snapshot an object's own enumerable string keys through a total boundary.
2475
2537
  *
2476
2538
  * @remarks
@@ -3004,6 +3066,35 @@ function sanitizeBudget(value, fallback) {
3004
3066
  }, "sanitizeBudget");
3005
3067
  }
3006
3068
  /**
3069
+ * Resolve a caller's depth budget to one the traversal can actually survive.
3070
+ *
3071
+ * @remarks
3072
+ * {@link sanitizeBudget} decides the SHAPE of a budget and deliberately lets any
3073
+ * finite non-negative integer through, because it must never read a fallback a
3074
+ * hostile caller supplied. That left the depth axis unbounded from above, and
3075
+ * depth is the axis that recurses: `1e9` is a valid integer, so the walk descended
3076
+ * until the call STACK failed, at a depth that varied between runs, and the
3077
+ * refusal surfaced as an unreadable value rather than as the exhaustion the guard
3078
+ * promises. Breadth needs no such ceiling — its loop is already bounded by the
3079
+ * entries actually present.
3080
+ *
3081
+ * So {@link INFER_DEPTH_LIMIT} is the ceiling as well as the default, and
3082
+ * `maxDepth` narrows the walk rather than widening it. One bound, and the same
3083
+ * answer on every host.
3084
+ *
3085
+ * @param value - The candidate depth budget
3086
+ * @returns A finite non-negative integer no greater than `INFER_DEPTH_LIMIT`
3087
+ *
3088
+ * @example
3089
+ * ```ts
3090
+ * sanitizeDepth(4) // 4
3091
+ * sanitizeDepth(1e9) // 32
3092
+ * ```
3093
+ */
3094
+ function sanitizeDepth(value) {
3095
+ return INTRINSICS.min(sanitizeBudget(value, 32), 32);
3096
+ }
3097
+ /**
3007
3098
  * Render a short, safe, TOTAL preview of an unknown value for a {@link Fault}'s
3008
3099
  * `received` field.
3009
3100
  *
@@ -5668,32 +5759,7 @@ function mapOf(keyGuard, valueGuard) {
5668
5759
  */
5669
5760
  function recordOf(shape, optional) {
5670
5761
  return contain(() => {
5671
- const declared = readValue(() => {
5672
- const members = INTRINSICS.members(shape);
5673
- const collected = INTRINSICS.create(null);
5674
- const names = [];
5675
- for (let index = 0; index < members.length; index += 1) {
5676
- const key = members[index];
5677
- if (!isString(key)) continue;
5678
- if (!INTRINSICS.own(collected, key)) names[names.length] = key;
5679
- collected[key] = shape[key];
5680
- }
5681
- return {
5682
- collected,
5683
- names,
5684
- vocabulary: collectMembers(names)
5685
- };
5686
- }, "recordOf", { subject: "shape" });
5687
- const optionalKeys = readValue(() => {
5688
- if (optional === true) return collectMembers(declared.names);
5689
- if (!INTRINSICS.array(optional)) return collectMembers([]);
5690
- const entries = readArrayEntries(optional);
5691
- if (!entries.success) throw entries.error;
5692
- if (!entries.value.dense) throw new INTRINSICS.error("Optional key list must be dense");
5693
- const keys = collectMembers([]);
5694
- for (let index = 0; index < entries.value.entries.length; index += 1) admitMember(keys, INTRINSICS.text(entries.value.entries[index]));
5695
- return keys;
5696
- }, "recordOf", { subject: "optional" });
5762
+ const declared = readGuardShape(shape, optional, "recordOf");
5697
5763
  return (value) => holds(() => {
5698
5764
  if (!isRecord(value)) return false;
5699
5765
  const members = INTRINSICS.members(value);
@@ -5704,9 +5770,9 @@ function recordOf(shape, optional) {
5704
5770
  for (let index = 0; index < declared.names.length; index += 1) {
5705
5771
  const key = declared.names[index];
5706
5772
  if (key === void 0) continue;
5707
- const guard = declared.collected[key];
5773
+ const guard = declared.guards[key];
5708
5774
  const present = INTRINSICS.own(value, key);
5709
- if (!matchesMember(optionalKeys, key) && !present) return false;
5775
+ if (!matchesMember(declared.optional, key) && !present) return false;
5710
5776
  if (present) {
5711
5777
  if (guard === void 0 || !guard(value[key])) return false;
5712
5778
  }
@@ -5716,6 +5782,47 @@ function recordOf(shape, optional) {
5716
5782
  }, "recordOf");
5717
5783
  }
5718
5784
  /**
5785
+ * Build a guard that accepts non-array objects matching an open guard shape.
5786
+ *
5787
+ * @remarks
5788
+ * Three calling modes mirror {@link recordOf}:
5789
+ * - **No `optional`** — all shape keys required; unknown members admitted.
5790
+ * - **`optional: K[]`** — the listed keys are optional; all others required.
5791
+ * - **`optional: true`** — every shape key is optional.
5792
+ *
5793
+ * Each declared member is read through `Reflect.get`, so inherited data and
5794
+ * prototype accessors can satisfy the shape. An optional member passes when its
5795
+ * read value is `undefined`; otherwise its guard must accept the value. Unknown
5796
+ * members are never enumerated or inspected. Member-carrying functions are
5797
+ * accepted. Arrays, `null`, and primitives are rejected. Hostile reads return
5798
+ * `false` rather than throwing.
5799
+ *
5800
+ * @example
5801
+ * ```ts
5802
+ * const isResult = objectOf({ conclusion: isBoolean })
5803
+ * isResult({ conclusion: true, metadata: 'retained' }) // true
5804
+ * isResult([]) // false
5805
+ * ```
5806
+ */
5807
+ function objectOf(shape, optional) {
5808
+ return contain(() => {
5809
+ const declared = readGuardShape(shape, optional, "objectOf");
5810
+ return (value) => holds(() => {
5811
+ if (!isObject(value) && !isFunction(value) || INTRINSICS.array(value)) return false;
5812
+ for (let index = 0; index < declared.names.length; index += 1) {
5813
+ const key = declared.names[index];
5814
+ if (key === void 0) continue;
5815
+ const guard = declared.guards[key];
5816
+ const member = INTRINSICS.read(value, key);
5817
+ if (matchesMember(declared.optional, key)) {
5818
+ if (member !== void 0 && (guard === void 0 || !guard(member))) return false;
5819
+ } else if (guard === void 0 || !guard(member)) return false;
5820
+ }
5821
+ return true;
5822
+ });
5823
+ }, "objectOf");
5824
+ }
5825
+ /**
5719
5826
  * Build a guard that accepts values that are own keys of the provided object.
5720
5827
  *
5721
5828
  * @remarks
@@ -8820,7 +8927,7 @@ function inferValue(value, depth, breadth, closed, format, visited, memo) {
8820
8927
  */
8821
8928
  function inferArray(value, depth, breadth, closed, format, visited, memo) {
8822
8929
  return contain(() => {
8823
- const maxDepth = sanitizeBudget(depth, 32);
8930
+ const maxDepth = sanitizeDepth(depth);
8824
8931
  const maxBreadth = sanitizeBudget(breadth, 256);
8825
8932
  return readValue(() => {
8826
8933
  if (!(maxDepth > 0) || matchesVisited(visited, value)) return {};
@@ -8901,9 +9008,10 @@ function inferArray(value, depth, breadth, closed, format, visited, memo) {
8901
9008
  function inferObject(value, depth, breadth, closed, format, visited, memo) {
8902
9009
  return contain(() => {
8903
9010
  const maxBreadth = sanitizeBudget(breadth, 256);
9011
+ const maxDepth = sanitizeDepth(depth);
8904
9012
  return readValue(() => {
8905
- if (!(depth > 0) || matchesVisited(visited, value)) return {};
8906
- const cached = INTRINSICS.apply(INTRINSICS.recall, memo, [value])?.get(depth);
9013
+ if (!(maxDepth > 0) || matchesVisited(visited, value)) return {};
9014
+ const cached = INTRINSICS.apply(INTRINSICS.recall, memo, [value])?.get(maxDepth);
8907
9015
  if (cached) return cached;
8908
9016
  admitVisited(visited, value);
8909
9017
  const outcome = attempt(() => {
@@ -8923,7 +9031,7 @@ function inferObject(value, depth, breadth, closed, format, visited, memo) {
8923
9031
  dropped = true;
8924
9032
  continue;
8925
9033
  }
8926
- properties[key] = inferValue(propertyValue, depth - 1, maxBreadth, closed, format, visited, memo);
9034
+ properties[key] = inferValue(propertyValue, maxDepth - 1, maxBreadth, closed, format, visited, memo);
8927
9035
  required[required.length] = key;
8928
9036
  }
8929
9037
  return {
@@ -8943,7 +9051,7 @@ function inferObject(value, depth, breadth, closed, format, visited, memo) {
8943
9051
  ...required.length > 0 ? { required } : {},
8944
9052
  additionalProperties: partial ? true : !closed
8945
9053
  };
8946
- retainDepth(memo, value, depth, schema);
9054
+ retainDepth(memo, value, maxDepth, schema);
8947
9055
  return schema;
8948
9056
  }, "inferObject");
8949
9057
  }, "inferObject");
@@ -8966,11 +9074,16 @@ function inferObject(value, depth, breadth, closed, format, visited, memo) {
8966
9074
  * expect an object-shaped `inputSchema`; wrap a non-object payload with
8967
9075
  * {@link schemaToObject} before advertising it as a tool's parameters.
8968
9076
  *
8969
- * `maxDepth` / `maxProperties` are sanitized via {@link sanitizeBudget} to a
8970
- * finite non-negative integer, falling back to {@link INFER_DEPTH_LIMIT} /
8971
- * {@link INFER_BREADTH_LIMIT} for anything else (`NaN`, `Infinity`, negative,
8972
- * fractional) — a hostile or malformed budget can never defeat the depth
8973
- * guard or corrupt the sampled key/element list.
9077
+ * `maxProperties` is sanitized via {@link sanitizeBudget} to a finite
9078
+ * non-negative integer, falling back to {@link INFER_BREADTH_LIMIT} for anything
9079
+ * else (`NaN`, `Infinity`, negative, fractional), so a malformed breadth cannot
9080
+ * corrupt the sampled key/element list.
9081
+ *
9082
+ * `maxDepth` goes through {@link sanitizeDepth}, which does the same and then caps
9083
+ * at {@link INFER_DEPTH_LIMIT}, so the option NARROWS the walk and cannot widen
9084
+ * it. The cap is what makes the depth guard unbreakable: depth is the recursing
9085
+ * axis, and a large-but-valid budget used to descend until the call STACK failed
9086
+ * rather than until the guard said stop.
8974
9087
  *
8975
9088
  * @param value - The value to infer a schema from
8976
9089
  * @param options - Optional `maxDepth` / `maxProperties` / `closed` / `format` bounds
@@ -8994,7 +9107,7 @@ function valueToSchema(value, options) {
8994
9107
  "format",
8995
9108
  "enum"
8996
9109
  ], "valueToSchema", "schema");
8997
- const maxDepth = sanitizeBudget(optionsSnapshot?.maxDepth, 32);
9110
+ const maxDepth = sanitizeDepth(optionsSnapshot?.maxDepth);
8998
9111
  const maxProperties = sanitizeBudget(optionsSnapshot?.maxProperties, 256);
8999
9112
  const closed = optionsSnapshot?.closed ?? true;
9000
9113
  const format = optionsSnapshot?.format ?? false;
@@ -9230,8 +9343,9 @@ function inferRecordSamples(samples, depth, breadth, closed, format, enumOn, mem
9230
9343
  * and `enum` (both default `false`) opt a low-cardinality/unanimous-format
9231
9344
  * slot into the corresponding keyword — see {@link inferSamples} for the
9232
9345
  * precedence and the multi-sample format-disabling seam. `maxDepth` /
9233
- * `maxProperties` are sanitized via {@link sanitizeBudget} the same way
9234
- * {@link valueToSchema} sanitizes them — see there for why.
9346
+ * `maxProperties` are resolved exactly as {@link valueToSchema} resolves them —
9347
+ * breadth through {@link sanitizeBudget}, depth through {@link sanitizeDepth},
9348
+ * which also caps at {@link INFER_DEPTH_LIMIT}; see there for why.
9235
9349
  *
9236
9350
  * @param samples - The example values to infer a schema from
9237
9351
  * @param options - Optional `maxDepth` / `maxProperties` / `closed` / `format` / `enum` bounds
@@ -9255,7 +9369,7 @@ function samplesToSchema(samples, options) {
9255
9369
  "format",
9256
9370
  "enum"
9257
9371
  ], "samplesToSchema", "schema");
9258
- const maxDepth = sanitizeBudget(optionsSnapshot?.maxDepth, 32);
9372
+ const maxDepth = sanitizeDepth(optionsSnapshot?.maxDepth);
9259
9373
  const maxProperties = sanitizeBudget(optionsSnapshot?.maxProperties, 256);
9260
9374
  const closed = optionsSnapshot?.closed ?? true;
9261
9375
  const format = optionsSnapshot?.format ?? false;
@@ -10462,6 +10576,7 @@ exports.nullShape = nullShape;
10462
10576
  exports.nullableOf = nullableOf;
10463
10577
  exports.nullableShape = nullableShape;
10464
10578
  exports.numberShape = numberShape;
10579
+ exports.objectOf = objectOf;
10465
10580
  exports.objectShape = objectShape;
10466
10581
  exports.omitOf = omitOf;
10467
10582
  exports.omitVisited = omitVisited;
@@ -10496,6 +10611,7 @@ exports.pinMembers = pinMembers;
10496
10611
  exports.preview = preview;
10497
10612
  exports.rawShape = rawShape;
10498
10613
  exports.readArrayEntries = readArrayEntries;
10614
+ exports.readGuardShape = readGuardShape;
10499
10615
  exports.readMapEntries = readMapEntries;
10500
10616
  exports.readOptions = readOptions;
10501
10617
  exports.readPattern = readPattern;
@@ -10512,6 +10628,7 @@ exports.retainDepth = retainDepth;
10512
10628
  exports.samplesToFormat = samplesToFormat;
10513
10629
  exports.samplesToSchema = samplesToSchema;
10514
10630
  exports.sanitizeBudget = sanitizeBudget;
10631
+ exports.sanitizeDepth = sanitizeDepth;
10515
10632
  exports.schemaNodeToShape = schemaNodeToShape;
10516
10633
  exports.schemaToObject = schemaToObject;
10517
10634
  exports.schemaToParameters = schemaToParameters;