@code-yeongyu/senpi 2026.8.26 → 2026.8.28-2
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/CHANGELOG.md +138 -0
- package/README.md +3 -3
- package/dist/cli/args.d.ts +5 -1
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +18 -1
- package/dist/cli/args.js.map +1 -1
- package/dist/core/agent-session-services.d.ts.map +1 -1
- package/dist/core/agent-session-services.js +2 -0
- package/dist/core/agent-session-services.js.map +1 -1
- package/dist/core/agent-session.d.ts +5 -0
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +11 -1
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/auth-storage.d.ts +1 -0
- package/dist/core/auth-storage.d.ts.map +1 -1
- package/dist/core/auth-storage.js +3 -0
- package/dist/core/auth-storage.js.map +1 -1
- package/dist/core/bash-executor.d.ts.map +1 -1
- package/dist/core/bash-executor.js +67 -29
- package/dist/core/bash-executor.js.map +1 -1
- package/dist/core/credential-accounts.d.ts +31 -0
- package/dist/core/credential-accounts.d.ts.map +1 -0
- package/dist/core/credential-accounts.js +135 -0
- package/dist/core/credential-accounts.js.map +1 -0
- package/dist/core/credential-pool/classify.d.ts +47 -0
- package/dist/core/credential-pool/classify.d.ts.map +1 -0
- package/dist/core/credential-pool/classify.js +92 -0
- package/dist/core/credential-pool/classify.js.map +1 -0
- package/dist/core/credential-pool/env-slots.d.ts +22 -0
- package/dist/core/credential-pool/env-slots.d.ts.map +1 -0
- package/dist/core/credential-pool/env-slots.js +37 -0
- package/dist/core/credential-pool/env-slots.js.map +1 -0
- package/dist/core/credential-pool/failover.d.ts +62 -0
- package/dist/core/credential-pool/failover.d.ts.map +1 -0
- package/dist/core/credential-pool/failover.js +124 -0
- package/dist/core/credential-pool/failover.js.map +1 -0
- package/dist/core/credential-pool/rotation-stream.d.ts +54 -0
- package/dist/core/credential-pool/rotation-stream.d.ts.map +1 -0
- package/dist/core/credential-pool/rotation-stream.js +183 -0
- package/dist/core/credential-pool/rotation-stream.js.map +1 -0
- package/dist/core/credential-pool/state-store.d.ts +89 -0
- package/dist/core/credential-pool/state-store.d.ts.map +1 -0
- package/dist/core/credential-pool/state-store.js +166 -0
- package/dist/core/credential-pool/state-store.js.map +1 -0
- package/dist/core/extensions/builtin/account/index.d.ts +9 -0
- package/dist/core/extensions/builtin/account/index.d.ts.map +1 -0
- package/dist/core/extensions/builtin/account/index.js +69 -0
- package/dist/core/extensions/builtin/account/index.js.map +1 -0
- package/dist/core/extensions/builtin/compaction/index.d.ts.map +1 -1
- package/dist/core/extensions/builtin/compaction/index.js +67 -0
- package/dist/core/extensions/builtin/compaction/index.js.map +1 -1
- package/dist/core/extensions/builtin/compaction/log.d.ts +1 -1
- package/dist/core/extensions/builtin/compaction/log.d.ts.map +1 -1
- package/dist/core/extensions/builtin/compaction/log.js +1 -0
- package/dist/core/extensions/builtin/compaction/log.js.map +1 -1
- package/dist/core/extensions/builtin/config-reload/index.js +14 -22
- package/dist/core/extensions/builtin/config-reload/index.js.map +1 -1
- package/dist/core/extensions/builtin/goal/continuation-recovery.d.ts +1 -0
- package/dist/core/extensions/builtin/goal/continuation-recovery.d.ts.map +1 -1
- package/dist/core/extensions/builtin/goal/continuation-recovery.js +2 -0
- package/dist/core/extensions/builtin/goal/continuation-recovery.js.map +1 -1
- package/dist/core/extensions/builtin/goal/continuation.d.ts +8 -1
- package/dist/core/extensions/builtin/goal/continuation.d.ts.map +1 -1
- package/dist/core/extensions/builtin/goal/continuation.js +15 -0
- package/dist/core/extensions/builtin/goal/continuation.js.map +1 -1
- package/dist/core/extensions/builtin/goal/direct-input-lifecycle.js +2 -2
- package/dist/core/extensions/builtin/goal/direct-input-lifecycle.js.map +1 -1
- package/dist/core/extensions/builtin/goal/format.d.ts +6 -1
- package/dist/core/extensions/builtin/goal/format.d.ts.map +1 -1
- package/dist/core/extensions/builtin/goal/format.js +6 -0
- package/dist/core/extensions/builtin/goal/format.js.map +1 -1
- package/dist/core/extensions/builtin/goal/index.d.ts.map +1 -1
- package/dist/core/extensions/builtin/goal/index.js +7 -0
- package/dist/core/extensions/builtin/goal/index.js.map +1 -1
- package/dist/core/extensions/builtin/goal/lifecycle-helpers.d.ts.map +1 -1
- package/dist/core/extensions/builtin/goal/lifecycle-helpers.js +5 -2
- package/dist/core/extensions/builtin/goal/lifecycle-helpers.js.map +1 -1
- package/dist/core/extensions/builtin/goal/monitor-continuation.d.ts.map +1 -1
- package/dist/core/extensions/builtin/goal/monitor-continuation.js +31 -7
- package/dist/core/extensions/builtin/goal/monitor-continuation.js.map +1 -1
- package/dist/core/extensions/builtin/goal/persistence.js +2 -0
- package/dist/core/extensions/builtin/goal/persistence.js.map +1 -1
- package/dist/core/extensions/builtin/goal/renderers.d.ts +14 -0
- package/dist/core/extensions/builtin/goal/renderers.d.ts.map +1 -0
- package/dist/core/extensions/builtin/goal/renderers.js +153 -0
- package/dist/core/extensions/builtin/goal/renderers.js.map +1 -0
- package/dist/core/extensions/builtin/goal/store.d.ts +6 -2
- package/dist/core/extensions/builtin/goal/store.d.ts.map +1 -1
- package/dist/core/extensions/builtin/goal/store.js +8 -2
- package/dist/core/extensions/builtin/goal/store.js.map +1 -1
- package/dist/core/extensions/builtin/goal/tool-registration.d.ts.map +1 -1
- package/dist/core/extensions/builtin/goal/tool-registration.js +16 -6
- package/dist/core/extensions/builtin/goal/tool-registration.js.map +1 -1
- package/dist/core/extensions/builtin/goal/types.d.ts +1 -0
- package/dist/core/extensions/builtin/goal/types.d.ts.map +1 -1
- package/dist/core/extensions/builtin/goal/types.js.map +1 -1
- package/dist/core/extensions/builtin/imagegen/index.d.ts.map +1 -1
- package/dist/core/extensions/builtin/imagegen/index.js +10 -3
- package/dist/core/extensions/builtin/imagegen/index.js.map +1 -1
- package/dist/core/extensions/builtin/index.d.ts.map +1 -1
- package/dist/core/extensions/builtin/index.js +4 -0
- package/dist/core/extensions/builtin/index.js.map +1 -1
- package/dist/core/extensions/builtin/service-tier.d.ts.map +1 -1
- package/dist/core/extensions/builtin/service-tier.js +13 -0
- package/dist/core/extensions/builtin/service-tier.js.map +1 -1
- package/dist/core/extensions/builtin/terminal/extension.d.ts.map +1 -1
- package/dist/core/extensions/builtin/terminal/extension.js +4 -2
- package/dist/core/extensions/builtin/terminal/extension.js.map +1 -1
- package/dist/core/extensions/builtin/todotools/index.js +1 -1
- package/dist/core/extensions/builtin/todotools/index.js.map +1 -1
- package/dist/core/extensions/builtin/todotools/native-todo-mirror.d.ts +1 -1
- package/dist/core/extensions/builtin/todotools/native-todo-mirror.d.ts.map +1 -1
- package/dist/core/extensions/builtin/todotools/native-todo-mirror.js +1 -1
- package/dist/core/extensions/builtin/todotools/native-todo-mirror.js.map +1 -1
- package/dist/core/hidden-stdout-log.d.ts +10 -0
- package/dist/core/hidden-stdout-log.d.ts.map +1 -1
- package/dist/core/hidden-stdout-log.js +36 -5
- package/dist/core/hidden-stdout-log.js.map +1 -1
- package/dist/core/model-config-schema.d.ts +85 -12
- package/dist/core/model-config-schema.d.ts.map +1 -1
- package/dist/core/model-config-schema.js +25 -0
- package/dist/core/model-config-schema.js.map +1 -1
- package/dist/core/model-runtime.d.ts +36 -1
- package/dist/core/model-runtime.d.ts.map +1 -1
- package/dist/core/model-runtime.js +142 -12
- package/dist/core/model-runtime.js.map +1 -1
- package/dist/core/retry-fallback/chains.d.ts +23 -1
- package/dist/core/retry-fallback/chains.d.ts.map +1 -1
- package/dist/core/retry-fallback/chains.js +59 -3
- package/dist/core/retry-fallback/chains.js.map +1 -1
- package/dist/core/retry-fallback/controller.d.ts.map +1 -1
- package/dist/core/retry-fallback/controller.js +9 -2
- package/dist/core/retry-fallback/controller.js.map +1 -1
- package/dist/core/retry-fallback/settings.d.ts.map +1 -1
- package/dist/core/retry-fallback/settings.js +9 -0
- package/dist/core/retry-fallback/settings.js.map +1 -1
- package/dist/core/retry-fallback/validate.js +1 -1
- package/dist/core/retry-fallback/validate.js.map +1 -1
- package/dist/core/sdk.js +1 -1
- package/dist/core/sdk.js.map +1 -1
- package/dist/core/session-manager.d.ts +9 -0
- package/dist/core/session-manager.d.ts.map +1 -1
- package/dist/core/session-manager.js +31 -0
- package/dist/core/session-manager.js.map +1 -1
- package/dist/core/tools/output-accumulator.d.ts +2 -0
- package/dist/core/tools/output-accumulator.d.ts.map +1 -1
- package/dist/core/tools/output-accumulator.js +32 -7
- package/dist/core/tools/output-accumulator.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/main.d.ts +7 -0
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +48 -4
- package/dist/main.js.map +1 -1
- package/dist/modes/app-server/server/account.d.ts.map +1 -1
- package/dist/modes/app-server/server/account.js +5 -5
- package/dist/modes/app-server/server/account.js.map +1 -1
- package/dist/modes/index.d.ts +1 -0
- package/dist/modes/index.d.ts.map +1 -1
- package/dist/modes/index.js +1 -0
- package/dist/modes/index.js.map +1 -1
- package/dist/modes/interactive/components/footer.d.ts +11 -3
- package/dist/modes/interactive/components/footer.d.ts.map +1 -1
- package/dist/modes/interactive/components/footer.js +31 -1
- package/dist/modes/interactive/components/footer.js.map +1 -1
- package/dist/modes/interactive/components/tool-execution-fallback.d.ts.map +1 -1
- package/dist/modes/interactive/components/tool-execution-fallback.js +82 -1
- package/dist/modes/interactive/components/tool-execution-fallback.js.map +1 -1
- package/dist/modes/interactive/grok/chrome.d.ts +4 -4
- package/dist/modes/interactive/grok/chrome.d.ts.map +1 -1
- package/dist/modes/interactive/grok/chrome.js.map +1 -1
- package/dist/modes/interactive/grok/footer.d.ts +3 -3
- package/dist/modes/interactive/grok/footer.d.ts.map +1 -1
- package/dist/modes/interactive/grok/footer.js.map +1 -1
- package/dist/modes/interactive/interactive-host-runtime.d.ts +44 -0
- package/dist/modes/interactive/interactive-host-runtime.d.ts.map +1 -0
- package/dist/modes/interactive/interactive-host-runtime.js +529 -0
- package/dist/modes/interactive/interactive-host-runtime.js.map +1 -0
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +340 -257
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/rpc/connection-handler.d.ts.map +1 -1
- package/dist/modes/rpc/connection-handler.js +151 -30
- package/dist/modes/rpc/connection-handler.js.map +1 -1
- package/dist/modes/rpc/host-ensure.d.ts +41 -0
- package/dist/modes/rpc/host-ensure.d.ts.map +1 -0
- package/dist/modes/rpc/host-ensure.js +245 -0
- package/dist/modes/rpc/host-ensure.js.map +1 -0
- package/dist/modes/rpc/host-lifecycle.d.ts +57 -0
- package/dist/modes/rpc/host-lifecycle.d.ts.map +1 -0
- package/dist/modes/rpc/host-lifecycle.js +491 -0
- package/dist/modes/rpc/host-lifecycle.js.map +1 -0
- package/dist/modes/rpc/host-watchdog.d.ts +33 -0
- package/dist/modes/rpc/host-watchdog.d.ts.map +1 -0
- package/dist/modes/rpc/host-watchdog.js +142 -0
- package/dist/modes/rpc/host-watchdog.js.map +1 -0
- package/dist/modes/rpc/multi-session-host.d.ts +7 -1
- package/dist/modes/rpc/multi-session-host.d.ts.map +1 -1
- package/dist/modes/rpc/multi-session-host.js +216 -32
- package/dist/modes/rpc/multi-session-host.js.map +1 -1
- package/dist/modes/rpc/rpc-client.d.ts +115 -8
- package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-client.js +200 -32
- package/dist/modes/rpc/rpc-client.js.map +1 -1
- package/dist/modes/rpc/rpc-mode.d.ts +4 -2
- package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-mode.js +4 -2
- package/dist/modes/rpc/rpc-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-types.d.ts +166 -3
- package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-types.js.map +1 -1
- package/dist/modes/rpc/session-command-router.d.ts +22 -0
- package/dist/modes/rpc/session-command-router.d.ts.map +1 -1
- package/dist/modes/rpc/session-command-router.js +165 -16
- package/dist/modes/rpc/session-command-router.js.map +1 -1
- package/dist/modes/rpc/session-event-writer.d.ts +15 -2
- package/dist/modes/rpc/session-event-writer.d.ts.map +1 -1
- package/dist/modes/rpc/session-event-writer.js +69 -25
- package/dist/modes/rpc/session-event-writer.js.map +1 -1
- package/dist/modes/rpc/session-registry.d.ts +16 -0
- package/dist/modes/rpc/session-registry.d.ts.map +1 -1
- package/dist/modes/rpc/session-registry.js +40 -3
- package/dist/modes/rpc/session-registry.js.map +1 -1
- package/docs/providers.md +48 -0
- package/docs/rpc.md +96 -5
- package/node_modules/@code-yeongyu/senpi-codemode/CHANGELOG.md +56 -0
- package/node_modules/@code-yeongyu/senpi-codemode/package.json +4 -4
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/context-manager.ts +13 -1
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/inline-worker.ts +15 -1
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/worker-indirect-eval.js +711 -1
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/py/transport.ts +14 -1
- package/node_modules/@code-yeongyu/senpi-codemode/src/prompt/eval-prompt.ts +10 -10
- package/node_modules/@code-yeongyu/senpi-codemode/src/tool/detached-eval-result.ts +9 -3
- package/node_modules/@code-yeongyu/senpi-codemode/src/tool/eval-tool.ts +6 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/tools/edit.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/tools/edit.js +20 -4
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/tools/edit.js.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/tools/index.d.ts +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/tools/index.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/tools/index.js.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/tools/post-mutate.d.ts +21 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/tools/post-mutate.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/tools/post-mutate.js +27 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/tools/post-mutate.js.map +1 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/tools/tool-context.d.ts +24 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/tools/tool-context.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/tools/tool-context.js.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/tools/write.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/tools/write.js +7 -2
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/tools/write.js.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/package.json +3 -3
- package/node_modules/@earendil-works/pi-ai/dist/api/anthropic-tool-pairs.d.ts +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/anthropic-tool-pairs.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/anthropic-tool-pairs.js +46 -14
- package/node_modules/@earendil-works/pi-ai/dist/api/anthropic-tool-pairs.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/cursor-agent.d.ts +11 -0
- package/node_modules/@earendil-works/pi-ai/dist/api/cursor-agent.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/cursor-agent.js +30 -16
- package/node_modules/@earendil-works/pi-ai/dist/api/cursor-agent.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/auth/credential-store.d.ts +2 -1
- package/node_modules/@earendil-works/pi-ai/dist/auth/credential-store.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/auth/credential-store.js +2 -1
- package/node_modules/@earendil-works/pi-ai/dist/auth/credential-store.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/auth/pool/classify.d.ts +16 -0
- package/node_modules/@earendil-works/pi-ai/dist/auth/pool/classify.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/auth/pool/classify.js +75 -0
- package/node_modules/@earendil-works/pi-ai/dist/auth/pool/classify.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/auth/pool/failover.d.ts +45 -0
- package/node_modules/@earendil-works/pi-ai/dist/auth/pool/failover.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/auth/pool/failover.js +81 -0
- package/node_modules/@earendil-works/pi-ai/dist/auth/pool/failover.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/auth/pool/select.d.ts +31 -0
- package/node_modules/@earendil-works/pi-ai/dist/auth/pool/select.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/auth/pool/select.js +65 -0
- package/node_modules/@earendil-works/pi-ai/dist/auth/pool/select.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/auth/pool/slots.d.ts +12 -0
- package/node_modules/@earendil-works/pi-ai/dist/auth/pool/slots.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/auth/pool/slots.js +37 -0
- package/node_modules/@earendil-works/pi-ai/dist/auth/pool/slots.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/auth/resolve.d.ts +6 -0
- package/node_modules/@earendil-works/pi-ai/dist/auth/resolve.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/auth/resolve.js +32 -6
- package/node_modules/@earendil-works/pi-ai/dist/auth/resolve.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/auth/types.d.ts +3 -2
- package/node_modules/@earendil-works/pi-ai/dist/auth/types.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/auth/types.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/env-api-keys.d.ts +6 -0
- package/node_modules/@earendil-works/pi-ai/dist/env-api-keys.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/env-api-keys.js +6 -1
- package/node_modules/@earendil-works/pi-ai/dist/env-api-keys.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/image-models.generated.d.ts +75 -0
- package/node_modules/@earendil-works/pi-ai/dist/image-models.generated.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/image-models.generated.js +75 -0
- package/node_modules/@earendil-works/pi-ai/dist/image-models.generated.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/.manifest.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/alibaba-token-plan.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/amazon-bedrock.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/baseten.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/cloudflare-ai-gateway.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/cloudflare-workers-ai.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/fireworks.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/github-copilot.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/huggingface.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/minimax-cn.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/minimax.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/mistral.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/nvidia.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/openai-codex.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/openai.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/opencode-go.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/opencode.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/opengateway.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/openrouter.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/qwen-token-plan-cn.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/qwen-token-plan.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/together.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/zai-coding-cn.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/zai.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/utils/overflow.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/utils/overflow.js +4 -0
- package/node_modules/@earendil-works/pi-ai/dist/utils/overflow.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/package.json +5 -5
- package/node_modules/@earendil-works/pi-pty/package.json +1 -1
- package/node_modules/@earendil-works/pi-telemetry/package.json +1 -1
- package/node_modules/@earendil-works/pi-tui/dist/terminal.d.ts +5 -0
- package/node_modules/@earendil-works/pi-tui/dist/terminal.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-tui/dist/terminal.js +90 -1
- package/node_modules/@earendil-works/pi-tui/dist/terminal.js.map +1 -1
- package/node_modules/@earendil-works/pi-tui/dist/tui.js +1 -1
- package/node_modules/@earendil-works/pi-tui/dist/tui.js.map +1 -1
- package/node_modules/@earendil-works/pi-tui/package.json +1 -1
- package/package.json +7 -7
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { dirname, join } from "node:path";
|
|
2
|
+
import { listSlots, pinSlot, removeSlot } from "@earendil-works/pi-ai/auth/pool/slots";
|
|
3
|
+
import { discoverEnvSlots } from "./credential-pool/env-slots.js";
|
|
4
|
+
import { CredentialSlotRepository, slotHealth } from "./credential-pool/state-store.js";
|
|
5
|
+
import { emitProviderAccountsChanged } from "./extensions/builtin/claude-sdk-oauth/account-events.js";
|
|
6
|
+
const ACCOUNT_NAME_PATTERN = /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$/;
|
|
7
|
+
export function assertValidAccountName(name) {
|
|
8
|
+
if (!ACCOUNT_NAME_PATTERN.test(name)) {
|
|
9
|
+
throw new Error(`Invalid account name '${name}': use letters, digits, '-' or '_', starting with a letter or digit`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function pooledFrom(credential) {
|
|
13
|
+
return credential === undefined ? undefined : credential;
|
|
14
|
+
}
|
|
15
|
+
function pinnedName(credential) {
|
|
16
|
+
return credential?.pinned;
|
|
17
|
+
}
|
|
18
|
+
function numberField(value, key) {
|
|
19
|
+
const found = Object.entries(value).find(([candidate]) => candidate === key)?.[1];
|
|
20
|
+
return typeof found === "number" ? found : undefined;
|
|
21
|
+
}
|
|
22
|
+
function defaultRepository(storage) {
|
|
23
|
+
const authPath = storage.getStoragePath();
|
|
24
|
+
return new CredentialSlotRepository(authPath ? join(dirname(authPath), "credential-pool-state.json") : undefined);
|
|
25
|
+
}
|
|
26
|
+
function stringField(value, key) {
|
|
27
|
+
const found = Object.entries(value).find(([candidate]) => candidate === key)?.[1];
|
|
28
|
+
return typeof found === "string" ? found : undefined;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Block state has two sources and both are authoritative: a slot persisted by an
|
|
32
|
+
* existing provider lane carries its own `blockedUntil`/`blockReason` inside
|
|
33
|
+
* auth.json, while generic pool failover records health in the sidecar. Reading
|
|
34
|
+
* only one would silently downgrade a real block to "available".
|
|
35
|
+
*/
|
|
36
|
+
function slotBlocked(slot, sidecar, now) {
|
|
37
|
+
if (slotHealth(sidecar, now) === "blocked")
|
|
38
|
+
return true;
|
|
39
|
+
const reason = stringField(slot, "blockReason");
|
|
40
|
+
if (reason === "auth_error" || reason === "account_disabled")
|
|
41
|
+
return true;
|
|
42
|
+
const blockedUntil = numberField(slot, "blockedUntil");
|
|
43
|
+
return blockedUntil !== undefined && blockedUntil > now;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Lists a provider's credential accounts for ANY provider, not just one lane.
|
|
47
|
+
* Stored slots own the listing when a credential exists; env slots are listed
|
|
48
|
+
* only when nothing is stored, mirroring resolution precedence exactly. Health
|
|
49
|
+
* comes from the pool sidecar, so a cooldown persisted by a failover shows up
|
|
50
|
+
* here without auth.json ever carrying block state.
|
|
51
|
+
*/
|
|
52
|
+
export async function getCredentialAccounts(storage, provider, env = process.env, repository) {
|
|
53
|
+
return summarizeCredentialAccounts(provider, storage.get(provider), env, repository ?? defaultRepository(storage));
|
|
54
|
+
}
|
|
55
|
+
/** Storage-free variant for callers that already hold the credential (e.g. auth check). */
|
|
56
|
+
export async function summarizeCredentialAccounts(provider, stored, env = process.env, repository = new CredentialSlotRepository()) {
|
|
57
|
+
const credential = pooledFrom(stored);
|
|
58
|
+
const now = Date.now();
|
|
59
|
+
const pinned = pinnedName(credential);
|
|
60
|
+
if (credential) {
|
|
61
|
+
const state = await repository.listSlots(provider, "stored");
|
|
62
|
+
return listSlots(credential).map((slot) => ({
|
|
63
|
+
name: slot.name,
|
|
64
|
+
source: slot.source ?? "login",
|
|
65
|
+
blocked: slotBlocked(slot, state[slot.name], now),
|
|
66
|
+
pinned: pinned === slot.name,
|
|
67
|
+
}));
|
|
68
|
+
}
|
|
69
|
+
const state = await repository.listSlots(provider, "env");
|
|
70
|
+
const summaries = [];
|
|
71
|
+
for (const slot of discoverEnvSlots(provider, (name) => env[name])) {
|
|
72
|
+
const persisted = state[slot.name];
|
|
73
|
+
const revision = await repository.envCredentialRevision(slot.envVarName, slot.key);
|
|
74
|
+
// A block belongs to the value that earned it; a rotated env key starts clean.
|
|
75
|
+
const applicable = persisted?.credentialRevision === revision ? persisted : undefined;
|
|
76
|
+
summaries.push({
|
|
77
|
+
name: slot.name,
|
|
78
|
+
source: "env",
|
|
79
|
+
blocked: slotHealth(applicable, now) === "blocked",
|
|
80
|
+
pinned: pinned === slot.name,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
return summaries;
|
|
84
|
+
}
|
|
85
|
+
/** Pins one slot, or clears the pin when `name` is null. */
|
|
86
|
+
export async function pinCredentialAccount(storage, provider, name, env = process.env, repository) {
|
|
87
|
+
const repo = repository ?? defaultRepository(storage);
|
|
88
|
+
if (name !== null) {
|
|
89
|
+
assertValidAccountName(name);
|
|
90
|
+
const accounts = await getCredentialAccounts(storage, provider, env, repo);
|
|
91
|
+
if (!accounts.some((account) => account.name === name)) {
|
|
92
|
+
throw new Error(`Provider account not found: ${name}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
await storage.modify(provider, async (current) => {
|
|
96
|
+
if (current === undefined)
|
|
97
|
+
throw new Error(`No stored credential for provider: ${provider}`);
|
|
98
|
+
if (name === null) {
|
|
99
|
+
if (pinnedName(current) === undefined)
|
|
100
|
+
return current;
|
|
101
|
+
const { pinned: _pinned, ...unpinned } = { ...current, pinned: undefined };
|
|
102
|
+
return unpinned;
|
|
103
|
+
}
|
|
104
|
+
return pinSlot(current, name);
|
|
105
|
+
});
|
|
106
|
+
// Subscribed clients re-read the list after any mutation; skipping this leaves
|
|
107
|
+
// a desktop account picker showing a stale pin.
|
|
108
|
+
emitProviderAccountsChanged(provider);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Removes one stored slot. Env-backed accounts are refused: they are owned by
|
|
112
|
+
* the environment, and deleting one here would silently disagree with the
|
|
113
|
+
* variable that still defines it.
|
|
114
|
+
*/
|
|
115
|
+
export async function removeCredentialAccount(storage, provider, name, env = process.env, repository) {
|
|
116
|
+
const repo = repository ?? defaultRepository(storage);
|
|
117
|
+
const accounts = await getCredentialAccounts(storage, provider, env, repo);
|
|
118
|
+
const account = accounts.find((candidate) => candidate.name === name);
|
|
119
|
+
if (!account)
|
|
120
|
+
throw new Error(`Provider account not found: ${name}`);
|
|
121
|
+
if (account.source === "env") {
|
|
122
|
+
throw new Error(`Environment provider account cannot be removed: ${name}`);
|
|
123
|
+
}
|
|
124
|
+
const next = await storage.read(provider);
|
|
125
|
+
if (next === undefined)
|
|
126
|
+
throw new Error(`No stored credential for provider: ${provider}`);
|
|
127
|
+
const remaining = removeSlot(next, name);
|
|
128
|
+
if (remaining === undefined)
|
|
129
|
+
await storage.delete(provider);
|
|
130
|
+
else
|
|
131
|
+
await storage.modify(provider, async () => remaining);
|
|
132
|
+
await repo.mutateSlotState(provider, "stored", name, () => undefined);
|
|
133
|
+
emitProviderAccountsChanged(provider);
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=credential-accounts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credential-accounts.js","sourceRoot":"","sources":["../../src/core/credential-accounts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAE1C,OAAO,EAAE,SAAS,EAAyB,OAAO,EAAE,UAAU,EAAE,MAAM,uCAAuC,CAAC;AAE9G,OAAO,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AAClE,OAAO,EAAE,wBAAwB,EAA4B,UAAU,EAAE,MAAM,kCAAkC,CAAC;AAClH,OAAO,EAAE,2BAA2B,EAAE,MAAM,yDAAyD,CAAC;AAYtG,MAAM,oBAAoB,GAAG,kCAAkC,CAAC;AAEhE,MAAM,UAAU,sBAAsB,CAAC,IAAY;IAClD,IAAI,CAAC,oBAAoB,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QACtC,MAAM,IAAI,KAAK,CACd,yBAAyB,IAAI,qEAAqE,CAClG,CAAC;IACH,CAAC;AACF,CAAC;AAED,SAAS,UAAU,CAAC,UAAkC;IACrD,OAAO,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,UAAU,CAAC;AAC1D,CAAC;AAED,SAAS,UAAU,CAAC,UAAwC;IAC3D,OAAO,UAAU,EAAE,MAAM,CAAC;AAC3B,CAAC;AAED,SAAS,WAAW,CAAC,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,IAAI,OAAO,CAAC,MAAM,KAAK,KAAK,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CAAC,mDAAmD,IAAI,EAAE,CAAC,CAAC;IAC5E,CAAC;IACD,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC1C,IAAI,IAAI,KAAK,SAAS;QAAE,MAAM,IAAI,KAAK,CAAC,sCAAsC,QAAQ,EAAE,CAAC,CAAC;IAC1F,MAAM,SAAS,GAAG,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IACzC,IAAI,SAAS,KAAK,SAAS;QAAE,MAAM,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;;QACvD,MAAM,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,KAAK,IAAI,EAAE,CAAC,SAAS,CAAC,CAAC;IAC3D,MAAM,IAAI,CAAC,eAAe,CAAC,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;IACtE,2BAA2B,CAAC,QAAQ,CAAC,CAAC;AACvC,CAAC","sourcesContent":["import { dirname, join } from \"node:path\";\nimport type { Credential } from \"@earendil-works/pi-ai\";\nimport { listSlots, type PooledCredential, pinSlot, removeSlot } from \"@earendil-works/pi-ai/auth/pool/slots\";\nimport type { AuthStorage } from \"./auth-storage.ts\";\nimport { discoverEnvSlots } from \"./credential-pool/env-slots.ts\";\nimport { CredentialSlotRepository, type CredentialSlotState, slotHealth } from \"./credential-pool/state-store.ts\";\nimport { emitProviderAccountsChanged } from \"./extensions/builtin/claude-sdk-oauth/account-events.ts\";\n\nexport type CredentialAccountSource = \"login\" | \"import\" | \"env\";\n\n/** Account metadata safe to surface: names and health only, never key material. */\nexport type CredentialAccountSummary = {\n\treadonly name: string;\n\treadonly source: CredentialAccountSource;\n\treadonly blocked: boolean;\n\treadonly pinned: boolean;\n};\n\nconst ACCOUNT_NAME_PATTERN = /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$/;\n\nexport function assertValidAccountName(name: string): void {\n\tif (!ACCOUNT_NAME_PATTERN.test(name)) {\n\t\tthrow new Error(\n\t\t\t`Invalid account name '${name}': use letters, digits, '-' or '_', starting with a letter or digit`,\n\t\t);\n\t}\n}\n\nfunction pooledFrom(credential: Credential | undefined): PooledCredential | undefined {\n\treturn credential === undefined ? undefined : credential;\n}\n\nfunction pinnedName(credential: PooledCredential | undefined): string | undefined {\n\treturn credential?.pinned;\n}\n\nfunction numberField(value: object, key: string): number | undefined {\n\tconst found = Object.entries(value).find(([candidate]) => candidate === key)?.[1];\n\treturn typeof found === \"number\" ? found : undefined;\n}\n\nfunction defaultRepository(storage: AuthStorage): CredentialSlotRepository {\n\tconst authPath = storage.getStoragePath();\n\treturn new CredentialSlotRepository(authPath ? join(dirname(authPath), \"credential-pool-state.json\") : undefined);\n}\n\nfunction stringField(value: object, key: string): string | undefined {\n\tconst found = Object.entries(value).find(([candidate]) => candidate === key)?.[1];\n\treturn typeof found === \"string\" ? found : undefined;\n}\n\n/**\n * Block state has two sources and both are authoritative: a slot persisted by an\n * existing provider lane carries its own `blockedUntil`/`blockReason` inside\n * auth.json, while generic pool failover records health in the sidecar. Reading\n * only one would silently downgrade a real block to \"available\".\n */\nfunction slotBlocked(slot: object, sidecar: CredentialSlotState | undefined, now: number): boolean {\n\tif (slotHealth(sidecar, now) === \"blocked\") return true;\n\tconst reason = stringField(slot, \"blockReason\");\n\tif (reason === \"auth_error\" || reason === \"account_disabled\") return true;\n\tconst blockedUntil = numberField(slot, \"blockedUntil\");\n\treturn blockedUntil !== undefined && blockedUntil > now;\n}\n\n/**\n * Lists a provider's credential accounts for ANY provider, not just one lane.\n * Stored slots own the listing when a credential exists; env slots are listed\n * only when nothing is stored, mirroring resolution precedence exactly. Health\n * comes from the pool sidecar, so a cooldown persisted by a failover shows up\n * here without auth.json ever carrying block state.\n */\nexport async function getCredentialAccounts(\n\tstorage: AuthStorage,\n\tprovider: string,\n\tenv: NodeJS.ProcessEnv = process.env,\n\trepository?: CredentialSlotRepository,\n): Promise<CredentialAccountSummary[]> {\n\treturn summarizeCredentialAccounts(provider, storage.get(provider), env, repository ?? defaultRepository(storage));\n}\n\n/** Storage-free variant for callers that already hold the credential (e.g. auth check). */\nexport async function summarizeCredentialAccounts(\n\tprovider: string,\n\tstored: Credential | undefined,\n\tenv: NodeJS.ProcessEnv = process.env,\n\trepository: CredentialSlotRepository = new CredentialSlotRepository(),\n): Promise<CredentialAccountSummary[]> {\n\tconst credential = pooledFrom(stored);\n\tconst now = Date.now();\n\tconst pinned = pinnedName(credential);\n\n\tif (credential) {\n\t\tconst state = await repository.listSlots(provider, \"stored\");\n\t\treturn listSlots(credential).map((slot) => ({\n\t\t\tname: slot.name,\n\t\t\tsource: slot.source ?? \"login\",\n\t\t\tblocked: slotBlocked(slot, state[slot.name], now),\n\t\t\tpinned: pinned === slot.name,\n\t\t}));\n\t}\n\n\tconst state = await repository.listSlots(provider, \"env\");\n\tconst summaries: CredentialAccountSummary[] = [];\n\tfor (const slot of discoverEnvSlots(provider, (name) => env[name])) {\n\t\tconst persisted = state[slot.name];\n\t\tconst revision = await repository.envCredentialRevision(slot.envVarName, slot.key);\n\t\t// A block belongs to the value that earned it; a rotated env key starts clean.\n\t\tconst applicable = persisted?.credentialRevision === revision ? persisted : undefined;\n\t\tsummaries.push({\n\t\t\tname: slot.name,\n\t\t\tsource: \"env\",\n\t\t\tblocked: slotHealth(applicable, now) === \"blocked\",\n\t\t\tpinned: pinned === slot.name,\n\t\t});\n\t}\n\treturn summaries;\n}\n\n/** Pins one slot, or clears the pin when `name` is null. */\nexport async function pinCredentialAccount(\n\tstorage: AuthStorage,\n\tprovider: string,\n\tname: string | null,\n\tenv: NodeJS.ProcessEnv = process.env,\n\trepository?: CredentialSlotRepository,\n): Promise<void> {\n\tconst repo = repository ?? defaultRepository(storage);\n\tif (name !== null) {\n\t\tassertValidAccountName(name);\n\t\tconst accounts = await getCredentialAccounts(storage, provider, env, repo);\n\t\tif (!accounts.some((account) => account.name === name)) {\n\t\t\tthrow new Error(`Provider account not found: ${name}`);\n\t\t}\n\t}\n\tawait storage.modify(provider, async (current) => {\n\t\tif (current === undefined) throw new Error(`No stored credential for provider: ${provider}`);\n\t\tif (name === null) {\n\t\t\tif (pinnedName(current) === undefined) return current;\n\t\t\tconst { pinned: _pinned, ...unpinned } = { ...current, pinned: undefined };\n\t\t\treturn unpinned;\n\t\t}\n\t\treturn pinSlot(current, name);\n\t});\n\t// Subscribed clients re-read the list after any mutation; skipping this leaves\n\t// a desktop account picker showing a stale pin.\n\temitProviderAccountsChanged(provider);\n}\n\n/**\n * Removes one stored slot. Env-backed accounts are refused: they are owned by\n * the environment, and deleting one here would silently disagree with the\n * variable that still defines it.\n */\nexport async function removeCredentialAccount(\n\tstorage: AuthStorage,\n\tprovider: string,\n\tname: string,\n\tenv: NodeJS.ProcessEnv = process.env,\n\trepository?: CredentialSlotRepository,\n): Promise<void> {\n\tconst repo = repository ?? defaultRepository(storage);\n\tconst accounts = await getCredentialAccounts(storage, provider, env, repo);\n\tconst account = accounts.find((candidate) => candidate.name === name);\n\tif (!account) throw new Error(`Provider account not found: ${name}`);\n\tif (account.source === \"env\") {\n\t\tthrow new Error(`Environment provider account cannot be removed: ${name}`);\n\t}\n\tconst next = await storage.read(provider);\n\tif (next === undefined) throw new Error(`No stored credential for provider: ${provider}`);\n\tconst remaining = removeSlot(next, name);\n\tif (remaining === undefined) await storage.delete(provider);\n\telse await storage.modify(provider, async () => remaining);\n\tawait repo.mutateSlotState(provider, \"stored\", name, () => undefined);\n\temitProviderAccountsChanged(provider);\n}\n"]}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export declare const COOLDOWN_BASE_MS = 60000;
|
|
2
|
+
export declare const COOLDOWN_CAP_MS: number;
|
|
3
|
+
export declare const RETRY_SAME_MAX_ATTEMPTS = 2;
|
|
4
|
+
export type CredentialBlock = {
|
|
5
|
+
reason: "auth_error";
|
|
6
|
+
} | {
|
|
7
|
+
reason: "account_disabled";
|
|
8
|
+
} | {
|
|
9
|
+
reason: "rate_limit";
|
|
10
|
+
cooldownMs: number;
|
|
11
|
+
retryAfterWasCapped: boolean;
|
|
12
|
+
};
|
|
13
|
+
export type CredentialAction = {
|
|
14
|
+
kind: "failover";
|
|
15
|
+
block: CredentialBlock;
|
|
16
|
+
} | {
|
|
17
|
+
kind: "retry_same";
|
|
18
|
+
maxAttempts: typeof RETRY_SAME_MAX_ATTEMPTS;
|
|
19
|
+
} | {
|
|
20
|
+
kind: "fail_request";
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Per-slot exponential cooldown with the server hint as a FLOOR, never an
|
|
24
|
+
* override: a hint can only lengthen the wait the failure count already earned,
|
|
25
|
+
* and everything caps at 48 hours with the capping recorded.
|
|
26
|
+
*/
|
|
27
|
+
export declare function rateLimitCooldown(failureCount: number, serverHintMs?: number, policy?: {
|
|
28
|
+
baseMs?: number;
|
|
29
|
+
capMs?: number;
|
|
30
|
+
}): {
|
|
31
|
+
cooldownMs: number;
|
|
32
|
+
retryAfterWasCapped: boolean;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Maps a provider failure onto the credential-pool action space. Credential-
|
|
36
|
+
* scoped failures fail over (permanently for auth/billing, cooldown for rate
|
|
37
|
+
* limits); provider-scoped faults (5xx/overload/network) retry the SAME slot
|
|
38
|
+
* without blocking it, because blocking a healthy credential for a provider
|
|
39
|
+
* outage only destroys prompt-cache locality; everything else fails the
|
|
40
|
+
* request so the model fallback chain above keeps owning it.
|
|
41
|
+
*/
|
|
42
|
+
export declare function classifyCredentialFailure(error: unknown, context?: {
|
|
43
|
+
failureCount?: number;
|
|
44
|
+
cooldownBaseMs?: number;
|
|
45
|
+
cooldownCapMs?: number;
|
|
46
|
+
}): CredentialAction;
|
|
47
|
+
//# sourceMappingURL=classify.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"classify.d.ts","sourceRoot":"","sources":["../../../src/core/credential-pool/classify.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,gBAAgB,QAAwB,CAAC;AACtD,eAAO,MAAM,eAAe,QAAoB,CAAC;AACjD,eAAO,MAAM,uBAAuB,IAAI,CAAC;AAEzC,MAAM,MAAM,eAAe,GACxB;IAAE,MAAM,EAAE,YAAY,CAAA;CAAE,GACxB;IAAE,MAAM,EAAE,kBAAkB,CAAA;CAAE,GAC9B;IAAE,MAAM,EAAE,YAAY,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,mBAAmB,EAAE,OAAO,CAAA;CAAE,CAAC;AAE9E,MAAM,MAAM,gBAAgB,GACzB;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,KAAK,EAAE,eAAe,CAAA;CAAE,GAC5C;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,WAAW,EAAE,OAAO,uBAAuB,CAAA;CAAE,GACnE;IAAE,IAAI,EAAE,cAAc,CAAA;CAAE,CAAC;AAE5B;;;;GAIG;AACH,wBAAgB,iBAAiB,CAChC,YAAY,EAAE,MAAM,EACpB,YAAY,CAAC,EAAE,MAAM,EACrB,MAAM,GAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAO,GAC9C;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,mBAAmB,EAAE,OAAO,CAAA;CAAE,CAStD;AAuBD;;;;;;;GAOG;AACH,wBAAgB,yBAAyB,CACxC,KAAK,EAAE,OAAO,EACd,OAAO,GAAE;IAAE,YAAY,CAAC,EAAE,MAAM,CAAC;IAAC,cAAc,CAAC,EAAE,MAAM,CAAC;IAAC,aAAa,CAAC,EAAE,MAAM,CAAA;CAAO,GACtF,gBAAgB,CA4ClB"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { DEFAULT_SLOT_BLOCK_MS, MAX_SLOT_BLOCK_MS } from "@earendil-works/pi-ai/auth/pool/failover";
|
|
2
|
+
import { normalizeProviderError } from "@earendil-works/pi-ai/utils/error-body";
|
|
3
|
+
import { getOverflowPatterns } from "@earendil-works/pi-ai/utils/overflow";
|
|
4
|
+
import { extract429RetryAfterMs } from "@earendil-works/pi-ai/utils/retry-hint";
|
|
5
|
+
export const COOLDOWN_BASE_MS = DEFAULT_SLOT_BLOCK_MS;
|
|
6
|
+
export const COOLDOWN_CAP_MS = MAX_SLOT_BLOCK_MS;
|
|
7
|
+
export const RETRY_SAME_MAX_ATTEMPTS = 2;
|
|
8
|
+
/**
|
|
9
|
+
* Per-slot exponential cooldown with the server hint as a FLOOR, never an
|
|
10
|
+
* override: a hint can only lengthen the wait the failure count already earned,
|
|
11
|
+
* and everything caps at 48 hours with the capping recorded.
|
|
12
|
+
*/
|
|
13
|
+
export function rateLimitCooldown(failureCount, serverHintMs, policy = {}) {
|
|
14
|
+
const cap = policy.capMs ?? COOLDOWN_CAP_MS;
|
|
15
|
+
const base = policy.baseMs ?? COOLDOWN_BASE_MS;
|
|
16
|
+
const backoff = Math.min(cap, base * 2 ** Math.max(0, failureCount));
|
|
17
|
+
const floored = serverHintMs === undefined ? backoff : Math.max(backoff, serverHintMs);
|
|
18
|
+
return {
|
|
19
|
+
cooldownMs: Math.min(cap, floored),
|
|
20
|
+
retryAfterWasCapped: floored > cap,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
const INVALID_KEY_TEXT = /invalid[ _-]?(?:api[ _-]?)?key|authentication[_ ]?error|invalid x-api-key|unauthorized/i;
|
|
24
|
+
const ACCOUNT_SCOPED_403_TEXT = /account|credential|token|api[ _-]?key|organization|subscription/i;
|
|
25
|
+
const RATE_LIMIT_TEXT = /rate[ _-]?limit|too many requests|resource_exhausted/i;
|
|
26
|
+
const BILLING_TEXT = /billing|credits?[ _-]?(?:required|exhausted|balance)|insufficient[ _-]?(?:funds|quota|credit)|payment[ _-]?required|quota[ _-]?exhausted/i;
|
|
27
|
+
const OVERLOAD_TEXT = /overloaded/i;
|
|
28
|
+
const NETWORK_TEXT = /econnreset|econnrefused|etimedout|enotfound|socket hang up|fetch failed|network error|request timed out/i;
|
|
29
|
+
const FAIL_FAST_TEXT = /context[ _-]?(?:length|window)|maximum context|invalid[ _-]?model|model[ _-]?not[ _-]?found|malformed[ _-]?stream|premature[ _-]?(?:close|stream)/i;
|
|
30
|
+
const ABORT_TEXT = /\baborted?\b/i;
|
|
31
|
+
function isAbort(error, message) {
|
|
32
|
+
if (error instanceof Error && error.name === "AbortError")
|
|
33
|
+
return true;
|
|
34
|
+
return ABORT_TEXT.test(message);
|
|
35
|
+
}
|
|
36
|
+
function isOverflowText(text) {
|
|
37
|
+
return getOverflowPatterns().some((pattern) => pattern.test(text));
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Maps a provider failure onto the credential-pool action space. Credential-
|
|
41
|
+
* scoped failures fail over (permanently for auth/billing, cooldown for rate
|
|
42
|
+
* limits); provider-scoped faults (5xx/overload/network) retry the SAME slot
|
|
43
|
+
* without blocking it, because blocking a healthy credential for a provider
|
|
44
|
+
* outage only destroys prompt-cache locality; everything else fails the
|
|
45
|
+
* request so the model fallback chain above keeps owning it.
|
|
46
|
+
*/
|
|
47
|
+
export function classifyCredentialFailure(error, context = {}) {
|
|
48
|
+
const normalized = normalizeProviderError(error);
|
|
49
|
+
const text = normalized.messageCarriesBody ? normalized.message : `${normalized.message} ${normalized.body ?? ""}`;
|
|
50
|
+
const status = normalized.status;
|
|
51
|
+
const failureCount = context.failureCount ?? 0;
|
|
52
|
+
if (isAbort(error, text))
|
|
53
|
+
return { kind: "fail_request" };
|
|
54
|
+
if (status === 401 || INVALID_KEY_TEXT.test(text)) {
|
|
55
|
+
return { kind: "failover", block: { reason: "auth_error" } };
|
|
56
|
+
}
|
|
57
|
+
if (status === 403) {
|
|
58
|
+
return ACCOUNT_SCOPED_403_TEXT.test(text)
|
|
59
|
+
? { kind: "failover", block: { reason: "auth_error" } }
|
|
60
|
+
: { kind: "fail_request" };
|
|
61
|
+
}
|
|
62
|
+
if (status === 402 || BILLING_TEXT.test(text)) {
|
|
63
|
+
return { kind: "failover", block: { reason: "account_disabled" } };
|
|
64
|
+
}
|
|
65
|
+
if (status === 429 || RATE_LIMIT_TEXT.test(text)) {
|
|
66
|
+
const hint = extract429RetryAfterMs({
|
|
67
|
+
status: status ?? 429,
|
|
68
|
+
bodyText: text,
|
|
69
|
+
});
|
|
70
|
+
return {
|
|
71
|
+
kind: "failover",
|
|
72
|
+
block: {
|
|
73
|
+
reason: "rate_limit",
|
|
74
|
+
...rateLimitCooldown(failureCount, hint, {
|
|
75
|
+
baseMs: context.cooldownBaseMs,
|
|
76
|
+
capMs: context.cooldownCapMs,
|
|
77
|
+
}),
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
if (isOverflowText(text) || status === 400 || status === 404 || FAIL_FAST_TEXT.test(text)) {
|
|
82
|
+
return { kind: "fail_request" };
|
|
83
|
+
}
|
|
84
|
+
if (status === 529 || OVERLOAD_TEXT.test(text) || (status !== undefined && status >= 500 && status < 600)) {
|
|
85
|
+
return { kind: "retry_same", maxAttempts: RETRY_SAME_MAX_ATTEMPTS };
|
|
86
|
+
}
|
|
87
|
+
if (status === 408 || NETWORK_TEXT.test(text)) {
|
|
88
|
+
return { kind: "retry_same", maxAttempts: RETRY_SAME_MAX_ATTEMPTS };
|
|
89
|
+
}
|
|
90
|
+
return { kind: "fail_request" };
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=classify.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"classify.js","sourceRoot":"","sources":["../../../src/core/credential-pool/classify.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,MAAM,0CAA0C,CAAC;AACpG,OAAO,EAAE,sBAAsB,EAAE,MAAM,wCAAwC,CAAC;AAChF,OAAO,EAAE,mBAAmB,EAAE,MAAM,sCA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,YAAY,EAAE,WAAW,EAAE,uBAAuB,EAAE,CAAC;IACrE,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,CAAC;AACjC,CAAC","sourcesContent":["import { DEFAULT_SLOT_BLOCK_MS, MAX_SLOT_BLOCK_MS } from \"@earendil-works/pi-ai/auth/pool/failover\";\nimport { normalizeProviderError } from \"@earendil-works/pi-ai/utils/error-body\";\nimport { getOverflowPatterns } from \"@earendil-works/pi-ai/utils/overflow\";\nimport { extract429RetryAfterMs } from \"@earendil-works/pi-ai/utils/retry-hint\";\n\nexport const COOLDOWN_BASE_MS = DEFAULT_SLOT_BLOCK_MS;\nexport const COOLDOWN_CAP_MS = MAX_SLOT_BLOCK_MS;\nexport const RETRY_SAME_MAX_ATTEMPTS = 2;\n\nexport type CredentialBlock =\n\t| { reason: \"auth_error\" }\n\t| { reason: \"account_disabled\" }\n\t| { reason: \"rate_limit\"; cooldownMs: number; retryAfterWasCapped: boolean };\n\nexport type CredentialAction =\n\t| { kind: \"failover\"; block: CredentialBlock }\n\t| { kind: \"retry_same\"; maxAttempts: typeof RETRY_SAME_MAX_ATTEMPTS }\n\t| { kind: \"fail_request\" };\n\n/**\n * Per-slot exponential cooldown with the server hint as a FLOOR, never an\n * override: a hint can only lengthen the wait the failure count already earned,\n * and everything caps at 48 hours with the capping recorded.\n */\nexport function rateLimitCooldown(\n\tfailureCount: number,\n\tserverHintMs?: number,\n\tpolicy: { baseMs?: number; capMs?: number } = {},\n): { cooldownMs: number; retryAfterWasCapped: boolean } {\n\tconst cap = policy.capMs ?? COOLDOWN_CAP_MS;\n\tconst base = policy.baseMs ?? COOLDOWN_BASE_MS;\n\tconst backoff = Math.min(cap, base * 2 ** Math.max(0, failureCount));\n\tconst floored = serverHintMs === undefined ? backoff : Math.max(backoff, serverHintMs);\n\treturn {\n\t\tcooldownMs: Math.min(cap, floored),\n\t\tretryAfterWasCapped: floored > cap,\n\t};\n}\n\nconst INVALID_KEY_TEXT = /invalid[ _-]?(?:api[ _-]?)?key|authentication[_ ]?error|invalid x-api-key|unauthorized/i;\nconst ACCOUNT_SCOPED_403_TEXT = /account|credential|token|api[ _-]?key|organization|subscription/i;\nconst RATE_LIMIT_TEXT = /rate[ _-]?limit|too many requests|resource_exhausted/i;\nconst BILLING_TEXT =\n\t/billing|credits?[ _-]?(?:required|exhausted|balance)|insufficient[ _-]?(?:funds|quota|credit)|payment[ _-]?required|quota[ _-]?exhausted/i;\nconst OVERLOAD_TEXT = /overloaded/i;\nconst NETWORK_TEXT =\n\t/econnreset|econnrefused|etimedout|enotfound|socket hang up|fetch failed|network error|request timed out/i;\nconst FAIL_FAST_TEXT =\n\t/context[ _-]?(?:length|window)|maximum context|invalid[ _-]?model|model[ _-]?not[ _-]?found|malformed[ _-]?stream|premature[ _-]?(?:close|stream)/i;\nconst ABORT_TEXT = /\\baborted?\\b/i;\n\nfunction isAbort(error: unknown, message: string): boolean {\n\tif (error instanceof Error && error.name === \"AbortError\") return true;\n\treturn ABORT_TEXT.test(message);\n}\n\nfunction isOverflowText(text: string): boolean {\n\treturn getOverflowPatterns().some((pattern) => pattern.test(text));\n}\n\n/**\n * Maps a provider failure onto the credential-pool action space. Credential-\n * scoped failures fail over (permanently for auth/billing, cooldown for rate\n * limits); provider-scoped faults (5xx/overload/network) retry the SAME slot\n * without blocking it, because blocking a healthy credential for a provider\n * outage only destroys prompt-cache locality; everything else fails the\n * request so the model fallback chain above keeps owning it.\n */\nexport function classifyCredentialFailure(\n\terror: unknown,\n\tcontext: { failureCount?: number; cooldownBaseMs?: number; cooldownCapMs?: number } = {},\n): CredentialAction {\n\tconst normalized = normalizeProviderError(error);\n\tconst text = normalized.messageCarriesBody ? normalized.message : `${normalized.message} ${normalized.body ?? \"\"}`;\n\tconst status = normalized.status;\n\tconst failureCount = context.failureCount ?? 0;\n\n\tif (isAbort(error, text)) return { kind: \"fail_request\" };\n\tif (status === 401 || INVALID_KEY_TEXT.test(text)) {\n\t\treturn { kind: \"failover\", block: { reason: \"auth_error\" } };\n\t}\n\tif (status === 403) {\n\t\treturn ACCOUNT_SCOPED_403_TEXT.test(text)\n\t\t\t? { kind: \"failover\", block: { reason: \"auth_error\" } }\n\t\t\t: { kind: \"fail_request\" };\n\t}\n\tif (status === 402 || BILLING_TEXT.test(text)) {\n\t\treturn { kind: \"failover\", block: { reason: \"account_disabled\" } };\n\t}\n\tif (status === 429 || RATE_LIMIT_TEXT.test(text)) {\n\t\tconst hint = extract429RetryAfterMs({\n\t\t\tstatus: status ?? 429,\n\t\t\tbodyText: text,\n\t\t});\n\t\treturn {\n\t\t\tkind: \"failover\",\n\t\t\tblock: {\n\t\t\t\treason: \"rate_limit\",\n\t\t\t\t...rateLimitCooldown(failureCount, hint, {\n\t\t\t\t\tbaseMs: context.cooldownBaseMs,\n\t\t\t\t\tcapMs: context.cooldownCapMs,\n\t\t\t\t}),\n\t\t\t},\n\t\t};\n\t}\n\tif (isOverflowText(text) || status === 400 || status === 404 || FAIL_FAST_TEXT.test(text)) {\n\t\treturn { kind: \"fail_request\" };\n\t}\n\tif (status === 529 || OVERLOAD_TEXT.test(text) || (status !== undefined && status >= 500 && status < 600)) {\n\t\treturn { kind: \"retry_same\", maxAttempts: RETRY_SAME_MAX_ATTEMPTS };\n\t}\n\tif (status === 408 || NETWORK_TEXT.test(text)) {\n\t\treturn { kind: \"retry_same\", maxAttempts: RETRY_SAME_MAX_ATTEMPTS };\n\t}\n\treturn { kind: \"fail_request\" };\n}\n"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export declare const MAX_ENV_SLOT_INDEX = 16;
|
|
2
|
+
export type EnvCredentialSlot = {
|
|
3
|
+
name: string;
|
|
4
|
+
envVarName: string;
|
|
5
|
+
key: string;
|
|
6
|
+
source: "env";
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Anthropic maps to bearer/OAuth token vars ahead of its API-key var; numbered
|
|
10
|
+
* slots must extend the API-key lane, so prefer the `*_API_KEY` entry when the
|
|
11
|
+
* canonical mapping lists several variables.
|
|
12
|
+
*/
|
|
13
|
+
export declare function primaryEnvVar(providerId: string): string | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Discovers numbered env credential slots: the primary var yields slot `env`,
|
|
16
|
+
* and `<VAR>_2` .. `<VAR>_16` yield `env-2` .. `env-16`. Discovery is
|
|
17
|
+
* gap-tolerant - a configured `<VAR>_3` with no `<VAR>_2` is still found and no
|
|
18
|
+
* slot is invented for the gap. Stored credentials keep outranking every env
|
|
19
|
+
* slot because resolution consults env only when nothing is stored.
|
|
20
|
+
*/
|
|
21
|
+
export declare function discoverEnvSlots(providerId: string, env: (name: string) => string | undefined): EnvCredentialSlot[];
|
|
22
|
+
//# sourceMappingURL=env-slots.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env-slots.d.ts","sourceRoot":"","sources":["../../../src/core/credential-pool/env-slots.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,kBAAkB,KAAK,CAAC;AAErC,MAAM,MAAM,iBAAiB,GAAG;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;IACnB,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,KAAK,CAAC;CACd,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAIpE;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,GAAG,iBAAiB,EAAE,CAYnH"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { getApiKeyEnvVars } from "@earendil-works/pi-ai";
|
|
2
|
+
export const MAX_ENV_SLOT_INDEX = 16;
|
|
3
|
+
/**
|
|
4
|
+
* Anthropic maps to bearer/OAuth token vars ahead of its API-key var; numbered
|
|
5
|
+
* slots must extend the API-key lane, so prefer the `*_API_KEY` entry when the
|
|
6
|
+
* canonical mapping lists several variables.
|
|
7
|
+
*/
|
|
8
|
+
export function primaryEnvVar(providerId) {
|
|
9
|
+
const envVars = getApiKeyEnvVars(providerId);
|
|
10
|
+
if (!envVars || envVars.length === 0)
|
|
11
|
+
return undefined;
|
|
12
|
+
return envVars.find((name) => name.endsWith("_API_KEY")) ?? envVars[0];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Discovers numbered env credential slots: the primary var yields slot `env`,
|
|
16
|
+
* and `<VAR>_2` .. `<VAR>_16` yield `env-2` .. `env-16`. Discovery is
|
|
17
|
+
* gap-tolerant - a configured `<VAR>_3` with no `<VAR>_2` is still found and no
|
|
18
|
+
* slot is invented for the gap. Stored credentials keep outranking every env
|
|
19
|
+
* slot because resolution consults env only when nothing is stored.
|
|
20
|
+
*/
|
|
21
|
+
export function discoverEnvSlots(providerId, env) {
|
|
22
|
+
const primary = primaryEnvVar(providerId);
|
|
23
|
+
if (!primary)
|
|
24
|
+
return [];
|
|
25
|
+
const slots = [];
|
|
26
|
+
const base = env(primary);
|
|
27
|
+
if (base)
|
|
28
|
+
slots.push({ name: "env", envVarName: primary, key: base, source: "env" });
|
|
29
|
+
for (let index = 2; index <= MAX_ENV_SLOT_INDEX; index++) {
|
|
30
|
+
const envVarName = `${primary}_${index}`;
|
|
31
|
+
const value = env(envVarName);
|
|
32
|
+
if (value)
|
|
33
|
+
slots.push({ name: `env-${index}`, envVarName, key: value, source: "env" });
|
|
34
|
+
}
|
|
35
|
+
return slots;
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=env-slots.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env-slots.js","sourceRoot":"","sources":["../../../src/core/credential-pool/env-slots.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD,MAAM,CAAC,MAAM,kBAAkB,GAAG,E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Vars } from \"@earendil-works/pi-ai\";\n\nexport const MAX_ENV_SLOT_INDEX = 16;\n\nexport type EnvCredentialSlot = {\n\tname: string;\n\tenvVarName: string;\n\tkey: string;\n\tsource: \"env\";\n};\n\n/**\n * Anthropic maps to bearer/OAuth token vars ahead of its API-key var; numbered\n * slots must extend the API-key lane, so prefer the `*_API_KEY` entry when the\n * canonical mapping lists several variables.\n */\nexport function primaryEnvVar(providerId: string): string | undefined {\n\tconst envVars = getApiKeyEnvVars(providerId);\n\tif (!envVars || envVars.length === 0) return undefined;\n\treturn envVars.find((name) => name.endsWith(\"_API_KEY\")) ?? envVars[0];\n}\n\n/**\n * Discovers numbered env credential slots: the primary var yields slot `env`,\n * and `<VAR>_2` .. `<VAR>_16` yield `env-2` .. `env-16`. Discovery is\n * gap-tolerant - a configured `<VAR>_3` with no `<VAR>_2` is still found and no\n * slot is invented for the gap. Stored credentials keep outranking every env\n * slot because resolution consults env only when nothing is stored.\n */\nexport function discoverEnvSlots(providerId: string, env: (name: string) => string | undefined): EnvCredentialSlot[] {\n\tconst primary = primaryEnvVar(providerId);\n\tif (!primary) return [];\n\tconst slots: EnvCredentialSlot[] = [];\n\tconst base = env(primary);\n\tif (base) slots.push({ name: \"env\", envVarName: primary, key: base, source: \"env\" });\n\tfor (let index = 2; index <= MAX_ENV_SLOT_INDEX; index++) {\n\t\tconst envVarName = `${primary}_${index}`;\n\t\tconst value = env(envVarName);\n\t\tif (value) slots.push({ name: `env-${index}`, envVarName, key: value, source: \"env\" });\n\t}\n\treturn slots;\n}\n"]}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { TURN_RETRY_SUPPRESSION_PREFIX } from "@earendil-works/pi-ai/auth/pool/failover";
|
|
2
|
+
import { type CredentialAction, type CredentialBlock } from "./classify.ts";
|
|
3
|
+
export { TURN_RETRY_SUPPRESSION_PREFIX };
|
|
4
|
+
export type RunSlot = {
|
|
5
|
+
name: string;
|
|
6
|
+
blockedUntil?: number;
|
|
7
|
+
blockReason?: string;
|
|
8
|
+
failureCount?: number;
|
|
9
|
+
lease?: {
|
|
10
|
+
id: string;
|
|
11
|
+
expiresAt: number;
|
|
12
|
+
};
|
|
13
|
+
pinned?: boolean;
|
|
14
|
+
};
|
|
15
|
+
export type CredentialFailoverEvent<TSlot extends RunSlot> = {
|
|
16
|
+
slot: TSlot;
|
|
17
|
+
block: CredentialBlock;
|
|
18
|
+
attempt: number;
|
|
19
|
+
committedOutput: boolean;
|
|
20
|
+
};
|
|
21
|
+
export type RunCredentialFailoverOptions<TEvent, TSlot extends RunSlot> = {
|
|
22
|
+
/** Re-read before every distinct-credential attempt so a newly added slot participates. */
|
|
23
|
+
listSlots: () => Promise<readonly TSlot[]> | readonly TSlot[];
|
|
24
|
+
select: (slots: readonly TSlot[]) => TSlot;
|
|
25
|
+
runAttempt: (slot: TSlot) => AsyncIterable<TEvent> | Promise<AsyncIterable<TEvent>>;
|
|
26
|
+
/**
|
|
27
|
+
* REQUIRED and default-DENY by contract: return false only for event types
|
|
28
|
+
* explicitly known to be pre-commit bookkeeping. Any event this predicate
|
|
29
|
+
* does not recognize must count as committed output.
|
|
30
|
+
*/
|
|
31
|
+
isCommittedOutput: (event: TEvent) => boolean;
|
|
32
|
+
classify?: (error: unknown, context: {
|
|
33
|
+
failureCount: number;
|
|
34
|
+
cooldownBaseMs?: number;
|
|
35
|
+
cooldownCapMs?: number;
|
|
36
|
+
}) => CredentialAction;
|
|
37
|
+
errorFromEvent?: (event: TEvent) => unknown | undefined;
|
|
38
|
+
/** Persisted BEFORE a replacement slot is selected so a crash never forgets a block. */
|
|
39
|
+
persistBlock: (slot: TSlot, block: CredentialBlock) => void | Promise<void>;
|
|
40
|
+
onRotate?: (event: CredentialFailoverEvent<TSlot>) => void | Promise<void>;
|
|
41
|
+
onSuccess?: (slot: TSlot) => void | Promise<void>;
|
|
42
|
+
now?: () => number;
|
|
43
|
+
};
|
|
44
|
+
export declare class CredentialFailoverError extends Error {
|
|
45
|
+
readonly action: CredentialAction;
|
|
46
|
+
readonly original: unknown;
|
|
47
|
+
readonly suppressTurnRetry: boolean;
|
|
48
|
+
readonly retryAt: number | undefined;
|
|
49
|
+
constructor(action: CredentialAction, original: unknown, options: {
|
|
50
|
+
suppressTurnRetry: boolean;
|
|
51
|
+
retryAt?: number;
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Generic in-lane credential failover. Runs at most one failover attempt per
|
|
56
|
+
* slot per request, retries provider-scoped faults on the SAME slot up to the
|
|
57
|
+
* classifier's bound without blocking it, and rotates only while no committed
|
|
58
|
+
* output has reached the caller - afterwards the failure is rethrown with the
|
|
59
|
+
* `senpi:no-turn-retry:` marker so the turn is never replayed.
|
|
60
|
+
*/
|
|
61
|
+
export declare function runCredentialFailover<TEvent, TSlot extends RunSlot>(options: RunCredentialFailoverOptions<TEvent, TSlot>): AsyncGenerator<TEvent>;
|
|
62
|
+
//# sourceMappingURL=failover.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"failover.d.ts","sourceRoot":"","sources":["../../../src/core/credential-pool/failover.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,6BAA6B,EAAE,MAAM,0CAA0C,CAAC;AACzF,OAAO,EAAE,KAAK,gBAAgB,EAAE,KAAK,eAAe,EAA6B,MAAM,eAAe,CAAC;AAEvG,OAAO,EAAE,6BAA6B,EAAE,CAAC;AAEzC,MAAM,MAAM,OAAO,GAAG;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,KAAK,CAAC,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,CAAC;IAC1C,MAAM,CAAC,EAAE,OAAO,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,uBAAuB,CAAC,KAAK,S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G,CAAC,EAAE,MAAM,MAAM,CAAC;CACnB,CAAC;AAEF,qBAAa,uBAAwB,SAAQ,KAAK;IACjD,QAAQ,CAAC,MAAM,EAAE,gBAAgB,CAAC;IAClC,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;IAC3B,QAAQ,CAAC,iBAAiB,EAAE,OAAO,CAAC;IACpC,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;gBAEzB,MAAM,EAAE,gBAAgB,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,EAAE;QAAE,iBAAiB,EAAE,OAAO,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE;CASlH;AA4BD;;;;;;GAMG;AACH,wBAAuB,qBAAqB,CAAC,MAAM,EAAE,KAAK,SAAS,OAAO,EACzE,OAAO,EAAE,4BAA4B,CAAC,MAAM,EAAE,KAAK,CAAC,GAClD,cAAc,CAAC,MAAM,CAAC,CAyExB"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { TURN_RETRY_SUPPRESSION_PREFIX } from "@earendil-works/pi-ai/auth/pool/failover";
|
|
2
|
+
import { classifyCredentialFailure } from "./classify.js";
|
|
3
|
+
export { TURN_RETRY_SUPPRESSION_PREFIX };
|
|
4
|
+
export class CredentialFailoverError extends Error {
|
|
5
|
+
constructor(action, original, options) {
|
|
6
|
+
const detail = original instanceof Error ? original.message : String(original);
|
|
7
|
+
super(`${options.suppressTurnRetry ? TURN_RETRY_SUPPRESSION_PREFIX : ""}${detail}`, { cause: original });
|
|
8
|
+
this.name = "CredentialFailoverError";
|
|
9
|
+
this.action = action;
|
|
10
|
+
this.original = original;
|
|
11
|
+
this.suppressTurnRetry = options.suppressTurnRetry;
|
|
12
|
+
this.retryAt = options.retryAt;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function isAvailable(slot, now) {
|
|
16
|
+
if (slot.blockReason === "auth_error" || slot.blockReason === "account_disabled")
|
|
17
|
+
return false;
|
|
18
|
+
if (slot.blockedUntil !== undefined && slot.blockedUntil > now)
|
|
19
|
+
return false;
|
|
20
|
+
// A live lease marks the one caller admitted to run the half-open probe.
|
|
21
|
+
// listSlots excludes that lease for later callers; the holder must remain runnable.
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
function soonestRetryAt(slots, now) {
|
|
25
|
+
const deadlines = slots
|
|
26
|
+
.map((slot) => slot.blockedUntil)
|
|
27
|
+
.filter((value) => value !== undefined && value > now);
|
|
28
|
+
return deadlines.length === 0 ? undefined : Math.min(...deadlines);
|
|
29
|
+
}
|
|
30
|
+
async function settle(stream) {
|
|
31
|
+
// A failed attempt must fully release its transport before a replacement
|
|
32
|
+
// starts, or two live streams can interleave provider-side effects.
|
|
33
|
+
const iterator = stream[Symbol.asyncIterator]();
|
|
34
|
+
try {
|
|
35
|
+
await iterator.return?.(undefined);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
// Settling a dead stream must never mask the original failure.
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Generic in-lane credential failover. Runs at most one failover attempt per
|
|
43
|
+
* slot per request, retries provider-scoped faults on the SAME slot up to the
|
|
44
|
+
* classifier's bound without blocking it, and rotates only while no committed
|
|
45
|
+
* output has reached the caller - afterwards the failure is rethrown with the
|
|
46
|
+
* `senpi:no-turn-retry:` marker so the turn is never replayed.
|
|
47
|
+
*/
|
|
48
|
+
export async function* runCredentialFailover(options) {
|
|
49
|
+
const now = options.now ?? Date.now;
|
|
50
|
+
const classify = options.classify ?? classifyCredentialFailure;
|
|
51
|
+
const attempted = new Set();
|
|
52
|
+
const retriesBySlot = new Map();
|
|
53
|
+
let lastError;
|
|
54
|
+
let lastOriginal;
|
|
55
|
+
while (true) {
|
|
56
|
+
const slots = await options.listSlots();
|
|
57
|
+
const candidates = slots.filter((slot) => !attempted.has(slot.name) && isAvailable(slot, now()));
|
|
58
|
+
if (candidates.length === 0) {
|
|
59
|
+
const retryAt = soonestRetryAt(slots, now());
|
|
60
|
+
throw lastError !== undefined && lastOriginal !== undefined
|
|
61
|
+
? new CredentialFailoverError(lastError.action, lastOriginal, {
|
|
62
|
+
suppressTurnRetry: lastError.suppressTurnRetry,
|
|
63
|
+
...(retryAt === undefined ? {} : { retryAt }),
|
|
64
|
+
})
|
|
65
|
+
: new CredentialFailoverError({ kind: "fail_request" }, new Error("No credential slots available"), {
|
|
66
|
+
suppressTurnRetry: false,
|
|
67
|
+
...(retryAt === undefined ? {} : { retryAt }),
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
const slot = options.select(candidates);
|
|
71
|
+
let committedOutput = false;
|
|
72
|
+
let attemptStream;
|
|
73
|
+
try {
|
|
74
|
+
attemptStream = await options.runAttempt(slot);
|
|
75
|
+
for await (const event of attemptStream) {
|
|
76
|
+
const failure = options.errorFromEvent?.(event);
|
|
77
|
+
if (failure !== undefined)
|
|
78
|
+
throw failure;
|
|
79
|
+
committedOutput ||= options.isCommittedOutput(event);
|
|
80
|
+
yield event;
|
|
81
|
+
}
|
|
82
|
+
await options.onSuccess?.(slot);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
if (attemptStream)
|
|
87
|
+
await settle(attemptStream);
|
|
88
|
+
const failureCount = (slot.failureCount ?? 0) + (retriesBySlot.get(slot.name) ?? 0);
|
|
89
|
+
const action = classify(error, {
|
|
90
|
+
failureCount,
|
|
91
|
+
});
|
|
92
|
+
lastOriginal = error;
|
|
93
|
+
if (action.kind === "retry_same" && !committedOutput) {
|
|
94
|
+
const used = (retriesBySlot.get(slot.name) ?? 0) + 1;
|
|
95
|
+
retriesBySlot.set(slot.name, used);
|
|
96
|
+
if (used < action.maxAttempts)
|
|
97
|
+
continue;
|
|
98
|
+
throw new CredentialFailoverError(action, error, {
|
|
99
|
+
suppressTurnRetry: false,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
if (action.kind !== "failover") {
|
|
103
|
+
throw new CredentialFailoverError(action, error, {
|
|
104
|
+
suppressTurnRetry: committedOutput,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
await options.persistBlock(slot, action.block);
|
|
108
|
+
attempted.add(slot.name);
|
|
109
|
+
const failure = new CredentialFailoverError(action, error, {
|
|
110
|
+
suppressTurnRetry: committedOutput,
|
|
111
|
+
});
|
|
112
|
+
lastError = failure;
|
|
113
|
+
await options.onRotate?.({
|
|
114
|
+
slot,
|
|
115
|
+
block: action.block,
|
|
116
|
+
attempt: attempted.size,
|
|
117
|
+
committedOutput,
|
|
118
|
+
});
|
|
119
|
+
if (committedOutput)
|
|
120
|
+
throw failure;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=failover.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"failover.js","sourceRoot":"","sources":["../../../src/core/credential-pool/failover.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,6BAA6B,EAAE,MAAM,0CAA0C,CAAC;AACzF,OAAO,EAA+C,yBAAyB,EAAE,MAAM,eAAe,CAAC;AAEvG,OAAO,EAAE,6BAA6B,EAAE,CAAC;AAyCzC,MAAM,OAAO,uBAAwB,SAAQ,KAAK;IAMjD,YAAY,MAAwB,EAAE,QAAiB,EAAE,OAAyD;QACjH,MAAM,MAAM,GAAG,QAAQ,YAAY,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAC/E,KAAK,CAAC,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC,CAAC,6BAA6B,CAAC,CAAC,CAAC,EAAE,GAAG,MAAM,EAAE,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAC;QACzG,IAAI,CAAC,IAAI,GAAG,yBAAyB,CAAC;QACtC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAA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AAC;gBACnC,IAAI,IAAI,GAAG,MAAM,CAAC,WAAW;oBAAE,SAAS;gBACxC,MAAM,IAAI,uBAAuB,CAAC,MAAM,EAAE,KAAK,EAAE;oBAChD,iBAAiB,EAAE,KAAK;iBACxB,CAAC,CAAC;YACJ,CAAC;YACD,IAAI,MAAM,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;gBAChC,MAAM,IAAI,uBAAuB,CAAC,MAAM,EAAE,KAAK,EAAE;oBAChD,iBAAiB,EAAE,eAAe;iBAClC,CAAC,CAAC;YACJ,CAAC;YAED,MAAM,OAAO,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;YAC/C,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACzB,MAAM,OAAO,GAAG,IAAI,uBAAuB,CAAC,MAAM,EAAE,KAAK,EAAE;gBAC1D,iBAAiB,EAAE,eAAe;aAClC,CAAC,CAAC;YACH,SAAS,GAAG,OAAO,CAAC;YACpB,MAAM,OAAO,CAAC,QAAQ,EAAE,CAAC;gBACxB,IAAI;gBACJ,KAAK,EAAE,MAAM,CAAC,KAAK;gBACnB,OAAO,EAAE,SAAS,CAAC,IAAI;gBACvB,eAAe;aACf,CAAC,CAAC;YACH,IAAI,eAAe;gBAAE,MAAM,OAAO,CAAC;QACpC,CAAC;IACF,CAAC;AACF,CAAC","sourcesContent":["import { TURN_RETRY_SUPPRESSION_PREFIX } from \"@earendil-works/pi-ai/auth/pool/failover\";\nimport { type CredentialAction, type CredentialBlock, classifyCredentialFailure } from \"./classify.ts\";\n\nexport { TURN_RETRY_SUPPRESSION_PREFIX };\n\nexport type RunSlot = {\n\tname: string;\n\tblockedUntil?: number;\n\tblockReason?: string;\n\tfailureCount?: number;\n\tlease?: { id: string; expiresAt: number };\n\tpinned?: boolean;\n};\n\nexport type CredentialFailoverEvent<TSlot extends RunSlot> = {\n\tslot: TSlot;\n\tblock: CredentialBlock;\n\tattempt: number;\n\tcommittedOutput: boolean;\n};\n\nexport type RunCredentialFailoverOptions<TEvent, TSlot extends RunSlot> = {\n\t/** Re-read before every distinct-credential attempt so a newly added slot participates. */\n\tlistSlots: () => Promise<readonly TSlot[]> | readonly TSlot[];\n\tselect: (slots: readonly TSlot[]) => TSlot;\n\trunAttempt: (slot: TSlot) => AsyncIterable<TEvent> | Promise<AsyncIterable<TEvent>>;\n\t/**\n\t * REQUIRED and default-DENY by contract: return false only for event types\n\t * explicitly known to be pre-commit bookkeeping. Any event this predicate\n\t * does not recognize must count as committed output.\n\t */\n\tisCommittedOutput: (event: TEvent) => boolean;\n\tclassify?: (\n\t\terror: unknown,\n\t\tcontext: { failureCount: number; cooldownBaseMs?: number; cooldownCapMs?: number },\n\t) => CredentialAction;\n\terrorFromEvent?: (event: TEvent) => unknown | undefined;\n\t/** Persisted BEFORE a replacement slot is selected so a crash never forgets a block. */\n\tpersistBlock: (slot: TSlot, block: CredentialBlock) => void | Promise<void>;\n\tonRotate?: (event: CredentialFailoverEvent<TSlot>) => void | Promise<void>;\n\tonSuccess?: (slot: TSlot) => void | Promise<void>;\n\tnow?: () => number;\n};\n\nexport class CredentialFailoverError extends Error {\n\treadonly action: CredentialAction;\n\treadonly original: unknown;\n\treadonly suppressTurnRetry: boolean;\n\treadonly retryAt: number | undefined;\n\n\tconstructor(action: CredentialAction, original: unknown, options: { suppressTurnRetry: boolean; retryAt?: number }) {\n\t\tconst detail = original instanceof Error ? original.message : String(original);\n\t\tsuper(`${options.suppressTurnRetry ? TURN_RETRY_SUPPRESSION_PREFIX : \"\"}${detail}`, { cause: original });\n\t\tthis.name = \"CredentialFailoverError\";\n\t\tthis.action = action;\n\t\tthis.original = original;\n\t\tthis.suppressTurnRetry = options.suppressTurnRetry;\n\t\tthis.retryAt = options.retryAt;\n\t}\n}\n\nfunction isAvailable(slot: RunSlot, now: number): boolean {\n\tif (slot.blockReason === \"auth_error\" || slot.blockReason === \"account_disabled\") return false;\n\tif (slot.blockedUntil !== undefined && slot.blockedUntil > now) return false;\n\t// A live lease marks the one caller admitted to run the half-open probe.\n\t// listSlots excludes that lease for later callers; the holder must remain runnable.\n\treturn true;\n}\n\nfunction soonestRetryAt(slots: readonly RunSlot[], now: number): number | undefined {\n\tconst deadlines = slots\n\t\t.map((slot) => slot.blockedUntil)\n\t\t.filter((value): value is number => value !== undefined && value > now);\n\treturn deadlines.length === 0 ? undefined : Math.min(...deadlines);\n}\n\nasync function settle<TEvent>(stream: AsyncIterable<TEvent>): Promise<void> {\n\t// A failed attempt must fully release its transport before a replacement\n\t// starts, or two live streams can interleave provider-side effects.\n\tconst iterator = stream[Symbol.asyncIterator]();\n\ttry {\n\t\tawait iterator.return?.(undefined);\n\t} catch {\n\t\t// Settling a dead stream must never mask the original failure.\n\t}\n}\n\n/**\n * Generic in-lane credential failover. Runs at most one failover attempt per\n * slot per request, retries provider-scoped faults on the SAME slot up to the\n * classifier's bound without blocking it, and rotates only while no committed\n * output has reached the caller - afterwards the failure is rethrown with the\n * `senpi:no-turn-retry:` marker so the turn is never replayed.\n */\nexport async function* runCredentialFailover<TEvent, TSlot extends RunSlot>(\n\toptions: RunCredentialFailoverOptions<TEvent, TSlot>,\n): AsyncGenerator<TEvent> {\n\tconst now = options.now ?? Date.now;\n\tconst classify = options.classify ?? classifyCredentialFailure;\n\tconst attempted = new Set<string>();\n\tconst retriesBySlot = new Map<string, number>();\n\tlet lastError: CredentialFailoverError | undefined;\n\tlet lastOriginal: unknown;\n\n\twhile (true) {\n\t\tconst slots = await options.listSlots();\n\t\tconst candidates = slots.filter((slot) => !attempted.has(slot.name) && isAvailable(slot, now()));\n\t\tif (candidates.length === 0) {\n\t\t\tconst retryAt = soonestRetryAt(slots, now());\n\t\t\tthrow lastError !== undefined && lastOriginal !== undefined\n\t\t\t\t? new CredentialFailoverError(lastError.action, lastOriginal, {\n\t\t\t\t\t\tsuppressTurnRetry: lastError.suppressTurnRetry,\n\t\t\t\t\t\t...(retryAt === undefined ? {} : { retryAt }),\n\t\t\t\t\t})\n\t\t\t\t: new CredentialFailoverError({ kind: \"fail_request\" }, new Error(\"No credential slots available\"), {\n\t\t\t\t\t\tsuppressTurnRetry: false,\n\t\t\t\t\t\t...(retryAt === undefined ? {} : { retryAt }),\n\t\t\t\t\t});\n\t\t}\n\t\tconst slot = options.select(candidates);\n\t\tlet committedOutput = false;\n\t\tlet attemptStream: AsyncIterable<TEvent> | undefined;\n\t\ttry {\n\t\t\tattemptStream = await options.runAttempt(slot);\n\t\t\tfor await (const event of attemptStream) {\n\t\t\t\tconst failure = options.errorFromEvent?.(event);\n\t\t\t\tif (failure !== undefined) throw failure;\n\t\t\t\tcommittedOutput ||= options.isCommittedOutput(event);\n\t\t\t\tyield event;\n\t\t\t}\n\t\t\tawait options.onSuccess?.(slot);\n\t\t\treturn;\n\t\t} catch (error) {\n\t\t\tif (attemptStream) await settle(attemptStream);\n\t\t\tconst failureCount = (slot.failureCount ?? 0) + (retriesBySlot.get(slot.name) ?? 0);\n\t\t\tconst action = classify(error, {\n\t\t\t\tfailureCount,\n\t\t\t});\n\t\t\tlastOriginal = error;\n\n\t\t\tif (action.kind === \"retry_same\" && !committedOutput) {\n\t\t\t\tconst used = (retriesBySlot.get(slot.name) ?? 0) + 1;\n\t\t\t\tretriesBySlot.set(slot.name, used);\n\t\t\t\tif (used < action.maxAttempts) continue;\n\t\t\t\tthrow new CredentialFailoverError(action, error, {\n\t\t\t\t\tsuppressTurnRetry: false,\n\t\t\t\t});\n\t\t\t}\n\t\t\tif (action.kind !== \"failover\") {\n\t\t\t\tthrow new CredentialFailoverError(action, error, {\n\t\t\t\t\tsuppressTurnRetry: committedOutput,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tawait options.persistBlock(slot, action.block);\n\t\t\tattempted.add(slot.name);\n\t\t\tconst failure = new CredentialFailoverError(action, error, {\n\t\t\t\tsuppressTurnRetry: committedOutput,\n\t\t\t});\n\t\t\tlastError = failure;\n\t\t\tawait options.onRotate?.({\n\t\t\t\tslot,\n\t\t\t\tblock: action.block,\n\t\t\t\tattempt: attempted.size,\n\t\t\t\tcommittedOutput,\n\t\t\t});\n\t\t\tif (committedOutput) throw failure;\n\t\t}\n\t}\n}\n"]}
|