@astrale-os/kernel-dsl 1.0.0-beta.34 → 1.0.0-beta.36
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/schema/lifetime.js +3 -5
- package/dist/v1/builder/lowering/value/zod.js +3 -1
- package/dist/v1/compiled/admission/semantics/policy/callable.js +7 -29
- package/dist/v1/compiled/admission/semantics/policy/parity.js +4 -11
- package/dist/v1/compiled/admission/wire/core.js +2 -6
- package/dist/v1/compiled/admission/wire/property.js +6 -19
- package/dist/v1/domain/projection/classes.js +5 -7
- package/dist/v1/language/class/inheritance.d.ts +26 -2
- package/dist/v1/language/class/inheritance.js +63 -40
- package/dist/v1/language/class/lifetime.d.ts +7 -15
- package/dist/v1/language/class/lifetime.js +19 -40
- package/dist/v1/language/class/property-values.d.ts +13 -0
- package/dist/v1/language/class/property-values.js +35 -4
- package/dist/v1/language/class/unique.d.ts +1 -1
- package/dist/v1/language/class/unique.js +3 -13
- package/dist/v1/language/index.d.ts +2 -0
- package/dist/v1/language/index.js +2 -0
- package/dist/v1/language/limits.d.ts +3 -1
- package/dist/v1/language/limits.js +3 -1
- package/dist/v1/language/policy/analysis/branches.d.ts +5 -1
- package/dist/v1/language/policy/analysis/branches.js +21 -5
- package/dist/v1/language/policy/analysis/budgets.d.ts +2 -0
- package/dist/v1/language/policy/analysis/budgets.js +7 -1
- package/dist/v1/language/policy/analysis/typing.d.ts +9 -1
- package/dist/v1/language/policy/analysis/typing.js +44 -49
- package/dist/v1/language/state/topology.d.ts +24 -0
- package/dist/v1/language/state/topology.js +60 -19
- package/dist/v1/language/validation/rule.js +1 -1
- package/dist/v1/language/value/evaluate/evaluator.js +4 -0
- package/dist/v1/language/value/evaluate/validity.optimization.js +4 -1
- package/dist/v1/language/value/inclusion/evidence.js +1 -1
- package/dist/v1/language/value/model/date.d.ts +8 -0
- package/dist/v1/language/value/model/date.js +19 -0
- package/dist/v1/language/value/model/instant.d.ts +0 -1
- package/dist/v1/language/value/model/instant.js +2 -15
- package/dist/v1/language/value/ordering.d.ts +6 -2
- package/dist/v1/language/value/ordering.js +36 -8
- package/dist/v1/language/value/profile/rules.js +3 -2
- package/dist/v1/language/value/profile/vocabulary.d.ts +1 -1
- package/dist/v1/language/value/profile/vocabulary.js +5 -0
- package/dist/v1/language/value/syntax/node-path/schema.d.ts +7 -0
- package/dist/v1/language/value/syntax/node-path/schema.js +32 -0
- package/dist/v1/schema/.spec/schemas/domain-schema.schema.json +3 -0
- package/dist/v1/schema/compatibility/comparators/classes.js +10 -21
- package/dist/v1/schema/enforcement/derived.d.ts +5 -0
- package/dist/v1/schema/enforcement/derived.js +7 -2
- package/dist/v1/schema/enforcement/dispose.d.ts +16 -6
- package/dist/v1/schema/enforcement/dispose.js +16 -6
- package/dist/v1/schema/resolution/origins.js +3 -5
- package/dist/v1/schema/rule.js +4 -3
- package/dist/v1/schema/validation/class.js +39 -47
- package/dist/v1/schema/validation/policy/budgets.js +1 -0
- package/dist/v1/schema/validation/policy/typing.js +7 -20
- package/dist/v1/schema/validation/policy/uses.js +5 -25
- package/dist/v1/schema/validation/value-api.d.ts +2 -1
- package/dist/v1/schema/validation/value-api.js +2 -1
- package/dist/v1/schema/validation/value.json-schema.js +3 -3
- package/dist/value/types.d.ts +1 -1
- package/package.json +1 -1
|
@@ -2,6 +2,7 @@ import { json } from '@astrale-os/kernel-dsl/value';
|
|
|
2
2
|
import { ClassKey } from '../../addressing/class-key.js';
|
|
3
3
|
import { compareUnicode } from '../../addressing/ordering.js';
|
|
4
4
|
import { PropertyKey } from '../../addressing/property-key.js';
|
|
5
|
+
import { ancestry } from './inheritance.js';
|
|
5
6
|
/** The kinds of key fields, in canonical order: the endpoints, the slug, then the Properties. */
|
|
6
7
|
const KINDS = Object.freeze([
|
|
7
8
|
'source',
|
|
@@ -139,7 +140,7 @@ export function enforcesEdgeUniqueTuple(forms, value) {
|
|
|
139
140
|
}
|
|
140
141
|
/**
|
|
141
142
|
* Whether a Property may be a key field (D-S1): a single-kind scalar by its ordering facet, a
|
|
142
|
-
* string, number, boolean or
|
|
143
|
+
* string, number, boolean, instant or date, that no automatic program writes.
|
|
143
144
|
*/
|
|
144
145
|
export function keyableProperty(ordering, auto) {
|
|
145
146
|
return ordering !== undefined && auto === undefined;
|
|
@@ -228,20 +229,9 @@ function serialized(values) {
|
|
|
228
229
|
*/
|
|
229
230
|
function coveringTuples(candidate, resolve) {
|
|
230
231
|
const tuples = [];
|
|
231
|
-
const
|
|
232
|
-
const visit = (value) => {
|
|
233
|
-
if (seen.has(value) || value.kind !== candidate.kind)
|
|
234
|
-
return;
|
|
235
|
-
seen.add(value);
|
|
232
|
+
for (const value of ancestry(candidate, resolve))
|
|
236
233
|
for (const fields of value.unique ?? [])
|
|
237
234
|
tuples.push(Object.freeze({ class: ClassKey.of(value.ref), fields }));
|
|
238
|
-
for (const ref of value.extends) {
|
|
239
|
-
const parent = resolve(ref);
|
|
240
|
-
if (parent !== undefined)
|
|
241
|
-
visit(parent);
|
|
242
|
-
}
|
|
243
|
-
};
|
|
244
|
-
visit(candidate);
|
|
245
235
|
return Object.freeze(tuples);
|
|
246
236
|
}
|
|
247
237
|
/** The keys a fact holds under `tuples`, its value under each tagged by the declaring Class. */
|
|
@@ -4,9 +4,11 @@ export { isBinaryInput } from './callable/model/callable.js';
|
|
|
4
4
|
export type { ClassRef, CoreRef, DefinitionBase, DefinitionKey, DefinitionRef, FunctionRef, MethodKey, PolicyRef, PropertyKey, ViewRef, } from '../addressing/index.js';
|
|
5
5
|
export type { Value as PropertyValue } from '@astrale-os/kernel-dsl/value';
|
|
6
6
|
export type { JsonSchema as ValueSchema, JsonSchemaObject as ValueSchemaObject, } from './value/model/schema.js';
|
|
7
|
+
export { isOrderingValue, valueProgramKind } from './value/ordering.js';
|
|
7
8
|
export type { OrderingKind } from './value/ordering.js';
|
|
8
9
|
export type { ValueProgram } from './value/compile/model.js';
|
|
9
10
|
export { valueProgramIncludes } from './value/inclusion/includes.js';
|
|
11
|
+
export { isDateValue } from './value/model/date.js';
|
|
10
12
|
export { isInstantValue } from './value/model/instant.js';
|
|
11
13
|
export { stateMachine } from './state/index.js';
|
|
12
14
|
export type { EventOf, StateMachine, StateOf, StateTopology } from './state/index.js';
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export { admissionInputFields, isAdmissionMaxAge, MAXIMUM_ADMISSION_AGE_SECONDS, } from './callable/admission.js';
|
|
2
2
|
export { isMethodExecutable } from './callable/executable.js';
|
|
3
3
|
export { isBinaryInput } from './callable/model/callable.js';
|
|
4
|
+
export { isOrderingValue, valueProgramKind } from './value/ordering.js';
|
|
4
5
|
export { valueProgramIncludes } from './value/inclusion/includes.js';
|
|
6
|
+
export { isDateValue } from './value/model/date.js';
|
|
5
7
|
export { isInstantValue } from './value/model/instant.js';
|
|
6
8
|
export { stateMachine } from './state/index.js';
|
|
7
9
|
export { admitsForeignSubclass, inheritanceOf } from './class/inheritance.js';
|
|
@@ -48,7 +48,9 @@ export declare const limits: Readonly<{
|
|
|
48
48
|
* Levels of a Policy pattern: an Edge or sameNode is one, and each allOf, anyOf, exists and
|
|
49
49
|
* composition adds one.
|
|
50
50
|
*/
|
|
51
|
-
patternDepth:
|
|
51
|
+
patternDepth: 8;
|
|
52
|
+
/** Nested exists scopes on one path. Boolean operators and named compositions add none. */
|
|
53
|
+
existsDepth: 3;
|
|
52
54
|
/**
|
|
53
55
|
* Branches of a Policy's normal form: an anyOf adds those of its operands, an allOf multiplies
|
|
54
56
|
* them.
|
|
@@ -48,7 +48,9 @@ export const limits = Object.freeze({
|
|
|
48
48
|
* Levels of a Policy pattern: an Edge or sameNode is one, and each allOf, anyOf, exists and
|
|
49
49
|
* composition adds one.
|
|
50
50
|
*/
|
|
51
|
-
patternDepth:
|
|
51
|
+
patternDepth: 8,
|
|
52
|
+
/** Nested exists scopes on one path. Boolean operators and named compositions add none. */
|
|
53
|
+
existsDepth: 3,
|
|
52
54
|
/**
|
|
53
55
|
* Branches of a Policy's normal form: an anyOf adds those of its operands, an allOf multiplies
|
|
54
56
|
* them.
|
|
@@ -5,7 +5,6 @@ export interface PatternBranch {
|
|
|
5
5
|
readonly edges: readonly PatternEdgeUse[];
|
|
6
6
|
readonly variables: ReadonlyMap<string, PolicyVariableSelector>;
|
|
7
7
|
readonly requiredVariables: ReadonlySet<string>;
|
|
8
|
-
readonly namespace: string;
|
|
9
8
|
}
|
|
10
9
|
export interface PatternEdgeUse {
|
|
11
10
|
readonly pattern: PolicyEdgePattern;
|
|
@@ -13,6 +12,11 @@ export interface PatternEdgeUse {
|
|
|
13
12
|
}
|
|
14
13
|
export type PolicyTargetMode = 'object' | 'edge' | 'named';
|
|
15
14
|
export declare function branchesOf(pattern: PolicyPatternExpression, namespace: string): readonly PatternBranch[];
|
|
15
|
+
/** Give one operand of a named conjunction its own existential variables. A cached expansion
|
|
16
|
+
* uses relative namespaces, so even two paths to the same declaration bind separate witnesses.
|
|
17
|
+
* Terms supplied by the use site (subject, object, inputs and references) remain shared.
|
|
18
|
+
*/
|
|
19
|
+
export declare function scopeOperand(branch: PatternBranch, operand: number): PatternBranch;
|
|
16
20
|
export declare function branchProduct(children: readonly (readonly PatternBranch[])[]): readonly PatternBranch[];
|
|
17
21
|
export declare function capBranches(values: readonly PatternBranch[]): readonly PatternBranch[];
|
|
18
22
|
export declare function isConnected(branch: PatternBranch): boolean;
|
|
@@ -5,6 +5,26 @@ export function branchesOf(pattern, namespace) {
|
|
|
5
5
|
const variables = collectVariableClasses(pattern, namespace);
|
|
6
6
|
return branchesForNode(pattern, namespace, variables);
|
|
7
7
|
}
|
|
8
|
+
/** Give one operand of a named conjunction its own existential variables. A cached expansion
|
|
9
|
+
* uses relative namespaces, so even two paths to the same declaration bind separate witnesses.
|
|
10
|
+
* Terms supplied by the use site (subject, object, inputs and references) remain shared.
|
|
11
|
+
*/
|
|
12
|
+
export function scopeOperand(branch, operand) {
|
|
13
|
+
if (branch.variables.size === 0)
|
|
14
|
+
return branch;
|
|
15
|
+
const suffix = '/' + operand;
|
|
16
|
+
const key = (term) => (branch.variables.has(term) ? term + suffix : term);
|
|
17
|
+
return {
|
|
18
|
+
terms: new Map([...branch.terms].map(([term, value]) => [key(term), value])),
|
|
19
|
+
equalities: branch.equalities.map(([left, right]) => [key(left), key(right)]),
|
|
20
|
+
edges: branch.edges.map(({ pattern, namespace }) => ({
|
|
21
|
+
pattern,
|
|
22
|
+
namespace: namespace + suffix,
|
|
23
|
+
})),
|
|
24
|
+
variables: new Map([...branch.variables].map(([term, selector]) => [key(term), selector])),
|
|
25
|
+
requiredVariables: new Set([...branch.requiredVariables].map(key)),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
8
28
|
export function branchProduct(children) {
|
|
9
29
|
let result = [emptyBranch()];
|
|
10
30
|
for (const child of children) {
|
|
@@ -80,7 +100,6 @@ function branchesForNode(pattern, namespace, variables) {
|
|
|
80
100
|
edges: [{ pattern, namespace }],
|
|
81
101
|
variables,
|
|
82
102
|
requiredVariables: new Set(),
|
|
83
|
-
namespace,
|
|
84
103
|
},
|
|
85
104
|
];
|
|
86
105
|
}
|
|
@@ -98,7 +117,6 @@ function branchesForNode(pattern, namespace, variables) {
|
|
|
98
117
|
edges: [],
|
|
99
118
|
variables,
|
|
100
119
|
requiredVariables: new Set(),
|
|
101
|
-
namespace,
|
|
102
120
|
},
|
|
103
121
|
];
|
|
104
122
|
}
|
|
@@ -120,7 +138,6 @@ function emptyBranch() {
|
|
|
120
138
|
edges: [],
|
|
121
139
|
variables: new Map(),
|
|
122
140
|
requiredVariables: new Set(),
|
|
123
|
-
namespace: '$',
|
|
124
141
|
};
|
|
125
142
|
}
|
|
126
143
|
function mergeBranches(left, right) {
|
|
@@ -130,7 +147,6 @@ function mergeBranches(left, right) {
|
|
|
130
147
|
edges: [...left.edges, ...right.edges],
|
|
131
148
|
variables: new Map([...left.variables, ...right.variables]),
|
|
132
149
|
requiredVariables: new Set([...left.requiredVariables, ...right.requiredVariables]),
|
|
133
|
-
namespace: `${left.namespace}&${right.namespace}`,
|
|
134
150
|
};
|
|
135
151
|
}
|
|
136
152
|
function collectVariableClasses(pattern, namespace) {
|
|
@@ -149,7 +165,7 @@ function collectVariableClasses(pattern, namespace) {
|
|
|
149
165
|
return result;
|
|
150
166
|
}
|
|
151
167
|
function variableKey(namespace, id) {
|
|
152
|
-
return `variable:${
|
|
168
|
+
return `variable:${id}@${namespace}`;
|
|
153
169
|
}
|
|
154
170
|
/** Disjoint sets of term keys, merged by the equalities of one Policy branch. */
|
|
155
171
|
export class UnionFind {
|
|
@@ -19,6 +19,8 @@ export interface PolicyMeasure {
|
|
|
19
19
|
readonly inputs: number;
|
|
20
20
|
/** Levels of the expanded pattern. */
|
|
21
21
|
readonly patternDepth: number;
|
|
22
|
+
/** The greatest number of nested exists scopes on one path. */
|
|
23
|
+
readonly existsDepth: number;
|
|
22
24
|
/** Branches of its normal form, at most `limits.policy.normalizedBranches + 1`. */
|
|
23
25
|
readonly normalizedBranches: number;
|
|
24
26
|
/** The most graph predicates, Edges and sameNode equalities, of one branch. */
|
|
@@ -75,6 +75,7 @@ export function policyMeasurer(origin, normalized, isLocal, inputOf) {
|
|
|
75
75
|
: {
|
|
76
76
|
inputs: measure.inputs ?? 0,
|
|
77
77
|
patternDepth: measure.depth,
|
|
78
|
+
existsDepth: measure.existsDepth,
|
|
78
79
|
normalizedBranches: measure.edgeCounts.length,
|
|
79
80
|
edgePredicates: Math.max(0, ...measure.edgeCounts),
|
|
80
81
|
referencedDomains: measure.domains.size,
|
|
@@ -85,6 +86,7 @@ function measurePattern(pattern, origin) {
|
|
|
85
86
|
if ('sameNode' in pattern)
|
|
86
87
|
return {
|
|
87
88
|
depth: 1,
|
|
89
|
+
existsDepth: 0,
|
|
88
90
|
edgeCounts: [1],
|
|
89
91
|
domains: new Set([
|
|
90
92
|
origin,
|
|
@@ -94,6 +96,7 @@ function measurePattern(pattern, origin) {
|
|
|
94
96
|
if ('source' in pattern)
|
|
95
97
|
return {
|
|
96
98
|
depth: 1,
|
|
99
|
+
existsDepth: 0,
|
|
97
100
|
edgeCounts: [1],
|
|
98
101
|
domains: new Set([
|
|
99
102
|
origin,
|
|
@@ -105,6 +108,7 @@ function measurePattern(pattern, origin) {
|
|
|
105
108
|
const child = measurePattern(pattern.exists.where, origin);
|
|
106
109
|
return {
|
|
107
110
|
depth: child.depth + 1,
|
|
111
|
+
existsDepth: child.existsDepth + 1,
|
|
108
112
|
edgeCounts: child.edgeCounts,
|
|
109
113
|
domains: new Set([
|
|
110
114
|
...child.domains,
|
|
@@ -120,10 +124,12 @@ function measurePattern(pattern, origin) {
|
|
|
120
124
|
}
|
|
121
125
|
function combineMeasures(kind, children) {
|
|
122
126
|
const depth = 1 + Math.max(0, ...children.map((child) => child.depth));
|
|
127
|
+
const existsDepth = Math.max(...children.map((child) => child.existsDepth));
|
|
123
128
|
const domains = new Set(children.flatMap((child) => [...child.domains]));
|
|
124
129
|
if (kind === 'anyOf')
|
|
125
130
|
return {
|
|
126
131
|
depth,
|
|
132
|
+
existsDepth,
|
|
127
133
|
edgeCounts: children.flatMap((child) => child.edgeCounts).slice(0, BRANCH_OVERFLOW),
|
|
128
134
|
domains,
|
|
129
135
|
};
|
|
@@ -141,7 +147,7 @@ function combineMeasures(kind, children) {
|
|
|
141
147
|
}
|
|
142
148
|
edgeCounts = next;
|
|
143
149
|
}
|
|
144
|
-
return { depth, edgeCounts, domains };
|
|
150
|
+
return { depth, existsDepth, edgeCounts, domains };
|
|
145
151
|
}
|
|
146
152
|
/** Measure one normalized check tree. */
|
|
147
153
|
export function measureChecks(expression) {
|
|
@@ -45,7 +45,15 @@ export declare class PolicyTyping {
|
|
|
45
45
|
readonly scope: PolicyTypingScope;
|
|
46
46
|
constructor(scope: PolicyTypingScope);
|
|
47
47
|
/** The normalized branches of a named Policy, `undefined` when it does not expand. */
|
|
48
|
-
expandedBranches(name: string
|
|
48
|
+
expandedBranches(name: string): readonly PatternBranch[] | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Type each expanded branch at its own default use, yielding in branch order each branch without
|
|
51
|
+
* a coherent target mode (`target`) or not typed at that use (`typing`); named branches are skipped.
|
|
52
|
+
*/
|
|
53
|
+
defaultUseFaults(branches: readonly PatternBranch[]): Generator<{
|
|
54
|
+
readonly index: number;
|
|
55
|
+
readonly kind: 'target' | 'typing';
|
|
56
|
+
}>;
|
|
49
57
|
/** Compositions inherit the signature of their operands, checked at declaration admission. */
|
|
50
58
|
signature(name: string): InputSignature | undefined;
|
|
51
59
|
validDeclaration(name: string): boolean;
|
|
@@ -1,29 +1,42 @@
|
|
|
1
1
|
import { Key } from '../../../addressing/index.js';
|
|
2
2
|
import { definitionRefIndex } from '../../../addressing/ordering.js';
|
|
3
|
-
import { classSatisfies, isGraphRoot } from '../../class/inheritance.js';
|
|
3
|
+
import { ancestry, classSatisfies, contractEndpoints, isGraphRoot, } from '../../class/inheritance.js';
|
|
4
4
|
import { limits } from '../../limits.js';
|
|
5
5
|
import { PolicyComposition } from '../model/policy.js';
|
|
6
|
-
import { branchesOf, branchProduct, capBranches, targetMode, termKey, UnionFind, } from './branches.js';
|
|
6
|
+
import { branchesOf, branchProduct, capBranches, scopeOperand, targetMode, termKey, UnionFind, } from './branches.js';
|
|
7
7
|
import { admittedCheckClasses, checkObjectName } from './checks.js';
|
|
8
8
|
export class PolicyTyping {
|
|
9
9
|
scope;
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
// none of which the expansion depends on), so a shared-child composition DAG is expanded in work
|
|
13
|
-
// linear in the number of Policies instead of enumerating every path. The expansion result is
|
|
14
|
-
// independent of the reaching path up to the opaque term-namespace labels, which only ever
|
|
15
|
-
// disambiguate variables within one branch and leave targetMode and branch typing unchanged.
|
|
16
|
-
// Two occurrences of one Policy in a branch share labels only when a Policy reaches it by two
|
|
17
|
-
// paths; for that to merge distinct variables, the Policy expands deeper than
|
|
18
|
-
// `limits.policy.patternDepth`.
|
|
10
|
+
// Cache each declaration once. Its variables are relative to its own expansion. A named
|
|
11
|
+
// conjunction scopes each operand before combining them; disjunctive branches stay separate.
|
|
19
12
|
#expansions = new Map();
|
|
20
13
|
#index;
|
|
21
14
|
constructor(scope) {
|
|
22
15
|
this.scope = scope;
|
|
23
16
|
}
|
|
24
17
|
/** The normalized branches of a named Policy, `undefined` when it does not expand. */
|
|
25
|
-
expandedBranches(name
|
|
26
|
-
return this.#expandedBranches(name, new Set()
|
|
18
|
+
expandedBranches(name) {
|
|
19
|
+
return this.#expandedBranches(name, new Set());
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Type each expanded branch at its own default use, yielding in branch order each branch without
|
|
23
|
+
* a coherent target mode (`target`) or not typed at that use (`typing`); named branches are skipped.
|
|
24
|
+
*/
|
|
25
|
+
*defaultUseFaults(branches) {
|
|
26
|
+
for (const [index, branch] of branches.entries()) {
|
|
27
|
+
const mode = targetMode(branch);
|
|
28
|
+
if (mode === 'named')
|
|
29
|
+
continue;
|
|
30
|
+
if (mode === undefined) {
|
|
31
|
+
yield { index, kind: 'target' };
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
const use = mode === 'object'
|
|
35
|
+
? { kind: 'object', object: { kind: 'default' } }
|
|
36
|
+
: { kind: 'edge', source: { kind: 'default' }, target: { kind: 'default' } };
|
|
37
|
+
if (!this.branchIsTyped(branch, use))
|
|
38
|
+
yield { index, kind: 'typing' };
|
|
39
|
+
}
|
|
27
40
|
}
|
|
28
41
|
/** Compositions inherit the signature of their operands, checked at declaration admission. */
|
|
29
42
|
signature(name) {
|
|
@@ -65,7 +78,7 @@ export class PolicyTyping {
|
|
|
65
78
|
}
|
|
66
79
|
namedCheck(name, bindings, actuals) {
|
|
67
80
|
const signature = this.signature(name);
|
|
68
|
-
const branches = this.expandedBranches(name
|
|
81
|
+
const branches = this.expandedBranches(name);
|
|
69
82
|
if (!signature ||
|
|
70
83
|
!branches ||
|
|
71
84
|
Object.keys(bindings).length !== Object.keys(signature).length ||
|
|
@@ -93,9 +106,9 @@ export class PolicyTyping {
|
|
|
93
106
|
const signature = this.signature(name);
|
|
94
107
|
if (signature && Object.keys(signature).some((key) => key !== 'source' && key !== 'target'))
|
|
95
108
|
return false;
|
|
96
|
-
const endpoints =
|
|
97
|
-
const sources = this.#concreteEndpointClasses(endpoints[0].accepts);
|
|
98
|
-
const targets = this.#concreteEndpointClasses(endpoints[1].accepts);
|
|
109
|
+
const endpoints = contractEndpoints(declaration);
|
|
110
|
+
const sources = this.#concreteEndpointClasses(endpoints[0].endpoint.accepts);
|
|
111
|
+
const targets = this.#concreteEndpointClasses(endpoints[1].endpoint.accepts);
|
|
99
112
|
return sources.every((source) => targets.every((target) => this.#policyAcceptsEdge(name, { kind: 'exact', ref: source }, { kind: 'exact', ref: target })));
|
|
100
113
|
}
|
|
101
114
|
/**
|
|
@@ -129,7 +142,7 @@ export class PolicyTyping {
|
|
|
129
142
|
}
|
|
130
143
|
/** Whether each normalized branch of the Policy is typed for an object of this contract. */
|
|
131
144
|
#policyBranchTyping(name, object) {
|
|
132
|
-
const branches = this
|
|
145
|
+
const branches = this.expandedBranches(name);
|
|
133
146
|
const signature = this.signature(name);
|
|
134
147
|
if (signature && (Object.keys(signature).length !== 1 || !Object.hasOwn(signature, 'object')))
|
|
135
148
|
return undefined;
|
|
@@ -139,7 +152,7 @@ export class PolicyTyping {
|
|
|
139
152
|
return branches?.map((branch) => this.branchIsTyped(branch, use));
|
|
140
153
|
}
|
|
141
154
|
#policyAcceptsEdge(name, source, target) {
|
|
142
|
-
const branches = this
|
|
155
|
+
const branches = this.expandedBranches(name);
|
|
143
156
|
const signature = this.signature(name);
|
|
144
157
|
const use = signature
|
|
145
158
|
? { kind: 'named', signature, actuals: { source: [source], target: [target] } }
|
|
@@ -154,22 +167,22 @@ export class PolicyTyping {
|
|
|
154
167
|
this.#index ??= nodeClassIndex(this.scope);
|
|
155
168
|
return this.#index;
|
|
156
169
|
}
|
|
157
|
-
#expandedBranches(name, visiting
|
|
170
|
+
#expandedBranches(name, visiting) {
|
|
158
171
|
if (visiting.has(name))
|
|
159
172
|
return undefined;
|
|
160
173
|
const cached = this.#expansions.get(name);
|
|
161
174
|
if (cached !== undefined || this.#expansions.has(name))
|
|
162
175
|
return cached;
|
|
163
|
-
const expanded = this.#expandDeclaration(name, visiting
|
|
176
|
+
const expanded = this.#expandDeclaration(name, visiting);
|
|
164
177
|
this.#expansions.set(name, expanded);
|
|
165
178
|
return expanded;
|
|
166
179
|
}
|
|
167
|
-
#expandDeclaration(name, visiting
|
|
180
|
+
#expandDeclaration(name, visiting) {
|
|
168
181
|
const declaration = this.scope.normalized.get(name);
|
|
169
182
|
if (declaration === undefined)
|
|
170
183
|
return undefined;
|
|
171
184
|
if ('match' in declaration)
|
|
172
|
-
return branchesOf(declaration.match,
|
|
185
|
+
return branchesOf(declaration.match, '');
|
|
173
186
|
const nested = new Set(visiting);
|
|
174
187
|
nested.add(name);
|
|
175
188
|
const { operator, operands: refs } = PolicyComposition.of(declaration);
|
|
@@ -177,10 +190,12 @@ export class PolicyTyping {
|
|
|
177
190
|
for (const [index, ref] of refs.entries()) {
|
|
178
191
|
if (!this.scope.isLocalPolicy(ref))
|
|
179
192
|
return undefined;
|
|
180
|
-
const expanded = this.#expandedBranches(ref.name, nested
|
|
193
|
+
const expanded = this.#expandedBranches(ref.name, nested);
|
|
181
194
|
if (expanded === undefined)
|
|
182
195
|
return undefined;
|
|
183
|
-
|
|
196
|
+
if (refs.length === 1)
|
|
197
|
+
return expanded;
|
|
198
|
+
children.push(operator === 'allOf' ? expanded.map((branch) => scopeOperand(branch, index)) : expanded);
|
|
184
199
|
}
|
|
185
200
|
return operator === 'allOf' ? branchProduct(children) : capBranches(children.flat());
|
|
186
201
|
}
|
|
@@ -434,21 +449,7 @@ function nodeClassIndex(scope) {
|
|
|
434
449
|
}
|
|
435
450
|
/** The keys of the Node Classes a Node Class's ancestry reaches, itself included. */
|
|
436
451
|
function ancestryKeys(actual, scope) {
|
|
437
|
-
|
|
438
|
-
const keys = new Set();
|
|
439
|
-
const pending = [actual];
|
|
440
|
-
for (let value = pending.pop(); value !== undefined; value = pending.pop()) {
|
|
441
|
-
if (seen.has(value))
|
|
442
|
-
continue;
|
|
443
|
-
seen.add(value);
|
|
444
|
-
keys.add(definitionRefIndex(value.ref));
|
|
445
|
-
for (const parent of value.extends) {
|
|
446
|
-
const resolved = scope.resolveClass(parent);
|
|
447
|
-
if (resolved?.kind === 'node')
|
|
448
|
-
pending.push(resolved);
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
return keys;
|
|
452
|
+
return new Set(Array.from(ancestry(actual, (ref) => scope.resolveClass(ref)), ({ ref }) => definitionRefIndex(ref)));
|
|
452
453
|
}
|
|
453
454
|
function targetContract(term, use) {
|
|
454
455
|
if (use.kind === 'named')
|
|
@@ -481,7 +482,7 @@ function variantsForEdge(use, typing) {
|
|
|
481
482
|
const declaration = typing.scope.edgeDeclaration(edge.class);
|
|
482
483
|
if (declaration === undefined)
|
|
483
484
|
return [];
|
|
484
|
-
const endpoints =
|
|
485
|
+
const endpoints = contractEndpoints(declaration);
|
|
485
486
|
const bounds = edge.repeat ?? { min: 1, max: 1 };
|
|
486
487
|
if (!Number.isInteger(bounds.min) ||
|
|
487
488
|
!Number.isInteger(bounds.max) ||
|
|
@@ -525,17 +526,11 @@ function variantsForEdge(use, typing) {
|
|
|
525
526
|
}
|
|
526
527
|
return uniqueBy(variants, edgeVariantKey);
|
|
527
528
|
}
|
|
528
|
-
/** The two endpoints of an Edge Class, a directed one's as source then target. */
|
|
529
|
-
function endpointPair(declaration) {
|
|
530
|
-
return declaration.orientation === 'directed'
|
|
531
|
-
? [declaration.endpoints.source, declaration.endpoints.target]
|
|
532
|
-
: declaration.endpoints;
|
|
533
|
-
}
|
|
534
529
|
function startEndpoint(endpoints, reversed) {
|
|
535
|
-
return reversed ? endpoints[1] : endpoints[0];
|
|
530
|
+
return (reversed ? endpoints[1] : endpoints[0]).endpoint;
|
|
536
531
|
}
|
|
537
532
|
function endEndpoint(endpoints, reversed) {
|
|
538
|
-
return reversed ? endpoints[0] : endpoints[1];
|
|
533
|
+
return (reversed ? endpoints[0] : endpoints[1]).endpoint;
|
|
539
534
|
}
|
|
540
535
|
/** The values with distinct keys, each at its key's first position and holding its last value. */
|
|
541
536
|
function uniqueBy(values, key) {
|
|
@@ -11,4 +11,28 @@ export interface StateTopology<Transitions extends TransitionRelation = Transiti
|
|
|
11
11
|
readonly transitions: Transitions;
|
|
12
12
|
}
|
|
13
13
|
export type StateTopologyOf<Value> = Value extends StateTopology<infer Transitions, infer Initial> ? StateTopology<Transitions, Initial> : undefined;
|
|
14
|
+
/** A State topology, or why an input describes none. */
|
|
15
|
+
export type StateTopologyResult = {
|
|
16
|
+
readonly ok: true;
|
|
17
|
+
readonly topology: StateTopology;
|
|
18
|
+
} | {
|
|
19
|
+
readonly ok: false;
|
|
20
|
+
readonly message: string;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* The canonical, frozen State topology `input` describes, or why it describes none. It never throws
|
|
24
|
+
* for a portable value, however malformed.
|
|
25
|
+
*/
|
|
26
|
+
export declare function validateStateTopology(input: unknown): StateTopologyResult;
|
|
27
|
+
/** The State topology `input` describes; a TypeError says why when it describes none. */
|
|
14
28
|
export declare function admitStateTopology(input: unknown): StateTopology;
|
|
29
|
+
/**
|
|
30
|
+
* Whether a Property can hold the States of `topology`: it is required and not automatic, and its
|
|
31
|
+
* value schema is a string enumeration of exactly those States, read in `any` order as authored or
|
|
32
|
+
* in the topology's `canonical` order as compiled. The schema is read first: in `any` order its
|
|
33
|
+
* members are sorted as strings, which throws a TypeError for one that cannot become a string.
|
|
34
|
+
*/
|
|
35
|
+
export declare function holdsStates(property: {
|
|
36
|
+
readonly required: unknown;
|
|
37
|
+
readonly auto?: unknown;
|
|
38
|
+
}, schema: unknown, topology: StateTopology, order: 'any' | 'canonical'): boolean;
|
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
import { compareUnicode } from '../../addressing/ordering.js';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* The canonical, frozen State topology `input` describes, or why it describes none. It never throws
|
|
4
|
+
* for a portable value, however malformed.
|
|
5
|
+
*/
|
|
6
|
+
export function validateStateTopology(input) {
|
|
3
7
|
if (!isRecord(input) || !exactKeys(input, ['initial', 'transitions']))
|
|
4
|
-
|
|
8
|
+
return refused(SHAPE);
|
|
5
9
|
const initial = input.initial;
|
|
6
10
|
const authored = input.transitions;
|
|
7
|
-
if (typeof initial !== 'string' || initial.length === 0 || !isRecord(authored))
|
|
8
|
-
|
|
11
|
+
if (typeof initial !== 'string' || initial.length === 0 || !isRecord(authored)) {
|
|
12
|
+
return refused(SHAPE);
|
|
13
|
+
}
|
|
9
14
|
const states = Object.keys(authored);
|
|
10
15
|
if (states.length === 0)
|
|
11
|
-
|
|
16
|
+
return refused('State topology requires at least one state.');
|
|
12
17
|
if (states.includes(''))
|
|
13
|
-
|
|
18
|
+
return refused('State topology has an empty state name.');
|
|
14
19
|
if (!states.includes(initial)) {
|
|
15
|
-
|
|
20
|
+
return refused(`State topology initial state "${initial}" is not declared.`);
|
|
16
21
|
}
|
|
17
22
|
const declared = new Set(states);
|
|
18
23
|
const transitions = Object.create(null);
|
|
@@ -20,17 +25,24 @@ export function admitStateTopology(input) {
|
|
|
20
25
|
for (const state of states.sort(compareUnicode)) {
|
|
21
26
|
const outgoing = authored[state];
|
|
22
27
|
if (!isRecord(outgoing))
|
|
23
|
-
|
|
28
|
+
return refused(`State "${state}" transitions must be an object.`);
|
|
24
29
|
const accepted = Object.create(null);
|
|
25
30
|
for (const event of Object.keys(outgoing).sort(compareUnicode)) {
|
|
26
31
|
const target = outgoing[event];
|
|
27
32
|
if (event.length === 0)
|
|
28
|
-
|
|
33
|
+
return refused(`State "${state}" has an empty event name.`);
|
|
29
34
|
if (typeof target !== 'string' || !declared.has(target)) {
|
|
30
|
-
|
|
35
|
+
// `String` runs a target's own conversion, which can throw: only reading the message runs
|
|
36
|
+
// it, so the refusal is returned whatever the target is.
|
|
37
|
+
return {
|
|
38
|
+
ok: false,
|
|
39
|
+
get message() {
|
|
40
|
+
return `State "${state}" event "${event}" targets undeclared state "${String(target)}".`;
|
|
41
|
+
},
|
|
42
|
+
};
|
|
31
43
|
}
|
|
32
44
|
if (target === state) {
|
|
33
|
-
|
|
45
|
+
return refused(`State "${state}" event "${event}" is a self-transition.`);
|
|
34
46
|
}
|
|
35
47
|
accepted[event] = target;
|
|
36
48
|
transitionCount += 1;
|
|
@@ -38,7 +50,7 @@ export function admitStateTopology(input) {
|
|
|
38
50
|
transitions[state] = Object.freeze(accepted);
|
|
39
51
|
}
|
|
40
52
|
if (transitionCount === 0)
|
|
41
|
-
|
|
53
|
+
return refused('State topology requires at least one transition.');
|
|
42
54
|
const reachable = new Set([initial]);
|
|
43
55
|
const pending = [initial];
|
|
44
56
|
while (pending.length > 0) {
|
|
@@ -50,15 +62,44 @@ export function admitStateTopology(input) {
|
|
|
50
62
|
pending.push(target);
|
|
51
63
|
}
|
|
52
64
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
65
|
+
const unreachable = states.find((state) => !reachable.has(state));
|
|
66
|
+
if (unreachable !== undefined) {
|
|
67
|
+
return refused(`State "${unreachable}" is unreachable from initial "${initial}".`);
|
|
68
|
+
}
|
|
69
|
+
return { ok: true, topology: Object.freeze({ initial, transitions: Object.freeze(transitions) }) };
|
|
70
|
+
}
|
|
71
|
+
/** The State topology `input` describes; a TypeError says why when it describes none. */
|
|
72
|
+
export function admitStateTopology(input) {
|
|
73
|
+
const result = validateStateTopology(input);
|
|
74
|
+
if (!result.ok)
|
|
75
|
+
throw new TypeError(result.message);
|
|
76
|
+
return result.topology;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Whether a Property can hold the States of `topology`: it is required and not automatic, and its
|
|
80
|
+
* value schema is a string enumeration of exactly those States, read in `any` order as authored or
|
|
81
|
+
* in the topology's `canonical` order as compiled. The schema is read first: in `any` order its
|
|
82
|
+
* members are sorted as strings, which throws a TypeError for one that cannot become a string.
|
|
83
|
+
*/
|
|
84
|
+
export function holdsStates(property, schema, topology, order) {
|
|
85
|
+
const enumerates = enumeratesStates(schema, Object.keys(topology.transitions), order);
|
|
86
|
+
return enumerates && Boolean(property.required) && property.auto === undefined;
|
|
87
|
+
}
|
|
88
|
+
function enumeratesStates(schema, states, order) {
|
|
89
|
+
if (!isRecord(schema) || Object.keys(schema).length !== 2 || schema.type !== 'string') {
|
|
90
|
+
return false;
|
|
57
91
|
}
|
|
58
|
-
|
|
92
|
+
const members = schema.enum;
|
|
93
|
+
if (!Array.isArray(members) || members.length !== states.length)
|
|
94
|
+
return false;
|
|
95
|
+
if (order === 'canonical')
|
|
96
|
+
return members.every((member, index) => member === states[index]);
|
|
97
|
+
const expected = states.sort();
|
|
98
|
+
return [...members].sort().every((member, index) => member === expected[index]);
|
|
59
99
|
}
|
|
60
|
-
|
|
61
|
-
|
|
100
|
+
const SHAPE = 'State topology must contain exactly initial and transitions.';
|
|
101
|
+
function refused(message) {
|
|
102
|
+
return { ok: false, message };
|
|
62
103
|
}
|
|
63
104
|
function isRecord(input) {
|
|
64
105
|
return input !== null && typeof input === 'object' && !Array.isArray(input);
|
|
@@ -98,7 +98,7 @@ export const VALUE_RULES = deepFreeze({
|
|
|
98
98
|
VS_VALUE_KIND: {
|
|
99
99
|
stage: 'schema',
|
|
100
100
|
positions: ['{/path*}/x-astrale-value-kind', '{/path*}/x-astrale-file'],
|
|
101
|
-
statement: 'x-astrale-value-kind declares instant beside type string at any location, blob as the whole schema of a Property, exactly as values.blob writes it, or file at any location of a callable input, exactly as values.file writes it, the only place x-astrale-file stands.',
|
|
101
|
+
statement: 'x-astrale-value-kind declares date or instant beside type string at any location, blob as the whole schema of a Property, exactly as values.blob writes it, or file at any location of a callable input, exactly as values.file writes it, the only place x-astrale-file stands.',
|
|
102
102
|
conditions: ['#/$defs/value-location/anyOf/1/if'],
|
|
103
103
|
},
|
|
104
104
|
VS_VALUE_KIND_JSON: {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { equal } from '@astrale-os/kernel-dsl/value';
|
|
2
2
|
import { runSuspended } from '../../suspended.js';
|
|
3
3
|
import { isCanonicalBlobRef } from '../model/blob.js';
|
|
4
|
+
import { isDateValue } from '../model/date.js';
|
|
4
5
|
import { isInstantValue } from '../model/instant.js';
|
|
5
6
|
import { isValueObject as isJsonObject } from '../model/object.js';
|
|
6
7
|
import { appendPointer } from '../syntax/pointer.js';
|
|
@@ -140,6 +141,9 @@ class InstanceEvaluator {
|
|
|
140
141
|
}
|
|
141
142
|
}
|
|
142
143
|
evaluateCommonAssertions(schema, input, instancePointer, failures) {
|
|
144
|
+
if (schema['x-astrale-value-kind'] === 'date' && !isDateValue(input)) {
|
|
145
|
+
failures.add(invalid(instancePointer, 'x-astrale-value-kind', 'Expected a Gregorian calendar date in YYYY-MM-DD form.'));
|
|
146
|
+
}
|
|
143
147
|
if (schema['x-astrale-value-kind'] === 'instant' && !isInstantValue(input)) {
|
|
144
148
|
failures.add(invalid(instancePointer, 'x-astrale-value-kind', 'Value is not an exact UTC millisecond Instant.'));
|
|
145
149
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { equal } from '@astrale-os/kernel-dsl/value';
|
|
2
2
|
import { isCanonicalBlobRef } from '../model/blob.js';
|
|
3
|
+
import { isDateValue } from '../model/date.js';
|
|
3
4
|
import { isInstantValue } from '../model/instant.js';
|
|
4
5
|
import { isValueObject as isJsonObject } from '../model/object.js';
|
|
5
6
|
import { isJsonSchema, schemaList } from '../syntax/walk.js';
|
|
@@ -156,7 +157,9 @@ function compileCompositions(schema, child, parts) {
|
|
|
156
157
|
function compileCommon(schema, parts) {
|
|
157
158
|
const kind = schema['x-astrale-value-kind'];
|
|
158
159
|
if (kind === 'instant')
|
|
159
|
-
parts.push(
|
|
160
|
+
parts.push(isInstantValue);
|
|
161
|
+
if (kind === 'date')
|
|
162
|
+
parts.push(isDateValue);
|
|
160
163
|
if (kind === 'blob')
|
|
161
164
|
parts.push((value) => isCanonicalBlobRef(value));
|
|
162
165
|
const type = schema.type;
|
|
@@ -50,7 +50,7 @@ export function evidenceOf(location) {
|
|
|
50
50
|
const valueKind = schema['x-astrale-value-kind'];
|
|
51
51
|
if (typeof valueKind === 'string') {
|
|
52
52
|
valueKinds.add(valueKind);
|
|
53
|
-
if (valueKind === 'instant')
|
|
53
|
+
if (valueKind === 'instant' || valueKind === 'date')
|
|
54
54
|
kinds &= STRING;
|
|
55
55
|
if (valueKind === 'blob')
|
|
56
56
|
kinds &= OBJECT;
|