@noopolis/daimon 0.1.2 → 0.2.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.
- package/README.md +275 -51
- package/dist/contracts/attentionContract.d.ts +142 -0
- package/dist/contracts/attentionContract.d.ts.map +1 -0
- package/dist/contracts/attentionContract.js +21 -0
- package/dist/contracts/canonicalJson.d.ts +3 -0
- package/dist/contracts/canonicalJson.d.ts.map +1 -0
- package/dist/contracts/canonicalJson.js +54 -0
- package/dist/contracts/grokBrokerTaskClock.d.ts +18 -0
- package/dist/contracts/grokBrokerTaskClock.d.ts.map +1 -0
- package/dist/contracts/grokBrokerTaskClock.js +16 -0
- package/dist/contracts/grokWorkerContract.d.ts +74 -0
- package/dist/contracts/grokWorkerContract.d.ts.map +1 -0
- package/dist/contracts/grokWorkerContract.js +88 -0
- package/dist/contracts/organizationRuntimeContract.d.ts +841 -0
- package/dist/contracts/organizationRuntimeContract.d.ts.map +1 -0
- package/dist/contracts/organizationRuntimeContract.js +123 -0
- package/dist/contracts/runtimeContractManifest.d.ts +1822 -0
- package/dist/contracts/runtimeContractManifest.d.ts.map +1 -0
- package/dist/contracts/runtimeContractManifest.js +206 -0
- package/dist/contracts/shellEnvironmentPolicy.d.ts +3 -0
- package/dist/contracts/shellEnvironmentPolicy.d.ts.map +1 -0
- package/dist/contracts/shellEnvironmentPolicy.js +5 -0
- package/dist/contracts/wakeMomentContract.d.ts +61 -0
- package/dist/contracts/wakeMomentContract.d.ts.map +1 -0
- package/dist/contracts/wakeMomentContract.js +27 -0
- package/dist/core/credentialRedaction.d.ts +4 -0
- package/dist/core/credentialRedaction.d.ts.map +1 -0
- package/dist/core/credentialRedaction.js +28 -0
- package/dist/core/types.d.ts +2 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/examples/jungianPlayAgent.d.ts +1 -0
- package/dist/examples/jungianPlayAgent.d.ts.map +1 -1
- package/dist/examples/jungianPlayAgent.js +6 -2
- package/dist/mcp/toolServer.d.ts +19 -2
- package/dist/mcp/toolServer.d.ts.map +1 -1
- package/dist/mcp/toolServer.js +57 -17
- package/dist/observability/causalEvents.d.ts +6 -6
- package/dist/observability/causalEvents.d.ts.map +1 -1
- package/dist/observability/causalEvents.js +107 -19
- package/dist/observability/orgObserver.d.ts.map +1 -1
- package/dist/observability/orgObserver.js +3 -3
- package/dist/pi/agyHeadlessResult.d.ts +60 -0
- package/dist/pi/agyHeadlessResult.d.ts.map +1 -0
- package/dist/pi/agyHeadlessResult.js +95 -0
- package/dist/pi/cliChildOutput.d.ts +67 -0
- package/dist/pi/cliChildOutput.d.ts.map +1 -0
- package/dist/pi/cliChildOutput.js +269 -0
- package/dist/pi/cliEngineRun.d.ts +16 -0
- package/dist/pi/cliEngineRun.d.ts.map +1 -0
- package/dist/pi/cliEngineRun.js +27 -0
- package/dist/pi/cliEngineSpawn.d.ts +55 -0
- package/dist/pi/cliEngineSpawn.d.ts.map +1 -0
- package/dist/pi/cliEngineSpawn.js +169 -0
- package/dist/pi/cliEnvironment.d.ts +12 -0
- package/dist/pi/cliEnvironment.d.ts.map +1 -0
- package/dist/pi/cliEnvironment.js +34 -0
- package/dist/pi/cliMcpRegistration.d.ts +61 -0
- package/dist/pi/cliMcpRegistration.d.ts.map +1 -0
- package/dist/pi/cliMcpRegistration.js +75 -0
- package/dist/pi/cliProcess.d.ts +7 -0
- package/dist/pi/cliProcess.d.ts.map +1 -0
- package/dist/pi/cliProcess.js +132 -0
- package/dist/pi/cliSession.d.ts +86 -24
- package/dist/pi/cliSession.d.ts.map +1 -1
- package/dist/pi/cliSession.js +251 -173
- package/dist/pi/cliSessionMcp.d.ts +9 -0
- package/dist/pi/cliSessionMcp.d.ts.map +1 -0
- package/dist/pi/cliSessionMcp.js +80 -0
- package/dist/pi/cliTurnMetering.d.ts +60 -0
- package/dist/pi/cliTurnMetering.d.ts.map +1 -0
- package/dist/pi/cliTurnMetering.js +60 -0
- package/dist/pi/codexFilesystemRules.d.ts +3 -0
- package/dist/pi/codexFilesystemRules.d.ts.map +1 -0
- package/dist/pi/codexFilesystemRules.js +28 -0
- package/dist/pi/codexHeadlessResult.d.ts +68 -0
- package/dist/pi/codexHeadlessResult.d.ts.map +1 -0
- package/dist/pi/codexHeadlessResult.js +122 -0
- package/dist/pi/codexRolloutUsage.d.ts +95 -0
- package/dist/pi/codexRolloutUsage.d.ts.map +1 -0
- package/dist/pi/codexRolloutUsage.js +221 -0
- package/dist/pi/engineTaskClock.d.ts +9 -0
- package/dist/pi/engineTaskClock.d.ts.map +1 -0
- package/dist/pi/engineTaskClock.js +28 -0
- package/dist/pi/engineWakeLimits.d.ts +39 -0
- package/dist/pi/engineWakeLimits.d.ts.map +1 -0
- package/dist/pi/engineWakeLimits.js +55 -0
- package/dist/pi/grokHeadlessResult.d.ts +36 -0
- package/dist/pi/grokHeadlessResult.d.ts.map +1 -0
- package/dist/pi/grokHeadlessResult.js +118 -0
- package/dist/pi/grokHomeMcpRegistration.d.ts +21 -0
- package/dist/pi/grokHomeMcpRegistration.d.ts.map +1 -0
- package/dist/pi/grokHomeMcpRegistration.js +58 -0
- package/dist/pi/grokSandbox.d.ts +11 -0
- package/dist/pi/grokSandbox.d.ts.map +1 -0
- package/dist/pi/grokSandbox.js +216 -0
- package/dist/pi/grokStreamUsage.d.ts +31 -0
- package/dist/pi/grokStreamUsage.d.ts.map +1 -0
- package/dist/pi/grokStreamUsage.js +51 -0
- package/dist/pi/memoryClock.d.ts +25 -0
- package/dist/pi/memoryClock.d.ts.map +1 -0
- package/dist/pi/memoryClock.js +18 -0
- package/dist/pi/piAgentHandle.d.ts +6 -3
- package/dist/pi/piAgentHandle.d.ts.map +1 -1
- package/dist/pi/piAgentHandle.js +42 -12
- package/dist/pi/piAgentWakeSupport.d.ts +35 -0
- package/dist/pi/piAgentWakeSupport.d.ts.map +1 -1
- package/dist/pi/piAgentWakeSupport.js +4 -0
- package/dist/pi/piHarness.d.ts +18 -3
- package/dist/pi/piHarness.d.ts.map +1 -1
- package/dist/pi/piHarness.js +83 -14
- package/dist/pi/prompts.d.ts +1 -0
- package/dist/pi/prompts.d.ts.map +1 -1
- package/dist/pi/prompts.js +24 -0
- package/dist/pi/taskClockProcess.d.ts +6 -0
- package/dist/pi/taskClockProcess.d.ts.map +1 -0
- package/dist/pi/taskClockProcess.js +8 -0
- package/dist/pi/turnCausal.d.ts +4 -2
- package/dist/pi/turnCausal.d.ts.map +1 -1
- package/dist/pi/turnCausal.js +4 -4
- package/dist/pi/turnTrace.d.ts.map +1 -1
- package/dist/pi/turnTrace.js +5 -8
- package/dist/pi/wakeAcceptance.d.ts +1 -1
- package/dist/pi/wakeAcceptance.d.ts.map +1 -1
- package/dist/pi/wakeAcceptance.js +2 -2
- package/dist/pi/worldTrajectory.d.ts +1 -1
- package/dist/pi/worldTrajectory.d.ts.map +1 -1
- package/dist/pi/worldTrajectory.js +3 -3
- package/dist/runtime/agySubscriptionBootstrap.d.ts +16 -0
- package/dist/runtime/agySubscriptionBootstrap.d.ts.map +1 -0
- package/dist/runtime/agySubscriptionBootstrap.js +80 -0
- package/dist/runtime/agySubscriptionRealm.d.ts +38 -0
- package/dist/runtime/agySubscriptionRealm.d.ts.map +1 -0
- package/dist/runtime/agySubscriptionRealm.js +242 -0
- package/dist/runtime/attention.d.ts +26 -0
- package/dist/runtime/attention.d.ts.map +1 -0
- package/dist/runtime/attention.js +49 -0
- package/dist/runtime/attentionDispatcher.d.ts +78 -0
- package/dist/runtime/attentionDispatcher.d.ts.map +1 -0
- package/dist/runtime/attentionDispatcher.js +365 -0
- package/dist/runtime/cli.d.ts +7 -0
- package/dist/runtime/cli.d.ts.map +1 -0
- package/dist/runtime/cli.js +256 -0
- package/dist/runtime/codexSandboxProjection.d.ts +17 -0
- package/dist/runtime/codexSandboxProjection.d.ts.map +1 -0
- package/dist/runtime/codexSandboxProjection.js +40 -0
- package/dist/runtime/contract-manifest.json +1 -0
- package/dist/runtime/contract-manifest.sha256 +1 -0
- package/dist/runtime/contractManifest.d.ts +8 -0
- package/dist/runtime/contractManifest.d.ts.map +1 -0
- package/dist/runtime/contractManifest.js +4 -0
- package/dist/runtime/engineBrokerCapabilities.d.ts +40 -0
- package/dist/runtime/engineBrokerCapabilities.d.ts.map +1 -0
- package/dist/runtime/engineBrokerCapabilities.js +69 -0
- package/dist/runtime/engineBrokerControlClient.d.ts +55 -0
- package/dist/runtime/engineBrokerControlClient.d.ts.map +1 -0
- package/dist/runtime/engineBrokerControlClient.js +228 -0
- package/dist/runtime/engineBrokerCredentialJournal.d.ts +12 -0
- package/dist/runtime/engineBrokerCredentialJournal.d.ts.map +1 -0
- package/dist/runtime/engineBrokerCredentialJournal.js +26 -0
- package/dist/runtime/engineBrokerInferenceProtocol.d.ts +53 -0
- package/dist/runtime/engineBrokerInferenceProtocol.d.ts.map +1 -0
- package/dist/runtime/engineBrokerInferenceProtocol.js +65 -0
- package/dist/runtime/engineBrokerMcpCallLog.d.ts +171 -0
- package/dist/runtime/engineBrokerMcpCallLog.d.ts.map +1 -0
- package/dist/runtime/engineBrokerMcpCallLog.js +231 -0
- package/dist/runtime/engineBrokerMcpFacade.d.ts +59 -0
- package/dist/runtime/engineBrokerMcpFacade.d.ts.map +1 -0
- package/dist/runtime/engineBrokerMcpFacade.js +346 -0
- package/dist/runtime/engineBrokerNativeClient.d.ts +52 -0
- package/dist/runtime/engineBrokerNativeClient.d.ts.map +1 -0
- package/dist/runtime/engineBrokerNativeClient.js +151 -0
- package/dist/runtime/engineBrokerProtocol.d.ts +167 -0
- package/dist/runtime/engineBrokerProtocol.d.ts.map +1 -0
- package/dist/runtime/engineBrokerProtocol.js +357 -0
- package/dist/runtime/engineBrokerSealLedger.d.ts +70 -0
- package/dist/runtime/engineBrokerSealLedger.d.ts.map +1 -0
- package/dist/runtime/engineBrokerSealLedger.js +132 -0
- package/dist/runtime/engineBrokerService.d.ts +43 -0
- package/dist/runtime/engineBrokerService.d.ts.map +1 -0
- package/dist/runtime/engineBrokerService.js +196 -0
- package/dist/runtime/engineBrokerServiceCli.d.ts +5 -0
- package/dist/runtime/engineBrokerServiceCli.d.ts.map +1 -0
- package/dist/runtime/engineBrokerServiceCli.js +32 -0
- package/dist/runtime/engineBrokerServiceConfig.d.ts +55 -0
- package/dist/runtime/engineBrokerServiceConfig.d.ts.map +1 -0
- package/dist/runtime/engineBrokerServiceConfig.js +130 -0
- package/dist/runtime/engineBrokerSingleflight.d.ts +13 -0
- package/dist/runtime/engineBrokerSingleflight.d.ts.map +1 -0
- package/dist/runtime/engineBrokerSingleflight.js +24 -0
- package/dist/runtime/engineBrokerTurnAccounting.d.ts +76 -0
- package/dist/runtime/engineBrokerTurnAccounting.d.ts.map +1 -0
- package/dist/runtime/engineBrokerTurnAccounting.js +108 -0
- package/dist/runtime/engineBrokerTurnRegistry.d.ts +57 -0
- package/dist/runtime/engineBrokerTurnRegistry.d.ts.map +1 -0
- package/dist/runtime/engineBrokerTurnRegistry.js +159 -0
- package/dist/runtime/engineDispatcher.d.ts +38 -0
- package/dist/runtime/engineDispatcher.d.ts.map +1 -0
- package/dist/runtime/engineDispatcher.js +228 -0
- package/dist/runtime/engineReadiness.d.ts +23 -0
- package/dist/runtime/engineReadiness.d.ts.map +1 -0
- package/dist/runtime/engineReadiness.js +222 -0
- package/dist/runtime/grokAuthenticationError.d.ts +8 -0
- package/dist/runtime/grokAuthenticationError.d.ts.map +1 -0
- package/dist/runtime/grokAuthenticationError.js +20 -0
- package/dist/runtime/grokBrokerCredentialAuthority.d.ts +23 -0
- package/dist/runtime/grokBrokerCredentialAuthority.d.ts.map +1 -0
- package/dist/runtime/grokBrokerCredentialAuthority.js +150 -0
- package/dist/runtime/grokBrokerCredentialReader.d.ts +6 -0
- package/dist/runtime/grokBrokerCredentialReader.d.ts.map +1 -0
- package/dist/runtime/grokBrokerCredentialReader.js +38 -0
- package/dist/runtime/grokBrokerModelPolicy.d.ts +23 -0
- package/dist/runtime/grokBrokerModelPolicy.d.ts.map +1 -0
- package/dist/runtime/grokBrokerModelPolicy.js +28 -0
- package/dist/runtime/grokBrokerProjection.d.ts +114 -0
- package/dist/runtime/grokBrokerProjection.d.ts.map +1 -0
- package/dist/runtime/grokBrokerProjection.js +101 -0
- package/dist/runtime/grokBrokerProviderClock.d.ts +26 -0
- package/dist/runtime/grokBrokerProviderClock.d.ts.map +1 -0
- package/dist/runtime/grokBrokerProviderClock.js +133 -0
- package/dist/runtime/grokBrokerProxy.d.ts +69 -0
- package/dist/runtime/grokBrokerProxy.d.ts.map +1 -0
- package/dist/runtime/grokBrokerProxy.js +326 -0
- package/dist/runtime/grokBrokerProxyJson.d.ts +10 -0
- package/dist/runtime/grokBrokerProxyJson.d.ts.map +1 -0
- package/dist/runtime/grokBrokerProxyJson.js +31 -0
- package/dist/runtime/grokBrokerProxyRequest.d.ts +30 -0
- package/dist/runtime/grokBrokerProxyRequest.d.ts.map +1 -0
- package/dist/runtime/grokBrokerProxyRequest.js +85 -0
- package/dist/runtime/grokBrokerRealmLease.d.ts +4 -0
- package/dist/runtime/grokBrokerRealmLease.d.ts.map +1 -0
- package/dist/runtime/grokBrokerRealmLease.js +33 -0
- package/dist/runtime/grokBrokerRefresh.d.ts +2 -0
- package/dist/runtime/grokBrokerRefresh.d.ts.map +1 -0
- package/dist/runtime/grokBrokerRefresh.js +15 -0
- package/dist/runtime/grokBrokerRunBudget.d.ts +20 -0
- package/dist/runtime/grokBrokerRunBudget.d.ts.map +1 -0
- package/dist/runtime/grokBrokerRunBudget.js +85 -0
- package/dist/runtime/grokBrokerSlot.d.ts +6 -0
- package/dist/runtime/grokBrokerSlot.d.ts.map +1 -0
- package/dist/runtime/grokBrokerSlot.js +21 -0
- package/dist/runtime/grokBrokerTaskClock.d.ts +32 -0
- package/dist/runtime/grokBrokerTaskClock.d.ts.map +1 -0
- package/dist/runtime/grokBrokerTaskClock.js +120 -0
- package/dist/runtime/grokBrokerTurnMeter.d.ts +180 -0
- package/dist/runtime/grokBrokerTurnMeter.d.ts.map +1 -0
- package/dist/runtime/grokBrokerTurnMeter.js +337 -0
- package/dist/runtime/grokBrokerWorkerConfig.d.ts +89 -0
- package/dist/runtime/grokBrokerWorkerConfig.d.ts.map +1 -0
- package/dist/runtime/grokBrokerWorkerConfig.js +154 -0
- package/dist/runtime/grokEngineBroker.d.ts +88 -0
- package/dist/runtime/grokEngineBroker.d.ts.map +1 -0
- package/dist/runtime/grokEngineBroker.js +202 -0
- package/dist/runtime/grokEngineBrokerLedger.d.ts +52 -0
- package/dist/runtime/grokEngineBrokerLedger.d.ts.map +1 -0
- package/dist/runtime/grokEngineBrokerLedger.js +114 -0
- package/dist/runtime/grokEngineBrokerMetering.d.ts +51 -0
- package/dist/runtime/grokEngineBrokerMetering.d.ts.map +1 -0
- package/dist/runtime/grokEngineBrokerMetering.js +34 -0
- package/dist/runtime/grokEngineBrokerTurn.d.ts +82 -0
- package/dist/runtime/grokEngineBrokerTurn.d.ts.map +1 -0
- package/dist/runtime/grokEngineBrokerTurn.js +348 -0
- package/dist/runtime/grokEngineBrokerTurnEnd.d.ts +88 -0
- package/dist/runtime/grokEngineBrokerTurnEnd.d.ts.map +1 -0
- package/dist/runtime/grokEngineBrokerTurnEnd.js +148 -0
- package/dist/runtime/grokInferenceClientConfig.d.ts +41 -0
- package/dist/runtime/grokInferenceClientConfig.d.ts.map +1 -0
- package/dist/runtime/grokInferenceClientConfig.js +62 -0
- package/dist/runtime/grokInferenceGrants.d.ts +77 -0
- package/dist/runtime/grokInferenceGrants.d.ts.map +1 -0
- package/dist/runtime/grokInferenceGrants.js +116 -0
- package/dist/runtime/grokInferenceProxy.d.ts +29 -0
- package/dist/runtime/grokInferenceProxy.d.ts.map +1 -0
- package/dist/runtime/grokInferenceProxy.js +85 -0
- package/dist/runtime/grokInferenceProxyRequest.d.ts +23 -0
- package/dist/runtime/grokInferenceProxyRequest.d.ts.map +1 -0
- package/dist/runtime/grokInferenceProxyRequest.js +63 -0
- package/dist/runtime/grokSlotPreflightReceipt.d.ts +70 -0
- package/dist/runtime/grokSlotPreflightReceipt.d.ts.map +1 -0
- package/dist/runtime/grokSlotPreflightReceipt.js +101 -0
- package/dist/runtime/grokSubscriptionRealm.d.ts +4 -0
- package/dist/runtime/grokSubscriptionRealm.d.ts.map +1 -0
- package/dist/runtime/grokSubscriptionRealm.js +435 -0
- package/dist/runtime/grokSubscriptionRealmTypes.d.ts +26 -0
- package/dist/runtime/grokSubscriptionRealmTypes.d.ts.map +1 -0
- package/dist/runtime/grokSubscriptionRealmTypes.js +1 -0
- package/dist/runtime/grokWorkerAttestation.d.ts +175 -0
- package/dist/runtime/grokWorkerAttestation.d.ts.map +1 -0
- package/dist/runtime/grokWorkerAttestation.js +266 -0
- package/dist/runtime/grokWorkerDenyPlacement.d.ts +80 -0
- package/dist/runtime/grokWorkerDenyPlacement.d.ts.map +1 -0
- package/dist/runtime/grokWorkerDenyPlacement.js +128 -0
- package/dist/runtime/grokWorkerHomeAttestation.d.ts +32 -0
- package/dist/runtime/grokWorkerHomeAttestation.d.ts.map +1 -0
- package/dist/runtime/grokWorkerHomeAttestation.js +75 -0
- package/dist/runtime/grokWorkerSandboxProfile.d.ts +27 -0
- package/dist/runtime/grokWorkerSandboxProfile.d.ts.map +1 -0
- package/dist/runtime/grokWorkerSandboxProfile.js +44 -0
- package/dist/runtime/grokWorkerTmpAttestation.d.ts +55 -0
- package/dist/runtime/grokWorkerTmpAttestation.d.ts.map +1 -0
- package/dist/runtime/grokWorkerTmpAttestation.js +63 -0
- package/dist/runtime/index.d.ts +31 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +27 -0
- package/dist/runtime/inferenceUsageLedger.d.ts +38 -0
- package/dist/runtime/inferenceUsageLedger.d.ts.map +1 -0
- package/dist/runtime/inferenceUsageLedger.js +53 -0
- package/dist/runtime/mcpToolResult.d.ts +127 -0
- package/dist/runtime/mcpToolResult.d.ts.map +1 -0
- package/dist/runtime/mcpToolResult.js +214 -0
- package/dist/runtime/moltnetMachineRead.d.ts +97 -0
- package/dist/runtime/moltnetMachineRead.d.ts.map +1 -0
- package/dist/runtime/moltnetMachineRead.js +183 -0
- package/dist/runtime/native/daimon-engine-broker +0 -0
- package/dist/runtime/organizationRuntime.d.ts +168 -0
- package/dist/runtime/organizationRuntime.d.ts.map +1 -0
- package/dist/runtime/organizationRuntime.js +3 -0
- package/dist/runtime/organizationRuntimeAdmission.d.ts +6 -0
- package/dist/runtime/organizationRuntimeAdmission.d.ts.map +1 -0
- package/dist/runtime/organizationRuntimeAdmission.js +22 -0
- package/dist/runtime/organizationRuntimeControl.d.ts +51 -0
- package/dist/runtime/organizationRuntimeControl.d.ts.map +1 -0
- package/dist/runtime/organizationRuntimeControl.js +283 -0
- package/dist/runtime/organizationRuntimeHost.d.ts +19 -0
- package/dist/runtime/organizationRuntimeHost.d.ts.map +1 -0
- package/dist/runtime/organizationRuntimeHost.js +368 -0
- package/dist/runtime/organizationRuntimeParsing.d.ts +6 -0
- package/dist/runtime/organizationRuntimeParsing.d.ts.map +1 -0
- package/dist/runtime/organizationRuntimeParsing.js +373 -0
- package/dist/runtime/organizationRuntimeReadiness.d.ts +12 -0
- package/dist/runtime/organizationRuntimeReadiness.d.ts.map +1 -0
- package/dist/runtime/organizationRuntimeReadiness.js +67 -0
- package/dist/runtime/organizationRuntimeRecording.d.ts +7 -0
- package/dist/runtime/organizationRuntimeRecording.d.ts.map +1 -0
- package/dist/runtime/organizationRuntimeRecording.js +58 -0
- package/dist/runtime/physicalReadiness.d.ts +59 -0
- package/dist/runtime/physicalReadiness.d.ts.map +1 -0
- package/dist/runtime/physicalReadiness.js +176 -0
- package/dist/runtime/portableCredentialAuth.d.ts +6 -0
- package/dist/runtime/portableCredentialAuth.d.ts.map +1 -0
- package/dist/runtime/portableCredentialAuth.js +70 -0
- package/dist/runtime/portableCredentialMaterial.d.ts +8 -0
- package/dist/runtime/portableCredentialMaterial.d.ts.map +1 -0
- package/dist/runtime/portableCredentialMaterial.js +169 -0
- package/dist/runtime/productionAgentTools.d.ts +5 -0
- package/dist/runtime/productionAgentTools.d.ts.map +1 -0
- package/dist/runtime/productionAgentTools.js +336 -0
- package/dist/runtime/relocateRuntimeConfig.d.ts +14 -0
- package/dist/runtime/relocateRuntimeConfig.d.ts.map +1 -0
- package/dist/runtime/relocateRuntimeConfig.js +38 -0
- package/dist/runtime/runtimeHomeLayout.d.ts +48 -0
- package/dist/runtime/runtimeHomeLayout.d.ts.map +1 -0
- package/dist/runtime/runtimeHomeLayout.js +84 -0
- package/dist/runtime/schedule.d.ts +40 -0
- package/dist/runtime/schedule.d.ts.map +1 -0
- package/dist/runtime/schedule.js +376 -0
- package/dist/runtime/storeCoordination.d.ts +33 -0
- package/dist/runtime/storeCoordination.d.ts.map +1 -0
- package/dist/runtime/storeCoordination.js +117 -0
- package/dist/runtime/taskClock.d.ts +42 -0
- package/dist/runtime/taskClock.d.ts.map +1 -0
- package/dist/runtime/taskClock.js +102 -0
- package/dist/runtime/taskClockProcess.d.ts +17 -0
- package/dist/runtime/taskClockProcess.d.ts.map +1 -0
- package/dist/runtime/taskClockProcess.js +58 -0
- package/dist/runtime/toolResultSpill.d.ts +152 -0
- package/dist/runtime/toolResultSpill.d.ts.map +1 -0
- package/dist/runtime/toolResultSpill.js +351 -0
- package/dist/runtime/turnRequestLedger.d.ts +155 -0
- package/dist/runtime/turnRequestLedger.d.ts.map +1 -0
- package/dist/runtime/turnRequestLedger.js +251 -0
- package/dist/runtime/turnUsageLedger.d.ts +143 -0
- package/dist/runtime/turnUsageLedger.d.ts.map +1 -0
- package/dist/runtime/turnUsageLedger.js +203 -0
- package/dist/runtime/wakeAcceptanceClaimDiagnostics.d.ts +34 -0
- package/dist/runtime/wakeAcceptanceClaimDiagnostics.d.ts.map +1 -0
- package/dist/runtime/wakeAcceptanceClaimDiagnostics.js +79 -0
- package/dist/runtime/wakeAcceptanceIndex.d.ts +55 -0
- package/dist/runtime/wakeAcceptanceIndex.d.ts.map +1 -0
- package/dist/runtime/wakeAcceptanceIndex.js +158 -0
- package/dist/runtime/wakeAcceptanceProcessIdentity.d.ts +25 -0
- package/dist/runtime/wakeAcceptanceProcessIdentity.d.ts.map +1 -0
- package/dist/runtime/wakeAcceptanceProcessIdentity.js +54 -0
- package/dist/runtime/wakeAcceptanceReconciliation.d.ts +211 -0
- package/dist/runtime/wakeAcceptanceReconciliation.d.ts.map +1 -0
- package/dist/runtime/wakeAcceptanceReconciliation.js +289 -0
- package/dist/runtime/wakeAcceptanceRecord.d.ts +25 -0
- package/dist/runtime/wakeAcceptanceRecord.d.ts.map +1 -0
- package/dist/runtime/wakeAcceptanceRecord.js +58 -0
- package/dist/runtime/wakeAcceptanceRetention.d.ts +36 -0
- package/dist/runtime/wakeAcceptanceRetention.d.ts.map +1 -0
- package/dist/runtime/wakeAcceptanceRetention.js +38 -0
- package/dist/runtime/wakeAcceptanceStore.d.ts +113 -0
- package/dist/runtime/wakeAcceptanceStore.d.ts.map +1 -0
- package/dist/runtime/wakeAcceptanceStore.js +561 -0
- package/dist/runtime/wakeAcceptanceTypes.d.ts +179 -0
- package/dist/runtime/wakeAcceptanceTypes.d.ts.map +1 -0
- package/dist/runtime/wakeAcceptanceTypes.js +95 -0
- package/dist/runtime/wakeFuse.d.ts +124 -0
- package/dist/runtime/wakeFuse.d.ts.map +1 -0
- package/dist/runtime/wakeFuse.js +466 -0
- package/dist/runtime/wakeMomentFs.d.ts +27 -0
- package/dist/runtime/wakeMomentFs.d.ts.map +1 -0
- package/dist/runtime/wakeMomentFs.js +223 -0
- package/dist/runtime/wakeMomentHistory.d.ts +26 -0
- package/dist/runtime/wakeMomentHistory.d.ts.map +1 -0
- package/dist/runtime/wakeMomentHistory.js +109 -0
- package/dist/runtime/wakeMomentIo.d.ts +22 -0
- package/dist/runtime/wakeMomentIo.d.ts.map +1 -0
- package/dist/runtime/wakeMomentIo.js +114 -0
- package/dist/runtime/wakeMomentRecorder.d.ts +33 -0
- package/dist/runtime/wakeMomentRecorder.d.ts.map +1 -0
- package/dist/runtime/wakeMomentRecorder.js +81 -0
- package/dist/runtime/wakeMomentRetention.d.ts +7 -0
- package/dist/runtime/wakeMomentRetention.d.ts.map +1 -0
- package/dist/runtime/wakeMomentRetention.js +143 -0
- package/dist/runtime/wakeMomentRows.d.ts +8 -0
- package/dist/runtime/wakeMomentRows.d.ts.map +1 -0
- package/dist/runtime/wakeMomentRows.js +66 -0
- package/dist/runtime/wakeMomentSnapshot.d.ts +56 -0
- package/dist/runtime/wakeMomentSnapshot.d.ts.map +1 -0
- package/dist/runtime/wakeMomentSnapshot.js +248 -0
- package/package.json +22 -11
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { needsBrokerTaskClockReconciliation, writeBrokerTaskClock } from "./grokBrokerTaskClock.js";
|
|
2
|
+
import { DurableGrokBrokerCredentialAuthority } from "./grokBrokerCredentialAuthority.js";
|
|
3
|
+
import { runNativeBrokerTurn } from "./engineBrokerNativeClient.js";
|
|
4
|
+
import type { EngineBrokerServiceRegistration } from "./engineBrokerServiceConfig.js";
|
|
5
|
+
import type { EngineBrokerTurnLimitOverrides } from "./engineBrokerTurnAccounting.js";
|
|
6
|
+
import { startGrokBrokerProxy } from "./grokBrokerProxy.js";
|
|
7
|
+
import { acquireGrokBrokerRealmLease } from "./grokBrokerRealmLease.js";
|
|
8
|
+
import { type GrokEngineBrokerTurnDependencies, type GrokEngineBrokerTurnResult } from "./grokEngineBrokerTurn.js";
|
|
9
|
+
import { createGrokWorkerIsolationGuard, prepareGrokWorkerAttestation } from "./grokWorkerAttestation.js";
|
|
10
|
+
import { grokWorkerIdentityEmpty } from "./grokEngineBrokerTurnEnd.js";
|
|
11
|
+
import { type GrokInferenceGrantRequest } from "./grokInferenceGrants.js";
|
|
12
|
+
export { EngineBrokerTurnFailure, type GrokEngineBrokerTurnResult } from "./grokEngineBrokerTurn.js";
|
|
13
|
+
export { finishBrokerTurnWithUsage } from "./grokEngineBrokerMetering.js";
|
|
14
|
+
export type GrokEngineBrokerRegistration = EngineBrokerServiceRegistration;
|
|
15
|
+
export type GrokEngineBroker = Awaited<ReturnType<typeof startGrokEngineBroker>>;
|
|
16
|
+
/**
|
|
17
|
+
* The Grok engine broker: one credential realm, one provider proxy, one MCP
|
|
18
|
+
* facade, and the root-provisioned registrations. Each registration declares
|
|
19
|
+
* its own model/effort (whose worker config bytes are attested), usage ledger,
|
|
20
|
+
* and turn limits (`engineBrokerServiceConfig.ts`).
|
|
21
|
+
*
|
|
22
|
+
* With an `inferenceLedgerPath` the broker also issues evaluator inference
|
|
23
|
+
* grants (`grokInferenceGrants.ts`) over the same credential authority and
|
|
24
|
+
* proxy; their rows go only to that ledger. A stale realm refuses a grant as
|
|
25
|
+
* `auth_stale`, exactly as it fails subject turns.
|
|
26
|
+
*/
|
|
27
|
+
/** Internal seams exercise the production startup and per-turn admission paths. */
|
|
28
|
+
export type GrokEngineBrokerDependencies = Readonly<{
|
|
29
|
+
acquireLease: typeof acquireGrokBrokerRealmLease;
|
|
30
|
+
createAuthority: (command: string, home: string) => Pick<DurableGrokBrokerCredentialAuthority, "initialize" | "accessToken" | "markRejected" | "isStale">;
|
|
31
|
+
startProxy: (...args: Parameters<typeof startGrokBrokerProxy>) => Promise<GrokEngineBrokerTurnDependencies["proxy"] & {
|
|
32
|
+
port: number;
|
|
33
|
+
close(): Promise<void>;
|
|
34
|
+
}>;
|
|
35
|
+
startMcp: () => Promise<GrokEngineBrokerTurnDependencies["mcp"] & {
|
|
36
|
+
close(): Promise<void>;
|
|
37
|
+
}>;
|
|
38
|
+
prepareAttestation: typeof prepareGrokWorkerAttestation;
|
|
39
|
+
needsTaskClockReconciliation: typeof needsBrokerTaskClockReconciliation;
|
|
40
|
+
createIsolationGuard: typeof createGrokWorkerIsolationGuard;
|
|
41
|
+
writeTaskClock: typeof writeBrokerTaskClock;
|
|
42
|
+
workerIdentityEmpty: typeof grokWorkerIdentityEmpty;
|
|
43
|
+
runNative: typeof runNativeBrokerTurn;
|
|
44
|
+
}>;
|
|
45
|
+
export declare function startGrokEngineBroker(options: Readonly<{
|
|
46
|
+
grokCommand: string;
|
|
47
|
+
nativeClient: string;
|
|
48
|
+
credentialHome: string;
|
|
49
|
+
turnStore: string;
|
|
50
|
+
registrations: readonly GrokEngineBrokerRegistration[];
|
|
51
|
+
inferenceLedgerPath?: string;
|
|
52
|
+
maxRunRequests?: number;
|
|
53
|
+
runRequestStore?: string;
|
|
54
|
+
}>, dependencies?: GrokEngineBrokerDependencies): Promise<{
|
|
55
|
+
turn: (agentId: string, wakeId: string, prompt: string, mcpEndpoint: string, signal?: AbortSignal | undefined, limits?: Readonly<Partial<Readonly<{
|
|
56
|
+
maxRequests: number;
|
|
57
|
+
maxTokens: number;
|
|
58
|
+
timeoutMs: number;
|
|
59
|
+
}>>> | undefined, processClock?: string | undefined, slot?: number | undefined) => Promise<GrokEngineBrokerTurnResult>;
|
|
60
|
+
resetSlot(slot: number): Promise<void>;
|
|
61
|
+
requestInferenceGrant(request: GrokInferenceGrantRequest): Readonly<{
|
|
62
|
+
grantId: string;
|
|
63
|
+
token: string;
|
|
64
|
+
purpose: import("./inferenceUsageLedger.js").GrokInferencePurpose;
|
|
65
|
+
policy: import("./grokBrokerModelPolicy.js").GrokBrokerModelPolicy;
|
|
66
|
+
expiresAt: number;
|
|
67
|
+
limits: import("./engineBrokerTurnAccounting.js").EngineBrokerTurnLimits;
|
|
68
|
+
}>;
|
|
69
|
+
releaseInferenceGrant(grantId: string): boolean;
|
|
70
|
+
close(): Promise<void>;
|
|
71
|
+
readiness: () => {
|
|
72
|
+
providerProxyPort: number;
|
|
73
|
+
mcpFacadePort: number;
|
|
74
|
+
registrations: number;
|
|
75
|
+
credentialStale: boolean;
|
|
76
|
+
realmLease: boolean;
|
|
77
|
+
workerIsolation: boolean;
|
|
78
|
+
};
|
|
79
|
+
}>;
|
|
80
|
+
/** @internal One slot identity carries one turn until cleanup settles, including clock publication. */
|
|
81
|
+
export type GrokBrokerActiveTurns = Map<number, {
|
|
82
|
+
controller: AbortController;
|
|
83
|
+
done: Promise<void>;
|
|
84
|
+
}>;
|
|
85
|
+
type TurnArguments = [wakeId: string, prompt: string, mcpEndpoint: string, signal?: AbortSignal, limits?: EngineBrokerTurnLimitOverrides, processClock?: string];
|
|
86
|
+
/** The production dispatch boundary, isolated from credential/network setup for lifecycle tests. */
|
|
87
|
+
export declare function createGrokEngineBrokerTurnDispatch(registrations: ReadonlyMap<number, GrokEngineBrokerRegistration>, active: GrokBrokerActiveTurns, isClosed: () => boolean, run: (registration: GrokEngineBrokerRegistration, ...args: TurnArguments) => Promise<GrokEngineBrokerTurnResult>, resetting?: ReadonlySet<number>): (agentId: string, ...input: [...TurnArguments, slot?: number]) => Promise<GrokEngineBrokerTurnResult>;
|
|
88
|
+
//# sourceMappingURL=grokEngineBroker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grokEngineBroker.d.ts","sourceRoot":"","sources":["../../src/runtime/grokEngineBroker.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,kCAAkC,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AACpG,OAAO,EAAE,oCAAoC,EAAE,MAAM,oCAAoC,CAAC;AAC1F,OAAO,EAAE,mBAAmB,EAAE,MAAM,+BAA+B,CAAC;AACpE,OAAO,KAAK,EAAE,+BAA+B,EAAE,MAAM,gCAAgC,CAAC;AACtF,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,iCAAiC,CAAC;AAItF,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAE5D,OAAO,EAAE,2BAA2B,EAAE,MAAM,2BAA2B,CAAC;AAGxE,OAAO,EAEL,KAAK,gCAAgC,EAAE,KAAK,0BAA0B,EACvE,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,8BAA8B,EAA4B,4BAA4B,EAAE,MAAM,4BAA4B,CAAC;AACpI,OAAO,EAA0D,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AAC/H,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,mFAAmF;AACnF,MAAM,MAAM,4BAA4B,GAAG,QAAQ,CAAC;IAClD,YAAY,EAAE,OAAO,2BAA2B,CAAC;IACjD,eAAe,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC,oCAAoC,EAC3F,YAAY,GAAG,aAAa,GAAG,cAAc,GAAG,SAAS,CAAC,CAAC;IAC7D,UAAU,EAAE,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,OAAO,oBAAoB,CAAC,KAAK,OAAO,CACvE,gCAAgC,CAAC,OAAO,CAAC,GAAG;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;KAAE,CAAC,CAAC;IACxF,QAAQ,EAAE,MAAM,OAAO,CAAC,gCAAgC,CAAC,KAAK,CAAC,GAAG;QAAE,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;KAAE,CAAC,CAAC;IAC9F,kBAAkB,EAAE,OAAO,4BAA4B,CAAC;IACxD,4BAA4B,EAAE,OAAO,kCAAkC,CAAC;IACxE,oBAAoB,EAAE,OAAO,8BAA8B,CAAC;IAC5D,cAAc,EAAE,OAAO,oBAAoB,CAAC;IAC5C,mBAAmB,EAAE,OAAO,uBAAuB,CAAC;IACpD,SAAS,EAAE,OAAO,mBAAmB,CAAC;CACvC,CAAC,CAAC;AAeH,wBAAsB,qBAAqB,CACzC,OAAO,EAAE,QAAQ,CAAC;IAChB,WAAW,EAAE,MAAM,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,cAAc,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IACrF,aAAa,EAAE,SAAS,4BAA4B,EAAE,CAAC;IAAC,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAAC,cAAc,CAAC,EAAE,MAAM,CAAC;IAAC,eAAe,CAAC,EAAE,MAAM,CAAC;CACzI,CAAC,EACF,YAAY,GAAE,4BAAiD;oBA0IxC,MAAM;;;;uFAAgD,OAAO,CAAC,0BAA0B,CAAC;oBAzDxF,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;mCAkBb,yBAAyB;;;;;;;;mCAKzB,MAAM,GAAG,OAAO;aAIhC,OAAO,CAAC,IAAI,CAAC;;;;;;;;;GAiB/B;AAED,uGAAuG;AACvG,MAAM,MAAM,qBAAqB,GAAG,GAAG,CAAC,MAAM,EAAE;IAAE,UAAU,EAAE,eAAe,CAAC;IAAC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,CAAA;CAAE,CAAC,CAAC;AACtG,KAAK,aAAa,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,WAAW,EAAE,MAAM,CAAC,EAAE,8BAA8B,EAAE,YAAY,CAAC,EAAE,MAAM,CAAC,CAAC;AAEjK,oGAAoG;AACpG,wBAAgB,kCAAkC,CAChD,aAAa,EAAE,WAAW,CAAC,MAAM,EAAE,4BAA4B,CAAC,EAAE,MAAM,EAAE,qBAAqB,EAC/F,QAAQ,EAAE,MAAM,OAAO,EACvB,GAAG,EAAE,CAAC,YAAY,EAAE,4BAA4B,EAAE,GAAG,IAAI,EAAE,aAAa,KAAK,OAAO,CAAC,0BAA0B,CAAC,EAChH,SAAS,GAAE,WAAW,CAAC,MAAM,CAAa,IAE5B,SAAS,MAAM,EAAE,GAAG,OAAO,CAAC,GAAG,aAAa,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,KAAG,OAAO,CAAC,0BAA0B,CAAC,CAiBjH"}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { rm } from "node:fs/promises";
|
|
3
|
+
import { assertBrokerTaskClockIsolation, brokerTaskClockFileFor } from "./grokBrokerSlot.js";
|
|
4
|
+
import { needsBrokerTaskClockReconciliation, writeBrokerTaskClock } from "./grokBrokerTaskClock.js";
|
|
5
|
+
import { DurableGrokBrokerCredentialAuthority } from "./grokBrokerCredentialAuthority.js";
|
|
6
|
+
import { runNativeBrokerTurn } from "./engineBrokerNativeClient.js";
|
|
7
|
+
import { ENGINE_BROKER_TASK_CLOCK_RESET_FAILED } from "./engineBrokerProtocol.js";
|
|
8
|
+
import { EngineBrokerTurnRegistry } from "./engineBrokerTurnRegistry.js";
|
|
9
|
+
import { GrokBrokerRunBudget } from "./grokBrokerRunBudget.js";
|
|
10
|
+
import { startGrokBrokerProxy } from "./grokBrokerProxy.js";
|
|
11
|
+
import { startEngineBrokerMcpFacade } from "./engineBrokerMcpFacade.js";
|
|
12
|
+
import { acquireGrokBrokerRealmLease } from "./grokBrokerRealmLease.js";
|
|
13
|
+
import { grokBrokerWorkerConfigPins } from "./grokBrokerWorkerConfig.js";
|
|
14
|
+
import { parseGrokBrokerModelPolicy } from "./grokBrokerModelPolicy.js";
|
|
15
|
+
import { EngineBrokerTurnFailure, runGrokEngineBrokerTurn } from "./grokEngineBrokerTurn.js";
|
|
16
|
+
import { createGrokWorkerIsolationGuard, grokBrokerAttestationInput, prepareGrokWorkerAttestation } from "./grokWorkerAttestation.js";
|
|
17
|
+
import { awaitGrokWorkerIdentityEmpty, GROK_WORKER_REAP_WAIT_MS, grokWorkerIdentityEmpty } from "./grokEngineBrokerTurnEnd.js";
|
|
18
|
+
import { createLedgeredGrokInferenceGrants, GrokInferenceGrantRefused } from "./grokInferenceGrants.js";
|
|
19
|
+
export { EngineBrokerTurnFailure } from "./grokEngineBrokerTurn.js";
|
|
20
|
+
export { finishBrokerTurnWithUsage } from "./grokEngineBrokerMetering.js";
|
|
21
|
+
const brokerDependencies = {
|
|
22
|
+
acquireLease: acquireGrokBrokerRealmLease,
|
|
23
|
+
createAuthority: (command, home) => new DurableGrokBrokerCredentialAuthority(command, home),
|
|
24
|
+
startProxy: startGrokBrokerProxy,
|
|
25
|
+
startMcp: startEngineBrokerMcpFacade,
|
|
26
|
+
prepareAttestation: prepareGrokWorkerAttestation,
|
|
27
|
+
needsTaskClockReconciliation: needsBrokerTaskClockReconciliation,
|
|
28
|
+
createIsolationGuard: createGrokWorkerIsolationGuard,
|
|
29
|
+
writeTaskClock: writeBrokerTaskClock,
|
|
30
|
+
workerIdentityEmpty: (uid) => grokWorkerIdentityEmpty(uid, undefined, true),
|
|
31
|
+
runNative: runNativeBrokerTurn
|
|
32
|
+
};
|
|
33
|
+
export async function startGrokEngineBroker(options, dependencies = brokerDependencies) {
|
|
34
|
+
const registrations = new Map(options.registrations.map((entry) => [
|
|
35
|
+
entry.slot, { ...entry, model: parseGrokBrokerModelPolicy(entry.model) }
|
|
36
|
+
]));
|
|
37
|
+
if (registrations.size !== options.registrations.length || new Set(options.registrations.map((entry) => entry.workerUid)).size !== options.registrations.length)
|
|
38
|
+
throw new Error("engine broker registration conflict");
|
|
39
|
+
const attestationFor = (registration) => grokBrokerAttestationInput(registration, [...registrations.values()], grokBrokerWorkerConfigPins(registration.model, registration.slot));
|
|
40
|
+
const grants = options.inferenceLedgerPath === undefined
|
|
41
|
+
? undefined : createLedgeredGrokInferenceGrants(options.inferenceLedgerPath);
|
|
42
|
+
const lease = await dependencies.acquireLease(options.credentialHome);
|
|
43
|
+
const authority = dependencies.createAuthority(options.grokCommand, options.credentialHome);
|
|
44
|
+
let proxy;
|
|
45
|
+
let mcp;
|
|
46
|
+
let runBudget;
|
|
47
|
+
const clockedSlots = new Set();
|
|
48
|
+
try {
|
|
49
|
+
// Reconcile before accepting traffic. A prior broker may have died after
|
|
50
|
+
// publishing an offset; its finally never ran. The realm lease excludes peers.
|
|
51
|
+
for (const registration of registrations.values()) {
|
|
52
|
+
const attestation = attestationFor(registration);
|
|
53
|
+
if (!await dependencies.needsTaskClockReconciliation(registration.profilePath, attestation.configSha256, brokerTaskClockFileFor(registration.slot)))
|
|
54
|
+
continue;
|
|
55
|
+
const snapshot = await dependencies.prepareAttestation(attestation);
|
|
56
|
+
assertBrokerTaskClockIsolation(snapshot.configVariant, registration.slot, [...registrations.keys()], snapshot.denyPaths);
|
|
57
|
+
const reaped = await awaitGrokWorkerIdentityEmpty(registration.workerUid, GROK_WORKER_REAP_WAIT_MS, dependencies.workerIdentityEmpty);
|
|
58
|
+
if (!reaped)
|
|
59
|
+
throw new Error("Grok broker task clock worker did not terminate");
|
|
60
|
+
await dependencies.writeTaskClock(undefined, brokerTaskClockFileFor(registration.slot));
|
|
61
|
+
clockedSlots.add(registration.slot);
|
|
62
|
+
}
|
|
63
|
+
await authority.initialize();
|
|
64
|
+
runBudget = options.maxRunRequests === undefined ? undefined :
|
|
65
|
+
await GrokBrokerRunBudget.open(options.maxRunRequests, path.join(options.runRequestStore ?? options.turnStore, "run-requests.json"));
|
|
66
|
+
proxy = await dependencies.startProxy(authority, undefined, undefined, undefined, grants, runBudget);
|
|
67
|
+
mcp = await dependencies.startMcp();
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
grants?.close();
|
|
71
|
+
await Promise.allSettled([mcp?.close(), proxy?.close(), runBudget?.close()]);
|
|
72
|
+
await Promise.allSettled([lease.close()]);
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
const slotStore = (slot) => path.join(options.turnStore, `slot-${slot}`);
|
|
76
|
+
const turnStores = new Map([...registrations.keys()].map((slot) => [slot, new EngineBrokerTurnRegistry(slotStore(slot))]));
|
|
77
|
+
const active = new Map();
|
|
78
|
+
let closed = false;
|
|
79
|
+
const clockFailed = new Set();
|
|
80
|
+
const resetting = new Set();
|
|
81
|
+
const resets = new Map();
|
|
82
|
+
const facade = mcp;
|
|
83
|
+
const provider = proxy;
|
|
84
|
+
const depsFor = (registration) => ({
|
|
85
|
+
turns: turnStores.get(registration.slot), proxy: provider, mcp: facade, credentialStale: () => authority.isStale(),
|
|
86
|
+
clockedSlot: clockedSlots.has(registration.slot),
|
|
87
|
+
writeTaskClock: (value) => dependencies.writeTaskClock(value, brokerTaskClockFileFor(registration.slot)),
|
|
88
|
+
workerIdentityEmpty: dependencies.workerIdentityEmpty,
|
|
89
|
+
taskClockFailed: () => { clockFailed.add(registration.slot); },
|
|
90
|
+
prepareIsolation: async (registration) => {
|
|
91
|
+
const attestation = attestationFor(registration);
|
|
92
|
+
const snapshot = await dependencies.prepareAttestation(attestation);
|
|
93
|
+
assertBrokerTaskClockIsolation(snapshot.configVariant, registration.slot, [...registrations.keys()], snapshot.denyPaths);
|
|
94
|
+
if (snapshot.configVariant === "clocked")
|
|
95
|
+
clockedSlots.add(registration.slot);
|
|
96
|
+
return {
|
|
97
|
+
guard: dependencies.createIsolationGuard(attestation, snapshot),
|
|
98
|
+
configVariant: snapshot.configVariant
|
|
99
|
+
};
|
|
100
|
+
},
|
|
101
|
+
runNative: (input, signal) => dependencies.runNative(options.nativeClient, input, signal)
|
|
102
|
+
});
|
|
103
|
+
return {
|
|
104
|
+
turn: createGrokEngineBrokerTurnDispatch(registrations, active, () => closed, async (registration, ...args) => {
|
|
105
|
+
if (clockFailed.has(registration.slot))
|
|
106
|
+
throw new EngineBrokerTurnFailure("unavailable", undefined, { outcome: "failed", usage: null, model: registration.model.model, requests: 0, limitReason: "none" }, undefined, undefined, undefined, ENGINE_BROKER_TASK_CLOCK_RESET_FAILED);
|
|
107
|
+
return runGrokEngineBrokerTurn(depsFor(registration), registration, ...args);
|
|
108
|
+
}, resetting),
|
|
109
|
+
async resetSlot(slot) {
|
|
110
|
+
const registration = registrations.get(slot);
|
|
111
|
+
if (closed || registration === undefined)
|
|
112
|
+
throw new Error("engine broker unavailable");
|
|
113
|
+
if (active.has(slot) || resetting.has(slot))
|
|
114
|
+
throw new Error("engine broker slot busy");
|
|
115
|
+
resetting.add(slot);
|
|
116
|
+
const reset = (async () => {
|
|
117
|
+
try {
|
|
118
|
+
const empty = await awaitGrokWorkerIdentityEmpty(registration.workerUid, GROK_WORKER_REAP_WAIT_MS, dependencies.workerIdentityEmpty);
|
|
119
|
+
if (!empty)
|
|
120
|
+
throw new Error("engine broker slot busy");
|
|
121
|
+
if (clockedSlots.has(slot))
|
|
122
|
+
await dependencies.writeTaskClock(undefined, brokerTaskClockFileFor(slot));
|
|
123
|
+
await rm(slotStore(slot), { recursive: true, force: true });
|
|
124
|
+
turnStores.set(slot, new EngineBrokerTurnRegistry(slotStore(slot)));
|
|
125
|
+
clockFailed.delete(slot);
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
clockFailed.add(slot);
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
})();
|
|
132
|
+
resets.set(slot, reset);
|
|
133
|
+
try {
|
|
134
|
+
await reset;
|
|
135
|
+
}
|
|
136
|
+
finally {
|
|
137
|
+
resetting.delete(slot);
|
|
138
|
+
resets.delete(slot);
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
requestInferenceGrant(request) {
|
|
142
|
+
if (closed || grants === undefined)
|
|
143
|
+
throw new GrokInferenceGrantRefused("unavailable");
|
|
144
|
+
if (authority.isStale())
|
|
145
|
+
throw new GrokInferenceGrantRefused("auth_stale");
|
|
146
|
+
return grants.issue(request);
|
|
147
|
+
},
|
|
148
|
+
releaseInferenceGrant(grantId) {
|
|
149
|
+
if (grants === undefined)
|
|
150
|
+
throw new GrokInferenceGrantRefused("unavailable");
|
|
151
|
+
return grants.release(grantId);
|
|
152
|
+
},
|
|
153
|
+
async close() {
|
|
154
|
+
if (closed)
|
|
155
|
+
return;
|
|
156
|
+
closed = true;
|
|
157
|
+
grants?.close();
|
|
158
|
+
const running = [...active.values()];
|
|
159
|
+
for (const entry of running)
|
|
160
|
+
entry.controller.abort();
|
|
161
|
+
await Promise.allSettled([...running.map((entry) => entry.done), ...resets.values()]);
|
|
162
|
+
const results = await Promise.allSettled([facade.close(), provider.close(), runBudget?.close()]);
|
|
163
|
+
results.push(...await Promise.allSettled([lease.close()]));
|
|
164
|
+
const failures = results.flatMap((entry) => entry.status === "rejected" ? [entry.reason] : []);
|
|
165
|
+
if (failures.length)
|
|
166
|
+
throw new AggregateError(failures, "engine broker shutdown failed");
|
|
167
|
+
},
|
|
168
|
+
readiness: () => ({
|
|
169
|
+
providerProxyPort: provider.port, mcpFacadePort: 43_124, registrations: registrations.size,
|
|
170
|
+
credentialStale: authority.isStale(), realmLease: true, workerIsolation: clockFailed.size === 0
|
|
171
|
+
})
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
/** The production dispatch boundary, isolated from credential/network setup for lifecycle tests. */
|
|
175
|
+
export function createGrokEngineBrokerTurnDispatch(registrations, active, isClosed, run, resetting = new Set()) {
|
|
176
|
+
return async (agentId, ...input) => {
|
|
177
|
+
const [wakeId, prompt, mcpEndpoint, signal_, limits, processClock, requestedSlot] = input;
|
|
178
|
+
const matches = [...registrations.values()].filter((entry) => entry.agentId === agentId);
|
|
179
|
+
const registration = requestedSlot === undefined ? (matches.length === 1 ? matches[0] : undefined) : registrations.get(requestedSlot);
|
|
180
|
+
const args = [wakeId, prompt, mcpEndpoint, signal_, limits, processClock];
|
|
181
|
+
if (isClosed() || registration === undefined || registration.agentId !== agentId)
|
|
182
|
+
throw new Error("engine broker unavailable");
|
|
183
|
+
const slot = registration.slot;
|
|
184
|
+
if (active.has(slot) || resetting.has(slot))
|
|
185
|
+
throw new Error("engine broker slot already active");
|
|
186
|
+
const signal = args[3], controller = new AbortController();
|
|
187
|
+
const onAbort = () => controller.abort();
|
|
188
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
189
|
+
if (signal?.aborted)
|
|
190
|
+
controller.abort();
|
|
191
|
+
args[3] = controller.signal;
|
|
192
|
+
const done = Promise.resolve().then(() => run(registration, ...args));
|
|
193
|
+
active.set(slot, { controller, done: done.then(() => undefined, () => undefined) });
|
|
194
|
+
try {
|
|
195
|
+
return await done;
|
|
196
|
+
}
|
|
197
|
+
finally {
|
|
198
|
+
signal?.removeEventListener("abort", onAbort);
|
|
199
|
+
active.delete(slot);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
}
|
|
@@ -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,82 @@
|
|
|
1
|
+
import type { GrokWorkerConfigVariant } from "../contracts/grokBrokerTaskClock.js";
|
|
2
|
+
import { writeBrokerTaskClock, type BrokerShellClock, type BrokerClockObservation } from "./grokBrokerTaskClock.js";
|
|
3
|
+
import { type GrokBrokerProviderClockObservation } from "./grokBrokerProviderClock.js";
|
|
4
|
+
import { type EngineBrokerCleanupObservation, type EngineBrokerFailureCode } from "./engineBrokerProtocol.js";
|
|
5
|
+
import type { EngineBrokerMcpCallObservation } from "./engineBrokerMcpCallLog.js";
|
|
6
|
+
import { type EngineBrokerTurnAccounting, type EngineBrokerTurnLimitOverrides } from "./engineBrokerTurnAccounting.js";
|
|
7
|
+
import { type NativeBrokerDiagnostic, type NativeBrokerTurn, type NativeBrokerTurnResult } from "./engineBrokerNativeClient.js";
|
|
8
|
+
import type { EngineBrokerTurnRegistry } from "./engineBrokerTurnRegistry.js";
|
|
9
|
+
import { type EngineBrokerServiceRegistration } from "./engineBrokerServiceConfig.js";
|
|
10
|
+
import type { GrokBrokerProxyTurn } from "./grokBrokerProxy.js";
|
|
11
|
+
import { type GrokBrokerTurnEndTiming, type GrokWorkerProcess } from "./grokEngineBrokerTurnEnd.js";
|
|
12
|
+
import type { EngineBrokerMcpActivity } from "./engineBrokerMcpCallLog.js";
|
|
13
|
+
export type GrokEngineBrokerTurnResult = Readonly<{
|
|
14
|
+
text: string;
|
|
15
|
+
workerPid: number;
|
|
16
|
+
workerUid: number;
|
|
17
|
+
workerStartTime: string;
|
|
18
|
+
}> & EngineBrokerTurnAccounting & BrokerClockObservation & EngineBrokerCleanupObservation;
|
|
19
|
+
export declare class EngineBrokerTurnFailure extends Error {
|
|
20
|
+
readonly code: Exclude<EngineBrokerFailureCode, "turn_conflict">;
|
|
21
|
+
readonly diagnostic?: NativeBrokerDiagnostic | undefined;
|
|
22
|
+
readonly accounting?: EngineBrokerTurnAccounting | undefined;
|
|
23
|
+
readonly mcpCalls?: EngineBrokerMcpCallObservation | undefined;
|
|
24
|
+
readonly shellClock?: BrokerShellClock | undefined;
|
|
25
|
+
readonly providerClock?: GrokBrokerProviderClockObservation | undefined;
|
|
26
|
+
readonly cleanupDiagnostic?: EngineBrokerCleanupObservation["cleanupDiagnostic"];
|
|
27
|
+
constructor(code: Exclude<EngineBrokerFailureCode, "turn_conflict">, diagnostic?: NativeBrokerDiagnostic | undefined, accounting?: EngineBrokerTurnAccounting | undefined, mcpCalls?: EngineBrokerMcpCallObservation | undefined, shellClock?: BrokerShellClock | undefined, providerClock?: GrokBrokerProviderClockObservation | undefined, cleanupDiagnostic?: EngineBrokerCleanupObservation["cleanupDiagnostic"]);
|
|
28
|
+
}
|
|
29
|
+
/** Everything one broker turn touches, injected so the accounting and limit paths run under test without a native launcher. */
|
|
30
|
+
export type GrokEngineBrokerTurnDependencies = Readonly<{
|
|
31
|
+
turns: EngineBrokerTurnRegistry;
|
|
32
|
+
proxy: Readonly<{
|
|
33
|
+
capabilities: Readonly<{
|
|
34
|
+
issue(agentId: string, turnId: string, ttlMs?: number): string;
|
|
35
|
+
revoke(turnId: string): void;
|
|
36
|
+
}>;
|
|
37
|
+
registerIsolationGuard(turnId: string, guard: () => Promise<void>): void;
|
|
38
|
+
revokeIsolationGuard(turnId: string): void;
|
|
39
|
+
registerTurn(turnId: string, turn: GrokBrokerProxyTurn): void;
|
|
40
|
+
revokeTurn(turnId: string): void;
|
|
41
|
+
}>;
|
|
42
|
+
mcp: Readonly<{
|
|
43
|
+
register(agentId: string, turnId: string, endpoint: string, ttlMs?: number): string;
|
|
44
|
+
revoke(turnId: string): void;
|
|
45
|
+
observe?(turnId: string): EngineBrokerMcpCallObservation | undefined;
|
|
46
|
+
activity?(turnId: string): EngineBrokerMcpActivity | undefined;
|
|
47
|
+
}>;
|
|
48
|
+
credentialStale(): boolean;
|
|
49
|
+
prepareIsolation(registration: EngineBrokerServiceRegistration): Promise<Readonly<{
|
|
50
|
+
guard: () => Promise<void>;
|
|
51
|
+
configVariant: GrokWorkerConfigVariant;
|
|
52
|
+
}>>;
|
|
53
|
+
writeTaskClock?: typeof writeBrokerTaskClock;
|
|
54
|
+
/** Startup attestation also owns cleanup when per-turn attestation fails. */
|
|
55
|
+
clockedSlot?: boolean;
|
|
56
|
+
/** A failed cleanup fences production dispatch until the broker restarts. */
|
|
57
|
+
taskClockFailed?(): void;
|
|
58
|
+
runNative(input: NativeBrokerTurn, signal: AbortSignal): Promise<Readonly<NativeBrokerTurnResult>>;
|
|
59
|
+
/** The worker the launcher started for this identity (`/proc` by default); only read when the broker ends a turn itself. */
|
|
60
|
+
observeWorker?(workerUid: number): Promise<GrokWorkerProcess | undefined>;
|
|
61
|
+
/** Overrides the turn-end grace and idle bounds (tests). */
|
|
62
|
+
turnEnd?: GrokBrokerTurnEndTiming;
|
|
63
|
+
/** Whether no live process holds the worker identity (`/proc` by default); polled, bounded, before an aborted turn is sealed. */
|
|
64
|
+
workerIdentityEmpty?(workerUid: number): Promise<boolean>;
|
|
65
|
+
workerReapWaitMs?: number;
|
|
66
|
+
}>;
|
|
67
|
+
/**
|
|
68
|
+
* One brokered Grok turn under its declared limits.
|
|
69
|
+
*
|
|
70
|
+
* The limits are the registration's, lowered (never raised) by the wake. The
|
|
71
|
+
* proxy meter refuses model requests past `maxRequests`, past the elapsed
|
|
72
|
+
* deadline, or once the upstream-reported running total reached `maxTokens`,
|
|
73
|
+
* and a tripped limit aborts the worker through the same cancel/kill path a
|
|
74
|
+
* client cancellation uses. A wall-clock timer trips `timeout` for a worker
|
|
75
|
+
* that is mid-request.
|
|
76
|
+
*
|
|
77
|
+
* Every terminal path — completed, failed, limit, cancelled — is sealed and
|
|
78
|
+
* metered through {@link finishBrokerTurnWithUsage}; a replayed turn returns its
|
|
79
|
+
* sealed accounting before any of this runs and never meters again.
|
|
80
|
+
*/
|
|
81
|
+
export declare function runGrokEngineBrokerTurn(deps: GrokEngineBrokerTurnDependencies, registration: EngineBrokerServiceRegistration, wakeId: string, prompt: string, mcpEndpoint: string, signal?: AbortSignal, overrides?: EngineBrokerTurnLimitOverrides, processClock?: string): Promise<GrokEngineBrokerTurnResult>;
|
|
82
|
+
//# sourceMappingURL=grokEngineBrokerTurn.d.ts.map
|