UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

278 lines • 13.4 kB
import { TURN_RETRY_SUPPRESSION_PREFIX } from "@earendil-works/pi-ai/auth/pool/failover"; import { attachProviderDiagnostic } from "@earendil-works/pi-ai/utils/provider-diagnostic-carrier"; import { credentialPoolBlockedDiagnostic } from "@earendil-works/pi-ai/utils/provider-diagnostic-vocabulary"; import { classifyCredentialFailure } from "./classify.js"; /** * Re-exported for the lanes that still recognize the marker (the Claude SDK * lane stamps it because tools execute mid-stream there). The generic pool * below never stamps it: see {@link runCredentialFailover}. */ export { TURN_RETRY_SUPPRESSION_PREFIX }; /** * Thrown when an attempt failed by THROWING (no terminal event to forward) and * the pool has nothing left to try. The message is the provider's own text: * whole-turn recovery (same-model retry, model fallback) is the session * layer's decision, made from that text exactly as it is for a single-key * provider. */ export class CredentialFailoverError extends Error { constructor(action, original, options = {}) { const detail = original instanceof Error ? original.message : String(original); super(detail, { cause: original }); this.name = "CredentialFailoverError"; this.action = action; this.original = original; this.retryAt = options.retryAt; } } export function isAvailable(slot, now) { if (slot.blockReason === "account_disabled") return false; if (slot.blockedUntil !== undefined && slot.blockedUntil > now) return false; // A live lease marks the one caller admitted to run the half-open probe. // listSlots excludes that lease for later callers; the holder must remain runnable. return true; } /** * When every slot is cooling and this request has tried none, the rate-limited slot due back first * still gets the request (senpi#3771). A cooldown only predicts when a limit lifts: the provider can * lift it earlier (a usage reset the user redeemed), and refusing locally would fail every turn with * no provider answer until the prediction expires, even across a restart. A slot still limited * answers at once and is blocked again; one that recovered serves the turn and is cleared. */ function coolingProbe(slots, attempted) { if (attempted.size > 0) return []; const cooling = slots.filter((slot) => slot.blockReason === "rate_limit" && slot.blockedUntil !== undefined); const soonest = cooling.reduce((best, slot) => (best === undefined || (slot.blockedUntil ?? 0) < (best.blockedUntil ?? 0) ? slot : best), undefined); return soonest === undefined ? [] : [soonest]; } /** * The slots a request's next attempt may pick from: the untried, unblocked ones, else the cooling * probe. Empty means the request is refused. Shared by the stream and by auxiliary callers that * resolve the session's account outside it (senpi#3924), so both treat an all-blocked pool alike. */ export function attemptCandidates(slots, attempted, now) { const available = slots.filter((slot) => !attempted.has(slot.name) && isAvailable(slot, now)); return available.length > 0 ? available : coolingProbe(slots, attempted); } /** The refusal a request gets when {@link attemptCandidates} is empty before any attempt ran. */ export function noSlotsError(slots, now) { const retryAt = soonestRetryAt(slots, now); return new CredentialFailoverError({ kind: "fail_request" }, new Error(noSlotsMessage(slots, now)), retryAt === undefined ? {} : { retryAt }); } /** * A slot's due-back time as the reader's local clock shows it, followed by the ISO instant that the goal * provider-retry scan (`providerRetryAt`) and turn-end liveness read from this text (senpi#4329). */ function dueBack(epochMs) { return `${localTime(epochMs)} (${new Date(epochMs).toISOString()})`; } function localTime(epochMs) { return new Date(epochMs).toLocaleString("en-US", { year: "numeric", month: "short", day: "numeric", hour: "numeric", minute: "2-digit", timeZoneName: "short", }); } /** * Why the request had nowhere to go: each slot it could not use, with its reason and the time it is * due back, so a refusal is never a bare "no slots" that only a re-login seemed to fix (senpi#2081). * It ends with the one step that works: wait for the first slot due back, or reconnect when none is * due back on its own (senpi#4329). */ function noSlotsMessage(slots, now) { const blocked = slots.map((slot) => { const reason = slot.blockReason ?? "blocked"; if (slot.blockedUntil !== undefined && slot.blockedUntil > now) { return `${slot.name} (${reason} until ${dueBack(slot.blockedUntil)})`; } return reason === "account_disabled" ? `${slot.name} (account_disabled until re-login)` : `${slot.name} (${reason})`; }); const firstBack = soonestRetryAt(slots, now); const step = firstBack === undefined ? "Sign in again or add a new API key for this provider, or choose another model." : `Wait until ${localTime(firstBack)}, or choose another model.`; return blocked.length === 0 ? `No credential slots available. ${step}` : `No credential slots available: ${blocked.join(", ")}. ${step}`; } function isRateLimitBlock(action) { return action.kind === "failover" && action.block.reason === "rate_limit"; } /** Blocks that need the user (a new login, a re-enabled or topped-up account), not a wait. */ const OUT_OF_POOL_REASONS = new Set(["auth_error", "account_disabled"]); /** * The soonest rate-limit reset when the pool waits only on rate limits, else undefined (senpi#3975). * Slots out for auth or a disabled account are not counted; every other slot must be rate-limited, so a * slot with an unknown block or none keeps the refusal unclassified, and a disabled slot's re-probe time * is never the pool's reset. */ function limitedUntil(slots, now) { const resets = []; for (const slot of slots) { if (OUT_OF_POOL_REASONS.has(slot.blockReason ?? "")) continue; if (slot.blockReason !== "rate_limit" || slot.blockedUntil === undefined || slot.blockedUntil <= now) { return undefined; } resets.push(slot.blockedUntil); } return resets.length === 0 ? undefined : Math.min(...resets); } function soonestRetryAt(slots, now) { const deadlines = slots .map((slot) => slot.blockedUntil) .filter((value) => value !== undefined && value > now); return deadlines.length === 0 ? undefined : Math.min(...deadlines); } async function settle(stream) { // A failed attempt must fully release its transport before a replacement // starts, or two live streams can interleave provider-side effects. const iterator = stream[Symbol.asyncIterator](); try { await iterator.return?.(undefined); } catch { // Settling a dead stream must never mask the original failure. } } /** * Generic in-lane credential failover. Runs at most one failover attempt per * slot per request, retries provider-scoped faults on the SAME slot up to the * classifier's bound without blocking it, and rotates only while no committed * output has reached the caller. * * The engine guards ONE thing: the integrity of the single event stream the * caller consumes. Two attempts may never interleave inside it, so once a * delta has been delivered no replacement attempt starts. It does NOT decide * whether the turn may be replayed: after committed output the provider's own * terminal event is forwarded unchanged, and the session layer treats it like * any other mid-stream provider failure (a stall, a reset) - retire the * partial, retry or fall back within its budgets. In the plain streaming lanes * no tool runs before the message completes, so nothing here needs the * `senpi:no-turn-retry:` marker; the Claude SDK lane keeps its own. */ export async function* runCredentialFailover(options) { const now = options.now ?? Date.now; const classify = options.classify ?? classifyCredentialFailure; const attempted = new Set(); const retriesBySlot = new Map(); let lastError; let lastOriginal; let lastTerminalEvent; let startAnnounced = false; // The slot this request selected and has not yet settled with a success or a persisted block. let unsettled; try { while (true) { const slots = await options.listSlots(); const candidates = attemptCandidates(slots, attempted, now()); if (candidates.length === 0) { // Only a refusal that is itself a limit is the pool's rate limit: a probe refused for auth, billing // or a missing model keeps its own classification, which the goal's auth block reads (senpi#3975). const limitedRetryAt = lastError === undefined || isRateLimitBlock(lastError.action) ? limitedUntil(slots, now()) : undefined; if (lastTerminalEvent !== undefined) { yield limitedRetryAt === undefined || options.withPoolRetryAt === undefined ? lastTerminalEvent : options.withPoolRetryAt(lastTerminalEvent, limitedRetryAt); return; } const retryAt = soonestRetryAt(slots, now()); const refusal = lastError === undefined || lastOriginal === undefined ? noSlotsError(slots, now()) : new CredentialFailoverError(lastError.action, lastOriginal, retryAt === undefined ? {} : { retryAt }); throw limitedRetryAt === undefined ? refusal : attachProviderDiagnostic(refusal, credentialPoolBlockedDiagnostic(limitedRetryAt)); } const slot = options.select(candidates); unsettled = slot; let committedOutput = false; let terminalEvent; let attemptStream; try { attemptStream = await options.runAttempt(slot); for await (const event of attemptStream) { const failure = options.errorFromEvent?.(event); if (failure !== undefined) { terminalEvent = event; throw failure; } if (options.isStreamStart?.(event)) { if (startAnnounced) continue; startAnnounced = true; } committedOutput ||= options.isCommittedOutput(event); yield event; } unsettled = undefined; await options.onSuccess?.(slot); return; } catch (error) { if (attemptStream) await settle(attemptStream); const failureCount = (slot.failureCount ?? 0) + (retriesBySlot.get(slot.name) ?? 0); const action = classify(error, { failureCount, }); lastOriginal = error; lastTerminalEvent = terminalEvent; if (action.kind === "retry_same" && !committedOutput) { const used = (retriesBySlot.get(slot.name) ?? 0) + 1; retriesBySlot.set(slot.name, used); if (used < action.maxAttempts) { // Hand back what the attempt reserved before listing again: the next pass takes its own // reservation, and a lease kept here would list this slot as another request's probe. unsettled = undefined; await options.releaseSlot?.(slot); continue; } if (terminalEvent !== undefined) { yield terminalEvent; return; } throw new CredentialFailoverError(action, error); } if (action.kind !== "failover") { if (terminalEvent !== undefined) { yield terminalEvent; return; } throw new CredentialFailoverError(action, error); } unsettled = undefined; await options.persistBlock(slot, action.block, error); attempted.add(slot.name); const failure = new CredentialFailoverError(action, error); lastError = failure; await options.onRotate?.({ slot, block: action.block, attempt: attempted.size, committedOutput, }); if (committedOutput) { if (terminalEvent !== undefined) { yield terminalEvent; return; } throw failure; } } } } finally { if (unsettled !== undefined) await options.releaseSlot?.(unsettled); } } //# sourceMappingURL=failover.js.map