@orkestrel/rater 0.0.19 → 0.0.21

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.
@@ -2,6 +2,13 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  let _orkestrel_contract = require("@orkestrel/contract");
3
3
  let _orkestrel_reason = require("@orkestrel/reason");
4
4
  let _orkestrel_emitter = require("@orkestrel/emitter");
5
+ // src/core/constants.ts
6
+ /** Lists the stages a worksheet derivation step can belong to. */
7
+ var WORKSHEET_STAGES = [
8
+ "factor",
9
+ "group",
10
+ "total"
11
+ ];
5
12
  // src/core/errors.ts
6
13
  /**
7
14
  * Represents a coded programmer error thrown by the rating layer.
@@ -59,24 +66,24 @@ function isRaterError(value) {
59
66
  }
60
67
  // src/core/validators.ts
61
68
  /**
62
- * Determines whether a value is a {@link Stage} literal.
69
+ * Determines whether a value is a {@link WorksheetStage} literal.
63
70
  *
64
71
  * @remarks
65
72
  * A scalar union has no open or exact axis, so this guard accepts one of the
66
- * `Stage` literals and leaves nothing unchecked.
73
+ * `WorksheetStage` literals and leaves nothing unchecked.
67
74
  *
68
75
  * @param value - The value to test
69
76
  * @returns True if `value` is `'factor'`, `'group'`, or `'total'`; false otherwise
70
77
  *
71
78
  * @example
72
79
  * ```ts
73
- * import { isStage } from '@orkestrel/rater'
80
+ * import { isWorksheetStage } from '@orkestrel/rater'
74
81
  *
75
- * isStage('group') // true
76
- * isStage('step') // false
82
+ * isWorksheetStage('group') // true
83
+ * isWorksheetStage('step') // false
77
84
  * ```
78
85
  */
79
- var isStage = (0, _orkestrel_contract.literalOf)("factor", "group", "total");
86
+ var isWorksheetStage = (0, _orkestrel_contract.literalOf)("factor", "group", "total");
80
87
  /**
81
88
  * Determines whether a value is an exact {@link LineDefinition} record.
82
89
  *
@@ -133,15 +140,20 @@ function isRatingDefinition(value) {
133
140
  name: _orkestrel_contract.isString,
134
141
  description: _orkestrel_contract.isString,
135
142
  lines: (0, _orkestrel_contract.arrayOf)(isLineDefinition),
136
- metadata: _orkestrel_contract.isJSONValue
137
- }, ["description", "metadata"])(value);
143
+ metadata: _orkestrel_contract.isJSONValue,
144
+ shape: _orkestrel_reason.isContractShape
145
+ }, [
146
+ "description",
147
+ "metadata",
148
+ "shape"
149
+ ])(value);
138
150
  }
139
151
  /**
140
152
  * Determines whether a value is an open result-side {@link Evidence} object.
141
153
  *
142
154
  * @remarks
143
155
  * Optional `field` is checked as a `FieldPath`, `label` as a string,
144
- * `comparison` as a `Comparison`, and `met` as a boolean, each when defined.
156
+ * `comparison` as a `Comparison`, and `state` as a `CheckState`, each when defined.
145
157
  * Unknown members, prototypes, and class instances are admitted. Arrays are
146
158
  * refused. `expected` and `actual` are `unknown`, so this guard does not read or
147
159
  * check them and a borrowed result is never narrowed on them; either member may
@@ -155,7 +167,7 @@ function isRatingDefinition(value) {
155
167
  * ```ts
156
168
  * import { isEvidence } from '@orkestrel/rater'
157
169
  *
158
- * isEvidence({ field: 'age', comparison: 'above', expected: 18, actual: 25, met: true }) // true
170
+ * isEvidence({ field: 'age', comparison: 'above', expected: 18, actual: 25, state: 'met' }) // true
159
171
  * ```
160
172
  */
161
173
  function isEvidence(value) {
@@ -163,12 +175,12 @@ function isEvidence(value) {
163
175
  field: _orkestrel_reason.isFieldPath,
164
176
  label: _orkestrel_contract.isString,
165
177
  comparison: _orkestrel_reason.isComparison,
166
- met: _orkestrel_contract.isBoolean
178
+ state: _orkestrel_reason.isCheckState
167
179
  }, [
168
180
  "field",
169
181
  "label",
170
182
  "comparison",
171
- "met"
183
+ "state"
172
184
  ])(value);
173
185
  }
174
186
  /**
@@ -239,7 +251,7 @@ function isWorksheetGroup(value) {
239
251
  * Determines whether a value is an open {@link Step} result object.
240
252
  *
241
253
  * @remarks
242
- * `stage` is checked through `isStage`, `id`, `name`, and `expression` when
254
+ * `stage` is checked through `isWorksheetStage`, `id`, `name`, and `expression` when
243
255
  * defined, and `value` as a plain number. Unknown members, prototypes, and
244
256
  * class instances are admitted and left unchecked, because a borrowed result
245
257
  * implementation may add them. Arrays are refused. Optional members may be
@@ -257,7 +269,7 @@ function isWorksheetGroup(value) {
257
269
  */
258
270
  function isStep(value) {
259
271
  return (0, _orkestrel_contract.objectOf)({
260
- stage: isStage,
272
+ stage: isWorksheetStage,
261
273
  id: _orkestrel_contract.isString,
262
274
  name: _orkestrel_contract.isString,
263
275
  value: _orkestrel_contract.isNumber,
@@ -286,7 +298,7 @@ function isStep(value) {
286
298
  * ```ts
287
299
  * import { isWorksheet } from '@orkestrel/rater'
288
300
  *
289
- * isWorksheet({ id: 'quote', name: 'Quote', aggregation: 'sum', value: 0, groups: [], steps: [], trace: [], errors: [], success: true }) // true
301
+ * isWorksheet({ id: 'quote', name: 'Quote', aggregation: 'sum', value: 0, groups: [], steps: [], trace: [], errors: [], success: true, faults: [], repairs: [], defaulted: [] }) // true
290
302
  * ```
291
303
  */
292
304
  function isWorksheet(value) {
@@ -300,7 +312,10 @@ function isWorksheet(value) {
300
312
  steps: (0, _orkestrel_contract.arrayOf)(isStep),
301
313
  trace: (0, _orkestrel_contract.arrayOf)(_orkestrel_contract.isString),
302
314
  errors: (0, _orkestrel_contract.arrayOf)(_orkestrel_contract.isString),
303
- success: _orkestrel_contract.isBoolean
315
+ success: _orkestrel_contract.isBoolean,
316
+ faults: (0, _orkestrel_contract.arrayOf)(_orkestrel_contract.isAuditFault),
317
+ repairs: (0, _orkestrel_contract.arrayOf)(_orkestrel_contract.isAuditFault),
318
+ defaulted: (0, _orkestrel_contract.arrayOf)(_orkestrel_reason.isFieldPath)
304
319
  }, ["precision"])(value);
305
320
  }
306
321
  /**
@@ -353,14 +368,18 @@ function isLineResult(value) {
353
368
  * ```ts
354
369
  * import { isRatingResult } from '@orkestrel/rater'
355
370
  *
356
- * isRatingResult({ lines: [], success: true }) // true
371
+ * isRatingResult({ lines: [], success: true, errors: [], faults: [], repairs: [], defaulted: [] }) // true
357
372
  * ```
358
373
  */
359
374
  function isRatingResult(value) {
360
375
  return (0, _orkestrel_contract.objectOf)({
361
376
  lines: (0, _orkestrel_contract.arrayOf)(isLineResult),
362
377
  total: _orkestrel_contract.isNumber,
363
- success: _orkestrel_contract.isBoolean
378
+ success: _orkestrel_contract.isBoolean,
379
+ errors: (0, _orkestrel_contract.arrayOf)(_orkestrel_contract.isString),
380
+ faults: (0, _orkestrel_contract.arrayOf)(_orkestrel_contract.isAuditFault),
381
+ repairs: (0, _orkestrel_contract.arrayOf)(_orkestrel_contract.isAuditFault),
382
+ defaulted: (0, _orkestrel_contract.arrayOf)(_orkestrel_reason.isFieldPath)
364
383
  }, ["total"])(value);
365
384
  }
366
385
  // src/core/helpers.ts
@@ -424,8 +443,7 @@ function buildRatingDefinition(id, name, lines, overrides) {
424
443
  *
425
444
  * @param check - The authored check
426
445
  * @param actual - The resolved subject value
427
- * @param met - If `true`, the check was met; if `false`, it was not; absent when the check
428
- * was not yet evaluated
446
+ * @param state - The check's state; absent when the check was not evaluated
429
447
  * @param labels - Optional field-to-label overrides, keyed by dot-joined field
430
448
  * @returns A fresh evidence row
431
449
  *
@@ -435,18 +453,19 @@ function buildRatingDefinition(id, name, lines, overrides) {
435
453
  * import { buildEvidence } from '@orkestrel/rater'
436
454
  *
437
455
  * const entry = createCheck('age', 'above', 18)
438
- * buildEvidence(entry, 25, true) // { field: 'age', comparison: 'above', expected: 18, actual: 25, met: true }
456
+ * buildEvidence(entry, 25, 'met') // { field: 'age', comparison: 'above', expected: 18, actual: 25, state: 'met' }
439
457
  * ```
440
458
  */
441
- function buildEvidence(check, actual, met, labels) {
459
+ function buildEvidence(check, actual, state, labels) {
442
460
  const field = (0, _orkestrel_reason.formatField)(check.field);
461
+ const label = labels !== void 0 && Object.hasOwn(labels, field) ? labels[field] : void 0;
443
462
  return {
444
463
  field: check.field,
445
- ...labels?.[field] === void 0 ? {} : { label: labels[field] },
464
+ ...label === void 0 ? {} : { label },
446
465
  comparison: check.operator,
447
466
  expected: check.value,
448
467
  actual,
449
- ...met === void 0 ? {} : { met }
468
+ ...state === void 0 ? {} : { state }
450
469
  };
451
470
  }
452
471
  /**
@@ -464,13 +483,13 @@ function buildEvidence(check, actual, met, labels) {
464
483
  * import { buildEvidenceRows } from '@orkestrel/rater'
465
484
  *
466
485
  * const checks = [createCheck('age', 'above', 18)]
467
- * buildEvidenceRows(checks, [{ field: 'age', met: true, actual: 25 }])
486
+ * buildEvidenceRows(checks, [{ field: 'age', state: 'met', actual: 25 }])
468
487
  * ```
469
488
  */
470
489
  function buildEvidenceRows(checks, results, labels) {
471
490
  return (checks ?? []).map((check, index) => {
472
491
  const result = results?.[index];
473
- return buildEvidence(check, result?.actual, result?.met, labels);
492
+ return buildEvidence(check, result?.actual, result?.state, labels);
474
493
  });
475
494
  }
476
495
  /**
@@ -616,7 +635,10 @@ function buildWorksheet(definition, result, labels) {
616
635
  steps: buildWorksheetSteps(definition, result, groups),
617
636
  trace: result.trace,
618
637
  errors: result.errors,
619
- success: result.success
638
+ success: result.success,
639
+ faults: result.faults,
640
+ repairs: result.repairs,
641
+ defaulted: result.defaulted
620
642
  };
621
643
  }
622
644
  /**
@@ -650,7 +672,7 @@ function buildLineResult(definition, result, labels) {
650
672
  * Sums defined line amounts.
651
673
  *
652
674
  * @param lines - The rated lines
653
- * @returns The sum of every line's `amount`, or `undefined` when no line has one
675
+ * @returns The sum, or `undefined` when no line has an amount or any line refused
654
676
  *
655
677
  * @example
656
678
  * ```ts
@@ -664,8 +686,145 @@ function buildLineResult(definition, result, labels) {
664
686
  * ```
665
687
  */
666
688
  function sumAmounts(lines) {
689
+ if (!lines.every((rated) => rated.worksheet.success)) return void 0;
667
690
  return Iterator.from(lines).reduce((total, rated) => rated.amount === void 0 ? total : (total ?? 0) + rated.amount, void 0);
668
691
  }
692
+ /**
693
+ * Projects a result to the JSON its contract reads.
694
+ *
695
+ * @remarks
696
+ * An evidence row's `actual` holds whatever the subject held and a worksheet's numbers may be
697
+ * `NaN`, so a result crosses a boundary as this projection, each `actual` on its own budget.
698
+ *
699
+ * @param result - The rating result
700
+ * @returns Its JSON projection, which `ratingResultContract` admits
701
+ *
702
+ * @example
703
+ * ```ts
704
+ * import { createRater, projectRatingResult, ratingResultContract } from '@orkestrel/rater'
705
+ *
706
+ * const rater = createRater()
707
+ * ratingResultContract.is(projectRatingResult(rater.rate([], {}))) // true
708
+ * rater.destroy()
709
+ * ```
710
+ */
711
+ function projectRatingResult(result) {
712
+ const lines = result.lines.map((line) => ({
713
+ ...line,
714
+ worksheet: {
715
+ ...line.worksheet,
716
+ groups: line.worksheet.groups.map((group) => ({
717
+ ...group,
718
+ factors: group.factors.map((factor) => ({
719
+ ...factor,
720
+ evidence: factor.evidence.map((row) => ({
721
+ ...row,
722
+ actual: (0, _orkestrel_reason.projectValue)(row.actual)
723
+ }))
724
+ }))
725
+ }))
726
+ }
727
+ }));
728
+ return (0, _orkestrel_reason.projectValue)({
729
+ ...result,
730
+ lines
731
+ }, /* @__PURE__ */ new Set(), { left: Number.POSITIVE_INFINITY });
732
+ }
733
+ // src/core/shapers.ts
734
+ /** Describes a `LineDefinition`. */
735
+ var lineDefinitionShape = (0, _orkestrel_contract.objectShape)({
736
+ id: (0, _orkestrel_contract.stringShape)(),
737
+ name: (0, _orkestrel_contract.stringShape)(),
738
+ description: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.stringShape)()),
739
+ rate: _orkestrel_reason.quantitativeDefinitionShape,
740
+ metadata: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.jsonShape)())
741
+ });
742
+ /**
743
+ * Describes a `RatingDefinition`.
744
+ */
745
+ var ratingDefinitionShape = (0, _orkestrel_contract.objectShape)({
746
+ id: (0, _orkestrel_contract.stringShape)(),
747
+ name: (0, _orkestrel_contract.stringShape)(),
748
+ description: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.stringShape)()),
749
+ lines: (0, _orkestrel_contract.arrayShape)(lineDefinitionShape),
750
+ metadata: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.jsonShape)()),
751
+ shape: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.shapeShape)())
752
+ });
753
+ /** Describes an `Evidence` row in its JSON projection. */
754
+ var evidenceShape = (0, _orkestrel_contract.objectShape)({
755
+ field: (0, _orkestrel_contract.optionalShape)(_orkestrel_reason.fieldPathShape),
756
+ label: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.stringShape)()),
757
+ comparison: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.literalShape)(_orkestrel_reason.COMPARISONS)),
758
+ expected: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.jsonShape)()),
759
+ actual: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.jsonShape)()),
760
+ state: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.literalShape)(_orkestrel_reason.CHECK_STATES))
761
+ });
762
+ /** Describes a `WorksheetFactor` in its JSON projection. */
763
+ var worksheetFactorShape = (0, _orkestrel_contract.objectShape)({
764
+ id: (0, _orkestrel_contract.stringShape)(),
765
+ name: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.stringShape)()),
766
+ description: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.stringShape)()),
767
+ applied: (0, _orkestrel_contract.booleanShape)(),
768
+ value: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.nullableShape)((0, _orkestrel_contract.numberShape)())),
769
+ evidence: (0, _orkestrel_contract.arrayShape)(evidenceShape)
770
+ });
771
+ /** Describes a `WorksheetGroup` in its JSON projection. */
772
+ var worksheetGroupShape = (0, _orkestrel_contract.objectShape)({
773
+ id: (0, _orkestrel_contract.stringShape)(),
774
+ name: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.stringShape)()),
775
+ description: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.stringShape)()),
776
+ applied: (0, _orkestrel_contract.booleanShape)(),
777
+ value: (0, _orkestrel_contract.nullableShape)((0, _orkestrel_contract.numberShape)()),
778
+ factors: (0, _orkestrel_contract.arrayShape)(worksheetFactorShape)
779
+ });
780
+ /** Describes a `Step` in its JSON projection. */
781
+ var worksheetStepShape = (0, _orkestrel_contract.objectShape)({
782
+ stage: (0, _orkestrel_contract.literalShape)(WORKSHEET_STAGES),
783
+ id: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.stringShape)()),
784
+ name: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.stringShape)()),
785
+ value: (0, _orkestrel_contract.nullableShape)((0, _orkestrel_contract.numberShape)()),
786
+ expression: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.stringShape)())
787
+ });
788
+ /** Describes a `Worksheet` in its JSON projection. */
789
+ var worksheetShape = (0, _orkestrel_contract.objectShape)({
790
+ id: (0, _orkestrel_contract.stringShape)(),
791
+ name: (0, _orkestrel_contract.stringShape)(),
792
+ aggregation: (0, _orkestrel_contract.literalShape)(_orkestrel_reason.AGGREGATIONS),
793
+ precision: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.numberShape)()),
794
+ value: (0, _orkestrel_contract.nullableShape)((0, _orkestrel_contract.numberShape)()),
795
+ groups: (0, _orkestrel_contract.arrayShape)(worksheetGroupShape),
796
+ steps: (0, _orkestrel_contract.arrayShape)(worksheetStepShape),
797
+ trace: (0, _orkestrel_contract.arrayShape)((0, _orkestrel_contract.stringShape)()),
798
+ errors: (0, _orkestrel_contract.arrayShape)((0, _orkestrel_contract.stringShape)()),
799
+ success: (0, _orkestrel_contract.booleanShape)(),
800
+ faults: (0, _orkestrel_contract.arrayShape)((0, _orkestrel_contract.auditFaultShape)()),
801
+ repairs: (0, _orkestrel_contract.arrayShape)((0, _orkestrel_contract.auditFaultShape)()),
802
+ defaulted: (0, _orkestrel_contract.arrayShape)(_orkestrel_reason.fieldPathShape)
803
+ });
804
+ /** Describes a `LineResult` in its JSON projection. */
805
+ var lineResultShape = (0, _orkestrel_contract.objectShape)({
806
+ id: (0, _orkestrel_contract.stringShape)(),
807
+ name: (0, _orkestrel_contract.stringShape)(),
808
+ amount: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.nullableShape)((0, _orkestrel_contract.numberShape)())),
809
+ worksheet: worksheetShape
810
+ });
811
+ /** Describes a `RatingResult` in its JSON projection. */
812
+ var ratingResultShape = (0, _orkestrel_contract.objectShape)({
813
+ lines: (0, _orkestrel_contract.arrayShape)(lineResultShape),
814
+ total: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.nullableShape)((0, _orkestrel_contract.numberShape)())),
815
+ success: (0, _orkestrel_contract.booleanShape)(),
816
+ errors: (0, _orkestrel_contract.arrayShape)((0, _orkestrel_contract.stringShape)()),
817
+ faults: (0, _orkestrel_contract.arrayShape)((0, _orkestrel_contract.auditFaultShape)()),
818
+ repairs: (0, _orkestrel_contract.arrayShape)((0, _orkestrel_contract.auditFaultShape)()),
819
+ defaulted: (0, _orkestrel_contract.arrayShape)(_orkestrel_reason.fieldPathShape)
820
+ });
821
+ // src/core/contracts.ts
822
+ /**
823
+ * Publishes a `RatingDefinition` as a contract. Cross it on `is`, never `parse`.
824
+ */
825
+ var ratingDefinitionContract = (0, _orkestrel_contract.createContract)(ratingDefinitionShape);
826
+ /** Publishes a `RatingResult`'s JSON projection as a contract, read through `projectRatingResult`. */
827
+ var ratingResultContract = (0, _orkestrel_contract.createContract)(ratingResultShape);
669
828
  // src/core/Rater.ts
670
829
  /**
671
830
  * Orchestrates rating — owns (or receives) the shared quantitative
@@ -685,7 +844,7 @@ function sumAmounts(lines) {
685
844
  * import { Rater } from '@orkestrel/rater'
686
845
  *
687
846
  * const rater = new Rater()
688
- * rater.rate([], { id: 'subject-1' }) // { lines: [], success: true }
847
+ * rater.rate([], { id: 'subject-1' }).success // true
689
848
  * rater.destroy()
690
849
  * ```
691
850
  */
@@ -695,6 +854,8 @@ var Rater = class {
695
854
  #owned;
696
855
  #total;
697
856
  #labels;
857
+ #door = (0, _orkestrel_reason.createSubjectDoor)();
858
+ #valid = /* @__PURE__ */ new WeakSet();
698
859
  #destroyed = false;
699
860
  constructor(options) {
700
861
  this.#emitter = new _orkestrel_emitter.Emitter({
@@ -721,15 +882,49 @@ var Rater = class {
721
882
  this.#ensureAlive();
722
883
  const lines = this.#normalize(input);
723
884
  if (!(0, _orkestrel_contract.isRecord)(subject)) throw new RaterError("MISMATCH", "Subject must be a record");
724
- const results = lines.map((line) => this.#rateLine(line, subject));
725
- const total = (this.#total ?? sumAmounts)(results);
726
- const success = results.every((entry) => entry.worksheet.success);
885
+ const admission = this.#door.admit(subject, this.#readShape(input));
886
+ const admitted = admission.subject;
887
+ if (admitted === void 0) {
888
+ const refused = {
889
+ lines: [],
890
+ success: false,
891
+ errors: admission.errors,
892
+ faults: admission.faults,
893
+ repairs: [],
894
+ defaulted: []
895
+ };
896
+ this.#emitter.emit("rate", subject, refused);
897
+ return refused;
898
+ }
899
+ const results = lines.map((line) => this.#rateLine(line, admitted));
900
+ const rated = results.every((entry) => entry.worksheet.success);
901
+ const summed = rated ? (this.#total ?? sumAmounts)(results) : void 0;
902
+ const overflowed = summed !== void 0 && !(0, _orkestrel_contract.isFiniteNumber)(summed);
903
+ const total = overflowed ? void 0 : summed;
904
+ const success = rated && !overflowed;
905
+ const faults = (0, _orkestrel_reason.filterUniqueFaults)(results.flatMap((entry) => entry.worksheet.faults));
906
+ const said = new Set(faults.map((fault) => (0, _orkestrel_contract.describeFaults)([fault])));
907
+ const other = results.flatMap((entry) => {
908
+ const { errors, success: settled } = entry.worksheet;
909
+ if (!settled && errors.length === 0) return [`${entry.id}: The line failed and said nothing`];
910
+ return errors.filter((error) => !said.has(error)).map((error) => `${entry.id}: ${error}`);
911
+ });
912
+ const repairs = (0, _orkestrel_reason.filterUniqueFaults)([...admission.repairs, ...results.flatMap((entry) => entry.worksheet.repairs)]);
913
+ const defaulted = (0, _orkestrel_reason.filterUniqueFields)([...admission.defaulted, ...results.flatMap((entry) => entry.worksheet.defaulted)]);
727
914
  const result = {
728
915
  lines: results,
729
916
  ...total === void 0 ? {} : { total },
730
- success
917
+ success,
918
+ errors: [
919
+ ...other,
920
+ ...said,
921
+ ...overflowed ? ["The total is not a finite number"] : []
922
+ ],
923
+ faults,
924
+ repairs,
925
+ defaulted
731
926
  };
732
- this.#emitter.emit("rate", subject, result);
927
+ this.#emitter.emit("rate", admitted, result);
733
928
  return result;
734
929
  }
735
930
  destroy() {
@@ -743,18 +938,25 @@ var Rater = class {
743
938
  if (isRatingDefinition(input)) return input.lines;
744
939
  throw new RaterError("DEFINITION", "Rating input must be an array of line definitions or a rating definition");
745
940
  }
941
+ #readShape(input) {
942
+ if ((0, _orkestrel_contract.arrayOf)(isLineDefinition)(input)) return void 0;
943
+ const { shape, lines } = input;
944
+ if (shape === void 0 || this.#valid.has(input)) return shape;
945
+ const checks = lines.flatMap((line) => line.rate.groups.flatMap((group) => group.factors.flatMap((factor) => factor.checks ?? [])));
946
+ const fields = lines.flatMap((line) => line.rate.groups.flatMap((group) => group.factors.flatMap((factor) => factor.source.origin === "static" ? [] : [factor.source.field])));
947
+ const errors = [...(0, _orkestrel_reason.scanShape)(shape), ...(0, _orkestrel_reason.scanShapeReads)(shape, checks, fields, /* @__PURE__ */ new Set()).errors];
948
+ if (errors.length > 0) throw new RaterError("DEFINITION", `Rating shape is invalid: ${errors.join(", ")}`);
949
+ this.#valid.add(input);
950
+ return shape;
951
+ }
746
952
  #rateLine(line, subject) {
747
953
  return buildLineResult(line, this.#reasonQuantitative(subject, line.rate), this.#labels);
748
954
  }
749
955
  #reasonQuantitative(subject, definition) {
750
- const result = this.#engine.reason(subject, definition);
751
- if (result.reasoning === "quantitative") return result;
752
- const failure = (0, _orkestrel_reason.buildErrorResult)(definition, `Expected quantitative result, got ${result.reasoning}`);
753
- const errors = [...(0, _orkestrel_contract.isArray)(result.errors) ? result.errors : [], ...failure.errors];
754
- if (failure.reasoning === "quantitative") return {
755
- ...failure,
756
- errors
757
- };
956
+ const result = this.#reason(subject, definition);
957
+ if ((0, _orkestrel_reason.isQuantitativeResult)(result)) return this.#settle(result);
958
+ const answer = (0, _orkestrel_contract.isRecord)(result) ? result : {};
959
+ const named = (0, _orkestrel_contract.isString)(answer.reasoning) ? answer.reasoning : "something else";
758
960
  return {
759
961
  reasoning: "quantitative",
760
962
  value: 0,
@@ -762,9 +964,30 @@ var Rater = class {
762
964
  count: 0,
763
965
  success: false,
764
966
  trace: [],
765
- errors
967
+ errors: [...(0, _orkestrel_contract.isArray)(answer.errors) ? answer.errors.filter(_orkestrel_contract.isString) : [], `Expected quantitative result, got ${named}`],
968
+ faults: [],
969
+ repairs: [],
970
+ defaulted: []
971
+ };
972
+ }
973
+ #settle(result) {
974
+ if (!result.success) return result;
975
+ if (result.errors.length === 0 && result.faults.length === 0 && (0, _orkestrel_contract.isFiniteNumber)(result.value)) return result;
976
+ const said = result.errors.length > 0 ? [] : ["The line succeeded beside a fault or no value"];
977
+ return {
978
+ ...result,
979
+ success: false,
980
+ errors: [...result.errors, ...said]
766
981
  };
767
982
  }
983
+ #reason(subject, definition) {
984
+ try {
985
+ return this.#engine.reason(subject, definition);
986
+ } catch (error) {
987
+ if ((0, _orkestrel_reason.isReasonError)(error) && error.code === "INVALID") throw new RaterError("DEFINITION", error.message);
988
+ throw error;
989
+ }
990
+ }
768
991
  #ensureAlive() {
769
992
  if (this.#destroyed) throw new RaterError("DESTROYED", "Rater has been destroyed");
770
993
  }
@@ -790,6 +1013,7 @@ function createRater(options) {
790
1013
  }
791
1014
  exports.Rater = Rater;
792
1015
  exports.RaterError = RaterError;
1016
+ exports.WORKSHEET_STAGES = WORKSHEET_STAGES;
793
1017
  exports.buildEvidence = buildEvidence;
794
1018
  exports.buildEvidenceRows = buildEvidenceRows;
795
1019
  exports.buildLineDefinition = buildLineDefinition;
@@ -801,17 +1025,29 @@ exports.buildWorksheetGroup = buildWorksheetGroup;
801
1025
  exports.buildWorksheetStep = buildWorksheetStep;
802
1026
  exports.buildWorksheetSteps = buildWorksheetSteps;
803
1027
  exports.createRater = createRater;
1028
+ exports.evidenceShape = evidenceShape;
804
1029
  exports.isEvidence = isEvidence;
805
1030
  exports.isLineDefinition = isLineDefinition;
806
1031
  exports.isLineResult = isLineResult;
807
1032
  exports.isRaterError = isRaterError;
808
1033
  exports.isRatingDefinition = isRatingDefinition;
809
1034
  exports.isRatingResult = isRatingResult;
810
- exports.isStage = isStage;
811
1035
  exports.isStep = isStep;
812
1036
  exports.isWorksheet = isWorksheet;
813
1037
  exports.isWorksheetFactor = isWorksheetFactor;
814
1038
  exports.isWorksheetGroup = isWorksheetGroup;
1039
+ exports.isWorksheetStage = isWorksheetStage;
1040
+ exports.lineDefinitionShape = lineDefinitionShape;
1041
+ exports.lineResultShape = lineResultShape;
1042
+ exports.projectRatingResult = projectRatingResult;
1043
+ exports.ratingDefinitionContract = ratingDefinitionContract;
1044
+ exports.ratingDefinitionShape = ratingDefinitionShape;
1045
+ exports.ratingResultContract = ratingResultContract;
1046
+ exports.ratingResultShape = ratingResultShape;
815
1047
  exports.sumAmounts = sumAmounts;
1048
+ exports.worksheetFactorShape = worksheetFactorShape;
1049
+ exports.worksheetGroupShape = worksheetGroupShape;
1050
+ exports.worksheetShape = worksheetShape;
1051
+ exports.worksheetStepShape = worksheetStepShape;
816
1052
 
817
1053
  //# sourceMappingURL=index.cjs.map