@noopolis/daimon 0.1.1 → 0.2.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 (404) hide show
  1. package/README.md +307 -45
  2. package/dist/contracts/attentionContract.d.ts +117 -0
  3. package/dist/contracts/attentionContract.d.ts.map +1 -0
  4. package/dist/contracts/attentionContract.js +14 -0
  5. package/dist/contracts/canonicalJson.d.ts +3 -0
  6. package/dist/contracts/canonicalJson.d.ts.map +1 -0
  7. package/dist/contracts/canonicalJson.js +54 -0
  8. package/dist/contracts/grokWorkerContract.d.ts +74 -0
  9. package/dist/contracts/grokWorkerContract.d.ts.map +1 -0
  10. package/dist/contracts/grokWorkerContract.js +88 -0
  11. package/dist/contracts/organizationRuntimeContract.d.ts +765 -0
  12. package/dist/contracts/organizationRuntimeContract.d.ts.map +1 -0
  13. package/dist/contracts/organizationRuntimeContract.js +121 -0
  14. package/dist/contracts/runtimeContractManifest.d.ts +1615 -0
  15. package/dist/contracts/runtimeContractManifest.d.ts.map +1 -0
  16. package/dist/contracts/runtimeContractManifest.js +181 -0
  17. package/dist/core/credentialRedaction.d.ts +4 -0
  18. package/dist/core/credentialRedaction.d.ts.map +1 -0
  19. package/dist/core/credentialRedaction.js +28 -0
  20. package/dist/core/siblingBuildFreshness.d.ts +21 -0
  21. package/dist/core/siblingBuildFreshness.d.ts.map +1 -0
  22. package/dist/core/siblingBuildFreshness.js +25 -0
  23. package/dist/core/types.d.ts +10 -1
  24. package/dist/core/types.d.ts.map +1 -1
  25. package/dist/examples/exampleCausalId.d.ts +3 -0
  26. package/dist/examples/exampleCausalId.d.ts.map +1 -0
  27. package/dist/examples/exampleCausalId.js +8 -0
  28. package/dist/examples/jungian-play-org.js +3 -2
  29. package/dist/examples/jungian-triad-org.js +3 -2
  30. package/dist/examples/jungianPlayAgent.d.ts +1 -1
  31. package/dist/examples/jungianPlayAgent.d.ts.map +1 -1
  32. package/dist/examples/jungianPlayAgent.js +1 -1
  33. package/dist/examples/jungianProfiles.d.ts +1 -1
  34. package/dist/examples/jungianProfiles.d.ts.map +1 -1
  35. package/dist/examples/jungianTriadProfiles.d.ts +1 -1
  36. package/dist/examples/jungianTriadProfiles.d.ts.map +1 -1
  37. package/dist/examples/pi-agent.js +3 -2
  38. package/dist/examples/pi-memory-org.js +6 -5
  39. package/dist/index.d.ts +1 -0
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.js +1 -0
  42. package/dist/mcp/toolServer.d.ts +31 -0
  43. package/dist/mcp/toolServer.d.ts.map +1 -0
  44. package/dist/mcp/toolServer.js +145 -0
  45. package/dist/observability/causalEvents.d.ts +118 -0
  46. package/dist/observability/causalEvents.d.ts.map +1 -0
  47. package/dist/observability/causalEvents.js +223 -0
  48. package/dist/observability/controlCausal.d.ts +117 -0
  49. package/dist/observability/controlCausal.d.ts.map +1 -0
  50. package/dist/observability/controlCausal.js +107 -0
  51. package/dist/observability/emitCausalFixture.d.ts +25 -0
  52. package/dist/observability/emitCausalFixture.d.ts.map +1 -0
  53. package/dist/observability/emitCausalFixture.js +98 -0
  54. package/dist/observability/index.d.ts +2 -0
  55. package/dist/observability/index.d.ts.map +1 -1
  56. package/dist/observability/index.js +2 -0
  57. package/dist/observability/orgObserver.d.ts.map +1 -1
  58. package/dist/observability/orgObserver.js +3 -3
  59. package/dist/pi/agyHeadlessResult.d.ts +60 -0
  60. package/dist/pi/agyHeadlessResult.d.ts.map +1 -0
  61. package/dist/pi/agyHeadlessResult.js +95 -0
  62. package/dist/pi/cliChildOutput.d.ts +67 -0
  63. package/dist/pi/cliChildOutput.d.ts.map +1 -0
  64. package/dist/pi/cliChildOutput.js +269 -0
  65. package/dist/pi/cliEngineRun.d.ts +16 -0
  66. package/dist/pi/cliEngineRun.d.ts.map +1 -0
  67. package/dist/pi/cliEngineRun.js +27 -0
  68. package/dist/pi/cliEngineSpawn.d.ts +55 -0
  69. package/dist/pi/cliEngineSpawn.d.ts.map +1 -0
  70. package/dist/pi/cliEngineSpawn.js +164 -0
  71. package/dist/pi/cliEnvironment.d.ts +12 -0
  72. package/dist/pi/cliEnvironment.d.ts.map +1 -0
  73. package/dist/pi/cliEnvironment.js +32 -0
  74. package/dist/pi/cliMcpRegistration.d.ts +61 -0
  75. package/dist/pi/cliMcpRegistration.d.ts.map +1 -0
  76. package/dist/pi/cliMcpRegistration.js +75 -0
  77. package/dist/pi/cliProcess.d.ts +7 -0
  78. package/dist/pi/cliProcess.d.ts.map +1 -0
  79. package/dist/pi/cliProcess.js +132 -0
  80. package/dist/pi/cliSession.d.ts +106 -0
  81. package/dist/pi/cliSession.d.ts.map +1 -0
  82. package/dist/pi/cliSession.js +379 -0
  83. package/dist/pi/cliTurnMetering.d.ts +60 -0
  84. package/dist/pi/cliTurnMetering.d.ts.map +1 -0
  85. package/dist/pi/cliTurnMetering.js +60 -0
  86. package/dist/pi/codexFilesystemRules.d.ts +3 -0
  87. package/dist/pi/codexFilesystemRules.d.ts.map +1 -0
  88. package/dist/pi/codexFilesystemRules.js +28 -0
  89. package/dist/pi/codexHeadlessResult.d.ts +68 -0
  90. package/dist/pi/codexHeadlessResult.d.ts.map +1 -0
  91. package/dist/pi/codexHeadlessResult.js +122 -0
  92. package/dist/pi/codexRolloutUsage.d.ts +95 -0
  93. package/dist/pi/codexRolloutUsage.d.ts.map +1 -0
  94. package/dist/pi/codexRolloutUsage.js +221 -0
  95. package/dist/pi/engineWakeLimits.d.ts +39 -0
  96. package/dist/pi/engineWakeLimits.d.ts.map +1 -0
  97. package/dist/pi/engineWakeLimits.js +55 -0
  98. package/dist/pi/grokHeadlessResult.d.ts +36 -0
  99. package/dist/pi/grokHeadlessResult.d.ts.map +1 -0
  100. package/dist/pi/grokHeadlessResult.js +118 -0
  101. package/dist/pi/grokHomeMcpRegistration.d.ts +21 -0
  102. package/dist/pi/grokHomeMcpRegistration.d.ts.map +1 -0
  103. package/dist/pi/grokHomeMcpRegistration.js +57 -0
  104. package/dist/pi/grokSandbox.d.ts +11 -0
  105. package/dist/pi/grokSandbox.d.ts.map +1 -0
  106. package/dist/pi/grokSandbox.js +216 -0
  107. package/dist/pi/grokStreamUsage.d.ts +31 -0
  108. package/dist/pi/grokStreamUsage.d.ts.map +1 -0
  109. package/dist/pi/grokStreamUsage.js +51 -0
  110. package/dist/pi/index.d.ts +5 -0
  111. package/dist/pi/index.d.ts.map +1 -1
  112. package/dist/pi/index.js +5 -0
  113. package/dist/pi/memoryTools.d.ts +55 -1
  114. package/dist/pi/memoryTools.d.ts.map +1 -1
  115. package/dist/pi/memoryTools.js +144 -24
  116. package/dist/pi/modelRegistry.d.ts +13 -0
  117. package/dist/pi/modelRegistry.d.ts.map +1 -0
  118. package/dist/pi/modelRegistry.js +19 -0
  119. package/dist/pi/piAgentHandle.d.ts +46 -0
  120. package/dist/pi/piAgentHandle.d.ts.map +1 -0
  121. package/dist/pi/piAgentHandle.js +351 -0
  122. package/dist/pi/piAgentWakeSupport.d.ts +103 -0
  123. package/dist/pi/piAgentWakeSupport.d.ts.map +1 -0
  124. package/dist/pi/piAgentWakeSupport.js +151 -0
  125. package/dist/pi/piHarness.d.ts +40 -5
  126. package/dist/pi/piHarness.d.ts.map +1 -1
  127. package/dist/pi/piHarness.js +151 -256
  128. package/dist/pi/prompts.d.ts +10 -0
  129. package/dist/pi/prompts.d.ts.map +1 -0
  130. package/dist/pi/prompts.js +63 -0
  131. package/dist/pi/rawTrainingCapture.d.ts +59 -0
  132. package/dist/pi/rawTrainingCapture.d.ts.map +1 -0
  133. package/dist/pi/rawTrainingCapture.js +175 -0
  134. package/dist/pi/turnCausal.d.ts +73 -0
  135. package/dist/pi/turnCausal.d.ts.map +1 -0
  136. package/dist/pi/turnCausal.js +75 -0
  137. package/dist/pi/turnTrace.d.ts +128 -0
  138. package/dist/pi/turnTrace.d.ts.map +1 -0
  139. package/dist/pi/turnTrace.js +138 -0
  140. package/dist/pi/wakeAcceptance.d.ts +49 -0
  141. package/dist/pi/wakeAcceptance.d.ts.map +1 -0
  142. package/dist/pi/wakeAcceptance.js +281 -0
  143. package/dist/pi/wakeAcceptanceFs.d.ts +44 -0
  144. package/dist/pi/wakeAcceptanceFs.d.ts.map +1 -0
  145. package/dist/pi/wakeAcceptanceFs.js +318 -0
  146. package/dist/pi/wakeAcceptanceSchema.d.ts +75 -0
  147. package/dist/pi/wakeAcceptanceSchema.d.ts.map +1 -0
  148. package/dist/pi/wakeAcceptanceSchema.js +265 -0
  149. package/dist/pi/wakeModes.d.ts +9 -0
  150. package/dist/pi/wakeModes.d.ts.map +1 -0
  151. package/dist/pi/wakeModes.js +19 -0
  152. package/dist/pi/worldNudge.d.ts +22 -0
  153. package/dist/pi/worldNudge.d.ts.map +1 -0
  154. package/dist/pi/worldNudge.js +62 -0
  155. package/dist/pi/worldToolProtocol.d.ts +13 -0
  156. package/dist/pi/worldToolProtocol.d.ts.map +1 -0
  157. package/dist/pi/worldToolProtocol.js +49 -0
  158. package/dist/pi/worldTools.d.ts +32 -0
  159. package/dist/pi/worldTools.d.ts.map +1 -0
  160. package/dist/pi/worldTools.js +373 -0
  161. package/dist/pi/worldTrajectory.d.ts +52 -0
  162. package/dist/pi/worldTrajectory.d.ts.map +1 -0
  163. package/dist/pi/worldTrajectory.js +131 -0
  164. package/dist/runtime/agySubscriptionBootstrap.d.ts +16 -0
  165. package/dist/runtime/agySubscriptionBootstrap.d.ts.map +1 -0
  166. package/dist/runtime/agySubscriptionBootstrap.js +80 -0
  167. package/dist/runtime/agySubscriptionRealm.d.ts +38 -0
  168. package/dist/runtime/agySubscriptionRealm.d.ts.map +1 -0
  169. package/dist/runtime/agySubscriptionRealm.js +242 -0
  170. package/dist/runtime/attention.d.ts +21 -0
  171. package/dist/runtime/attention.d.ts.map +1 -0
  172. package/dist/runtime/attention.js +36 -0
  173. package/dist/runtime/attentionDispatcher.d.ts +66 -0
  174. package/dist/runtime/attentionDispatcher.d.ts.map +1 -0
  175. package/dist/runtime/attentionDispatcher.js +271 -0
  176. package/dist/runtime/cli.d.ts +7 -0
  177. package/dist/runtime/cli.d.ts.map +1 -0
  178. package/dist/runtime/cli.js +251 -0
  179. package/dist/runtime/codexSandboxProjection.d.ts +17 -0
  180. package/dist/runtime/codexSandboxProjection.d.ts.map +1 -0
  181. package/dist/runtime/codexSandboxProjection.js +40 -0
  182. package/dist/runtime/contract-manifest.json +1 -0
  183. package/dist/runtime/contract-manifest.sha256 +1 -0
  184. package/dist/runtime/contractManifest.d.ts +8 -0
  185. package/dist/runtime/contractManifest.d.ts.map +1 -0
  186. package/dist/runtime/contractManifest.js +4 -0
  187. package/dist/runtime/engineBrokerCapabilities.d.ts +30 -0
  188. package/dist/runtime/engineBrokerCapabilities.d.ts.map +1 -0
  189. package/dist/runtime/engineBrokerCapabilities.js +59 -0
  190. package/dist/runtime/engineBrokerControlClient.d.ts +44 -0
  191. package/dist/runtime/engineBrokerControlClient.d.ts.map +1 -0
  192. package/dist/runtime/engineBrokerControlClient.js +132 -0
  193. package/dist/runtime/engineBrokerCredentialJournal.d.ts +10 -0
  194. package/dist/runtime/engineBrokerCredentialJournal.d.ts.map +1 -0
  195. package/dist/runtime/engineBrokerCredentialJournal.js +17 -0
  196. package/dist/runtime/engineBrokerInferenceProtocol.d.ts +52 -0
  197. package/dist/runtime/engineBrokerInferenceProtocol.d.ts.map +1 -0
  198. package/dist/runtime/engineBrokerInferenceProtocol.js +65 -0
  199. package/dist/runtime/engineBrokerMcpCallLog.d.ts +160 -0
  200. package/dist/runtime/engineBrokerMcpCallLog.d.ts.map +1 -0
  201. package/dist/runtime/engineBrokerMcpCallLog.js +219 -0
  202. package/dist/runtime/engineBrokerMcpFacade.d.ts +56 -0
  203. package/dist/runtime/engineBrokerMcpFacade.d.ts.map +1 -0
  204. package/dist/runtime/engineBrokerMcpFacade.js +343 -0
  205. package/dist/runtime/engineBrokerNativeClient.d.ts +52 -0
  206. package/dist/runtime/engineBrokerNativeClient.d.ts.map +1 -0
  207. package/dist/runtime/engineBrokerNativeClient.js +151 -0
  208. package/dist/runtime/engineBrokerProtocol.d.ts +135 -0
  209. package/dist/runtime/engineBrokerProtocol.d.ts.map +1 -0
  210. package/dist/runtime/engineBrokerProtocol.js +274 -0
  211. package/dist/runtime/engineBrokerSealLedger.d.ts +70 -0
  212. package/dist/runtime/engineBrokerSealLedger.d.ts.map +1 -0
  213. package/dist/runtime/engineBrokerSealLedger.js +123 -0
  214. package/dist/runtime/engineBrokerService.d.ts +37 -0
  215. package/dist/runtime/engineBrokerService.d.ts.map +1 -0
  216. package/dist/runtime/engineBrokerService.js +138 -0
  217. package/dist/runtime/engineBrokerServiceCli.d.ts +5 -0
  218. package/dist/runtime/engineBrokerServiceCli.d.ts.map +1 -0
  219. package/dist/runtime/engineBrokerServiceCli.js +32 -0
  220. package/dist/runtime/engineBrokerServiceConfig.d.ts +50 -0
  221. package/dist/runtime/engineBrokerServiceConfig.d.ts.map +1 -0
  222. package/dist/runtime/engineBrokerServiceConfig.js +92 -0
  223. package/dist/runtime/engineBrokerSingleflight.d.ts +13 -0
  224. package/dist/runtime/engineBrokerSingleflight.d.ts.map +1 -0
  225. package/dist/runtime/engineBrokerSingleflight.js +24 -0
  226. package/dist/runtime/engineBrokerTurnAccounting.d.ts +76 -0
  227. package/dist/runtime/engineBrokerTurnAccounting.d.ts.map +1 -0
  228. package/dist/runtime/engineBrokerTurnAccounting.js +108 -0
  229. package/dist/runtime/engineBrokerTurnRegistry.d.ts +57 -0
  230. package/dist/runtime/engineBrokerTurnRegistry.d.ts.map +1 -0
  231. package/dist/runtime/engineBrokerTurnRegistry.js +158 -0
  232. package/dist/runtime/engineDispatcher.d.ts +36 -0
  233. package/dist/runtime/engineDispatcher.d.ts.map +1 -0
  234. package/dist/runtime/engineDispatcher.js +216 -0
  235. package/dist/runtime/engineReadiness.d.ts +23 -0
  236. package/dist/runtime/engineReadiness.d.ts.map +1 -0
  237. package/dist/runtime/engineReadiness.js +222 -0
  238. package/dist/runtime/grokAuthenticationError.d.ts +8 -0
  239. package/dist/runtime/grokAuthenticationError.d.ts.map +1 -0
  240. package/dist/runtime/grokAuthenticationError.js +20 -0
  241. package/dist/runtime/grokBrokerCredentialAuthority.d.ts +22 -0
  242. package/dist/runtime/grokBrokerCredentialAuthority.d.ts.map +1 -0
  243. package/dist/runtime/grokBrokerCredentialAuthority.js +124 -0
  244. package/dist/runtime/grokBrokerCredentialReader.d.ts +5 -0
  245. package/dist/runtime/grokBrokerCredentialReader.d.ts.map +1 -0
  246. package/dist/runtime/grokBrokerCredentialReader.js +37 -0
  247. package/dist/runtime/grokBrokerModelPolicy.d.ts +23 -0
  248. package/dist/runtime/grokBrokerModelPolicy.d.ts.map +1 -0
  249. package/dist/runtime/grokBrokerModelPolicy.js +28 -0
  250. package/dist/runtime/grokBrokerProjection.d.ts +106 -0
  251. package/dist/runtime/grokBrokerProjection.d.ts.map +1 -0
  252. package/dist/runtime/grokBrokerProjection.js +94 -0
  253. package/dist/runtime/grokBrokerProxy.d.ts +55 -0
  254. package/dist/runtime/grokBrokerProxy.d.ts.map +1 -0
  255. package/dist/runtime/grokBrokerProxy.js +262 -0
  256. package/dist/runtime/grokBrokerProxyRequest.d.ts +30 -0
  257. package/dist/runtime/grokBrokerProxyRequest.d.ts.map +1 -0
  258. package/dist/runtime/grokBrokerProxyRequest.js +74 -0
  259. package/dist/runtime/grokBrokerRealmLease.d.ts +4 -0
  260. package/dist/runtime/grokBrokerRealmLease.d.ts.map +1 -0
  261. package/dist/runtime/grokBrokerRealmLease.js +33 -0
  262. package/dist/runtime/grokBrokerRefresh.d.ts +2 -0
  263. package/dist/runtime/grokBrokerRefresh.d.ts.map +1 -0
  264. package/dist/runtime/grokBrokerRefresh.js +15 -0
  265. package/dist/runtime/grokBrokerTurnMeter.d.ts +128 -0
  266. package/dist/runtime/grokBrokerTurnMeter.d.ts.map +1 -0
  267. package/dist/runtime/grokBrokerTurnMeter.js +251 -0
  268. package/dist/runtime/grokBrokerWorkerConfig.d.ts +86 -0
  269. package/dist/runtime/grokBrokerWorkerConfig.d.ts.map +1 -0
  270. package/dist/runtime/grokBrokerWorkerConfig.js +135 -0
  271. package/dist/runtime/grokEngineBroker.d.ts +48 -0
  272. package/dist/runtime/grokEngineBroker.d.ts.map +1 -0
  273. package/dist/runtime/grokEngineBroker.js +114 -0
  274. package/dist/runtime/grokEngineBrokerLedger.d.ts +52 -0
  275. package/dist/runtime/grokEngineBrokerLedger.d.ts.map +1 -0
  276. package/dist/runtime/grokEngineBrokerLedger.js +114 -0
  277. package/dist/runtime/grokEngineBrokerMetering.d.ts +51 -0
  278. package/dist/runtime/grokEngineBrokerMetering.d.ts.map +1 -0
  279. package/dist/runtime/grokEngineBrokerMetering.js +34 -0
  280. package/dist/runtime/grokEngineBrokerTurn.d.ts +58 -0
  281. package/dist/runtime/grokEngineBrokerTurn.d.ts.map +1 -0
  282. package/dist/runtime/grokEngineBrokerTurn.js +165 -0
  283. package/dist/runtime/grokInferenceClientConfig.d.ts +41 -0
  284. package/dist/runtime/grokInferenceClientConfig.d.ts.map +1 -0
  285. package/dist/runtime/grokInferenceClientConfig.js +62 -0
  286. package/dist/runtime/grokInferenceGrants.d.ts +77 -0
  287. package/dist/runtime/grokInferenceGrants.d.ts.map +1 -0
  288. package/dist/runtime/grokInferenceGrants.js +116 -0
  289. package/dist/runtime/grokInferenceProxy.d.ts +29 -0
  290. package/dist/runtime/grokInferenceProxy.d.ts.map +1 -0
  291. package/dist/runtime/grokInferenceProxy.js +82 -0
  292. package/dist/runtime/grokInferenceProxyRequest.d.ts +23 -0
  293. package/dist/runtime/grokInferenceProxyRequest.d.ts.map +1 -0
  294. package/dist/runtime/grokInferenceProxyRequest.js +63 -0
  295. package/dist/runtime/grokSlotPreflightReceipt.d.ts +70 -0
  296. package/dist/runtime/grokSlotPreflightReceipt.d.ts.map +1 -0
  297. package/dist/runtime/grokSlotPreflightReceipt.js +101 -0
  298. package/dist/runtime/grokSubscriptionRealm.d.ts +4 -0
  299. package/dist/runtime/grokSubscriptionRealm.d.ts.map +1 -0
  300. package/dist/runtime/grokSubscriptionRealm.js +435 -0
  301. package/dist/runtime/grokSubscriptionRealmTypes.d.ts +26 -0
  302. package/dist/runtime/grokSubscriptionRealmTypes.d.ts.map +1 -0
  303. package/dist/runtime/grokSubscriptionRealmTypes.js +1 -0
  304. package/dist/runtime/grokWorkerAttestation.d.ts +170 -0
  305. package/dist/runtime/grokWorkerAttestation.d.ts.map +1 -0
  306. package/dist/runtime/grokWorkerAttestation.js +266 -0
  307. package/dist/runtime/grokWorkerDenyPlacement.d.ts +80 -0
  308. package/dist/runtime/grokWorkerDenyPlacement.d.ts.map +1 -0
  309. package/dist/runtime/grokWorkerDenyPlacement.js +128 -0
  310. package/dist/runtime/grokWorkerHomeAttestation.d.ts +30 -0
  311. package/dist/runtime/grokWorkerHomeAttestation.d.ts.map +1 -0
  312. package/dist/runtime/grokWorkerHomeAttestation.js +69 -0
  313. package/dist/runtime/grokWorkerSandboxProfile.d.ts +27 -0
  314. package/dist/runtime/grokWorkerSandboxProfile.d.ts.map +1 -0
  315. package/dist/runtime/grokWorkerSandboxProfile.js +44 -0
  316. package/dist/runtime/grokWorkerTmpAttestation.d.ts +55 -0
  317. package/dist/runtime/grokWorkerTmpAttestation.d.ts.map +1 -0
  318. package/dist/runtime/grokWorkerTmpAttestation.js +63 -0
  319. package/dist/runtime/index.d.ts +26 -0
  320. package/dist/runtime/index.d.ts.map +1 -0
  321. package/dist/runtime/index.js +23 -0
  322. package/dist/runtime/inferenceUsageLedger.d.ts +38 -0
  323. package/dist/runtime/inferenceUsageLedger.d.ts.map +1 -0
  324. package/dist/runtime/inferenceUsageLedger.js +53 -0
  325. package/dist/runtime/mcpToolResult.d.ts +127 -0
  326. package/dist/runtime/mcpToolResult.d.ts.map +1 -0
  327. package/dist/runtime/mcpToolResult.js +214 -0
  328. package/dist/runtime/moltnetMachineRead.d.ts +97 -0
  329. package/dist/runtime/moltnetMachineRead.d.ts.map +1 -0
  330. package/dist/runtime/moltnetMachineRead.js +183 -0
  331. package/dist/runtime/native/daimon-engine-broker +0 -0
  332. package/dist/runtime/organizationRuntime.d.ts +165 -0
  333. package/dist/runtime/organizationRuntime.d.ts.map +1 -0
  334. package/dist/runtime/organizationRuntime.js +3 -0
  335. package/dist/runtime/organizationRuntimeControl.d.ts +42 -0
  336. package/dist/runtime/organizationRuntimeControl.d.ts.map +1 -0
  337. package/dist/runtime/organizationRuntimeControl.js +235 -0
  338. package/dist/runtime/organizationRuntimeHost.d.ts +19 -0
  339. package/dist/runtime/organizationRuntimeHost.d.ts.map +1 -0
  340. package/dist/runtime/organizationRuntimeHost.js +359 -0
  341. package/dist/runtime/organizationRuntimeParsing.d.ts +6 -0
  342. package/dist/runtime/organizationRuntimeParsing.d.ts.map +1 -0
  343. package/dist/runtime/organizationRuntimeParsing.js +370 -0
  344. package/dist/runtime/organizationRuntimeReadiness.d.ts +12 -0
  345. package/dist/runtime/organizationRuntimeReadiness.d.ts.map +1 -0
  346. package/dist/runtime/organizationRuntimeReadiness.js +59 -0
  347. package/dist/runtime/physicalReadiness.d.ts +47 -0
  348. package/dist/runtime/physicalReadiness.d.ts.map +1 -0
  349. package/dist/runtime/physicalReadiness.js +142 -0
  350. package/dist/runtime/portableCredentialAuth.d.ts +6 -0
  351. package/dist/runtime/portableCredentialAuth.d.ts.map +1 -0
  352. package/dist/runtime/portableCredentialAuth.js +70 -0
  353. package/dist/runtime/portableCredentialMaterial.d.ts +8 -0
  354. package/dist/runtime/portableCredentialMaterial.d.ts.map +1 -0
  355. package/dist/runtime/portableCredentialMaterial.js +169 -0
  356. package/dist/runtime/productionAgentTools.d.ts +5 -0
  357. package/dist/runtime/productionAgentTools.d.ts.map +1 -0
  358. package/dist/runtime/productionAgentTools.js +328 -0
  359. package/dist/runtime/runtimeHomeLayout.d.ts +48 -0
  360. package/dist/runtime/runtimeHomeLayout.d.ts.map +1 -0
  361. package/dist/runtime/runtimeHomeLayout.js +84 -0
  362. package/dist/runtime/schedule.d.ts +40 -0
  363. package/dist/runtime/schedule.d.ts.map +1 -0
  364. package/dist/runtime/schedule.js +371 -0
  365. package/dist/runtime/storeCoordination.d.ts +23 -0
  366. package/dist/runtime/storeCoordination.d.ts.map +1 -0
  367. package/dist/runtime/storeCoordination.js +94 -0
  368. package/dist/runtime/toolResultSpill.d.ts +152 -0
  369. package/dist/runtime/toolResultSpill.d.ts.map +1 -0
  370. package/dist/runtime/toolResultSpill.js +351 -0
  371. package/dist/runtime/turnRequestLedger.d.ts +160 -0
  372. package/dist/runtime/turnRequestLedger.d.ts.map +1 -0
  373. package/dist/runtime/turnRequestLedger.js +249 -0
  374. package/dist/runtime/turnUsageLedger.d.ts +143 -0
  375. package/dist/runtime/turnUsageLedger.d.ts.map +1 -0
  376. package/dist/runtime/turnUsageLedger.js +203 -0
  377. package/dist/runtime/wakeAcceptanceProcessIdentity.d.ts +17 -0
  378. package/dist/runtime/wakeAcceptanceProcessIdentity.d.ts.map +1 -0
  379. package/dist/runtime/wakeAcceptanceProcessIdentity.js +50 -0
  380. package/dist/runtime/wakeAcceptanceReconciliation.d.ts +211 -0
  381. package/dist/runtime/wakeAcceptanceReconciliation.d.ts.map +1 -0
  382. package/dist/runtime/wakeAcceptanceReconciliation.js +289 -0
  383. package/dist/runtime/wakeAcceptanceRecord.d.ts +23 -0
  384. package/dist/runtime/wakeAcceptanceRecord.d.ts.map +1 -0
  385. package/dist/runtime/wakeAcceptanceRecord.js +56 -0
  386. package/dist/runtime/wakeAcceptanceRetention.d.ts +12 -0
  387. package/dist/runtime/wakeAcceptanceRetention.d.ts.map +1 -0
  388. package/dist/runtime/wakeAcceptanceRetention.js +12 -0
  389. package/dist/runtime/wakeAcceptanceStore.d.ts +113 -0
  390. package/dist/runtime/wakeAcceptanceStore.d.ts.map +1 -0
  391. package/dist/runtime/wakeAcceptanceStore.js +558 -0
  392. package/dist/runtime/wakeAcceptanceTypes.d.ts +179 -0
  393. package/dist/runtime/wakeAcceptanceTypes.d.ts.map +1 -0
  394. package/dist/runtime/wakeAcceptanceTypes.js +95 -0
  395. package/dist/runtime/wakeFuse.d.ts +69 -0
  396. package/dist/runtime/wakeFuse.d.ts.map +1 -0
  397. package/dist/runtime/wakeFuse.js +367 -0
  398. package/package.json +25 -17
  399. package/dist/examples/mixed-engine-org.d.ts +0 -2
  400. package/dist/examples/mixed-engine-org.d.ts.map +0 -1
  401. package/dist/examples/mixed-engine-org.js +0 -351
  402. package/dist/examples/mixedEngineCli.d.ts +0 -15
  403. package/dist/examples/mixedEngineCli.d.ts.map +0 -1
  404. package/dist/examples/mixedEngineCli.js +0 -174
@@ -0,0 +1,48 @@
1
+ import type { EngineBrokerServiceRegistration } from "./engineBrokerServiceConfig.js";
2
+ import type { EngineBrokerTurnLimitOverrides } from "./engineBrokerTurnAccounting.js";
3
+ import { type GrokEngineBrokerTurnResult } from "./grokEngineBrokerTurn.js";
4
+ import { type GrokInferenceGrantRequest } from "./grokInferenceGrants.js";
5
+ export { EngineBrokerTurnFailure, type GrokEngineBrokerTurnResult } from "./grokEngineBrokerTurn.js";
6
+ export { finishBrokerTurnWithUsage } from "./grokEngineBrokerMetering.js";
7
+ export type GrokEngineBrokerRegistration = EngineBrokerServiceRegistration;
8
+ export type GrokEngineBroker = Awaited<ReturnType<typeof startGrokEngineBroker>>;
9
+ /**
10
+ * The Grok engine broker: one credential realm, one provider proxy, one MCP
11
+ * facade, and the root-provisioned registrations. Each registration declares
12
+ * its own model/effort (whose worker config bytes are attested), usage ledger,
13
+ * and turn limits (`engineBrokerServiceConfig.ts`).
14
+ *
15
+ * With an `inferenceLedgerPath` the broker also issues evaluator inference
16
+ * grants (`grokInferenceGrants.ts`) over the same credential authority and
17
+ * proxy; their rows go only to that ledger. A stale realm refuses a grant as
18
+ * `auth_stale`, exactly as it fails subject turns.
19
+ */
20
+ export declare function startGrokEngineBroker(options: Readonly<{
21
+ grokCommand: string;
22
+ nativeClient: string;
23
+ credentialHome: string;
24
+ turnStore: string;
25
+ registrations: readonly GrokEngineBrokerRegistration[];
26
+ inferenceLedgerPath?: string;
27
+ }>): Promise<{
28
+ turn(agentId: string, wakeId: string, prompt: string, mcpEndpoint: string, signal?: AbortSignal, limits?: EngineBrokerTurnLimitOverrides): Promise<GrokEngineBrokerTurnResult>;
29
+ requestInferenceGrant(request: GrokInferenceGrantRequest): Readonly<{
30
+ grantId: string;
31
+ token: string;
32
+ purpose: import("./inferenceUsageLedger.js").GrokInferencePurpose;
33
+ policy: import("./grokBrokerModelPolicy.js").GrokBrokerModelPolicy;
34
+ expiresAt: number;
35
+ limits: import("./engineBrokerTurnAccounting.js").EngineBrokerTurnLimits;
36
+ }>;
37
+ releaseInferenceGrant(grantId: string): boolean;
38
+ close(): Promise<void>;
39
+ readiness: () => {
40
+ providerProxyPort: number;
41
+ mcpFacadePort: number;
42
+ registrations: number;
43
+ credentialStale: boolean;
44
+ realmLease: boolean;
45
+ workerIsolation: boolean;
46
+ };
47
+ }>;
48
+ //# sourceMappingURL=grokEngineBroker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grokEngineBroker.d.ts","sourceRoot":"","sources":["../../src/runtime/grokEngineBroker.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,+BAA+B,EAAE,MAAM,gCAAgC,CAAC;AACtF,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,iCAAiC,CAAC;AAOtF,OAAO,EAA2B,KAAK,0BAA0B,EAAE,MAAM,2BAA2B,CAAC;AAErG,OAAO,EAAgE,KAAK,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;AAExI,OAAO,EAAE,uBAAuB,EAAE,KAAK,0BAA0B,EAAE,MAAM,2BAA2B,CAAC;AACrG,OAAO,EAAE,yBAAyB,EAAE,MAAM,+BAA+B,CAAC;AAC1E,MAAM,MAAM,4BAA4B,GAAG,+BAA+B,CAAC;AAC3E,MAAM,MAAM,gBAAgB,GAAG,OAAO,CAAC,UAAU,CAAC,OAAO,qBAAqB,CAAC,CAAC,CAAC;AAEjF;;;;;;;;;;GAUG;AACH,wBAAsB,qBAAqB,CAAC,OAAO,EAAE,QAAQ,CAAC;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,cAAc,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,aAAa,EAAE,SAAS,4BAA4B,EAAE,CAAC;IAAC,mBAAmB,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;kBAYrN,MAAM,UAAU,MAAM,UAAU,MAAM,eAAe,MAAM,WAAW,WAAW,WAAW,8BAA8B,GAAG,OAAO,CAAC,0BAA0B,CAAC;mCAOrJ,yBAAyB;;;;;;;;mCAKzB,MAAM,GAAG,OAAO;aAIhC,OAAO,CAAC,IAAI,CAAC;;;;;;;;;GAG/B"}
@@ -0,0 +1,114 @@
1
+ import { DurableGrokBrokerCredentialAuthority } from "./grokBrokerCredentialAuthority.js";
2
+ import { runNativeBrokerTurn } from "./engineBrokerNativeClient.js";
3
+ import { EngineBrokerTurnRegistry } from "./engineBrokerTurnRegistry.js";
4
+ import { startGrokBrokerProxy } from "./grokBrokerProxy.js";
5
+ import { startEngineBrokerMcpFacade } from "./engineBrokerMcpFacade.js";
6
+ import { acquireGrokBrokerRealmLease } from "./grokBrokerRealmLease.js";
7
+ import { grokBrokerWorkerConfigSha256 } from "./grokBrokerWorkerConfig.js";
8
+ import { parseGrokBrokerModelPolicy } from "./grokBrokerModelPolicy.js";
9
+ import { runGrokEngineBrokerTurn } from "./grokEngineBrokerTurn.js";
10
+ import { createGrokWorkerIsolationGuard, grokBrokerAttestationInput, prepareGrokWorkerAttestation } from "./grokWorkerAttestation.js";
11
+ import { createLedgeredGrokInferenceGrants, GrokInferenceGrantRefused } from "./grokInferenceGrants.js";
12
+ export { EngineBrokerTurnFailure } from "./grokEngineBrokerTurn.js";
13
+ export { finishBrokerTurnWithUsage } from "./grokEngineBrokerMetering.js";
14
+ /**
15
+ * The Grok engine broker: one credential realm, one provider proxy, one MCP
16
+ * facade, and the root-provisioned registrations. Each registration declares
17
+ * its own model/effort (whose worker config bytes are attested), usage ledger,
18
+ * and turn limits (`engineBrokerServiceConfig.ts`).
19
+ *
20
+ * With an `inferenceLedgerPath` the broker also issues evaluator inference
21
+ * grants (`grokInferenceGrants.ts`) over the same credential authority and
22
+ * proxy; their rows go only to that ledger. A stale realm refuses a grant as
23
+ * `auth_stale`, exactly as it fails subject turns.
24
+ */
25
+ export async function startGrokEngineBroker(options) {
26
+ const registrations = new Map(options.registrations.map((entry) => [entry.agentId, { ...entry, model: parseGrokBrokerModelPolicy(entry.model) }]));
27
+ if (registrations.size !== options.registrations.length)
28
+ throw new Error("engine broker registration conflict");
29
+ const attestationFor = (registration) => grokBrokerAttestationInput(registration, [...registrations.values()], grokBrokerWorkerConfigSha256(registration.model));
30
+ const inferenceLedgerPath = options.inferenceLedgerPath;
31
+ const grants = inferenceLedgerPath === undefined ? undefined : createLedgeredGrokInferenceGrants(inferenceLedgerPath);
32
+ const lease = await acquireGrokBrokerRealmLease(options.credentialHome);
33
+ const authority = new DurableGrokBrokerCredentialAuthority(options.grokCommand, options.credentialHome);
34
+ try {
35
+ await authority.initialize();
36
+ }
37
+ catch (error) {
38
+ await lease.close();
39
+ throw error;
40
+ }
41
+ let proxy;
42
+ try {
43
+ proxy = await startGrokBrokerProxy(authority, undefined, undefined, undefined, grants);
44
+ }
45
+ catch (error) {
46
+ await lease.close();
47
+ throw error;
48
+ }
49
+ let mcp;
50
+ try {
51
+ mcp = await startEngineBrokerMcpFacade();
52
+ for (const registration of registrations.values())
53
+ await prepareGrokWorkerAttestation(attestationFor(registration));
54
+ }
55
+ catch (error) {
56
+ if (mcp)
57
+ await mcp.close().catch(() => undefined);
58
+ await proxy.close();
59
+ await lease.close();
60
+ throw error;
61
+ }
62
+ if (!mcp)
63
+ throw new Error("engine broker unavailable");
64
+ const turns = new EngineBrokerTurnRegistry(options.turnStore);
65
+ const active = new Map();
66
+ let closed = false;
67
+ const facade = mcp;
68
+ const deps = {
69
+ turns, proxy, mcp: facade, credentialStale: () => authority.isStale(),
70
+ prepareIsolation: async (registration) => { const attestation = attestationFor(registration); return createGrokWorkerIsolationGuard(attestation, await prepareGrokWorkerAttestation(attestation)); },
71
+ runNative: (input, signal) => runNativeBrokerTurn(options.nativeClient, input, signal)
72
+ };
73
+ return {
74
+ async turn(agentId, wakeId, prompt, mcpEndpoint, signal, limits) {
75
+ if (closed)
76
+ throw new Error("engine broker unavailable");
77
+ const registration = registrations.get(agentId);
78
+ if (registration === undefined)
79
+ throw new Error("engine broker unavailable");
80
+ const controller = new AbortController();
81
+ const onAbort = () => controller.abort();
82
+ signal?.addEventListener("abort", onAbort, { once: true });
83
+ if (signal?.aborted)
84
+ controller.abort();
85
+ const key = `${agentId}\0${wakeId}`;
86
+ const done = runGrokEngineBrokerTurn(deps, registration, wakeId, prompt, mcpEndpoint, controller.signal, limits);
87
+ active.set(key, { controller, done: done.then(() => undefined, () => undefined) });
88
+ try {
89
+ return await done;
90
+ }
91
+ finally {
92
+ signal?.removeEventListener("abort", onAbort);
93
+ active.delete(key);
94
+ }
95
+ },
96
+ requestInferenceGrant(request) {
97
+ if (closed || grants === undefined)
98
+ throw new GrokInferenceGrantRefused("unavailable");
99
+ if (authority.isStale())
100
+ throw new GrokInferenceGrantRefused("auth_stale");
101
+ return grants.issue(request);
102
+ },
103
+ releaseInferenceGrant(grantId) {
104
+ if (grants === undefined)
105
+ throw new GrokInferenceGrantRefused("unavailable");
106
+ return grants.release(grantId);
107
+ },
108
+ async close() { if (closed)
109
+ return; closed = true; grants?.close(); const running = [...active.values()]; for (const entry of running)
110
+ entry.controller.abort(); await Promise.allSettled(running.map((entry) => entry.done)); const results = await Promise.allSettled([facade.close(), proxy.close(), lease.close()]); const failures = results.flatMap((entry) => entry.status === "rejected" ? [entry.reason] : []); if (failures.length)
111
+ throw new AggregateError(failures, "engine broker shutdown failed"); },
112
+ readiness: () => ({ providerProxyPort: proxy.port, mcpFacadePort: 43_124, registrations: registrations.size, credentialStale: authority.isStale(), realmLease: true, workerIsolation: true })
113
+ };
114
+ }
@@ -0,0 +1,52 @@
1
+ import { type GrokTurnRequest } from "./turnRequestLedger.js";
2
+ import { type TurnUsageFailureReason } from "./turnUsageLedger.js";
3
+ import type { EngineBrokerTerminalResponse } from "./engineBrokerProtocol.js";
4
+ /**
5
+ * The exact ledger bytes a terminal broker turn owes, sealed into its turn
6
+ * record *before* they are appended.
7
+ *
8
+ * The record is published first and the ledger appended second, so a crash in
9
+ * between used to leave a sealed turn whose spend never reached the ledger —
10
+ * and a replay never metered. Now a replay re-checks: if the ledger holds no
11
+ * row for this `turn`, it appends these same bytes (same `at`, same numbers).
12
+ * That is completing the original metering, not re-metering: a replay after a
13
+ * normal append finds the row and writes nothing, and readers dedupe on `turn`
14
+ * should two replays race.
15
+ */
16
+ export type BrokerTurnLedgerLines = Readonly<{
17
+ usage: string | null;
18
+ requests: string;
19
+ seal?: string;
20
+ }>;
21
+ /** A v1 record, or a replay with no sealed bytes at all: nothing to append, including no seal. */
22
+ export declare const EMPTY_BROKER_TURN_LEDGER: BrokerTurnLedgerLines;
23
+ export type BrokerTurnLedgerDetail = Readonly<{
24
+ agentId: string;
25
+ wakeId: string;
26
+ notionalUsd: number;
27
+ complete: boolean;
28
+ reason?: TurnUsageFailureReason;
29
+ requests: readonly GrokTurnRequest[];
30
+ session?: string;
31
+ estimatedRequests: number;
32
+ }>;
33
+ export declare function renderBrokerTurnLedger(terminal: EngineBrokerTerminalResponse, detail: BrokerTurnLedgerDetail): BrokerTurnLedgerLines;
34
+ /**
35
+ * Strict check of stored ledger bytes: exactly the turn's own rows,
36
+ * newline-terminated, bounded.
37
+ *
38
+ * `seal` is optional so a record sealed before this stream existed still
39
+ * replays; its absence means the turn owes no seal row, never that one was
40
+ * lost.
41
+ */
42
+ export declare function parseBrokerTurnLedgerLines(value: unknown, turnId: string): BrokerTurnLedgerLines;
43
+ export type BrokerTurnLedgerPaths = Readonly<{
44
+ usageLedgerPath: string;
45
+ requestLedgerPath: string;
46
+ sealLedgerPath: string;
47
+ }>;
48
+ /** Appends sealed lines on the first metering: no presence scan is needed, nothing was appended before the record existed. */
49
+ export declare function appendBrokerTurnLedger(lines: BrokerTurnLedgerLines, paths: BrokerTurnLedgerPaths): Promise<void>;
50
+ /** On replay: append each stream's sealed lines only when that stream (current file or its `.1`) holds no row for this turn. Never rejects. */
51
+ export declare function ensureBrokerTurnLedgered(lines: BrokerTurnLedgerLines, turnId: string, paths: BrokerTurnLedgerPaths): Promise<void>;
52
+ //# sourceMappingURL=grokEngineBrokerLedger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grokEngineBrokerLedger.d.ts","sourceRoot":"","sources":["../../src/runtime/grokEngineBrokerLedger.ts"],"names":[],"mappings":"AAGA,OAAO,EAA8E,KAAK,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAC1I,OAAO,EAAkD,KAAK,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AACnH,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,2BAA2B,CAAC;AAE9E;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,qBAAqB,GAAG,QAAQ,CAAC;IAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC;AACxG,kGAAkG;AAClG,eAAO,MAAM,wBAAwB,EAAE,qBAAoE,CAAC;AAE5G,MAAM,MAAM,sBAAsB,GAAG,QAAQ,CAAC;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,OAAO,CAAC;IAAC,MAAM,CAAC,EAAE,sBAAsB,CAAC;IAAC,QAAQ,EAAE,SAAS,eAAe,EAAE,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAAC,iBAAiB,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC;AAE/O,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,4BAA4B,EAAE,MAAM,EAAE,sBAAsB,GAAG,qBAAqB,CAmBpI;AAKD;;;;;;;GAOG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,qBAAqB,CAqBhG;AAED,MAAM,MAAM,qBAAqB,GAAG,QAAQ,CAAC;IAAE,eAAe,EAAE,MAAM,CAAC;IAAC,iBAAiB,EAAE,MAAM,CAAC;IAAC,cAAc,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC;AAE7H,8HAA8H;AAC9H,wBAAsB,sBAAsB,CAAC,KAAK,EAAE,qBAAqB,EAAE,KAAK,EAAE,qBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC,CAMtH;AAED,+IAA+I;AAC/I,wBAAsB,wBAAwB,CAAC,KAAK,EAAE,qBAAqB,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,qBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC,CAMxI"}
@@ -0,0 +1,114 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import { renderBrokerTurnSealLine, TURN_SEAL_LEDGER_VERSION, TURN_SEAL_MAX_LINE_BYTES } from "./engineBrokerSealLedger.js";
3
+ import { recordLedgerLines, renderGrokTurnRequestLines, TURN_REQUEST_LEDGER_VERSION } from "./turnRequestLedger.js";
4
+ import { renderTurnUsageLine, TURN_USAGE_LEDGER_VERSION } from "./turnUsageLedger.js";
5
+ /** A v1 record, or a replay with no sealed bytes at all: nothing to append, including no seal. */
6
+ export const EMPTY_BROKER_TURN_LEDGER = Object.freeze({ usage: null, requests: "" });
7
+ export function renderBrokerTurnLedger(terminal, detail) {
8
+ const at = new Date().toISOString();
9
+ // The seal row is rendered for *every* terminal turn, including one whose
10
+ // usage is null. That is the whole point: a turn cancelled before any usage
11
+ // could be attributed is exactly the turn whose outstanding MCP call and
12
+ // redacted last words have no other route to the host.
13
+ const seal = renderBrokerTurnSealLine(terminal, { agent: detail.agentId, wake: detail.wakeId, at });
14
+ if (terminal.usage === null)
15
+ return { usage: null, requests: "", seal };
16
+ const { usage } = terminal;
17
+ return {
18
+ seal,
19
+ usage: renderTurnUsageLine({
20
+ agent: detail.agentId, wake: detail.wakeId, engine: "grok", at,
21
+ usage: { input: usage.input, output: usage.output, cacheRead: usage.cacheRead, cacheWrite: usage.cacheWrite, total: usage.total, calls: terminal.requests, notionalUsd: detail.notionalUsd, complete: detail.complete },
22
+ outcome: terminal.kind === "completed" ? { status: "completed" } : { status: "failed", reason: detail.reason ?? "unknown" },
23
+ turn: terminal.turnId, limitReason: terminal.limitReason, model: terminal.model, estimatedRequests: detail.estimatedRequests
24
+ }),
25
+ requests: renderGrokTurnRequestLines({ agent: detail.agentId, wake: detail.wakeId, turn: terminal.turnId, model: terminal.model, requests: detail.requests, requestCount: terminal.requests, at, ...(detail.session === undefined ? {} : { session: detail.session }) })
26
+ };
27
+ }
28
+ const MAX_USAGE_LINE_BYTES = 4_096, MAX_REQUEST_LINES_BYTES = 262_144;
29
+ const rows = (text) => text.split("\n").filter((line) => line.length > 0).map((line) => { const value = JSON.parse(line); if (value === null || typeof value !== "object" || Array.isArray(value))
30
+ throw new Error(); return value; });
31
+ /**
32
+ * Strict check of stored ledger bytes: exactly the turn's own rows,
33
+ * newline-terminated, bounded.
34
+ *
35
+ * `seal` is optional so a record sealed before this stream existed still
36
+ * replays; its absence means the turn owes no seal row, never that one was
37
+ * lost.
38
+ */
39
+ export function parseBrokerTurnLedgerLines(value, turnId) {
40
+ const invalid = () => new Error("broker turn registry unavailable");
41
+ if (value === null || typeof value !== "object" || Array.isArray(value) || !Object.hasOwn(value, "usage") || !Object.hasOwn(value, "requests"))
42
+ throw invalid();
43
+ const sealed = Object.hasOwn(value, "seal");
44
+ if (Object.keys(value).length !== (sealed ? 3 : 2))
45
+ throw invalid();
46
+ const { usage, requests, seal } = value;
47
+ try {
48
+ if (usage !== null) {
49
+ if (typeof usage !== "string" || !usage.endsWith("\n") || Buffer.byteLength(usage) > MAX_USAGE_LINE_BYTES)
50
+ throw invalid();
51
+ const parsed = rows(usage);
52
+ if (parsed.length !== 1 || parsed[0].v !== TURN_USAGE_LEDGER_VERSION || parsed[0].turn !== turnId)
53
+ throw invalid();
54
+ }
55
+ if (typeof requests !== "string" || (requests.length > 0 && (usage === null || !requests.endsWith("\n"))) || Buffer.byteLength(requests) > MAX_REQUEST_LINES_BYTES)
56
+ throw invalid();
57
+ if (rows(requests).some((row) => row.v !== TURN_REQUEST_LEDGER_VERSION || row.turn !== turnId))
58
+ throw invalid();
59
+ if (sealed) {
60
+ if (typeof seal !== "string" || !seal.endsWith("\n") || Buffer.byteLength(seal) > TURN_SEAL_MAX_LINE_BYTES)
61
+ throw invalid();
62
+ const parsed = rows(seal);
63
+ if (parsed.length !== 1 || parsed[0].v !== TURN_SEAL_LEDGER_VERSION || parsed[0].turn !== turnId)
64
+ throw invalid();
65
+ }
66
+ }
67
+ catch {
68
+ throw invalid();
69
+ }
70
+ return { usage: usage, requests, ...(sealed ? { seal: seal } : {}) };
71
+ }
72
+ /** Appends sealed lines on the first metering: no presence scan is needed, nothing was appended before the record existed. */
73
+ export async function appendBrokerTurnLedger(lines, paths) {
74
+ if (lines.usage !== null)
75
+ await recordLedgerLines(paths.usageLedgerPath, lines.usage);
76
+ await recordLedgerLines(paths.requestLedgerPath, lines.requests);
77
+ // Last, and never conditional on usage: a turn with no attributable spend is
78
+ // precisely the one whose seal row is its only account of itself.
79
+ if (lines.seal !== undefined)
80
+ await recordLedgerLines(paths.sealLedgerPath, lines.seal);
81
+ }
82
+ /** On replay: append each stream's sealed lines only when that stream (current file or its `.1`) holds no row for this turn. Never rejects. */
83
+ export async function ensureBrokerTurnLedgered(lines, turnId, paths) {
84
+ try {
85
+ if (lines.usage !== null && !await ledgerHasTurn(paths.usageLedgerPath, turnId))
86
+ await recordLedgerLines(paths.usageLedgerPath, lines.usage);
87
+ if (lines.requests.length > 0 && !await ledgerHasTurn(paths.requestLedgerPath, turnId))
88
+ await recordLedgerLines(paths.requestLedgerPath, lines.requests);
89
+ if (lines.seal !== undefined && !await ledgerHasTurn(paths.sealLedgerPath, turnId))
90
+ await recordLedgerLines(paths.sealLedgerPath, lines.seal);
91
+ }
92
+ catch { /* advisory: a replay never fails on its ledger */ }
93
+ }
94
+ async function ledgerHasTurn(file, turnId) {
95
+ for (const candidate of [`${file}.1`, file]) {
96
+ let text;
97
+ try {
98
+ text = await readFile(candidate, "utf8");
99
+ }
100
+ catch {
101
+ continue;
102
+ }
103
+ for (const line of text.split("\n")) {
104
+ if (!line.includes(turnId))
105
+ continue;
106
+ try {
107
+ if (JSON.parse(line).turn === turnId)
108
+ return true;
109
+ }
110
+ catch { /* a torn line is not this turn's row */ }
111
+ }
112
+ }
113
+ return false;
114
+ }
@@ -0,0 +1,51 @@
1
+ import type { EngineBrokerRequest, EngineBrokerTerminalResponse } from "./engineBrokerProtocol.js";
2
+ import type { EngineBrokerTurnRegistry } from "./engineBrokerTurnRegistry.js";
3
+ import type { GrokTurnRequest } from "./turnRequestLedger.js";
4
+ import type { TurnUsageFailureReason } from "./turnUsageLedger.js";
5
+ export type BrokerTurnMetering = Readonly<{
6
+ usageLedgerPath: string;
7
+ requestLedgerPath: string;
8
+ /** Where the sealed response's operator-visible projection is appended (`engineBrokerSealLedger.ts`). */
9
+ sealLedgerPath: string;
10
+ agentId: string;
11
+ wakeId: string;
12
+ }>;
13
+ export type BrokerTurnMeteringDetail = Readonly<{
14
+ notionalUsd: number;
15
+ complete: boolean;
16
+ reason?: TurnUsageFailureReason;
17
+ requests: readonly GrokTurnRequest[];
18
+ session?: string;
19
+ estimatedRequests: number;
20
+ }>;
21
+ /**
22
+ * Seal a terminal turn, then meter it. The broker is the single writer.
23
+ *
24
+ * Order is load-bearing. The ledger bytes are rendered first and sealed into
25
+ * the durable terminal record together with its accounting
26
+ * (`grokEngineBrokerLedger.ts`); only after `turns.finish` published that
27
+ * record are the same bytes appended. A replayed turn returns before the
28
+ * broker's `try` block and never meters again — it only completes an append a
29
+ * crash interrupted (`ensureBrokerTurnLedgered`), and every row carries the
30
+ * turn id as `turn`, so a reader that sees one twice counts it once.
31
+ *
32
+ * Remaining window, documented rather than closed: a crash before the record's
33
+ * rename (while the turn is still `active`, including mid-turn) makes the next
34
+ * boot seal that turn `failed` with `usage: null`, so its spend is unmetered.
35
+ * Closing it needs the running proxy usage checkpointed into the active record
36
+ * on every request (an fsync'd rewrite per model request); not done here.
37
+ *
38
+ * Both terminal kinds meter: a failed turn spent real tokens, so its partial
39
+ * usage is written with `outcome: failed` and its closed `limitReason`. A turn
40
+ * with no usage at all (`usage: null`) writes no *usage* row — a zero row is
41
+ * byte-identical to a measured zero — but it still writes its seal row, which
42
+ * is a record of what the turn did rather than of what it spent.
43
+ *
44
+ * Appends never reject, so an append failure cannot escape into the caller's
45
+ * `catch` and rewrite a completed turn as failed; the caller also refuses to
46
+ * re-seal a turn this helper already sealed.
47
+ */
48
+ export declare function finishBrokerTurnWithUsage(turns: EngineBrokerTurnRegistry, request: Extract<EngineBrokerRequest, {
49
+ kind: "start_turn";
50
+ }>, terminal: EngineBrokerTerminalResponse, metering: BrokerTurnMetering, detail: BrokerTurnMeteringDetail, onSealed?: () => void): Promise<void>;
51
+ //# sourceMappingURL=grokEngineBrokerMetering.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grokEngineBrokerMetering.d.ts","sourceRoot":"","sources":["../../src/runtime/grokEngineBrokerMetering.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,4BAA4B,EAAE,MAAM,2BAA2B,CAAC;AACnG,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAC;AAE9E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAC9D,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AAEnE,MAAM,MAAM,kBAAkB,GAAG,QAAQ,CAAC;IACxC,eAAe,EAAE,MAAM,CAAC;IACxB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,yGAAyG;IACzG,cAAc,EAAE,MAAM,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC,CAAC;AACH,MAAM,MAAM,wBAAwB,GAAG,QAAQ,CAAC;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,OAAO,CAAC;IAAC,MAAM,CAAC,EAAE,sBAAsB,CAAC;IAAC,QAAQ,EAAE,SAAS,eAAe,EAAE,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAAC,iBAAiB,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC;AAEhN;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAsB,yBAAyB,CAAC,KAAK,EAAE,wBAAwB,EAAE,OAAO,EAAE,OAAO,CAAC,mBAAmB,EAAE;IAAE,IAAI,EAAE,YAAY,CAAA;CAAE,CAAC,EAAE,QAAQ,EAAE,4BAA4B,EAAE,QAAQ,EAAE,kBAAkB,EAAE,MAAM,EAAE,wBAAwB,EAAE,QAAQ,GAAE,MAAM,IAAsB,GAAG,OAAO,CAAC,IAAI,CAAC,CAK7S"}
@@ -0,0 +1,34 @@
1
+ import { appendBrokerTurnLedger, renderBrokerTurnLedger } from "./grokEngineBrokerLedger.js";
2
+ /**
3
+ * Seal a terminal turn, then meter it. The broker is the single writer.
4
+ *
5
+ * Order is load-bearing. The ledger bytes are rendered first and sealed into
6
+ * the durable terminal record together with its accounting
7
+ * (`grokEngineBrokerLedger.ts`); only after `turns.finish` published that
8
+ * record are the same bytes appended. A replayed turn returns before the
9
+ * broker's `try` block and never meters again — it only completes an append a
10
+ * crash interrupted (`ensureBrokerTurnLedgered`), and every row carries the
11
+ * turn id as `turn`, so a reader that sees one twice counts it once.
12
+ *
13
+ * Remaining window, documented rather than closed: a crash before the record's
14
+ * rename (while the turn is still `active`, including mid-turn) makes the next
15
+ * boot seal that turn `failed` with `usage: null`, so its spend is unmetered.
16
+ * Closing it needs the running proxy usage checkpointed into the active record
17
+ * on every request (an fsync'd rewrite per model request); not done here.
18
+ *
19
+ * Both terminal kinds meter: a failed turn spent real tokens, so its partial
20
+ * usage is written with `outcome: failed` and its closed `limitReason`. A turn
21
+ * with no usage at all (`usage: null`) writes no *usage* row — a zero row is
22
+ * byte-identical to a measured zero — but it still writes its seal row, which
23
+ * is a record of what the turn did rather than of what it spent.
24
+ *
25
+ * Appends never reject, so an append failure cannot escape into the caller's
26
+ * `catch` and rewrite a completed turn as failed; the caller also refuses to
27
+ * re-seal a turn this helper already sealed.
28
+ */
29
+ export async function finishBrokerTurnWithUsage(turns, request, terminal, metering, detail, onSealed = () => undefined) {
30
+ const lines = renderBrokerTurnLedger(terminal, { ...detail, agentId: metering.agentId, wakeId: metering.wakeId });
31
+ await turns.finish(request, terminal, lines);
32
+ onSealed();
33
+ await appendBrokerTurnLedger(lines, metering);
34
+ }
@@ -0,0 +1,58 @@
1
+ import { type EngineBrokerFailureCode } from "./engineBrokerProtocol.js";
2
+ import type { EngineBrokerMcpCallObservation } from "./engineBrokerMcpCallLog.js";
3
+ import { type EngineBrokerTurnAccounting, type EngineBrokerTurnLimitOverrides } from "./engineBrokerTurnAccounting.js";
4
+ import { type NativeBrokerDiagnostic, type NativeBrokerTurn, type NativeBrokerTurnResult } from "./engineBrokerNativeClient.js";
5
+ import type { EngineBrokerTurnRegistry } from "./engineBrokerTurnRegistry.js";
6
+ import { type EngineBrokerServiceRegistration } from "./engineBrokerServiceConfig.js";
7
+ import type { GrokBrokerProxyTurn } from "./grokBrokerProxy.js";
8
+ export type GrokEngineBrokerTurnResult = Readonly<{
9
+ text: string;
10
+ workerPid: number;
11
+ workerUid: number;
12
+ workerStartTime: string;
13
+ }> & EngineBrokerTurnAccounting;
14
+ export declare class EngineBrokerTurnFailure extends Error {
15
+ readonly code: Exclude<EngineBrokerFailureCode, "turn_conflict" | "unavailable">;
16
+ readonly diagnostic?: NativeBrokerDiagnostic | undefined;
17
+ readonly accounting?: EngineBrokerTurnAccounting | undefined;
18
+ readonly mcpCalls?: EngineBrokerMcpCallObservation | undefined;
19
+ constructor(code: Exclude<EngineBrokerFailureCode, "turn_conflict" | "unavailable">, diagnostic?: NativeBrokerDiagnostic | undefined, accounting?: EngineBrokerTurnAccounting | undefined, mcpCalls?: EngineBrokerMcpCallObservation | undefined);
20
+ }
21
+ /** Everything one broker turn touches, injected so the accounting and limit paths run under test without a native launcher. */
22
+ export type GrokEngineBrokerTurnDependencies = Readonly<{
23
+ turns: EngineBrokerTurnRegistry;
24
+ proxy: Readonly<{
25
+ capabilities: Readonly<{
26
+ issue(agentId: string, turnId: string): string;
27
+ revoke(turnId: string): void;
28
+ }>;
29
+ registerIsolationGuard(turnId: string, guard: () => Promise<void>): void;
30
+ revokeIsolationGuard(turnId: string): void;
31
+ registerTurn(turnId: string, turn: GrokBrokerProxyTurn): void;
32
+ revokeTurn(turnId: string): void;
33
+ }>;
34
+ mcp: Readonly<{
35
+ register(agentId: string, turnId: string, endpoint: string): string;
36
+ revoke(turnId: string): void;
37
+ observe?(turnId: string): EngineBrokerMcpCallObservation | undefined;
38
+ }>;
39
+ credentialStale(): boolean;
40
+ prepareIsolation(registration: EngineBrokerServiceRegistration): Promise<() => Promise<void>>;
41
+ runNative(input: NativeBrokerTurn, signal: AbortSignal): Promise<Readonly<NativeBrokerTurnResult>>;
42
+ }>;
43
+ /**
44
+ * One brokered Grok turn under its declared limits.
45
+ *
46
+ * The limits are the registration's, lowered (never raised) by the wake. The
47
+ * proxy meter refuses model requests past `maxRequests`, past the elapsed
48
+ * deadline, or once the upstream-reported running total reached `maxTokens`,
49
+ * and a tripped limit aborts the worker through the same cancel/kill path a
50
+ * client cancellation uses. A wall-clock timer trips `timeout` for a worker
51
+ * that is mid-request.
52
+ *
53
+ * Every terminal path — completed, failed, limit, cancelled — is sealed and
54
+ * metered through {@link finishBrokerTurnWithUsage}; a replayed turn returns its
55
+ * sealed accounting before any of this runs and never meters again.
56
+ */
57
+ export declare function runGrokEngineBrokerTurn(deps: GrokEngineBrokerTurnDependencies, registration: EngineBrokerServiceRegistration, wakeId: string, prompt: string, mcpEndpoint: string, signal?: AbortSignal, overrides?: EngineBrokerTurnLimitOverrides): Promise<GrokEngineBrokerTurnResult>;
58
+ //# sourceMappingURL=grokEngineBrokerTurn.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grokEngineBrokerTurn.d.ts","sourceRoot":"","sources":["../../src/runtime/grokEngineBrokerTurn.ts"],"names":[],"mappings":"AAIA,OAAO,EAAyB,KAAK,uBAAuB,EAAqC,MAAM,2BAA2B,CAAC;AACnI,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,6BAA6B,CAAC;AAClF,OAAO,EAAqD,KAAK,0BAA0B,EAAE,KAAK,8BAA8B,EAA8B,MAAM,iCAAiC,CAAC;AACtM,OAAO,EAA2B,KAAK,sBAAsB,EAAE,KAAK,gBAAgB,EAAE,KAAK,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACzJ,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAC;AAC9E,OAAO,EAAmE,KAAK,+BAA+B,EAAE,MAAM,gCAAgC,CAAC;AAGvJ,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAIhE,MAAM,MAAM,0BAA0B,GAAG,QAAQ,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,eAAe,EAAE,MAAM,CAAA;CAAE,CAAC,GAAG,0BAA0B,CAAC;AAChK,qBAAa,uBAAwB,SAAQ,KAAK;IACpC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,uBAAuB,EAAE,eAAe,GAAG,aAAa,CAAC;IAAE,QAAQ,CAAC,UAAU,CAAC,EAAE,sBAAsB;IAAE,QAAQ,CAAC,UAAU,CAAC,EAAE,0BAA0B;IAAE,QAAQ,CAAC,QAAQ,CAAC,EAAE,8BAA8B;gBAA3N,IAAI,EAAE,OAAO,CAAC,uBAAuB,EAAE,eAAe,GAAG,aAAa,CAAC,EAAW,UAAU,CAAC,EAAE,sBAAsB,YAAA,EAAW,UAAU,CAAC,EAAE,0BAA0B,YAAA,EAAW,QAAQ,CAAC,EAAE,8BAA8B,YAAA;CACjP;AAED,+HAA+H;AAC/H,MAAM,MAAM,gCAAgC,GAAG,QAAQ,CAAC;IACtD,KAAK,EAAE,wBAAwB,CAAC;IAChC,KAAK,EAAE,QAAQ,CAAC;QAAE,YAAY,EAAE,QAAQ,CAAC;YAAE,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC;YAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;SAAE,CAAC,CAAC;QAAC,sBAAsB,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;QAAC,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;QAAC,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,mBAAmB,GAAG,IAAI,CAAC;QAAC,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,CAAC,CAAC;IACrV,GAAG,EAAE,QAAQ,CAAC;QAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC;QAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;QAAC,OAAO,CAAC,CAAC,MAAM,EAAE,MAAM,GAAG,8BAA8B,GAAG,SAAS,CAAA;KAAE,CAAC,CAAC;IAC3L,eAAe,IAAI,OAAO,CAAC;IAC3B,gBAAgB,CAAC,YAAY,EAAE,+BAA+B,GAAG,OAAO,CAAC,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;IAC9F,SAAS,CAAC,KAAK,EAAE,gBAAgB,EAAE,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,QAAQ,CAAC,sBAAsB,CAAC,CAAC,CAAC;CACpG,CAAC,CAAC;AAKH;;;;;;;;;;;;;GAaG;AACH,wBAAsB,uBAAuB,CAAC,IAAI,EAAE,gCAAgC,EAAE,YAAY,EAAE,+BAA+B,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,WAAW,EAAE,SAAS,CAAC,EAAE,8BAA8B,GAAG,OAAO,CAAC,0BAA0B,CAAC,CA2D/R"}