@brud/kernel 0.0.0-stage → 0.0.1

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 (278) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +63 -2
  3. package/dist/index.d.ts +15 -0
  4. package/dist/index.js +13 -0
  5. package/dist/serviceModules/events.d.ts +1 -0
  6. package/dist/serviceModules/events.js +1 -0
  7. package/dist/serviceModules/exposedApiRegistry.d.ts +1 -0
  8. package/dist/serviceModules/exposedApiRegistry.js +1 -0
  9. package/dist/serviceModules/identityRegistry.d.ts +1 -0
  10. package/dist/serviceModules/identityRegistry.js +1 -0
  11. package/dist/serviceModules/interception.d.ts +1 -0
  12. package/dist/serviceModules/interception.js +1 -0
  13. package/dist/serviceModules/parser.d.ts +1 -0
  14. package/dist/serviceModules/parser.js +1 -0
  15. package/dist/serviceModules/router.d.ts +1 -0
  16. package/dist/serviceModules/router.js +1 -0
  17. package/dist/serviceModules/wasmCage.d.ts +1 -0
  18. package/dist/serviceModules/wasmCage.js +1 -0
  19. package/dist/services/Events/contracts/IEventsService.d.ts +64 -0
  20. package/dist/services/Events/contracts/IEventsService.js +1 -0
  21. package/dist/services/Events/contracts/errors.d.ts +31 -0
  22. package/dist/services/Events/contracts/errors.js +40 -0
  23. package/dist/services/Events/contracts/index.d.ts +3 -0
  24. package/dist/services/Events/contracts/index.js +3 -0
  25. package/dist/services/Events/contracts/tokens.d.ts +2 -0
  26. package/dist/services/Events/contracts/tokens.js +2 -0
  27. package/dist/services/Events/descriptor/EventsDescriptor.d.ts +9 -0
  28. package/dist/services/Events/descriptor/EventsDescriptor.js +56 -0
  29. package/dist/services/Events/descriptor/tokens.d.ts +1 -0
  30. package/dist/services/Events/descriptor/tokens.js +1 -0
  31. package/dist/services/Events/index.d.ts +4 -0
  32. package/dist/services/Events/index.js +4 -0
  33. package/dist/services/Events/orchestrator/EventsService.d.ts +51 -0
  34. package/dist/services/Events/orchestrator/EventsService.js +339 -0
  35. package/dist/services/Events/orchestrator/index.d.ts +2 -0
  36. package/dist/services/Events/orchestrator/index.js +2 -0
  37. package/dist/services/Events/orchestrator/limits.d.ts +22 -0
  38. package/dist/services/Events/orchestrator/limits.js +33 -0
  39. package/dist/services/Events/serviceModule.d.ts +2 -0
  40. package/dist/services/Events/serviceModule.js +5 -0
  41. package/dist/services/ExposedApiRegistry/contracts/IExposedApiRegistry.d.ts +44 -0
  42. package/dist/services/ExposedApiRegistry/contracts/IExposedApiRegistry.js +1 -0
  43. package/dist/services/ExposedApiRegistry/contracts/errors.d.ts +132 -0
  44. package/dist/services/ExposedApiRegistry/contracts/errors.js +195 -0
  45. package/dist/services/ExposedApiRegistry/contracts/index.d.ts +9 -0
  46. package/dist/services/ExposedApiRegistry/contracts/index.js +4 -0
  47. package/dist/services/ExposedApiRegistry/contracts/policy.d.ts +90 -0
  48. package/dist/services/ExposedApiRegistry/contracts/policy.js +169 -0
  49. package/dist/services/ExposedApiRegistry/contracts/registrant.d.ts +37 -0
  50. package/dist/services/ExposedApiRegistry/contracts/registrant.js +36 -0
  51. package/dist/services/ExposedApiRegistry/contracts/types.d.ts +112 -0
  52. package/dist/services/ExposedApiRegistry/contracts/types.js +73 -0
  53. package/dist/services/ExposedApiRegistry/descriptor/ExposedApiRegistryDescriptor.d.ts +44 -0
  54. package/dist/services/ExposedApiRegistry/descriptor/ExposedApiRegistryDescriptor.js +58 -0
  55. package/dist/services/ExposedApiRegistry/descriptor/tokens.d.ts +8 -0
  56. package/dist/services/ExposedApiRegistry/descriptor/tokens.js +8 -0
  57. package/dist/services/ExposedApiRegistry/index.d.ts +6 -0
  58. package/dist/services/ExposedApiRegistry/index.js +5 -0
  59. package/dist/services/ExposedApiRegistry/orchestrator/ExposedApiRegistryService.d.ts +61 -0
  60. package/dist/services/ExposedApiRegistry/orchestrator/ExposedApiRegistryService.js +407 -0
  61. package/dist/services/ExposedApiRegistry/orchestrator/index.d.ts +2 -0
  62. package/dist/services/ExposedApiRegistry/orchestrator/index.js +1 -0
  63. package/dist/services/ExposedApiRegistry/serviceModule.d.ts +10 -0
  64. package/dist/services/ExposedApiRegistry/serviceModule.js +20 -0
  65. package/dist/services/ExposedApiRegistry/storage/protection.d.ts +21 -0
  66. package/dist/services/ExposedApiRegistry/storage/protection.js +35 -0
  67. package/dist/services/ExposedApiRegistry/storage/scopeRecord.d.ts +45 -0
  68. package/dist/services/ExposedApiRegistry/storage/scopeRecord.js +178 -0
  69. package/dist/services/IdentityRegistry/contracts/errors.d.ts +88 -0
  70. package/dist/services/IdentityRegistry/contracts/errors.js +128 -0
  71. package/dist/services/IdentityRegistry/contracts/index.d.ts +5 -0
  72. package/dist/services/IdentityRegistry/contracts/index.js +3 -0
  73. package/dist/services/IdentityRegistry/contracts/types.d.ts +77 -0
  74. package/dist/services/IdentityRegistry/contracts/types.js +18 -0
  75. package/dist/services/IdentityRegistry/descriptor/descriptor.d.ts +36 -0
  76. package/dist/services/IdentityRegistry/descriptor/descriptor.js +44 -0
  77. package/dist/services/IdentityRegistry/descriptor/tokens.d.ts +8 -0
  78. package/dist/services/IdentityRegistry/descriptor/tokens.js +8 -0
  79. package/dist/services/IdentityRegistry/index.d.ts +11 -0
  80. package/dist/services/IdentityRegistry/index.js +6 -0
  81. package/dist/services/IdentityRegistry/orchestrator/IdentityRegistryService.d.ts +47 -0
  82. package/dist/services/IdentityRegistry/orchestrator/IdentityRegistryService.js +240 -0
  83. package/dist/services/IdentityRegistry/serviceModule.d.ts +10 -0
  84. package/dist/services/IdentityRegistry/serviceModule.js +24 -0
  85. package/dist/services/IdentityRegistry/storage/registryDocument.d.ts +55 -0
  86. package/dist/services/IdentityRegistry/storage/registryDocument.js +235 -0
  87. package/dist/services/Interception/contracts/IInterceptionService.d.ts +65 -0
  88. package/dist/services/Interception/contracts/IInterceptionService.js +1 -0
  89. package/dist/services/Interception/contracts/errors.d.ts +26 -0
  90. package/dist/services/Interception/contracts/errors.js +35 -0
  91. package/dist/services/Interception/contracts/index.d.ts +3 -0
  92. package/dist/services/Interception/contracts/index.js +3 -0
  93. package/dist/services/Interception/contracts/tokens.d.ts +2 -0
  94. package/dist/services/Interception/contracts/tokens.js +2 -0
  95. package/dist/services/Interception/descriptor/InterceptionDescriptor.d.ts +9 -0
  96. package/dist/services/Interception/descriptor/InterceptionDescriptor.js +49 -0
  97. package/dist/services/Interception/descriptor/tokens.d.ts +1 -0
  98. package/dist/services/Interception/descriptor/tokens.js +1 -0
  99. package/dist/services/Interception/index.d.ts +4 -0
  100. package/dist/services/Interception/index.js +4 -0
  101. package/dist/services/Interception/orchestrator/InterceptionService.d.ts +32 -0
  102. package/dist/services/Interception/orchestrator/InterceptionService.js +180 -0
  103. package/dist/services/Interception/orchestrator/index.d.ts +1 -0
  104. package/dist/services/Interception/orchestrator/index.js +1 -0
  105. package/dist/services/Interception/serviceModule.d.ts +2 -0
  106. package/dist/services/Interception/serviceModule.js +5 -0
  107. package/dist/services/Parser/contracts/IParserService.d.ts +21 -0
  108. package/dist/services/Parser/contracts/IParserService.js +1 -0
  109. package/dist/services/Parser/contracts/errors.d.ts +25 -0
  110. package/dist/services/Parser/contracts/errors.js +33 -0
  111. package/dist/services/Parser/contracts/index.d.ts +4 -0
  112. package/dist/services/Parser/contracts/index.js +1 -0
  113. package/dist/services/Parser/contracts/types.d.ts +42 -0
  114. package/dist/services/Parser/contracts/types.js +1 -0
  115. package/dist/services/Parser/descriptor/ParserDescriptor.d.ts +23 -0
  116. package/dist/services/Parser/descriptor/ParserDescriptor.js +50 -0
  117. package/dist/services/Parser/descriptor/tokens.d.ts +8 -0
  118. package/dist/services/Parser/descriptor/tokens.js +8 -0
  119. package/dist/services/Parser/index.d.ts +8 -0
  120. package/dist/services/Parser/index.js +8 -0
  121. package/dist/services/Parser/orchestrator/ParserService.d.ts +20 -0
  122. package/dist/services/Parser/orchestrator/ParserService.js +105 -0
  123. package/dist/services/Parser/orchestrator/index.d.ts +1 -0
  124. package/dist/services/Parser/orchestrator/index.js +1 -0
  125. package/dist/services/Parser/parser/indexSynthesis.d.ts +11 -0
  126. package/dist/services/Parser/parser/indexSynthesis.js +13 -0
  127. package/dist/services/Parser/parser/parameters.d.ts +14 -0
  128. package/dist/services/Parser/parser/parameters.js +89 -0
  129. package/dist/services/Parser/parser/scanner.d.ts +48 -0
  130. package/dist/services/Parser/parser/scanner.js +156 -0
  131. package/dist/services/Parser/parser/syntax.d.ts +30 -0
  132. package/dist/services/Parser/parser/syntax.js +34 -0
  133. package/dist/services/Parser/serviceModule.d.ts +2 -0
  134. package/dist/services/Parser/serviceModule.js +5 -0
  135. package/dist/services/Router/bootOptions.d.ts +2 -0
  136. package/dist/services/Router/bootOptions.js +2 -0
  137. package/dist/services/Router/contracts/IRouterService.d.ts +43 -0
  138. package/dist/services/Router/contracts/IRouterService.js +1 -0
  139. package/dist/services/Router/contracts/caller.d.ts +6 -0
  140. package/dist/services/Router/contracts/caller.js +6 -0
  141. package/dist/services/Router/contracts/errors.d.ts +56 -0
  142. package/dist/services/Router/contracts/errors.js +75 -0
  143. package/dist/services/Router/contracts/index.d.ts +8 -0
  144. package/dist/services/Router/contracts/index.js +4 -0
  145. package/dist/services/Router/contracts/kind.d.ts +7 -0
  146. package/dist/services/Router/contracts/kind.js +10 -0
  147. package/dist/services/Router/contracts/registrant.d.ts +23 -0
  148. package/dist/services/Router/contracts/registrant.js +13 -0
  149. package/dist/services/Router/contracts/types.d.ts +90 -0
  150. package/dist/services/Router/contracts/types.js +1 -0
  151. package/dist/services/Router/descriptor/RouterDescriptor.d.ts +32 -0
  152. package/dist/services/Router/descriptor/RouterDescriptor.js +92 -0
  153. package/dist/services/Router/descriptor/tokens.d.ts +9 -0
  154. package/dist/services/Router/descriptor/tokens.js +9 -0
  155. package/dist/services/Router/index.d.ts +4 -0
  156. package/dist/services/Router/index.js +4 -0
  157. package/dist/services/Router/orchestrator/RouterService.d.ts +74 -0
  158. package/dist/services/Router/orchestrator/RouterService.js +332 -0
  159. package/dist/services/Router/orchestrator/index.d.ts +2 -0
  160. package/dist/services/Router/orchestrator/index.js +1 -0
  161. package/dist/services/Router/serviceModule.d.ts +4 -0
  162. package/dist/services/Router/serviceModule.js +22 -0
  163. package/dist/services/WasmCage/index.d.ts +7 -0
  164. package/dist/services/WasmCage/index.js +4 -0
  165. package/dist/services/WasmCage/scripts/prepin.d.ts +1 -0
  166. package/dist/services/WasmCage/scripts/prepin.js +38 -0
  167. package/dist/services/WasmCage/serviceModule.d.ts +12 -0
  168. package/dist/services/WasmCage/serviceModule.js +18 -0
  169. package/dist/services/WasmCage/src/caged.d.ts +104 -0
  170. package/dist/services/WasmCage/src/caged.js +381 -0
  171. package/dist/services/WasmCage/src/descriptor.d.ts +47 -0
  172. package/dist/services/WasmCage/src/descriptor.js +56 -0
  173. package/dist/services/WasmCage/src/native.d.ts +29 -0
  174. package/dist/services/WasmCage/src/native.js +66 -0
  175. package/dist/services/WasmCage/src/nativeDigests.d.ts +9 -0
  176. package/dist/services/WasmCage/src/nativeDigests.js +15 -0
  177. package/dist/services/WasmCage/src/package.d.ts +36 -0
  178. package/dist/services/WasmCage/src/package.js +122 -0
  179. package/dist/services/shared/canonicalJson.d.ts +29 -0
  180. package/dist/services/shared/canonicalJson.js +93 -0
  181. package/dist/services/shared/documentStore.d.ts +16 -0
  182. package/dist/services/shared/documentStore.js +98 -0
  183. package/dist/src/bootstrap/bootKernelInternal.d.ts +18 -0
  184. package/dist/src/bootstrap/bootKernelInternal.js +420 -0
  185. package/dist/src/bootstrap/bootTypes.d.ts +89 -0
  186. package/dist/src/bootstrap/bootTypes.js +1 -0
  187. package/dist/src/bootstrap/config/index.d.ts +3 -0
  188. package/dist/src/bootstrap/config/index.js +2 -0
  189. package/dist/src/bootstrap/config/integrity.d.ts +34 -0
  190. package/dist/src/bootstrap/config/integrity.js +79 -0
  191. package/dist/src/bootstrap/config/parse.d.ts +15 -0
  192. package/dist/src/bootstrap/config/parse.js +81 -0
  193. package/dist/src/bootstrap/config/pathWriteGuard.d.ts +20 -0
  194. package/dist/src/bootstrap/config/pathWriteGuard.js +349 -0
  195. package/dist/src/bootstrap/config/types.d.ts +41 -0
  196. package/dist/src/bootstrap/config/types.js +15 -0
  197. package/dist/src/bootstrap/hardcore.d.ts +212 -0
  198. package/dist/src/bootstrap/hardcore.js +312 -0
  199. package/dist/src/bootstrap/hardcoreServices.d.ts +10 -0
  200. package/dist/src/bootstrap/hardcoreServices.js +258 -0
  201. package/dist/src/bootstrap/index.d.ts +8 -0
  202. package/dist/src/bootstrap/index.js +5 -0
  203. package/dist/src/bootstrap/packageRequirements.d.ts +26 -0
  204. package/dist/src/bootstrap/packageRequirements.js +52 -0
  205. package/dist/src/contracts/accessBroker.d.ts +53 -0
  206. package/dist/src/contracts/accessBroker.js +1 -0
  207. package/dist/src/contracts/brudOperation.d.ts +20 -0
  208. package/dist/src/contracts/brudOperation.js +20 -0
  209. package/dist/src/contracts/callChain.d.ts +161 -0
  210. package/dist/src/contracts/callChain.js +19 -0
  211. package/dist/src/contracts/capability.d.ts +26 -0
  212. package/dist/src/contracts/capability.js +26 -0
  213. package/dist/src/contracts/container.d.ts +17 -0
  214. package/dist/src/contracts/container.js +8 -0
  215. package/dist/src/contracts/context.d.ts +19 -0
  216. package/dist/src/contracts/context.js +1 -0
  217. package/dist/src/contracts/contextContract.d.ts +129 -0
  218. package/dist/src/contracts/contextContract.js +285 -0
  219. package/dist/src/contracts/dispatchView.d.ts +21 -0
  220. package/dist/src/contracts/dispatchView.js +1 -0
  221. package/dist/src/contracts/handler.d.ts +28 -0
  222. package/dist/src/contracts/handler.js +1 -0
  223. package/dist/src/contracts/index.d.ts +25 -0
  224. package/dist/src/contracts/index.js +8 -0
  225. package/dist/src/contracts/kernel.d.ts +60 -0
  226. package/dist/src/contracts/kernel.js +1 -0
  227. package/dist/src/contracts/lifecycle.d.ts +59 -0
  228. package/dist/src/contracts/lifecycle.js +1 -0
  229. package/dist/src/contracts/manifest.d.ts +145 -0
  230. package/dist/src/contracts/manifest.js +295 -0
  231. package/dist/src/contracts/pipeline.d.ts +8 -0
  232. package/dist/src/contracts/pipeline.js +1 -0
  233. package/dist/src/contracts/policy.d.ts +75 -0
  234. package/dist/src/contracts/policy.js +77 -0
  235. package/dist/src/contracts/registry.d.ts +7 -0
  236. package/dist/src/contracts/registry.js +1 -0
  237. package/dist/src/contracts/serviceDescriptor.d.ts +67 -0
  238. package/dist/src/contracts/serviceDescriptor.js +1 -0
  239. package/dist/src/contracts/serviceToken.d.ts +36 -0
  240. package/dist/src/contracts/serviceToken.js +16 -0
  241. package/dist/src/engine/BrudKernel.d.ts +118 -0
  242. package/dist/src/engine/BrudKernel.js +366 -0
  243. package/dist/src/engine/ExecutionPipeline.d.ts +13 -0
  244. package/dist/src/engine/ExecutionPipeline.js +62 -0
  245. package/dist/src/engine/KernelAccessBroker.d.ts +47 -0
  246. package/dist/src/engine/KernelAccessBroker.js +107 -0
  247. package/dist/src/engine/KernelRegistry.d.ts +14 -0
  248. package/dist/src/engine/KernelRegistry.js +33 -0
  249. package/dist/src/engine/OperationRegistry.d.ts +9 -0
  250. package/dist/src/engine/OperationRegistry.js +18 -0
  251. package/dist/src/engine/ServiceContainer.d.ts +13 -0
  252. package/dist/src/engine/ServiceContainer.js +53 -0
  253. package/dist/src/engine/callChain.d.ts +68 -0
  254. package/dist/src/engine/callChain.js +180 -0
  255. package/dist/src/engine/dag.d.ts +2 -0
  256. package/dist/src/engine/dag.js +34 -0
  257. package/dist/src/engine/errors.d.ts +26 -0
  258. package/dist/src/engine/errors.js +52 -0
  259. package/dist/src/engine/index.d.ts +11 -0
  260. package/dist/src/engine/index.js +8 -0
  261. package/dist/src/engine/state.d.ts +1 -0
  262. package/dist/src/engine/state.js +1 -0
  263. package/dist/src/validation/jsonSchema/compile.d.ts +8 -0
  264. package/dist/src/validation/jsonSchema/compile.js +386 -0
  265. package/dist/src/validation/jsonSchema/index.d.ts +3 -0
  266. package/dist/src/validation/jsonSchema/index.js +2 -0
  267. package/dist/src/validation/jsonSchema/jsonValue.d.ts +29 -0
  268. package/dist/src/validation/jsonSchema/jsonValue.js +190 -0
  269. package/dist/src/validation/jsonSchema/pattern.d.ts +28 -0
  270. package/dist/src/validation/jsonSchema/pattern.js +155 -0
  271. package/dist/src/validation/jsonSchema/types.d.ts +71 -0
  272. package/dist/src/validation/jsonSchema/types.js +15 -0
  273. package/package.json +59 -4
  274. package/services/WasmCage/native/brud-wasm-cage.darwin-arm64.node +0 -0
  275. package/services/WasmCage/native/brud-wasm-cage.darwin-x64.node +0 -0
  276. package/services/WasmCage/native/brud-wasm-cage.linux-arm64-gnu.node +0 -0
  277. package/services/WasmCage/native/brud-wasm-cage.linux-x64-gnu.node +0 -0
  278. package/services/WasmCage/native/brud-wasm-cage.win32-x64-msvc.node +0 -0
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,67 @@
1
+ import type { IKernelAccessBroker } from './accessBroker.js';
2
+ import type { IServiceLifecycle, ServiceContext, ServiceTier } from './lifecycle.js';
3
+ import type { ParticipantManifest } from './manifest.js';
4
+ import type { ServiceAccessPolicy } from './policy.js';
5
+ import type { ServiceToken } from './serviceToken.js';
6
+ /**
7
+ * Contract a service descriptor declares for its public interface.
8
+ *
9
+ * The declaration is descriptive: it names the version and the methods the
10
+ * service promises. The kernel only compares it against hardcore requirements;
11
+ * it does not interpret any method.
12
+ */
13
+ export interface ServiceContractDeclaration {
14
+ /** Contract version the service implements. */
15
+ readonly version: string;
16
+ /** Method names the service implements for this contract. */
17
+ readonly methods: readonly string[];
18
+ }
19
+ /**
20
+ * Self-describing registration record for a kernel service.
21
+ *
22
+ * The factory may build a richer internal shape `TInternal`, while `project`
23
+ * narrows that instance to the public `TService` interface for each caller.
24
+ *
25
+ * @typeParam TService - The public interface exposed to callers.
26
+ * @typeParam TInternal - The internal instance type, which must extend `TService`.
27
+ */
28
+ export interface ServiceDescriptor<TService, TInternal extends TService = TService> {
29
+ /** Token identifying the service. */
30
+ readonly token: ServiceToken<TService>;
31
+ /** Privilege tier the service belongs to. */
32
+ readonly tier: ServiceTier;
33
+ /** Services that must be registered and initialized before this one. */
34
+ readonly dependencies: readonly ServiceToken<unknown>[];
35
+ /** Access control rules applied to every request. */
36
+ readonly accessPolicy?: ServiceAccessPolicy;
37
+ /**
38
+ * Declared contract for the public service interface.
39
+ *
40
+ * Optional for ordinary services. The kernel's fixed hardcore list requires
41
+ * a declared contract on the services it names.
42
+ */
43
+ readonly contract?: ServiceContractDeclaration;
44
+ /**
45
+ * Declarative manifest: what the participant requests and which exposures
46
+ * it gives, with their scopes and policies.
47
+ *
48
+ * Data only. The kernel admits it at boot without invoking {@link factory}.
49
+ */
50
+ readonly manifest?: ParticipantManifest;
51
+ /**
52
+ * Builds the service instance.
53
+ *
54
+ * @param context - Context of the service being constructed.
55
+ * @param broker - Mediated access broker for dependency resolution.
56
+ */
57
+ readonly factory: (context: ServiceContext, broker: IKernelAccessBroker) => TInternal & IServiceLifecycle;
58
+ /**
59
+ * Projects the internal instance into the caller-facing interface.
60
+ *
61
+ * Defaults to returning the instance unchanged when omitted.
62
+ *
63
+ * @param instance - The internal service instance.
64
+ * @param caller - Context of the requesting caller.
65
+ */
66
+ readonly project?: (instance: TInternal, caller: ServiceContext) => TService;
67
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,36 @@
1
+ /**
2
+ * A phantom-branded capability token identifying a service within the kernel.
3
+ *
4
+ * The brand is represented by a tuple so the wrapped service interface is
5
+ * preserved invariantly. Consumers must never read `_brand` at runtime; it
6
+ * exists purely to carry `TService` through registration and lookup.
7
+ *
8
+ * @typeParam TService - The public interface exposed by the service.
9
+ */
10
+ export interface ServiceToken<TService> {
11
+ /** Stable, human-readable identifier used for diagnostics and policies. */
12
+ readonly id: string;
13
+ /** Short explanation of what the service provides. */
14
+ readonly description: string;
15
+ /** Phantom brand preserving `TService` for strict type inference. */
16
+ readonly _brand: readonly [TService];
17
+ }
18
+ /**
19
+ * Creates a service token for the given identifier and description.
20
+ *
21
+ * @typeParam TService - The public interface exposed by the service.
22
+ * @param id - Stable identifier for the service.
23
+ * @param description - Short explanation of what the service provides. When
24
+ * omitted, a description is derived from the identifier.
25
+ * @returns A phantom-branded token bound to `TService`.
26
+ */
27
+ export declare function createServiceToken<TService>(id: string, description?: string): ServiceToken<TService>;
28
+ /**
29
+ * Extracts the public service interface carried by a service token.
30
+ *
31
+ * Planned: this is a universal type-level helper with no current consumer. It
32
+ * is retained deliberately as part of the token primitive.
33
+ *
34
+ * @typeParam TToken - A {@link ServiceToken} type.
35
+ */
36
+ export type InferService<TToken> = TToken extends ServiceToken<infer TService> ? TService : never;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Creates a service token for the given identifier and description.
3
+ *
4
+ * @typeParam TService - The public interface exposed by the service.
5
+ * @param id - Stable identifier for the service.
6
+ * @param description - Short explanation of what the service provides. When
7
+ * omitted, a description is derived from the identifier.
8
+ * @returns A phantom-branded token bound to `TService`.
9
+ */
10
+ export function createServiceToken(id, description = `Service "${id}"`) {
11
+ return {
12
+ id,
13
+ description,
14
+ _brand: [],
15
+ };
16
+ }
@@ -0,0 +1,118 @@
1
+ import type { ContextContract, ContextSchema } from '../contracts/contextContract.js';
2
+ import type { OperationResult, PromptDescriptor } from '../contracts/handler.js';
3
+ import type { IExecutionPipeline } from '../contracts/pipeline.js';
4
+ import type { IServiceContainer } from '../contracts/container.js';
5
+ import type { IOperationRegistry } from '../contracts/registry.js';
6
+ import type { ServiceInfo } from '../contracts/accessBroker.js';
7
+ import type { KernelBootOptions, IBrudKernel } from '../contracts/kernel.js';
8
+ import type { IServiceLifecycle, ServiceContext } from '../contracts/lifecycle.js';
9
+ import type { ServiceDescriptor } from '../contracts/serviceDescriptor.js';
10
+ import type { ServiceToken } from '../contracts/serviceToken.js';
11
+ import type { KernelLifecycleState } from './state.js';
12
+ import type { KernelCallAuthority, KernelCallLimits, KernelHostSession, KernelObservation } from '../contracts/callChain.js';
13
+ import { type ChainParticipant } from './callChain.js';
14
+ /**
15
+ * Runs the compiled hardcore boot calls.
16
+ *
17
+ * Called once, after the hardcore partition initializes and before any
18
+ * composed participant is constructed. Returns every admitted participant
19
+ * with the caller the kernel records for it.
20
+ */
21
+ export type HardcoreBootCall = (instances: readonly {
22
+ readonly tokenId: string;
23
+ readonly instance: IServiceLifecycle;
24
+ }[], authority: KernelCallAuthority) => readonly ChainParticipant[] | void | Promise<readonly ChainParticipant[] | void>;
25
+ /**
26
+ * Planned: the operation dispatch surface is a universal kernel primitive.
27
+ * No production caller drives it yet; it is retained deliberately.
28
+ */
29
+ export interface KernelExecuteOptions {
30
+ signal?: AbortSignal;
31
+ metadata?: Record<string, unknown>;
32
+ maxCallDepth?: number;
33
+ }
34
+ export interface PromptManifestEntry {
35
+ kind: string;
36
+ promptDescriptor: PromptDescriptor;
37
+ }
38
+ export interface PromptManifest {
39
+ operations: PromptManifestEntry[];
40
+ }
41
+ export declare class BrudKernel implements IBrudKernel {
42
+ readonly services: IServiceContainer;
43
+ readonly operations: IOperationRegistry;
44
+ readonly pipeline: IExecutionPipeline;
45
+ private readonly maxCallDepth;
46
+ private readonly registry;
47
+ private readonly accessBroker;
48
+ private readonly initializedServices;
49
+ private state;
50
+ private runningHooksPending;
51
+ /**
52
+ * The host-supplied options bag, stored once at boot.
53
+ *
54
+ * The kernel treats it as opaque: it is attached unchanged to every service
55
+ * and execution context and is never indexed here.
56
+ */
57
+ private bootOptions;
58
+ private readonly bootPinner;
59
+ private readonly bootHardcore;
60
+ private readonly contextContract;
61
+ /** The kernel's call chain: origins, callers, limits, and observations. */
62
+ private readonly calls;
63
+ /**
64
+ * Tokens the compiled hardcore list supplies to the boot loader.
65
+ *
66
+ * The engine uses this only to partition the initialization order. It never
67
+ * infers hardcoredness from a descriptor's token or tier.
68
+ */
69
+ private readonly hardcoreTokens;
70
+ constructor(options?: {
71
+ maxCallDepth?: number;
72
+ bootPinner?: (instances: readonly IServiceLifecycle[]) => Promise<void>;
73
+ bootHardcore?: HardcoreBootCall;
74
+ contextContract?: ContextContract;
75
+ hardcoreTokens?: readonly string[];
76
+ callLimits?: KernelCallLimits;
77
+ });
78
+ /**
79
+ * Runs an operation inside a host-origin call chain.
80
+ *
81
+ * @param session - The host session's declaration.
82
+ * @param operation - Work performed inside the chain.
83
+ */
84
+ runHostCall<T>(session: KernelHostSession, operation: () => T): T;
85
+ /** Reads the kernel's observation stream. Only the kernel's holder can. */
86
+ readObservations(): readonly KernelObservation[];
87
+ /**
88
+ * Appends one record to the observation stream.
89
+ *
90
+ * Used by the kernel's own boot loader; participants never hold the kernel.
91
+ */
92
+ recordObservation(source: string, event: string, detail: Readonly<Record<string, unknown>>): void;
93
+ /**
94
+ * The root origin and caller under which the kernel runs one service's
95
+ * lifecycle work.
96
+ *
97
+ * A hardcore service is the kernel's own root primitive and runs in a
98
+ * kernel-origin chain. Every other participant runs in a runtime-origin chain
99
+ * as itself.
100
+ */
101
+ private frameFor;
102
+ /** Runs one service's lifecycle work inside its own frame. */
103
+ private runFor;
104
+ get lifecycleState(): KernelLifecycleState;
105
+ register<TService, TInternal extends TService = TService>(descriptor: ServiceDescriptor<TService, TInternal>): void;
106
+ boot(options: KernelBootOptions): Promise<void>;
107
+ shutdown(): Promise<void>;
108
+ getContextSchema(): ContextSchema;
109
+ validateContextOptions(options: Readonly<Record<string, unknown>>): void;
110
+ requestAccess<TService>(caller: ServiceContext, token: ServiceToken<TService>): TService;
111
+ hasService(token: ServiceToken<unknown>): boolean;
112
+ listServices(): readonly ServiceInfo[];
113
+ private descriptorMap;
114
+ private rollbackInitialized;
115
+ execute<TInput, TOutput>(kind: string, input: TInput, options?: KernelExecuteOptions): Promise<OperationResult<TOutput>>;
116
+ private createExecutionContext;
117
+ generatePromptManifest(): PromptManifest;
118
+ }
@@ -0,0 +1,366 @@
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
+ const instance = descriptor.factory(context, this.accessBroker);
158
+ this.registry.setInstance(descriptor.token.id, instance);
159
+ if (typeof instance.initialize === 'function') {
160
+ await instance.initialize(this.accessBroker);
161
+ }
162
+ this.initializedServices.push({ tokenId: descriptor.token.id, instance });
163
+ });
164
+ };
165
+ for (const descriptor of hardcoreOrder) {
166
+ await construct(descriptor);
167
+ }
168
+ // Admission. The hardcore boot calls run once the hardcore partition is
169
+ // initialized and before any composed participant is constructed. They
170
+ // record each participant's identity, admit each participant's declared
171
+ // exposures from its manifest, and seal the registration window. No
172
+ // participant code has run yet.
173
+ const hardcore = this.bootHardcore;
174
+ if (hardcore !== undefined) {
175
+ const participants = await this.calls.runRoot('kernel', KERNEL_CALLER, () => hardcore(this.initializedServices.map((entry) => ({
176
+ tokenId: entry.tokenId,
177
+ instance: entry.instance,
178
+ })), this.calls));
179
+ if (participants !== undefined) {
180
+ this.calls.admitParticipants(participants);
181
+ }
182
+ }
183
+ for (const descriptor of composedOrder) {
184
+ await construct(descriptor);
185
+ }
186
+ // Boot snapshot pinning. A module whose policy must be frozen from
187
+ // another participant's state registers a pinner; it runs here, while
188
+ // the broker's lifecycle gate is suspended for this one bounded
189
+ // operation, so the frozen policy exists before any caller can resolve
190
+ // the token. The hook is a no-op when no pinner was registered. Module
191
+ // hooks are participant code, so they run in a runtime-origin chain.
192
+ const pinner = this.bootPinner;
193
+ if (pinner !== undefined) {
194
+ await this.calls.runRoot('runtime', UNIDENTIFIED_PARTICIPANT, async () => {
195
+ await this.accessBroker.withLifecycleGateSuspended(async () => {
196
+ await pinner(this.initializedServices.map((entry) => entry.instance));
197
+ });
198
+ });
199
+ }
200
+ // The pin phase is the last point where a service may declare a field.
201
+ // Validate again so declarations made there are enforced too.
202
+ this.contextContract?.validate(this.bootOptions);
203
+ this.state = 'RUNNING';
204
+ this.contextContract?.lock();
205
+ }
206
+ catch (error) {
207
+ await this.rollbackInitialized();
208
+ this.state = 'DISPOSED';
209
+ throw error;
210
+ }
211
+ // Post-boot start. The broker now grants access, so services can resolve
212
+ // their dependencies. Hooks run one at a time in initialization order.
213
+ // Any failure rolls the whole kernel back: the broker stops granting,
214
+ // every initialized service is disposed in reverse order, and boot()
215
+ // rejects with the original error.
216
+ this.runningHooksPending = true;
217
+ try {
218
+ for (const { tokenId, instance } of this.initializedServices) {
219
+ if (typeof instance.onRunning === 'function') {
220
+ await this.runFor(tokenId, () => instance.onRunning?.(this.accessBroker));
221
+ }
222
+ }
223
+ }
224
+ catch (error) {
225
+ this.state = 'SHUTTING_DOWN';
226
+ await this.rollbackInitialized();
227
+ this.state = 'DISPOSED';
228
+ throw error;
229
+ }
230
+ finally {
231
+ this.runningHooksPending = false;
232
+ }
233
+ }
234
+ async shutdown() {
235
+ if (this.runningHooksPending) {
236
+ // Disposing services while onRunning hooks are still starting them would
237
+ // tear down dependencies underneath an in-flight activation.
238
+ throw new LifecycleStateError('shutdown while onRunning hooks are pending', this.state);
239
+ }
240
+ if (this.state !== 'RUNNING') {
241
+ throw new LifecycleStateError('shutdown', this.state, 'RUNNING');
242
+ }
243
+ this.state = 'SHUTTING_DOWN';
244
+ let firstError;
245
+ const pending = this.initializedServices.splice(0).reverse();
246
+ for (const { instance } of pending) {
247
+ try {
248
+ await instance.dispose?.();
249
+ }
250
+ catch (error) {
251
+ if (firstError === undefined) {
252
+ firstError = error;
253
+ }
254
+ }
255
+ }
256
+ this.registry.clearInstances();
257
+ this.calls.clearParticipants();
258
+ this.state = 'DISPOSED';
259
+ if (firstError !== undefined) {
260
+ throw firstError;
261
+ }
262
+ }
263
+ getContextSchema() {
264
+ if (this.contextContract !== undefined) {
265
+ return this.contextContract.schema();
266
+ }
267
+ return { locked: false, fields: [] };
268
+ }
269
+ validateContextOptions(options) {
270
+ this.contextContract?.validate(options);
271
+ }
272
+ requestAccess(caller, token) {
273
+ return this.accessBroker.requestAccess(caller, token);
274
+ }
275
+ hasService(token) {
276
+ return this.accessBroker.hasService(token);
277
+ }
278
+ listServices() {
279
+ return this.accessBroker.listServices();
280
+ }
281
+ descriptorMap() {
282
+ return new Map(this.registry.listDescriptors().map((descriptor) => [descriptor.token.id, descriptor]));
283
+ }
284
+ async rollbackInitialized() {
285
+ const pending = this.initializedServices.splice(0).reverse();
286
+ for (const { instance } of pending) {
287
+ try {
288
+ await instance.dispose?.();
289
+ }
290
+ catch {
291
+ continue;
292
+ }
293
+ }
294
+ this.registry.clearInstances();
295
+ this.calls.clearParticipants();
296
+ }
297
+ async execute(kind, input, options) {
298
+ const handler = this.operations.get(kind);
299
+ if (!handler) {
300
+ return {
301
+ status: "failed",
302
+ error: `Unknown operation kind: "${kind}"`,
303
+ durationMs: 0,
304
+ };
305
+ }
306
+ const context = this.createExecutionContext(options);
307
+ return this.pipeline.execute(handler, context, input);
308
+ }
309
+ createExecutionContext(options) {
310
+ const kernel = this;
311
+ const effectiveMaxDepth = options?.maxCallDepth ?? this.maxCallDepth;
312
+ const sessionId = crypto.randomUUID();
313
+ async function invokeImpl(kind, input) {
314
+ const nextDepth = this.callDepth + 1;
315
+ if (nextDepth > effectiveMaxDepth) {
316
+ return {
317
+ status: "failed",
318
+ error: `Maximum call depth (${effectiveMaxDepth}) exceeded`,
319
+ };
320
+ }
321
+ const childCtx = this.createChildContext({ callDepth: nextDepth });
322
+ const handler = kernel.operations.get(kind);
323
+ if (!handler) {
324
+ return {
325
+ status: "failed",
326
+ error: `Unknown operation kind: "${kind}"`,
327
+ durationMs: 0,
328
+ };
329
+ }
330
+ return kernel.pipeline.execute(handler, childCtx, input);
331
+ }
332
+ function createChildContextImpl(overrides) {
333
+ return {
334
+ sessionId: overrides?.sessionId ?? this.sessionId,
335
+ options: overrides?.options ?? this.options,
336
+ signal: overrides?.signal ?? this.signal,
337
+ callDepth: overrides?.callDepth ?? this.callDepth,
338
+ metadata: overrides?.metadata ?? this.metadata,
339
+ resolve: overrides?.resolve ?? this.resolve,
340
+ invoke: overrides?.invoke ?? this.invoke,
341
+ createChildContext: overrides?.createChildContext ?? this.createChildContext,
342
+ };
343
+ }
344
+ const ctx = {
345
+ sessionId,
346
+ options: this.bootOptions,
347
+ signal: options?.signal,
348
+ callDepth: 0,
349
+ metadata: options?.metadata,
350
+ resolve: (token) => kernel.services.resolve(token),
351
+ invoke: invokeImpl,
352
+ createChildContext: createChildContextImpl,
353
+ };
354
+ return ctx;
355
+ }
356
+ generatePromptManifest() {
357
+ const operations = this.operations
358
+ .list()
359
+ .filter((h) => h.promptDescriptor !== undefined)
360
+ .map((h) => ({
361
+ kind: h.kind,
362
+ promptDescriptor: h.promptDescriptor,
363
+ }));
364
+ return { operations };
365
+ }
366
+ }
@@ -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
+ }