@astrale-os/kernel-dsl 1.0.0-beta.27 → 1.0.0-beta.29

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 (91) hide show
  1. package/dist/v1/addressing/ordering.d.ts +2 -2
  2. package/dist/v1/addressing/ordering.js +2 -4
  3. package/dist/v1/builder/index.d.ts +2 -0
  4. package/dist/v1/builder/index.js +1 -0
  5. package/dist/v1/builder/schema/projection/identity.d.ts +15 -0
  6. package/dist/v1/builder/schema/projection/property.d.ts +3 -3
  7. package/dist/v1/builder/schema/projection/resolved-class.d.ts +3 -3
  8. package/dist/v1/builder/schema/rule.d.ts +316 -0
  9. package/dist/v1/builder/schema/rule.js +192 -0
  10. package/dist/v1/bundle/index.d.ts +2 -0
  11. package/dist/v1/bundle/index.js +1 -0
  12. package/dist/v1/bundle/rule.d.ts +14 -0
  13. package/dist/v1/bundle/rule.js +41 -0
  14. package/dist/v1/compiled/admission/diagnostic.d.ts +14 -0
  15. package/dist/v1/compiled/admission/diagnostic.js +6 -0
  16. package/dist/v1/compiled/admission/semantics/policy/callable.js +2 -3
  17. package/dist/v1/compiled/admission/semantics/policy/parity.js +8 -13
  18. package/dist/v1/compiled/admission/semantics/policy/pattern.js +3 -5
  19. package/dist/v1/compiled/admission/wire/policy.js +16 -11
  20. package/dist/v1/compiled/index.d.ts +2 -0
  21. package/dist/v1/compiled/index.js +1 -0
  22. package/dist/v1/compiled/rule.d.ts +49 -0
  23. package/dist/v1/compiled/rule.js +169 -0
  24. package/dist/v1/domain/namespace/member-collection.js +4 -1
  25. package/dist/v1/language/callable/admission.js +2 -1
  26. package/dist/v1/language/class/resolved.d.ts +9 -4
  27. package/dist/v1/language/policy/analysis/branches.js +6 -7
  28. package/dist/v1/language/policy/analysis/budgets.js +8 -6
  29. package/dist/v1/language/policy/analysis/checks.js +5 -4
  30. package/dist/v1/language/policy/analysis/composition.js +2 -2
  31. package/dist/v1/language/policy/analysis/typing.js +3 -2
  32. package/dist/v1/language/policy/model/policy.d.ts +22 -0
  33. package/dist/v1/language/policy/model/policy.js +28 -4
  34. package/dist/v1/language/policy/normalization/normalize.js +33 -56
  35. package/dist/v1/language/value/evaluate/assertions.d.ts +2 -2
  36. package/dist/v1/language/value/evaluate/assertions.js +4 -34
  37. package/dist/v1/language/value/evaluate/evaluator.js +10 -15
  38. package/dist/v1/language/value/evaluate/validity.optimization.js +9 -14
  39. package/dist/v1/language/value/files.js +2 -1
  40. package/dist/v1/language/value/model/blob.d.ts +6 -0
  41. package/dist/v1/language/value/model/blob.js +8 -0
  42. package/dist/v1/language/value/ordering.js +1 -3
  43. package/dist/v1/language/value/profile/canonicalize.js +1 -3
  44. package/dist/v1/language/value/syntax/walk.d.ts +2 -0
  45. package/dist/v1/language/value/syntax/walk.js +4 -0
  46. package/dist/v1/schema/compatibility/api.d.ts +2 -0
  47. package/dist/v1/schema/compatibility/api.js +1 -0
  48. package/dist/v1/schema/compatibility/rule.d.ts +13 -0
  49. package/dist/v1/schema/compatibility/rule.js +23 -0
  50. package/dist/v1/schema/enforcement/changes.d.ts +590 -0
  51. package/dist/v1/schema/enforcement/changes.js +566 -0
  52. package/dist/v1/schema/enforcement/derived.d.ts +236 -0
  53. package/dist/v1/schema/enforcement/derived.js +243 -0
  54. package/dist/v1/schema/enforcement/dispose.d.ts +308 -0
  55. package/dist/v1/schema/enforcement/dispose.js +275 -0
  56. package/dist/v1/schema/enforcement/facts.d.ts +89 -0
  57. package/dist/v1/schema/enforcement/facts.js +70 -0
  58. package/dist/v1/schema/enforcement/fields.d.ts +65 -0
  59. package/dist/v1/schema/enforcement/fields.js +36 -0
  60. package/dist/v1/schema/enforcement/index.d.ts +39 -0
  61. package/dist/v1/schema/enforcement/index.js +10 -0
  62. package/dist/v1/schema/enforcement/install.d.ts +1634 -0
  63. package/dist/v1/schema/enforcement/install.js +1223 -0
  64. package/dist/v1/schema/enforcement/lifecycle.d.ts +87 -0
  65. package/dist/v1/schema/enforcement/lifecycle.js +91 -0
  66. package/dist/v1/schema/enforcement/model.d.ts +253 -0
  67. package/dist/v1/schema/enforcement/model.js +1 -0
  68. package/dist/v1/schema/enforcement/obligations.d.ts +248 -0
  69. package/dist/v1/schema/enforcement/obligations.js +240 -0
  70. package/dist/v1/schema/enforcement/write.d.ts +1314 -0
  71. package/dist/v1/schema/enforcement/write.js +1017 -0
  72. package/dist/v1/schema/index.d.ts +1 -0
  73. package/dist/v1/schema/index.js +1 -0
  74. package/dist/v1/schema/resolution/origins.js +4 -7
  75. package/dist/v1/schema/rule.d.ts +4 -5
  76. package/dist/v1/schema/rule.js +19 -13
  77. package/dist/v1/schema/validation/policy/references.js +5 -11
  78. package/dist/v1/schema/validation/policy/uses.js +3 -8
  79. package/dist/v1/schema/validation/value-api.d.ts +1 -1
  80. package/dist/v1/schema/validation/value-api.js +1 -1
  81. package/dist/value/accept.d.ts +6 -0
  82. package/dist/value/accept.js +25 -1
  83. package/dist/value/canonical.d.ts +16 -1
  84. package/dist/value/canonical.js +90 -33
  85. package/dist/value/immutable.optimization.d.ts +2 -0
  86. package/dist/value/immutable.optimization.js +4 -0
  87. package/dist/value/index.d.ts +3 -1
  88. package/dist/value/index.js +2 -1
  89. package/dist/value/rule.d.ts +12 -0
  90. package/dist/value/rule.js +50 -0
  91. package/package.json +5 -1
@@ -1,6 +1,6 @@
1
1
  import { limits } from '../../limits.js';
2
2
  import { runSuspended } from '../../suspended.js';
3
- import { policyObjectOrigin, policyVariableClass } from '../model/policy.js';
3
+ import { PolicyComposition, policyObjectOrigin, policyVariableClass } from '../model/policy.js';
4
4
  /**
5
5
  * Where a count of Policy branches stops: one past its limit. Each branch count is exact up to its
6
6
  * limit plus one and reads as that once it is over, so expansion stays bounded.
@@ -41,7 +41,7 @@ export function policyMeasurer(origin, normalized, isLocal) {
41
41
  function* measureDeclaration(declaration) {
42
42
  if ('match' in declaration)
43
43
  return measurePattern(declaration.match, origin);
44
- const refs = 'allOf' in declaration ? declaration.allOf : declaration.anyOf;
44
+ const { operator, operands: refs } = PolicyComposition.of(declaration);
45
45
  const children = [];
46
46
  for (const ref of refs) {
47
47
  if (!isLocal(ref))
@@ -52,7 +52,7 @@ export function policyMeasurer(origin, normalized, isLocal) {
52
52
  }
53
53
  if (children.length !== refs.length || children.length === 0)
54
54
  return undefined;
55
- return combineMeasures('allOf' in declaration ? 'allOf' : 'anyOf', children);
55
+ return combineMeasures(operator, children);
56
56
  }
57
57
  return (name) => {
58
58
  const measure = runSuspended(measurePolicy(name));
@@ -100,7 +100,8 @@ function measurePattern(pattern, origin) {
100
100
  ]),
101
101
  };
102
102
  }
103
- return combineMeasures('allOf' in pattern ? 'allOf' : 'anyOf', ('allOf' in pattern ? pattern.allOf : pattern.anyOf).map((child) => measurePattern(child, origin)));
103
+ const { operator, operands } = PolicyComposition.of(pattern);
104
+ return combineMeasures(operator, operands.map((child) => measurePattern(child, origin)));
104
105
  }
105
106
  function combineMeasures(kind, children) {
106
107
  const depth = 1 + Math.max(0, ...children.map((child) => child.depth));
@@ -131,8 +132,9 @@ function combineMeasures(kind, children) {
131
132
  export function measureChecks(expression) {
132
133
  if ('check' in expression || 'sameNode' in expression)
133
134
  return { checkDepth: 1, checkBranches: 1, checkLeaves: 1 };
134
- const children = ('allOf' in expression ? expression.allOf : expression.anyOf).map(measureChecks);
135
- const checkBranches = 'allOf' in expression
135
+ const { operator, operands } = PolicyComposition.of(expression);
136
+ const children = operands.map(measureChecks);
137
+ const checkBranches = operator === 'allOf'
136
138
  ? children.reduce((value, child) => Math.min(CHECK_BRANCH_OVERFLOW, value * child.checkBranches), 1)
137
139
  : children.reduce((value, child) => Math.min(CHECK_BRANCH_OVERFLOW, value + child.checkBranches), 0);
138
140
  return {
@@ -1,5 +1,5 @@
1
1
  import { limits } from '../../limits.js';
2
- import { policyObjectKey } from '../model/policy.js';
2
+ import { PolicyComposition, policyObjectKey } from '../model/policy.js';
3
3
  import { measureChecks } from './budgets.js';
4
4
  /** Names a callable check object. Every leaf over one object sees the same Node at runtime. */
5
5
  function checkObjectName(object) {
@@ -73,9 +73,10 @@ export function neverAdmittedClasses(expression, leaves) {
73
73
  function disjunctiveForm(expression) {
74
74
  if ('check' in expression || 'sameNode' in expression)
75
75
  return [[expression]];
76
- if ('anyOf' in expression)
77
- return expression.anyOf.flatMap(disjunctiveForm);
78
- return expression.allOf
76
+ const { operator, operands } = PolicyComposition.of(expression);
77
+ if (operator === 'anyOf')
78
+ return operands.flatMap(disjunctiveForm);
79
+ return operands
79
80
  .map(disjunctiveForm)
80
81
  .reduce((left, right) => left.flatMap((prefix) => right.map((suffix) => [...prefix, ...suffix])), [[]]);
81
82
  }
@@ -1,11 +1,11 @@
1
1
  import { cycleEdges } from '../../cycle.js';
2
+ import { PolicyComposition } from '../model/policy.js';
2
3
  /** Each edge closing a cycle among the named Policies of `origin`, walking from `names` in order. */
3
4
  export function compositionCycles(names, expressionOf, origin) {
4
5
  return cycleEdges(names, (name) => {
5
6
  const expression = expressionOf(name);
6
7
  if (expression === undefined || 'match' in expression)
7
8
  return [];
8
- const refs = 'allOf' in expression ? expression.allOf : expression.anyOf;
9
- return refs.flatMap((ref) => (ref.origin === origin ? [ref.name] : []));
9
+ return PolicyComposition.of(expression).operands.flatMap((ref) => ref.origin === origin ? [ref.name] : []);
10
10
  });
11
11
  }
@@ -2,6 +2,7 @@ import { Key } from '../../../addressing/index.js';
2
2
  import { definitionRefIndex } from '../../../addressing/ordering.js';
3
3
  import { classSatisfies, isGraphRoot } from '../../class/inheritance.js';
4
4
  import { limits } from '../../limits.js';
5
+ import { PolicyComposition } from '../model/policy.js';
5
6
  import { branchesOf, branchProduct, capBranches, targetMode, termKey, UnionFind, } from './branches.js';
6
7
  import { admittedCheckClasses } from './checks.js';
7
8
  export class PolicyTyping {
@@ -94,7 +95,7 @@ export class PolicyTyping {
94
95
  return branchesOf(declaration.match, namespace);
95
96
  const nested = new Set(visiting);
96
97
  nested.add(name);
97
- const refs = 'allOf' in declaration ? declaration.allOf : declaration.anyOf;
98
+ const { operator, operands: refs } = PolicyComposition.of(declaration);
98
99
  const children = [];
99
100
  for (const [index, ref] of refs.entries()) {
100
101
  if (!this.scope.isLocalPolicy(ref))
@@ -104,7 +105,7 @@ export class PolicyTyping {
104
105
  return undefined;
105
106
  children.push(expanded);
106
107
  }
107
- return 'allOf' in declaration ? branchProduct(children) : capBranches(children.flat());
108
+ return operator === 'allOf' ? branchProduct(children) : capBranches(children.flat());
108
109
  }
109
110
  #constraintsAreSatisfiable(branch, variants, use) {
110
111
  const terms = new Set(use.kind === 'object' ? ['subject', 'object'] : ['subject', 'source', 'target']);
@@ -73,6 +73,28 @@ export type PolicyPatternExpression = PolicyEdgePattern | PolicySameNode<PolicyP
73
73
  } | {
74
74
  readonly anyOf: readonly [PolicyPatternExpression, ...PolicyPatternExpression[]];
75
75
  };
76
+ /**
77
+ * A composition of a Pattern, a named Policy or Policy checks, read through its operator: `allOf`
78
+ * holds where every operand holds, `anyOf` where one does. It is stored under the one key its
79
+ * operator names; every reader takes the operator and the operands from here.
80
+ */
81
+ export declare namespace PolicyComposition {
82
+ type Operator = 'allOf' | 'anyOf';
83
+ type Operands<Operand> = readonly [Operand, ...Operand[]];
84
+ /** The stored form: the operands under the key their operator names. */
85
+ type Stored<Operand> = {
86
+ readonly allOf: Operands<Operand>;
87
+ } | {
88
+ readonly anyOf: Operands<Operand>;
89
+ };
90
+ function is<Value extends object>(value: Value): value is Extract<Value, Stored<unknown>>;
91
+ function of<Operand>(composition: Stored<Operand>): {
92
+ readonly operator: Operator;
93
+ readonly operands: Operands<Operand>;
94
+ };
95
+ /** Store `operands` under the key `operator` names. */
96
+ function from<Operand>(operator: Operator, operands: Operands<Operand>): Stored<Operand>;
97
+ }
76
98
  export type PolicyExpression = {
77
99
  readonly match: PolicyPatternExpression;
78
100
  } | {
@@ -7,6 +7,29 @@ export function policyVariableClass(variable) {
7
7
  const selector = policyVariableSelector(variable);
8
8
  return selector.kind === 'any' ? undefined : selector.class;
9
9
  }
10
+ /**
11
+ * A composition of a Pattern, a named Policy or Policy checks, read through its operator: `allOf`
12
+ * holds where every operand holds, `anyOf` where one does. It is stored under the one key its
13
+ * operator names; every reader takes the operator and the operands from here.
14
+ */
15
+ export var PolicyComposition;
16
+ (function (PolicyComposition) {
17
+ function is(value) {
18
+ return 'allOf' in value || 'anyOf' in value;
19
+ }
20
+ PolicyComposition.is = is;
21
+ function of(composition) {
22
+ return 'allOf' in composition
23
+ ? { operator: 'allOf', operands: composition.allOf }
24
+ : { operator: 'anyOf', operands: composition.anyOf };
25
+ }
26
+ PolicyComposition.of = of;
27
+ /** Store `operands` under the key `operator` names. */
28
+ function from(operator, operands) {
29
+ return operator === 'allOf' ? { allOf: operands } : { anyOf: operands };
30
+ }
31
+ PolicyComposition.from = from;
32
+ })(PolicyComposition || (PolicyComposition = {}));
10
33
  /** Exact projected identity, including Class-owned Method declarations. */
11
34
  export function policyObjectKey(ref) {
12
35
  return ref.kind === 'method'
@@ -49,7 +72,7 @@ export function isClassObservation(operation) {
49
72
  }
50
73
  export function isClassRuleChecks(value) {
51
74
  return (typeof value === 'object' &&
52
- ('check' in value || 'sameNode' in value || 'allOf' in value || 'anyOf' in value));
75
+ ('check' in value || 'sameNode' in value || PolicyComposition.is(value)));
53
76
  }
54
77
  /**
55
78
  * Whether every way Policy checks can hold passes through a check, so that the caller decides each
@@ -60,7 +83,8 @@ export function checksNameTheCaller(value) {
60
83
  return true;
61
84
  if ('sameNode' in value)
62
85
  return false;
63
- if ('allOf' in value)
64
- return value.allOf.some(checksNameTheCaller);
65
- return value.anyOf.every(checksNameTheCaller);
86
+ const { operator, operands } = PolicyComposition.of(value);
87
+ return operator === 'allOf'
88
+ ? operands.some(checksNameTheCaller)
89
+ : operands.every(checksNameTheCaller);
66
90
  }
@@ -1,14 +1,12 @@
1
1
  import { json } from '@astrale-os/kernel-dsl/value';
2
2
  import { definitionRefIndex, compareDefinitionRefs } from '../../../addressing/ordering.js';
3
- import { isClassRuleChecks, policyObjectKey, policyVariableSelector } from '../model/policy.js';
3
+ import { isClassRuleChecks, PolicyComposition, policyObjectKey, policyVariableSelector, } from '../model/policy.js';
4
4
  export function normalizePolicyExpression(value) {
5
5
  if ('match' in value) {
6
6
  return { match: normalizePolicyPattern(value.match) };
7
7
  }
8
- if ('allOf' in value) {
9
- return { allOf: operands(uniqueRefs(value.allOf)) };
10
- }
11
- return { anyOf: operands(uniqueRefs(value.anyOf)) };
8
+ const { operator, operands: refs } = PolicyComposition.of(value);
9
+ return PolicyComposition.from(operator, operands(uniqueRefs(refs)));
12
10
  }
13
11
  /**
14
12
  * Passes after the first, while one still changes the Pattern. A pass numbers variables by the
@@ -55,36 +53,20 @@ export function normalizeClassRule(value) {
55
53
  function unwrapSingleOperands(value) {
56
54
  if ('check' in value || 'sameNode' in value)
57
55
  return value;
58
- const all = 'allOf' in value;
59
- const children = uniqueJson((all ? value.allOf : value.anyOf).flatMap((child) => {
60
- const unwrapped = unwrapSingleOperands(child);
61
- if (all && 'allOf' in unwrapped)
62
- return unwrapped.allOf;
63
- if (!all && 'anyOf' in unwrapped)
64
- return unwrapped.anyOf;
65
- return [unwrapped];
66
- }));
56
+ const { operator, operands: values } = PolicyComposition.of(value);
57
+ const children = uniqueJson(values.flatMap((child) => operandsUnder(operator, unwrapSingleOperands(child))));
67
58
  if (children.length === 1)
68
59
  return children[0];
69
- return all ? { allOf: operands(children) } : { anyOf: operands(children) };
60
+ return PolicyComposition.from(operator, operands(children));
70
61
  }
71
62
  export function normalizePolicyChecks(value) {
72
63
  if ('check' in value)
73
64
  return value;
74
65
  if ('sameNode' in value)
75
66
  return { sameNode: normalizeEquality(value.sameNode) };
76
- if ('allOf' in value) {
77
- const children = value.allOf.flatMap((child) => {
78
- const normalized = normalizePolicyChecks(child);
79
- return 'allOf' in normalized ? normalized.allOf : [normalized];
80
- });
81
- return { allOf: operands(uniqueJson(children)) };
82
- }
83
- const children = value.anyOf.flatMap((child) => {
84
- const normalized = normalizePolicyChecks(child);
85
- return 'anyOf' in normalized ? normalized.anyOf : [normalized];
86
- });
87
- return { anyOf: operands(uniqueJson(children)) };
67
+ const { operator, operands: values } = PolicyComposition.of(value);
68
+ const children = values.flatMap((child) => operandsUnder(operator, normalizePolicyChecks(child)));
69
+ return PolicyComposition.from(operator, operands(uniqueJson(children)));
88
70
  }
89
71
  /**
90
72
  * The operands of a non-empty list, mapped, flattened, deduplicated or sorted one by one: still at
@@ -93,6 +75,16 @@ export function normalizePolicyChecks(value) {
93
75
  function operands(values) {
94
76
  return values;
95
77
  }
78
+ /**
79
+ * What `value` contributes to a composition under `operator`: its operands if it composes so. The
80
+ * grammar is recursive, so a composition's operands are of its own expression type.
81
+ */
82
+ function operandsUnder(operator, value) {
83
+ if (!PolicyComposition.is(value))
84
+ return [value];
85
+ const composed = PolicyComposition.of(value);
86
+ return composed.operator === operator ? composed.operands : [value];
87
+ }
96
88
  function normalizePatternNode(value) {
97
89
  if ('sameNode' in value)
98
90
  return { sameNode: normalizeEquality(value.sameNode) };
@@ -112,18 +104,9 @@ function normalizePatternNode(value) {
112
104
  },
113
105
  };
114
106
  }
115
- if ('allOf' in value) {
116
- const children = value.allOf.flatMap((child) => {
117
- const normalized = normalizePatternNode(child);
118
- return 'allOf' in normalized ? normalized.allOf : [normalized];
119
- });
120
- return { allOf: operands(uniqueJson(children)) };
121
- }
122
- const children = value.anyOf.flatMap((child) => {
123
- const normalized = normalizePatternNode(child);
124
- return 'anyOf' in normalized ? normalized.anyOf : [normalized];
125
- });
126
- return { anyOf: operands(uniqueJson(children)) };
107
+ const { operator, operands: values } = PolicyComposition.of(value);
108
+ const children = values.flatMap((child) => operandsUnder(operator, normalizePatternNode(child)));
109
+ return PolicyComposition.from(operator, operands(uniqueJson(children)));
127
110
  }
128
111
  function canonicalVariableIds(value) {
129
112
  const declarations = new Map();
@@ -181,12 +164,10 @@ function collectDeclarations(value, result, depths, depth = 0) {
181
164
  collectDeclarations(value.exists.where, result, depths, depth + 1);
182
165
  return;
183
166
  }
184
- if ('allOf' in value)
185
- for (const child of value.allOf)
186
- collectDeclarations(child, result, depths, depth);
187
- if ('anyOf' in value)
188
- for (const child of value.anyOf)
189
- collectDeclarations(child, result, depths, depth);
167
+ if (!PolicyComposition.is(value))
168
+ return;
169
+ for (const child of PolicyComposition.of(value).operands)
170
+ collectDeclarations(child, result, depths, depth);
190
171
  }
191
172
  function collectUses(value, result) {
192
173
  if ('sameNode' in value) {
@@ -207,8 +188,7 @@ function collectUses(value, result) {
207
188
  }
208
189
  if ('exists' in value)
209
190
  return collectUses(value.exists.where, result);
210
- const children = 'allOf' in value ? value.allOf : value.anyOf;
211
- for (const child of children)
191
+ for (const child of PolicyComposition.of(value).operands)
212
192
  collectUses(child, result);
213
193
  }
214
194
  function rank(names) {
@@ -254,12 +234,11 @@ function collectFirstUseKeys(value, colors, scoped, result, prefix = '$') {
254
234
  collectFirstUseKeys(value.exists.where, colors, scoped, result, `${prefix}:exists`);
255
235
  return;
256
236
  }
257
- const key = 'allOf' in value ? 'allOf' : 'anyOf';
258
- const operands = 'allOf' in value ? value.allOf : value.anyOf;
237
+ const { operator, operands } = PolicyComposition.of(value);
259
238
  const children = operands
260
239
  .map((child) => [patternSignature(child, colors), patternSignature(child, scoped), child])
261
240
  .sort(([left, leftScoped], [right, rightScoped]) => compareUtf8Bytes(left, right) || compareUtf8Bytes(leftScoped, rightScoped));
262
- children.forEach(([, , child], index) => collectFirstUseKeys(child, colors, scoped, result, `${prefix}:${key}:${index}`));
241
+ children.forEach(([, , child], index) => collectFirstUseKeys(child, colors, scoped, result, `${prefix}:${operator}:${index}`));
263
242
  }
264
243
  function patternSignature(value, colors) {
265
244
  if ('sameNode' in value)
@@ -273,9 +252,8 @@ function patternSignature(value, colors) {
273
252
  .sort(compareUtf8Bytes)
274
253
  .join(',')}:${patternSignature(value.exists.where, colors)}`;
275
254
  }
276
- const key = 'allOf' in value ? 'allOf' : 'anyOf';
277
- const operands = 'allOf' in value ? value.allOf : value.anyOf;
278
- return `${key}:${operands
255
+ const { operator, operands } = PolicyComposition.of(value);
256
+ return `${operator}:${operands
279
257
  .map((child) => patternSignature(child, colors))
280
258
  .sort(compareUtf8Bytes)
281
259
  .join('|')}`;
@@ -311,9 +289,8 @@ function rewriteVariables(value, ids) {
311
289
  },
312
290
  };
313
291
  }
314
- if ('allOf' in value)
315
- return { allOf: operands(value.allOf.map((child) => rewriteVariables(child, ids))) };
316
- return { anyOf: operands(value.anyOf.map((child) => rewriteVariables(child, ids))) };
292
+ const { operator, operands: values } = PolicyComposition.of(value);
293
+ return PolicyComposition.from(operator, operands(values.map((child) => rewriteVariables(child, ids))));
317
294
  }
318
295
  function selectorSignature(node) {
319
296
  const selector = policyVariableSelector(node);
@@ -4,8 +4,8 @@ export declare function matchesType(input: string, value: JsonValue): boolean;
4
4
  export declare function codePointLength(input: string): number;
5
5
  /**
6
6
  * Whether no two items are the same JSON value, at any size their admission allowed, in time linear
7
- * in their tokens: an item whose reading adds no node to the trie of the readings before it repeats
8
- * one of them, since no reading is a prefix of another.
7
+ * in their canonical tokens: an item whose tokens add no node to the trie of the items before it
8
+ * repeats one of them, since no item's tokens are a prefix of another's.
9
9
  */
10
10
  export declare function unique(values: readonly JsonValue[]): boolean;
11
11
  /** Compare finite JSON numbers as their exact ECMAScript decimal spellings. */
@@ -1,3 +1,4 @@
1
+ import { canonicalTokens } from '../../../../value/canonical.js';
1
2
  import { isValueObject as isJsonObject } from '../model/object.js';
2
3
  export function matchesType(input, value) {
3
4
  switch (input) {
@@ -32,15 +33,15 @@ export function codePointLength(input) {
32
33
  }
33
34
  /**
34
35
  * Whether no two items are the same JSON value, at any size their admission allowed, in time linear
35
- * in their tokens: an item whose reading adds no node to the trie of the readings before it repeats
36
- * one of them, since no reading is a prefix of another.
36
+ * in their canonical tokens: an item whose tokens add no node to the trie of the items before it
37
+ * repeats one of them, since no item's tokens are a prefix of another's.
37
38
  */
38
39
  export function unique(values) {
39
40
  const readings = new Map();
40
41
  for (const value of values) {
41
42
  let node = readings;
42
43
  let repeated = true;
43
- for (const token of readingOf(value)) {
44
+ for (const token of canonicalTokens(value)) {
44
45
  let next = node.get(token);
45
46
  if (next === undefined) {
46
47
  next = new Map();
@@ -54,37 +55,6 @@ export function unique(values) {
54
55
  }
55
56
  return true;
56
57
  }
57
- const ARRAY = Symbol('array');
58
- const OBJECT = Symbol('object');
59
- const END = Symbol('end');
60
- /**
61
- * An admitted value as tokens: each scalar and member name, and where each container starts and
62
- * ends, with member names sorted. Two values have the same reading exactly when they have the same
63
- * canonical JSON text, and no reading is a prefix of another. Admission bounded the value under the
64
- * limits of its validation, so a reading checks none of its own, and it holds neither the text,
65
- * which may outgrow any string, nor a stack frame per level.
66
- */
67
- function* readingOf(value) {
68
- const pending = [value];
69
- while (pending.length > 0) {
70
- const next = pending.pop();
71
- if (next === null || typeof next !== 'object') {
72
- yield next;
73
- }
74
- else if (isJsonObject(next)) {
75
- yield OBJECT;
76
- pending.push(END);
77
- for (const name of Object.keys(next).sort())
78
- pending.push(next[name], name);
79
- }
80
- else {
81
- yield ARRAY;
82
- pending.push(END);
83
- for (const item of next)
84
- pending.push(item);
85
- }
86
- }
87
- }
88
58
  /** Compare finite JSON numbers as their exact ECMAScript decimal spellings. */
89
59
  export function isMultipleOf(value, divisor) {
90
60
  if (!(divisor > 0))
@@ -4,6 +4,7 @@ import { isCanonicalBlobRef } from '../model/blob.js';
4
4
  import { isInstantValue } from '../model/instant.js';
5
5
  import { isValueObject as isJsonObject } from '../model/object.js';
6
6
  import { appendPointer } from '../syntax/pointer.js';
7
+ import { isJsonSchema, schemaList } from '../syntax/walk.js';
7
8
  import { codePointLength, isMultipleOf, matchesType, unique } from './assertions.js';
8
9
  import { evaluation, mergeAnnotations } from './types.js';
9
10
  /** Evaluate complete Draft 2020-12 acceptance without dynamic source generation. */
@@ -122,17 +123,17 @@ class InstanceEvaluator {
122
123
  mergeAnnotations(properties, items, admitted[0]);
123
124
  }
124
125
  }
125
- if (isSchema(schema.not)) {
126
+ if (isJsonSchema(schema.not)) {
126
127
  const result = yield this.evaluate(this.child(location, 'not'), input, instancePointer);
127
128
  if (result.valid)
128
129
  failures.add(invalid(instancePointer, 'not', 'Value satisfies not.'));
129
130
  }
130
- if (isSchema(schema.if)) {
131
+ if (isJsonSchema(schema.if)) {
131
132
  const condition = yield this.evaluate(this.child(location, 'if'), input, instancePointer);
132
133
  if (condition.valid)
133
134
  mergeAnnotations(properties, items, condition);
134
135
  const branch = condition.valid ? 'then' : 'else';
135
- if (isSchema(schema[branch])) {
136
+ if (isJsonSchema(schema[branch])) {
136
137
  const result = yield this.evaluate(this.child(location, branch), input, instancePointer);
137
138
  absorb(result, failures, properties, items);
138
139
  }
@@ -223,7 +224,7 @@ class InstanceEvaluator {
223
224
  for (let index = 0; index < presentPrefix; index++)
224
225
  items.add(index);
225
226
  }
226
- if (isSchema(schema.items)) {
227
+ if (isJsonSchema(schema.items)) {
227
228
  let itemValid = true;
228
229
  const admitted = [];
229
230
  for (let index = prefixItems.length; index < input.length; index++) {
@@ -238,7 +239,7 @@ class InstanceEvaluator {
238
239
  if (itemValid)
239
240
  admitted.forEach((index) => items.add(index));
240
241
  }
241
- if (isSchema(schema.contains)) {
242
+ if (isJsonSchema(schema.contains)) {
242
243
  const matches = [];
243
244
  for (let index = 0; index < input.length; index++) {
244
245
  const result = yield this.evaluate(this.child(location, 'contains'), input[index], appendPointer(instancePointer, index));
@@ -254,7 +255,7 @@ class InstanceEvaluator {
254
255
  matches.forEach((index) => items.add(index));
255
256
  }
256
257
  }
257
- if (isSchema(schema.unevaluatedItems)) {
258
+ if (isJsonSchema(schema.unevaluatedItems)) {
258
259
  let valid = true;
259
260
  const admitted = [];
260
261
  for (let index = 0; index < input.length; index++) {
@@ -327,7 +328,7 @@ class InstanceEvaluator {
327
328
  }
328
329
  if (patternsValid)
329
330
  patternMatched.forEach((name) => properties.add(name));
330
- if (isSchema(schema.additionalProperties)) {
331
+ if (isJsonSchema(schema.additionalProperties)) {
331
332
  const additional = names.filter((name) => !Object.hasOwn(declared, name) && !patternMatched.has(name));
332
333
  let additionalValid = true;
333
334
  for (const name of additional) {
@@ -340,7 +341,7 @@ class InstanceEvaluator {
340
341
  if (additionalValid)
341
342
  additional.forEach((name) => properties.add(name));
342
343
  }
343
- if (isSchema(schema.propertyNames)) {
344
+ if (isJsonSchema(schema.propertyNames)) {
344
345
  for (const name of names) {
345
346
  const result = yield this.evaluate(this.child(location, 'propertyNames'), name, appendPointer(instancePointer, name));
346
347
  if (!result.valid)
@@ -358,7 +359,7 @@ class InstanceEvaluator {
358
359
  addFailures(failures, result);
359
360
  }
360
361
  }
361
- if (isSchema(schema.unevaluatedProperties)) {
362
+ if (isJsonSchema(schema.unevaluatedProperties)) {
362
363
  const unevaluated = names.filter((name) => !properties.has(name));
363
364
  let valid = true;
364
365
  const admitted = [];
@@ -435,12 +436,6 @@ function contradictsRequiredConst(schema, input) {
435
436
  }
436
437
  return false;
437
438
  }
438
- function schemaList(input) {
439
- return Array.isArray(input) ? input : undefined;
440
- }
441
- function isSchema(input) {
442
- return typeof input === 'boolean' || isJsonObject(input);
443
- }
444
439
  function invalid(instancePointer, keyword, message) {
445
440
  return { instancePointer, keyword, message };
446
441
  }
@@ -2,6 +2,7 @@ import { equal } from '@astrale-os/kernel-dsl/value';
2
2
  import { isCanonicalBlobRef } from '../model/blob.js';
3
3
  import { isInstantValue } from '../model/instant.js';
4
4
  import { isValueObject as isJsonObject } from '../model/object.js';
5
+ import { isJsonSchema, schemaList } from '../syntax/walk.js';
5
6
  import { codePointLength, isMultipleOf, matchesType, unique } from './assertions.js';
6
7
  /**
7
8
  * Whether a value satisfies its prepared schema, as the evaluator decides it, without building a
@@ -138,14 +139,14 @@ function compileCompositions(schema, child, parts) {
138
139
  return admitted === 1;
139
140
  });
140
141
  }
141
- if (isSchema(schema.not)) {
142
+ if (isJsonSchema(schema.not)) {
142
143
  const not = child('not');
143
144
  parts.push((value, depth) => !not(value, depth + 1));
144
145
  }
145
- if (isSchema(schema.if)) {
146
+ if (isJsonSchema(schema.if)) {
146
147
  const condition = child('if');
147
- const then = isSchema(schema.then) ? child('then') : undefined;
148
- const otherwise = isSchema(schema.else) ? child('else') : undefined;
148
+ const then = isJsonSchema(schema.then) ? child('then') : undefined;
149
+ const otherwise = isJsonSchema(schema.else) ? child('else') : undefined;
149
150
  parts.push((value, depth) => {
150
151
  const branch = condition(value, depth + 1) ? then : otherwise;
151
152
  return branch === undefined || branch(value, depth + 1);
@@ -226,7 +227,7 @@ function compileArray(schema, child, parts) {
226
227
  (!distinct || unique(value))));
227
228
  }
228
229
  const prefix = schemaList(schema.prefixItems)?.map((_, index) => child('prefixItems', index));
229
- const items = isSchema(schema.items) ? child('items') : undefined;
230
+ const items = isJsonSchema(schema.items) ? child('items') : undefined;
230
231
  if (prefix !== undefined || items !== undefined) {
231
232
  const leading = prefix ?? [];
232
233
  parts.push((value, depth) => {
@@ -240,7 +241,7 @@ function compileArray(schema, child, parts) {
240
241
  return true;
241
242
  });
242
243
  }
243
- if (isSchema(schema.contains)) {
244
+ if (isJsonSchema(schema.contains)) {
244
245
  const contains = child('contains');
245
246
  const least = typeof schema.minContains === 'number' ? schema.minContains : 1;
246
247
  const most = typeof schema.maxContains === 'number' ? schema.maxContains : Number.POSITIVE_INFINITY;
@@ -297,10 +298,10 @@ function compileObject(compiled, schema, child, parts) {
297
298
  const patterns = isJsonObject(schema.patternProperties)
298
299
  ? Object.keys(schema.patternProperties).map((source) => [compiled.matcher(source), child('patternProperties', source)])
299
300
  : [];
300
- const additional = isSchema(schema.additionalProperties)
301
+ const additional = isJsonSchema(schema.additionalProperties)
301
302
  ? child('additionalProperties')
302
303
  : undefined;
303
- const names = isSchema(schema.propertyNames) ? child('propertyNames') : undefined;
304
+ const names = isJsonSchema(schema.propertyNames) ? child('propertyNames') : undefined;
304
305
  if (properties.size > 0 || patterns.length > 0 || additional !== undefined || names) {
305
306
  parts.push((value, depth) => {
306
307
  if (!isJsonObject(value))
@@ -335,9 +336,3 @@ function compileObject(compiled, schema, child, parts) {
335
336
  dependent.every(([name, check]) => !Object.hasOwn(value, name) || check(value, depth + 1)));
336
337
  }
337
338
  }
338
- function schemaList(input) {
339
- return Array.isArray(input) ? input : undefined;
340
- }
341
- function isSchema(input) {
342
- return typeof input === 'boolean' || isJsonObject(input);
343
- }
@@ -1,6 +1,7 @@
1
1
  import { fileOptionsOf, isFileValueSchema } from './model/file.js';
2
2
  import { isValueObject as isJsonObject } from './model/object.js';
3
3
  import { escapePointerToken, parseJsonPointerFragment, resolvePointer } from './syntax/pointer.js';
4
+ import { isJsonSchema } from './syntax/walk.js';
4
5
  const SAME_VALUE = ['allOf', 'anyOf', 'oneOf'];
5
6
  // `not` admits no value through its subschema, so no file field lies below it.
6
7
  const SAME_VALUE_SINGLE = ['if', 'then', 'else'];
@@ -143,7 +144,7 @@ export function locateFiles(positions, input) {
143
144
  function resolveLocal(root, reference) {
144
145
  const tokens = parseJsonPointerFragment(reference);
145
146
  const target = tokens === undefined ? undefined : resolvePointer(root, tokens);
146
- return typeof target === 'boolean' || isJsonObject(target) ? target : undefined;
147
+ return isJsonSchema(target) ? target : undefined;
147
148
  }
148
149
  function isPlainObject(value) {
149
150
  return value !== null && typeof value === 'object' && !Array.isArray(value);
@@ -10,6 +10,12 @@ export interface BlobRef {
10
10
  export type BlobReceipt = string & {
11
11
  readonly __brand: 'BlobReceipt';
12
12
  };
13
+ /**
14
+ * Whether `input` has the form of an upload receipt, the ordinary write input of a blob property:
15
+ * text, never a Ref. Only the Kernel that issued a receipt admits it; the Ref it names must then
16
+ * satisfy the property's schema.
17
+ */
18
+ export declare function isBlobReceipt(input: unknown): input is BlobReceipt;
13
19
  /** Type-only marker; the serialized schema remains ordinary JSON. */
14
20
  export interface BlobSchema extends JsonSchemaObject {
15
21
  readonly [BLOB_SCHEMA]: true;
@@ -1,3 +1,11 @@
1
+ /**
2
+ * Whether `input` has the form of an upload receipt, the ordinary write input of a blob property:
3
+ * text, never a Ref. Only the Kernel that issued a receipt admits it; the Ref it names must then
4
+ * satisfy the property's schema.
5
+ */
6
+ export function isBlobReceipt(input) {
7
+ return typeof input === 'string';
8
+ }
1
9
  export const BLOB_VALUE_KIND = 'blob';
2
10
  const BLOB_DIGEST_PATTERN = 'sha256:[0-9a-f]{64}';
3
11
  const BLOB_MEDIA_TYPE_PATTERN = "[a-z0-9!#$%&'*+.^_`|~-]+/[a-z0-9!#$%&'*+.^_`|~-]+(;[a-z0-9!#$%&'*+.^_`|~-]+=[!#$%&'*+.^_`|~0-9A-Za-z-]+)*";
@@ -1,6 +1,7 @@
1
1
  import { runSuspended } from '../suspended.js';
2
2
  import { loadValueProgram } from './compile/load.js';
3
3
  import { prepareValueSchema } from './evaluate/preparation.js';
4
+ import { schemaList } from './syntax/walk.js';
4
5
  const ALL_JSON_KINDS = new Set([
5
6
  'null',
6
7
  'boolean',
@@ -234,6 +235,3 @@ function normalize(input) {
234
235
  input.delete('set');
235
236
  return input;
236
237
  }
237
- function schemaList(input) {
238
- return Array.isArray(input) ? input : undefined;
239
- }