@astrale-os/kernel-dsl 1.0.0-beta.23 → 1.0.0-beta.25
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/dist/v1/builder/lowering/policy.js +4 -24
- package/dist/v1/builder/lowering/schema/context.js +0 -3
- package/dist/v1/builder/lowering/value/compile.js +24 -21
- package/dist/v1/builder/lowering/value/zod.js +10 -4
- package/dist/v1/builder/schema/diagnostic.d.ts +1 -1
- package/dist/v1/bundle/codec.js +50 -67
- package/dist/v1/bundle/diagnostic.d.ts +1 -1
- package/dist/v1/bundle/structure.js +2 -2
- package/dist/v1/compiled/admission/accept.js +6 -6
- package/dist/v1/compiled/admission/closure.js +16 -33
- package/dist/v1/compiled/admission/diagnostic.d.ts +8 -0
- package/dist/v1/compiled/admission/diagnostic.js +33 -0
- package/dist/v1/compiled/admission/semantics/class.js +11 -7
- package/dist/v1/compiled/admission/semantics/policy/admit.js +4 -19
- package/dist/v1/compiled/admission/wire/callable.d.ts +5 -2
- package/dist/v1/compiled/admission/wire/callable.js +13 -19
- package/dist/v1/compiled/admission/wire/class.js +6 -9
- package/dist/v1/compiled/admission/wire/entry.d.ts +1 -1
- package/dist/v1/compiled/admission/wire/entry.js +8 -13
- package/dist/v1/compiled/admission/wire/policy.js +4 -3
- package/dist/v1/compiled/admission/wire/program.d.ts +13 -3
- package/dist/v1/compiled/admission/wire/program.js +39 -44
- package/dist/v1/compiled/admission/wire/property.d.ts +4 -1
- package/dist/v1/compiled/admission/wire/property.js +8 -5
- package/dist/v1/compiled/admission/wire/value.d.ts +0 -2
- package/dist/v1/compiled/admission/wire/value.js +0 -7
- package/dist/v1/compiled/codec/json.js +4 -3
- package/dist/v1/compiled/compiler/compile.js +6 -17
- package/dist/v1/compiled/diagnostic.d.ts +1 -1
- package/dist/v1/compiled/loader/load.js +2 -7
- package/dist/v1/domain/model/input.d.ts +2 -2
- package/dist/v1/domain/validation/create.d.ts +3 -1
- package/dist/v1/domain/validation/create.js +13 -8
- package/dist/v1/language/callable/resolved.d.ts +3 -3
- package/dist/v1/language/chain.d.ts +7 -0
- package/dist/v1/language/chain.js +24 -0
- package/dist/v1/language/cycle.d.ts +10 -0
- package/dist/v1/language/cycle.js +30 -0
- package/dist/v1/language/limits.d.ts +19 -0
- package/dist/v1/language/limits.js +17 -0
- package/dist/v1/language/policy/analysis/budgets.js +17 -13
- package/dist/v1/language/policy/analysis/composition.d.ts +4 -0
- package/dist/v1/language/policy/analysis/composition.js +11 -0
- package/dist/v1/language/suspended.d.ts +7 -0
- package/dist/v1/language/suspended.js +23 -0
- package/dist/v1/language/value/compile/fields.js +12 -8
- package/dist/v1/language/value/compile/load.d.ts +5 -0
- package/dist/v1/language/value/compile/load.js +13 -0
- package/dist/v1/language/value/evaluate/assertions.d.ts +5 -1
- package/dist/v1/language/value/evaluate/assertions.js +49 -8
- package/dist/v1/language/value/evaluate/evaluator.js +107 -105
- package/dist/v1/language/value/evaluate/preparation.d.ts +16 -0
- package/dist/v1/language/value/evaluate/preparation.js +100 -47
- package/dist/v1/language/value/evaluate/program.d.ts +2 -2
- package/dist/v1/language/value/evaluate/program.js +5 -5
- package/dist/v1/language/value/evaluate/validate.d.ts +3 -3
- package/dist/v1/language/value/evaluate/validate.js +9 -9
- package/dist/v1/language/value/evaluate/validity.optimization.js +4 -3
- package/dist/v1/language/value/inclusion/equivalence.js +4 -2
- package/dist/v1/language/value/inclusion/evidence.js +2 -2
- package/dist/v1/language/value/inclusion/includes.d.ts +13 -4
- package/dist/v1/language/value/inclusion/includes.js +34 -5
- package/dist/v1/language/value/inclusion/location.js +10 -7
- package/dist/v1/language/value/ordering.js +62 -70
- package/dist/v1/language/value/profile/canonicalize.d.ts +6 -1
- package/dist/v1/language/value/profile/canonicalize.js +64 -83
- package/dist/v1/language/value/profile/json.validate.d.ts +13 -3
- package/dist/v1/language/value/profile/json.validate.js +18 -6
- package/dist/v1/language/value/reference/closure.js +3 -2
- package/dist/v1/language/value/reference/recursion.js +7 -26
- package/dist/v1/language/value/reference/references.d.ts +3 -1
- package/dist/v1/language/value/reference/references.js +7 -7
- package/dist/v1/language/value/syntax/walk.d.ts +5 -0
- package/dist/v1/language/value/syntax/walk.js +14 -0
- package/dist/v1/schema/.spec/schemas/domain-schema.schema.json +1 -1
- package/dist/v1/schema/admission/accept.d.ts +3 -1
- package/dist/v1/schema/admission/accept.js +4 -1
- package/dist/v1/schema/analysis/class.d.ts +4 -0
- package/dist/v1/schema/analysis/class.js +24 -2
- package/dist/v1/schema/canonical/revision.d.ts +4 -1
- package/dist/v1/schema/canonical/revision.js +4 -1
- package/dist/v1/schema/compatibility/context.d.ts +5 -2
- package/dist/v1/schema/compatibility/context.js +8 -6
- package/dist/v1/schema/compatibility/model.d.ts +1 -1
- package/dist/v1/schema/compatibility/structure/requirements.js +2 -2
- package/dist/v1/schema/compatibility/walk.js +2 -2
- package/dist/v1/schema/diagnostic.d.ts +1 -1
- package/dist/v1/schema/normalization/domain.js +8 -4
- package/dist/v1/schema/resolution/context.js +6 -7
- package/dist/v1/schema/resolution/domain.js +3 -11
- package/dist/v1/schema/rule.js +10 -3
- package/dist/v1/schema/validation/class.js +1 -1
- package/dist/v1/schema/validation/core.js +16 -8
- package/dist/v1/schema/validation/domain.js +20 -3
- package/dist/v1/schema/validation/policy/budgets.d.ts +5 -1
- package/dist/v1/schema/validation/policy/budgets.js +8 -1
- package/dist/v1/schema/validation/policy/references.js +5 -25
- package/dist/value/accept.d.ts +3 -3
- package/dist/value/canonical.js +2 -2
- package/dist/value/equal.d.ts +9 -1
- package/dist/value/equal.js +37 -3
- package/dist/value/index.d.ts +1 -1
- package/dist/value/limits.d.ts +11 -0
- package/package.json +2 -2
- package/dist/v1/compiled/admission/wire/pattern.d.ts +0 -12
- 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 =
|
|
19
|
+
const locations = closureLocations(closure);
|
|
17
20
|
const matchers = new Map();
|
|
18
|
-
for (const
|
|
19
|
-
|
|
20
|
-
|
|
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 ??=
|
|
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
|
-
|
|
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
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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,
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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 {
|
|
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?:
|
|
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 {
|
|
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
|
|
6
|
-
if (
|
|
7
|
-
return
|
|
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),
|
|
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 {
|
|
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:
|
|
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?:
|
|
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 {
|
|
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
|
|
16
|
-
if (
|
|
17
|
-
return
|
|
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
|
|
29
|
-
return
|
|
30
|
-
?
|
|
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,
|
|
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) =>
|
|
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) =>
|
|
174
|
+
parts.push((value) => choices.some((candidate) => equal(value, candidate)));
|
|
174
175
|
}
|
|
175
176
|
}
|
|
176
177
|
function compileNumber(schema, parts) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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) || !
|
|
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 {
|
|
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) =>
|
|
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
|
|
8
|
-
* the values themselves. Each rule relates one assertion of
|
|
9
|
-
* `inner`; a finite `inner` is decided exactly by evaluating its
|
|
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
|
-
/**
|
|
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
|
|
10
|
-
* the values themselves. Each rule relates one assertion of
|
|
11
|
-
* `inner`; a finite `inner` is decided exactly by evaluating its
|
|
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
|
-
|
|
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
|
-
/**
|
|
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
|
|
42
|
+
const waiting = [location];
|
|
43
|
+
while (waiting.length > 0) {
|
|
44
|
+
const current = waiting.pop();
|
|
43
45
|
if (seen.has(current.index))
|
|
44
|
-
|
|
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
|
-
|
|
50
|
-
|
|
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. */
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { runSuspended } from '../suspended.js';
|
|
1
2
|
import { loadValueProgram } from './compile/load.js';
|
|
2
3
|
import { prepareValueSchema } from './evaluate/preparation.js';
|
|
3
4
|
const ALL_JSON_KINDS = new Set([
|
|
@@ -26,7 +27,7 @@ export function classifyValueProgramOrdering(program) {
|
|
|
26
27
|
}
|
|
27
28
|
export function classifyPreparedOrdering(compiled) {
|
|
28
29
|
// An enum orders as the strings it lists; a set is an array.
|
|
29
|
-
const candidates = new Set([...
|
|
30
|
+
const candidates = new Set([...candidatesOf(compiled)].map((kind) => kind === 'enum' ? 'string' : kind === 'set' ? 'array' : kind));
|
|
30
31
|
if (candidates.size !== 1)
|
|
31
32
|
return undefined;
|
|
32
33
|
const [kind] = candidates;
|
|
@@ -61,7 +62,7 @@ export function classifyValueKind(contract, registry) {
|
|
|
61
62
|
return classifyPreparedValueKind(prepared.value);
|
|
62
63
|
}
|
|
63
64
|
export function classifyPreparedValueKind(compiled) {
|
|
64
|
-
const candidates = new Set(
|
|
65
|
+
const candidates = new Set(candidatesOf(compiled));
|
|
65
66
|
candidates.delete('null');
|
|
66
67
|
if (candidates.size !== 1)
|
|
67
68
|
return 'json';
|
|
@@ -79,81 +80,72 @@ export function classifyPreparedValueKind(compiled) {
|
|
|
79
80
|
return 'json';
|
|
80
81
|
}
|
|
81
82
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
83
|
+
/**
|
|
84
|
+
* The candidates at a program's root. References chain to any length, so the classifications waiting
|
|
85
|
+
* on one another run on a stack of their own, never on the call stack.
|
|
86
|
+
*/
|
|
87
|
+
function candidatesOf(compiled) {
|
|
88
|
+
const known = new Map();
|
|
89
|
+
function* read(location) {
|
|
90
|
+
const candidates = known.get(location.key);
|
|
91
|
+
if (candidates !== undefined)
|
|
92
|
+
return candidates;
|
|
93
|
+
// A location read again before its classification ends sits on a cycle: it admits anything.
|
|
94
|
+
known.set(location.key, ALL_JSON_KINDS);
|
|
95
|
+
const classified = yield* classifySchema(compiled, location, read);
|
|
96
|
+
known.set(location.key, classified);
|
|
97
|
+
return classified;
|
|
88
98
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
finally {
|
|
102
|
-
this.active.delete(location.key);
|
|
99
|
+
return runSuspended(read(compiled.root));
|
|
100
|
+
}
|
|
101
|
+
function* classifySchema(compiled, location, read) {
|
|
102
|
+
if (location.schema === false)
|
|
103
|
+
return new Set();
|
|
104
|
+
if (location.schema === true)
|
|
105
|
+
return ALL_JSON_KINDS;
|
|
106
|
+
const schema = location.schema;
|
|
107
|
+
let candidates = ALL_JSON_KINDS;
|
|
108
|
+
if (Object.hasOwn(schema, 'x-astrale-value-kind')) {
|
|
109
|
+
if (schema['x-astrale-value-kind'] !== 'instant' || schema.type !== 'string') {
|
|
110
|
+
return new Set();
|
|
103
111
|
}
|
|
112
|
+
candidates = intersect(candidates, new Set(['instant']));
|
|
113
|
+
}
|
|
114
|
+
const declaredTypes = typesOf(schema.type);
|
|
115
|
+
if (declaredTypes !== undefined)
|
|
116
|
+
candidates = intersect(candidates, declaredTypes);
|
|
117
|
+
if (schema.uniqueItems === true) {
|
|
118
|
+
candidates = new Set([...candidates].map((kind) => (kind === 'array' ? 'set' : kind)));
|
|
119
|
+
}
|
|
120
|
+
if (Object.hasOwn(schema, 'const')) {
|
|
121
|
+
candidates = intersect(candidates, new Set([kindOfValue(schema.const)]));
|
|
122
|
+
}
|
|
123
|
+
if (Array.isArray(schema.enum)) {
|
|
124
|
+
candidates = intersect(candidates, normalize(new Set(schema.enum.map(kindOfValue))));
|
|
104
125
|
}
|
|
105
|
-
|
|
106
|
-
|
|
126
|
+
if (Object.hasOwn(schema, '$ref')) {
|
|
127
|
+
const target = compiled.reference(location);
|
|
128
|
+
if (target === undefined)
|
|
107
129
|
return new Set();
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
return new Set();
|
|
115
|
-
}
|
|
116
|
-
candidates = intersect(candidates, new Set(['instant']));
|
|
117
|
-
}
|
|
118
|
-
const declaredTypes = typesOf(schema.type);
|
|
119
|
-
if (declaredTypes !== undefined)
|
|
120
|
-
candidates = intersect(candidates, declaredTypes);
|
|
121
|
-
if (schema.uniqueItems === true) {
|
|
122
|
-
candidates = new Set([...candidates].map((kind) => (kind === 'array' ? 'set' : kind)));
|
|
123
|
-
}
|
|
124
|
-
if (Object.hasOwn(schema, 'const')) {
|
|
125
|
-
candidates = intersect(candidates, new Set([kindOfValue(schema.const)]));
|
|
126
|
-
}
|
|
127
|
-
if (Array.isArray(schema.enum)) {
|
|
128
|
-
candidates = intersect(candidates, normalize(new Set(schema.enum.map(kindOfValue))));
|
|
129
|
-
}
|
|
130
|
-
if (Object.hasOwn(schema, '$ref')) {
|
|
131
|
-
const target = this.compiled.reference(location);
|
|
132
|
-
if (target === undefined)
|
|
133
|
-
return new Set();
|
|
134
|
-
candidates = intersect(candidates, this.classify(target));
|
|
135
|
-
}
|
|
136
|
-
const allOf = schemaList(schema.allOf);
|
|
137
|
-
if (allOf !== undefined) {
|
|
138
|
-
for (let index = 0; index < allOf.length; index += 1) {
|
|
139
|
-
candidates = intersect(candidates, this.classify(this.child(location, 'allOf', index)));
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
for (const keyword of ['anyOf', 'oneOf']) {
|
|
143
|
-
const branches = schemaList(schema[keyword]);
|
|
144
|
-
if (branches === undefined)
|
|
145
|
-
continue;
|
|
146
|
-
let admitted = new Set();
|
|
147
|
-
for (let index = 0; index < branches.length; index += 1) {
|
|
148
|
-
admitted = union(admitted, this.classify(this.child(location, keyword, index)));
|
|
149
|
-
}
|
|
150
|
-
candidates = intersect(candidates, admitted);
|
|
130
|
+
candidates = intersect(candidates, yield read(target));
|
|
131
|
+
}
|
|
132
|
+
const allOf = schemaList(schema.allOf);
|
|
133
|
+
if (allOf !== undefined) {
|
|
134
|
+
for (let index = 0; index < allOf.length; index += 1) {
|
|
135
|
+
candidates = intersect(candidates, yield read(compiled.child(location, 'allOf', index)));
|
|
151
136
|
}
|
|
152
|
-
return candidates;
|
|
153
137
|
}
|
|
154
|
-
|
|
155
|
-
|
|
138
|
+
for (const keyword of ['anyOf', 'oneOf']) {
|
|
139
|
+
const branches = schemaList(schema[keyword]);
|
|
140
|
+
if (branches === undefined)
|
|
141
|
+
continue;
|
|
142
|
+
let admitted = new Set();
|
|
143
|
+
for (let index = 0; index < branches.length; index += 1) {
|
|
144
|
+
admitted = union(admitted, yield read(compiled.child(location, keyword, index)));
|
|
145
|
+
}
|
|
146
|
+
candidates = intersect(candidates, admitted);
|
|
156
147
|
}
|
|
148
|
+
return candidates;
|
|
157
149
|
}
|
|
158
150
|
function typesOf(input) {
|
|
159
151
|
if (typeof input === 'string')
|