@orkestrel/qualifier 0.0.25 → 0.0.27
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/README.md +1 -1
- package/dist/src/core/index.cjs +398 -300
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +261 -224
- package/dist/src/core/index.d.ts +261 -224
- package/dist/src/core/index.js +391 -294
- package/dist/src/core/index.js.map +1 -1
- package/package.json +17 -16
package/dist/src/core/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { INTRINSICS, arrayOf, arrayShape, auditFaultShape, booleanShape, createContract,
|
|
2
|
-
import { CHECK_STATES,
|
|
1
|
+
import { INTRINSICS, arrayOf, arrayShape, attempt, auditFaultShape, booleanShape, createContract, holds, isArray, isAuditFault, isBoolean, isError, isFiniteNumber, isFunction, isJSONValue, isNumber, isObject, isRecord, isString, isSuccess, jsonShape, literalOf, literalShape, nullableShape, numberShape, objectOf, objectShape, optionalShape, recordOf, recordShape, resolveField, shapeShape, stringShape, unionOf, unionShape, whereOf } from "@orkestrel/contract";
|
|
2
|
+
import { CHECK_STATES, checkResultShape, collectConcluders, createLogicalDefinitionShape, createLogicalReasoner, createQuantitativeReasoner, createReason, createSubjectDoor, extractAtoms, extractConclusions, fieldPathShape, filterUniqueFaults, findDuplicates, findFieldShape, formatField, isCheckResult, isCheckState, isContractShape, isFieldPath, isLogicalDefinition, isQuantitativeDefinition, isReasonError, isReasonRefusal, isReasonResult, nestRefusals, projectCheck, projectRefusal, projectValue, quantitativeDefinitionShape, reasonRefusalShape, scanShape, scanShapeReads } from "@orkestrel/reason";
|
|
3
3
|
import { Emitter } from "@orkestrel/emitter";
|
|
4
4
|
// src/core/constants.ts
|
|
5
5
|
/**
|
|
@@ -128,41 +128,35 @@ function isEligibilityRecord(value) {
|
|
|
128
128
|
return whereOf(objectOf({}), (record) => Object.getOwnPropertyNames(record).every((key) => isEligibility(Reflect.get(record, key))))(value);
|
|
129
129
|
}
|
|
130
130
|
/**
|
|
131
|
-
* Determines whether a value is an open result-side {@link
|
|
131
|
+
* Determines whether a value is an open result-side {@link MessageSlot}.
|
|
132
132
|
*
|
|
133
133
|
* @remarks
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
* because its published type is `unknown`. `actual` is unchecked and may be absent or contain
|
|
137
|
-
* any value for the same reason. Unknown members are admitted, because an open result admits
|
|
134
|
+
* `field` is checked. `actual` must be present and may hold anything, and is never read, because
|
|
135
|
+
* its published type is `unknown`. Unknown members are admitted, because an open result admits
|
|
138
136
|
* what the published contract does not name.
|
|
139
137
|
*
|
|
140
138
|
* @param value - The value to test
|
|
141
|
-
* @returns True if
|
|
139
|
+
* @returns True if the slot names a field and carries a value; false otherwise
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```ts
|
|
143
|
+
* import { isMessageSlot } from '@orkestrel/qualifier'
|
|
144
|
+
*
|
|
145
|
+
* isMessageSlot({ field: ['age'], actual: undefined }) // true
|
|
146
|
+
* isMessageSlot({ field: ['age'] }) // false
|
|
147
|
+
* ```
|
|
142
148
|
*/
|
|
143
|
-
function
|
|
144
|
-
return objectOf({
|
|
145
|
-
field: isFieldPath,
|
|
146
|
-
label: isString,
|
|
147
|
-
description: isString,
|
|
148
|
-
comparison: isComparison,
|
|
149
|
-
state: isCheckState
|
|
150
|
-
}, [
|
|
151
|
-
"field",
|
|
152
|
-
"label",
|
|
153
|
-
"description",
|
|
154
|
-
"comparison",
|
|
155
|
-
"state"
|
|
156
|
-
])(value);
|
|
149
|
+
function isMessageSlot(value) {
|
|
150
|
+
return whereOf(objectOf({ field: isFieldPath }), (slot) => Reflect.has(slot, "actual"))(value);
|
|
157
151
|
}
|
|
158
152
|
/**
|
|
159
153
|
* Determines whether a value is an open result-side {@link Finding}.
|
|
160
154
|
*
|
|
161
155
|
* @remarks
|
|
162
|
-
* Every published member is checked, including each nested {@link
|
|
163
|
-
* and `message` members may be absent or `undefined`; a present defined value
|
|
164
|
-
*
|
|
165
|
-
* without changing the published contract.
|
|
156
|
+
* Every published member is checked, including each nested {@link MessageSlot} and check result.
|
|
157
|
+
* Optional `scope` and `message` members may be absent or `undefined`; a present defined value is
|
|
158
|
+
* checked in full. Unknown members are admitted, because a foreign result implementation may add
|
|
159
|
+
* them without changing the published contract.
|
|
166
160
|
*
|
|
167
161
|
* @param value - The value to test
|
|
168
162
|
* @returns True if every published finding member is valid; false otherwise
|
|
@@ -175,8 +169,8 @@ function isFinding(value) {
|
|
|
175
169
|
effect: isQualificationEffect,
|
|
176
170
|
scope: isString,
|
|
177
171
|
state: isCheckState,
|
|
178
|
-
message: isString,
|
|
179
|
-
|
|
172
|
+
message: arrayOf(unionOf(isString, isMessageSlot)),
|
|
173
|
+
checks: arrayOf(isCheckResult)
|
|
180
174
|
}, ["scope", "message"])(value);
|
|
181
175
|
}
|
|
182
176
|
/**
|
|
@@ -184,9 +178,8 @@ function isFinding(value) {
|
|
|
184
178
|
*
|
|
185
179
|
* @remarks
|
|
186
180
|
* Every published member is checked: `value` follows the published `number` type, including
|
|
187
|
-
* `NaN` and the infinities
|
|
188
|
-
*
|
|
189
|
-
* published contract.
|
|
181
|
+
* `NaN` and the infinities. Unknown members are admitted, because a foreign result
|
|
182
|
+
* implementation may add them without changing the published contract.
|
|
190
183
|
*
|
|
191
184
|
* @param value - The value to test
|
|
192
185
|
* @returns True if every published derivation member is valid; false otherwise
|
|
@@ -197,7 +190,8 @@ function isDerivation(value) {
|
|
|
197
190
|
value: isNumber,
|
|
198
191
|
success: isBoolean,
|
|
199
192
|
trace: arrayOf(isString),
|
|
200
|
-
|
|
193
|
+
refusals: arrayOf(isReasonRefusal),
|
|
194
|
+
faults: arrayOf(isAuditFault)
|
|
201
195
|
})(value);
|
|
202
196
|
}
|
|
203
197
|
/**
|
|
@@ -223,7 +217,7 @@ function isQualificationResult(value) {
|
|
|
223
217
|
derivations: arrayOf(isDerivation),
|
|
224
218
|
success: isBoolean,
|
|
225
219
|
trace: arrayOf(isString),
|
|
226
|
-
|
|
220
|
+
refusals: arrayOf(isReasonRefusal),
|
|
227
221
|
faults: arrayOf(isAuditFault),
|
|
228
222
|
repairs: arrayOf(isAuditFault),
|
|
229
223
|
defaulted: arrayOf(isFieldPath)
|
|
@@ -307,212 +301,74 @@ function isQualificationDefinition(value) {
|
|
|
307
301
|
}
|
|
308
302
|
// src/core/helpers.ts
|
|
309
303
|
/**
|
|
310
|
-
*
|
|
304
|
+
* Splits a message template at its `{{dotted.path}}` tokens, reading each from a subject.
|
|
311
305
|
*
|
|
312
306
|
* @remarks
|
|
313
|
-
*
|
|
314
|
-
* `
|
|
315
|
-
*
|
|
316
|
-
*
|
|
317
|
-
* An unresolved path renders as the empty string.
|
|
307
|
+
* The text between tokens stays as authored, and an empty text is left out. Each token becomes a
|
|
308
|
+
* {@link MessageSlot} whose `field` is the token's path as an array of keys and whose `actual` is
|
|
309
|
+
* the raw value there, `undefined` for an absent one. Nothing is formatted. The tokens are those
|
|
310
|
+
* {@link findMessageTokens} finds.
|
|
318
311
|
*
|
|
319
312
|
* @param template - The message template carrying `{{dotted.path}}` tokens
|
|
320
313
|
* @param subject - The record tokens resolve against
|
|
321
|
-
* @returns The
|
|
314
|
+
* @returns The authored texts and the slots, in order
|
|
322
315
|
*
|
|
323
316
|
* @example
|
|
324
317
|
* ```ts
|
|
325
|
-
* import {
|
|
318
|
+
* import { splitMessage } from '@orkestrel/qualifier'
|
|
326
319
|
*
|
|
327
|
-
*
|
|
328
|
-
*
|
|
320
|
+
* splitMessage('Limit is {{limit}}', { limit: 5010 })
|
|
321
|
+
* // ['Limit is ', { field: ['limit'], actual: 5010 }]
|
|
329
322
|
* ```
|
|
330
323
|
*/
|
|
331
|
-
function
|
|
332
|
-
return template.
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
324
|
+
function splitMessage(template, subject) {
|
|
325
|
+
return template.split(MESSAGE_TOKEN).flatMap((part, index) => {
|
|
326
|
+
if (index % 2 === 0) return part === "" ? [] : [part];
|
|
327
|
+
const field = part.split(".");
|
|
328
|
+
return [{
|
|
329
|
+
field,
|
|
330
|
+
actual: resolveField(subject, field)
|
|
331
|
+
}];
|
|
337
332
|
});
|
|
338
333
|
}
|
|
339
334
|
/**
|
|
340
|
-
*
|
|
341
|
-
*
|
|
342
|
-
* @param template - The message template
|
|
343
|
-
* @returns Each token's dotted path, trimmed, in order, repeats included
|
|
344
|
-
*
|
|
345
|
-
* @example
|
|
346
|
-
* ```ts
|
|
347
|
-
* import { findMessageTokens } from '@orkestrel/qualifier'
|
|
348
|
-
*
|
|
349
|
-
* findMessageTokens('Age {{ age }} in {{home.city}}') // ['age', 'home.city']
|
|
350
|
-
* ```
|
|
351
|
-
*/
|
|
352
|
-
function findMessageTokens(template) {
|
|
353
|
-
return [...template.matchAll(MESSAGE_TOKEN)].map((match) => match[1] ?? "");
|
|
354
|
-
}
|
|
355
|
-
/**
|
|
356
|
-
* Renders a {@link Premise} comparison as a display-neutral verb phrase.
|
|
357
|
-
*
|
|
358
|
-
* @param comparison - The comparison to describe
|
|
359
|
-
* @returns A display-neutral phrase
|
|
360
|
-
*
|
|
361
|
-
* @example
|
|
362
|
-
* ```ts
|
|
363
|
-
* import { renderComparison } from '@orkestrel/qualifier'
|
|
364
|
-
*
|
|
365
|
-
* renderComparison('above') // 'is more than'
|
|
366
|
-
* ```
|
|
367
|
-
*/
|
|
368
|
-
function renderComparison(comparison) {
|
|
369
|
-
switch (comparison) {
|
|
370
|
-
case "equals": return "is";
|
|
371
|
-
case "not": return "is not";
|
|
372
|
-
case "above": return "is more than";
|
|
373
|
-
case "below": return "is less than";
|
|
374
|
-
case "from": return "is at least";
|
|
375
|
-
case "to": return "is at most";
|
|
376
|
-
case "any": return "is any of";
|
|
377
|
-
case "none": return "is none of";
|
|
378
|
-
case "between": return "is between";
|
|
379
|
-
case "outside": return "is outside";
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
/**
|
|
383
|
-
* Renders a structured or scalar expected value display-neutrally.
|
|
384
|
-
*
|
|
385
|
-
* @remarks
|
|
386
|
-
* An array renders as its elements, each `String`-coerced, joined with `', '`.
|
|
387
|
-
* A reason `Bounds`-shaped record (the `between` / `outside` expected value)
|
|
388
|
-
* renders its present sides joined with the word `and`; an absent side is
|
|
389
|
-
* omitted. A finite number and everything else fall back to plain `String()` —
|
|
390
|
-
* a finite number is not grouped here (grouping is {@link interpolateMessage}'s
|
|
391
|
-
* message-token-only behavior).
|
|
392
|
-
*
|
|
393
|
-
* @param value - The value to render
|
|
394
|
-
* @returns A display-neutral rendering
|
|
395
|
-
*
|
|
396
|
-
* @example
|
|
397
|
-
* ```ts
|
|
398
|
-
* import { renderValue } from '@orkestrel/qualifier'
|
|
399
|
-
*
|
|
400
|
-
* renderValue([18, 25, 40]) // '18, 25, 40'
|
|
401
|
-
* renderValue({ minimum: 18, maximum: 65 }) // '18 and 65'
|
|
402
|
-
* renderValue(42) // '42'
|
|
403
|
-
* ```
|
|
404
|
-
*/
|
|
405
|
-
function renderValue(value) {
|
|
406
|
-
if (isArray(value)) return value.map((entry) => String(entry)).join(", ");
|
|
407
|
-
if (isBounds(value)) {
|
|
408
|
-
const sides = [];
|
|
409
|
-
if (value.minimum !== void 0) sides.push(String(value.minimum));
|
|
410
|
-
if (value.maximum !== void 0) sides.push(String(value.maximum));
|
|
411
|
-
return sides.join(" and ");
|
|
412
|
-
}
|
|
413
|
-
return String(value);
|
|
414
|
-
}
|
|
415
|
-
/**
|
|
416
|
-
* Renders one {@link Premise} into a display-neutral sentence.
|
|
335
|
+
* Projects a slot's `actual` to JSON on its own budget, as `undefined` when it has no JSON form.
|
|
417
336
|
*
|
|
418
|
-
* @
|
|
419
|
-
* The
|
|
420
|
-
* present, and `description` then goes unused; a premise missing either half
|
|
421
|
-
* of the checked pair renders as described instead.
|
|
422
|
-
*
|
|
423
|
-
* @param entry - The premise to render
|
|
424
|
-
* @param labels - Optional field-to-label overrides, keyed by dot-joined field
|
|
425
|
-
* @returns A display-neutral sentence
|
|
337
|
+
* @param slot - The slot
|
|
338
|
+
* @returns The slot, its `actual` JSON or `undefined`
|
|
426
339
|
*
|
|
427
340
|
* @example
|
|
428
341
|
* ```ts
|
|
429
|
-
* import {
|
|
430
|
-
*
|
|
431
|
-
* renderPremise({ field: 'age', comparison: 'above', expected: 18, actual: 25, state: 'met' })
|
|
432
|
-
* // 'age is more than 18 → met'
|
|
342
|
+
* import { projectMessageSlot } from '@orkestrel/qualifier'
|
|
433
343
|
*
|
|
434
|
-
*
|
|
435
|
-
* // 'Applicant is enrolled → met'
|
|
344
|
+
* projectMessageSlot({ field: ['limit'], actual: Number.NaN }) // { field: ['limit'], actual: null }
|
|
436
345
|
* ```
|
|
437
346
|
*/
|
|
438
|
-
function
|
|
439
|
-
const status = entry.state === "met" ? "met" : entry.state === "unmet" ? "not met" : "unknown";
|
|
440
|
-
if (entry.field === void 0 || entry.comparison === void 0) return `${entry.description ?? "Premise"} → ${status}`;
|
|
441
|
-
const field = formatField(entry.field);
|
|
442
|
-
const label = labels?.[field] ?? entry.label ?? field;
|
|
443
|
-
const expected = entry.expected === void 0 ? "" : ` ${renderValue(entry.expected)}`;
|
|
444
|
-
return `${label} ${renderComparison(entry.comparison)}${expected} → ${status}`;
|
|
445
|
-
}
|
|
446
|
-
/**
|
|
447
|
-
* Builds a {@link Premise} from an authored {@link Check} and its evaluated
|
|
448
|
-
* `CheckResult`.
|
|
449
|
-
*
|
|
450
|
-
* @param check - The authored check
|
|
451
|
-
* @param result - The evaluated check result carrying the resolved `actual` and `state`
|
|
452
|
-
* @param labels - Optional field-to-label overrides, keyed by dot-joined field
|
|
453
|
-
* @returns A fresh premise
|
|
454
|
-
*
|
|
455
|
-
* @example
|
|
456
|
-
* ```ts
|
|
457
|
-
* import { checkToPremise } from '@orkestrel/qualifier'
|
|
458
|
-
* import { createCheck } from '@orkestrel/reason'
|
|
459
|
-
*
|
|
460
|
-
* const authored = createCheck('age', 'above', 18)
|
|
461
|
-
* checkToPremise(authored, { field: 'age', actual: 25, state: 'met' })
|
|
462
|
-
* // { field: 'age', comparison: 'above', expected: 18, actual: 25, state: 'met' }
|
|
463
|
-
* ```
|
|
464
|
-
*/
|
|
465
|
-
function checkToPremise(check, result, labels) {
|
|
466
|
-
const field = formatField(check.field);
|
|
347
|
+
function projectMessageSlot(slot) {
|
|
467
348
|
return {
|
|
468
|
-
field:
|
|
469
|
-
|
|
470
|
-
comparison: check.operator,
|
|
471
|
-
expected: check.value,
|
|
472
|
-
actual: result.actual,
|
|
473
|
-
state: result.state
|
|
349
|
+
field: slot.field,
|
|
350
|
+
actual: projectValue(slot.actual)
|
|
474
351
|
};
|
|
475
352
|
}
|
|
476
353
|
/**
|
|
477
|
-
*
|
|
478
|
-
* and reading each from what the rule read.
|
|
354
|
+
* Finds the `{{dotted.path}}` tokens a message template reads.
|
|
479
355
|
*
|
|
480
356
|
* @remarks
|
|
481
|
-
*
|
|
482
|
-
*
|
|
483
|
-
* the pass. A rule with no result never ran, so its premises carry no `state` or `actual`.
|
|
357
|
+
* White space around a path is trimmed, and the white space inside it stays. A token of white
|
|
358
|
+
* space alone names one key, its last character, and `{{}}` is text.
|
|
484
359
|
*
|
|
485
|
-
* @param
|
|
486
|
-
* @
|
|
487
|
-
* @param labels - Optional field-to-label overrides, keyed by dot-joined field
|
|
488
|
-
* @returns A fresh, ordered list of rich premises
|
|
360
|
+
* @param template - The message template
|
|
361
|
+
* @returns Each token's dotted path, in order, repeats included
|
|
489
362
|
*
|
|
490
363
|
* @example
|
|
491
364
|
* ```ts
|
|
492
|
-
* import {
|
|
493
|
-
*
|
|
494
|
-
*
|
|
495
|
-
* const rule = createRule('licensed', [createAtom('licensed', 'equals', false)], createAtom('blocked', 'equals', true))
|
|
496
|
-
* const resolved = {
|
|
497
|
-
* id: 'licensed',
|
|
498
|
-
* state: 'met' as const,
|
|
499
|
-
* premises: ['met' as const],
|
|
500
|
-
* checks: [{ field: 'licensed', state: 'met' as const, actual: false }],
|
|
501
|
-
* }
|
|
502
|
-
* ruleToPremises(rule, resolved)[0]?.state // 'met'
|
|
365
|
+
* import { findMessageTokens } from '@orkestrel/qualifier'
|
|
366
|
+
*
|
|
367
|
+
* findMessageTokens('Age {{ age }} in {{home.city}}') // ['age', 'home.city']
|
|
503
368
|
* ```
|
|
504
369
|
*/
|
|
505
|
-
function
|
|
506
|
-
return
|
|
507
|
-
const read = resolved?.checks[index];
|
|
508
|
-
if (read !== void 0) return checkToPremise(atom.check, read, labels);
|
|
509
|
-
const { state: _state, actual: _actual, ...unrun } = checkToPremise(atom.check, {
|
|
510
|
-
field: atom.check.field,
|
|
511
|
-
state: "unread",
|
|
512
|
-
actual: void 0
|
|
513
|
-
}, labels);
|
|
514
|
-
return unrun;
|
|
515
|
-
});
|
|
370
|
+
function findMessageTokens(template) {
|
|
371
|
+
return [...template.matchAll(MESSAGE_TOKEN)].map((match) => match[1] ?? "");
|
|
516
372
|
}
|
|
517
373
|
/**
|
|
518
374
|
* Locates an authored {@link Rule} by id.
|
|
@@ -564,7 +420,10 @@ function findRule(definition, id) {
|
|
|
564
420
|
* count: 1,
|
|
565
421
|
* success: true,
|
|
566
422
|
* trace: [],
|
|
567
|
-
*
|
|
423
|
+
* refusals: [],
|
|
424
|
+
* faults: [],
|
|
425
|
+
* repairs: [],
|
|
426
|
+
* defaulted: [],
|
|
568
427
|
* }
|
|
569
428
|
* reasonResultToProjection(cap, result) // 500000
|
|
570
429
|
* ```
|
|
@@ -696,32 +555,135 @@ function describeReservedConclusions(definition) {
|
|
|
696
555
|
return definition.passes.flatMap((pass) => pass.reasoning !== "logical" ? [] : pass.rules.filter((rule) => Object.hasOwn(extractConclusions(rule.conclusion), "conclusion")).map((rule) => `Rule '${rule.id}' in pass '${pass.id}' concludes the reserved key 'conclusion'`));
|
|
697
556
|
}
|
|
698
557
|
/**
|
|
699
|
-
*
|
|
558
|
+
* Finds each rule a logical result does not answer: one it left unread, and an enabled one it
|
|
559
|
+
* omits.
|
|
700
560
|
*
|
|
701
|
-
* @param
|
|
702
|
-
* @
|
|
561
|
+
* @param definition - The logical pass
|
|
562
|
+
* @param result - The result of the pass
|
|
563
|
+
* @returns The rule ids, each once, the unread ones first
|
|
703
564
|
*
|
|
704
565
|
* @example
|
|
705
566
|
* ```ts
|
|
706
|
-
* import {
|
|
567
|
+
* import { findUnansweredRules } from '@orkestrel/qualifier'
|
|
568
|
+
* import { createAtom, createLogicalDefinition, createRule } from '@orkestrel/reason'
|
|
707
569
|
*
|
|
708
|
-
* const
|
|
709
|
-
*
|
|
570
|
+
* const gates = createLogicalDefinition('gates', 'Gates', [
|
|
571
|
+
* createRule('licensed', [createAtom('licensed', 'equals', false)], createAtom('blocked', 'equals', true)),
|
|
572
|
+
* ])
|
|
573
|
+
* findUnansweredRules(gates, {
|
|
710
574
|
* reasoning: 'logical',
|
|
711
|
-
* conclusion: '
|
|
712
|
-
* rules: [
|
|
575
|
+
* conclusion: 'unmet',
|
|
576
|
+
* rules: [],
|
|
577
|
+
* count: 0,
|
|
578
|
+
* success: true,
|
|
579
|
+
* trace: [],
|
|
580
|
+
* refusals: [],
|
|
581
|
+
* faults: [],
|
|
582
|
+
* repairs: [],
|
|
583
|
+
* defaulted: [],
|
|
584
|
+
* }) // ['licensed']
|
|
585
|
+
* ```
|
|
586
|
+
*/
|
|
587
|
+
function findUnansweredRules(definition, result) {
|
|
588
|
+
const answered = new Set(result.rules.map((rule) => rule.id));
|
|
589
|
+
return [.../* @__PURE__ */ new Set([...result.rules.filter((rule) => rule.state === "unread").map((rule) => rule.id), ...definition.rules.filter((rule) => rule.enabled !== false && !answered.has(rule.id)).map((rule) => rule.id)])];
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* Collects why an answer refused: the engine's own refusals, and what the answer leaves out or
|
|
593
|
+
* contradicts by itself.
|
|
594
|
+
*
|
|
595
|
+
* @remarks
|
|
596
|
+
* A failure naming no refusal and no fault is `silent`. A success is `malformed` beside a refusal
|
|
597
|
+
* or a fault, and at a value that is not finite, held in `actual`. Whatever verdict it claims, an
|
|
598
|
+
* answer is `malformed` for each rule it lists more than once, named in `rule`.
|
|
599
|
+
*
|
|
600
|
+
* @param result - The engine's answer
|
|
601
|
+
* @returns The refusals, with no path of this package's
|
|
602
|
+
*
|
|
603
|
+
* @example
|
|
604
|
+
* ```ts
|
|
605
|
+
* import { collectAnswerRefusals } from '@orkestrel/qualifier'
|
|
606
|
+
*
|
|
607
|
+
* collectAnswerRefusals({
|
|
608
|
+
* reasoning: 'quantitative',
|
|
609
|
+
* value: Number.NaN,
|
|
610
|
+
* groups: [],
|
|
611
|
+
* count: 0,
|
|
612
|
+
* success: true,
|
|
613
|
+
* trace: [],
|
|
614
|
+
* refusals: [],
|
|
615
|
+
* faults: [],
|
|
616
|
+
* repairs: [],
|
|
617
|
+
* defaulted: [],
|
|
618
|
+
* }) // [{ cause: 'malformed', actual: NaN }]
|
|
619
|
+
* ```
|
|
620
|
+
*/
|
|
621
|
+
function collectAnswerRefusals(result) {
|
|
622
|
+
const { refusals, faults, success } = result;
|
|
623
|
+
const named = refusals.length > 0 || faults.length > 0;
|
|
624
|
+
const repeated = result.reasoning === "logical" ? findDuplicates(result.rules).map((rule) => ({
|
|
625
|
+
cause: "malformed",
|
|
626
|
+
rule
|
|
627
|
+
})) : [];
|
|
628
|
+
const silent = named ? [] : [{ cause: "silent" }];
|
|
629
|
+
if (!success) return [
|
|
630
|
+
...refusals,
|
|
631
|
+
...silent,
|
|
632
|
+
...repeated
|
|
633
|
+
];
|
|
634
|
+
const beside = named ? [{ cause: "malformed" }] : [];
|
|
635
|
+
const valued = result.reasoning === "quantitative" && !isFiniteNumber(result.value) ? [{
|
|
636
|
+
cause: "malformed",
|
|
637
|
+
actual: result.value
|
|
638
|
+
}] : [];
|
|
639
|
+
return [
|
|
640
|
+
...refusals,
|
|
641
|
+
...beside,
|
|
642
|
+
...valued,
|
|
643
|
+
...repeated
|
|
644
|
+
];
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* Collects why a pass refused: the engine's own refusals under the pass id, and what its answer
|
|
648
|
+
* leaves out or contradicts.
|
|
649
|
+
*
|
|
650
|
+
* @remarks
|
|
651
|
+
* An injected engine answers through an interface, so its verdict is checked against the rest of
|
|
652
|
+
* its answer, as `collectAnswerRefusals` does, and against its pass. An answer of another
|
|
653
|
+
* reasoning is `malformed`, and so is a success for each rule it does not answer, named in `rule`.
|
|
654
|
+
*
|
|
655
|
+
* @param pass - The pass the engine ran
|
|
656
|
+
* @param result - The engine's answer
|
|
657
|
+
* @returns The refusals, each with the pass id at the head of its `path`
|
|
658
|
+
*
|
|
659
|
+
* @example
|
|
660
|
+
* ```ts
|
|
661
|
+
* import { collectPassRefusals } from '@orkestrel/qualifier'
|
|
662
|
+
* import { createQuantitativeDefinition } from '@orkestrel/reason'
|
|
663
|
+
*
|
|
664
|
+
* collectPassRefusals(createQuantitativeDefinition('cap', 'Cap', []), {
|
|
665
|
+
* reasoning: 'quantitative',
|
|
666
|
+
* value: 0,
|
|
667
|
+
* groups: [],
|
|
713
668
|
* count: 0,
|
|
714
669
|
* success: false,
|
|
715
670
|
* trace: [],
|
|
716
|
-
*
|
|
671
|
+
* refusals: [],
|
|
717
672
|
* faults: [],
|
|
718
673
|
* repairs: [],
|
|
719
674
|
* defaulted: [],
|
|
720
|
-
* }) //
|
|
675
|
+
* }) // [{ cause: 'silent', path: ['cap'] }]
|
|
721
676
|
* ```
|
|
722
677
|
*/
|
|
723
|
-
function
|
|
724
|
-
|
|
678
|
+
function collectPassRefusals(pass, result) {
|
|
679
|
+
if (result.reasoning !== pass.reasoning) return nestRefusals([...result.refusals, { cause: "malformed" }], pass.id);
|
|
680
|
+
const refusals = collectAnswerRefusals(result);
|
|
681
|
+
const refused = new Set(refusals.filter(({ cause }) => cause === "malformed").map(({ rule }) => rule));
|
|
682
|
+
const unanswered = result.success && pass.reasoning === "logical" && result.reasoning === "logical" ? findUnansweredRules(pass, result).filter((rule) => !refused.has(rule)) : [];
|
|
683
|
+
return nestRefusals([...refusals, ...unanswered.map((rule) => ({
|
|
684
|
+
cause: "malformed",
|
|
685
|
+
rule
|
|
686
|
+
}))], pass.id);
|
|
725
687
|
}
|
|
726
688
|
/**
|
|
727
689
|
* Describes each message token no field of the definition's shape answers.
|
|
@@ -756,8 +718,9 @@ function describeUnknownTokens(definition) {
|
|
|
756
718
|
* Projects a result to the JSON its contract reads.
|
|
757
719
|
*
|
|
758
720
|
* @remarks
|
|
759
|
-
* A
|
|
760
|
-
* so a result crosses a boundary as this projection, each `actual` on its
|
|
721
|
+
* A check's, a slot's, and a refusal's `actual` holds whatever the run read, and a derivation's
|
|
722
|
+
* `value` may be `NaN`, so a result crosses a boundary as this projection, each `actual` on its
|
|
723
|
+
* own budget.
|
|
761
724
|
*
|
|
762
725
|
* @param result - The qualification result
|
|
763
726
|
* @returns Its JSON projection, which `qualificationResultContract` admits
|
|
@@ -775,19 +738,28 @@ function describeUnknownTokens(definition) {
|
|
|
775
738
|
function projectQualificationResult(result) {
|
|
776
739
|
const findings = result.findings.map((finding) => ({
|
|
777
740
|
...finding,
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
741
|
+
...finding.message === void 0 ? {} : { message: finding.message.map((part) => isString(part) ? part : projectMessageSlot(part)) },
|
|
742
|
+
checks: finding.checks.map((check) => projectCheck(check))
|
|
743
|
+
}));
|
|
744
|
+
const derivations = result.derivations.map((derivation) => ({
|
|
745
|
+
...derivation,
|
|
746
|
+
refusals: derivation.refusals.map((refusal) => projectRefusal(refusal))
|
|
782
747
|
}));
|
|
748
|
+
const refusals = result.refusals.map((refusal) => projectRefusal(refusal));
|
|
783
749
|
return projectValue({
|
|
784
750
|
...result,
|
|
785
|
-
findings
|
|
751
|
+
findings,
|
|
752
|
+
derivations,
|
|
753
|
+
refusals
|
|
786
754
|
}, /* @__PURE__ */ new Set(), { left: Number.POSITIVE_INFINITY });
|
|
787
755
|
}
|
|
788
756
|
/**
|
|
789
757
|
* Projects a quantitative result into a {@link Derivation} audit record.
|
|
790
758
|
*
|
|
759
|
+
* @remarks
|
|
760
|
+
* `refusals` are what `collectAnswerRefusals` collects, and `success` is false exactly when they or
|
|
761
|
+
* the faults hold an entry, whatever verdict the result claims.
|
|
762
|
+
*
|
|
791
763
|
* @param id - The pass id
|
|
792
764
|
* @param result - The evaluated quantitative result
|
|
793
765
|
* @returns A fresh derivation
|
|
@@ -796,18 +768,21 @@ function projectQualificationResult(result) {
|
|
|
796
768
|
* ```ts
|
|
797
769
|
* import { quantitativeResultToDerivation } from '@orkestrel/qualifier'
|
|
798
770
|
*
|
|
799
|
-
* const result = { reasoning: 'quantitative' as const, value: 500_000, groups: [], count: 1, success: true, trace: ['cap: 500000'],
|
|
771
|
+
* const result = { reasoning: 'quantitative' as const, value: 500_000, groups: [], count: 1, success: true, trace: ['cap: 500000'], refusals: [], faults: [], repairs: [], defaulted: [] }
|
|
800
772
|
* quantitativeResultToDerivation('cap', result)
|
|
801
|
-
* // { id: 'cap', value: 500000, success: true, trace: ['cap: 500000'],
|
|
773
|
+
* // { id: 'cap', value: 500000, success: true, trace: ['cap: 500000'], refusals: [], faults: [] }
|
|
802
774
|
* ```
|
|
803
775
|
*/
|
|
804
776
|
function quantitativeResultToDerivation(id, result) {
|
|
777
|
+
const refusals = [...collectAnswerRefusals(result)];
|
|
778
|
+
const faults = [...result.faults];
|
|
805
779
|
return {
|
|
806
780
|
id,
|
|
807
781
|
value: result.value,
|
|
808
|
-
success:
|
|
782
|
+
success: refusals.length === 0 && faults.length === 0,
|
|
809
783
|
trace: [...result.trace],
|
|
810
|
-
|
|
784
|
+
refusals,
|
|
785
|
+
faults
|
|
811
786
|
};
|
|
812
787
|
}
|
|
813
788
|
/**
|
|
@@ -855,13 +830,14 @@ function mergeQualificationContext(context, id, projection) {
|
|
|
855
830
|
* @remarks
|
|
856
831
|
* `state` is the state of the ruling's rule. A rule the result omits reads `unmet` when it is
|
|
857
832
|
* disabled, which is authored, and `unread` otherwise, because a refused pass evaluated no rule.
|
|
858
|
-
*
|
|
833
|
+
* `checks` are the rule result's own, empty for a rule the result omits. A rule the result lists
|
|
834
|
+
* more than once reads `unread` with no checks, because no entry answers for it. The message is
|
|
835
|
+
* split against the subject.
|
|
859
836
|
*
|
|
860
837
|
* @param ruling - The authored ruling
|
|
861
838
|
* @param pass - The pass the ruling's rule lives in
|
|
862
839
|
* @param result - The pass's evaluated logical result
|
|
863
840
|
* @param subject - The pre-projection subject snapshot the pass evaluated
|
|
864
|
-
* @param labels - Optional field-to-label overrides, keyed by dot-joined field
|
|
865
841
|
* @returns A fresh finding
|
|
866
842
|
*
|
|
867
843
|
* @example
|
|
@@ -880,7 +856,7 @@ function mergeQualificationContext(context, id, projection) {
|
|
|
880
856
|
* count: 1,
|
|
881
857
|
* success: true,
|
|
882
858
|
* trace: [],
|
|
883
|
-
*
|
|
859
|
+
* refusals: [],
|
|
884
860
|
* faults: [],
|
|
885
861
|
* repairs: [],
|
|
886
862
|
* defaulted: [],
|
|
@@ -888,19 +864,20 @@ function mergeQualificationContext(context, id, projection) {
|
|
|
888
864
|
* rulingToFinding(ruling, gates, result, { licensed: false }).state // 'met'
|
|
889
865
|
* ```
|
|
890
866
|
*/
|
|
891
|
-
function rulingToFinding(ruling, pass, result, subject
|
|
892
|
-
const
|
|
867
|
+
function rulingToFinding(ruling, pass, result, subject) {
|
|
868
|
+
const entries = result.rules.filter((resolved) => resolved.id === ruling.rule);
|
|
869
|
+
const repeated = entries.length > 1;
|
|
870
|
+
const entry = repeated ? void 0 : entries[0];
|
|
893
871
|
const rule = pass.reasoning === "logical" ? findRule(pass, ruling.rule) : void 0;
|
|
894
|
-
const premises = rule === void 0 ? [] : ruleToPremises(rule, entry, labels);
|
|
895
872
|
return {
|
|
896
873
|
id: ruling.id,
|
|
897
874
|
pass: ruling.pass,
|
|
898
875
|
rule: ruling.rule,
|
|
899
876
|
effect: ruling.effect,
|
|
900
877
|
...ruling.scope === void 0 ? {} : { scope: ruling.scope },
|
|
901
|
-
state: entry?.state ?? (rule?.enabled === false ? "unmet" : "unread"),
|
|
902
|
-
...ruling.message === void 0 ? {} : { message:
|
|
903
|
-
|
|
878
|
+
state: entry?.state ?? (rule?.enabled === false && !repeated ? "unmet" : "unread"),
|
|
879
|
+
...ruling.message === void 0 ? {} : { message: splitMessage(ruling.message, subject) },
|
|
880
|
+
checks: [...entry?.checks ?? []]
|
|
904
881
|
};
|
|
905
882
|
}
|
|
906
883
|
/**
|
|
@@ -920,7 +897,7 @@ function rulingToFinding(ruling, pass, result, subject, labels) {
|
|
|
920
897
|
* ```ts
|
|
921
898
|
* import { deriveFindingEligibility } from '@orkestrel/qualifier'
|
|
922
899
|
*
|
|
923
|
-
* const finding = { id: 'license', pass: 'gates', rule: 'licensed', effect: 'restriction' as const, state: 'met' as const,
|
|
900
|
+
* const finding = { id: 'license', pass: 'gates', rule: 'licensed', effect: 'restriction' as const, state: 'met' as const, checks: [] }
|
|
924
901
|
* deriveFindingEligibility([finding]) // 'ineligible'
|
|
925
902
|
* ```
|
|
926
903
|
*/
|
|
@@ -951,8 +928,8 @@ function combineEligibilities(eligibilities) {
|
|
|
951
928
|
*
|
|
952
929
|
* @remarks
|
|
953
930
|
* Only scoped findings that are `met` contribute. Each named scope combines its own
|
|
954
|
-
* findings' impacts by severity.
|
|
955
|
-
*
|
|
931
|
+
* findings' impacts by severity. A scope no finding is met for is absent, which is eligible only
|
|
932
|
+
* where the run succeeded, because a finding left unread leaves its scope absent too.
|
|
956
933
|
*
|
|
957
934
|
* @param findings - The resolved findings
|
|
958
935
|
* @returns A fresh record of scope to its combined eligibility
|
|
@@ -961,7 +938,7 @@ function combineEligibilities(eligibilities) {
|
|
|
961
938
|
* ```ts
|
|
962
939
|
* import { deriveScopeEligibilities } from '@orkestrel/qualifier'
|
|
963
940
|
*
|
|
964
|
-
* const finding = { id: 'coastal', pass: 'wind', rule: 'coastal', effect: 'restriction' as const, scope: 'wind', state: 'met' as const,
|
|
941
|
+
* const finding = { id: 'coastal', pass: 'wind', rule: 'coastal', effect: 'restriction' as const, scope: 'wind', state: 'met' as const, checks: [] }
|
|
965
942
|
* deriveScopeEligibilities([finding]) // { wind: 'ineligible' }
|
|
966
943
|
* ```
|
|
967
944
|
*/
|
|
@@ -1166,6 +1143,142 @@ function describeUnreadDerivations(definition) {
|
|
|
1166
1143
|
});
|
|
1167
1144
|
return warnings;
|
|
1168
1145
|
}
|
|
1146
|
+
// src/core/cloners.ts
|
|
1147
|
+
/**
|
|
1148
|
+
* Copies a list, reading its length and each entry once.
|
|
1149
|
+
*
|
|
1150
|
+
* @param value - The value to copy
|
|
1151
|
+
* @param clone - Copies one entry. Default: carries it as it is
|
|
1152
|
+
* @returns A fresh list, or the value itself when it is no list
|
|
1153
|
+
*
|
|
1154
|
+
* @example
|
|
1155
|
+
* ```ts
|
|
1156
|
+
* import { cloneList } from '@orkestrel/qualifier'
|
|
1157
|
+
*
|
|
1158
|
+
* cloneList(['a', 'b']) // ['a', 'b']
|
|
1159
|
+
* cloneList('a') // 'a'
|
|
1160
|
+
* ```
|
|
1161
|
+
*/
|
|
1162
|
+
function cloneList(value, clone) {
|
|
1163
|
+
if (!isArray(value)) return value;
|
|
1164
|
+
return Array.from({ length: value.length }, (_, index) => {
|
|
1165
|
+
const entry = value[index];
|
|
1166
|
+
return clone === void 0 ? entry : clone(entry);
|
|
1167
|
+
});
|
|
1168
|
+
}
|
|
1169
|
+
/**
|
|
1170
|
+
* Copies the named members a value has into a plain record, reading each once.
|
|
1171
|
+
*
|
|
1172
|
+
* @remarks
|
|
1173
|
+
* An inherited member and an accessor count, so a class instance keeps what it gives. A member the
|
|
1174
|
+
* names leave out is dropped, and one the value lacks stays absent.
|
|
1175
|
+
*
|
|
1176
|
+
* @param value - The value to copy
|
|
1177
|
+
* @param names - The members to read
|
|
1178
|
+
* @param clones - Copies a member of that name; any other is carried as it is. Default: none
|
|
1179
|
+
* @returns A fresh record, or the value itself when it is a list or no object
|
|
1180
|
+
*
|
|
1181
|
+
* @example
|
|
1182
|
+
* ```ts
|
|
1183
|
+
* import { cloneMembers } from '@orkestrel/qualifier'
|
|
1184
|
+
*
|
|
1185
|
+
* cloneMembers({ a: 1, b: 2 }, ['a']) // { a: 1 }
|
|
1186
|
+
* ```
|
|
1187
|
+
*/
|
|
1188
|
+
function cloneMembers(value, names, clones = {}) {
|
|
1189
|
+
if (!isObject(value) || isArray(value)) return value;
|
|
1190
|
+
return Object.fromEntries(names.filter((name) => Reflect.has(value, name)).map((name) => {
|
|
1191
|
+
const member = Reflect.get(value, name);
|
|
1192
|
+
const clone = isString(name) && Object.hasOwn(clones, name) ? clones[name] : void 0;
|
|
1193
|
+
return [name, clone === void 0 ? member : clone(member)];
|
|
1194
|
+
}));
|
|
1195
|
+
}
|
|
1196
|
+
/**
|
|
1197
|
+
* Copies an untrusted engine's answer, reading each member a verdict rests on once, and admits the
|
|
1198
|
+
* copy alone.
|
|
1199
|
+
*
|
|
1200
|
+
* @remarks
|
|
1201
|
+
* Reading the answer again could give another verdict, so everything is derived from the copy. A
|
|
1202
|
+
* check's and a refusal's `actual` and a check's expected value are carried by identity, and so are
|
|
1203
|
+
* the members that hold no verdict, such as a quantitative result's `groups`. A member its contract
|
|
1204
|
+
* does not name is dropped. It never throws.
|
|
1205
|
+
*
|
|
1206
|
+
* @param value - The answer
|
|
1207
|
+
* @returns The copy, or `undefined` when it is no reason result or throws on being read
|
|
1208
|
+
*
|
|
1209
|
+
* @example
|
|
1210
|
+
* ```ts
|
|
1211
|
+
* import { cloneReasonResult } from '@orkestrel/qualifier'
|
|
1212
|
+
* import { createLogicalDefinition, createLogicalReasoner } from '@orkestrel/reason'
|
|
1213
|
+
*
|
|
1214
|
+
* const result = createLogicalReasoner().reason({}, createLogicalDefinition('d', 'd', []))
|
|
1215
|
+
* cloneReasonResult(result)?.success // true
|
|
1216
|
+
* cloneReasonResult({ success: true }) // undefined
|
|
1217
|
+
* ```
|
|
1218
|
+
*/
|
|
1219
|
+
function cloneReasonResult(value) {
|
|
1220
|
+
const outcome = attempt(() => cloneMembers(value, [
|
|
1221
|
+
"reasoning",
|
|
1222
|
+
"value",
|
|
1223
|
+
"groups",
|
|
1224
|
+
"conclusion",
|
|
1225
|
+
"rules",
|
|
1226
|
+
"count",
|
|
1227
|
+
"solutions",
|
|
1228
|
+
"bindings",
|
|
1229
|
+
"derived",
|
|
1230
|
+
"proof",
|
|
1231
|
+
"success",
|
|
1232
|
+
"trace",
|
|
1233
|
+
"refusals",
|
|
1234
|
+
"faults",
|
|
1235
|
+
"repairs",
|
|
1236
|
+
"defaulted"
|
|
1237
|
+
], {
|
|
1238
|
+
trace: (trace) => cloneList(trace),
|
|
1239
|
+
refusals: (refusals) => cloneList(refusals, (refusal) => cloneMembers(refusal, [
|
|
1240
|
+
"cause",
|
|
1241
|
+
"path",
|
|
1242
|
+
"rule",
|
|
1243
|
+
"group",
|
|
1244
|
+
"factor",
|
|
1245
|
+
"equation",
|
|
1246
|
+
"deduction",
|
|
1247
|
+
"index",
|
|
1248
|
+
"key",
|
|
1249
|
+
"operator",
|
|
1250
|
+
"field",
|
|
1251
|
+
"actual",
|
|
1252
|
+
"depth",
|
|
1253
|
+
"message"
|
|
1254
|
+
], {
|
|
1255
|
+
path: (path) => cloneList(path),
|
|
1256
|
+
field: (field) => cloneList(field)
|
|
1257
|
+
})),
|
|
1258
|
+
faults: (faults) => cloneList(faults, (fault) => isRecord(fault) ? cloneMembers(fault, Reflect.ownKeys(fault), {
|
|
1259
|
+
path: (path) => cloneList(path),
|
|
1260
|
+
expected: (expected) => cloneList(expected)
|
|
1261
|
+
}) : fault),
|
|
1262
|
+
rules: (rules) => cloneList(rules, (rule) => cloneMembers(rule, [
|
|
1263
|
+
"id",
|
|
1264
|
+
"state",
|
|
1265
|
+
"premises",
|
|
1266
|
+
"checks"
|
|
1267
|
+
], {
|
|
1268
|
+
premises: (premises) => cloneList(premises),
|
|
1269
|
+
checks: (checks) => cloneList(checks, (read) => cloneMembers(read, [
|
|
1270
|
+
"check",
|
|
1271
|
+
"state",
|
|
1272
|
+
"actual"
|
|
1273
|
+
], { check: (check) => cloneMembers(check, [
|
|
1274
|
+
"field",
|
|
1275
|
+
"operator",
|
|
1276
|
+
"value"
|
|
1277
|
+
], { field: (field) => cloneList(field) }) }))
|
|
1278
|
+
}))
|
|
1279
|
+
}));
|
|
1280
|
+
return isSuccess(outcome) && isReasonResult(outcome.value) ? outcome.value : void 0;
|
|
1281
|
+
}
|
|
1169
1282
|
// src/core/shapers.ts
|
|
1170
1283
|
/** Describes a `Ruling`. */
|
|
1171
1284
|
var rulingShape = objectShape({
|
|
@@ -1203,15 +1316,10 @@ function createQualificationDefinitionShape() {
|
|
|
1203
1316
|
shape: optionalShape(shapeShape())
|
|
1204
1317
|
});
|
|
1205
1318
|
}
|
|
1206
|
-
/** Describes a `
|
|
1207
|
-
var
|
|
1208
|
-
field:
|
|
1209
|
-
|
|
1210
|
-
description: optionalShape(stringShape()),
|
|
1211
|
-
comparison: optionalShape(literalShape(COMPARISONS)),
|
|
1212
|
-
expected: optionalShape(jsonShape()),
|
|
1213
|
-
actual: optionalShape(jsonShape()),
|
|
1214
|
-
state: optionalShape(literalShape(CHECK_STATES))
|
|
1319
|
+
/** Describes a `MessageSlot` in its JSON projection. */
|
|
1320
|
+
var messageSlotShape = objectShape({
|
|
1321
|
+
field: fieldPathShape,
|
|
1322
|
+
actual: optionalShape(jsonShape())
|
|
1215
1323
|
});
|
|
1216
1324
|
/** Describes a `Finding` in its JSON projection. */
|
|
1217
1325
|
var findingShape = objectShape({
|
|
@@ -1221,8 +1329,8 @@ var findingShape = objectShape({
|
|
|
1221
1329
|
effect: literalShape(QUALIFICATION_EFFECTS),
|
|
1222
1330
|
scope: optionalShape(stringShape()),
|
|
1223
1331
|
state: literalShape(CHECK_STATES),
|
|
1224
|
-
message: optionalShape(stringShape()),
|
|
1225
|
-
|
|
1332
|
+
message: optionalShape(arrayShape(unionShape(stringShape(), messageSlotShape))),
|
|
1333
|
+
checks: arrayShape(checkResultShape)
|
|
1226
1334
|
});
|
|
1227
1335
|
/** Describes a `Derivation` in its JSON projection, where a non-finite number is `null`. */
|
|
1228
1336
|
var derivationShape = objectShape({
|
|
@@ -1230,7 +1338,8 @@ var derivationShape = objectShape({
|
|
|
1230
1338
|
value: nullableShape(numberShape()),
|
|
1231
1339
|
success: booleanShape(),
|
|
1232
1340
|
trace: arrayShape(stringShape()),
|
|
1233
|
-
|
|
1341
|
+
refusals: arrayShape(reasonRefusalShape),
|
|
1342
|
+
faults: arrayShape(auditFaultShape())
|
|
1234
1343
|
});
|
|
1235
1344
|
/** Describes a `QualificationResult` in its JSON projection. */
|
|
1236
1345
|
var qualificationResultShape = objectShape({
|
|
@@ -1242,7 +1351,7 @@ var qualificationResultShape = objectShape({
|
|
|
1242
1351
|
derivations: arrayShape(derivationShape),
|
|
1243
1352
|
success: booleanShape(),
|
|
1244
1353
|
trace: arrayShape(stringShape()),
|
|
1245
|
-
|
|
1354
|
+
refusals: arrayShape(reasonRefusalShape),
|
|
1246
1355
|
faults: arrayShape(auditFaultShape()),
|
|
1247
1356
|
repairs: arrayShape(auditFaultShape()),
|
|
1248
1357
|
defaulted: arrayShape(fieldPathShape)
|
|
@@ -1266,7 +1375,6 @@ var Qualifier = class {
|
|
|
1266
1375
|
#engine;
|
|
1267
1376
|
#owned;
|
|
1268
1377
|
#validate;
|
|
1269
|
-
#labels;
|
|
1270
1378
|
#door = createSubjectDoor();
|
|
1271
1379
|
#valid = /* @__PURE__ */ new WeakSet();
|
|
1272
1380
|
#shaped = /* @__PURE__ */ new WeakSet();
|
|
@@ -1282,7 +1390,6 @@ var Qualifier = class {
|
|
|
1282
1390
|
bail: false
|
|
1283
1391
|
});
|
|
1284
1392
|
this.#validate = options?.validate ?? true;
|
|
1285
|
-
this.#labels = options?.labels;
|
|
1286
1393
|
}
|
|
1287
1394
|
/**
|
|
1288
1395
|
* Holds the typed observation surface carrying `derive`, `finding`, `qualify`, and
|
|
@@ -1312,7 +1419,8 @@ var Qualifier = class {
|
|
|
1312
1419
|
* `shape` on the definition admits the subject through a door first. An unscoped
|
|
1313
1420
|
* restriction stops the remaining passes because no later finding can be more severe.
|
|
1314
1421
|
* A pass left unread projects nothing and reads `referral`, and the run goes on, so a later
|
|
1315
|
-
* restriction can still outrank it.
|
|
1422
|
+
* restriction can still outrank it. A refusal stops the run, and so does an answer that
|
|
1423
|
+
* contradicts itself or its pass, which is refused as `malformed`.
|
|
1316
1424
|
*
|
|
1317
1425
|
* @param subject - The record to qualify
|
|
1318
1426
|
* @param definition - The authored qualification definition
|
|
@@ -1466,16 +1574,15 @@ var Qualifier = class {
|
|
|
1466
1574
|
const admission = this.#door.admit(subject, definition.shape);
|
|
1467
1575
|
const admitted = admission.subject;
|
|
1468
1576
|
if (admitted !== void 0) assertSubject(admitted);
|
|
1469
|
-
if (admitted === void 0) return this.#finish(definition, [], [], [], [...admission.
|
|
1577
|
+
if (admitted === void 0) return this.#finish(definition, [], [], [], [...admission.refusals], [...admission.faults], admission);
|
|
1470
1578
|
const steps = [{
|
|
1471
1579
|
context: {},
|
|
1472
|
-
subject: { ...admitted }
|
|
1473
|
-
success: true
|
|
1580
|
+
subject: { ...admitted }
|
|
1474
1581
|
}];
|
|
1475
1582
|
const findings = [];
|
|
1476
1583
|
const derivations = [];
|
|
1477
1584
|
const trace = [];
|
|
1478
|
-
const
|
|
1585
|
+
const refusals = [];
|
|
1479
1586
|
const faults = [];
|
|
1480
1587
|
const unread = /* @__PURE__ */ new Map();
|
|
1481
1588
|
for (const pass of definition.passes) {
|
|
@@ -1483,56 +1590,48 @@ var Qualifier = class {
|
|
|
1483
1590
|
const previous = steps.at(-1);
|
|
1484
1591
|
if (previous === void 0) throw new Error("Qualification has no initial step");
|
|
1485
1592
|
const evaluated = previous.subject;
|
|
1486
|
-
const result = this.#reason(evaluated, pass);
|
|
1487
|
-
if (
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
success: false
|
|
1593
|
+
const result = cloneReasonResult(this.#reason(evaluated, pass));
|
|
1594
|
+
if (result === void 0) {
|
|
1595
|
+
refusals.push({
|
|
1596
|
+
cause: "malformed",
|
|
1597
|
+
path: [pass.id]
|
|
1492
1598
|
});
|
|
1493
1599
|
break;
|
|
1494
1600
|
}
|
|
1495
1601
|
trace.push(...result.trace.map((entry) => `${pass.id}: ${entry}`));
|
|
1496
|
-
const
|
|
1497
|
-
|
|
1498
|
-
...result.errors.filter((entry) => !said.has(entry)),
|
|
1499
|
-
...!result.success && result.errors.length === 0 ? ["The pass failed and said nothing"] : [],
|
|
1500
|
-
...result.success && hasUnreadRule(result) ? ["The pass succeeded beside an unread rule"] : []
|
|
1501
|
-
];
|
|
1502
|
-
errors.push(...fatal.map((entry) => `${pass.id}: ${entry}`));
|
|
1602
|
+
const refused = collectPassRefusals(pass, result);
|
|
1603
|
+
refusals.push(...refused);
|
|
1503
1604
|
const leaves = expandPassFaults(result.faults, unread);
|
|
1504
1605
|
faults.push(...leaves);
|
|
1505
1606
|
if (leaves.length > 0) unread.set(pass.id, leaves);
|
|
1506
|
-
if (result.reasoning === "quantitative") {
|
|
1607
|
+
if (pass.reasoning === "quantitative" && result.reasoning === "quantitative") {
|
|
1507
1608
|
const derivation = quantitativeResultToDerivation(pass.id, result);
|
|
1508
1609
|
derivations.push(derivation);
|
|
1509
1610
|
this.#emitter.emit("derive", derivation);
|
|
1510
1611
|
}
|
|
1511
|
-
if (result.reasoning === "logical") {
|
|
1612
|
+
if (pass.reasoning === "logical" && result.reasoning === "logical") {
|
|
1512
1613
|
const rulings = (definition.rulings ?? []).filter((ruling) => ruling.pass === pass.id);
|
|
1513
1614
|
for (const ruling of rulings) {
|
|
1514
|
-
const finding = rulingToFinding(ruling, pass, result, evaluated
|
|
1615
|
+
const finding = rulingToFinding(ruling, pass, result, evaluated);
|
|
1515
1616
|
findings.push(finding);
|
|
1516
1617
|
this.#emitter.emit("finding", finding);
|
|
1517
1618
|
}
|
|
1518
1619
|
}
|
|
1519
|
-
|
|
1620
|
+
if (refused.length > 0) break;
|
|
1621
|
+
if (deriveFindingEligibility(findings) === "ineligible") break;
|
|
1622
|
+
const context = result.success ? mergeQualificationContext(previous.context, pass.id, reasonResultToProjection(pass, result)) : previous.context;
|
|
1520
1623
|
steps.push({
|
|
1521
1624
|
context,
|
|
1522
1625
|
subject: {
|
|
1523
1626
|
...admitted,
|
|
1524
1627
|
...qualificationToRecord(context)
|
|
1525
|
-
}
|
|
1526
|
-
success: result.success && fatal.length === 0
|
|
1628
|
+
}
|
|
1527
1629
|
});
|
|
1528
|
-
if (fatal.length > 0) break;
|
|
1529
|
-
if (deriveFindingEligibility(findings) === "ineligible") break;
|
|
1530
1630
|
}
|
|
1531
|
-
|
|
1532
|
-
return this.#finish(definition, findings, derivations, trace, [...errors, ...leaves.map((fault) => describeFaults([fault]))], leaves, admission);
|
|
1631
|
+
return this.#finish(definition, findings, derivations, trace, refusals, filterUniqueFaults(faults), admission);
|
|
1533
1632
|
}
|
|
1534
|
-
#finish(definition, findings, derivations, trace,
|
|
1535
|
-
const success =
|
|
1633
|
+
#finish(definition, findings, derivations, trace, refusals, faults, admission) {
|
|
1634
|
+
const success = refusals.length === 0 && faults.length === 0;
|
|
1536
1635
|
const qualification = Object.freeze({
|
|
1537
1636
|
id: definition.id,
|
|
1538
1637
|
name: definition.name,
|
|
@@ -1542,7 +1641,7 @@ var Qualifier = class {
|
|
|
1542
1641
|
derivations: Object.freeze(derivations),
|
|
1543
1642
|
success,
|
|
1544
1643
|
trace: Object.freeze(trace),
|
|
1545
|
-
|
|
1644
|
+
refusals: Object.freeze(refusals),
|
|
1546
1645
|
faults: Object.freeze(faults),
|
|
1547
1646
|
repairs: Object.freeze([...admission.repairs]),
|
|
1548
1647
|
defaulted: Object.freeze([...admission.defaulted])
|
|
@@ -1596,11 +1695,9 @@ function createQualificationDefinitionContract() {
|
|
|
1596
1695
|
* A standalone qualifier creates and owns one shared quantitative-plus-logical
|
|
1597
1696
|
* reason engine, destroying it on `destroy()`. An injected `options.engine`
|
|
1598
1697
|
* remains caller-owned and is never destroyed. `validate` runs semantic
|
|
1599
|
-
* validation before every qualification. Default: `true`.
|
|
1600
|
-
* display name of a premise field, keyed by the dot-joined field path
|
|
1601
|
-
* `formatField` produces, and takes precedence over a premise's own `label`.
|
|
1698
|
+
* validation before every qualification. Default: `true`.
|
|
1602
1699
|
*
|
|
1603
|
-
* @param options - Optional injected engine, validation,
|
|
1700
|
+
* @param options - Optional injected engine, validation, and emitter hooks
|
|
1604
1701
|
* @returns A {@link QualifierInterface}
|
|
1605
1702
|
*
|
|
1606
1703
|
* @example Create a qualifier
|
|
@@ -1698,6 +1795,6 @@ function createRuling(id, pass, rule, effect, input) {
|
|
|
1698
1795
|
...input?.message === void 0 ? {} : { message: input.message }
|
|
1699
1796
|
};
|
|
1700
1797
|
}
|
|
1701
|
-
export { DEFAULT_QUALIFIER_VALIDATE, EFFECT_ELIGIBILITIES, ELIGIBILITIES, ELIGIBILITY_PRECEDENCE, MESSAGE_TOKEN, QUALIFICATION_EFFECTS, QUALIFICATION_KEY, Qualifier, QualifierError, assertSubject,
|
|
1798
|
+
export { DEFAULT_QUALIFIER_VALIDATE, EFFECT_ELIGIBILITIES, ELIGIBILITIES, ELIGIBILITY_PRECEDENCE, MESSAGE_TOKEN, QUALIFICATION_EFFECTS, QUALIFICATION_KEY, Qualifier, QualifierError, assertSubject, cloneList, cloneMembers, cloneReasonResult, collectAnswerRefusals, collectPassRefusals, combineEligibilities, createQualificationDefinition, createQualificationDefinitionContract, createQualificationDefinitionShape, createQualifier, createRuling, derivationShape, deriveFindingEligibility, deriveScopeEligibilities, describeEmptyLogicalPasses, describeMissingReferences, describeReservedConclusions, describeUnknownTokens, describeUnreadDerivations, expandPassFaults, findMessageTokens, findRule, findUnansweredRules, findingShape, hasReservedKey, isDefinitionMetadata, isDerivation, isEligibility, isEligibilityRecord, isFinding, isMessageSlot, isQualificationDefinition, isQualificationEffect, isQualificationPass, isQualificationResult, isQualifierError, isRuling, mapEngineError, mergeQualificationContext, messageSlotShape, projectMessageSlot, projectQualificationResult, qualificationResultContract, qualificationResultShape, qualificationToRecord, quantitativeResultToDerivation, readsProjection, reasonResultToProjection, rulingShape, rulingToFinding, scanDefinitionShape, splitMessage };
|
|
1702
1799
|
|
|
1703
1800
|
//# sourceMappingURL=index.js.map
|