@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.
- package/README.md +307 -45
- package/dist/contracts/attentionContract.d.ts +117 -0
- package/dist/contracts/attentionContract.d.ts.map +1 -0
- package/dist/contracts/attentionContract.js +14 -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/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 +765 -0
- package/dist/contracts/organizationRuntimeContract.d.ts.map +1 -0
- package/dist/contracts/organizationRuntimeContract.js +121 -0
- package/dist/contracts/runtimeContractManifest.d.ts +1615 -0
- package/dist/contracts/runtimeContractManifest.d.ts.map +1 -0
- package/dist/contracts/runtimeContractManifest.js +181 -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/siblingBuildFreshness.d.ts +21 -0
- package/dist/core/siblingBuildFreshness.d.ts.map +1 -0
- package/dist/core/siblingBuildFreshness.js +25 -0
- package/dist/core/types.d.ts +10 -1
- package/dist/core/types.d.ts.map +1 -1
- package/dist/examples/exampleCausalId.d.ts +3 -0
- package/dist/examples/exampleCausalId.d.ts.map +1 -0
- package/dist/examples/exampleCausalId.js +8 -0
- package/dist/examples/jungian-play-org.js +3 -2
- package/dist/examples/jungian-triad-org.js +3 -2
- package/dist/examples/jungianPlayAgent.d.ts +1 -1
- package/dist/examples/jungianPlayAgent.d.ts.map +1 -1
- package/dist/examples/jungianPlayAgent.js +1 -1
- package/dist/examples/jungianProfiles.d.ts +1 -1
- package/dist/examples/jungianProfiles.d.ts.map +1 -1
- package/dist/examples/jungianTriadProfiles.d.ts +1 -1
- package/dist/examples/jungianTriadProfiles.d.ts.map +1 -1
- package/dist/examples/pi-agent.js +3 -2
- package/dist/examples/pi-memory-org.js +6 -5
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/mcp/toolServer.d.ts +31 -0
- package/dist/mcp/toolServer.d.ts.map +1 -0
- package/dist/mcp/toolServer.js +145 -0
- package/dist/observability/causalEvents.d.ts +118 -0
- package/dist/observability/causalEvents.d.ts.map +1 -0
- package/dist/observability/causalEvents.js +223 -0
- package/dist/observability/controlCausal.d.ts +117 -0
- package/dist/observability/controlCausal.d.ts.map +1 -0
- package/dist/observability/controlCausal.js +107 -0
- package/dist/observability/emitCausalFixture.d.ts +25 -0
- package/dist/observability/emitCausalFixture.d.ts.map +1 -0
- package/dist/observability/emitCausalFixture.js +98 -0
- package/dist/observability/index.d.ts +2 -0
- package/dist/observability/index.d.ts.map +1 -1
- package/dist/observability/index.js +2 -0
- 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 +164 -0
- package/dist/pi/cliEnvironment.d.ts +12 -0
- package/dist/pi/cliEnvironment.d.ts.map +1 -0
- package/dist/pi/cliEnvironment.js +32 -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 +106 -0
- package/dist/pi/cliSession.d.ts.map +1 -0
- package/dist/pi/cliSession.js +379 -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/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 +57 -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/index.d.ts +5 -0
- package/dist/pi/index.d.ts.map +1 -1
- package/dist/pi/index.js +5 -0
- package/dist/pi/memoryTools.d.ts +55 -1
- package/dist/pi/memoryTools.d.ts.map +1 -1
- package/dist/pi/memoryTools.js +144 -24
- package/dist/pi/modelRegistry.d.ts +13 -0
- package/dist/pi/modelRegistry.d.ts.map +1 -0
- package/dist/pi/modelRegistry.js +19 -0
- package/dist/pi/piAgentHandle.d.ts +46 -0
- package/dist/pi/piAgentHandle.d.ts.map +1 -0
- package/dist/pi/piAgentHandle.js +351 -0
- package/dist/pi/piAgentWakeSupport.d.ts +103 -0
- package/dist/pi/piAgentWakeSupport.d.ts.map +1 -0
- package/dist/pi/piAgentWakeSupport.js +151 -0
- package/dist/pi/piHarness.d.ts +40 -5
- package/dist/pi/piHarness.d.ts.map +1 -1
- package/dist/pi/piHarness.js +151 -256
- package/dist/pi/prompts.d.ts +10 -0
- package/dist/pi/prompts.d.ts.map +1 -0
- package/dist/pi/prompts.js +63 -0
- package/dist/pi/rawTrainingCapture.d.ts +59 -0
- package/dist/pi/rawTrainingCapture.d.ts.map +1 -0
- package/dist/pi/rawTrainingCapture.js +175 -0
- package/dist/pi/turnCausal.d.ts +73 -0
- package/dist/pi/turnCausal.d.ts.map +1 -0
- package/dist/pi/turnCausal.js +75 -0
- package/dist/pi/turnTrace.d.ts +128 -0
- package/dist/pi/turnTrace.d.ts.map +1 -0
- package/dist/pi/turnTrace.js +138 -0
- package/dist/pi/wakeAcceptance.d.ts +49 -0
- package/dist/pi/wakeAcceptance.d.ts.map +1 -0
- package/dist/pi/wakeAcceptance.js +281 -0
- package/dist/pi/wakeAcceptanceFs.d.ts +44 -0
- package/dist/pi/wakeAcceptanceFs.d.ts.map +1 -0
- package/dist/pi/wakeAcceptanceFs.js +318 -0
- package/dist/pi/wakeAcceptanceSchema.d.ts +75 -0
- package/dist/pi/wakeAcceptanceSchema.d.ts.map +1 -0
- package/dist/pi/wakeAcceptanceSchema.js +265 -0
- package/dist/pi/wakeModes.d.ts +9 -0
- package/dist/pi/wakeModes.d.ts.map +1 -0
- package/dist/pi/wakeModes.js +19 -0
- package/dist/pi/worldNudge.d.ts +22 -0
- package/dist/pi/worldNudge.d.ts.map +1 -0
- package/dist/pi/worldNudge.js +62 -0
- package/dist/pi/worldToolProtocol.d.ts +13 -0
- package/dist/pi/worldToolProtocol.d.ts.map +1 -0
- package/dist/pi/worldToolProtocol.js +49 -0
- package/dist/pi/worldTools.d.ts +32 -0
- package/dist/pi/worldTools.d.ts.map +1 -0
- package/dist/pi/worldTools.js +373 -0
- package/dist/pi/worldTrajectory.d.ts +52 -0
- package/dist/pi/worldTrajectory.d.ts.map +1 -0
- package/dist/pi/worldTrajectory.js +131 -0
- 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 +21 -0
- package/dist/runtime/attention.d.ts.map +1 -0
- package/dist/runtime/attention.js +36 -0
- package/dist/runtime/attentionDispatcher.d.ts +66 -0
- package/dist/runtime/attentionDispatcher.d.ts.map +1 -0
- package/dist/runtime/attentionDispatcher.js +271 -0
- package/dist/runtime/cli.d.ts +7 -0
- package/dist/runtime/cli.d.ts.map +1 -0
- package/dist/runtime/cli.js +251 -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 +30 -0
- package/dist/runtime/engineBrokerCapabilities.d.ts.map +1 -0
- package/dist/runtime/engineBrokerCapabilities.js +59 -0
- package/dist/runtime/engineBrokerControlClient.d.ts +44 -0
- package/dist/runtime/engineBrokerControlClient.d.ts.map +1 -0
- package/dist/runtime/engineBrokerControlClient.js +132 -0
- package/dist/runtime/engineBrokerCredentialJournal.d.ts +10 -0
- package/dist/runtime/engineBrokerCredentialJournal.d.ts.map +1 -0
- package/dist/runtime/engineBrokerCredentialJournal.js +17 -0
- package/dist/runtime/engineBrokerInferenceProtocol.d.ts +52 -0
- package/dist/runtime/engineBrokerInferenceProtocol.d.ts.map +1 -0
- package/dist/runtime/engineBrokerInferenceProtocol.js +65 -0
- package/dist/runtime/engineBrokerMcpCallLog.d.ts +160 -0
- package/dist/runtime/engineBrokerMcpCallLog.d.ts.map +1 -0
- package/dist/runtime/engineBrokerMcpCallLog.js +219 -0
- package/dist/runtime/engineBrokerMcpFacade.d.ts +56 -0
- package/dist/runtime/engineBrokerMcpFacade.d.ts.map +1 -0
- package/dist/runtime/engineBrokerMcpFacade.js +343 -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 +135 -0
- package/dist/runtime/engineBrokerProtocol.d.ts.map +1 -0
- package/dist/runtime/engineBrokerProtocol.js +274 -0
- package/dist/runtime/engineBrokerSealLedger.d.ts +70 -0
- package/dist/runtime/engineBrokerSealLedger.d.ts.map +1 -0
- package/dist/runtime/engineBrokerSealLedger.js +123 -0
- package/dist/runtime/engineBrokerService.d.ts +37 -0
- package/dist/runtime/engineBrokerService.d.ts.map +1 -0
- package/dist/runtime/engineBrokerService.js +138 -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 +50 -0
- package/dist/runtime/engineBrokerServiceConfig.d.ts.map +1 -0
- package/dist/runtime/engineBrokerServiceConfig.js +92 -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 +158 -0
- package/dist/runtime/engineDispatcher.d.ts +36 -0
- package/dist/runtime/engineDispatcher.d.ts.map +1 -0
- package/dist/runtime/engineDispatcher.js +216 -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 +22 -0
- package/dist/runtime/grokBrokerCredentialAuthority.d.ts.map +1 -0
- package/dist/runtime/grokBrokerCredentialAuthority.js +124 -0
- package/dist/runtime/grokBrokerCredentialReader.d.ts +5 -0
- package/dist/runtime/grokBrokerCredentialReader.d.ts.map +1 -0
- package/dist/runtime/grokBrokerCredentialReader.js +37 -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 +106 -0
- package/dist/runtime/grokBrokerProjection.d.ts.map +1 -0
- package/dist/runtime/grokBrokerProjection.js +94 -0
- package/dist/runtime/grokBrokerProxy.d.ts +55 -0
- package/dist/runtime/grokBrokerProxy.d.ts.map +1 -0
- package/dist/runtime/grokBrokerProxy.js +262 -0
- package/dist/runtime/grokBrokerProxyRequest.d.ts +30 -0
- package/dist/runtime/grokBrokerProxyRequest.d.ts.map +1 -0
- package/dist/runtime/grokBrokerProxyRequest.js +74 -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/grokBrokerTurnMeter.d.ts +128 -0
- package/dist/runtime/grokBrokerTurnMeter.d.ts.map +1 -0
- package/dist/runtime/grokBrokerTurnMeter.js +251 -0
- package/dist/runtime/grokBrokerWorkerConfig.d.ts +86 -0
- package/dist/runtime/grokBrokerWorkerConfig.d.ts.map +1 -0
- package/dist/runtime/grokBrokerWorkerConfig.js +135 -0
- package/dist/runtime/grokEngineBroker.d.ts +48 -0
- package/dist/runtime/grokEngineBroker.d.ts.map +1 -0
- package/dist/runtime/grokEngineBroker.js +114 -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 +58 -0
- package/dist/runtime/grokEngineBrokerTurn.d.ts.map +1 -0
- package/dist/runtime/grokEngineBrokerTurn.js +165 -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 +82 -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 +170 -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 +30 -0
- package/dist/runtime/grokWorkerHomeAttestation.d.ts.map +1 -0
- package/dist/runtime/grokWorkerHomeAttestation.js +69 -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 +26 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +23 -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 +165 -0
- package/dist/runtime/organizationRuntime.d.ts.map +1 -0
- package/dist/runtime/organizationRuntime.js +3 -0
- package/dist/runtime/organizationRuntimeControl.d.ts +42 -0
- package/dist/runtime/organizationRuntimeControl.d.ts.map +1 -0
- package/dist/runtime/organizationRuntimeControl.js +235 -0
- package/dist/runtime/organizationRuntimeHost.d.ts +19 -0
- package/dist/runtime/organizationRuntimeHost.d.ts.map +1 -0
- package/dist/runtime/organizationRuntimeHost.js +359 -0
- package/dist/runtime/organizationRuntimeParsing.d.ts +6 -0
- package/dist/runtime/organizationRuntimeParsing.d.ts.map +1 -0
- package/dist/runtime/organizationRuntimeParsing.js +370 -0
- package/dist/runtime/organizationRuntimeReadiness.d.ts +12 -0
- package/dist/runtime/organizationRuntimeReadiness.d.ts.map +1 -0
- package/dist/runtime/organizationRuntimeReadiness.js +59 -0
- package/dist/runtime/physicalReadiness.d.ts +47 -0
- package/dist/runtime/physicalReadiness.d.ts.map +1 -0
- package/dist/runtime/physicalReadiness.js +142 -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 +328 -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 +371 -0
- package/dist/runtime/storeCoordination.d.ts +23 -0
- package/dist/runtime/storeCoordination.d.ts.map +1 -0
- package/dist/runtime/storeCoordination.js +94 -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 +160 -0
- package/dist/runtime/turnRequestLedger.d.ts.map +1 -0
- package/dist/runtime/turnRequestLedger.js +249 -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/wakeAcceptanceProcessIdentity.d.ts +17 -0
- package/dist/runtime/wakeAcceptanceProcessIdentity.d.ts.map +1 -0
- package/dist/runtime/wakeAcceptanceProcessIdentity.js +50 -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 +23 -0
- package/dist/runtime/wakeAcceptanceRecord.d.ts.map +1 -0
- package/dist/runtime/wakeAcceptanceRecord.js +56 -0
- package/dist/runtime/wakeAcceptanceRetention.d.ts +12 -0
- package/dist/runtime/wakeAcceptanceRetention.d.ts.map +1 -0
- package/dist/runtime/wakeAcceptanceRetention.js +12 -0
- package/dist/runtime/wakeAcceptanceStore.d.ts +113 -0
- package/dist/runtime/wakeAcceptanceStore.d.ts.map +1 -0
- package/dist/runtime/wakeAcceptanceStore.js +558 -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 +69 -0
- package/dist/runtime/wakeFuse.d.ts.map +1 -0
- package/dist/runtime/wakeFuse.js +367 -0
- package/package.json +25 -17
- package/dist/examples/mixed-engine-org.d.ts +0 -2
- package/dist/examples/mixed-engine-org.d.ts.map +0 -1
- package/dist/examples/mixed-engine-org.js +0 -351
- package/dist/examples/mixedEngineCli.d.ts +0 -15
- package/dist/examples/mixedEngineCli.d.ts.map +0 -1
- package/dist/examples/mixedEngineCli.js +0 -174
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { decodeGrokHeadlessTurn } from "../pi/grokHeadlessResult.js";
|
|
3
|
+
import { decodeGrokStreamUsage } from "../pi/grokStreamUsage.js";
|
|
4
|
+
import { ENGINE_BROKER_VERSION } from "./engineBrokerProtocol.js";
|
|
5
|
+
import { lowerEngineBrokerTurnLimits, mapGrokReportedModel } from "./engineBrokerTurnAccounting.js";
|
|
6
|
+
import { NativeBrokerTurnFailure } from "./engineBrokerNativeClient.js";
|
|
7
|
+
import { engineBrokerRequestLedgerPathFor, engineBrokerSealLedgerPathFor } from "./engineBrokerServiceConfig.js";
|
|
8
|
+
import { ensureBrokerTurnLedgered } from "./grokEngineBrokerLedger.js";
|
|
9
|
+
import { finishBrokerTurnWithUsage } from "./grokEngineBrokerMetering.js";
|
|
10
|
+
import { GrokBrokerTurnMeter } from "./grokBrokerTurnMeter.js";
|
|
11
|
+
import { GrokWorkerAttestationFailure } from "./grokWorkerAttestation.js";
|
|
12
|
+
export class EngineBrokerTurnFailure extends Error {
|
|
13
|
+
code;
|
|
14
|
+
diagnostic;
|
|
15
|
+
accounting;
|
|
16
|
+
mcpCalls;
|
|
17
|
+
constructor(code, diagnostic, accounting, mcpCalls) {
|
|
18
|
+
super("engine broker turn failed");
|
|
19
|
+
this.code = code;
|
|
20
|
+
this.diagnostic = diagnostic;
|
|
21
|
+
this.accounting = accounting;
|
|
22
|
+
this.mcpCalls = mcpCalls;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const limitReasonFor = { tokens: "token_ceiling", requests: "request_ceiling", timeout: "wake_timeout" };
|
|
26
|
+
const usageOf = (usage) => ({ input: usage.input, cacheRead: usage.cacheRead, cacheWrite: usage.cacheWrite, output: usage.output, total: usage.total });
|
|
27
|
+
/**
|
|
28
|
+
* One brokered Grok turn under its declared limits.
|
|
29
|
+
*
|
|
30
|
+
* The limits are the registration's, lowered (never raised) by the wake. The
|
|
31
|
+
* proxy meter refuses model requests past `maxRequests`, past the elapsed
|
|
32
|
+
* deadline, or once the upstream-reported running total reached `maxTokens`,
|
|
33
|
+
* and a tripped limit aborts the worker through the same cancel/kill path a
|
|
34
|
+
* client cancellation uses. A wall-clock timer trips `timeout` for a worker
|
|
35
|
+
* that is mid-request.
|
|
36
|
+
*
|
|
37
|
+
* Every terminal path — completed, failed, limit, cancelled — is sealed and
|
|
38
|
+
* metered through {@link finishBrokerTurnWithUsage}; a replayed turn returns its
|
|
39
|
+
* sealed accounting before any of this runs and never meters again.
|
|
40
|
+
*/
|
|
41
|
+
export async function runGrokEngineBrokerTurn(deps, registration, wakeId, prompt, mcpEndpoint, signal, overrides) {
|
|
42
|
+
const { agentId } = registration, declared = registration.model.model;
|
|
43
|
+
let limits;
|
|
44
|
+
try {
|
|
45
|
+
limits = lowerEngineBrokerTurnLimits(registration.limits, overrides);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
throw new EngineBrokerTurnFailure("invalid_request", undefined, { outcome: "failed", usage: null, model: declared, requests: 0, limitReason: "none" });
|
|
49
|
+
}
|
|
50
|
+
const turnId = createHash("sha256").update(`${agentId}\0${wakeId}`).digest("hex");
|
|
51
|
+
const request = { version: ENGINE_BROKER_VERSION, kind: "start_turn", requestId: randomUUID(), turnId, agentId, wakeId, prompt, mcpEndpoint, ...(overrides === undefined ? {} : { limits: overrides }) };
|
|
52
|
+
const begun = await deps.turns.begin(request, declared);
|
|
53
|
+
const metering = { usageLedgerPath: registration.usageLedgerPath, requestLedgerPath: engineBrokerRequestLedgerPathFor(registration.usageLedgerPath), sealLedgerPath: engineBrokerSealLedgerPathFor(registration.usageLedgerPath), agentId, wakeId };
|
|
54
|
+
if (begun !== "start") {
|
|
55
|
+
await ensureBrokerTurnLedgered(begun.ledger, turnId, metering);
|
|
56
|
+
return replay(begun.replay);
|
|
57
|
+
}
|
|
58
|
+
const controller = new AbortController();
|
|
59
|
+
const meter = new GrokBrokerTurnMeter(limits, () => controller.abort());
|
|
60
|
+
const onAbort = () => controller.abort();
|
|
61
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
62
|
+
if (signal?.aborted)
|
|
63
|
+
controller.abort();
|
|
64
|
+
const timer = setTimeout(() => meter.trip("timeout"), limits.timeoutMs);
|
|
65
|
+
timer.unref?.();
|
|
66
|
+
let nativeDiagnostic, attested = false, output, rejected = false, sealed;
|
|
67
|
+
try {
|
|
68
|
+
const isolationGuard = await deps.prepareIsolation(registration);
|
|
69
|
+
deps.proxy.registerIsolationGuard(turnId, isolationGuard);
|
|
70
|
+
deps.proxy.registerTurn(turnId, { policy: registration.model, meter });
|
|
71
|
+
const providerCapability = deps.proxy.capabilities.issue(agentId, turnId), mcpCapability = deps.mcp.register(agentId, turnId, mcpEndpoint);
|
|
72
|
+
const result = await deps.runNative({ slot: registration.slot, requestId: request.requestId, turnId, agentId, wakeId, prompt, providerCapability, mcpCapability }, controller.signal);
|
|
73
|
+
nativeDiagnostic = result.diagnostic;
|
|
74
|
+
output = result.text;
|
|
75
|
+
if (result.workerUid !== registration.workerUid)
|
|
76
|
+
throw new Error("engine broker worker identity mismatch");
|
|
77
|
+
await isolationGuard();
|
|
78
|
+
attested = true;
|
|
79
|
+
rejected = true;
|
|
80
|
+
const decoded = decodeGrokHeadlessTurn(result.text), stream = decodeGrokStreamUsage(result.text);
|
|
81
|
+
if (stream.reportedModels.some((reported) => mapGrokReportedModel(reported, declared) === undefined))
|
|
82
|
+
throw new Error("engine broker reported an undeclared model");
|
|
83
|
+
rejected = false;
|
|
84
|
+
const snapshot = meter.snapshot();
|
|
85
|
+
if (snapshot.limitReason !== "none")
|
|
86
|
+
throw new Error("engine broker turn limit reached");
|
|
87
|
+
const usage = decoded.usage === undefined ? streamOrMeterUsage(stream, snapshot) : usageOf(decoded.usage);
|
|
88
|
+
const accounting = { outcome: "completed", usage, model: declared, requests: requestCount(stream, snapshot), limitReason: "none" };
|
|
89
|
+
const completed = { version: request.version, kind: "completed", requestId: request.requestId, turnId, text: decoded.text, workerPid: result.workerPid, workerUid: result.workerUid, workerStartTime: result.startTicks.toString(), ...accounting };
|
|
90
|
+
const result_ = { text: completed.text, workerPid: completed.workerPid, workerUid: completed.workerUid, workerStartTime: completed.workerStartTime, ...accounting };
|
|
91
|
+
await finishBrokerTurnWithUsage(deps.turns, request, completed, metering, { notionalUsd: decoded.usage?.notionalUsd ?? 0, complete: decoded.usage?.complete ?? false, estimatedRequests: snapshot.estimatedRequests, requests: requestRows(stream, snapshot), ...(stream.sessionId === undefined ? {} : { session: stream.sessionId }) }, () => { sealed = result_; });
|
|
92
|
+
return result_;
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
// Once the completed record is published it is the durable truth: anything
|
|
96
|
+
// failing after that (metering) must neither re-seal the turn as failed nor
|
|
97
|
+
// append a second row.
|
|
98
|
+
if (sealed !== undefined)
|
|
99
|
+
return sealed;
|
|
100
|
+
const snapshot = meter.snapshot();
|
|
101
|
+
const code = snapshot.limitReason !== "none" ? "limit_exceeded" : deps.credentialStale() ? "auth_stale" : controller.signal.aborted ? "cancelled" : "engine_failed";
|
|
102
|
+
const diagnostic = error instanceof NativeBrokerTurnFailure ? error.diagnostic : nativeDiagnostic && !attested ? { ...nativeDiagnostic, status: "worker_failed", stage: "attestation", failureClass: error instanceof GrokWorkerAttestationFailure ? error.failureClass : "profile_invalid", profileApplied: false } : undefined;
|
|
103
|
+
const stream = output === undefined ? undefined : decodeGrokStreamUsage(output);
|
|
104
|
+
const accounting = { outcome: "failed", usage: streamOrMeterUsage(stream, snapshot), model: declared, requests: requestCount(stream, snapshot), limitReason: snapshot.limitReason };
|
|
105
|
+
// Read before the `finally` revokes this turn's MCP registration, and
|
|
106
|
+
// swallowed like every other instrument here: it must never be the reason
|
|
107
|
+
// a turn reports something other than why it failed.
|
|
108
|
+
let mcpCalls;
|
|
109
|
+
try {
|
|
110
|
+
mcpCalls = deps.mcp.observe?.(turnId);
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
mcpCalls = undefined;
|
|
114
|
+
}
|
|
115
|
+
const failed = { version: request.version, kind: "failed", requestId: request.requestId, turnId, code, ...(diagnostic ? { diagnostic } : {}), ...(mcpCalls === undefined ? {} : { mcpCalls }), ...accounting };
|
|
116
|
+
const reason = snapshot.limitReason !== "none" ? limitReasonFor[snapshot.limitReason] : rejected ? "turn_rejected" : "unknown";
|
|
117
|
+
await finishBrokerTurnWithUsage(deps.turns, request, failed, metering, { notionalUsd: 0, complete: false, reason, estimatedRequests: snapshot.estimatedRequests, requests: requestRows(stream, snapshot), ...(stream?.sessionId === undefined ? {} : { session: stream.sessionId }) });
|
|
118
|
+
throw new EngineBrokerTurnFailure(code, diagnostic, accounting, mcpCalls);
|
|
119
|
+
}
|
|
120
|
+
finally {
|
|
121
|
+
clearTimeout(timer);
|
|
122
|
+
signal?.removeEventListener("abort", onAbort);
|
|
123
|
+
meter.abortInFlight();
|
|
124
|
+
deps.proxy.revokeTurn(turnId);
|
|
125
|
+
deps.proxy.revokeIsolationGuard(turnId);
|
|
126
|
+
deps.proxy.capabilities.revoke(turnId);
|
|
127
|
+
deps.mcp.revoke(turnId);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
function replay(response) {
|
|
131
|
+
const accounting = { outcome: response.outcome, usage: response.usage, model: response.model, requests: response.requests, limitReason: response.limitReason };
|
|
132
|
+
if (response.kind === "completed")
|
|
133
|
+
return { text: response.text, workerPid: response.workerPid, workerUid: response.workerUid, workerStartTime: response.workerStartTime, ...accounting, outcome: "completed" };
|
|
134
|
+
const code = response.code === "turn_conflict" || response.code === "unavailable" ? "engine_failed" : response.code;
|
|
135
|
+
throw new EngineBrokerTurnFailure(code, response.diagnostic, accounting, response.mcpCalls);
|
|
136
|
+
}
|
|
137
|
+
/** The proxy saw every forwarded request; the stream is the fallback when no request crossed this proxy. */
|
|
138
|
+
const requestCount = (stream, snapshot) => snapshot.requests > 0 ? snapshot.requests : stream?.requests.length ?? 0;
|
|
139
|
+
/** Best partial usage: the worker's own per-request frames when any arrived, else what upstream reported to the proxy. */
|
|
140
|
+
function streamOrMeterUsage(stream, snapshot) {
|
|
141
|
+
if (stream !== undefined && stream.requests.length > 0) {
|
|
142
|
+
const sum = (pick) => stream.requests.reduce((total, value) => total + pick(value), 0);
|
|
143
|
+
return { input: sum((value) => value.input), cacheRead: sum((value) => value.cacheRead), cacheWrite: sum((value) => value.cacheWrite), output: sum((value) => value.output), total: sum((value) => value.total) };
|
|
144
|
+
}
|
|
145
|
+
return snapshot.usage;
|
|
146
|
+
}
|
|
147
|
+
/** Per-request rows: stream usage with the proxy's own observation when both describe the same requests, else the proxy's measured requests. */
|
|
148
|
+
function requestRows(stream, snapshot) {
|
|
149
|
+
if (stream !== undefined && stream.requests.length > 0) {
|
|
150
|
+
const timed = snapshot.timings.length === stream.requests.length;
|
|
151
|
+
return stream.requests.map((value, index) => ({ ...value, usageSource: "stream", ...(timed ? observed(snapshot.timings[index]) : {}) }));
|
|
152
|
+
}
|
|
153
|
+
return snapshot.timings.flatMap((timing, index) => timing.usage === undefined ? [] : [{ index, ...usageOf(timing.usage), usageSource: timing.estimated === true ? "estimated" : "upstream", ...observed(timing) }]);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* What only the proxy saw of one request: its clock, and the tool-call names the
|
|
157
|
+
* response carried. Both are attached on the stream path only when the two
|
|
158
|
+
* descriptions are request-for-request aligned, because an unaligned index would
|
|
159
|
+
* credit one request's attempt to another.
|
|
160
|
+
*/
|
|
161
|
+
const observed = (timing) => ({
|
|
162
|
+
startedAt: timing.startedAt,
|
|
163
|
+
...(timing.endedAt === undefined ? {} : { endedAt: timing.endedAt }),
|
|
164
|
+
...(timing.toolCalls === undefined ? {} : { toolCalls: timing.toolCalls })
|
|
165
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type GrokBrokerModelPolicy } from "./grokBrokerModelPolicy.js";
|
|
2
|
+
/** The evaluator CLI's only model id: Paideia passes `--model daimon-inference-grok`, never a catalog id. */
|
|
3
|
+
export declare const GROK_INFERENCE_CLIENT_MODEL_ID: "daimon-inference-grok";
|
|
4
|
+
/** The environment variable the evaluator CLI reads its grant token from. */
|
|
5
|
+
export declare const GROK_INFERENCE_GRANT_ENV: "DAIMON_INFERENCE_GRANT";
|
|
6
|
+
export type GrokInferenceClientConfigInput = Readonly<{
|
|
7
|
+
baseUrl: string;
|
|
8
|
+
model: GrokBrokerModelPolicy["model"];
|
|
9
|
+
reasoningEffort: GrokBrokerModelPolicy["reasoningEffort"];
|
|
10
|
+
envKey: string;
|
|
11
|
+
}>;
|
|
12
|
+
/**
|
|
13
|
+
* `config.toml` bytes for an evaluator Grok CLI (Paideia judge or optimizer,
|
|
14
|
+
* uid 2000) that reaches the broker proxy through an inference grant.
|
|
15
|
+
*
|
|
16
|
+
* Paideia writes it into a private `GROK_HOME` (`0700`, uid 2000) and sets the
|
|
17
|
+
* grant token in `envKey`; the CLI never holds the broker credential. Grok
|
|
18
|
+
* 1.0.34 ignores `[auth_provider.*]` helpers for a custom model, so the token
|
|
19
|
+
* travels through `env_key` exactly as the worker's turn capability does.
|
|
20
|
+
*
|
|
21
|
+
* It mirrors the worker renderer's lean settings: every bundled skill
|
|
22
|
+
* disabled, workflows off, the per-call `session_title` request pointed at a
|
|
23
|
+
* hidden model with a placeholder key the proxy refuses before any credential
|
|
24
|
+
* read or upstream call, and the declared effort as the model's single
|
|
25
|
+
* effort. There is no MCP server. `max_retries = 0`: with the default, a
|
|
26
|
+
* refused request (HTTP 503) is retried with backoff past a 45 s bound
|
|
27
|
+
* (live stub capture), so a gate refusal would hang the judge until its own
|
|
28
|
+
* timeout; with it the CLI fails in ~0.35 s and the caller's routed retry
|
|
29
|
+
* policy decides. HTTP 401 is never retried either way. `baseUrl` is the `baseUrl` of the grant (the
|
|
30
|
+
* loopback provider proxy); the manifest pins the sha256 for the production
|
|
31
|
+
* proxy URL and {@link GROK_INFERENCE_GRANT_ENV}.
|
|
32
|
+
*
|
|
33
|
+
* Paideia must also accept the init frame this produces: `apiKeySource` is
|
|
34
|
+
* `"user"` (not `"oauth"`), with `tools: []` and `mcp_servers: []` (live
|
|
35
|
+
* stub capture with the Paideia judge argv).
|
|
36
|
+
*/
|
|
37
|
+
export declare function renderGrokInferenceClientConfig(input: GrokInferenceClientConfigInput): string;
|
|
38
|
+
export declare const grokInferenceClientConfigSha256: (input: GrokInferenceClientConfigInput) => string;
|
|
39
|
+
/** The production bytes for a declared model/effort: the grant's proxy URL and the canonical env key. */
|
|
40
|
+
export declare const renderProductionGrokInferenceClientConfig: (policy: GrokBrokerModelPolicy) => string;
|
|
41
|
+
//# sourceMappingURL=grokInferenceClientConfig.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grokInferenceClientConfig.d.ts","sourceRoot":"","sources":["../../src/runtime/grokInferenceClientConfig.ts"],"names":[],"mappings":"AAIA,OAAO,EAA8B,KAAK,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAIpG,6GAA6G;AAC7G,eAAO,MAAM,8BAA8B,yBAAe,CAAC;AAC3D,6EAA6E;AAC7E,eAAO,MAAM,wBAAwB,0BAAc,CAAC;AAEpD,MAAM,MAAM,8BAA8B,GAAG,QAAQ,CAAC;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,qBAAqB,CAAC,OAAO,CAAC,CAAC;IAAC,eAAe,EAAE,qBAAqB,CAAC,iBAAiB,CAAC,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC;AAE7L;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,+BAA+B,CAAC,KAAK,EAAE,8BAA8B,GAAG,MAAM,CAe7F;AAED,eAAO,MAAM,+BAA+B,GAAI,OAAO,8BAA8B,KAAG,MACL,CAAC;AAEpF,yGAAyG;AACzG,eAAO,MAAM,yCAAyC,GAAI,QAAQ,qBAAqB,KAAG,MACmF,CAAC"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { GROK_ENGINE_BROKER } from "../contracts/runtimeContractManifest.js";
|
|
3
|
+
import { GROK_INFERENCE_PROXY_BASE_URL } from "./engineBrokerInferenceProtocol.js";
|
|
4
|
+
import { parseGrokBrokerModelPolicy } from "./grokBrokerModelPolicy.js";
|
|
5
|
+
import { GROK_SESSION_TITLE_SINK_KEY, GROK_SESSION_TITLE_SINK_MODEL_ID, renderGrokLeanBaseConfig } from "./grokBrokerWorkerConfig.js";
|
|
6
|
+
const SPEC = GROK_ENGINE_BROKER.inferenceGrants.client;
|
|
7
|
+
/** The evaluator CLI's only model id: Paideia passes `--model daimon-inference-grok`, never a catalog id. */
|
|
8
|
+
export const GROK_INFERENCE_CLIENT_MODEL_ID = SPEC.modelId;
|
|
9
|
+
/** The environment variable the evaluator CLI reads its grant token from. */
|
|
10
|
+
export const GROK_INFERENCE_GRANT_ENV = SPEC.envKey;
|
|
11
|
+
/**
|
|
12
|
+
* `config.toml` bytes for an evaluator Grok CLI (Paideia judge or optimizer,
|
|
13
|
+
* uid 2000) that reaches the broker proxy through an inference grant.
|
|
14
|
+
*
|
|
15
|
+
* Paideia writes it into a private `GROK_HOME` (`0700`, uid 2000) and sets the
|
|
16
|
+
* grant token in `envKey`; the CLI never holds the broker credential. Grok
|
|
17
|
+
* 1.0.34 ignores `[auth_provider.*]` helpers for a custom model, so the token
|
|
18
|
+
* travels through `env_key` exactly as the worker's turn capability does.
|
|
19
|
+
*
|
|
20
|
+
* It mirrors the worker renderer's lean settings: every bundled skill
|
|
21
|
+
* disabled, workflows off, the per-call `session_title` request pointed at a
|
|
22
|
+
* hidden model with a placeholder key the proxy refuses before any credential
|
|
23
|
+
* read or upstream call, and the declared effort as the model's single
|
|
24
|
+
* effort. There is no MCP server. `max_retries = 0`: with the default, a
|
|
25
|
+
* refused request (HTTP 503) is retried with backoff past a 45 s bound
|
|
26
|
+
* (live stub capture), so a gate refusal would hang the judge until its own
|
|
27
|
+
* timeout; with it the CLI fails in ~0.35 s and the caller's routed retry
|
|
28
|
+
* policy decides. HTTP 401 is never retried either way. `baseUrl` is the `baseUrl` of the grant (the
|
|
29
|
+
* loopback provider proxy); the manifest pins the sha256 for the production
|
|
30
|
+
* proxy URL and {@link GROK_INFERENCE_GRANT_ENV}.
|
|
31
|
+
*
|
|
32
|
+
* Paideia must also accept the init frame this produces: `apiKeySource` is
|
|
33
|
+
* `"user"` (not `"oauth"`), with `tools: []` and `mcp_servers: []` (live
|
|
34
|
+
* stub capture with the Paideia judge argv).
|
|
35
|
+
*/
|
|
36
|
+
export function renderGrokInferenceClientConfig(input) {
|
|
37
|
+
let declared;
|
|
38
|
+
try {
|
|
39
|
+
declared = parseGrokBrokerModelPolicy({ model: input.model, reasoningEffort: input.reasoningEffort });
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
throw new TypeError("invalid Grok inference client configuration");
|
|
43
|
+
}
|
|
44
|
+
if (declared.model !== input.model || declared.reasoningEffort !== input.reasoningEffort)
|
|
45
|
+
throw new TypeError("invalid Grok inference client configuration");
|
|
46
|
+
if (typeof input.baseUrl !== "string" || !/^http:\/\/127\.0\.0\.1:([1-9][0-9]{0,4})\/v1$/u.test(input.baseUrl) || Number(input.baseUrl.slice(17, -3)) > 65_535)
|
|
47
|
+
throw new TypeError("invalid Grok inference client configuration");
|
|
48
|
+
if (typeof input.envKey !== "string" || !/^[A-Z][A-Z0-9_]{2,63}$/u.test(input.envKey))
|
|
49
|
+
throw new TypeError("invalid Grok inference client configuration");
|
|
50
|
+
const label = `${declared.reasoningEffort[0].toUpperCase()}${declared.reasoningEffort.slice(1)}`;
|
|
51
|
+
return [
|
|
52
|
+
renderGrokLeanBaseConfig(),
|
|
53
|
+
"[models]", `default = "${GROK_INFERENCE_CLIENT_MODEL_ID}"`, `default_reasoning_effort = "${declared.reasoningEffort}"`, `session_summary = "${GROK_SESSION_TITLE_SINK_MODEL_ID}"`, "",
|
|
54
|
+
`[model.${GROK_SESSION_TITLE_SINK_MODEL_ID}]`, 'model = "disabled"', `base_url = "${input.baseUrl}"`, `api_key = "${GROK_SESSION_TITLE_SINK_KEY}"`, "max_retries = 0", "hidden = true", "",
|
|
55
|
+
`[model.${GROK_INFERENCE_CLIENT_MODEL_ID}]`, `model = "${declared.model}"`, `base_url = "${input.baseUrl}"`, `env_key = "${input.envKey}"`,
|
|
56
|
+
'api_backend = "chat_completions"', "context_window = 131072", "supports_backend_search = false", "max_retries = 0", "",
|
|
57
|
+
`[[model.${GROK_INFERENCE_CLIENT_MODEL_ID}.reasoning_efforts]]`, `value = "${declared.reasoningEffort}"`, `label = "${label}"`, "default = true", ""
|
|
58
|
+
].join("\n");
|
|
59
|
+
}
|
|
60
|
+
export const grokInferenceClientConfigSha256 = (input) => createHash("sha256").update(renderGrokInferenceClientConfig(input)).digest("hex");
|
|
61
|
+
/** The production bytes for a declared model/effort: the grant's proxy URL and the canonical env key. */
|
|
62
|
+
export const renderProductionGrokInferenceClientConfig = (policy) => renderGrokInferenceClientConfig({ baseUrl: GROK_INFERENCE_PROXY_BASE_URL, model: policy.model, reasoningEffort: policy.reasoningEffort, envKey: GROK_INFERENCE_GRANT_ENV });
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { EngineBrokerInferenceFailureCode } from "./engineBrokerInferenceProtocol.js";
|
|
2
|
+
import type { EngineBrokerTurnLimits, EngineBrokerTurnUsage } from "./engineBrokerTurnAccounting.js";
|
|
3
|
+
import { type GrokBrokerModelPolicy } from "./grokBrokerModelPolicy.js";
|
|
4
|
+
import { GrokBrokerTurnMeter } from "./grokBrokerTurnMeter.js";
|
|
5
|
+
import { type GrokInferencePurpose, type InferenceUsageEntry } from "./inferenceUsageLedger.js";
|
|
6
|
+
/** One live evaluator grant as the proxy sees it. */
|
|
7
|
+
export type GrokInferenceGrant = Readonly<{
|
|
8
|
+
grantId: string;
|
|
9
|
+
purpose: GrokInferencePurpose;
|
|
10
|
+
policy: GrokBrokerModelPolicy;
|
|
11
|
+
expiresAt: number;
|
|
12
|
+
meter: GrokBrokerTurnMeter;
|
|
13
|
+
}>;
|
|
14
|
+
export type GrokInferenceGrantIssued = Readonly<{
|
|
15
|
+
grantId: string;
|
|
16
|
+
token: string;
|
|
17
|
+
purpose: GrokInferencePurpose;
|
|
18
|
+
policy: GrokBrokerModelPolicy;
|
|
19
|
+
expiresAt: number;
|
|
20
|
+
limits: EngineBrokerTurnLimits;
|
|
21
|
+
}>;
|
|
22
|
+
export type GrokInferenceGrantRequest = Readonly<{
|
|
23
|
+
model: unknown;
|
|
24
|
+
reasoningEffort: unknown;
|
|
25
|
+
purpose: unknown;
|
|
26
|
+
}>;
|
|
27
|
+
export declare class GrokInferenceGrantRefused extends Error {
|
|
28
|
+
readonly code: EngineBrokerInferenceFailureCode;
|
|
29
|
+
constructor(code: EngineBrokerInferenceFailureCode);
|
|
30
|
+
}
|
|
31
|
+
export type GrokInferenceGrantsOptions = Readonly<{
|
|
32
|
+
now?: () => number;
|
|
33
|
+
/** Test seam: a fixed grant id proves grants never share a key space with turn capabilities. */
|
|
34
|
+
grantId?: () => string;
|
|
35
|
+
onSettled?: (entry: InferenceUsageEntry) => void;
|
|
36
|
+
ttlMs?: number;
|
|
37
|
+
maxLiveGrants?: number;
|
|
38
|
+
}>;
|
|
39
|
+
/**
|
|
40
|
+
* Evaluator inference grants: a distinct kind beside subject turn capabilities.
|
|
41
|
+
*
|
|
42
|
+
* Grants live in their own map keyed by a random grant id; turn capabilities
|
|
43
|
+
* (`engineBrokerCapabilities.ts`) are keyed by turn id and never consulted
|
|
44
|
+
* here, and grant tokens carry `inference_` so the proxy routes a bearer to
|
|
45
|
+
* exactly one of the two lookups. A grant has no worker isolation guard (it is
|
|
46
|
+
* issued only to the organization uid, the trusted evaluator side), but it
|
|
47
|
+
* carries the same spend gate as a subject turn: a {@link GrokBrokerTurnMeter}
|
|
48
|
+
* with one request in flight, the request ceiling, the between-requests token
|
|
49
|
+
* ceiling and the estimate for a response without usage. Its lifetime is the
|
|
50
|
+
* meter's time limit: past `expiresAt` it is gone from the map, and a request
|
|
51
|
+
* still in flight at expiry is aborted.
|
|
52
|
+
*
|
|
53
|
+
* At most `maxLiveGrants` grants exist at once; a caller frees a slot early
|
|
54
|
+
* with {@link release}. Nothing here is durable — a broker restart drops every
|
|
55
|
+
* grant, and callers request a new one.
|
|
56
|
+
*/
|
|
57
|
+
export declare class GrokInferenceGrants {
|
|
58
|
+
private readonly options;
|
|
59
|
+
private readonly grants;
|
|
60
|
+
private readonly now;
|
|
61
|
+
private readonly ttlMs;
|
|
62
|
+
private readonly maxLive;
|
|
63
|
+
constructor(options?: GrokInferenceGrantsOptions);
|
|
64
|
+
issue(request: GrokInferenceGrantRequest): GrokInferenceGrantIssued;
|
|
65
|
+
/** The live, unexpired grant a bearer names, or `undefined`. Never counts a request. */
|
|
66
|
+
authorize(token: string): GrokInferenceGrant | undefined;
|
|
67
|
+
/** Records one admitted request's end on the grant's meter and emits its ledger row. */
|
|
68
|
+
settle(grant: GrokInferenceGrant, index: number, usage: EngineBrokerTurnUsage | undefined, requestBytes: number): void;
|
|
69
|
+
/** Revokes a grant and aborts its in-flight request. Returns whether it was live. */
|
|
70
|
+
release(grantId: string): boolean;
|
|
71
|
+
live(): number;
|
|
72
|
+
close(): void;
|
|
73
|
+
private prune;
|
|
74
|
+
}
|
|
75
|
+
/** The broker's grants: every settled request is appended to the evaluator inference ledger and nowhere else. */
|
|
76
|
+
export declare const createLedgeredGrokInferenceGrants: (inferenceLedgerPath: string) => GrokInferenceGrants;
|
|
77
|
+
//# sourceMappingURL=grokInferenceGrants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grokInferenceGrants.d.ts","sourceRoot":"","sources":["../../src/runtime/grokInferenceGrants.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,gCAAgC,EAAE,MAAM,oCAAoC,CAAC;AAC3F,OAAO,KAAK,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,MAAM,iCAAiC,CAAC;AACrG,OAAO,EAA8B,KAAK,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AACpG,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EAAiD,KAAK,oBAAoB,EAAE,KAAK,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAI/I,qDAAqD;AACrD,MAAM,MAAM,kBAAkB,GAAG,QAAQ,CAAC;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,oBAAoB,CAAC;IAAC,MAAM,EAAE,qBAAqB,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,mBAAmB,CAAA;CAAE,CAAC,CAAC;AAC5K,MAAM,MAAM,wBAAwB,GAAG,QAAQ,CAAC;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,oBAAoB,CAAC;IAAC,MAAM,EAAE,qBAAqB,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,sBAAsB,CAAA;CAAE,CAAC,CAAC;AACrM,MAAM,MAAM,yBAAyB,GAAG,QAAQ,CAAC;IAAE,KAAK,EAAE,OAAO,CAAC;IAAC,eAAe,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC,CAAC;AAEjH,qBAAa,yBAA0B,SAAQ,KAAK;IACtC,QAAQ,CAAC,IAAI,EAAE,gCAAgC;gBAAtC,IAAI,EAAE,gCAAgC;CAC5D;AAGD,MAAM,MAAM,0BAA0B,GAAG,QAAQ,CAAC;IAChD,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;IACnB,gGAAgG;IAChG,OAAO,CAAC,EAAE,MAAM,MAAM,CAAC;IACvB,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,mBAAmB,KAAK,IAAI,CAAC;IACjD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC,CAAC;AAEH;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,mBAAmB;IAKlB,OAAO,CAAC,QAAQ,CAAC,OAAO;IAJpC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA4B;IACnD,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAe;IACnC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;IAC/B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;gBACJ,OAAO,GAAE,0BAA+B;IAOrE,KAAK,CAAC,OAAO,EAAE,yBAAyB,GAAG,wBAAwB;IAiBnE,wFAAwF;IACxF,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,kBAAkB,GAAG,SAAS;IAQxD,wFAAwF;IACxF,MAAM,CAAC,KAAK,EAAE,kBAAkB,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,qBAAqB,GAAG,SAAS,EAAE,YAAY,EAAE,MAAM,GAAG,IAAI;IAStH,qFAAqF;IACrF,OAAO,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAOjC,IAAI,IAAI,MAAM;IAEd,KAAK,IAAI,IAAI;IAEb,OAAO,CAAC,KAAK;CAId;AAED,iHAAiH;AACjH,eAAO,MAAM,iCAAiC,GAAI,qBAAqB,MAAM,KAAG,mBAC+B,CAAC"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { createHash, randomBytes, timingSafeEqual } from "node:crypto";
|
|
2
|
+
import { GROK_ENGINE_BROKER } from "../contracts/runtimeContractManifest.js";
|
|
3
|
+
import { parseGrokBrokerModelPolicy } from "./grokBrokerModelPolicy.js";
|
|
4
|
+
import { GrokBrokerTurnMeter } from "./grokBrokerTurnMeter.js";
|
|
5
|
+
import { GROK_INFERENCE_PURPOSES, recordInferenceUsage } from "./inferenceUsageLedger.js";
|
|
6
|
+
const SPEC = GROK_ENGINE_BROKER.inferenceGrants;
|
|
7
|
+
export class GrokInferenceGrantRefused extends Error {
|
|
8
|
+
code;
|
|
9
|
+
constructor(code) {
|
|
10
|
+
super(`inference grant refused (${code})`);
|
|
11
|
+
this.code = code;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Evaluator inference grants: a distinct kind beside subject turn capabilities.
|
|
16
|
+
*
|
|
17
|
+
* Grants live in their own map keyed by a random grant id; turn capabilities
|
|
18
|
+
* (`engineBrokerCapabilities.ts`) are keyed by turn id and never consulted
|
|
19
|
+
* here, and grant tokens carry `inference_` so the proxy routes a bearer to
|
|
20
|
+
* exactly one of the two lookups. A grant has no worker isolation guard (it is
|
|
21
|
+
* issued only to the organization uid, the trusted evaluator side), but it
|
|
22
|
+
* carries the same spend gate as a subject turn: a {@link GrokBrokerTurnMeter}
|
|
23
|
+
* with one request in flight, the request ceiling, the between-requests token
|
|
24
|
+
* ceiling and the estimate for a response without usage. Its lifetime is the
|
|
25
|
+
* meter's time limit: past `expiresAt` it is gone from the map, and a request
|
|
26
|
+
* still in flight at expiry is aborted.
|
|
27
|
+
*
|
|
28
|
+
* At most `maxLiveGrants` grants exist at once; a caller frees a slot early
|
|
29
|
+
* with {@link release}. Nothing here is durable — a broker restart drops every
|
|
30
|
+
* grant, and callers request a new one.
|
|
31
|
+
*/
|
|
32
|
+
export class GrokInferenceGrants {
|
|
33
|
+
options;
|
|
34
|
+
grants = new Map();
|
|
35
|
+
now;
|
|
36
|
+
ttlMs;
|
|
37
|
+
maxLive;
|
|
38
|
+
constructor(options = {}) {
|
|
39
|
+
this.options = options;
|
|
40
|
+
this.now = options.now ?? Date.now;
|
|
41
|
+
this.ttlMs = options.ttlMs ?? SPEC.ttlMs;
|
|
42
|
+
this.maxLive = options.maxLiveGrants ?? SPEC.maxLiveGrants;
|
|
43
|
+
if (!Number.isSafeInteger(this.ttlMs) || this.ttlMs < 1 || this.ttlMs > SPEC.ttlMs || !Number.isSafeInteger(this.maxLive) || this.maxLive < 1 || this.maxLive > SPEC.maxLiveGrants)
|
|
44
|
+
throw new TypeError("invalid inference grant policy");
|
|
45
|
+
}
|
|
46
|
+
issue(request) {
|
|
47
|
+
let policy;
|
|
48
|
+
try {
|
|
49
|
+
policy = parseGrokBrokerModelPolicy({ model: request.model, reasoningEffort: request.reasoningEffort });
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
throw new GrokInferenceGrantRefused("invalid_request");
|
|
53
|
+
}
|
|
54
|
+
// Nothing is defaulted: the model parser fills an absent member, a grant must name both.
|
|
55
|
+
if (request.model !== policy.model || request.reasoningEffort !== policy.reasoningEffort || !GROK_INFERENCE_PURPOSES.includes(request.purpose))
|
|
56
|
+
throw new GrokInferenceGrantRefused("invalid_request");
|
|
57
|
+
this.prune();
|
|
58
|
+
if (this.grants.size >= this.maxLive)
|
|
59
|
+
throw new GrokInferenceGrantRefused("grant_limit");
|
|
60
|
+
const grantId = this.options.grantId?.() ?? randomBytes(16).toString("hex");
|
|
61
|
+
if (!/^[a-f0-9]{32}$/u.test(grantId) || this.grants.has(grantId))
|
|
62
|
+
throw new GrokInferenceGrantRefused("grant_limit");
|
|
63
|
+
const limits = Object.freeze({ maxRequests: SPEC.limits.maxRequests, maxTokens: SPEC.limits.maxTokens, timeoutMs: this.ttlMs });
|
|
64
|
+
const token = `${SPEC.tokenPrefix}${randomBytes(32).toString("base64url")}`;
|
|
65
|
+
const grant = Object.freeze({ grantId, purpose: request.purpose, policy, expiresAt: this.now() + this.ttlMs, meter: new GrokBrokerTurnMeter(limits, () => undefined, this.now) });
|
|
66
|
+
const timer = setTimeout(() => this.release(grantId), this.ttlMs);
|
|
67
|
+
timer.unref?.();
|
|
68
|
+
this.grants.set(grantId, { grant, digest: digest(token), timer });
|
|
69
|
+
return Object.freeze({ grantId, token, purpose: grant.purpose, policy, expiresAt: grant.expiresAt, limits });
|
|
70
|
+
}
|
|
71
|
+
/** The live, unexpired grant a bearer names, or `undefined`. Never counts a request. */
|
|
72
|
+
authorize(token) {
|
|
73
|
+
if (!token.startsWith(SPEC.tokenPrefix))
|
|
74
|
+
return undefined;
|
|
75
|
+
this.prune();
|
|
76
|
+
const candidate = digest(token);
|
|
77
|
+
for (const entry of this.grants.values())
|
|
78
|
+
if (timingSafeEqual(entry.digest, candidate))
|
|
79
|
+
return entry.grant;
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
/** Records one admitted request's end on the grant's meter and emits its ledger row. */
|
|
83
|
+
settle(grant, index, usage, requestBytes) {
|
|
84
|
+
const before = grant.meter.snapshot().timings[index];
|
|
85
|
+
if (before === undefined || before.endedAt !== undefined)
|
|
86
|
+
return;
|
|
87
|
+
grant.meter.settle(index, usage, requestBytes);
|
|
88
|
+
const timing = grant.meter.snapshot().timings[index];
|
|
89
|
+
if (timing?.usage === undefined || timing.endedAt === undefined)
|
|
90
|
+
return;
|
|
91
|
+
this.options.onSettled?.({ grant: grant.grantId, purpose: grant.purpose, model: grant.policy.model, request: index, usage: timing.usage, usageSource: timing.estimated === true ? "estimated" : "upstream", startedAt: timing.startedAt, endedAt: timing.endedAt });
|
|
92
|
+
}
|
|
93
|
+
/** Revokes a grant and aborts its in-flight request. Returns whether it was live. */
|
|
94
|
+
release(grantId) {
|
|
95
|
+
const entry = this.grants.get(grantId);
|
|
96
|
+
if (entry === undefined)
|
|
97
|
+
return false;
|
|
98
|
+
clearTimeout(entry.timer);
|
|
99
|
+
entry.grant.meter.trip("timeout");
|
|
100
|
+
entry.digest.fill(0);
|
|
101
|
+
this.grants.delete(grantId);
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
live() { this.prune(); return this.grants.size; }
|
|
105
|
+
close() { for (const grantId of [...this.grants.keys()])
|
|
106
|
+
this.release(grantId); }
|
|
107
|
+
prune() {
|
|
108
|
+
const now = this.now();
|
|
109
|
+
for (const [grantId, entry] of this.grants)
|
|
110
|
+
if (entry.grant.expiresAt <= now)
|
|
111
|
+
this.release(grantId);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/** The broker's grants: every settled request is appended to the evaluator inference ledger and nowhere else. */
|
|
115
|
+
export const createLedgeredGrokInferenceGrants = (inferenceLedgerPath) => new GrokInferenceGrants({ onSettled: (entry) => { void recordInferenceUsage(inferenceLedgerPath, entry); } });
|
|
116
|
+
const digest = (value) => createHash("sha256").update(value).digest();
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ServerResponse } from "node:http";
|
|
2
|
+
import type { GrokBrokerCredentialAuthority, GrokBrokerUpstream } from "./grokBrokerProxy.js";
|
|
3
|
+
import type { GrokInferenceGrants } from "./grokInferenceGrants.js";
|
|
4
|
+
export type GrokInferenceProxyInput = Readonly<{
|
|
5
|
+
method: string;
|
|
6
|
+
pathname: string;
|
|
7
|
+
headers: Readonly<Record<string, string | undefined>>;
|
|
8
|
+
body: Buffer;
|
|
9
|
+
token: string;
|
|
10
|
+
}>;
|
|
11
|
+
/** HTTP 401 with a fixed code: the broker realm is stale, distinct from a generic 503 broker failure. */
|
|
12
|
+
export declare const GROK_INFERENCE_AUTH_STALE_BODY = "{\"error\":\"auth_stale\"}";
|
|
13
|
+
/**
|
|
14
|
+
* Serves one evaluator grant request; never throws.
|
|
15
|
+
*
|
|
16
|
+
* Order mirrors the subject path: the body is proven a grant-shaped request
|
|
17
|
+
* (declared model/effort, no tools) before the credential is read, the grant
|
|
18
|
+
* meter admits it before any upstream call, and exactly one ledger row is
|
|
19
|
+
* written once an admitted request settles. No worker isolation guard applies:
|
|
20
|
+
* grants are issued only to the organization uid.
|
|
21
|
+
*
|
|
22
|
+
* Stale realm: the grant shares the subject's credential authority, so a
|
|
23
|
+
* stale realm fails judges and subject turns alike (accepted shared fate). A
|
|
24
|
+
* grant request that finds the realm stale — before the upstream call or
|
|
25
|
+
* after a rejected refresh — is answered 401 `{"error":"auth_stale"}`, never
|
|
26
|
+
* the generic 503, so the evaluator can report it as a credential failure.
|
|
27
|
+
*/
|
|
28
|
+
export declare function serveGrokInferenceGrant(input: GrokInferenceProxyInput, response: ServerResponse, grants: GrokInferenceGrants, authority: GrokBrokerCredentialAuthority, upstream: GrokBrokerUpstream): Promise<void>;
|
|
29
|
+
//# sourceMappingURL=grokInferenceProxy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grokInferenceProxy.d.ts","sourceRoot":"","sources":["../../src/runtime/grokInferenceProxy.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAGhD,OAAO,KAAK,EAAE,6BAA6B,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE9F,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAGpE,MAAM,MAAM,uBAAuB,GAAG,QAAQ,CAAC;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC;AAEzK,yGAAyG;AACzG,eAAO,MAAM,8BAA8B,+BAA2B,CAAC;AAEvE;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,uBAAuB,CAAC,KAAK,EAAE,uBAAuB,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,EAAE,mBAAmB,EAAE,SAAS,EAAE,6BAA6B,EAAE,QAAQ,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC,CAiC1N"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { GrokBrokerProxyRefusal } from "./grokBrokerProxy.js";
|
|
3
|
+
import { parseGrokUpstreamUsage } from "./grokBrokerTurnMeter.js";
|
|
4
|
+
import { authorizeGrokInferenceProxyRequest } from "./grokInferenceProxyRequest.js";
|
|
5
|
+
/** HTTP 401 with a fixed code: the broker realm is stale, distinct from a generic 503 broker failure. */
|
|
6
|
+
export const GROK_INFERENCE_AUTH_STALE_BODY = '{"error":"auth_stale"}';
|
|
7
|
+
/**
|
|
8
|
+
* Serves one evaluator grant request; never throws.
|
|
9
|
+
*
|
|
10
|
+
* Order mirrors the subject path: the body is proven a grant-shaped request
|
|
11
|
+
* (declared model/effort, no tools) before the credential is read, the grant
|
|
12
|
+
* meter admits it before any upstream call, and exactly one ledger row is
|
|
13
|
+
* written once an admitted request settles. No worker isolation guard applies:
|
|
14
|
+
* grants are issued only to the organization uid.
|
|
15
|
+
*
|
|
16
|
+
* Stale realm: the grant shares the subject's credential authority, so a
|
|
17
|
+
* stale realm fails judges and subject turns alike (accepted shared fate). A
|
|
18
|
+
* grant request that finds the realm stale — before the upstream call or
|
|
19
|
+
* after a rejected refresh — is answered 401 `{"error":"auth_stale"}`, never
|
|
20
|
+
* the generic 503, so the evaluator can report it as a credential failure.
|
|
21
|
+
*/
|
|
22
|
+
export async function serveGrokInferenceGrant(input, response, grants, authority, upstream) {
|
|
23
|
+
let settle;
|
|
24
|
+
try {
|
|
25
|
+
const grant = grants.authorize(input.token);
|
|
26
|
+
if (grant === undefined)
|
|
27
|
+
throw new GrokBrokerProxyRefusal("unknown_or_expired_grant");
|
|
28
|
+
let prepared;
|
|
29
|
+
try {
|
|
30
|
+
prepared = authorizeGrokInferenceProxyRequest(input, "pending", grant.policy);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
throw new GrokBrokerProxyRefusal("request_body_rejected");
|
|
34
|
+
}
|
|
35
|
+
let token = await authority.accessToken(false);
|
|
36
|
+
const rejectedDigest = createHash("sha256").update(token).digest("hex");
|
|
37
|
+
prepared = withBearer(prepared, token);
|
|
38
|
+
token = "";
|
|
39
|
+
const admission = grant.meter.admit();
|
|
40
|
+
if ("refused" in admission)
|
|
41
|
+
return json(response, 429, JSON.stringify({ error: "grant limit reached", limit: admission.refused }));
|
|
42
|
+
if ("busy" in admission)
|
|
43
|
+
return json(response, 429, '{"error":"grant request in flight"}');
|
|
44
|
+
settle = (usage) => { settle = undefined; grants.settle(grant, admission.index, usage, input.body.byteLength); };
|
|
45
|
+
let result = await upstream(prepared, admission.signal);
|
|
46
|
+
if (result.status === 401) {
|
|
47
|
+
token = authority.refreshAfterRejection ? await authority.refreshAfterRejection(rejectedDigest) : await authority.accessToken(true);
|
|
48
|
+
const refreshedDigest = createHash("sha256").update(token).digest("hex");
|
|
49
|
+
prepared = withBearer(prepared, token);
|
|
50
|
+
token = "";
|
|
51
|
+
result = await upstream(prepared, admission.signal);
|
|
52
|
+
if (result.status === 401)
|
|
53
|
+
await authority.markRejected(refreshedDigest);
|
|
54
|
+
}
|
|
55
|
+
settle?.(parseGrokUpstreamUsage(result.body, result.headers["content-type"]));
|
|
56
|
+
json(response, result.status, result.body, result.headers["content-type"]);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
settle?.(undefined);
|
|
60
|
+
// Same rule as the subject path: a policy miss is non-retryable (400), because a
|
|
61
|
+
// retryable 503 makes the client re-send a request the broker will never accept,
|
|
62
|
+
// charging estimated usage for every attempt. 503 stays for transient faults only.
|
|
63
|
+
process.stderr.write(`[grok-proxy] inference refused: ${error instanceof GrokBrokerProxyRefusal ? error.reason : "broker_unavailable"}\n`);
|
|
64
|
+
if (authority.isStale?.() === true)
|
|
65
|
+
json(response, 401, GROK_INFERENCE_AUTH_STALE_BODY);
|
|
66
|
+
else if (error instanceof GrokBrokerProxyRefusal)
|
|
67
|
+
json(response, 400, JSON.stringify({ error: "broker refused this request", reason: error.reason }));
|
|
68
|
+
else
|
|
69
|
+
json(response, 503, '{"error":"broker unavailable"}');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const withBearer = (prepared, token) => {
|
|
73
|
+
if (!token || /[\r\n]/u.test(token))
|
|
74
|
+
throw new Error("broker credential authority unavailable");
|
|
75
|
+
return { ...prepared, headers: { ...prepared.headers, authorization: `Bearer ${token}` } };
|
|
76
|
+
};
|
|
77
|
+
function json(response, status, body, contentType = "application/json") {
|
|
78
|
+
if (response.headersSent)
|
|
79
|
+
return;
|
|
80
|
+
response.writeHead(status, { "content-type": contentType, "cache-control": "no-store" });
|
|
81
|
+
response.end(body);
|
|
82
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { GrokBrokerProxyInput, GrokBrokerUpstreamRequest } from "./grokBrokerProxyRequest.js";
|
|
2
|
+
import { type GrokBrokerModelPolicy } from "./grokBrokerModelPolicy.js";
|
|
3
|
+
/**
|
|
4
|
+
* Authorizes one evaluator grant request body and rebuilds it for the provider.
|
|
5
|
+
*
|
|
6
|
+
* The accepted shape is exactly what Grok CLI 1.0.34 sends for a Paideia
|
|
7
|
+
* judge/optimizer call (`--tools read_file --disallowed-tools
|
|
8
|
+
* read_file,search_tool,use_tool --max-turns 1`, with and without
|
|
9
|
+
* `--system-prompt-override` and `--json-schema`; live stub capture):
|
|
10
|
+
*
|
|
11
|
+
* - `stream: true` with `stream_options: {include_usage: true}` — the CLI never
|
|
12
|
+
* sends a non-streaming request, so none is accepted;
|
|
13
|
+
* - `model` and `reasoning_effort` equal to the grant's declaration;
|
|
14
|
+
* - `messages` of plain `{role, content}` string turns, roles system/user/assistant;
|
|
15
|
+
* - optional `response_format` `{type: "json_schema", json_schema: {name, schema, strict}}`;
|
|
16
|
+
* - no `tools` and no `tool_choice` member at all, not even an empty one. The
|
|
17
|
+
* CLI's per-call `session_title` request carries both and is refused here.
|
|
18
|
+
*
|
|
19
|
+
* The client version must be the pinned CLI, and the forwarded body is the
|
|
20
|
+
* re-serialized parse, never the caller's bytes.
|
|
21
|
+
*/
|
|
22
|
+
export declare function authorizeGrokInferenceProxyRequest(input: Omit<GrokBrokerProxyInput, "agentId" | "turnId">, bearer: string, policy: GrokBrokerModelPolicy): GrokBrokerUpstreamRequest;
|
|
23
|
+
//# sourceMappingURL=grokInferenceProxyRequest.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grokInferenceProxyRequest.d.ts","sourceRoot":"","sources":["../../src/runtime/grokInferenceProxyRequest.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,oBAAoB,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AACnG,OAAO,EAA8B,KAAK,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAUpG;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,kCAAkC,CAAC,KAAK,EAAE,IAAI,CAAC,oBAAoB,EAAE,SAAS,GAAG,QAAQ,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,qBAAqB,GAAG,yBAAyB,CAcpL"}
|