@astrale-os/kernel-dsl 1.0.0-beta.24 → 1.0.0-beta.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/dist/v1/builder/class/authoring.js +0 -3
  2. package/dist/v1/builder/lowering/policy.js +4 -24
  3. package/dist/v1/builder/lowering/schema/context.js +0 -3
  4. package/dist/v1/builder/lowering/value/compile.js +24 -21
  5. package/dist/v1/builder/lowering/value/zod.js +10 -4
  6. package/dist/v1/builder/schema/diagnostic.d.ts +1 -1
  7. package/dist/v1/bundle/codec.js +50 -67
  8. package/dist/v1/bundle/diagnostic.d.ts +1 -1
  9. package/dist/v1/bundle/structure.js +2 -2
  10. package/dist/v1/compiled/admission/accept.js +6 -6
  11. package/dist/v1/compiled/admission/closure.js +16 -33
  12. package/dist/v1/compiled/admission/diagnostic.d.ts +8 -0
  13. package/dist/v1/compiled/admission/diagnostic.js +33 -0
  14. package/dist/v1/compiled/admission/semantics/class.js +11 -7
  15. package/dist/v1/compiled/admission/semantics/policy/admit.js +4 -19
  16. package/dist/v1/compiled/admission/wire/callable.d.ts +5 -2
  17. package/dist/v1/compiled/admission/wire/callable.js +13 -19
  18. package/dist/v1/compiled/admission/wire/class.js +6 -9
  19. package/dist/v1/compiled/admission/wire/entry.d.ts +1 -1
  20. package/dist/v1/compiled/admission/wire/entry.js +8 -13
  21. package/dist/v1/compiled/admission/wire/policy.js +4 -3
  22. package/dist/v1/compiled/admission/wire/program.d.ts +13 -3
  23. package/dist/v1/compiled/admission/wire/program.js +39 -44
  24. package/dist/v1/compiled/admission/wire/property.d.ts +4 -1
  25. package/dist/v1/compiled/admission/wire/property.js +8 -5
  26. package/dist/v1/compiled/admission/wire/value.d.ts +0 -2
  27. package/dist/v1/compiled/admission/wire/value.js +0 -7
  28. package/dist/v1/compiled/codec/json.js +4 -3
  29. package/dist/v1/compiled/compiler/compile.js +10 -29
  30. package/dist/v1/compiled/diagnostic.d.ts +1 -1
  31. package/dist/v1/compiled/loader/load.js +2 -7
  32. package/dist/v1/domain/model/input.d.ts +2 -2
  33. package/dist/v1/domain/validation/create.d.ts +3 -1
  34. package/dist/v1/domain/validation/create.js +13 -8
  35. package/dist/v1/language/callable/resolved.d.ts +3 -3
  36. package/dist/v1/language/chain.d.ts +7 -0
  37. package/dist/v1/language/chain.js +24 -0
  38. package/dist/v1/language/class/inheritance.js +0 -2
  39. package/dist/v1/language/cycle.d.ts +10 -0
  40. package/dist/v1/language/cycle.js +30 -0
  41. package/dist/v1/language/limits.d.ts +19 -0
  42. package/dist/v1/language/limits.js +17 -0
  43. package/dist/v1/language/policy/analysis/budgets.js +17 -13
  44. package/dist/v1/language/policy/analysis/composition.d.ts +4 -0
  45. package/dist/v1/language/policy/analysis/composition.js +11 -0
  46. package/dist/v1/language/suspended.d.ts +7 -0
  47. package/dist/v1/language/suspended.js +23 -0
  48. package/dist/v1/language/value/compile/fields.js +12 -8
  49. package/dist/v1/language/value/compile/load.d.ts +5 -0
  50. package/dist/v1/language/value/compile/load.js +13 -0
  51. package/dist/v1/language/value/evaluate/assertions.d.ts +5 -1
  52. package/dist/v1/language/value/evaluate/assertions.js +49 -8
  53. package/dist/v1/language/value/evaluate/evaluator.js +107 -105
  54. package/dist/v1/language/value/evaluate/preparation.d.ts +16 -0
  55. package/dist/v1/language/value/evaluate/preparation.js +100 -47
  56. package/dist/v1/language/value/evaluate/program.d.ts +2 -2
  57. package/dist/v1/language/value/evaluate/program.js +5 -5
  58. package/dist/v1/language/value/evaluate/validate.d.ts +3 -3
  59. package/dist/v1/language/value/evaluate/validate.js +9 -9
  60. package/dist/v1/language/value/evaluate/validity.optimization.js +4 -3
  61. package/dist/v1/language/value/inclusion/assertions.js +7 -8
  62. package/dist/v1/language/value/inclusion/equivalence.js +4 -2
  63. package/dist/v1/language/value/inclusion/evidence.js +2 -2
  64. package/dist/v1/language/value/inclusion/includes.d.ts +13 -4
  65. package/dist/v1/language/value/inclusion/includes.js +34 -5
  66. package/dist/v1/language/value/inclusion/location.js +10 -7
  67. package/dist/v1/language/value/ordering.js +62 -70
  68. package/dist/v1/language/value/profile/canonicalize.d.ts +6 -1
  69. package/dist/v1/language/value/profile/canonicalize.js +64 -83
  70. package/dist/v1/language/value/profile/json.validate.d.ts +13 -3
  71. package/dist/v1/language/value/profile/json.validate.js +18 -6
  72. package/dist/v1/language/value/reference/closure.js +3 -2
  73. package/dist/v1/language/value/reference/recursion.js +7 -26
  74. package/dist/v1/language/value/reference/references.d.ts +3 -1
  75. package/dist/v1/language/value/reference/references.js +7 -7
  76. package/dist/v1/language/value/syntax/walk.d.ts +5 -0
  77. package/dist/v1/language/value/syntax/walk.js +14 -0
  78. package/dist/v1/schema/.spec/schemas/domain-schema.schema.json +1 -1
  79. package/dist/v1/schema/admission/accept.d.ts +3 -1
  80. package/dist/v1/schema/admission/accept.js +4 -1
  81. package/dist/v1/schema/analysis/class.d.ts +4 -0
  82. package/dist/v1/schema/analysis/class.js +24 -2
  83. package/dist/v1/schema/canonical/revision.d.ts +4 -1
  84. package/dist/v1/schema/canonical/revision.js +4 -1
  85. package/dist/v1/schema/compatibility/comparators/classes.js +22 -22
  86. package/dist/v1/schema/compatibility/context.d.ts +5 -2
  87. package/dist/v1/schema/compatibility/context.js +8 -6
  88. package/dist/v1/schema/compatibility/meaning/footprint.js +1 -2
  89. package/dist/v1/schema/compatibility/model.d.ts +1 -1
  90. package/dist/v1/schema/compatibility/references.d.ts +0 -3
  91. package/dist/v1/schema/compatibility/references.js +1 -9
  92. package/dist/v1/schema/compatibility/structure/requirements.js +4 -6
  93. package/dist/v1/schema/compatibility/walk.js +2 -2
  94. package/dist/v1/schema/diagnostic.d.ts +1 -1
  95. package/dist/v1/schema/normalization/domain.js +8 -4
  96. package/dist/v1/schema/resolution/context.js +6 -7
  97. package/dist/v1/schema/resolution/domain.js +3 -11
  98. package/dist/v1/schema/rule.js +10 -3
  99. package/dist/v1/schema/validation/class.js +1 -1
  100. package/dist/v1/schema/validation/core.js +16 -8
  101. package/dist/v1/schema/validation/domain.js +20 -3
  102. package/dist/v1/schema/validation/policy/budgets.d.ts +5 -1
  103. package/dist/v1/schema/validation/policy/budgets.js +8 -1
  104. package/dist/v1/schema/validation/policy/references.js +5 -25
  105. package/dist/value/accept.d.ts +3 -3
  106. package/dist/value/canonical.js +2 -2
  107. package/dist/value/equal.d.ts +9 -1
  108. package/dist/value/equal.js +37 -3
  109. package/dist/value/index.d.ts +1 -1
  110. package/dist/value/limits.d.ts +11 -0
  111. package/package.json +1 -1
  112. package/dist/v1/compiled/admission/wire/pattern.d.ts +0 -12
  113. package/dist/v1/compiled/admission/wire/pattern.js +0 -111
@@ -1,10 +1,13 @@
1
+ import { diagnostic } from '../../validation/diagnostic.js';
1
2
  import { isValueObject as isJsonObject } from '../model/object.js';
2
3
  import { collectResourceClosure, locationKey, } from '../reference/closure.js';
4
+ import { validateProductiveRecursion } from '../reference/recursion.js';
3
5
  import { indexRegistry } from '../reference/registry.index.js';
4
6
  import { compilePattern } from '../regexp/compile.js';
5
7
  import { loadPattern } from '../regexp/matcher.js';
8
+ import { graphNodePathSchema } from '../syntax/node-path/schema.js';
6
9
  import { appendPointer } from '../syntax/pointer.js';
7
- import { walkSchema } from '../syntax/walk.js';
10
+ import { forEachSubschema, holdsOnlySubschemas, walkSchema } from '../syntax/walk.js';
8
11
  /** Prepare reference and linear-regexp state without generating executable source. */
9
12
  export function prepareValueSchema(schema, registry, pointer, pool = new Map()) {
10
13
  const indexed = indexRegistry(registry, pointer);
@@ -13,30 +16,11 @@ export function prepareValueSchema(schema, registry, pointer, pool = new Map())
13
16
  const closure = collectResourceClosure(schema, 'callable-input', indexed.index, pointer);
14
17
  if (closure.diagnostics.length > 0)
15
18
  return failed(closure.diagnostics);
16
- const locations = new Map();
19
+ const locations = closureLocations(closure);
17
20
  const matchers = new Map();
18
- for (const resource of closure.resources.values()) {
19
- walkSchema(resource.schema, (location) => {
20
- const key = locationKey(resource.key, location.pointer);
21
- locations.set(key, {
22
- key,
23
- pointer: location.pointer,
24
- resource,
25
- schema: location.schema,
26
- });
27
- if (!isJsonObject(location.schema))
28
- return;
29
- rememberMatcher(matchers, pool, location.schema.pattern);
30
- rememberMatcher(matchers, pool, location.schema.fullMatch);
31
- if (isJsonObject(location.schema.patternProperties)) {
32
- for (const pattern of Object.keys(location.schema.patternProperties)) {
33
- rememberMatcher(matchers, pool, pattern);
34
- }
35
- }
36
- });
37
- }
38
- const initial = closure.resources.values().next().value;
39
- const root = initial === undefined ? undefined : locations.get(locationKey(initial.key, ''));
21
+ for (const source of patternSources(locations))
22
+ rememberMatcher(matchers, pool, source);
23
+ const root = closureRoot(closure, locations);
40
24
  if (root === undefined) {
41
25
  return { compiled: false, message: 'The root value schema is absent from its closure.' };
42
26
  }
@@ -47,7 +31,10 @@ export function prepareValueSchema(schema, registry, pointer, pool = new Map())
47
31
  root,
48
32
  locations,
49
33
  resolutions: closure.resolutions,
50
- program: () => (program ??= portableProgram(root, locations, closure.resolutions, matchers)),
34
+ program: () => (program ??= Object.freeze({
35
+ ...valueGraph(root, locations, closure.resolutions),
36
+ patterns: Object.freeze([...matchers.values()].map(({ pattern }) => pattern)),
37
+ })),
51
38
  child(location, keyword, member) {
52
39
  const keywordPointer = appendPointer(location.pointer, keyword);
53
40
  const pointer = member === undefined ? keywordPointer : appendPointer(keywordPointer, member);
@@ -73,18 +60,90 @@ export function prepareValueSchema(schema, registry, pointer, pool = new Map())
73
60
  },
74
61
  };
75
62
  }
76
- function portableProgram(root, locations, resolutions, patterns) {
63
+ const NO_DOMAINS = {
64
+ registry: {
65
+ current: { origin: '', dependencies: [], nodeDefinitions: [] },
66
+ directDependencies: [],
67
+ transitiveDependencies: [],
68
+ graphNodePath: graphNodePathSchema,
69
+ },
70
+ domains: new Map(),
71
+ dependenciesByOwner: new Map(),
72
+ nodeDefinitions: new Set(),
73
+ };
74
+ /**
75
+ * Derive the graph of a schema no admission has seen and the pattern sources it uses, or the refusal
76
+ * of one with no graph to read: a reference with no target, a cycle consuming nothing, or a subschema
77
+ * slot holding anything else. Closed under the two reference forms, it needs no registry and compiles
78
+ * no pattern.
79
+ */
80
+ export function deriveValueGraph(schema, pointer) {
81
+ const closure = collectResourceClosure(schema, 'callable-input', NO_DOMAINS, pointer);
82
+ const refusal = closure.diagnostics[0] ?? validateProductiveRecursion(closure.resources, closure.resolutions)[0];
83
+ if (refusal !== undefined)
84
+ return { refusal };
85
+ const locations = closureLocations(closure);
86
+ for (const location of locations.values()) {
87
+ if (!isJsonObject(location.schema))
88
+ continue;
89
+ for (const [keyword, value] of Object.entries(location.schema)) {
90
+ if (holdsOnlySubschemas(keyword, value))
91
+ continue;
92
+ const at = appendPointer(`${pointer}${location.pointer}`, keyword);
93
+ return {
94
+ refusal: diagnostic('VS_META_SCHEMA', at, `${keyword} must hold only subschemas.`),
95
+ };
96
+ }
97
+ }
98
+ return {
99
+ graph: valueGraph(closureRoot(closure, locations), locations, closure.resolutions),
100
+ sources: patternSources(locations),
101
+ };
102
+ }
103
+ /** The I-Regexp sources the locations of a closure use, each once, in location order. */
104
+ function patternSources(locations) {
105
+ const sources = new Set();
106
+ const use = (source) => {
107
+ if (typeof source === 'string')
108
+ sources.add(source);
109
+ };
110
+ for (const { schema } of locations.values()) {
111
+ if (!isJsonObject(schema))
112
+ continue;
113
+ use(schema.pattern);
114
+ use(schema.fullMatch);
115
+ if (isJsonObject(schema.patternProperties)) {
116
+ Object.keys(schema.patternProperties).forEach(use);
117
+ }
118
+ }
119
+ return [...sources];
120
+ }
121
+ function closureLocations(closure) {
122
+ const locations = new Map();
123
+ for (const resource of closure.resources.values()) {
124
+ walkSchema(resource.schema, ({ pointer, schema }) => {
125
+ const key = locationKey(resource.key, pointer);
126
+ locations.set(key, { key, pointer, resource, schema });
127
+ });
128
+ }
129
+ return locations;
130
+ }
131
+ function closureRoot(closure, locations) {
132
+ const initial = closure.resources.values().next().value;
133
+ return initial === undefined ? undefined : locations.get(locationKey(initial.key, ''));
134
+ }
135
+ function valueGraph(root, locations, resolutions) {
77
136
  const ordered = [...locations.values()];
78
137
  const indices = new Map(ordered.map((location, index) => [location.key, index]));
79
138
  const nodes = ordered.map((location) => {
80
139
  const children = {};
81
- walkSchema(location.schema, (child) => {
82
- if (child.depth !== 1)
83
- return;
84
- const target = indices.get(locationKey(location.resource.key, `${location.pointer}${child.pointer}`));
85
- if (target !== undefined)
86
- children[childKey(child.pointer)] = target;
87
- });
140
+ for (const [keyword, value] of Object.entries(location.schema)) {
141
+ forEachSubschema(keyword, value, appendPointer('', keyword), (_, at) => {
142
+ const target = indices.get(locationKey(location.resource.key, `${location.pointer}${at}`));
143
+ if (target !== undefined)
144
+ children[childKey(at)] = target;
145
+ });
146
+ }
88
147
  const resolved = resolutions.get(location.key);
89
148
  const reference = resolved === undefined
90
149
  ? undefined
@@ -98,25 +157,19 @@ function portableProgram(root, locations, resolutions, patterns) {
98
157
  ...(reference === undefined ? {} : { reference }),
99
158
  });
100
159
  });
101
- return Object.freeze({
102
- root: indices.get(root.key),
103
- nodes: Object.freeze(nodes),
104
- patterns: Object.freeze([...patterns.values()].map(({ pattern }) => pattern)),
105
- });
160
+ return { root: indices.get(root.key), nodes: Object.freeze(nodes) };
106
161
  }
107
162
  export function childKey(pointer) {
108
163
  return pointer.slice(1);
109
164
  }
110
- function rememberMatcher(matchers, pool, input) {
111
- if (typeof input === 'string' && !matchers.has(input)) {
112
- let prepared = pool.get(input);
113
- if (prepared === undefined) {
114
- const pattern = compilePattern(input);
115
- prepared = { pattern, matcher: loadPattern(pattern) };
116
- pool.set(input, prepared);
117
- }
118
- matchers.set(input, prepared);
165
+ function rememberMatcher(matchers, pool, source) {
166
+ let prepared = pool.get(source);
167
+ if (prepared === undefined) {
168
+ const pattern = compilePattern(source);
169
+ prepared = { pattern, matcher: loadPattern(pattern) };
170
+ pool.set(source, prepared);
119
171
  }
172
+ matchers.set(source, prepared);
120
173
  }
121
174
  function failed(diagnostics) {
122
175
  return {
@@ -1,4 +1,4 @@
1
- import type { Options } from '@astrale-os/kernel-dsl/value';
1
+ import type { StructuralOptions } from '@astrale-os/kernel-dsl/value';
2
2
  import type { Diagnostic } from '../../validation/diagnostic.js';
3
3
  import type { ValueProgram } from '../compile/model.js';
4
- export declare function validateValueProgram(program: ValueProgram, input: unknown, roots?: readonly number[], options?: Options): readonly Diagnostic[];
4
+ export declare function validateValueProgram(program: ValueProgram, input: unknown, roots?: readonly number[], options?: StructuralOptions): readonly Diagnostic[];
@@ -1,12 +1,12 @@
1
1
  import { loadValueProgram } from '../compile/load.js';
2
- import { validateCanonicalJsonDomain } from '../profile/json.validate.js';
2
+ import { acceptInstance } from '../profile/json.validate.js';
3
3
  import { evaluateInstance } from './evaluator.js';
4
4
  export function validateValueProgram(program, input, roots = [program.root], options) {
5
- const canonical = validateCanonicalJsonDomain(input, '', options);
6
- if (canonical.length > 0)
7
- return canonical;
5
+ const accepted = acceptInstance(input, '', options);
6
+ if ('refusal' in accepted)
7
+ return [accepted.refusal];
8
8
  return roots.flatMap((root) => {
9
- const result = evaluateInstance(loadValueProgram(program, root), input);
9
+ const result = evaluateInstance(loadValueProgram(program, root), accepted.value);
10
10
  return result.failures.map((failure) => ({
11
11
  code: 'VALUE_SCHEMA_INSTANCE_INVALID',
12
12
  pointer: failure.instancePointer,
@@ -1,4 +1,4 @@
1
- import type { Value as JsonValue, Options } from '@astrale-os/kernel-dsl/value';
1
+ import type { StructuralOptions } from '@astrale-os/kernel-dsl/value';
2
2
  import type { Diagnostic } from '../../validation/diagnostic.js';
3
3
  import type { JsonSchema } from '../model/schema.js';
4
4
  import type { ValueSchemaRegistry } from '../reference/registry.js';
@@ -11,6 +11,6 @@ import { type PatternPool } from './preparation.js';
11
11
  * resolution or dynamic source generation; patterns use the portable executor.
12
12
  * `format` remains annotation-only; the graph NodePath codec performs its separate locator check.
13
13
  */
14
- export declare function validateJsonInstance(schema: JsonSchema, value: JsonValue, registry: ValueSchemaRegistry, pointer?: string, patterns?: PatternPool): Diagnostic[];
14
+ export declare function validateJsonInstance(schema: JsonSchema, value: unknown, registry: ValueSchemaRegistry, pointer?: string, patterns?: PatternPool): Diagnostic[];
15
15
  /** Validate with the exact preparation already retained by a source-resolved Property or Callable. */
16
- export declare function validatePreparedInstance(compiled: CompiledInstanceSchema, value: unknown, options?: Options): Diagnostic[];
16
+ export declare function validatePreparedInstance(compiled: CompiledInstanceSchema, value: unknown, options?: StructuralOptions): Diagnostic[];
@@ -1,5 +1,5 @@
1
1
  import { diagnostic } from '../../validation/diagnostic.js';
2
- import { validateCanonicalJsonDomain } from '../profile/json.validate.js';
2
+ import { acceptInstance } from '../profile/json.validate.js';
3
3
  import { evaluateInstance } from './evaluator.js';
4
4
  import { prepareValueSchema, } from './preparation.js';
5
5
  import { satisfies } from './validity.optimization.js';
@@ -12,23 +12,23 @@ const validatorCache = new WeakMap();
12
12
  * `format` remains annotation-only; the graph NodePath codec performs its separate locator check.
13
13
  */
14
14
  export function validateJsonInstance(schema, value, registry, pointer = '', patterns) {
15
- const diagnostics = [...validateCanonicalJsonDomain(value, pointer)];
16
- if (diagnostics.length > 0)
17
- return diagnostics;
15
+ const accepted = acceptInstance(value, pointer);
16
+ if ('refusal' in accepted)
17
+ return [accepted.refusal];
18
18
  const prepared = preparedInstanceValidator(schema, registry, pointer, patterns);
19
19
  if (!prepared.compiled) {
20
20
  return [
21
21
  diagnostic('VS_REFERENCE_RESOLUTION', pointer, `Value validation failed closed while preparing exact schema resources: ${prepared.message}.`),
22
22
  ];
23
23
  }
24
- return instanceDiagnostics(prepared.value, value, pointer);
24
+ return instanceDiagnostics(prepared.value, accepted.value, pointer);
25
25
  }
26
26
  /** Validate with the exact preparation already retained by a source-resolved Property or Callable. */
27
27
  export function validatePreparedInstance(compiled, value, options) {
28
- const diagnostics = [...validateCanonicalJsonDomain(value, '', options)];
29
- return diagnostics.length > 0
30
- ? diagnostics
31
- : instanceDiagnostics(compiled, value, '');
28
+ const accepted = acceptInstance(value, '', options);
29
+ return 'refusal' in accepted
30
+ ? [accepted.refusal]
31
+ : instanceDiagnostics(compiled, accepted.value, '');
32
32
  }
33
33
  function instanceDiagnostics(compiled, value, pointer) {
34
34
  // A value the compiled check finds valid has nothing to report; any other is evaluated.
@@ -1,7 +1,8 @@
1
+ import { equal } from '@astrale-os/kernel-dsl/value';
1
2
  import { isCanonicalBlobRef } from '../model/blob.js';
2
3
  import { isInstantValue } from '../model/instant.js';
3
4
  import { isValueObject as isJsonObject } from '../model/object.js';
4
- import { codePointLength, isMultipleOf, jsonEqual, matchesType, unique } from './assertions.js';
5
+ import { codePointLength, isMultipleOf, matchesType, unique } from './assertions.js';
5
6
  /**
6
7
  * Whether a value satisfies its prepared schema, as the evaluator decides it, without building a
7
8
  * failure, an instance pointer or an annotation: the evaluator's verdict, or undefined when this
@@ -166,11 +167,11 @@ function compileCommon(schema, parts) {
166
167
  }
167
168
  if (Object.hasOwn(schema, 'const')) {
168
169
  const expected = schema.const;
169
- parts.push((value) => jsonEqual(value, expected));
170
+ parts.push((value) => equal(value, expected));
170
171
  }
171
172
  const choices = schema.enum;
172
173
  if (Array.isArray(choices)) {
173
- parts.push((value) => choices.some((candidate) => jsonEqual(value, candidate)));
174
+ parts.push((value) => choices.some((candidate) => equal(value, candidate)));
174
175
  }
175
176
  }
176
177
  function compileNumber(schema, parts) {
@@ -15,15 +15,14 @@ const RULES = new Map([
15
15
  ]);
16
16
  /** Whether every keyword of the outer `schema` holds for the values `inner` admits. */
17
17
  export function assertionsHold(inclusion, outer, schema, inner, evidence) {
18
- // Annotation-dependent keywords are decided only by equivalence.
19
- if (Object.hasOwn(schema, 'unevaluatedProperties'))
18
+ // A keyword with no rule holds only when it asserts nothing. The annotation-dependent keywords,
19
+ // `unevaluatedProperties` and `unevaluatedItems`, have none: only equivalence decides them.
20
+ const keywords = Object.keys(schema);
21
+ if (!keywords.every((keyword) => RULES.has(keyword) || INERT.has(keyword)))
20
22
  return false;
21
- if (Object.hasOwn(schema, 'unevaluatedItems'))
22
- return false;
23
- return Object.keys(schema).every((keyword) => {
23
+ return keywords.every((keyword) => {
24
24
  const rule = RULES.get(keyword);
25
- if (rule === undefined)
26
- return INERT.has(keyword);
27
- return rule({ inclusion, keyword, value: schema[keyword], outer, schema, inner, evidence });
25
+ return (rule === undefined ||
26
+ rule({ inclusion, keyword, value: schema[keyword], outer, schema, inner, evidence }));
28
27
  });
29
28
  }
@@ -1,4 +1,4 @@
1
- import { jsonEqual } from '../evaluate/assertions.js';
1
+ import { equal } from '@astrale-os/kernel-dsl/value';
2
2
  import { INERT } from './keywords.js';
3
3
  import { at, referenceOf, schemaOf } from './location.js';
4
4
  /** Keywords whose subschemas the equivalence check compares positionally. */
@@ -26,6 +26,8 @@ export function equivalent(inclusion, left, right) {
26
26
  const pending = [[left, right]];
27
27
  const seen = new Set();
28
28
  while (pending.length > 0) {
29
+ if (!inclusion.spend(1))
30
+ return false;
29
31
  const [first, second] = pending.pop();
30
32
  const key = inclusion.pair(first, second);
31
33
  if (seen.has(key))
@@ -42,7 +44,7 @@ export function equivalent(inclusion, left, right) {
42
44
  if (assertions.length !== assertionKeys(b).length)
43
45
  return false;
44
46
  for (const keyword of assertions) {
45
- if (!Object.hasOwn(b, keyword) || !jsonEqual(a[keyword], b[keyword]))
47
+ if (!Object.hasOwn(b, keyword) || !equal(a[keyword], b[keyword]))
46
48
  return false;
47
49
  }
48
50
  const firstChildren = subschemaChildren(first);
@@ -1,4 +1,4 @@
1
- import { jsonEqual } from '../evaluate/assertions.js';
1
+ import { equal } from '@astrale-os/kernel-dsl/value';
2
2
  import { isValueObject as isJsonObject } from '../model/object.js';
3
3
  import { ANY, OBJECT, STRING, finiteKinds, kindOf, typeMask } from './kinds.js';
4
4
  import { conjunctsOf, schemaOf } from './location.js';
@@ -30,7 +30,7 @@ export function evidenceOf(location) {
30
30
  values =
31
31
  values === undefined
32
32
  ? [...candidates]
33
- : values.filter((value) => candidates.some((candidate) => jsonEqual(value, candidate)));
33
+ : values.filter((value) => candidates.some((candidate) => equal(value, candidate)));
34
34
  };
35
35
  for (const conjunct of conjuncts) {
36
36
  const schema = schemaOf(conjunct);
@@ -4,21 +4,30 @@ import type { At } from './location.js';
4
4
  /**
5
5
  * Whether `outer` admits every value `inner` admits, under the one V1 evaluator.
6
6
  *
7
- * `true` is a proof. `false` only means that no rule proved inclusion: a caller must then decide
8
- * the values themselves. Each rule relates one assertion of `outer` to evidence carried by
9
- * `inner`; a finite `inner` is decided exactly by evaluating its candidates.
7
+ * `true` is a proof. `false` only means that no rule proved inclusion, or that the question ran out
8
+ * of budget: a caller must then decide the values themselves. Each rule relates one assertion of
9
+ * `outer` to evidence carried by `inner`; a finite `inner` is decided exactly by evaluating its
10
+ * candidates.
10
11
  */
11
12
  export declare function valueProgramIncludes(outer: ValueProgram, inner: ValueProgram): boolean;
12
- /** One inclusion question: the decided pairs, the pairs being decided and the evidence cache. */
13
+ /**
14
+ * One inclusion question: the decided pairs, the pairs being decided, the evidence cache and the
15
+ * budget spent.
16
+ */
13
17
  export declare class Inclusion {
14
18
  private readonly programs;
15
19
  private readonly decided;
16
20
  private readonly pending;
17
21
  private readonly evidenceByAt;
22
+ private reads;
18
23
  /** Whether `outer` admits every value `inner` admits; a cyclic premise proves nothing. */
19
24
  includes(outer: At, inner: At): boolean;
20
25
  /** The evidence of one location, computed once per question. */
21
26
  evidence(location: At): Evidence;
27
+ /** Charge `reads` locations to this question; whether its budget still holds. */
28
+ spend(reads: number): boolean;
29
+ /** Whether this question has read fewer locations than it may. */
30
+ withinBudget(): boolean;
22
31
  /** A key identifying an ordered pair of locations within this question. */
23
32
  pair(outer: At, inner: At): string;
24
33
  private decide;
@@ -3,22 +3,38 @@ import { equivalent } from './equivalence.js';
3
3
  import { evidenceOf } from './evidence.js';
4
4
  import { isTop } from './keywords.js';
5
5
  import { accepts, at, childrenOf, schemaOf } from './location.js';
6
+ /**
7
+ * How many pairs one question may hold open. Nesting stops at the depth limit and a chain of
8
+ * references does not, so this keeps the rules, which call one another, far from the call stack's
9
+ * limit.
10
+ */
11
+ const MAXIMUM_OPEN_PAIRS = 256;
12
+ /** How many locations one question may read: the pairs of two programs grow as their product. */
13
+ const MAXIMUM_READS = 1_000_000;
6
14
  /**
7
15
  * Whether `outer` admits every value `inner` admits, under the one V1 evaluator.
8
16
  *
9
- * `true` is a proof. `false` only means that no rule proved inclusion: a caller must then decide
10
- * the values themselves. Each rule relates one assertion of `outer` to evidence carried by
11
- * `inner`; a finite `inner` is decided exactly by evaluating its candidates.
17
+ * `true` is a proof. `false` only means that no rule proved inclusion, or that the question ran out
18
+ * of budget: a caller must then decide the values themselves. Each rule relates one assertion of
19
+ * `outer` to evidence carried by `inner`; a finite `inner` is decided exactly by evaluating its
20
+ * candidates.
12
21
  */
13
22
  export function valueProgramIncludes(outer, inner) {
14
- return new Inclusion().includes(at(outer, outer.root), at(inner, inner.root));
23
+ const inclusion = new Inclusion();
24
+ const proved = inclusion.includes(at(outer, outer.root), at(inner, inner.root));
25
+ // A rule under way can read past the budget and still prove: the budget is the question's.
26
+ return proved && inclusion.withinBudget();
15
27
  }
16
- /** One inclusion question: the decided pairs, the pairs being decided and the evidence cache. */
28
+ /**
29
+ * One inclusion question: the decided pairs, the pairs being decided, the evidence cache and the
30
+ * budget spent.
31
+ */
17
32
  export class Inclusion {
18
33
  programs = new Map();
19
34
  decided = new Map();
20
35
  pending = new Set();
21
36
  evidenceByAt = new Map();
37
+ reads = 0;
22
38
  /** Whether `outer` admits every value `inner` admits; a cyclic premise proves nothing. */
23
39
  includes(outer, inner) {
24
40
  const key = this.pair(outer, inner);
@@ -28,6 +44,9 @@ export class Inclusion {
28
44
  // Assuming a pending pair would make a later result depend on an unproven premise.
29
45
  if (this.pending.has(key))
30
46
  return false;
47
+ // Past its budget a question proves nothing more, which the answer `false` already allows.
48
+ if (this.pending.size === MAXIMUM_OPEN_PAIRS || !this.spend(1))
49
+ return false;
31
50
  this.pending.add(key);
32
51
  try {
33
52
  const result = this.decide(outer, inner);
@@ -45,9 +64,19 @@ export class Inclusion {
45
64
  if (cached !== undefined)
46
65
  return cached;
47
66
  const result = evidenceOf(location);
67
+ this.spend(result.conjuncts.length);
48
68
  this.evidenceByAt.set(key, result);
49
69
  return result;
50
70
  }
71
+ /** Charge `reads` locations to this question; whether its budget still holds. */
72
+ spend(reads) {
73
+ this.reads += reads;
74
+ return this.withinBudget();
75
+ }
76
+ /** Whether this question has read fewer locations than it may. */
77
+ withinBudget() {
78
+ return this.reads < MAXIMUM_READS;
79
+ }
51
80
  /** A key identifying an ordered pair of locations within this question. */
52
81
  pair(outer, inner) {
53
82
  return `${this.location(outer)}|${this.location(inner)}`;
@@ -39,18 +39,21 @@ export function referenceOf(location) {
39
39
  export function conjunctsOf(location) {
40
40
  const seen = new Set();
41
41
  const conjuncts = [];
42
- const visit = (current) => {
42
+ const waiting = [location];
43
+ while (waiting.length > 0) {
44
+ const current = waiting.pop();
43
45
  if (seen.has(current.index))
44
- return;
46
+ continue;
45
47
  seen.add(current.index);
46
48
  conjuncts.push(current);
49
+ // Last in, first out: the reference is read first, then the branches in their order.
50
+ const branches = childrenOf(current, 'allOf');
51
+ for (let branch = branches.length - 1; branch >= 0; branch -= 1)
52
+ waiting.push(branches[branch]);
47
53
  const reference = referenceOf(current);
48
54
  if (reference !== undefined)
49
- visit(reference);
50
- for (const branch of childrenOf(current, 'allOf'))
51
- visit(branch);
52
- };
53
- visit(location);
55
+ waiting.push(reference);
56
+ }
54
57
  return conjuncts;
55
58
  }
56
59
  /** Whether `name` matches `pattern` under the program's regular-expression dialect. */