@astram/core 0.1.0 → 1.1.0

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 (142) hide show
  1. package/dist/admin/admin-service.d.ts +1 -1
  2. package/dist/admin/command/grants.js +3 -3
  3. package/dist/admin/command/grants.js.map +1 -1
  4. package/dist/admin/command/invitations.d.ts +2 -2
  5. package/dist/admin/command/invitations.js +8 -8
  6. package/dist/admin/command/invitations.js.map +1 -1
  7. package/dist/admin/command/service-accounts.d.ts +1 -1
  8. package/dist/admin/command/service-accounts.js +1 -1
  9. package/dist/admin/query/simulate.d.ts.map +1 -1
  10. package/dist/admin/query/simulate.js +4 -0
  11. package/dist/admin/query/simulate.js.map +1 -1
  12. package/dist/admin/safeguards-impl.d.ts +2 -2
  13. package/dist/admin/safeguards-impl.js +9 -9
  14. package/dist/admin/safeguards-impl.js.map +1 -1
  15. package/dist/admin/safeguards.d.ts +4 -4
  16. package/dist/admin/safeguards.js +4 -4
  17. package/dist/admin/types.d.ts +1 -1
  18. package/dist/admin/types.d.ts.map +1 -1
  19. package/dist/authentication/authenticator.d.ts +2 -2
  20. package/dist/authentication/authenticator.js +2 -2
  21. package/dist/authentication/authenticator.js.map +1 -1
  22. package/dist/authentication/credential-resolver.d.ts +1 -1
  23. package/dist/authorization/cross-tenant/grant-evaluator.d.ts +2 -2
  24. package/dist/authorization/cross-tenant/grant-evaluator.js +2 -2
  25. package/dist/authorization/decision-authority.d.ts +2 -2
  26. package/dist/authorization/decision-authority.d.ts.map +1 -1
  27. package/dist/authorization/decision-authority.js +11 -3
  28. package/dist/authorization/decision-authority.js.map +1 -1
  29. package/dist/authorization/errors.d.ts +37 -0
  30. package/dist/authorization/errors.d.ts.map +1 -1
  31. package/dist/authorization/errors.js +43 -0
  32. package/dist/authorization/errors.js.map +1 -1
  33. package/dist/authorization/gates/object-type.d.ts +2 -2
  34. package/dist/authorization/gates/object-type.d.ts.map +1 -1
  35. package/dist/authorization/gates/object-type.js +20 -7
  36. package/dist/authorization/gates/object-type.js.map +1 -1
  37. package/dist/authorization/gates/tenant.d.ts +3 -3
  38. package/dist/authorization/gates/tenant.js +7 -7
  39. package/dist/authorization/gates/tenant.js.map +1 -1
  40. package/dist/authorization/index.d.ts +33 -4
  41. package/dist/authorization/index.d.ts.map +1 -1
  42. package/dist/authorization/index.js +39 -4
  43. package/dist/authorization/index.js.map +1 -1
  44. package/dist/authorization/ports.d.ts +3 -3
  45. package/dist/authorization/reason-codes.d.ts +7 -1
  46. package/dist/authorization/reason-codes.d.ts.map +1 -1
  47. package/dist/authorization/reason-codes.js +9 -1
  48. package/dist/authorization/reason-codes.js.map +1 -1
  49. package/dist/authorization/record/owner-baseline.d.ts +43 -0
  50. package/dist/authorization/record/owner-baseline.d.ts.map +1 -0
  51. package/dist/authorization/record/owner-baseline.js +72 -0
  52. package/dist/authorization/record/owner-baseline.js.map +1 -0
  53. package/dist/authorization/record/permit-tier.d.ts.map +1 -1
  54. package/dist/authorization/record/permit-tier.js +16 -5
  55. package/dist/authorization/record/permit-tier.js.map +1 -1
  56. package/dist/identity/broker.d.ts +8 -8
  57. package/dist/identity/broker.d.ts.map +1 -1
  58. package/dist/identity/broker.js +8 -8
  59. package/dist/identity/broker.js.map +1 -1
  60. package/dist/identity/claim-mapper.d.ts +8 -8
  61. package/dist/identity/claim-mapper.d.ts.map +1 -1
  62. package/dist/identity/claim-mapper.js +4 -4
  63. package/dist/identity/index.d.ts +2 -2
  64. package/dist/identity/index.d.ts.map +1 -1
  65. package/dist/identity/internal-adapter.d.ts +12 -12
  66. package/dist/identity/internal-adapter.d.ts.map +1 -1
  67. package/dist/identity/internal-adapter.js +9 -9
  68. package/dist/identity/internal-adapter.js.map +1 -1
  69. package/dist/metadata/index.d.ts +43 -0
  70. package/dist/metadata/index.d.ts.map +1 -1
  71. package/dist/metadata/index.js +6 -0
  72. package/dist/metadata/index.js.map +1 -1
  73. package/dist/metadata/registry.d.ts.map +1 -1
  74. package/dist/metadata/registry.js +2 -1
  75. package/dist/metadata/registry.js.map +1 -1
  76. package/dist-cjs/admin/admin-service.js +159 -0
  77. package/dist-cjs/admin/command/grants.js +200 -0
  78. package/dist-cjs/admin/command/index.js +38 -0
  79. package/dist-cjs/admin/command/invitations.js +246 -0
  80. package/dist-cjs/admin/command/permissions.js +69 -0
  81. package/dist-cjs/admin/command/pipeline.js +110 -0
  82. package/dist-cjs/admin/command/policies.js +223 -0
  83. package/dist-cjs/admin/command/service-accounts.js +278 -0
  84. package/dist-cjs/admin/command/service.js +130 -0
  85. package/dist-cjs/admin/command/tiers.js +81 -0
  86. package/dist-cjs/admin/index.js +78 -0
  87. package/dist-cjs/admin/ports.js +41 -0
  88. package/dist-cjs/admin/query/audit-view.js +88 -0
  89. package/dist-cjs/admin/query/catalog.js +174 -0
  90. package/dist-cjs/admin/query/explain.js +149 -0
  91. package/dist-cjs/admin/query/index.js +74 -0
  92. package/dist-cjs/admin/query/simulate.js +98 -0
  93. package/dist-cjs/admin/safeguards-impl.js +322 -0
  94. package/dist-cjs/admin/safeguards.js +95 -0
  95. package/dist-cjs/admin/types.js +40 -0
  96. package/dist-cjs/audit/audit-recorder.js +269 -0
  97. package/dist-cjs/audit/canonicalize.js +176 -0
  98. package/dist-cjs/audit/errors.js +44 -0
  99. package/dist-cjs/audit/index.js +39 -0
  100. package/dist-cjs/audit/ports.js +27 -0
  101. package/dist-cjs/audit/sensitive.js +162 -0
  102. package/dist-cjs/audit/walk.js +123 -0
  103. package/dist-cjs/authentication/authenticator.js +210 -0
  104. package/dist-cjs/authentication/credential-resolver.js +21 -0
  105. package/dist-cjs/authentication/index.js +42 -0
  106. package/dist-cjs/authentication/password-hasher.js +21 -0
  107. package/dist-cjs/authentication/totp.js +155 -0
  108. package/dist-cjs/authorization/cross-tenant/grant-evaluator.js +167 -0
  109. package/dist-cjs/authorization/decision-authority.js +213 -0
  110. package/dist-cjs/authorization/default-gates.js +53 -0
  111. package/dist-cjs/authorization/delegation/operation-identity.js +226 -0
  112. package/dist-cjs/authorization/errors.js +110 -0
  113. package/dist-cjs/authorization/gates/object-type.js +192 -0
  114. package/dist-cjs/authorization/gates/tenant.js +89 -0
  115. package/dist-cjs/authorization/gates/wiring.js +59 -0
  116. package/dist-cjs/authorization/index.js +134 -0
  117. package/dist-cjs/authorization/ports.js +36 -0
  118. package/dist-cjs/authorization/reason-codes.js +117 -0
  119. package/dist-cjs/authorization/record/owner-baseline.js +74 -0
  120. package/dist-cjs/authorization/record/permit-tier.js +92 -0
  121. package/dist-cjs/authorization/record/rule-evaluator.js +128 -0
  122. package/dist-cjs/authorization/record/shares.js +110 -0
  123. package/dist-cjs/identity/broker.js +139 -0
  124. package/dist-cjs/identity/claim-mapper.js +51 -0
  125. package/dist-cjs/identity/index.js +39 -0
  126. package/dist-cjs/identity/internal-adapter.js +91 -0
  127. package/dist-cjs/index.js +63 -0
  128. package/dist-cjs/metadata/completeness.js +108 -0
  129. package/dist-cjs/metadata/dependencies.js +110 -0
  130. package/dist-cjs/metadata/index.js +76 -0
  131. package/dist-cjs/metadata/registry.js +207 -0
  132. package/dist-cjs/package.json +3 -0
  133. package/dist-cjs/risk/evaluator.js +63 -0
  134. package/dist-cjs/risk/index.js +35 -0
  135. package/dist-cjs/risk/risk-engine.js +290 -0
  136. package/dist-cjs/risk/signals.js +106 -0
  137. package/dist-cjs/risk/thresholds.js +63 -0
  138. package/dist-cjs/session/index.js +35 -0
  139. package/dist-cjs/session/lifetimes.js +70 -0
  140. package/dist-cjs/session/ports.js +23 -0
  141. package/dist-cjs/session/session-authority.js +398 -0
  142. package/package.json +13 -10
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ /**
3
+ * **Supporting read-only dependency auto-derivation** (ADR-0012 §"Supporting
4
+ * read-only lookups", ADR-0010 stable identity, ADR-0004 tiers).
5
+ *
6
+ * ADR-0012's decision: *"Supporting read-only lookups (e.g. the department/role
7
+ * lists a create-user form needs) are auto-derived from object reference fields
8
+ * and granted read-only with the owning permission; a sensitive lookup can be
9
+ * marked 'requires its own permission.'"*
10
+ *
11
+ * This module turns that decision into PURE functions over metadata. Given an
12
+ * object type's reference fields ({@link FieldDescriptor.referencesTypeKey}), it
13
+ * derives the READ-ONLY lookups those fields imply — the referenced object types
14
+ * each need a read-only list so a form can populate its dropdowns. A reference
15
+ * whose {@link SupportingDependency.neverAutoGrant} is `true` (a sensitive
16
+ * lookup, ADR-0012 escape hatch) is EXCLUDED — it requires its own explicit
17
+ * permission and is never auto-granted.
18
+ *
19
+ * PURE (ADR-0003, framework-agnostic): no I/O, no registry mutation, no imports
20
+ * of engine/registry code. To validate that a referenced type actually exists,
21
+ * the caller injects a resolver (dependency injection) rather than this module
22
+ * reaching into the registry — keeping it decoupled from Team A and trivially
23
+ * testable.
24
+ *
25
+ * SCOPE: derivation only. This computes WHICH read-only lookups a permission
26
+ * implies from the declared reference fields. It makes NO access decision — that
27
+ * is the Authorization step's `DecisionAuthority.decide`, not here.
28
+ */
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ exports.SUPPORTING_READ_CAPABILITY = void 0;
31
+ exports.collectNeverAutoGrantTypeKeys = collectNeverAutoGrantTypeKeys;
32
+ exports.collectReferencedTypeKeys = collectReferencedTypeKeys;
33
+ exports.deriveSupportingDependencies = deriveSupportingDependencies;
34
+ /**
35
+ * The read-only capability auto-derived lookups confer. Single-source constant so
36
+ * every derived dependency is stamped read-only from one place (ADR-0012).
37
+ */
38
+ exports.SUPPORTING_READ_CAPABILITY = 'read';
39
+ /**
40
+ * The set of `typeKey`s marked sensitive (`neverAutoGrant: true`) among the
41
+ * action's declared dependencies. PURE helper — exported for reuse/testing.
42
+ */
43
+ function collectNeverAutoGrantTypeKeys(declaredDependencies) {
44
+ const sensitive = new Set();
45
+ for (const dep of declaredDependencies ?? []) {
46
+ if (dep.neverAutoGrant) {
47
+ sensitive.add(dep.typeKey);
48
+ }
49
+ }
50
+ return sensitive;
51
+ }
52
+ /**
53
+ * Extract the referenced type keys from an object type's reference fields, in
54
+ * declaration order and de-duplicated. A field is a reference iff its
55
+ * {@link FieldDescriptor.referencesTypeKey} is a non-empty string. PURE helper —
56
+ * exported for reuse/testing.
57
+ */
58
+ function collectReferencedTypeKeys(fields) {
59
+ const seen = new Set();
60
+ const ordered = [];
61
+ for (const field of fields) {
62
+ const ref = field.referencesTypeKey;
63
+ if (typeof ref === 'string' && ref.length > 0 && !seen.has(ref)) {
64
+ seen.add(ref);
65
+ ordered.push(ref);
66
+ }
67
+ }
68
+ return ordered;
69
+ }
70
+ /**
71
+ * Auto-derive the READ-ONLY supporting lookups an object type's reference fields
72
+ * imply (ADR-0012). For every field with a `referencesTypeKey`, the referenced
73
+ * type gets a read-only lookup so a form over `objectType` can populate that
74
+ * field's dropdown — UNLESS the referenced type is marked `neverAutoGrant` in the
75
+ * action's declared dependencies (sensitive → its own explicit permission).
76
+ *
77
+ * PURE: takes descriptors (and an optional injected resolver), returns derived
78
+ * dependencies. No I/O, no registry mutation, no access decision.
79
+ *
80
+ * Rules enforced (ADR-0012):
81
+ * 1. Derived lookups are ALWAYS read-only — `capability` is always `'read'`;
82
+ * never create/edit/delete on the dependency.
83
+ * 2. A `neverAutoGrant` dependency is EXCLUDED (requires its own permission).
84
+ * 3. No reference fields ⇒ empty list.
85
+ * 4. Duplicate references collapse to a single lookup (declaration order).
86
+ * 5. A `resolveObjectType` that returns `undefined` for a reference ⇒ that
87
+ * reference is skipped (unknown type gets no auto-grant).
88
+ *
89
+ * @param objectType the object type whose reference fields drive the derivation.
90
+ * @param options declared dependencies (for the `neverAutoGrant` escape hatch)
91
+ * and an optional resolver to validate referenced types exist.
92
+ * @returns the de-duplicated, declaration-ordered read-only supporting lookups.
93
+ */
94
+ function deriveSupportingDependencies(objectType, options = {}) {
95
+ const sensitive = collectNeverAutoGrantTypeKeys(options.declaredDependencies);
96
+ const resolve = options.resolveObjectType;
97
+ const derived = [];
98
+ for (const typeKey of collectReferencedTypeKeys(objectType.fields)) {
99
+ // Escape hatch (ADR-0012): a sensitive lookup is never auto-granted.
100
+ if (sensitive.has(typeKey)) {
101
+ continue;
102
+ }
103
+ // Optional validation: skip references to types the platform doesn't know.
104
+ if (resolve && resolve(typeKey) === undefined) {
105
+ continue;
106
+ }
107
+ derived.push({ typeKey, capability: exports.SUPPORTING_READ_CAPABILITY });
108
+ }
109
+ return derived;
110
+ }
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ /**
3
+ * The **module-registers-metadata contract**.
4
+ *
5
+ * Per ADR-0003 + 010_Architecture "Module model": a business module plugs into
6
+ * the Core by **declaring metadata** (its object types + fields, the actions it
7
+ * exposes, each operation's access tier / required permission, supporting
8
+ * read-only dependencies, and policies) — it NEVER implements access checks
9
+ * itself. The Core discovers this metadata and gates everything centrally.
10
+ *
11
+ * A module that adds an operation without a classification is denied by default
12
+ * and flagged at build/boot — never silently open (§5.4). Dynamic (admin-defined)
13
+ * object types register via the SAME contract at runtime (§5.5, ADR-0014).
14
+ *
15
+ * TYPES + REGISTRY INTERFACE STUB ONLY — no discovery/enforcement logic here.
16
+ * ADR-0003: framework-agnostic.
17
+ */
18
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
19
+ if (k2 === undefined) k2 = k;
20
+ var desc = Object.getOwnPropertyDescriptor(m, k);
21
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
22
+ desc = { enumerable: true, get: function() { return m[k]; } };
23
+ }
24
+ Object.defineProperty(o, k2, desc);
25
+ }) : (function(o, m, k, k2) {
26
+ if (k2 === undefined) k2 = k;
27
+ o[k2] = m[k];
28
+ }));
29
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
30
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
31
+ };
32
+ Object.defineProperty(exports, "__esModule", { value: true });
33
+ exports.DEFAULT_CAPABILITY_PLANE = void 0;
34
+ exports.resolveTier = resolveTier;
35
+ exports.resolvePlane = resolvePlane;
36
+ exports.resolveActionPermissionKey = resolveActionPermissionKey;
37
+ const index_js_1 = require("../authorization/index.js");
38
+ // The boot-completeness-check contract (ADR-0002/0010) — types + signature only.
39
+ __exportStar(require("./completeness.js"), exports);
40
+ // The ModuleRegistry implementation (Team A, L3) — `createModuleRegistry()`
41
+ // backs the `ModuleRegistry` interface declared below. Storage/lookup only, no
42
+ // engine logic (see ./registry.ts).
43
+ __exportStar(require("./registry.js"), exports);
44
+ // Supporting read-only dependency auto-derivation (ADR-0012, Team B, L3) — pure
45
+ // functions that derive the read-only lookups a permission implies from an
46
+ // object type's reference fields, honoring the neverAutoGrant escape hatch.
47
+ __exportStar(require("./dependencies.js"), exports);
48
+ exports.DEFAULT_CAPABILITY_PLANE = 'data';
49
+ /**
50
+ * Resolve an action's baseline access tier (§6, ADR-0004): the action's own
51
+ * `tier`, or `gated` when omitted — the fail-closed default. PURE. This is the
52
+ * single canonical tier-resolution rule; every consumer (registry lookups,
53
+ * completeness check, the future evaluator) MUST route through it rather than
54
+ * re-implementing the `?? 'gated'` default, so the default can never drift.
55
+ */
56
+ function resolveTier(action) {
57
+ return action.tier ?? index_js_1.DEFAULT_ACCESS_TIER;
58
+ }
59
+ /** Resolve an action's plane, defaulting to `data` (fail-safe). Single source —
60
+ * never re-derive elsewhere. */
61
+ function resolvePlane(action) {
62
+ return action.plane ?? exports.DEFAULT_CAPABILITY_PLANE;
63
+ }
64
+ /**
65
+ * Derive an action's stable permission identity (ADR-0010) within its owning
66
+ * module: `moduleKey:typeKey:actionKey`. PURE — wraps {@link derivePermissionKey}
67
+ * with the action's own `typeKey`. Grants bind to this result.
68
+ *
69
+ * NOTE: `public`-tier actions are reachable without a grant; the permission key
70
+ * still derives (for catalog/audit listing per ADR-0012) but a Public action
71
+ * needs no bound grant to be allowed. The consumer decides whether to require a
72
+ * grant based on {@link resolveTier}.
73
+ */
74
+ function resolveActionPermissionKey(moduleKey, action) {
75
+ return (0, index_js_1.derivePermissionKey)(moduleKey, action.typeKey, action.actionKey);
76
+ }
@@ -0,0 +1,207 @@
1
+ "use strict";
2
+ /**
3
+ * `ModuleRegistry` implementation (Team A, L3) — the Core's **metadata
4
+ * intake / storage / lookup** for the module-registers-metadata contract
5
+ * (`./index.ts`, §5 "Module model", ADR-0010/0014).
6
+ *
7
+ * SCOPE: intake, structural validation, indexing, and lookup ONLY. This is a
8
+ * registry — it stores declarations and answers "what is registered?". It makes
9
+ * NO access decisions: there is zero `decide` / `normalize` / `authenticate` /
10
+ * `assess` / `establish` / `record` logic here (those live in later engine
11
+ * layers). ADR-0003: framework-agnostic — no framework imports.
12
+ *
13
+ * Every resolved {@link RegisteredAction} derives its `permissionKey` and `tier`
14
+ * via the FROZEN pure helpers {@link resolveActionPermissionKey} /
15
+ * {@link resolveTier} — this file NEVER re-derives a key or re-applies the
16
+ * `?? 'gated'` default itself, so the single-source rules can't drift.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.createModuleRegistry = createModuleRegistry;
20
+ const index_js_1 = require("./index.js");
21
+ /**
22
+ * In-memory implementation of {@link ModuleRegistry}. Fail-closed posture
23
+ * (ADR-0002): any structural violation at intake throws LOUD so a malformed
24
+ * module can never be silently half-registered.
25
+ */
26
+ class InMemoryModuleRegistry {
27
+ /** Static modules, keyed by `moduleKey`. Insertion order = registration order. */
28
+ modules = new Map();
29
+ /** Runtime (dynamic-DDL, ADR-0014) object types, keyed by `typeKey`. */
30
+ dynamicObjectTypes = new Map();
31
+ /**
32
+ * Index: every registered object type (static + dynamic) → its descriptor.
33
+ * Spans all modules so {@link resolveObjectType} is O(1) and reference
34
+ * integrity can be checked against the whole registry.
35
+ */
36
+ objectTypeIndex = new Map();
37
+ /** Index: stable {@link PermissionKey} → the resolved action that owns it. */
38
+ actionIndex = new Map();
39
+ register(metadata) {
40
+ const { moduleKey } = metadata;
41
+ if (moduleKey.length === 0) {
42
+ throw new Error('ModuleRegistry.register: moduleKey must be non-empty');
43
+ }
44
+ if (this.modules.has(moduleKey)) {
45
+ throw new Error(`ModuleRegistry.register: module "${moduleKey}" is already registered (duplicate moduleKey)`);
46
+ }
47
+ // --- Structural validation (all-or-nothing: validate BEFORE mutating any
48
+ // shared index, so a rejected module leaves the registry untouched). ---
49
+ // (1) No duplicate object-type key WITHIN this module, and no collision with
50
+ // an already-registered type key across the registry.
51
+ const localTypeKeys = new Set();
52
+ for (const objectType of metadata.objectTypes) {
53
+ const { typeKey } = objectType;
54
+ if (typeKey.length === 0) {
55
+ throw new Error(`ModuleRegistry.register: module "${moduleKey}" declares an object type with an empty typeKey`);
56
+ }
57
+ if (localTypeKeys.has(typeKey)) {
58
+ throw new Error(`ModuleRegistry.register: module "${moduleKey}" declares duplicate object type "${typeKey}"`);
59
+ }
60
+ if (this.objectTypeIndex.has(typeKey)) {
61
+ throw new Error(`ModuleRegistry.register: object type "${typeKey}" (module "${moduleKey}") collides with an already-registered type`);
62
+ }
63
+ localTypeKeys.add(typeKey);
64
+ }
65
+ // (2) No duplicate action key WITHIN an object type of this module, and every
66
+ // action's derived permission key must be globally unique. The permission
67
+ // key derivation (via the frozen helper) also fail-closes on empty /
68
+ // separator-containing segments.
69
+ const resolvedActions = [];
70
+ const localActionKeysByType = new Map();
71
+ for (const action of metadata.actions) {
72
+ this.assertActionUnique(moduleKey, action, localActionKeysByType);
73
+ const permissionKey = (0, index_js_1.resolveActionPermissionKey)(moduleKey, action);
74
+ if (this.actionIndex.has(permissionKey)) {
75
+ throw new Error(`ModuleRegistry.register: permission key "${permissionKey}" (module "${moduleKey}") collides with an already-registered action`);
76
+ }
77
+ resolvedActions.push({
78
+ moduleKey,
79
+ action,
80
+ permissionKey,
81
+ tier: (0, index_js_1.resolveTier)(action),
82
+ plane: (0, index_js_1.resolvePlane)(action),
83
+ });
84
+ }
85
+ // (3) Reference integrity — FieldDescriptor.referencesTypeKey and
86
+ // SupportingDependency.typeKey should resolve to a registered object type.
87
+ // Resolvable NOW = this module's own types + any already-registered type
88
+ // (static or dynamic). A reference that resolves within-module or to an
89
+ // earlier module is validated here and fails LOUD on a dangling target.
90
+ //
91
+ // DEFERRED (cross-module forward reference): a reference to a type owned
92
+ // by a module registered LATER is NOT yet resolvable and cannot be
93
+ // validated at this call without ordering assumptions. Per the prelude
94
+ // DONE.md the registry's job is intake/lookup, and modules may register in
95
+ // any order, so such a reference is left to a later whole-registry
96
+ // completeness pass rather than rejected here. See the TODO below.
97
+ // TODO(cross-module-ref): once every module is registered, a boot-time
98
+ // sweep should assert every referencesTypeKey / dependency typeKey
99
+ // resolves via resolveObjectType (spanning the full registry). That sweep
100
+ // is out of this registry's intake scope (Team C completeness owns the
101
+ // whole-registry reconciliation).
102
+ this.assertReferenceIntegrity(moduleKey, metadata);
103
+ // --- Commit: validation passed, mutate the indices atomically. ---
104
+ this.modules.set(moduleKey, metadata);
105
+ for (const objectType of metadata.objectTypes) {
106
+ this.objectTypeIndex.set(objectType.typeKey, objectType);
107
+ }
108
+ for (const resolved of resolvedActions) {
109
+ this.actionIndex.set(resolved.permissionKey, resolved);
110
+ }
111
+ }
112
+ registerDynamicObjectType(objectType) {
113
+ const { typeKey } = objectType;
114
+ if (typeKey.length === 0) {
115
+ throw new Error('ModuleRegistry.registerDynamicObjectType: typeKey must be non-empty');
116
+ }
117
+ if (this.objectTypeIndex.has(typeKey)) {
118
+ throw new Error(`ModuleRegistry.registerDynamicObjectType: object type "${typeKey}" is already registered (duplicate typeKey)`);
119
+ }
120
+ // ADR-0014 / ADR-0004: a runtime-created object type is REGISTERED (so the
121
+ // decision path can resolve it) and Gated by default. "Gated by default" is
122
+ // the classification of any ACTION on this type — an unclassified action
123
+ // resolves to `gated` via resolveTier; the type itself carries no actions
124
+ // here (dynamic actions, if any, are declared via register()). No DDL, no
125
+ // action seeding — registration + Gated classification only.
126
+ this.dynamicObjectTypes.set(typeKey, objectType);
127
+ this.objectTypeIndex.set(typeKey, objectType);
128
+ }
129
+ list() {
130
+ return [...this.modules.values()];
131
+ }
132
+ resolveObjectType(typeKey) {
133
+ // Spans static + dynamic types; a miss ⇒ the decision path treats it as
134
+ // `resource_not_registered` → Deny. Return undefined, NEVER throw.
135
+ return this.objectTypeIndex.get(typeKey);
136
+ }
137
+ resolveByPermissionKey(permissionKey) {
138
+ // A miss ⇒ ORPHANED permission (a grant referencing a permission no live
139
+ // action derives). Return undefined, NEVER throw.
140
+ return this.actionIndex.get(permissionKey);
141
+ }
142
+ listActions() {
143
+ return [...this.actionIndex.values()];
144
+ }
145
+ /**
146
+ * Assert an action key is unique within its object type for this module, and
147
+ * that its `typeKey`/`actionKey` are non-empty. Mutates `seenByType` to record
148
+ * the action key as seen. Throws on a collision (fail-closed).
149
+ */
150
+ assertActionUnique(moduleKey, action, seenByType) {
151
+ const { actionKey, typeKey } = action;
152
+ if (actionKey.length === 0) {
153
+ throw new Error(`ModuleRegistry.register: module "${moduleKey}" declares an action with an empty actionKey`);
154
+ }
155
+ if (typeKey.length === 0) {
156
+ throw new Error(`ModuleRegistry.register: module "${moduleKey}" declares action "${actionKey}" with an empty typeKey`);
157
+ }
158
+ let seen = seenByType.get(typeKey);
159
+ if (seen === undefined) {
160
+ seen = new Set();
161
+ seenByType.set(typeKey, seen);
162
+ }
163
+ if (seen.has(actionKey)) {
164
+ throw new Error(`ModuleRegistry.register: module "${moduleKey}" declares duplicate action "${actionKey}" on object type "${typeKey}"`);
165
+ }
166
+ seen.add(actionKey);
167
+ }
168
+ /**
169
+ * Validate reference SHAPE at intake: every `FieldDescriptor.referencesTypeKey`
170
+ * and `SupportingDependency.typeKey` that is present must be a non-empty key
171
+ * (an empty target is a malformed declaration → fail loud, fail-closed).
172
+ *
173
+ * Whether the target actually RESOLVES to a registered type is deliberately NOT
174
+ * enforced here: a valid reference may point at a type owned by a module that
175
+ * registers later (cross-module forward reference), which is unresolvable at
176
+ * this call without ordering assumptions. That whole-registry resolution is
177
+ * deferred to a boot-time completeness sweep (see the DEFERRED note in
178
+ * {@link register}). A caller can resolve any reference at any time via
179
+ * {@link resolveObjectType}.
180
+ */
181
+ assertReferenceIntegrity(moduleKey, metadata) {
182
+ // FieldDescriptor.referencesTypeKey — a field pointing at another type.
183
+ for (const objectType of metadata.objectTypes) {
184
+ for (const field of objectType.fields) {
185
+ const ref = field.referencesTypeKey;
186
+ if (ref !== undefined && ref.length === 0) {
187
+ throw new Error(`ModuleRegistry.register: module "${moduleKey}" object type "${objectType.typeKey}" field "${field.key}" has an empty referencesTypeKey`);
188
+ }
189
+ }
190
+ }
191
+ // SupportingDependency.typeKey — a read-only lookup an action depends on.
192
+ for (const action of metadata.actions) {
193
+ for (const dep of action.supportingDependencies ?? []) {
194
+ if (dep.typeKey.length === 0) {
195
+ throw new Error(`ModuleRegistry.register: module "${moduleKey}" action "${action.actionKey}" declares a supporting dependency with an empty typeKey`);
196
+ }
197
+ }
198
+ }
199
+ }
200
+ }
201
+ /**
202
+ * Construct a fresh, empty {@link ModuleRegistry}. The registry is stateful
203
+ * (modules accumulate across `register` calls) — one instance per Core boot.
204
+ */
205
+ function createModuleRegistry() {
206
+ return new InMemoryModuleRegistry();
207
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ /**
3
+ * The **risk-signal evaluator CONTRACT** + its registry — the extension seam the
4
+ * Risk engine routes each observed `RiskSignal` through. One evaluator maps one
5
+ * signal to a weighted `0..1` risk CONTRIBUTION plus which `RiskSignalKind`
6
+ * contributed. Mirrors the Authentication factor-verifier contract + registry
7
+ * (`../authentication/totp.ts`) EXACTLY: a discriminator (`name`), a pure
8
+ * evaluate, a tiny fail-closed registry (duplicate → throw LOUD).
9
+ *
10
+ * ⛔ SCOPE: an evaluator SCORES a signal — it never blocks, mints, denies, or
11
+ * throws-to-deny. Risk ADVISES. The engine aggregates these contributions into a
12
+ * `RiskScore` VALUE; enforcement is a later layer.
13
+ *
14
+ * ⛔ FAIL-SAFE DIRECTION (this layer's whole point): an evaluator that cannot make
15
+ * sense of its signal MUST return a HIGH contribution (`elevated(...)`), never a
16
+ * low/zero one, and never throw. Uncertainty ELEVATES risk. An UNKNOWN signal
17
+ * (no registered evaluator for its kind/name) is likewise treated by the engine as
18
+ * elevating — never silently ignored. See `risk-engine.ts` for the aggregation.
19
+ *
20
+ * ADR-0003: framework-agnostic — no framework / HTTP / DB / node imports.
21
+ */
22
+ Object.defineProperty(exports, "__esModule", { value: true });
23
+ exports.RiskSignalEvaluatorRegistry = void 0;
24
+ exports.clampContribution = clampContribution;
25
+ /** Clamp any number-ish value into `[0,1]`; non-finite / non-number ⇒ the fail-safe floor. */
26
+ function clampContribution(v, failSafeFloor) {
27
+ if (typeof v !== 'number' || !Number.isFinite(v))
28
+ return failSafeFloor;
29
+ if (v < 0)
30
+ return 0;
31
+ if (v > 1)
32
+ return 1;
33
+ return v;
34
+ }
35
+ /**
36
+ * A tiny registry of {@link RiskSignalEvaluator}s, keyed by `name`. The engine
37
+ * resolves the evaluator for each signal through this; a MISS is an unknown signal
38
+ * → the engine treats it as ELEVATING (fail-safe), never ignores it. Fail-closed
39
+ * on empty/duplicate registration, mirroring `FactorVerifierRegistry`.
40
+ */
41
+ class RiskSignalEvaluatorRegistry {
42
+ byName = new Map();
43
+ /**
44
+ * Register an evaluator under its `name`. Fail-closed: an empty name or a
45
+ * duplicate registration throws LOUD (a silently-shadowed evaluator could route a
46
+ * signal to the wrong scorer, corrupting the assessment).
47
+ */
48
+ register(evaluator) {
49
+ const { name } = evaluator;
50
+ if (typeof name !== 'string' || name.length === 0) {
51
+ throw new Error('RiskSignalEvaluatorRegistry.register: name must be non-empty');
52
+ }
53
+ if (this.byName.has(name)) {
54
+ throw new Error(`RiskSignalEvaluatorRegistry.register: name "${name}" is already registered (duplicate evaluator)`);
55
+ }
56
+ this.byName.set(name, evaluator);
57
+ }
58
+ /** Look up an evaluator by name, or `undefined` if none is registered. */
59
+ resolve(name) {
60
+ return this.byName.get(name);
61
+ }
62
+ }
63
+ exports.RiskSignalEvaluatorRegistry = RiskSignalEvaluatorRegistry;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ /**
3
+ * Engine seam: **Risk** — scores the request to drive adaptive decisions.
4
+ *
5
+ * Evaluates device, behaviour, and context signals into a score that can drive
6
+ * adaptive decisions (e.g. step-up MFA) — 010_Architecture §4. This seam names
7
+ * the scoring contract only; the scoring model is a later layer.
8
+ *
9
+ * TYPES / INTERFACES ONLY. ADR-0003: framework-agnostic.
10
+ */
11
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
12
+ if (k2 === undefined) k2 = k;
13
+ var desc = Object.getOwnPropertyDescriptor(m, k);
14
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
15
+ desc = { enumerable: true, get: function() { return m[k]; } };
16
+ }
17
+ Object.defineProperty(o, k2, desc);
18
+ }) : (function(o, m, k, k2) {
19
+ if (k2 === undefined) k2 = k;
20
+ o[k2] = m[k];
21
+ }));
22
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
23
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ // --- Engine implementation behind the frozen seam above (this layer) ---------
27
+ // The signal-evaluator CONTRACT (the extension seam evaluators plug into) + its
28
+ // registry + the contribution-clamp helper:
29
+ __exportStar(require("./evaluator.js"), exports);
30
+ // The reference signal evaluators (unknown-device, sensitive-action) — Rule of Three:
31
+ __exportStar(require("./signals.js"), exports);
32
+ // The threshold/config schema (band cutoffs + step-up threshold) + its validation:
33
+ __exportStar(require("./thresholds.js"), exports);
34
+ // The scoring engine `createRiskEngine` + the SEPARATE advisory `recommendStepUp`:
35
+ __exportStar(require("./risk-engine.js"), exports);