@opengeni/worker-bundle 2.0.1 → 2.0.2-canary.38789554071001
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/dist/activities/agent-run-admission.d.ts +1 -12
- package/dist/activities/agent-turn/admission.d.ts +88 -10
- package/dist/activities/agent-turn/agent-build.d.ts +14 -3
- package/dist/activities/agent-turn/claim.d.ts +4 -1
- package/dist/activities/agent-turn/claude-core.d.ts +195 -0
- package/dist/activities/agent-turn/code-search.d.ts +60 -0
- package/dist/activities/agent-turn/codex-capacity.d.ts +3 -0
- package/dist/activities/agent-turn/codex-core-capacity.d.ts +37 -0
- package/dist/activities/agent-turn/codex-core-claim.d.ts +40 -0
- package/dist/activities/agent-turn/codex-core-errors.d.ts +102 -0
- package/dist/activities/agent-turn/codex-core-failover.d.ts +15 -0
- package/dist/activities/agent-turn/codex-core-requests.d.ts +28 -0
- package/dist/activities/agent-turn/codex-core-settlement.d.ts +77 -0
- package/dist/activities/agent-turn/codex-credit-policy.d.ts +30 -0
- package/dist/activities/agent-turn/codex-plan-entitlement.d.ts +105 -0
- package/dist/activities/agent-turn/codex-usage-policy.d.ts +2 -0
- package/dist/activities/agent-turn/codex.d.ts +2 -19
- package/dist/activities/agent-turn/compaction-interruption.d.ts +32 -0
- package/dist/activities/agent-turn/compaction-prep.d.ts +30 -1
- package/dist/activities/agent-turn/credential-leases.d.ts +64 -37
- package/dist/activities/agent-turn/credential-restriction.d.ts +3 -0
- package/dist/activities/agent-turn/errors.d.ts +185 -26
- package/dist/activities/agent-turn/failure-settlement.d.ts +10 -43
- package/dist/activities/agent-turn/final-reply.d.ts +18 -0
- package/dist/activities/agent-turn/finalization-monitor.d.ts +7 -2
- package/dist/activities/agent-turn/finalization.d.ts +2 -0
- package/dist/activities/agent-turn/github-repository-bindings.d.ts +2 -0
- package/dist/activities/agent-turn/governance-model.d.ts +13 -1
- package/dist/activities/agent-turn/history-sink.d.ts +4 -1
- package/dist/activities/agent-turn/history.d.ts +2 -0
- package/dist/activities/agent-turn/input-wait-reply.d.ts +76 -0
- package/dist/activities/agent-turn/meaningful-output.d.ts +9 -0
- package/dist/activities/agent-turn/media-artifacts.d.ts +5 -3
- package/dist/activities/agent-turn/model-call-admission.d.ts +15 -0
- package/dist/activities/agent-turn/model-usage.d.ts +56 -14
- package/dist/activities/agent-turn/model-wire-dispatch.d.ts +15 -0
- package/dist/activities/agent-turn/optional-repositories.d.ts +59 -0
- package/dist/activities/agent-turn/prompt-tool-availability.d.ts +17 -0
- package/dist/activities/agent-turn/provider-dispatch-barrier.d.ts +12 -1
- package/dist/activities/agent-turn/provider-recovery-copy.d.ts +24 -0
- package/dist/activities/agent-turn/provider-recovery-metrics.d.ts +18 -0
- package/dist/activities/agent-turn/provider-recovery-policy.d.ts +6 -0
- package/dist/activities/agent-turn/quiescence.d.ts +6 -7
- package/dist/activities/agent-turn/recovery-warning.d.ts +6 -5
- package/dist/activities/agent-turn/refresh-credentials.d.ts +6 -0
- package/dist/activities/agent-turn/run-credentials.d.ts +9 -5
- package/dist/activities/agent-turn/run.d.ts +27 -2
- package/dist/activities/agent-turn/sandbox-establish.d.ts +1 -0
- package/dist/activities/agent-turn/sandbox-provision.d.ts +25 -1
- package/dist/activities/agent-turn/sandbox-route.d.ts +3 -0
- package/dist/activities/agent-turn/sandbox-runtime.d.ts +1 -1
- package/dist/activities/agent-turn/secret-resolver.d.ts +64 -0
- package/dist/activities/agent-turn/session-title.d.ts +60 -3
- package/dist/activities/agent-turn/session-usage-custody.d.ts +66 -0
- package/dist/activities/agent-turn/skill-checkout.d.ts +25 -6
- package/dist/activities/agent-turn/skill-read.d.ts +70 -2
- package/dist/activities/agent-turn/skill-search.d.ts +2 -0
- package/dist/activities/agent-turn/skill-tools.d.ts +41 -4
- package/dist/activities/agent-turn/skill-transfer.d.ts +18 -6
- package/dist/activities/agent-turn/stream-attempt.d.ts +6 -3
- package/dist/activities/agent-turn/subscription-capacity-arming.d.ts +41 -0
- package/dist/activities/agent-turn/subscription-core-capacity.d.ts +86 -0
- package/dist/activities/agent-turn/subscription-core-failover.d.ts +69 -0
- package/dist/activities/agent-turn/subscription-core-placed.d.ts +14 -0
- package/dist/activities/agent-turn/subscription-core-requests.d.ts +109 -0
- package/dist/activities/agent-turn/subscription-core-settlement.d.ts +112 -0
- package/dist/activities/agent-turn/subscription-lease.d.ts +44 -0
- package/dist/activities/agent-turn/subscription-serving-capacity.d.ts +6 -0
- package/dist/activities/agent-turn/subscription-serving-catalog.d.ts +13 -0
- package/dist/activities/agent-turn/subscription-serving-claim.d.ts +30 -0
- package/dist/activities/agent-turn/subscription-serving-providers.d.ts +2 -0
- package/dist/activities/agent-turn/tool-environment.d.ts +31 -6
- package/dist/activities/agent-turn/tool-policy.d.ts +55 -3
- package/dist/activities/agent-turn/turn-context.d.ts +104 -5
- package/dist/activities/agent-turn/web-search.d.ts +57 -0
- package/dist/activities/agent-turn/xai-core.d.ts +96 -0
- package/dist/activities/automations.d.ts +3 -1
- package/dist/activities/browser-deadline-checkpoint.d.ts +34 -0
- package/dist/activities/capabilities.d.ts +8 -20
- package/dist/activities/codemode-dispatcher.d.ts +20 -3
- package/dist/activities/codex-capacity.d.ts +1 -56
- package/dist/activities/codex-image-generation.d.ts +49 -3
- package/dist/activities/common.d.ts +3 -1
- package/dist/activities/context-compaction.d.ts +41 -2
- package/dist/activities/environment.d.ts +1 -19
- package/dist/activities/gmail-files.d.ts +50 -0
- package/dist/activities/goals.d.ts +1 -9
- package/dist/activities/incident-telemetry-authority.d.ts +5 -2
- package/dist/activities/incident-telemetry-preflight.d.ts +10 -3
- package/dist/activities/knowledge-indexing.d.ts +19 -0
- package/dist/activities/mcp-account-routes.d.ts +2 -21
- package/dist/activities/model-call-fact-reconciler.d.ts +34 -0
- package/dist/activities/open-suffix-resume.d.ts +15 -0
- package/dist/activities/parent-wake.d.ts +17 -12
- package/dist/activities/programmatic-approvals.d.ts +10 -0
- package/dist/activities/requester-unavailable-receipt.d.ts +8 -0
- package/dist/activities/retained-screenshots.d.ts +7 -2
- package/dist/activities/run-credential-renewal.d.ts +1 -0
- package/dist/activities/run-credentials.d.ts +16 -4
- package/dist/activities/run-input.d.ts +17 -0
- package/dist/activities/sandbox-lease.d.ts +132 -12
- package/dist/activities/scheduled-tasks.d.ts +4 -35
- package/dist/activities/session-archive.d.ts +58 -0
- package/dist/activities/session-state.d.ts +15 -3
- package/dist/activities/session-storage.d.ts +44 -0
- package/dist/activities/subscription-core-claude-waits.d.ts +2 -0
- package/dist/activities/subscription-core-codex-quota.d.ts +25 -0
- package/dist/activities/subscription-core-codex-waits.d.ts +153 -0
- package/dist/activities/subscription-core-image-operation.d.ts +29 -0
- package/dist/activities/subscription-core-usage-recheck.d.ts +49 -0
- package/dist/activities/subscription-core-wait-bindings.d.ts +3 -0
- package/dist/activities/subscription-core-xai-quota.d.ts +30 -0
- package/dist/activities/subscription-core-xai-waits.d.ts +2 -0
- package/dist/activities/types.d.ts +68 -5
- package/dist/activities/video-generation-admission.d.ts +2 -8
- package/dist/activities/video-generation-credential.d.ts +15 -12
- package/dist/activities/video-generation-subscription-core.d.ts +70 -0
- package/dist/activities/workspace-capture.d.ts +2 -0
- package/dist/activities/workspace-credential-provider.d.ts +47 -0
- package/dist/activities/xai-auth.d.ts +6 -18
- package/dist/activities/xai-image-generation.d.ts +22 -6
- package/dist/{activities-control-QA6OLRQY.js → activities-control-5IIVRXLA.js} +1569 -2883
- package/dist/activities-control-5IIVRXLA.js.map +1 -0
- package/dist/activities-control.d.ts +64 -4
- package/dist/{activities-turn-YVVKCGAK.js → activities-turn-FLYJOM54.js} +9300 -4403
- package/dist/activities-turn-FLYJOM54.js.map +1 -0
- package/dist/activities.d.ts +64 -4
- package/dist/artifact-materializer-entry.js +107 -36
- package/dist/artifact-materializer-entry.js.map +1 -1
- package/dist/artifact-outbox-entry.js +132 -61
- package/dist/artifact-outbox-entry.js.map +1 -1
- package/dist/browser-deadline-checkpoint-contract.d.ts +2 -0
- package/dist/{chunk-GDCLDKSH.js → chunk-24GP3BX5.js} +79 -72
- package/dist/chunk-24GP3BX5.js.map +1 -0
- package/dist/{chunk-RAMNYSAW.js → chunk-4ER3YXOT.js} +22 -4
- package/dist/chunk-4ER3YXOT.js.map +1 -0
- package/dist/{chunk-I7HKKMUJ.js → chunk-5NNS4327.js} +734 -58
- package/dist/chunk-5NNS4327.js.map +1 -0
- package/dist/chunk-MVESOYFC.js +3746 -0
- package/dist/chunk-MVESOYFC.js.map +1 -0
- package/dist/{chunk-M3NVMF2V.js → chunk-N57B3PJ5.js} +2307 -3951
- package/dist/chunk-N57B3PJ5.js.map +1 -0
- package/dist/chunk-NQDVJ6RI.js +2823 -0
- package/dist/chunk-NQDVJ6RI.js.map +1 -0
- package/dist/chunk-O7UPBWXK.js +79 -0
- package/dist/chunk-O7UPBWXK.js.map +1 -0
- package/dist/chunk-W2QSM5HR.js +414 -0
- package/dist/chunk-W2QSM5HR.js.map +1 -0
- package/dist/chunk-ZN3XJR33.js +227 -0
- package/dist/chunk-ZN3XJR33.js.map +1 -0
- package/dist/editable-artifact-outbox-dispatcher.d.ts +12 -1
- package/dist/editable-artifact-outbox-service.d.ts +3 -1
- package/dist/host-export-pump.d.ts +3 -1
- package/dist/host-export.d.ts +2 -2
- package/dist/host-export.js +1 -1
- package/dist/http.d.ts +1 -1
- package/dist/index.d.ts +49 -48
- package/dist/index.js +835 -256
- package/dist/index.js.map +1 -1
- package/dist/internal-maintenance.d.ts +63 -0
- package/dist/maintenance-cadence.d.ts +43 -0
- package/dist/observability-metrics.d.ts +293 -24
- package/dist/onepassword-vault-provider.d.ts +7 -0
- package/dist/parent-wake-PCLH7A3Y.js +24 -0
- package/dist/parent-wake-PCLH7A3Y.js.map +1 -0
- package/dist/personal-github-git-credentials.d.ts +1 -1
- package/dist/readiness-checks.d.ts +39 -0
- package/dist/retained-process-retry.d.ts +7 -0
- package/dist/sandbox-lease-BJYU72N3.js +84 -0
- package/dist/sandbox-lease-BJYU72N3.js.map +1 -0
- package/dist/sandbox-reaper-contract.d.ts +25 -0
- package/dist/sandbox-resume.d.ts +72 -4
- package/dist/sandbox-routing.d.ts +4 -2
- package/dist/session-event-fanout.d.ts +6 -0
- package/dist/site-auth-maintenance-WK3MR2PV.js +12 -0
- package/dist/site-auth-maintenance-WK3MR2PV.js.map +1 -0
- package/dist/telemetry-scheduler.d.ts +14 -0
- package/dist/temporal-runtime-logger.d.ts +22 -0
- package/dist/turn-task-queue-reader.d.ts +18 -0
- package/dist/turn-worker-activity-telemetry.d.ts +47 -0
- package/dist/worker-service-lifecycle.d.ts +12 -0
- package/dist/workflow-bundle.js +468 -71
- package/dist/workflows/activities.d.ts +1 -1
- package/dist/workflows/browser-deadline-checkpoint.d.ts +10 -0
- package/dist/workflows/sandbox-reaper.d.ts +10 -1
- package/dist/workflows/scheduled-tasks.d.ts +4 -0
- package/dist/workflows/session-storage.d.ts +2 -0
- package/dist/workflows/session.d.ts +36 -0
- package/dist/workflows/site-auth-maintenance.d.ts +2 -1
- package/dist/workflows/workflow-wake.d.ts +14 -3
- package/dist/workflows.d.ts +3 -1
- package/package.json +26 -20
- package/src/activities/agent-run-admission.ts +1 -82
- package/src/activities/agent-turn/admission.ts +175 -24
- package/src/activities/agent-turn/agent-build.ts +226 -145
- package/src/activities/agent-turn/claim.ts +363 -64
- package/src/activities/agent-turn/claude-core.ts +587 -0
- package/src/activities/agent-turn/code-search.ts +275 -0
- package/src/activities/agent-turn/codex-capacity.ts +30 -757
- package/src/activities/agent-turn/codex-core-capacity.ts +190 -0
- package/src/activities/agent-turn/codex-core-claim.ts +83 -0
- package/src/activities/agent-turn/codex-core-errors.ts +308 -0
- package/src/activities/agent-turn/codex-core-failover.ts +73 -0
- package/src/activities/agent-turn/codex-core-requests.ts +86 -0
- package/src/activities/agent-turn/codex-core-settlement.ts +263 -0
- package/src/activities/agent-turn/codex-credit-policy.ts +247 -0
- package/src/activities/agent-turn/codex-plan-entitlement.ts +254 -0
- package/src/activities/agent-turn/codex-usage-policy.ts +2 -0
- package/src/activities/agent-turn/codex.ts +8 -76
- package/src/activities/agent-turn/compaction-interruption.ts +56 -0
- package/src/activities/agent-turn/compaction-prep.ts +249 -36
- package/src/activities/agent-turn/credential-leases.ts +269 -214
- package/src/activities/agent-turn/credential-restriction.ts +14 -0
- package/src/activities/agent-turn/errors.ts +1152 -82
- package/src/activities/agent-turn/failure-settlement.ts +879 -886
- package/src/activities/agent-turn/final-reply.ts +46 -0
- package/src/activities/agent-turn/finalization-monitor.ts +17 -5
- package/src/activities/agent-turn/finalization.ts +107 -88
- package/src/activities/agent-turn/github-repository-bindings.ts +3 -1
- package/src/activities/agent-turn/governance-model.ts +90 -22
- package/src/activities/agent-turn/history-sink.ts +42 -12
- package/src/activities/agent-turn/history.ts +25 -5
- package/src/activities/agent-turn/input-wait-reply.ts +139 -0
- package/src/activities/agent-turn/knowledge-source-tools.ts +2 -0
- package/src/activities/agent-turn/meaningful-output.ts +44 -0
- package/src/activities/agent-turn/media-artifacts.ts +3 -1
- package/src/activities/agent-turn/model-call-admission.ts +114 -0
- package/src/activities/agent-turn/model-usage.ts +299 -233
- package/src/activities/agent-turn/model-wire-dispatch.ts +27 -0
- package/src/activities/agent-turn/optional-repositories.ts +224 -0
- package/src/activities/agent-turn/prompt-tool-availability.ts +27 -0
- package/src/activities/agent-turn/provider-dispatch-barrier.ts +22 -0
- package/src/activities/agent-turn/provider-recovery-copy.ts +76 -0
- package/src/activities/agent-turn/provider-recovery-metrics.ts +64 -0
- package/src/activities/agent-turn/provider-recovery-policy.ts +25 -0
- package/src/activities/agent-turn/quiescence.ts +7 -7
- package/src/activities/agent-turn/recovery-warning.ts +6 -8
- package/src/activities/agent-turn/refresh-credentials.ts +44 -0
- package/src/activities/agent-turn/run-credentials.ts +124 -76
- package/src/activities/agent-turn/run.ts +501 -112
- package/src/activities/agent-turn/sandbox-establish.ts +152 -75
- package/src/activities/agent-turn/sandbox-provision.ts +54 -1
- package/src/activities/agent-turn/sandbox-route.ts +4 -1
- package/src/activities/agent-turn/sandbox-runtime.ts +319 -11
- package/src/activities/agent-turn/secret-resolver.ts +155 -0
- package/src/activities/agent-turn/session-title.ts +177 -12
- package/src/activities/agent-turn/session-usage-custody.ts +170 -0
- package/src/activities/agent-turn/skill-checkout.ts +111 -24
- package/src/activities/agent-turn/skill-install.ts +1 -1
- package/src/activities/agent-turn/skill-read.ts +374 -28
- package/src/activities/agent-turn/skill-save.ts +35 -7
- package/src/activities/agent-turn/skill-search.ts +9 -0
- package/src/activities/agent-turn/skill-selection.ts +4 -0
- package/src/activities/agent-turn/skill-tools.ts +208 -21
- package/src/activities/agent-turn/skill-transfer.ts +68 -23
- package/src/activities/agent-turn/stream-attempt.ts +634 -111
- package/src/activities/agent-turn/subscription-capacity-arming.ts +81 -0
- package/src/activities/agent-turn/subscription-core-capacity.ts +439 -0
- package/src/activities/agent-turn/subscription-core-failover.ts +229 -0
- package/src/activities/agent-turn/subscription-core-placed.ts +37 -0
- package/src/activities/agent-turn/subscription-core-requests.ts +271 -0
- package/src/activities/agent-turn/subscription-core-settlement.ts +234 -0
- package/src/activities/agent-turn/subscription-lease.ts +140 -0
- package/src/activities/agent-turn/subscription-serving-capacity.ts +195 -0
- package/src/activities/agent-turn/subscription-serving-catalog.ts +71 -0
- package/src/activities/agent-turn/subscription-serving-claim.ts +178 -0
- package/src/activities/agent-turn/subscription-serving-providers.ts +15 -0
- package/src/activities/agent-turn/tool-environment.ts +384 -292
- package/src/activities/agent-turn/tool-policy.ts +104 -4
- package/src/activities/agent-turn/tool-result-spill.ts +1 -0
- package/src/activities/agent-turn/turn-context.ts +124 -8
- package/src/activities/agent-turn/web-search.ts +431 -0
- package/src/activities/agent-turn/xai-core.ts +301 -0
- package/src/activities/automations.ts +81 -6
- package/src/activities/browser-deadline-checkpoint.ts +297 -0
- package/src/activities/capabilities.ts +53 -213
- package/src/activities/codemode-dispatcher.ts +236 -14
- package/src/activities/codex-capacity.ts +27 -380
- package/src/activities/codex-image-generation.ts +252 -42
- package/src/activities/common.ts +12 -0
- package/src/activities/connector-attachments.ts +3 -1
- package/src/activities/context-compaction.ts +182 -21
- package/src/activities/environment.ts +18 -106
- package/src/activities/gmail-files.ts +108 -0
- package/src/activities/goals.ts +120 -204
- package/src/activities/image-generation-operation.ts +1 -1
- package/src/activities/incident-telemetry-authority.ts +80 -50
- package/src/activities/incident-telemetry-preflight.ts +27 -15
- package/src/activities/knowledge-indexing.ts +287 -17
- package/src/activities/knowledge-source-sync.ts +10 -0
- package/src/activities/mcp-account-routes.ts +2 -124
- package/src/activities/model-call-fact-reconciler.ts +112 -0
- package/src/activities/open-suffix-resume.ts +122 -88
- package/src/activities/parent-wake.ts +89 -21
- package/src/activities/programmatic-approvals.ts +26 -0
- package/src/activities/requester-unavailable-receipt.ts +14 -0
- package/src/activities/retained-screenshots.ts +135 -24
- package/src/activities/run-credential-renewal.ts +13 -1
- package/src/activities/run-credentials.ts +61 -19
- package/src/activities/run-input.ts +124 -9
- package/src/activities/sandbox-lease.ts +1615 -73
- package/src/activities/scheduled-tasks.ts +780 -147
- package/src/activities/session-archive.ts +382 -0
- package/src/activities/session-state.ts +219 -10
- package/src/activities/session-storage.ts +124 -0
- package/src/activities/site-auth-maintenance.ts +5 -2
- package/src/activities/subscription-core-claude-waits.ts +49 -0
- package/src/activities/subscription-core-codex-quota.ts +46 -0
- package/src/activities/subscription-core-codex-waits.ts +488 -0
- package/src/activities/subscription-core-image-operation.ts +126 -0
- package/src/activities/subscription-core-usage-recheck.ts +146 -0
- package/src/activities/subscription-core-wait-bindings.ts +12 -0
- package/src/activities/subscription-core-xai-quota.ts +85 -0
- package/src/activities/subscription-core-xai-waits.ts +48 -0
- package/src/activities/types.ts +85 -9
- package/src/activities/video-generation-admission.ts +1 -36
- package/src/activities/video-generation-credential.ts +38 -56
- package/src/activities/video-generation-reconciliation.ts +150 -131
- package/src/activities/video-generation-subscription-core.ts +180 -0
- package/src/activities/workspace-capture.ts +13 -2
- package/src/activities/workspace-credential-provider.ts +302 -0
- package/src/activities/xai-auth.ts +34 -112
- package/src/activities/xai-image-generation.ts +84 -36
- package/src/activities-control.ts +8 -0
- package/src/activities-turn.ts +5 -0
- package/src/activities.ts +17 -0
- package/src/activity-services.ts +29 -4
- package/src/browser-deadline-checkpoint-contract.ts +5 -0
- package/src/editable-artifact-hint-broker.ts +3 -0
- package/src/editable-artifact-materializer-service.ts +85 -39
- package/src/editable-artifact-materializer-subprocess.ts +47 -2
- package/src/editable-artifact-outbox-dispatcher.ts +107 -46
- package/src/editable-artifact-outbox-service.ts +55 -25
- package/src/github-rest-mcp.ts +1 -1
- package/src/host-export-pump.ts +26 -4
- package/src/host-export.ts +4 -1
- package/src/http.ts +4 -2
- package/src/index.ts +222 -282
- package/src/internal-maintenance.ts +347 -0
- package/src/maintenance-cadence.ts +156 -0
- package/src/observability-metrics.ts +1173 -73
- package/src/onepassword-vault-provider.ts +11 -0
- package/src/personal-github-git-credentials.ts +3 -1
- package/src/pr-review-credentials.ts +1 -1
- package/src/readiness-checks.ts +113 -0
- package/src/retained-process-retry.ts +36 -0
- package/src/sandbox-reaper-contract.ts +33 -0
- package/src/sandbox-resume.ts +444 -73
- package/src/sandbox-routing.ts +230 -75
- package/src/session-event-fanout.ts +23 -0
- package/src/telemetry-scheduler.ts +53 -0
- package/src/temporal-runtime-logger.ts +44 -0
- package/src/turn-task-queue-reader.ts +110 -0
- package/src/turn-worker-activity-telemetry.ts +209 -0
- package/src/worker-service-lifecycle.ts +54 -1
- package/src/workflows/activities.ts +16 -1
- package/src/workflows/browser-deadline-checkpoint.ts +55 -0
- package/src/workflows/file-upload-reaper.ts +2 -0
- package/src/workflows/sandbox-reaper.ts +88 -0
- package/src/workflows/scheduled-tasks.ts +26 -13
- package/src/workflows/session-storage.ts +19 -0
- package/src/workflows/session.ts +238 -33
- package/src/workflows/site-auth-maintenance.ts +2 -1
- package/src/workflows/workflow-wake.ts +26 -4
- package/src/workflows.ts +7 -0
- package/dist/activities/agent-turn/xai-capacity.d.ts +0 -2
- package/dist/activities/codex-fleet-shadow.d.ts +0 -74
- package/dist/activities/codex-rotation.d.ts +0 -200
- package/dist/activities/xai-quota.d.ts +0 -3
- package/dist/activities-control-QA6OLRQY.js.map +0 -1
- package/dist/activities-turn-YVVKCGAK.js.map +0 -1
- package/dist/chunk-GDCLDKSH.js.map +0 -1
- package/dist/chunk-I7HKKMUJ.js.map +0 -1
- package/dist/chunk-M3NVMF2V.js.map +0 -1
- package/dist/chunk-RAMNYSAW.js.map +0 -1
- package/src/activities/agent-turn/xai-capacity.ts +0 -257
- package/src/activities/codex-fleet-shadow.ts +0 -306
- package/src/activities/codex-rotation.ts +0 -685
- package/src/activities/xai-quota.ts +0 -35
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { CodexProviderRequestIdentity, CodexProviderRequestSettlement } from "@opengeni/codex";
|
|
2
|
+
import { fetchCodexUsage } from "@opengeni/codex";
|
|
3
|
+
import {
|
|
4
|
+
buildSubscriptionCoreCodexOperationFetch,
|
|
5
|
+
reserveSubscriptionCoreCodexTurnCredentialRequest,
|
|
6
|
+
settleSubscriptionCoreCodexTurnCredentialRequest,
|
|
7
|
+
SubscriptionCoreCodexRequestOutcomeUnknownError,
|
|
8
|
+
type Database,
|
|
9
|
+
type SubscriptionCoreTurnIdentity,
|
|
10
|
+
type SubscriptionCoreCodexLeaseRef,
|
|
11
|
+
} from "@opengeni/db";
|
|
12
|
+
import { createCoreSubscriptionRequests } from "./subscription-core-requests";
|
|
13
|
+
|
|
14
|
+
export { providerRequestIdPrefix } from "./subscription-core-requests";
|
|
15
|
+
|
|
16
|
+
/** The usage probe is a finite read, authorized by this attempt's chat lease. */
|
|
17
|
+
export function buildCoreCodexUsageReader(
|
|
18
|
+
db: Database,
|
|
19
|
+
identity: SubscriptionCoreTurnIdentity,
|
|
20
|
+
ref: SubscriptionCoreCodexLeaseRef,
|
|
21
|
+
execution: { attemptId: string; executionGeneration: number },
|
|
22
|
+
fetchImpl?: Parameters<typeof buildSubscriptionCoreCodexOperationFetch>[4],
|
|
23
|
+
): typeof fetchCodexUsage {
|
|
24
|
+
const physicalFetch = buildSubscriptionCoreCodexOperationFetch(
|
|
25
|
+
db,
|
|
26
|
+
{ kind: "turn", identity },
|
|
27
|
+
null,
|
|
28
|
+
ref.connectionId,
|
|
29
|
+
fetchImpl,
|
|
30
|
+
{
|
|
31
|
+
// There is no separate operation lease for a turn's usage precheck.
|
|
32
|
+
// Keep exact-attempt/chat-lease authority, but classify the read as a
|
|
33
|
+
// credential request: an unknown GET is not an unknown model response.
|
|
34
|
+
// Reuse finite-body buffering and bounded timeout custody.
|
|
35
|
+
reserve: (_db, _scope, _lease, _connection, request) =>
|
|
36
|
+
reserveSubscriptionCoreCodexTurnCredentialRequest(db, identity, ref, {
|
|
37
|
+
...request,
|
|
38
|
+
...execution,
|
|
39
|
+
}),
|
|
40
|
+
settle: (_db, _scope, request) =>
|
|
41
|
+
settleSubscriptionCoreCodexTurnCredentialRequest(db, identity, ref, {
|
|
42
|
+
...request,
|
|
43
|
+
...execution,
|
|
44
|
+
}),
|
|
45
|
+
},
|
|
46
|
+
);
|
|
47
|
+
return async (auth, _fetch, timeout) => {
|
|
48
|
+
// The shared finite-read client normalizes transport errors. This native
|
|
49
|
+
// admission refusal must survive that boundary instead of becoming a
|
|
50
|
+
// retryable/rotatable usage-policy error. Keep it local to this probe.
|
|
51
|
+
let unresolved: SubscriptionCoreCodexRequestOutcomeUnknownError | null = null;
|
|
52
|
+
try {
|
|
53
|
+
return await fetchCodexUsage(
|
|
54
|
+
auth,
|
|
55
|
+
async (url, init) => {
|
|
56
|
+
try {
|
|
57
|
+
return await physicalFetch(url, init);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
if (error instanceof SubscriptionCoreCodexRequestOutcomeUnknownError)
|
|
60
|
+
unresolved = error;
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
timeout,
|
|
65
|
+
);
|
|
66
|
+
} catch (error) {
|
|
67
|
+
throw unresolved ?? error;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** One attempt's bounded Codex request custody (the shared custody, Codex errors). */
|
|
73
|
+
export function createCoreCodexRequests(deps: {
|
|
74
|
+
reserve: (request: CodexProviderRequestIdentity) => Promise<{ operationId: string }>;
|
|
75
|
+
settle: (request: {
|
|
76
|
+
operationId: string;
|
|
77
|
+
outcome: CodexProviderRequestSettlement["outcome"];
|
|
78
|
+
}) => Promise<void>;
|
|
79
|
+
}) {
|
|
80
|
+
return createCoreSubscriptionRequests({
|
|
81
|
+
...deps,
|
|
82
|
+
displayName: "Codex",
|
|
83
|
+
outcomeUnknown: () => new SubscriptionCoreCodexRequestOutcomeUnknownError(),
|
|
84
|
+
isOutcomeUnknown: (error) => error instanceof SubscriptionCoreCodexRequestOutcomeUnknownError,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Failure, usage and quota bookkeeping for Codex turns placed by the shared
|
|
3
|
+
* subscription core (M3 PR 1, inventory EP-T06..T08). Every write is scoped
|
|
4
|
+
* to the exact accepted turn and its live lease, and is best effort: it can
|
|
5
|
+
* never change how the turn itself settles.
|
|
6
|
+
*/
|
|
7
|
+
import type { CodexUsageHeaderSnapshot } from "@opengeni/codex";
|
|
8
|
+
import {
|
|
9
|
+
applySubscriptionCoreCodexQuotaObservation,
|
|
10
|
+
quarantineSubscriptionCoreCodexConnection,
|
|
11
|
+
recordSubscriptionCoreCodexModelCooldown,
|
|
12
|
+
recordSubscriptionCoreCodexQuotaObservation,
|
|
13
|
+
recordSubscriptionCoreCodexTurnFailure,
|
|
14
|
+
SUBSCRIPTION_CORE_CODEX_ENTITLEMENT_COOLDOWN_MS,
|
|
15
|
+
touchSubscriptionCoreCodexBinding,
|
|
16
|
+
type Database,
|
|
17
|
+
type SubscriptionCoreCodexLeaseRef,
|
|
18
|
+
} from "@opengeni/db";
|
|
19
|
+
import type { QuotaWindow, SubscriptionQuota } from "@opengeni/subscriptions";
|
|
20
|
+
import { codexCredentialCooldownUntil, type CodexCredentialFailure } from "./errors";
|
|
21
|
+
import type { CodexTurnLease } from "./credential-leases";
|
|
22
|
+
import type { CodexSubscriptionCoreTurn, ProviderTurnState } from "./turn-context";
|
|
23
|
+
|
|
24
|
+
/** Record a Codex model call that produced a response (diagnostic phase `completed`). */
|
|
25
|
+
export function observeCodexResponseCompletion(
|
|
26
|
+
providerTurn: Pick<ProviderTurnState, "lastCodexResponseCompletedAt">,
|
|
27
|
+
event: { phase: string },
|
|
28
|
+
now: number = Date.now(),
|
|
29
|
+
): void {
|
|
30
|
+
if (event.phase === "completed") providerTurn.lastCodexResponseCompletedAt = now;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* When the binding's cache clock may advance: only after a model call that
|
|
35
|
+
* produced a response. A refused or failed call leaves no warm cache.
|
|
36
|
+
*/
|
|
37
|
+
export function coreCodexModelCallCompletedAt(
|
|
38
|
+
providerTurn: Pick<ProviderTurnState, "lastCodexResponseCompletedAt">,
|
|
39
|
+
): Date | null {
|
|
40
|
+
const at = providerTurn.lastCodexResponseCompletedAt;
|
|
41
|
+
return typeof at === "number" && Number.isFinite(at) ? new Date(at) : null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Same banding as the legacy usage cache projection (legacy-subscription-world). */
|
|
45
|
+
const NEAR_EXHAUSTION_PERCENT = 90;
|
|
46
|
+
|
|
47
|
+
function usageWindow(id: string, usedPercent: number, resetsAt: Date | null): QuotaWindow {
|
|
48
|
+
return {
|
|
49
|
+
id,
|
|
50
|
+
usedPercent,
|
|
51
|
+
resetsAt: resetsAt?.getTime() ?? null,
|
|
52
|
+
status:
|
|
53
|
+
usedPercent >= 100 ? "exhausted" : usedPercent >= NEAR_EXHAUSTION_PERCENT ? "warning" : "ok",
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Codex usage response headers as a shared quota observation (design 2.2). */
|
|
58
|
+
export function codexUsageHeadersQuotaObservation(
|
|
59
|
+
snapshot: CodexUsageHeaderSnapshot,
|
|
60
|
+
observedRefreshGeneration: number,
|
|
61
|
+
): SubscriptionQuota {
|
|
62
|
+
return {
|
|
63
|
+
windows: [
|
|
64
|
+
usageWindow("primary", snapshot.primaryUsedPercent, snapshot.primaryResetAt),
|
|
65
|
+
usageWindow("secondary", snapshot.secondaryUsedPercent, snapshot.secondaryResetAt),
|
|
66
|
+
],
|
|
67
|
+
modelCooldowns: {},
|
|
68
|
+
exhaustedUntil: null,
|
|
69
|
+
exhaustedKind: null,
|
|
70
|
+
revision: 0,
|
|
71
|
+
observedAt: snapshot.checkedAt.getTime(),
|
|
72
|
+
observedRefreshGeneration,
|
|
73
|
+
source: "response_headers",
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** A Codex quota or rate-limit refusal as a shared quota observation. */
|
|
78
|
+
export function codexRefusalQuotaObservation(
|
|
79
|
+
failure: CodexCredentialFailure,
|
|
80
|
+
observedRefreshGeneration: number,
|
|
81
|
+
now: Date,
|
|
82
|
+
): SubscriptionQuota | null {
|
|
83
|
+
if (failure.kind !== "quota" && failure.kind !== "rate_limit") return null;
|
|
84
|
+
const until = codexCredentialCooldownUntil(failure, null, now);
|
|
85
|
+
return {
|
|
86
|
+
windows: [],
|
|
87
|
+
modelCooldowns: {},
|
|
88
|
+
exhaustedUntil: until?.getTime() ?? null,
|
|
89
|
+
exhaustedKind: failure.kind,
|
|
90
|
+
revision: 0,
|
|
91
|
+
observedAt: now.getTime(),
|
|
92
|
+
observedRefreshGeneration,
|
|
93
|
+
source: "refusal",
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function leaseRef(
|
|
98
|
+
core: CodexSubscriptionCoreTurn,
|
|
99
|
+
lease: Pick<CodexTurnLease, "holderId" | "generation">,
|
|
100
|
+
): SubscriptionCoreCodexLeaseRef | null {
|
|
101
|
+
if (!lease.holderId || lease.generation === null) return null;
|
|
102
|
+
return {
|
|
103
|
+
connectionId: core.connectionId,
|
|
104
|
+
holderId: lease.holderId,
|
|
105
|
+
generation: lease.generation,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Record a definitive Codex refusal against the leased connection: always a
|
|
111
|
+
* (turn, connection) failure receipt, which also counts toward the per-turn
|
|
112
|
+
* failover bound, then the connection state that keeps placement away from
|
|
113
|
+
* it (M3 PR 2a):
|
|
114
|
+
*
|
|
115
|
+
* - quota and rate limits: a quota observation fenced on the refresh
|
|
116
|
+
* generation the refused bearer carried (exhausted until the reset);
|
|
117
|
+
* - a 401 that survived refresh: health `needs_relogin` (the refresh seam
|
|
118
|
+
* itself already marks a refused OAuth refresh);
|
|
119
|
+
* - a 403 that survived refresh: a time-bound health quarantine;
|
|
120
|
+
* - a plan-entitlement refusal: a cooldown of that model on the connection.
|
|
121
|
+
*
|
|
122
|
+
* A credit-policy refusal (`failure.origin`) is the worker's own refusal, not
|
|
123
|
+
* a provider usage reading, so it is recorded as a `refusal` observation: it
|
|
124
|
+
* keeps a stored exhausted window with a known future reset (for example the
|
|
125
|
+
* weekly `secondary` window) instead of replacing every window, and only a
|
|
126
|
+
* real usage reading replaces them all (design 2.2).
|
|
127
|
+
*
|
|
128
|
+
* Every write requires the exact accepted turn, the enabled gate and this
|
|
129
|
+
* turn's live lease. `receipt` reports whether the refusal was recorded.
|
|
130
|
+
*/
|
|
131
|
+
export async function recordCoreCodexRefusal(input: {
|
|
132
|
+
db: Database;
|
|
133
|
+
core: CodexSubscriptionCoreTurn;
|
|
134
|
+
lease: Pick<CodexTurnLease, "holderId" | "generation">;
|
|
135
|
+
failure: CodexCredentialFailure;
|
|
136
|
+
credentialVersion: number | null;
|
|
137
|
+
/** The refused model; required to keep a plan-entitlement refusal model-scoped. */
|
|
138
|
+
modelId?: string | null;
|
|
139
|
+
now?: Date;
|
|
140
|
+
}): Promise<{ receipt: boolean; health: boolean }> {
|
|
141
|
+
const ref = leaseRef(input.core, input.lease);
|
|
142
|
+
if (!ref) return { receipt: false, health: false };
|
|
143
|
+
const now = input.now ?? new Date();
|
|
144
|
+
const generation = input.credentialVersion ?? input.core.placedRefreshGeneration;
|
|
145
|
+
if (input.failure.origin) {
|
|
146
|
+
const unknown = input.failure.origin === "usage_verification_policy";
|
|
147
|
+
const until = now.getTime() + Math.max(60, input.failure.cooldownSeconds ?? 60) * 1000;
|
|
148
|
+
const health = await recordSubscriptionCoreCodexQuotaObservation(
|
|
149
|
+
input.db,
|
|
150
|
+
input.core.identity,
|
|
151
|
+
ref,
|
|
152
|
+
{
|
|
153
|
+
windows: unknown
|
|
154
|
+
? []
|
|
155
|
+
: [{ id: "included_usage", usedPercent: 100, resetsAt: until, status: "exhausted" }],
|
|
156
|
+
modelCooldowns: {},
|
|
157
|
+
exhaustedUntil: unknown ? until : null,
|
|
158
|
+
exhaustedKind: unknown ? "rate_limit" : null,
|
|
159
|
+
revision: 0,
|
|
160
|
+
observedAt: now.getTime(),
|
|
161
|
+
observedRefreshGeneration: generation,
|
|
162
|
+
source: "refusal",
|
|
163
|
+
},
|
|
164
|
+
).catch(() => false);
|
|
165
|
+
return { receipt: health, health };
|
|
166
|
+
}
|
|
167
|
+
const receipt = await recordSubscriptionCoreCodexTurnFailure(input.db, input.core.identity, ref, {
|
|
168
|
+
kind: input.failure.kind,
|
|
169
|
+
evidence: {
|
|
170
|
+
refreshGeneration: generation,
|
|
171
|
+
cooldownSeconds: input.failure.cooldownSeconds,
|
|
172
|
+
...(input.failure.origin ? { origin: input.failure.origin } : {}),
|
|
173
|
+
},
|
|
174
|
+
}).catch(() => false);
|
|
175
|
+
let health = false;
|
|
176
|
+
try {
|
|
177
|
+
switch (input.failure.kind) {
|
|
178
|
+
case "quota":
|
|
179
|
+
case "rate_limit": {
|
|
180
|
+
const observation = codexRefusalQuotaObservation(input.failure, generation, now);
|
|
181
|
+
health = observation
|
|
182
|
+
? await recordSubscriptionCoreCodexQuotaObservation(
|
|
183
|
+
input.db,
|
|
184
|
+
input.core.identity,
|
|
185
|
+
ref,
|
|
186
|
+
observation,
|
|
187
|
+
)
|
|
188
|
+
: false;
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
case "auth":
|
|
192
|
+
case "forbidden":
|
|
193
|
+
health = await quarantineSubscriptionCoreCodexConnection(
|
|
194
|
+
input.db,
|
|
195
|
+
input.core.identity,
|
|
196
|
+
ref,
|
|
197
|
+
{
|
|
198
|
+
kind: input.failure.kind === "auth" ? "sign_in" : "forbidden",
|
|
199
|
+
refreshGeneration: generation,
|
|
200
|
+
now,
|
|
201
|
+
},
|
|
202
|
+
);
|
|
203
|
+
break;
|
|
204
|
+
case "plan_entitlement":
|
|
205
|
+
health = input.modelId
|
|
206
|
+
? await recordSubscriptionCoreCodexModelCooldown(input.db, input.core.identity, ref, {
|
|
207
|
+
modelId: input.modelId,
|
|
208
|
+
until: new Date(now.getTime() + SUBSCRIPTION_CORE_CODEX_ENTITLEMENT_COOLDOWN_MS),
|
|
209
|
+
refreshGeneration: generation,
|
|
210
|
+
})
|
|
211
|
+
: false;
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
} catch {
|
|
215
|
+
health = false;
|
|
216
|
+
}
|
|
217
|
+
return { receipt, health };
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Finalization for a core turn: the latest usage headers become a quota
|
|
222
|
+
* observation on the leased connection, and a completed model call moves
|
|
223
|
+
* the binding's cache-warmth clock. Both run before the lease is released.
|
|
224
|
+
* `capacityRecovered` reports an observation that ended a stored exhaustion,
|
|
225
|
+
* which the caller turns into a wake for the account's waiters.
|
|
226
|
+
*/
|
|
227
|
+
export async function finalizeCoreCodexUsage(input: {
|
|
228
|
+
db: Database;
|
|
229
|
+
core: CodexSubscriptionCoreTurn;
|
|
230
|
+
lease: Pick<CodexTurnLease, "holderId" | "generation">;
|
|
231
|
+
usage: CodexUsageHeaderSnapshot | null;
|
|
232
|
+
credentialVersion: number | null;
|
|
233
|
+
modelCallCompletedAt: Date | null;
|
|
234
|
+
}): Promise<{ capacityRecovered: boolean }> {
|
|
235
|
+
const ref = leaseRef(input.core, input.lease);
|
|
236
|
+
if (!ref) return { capacityRecovered: false };
|
|
237
|
+
let capacityRecovered = false;
|
|
238
|
+
const writes: Promise<unknown>[] = [];
|
|
239
|
+
if (input.usage && input.credentialVersion !== null) {
|
|
240
|
+
writes.push(
|
|
241
|
+
applySubscriptionCoreCodexQuotaObservation(
|
|
242
|
+
input.db,
|
|
243
|
+
input.core.identity,
|
|
244
|
+
ref,
|
|
245
|
+
codexUsageHeadersQuotaObservation(input.usage, input.credentialVersion),
|
|
246
|
+
).then((applied) => {
|
|
247
|
+
capacityRecovered = applied.recovered;
|
|
248
|
+
}),
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
if (input.modelCallCompletedAt) {
|
|
252
|
+
writes.push(
|
|
253
|
+
touchSubscriptionCoreCodexBinding(
|
|
254
|
+
input.db,
|
|
255
|
+
input.core.identity,
|
|
256
|
+
ref,
|
|
257
|
+
input.modelCallCompletedAt,
|
|
258
|
+
),
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
await Promise.allSettled(writes);
|
|
262
|
+
return { capacityRecovered };
|
|
263
|
+
}
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CODEX_CLIENT_VERSION,
|
|
3
|
+
fetchCodexUsage,
|
|
4
|
+
normalizeCodexUsage,
|
|
5
|
+
type CodexTokenSnapshot,
|
|
6
|
+
type CodexUsageHeaderSnapshot,
|
|
7
|
+
type CodexUsagePayload,
|
|
8
|
+
} from "@opengeni/codex";
|
|
9
|
+
import { SubscriptionCoreCodexRequestOutcomeUnknownError } from "@opengeni/db";
|
|
10
|
+
|
|
11
|
+
/** Local admission decision, never a synthetic provider/transport error. */
|
|
12
|
+
export class CodexIncludedUsageExhaustedError extends Error {
|
|
13
|
+
readonly code = "codex_included_usage_exhausted";
|
|
14
|
+
constructor(readonly resetsInSeconds: number | null) {
|
|
15
|
+
super("This account's included Codex usage is exhausted. Extra credits are protected.");
|
|
16
|
+
this.name = "CodexIncludedUsageExhaustedError";
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class CodexIncludedUsageUnknownError extends Error {
|
|
21
|
+
readonly code = "codex_included_usage_unknown";
|
|
22
|
+
constructor() {
|
|
23
|
+
super(
|
|
24
|
+
"Couldn't verify included Codex usage. This account is temporarily unavailable while extra credits are protected.",
|
|
25
|
+
);
|
|
26
|
+
this.name = "CodexIncludedUsageUnknownError";
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function findCodexCreditPolicyError(
|
|
31
|
+
error: unknown,
|
|
32
|
+
): CodexIncludedUsageExhaustedError | CodexIncludedUsageUnknownError | null {
|
|
33
|
+
const seen = new Set<object>();
|
|
34
|
+
for (let depth = 0; depth < 8 && error && typeof error === "object"; depth++) {
|
|
35
|
+
if (seen.has(error)) break;
|
|
36
|
+
seen.add(error);
|
|
37
|
+
if (
|
|
38
|
+
error instanceof CodexIncludedUsageExhaustedError ||
|
|
39
|
+
error instanceof CodexIncludedUsageUnknownError
|
|
40
|
+
)
|
|
41
|
+
return error;
|
|
42
|
+
error = (error as { cause?: unknown }).cause;
|
|
43
|
+
}
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** A model call can cross the provider's limit; the next call must be admitted again. */
|
|
48
|
+
export function createCodexCreditGuard(input: {
|
|
49
|
+
allowExtraCredits?: boolean;
|
|
50
|
+
/** Live accepted-pool admission, rechecked immediately before dispatch. */
|
|
51
|
+
canSpendCredits?: () => Promise<boolean>;
|
|
52
|
+
readUsage?: (token: CodexTokenSnapshot) => Promise<CodexUsagePayload>;
|
|
53
|
+
fetchUsage?: typeof fetchCodexUsage;
|
|
54
|
+
refreshToken?: () => Promise<CodexTokenSnapshot>;
|
|
55
|
+
now?: () => number;
|
|
56
|
+
onUsage?: (snapshot: CodexUsageHeaderSnapshot) => void;
|
|
57
|
+
}) {
|
|
58
|
+
const now = input.now ?? Date.now;
|
|
59
|
+
const readUsage =
|
|
60
|
+
input.readUsage ??
|
|
61
|
+
(async (token: CodexTokenSnapshot) => {
|
|
62
|
+
const result = await (input.fetchUsage ?? fetchCodexUsage)({
|
|
63
|
+
...token,
|
|
64
|
+
clientVersion: CODEX_CLIENT_VERSION,
|
|
65
|
+
});
|
|
66
|
+
if (result.status === 401) throw new CodexUsageUnauthorizedError();
|
|
67
|
+
return normalizeCodexUsage(result.status, result.payload);
|
|
68
|
+
});
|
|
69
|
+
let token: CodexTokenSnapshot | null = null;
|
|
70
|
+
type Observation = { windows: Array<{ used: number; reset: Date | null }>; checkedAt: Date };
|
|
71
|
+
let snapshot: Observation | null = null;
|
|
72
|
+
let spendingRefusal: CodexIncludedUsageExhaustedError | CodexIncludedUsageUnknownError | null =
|
|
73
|
+
null;
|
|
74
|
+
let observationRevision = 0;
|
|
75
|
+
let admissionTail: Promise<void> = Promise.resolve();
|
|
76
|
+
const observe = (next: CodexUsageHeaderSnapshot) => {
|
|
77
|
+
if (snapshot && next.checkedAt.getTime() < snapshot.checkedAt.getTime()) return;
|
|
78
|
+
observationRevision++;
|
|
79
|
+
snapshot = {
|
|
80
|
+
checkedAt: next.checkedAt,
|
|
81
|
+
windows: [
|
|
82
|
+
{ used: next.primaryUsedPercent, reset: next.primaryResetAt },
|
|
83
|
+
{ used: next.secondaryUsedPercent, reset: next.secondaryResetAt },
|
|
84
|
+
],
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
const check = (value: Observation, at: number): boolean => {
|
|
88
|
+
const windows = value.windows;
|
|
89
|
+
const exhausted = windows.filter((w) => w.used >= 100 && (!w.reset || w.reset.getTime() > at));
|
|
90
|
+
if (exhausted.length) {
|
|
91
|
+
const reset = exhausted.every((w) => w.reset)
|
|
92
|
+
? Math.max(...exhausted.map((w) => w.reset!.getTime()))
|
|
93
|
+
: null;
|
|
94
|
+
throw new CodexIncludedUsageExhaustedError(
|
|
95
|
+
reset === null ? null : Math.ceil((reset - at) / 1000),
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
// A passed reset is a reason to refresh, not proof of new allowance.
|
|
99
|
+
return windows.every((w) => Number.isFinite(w.used) && (!w.reset || w.reset.getTime() > at));
|
|
100
|
+
};
|
|
101
|
+
const assertCanDispatch = async () => {
|
|
102
|
+
if (input.allowExtraCredits) return;
|
|
103
|
+
const at = now();
|
|
104
|
+
// Headers can refuse dispatch immediately. A successful observation is
|
|
105
|
+
// never a reusable spending permit: the preceding response or another
|
|
106
|
+
// session may have consumed the remaining allowance.
|
|
107
|
+
if (snapshot) check(snapshot, at);
|
|
108
|
+
if (!token) throw new CodexIncludedUsageUnknownError();
|
|
109
|
+
const revisionAtRead = observationRevision;
|
|
110
|
+
let usage: CodexUsagePayload | null;
|
|
111
|
+
try {
|
|
112
|
+
usage = await readUsage(token);
|
|
113
|
+
} catch (error) {
|
|
114
|
+
if (error instanceof SubscriptionCoreCodexRequestOutcomeUnknownError) throw error;
|
|
115
|
+
if (error instanceof CodexUsageUnauthorizedError && input.refreshToken) {
|
|
116
|
+
// This runs during credential resolution, before the model's auth
|
|
117
|
+
// headers are constructed. The caller tracks the refreshed version.
|
|
118
|
+
token = await input.refreshToken();
|
|
119
|
+
usage = await readUsage(token).catch((usageError: unknown) => {
|
|
120
|
+
if (usageError instanceof SubscriptionCoreCodexRequestOutcomeUnknownError)
|
|
121
|
+
throw usageError;
|
|
122
|
+
return null;
|
|
123
|
+
});
|
|
124
|
+
} else {
|
|
125
|
+
usage = null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (!usage || usage.status === "error" || usage.status === "no-data")
|
|
129
|
+
throw new CodexIncludedUsageUnknownError();
|
|
130
|
+
const windows = [usage.fiveHour, usage.weekly].flatMap((window) =>
|
|
131
|
+
window
|
|
132
|
+
? [
|
|
133
|
+
{
|
|
134
|
+
used: window.percent,
|
|
135
|
+
reset: window.resetAt ? new Date(window.resetAt) : null,
|
|
136
|
+
},
|
|
137
|
+
]
|
|
138
|
+
: [],
|
|
139
|
+
);
|
|
140
|
+
if (!windows.length) {
|
|
141
|
+
if (usage.limitReached || usage.status === "limit_reached")
|
|
142
|
+
throw new CodexIncludedUsageExhaustedError(null);
|
|
143
|
+
throw new CodexIncludedUsageUnknownError();
|
|
144
|
+
}
|
|
145
|
+
const next: Observation = { windows, checkedAt: new Date(now()) };
|
|
146
|
+
// A response header received during this read is newer evidence than the
|
|
147
|
+
// request's snapshot. Never let a delayed usage response erase it.
|
|
148
|
+
if (observationRevision === revisionAtRead) snapshot = next;
|
|
149
|
+
// The legacy cache contract only accepts a complete duration-labeled pair.
|
|
150
|
+
if (usage.fiveHour && usage.weekly && observationRevision === revisionAtRead)
|
|
151
|
+
input.onUsage?.({
|
|
152
|
+
primaryUsedPercent: usage.fiveHour.percent,
|
|
153
|
+
primaryResetAt: usage.fiveHour.resetAt ? new Date(usage.fiveHour.resetAt) : null,
|
|
154
|
+
secondaryUsedPercent: usage.weekly.percent,
|
|
155
|
+
secondaryResetAt: usage.weekly.resetAt ? new Date(usage.weekly.resetAt) : null,
|
|
156
|
+
checkedAt: next.checkedAt,
|
|
157
|
+
});
|
|
158
|
+
if (snapshot) check(snapshot, now());
|
|
159
|
+
if (!check(next, now())) throw new CodexIncludedUsageUnknownError();
|
|
160
|
+
// Provider feature identifiers are not guaranteed to be model slugs.
|
|
161
|
+
// Until an authoritative mapping exists, an exhausted extra allowance
|
|
162
|
+
// cannot grant permission to continue spending on this account.
|
|
163
|
+
for (const limit of usage.additionalLimits ?? []) {
|
|
164
|
+
if (limit.unknownWindowExhausted) throw new CodexIncludedUsageExhaustedError(null);
|
|
165
|
+
const featureWindows = [limit.fiveHour, limit.weekly].flatMap((window) =>
|
|
166
|
+
window
|
|
167
|
+
? [
|
|
168
|
+
{
|
|
169
|
+
used: window.percent,
|
|
170
|
+
reset: window.resetAt ? new Date(window.resetAt) : null,
|
|
171
|
+
},
|
|
172
|
+
]
|
|
173
|
+
: [],
|
|
174
|
+
);
|
|
175
|
+
if (
|
|
176
|
+
featureWindows.length &&
|
|
177
|
+
!check({ windows: featureWindows, checkedAt: next.checkedAt }, now())
|
|
178
|
+
) {
|
|
179
|
+
throw new CodexIncludedUsageUnknownError();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (usage.limitReached || usage.status === "limit_reached")
|
|
183
|
+
throw new CodexIncludedUsageExhaustedError(null);
|
|
184
|
+
};
|
|
185
|
+
return {
|
|
186
|
+
setToken: (next: CodexTokenSnapshot) => {
|
|
187
|
+
token = next;
|
|
188
|
+
},
|
|
189
|
+
observe,
|
|
190
|
+
assertObservedUsageAllowsDispatch: async () => {
|
|
191
|
+
if (input.allowExtraCredits) return;
|
|
192
|
+
const refusal = spendingRefusal;
|
|
193
|
+
if (refusal instanceof CodexIncludedUsageExhaustedError) {
|
|
194
|
+
const allowed = await input.canSpendCredits?.();
|
|
195
|
+
// A concurrent read can learn that usage is unverifiable while this
|
|
196
|
+
// policy query is pending. Earlier consent cannot erase newer evidence.
|
|
197
|
+
if (spendingRefusal !== refusal) {
|
|
198
|
+
if (spendingRefusal) throw spendingRefusal;
|
|
199
|
+
if (snapshot) check(snapshot, now());
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
if (allowed) return;
|
|
203
|
+
}
|
|
204
|
+
if (spendingRefusal) throw spendingRefusal;
|
|
205
|
+
if (snapshot) check(snapshot, now());
|
|
206
|
+
},
|
|
207
|
+
assertCanDispatch: () => {
|
|
208
|
+
// Main and title requests share this guard. Serialize their reads so a
|
|
209
|
+
// late successful response cannot overwrite a newer exhaustion result.
|
|
210
|
+
const admission = admissionTail.then(async () => {
|
|
211
|
+
try {
|
|
212
|
+
await assertCanDispatch();
|
|
213
|
+
// Pending reads cannot erase an earlier feature-limit refusal. Only
|
|
214
|
+
// a complete, successful usage observation restores included access.
|
|
215
|
+
spendingRefusal = null;
|
|
216
|
+
} catch (error) {
|
|
217
|
+
if (
|
|
218
|
+
error instanceof CodexIncludedUsageExhaustedError ||
|
|
219
|
+
error instanceof CodexIncludedUsageUnknownError
|
|
220
|
+
)
|
|
221
|
+
spendingRefusal = error;
|
|
222
|
+
if (
|
|
223
|
+
error instanceof CodexIncludedUsageExhaustedError &&
|
|
224
|
+
(await input.canSpendCredits?.()) &&
|
|
225
|
+
spendingRefusal === error
|
|
226
|
+
) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
throw error;
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
admissionTail = admission.catch(() => undefined);
|
|
233
|
+
return admission;
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
class CodexUsageUnauthorizedError extends Error {}
|
|
239
|
+
|
|
240
|
+
/** No awaited policy work may run after the final physical lease fence. */
|
|
241
|
+
export async function assertCodexDispatchAdmission(
|
|
242
|
+
assertCredits: () => Promise<void>,
|
|
243
|
+
assertLease: () => Promise<void>,
|
|
244
|
+
): Promise<void> {
|
|
245
|
+
await assertCredits();
|
|
246
|
+
await assertLease();
|
|
247
|
+
}
|