@orkestrel/reason 0.0.4 → 0.0.6

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.
@@ -1,4 +1,4 @@
1
- import { arrayOf, isArray, isBoolean, isFiniteNumber, isNumber, isObject, isRecord, isString, lazyOf, literalOf, notOf, orOf, parseJSONAs, parseNumber, parseNumberField, recordOf, resolveField, unionOf, whereOf } from "@orkestrel/contract";
1
+ import { GUARD_DEPTH_LIMIT, arrayOf, holds, isArray, isBoolean, isFiniteNumber, isNumber, isObject, isRecord, isString, lazyOf, literalOf, notOf, orOf, parseJSONAs, parseNumber, parseNumberField, readArrayEntries, recordOf, resolveField, unionOf, whereOf } from "@orkestrel/contract";
2
2
  import { Emitter } from "@orkestrel/emitter";
3
3
  //#region src/core/constants.ts
4
4
  /**
@@ -769,6 +769,303 @@ function isDefinition(value) {
769
769
  return isQuantitativeDefinition(value) || isLogicalDefinition(value) || isSymbolicDefinition(value) || isInferentialDefinition(value);
770
770
  }
771
771
  /**
772
+ * Determine whether a value is a result-side {@link Fact}.
773
+ *
774
+ * @remarks
775
+ * Unlike the exact input guard {@link isFact}, this guard accepts unknown
776
+ * members, class instances, and every JavaScript `number` confidence.
777
+ *
778
+ * @param value - The value to test
779
+ * @returns `true` when every published fact member is valid for a result
780
+ *
781
+ * @example
782
+ * ```ts
783
+ * import { isResultFact } from '@src/core'
784
+ *
785
+ * isResultFact({ id: 'f1', predicate: 'bird', terms: ['tweety'], note: 'derived' }) // true
786
+ * isResultFact({ id: 'f1', predicate: 'bird' }) // false
787
+ * ```
788
+ */
789
+ function isResultFact(value) {
790
+ return whereOf(isObject, (result) => {
791
+ if (isArray(result)) return false;
792
+ const confidence = Reflect.get(result, "confidence");
793
+ return isString(Reflect.get(result, "id")) && isString(Reflect.get(result, "predicate")) && isArray(Reflect.get(result, "terms")) && (confidence === void 0 || isNumber(confidence));
794
+ })(value);
795
+ }
796
+ /**
797
+ * Determine whether a value is a {@link CheckResult}.
798
+ *
799
+ * @param value - The value to test
800
+ * @returns `true` when every published check-result member is valid
801
+ *
802
+ * @example
803
+ * ```ts
804
+ * import { isCheckResult } from '@src/core'
805
+ *
806
+ * isCheckResult({ field: 'age', met: true, actual: 30 }) // true
807
+ * isCheckResult({ field: 'age', met: 'yes', actual: 30 }) // false
808
+ * ```
809
+ */
810
+ function isCheckResult(value) {
811
+ return whereOf(isObject, (result) => {
812
+ if (isArray(result)) return false;
813
+ const error = Reflect.get(result, "error");
814
+ return isFieldPath(Reflect.get(result, "field")) && isBoolean(Reflect.get(result, "met")) && Reflect.has(result, "actual") && (error === void 0 || isString(error));
815
+ })(value);
816
+ }
817
+ /**
818
+ * Determine whether a value is a {@link FactorResult}.
819
+ *
820
+ * @param value - The value to test
821
+ * @returns `true` when every published factor-result member is valid
822
+ *
823
+ * @example
824
+ * ```ts
825
+ * import { isFactorResult } from '@src/core'
826
+ *
827
+ * isFactorResult({ id: 'age', applied: true, value: 5 }) // true
828
+ * isFactorResult({ id: 'age', applied: true, value: '5' }) // false
829
+ * ```
830
+ */
831
+ function isFactorResult(value) {
832
+ return whereOf(isObject, (result) => {
833
+ if (isArray(result)) return false;
834
+ const raw = Reflect.get(result, "raw");
835
+ const checks = Reflect.get(result, "checks");
836
+ return isString(Reflect.get(result, "id")) && isBoolean(Reflect.get(result, "applied")) && isNumber(Reflect.get(result, "value")) && (raw === void 0 || isNumber(raw)) && (checks === void 0 || arrayOf(isCheckResult)(checks));
837
+ })(value);
838
+ }
839
+ /**
840
+ * Determine whether a value is a {@link GroupResult}.
841
+ *
842
+ * @param value - The value to test
843
+ * @returns `true` when every published group-result member is valid
844
+ *
845
+ * @example
846
+ * ```ts
847
+ * import { isGroupResult } from '@src/core'
848
+ *
849
+ * isGroupResult({ id: 'risk', applied: true, value: 5, factors: [] }) // true
850
+ * isGroupResult({ id: 'risk', applied: true, value: 5 }) // false
851
+ * ```
852
+ */
853
+ function isGroupResult(value) {
854
+ return whereOf(isObject, (result) => {
855
+ return !isArray(result) && isString(Reflect.get(result, "id")) && isBoolean(Reflect.get(result, "applied")) && isNumber(Reflect.get(result, "value")) && arrayOf(isFactorResult)(Reflect.get(result, "factors"));
856
+ })(value);
857
+ }
858
+ /**
859
+ * Determine whether a value is a {@link RuleResult}.
860
+ *
861
+ * @param value - The value to test
862
+ * @returns `true` when every published rule-result member is valid
863
+ *
864
+ * @example
865
+ * ```ts
866
+ * import { isRuleResult } from '@src/core'
867
+ *
868
+ * isRuleResult({ id: 'adult', applied: true, premises: [true], conclusion: true }) // true
869
+ * isRuleResult({ id: 'adult', applied: true, premises: [1], conclusion: true }) // false
870
+ * ```
871
+ */
872
+ function isRuleResult(value) {
873
+ return whereOf(isObject, (result) => {
874
+ return !isArray(result) && isString(Reflect.get(result, "id")) && isBoolean(Reflect.get(result, "applied")) && arrayOf(isBoolean)(Reflect.get(result, "premises")) && isBoolean(Reflect.get(result, "conclusion"));
875
+ })(value);
876
+ }
877
+ /**
878
+ * Determine whether a value is a depth-bounded, acyclic {@link ProofNode} tree.
879
+ *
880
+ * @remarks
881
+ * Traversal is iterative and reuses `GUARD_DEPTH_LIMIT`. Active ancestors refuse cycles, and a
882
+ * settled-depth record prevents repeated validation of shared subtrees while keeping the bound
883
+ * exact: a node revisited no deeper than its validated depth is skipped, and one reached deeper
884
+ * is walked again. The traversal accepts an acyclic graph only when every reachable node is
885
+ * valid and every root-to-node path contains at most `GUARD_DEPTH_LIMIT` nodes; shared aliases
886
+ * and sibling order do not affect the verdict.
887
+ *
888
+ * @param value - The value to test
889
+ * @returns `true` when every proof node is valid within the runtime guard bound
890
+ *
891
+ * @example
892
+ * ```ts
893
+ * import { isProofNode } from '@src/core'
894
+ *
895
+ * isProofNode({ fact: 'bird', depth: 0, children: [{ fact: 'feathers', depth: 1 }] }) // true
896
+ * isProofNode({ fact: 'bird', children: [] }) // false
897
+ * ```
898
+ */
899
+ function isProofNode(value) {
900
+ return holds(() => {
901
+ if (!isObject(value) || isArray(value)) return false;
902
+ const active = /* @__PURE__ */ new WeakSet();
903
+ const settled = /* @__PURE__ */ new WeakMap();
904
+ const worklist = [[
905
+ value,
906
+ 1,
907
+ false
908
+ ]];
909
+ while (worklist.length > 0) {
910
+ const frame = worklist.pop();
911
+ if (frame === void 0) continue;
912
+ const node = frame[0];
913
+ const depth = frame[1];
914
+ if (frame[2]) {
915
+ active.delete(node);
916
+ const known = settled.get(node);
917
+ settled.set(node, known === void 0 || depth > known ? depth : known);
918
+ continue;
919
+ }
920
+ if (depth > GUARD_DEPTH_LIMIT || active.has(node)) return false;
921
+ const known = settled.get(node);
922
+ if (known !== void 0 && depth <= known) continue;
923
+ active.add(node);
924
+ worklist.push([
925
+ node,
926
+ depth,
927
+ true
928
+ ]);
929
+ const inference = Reflect.get(node, "inference");
930
+ const children = Reflect.get(node, "children");
931
+ if (!isString(Reflect.get(node, "fact")) || !isNumber(Reflect.get(node, "depth")) || inference !== void 0 && !isString(inference)) return false;
932
+ if (children === void 0) continue;
933
+ if (!isArray(children)) return false;
934
+ const entries = readArrayEntries(children);
935
+ if (!entries.success || !entries.value.dense) return false;
936
+ for (let index = entries.value.entries.length - 1; index >= 0; index -= 1) {
937
+ const child = entries.value.entries[index];
938
+ if (!isObject(child) || isArray(child)) return false;
939
+ worklist.push([
940
+ child,
941
+ depth + 1,
942
+ false
943
+ ]);
944
+ }
945
+ }
946
+ return true;
947
+ });
948
+ }
949
+ /**
950
+ * Determine whether a value is a {@link QuantitativeResult}.
951
+ *
952
+ * @param value - The value to test
953
+ * @returns `true` when every published quantitative-result member is valid
954
+ *
955
+ * @example
956
+ * ```ts
957
+ * import { isQuantitativeResult } from '@src/core'
958
+ *
959
+ * isQuantitativeResult({ reasoning: 'quantitative', value: 0, groups: [], count: 0, success: true, trace: [], errors: [] }) // true
960
+ * isQuantitativeResult({ reasoning: 'quantitative' }) // false
961
+ * ```
962
+ */
963
+ function isQuantitativeResult(value) {
964
+ return whereOf(isObject, (result) => {
965
+ return !isArray(result) && Reflect.get(result, "reasoning") === "quantitative" && isNumber(Reflect.get(result, "value")) && arrayOf(isGroupResult)(Reflect.get(result, "groups")) && isNumber(Reflect.get(result, "count")) && isBoolean(Reflect.get(result, "success")) && arrayOf(isString)(Reflect.get(result, "trace")) && arrayOf(isString)(Reflect.get(result, "errors"));
966
+ })(value);
967
+ }
968
+ /**
969
+ * Determine whether a value is a {@link LogicalResult}.
970
+ *
971
+ * @param value - The value to test
972
+ * @returns `true` when every published logical-result member is valid
973
+ *
974
+ * @example
975
+ * ```ts
976
+ * import { isLogicalResult } from '@src/core'
977
+ *
978
+ * isLogicalResult({ reasoning: 'logical', conclusion: false, rules: [], count: 0, success: true, trace: [], errors: [] }) // true
979
+ * isLogicalResult({ reasoning: 'logical', rules: [] }) // false
980
+ * ```
981
+ */
982
+ function isLogicalResult(value) {
983
+ return whereOf(isObject, (result) => {
984
+ return !isArray(result) && Reflect.get(result, "reasoning") === "logical" && isBoolean(Reflect.get(result, "conclusion")) && arrayOf(isRuleResult)(Reflect.get(result, "rules")) && isNumber(Reflect.get(result, "count")) && isBoolean(Reflect.get(result, "success")) && arrayOf(isString)(Reflect.get(result, "trace")) && arrayOf(isString)(Reflect.get(result, "errors"));
985
+ })(value);
986
+ }
987
+ /**
988
+ * Determine whether a value is a {@link SymbolicResult}.
989
+ *
990
+ * @remarks
991
+ * `solutions` is checked across its OWN string-named members, enumerable or
992
+ * not. Inherited members are deliberately unread: they belong to the
993
+ * accepted-prototype property, not to the record this guard certifies.
994
+ *
995
+ * @param value - The value to test
996
+ * @returns `true` when every published symbolic-result member is valid
997
+ *
998
+ * @example
999
+ * ```ts
1000
+ * import { isSymbolicResult } from '@src/core'
1001
+ *
1002
+ * isSymbolicResult({ reasoning: 'symbolic', solutions: { x: 4 }, success: true, trace: [], errors: [] }) // true
1003
+ * isSymbolicResult({ reasoning: 'symbolic', solutions: { x: '4' }, success: true, trace: [], errors: [] }) // false
1004
+ * ```
1005
+ */
1006
+ function isSymbolicResult(value) {
1007
+ return whereOf(isObject, (result) => {
1008
+ return !isArray(result) && Reflect.get(result, "reasoning") === "symbolic" && whereOf(isObject, (solutions) => !isArray(solutions) && Object.getOwnPropertyNames(solutions).every((key) => isNumber(Reflect.get(solutions, key))))(Reflect.get(result, "solutions")) && isBoolean(Reflect.get(result, "success")) && arrayOf(isString)(Reflect.get(result, "trace")) && arrayOf(isString)(Reflect.get(result, "errors"));
1009
+ })(value);
1010
+ }
1011
+ /**
1012
+ * Determine whether a value is an {@link InferentialResult}.
1013
+ *
1014
+ * @param value - The value to test
1015
+ * @returns `true` when every published inferential-result member is valid
1016
+ *
1017
+ * @example
1018
+ * ```ts
1019
+ * import { isInferentialResult } from '@src/core'
1020
+ *
1021
+ * isInferentialResult({ reasoning: 'inferential', derived: [], success: true, trace: [], errors: [] }) // true
1022
+ * isInferentialResult({ reasoning: 'inferential', derived: [null], success: true, trace: [], errors: [] }) // false
1023
+ * ```
1024
+ */
1025
+ function isInferentialResult(value) {
1026
+ return whereOf(isObject, (result) => {
1027
+ if (isArray(result)) return false;
1028
+ const proof = Reflect.get(result, "proof");
1029
+ return Reflect.get(result, "reasoning") === "inferential" && arrayOf(isResultFact)(Reflect.get(result, "derived")) && (proof === void 0 || isProofNode(proof)) && isBoolean(Reflect.get(result, "success")) && arrayOf(isString)(Reflect.get(result, "trace")) && arrayOf(isString)(Reflect.get(result, "errors"));
1030
+ })(value);
1031
+ }
1032
+ /**
1033
+ * Determine whether a value is any {@link ReasonResult} arm.
1034
+ *
1035
+ * @param value - The value to test
1036
+ * @returns `true` when one complete reasoning-result guard accepts the value
1037
+ *
1038
+ * @example
1039
+ * ```ts
1040
+ * import { isReasonResult } from '@src/core'
1041
+ *
1042
+ * isReasonResult({ reasoning: 'symbolic', solutions: {}, success: true, trace: [], errors: [] }) // true
1043
+ * isReasonResult({ reasoning: 'unknown' }) // false
1044
+ * ```
1045
+ */
1046
+ function isReasonResult(value) {
1047
+ return isQuantitativeResult(value) || isLogicalResult(value) || isSymbolicResult(value) || isInferentialResult(value);
1048
+ }
1049
+ /**
1050
+ * Determine whether a value is a {@link ReasonValidationResult}.
1051
+ *
1052
+ * @param value - The value to test
1053
+ * @returns `true` when every published validation-result member is valid
1054
+ *
1055
+ * @example
1056
+ * ```ts
1057
+ * import { isReasonValidationResult } from '@src/core'
1058
+ *
1059
+ * isReasonValidationResult({ valid: true, errors: [], warnings: [] }) // true
1060
+ * isReasonValidationResult({ valid: true, errors: [] }) // false
1061
+ * ```
1062
+ */
1063
+ function isReasonValidationResult(value) {
1064
+ return whereOf(isObject, (result) => {
1065
+ return !isArray(result) && isBoolean(Reflect.get(result, "valid")) && arrayOf(isString)(Reflect.get(result, "errors")) && arrayOf(isString)(Reflect.get(result, "warnings"));
1066
+ })(value);
1067
+ }
1068
+ /**
772
1069
  * Determine whether a value is a `DefinitionBuilder` ENTITY — the brand-guarded
773
1070
  * stateful workspace, not the plain {@link Definition} data union.
774
1071
  *
@@ -1797,13 +2094,15 @@ function matchFacts(pattern, candidate) {
1797
2094
  for (let index = 0; index < pattern.terms.length; index++) {
1798
2095
  const patternTerm = pattern.terms[index];
1799
2096
  const factTerm = candidate.terms[index];
1800
- if (typeof patternTerm === "string" && patternTerm.startsWith("?")) if (patternTerm in bindings) {
1801
- if (bindings[patternTerm] !== factTerm) return void 0;
1802
- } else bindings[patternTerm] = factTerm;
1803
- else if (typeof factTerm === "string" && factTerm.startsWith("?")) if (factTerm in bindings) {
1804
- if (bindings[factTerm] !== patternTerm) return void 0;
1805
- } else bindings[factTerm] = patternTerm;
1806
- else if (patternTerm !== factTerm) return;
2097
+ if (typeof patternTerm === "string" && patternTerm.startsWith("?")) {
2098
+ if (patternTerm in bindings) {
2099
+ if (bindings[patternTerm] !== factTerm) return void 0;
2100
+ } else bindings[patternTerm] = factTerm;
2101
+ } else if (typeof factTerm === "string" && factTerm.startsWith("?")) {
2102
+ if (factTerm in bindings) {
2103
+ if (bindings[factTerm] !== patternTerm) return void 0;
2104
+ } else bindings[factTerm] = patternTerm;
2105
+ } else if (patternTerm !== factTerm) return;
1807
2106
  }
1808
2107
  return bindings;
1809
2108
  }
@@ -5314,7 +5613,6 @@ var DefinitionBuilder = class {
5314
5613
  case "inferential":
5315
5614
  this.facts.collection = definition.facts;
5316
5615
  this.inferences.collection = definition.inferences;
5317
- break;
5318
5616
  }
5319
5617
  this.#envelope = this.#strip(definition);
5320
5618
  }
@@ -5880,6 +6178,6 @@ function createInferenceManager(options) {
5880
6178
  return new InferenceManager(options);
5881
6179
  }
5882
6180
  //#endregion
5883
- export { AGGREGATOR_ID, Aggregator, CONFIDENCE_PRECISION, DEFAULT_BASE, DEFAULT_CONFIDENCE, DEFAULT_DEPTH, DEFAULT_PRECISION, DEFAULT_PRIORITY, DEFAULT_REASON_BAIL, DEFAULT_VALIDATE, DEFAULT_WEIGHT, DEFINITION_BUILDER_BRAND, DefinitionBuilder, EVALUATOR_ID, EquationManager, Evaluator, FactManager, FactorManager, GroupManager, INFERENTIAL_ID, INVERTIBLE_OPERATIONS, InferenceManager, InferentialReasoner, LOGICAL_ID, LogicalReasoner, QUANTITATIVE_ID, QuantitativeReasoner, Reason, ReasonError, RuleManager, SUBJECT_BUILDER_BRAND, SYMBOLIC_ID, SubjectBuilder, SymbolicReasoner, TRANSFORMER_ID, Transformer, VariableManager, addVariable, appendById, appendEquation, appendFact, appendFactor, appendGroup, appendInference, appendRule, applyOperation, assignField, atom, bounds, buildErrorResult, check, clamp, clearInferentialDefinition, clearLogicalDefinition, clearQuantitativeDefinition, clearSymbolicDefinition, compound, constant, containsVariable, createAggregator, createDefinitionBuilder, createEquationManager, createEvaluator, createFactManager, createFactorManager, createGroupManager, createInferenceManager, createInferentialReasoner, createLogicalReasoner, createQuantitativeReasoner, createReason, createRuleManager, createSubjectBuilder, createSymbolicReasoner, createTransformer, createVariableManager, equalValues, equation, extractAtoms, extractConclusions, fact, factToArityKey, factToKey, factorGroup, fieldFactor, fieldSource, findDuplicates, findOverlayMismatches, findUnboundVariables, formatField, indexByArity, inference, inferentialDefinition, instantiateFact, invertLeft, invertRight, isAggregation, isBounds, isChainingStrategy, isCheck, isComparison, isDefinition, isDefinitionBuilder, isEquation, isExpression, isFact, isFactor, isFactorGroup, isFactorRange, isFieldPath, isInference, isInferentialDefinition, isLogicalDefinition, isLogicalOperator, isMathOperation, isNumberRecord, isQuantitativeDefinition, isReasonError, isReasoning, isRule, isSource, isSubject, isSubjectBuilder, isSymbolicDefinition, isSymbolicExpression, isTransform, logicalDefinition, lookupFactor, lookupSource, matchFacts, mergeById, mergeInferentialDefinition, mergeLogicalDefinition, mergeQuantitativeDefinition, mergeSubjects, mergeSymbolicDefinition, operation, parseDefinition, prependById, prependEquation, prependFact, prependFactor, prependGroup, prependInference, prependRule, quantitativeDefinition, rangeFactor, rangeSource, removeById, removeEquation, removeFact, removeFactor, removeField, removeGroup, removeInference, removeRule, removeVariable, repeatSubject, replaceById, replaceEquation, replaceFact, replaceFactor, replaceGroup, replaceInference, replaceRule, roundTo, rule, sortByPriority, staticFactor, staticSource, subjectToFacts, symbolicDefinition, termToKey, transform, variable };
6181
+ export { AGGREGATOR_ID, Aggregator, CONFIDENCE_PRECISION, DEFAULT_BASE, DEFAULT_CONFIDENCE, DEFAULT_DEPTH, DEFAULT_PRECISION, DEFAULT_PRIORITY, DEFAULT_REASON_BAIL, DEFAULT_VALIDATE, DEFAULT_WEIGHT, DEFINITION_BUILDER_BRAND, DefinitionBuilder, EVALUATOR_ID, EquationManager, Evaluator, FactManager, FactorManager, GroupManager, INFERENTIAL_ID, INVERTIBLE_OPERATIONS, InferenceManager, InferentialReasoner, LOGICAL_ID, LogicalReasoner, QUANTITATIVE_ID, QuantitativeReasoner, Reason, ReasonError, RuleManager, SUBJECT_BUILDER_BRAND, SYMBOLIC_ID, SubjectBuilder, SymbolicReasoner, TRANSFORMER_ID, Transformer, VariableManager, addVariable, appendById, appendEquation, appendFact, appendFactor, appendGroup, appendInference, appendRule, applyOperation, assignField, atom, bounds, buildErrorResult, check, clamp, clearInferentialDefinition, clearLogicalDefinition, clearQuantitativeDefinition, clearSymbolicDefinition, compound, constant, containsVariable, createAggregator, createDefinitionBuilder, createEquationManager, createEvaluator, createFactManager, createFactorManager, createGroupManager, createInferenceManager, createInferentialReasoner, createLogicalReasoner, createQuantitativeReasoner, createReason, createRuleManager, createSubjectBuilder, createSymbolicReasoner, createTransformer, createVariableManager, equalValues, equation, extractAtoms, extractConclusions, fact, factToArityKey, factToKey, factorGroup, fieldFactor, fieldSource, findDuplicates, findOverlayMismatches, findUnboundVariables, formatField, indexByArity, inference, inferentialDefinition, instantiateFact, invertLeft, invertRight, isAggregation, isBounds, isChainingStrategy, isCheck, isCheckResult, isComparison, isDefinition, isDefinitionBuilder, isEquation, isExpression, isFact, isFactor, isFactorGroup, isFactorRange, isFactorResult, isFieldPath, isGroupResult, isInference, isInferentialDefinition, isInferentialResult, isLogicalDefinition, isLogicalOperator, isLogicalResult, isMathOperation, isNumberRecord, isProofNode, isQuantitativeDefinition, isQuantitativeResult, isReasonError, isReasonResult, isReasonValidationResult, isReasoning, isResultFact, isRule, isRuleResult, isSource, isSubject, isSubjectBuilder, isSymbolicDefinition, isSymbolicExpression, isSymbolicResult, isTransform, logicalDefinition, lookupFactor, lookupSource, matchFacts, mergeById, mergeInferentialDefinition, mergeLogicalDefinition, mergeQuantitativeDefinition, mergeSubjects, mergeSymbolicDefinition, operation, parseDefinition, prependById, prependEquation, prependFact, prependFactor, prependGroup, prependInference, prependRule, quantitativeDefinition, rangeFactor, rangeSource, removeById, removeEquation, removeFact, removeFactor, removeField, removeGroup, removeInference, removeRule, removeVariable, repeatSubject, replaceById, replaceEquation, replaceFact, replaceFactor, replaceGroup, replaceInference, replaceRule, roundTo, rule, sortByPriority, staticFactor, staticSource, subjectToFacts, symbolicDefinition, termToKey, transform, variable };
5884
6182
 
5885
6183
  //# sourceMappingURL=index.js.map