@brud/kernel 0.0.0-stage → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +63 -2
- package/dist/index.d.ts +15 -0
- package/dist/index.js +13 -0
- package/dist/serviceModules/events.d.ts +1 -0
- package/dist/serviceModules/events.js +1 -0
- package/dist/serviceModules/exposedApiRegistry.d.ts +1 -0
- package/dist/serviceModules/exposedApiRegistry.js +1 -0
- package/dist/serviceModules/identityRegistry.d.ts +1 -0
- package/dist/serviceModules/identityRegistry.js +1 -0
- package/dist/serviceModules/interception.d.ts +1 -0
- package/dist/serviceModules/interception.js +1 -0
- package/dist/serviceModules/parser.d.ts +1 -0
- package/dist/serviceModules/parser.js +1 -0
- package/dist/serviceModules/router.d.ts +1 -0
- package/dist/serviceModules/router.js +1 -0
- package/dist/serviceModules/wasmCage.d.ts +1 -0
- package/dist/serviceModules/wasmCage.js +1 -0
- package/dist/services/Events/contracts/IEventsService.d.ts +64 -0
- package/dist/services/Events/contracts/IEventsService.js +1 -0
- package/dist/services/Events/contracts/errors.d.ts +31 -0
- package/dist/services/Events/contracts/errors.js +40 -0
- package/dist/services/Events/contracts/index.d.ts +3 -0
- package/dist/services/Events/contracts/index.js +3 -0
- package/dist/services/Events/contracts/tokens.d.ts +2 -0
- package/dist/services/Events/contracts/tokens.js +2 -0
- package/dist/services/Events/descriptor/EventsDescriptor.d.ts +9 -0
- package/dist/services/Events/descriptor/EventsDescriptor.js +56 -0
- package/dist/services/Events/descriptor/tokens.d.ts +1 -0
- package/dist/services/Events/descriptor/tokens.js +1 -0
- package/dist/services/Events/index.d.ts +4 -0
- package/dist/services/Events/index.js +4 -0
- package/dist/services/Events/orchestrator/EventsService.d.ts +51 -0
- package/dist/services/Events/orchestrator/EventsService.js +339 -0
- package/dist/services/Events/orchestrator/index.d.ts +2 -0
- package/dist/services/Events/orchestrator/index.js +2 -0
- package/dist/services/Events/orchestrator/limits.d.ts +22 -0
- package/dist/services/Events/orchestrator/limits.js +33 -0
- package/dist/services/Events/serviceModule.d.ts +2 -0
- package/dist/services/Events/serviceModule.js +5 -0
- package/dist/services/ExposedApiRegistry/contracts/IExposedApiRegistry.d.ts +56 -0
- package/dist/services/ExposedApiRegistry/contracts/IExposedApiRegistry.js +1 -0
- package/dist/services/ExposedApiRegistry/contracts/errors.d.ts +132 -0
- package/dist/services/ExposedApiRegistry/contracts/errors.js +195 -0
- package/dist/services/ExposedApiRegistry/contracts/index.d.ts +9 -0
- package/dist/services/ExposedApiRegistry/contracts/index.js +4 -0
- package/dist/services/ExposedApiRegistry/contracts/policy.d.ts +90 -0
- package/dist/services/ExposedApiRegistry/contracts/policy.js +169 -0
- package/dist/services/ExposedApiRegistry/contracts/registrant.d.ts +37 -0
- package/dist/services/ExposedApiRegistry/contracts/registrant.js +36 -0
- package/dist/services/ExposedApiRegistry/contracts/types.d.ts +133 -0
- package/dist/services/ExposedApiRegistry/contracts/types.js +73 -0
- package/dist/services/ExposedApiRegistry/descriptor/ExposedApiRegistryDescriptor.d.ts +44 -0
- package/dist/services/ExposedApiRegistry/descriptor/ExposedApiRegistryDescriptor.js +58 -0
- package/dist/services/ExposedApiRegistry/descriptor/tokens.d.ts +8 -0
- package/dist/services/ExposedApiRegistry/descriptor/tokens.js +8 -0
- package/dist/services/ExposedApiRegistry/index.d.ts +6 -0
- package/dist/services/ExposedApiRegistry/index.js +5 -0
- package/dist/services/ExposedApiRegistry/orchestrator/ExposedApiRegistryService.d.ts +68 -0
- package/dist/services/ExposedApiRegistry/orchestrator/ExposedApiRegistryService.js +452 -0
- package/dist/services/ExposedApiRegistry/orchestrator/index.d.ts +2 -0
- package/dist/services/ExposedApiRegistry/orchestrator/index.js +1 -0
- package/dist/services/ExposedApiRegistry/serviceModule.d.ts +10 -0
- package/dist/services/ExposedApiRegistry/serviceModule.js +20 -0
- package/dist/services/ExposedApiRegistry/storage/protection.d.ts +21 -0
- package/dist/services/ExposedApiRegistry/storage/protection.js +35 -0
- package/dist/services/ExposedApiRegistry/storage/scopeRecord.d.ts +45 -0
- package/dist/services/ExposedApiRegistry/storage/scopeRecord.js +178 -0
- package/dist/services/IdentityRegistry/contracts/errors.d.ts +88 -0
- package/dist/services/IdentityRegistry/contracts/errors.js +128 -0
- package/dist/services/IdentityRegistry/contracts/index.d.ts +5 -0
- package/dist/services/IdentityRegistry/contracts/index.js +3 -0
- package/dist/services/IdentityRegistry/contracts/types.d.ts +77 -0
- package/dist/services/IdentityRegistry/contracts/types.js +18 -0
- package/dist/services/IdentityRegistry/descriptor/descriptor.d.ts +36 -0
- package/dist/services/IdentityRegistry/descriptor/descriptor.js +44 -0
- package/dist/services/IdentityRegistry/descriptor/tokens.d.ts +8 -0
- package/dist/services/IdentityRegistry/descriptor/tokens.js +8 -0
- package/dist/services/IdentityRegistry/index.d.ts +11 -0
- package/dist/services/IdentityRegistry/index.js +6 -0
- package/dist/services/IdentityRegistry/orchestrator/IdentityRegistryService.d.ts +47 -0
- package/dist/services/IdentityRegistry/orchestrator/IdentityRegistryService.js +240 -0
- package/dist/services/IdentityRegistry/serviceModule.d.ts +10 -0
- package/dist/services/IdentityRegistry/serviceModule.js +24 -0
- package/dist/services/IdentityRegistry/storage/registryDocument.d.ts +55 -0
- package/dist/services/IdentityRegistry/storage/registryDocument.js +235 -0
- package/dist/services/Interception/contracts/IInterceptionService.d.ts +155 -0
- package/dist/services/Interception/contracts/IInterceptionService.js +12 -0
- package/dist/services/Interception/contracts/errors.d.ts +26 -0
- package/dist/services/Interception/contracts/errors.js +35 -0
- package/dist/services/Interception/contracts/index.d.ts +3 -0
- package/dist/services/Interception/contracts/index.js +3 -0
- package/dist/services/Interception/contracts/tokens.d.ts +2 -0
- package/dist/services/Interception/contracts/tokens.js +2 -0
- package/dist/services/Interception/descriptor/InterceptionDescriptor.d.ts +9 -0
- package/dist/services/Interception/descriptor/InterceptionDescriptor.js +50 -0
- package/dist/services/Interception/descriptor/tokens.d.ts +1 -0
- package/dist/services/Interception/descriptor/tokens.js +1 -0
- package/dist/services/Interception/index.d.ts +4 -0
- package/dist/services/Interception/index.js +4 -0
- package/dist/services/Interception/orchestrator/InterceptionService.d.ts +40 -0
- package/dist/services/Interception/orchestrator/InterceptionService.js +343 -0
- package/dist/services/Interception/orchestrator/index.d.ts +1 -0
- package/dist/services/Interception/orchestrator/index.js +1 -0
- package/dist/services/Interception/serviceModule.d.ts +2 -0
- package/dist/services/Interception/serviceModule.js +5 -0
- package/dist/services/Parser/contracts/IParserService.d.ts +21 -0
- package/dist/services/Parser/contracts/IParserService.js +1 -0
- package/dist/services/Parser/contracts/errors.d.ts +25 -0
- package/dist/services/Parser/contracts/errors.js +33 -0
- package/dist/services/Parser/contracts/index.d.ts +4 -0
- package/dist/services/Parser/contracts/index.js +1 -0
- package/dist/services/Parser/contracts/types.d.ts +42 -0
- package/dist/services/Parser/contracts/types.js +1 -0
- package/dist/services/Parser/descriptor/ParserDescriptor.d.ts +23 -0
- package/dist/services/Parser/descriptor/ParserDescriptor.js +50 -0
- package/dist/services/Parser/descriptor/tokens.d.ts +8 -0
- package/dist/services/Parser/descriptor/tokens.js +8 -0
- package/dist/services/Parser/index.d.ts +8 -0
- package/dist/services/Parser/index.js +8 -0
- package/dist/services/Parser/orchestrator/ParserService.d.ts +20 -0
- package/dist/services/Parser/orchestrator/ParserService.js +105 -0
- package/dist/services/Parser/orchestrator/index.d.ts +1 -0
- package/dist/services/Parser/orchestrator/index.js +1 -0
- package/dist/services/Parser/parser/indexSynthesis.d.ts +11 -0
- package/dist/services/Parser/parser/indexSynthesis.js +13 -0
- package/dist/services/Parser/parser/parameters.d.ts +14 -0
- package/dist/services/Parser/parser/parameters.js +89 -0
- package/dist/services/Parser/parser/scanner.d.ts +48 -0
- package/dist/services/Parser/parser/scanner.js +156 -0
- package/dist/services/Parser/parser/syntax.d.ts +30 -0
- package/dist/services/Parser/parser/syntax.js +34 -0
- package/dist/services/Parser/serviceModule.d.ts +2 -0
- package/dist/services/Parser/serviceModule.js +5 -0
- package/dist/services/Router/bootOptions.d.ts +2 -0
- package/dist/services/Router/bootOptions.js +2 -0
- package/dist/services/Router/contracts/IRouterService.d.ts +43 -0
- package/dist/services/Router/contracts/IRouterService.js +1 -0
- package/dist/services/Router/contracts/caller.d.ts +6 -0
- package/dist/services/Router/contracts/caller.js +6 -0
- package/dist/services/Router/contracts/errors.d.ts +67 -0
- package/dist/services/Router/contracts/errors.js +90 -0
- package/dist/services/Router/contracts/index.d.ts +8 -0
- package/dist/services/Router/contracts/index.js +4 -0
- package/dist/services/Router/contracts/kind.d.ts +7 -0
- package/dist/services/Router/contracts/kind.js +10 -0
- package/dist/services/Router/contracts/registrant.d.ts +23 -0
- package/dist/services/Router/contracts/registrant.js +13 -0
- package/dist/services/Router/contracts/types.d.ts +110 -0
- package/dist/services/Router/contracts/types.js +1 -0
- package/dist/services/Router/descriptor/RouterDescriptor.d.ts +32 -0
- package/dist/services/Router/descriptor/RouterDescriptor.js +92 -0
- package/dist/services/Router/descriptor/tokens.d.ts +9 -0
- package/dist/services/Router/descriptor/tokens.js +9 -0
- package/dist/services/Router/index.d.ts +4 -0
- package/dist/services/Router/index.js +4 -0
- package/dist/services/Router/orchestrator/RouterService.d.ts +93 -0
- package/dist/services/Router/orchestrator/RouterService.js +457 -0
- package/dist/services/Router/orchestrator/index.d.ts +2 -0
- package/dist/services/Router/orchestrator/index.js +1 -0
- package/dist/services/Router/serviceModule.d.ts +4 -0
- package/dist/services/Router/serviceModule.js +22 -0
- package/dist/services/WasmCage/index.d.ts +7 -0
- package/dist/services/WasmCage/index.js +4 -0
- package/dist/services/WasmCage/scripts/prepin.d.ts +1 -0
- package/dist/services/WasmCage/scripts/prepin.js +38 -0
- package/dist/services/WasmCage/serviceModule.d.ts +12 -0
- package/dist/services/WasmCage/serviceModule.js +18 -0
- package/dist/services/WasmCage/src/caged.d.ts +121 -0
- package/dist/services/WasmCage/src/caged.js +460 -0
- package/dist/services/WasmCage/src/descriptor.d.ts +47 -0
- package/dist/services/WasmCage/src/descriptor.js +56 -0
- package/dist/services/WasmCage/src/native.d.ts +29 -0
- package/dist/services/WasmCage/src/native.js +66 -0
- package/dist/services/WasmCage/src/nativeDigests.d.ts +9 -0
- package/dist/services/WasmCage/src/nativeDigests.js +15 -0
- package/dist/services/WasmCage/src/package.d.ts +36 -0
- package/dist/services/WasmCage/src/package.js +122 -0
- package/dist/services/shared/canonicalJson.d.ts +29 -0
- package/dist/services/shared/canonicalJson.js +93 -0
- package/dist/services/shared/documentStore.d.ts +16 -0
- package/dist/services/shared/documentStore.js +98 -0
- package/dist/src/bootstrap/bootKernelInternal.d.ts +19 -0
- package/dist/src/bootstrap/bootKernelInternal.js +633 -0
- package/dist/src/bootstrap/bootTypes.d.ts +113 -0
- package/dist/src/bootstrap/bootTypes.js +1 -0
- package/dist/src/bootstrap/config/index.d.ts +3 -0
- package/dist/src/bootstrap/config/index.js +2 -0
- package/dist/src/bootstrap/config/integrity.d.ts +45 -0
- package/dist/src/bootstrap/config/integrity.js +92 -0
- package/dist/src/bootstrap/config/parse.d.ts +15 -0
- package/dist/src/bootstrap/config/parse.js +81 -0
- package/dist/src/bootstrap/config/pathWriteGuard.d.ts +20 -0
- package/dist/src/bootstrap/config/pathWriteGuard.js +349 -0
- package/dist/src/bootstrap/config/types.d.ts +41 -0
- package/dist/src/bootstrap/config/types.js +15 -0
- package/dist/src/bootstrap/hardcore.d.ts +212 -0
- package/dist/src/bootstrap/hardcore.js +312 -0
- package/dist/src/bootstrap/hardcoreServices.d.ts +10 -0
- package/dist/src/bootstrap/hardcoreServices.js +258 -0
- package/dist/src/bootstrap/index.d.ts +13 -0
- package/dist/src/bootstrap/index.js +7 -0
- package/dist/src/bootstrap/loaded.d.ts +30 -0
- package/dist/src/bootstrap/loaded.js +45 -0
- package/dist/src/bootstrap/packageRequirements.d.ts +26 -0
- package/dist/src/bootstrap/packageRequirements.js +52 -0
- package/dist/src/bootstrap/reload.d.ts +44 -0
- package/dist/src/bootstrap/reload.js +45 -0
- package/dist/src/bootstrap/seal.d.ts +32 -0
- package/dist/src/bootstrap/seal.js +101 -0
- package/dist/src/contracts/accessBroker.d.ts +53 -0
- package/dist/src/contracts/accessBroker.js +1 -0
- package/dist/src/contracts/brudOperation.d.ts +20 -0
- package/dist/src/contracts/brudOperation.js +20 -0
- package/dist/src/contracts/callChain.d.ts +161 -0
- package/dist/src/contracts/callChain.js +19 -0
- package/dist/src/contracts/capability.d.ts +26 -0
- package/dist/src/contracts/capability.js +26 -0
- package/dist/src/contracts/capabilitySet.d.ts +23 -0
- package/dist/src/contracts/capabilitySet.js +72 -0
- package/dist/src/contracts/container.d.ts +17 -0
- package/dist/src/contracts/container.js +8 -0
- package/dist/src/contracts/context.d.ts +19 -0
- package/dist/src/contracts/context.js +1 -0
- package/dist/src/contracts/contextContract.d.ts +132 -0
- package/dist/src/contracts/contextContract.js +274 -0
- package/dist/src/contracts/dispatchView.d.ts +21 -0
- package/dist/src/contracts/dispatchView.js +1 -0
- package/dist/src/contracts/handler.d.ts +21 -0
- package/dist/src/contracts/handler.js +1 -0
- package/dist/src/contracts/index.d.ts +26 -0
- package/dist/src/contracts/index.js +9 -0
- package/dist/src/contracts/kernel.d.ts +60 -0
- package/dist/src/contracts/kernel.js +1 -0
- package/dist/src/contracts/lifecycle.d.ts +59 -0
- package/dist/src/contracts/lifecycle.js +1 -0
- package/dist/src/contracts/manifest.d.ts +180 -0
- package/dist/src/contracts/manifest.js +334 -0
- package/dist/src/contracts/pipeline.d.ts +8 -0
- package/dist/src/contracts/pipeline.js +1 -0
- package/dist/src/contracts/policy.d.ts +75 -0
- package/dist/src/contracts/policy.js +77 -0
- package/dist/src/contracts/registry.d.ts +7 -0
- package/dist/src/contracts/registry.js +1 -0
- package/dist/src/contracts/serviceDescriptor.d.ts +67 -0
- package/dist/src/contracts/serviceDescriptor.js +1 -0
- package/dist/src/contracts/serviceToken.d.ts +36 -0
- package/dist/src/contracts/serviceToken.js +16 -0
- package/dist/src/engine/BrudKernel.d.ts +120 -0
- package/dist/src/engine/BrudKernel.js +395 -0
- package/dist/src/engine/ExecutionPipeline.d.ts +13 -0
- package/dist/src/engine/ExecutionPipeline.js +62 -0
- package/dist/src/engine/KernelAccessBroker.d.ts +47 -0
- package/dist/src/engine/KernelAccessBroker.js +107 -0
- package/dist/src/engine/KernelRegistry.d.ts +14 -0
- package/dist/src/engine/KernelRegistry.js +33 -0
- package/dist/src/engine/OperationRegistry.d.ts +9 -0
- package/dist/src/engine/OperationRegistry.js +18 -0
- package/dist/src/engine/ServiceContainer.d.ts +13 -0
- package/dist/src/engine/ServiceContainer.js +53 -0
- package/dist/src/engine/callChain.d.ts +68 -0
- package/dist/src/engine/callChain.js +175 -0
- package/dist/src/engine/dag.d.ts +2 -0
- package/dist/src/engine/dag.js +34 -0
- package/dist/src/engine/errors.d.ts +26 -0
- package/dist/src/engine/errors.js +52 -0
- package/dist/src/engine/index.d.ts +11 -0
- package/dist/src/engine/index.js +8 -0
- package/dist/src/engine/state.d.ts +1 -0
- package/dist/src/engine/state.js +1 -0
- package/dist/src/validation/jsonSchema/compile.d.ts +8 -0
- package/dist/src/validation/jsonSchema/compile.js +386 -0
- package/dist/src/validation/jsonSchema/index.d.ts +3 -0
- package/dist/src/validation/jsonSchema/index.js +2 -0
- package/dist/src/validation/jsonSchema/jsonValue.d.ts +29 -0
- package/dist/src/validation/jsonSchema/jsonValue.js +190 -0
- package/dist/src/validation/jsonSchema/pattern.d.ts +28 -0
- package/dist/src/validation/jsonSchema/pattern.js +155 -0
- package/dist/src/validation/jsonSchema/types.d.ts +71 -0
- package/dist/src/validation/jsonSchema/types.js +15 -0
- package/package.json +59 -4
- package/services/WasmCage/native/brud-wasm-cage.darwin-arm64.node +0 -0
- package/services/WasmCage/native/brud-wasm-cage.darwin-x64.node +0 -0
- package/services/WasmCage/native/brud-wasm-cage.linux-arm64-gnu.node +0 -0
- package/services/WasmCage/native/brud-wasm-cage.linux-x64-gnu.node +0 -0
- package/services/WasmCage/native/brud-wasm-cage.win32-x64-msvc.node +0 -0
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
import { ServiceContainer } from './ServiceContainer.js';
|
|
2
|
+
import { OperationRegistry } from './OperationRegistry.js';
|
|
3
|
+
import { ExecutionPipeline } from './ExecutionPipeline.js';
|
|
4
|
+
import { KernelRegistry } from './KernelRegistry.js';
|
|
5
|
+
import { KernelAccessBroker } from './KernelAccessBroker.js';
|
|
6
|
+
import { topologicalSort } from './dag.js';
|
|
7
|
+
import { LifecycleStateError } from './errors.js';
|
|
8
|
+
import { KERNEL_CALLER, KernelCallChain, UNIDENTIFIED_PARTICIPANT, } from './callChain.js';
|
|
9
|
+
/**
|
|
10
|
+
* Raised when a hardcore service declares a dependency on a composed service.
|
|
11
|
+
*
|
|
12
|
+
* Hardcore services initialize before composed services, so a hardcore service
|
|
13
|
+
* can never be satisfied by a composed prerequisite. The compiled hardcore list
|
|
14
|
+
* must therefore be self-contained.
|
|
15
|
+
*/
|
|
16
|
+
class HardcoreDependencyError extends Error {
|
|
17
|
+
code = 'HARDCORE_DEPENDENCY';
|
|
18
|
+
service;
|
|
19
|
+
dependency;
|
|
20
|
+
constructor(service, dependency) {
|
|
21
|
+
super(`Hardcore service "${service}" depends on composed service "${dependency}"`);
|
|
22
|
+
this.name = 'HardcoreDependencyError';
|
|
23
|
+
this.service = service;
|
|
24
|
+
this.dependency = dependency;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export class BrudKernel {
|
|
28
|
+
services;
|
|
29
|
+
operations;
|
|
30
|
+
pipeline;
|
|
31
|
+
maxCallDepth;
|
|
32
|
+
registry;
|
|
33
|
+
accessBroker;
|
|
34
|
+
initializedServices = [];
|
|
35
|
+
state = 'UNINITIALIZED';
|
|
36
|
+
runningHooksPending = false;
|
|
37
|
+
/**
|
|
38
|
+
* The host-supplied options bag, stored once at boot.
|
|
39
|
+
*
|
|
40
|
+
* The kernel treats it as opaque: it is attached unchanged to every service
|
|
41
|
+
* and execution context and is never indexed here.
|
|
42
|
+
*/
|
|
43
|
+
bootOptions = {};
|
|
44
|
+
bootPinner;
|
|
45
|
+
bootHardcore;
|
|
46
|
+
contextContract;
|
|
47
|
+
/** The kernel's call chain: origins, callers, limits, and observations. */
|
|
48
|
+
calls;
|
|
49
|
+
/**
|
|
50
|
+
* Tokens the compiled hardcore list supplies to the boot loader.
|
|
51
|
+
*
|
|
52
|
+
* The engine uses this only to partition the initialization order. It never
|
|
53
|
+
* infers hardcoredness from a descriptor's token or tier.
|
|
54
|
+
*/
|
|
55
|
+
hardcoreTokens;
|
|
56
|
+
constructor(options) {
|
|
57
|
+
this.bootPinner = options?.bootPinner;
|
|
58
|
+
this.bootHardcore = options?.bootHardcore;
|
|
59
|
+
this.contextContract = options?.contextContract;
|
|
60
|
+
this.hardcoreTokens = new Set(options?.hardcoreTokens ?? []);
|
|
61
|
+
this.services = new ServiceContainer();
|
|
62
|
+
this.operations = new OperationRegistry();
|
|
63
|
+
this.pipeline = new ExecutionPipeline(this.services);
|
|
64
|
+
this.maxCallDepth = options?.maxCallDepth ?? 5;
|
|
65
|
+
this.registry = new KernelRegistry();
|
|
66
|
+
this.calls = new KernelCallChain((tokenId) => this.registry.getInstance(tokenId), options?.callLimits);
|
|
67
|
+
this.accessBroker = new KernelAccessBroker(this.registry, () => this.state, this.calls);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Runs an operation inside a host-origin call chain.
|
|
71
|
+
*
|
|
72
|
+
* @param session - The host session's declaration.
|
|
73
|
+
* @param operation - Work performed inside the chain.
|
|
74
|
+
*/
|
|
75
|
+
runHostCall(session, operation) {
|
|
76
|
+
return this.calls.runHost(session, operation);
|
|
77
|
+
}
|
|
78
|
+
/** Reads the kernel's observation stream. Only the kernel's holder can. */
|
|
79
|
+
readObservations() {
|
|
80
|
+
return this.calls.observations();
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Appends one record to the observation stream.
|
|
84
|
+
*
|
|
85
|
+
* Used by the kernel's own boot loader; participants never hold the kernel.
|
|
86
|
+
*/
|
|
87
|
+
recordObservation(source, event, detail) {
|
|
88
|
+
this.calls.observe(source, event, detail);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The root origin and caller under which the kernel runs one service's
|
|
92
|
+
* lifecycle work.
|
|
93
|
+
*
|
|
94
|
+
* A hardcore service is the kernel's own root primitive and runs in a
|
|
95
|
+
* kernel-origin chain. Every other participant runs in a runtime-origin chain
|
|
96
|
+
* as itself.
|
|
97
|
+
*/
|
|
98
|
+
frameFor(tokenId) {
|
|
99
|
+
if (this.hardcoreTokens.has(tokenId)) {
|
|
100
|
+
return { origin: 'kernel', caller: KERNEL_CALLER };
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
origin: 'runtime',
|
|
104
|
+
caller: this.calls.callerForToken(tokenId) ?? UNIDENTIFIED_PARTICIPANT,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/** Runs one service's lifecycle work inside its own frame. */
|
|
108
|
+
runFor(tokenId, operation) {
|
|
109
|
+
const frame = this.frameFor(tokenId);
|
|
110
|
+
return this.calls.runRoot(frame.origin, frame.caller, operation);
|
|
111
|
+
}
|
|
112
|
+
get lifecycleState() {
|
|
113
|
+
return this.state;
|
|
114
|
+
}
|
|
115
|
+
register(descriptor) {
|
|
116
|
+
if (this.state !== 'UNINITIALIZED') {
|
|
117
|
+
throw new LifecycleStateError('register', this.state, 'UNINITIALIZED');
|
|
118
|
+
}
|
|
119
|
+
this.registry.register(descriptor);
|
|
120
|
+
}
|
|
121
|
+
async boot(options) {
|
|
122
|
+
if (this.state !== 'UNINITIALIZED') {
|
|
123
|
+
throw new LifecycleStateError('boot', this.state, 'UNINITIALIZED');
|
|
124
|
+
}
|
|
125
|
+
// The host bag is validated before any service is constructed. Services
|
|
126
|
+
// declared their requirements during composition.
|
|
127
|
+
this.contextContract?.validate(options.options ?? {});
|
|
128
|
+
this.state = 'BOOTING';
|
|
129
|
+
this.registry.freeze();
|
|
130
|
+
try {
|
|
131
|
+
const ordered = topologicalSort(this.descriptorMap());
|
|
132
|
+
// One boot, two ordered partitions. The hardcore set comes only from the
|
|
133
|
+
// boot loader's compiled hardcore list. Filtering the topological order
|
|
134
|
+
// preserves dependency order inside each partition and establishes every
|
|
135
|
+
// hardcore service before any composed service is constructed. A hardcore
|
|
136
|
+
// service may never depend on a composed one, because that prerequisite
|
|
137
|
+
// could not exist yet.
|
|
138
|
+
const hardcoreOrder = ordered.filter((descriptor) => this.hardcoreTokens.has(descriptor.token.id));
|
|
139
|
+
const composedOrder = ordered.filter((descriptor) => !this.hardcoreTokens.has(descriptor.token.id));
|
|
140
|
+
for (const descriptor of hardcoreOrder) {
|
|
141
|
+
for (const dependency of descriptor.dependencies) {
|
|
142
|
+
if (!this.hardcoreTokens.has(dependency.id)) {
|
|
143
|
+
throw new HardcoreDependencyError(descriptor.token.id, dependency.id);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
this.initializedServices.length = 0;
|
|
148
|
+
this.bootOptions = options.options ?? {};
|
|
149
|
+
const construct = async (descriptor) => {
|
|
150
|
+
await this.runFor(descriptor.token.id, async () => {
|
|
151
|
+
const context = {
|
|
152
|
+
callerId: 'kernel',
|
|
153
|
+
tier: descriptor.tier,
|
|
154
|
+
options: this.bootOptions,
|
|
155
|
+
...(options.logger === undefined ? {} : { logger: options.logger }),
|
|
156
|
+
};
|
|
157
|
+
try {
|
|
158
|
+
const instance = descriptor.factory(context, this.accessBroker);
|
|
159
|
+
this.registry.setInstance(descriptor.token.id, instance);
|
|
160
|
+
if (typeof instance.initialize === 'function') {
|
|
161
|
+
await instance.initialize(this.accessBroker);
|
|
162
|
+
}
|
|
163
|
+
this.initializedServices.push({ tokenId: descriptor.token.id, instance });
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
tagFailingService(error, descriptor.token.id);
|
|
167
|
+
throw error;
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
};
|
|
171
|
+
for (const descriptor of hardcoreOrder) {
|
|
172
|
+
await construct(descriptor);
|
|
173
|
+
}
|
|
174
|
+
// Admission. The hardcore boot calls run once the hardcore partition is
|
|
175
|
+
// initialized and before any composed participant is constructed. They
|
|
176
|
+
// record each participant's identity, admit each participant's declared
|
|
177
|
+
// exposures from its manifest, and seal the registration window. No
|
|
178
|
+
// participant code has run yet.
|
|
179
|
+
const hardcore = this.bootHardcore;
|
|
180
|
+
if (hardcore !== undefined) {
|
|
181
|
+
const participants = await this.calls.runRoot('kernel', KERNEL_CALLER, () => hardcore(this.initializedServices.map((entry) => ({
|
|
182
|
+
tokenId: entry.tokenId,
|
|
183
|
+
instance: entry.instance,
|
|
184
|
+
})), this.calls));
|
|
185
|
+
if (participants !== undefined) {
|
|
186
|
+
this.calls.admitParticipants(participants);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
for (const descriptor of composedOrder) {
|
|
190
|
+
await construct(descriptor);
|
|
191
|
+
}
|
|
192
|
+
// Boot snapshot pinning. A module whose policy must be frozen from
|
|
193
|
+
// another participant's state registers a pinner; it runs here, while
|
|
194
|
+
// the broker's lifecycle gate is suspended for this one bounded
|
|
195
|
+
// operation, so the frozen policy exists before any caller can resolve
|
|
196
|
+
// the token. The hook is a no-op when no pinner was registered. Module
|
|
197
|
+
// hooks are participant code, so they run in a runtime-origin chain.
|
|
198
|
+
const pinner = this.bootPinner;
|
|
199
|
+
if (pinner !== undefined) {
|
|
200
|
+
await this.calls.runRoot('runtime', UNIDENTIFIED_PARTICIPANT, async () => {
|
|
201
|
+
await this.accessBroker.withLifecycleGateSuspended(async () => {
|
|
202
|
+
await pinner(this.initializedServices.map((entry) => entry.instance));
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
// The pin phase is the last point where a service may declare a field.
|
|
207
|
+
// Validate again so declarations made there are enforced too.
|
|
208
|
+
this.contextContract?.validate(this.bootOptions);
|
|
209
|
+
this.state = 'RUNNING';
|
|
210
|
+
this.contextContract?.lock();
|
|
211
|
+
}
|
|
212
|
+
catch (error) {
|
|
213
|
+
await this.rollbackInitialized();
|
|
214
|
+
this.state = 'DISPOSED';
|
|
215
|
+
throw error;
|
|
216
|
+
}
|
|
217
|
+
// Post-boot start. The broker now grants access, so services can resolve
|
|
218
|
+
// their dependencies. Hooks run one at a time in initialization order.
|
|
219
|
+
// Any failure rolls the whole kernel back: the broker stops granting,
|
|
220
|
+
// every initialized service is disposed in reverse order, and boot()
|
|
221
|
+
// rejects with the original error.
|
|
222
|
+
this.runningHooksPending = true;
|
|
223
|
+
try {
|
|
224
|
+
for (const { tokenId, instance } of this.initializedServices) {
|
|
225
|
+
if (typeof instance.onRunning === 'function') {
|
|
226
|
+
try {
|
|
227
|
+
await this.runFor(tokenId, () => instance.onRunning?.(this.accessBroker));
|
|
228
|
+
}
|
|
229
|
+
catch (error) {
|
|
230
|
+
tagFailingService(error, tokenId);
|
|
231
|
+
throw error;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
catch (error) {
|
|
237
|
+
this.state = 'SHUTTING_DOWN';
|
|
238
|
+
await this.rollbackInitialized();
|
|
239
|
+
this.state = 'DISPOSED';
|
|
240
|
+
throw error;
|
|
241
|
+
}
|
|
242
|
+
finally {
|
|
243
|
+
this.runningHooksPending = false;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
async shutdown() {
|
|
247
|
+
if (this.runningHooksPending) {
|
|
248
|
+
// Disposing services while onRunning hooks are still starting them would
|
|
249
|
+
// tear down dependencies underneath an in-flight activation.
|
|
250
|
+
throw new LifecycleStateError('shutdown while onRunning hooks are pending', this.state);
|
|
251
|
+
}
|
|
252
|
+
if (this.state !== 'RUNNING') {
|
|
253
|
+
throw new LifecycleStateError('shutdown', this.state, 'RUNNING');
|
|
254
|
+
}
|
|
255
|
+
this.state = 'SHUTTING_DOWN';
|
|
256
|
+
let firstError;
|
|
257
|
+
const pending = this.initializedServices.splice(0).reverse();
|
|
258
|
+
for (const { instance } of pending) {
|
|
259
|
+
try {
|
|
260
|
+
await instance.dispose?.();
|
|
261
|
+
}
|
|
262
|
+
catch (error) {
|
|
263
|
+
if (firstError === undefined) {
|
|
264
|
+
firstError = error;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
this.registry.clearInstances();
|
|
269
|
+
this.calls.clearParticipants();
|
|
270
|
+
this.state = 'DISPOSED';
|
|
271
|
+
if (firstError !== undefined) {
|
|
272
|
+
throw firstError;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
getContextSchema() {
|
|
276
|
+
if (this.contextContract !== undefined) {
|
|
277
|
+
return this.contextContract.schema();
|
|
278
|
+
}
|
|
279
|
+
return { locked: false, fields: [] };
|
|
280
|
+
}
|
|
281
|
+
validateContextOptions(options) {
|
|
282
|
+
this.contextContract?.validate(options);
|
|
283
|
+
}
|
|
284
|
+
requestAccess(caller, token) {
|
|
285
|
+
return this.accessBroker.requestAccess(caller, token);
|
|
286
|
+
}
|
|
287
|
+
hasService(token) {
|
|
288
|
+
return this.accessBroker.hasService(token);
|
|
289
|
+
}
|
|
290
|
+
listServices() {
|
|
291
|
+
return this.accessBroker.listServices();
|
|
292
|
+
}
|
|
293
|
+
descriptorMap() {
|
|
294
|
+
return new Map(this.registry.listDescriptors().map((descriptor) => [descriptor.token.id, descriptor]));
|
|
295
|
+
}
|
|
296
|
+
async rollbackInitialized() {
|
|
297
|
+
const pending = this.initializedServices.splice(0).reverse();
|
|
298
|
+
for (const { instance } of pending) {
|
|
299
|
+
try {
|
|
300
|
+
await instance.dispose?.();
|
|
301
|
+
}
|
|
302
|
+
catch {
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
this.registry.clearInstances();
|
|
307
|
+
this.calls.clearParticipants();
|
|
308
|
+
}
|
|
309
|
+
async execute(kind, input, options) {
|
|
310
|
+
const handler = this.operations.get(kind);
|
|
311
|
+
if (!handler) {
|
|
312
|
+
return {
|
|
313
|
+
status: "failed",
|
|
314
|
+
error: `Unknown operation kind: "${kind}"`,
|
|
315
|
+
durationMs: 0,
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
const context = this.createExecutionContext(options);
|
|
319
|
+
return this.pipeline.execute(handler, context, input);
|
|
320
|
+
}
|
|
321
|
+
createExecutionContext(options) {
|
|
322
|
+
const kernel = this;
|
|
323
|
+
const effectiveMaxDepth = options?.maxCallDepth ?? this.maxCallDepth;
|
|
324
|
+
const sessionId = crypto.randomUUID();
|
|
325
|
+
async function invokeImpl(kind, input) {
|
|
326
|
+
const nextDepth = this.callDepth + 1;
|
|
327
|
+
if (nextDepth > effectiveMaxDepth) {
|
|
328
|
+
return {
|
|
329
|
+
status: "failed",
|
|
330
|
+
error: `Maximum call depth (${effectiveMaxDepth}) exceeded`,
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
const childCtx = this.createChildContext({ callDepth: nextDepth });
|
|
334
|
+
const handler = kernel.operations.get(kind);
|
|
335
|
+
if (!handler) {
|
|
336
|
+
return {
|
|
337
|
+
status: "failed",
|
|
338
|
+
error: `Unknown operation kind: "${kind}"`,
|
|
339
|
+
durationMs: 0,
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
return kernel.pipeline.execute(handler, childCtx, input);
|
|
343
|
+
}
|
|
344
|
+
function createChildContextImpl(overrides) {
|
|
345
|
+
return {
|
|
346
|
+
sessionId: overrides?.sessionId ?? this.sessionId,
|
|
347
|
+
options: overrides?.options ?? this.options,
|
|
348
|
+
signal: overrides?.signal ?? this.signal,
|
|
349
|
+
callDepth: overrides?.callDepth ?? this.callDepth,
|
|
350
|
+
metadata: overrides?.metadata ?? this.metadata,
|
|
351
|
+
resolve: overrides?.resolve ?? this.resolve,
|
|
352
|
+
invoke: overrides?.invoke ?? this.invoke,
|
|
353
|
+
createChildContext: overrides?.createChildContext ?? this.createChildContext,
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
const ctx = {
|
|
357
|
+
sessionId,
|
|
358
|
+
options: this.bootOptions,
|
|
359
|
+
signal: options?.signal,
|
|
360
|
+
callDepth: 0,
|
|
361
|
+
metadata: options?.metadata,
|
|
362
|
+
resolve: (token) => kernel.services.resolve(token),
|
|
363
|
+
invoke: invokeImpl,
|
|
364
|
+
createChildContext: createChildContextImpl,
|
|
365
|
+
};
|
|
366
|
+
return ctx;
|
|
367
|
+
}
|
|
368
|
+
generatePromptManifest() {
|
|
369
|
+
const operations = this.operations
|
|
370
|
+
.list()
|
|
371
|
+
.filter((h) => h.promptDescriptor !== undefined)
|
|
372
|
+
.map((h) => ({
|
|
373
|
+
kind: h.kind,
|
|
374
|
+
promptDescriptor: h.promptDescriptor,
|
|
375
|
+
}));
|
|
376
|
+
return { operations };
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Records, on an error thrown by a service's own lifecycle code, which service
|
|
381
|
+
* threw it. The mark is not enumerable and never replaces an earlier one, so
|
|
382
|
+
* the error keeps its type and message. A caller that must decide what to do
|
|
383
|
+
* about a failed boot reads it with `failingServiceOf`.
|
|
384
|
+
*/
|
|
385
|
+
function tagFailingService(error, tokenId) {
|
|
386
|
+
if (error !== null && typeof error === 'object' && !(FAILING_SERVICE in error)) {
|
|
387
|
+
Object.defineProperty(error, FAILING_SERVICE, { value: tokenId, enumerable: false });
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
const FAILING_SERVICE = Symbol.for('brud.kernel.failingService');
|
|
391
|
+
/** The token of the service whose lifecycle code threw `error`, when the kernel recorded it. */
|
|
392
|
+
export function failingServiceOf(error) {
|
|
393
|
+
const value = error !== null && typeof error === 'object' ? error[FAILING_SERVICE] : undefined;
|
|
394
|
+
return typeof value === 'string' ? value : undefined;
|
|
395
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ExecutionContext } from '../contracts/context.js';
|
|
2
|
+
import type { OperationHandler, OperationResult } from '../contracts/handler.js';
|
|
3
|
+
import type { Middleware, IExecutionPipeline } from '../contracts/pipeline.js';
|
|
4
|
+
import type { IServiceContainer } from '../contracts/container.js';
|
|
5
|
+
export declare class ExecutionPipeline implements IExecutionPipeline {
|
|
6
|
+
private readonly middlewares;
|
|
7
|
+
private readonly serviceContainer;
|
|
8
|
+
constructor(serviceContainer: IServiceContainer);
|
|
9
|
+
use(middleware: Middleware): void;
|
|
10
|
+
execute<TInput, TOutput>(handler: OperationHandler<TInput, TOutput>, context: ExecutionContext, input: TInput): Promise<OperationResult<TOutput>>;
|
|
11
|
+
private verifyCapabilities;
|
|
12
|
+
private compose;
|
|
13
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export class ExecutionPipeline {
|
|
2
|
+
middlewares = [];
|
|
3
|
+
serviceContainer;
|
|
4
|
+
constructor(serviceContainer) {
|
|
5
|
+
this.serviceContainer = serviceContainer;
|
|
6
|
+
}
|
|
7
|
+
use(middleware) {
|
|
8
|
+
this.middlewares.push(middleware);
|
|
9
|
+
}
|
|
10
|
+
async execute(handler, context, input) {
|
|
11
|
+
const start = performance.now();
|
|
12
|
+
const missing = this.verifyCapabilities(handler);
|
|
13
|
+
if (missing) {
|
|
14
|
+
return {
|
|
15
|
+
status: "failed",
|
|
16
|
+
error: `Missing required capability: ${missing.description ?? String(missing)}`,
|
|
17
|
+
durationMs: 0,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
const composed = this.compose(handler);
|
|
22
|
+
const enhancedContext = {
|
|
23
|
+
...context,
|
|
24
|
+
metadata: { ...context.metadata, operationKind: handler.kind },
|
|
25
|
+
};
|
|
26
|
+
const result = await composed(enhancedContext, input);
|
|
27
|
+
result.durationMs = performance.now() - start;
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
return {
|
|
32
|
+
status: "failed",
|
|
33
|
+
error: err instanceof Error ? err : String(err),
|
|
34
|
+
durationMs: performance.now() - start,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
verifyCapabilities(handler) {
|
|
39
|
+
if (!handler.requiredCapabilities) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
for (const token of handler.requiredCapabilities) {
|
|
43
|
+
if (!this.serviceContainer.has(token)) {
|
|
44
|
+
return token;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
compose(handler) {
|
|
50
|
+
let runner = (ctx, inp) => handler.execute(ctx, inp);
|
|
51
|
+
for (let i = this.middlewares.length - 1; i >= 0; i--) {
|
|
52
|
+
const mw = this.middlewares[i];
|
|
53
|
+
const next = runner;
|
|
54
|
+
runner = async (context, input) => {
|
|
55
|
+
const nextWrapper = () => next(context, input);
|
|
56
|
+
const mwResult = await mw(context, input, nextWrapper);
|
|
57
|
+
return mwResult;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
return runner;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { IKernelAccessBroker, ServiceInfo } from '../contracts/accessBroker.js';
|
|
2
|
+
import type { ServiceContext } from '../contracts/lifecycle.js';
|
|
3
|
+
import type { KernelCallContext } from '../contracts/callChain.js';
|
|
4
|
+
import type { ServiceToken } from '../contracts/serviceToken.js';
|
|
5
|
+
import type { KernelRegistry } from './KernelRegistry.js';
|
|
6
|
+
import type { KernelLifecycleState } from './state.js';
|
|
7
|
+
export type KernelStateChecker = () => KernelLifecycleState;
|
|
8
|
+
/** The call-chain view the broker binds token access to. */
|
|
9
|
+
export interface KernelAccessAuthority {
|
|
10
|
+
current(): KernelCallContext | undefined;
|
|
11
|
+
tokenIdOf(identity: string): string | undefined;
|
|
12
|
+
callerForToken(tokenId: string): unknown;
|
|
13
|
+
}
|
|
14
|
+
export declare class KernelAccessBroker implements IKernelAccessBroker {
|
|
15
|
+
private readonly registry;
|
|
16
|
+
private readonly readState;
|
|
17
|
+
private paused;
|
|
18
|
+
private readonly authority;
|
|
19
|
+
constructor(registry: KernelRegistry, readState: KernelStateChecker, authority?: KernelAccessAuthority);
|
|
20
|
+
/**
|
|
21
|
+
* Binds the asserted caller id to the active call chain.
|
|
22
|
+
*
|
|
23
|
+
* - Kernel frames (hardcores, kernel work): trusted as before.
|
|
24
|
+
* - Participant frames with a kernel-issued identity: the caller id must be
|
|
25
|
+
* the participant's own token id or kernel identity; anything else is
|
|
26
|
+
* refused. Unidentified frames (no identity registry) are unchanged.
|
|
27
|
+
* - Host frames: may not claim `kernel` or a live participant.
|
|
28
|
+
* - No active chain (host process code outside any chain): unchanged.
|
|
29
|
+
*/
|
|
30
|
+
private assertCallerBinding;
|
|
31
|
+
/**
|
|
32
|
+
* Suspends the lifecycle gate for a bounded boot-window operation.
|
|
33
|
+
*
|
|
34
|
+
* The kernel composition uses this around the boot pinner so any service
|
|
35
|
+
* module's `pinBootPolicies` hook can ingest its boot snapshot before the
|
|
36
|
+
* kernel announces RUNNING. The rule is general: it is available to every
|
|
37
|
+
* participant on the same terms. Access policies, denial lists, and
|
|
38
|
+
* allowlists still apply, so a pause cannot widen anyone's authority; it
|
|
39
|
+
* only removes the "broker is idle" guard.
|
|
40
|
+
*
|
|
41
|
+
* @param operation - Work performed while the gate is suspended.
|
|
42
|
+
*/
|
|
43
|
+
withLifecycleGateSuspended<T>(operation: () => Promise<T>): Promise<T>;
|
|
44
|
+
requestAccess<TService>(caller: ServiceContext, token: ServiceToken<TService>): TService;
|
|
45
|
+
hasService(token: ServiceToken<unknown>): boolean;
|
|
46
|
+
listServices(): readonly ServiceInfo[];
|
|
47
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { AllowlistViolationError, BlacklistedAccessViolationError, CallerIdentityMismatchError, } from '../contracts/policy.js';
|
|
2
|
+
import { LifecycleStateError, ServiceNotFoundError } from './errors.js';
|
|
3
|
+
function matchesCaller(entry, callerId) {
|
|
4
|
+
return typeof entry === 'string' ? entry === callerId : entry.id === callerId;
|
|
5
|
+
}
|
|
6
|
+
export class KernelAccessBroker {
|
|
7
|
+
registry;
|
|
8
|
+
readState;
|
|
9
|
+
paused = false;
|
|
10
|
+
authority;
|
|
11
|
+
constructor(registry, readState, authority) {
|
|
12
|
+
this.registry = registry;
|
|
13
|
+
this.readState = readState;
|
|
14
|
+
this.authority = authority;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Binds the asserted caller id to the active call chain.
|
|
18
|
+
*
|
|
19
|
+
* - Kernel frames (hardcores, kernel work): trusted as before.
|
|
20
|
+
* - Participant frames with a kernel-issued identity: the caller id must be
|
|
21
|
+
* the participant's own token id or kernel identity; anything else is
|
|
22
|
+
* refused. Unidentified frames (no identity registry) are unchanged.
|
|
23
|
+
* - Host frames: may not claim `kernel` or a live participant.
|
|
24
|
+
* - No active chain (host process code outside any chain): unchanged.
|
|
25
|
+
*/
|
|
26
|
+
assertCallerBinding(caller, targetServiceId) {
|
|
27
|
+
const context = this.authority?.current();
|
|
28
|
+
if (context === undefined || context.caller.callerClass === 'kernel') {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (context.caller.callerClass === 'participant') {
|
|
32
|
+
const identity = context.caller.identity;
|
|
33
|
+
// No kernel-issued identity in this composition (no identity registry):
|
|
34
|
+
// there is nothing to bind to, so the asserted id is taken as before.
|
|
35
|
+
if (identity === null) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const bound = this.authority?.tokenIdOf(identity);
|
|
39
|
+
if (bound === undefined || (caller.callerId !== bound && caller.callerId !== identity)) {
|
|
40
|
+
throw new CallerIdentityMismatchError(caller.callerId, targetServiceId, bound);
|
|
41
|
+
}
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (caller.callerId === 'kernel' || this.authority?.callerForToken(caller.callerId) !== undefined) {
|
|
45
|
+
throw new CallerIdentityMismatchError(caller.callerId, targetServiceId, undefined);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Suspends the lifecycle gate for a bounded boot-window operation.
|
|
50
|
+
*
|
|
51
|
+
* The kernel composition uses this around the boot pinner so any service
|
|
52
|
+
* module's `pinBootPolicies` hook can ingest its boot snapshot before the
|
|
53
|
+
* kernel announces RUNNING. The rule is general: it is available to every
|
|
54
|
+
* participant on the same terms. Access policies, denial lists, and
|
|
55
|
+
* allowlists still apply, so a pause cannot widen anyone's authority; it
|
|
56
|
+
* only removes the "broker is idle" guard.
|
|
57
|
+
*
|
|
58
|
+
* @param operation - Work performed while the gate is suspended.
|
|
59
|
+
*/
|
|
60
|
+
async withLifecycleGateSuspended(operation) {
|
|
61
|
+
this.paused = true;
|
|
62
|
+
try {
|
|
63
|
+
return await operation();
|
|
64
|
+
}
|
|
65
|
+
finally {
|
|
66
|
+
this.paused = false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
requestAccess(caller, token) {
|
|
70
|
+
const state = this.readState();
|
|
71
|
+
if (state !== 'RUNNING' && !this.paused) {
|
|
72
|
+
throw new LifecycleStateError('requestAccess', state, 'RUNNING');
|
|
73
|
+
}
|
|
74
|
+
const descriptor = this.registry.getDescriptor(token.id);
|
|
75
|
+
if (descriptor === undefined) {
|
|
76
|
+
throw new ServiceNotFoundError(token.id);
|
|
77
|
+
}
|
|
78
|
+
const instance = this.registry.getInstance(token.id);
|
|
79
|
+
if (instance === undefined) {
|
|
80
|
+
throw new ServiceNotFoundError(token.id);
|
|
81
|
+
}
|
|
82
|
+
this.assertCallerBinding(caller, token.id);
|
|
83
|
+
const policy = descriptor.accessPolicy;
|
|
84
|
+
if (policy?.deniedCallers?.some((entry) => matchesCaller(entry, caller.callerId))) {
|
|
85
|
+
throw new BlacklistedAccessViolationError(caller.callerId, token.id, 'caller is explicitly denied by the service access policy');
|
|
86
|
+
}
|
|
87
|
+
if (policy?.allowedCallers !== undefined
|
|
88
|
+
&& !policy.allowedCallers.some((entry) => matchesCaller(entry, caller.callerId))) {
|
|
89
|
+
throw new AllowlistViolationError(caller.callerId, token.id);
|
|
90
|
+
}
|
|
91
|
+
if (descriptor.project !== undefined) {
|
|
92
|
+
return descriptor.project(instance, caller);
|
|
93
|
+
}
|
|
94
|
+
return instance;
|
|
95
|
+
}
|
|
96
|
+
hasService(token) {
|
|
97
|
+
return this.registry.getDescriptor(token.id) !== undefined;
|
|
98
|
+
}
|
|
99
|
+
listServices() {
|
|
100
|
+
return this.registry.listDescriptors().map((descriptor) => ({
|
|
101
|
+
id: descriptor.token.id,
|
|
102
|
+
description: descriptor.token.description,
|
|
103
|
+
tier: descriptor.tier,
|
|
104
|
+
initialized: this.registry.getInstance(descriptor.token.id) !== undefined,
|
|
105
|
+
}));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ServiceDescriptor } from '../contracts/serviceDescriptor.js';
|
|
2
|
+
export declare class KernelRegistry {
|
|
3
|
+
private readonly descriptors;
|
|
4
|
+
private readonly instances;
|
|
5
|
+
private frozen;
|
|
6
|
+
register<TService, TInternal extends TService = TService>(descriptor: ServiceDescriptor<TService, TInternal>): void;
|
|
7
|
+
getDescriptor(tokenId: string): ServiceDescriptor<unknown> | undefined;
|
|
8
|
+
getInstance(tokenId: string): unknown | undefined;
|
|
9
|
+
setInstance(tokenId: string, instance: unknown): void;
|
|
10
|
+
listDescriptors(): readonly ServiceDescriptor<unknown>[];
|
|
11
|
+
freeze(): void;
|
|
12
|
+
get isFrozen(): boolean;
|
|
13
|
+
clearInstances(): void;
|
|
14
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { LifecycleStateError } from './errors.js';
|
|
2
|
+
export class KernelRegistry {
|
|
3
|
+
descriptors = new Map();
|
|
4
|
+
instances = new Map();
|
|
5
|
+
frozen = false;
|
|
6
|
+
register(descriptor) {
|
|
7
|
+
if (this.frozen) {
|
|
8
|
+
throw new LifecycleStateError('register service after the registry was frozen');
|
|
9
|
+
}
|
|
10
|
+
this.descriptors.set(descriptor.token.id, descriptor);
|
|
11
|
+
}
|
|
12
|
+
getDescriptor(tokenId) {
|
|
13
|
+
return this.descriptors.get(tokenId);
|
|
14
|
+
}
|
|
15
|
+
getInstance(tokenId) {
|
|
16
|
+
return this.instances.get(tokenId);
|
|
17
|
+
}
|
|
18
|
+
setInstance(tokenId, instance) {
|
|
19
|
+
this.instances.set(tokenId, instance);
|
|
20
|
+
}
|
|
21
|
+
listDescriptors() {
|
|
22
|
+
return Array.from(this.descriptors.values());
|
|
23
|
+
}
|
|
24
|
+
freeze() {
|
|
25
|
+
this.frozen = true;
|
|
26
|
+
}
|
|
27
|
+
get isFrozen() {
|
|
28
|
+
return this.frozen;
|
|
29
|
+
}
|
|
30
|
+
clearInstances() {
|
|
31
|
+
this.instances.clear();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { OperationHandler } from '../contracts/handler.js';
|
|
2
|
+
import type { IOperationRegistry } from '../contracts/registry.js';
|
|
3
|
+
export declare class OperationRegistry implements IOperationRegistry {
|
|
4
|
+
private readonly handlers;
|
|
5
|
+
register(handler: OperationHandler): void;
|
|
6
|
+
get<TInput = unknown, TOutput = unknown>(kind: string): OperationHandler<TInput, TOutput> | undefined;
|
|
7
|
+
has(kind: string): boolean;
|
|
8
|
+
list(): readonly OperationHandler[];
|
|
9
|
+
}
|