@opengeni/worker-bundle 2.0.1 → 2.0.2-canary.38686408778001
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 +172 -28
- 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 +3 -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 +30 -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-NTXZHYET.js} +1552 -2876
- package/dist/activities-control-NTXZHYET.js.map +1 -0
- package/dist/activities-control.d.ts +64 -4
- package/dist/{activities-turn-YVVKCGAK.js → activities-turn-RKRI5GY4.js} +9238 -4392
- package/dist/activities-turn-RKRI5GY4.js.map +1 -0
- package/dist/activities.d.ts +64 -4
- package/dist/artifact-materializer-entry.js +104 -35
- 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-M3NVMF2V.js → chunk-7BIHFUGB.js} +2293 -3956
- package/dist/chunk-7BIHFUGB.js.map +1 -0
- package/dist/chunk-MVESOYFC.js +3746 -0
- package/dist/chunk-MVESOYFC.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 +1134 -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 +26 -4
- 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 +94 -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 +599 -110
- 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 +162 -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 +116 -9
- package/src/activities/sandbox-lease.ts +1615 -73
- package/src/activities/scheduled-tasks.ts +758 -141
- 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 +79 -38
- 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,587 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Claude on the shared subscription core (design 5.3, Claude track C1a): the
|
|
3
|
+
* provider-neutral chat placement and request custody bound to Claude, and
|
|
4
|
+
* the turn's route. The route is `core` only after the Claude drained cutover
|
|
5
|
+
* commits its receipt (C3); the legacy Claude path is deleted (C4), so any
|
|
6
|
+
* other route fails closed.
|
|
7
|
+
*/
|
|
8
|
+
import {
|
|
9
|
+
buildSubscriptionCoreClaudeTokenResolver,
|
|
10
|
+
CodexCredentialLeaseAttemptFencedError,
|
|
11
|
+
decodeSubscriptionCoreClaudeQuota,
|
|
12
|
+
readSubscriptionCoreProviderRoute,
|
|
13
|
+
SUBSCRIPTION_CORE_CLAUDE,
|
|
14
|
+
SUBSCRIPTION_CORE_CLAUDE_LEASE_TTL_MS,
|
|
15
|
+
SUBSCRIPTION_CORE_CLAUDE_MODEL_PREFIXES,
|
|
16
|
+
SubscriptionCoreError,
|
|
17
|
+
subscriptionCoreRequests,
|
|
18
|
+
subscriptionCoreTurns,
|
|
19
|
+
withRlsContext,
|
|
20
|
+
type Database,
|
|
21
|
+
type SubscriptionCoreClaudeTokenSnapshot,
|
|
22
|
+
type SubscriptionCoreProvider,
|
|
23
|
+
} from "@opengeni/db";
|
|
24
|
+
import { parseClaudeUsageHeaders, parseModelProvidersJson, type Settings } from "@opengeni/config";
|
|
25
|
+
import type { PreparedUsageRequest } from "@opengeni/runtime";
|
|
26
|
+
import type { CapacityPhaseDeps, CapacityPhaseOutcome } from "./codex-capacity";
|
|
27
|
+
import { subscriptionCoreCutoverDisabledFailure } from "./codex-core-errors";
|
|
28
|
+
import type { ClaudeTurnLease } from "./credential-leases";
|
|
29
|
+
import { classifyClaudeProviderError } from "./errors";
|
|
30
|
+
import {
|
|
31
|
+
assertCoreSourceConnected,
|
|
32
|
+
coreLeaseRef,
|
|
33
|
+
guardCoreTokenResolver,
|
|
34
|
+
parkCoreSubscriptionTurn,
|
|
35
|
+
selectCoreSubscriptionTurnCapacity,
|
|
36
|
+
type CoreTurnPlacementProvider,
|
|
37
|
+
} from "./subscription-core-capacity";
|
|
38
|
+
import {
|
|
39
|
+
SUBSCRIPTION_CORE_TURN_REFUSAL_LIMIT,
|
|
40
|
+
subscriptionCoreFailoverMetrics,
|
|
41
|
+
} from "./subscription-core-failover";
|
|
42
|
+
import {
|
|
43
|
+
coreRequestOutcomeUnknownFailure,
|
|
44
|
+
createCoreSubscriptionRequests,
|
|
45
|
+
settleCoreResponse,
|
|
46
|
+
type CoreResponseInspector,
|
|
47
|
+
type CoreSubscriptionRequests,
|
|
48
|
+
} from "./subscription-core-requests";
|
|
49
|
+
import {
|
|
50
|
+
finalizeCoreSubscriptionTurn,
|
|
51
|
+
type CoreRefusalBinding,
|
|
52
|
+
} from "./subscription-core-settlement";
|
|
53
|
+
import type { SubscriptionCoreTurn } from "./turn-context";
|
|
54
|
+
import {
|
|
55
|
+
TURN_FINALIZER_STEP_TIMEOUT_MS,
|
|
56
|
+
turnFinalizerCancellationSignal,
|
|
57
|
+
waitForTurnFinalizerStep,
|
|
58
|
+
} from "./quiescence";
|
|
59
|
+
import { subscriptionTurnLeaseHolderId } from "./subscription-lease";
|
|
60
|
+
import {
|
|
61
|
+
recoverCoreHealthAndWake,
|
|
62
|
+
wakeSubscriptionCoreWaitersAndDeliver,
|
|
63
|
+
type CoreCodexWakeServices,
|
|
64
|
+
} from "../subscription-core-codex-waits";
|
|
65
|
+
import { CLAUDE_CORE_WAIT_BINDING } from "../subscription-core-claude-waits";
|
|
66
|
+
|
|
67
|
+
const CLAUDE = SUBSCRIPTION_CORE_CLAUDE as SubscriptionCoreProvider;
|
|
68
|
+
const DISPLAY_NAME = "Claude";
|
|
69
|
+
|
|
70
|
+
const hasCoreCode = (error: unknown, code: string): boolean =>
|
|
71
|
+
error instanceof SubscriptionCoreError && error.code === code;
|
|
72
|
+
|
|
73
|
+
/** Claude's chat placement on the shared core. */
|
|
74
|
+
export const CLAUDE_CORE_TURN_PROVIDER: CoreTurnPlacementProvider = {
|
|
75
|
+
id: "claude",
|
|
76
|
+
displayName: DISPLAY_NAME,
|
|
77
|
+
startupPhaseProvider: "claude-subscription",
|
|
78
|
+
leaseTtlMs: SUBSCRIPTION_CORE_CLAUDE_LEASE_TTL_MS,
|
|
79
|
+
lease: (deps) => deps.leases.claude,
|
|
80
|
+
place: (db, request) => subscriptionCoreTurns(CLAUDE).placeSubscriptionCoreTurn(db, request),
|
|
81
|
+
// A recovery wakes the account's other Claude waiters too.
|
|
82
|
+
recoverHealth: async ({ db, signalCodexCapacityWorkflow }, identity) => {
|
|
83
|
+
await recoverCoreHealthAndWake(
|
|
84
|
+
{ db, signalCodexCapacityWorkflow },
|
|
85
|
+
CLAUDE_CORE_WAIT_BINDING,
|
|
86
|
+
identity,
|
|
87
|
+
);
|
|
88
|
+
},
|
|
89
|
+
adopt: ({ providerTurn, turnExecutionPolicy }, core, placement) => {
|
|
90
|
+
// A Claude rate limit cools the accepted model of the connection.
|
|
91
|
+
core.productModelId = turnExecutionPolicy.productModelId;
|
|
92
|
+
providerTurn.claudeSubscriptionCore = core;
|
|
93
|
+
providerTurn.effectiveClaudeCredentialId = placement.connectionId;
|
|
94
|
+
},
|
|
95
|
+
// A wait parks the same turn on the durable core waiter (EP-T09).
|
|
96
|
+
park: (deps, wait) =>
|
|
97
|
+
parkCoreSubscriptionTurn(deps, wait, CLAUDE_CORE_WAIT_BINDING, DISPLAY_NAME),
|
|
98
|
+
isSourceDisconnected: (db, identity, connectionId) =>
|
|
99
|
+
subscriptionCoreRequests(CLAUDE).isSubscriptionCoreSourceDisconnected(
|
|
100
|
+
db,
|
|
101
|
+
identity,
|
|
102
|
+
connectionId,
|
|
103
|
+
),
|
|
104
|
+
sourceDisconnectedError: () => CLAUDE.errors.sourceDisconnected(),
|
|
105
|
+
isAttemptFenced: (error) => error instanceof CodexCredentialLeaseAttemptFencedError,
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The Claude capacity phase by the Claude route: `core` places on the shared
|
|
110
|
+
* core; `maintenance` and `legacy` (no committed receipt) fail closed, since
|
|
111
|
+
* the legacy Claude selection is deleted.
|
|
112
|
+
*/
|
|
113
|
+
export async function selectClaudeTurnCapacityByRoute(
|
|
114
|
+
deps: CapacityPhaseDeps,
|
|
115
|
+
): Promise<CapacityPhaseOutcome> {
|
|
116
|
+
if (!deps.billingState.isClaudeTurn) return { ok: true };
|
|
117
|
+
const { input } = deps;
|
|
118
|
+
const route = await withRlsContext(
|
|
119
|
+
deps.db,
|
|
120
|
+
{ accountId: input.accountId, workspaceId: input.workspaceId },
|
|
121
|
+
(scoped) =>
|
|
122
|
+
readSubscriptionCoreProviderRoute(scoped, { accountId: input.accountId, provider: "claude" }),
|
|
123
|
+
);
|
|
124
|
+
if (route !== "core") throw subscriptionCoreCutoverDisabledFailure(DISPLAY_NAME);
|
|
125
|
+
const turnId = deps.attempt.turnId;
|
|
126
|
+
if (!turnId) throw new Error("Turn id was not initialized");
|
|
127
|
+
deps.leases.claude.holderId ??= subscriptionTurnLeaseHolderId(
|
|
128
|
+
"claude",
|
|
129
|
+
DISPLAY_NAME,
|
|
130
|
+
input,
|
|
131
|
+
turnId,
|
|
132
|
+
);
|
|
133
|
+
return await selectCoreSubscriptionTurnCapacity(deps, CLAUDE_CORE_TURN_PROVIDER);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** One attempt's Claude request custody on the core (unknown outcomes are never replayed). */
|
|
137
|
+
export function createCoreClaudeRequests(
|
|
138
|
+
db: Database,
|
|
139
|
+
core: SubscriptionCoreTurn,
|
|
140
|
+
ref: { connectionId: string; holderId: string; generation: number },
|
|
141
|
+
execution: { attemptId: string; executionGeneration: number },
|
|
142
|
+
lane: "main" | "title",
|
|
143
|
+
) {
|
|
144
|
+
const requests = subscriptionCoreRequests(CLAUDE);
|
|
145
|
+
return createCoreSubscriptionRequests({
|
|
146
|
+
reserve: (request) =>
|
|
147
|
+
requests.reserveSubscriptionCoreRequest(db, core.identity, ref, { ...request, ...execution }),
|
|
148
|
+
settle: (request) =>
|
|
149
|
+
requests.settleSubscriptionCoreRequest(db, core.identity, ref, { ...request, ...execution }),
|
|
150
|
+
displayName: DISPLAY_NAME,
|
|
151
|
+
outcomeUnknown: () => CLAUDE.errors.requestOutcomeUnknown(),
|
|
152
|
+
isOutcomeUnknown: (error) => hasCoreCode(error, "subscription_core_request_outcome_unknown"),
|
|
153
|
+
// A paused web search continues the same model call with a new request
|
|
154
|
+
// after a complete response (`pause_turn`); a title call has no server
|
|
155
|
+
// tools and never continues.
|
|
156
|
+
continuesAfterResponse: lane === "main",
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export type ClaudeCoreRequestAuthorization = {
|
|
161
|
+
/** The current bearer (the shared resolver refreshes a stale one first). */
|
|
162
|
+
getToken: () => Promise<SubscriptionCoreClaudeTokenSnapshot>;
|
|
163
|
+
/**
|
|
164
|
+
* After a 401 on a bearer of `generation`: the turn's one forced refresh
|
|
165
|
+
* under the connection's refresh lock, shared by every request of the
|
|
166
|
+
* attempt, and its bearer only when the generation advanced past the
|
|
167
|
+
* refused one (null: surface the refusal).
|
|
168
|
+
*/
|
|
169
|
+
renewAfterUnauthorized: (
|
|
170
|
+
generation: number,
|
|
171
|
+
) => Promise<SubscriptionCoreClaudeTokenSnapshot | null>;
|
|
172
|
+
/** The dispatch fence every physical request passes before its reservation. */
|
|
173
|
+
assertDispatch: () => Promise<void>;
|
|
174
|
+
/**
|
|
175
|
+
* A main-lane request with a bearer of `generation` was admitted: the
|
|
176
|
+
* refusal settlement fences its health write on that generation. Main-lane
|
|
177
|
+
* requests are sequential, so at settlement it is the refused request's;
|
|
178
|
+
* session-title requests (their own lane) never move it.
|
|
179
|
+
*/
|
|
180
|
+
mainLaneAdmitted: (generation: number) => void;
|
|
181
|
+
/**
|
|
182
|
+
* A response's usage headers, made with a bearer of `generation`, as a
|
|
183
|
+
* quota observation of the leased connection (`decodeQuota`), recorded in
|
|
184
|
+
* order and best effort; `core.quota` joins them.
|
|
185
|
+
*/
|
|
186
|
+
observeResponse: (response: Response, generation: number, upstreamModelId?: string) => void;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Materialization and refresh for a core-placed Claude turn: the bearer comes
|
|
191
|
+
* from the shared core resolver under the exact accepted turn and this
|
|
192
|
+
* attempt's live core lease, and both request lanes (the agent's and the
|
|
193
|
+
* session title's) are created on the turn. `upstreamModelIds` lists the
|
|
194
|
+
* deployment's Claude models, so a family limit in a response's usage
|
|
195
|
+
* headers cools each of them.
|
|
196
|
+
*/
|
|
197
|
+
export function buildClaudeCoreRequestAuthorization(input: {
|
|
198
|
+
db: Database;
|
|
199
|
+
settings: Settings;
|
|
200
|
+
core: SubscriptionCoreTurn;
|
|
201
|
+
lease: ClaudeTurnLease;
|
|
202
|
+
execution: { attemptId: string; executionGeneration: number };
|
|
203
|
+
upstreamModelIds: readonly string[];
|
|
204
|
+
}): ClaudeCoreRequestAuthorization {
|
|
205
|
+
const { db, core, lease } = input;
|
|
206
|
+
const ref = coreLeaseRef(lease, core, DISPLAY_NAME);
|
|
207
|
+
core.requests = createCoreClaudeRequests(db, core, ref, input.execution, "main");
|
|
208
|
+
core.titleRequests = createCoreClaudeRequests(db, core, ref, input.execution, "title");
|
|
209
|
+
const resolver = guardCoreTokenResolver(
|
|
210
|
+
db,
|
|
211
|
+
CLAUDE_CORE_TURN_PROVIDER,
|
|
212
|
+
core,
|
|
213
|
+
lease,
|
|
214
|
+
buildSubscriptionCoreClaudeTokenResolver(db, input.settings, core.identity, ref),
|
|
215
|
+
(error) => hasCoreCode(error, "subscription_core_lease_lost"),
|
|
216
|
+
);
|
|
217
|
+
let forced: Promise<SubscriptionCoreClaudeTokenSnapshot> | null = null;
|
|
218
|
+
let observed: Promise<boolean> = Promise.resolve(false);
|
|
219
|
+
core.quota = { settled: () => observed };
|
|
220
|
+
return {
|
|
221
|
+
getToken: () => resolver.getToken(),
|
|
222
|
+
// One forced refresh per attempt, shared by both lanes: a refresh that
|
|
223
|
+
// failed stays failed for the rest of the attempt (no second refresh).
|
|
224
|
+
renewAfterUnauthorized: async (generation) => {
|
|
225
|
+
forced ??= resolver.refresh();
|
|
226
|
+
const token = await forced;
|
|
227
|
+
return token.credentialVersion > generation ? token : null;
|
|
228
|
+
},
|
|
229
|
+
assertDispatch: async () => {
|
|
230
|
+
await assertCoreSourceConnected(db, CLAUDE_CORE_TURN_PROVIDER, core);
|
|
231
|
+
await lease.assertCurrentForDispatch();
|
|
232
|
+
},
|
|
233
|
+
mainLaneAdmitted: (generation) => {
|
|
234
|
+
core.credentialVersion = generation;
|
|
235
|
+
},
|
|
236
|
+
observeResponse: (response, generation, upstreamModelId) => {
|
|
237
|
+
// Every link is total: a failure records nothing and keeps what an
|
|
238
|
+
// earlier observation recovered, so the join never rejects.
|
|
239
|
+
const observedAt = new Date();
|
|
240
|
+
observed = observed.then(async (recovered) => {
|
|
241
|
+
try {
|
|
242
|
+
const observation = parseClaudeUsageHeaders(
|
|
243
|
+
response.headers,
|
|
244
|
+
observedAt,
|
|
245
|
+
upstreamModelId,
|
|
246
|
+
);
|
|
247
|
+
if (!observation) return recovered;
|
|
248
|
+
const quota = {
|
|
249
|
+
...decodeSubscriptionCoreClaudeQuota({
|
|
250
|
+
observation,
|
|
251
|
+
refreshGeneration: generation,
|
|
252
|
+
upstreamModelIds: input.upstreamModelIds,
|
|
253
|
+
}),
|
|
254
|
+
revision: 0,
|
|
255
|
+
};
|
|
256
|
+
const applied = await subscriptionCoreTurns(CLAUDE).applySubscriptionCoreQuotaObservation(
|
|
257
|
+
db,
|
|
258
|
+
core.identity,
|
|
259
|
+
ref,
|
|
260
|
+
quota,
|
|
261
|
+
);
|
|
262
|
+
return recovered || applied.recovered;
|
|
263
|
+
} catch {
|
|
264
|
+
return recovered;
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
},
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* The prefix of the bearer a core-placed Claude turn's capability settings
|
|
273
|
+
* carry instead of the live one: each core request's custody sets the real
|
|
274
|
+
* bearer, so a request that bypasses custody is refused upstream rather than
|
|
275
|
+
* spending the account unreserved.
|
|
276
|
+
*/
|
|
277
|
+
export const CLAUDE_CORE_SETTINGS_PLACEHOLDER_TOKEN = "sk-ant-oat01-opengeni-core-custody-only";
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* The placeholder bearer of one connection at one credential version. It is
|
|
281
|
+
* unique per connection and version because the settings' key also scopes
|
|
282
|
+
* stored hosted web searches when the account has no UUID (a setup token):
|
|
283
|
+
* one shared placeholder would replay one account's encrypted search results
|
|
284
|
+
* to another, which Anthropic rejects. A refresh moves the version, which,
|
|
285
|
+
* like a rotated live token on the legacy path, only costs a readable replay.
|
|
286
|
+
*/
|
|
287
|
+
export function claudeCoreSettingsPlaceholderToken(
|
|
288
|
+
connectionId: string,
|
|
289
|
+
credentialVersion: number,
|
|
290
|
+
): string {
|
|
291
|
+
return `${CLAUDE_CORE_SETTINGS_PLACEHOLDER_TOKEN}-${connectionId}-${credentialVersion}`;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* The Claude credential a core-placed turn's capability settings carry: the
|
|
296
|
+
* account identity with the connection's placeholder bearer, never the live
|
|
297
|
+
* token or any refresh material.
|
|
298
|
+
*/
|
|
299
|
+
export function claudeCoreSettingsCredential(
|
|
300
|
+
token: SubscriptionCoreClaudeTokenSnapshot,
|
|
301
|
+
connectionId: string,
|
|
302
|
+
): string {
|
|
303
|
+
return JSON.stringify({
|
|
304
|
+
version: token.credential.version,
|
|
305
|
+
token: claudeCoreSettingsPlaceholderToken(connectionId, token.credentialVersion),
|
|
306
|
+
identity: token.credential.identity,
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** The scope a Claude product model id names (its prefix). */
|
|
311
|
+
export function claudeCoreModelScope(productModelId: string): "organization" | "workspace" {
|
|
312
|
+
return productModelId.startsWith(`${SUBSCRIPTION_CORE_CLAUDE_MODEL_PREFIXES[1]}/`)
|
|
313
|
+
? "workspace"
|
|
314
|
+
: "organization";
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** The upstream model ids of the deployment's Claude subscription catalog. */
|
|
318
|
+
export function claudeSubscriptionUpstreamModelIds(
|
|
319
|
+
settings: Pick<Settings, "modelProvidersJson">,
|
|
320
|
+
): string[] {
|
|
321
|
+
return [
|
|
322
|
+
...new Set(
|
|
323
|
+
parseModelProvidersJson(settings.modelProvidersJson)
|
|
324
|
+
.filter(
|
|
325
|
+
(provider) =>
|
|
326
|
+
provider.kind === "claude-subscription-workspace" ||
|
|
327
|
+
provider.kind === "claude-subscription-organization",
|
|
328
|
+
)
|
|
329
|
+
.flatMap((provider) => provider.models.map((model) => model.upstreamModelId ?? model.id)),
|
|
330
|
+
),
|
|
331
|
+
];
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** The Claude subscription provider ids of the deployment's catalog. */
|
|
335
|
+
export function claudeSubscriptionProviderIds(
|
|
336
|
+
settings: Pick<Settings, "modelProvidersJson">,
|
|
337
|
+
): ReadonlySet<string> {
|
|
338
|
+
return new Set(
|
|
339
|
+
parseModelProvidersJson(settings.modelProvidersJson)
|
|
340
|
+
.filter(
|
|
341
|
+
(provider) =>
|
|
342
|
+
provider.kind === "claude-subscription-workspace" ||
|
|
343
|
+
provider.kind === "claude-subscription-organization",
|
|
344
|
+
)
|
|
345
|
+
.map((provider) => provider.id),
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
const SSE_EVENT = /^event:\s*(\S+)/;
|
|
350
|
+
const SSE_DATA = /^data:\s*(.*)$/;
|
|
351
|
+
/** Stream error types that are a refusal (the provider did no model work). */
|
|
352
|
+
const CLAUDE_REFUSAL_ERROR_TYPES = new Set(["overloaded_error", "rate_limit_error"]);
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* The outcome of a streamed Claude response, read from its event names as
|
|
356
|
+
* the body passes: a `message_delta` (the model's final stop reason) or
|
|
357
|
+
* `message_stop` means the model answered, even when the reader stops right
|
|
358
|
+
* after it; an `error` event before any content block whose type is an
|
|
359
|
+
* overload or rate limit is a refusal (reported with HTTP 200); any other
|
|
360
|
+
* stream error (a timeout or conflict, as their HTTP statuses) and anything
|
|
361
|
+
* else that ends or stops early is unknown. A body that is not a stream is
|
|
362
|
+
* a response once it ends.
|
|
363
|
+
*/
|
|
364
|
+
export function claudeResponseInspector(response: Response): CoreResponseInspector {
|
|
365
|
+
const streamed = (response.headers.get("content-type") ?? "").includes("text/event-stream");
|
|
366
|
+
const decoder = new TextDecoder();
|
|
367
|
+
let pending = "";
|
|
368
|
+
let content = false;
|
|
369
|
+
let answered = false;
|
|
370
|
+
let inError = false;
|
|
371
|
+
let refused = false;
|
|
372
|
+
return {
|
|
373
|
+
chunk(bytes) {
|
|
374
|
+
if (!streamed || answered) return;
|
|
375
|
+
pending += decoder.decode(bytes, { stream: true });
|
|
376
|
+
const lines = pending.split("\n");
|
|
377
|
+
pending = lines.pop() ?? "";
|
|
378
|
+
for (const line of lines) {
|
|
379
|
+
const trimmed = line.trim();
|
|
380
|
+
const data = inError ? SSE_DATA.exec(trimmed)?.[1] : undefined;
|
|
381
|
+
if (data !== undefined) {
|
|
382
|
+
inError = false;
|
|
383
|
+
refused ||= CLAUDE_REFUSAL_ERROR_TYPES.has(claudeStreamErrorType(data) ?? "");
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
const event = SSE_EVENT.exec(trimmed)?.[1];
|
|
387
|
+
if (!event) continue;
|
|
388
|
+
inError = event === "error";
|
|
389
|
+
if (event === "message_delta" || event === "message_stop") answered = true;
|
|
390
|
+
else if (event.startsWith("content_block_")) content = true;
|
|
391
|
+
}
|
|
392
|
+
},
|
|
393
|
+
outcome(ended) {
|
|
394
|
+
if (!streamed) return ended ? "response_received" : "unknown";
|
|
395
|
+
if (answered) return "response_received";
|
|
396
|
+
return refused && !content ? "refused" : "unknown";
|
|
397
|
+
},
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function claudeStreamErrorType(data: string): string | null {
|
|
402
|
+
try {
|
|
403
|
+
const value = JSON.parse(data) as { error?: { type?: unknown } } | null;
|
|
404
|
+
return typeof value?.error?.type === "string" ? value.error.type : null;
|
|
405
|
+
} catch {
|
|
406
|
+
return null;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* The Anthropic transport's per-request preparation for a core-placed Claude
|
|
412
|
+
* turn (through `withClaudeUsageObserver`): each physical request carries the
|
|
413
|
+
* core bearer, is fenced and reserved on its lane as the last step before the
|
|
414
|
+
* fetch, and settles from its response. A 401 renews the bearer through the
|
|
415
|
+
* turn's one forced refresh and retries the same request on the same
|
|
416
|
+
* connection as a new reservation, only when the generation advanced.
|
|
417
|
+
* `observe` receives every response with the generation of the bearer that
|
|
418
|
+
* made it (its usage headers become quota observations of the connection).
|
|
419
|
+
*/
|
|
420
|
+
export function claudeCoreRequestPreparer(input: {
|
|
421
|
+
authorization: ClaudeCoreRequestAuthorization;
|
|
422
|
+
lane: CoreSubscriptionRequests;
|
|
423
|
+
/** The agent's lane (not the session title's): admissions move the refusal fence. */
|
|
424
|
+
mainLane: boolean;
|
|
425
|
+
nextRequestId: () => string;
|
|
426
|
+
providerIds: ReadonlySet<string>;
|
|
427
|
+
observe?: (response: Response, generation: number, upstreamModelId?: string) => void;
|
|
428
|
+
}) {
|
|
429
|
+
return async (providerId: string, headers: Headers): Promise<Headers | PreparedUsageRequest> => {
|
|
430
|
+
if (!input.providerIds.has(providerId)) return headers;
|
|
431
|
+
const requestId = input.nextRequestId();
|
|
432
|
+
const prepare = (
|
|
433
|
+
token: SubscriptionCoreClaudeTokenSnapshot,
|
|
434
|
+
transportAttempt: number,
|
|
435
|
+
): PreparedUsageRequest => {
|
|
436
|
+
const request = { requestId, transportAttempt };
|
|
437
|
+
const bearer = new Headers(headers);
|
|
438
|
+
bearer.set("authorization", `Bearer ${token.credential.token}`);
|
|
439
|
+
bearer.delete("x-api-key");
|
|
440
|
+
return {
|
|
441
|
+
headers: bearer,
|
|
442
|
+
observe: (_provider, response, upstreamModelId) =>
|
|
443
|
+
input.observe?.(response, token.credentialVersion, upstreamModelId),
|
|
444
|
+
custody: {
|
|
445
|
+
admit: async () => {
|
|
446
|
+
await input.authorization.assertDispatch();
|
|
447
|
+
await input.lane.reserve(request);
|
|
448
|
+
if (input.mainLane) input.authorization.mainLaneAdmitted(token.credentialVersion);
|
|
449
|
+
},
|
|
450
|
+
responded: (response) =>
|
|
451
|
+
settleCoreResponse(input.lane, request, response, claudeResponseInspector(response)),
|
|
452
|
+
failed: () => input.lane.observe({ ...request, outcome: "unknown" }),
|
|
453
|
+
...(transportAttempt === 1
|
|
454
|
+
? {
|
|
455
|
+
renew: async () => {
|
|
456
|
+
const renewed = await input.authorization.renewAfterUnauthorized(
|
|
457
|
+
token.credentialVersion,
|
|
458
|
+
);
|
|
459
|
+
return renewed ? prepare(renewed, transportAttempt + 1) : null;
|
|
460
|
+
},
|
|
461
|
+
}
|
|
462
|
+
: {}),
|
|
463
|
+
},
|
|
464
|
+
};
|
|
465
|
+
};
|
|
466
|
+
return prepare(await input.authorization.getToken(), 1);
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* The terminal unknown-outcome failure of a core Claude turn, or null: an
|
|
472
|
+
* earlier or own request whose outcome is unknown is never replayed
|
|
473
|
+
* automatically.
|
|
474
|
+
*/
|
|
475
|
+
export function claudeCoreRequestOutcomeUnknown(
|
|
476
|
+
error: unknown,
|
|
477
|
+
core: Pick<SubscriptionCoreTurn, "requests">,
|
|
478
|
+
): (Error & { code: string }) | null {
|
|
479
|
+
return coreRequestOutcomeUnknownFailure(error, core, () => CLAUDE.errors.requestOutcomeUnknown());
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Claude's in-turn failover and refusal settlement on the core (C1b) for a
|
|
484
|
+
* turn of `productModelId`: a 401 after the turn's one forced refresh is a
|
|
485
|
+
* sign-in quarantine, a 429 cools that model of the connection, and both
|
|
486
|
+
* re-place the same turn within the shared bound; 529 (overload) and 403
|
|
487
|
+
* (fatal) record nothing and never move the turn to another account.
|
|
488
|
+
* A 429 without the accepted model is not settled (null): it never widens
|
|
489
|
+
* into a whole-connection exhaustion.
|
|
490
|
+
*
|
|
491
|
+
* The transport already spent the turn's one forced refresh before a 401
|
|
492
|
+
* reaches settlement (`refreshedBeforeRefusal`), so the plan never asks for
|
|
493
|
+
* a second one: a refused bearer is a sign-in quarantine.
|
|
494
|
+
*/
|
|
495
|
+
export function claudeCoreRefusal(productModelId: string | undefined): CoreRefusalBinding {
|
|
496
|
+
return {
|
|
497
|
+
provider: CLAUDE,
|
|
498
|
+
displayName: DISPLAY_NAME,
|
|
499
|
+
credential: { renewable: true, refreshedBeforeRefusal: true },
|
|
500
|
+
classify: (error) => {
|
|
501
|
+
const outcome = classifyClaudeProviderError(error, productModelId);
|
|
502
|
+
return outcome?.kind === "rate_limited" && productModelId === undefined ? null : outcome;
|
|
503
|
+
},
|
|
504
|
+
joinObservations: (deps, core) =>
|
|
505
|
+
joinCoreClaudeObservations(
|
|
506
|
+
{ db: deps.db, signalCodexCapacityWorkflow: deps.signalCodexCapacityWorkflow },
|
|
507
|
+
core,
|
|
508
|
+
{
|
|
509
|
+
signal: turnFinalizerCancellationSignal(
|
|
510
|
+
deps.cancellationSignal,
|
|
511
|
+
deps.control.activityStatus,
|
|
512
|
+
),
|
|
513
|
+
},
|
|
514
|
+
),
|
|
515
|
+
lease: (deps) => deps.leases.claude,
|
|
516
|
+
isSourceDisconnected: CLAUDE_CORE_TURN_PROVIDER.isSourceDisconnected,
|
|
517
|
+
failover: {
|
|
518
|
+
displayName: DISPLAY_NAME,
|
|
519
|
+
lease: (deps) => deps.leases.claude,
|
|
520
|
+
countRefusals: (db, identity) =>
|
|
521
|
+
subscriptionCoreTurns(CLAUDE).countSubscriptionCoreTurnRefusals(db, identity),
|
|
522
|
+
refusalLimit: SUBSCRIPTION_CORE_TURN_REFUSAL_LIMIT,
|
|
523
|
+
recoveryReason: "claude_credential_failover",
|
|
524
|
+
recoveryProvider: "claude-subscription",
|
|
525
|
+
metrics: subscriptionCoreFailoverMetrics("claude"),
|
|
526
|
+
},
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* Joins a core Claude turn's usage-header observations so far, under its
|
|
532
|
+
* still-held lease: one that ended a stored exhaustion is a capacity change
|
|
533
|
+
* for the account's Claude waiters, delivered once per joined chain (the
|
|
534
|
+
* refusal settlement and finalization may both join it). Never throws. The
|
|
535
|
+
* queued writes are awaited in full (nothing may be released while one is
|
|
536
|
+
* queued); the wake delivery is bounded like a finalizer step, and the
|
|
537
|
+
* capacity outbox remains its backstop. A cancellation signal detaches only
|
|
538
|
+
* the wake, never the queued writes.
|
|
539
|
+
*/
|
|
540
|
+
export async function joinCoreClaudeObservations(
|
|
541
|
+
services: CoreCodexWakeServices,
|
|
542
|
+
core: SubscriptionCoreTurn,
|
|
543
|
+
options: { signal?: AbortSignal | undefined; wakeTimeoutMs?: number } = {},
|
|
544
|
+
): Promise<void> {
|
|
545
|
+
const chain = core.quota?.settled();
|
|
546
|
+
if (!chain || wokenObservationChains.get(core) === chain) return;
|
|
547
|
+
const recovered = await chain.catch(() => false);
|
|
548
|
+
if (!recovered || wokenObservationChains.get(core) === chain) return;
|
|
549
|
+
wokenObservationChains.set(core, chain);
|
|
550
|
+
await waitForTurnFinalizerStep(
|
|
551
|
+
wakeSubscriptionCoreWaitersAndDeliver(services, CLAUDE_CORE_WAIT_BINDING, {
|
|
552
|
+
accountId: core.identity.accountId,
|
|
553
|
+
reason: "quota_observed_available",
|
|
554
|
+
}).catch(() => undefined),
|
|
555
|
+
options.signal,
|
|
556
|
+
options.wakeTimeoutMs ?? TURN_FINALIZER_STEP_TIMEOUT_MS,
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
const wokenObservationChains = new WeakMap<SubscriptionCoreTurn, Promise<boolean>>();
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* Finalization of a core Claude turn before its lease is released: the
|
|
564
|
+
* turn's usage-header observations are joined first
|
|
565
|
+
* (`joinCoreClaudeObservations`), then the shared finalization step runs.
|
|
566
|
+
* Claude declares an exact cache lifetime, so that step leaves the binding's
|
|
567
|
+
* cache clock alone: the clock moves when each request is reserved, never at
|
|
568
|
+
* finalization. Best effort.
|
|
569
|
+
*/
|
|
570
|
+
export async function finalizeCoreClaudeTurn(
|
|
571
|
+
input: CoreCodexWakeServices & {
|
|
572
|
+
core: SubscriptionCoreTurn;
|
|
573
|
+
lease: ClaudeTurnLease;
|
|
574
|
+
signal?: AbortSignal | undefined;
|
|
575
|
+
},
|
|
576
|
+
): Promise<void> {
|
|
577
|
+
await joinCoreClaudeObservations(input, input.core, { signal: input.signal });
|
|
578
|
+
await waitForTurnFinalizerStep(
|
|
579
|
+
finalizeCoreSubscriptionTurn({
|
|
580
|
+
db: input.db,
|
|
581
|
+
provider: CLAUDE,
|
|
582
|
+
core: input.core,
|
|
583
|
+
lease: input.lease,
|
|
584
|
+
}),
|
|
585
|
+
input.signal,
|
|
586
|
+
);
|
|
587
|
+
}
|