@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.
Files changed (106) hide show
  1. package/dist/v1/builder/lowering/policy.js +4 -24
  2. package/dist/v1/builder/lowering/schema/context.js +0 -3
  3. package/dist/v1/builder/lowering/value/compile.js +24 -21
  4. package/dist/v1/builder/lowering/value/zod.js +10 -4
  5. package/dist/v1/builder/schema/diagnostic.d.ts +1 -1
  6. package/dist/v1/bundle/codec.js +50 -67
  7. package/dist/v1/bundle/diagnostic.d.ts +1 -1
  8. package/dist/v1/bundle/structure.js +2 -2
  9. package/dist/v1/compiled/admission/accept.js +6 -6
  10. package/dist/v1/compiled/admission/closure.js +16 -33
  11. package/dist/v1/compiled/admission/diagnostic.d.ts +8 -0
  12. package/dist/v1/compiled/admission/diagnostic.js +33 -0
  13. package/dist/v1/compiled/admission/semantics/class.js +11 -7
  14. package/dist/v1/compiled/admission/semantics/policy/admit.js +4 -19
  15. package/dist/v1/compiled/admission/wire/callable.d.ts +5 -2
  16. package/dist/v1/compiled/admission/wire/callable.js +13 -19
  17. package/dist/v1/compiled/admission/wire/class.js +6 -9
  18. package/dist/v1/compiled/admission/wire/entry.d.ts +1 -1
  19. package/dist/v1/compiled/admission/wire/entry.js +8 -13
  20. package/dist/v1/compiled/admission/wire/policy.js +4 -3
  21. package/dist/v1/compiled/admission/wire/program.d.ts +13 -3
  22. package/dist/v1/compiled/admission/wire/program.js +39 -44
  23. package/dist/v1/compiled/admission/wire/property.d.ts +4 -1
  24. package/dist/v1/compiled/admission/wire/property.js +8 -5
  25. package/dist/v1/compiled/admission/wire/value.d.ts +0 -2
  26. package/dist/v1/compiled/admission/wire/value.js +0 -7
  27. package/dist/v1/compiled/codec/json.js +4 -3
  28. package/dist/v1/compiled/compiler/compile.js +6 -17
  29. package/dist/v1/compiled/diagnostic.d.ts +1 -1
  30. package/dist/v1/compiled/loader/load.js +2 -7
  31. package/dist/v1/domain/model/input.d.ts +2 -2
  32. package/dist/v1/domain/validation/create.d.ts +3 -1
  33. package/dist/v1/domain/validation/create.js +13 -8
  34. package/dist/v1/language/callable/resolved.d.ts +3 -3
  35. package/dist/v1/language/chain.d.ts +7 -0
  36. package/dist/v1/language/chain.js +24 -0
  37. package/dist/v1/language/cycle.d.ts +10 -0
  38. package/dist/v1/language/cycle.js +30 -0
  39. package/dist/v1/language/limits.d.ts +19 -0
  40. package/dist/v1/language/limits.js +17 -0
  41. package/dist/v1/language/policy/analysis/budgets.js +17 -13
  42. package/dist/v1/language/policy/analysis/composition.d.ts +4 -0
  43. package/dist/v1/language/policy/analysis/composition.js +11 -0
  44. package/dist/v1/language/suspended.d.ts +7 -0
  45. package/dist/v1/language/suspended.js +23 -0
  46. package/dist/v1/language/value/compile/fields.js +12 -8
  47. package/dist/v1/language/value/compile/load.d.ts +5 -0
  48. package/dist/v1/language/value/compile/load.js +13 -0
  49. package/dist/v1/language/value/evaluate/assertions.d.ts +5 -1
  50. package/dist/v1/language/value/evaluate/assertions.js +49 -8
  51. package/dist/v1/language/value/evaluate/evaluator.js +107 -105
  52. package/dist/v1/language/value/evaluate/preparation.d.ts +16 -0
  53. package/dist/v1/language/value/evaluate/preparation.js +100 -47
  54. package/dist/v1/language/value/evaluate/program.d.ts +2 -2
  55. package/dist/v1/language/value/evaluate/program.js +5 -5
  56. package/dist/v1/language/value/evaluate/validate.d.ts +3 -3
  57. package/dist/v1/language/value/evaluate/validate.js +9 -9
  58. package/dist/v1/language/value/evaluate/validity.optimization.js +4 -3
  59. package/dist/v1/language/value/inclusion/equivalence.js +4 -2
  60. package/dist/v1/language/value/inclusion/evidence.js +2 -2
  61. package/dist/v1/language/value/inclusion/includes.d.ts +13 -4
  62. package/dist/v1/language/value/inclusion/includes.js +34 -5
  63. package/dist/v1/language/value/inclusion/location.js +10 -7
  64. package/dist/v1/language/value/ordering.js +62 -70
  65. package/dist/v1/language/value/profile/canonicalize.d.ts +6 -1
  66. package/dist/v1/language/value/profile/canonicalize.js +64 -83
  67. package/dist/v1/language/value/profile/json.validate.d.ts +13 -3
  68. package/dist/v1/language/value/profile/json.validate.js +18 -6
  69. package/dist/v1/language/value/reference/closure.js +3 -2
  70. package/dist/v1/language/value/reference/recursion.js +7 -26
  71. package/dist/v1/language/value/reference/references.d.ts +3 -1
  72. package/dist/v1/language/value/reference/references.js +7 -7
  73. package/dist/v1/language/value/syntax/walk.d.ts +5 -0
  74. package/dist/v1/language/value/syntax/walk.js +14 -0
  75. package/dist/v1/schema/.spec/schemas/domain-schema.schema.json +1 -1
  76. package/dist/v1/schema/admission/accept.d.ts +3 -1
  77. package/dist/v1/schema/admission/accept.js +4 -1
  78. package/dist/v1/schema/analysis/class.d.ts +4 -0
  79. package/dist/v1/schema/analysis/class.js +24 -2
  80. package/dist/v1/schema/canonical/revision.d.ts +4 -1
  81. package/dist/v1/schema/canonical/revision.js +4 -1
  82. package/dist/v1/schema/compatibility/context.d.ts +5 -2
  83. package/dist/v1/schema/compatibility/context.js +8 -6
  84. package/dist/v1/schema/compatibility/model.d.ts +1 -1
  85. package/dist/v1/schema/compatibility/structure/requirements.js +2 -2
  86. package/dist/v1/schema/compatibility/walk.js +2 -2
  87. package/dist/v1/schema/diagnostic.d.ts +1 -1
  88. package/dist/v1/schema/normalization/domain.js +8 -4
  89. package/dist/v1/schema/resolution/context.js +6 -7
  90. package/dist/v1/schema/resolution/domain.js +3 -11
  91. package/dist/v1/schema/rule.js +10 -3
  92. package/dist/v1/schema/validation/class.js +1 -1
  93. package/dist/v1/schema/validation/core.js +16 -8
  94. package/dist/v1/schema/validation/domain.js +20 -3
  95. package/dist/v1/schema/validation/policy/budgets.d.ts +5 -1
  96. package/dist/v1/schema/validation/policy/budgets.js +8 -1
  97. package/dist/v1/schema/validation/policy/references.js +5 -25
  98. package/dist/value/accept.d.ts +3 -3
  99. package/dist/value/canonical.js +2 -2
  100. package/dist/value/equal.d.ts +9 -1
  101. package/dist/value/equal.js +37 -3
  102. package/dist/value/index.d.ts +1 -1
  103. package/dist/value/limits.d.ts +11 -0
  104. package/package.json +2 -2
  105. package/dist/v1/compiled/admission/wire/pattern.d.ts +0 -12
  106. package/dist/v1/compiled/admission/wire/pattern.js +0 -111
@@ -1,3 +1,4 @@
1
+ import { compositionCycles } from '../../language/policy/analysis/composition.js';
1
2
  import { normalizePolicyExpression } from '../../language/policy/normalization/normalize.js';
2
3
  import { callableRef, callableSelf } from '../callable/authoring.js';
3
4
  import { createPatternContext, policyDeclaration } from '../policy/authoring.js';
@@ -36,30 +37,9 @@ export class V1PolicyCompiler {
36
37
  }
37
38
  }
38
39
  export function assertPolicyCompositionAcyclic(declarations, origin) {
39
- const visiting = new Set();
40
- const visited = new Set();
41
- const visit = (name) => {
42
- if (visiting.has(name)) {
43
- fail('BUILDER_POLICY_CYCLE', `/policies/${name}`, `Policy composition cycle reaches ${origin}:policy.${name}.`);
44
- }
45
- if (visited.has(name))
46
- return;
47
- visiting.add(name);
48
- const declaration = declarations[name];
49
- const expression = declaration?.expression;
50
- const refs = expression !== undefined && 'allOf' in expression
51
- ? expression.allOf
52
- : expression !== undefined && 'anyOf' in expression
53
- ? expression.anyOf
54
- : [];
55
- for (const ref of refs)
56
- if (ref.origin === origin)
57
- visit(ref.name);
58
- visiting.delete(name);
59
- visited.add(name);
60
- };
61
- for (const name of Object.keys(declarations))
62
- visit(name);
40
+ const [cycle] = compositionCycles(Object.keys(declarations), (name) => declarations[name]?.expression, origin);
41
+ if (cycle !== undefined)
42
+ fail('BUILDER_POLICY_CYCLE', `/policies/${cycle.target}`, `Policy composition cycle reaches ${origin}:policy.${cycle.target}.`);
63
43
  }
64
44
  /**
65
45
  * The check vocabulary of every rule site: an authorized callable's Policy and a Node Class rule.
@@ -46,9 +46,6 @@ export class BuilderContext {
46
46
  }
47
47
  captureDefinition(value, ref, pointer) {
48
48
  const key = definitionRefIndex(ref);
49
- if (this.localDefinitions.has(key)) {
50
- fail('BUILDER_DUPLICATE_MEMBER', pointer, `Definition ${ref.kind}.${ref.name} is duplicated.`);
51
- }
52
49
  this.localDefinitions.set(key, ref);
53
50
  this.localClassKinds.set(key, value.kind);
54
51
  this.#localClasses.set(key, value);
@@ -24,7 +24,7 @@ export class BuilderValueSchemas {
24
24
  }
25
25
  properties(input, pointer) {
26
26
  const fields = input ?? {};
27
- const compiled = {};
27
+ const compiled = [];
28
28
  for (const [name, value] of Object.entries(fields)) {
29
29
  const auto = isAutoProperty(value) ? value : undefined;
30
30
  const autoInput = auto === undefined ? undefined : autoPropertyAuthoringInput(auto);
@@ -34,27 +34,30 @@ export class BuilderValueSchemas {
34
34
  const schemaInput = authoredInput === undefined ? value : authoredInput.schema;
35
35
  const schema = this.lower(schemaInput, 'callable-output', `${propertyPointer}/schema`, 'object-property');
36
36
  this.assertValid(schema, 'property-facet', `${propertyPointer}/schema`);
37
- compiled[name] = {
38
- schema: canonicalizeValueSchema(schema, 'property-facet', this.registry),
39
- required: authoredInput?.options.required ?? isRequired(schemaInput),
40
- ...(autoInput === undefined ? {} : { auto: autoInput.options }),
41
- ...(authoredInput?.options.immutable === undefined
42
- ? {}
43
- : { immutable: authoredInput.options.immutable }),
44
- ...(authoredInput?.options.description === undefined
45
- ? {}
46
- : { description: authoredInput.options.description }),
47
- ...(authoredInput?.machine === undefined
48
- ? {}
49
- : {
50
- state: {
51
- initial: authoredInput.machine.initial,
52
- transitions: authoredInput.machine.transitions,
53
- },
54
- }),
55
- };
37
+ compiled.push([
38
+ name,
39
+ {
40
+ schema: canonicalizeValueSchema(schema, 'property-facet', this.registry),
41
+ required: authoredInput?.options.required ?? isRequired(schemaInput),
42
+ ...(autoInput === undefined ? {} : { auto: autoInput.options }),
43
+ ...(authoredInput?.options.immutable === undefined
44
+ ? {}
45
+ : { immutable: authoredInput.options.immutable }),
46
+ ...(authoredInput?.options.description === undefined
47
+ ? {}
48
+ : { description: authoredInput.options.description }),
49
+ ...(authoredInput?.machine === undefined
50
+ ? {}
51
+ : {
52
+ state: {
53
+ initial: authoredInput.machine.initial,
54
+ transitions: authoredInput.machine.transitions,
55
+ },
56
+ }),
57
+ },
58
+ ]);
56
59
  }
57
- return compiled;
60
+ return Object.fromEntries(compiled);
58
61
  }
59
62
  lower(input, conversionContext, pointer, zodPosition = 'value') {
60
63
  let schema;
@@ -5,7 +5,7 @@ import { validateValueSchema } from '../../../language/value/profile/validate.js
5
5
  import { isIRegexp } from '../../../language/value/regexp/syntax.js';
6
6
  import { fullMatchSource } from '../../../language/value/syntax/full-match.js';
7
7
  import { escapePointerToken } from '../../../language/value/syntax/pointer.js';
8
- import { diagnostic } from '../../schema/diagnostic.js';
8
+ import { BuilderError, diagnostic } from '../../schema/diagnostic.js';
9
9
  /**
10
10
  * Converts only Zod contracts whose JSON-value acceptance is represented
11
11
  * without coercion, effects, custom predicates, or format assertions.
@@ -45,6 +45,9 @@ export function convertZodToValueSchema(zodSchema, context, registry, pointer =
45
45
  }
46
46
  }
47
47
  catch (error) {
48
+ // Zod runs the override inside its conversion, so a refusal the override raises passes through here.
49
+ if (error instanceof BuilderError)
50
+ return { ok: false, diagnostics: error.diagnostics };
48
51
  return {
49
52
  ok: false,
50
53
  diagnostics: [
@@ -93,7 +96,7 @@ function copyZodJson(value) {
93
96
  if (value === null || typeof value !== 'object') {
94
97
  throw new TypeError('Zod emitted a non-JSON schema value.');
95
98
  }
96
- const result = {};
99
+ const members = [];
97
100
  for (const key of Reflect.ownKeys(value)) {
98
101
  if (typeof key === 'symbol')
99
102
  throw new TypeError('Zod emitted symbol-keyed schema metadata.');
@@ -109,9 +112,9 @@ function copyZodJson(value) {
109
112
  }
110
113
  if (!('value' in descriptor))
111
114
  throw new TypeError(`Zod emitted accessor schema member ${key}.`);
112
- result[key] = copyZodJson(descriptor.value);
115
+ members.push([key, copyZodJson(descriptor.value)]);
113
116
  }
114
- return result;
117
+ return Object.fromEntries(members);
115
118
  }
116
119
  /** Whether explicit Zod optionality makes a containing object property absent-able. */
117
120
  export function zodObjectPropertyIsOptional(schema) {
@@ -142,6 +145,9 @@ function inspectZodSchema(root, pointer, position) {
142
145
  if (def.coerce === true) {
143
146
  diagnostics.push(diagnostic('ZOD_SCHEMA_NOT_LOSSLESS', schemaPointer, 'Zod coercion changes the accepted input domain and is not serializable as JSON Schema.'));
144
147
  }
148
+ if (type === 'object' && Object.hasOwn(Object(def.shape), '__proto__')) {
149
+ diagnostics.push(diagnostic('ZOD_SCHEMA_NOT_LOSSLESS', `${schemaPointer}/properties/__proto__`, 'Zod renders no member named __proto__, while its object still requires that member.'));
150
+ }
145
151
  if (type === 'record' && isZodType(def.keyType)) {
146
152
  if (getSchemaDefinition(def.keyType)?.type === 'number') {
147
153
  diagnostics.push(diagnostic('ZOD_SCHEMA_NOT_LOSSLESS', `${schemaPointer}/propertyNames`, 'Zod number record keys accept the member names that spell a number, which V1 states by no portable pattern.'));
@@ -1,7 +1,7 @@
1
1
  import type { DiagnosticCode as LanguageDiagnosticCode } from '../../language/validation/diagnostic.js';
2
2
  import { escapePointerToken as escapeToken } from '../../language/value/syntax/pointer.js';
3
3
  export { escapeToken };
4
- export type BuilderDiagnosticCode = LanguageDiagnosticCode | 'BUILDER_ABSTRACT_POLICY' | 'BUILDER_AMBIGUOUS_OWNERSHIP' | 'BUILDER_AUTO_EVENT_INVALID' | 'BUILDER_AUTO_IMMUTABLE' | 'BUILDER_AUTO_NESTED' | 'BUILDER_AUTO_SET_INVALID' | 'BUILDER_CALLABLE_AUTH' | 'BUILDER_CALLABLE_PLACEMENT' | 'BUILDER_CALLABLE_POLICY_AUTH' | 'BUILDER_CLASS_INHERITANCE' | 'BUILDER_CORE_CLASS' | 'BUILDER_CORE_EDGE' | 'BUILDER_CORE_NODE' | 'BUILDER_CORE_PROPERTY' | 'BUILDER_CORE_REF' | 'BUILDER_DEFINITION' | 'BUILDER_DEPENDENCY_CONFLICT' | 'BUILDER_DEPENDENCY_ALIAS' | 'BUILDER_DEPENDENCY_DIRECT' | 'BUILDER_DUPLICATE_MEMBER' | 'BUILDER_EDGE_LIFETIME' | 'BUILDER_FILE_INVALID' | 'BUILDER_FOREIGN_POLICY' | 'BUILDER_FULL_MATCH_INVALID' | 'BUILDER_FUNCTION' | 'BUILDER_METHOD' | 'BUILDER_PATH_DUPLICATE' | 'BUILDER_PATH_EMPTY' | 'BUILDER_POLICY' | 'BUILDER_POLICY_COMPOSITION' | 'BUILDER_POLICY_CYCLE' | 'BUILDER_POLICY_EMPTY_COMPOSITION' | 'BUILDER_POLICY_EMPTY_EXISTS' | 'BUILDER_POLICY_INPUT' | 'BUILDER_POLICY_REF' | 'BUILDER_POLICY_SELF' | 'BUILDER_PROPERTY_IMMUTABLE_INVALID' | 'BUILDER_REFERENCE_CYCLE' | 'BUILDER_REFERENCE_KIND' | 'BUILDER_REFERENCE_MISSING' | 'BUILDER_UNIQUE_PROPERTY' | 'BUILDER_UNKNOWN_FIELD' | 'BUILDER_UNOWNED_REFERENCE' | 'BUILDER_VIEW' | 'ZOD_SCHEMA_NOT_LOSSLESS';
4
+ export type BuilderDiagnosticCode = LanguageDiagnosticCode | 'BUILDER_ABSTRACT_POLICY' | 'BUILDER_AMBIGUOUS_OWNERSHIP' | 'BUILDER_AUTO_EVENT_INVALID' | 'BUILDER_AUTO_IMMUTABLE' | 'BUILDER_AUTO_NESTED' | 'BUILDER_AUTO_SET_INVALID' | 'BUILDER_CALLABLE_AUTH' | 'BUILDER_CALLABLE_PLACEMENT' | 'BUILDER_CALLABLE_POLICY_AUTH' | 'BUILDER_CLASS_INHERITANCE' | 'BUILDER_CORE_CLASS' | 'BUILDER_CORE_EDGE' | 'BUILDER_CORE_NODE' | 'BUILDER_CORE_PROPERTY' | 'BUILDER_CORE_REF' | 'BUILDER_DEFINITION' | 'BUILDER_DEPENDENCY_CONFLICT' | 'BUILDER_DEPENDENCY_ALIAS' | 'BUILDER_DEPENDENCY_DIRECT' | 'BUILDER_EDGE_LIFETIME' | 'BUILDER_FILE_INVALID' | 'BUILDER_FOREIGN_POLICY' | 'BUILDER_FULL_MATCH_INVALID' | 'BUILDER_FUNCTION' | 'BUILDER_METHOD' | 'BUILDER_PATH_DUPLICATE' | 'BUILDER_PATH_EMPTY' | 'BUILDER_POLICY' | 'BUILDER_POLICY_COMPOSITION' | 'BUILDER_POLICY_CYCLE' | 'BUILDER_POLICY_EMPTY_COMPOSITION' | 'BUILDER_POLICY_EMPTY_EXISTS' | 'BUILDER_POLICY_INPUT' | 'BUILDER_POLICY_REF' | 'BUILDER_POLICY_SELF' | 'BUILDER_PROPERTY_IMMUTABLE_INVALID' | 'BUILDER_REFERENCE_CYCLE' | 'BUILDER_REFERENCE_KIND' | 'BUILDER_REFERENCE_MISSING' | 'BUILDER_UNIQUE_PROPERTY' | 'BUILDER_UNKNOWN_FIELD' | 'BUILDER_UNOWNED_REFERENCE' | 'BUILDER_VIEW' | 'ZOD_SCHEMA_NOT_LOSSLESS';
5
5
  export interface BuilderDiagnostic<out Code extends BuilderDiagnosticCode = BuilderDiagnosticCode> {
6
6
  readonly code: Code;
7
7
  readonly pointer: string;
@@ -1,8 +1,8 @@
1
- import { accept as acceptJsonValue, BoundaryError as JsonBoundaryError, json, } from '@astrale-os/kernel-dsl/value';
1
+ import { acceptImmutable, BoundaryError as JsonBoundaryError, json, } from '@astrale-os/kernel-dsl/value';
2
2
  import { compareUnicode } from '../addressing/ordering.js';
3
- import { limits } from '../language/limits.js';
3
+ import { closureDocument, limits } from '../language/limits.js';
4
4
  import { escapePointerToken } from '../language/value/syntax/pointer.js';
5
- import { acceptDomainSchema } from '../schema/admission/accept.js';
5
+ import { acceptStructuredDomainSchema } from '../schema/admission/accept.js';
6
6
  import { canonicalDomainSchemaAfterValidation } from '../schema/canonical/revision.js';
7
7
  import { revisionOfDomainSchema } from '../schema/canonical/revision.js';
8
8
  import { MemoryDomainSchemaLookup } from '../schema/dependency/lookup.js';
@@ -10,35 +10,32 @@ import { SchemaError } from '../schema/diagnostic.js';
10
10
  import { BundleError, diagnostic, schemaDiagnosticAt } from './diagnostic.js';
11
11
  import { asDomainSchemaBundle, } from './model.js';
12
12
  import { parseDomainSchemaBundleStructure } from './structure.js';
13
- export function acceptDomainSchemaBundle(input) {
14
- let admitted;
13
+ /** Cross the portable value boundary with a document at `at`, whose refusal is answered as one. */
14
+ function bundleDocument(cross, at = '') {
15
15
  try {
16
- admitted = acceptJsonValue(input);
16
+ return cross();
17
17
  }
18
18
  catch (error) {
19
19
  if (error instanceof JsonBoundaryError) {
20
- throw new BundleError([diagnostic('DV_JSON_VALUE', error.pointer, error.message)]);
20
+ throw new BundleError([diagnostic('DV_JSON_VALUE', `${at}${error.pointer}`, error.message)]);
21
21
  }
22
- throw error;
23
- }
24
- return acceptBundleValue(admitted);
25
- }
26
- export function decodeDomainSchemaBundle(input) {
27
- try {
28
- return acceptBundleValue(json.decode(input));
29
- }
30
- catch (error) {
31
22
  if (error instanceof json.DecodeError) {
32
- throw new BundleError([diagnostic('DV_JSON_VALUE', '', error.message)]);
23
+ throw new BundleError([diagnostic('DV_JSON_VALUE', at, error.message)]);
33
24
  }
34
25
  throw error;
35
26
  }
36
27
  }
28
+ export function acceptDomainSchemaBundle(input) {
29
+ return acceptBundleValue(bundleDocument(() => acceptImmutable(input, closureDocument)));
30
+ }
31
+ export function decodeDomainSchemaBundle(input) {
32
+ return acceptBundleValue(bundleDocument(() => json.decode(input, closureDocument)));
33
+ }
37
34
  export function serializeDomainSchemaBundle(bundle) {
38
- return json.serialize(bundle);
35
+ return bundleDocument(() => json.serialize(bundle, closureDocument));
39
36
  }
40
37
  export function encodeDomainSchemaBundle(bundle) {
41
- return json.encode(bundle);
38
+ return bundleDocument(() => json.encode(bundle, closureDocument));
42
39
  }
43
40
  function acceptBundleValue(input) {
44
41
  const structure = parseDomainSchemaBundleStructure(input);
@@ -49,11 +46,6 @@ function acceptBundleValue(input) {
49
46
  if (closure.length + 1 > limits.dependency.closure) {
50
47
  diagnostics.push(diagnostic('DV_DEPENDENCY_LIMITS', '/closure', `A dependency closure may contain at most ${limits.dependency.closure} Domains.`));
51
48
  }
52
- for (const entry of [root, ...closure]) {
53
- if (Object.keys(entry.schema.dependencies).length > limits.dependency.direct) {
54
- diagnostics.push(diagnostic('DV_DEPENDENCY_LIMITS', `${entry.pointer}/dependencies`, `A Domain may declare at most ${limits.dependency.direct} direct dependency aliases.`));
55
- }
56
- }
57
49
  for (const entry of closure) {
58
50
  if (entry.key === root.key) {
59
51
  diagnostics.push(diagnostic('BUNDLE_ROOT_DUPLICATED', entry.pointer, 'The root schema must not also appear in the bundle closure.'));
@@ -66,8 +58,7 @@ function acceptBundleValue(input) {
66
58
  }
67
59
  candidates.set(entry.key, entry);
68
60
  }
69
- const reachable = new Set();
70
- collectReachable(root, root, candidates, reachable, new Set(), new Map(), diagnostics, 0);
61
+ const reachable = collectReachable(root, candidates, diagnostics);
71
62
  for (const entry of candidates.values()) {
72
63
  if (!reachable.has(entry.key)) {
73
64
  diagnostics.push(diagnostic('BUNDLE_CLOSURE_EXTRA', entry.pointer, `Schema ${entry.schema.origin}@${entry.revision} is not reachable from the root.`));
@@ -86,14 +77,8 @@ function acceptBundleValue(input) {
86
77
  acceptCandidate(nested);
87
78
  }
88
79
  try {
89
- const schema = acceptDomainSchema(entry.schema, new MemoryDomainSchemaLookup([...accepted.values()]));
90
- const revision = revisionOfDomainSchema(schema);
91
- if (revision !== entry.revision) {
92
- throw new BundleError([
93
- diagnostic('BUNDLE_SCHEMA_REVISION', '', `Accepted schema revision ${revision} differs from indexed revision ${entry.revision}.`),
94
- ]);
95
- }
96
- accepted.set(entry.key, { schema, revision });
80
+ const schema = acceptStructuredDomainSchema(entry.schema, new MemoryDomainSchemaLookup([...accepted.values()]));
81
+ accepted.set(entry.key, { schema, revision: entry.revision });
97
82
  return schema;
98
83
  }
99
84
  catch (error) {
@@ -114,7 +99,7 @@ function acceptBundleValue(input) {
114
99
  return asDomainSchemaBundle(rootSchema, acceptedClosure);
115
100
  }
116
101
  function candidate(schema, pointer) {
117
- const canonical = canonicalDomainSchemaAfterValidation(schema);
102
+ const canonical = bundleDocument(() => canonicalDomainSchemaAfterValidation(schema), pointer);
118
103
  return {
119
104
  schema,
120
105
  revision: canonical.revision,
@@ -122,40 +107,38 @@ function candidate(schema, pointer) {
122
107
  pointer,
123
108
  };
124
109
  }
125
- function collectReachable(current, root, candidates, reachable, active, revisions, diagnostics, depth) {
126
- if (active.has(current.key)) {
127
- diagnostics.push(diagnostic('BUNDLE_CLOSURE_CYCLE', current.pointer, `Bundle dependency cycle reaches ${current.schema.origin}@${current.revision}.`));
128
- return;
129
- }
130
- active.add(current.key);
131
- if (depth > limits.dependency.depth) {
132
- diagnostics.push(diagnostic('DV_DEPENDENCY_LIMITS', current.pointer, `Dependency depth ${depth} exceeds the maximum ${limits.dependency.depth}.`));
133
- active.delete(current.key);
134
- return;
135
- }
136
- for (const [alias, dependency] of Object.entries(current.schema.dependencies)) {
137
- const pointer = `${current.pointer}/dependencies/${escapePointerToken(alias)}`;
138
- const dependencyKey = keyOf(dependency.origin, dependency.revision);
139
- if (dependencyKey === root.key) {
140
- diagnostics.push(diagnostic('BUNDLE_CLOSURE_CYCLE', pointer, 'Bundle dependency closure returns to its root.'));
141
- continue;
142
- }
143
- const previous = revisions.get(dependency.origin);
144
- if (previous !== undefined && previous !== dependency.revision) {
145
- diagnostics.push(diagnostic('BUNDLE_REVISION_COHERENCE', pointer, `Bundle requires ${dependency.origin} at both ${previous} and ${dependency.revision}.`));
146
- }
147
- else {
148
- revisions.set(dependency.origin, dependency.revision);
149
- }
150
- const nested = candidates.get(dependencyKey);
151
- if (nested === undefined) {
152
- diagnostics.push(diagnostic('BUNDLE_CLOSURE_MISSING', pointer, `Bundle has no exact ${dependency.origin}@${dependency.revision}.`));
153
- continue;
110
+ /**
111
+ * The keys of the Domains `root` reaches, with a refusal at each dependency the Bundle cannot serve.
112
+ * Each Domain is read once, however many chains share it: `pending` grows as the loop reads it. A
113
+ * revision covers its pins, so a dependency cannot name the root except as a missing one, and each
114
+ * Domain's admission below holds the graph to its direct and depth limits.
115
+ */
116
+ function collectReachable(root, candidates, diagnostics) {
117
+ const reachable = new Set();
118
+ const revisions = new Map();
119
+ const pending = [root];
120
+ for (const current of pending) {
121
+ for (const [alias, dependency] of Object.entries(current.schema.dependencies)) {
122
+ const pointer = `${current.pointer}/dependencies/${escapePointerToken(alias)}`;
123
+ const previous = revisions.get(dependency.origin);
124
+ if (previous !== undefined && previous !== dependency.revision) {
125
+ diagnostics.push(diagnostic('BUNDLE_REVISION_COHERENCE', pointer, `Bundle requires ${dependency.origin} at both ${previous} and ${dependency.revision}.`));
126
+ }
127
+ else {
128
+ revisions.set(dependency.origin, dependency.revision);
129
+ }
130
+ const nested = candidates.get(keyOf(dependency.origin, dependency.revision));
131
+ if (nested === undefined) {
132
+ diagnostics.push(diagnostic('BUNDLE_CLOSURE_MISSING', pointer, `Bundle has no exact ${dependency.origin}@${dependency.revision}.`));
133
+ continue;
134
+ }
135
+ if (!reachable.has(nested.key)) {
136
+ reachable.add(nested.key);
137
+ pending.push(nested);
138
+ }
154
139
  }
155
- reachable.add(nested.key);
156
- collectReachable(nested, root, candidates, reachable, active, revisions, diagnostics, depth + 1);
157
140
  }
158
- active.delete(current.key);
141
+ return reachable;
159
142
  }
160
143
  function compareSchemas(left, right) {
161
144
  return (compareUnicode(left.origin, right.origin) ||
@@ -1,5 +1,5 @@
1
1
  import type { SchemaDiagnostic, SchemaDiagnosticCode } from '../schema/diagnostic.js';
2
- export type BundleDiagnosticCode = SchemaDiagnosticCode | 'BUNDLE_CLOSURE_CYCLE' | 'BUNDLE_CLOSURE_EXTRA' | 'BUNDLE_CLOSURE_MISSING' | 'BUNDLE_REVISION_COHERENCE' | 'BUNDLE_ROOT_DUPLICATED' | 'BUNDLE_SCHEMA_DUPLICATED' | 'BUNDLE_SCHEMA_REVISION' | 'BUNDLE_STRUCTURE' | 'UNSUPPORTED_BUNDLE_FORMAT' | 'UNSUPPORTED_BUNDLE_VERSION';
2
+ export type BundleDiagnosticCode = SchemaDiagnosticCode | 'BUNDLE_CLOSURE_EXTRA' | 'BUNDLE_CLOSURE_MISSING' | 'BUNDLE_REVISION_COHERENCE' | 'BUNDLE_ROOT_DUPLICATED' | 'BUNDLE_SCHEMA_DUPLICATED' | 'BUNDLE_STRUCTURE' | 'UNSUPPORTED_BUNDLE_FORMAT' | 'UNSUPPORTED_BUNDLE_VERSION';
3
3
  export type BundleDiagnostic<Code extends BundleDiagnosticCode = BundleDiagnosticCode> = Omit<SchemaDiagnostic, 'code'> & {
4
4
  readonly code: Code;
5
5
  };
@@ -1,6 +1,6 @@
1
1
  import { escapePointerToken } from '../language/value/syntax/pointer.js';
2
2
  import { SchemaError } from '../schema/diagnostic.js';
3
- import { routeDomainSchema } from '../schema/validation/domain.route.js';
3
+ import { structureDomainSchema } from '../schema/validation/domain.route.js';
4
4
  import { BundleError, diagnostic, schemaDiagnosticAt } from './diagnostic.js';
5
5
  import { DOMAIN_BUNDLE_FORMAT, DOMAIN_BUNDLE_VERSION } from './model.js';
6
6
  export function parseDomainSchemaBundleStructure(input) {
@@ -52,7 +52,7 @@ function routeAt(value, pointer, diagnostics) {
52
52
  if (value === undefined)
53
53
  return undefined;
54
54
  try {
55
- return routeDomainSchema(value);
55
+ return structureDomainSchema(value);
56
56
  }
57
57
  catch (error) {
58
58
  if (!(error instanceof SchemaError))
@@ -1,18 +1,18 @@
1
- import { accept as acceptPortable } from '@astrale-os/kernel-dsl/value';
1
+ import { acceptImmutable } from '@astrale-os/kernel-dsl/value';
2
2
  import { deepFreeze } from '../../language/freeze.js';
3
- import { limits } from '../../language/limits.js';
3
+ import { closureDocument, limits } from '../../language/limits.js';
4
4
  import {} from '../model/schema.js';
5
5
  import { validateClosure } from './closure.js';
6
- import { invalid, pointer } from './diagnostic.js';
6
+ import { compiledDocument, invalid, pointer } from './diagnostic.js';
7
7
  import { validateCompiledSemantics } from './semantics.js';
8
8
  import { dependency } from './wire/dependency.js';
9
9
  import { entry } from './wire/entry.js';
10
- import {} from './wire/pattern.js';
10
+ import {} from './wire/program.js';
11
11
  import { object, exact, array } from './wire/value.js';
12
- const MAXIMUM_COMPILED_SCHEMA_MEMBERS = 1_000_000;
13
12
  /** Admit one detached compiled closure before indices and declarations become executable. */
14
13
  export function acceptCompiledSchema(input) {
15
- const root = object(acceptPortable(input, { limits: { maxMembers: MAXIMUM_COMPILED_SCHEMA_MEMBERS } }), 'CompiledSchema', '');
14
+ const document = compiledDocument(() => acceptImmutable(input, closureDocument));
15
+ const root = object(document, 'CompiledSchema', '');
16
16
  exact(root, ['format', 'version', 'root', 'closure'], 'CompiledSchema', '');
17
17
  if (root.format !== 'astrale.dsl.compiled-schema')
18
18
  invalid('format', undefined, '/format');
@@ -1,12 +1,13 @@
1
1
  import {} from '@astrale-os/kernel-dsl/value';
2
2
  import { Key, isDefinitionRef } from '../../addressing/index.js';
3
+ import { chainDepths } from '../../language/chain.js';
3
4
  import {} from '../../language/class/model/edge.js';
4
5
  import { limits } from '../../language/limits.js';
5
6
  import {} from '../model/schema.js';
6
7
  import { invalid, pointer } from './diagnostic.js';
7
8
  /** Require closed direct references, acyclic inheritance and concrete Core Classes. */
8
9
  export function validateClosure(candidate) {
9
- validateDependencyCycles(candidate);
10
+ validateDependencyDepth(candidate);
10
11
  const definitions = new Map();
11
12
  for (const [entryIndex, entry] of candidate.closure.entries()) {
12
13
  const collections = [
@@ -64,6 +65,10 @@ export function validateClosure(candidate) {
64
65
  value: definition,
65
66
  path: `/closure/${entryIndex}/definitions/classes/${classIndex}`,
66
67
  });
68
+ const depths = chainDepths(classes.values(), ({ value }) => value.extends.flatMap((ref) => classes.get(Key.of(ref)) ?? []));
69
+ for (const [{ path }, depth] of depths)
70
+ if (depth > limits.inheritance.depth)
71
+ invalid('Class inheritance depth limit', 'COMPILED_SOURCE_INVALID', `${path}/extends`);
67
72
  const visiting = new Set();
68
73
  const visited = new Set();
69
74
  const visitClass = (key) => {
@@ -122,38 +127,16 @@ export function validateClosure(candidate) {
122
127
  }
123
128
  }
124
129
  }
125
- function validateDependencyCycles(candidate) {
126
- const byOrigin = new Map(candidate.closure.map((entry, index) => [entry.origin, { entry, index }]));
127
- const active = new Set();
128
- const visited = new Set();
129
- const frames = [];
130
- const push = (origin, depth) => {
131
- if (depth > limits.dependency.depth) {
132
- invalid('dependency depth limit', 'COMPILED_DEPENDENCY_LIMITS', `/closure/${byOrigin.get(origin)?.index ?? 0}/dependencies`);
133
- }
134
- active.add(origin);
135
- frames.push({
136
- origin,
137
- depth,
138
- dependencies: Object.values(byOrigin.get(origin)?.entry.dependencies ?? {}),
139
- index: 0,
140
- });
141
- };
142
- push(candidate.root.origin, 0);
143
- while (frames.length > 0) {
144
- const frame = frames.at(-1);
145
- const dependency = frame.dependencies[frame.index++];
146
- if (dependency === undefined) {
147
- frames.pop();
148
- active.delete(frame.origin);
149
- visited.add(frame.origin);
150
- continue;
151
- }
152
- if (active.has(dependency.origin)) {
153
- invalid('dependency cycle', 'COMPILED_DEPENDENCY_CYCLE', `/closure/${byOrigin.get(dependency.origin)?.index ?? 0}/dependencies`);
154
- }
155
- if (!visited.has(dependency.origin))
156
- push(dependency.origin, frame.depth + 1);
130
+ /**
131
+ * Refuse the first Domain whose longest chain of dependencies passes the limit. Admission proved
132
+ * every pin before this, and an entry's revision covers its pins, so the chains cannot close a cycle.
133
+ */
134
+ function validateDependencyDepth(candidate) {
135
+ const byOrigin = new Map(candidate.closure.map((entry) => [entry.origin, entry]));
136
+ const depths = chainDepths(candidate.closure, (entry) => Object.values(entry.dependencies).map(({ origin }) => byOrigin.get(origin)));
137
+ const index = candidate.closure.findIndex((entry) => depths.get(entry) > limits.dependency.depth);
138
+ if (index >= 0) {
139
+ invalid('dependency depth limit', 'COMPILED_DEPENDENCY_LIMITS', `/closure/${index}/dependencies`);
157
140
  }
158
141
  }
159
142
  function walk(input, path, visit) {
@@ -5,3 +5,11 @@ export { appendPointer as pointer } from '../../language/value/syntax/pointer.js
5
5
  export declare function invalid(label: string, code?: CompiledDiagnosticCode, path?: string): never;
6
6
  /** Report a contextual compiled-language violation at its original source pointer. */
7
7
  export declare function fail(pointer: string, message: string): never;
8
+ /** Cross the portable value boundary with a compiled document, whose refusal is answered as one. */
9
+ export declare function compiledDocument<Result>(cross: () => Result): Result;
10
+ /**
11
+ * Encode a document's revision canonically, refusing at `path` a part over the encoder's limits. A
12
+ * document may hold more members than the encoder takes, and has no revision then. What the encoder
13
+ * reports is located in what it encoded, so its pointer stays in the message.
14
+ */
15
+ export declare function withinEncodingLimits<Result>(path: string, encode: () => Result): Result;
@@ -1,3 +1,4 @@
1
+ import { BoundaryError, json } from '@astrale-os/kernel-dsl/value';
1
2
  import { invalidCompiled } from '../diagnostic.js';
2
3
  /** Append one escaped JSON Pointer segment. */
3
4
  export { appendPointer as pointer } from '../../language/value/syntax/pointer.js';
@@ -9,3 +10,35 @@ export function invalid(label, code = 'COMPILED_STRUCTURE_INVALID', path = '') {
9
10
  export function fail(pointer, message) {
10
11
  return invalidCompiled('COMPILED_SOURCE_INVALID', message, pointer);
11
12
  }
13
+ /** Cross the portable value boundary with a compiled document, whose refusal is answered as one. */
14
+ export function compiledDocument(cross) {
15
+ try {
16
+ return cross();
17
+ }
18
+ catch (error) {
19
+ if (error instanceof BoundaryError) {
20
+ return invalidCompiled('COMPILED_STRUCTURE_INVALID', error.message, error.pointer, {
21
+ cause: error,
22
+ });
23
+ }
24
+ if (error instanceof json.DecodeError) {
25
+ return invalidCompiled('COMPILED_STRUCTURE_INVALID', error.message, '', { cause: error });
26
+ }
27
+ throw error;
28
+ }
29
+ }
30
+ /**
31
+ * Encode a document's revision canonically, refusing at `path` a part over the encoder's limits. A
32
+ * document may hold more members than the encoder takes, and has no revision then. What the encoder
33
+ * reports is located in what it encoded, so its pointer stays in the message.
34
+ */
35
+ export function withinEncodingLimits(path, encode) {
36
+ try {
37
+ return encode();
38
+ }
39
+ catch (error) {
40
+ if (!(error instanceof BoundaryError) || error.code !== 'VALUE_LIMIT')
41
+ throw error;
42
+ return invalidCompiled('COMPILED_STRUCTURE_INVALID', error.message, path, { cause: error });
43
+ }
44
+ }
@@ -1,4 +1,4 @@
1
- import { json } from '@astrale-os/kernel-dsl/value';
1
+ import { equal } from '@astrale-os/kernel-dsl/value';
2
2
  import { Key, PropertyKey as PropertyKeyValue, } from '../../../addressing/index.js';
3
3
  import { composeMethods } from '../../../language/callable/composition.js';
4
4
  import { isMethodExecutable } from '../../../language/callable/executable.js';
@@ -75,17 +75,21 @@ export function analyzeClasses(classes) {
75
75
  return analyses;
76
76
  }
77
77
  function sameMethodSignature(leftEntry, left, rightEntry, right) {
78
- return (programKey(leftEntry, left.input) === programKey(rightEntry, right.input) &&
79
- outputKey(leftEntry, left.output) === outputKey(rightEntry, right.output) &&
78
+ return (sameProgram(leftEntry, left.input, rightEntry, right.input) &&
79
+ sameOutput(leftEntry, left.output, rightEntry, right.output) &&
80
80
  left.auth === right.auth &&
81
81
  left.static === right.static &&
82
82
  left.execution?.durable === right.execution?.durable);
83
83
  }
84
- function outputKey(entry, output) {
85
- return output.mode === 'binary' ? 'binary' : `${output.mode}:${programKey(entry, output.value)}`;
84
+ function sameOutput(leftEntry, left, rightEntry, right) {
85
+ if (left.mode !== right.mode)
86
+ return false;
87
+ if (left.mode === 'binary' || right.mode === 'binary')
88
+ return true;
89
+ return sameProgram(leftEntry, left.value, rightEntry, right.value);
86
90
  }
87
- function programKey(entry, value) {
88
- return json.serialize(bindValueProgram(entry.values[value], entry.patterns));
91
+ function sameProgram(leftEntry, left, rightEntry, right) {
92
+ return equal(bindValueProgram(leftEntry.values[left], leftEntry.patterns), bindValueProgram(rightEntry.values[right], rightEntry.patterns));
89
93
  }
90
94
  /** Check automatic Properties, immutability and abstract Class obligations. */
91
95
  export function validateClassFacets(context) {
@@ -1,4 +1,5 @@
1
1
  import { Key } from '../../../../addressing/index.js';
2
+ import { compositionCycles } from '../../../../language/policy/analysis/composition.js';
2
3
  import { isClassRuleChecks } from '../../../../language/policy/model/policy.js';
3
4
  import {} from '../../../model/schema.js';
4
5
  import { fail } from '../../diagnostic.js';
@@ -41,23 +42,7 @@ export function validatePolicies(candidate, context, admitted) {
41
42
  }
42
43
  }
43
44
  function validatePolicyCycles(entry, local, entryIndex) {
44
- const visiting = new Set();
45
- const visited = new Set();
46
- const visit = (name) => {
47
- if (visiting.has(name))
48
- fail(`/closure/${entryIndex}/definitions/policies`, `Named Policy composition cycle reaches ${name}.`);
49
- if (visited.has(name))
50
- return;
51
- visiting.add(name);
52
- const expression = local.get(name)?.expression;
53
- if (expression !== undefined && !('match' in expression)) {
54
- for (const ref of 'allOf' in expression ? expression.allOf : expression.anyOf)
55
- if (ref.origin === entry.origin && ref.kind === 'policy')
56
- visit(ref.name);
57
- }
58
- visiting.delete(name);
59
- visited.add(name);
60
- };
61
- for (const name of local.keys())
62
- visit(name);
45
+ const [cycle] = compositionCycles(local.keys(), (name) => local.get(name)?.expression, entry.origin);
46
+ if (cycle !== undefined)
47
+ fail(`/closure/${entryIndex}/definitions/policies`, `Named Policy composition cycle reaches ${cycle.target}.`);
63
48
  }
@@ -2,5 +2,8 @@ import { type Value as JsonValue, type Object as JsonObject } from '@astrale-os/
2
2
  import { type ValueProgram, type CompiledFunction } from '../../model/schema.js';
3
3
  /** Decode one Function declaration and its callable contract. */
4
4
  export declare function compiledFunction(input: JsonValue, values: readonly ValueProgram[], path: string): CompiledFunction;
5
- /** Check the shared Function and Method wire grammar. */
6
- export declare function compiledCallable(input: JsonValue | JsonObject, values: readonly ValueProgram[], method: boolean, path: string): void;
5
+ /**
6
+ * Check the shared Function and Method wire grammar and return the Callable with the fields its input
7
+ * derives. The revision leaves fields out, so the received ones are not read.
8
+ */
9
+ export declare function compiledCallable(input: JsonValue, values: readonly ValueProgram[], method: boolean, path: string): JsonObject;