UNPKG

@code-yeongyu/senpi

Version:

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

116 lines • 6 kB
import { TURN_RETRY_SUPPRESSION_PREFIX } from "@earendil-works/pi-ai/auth/pool/failover"; import { type CredentialAction, type CredentialBlock } from "./classify.ts"; /** * 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 }; export type RunSlot = { name: string; blockedUntil?: number; blockReason?: string; failureCount?: number; lease?: { id: string; expiresAt: number; }; pinned?: boolean; }; export type CredentialFailoverEvent<TSlot extends RunSlot> = { slot: TSlot; block: CredentialBlock; attempt: number; committedOutput: boolean; }; export type RunCredentialFailoverOptions<TEvent, TSlot extends RunSlot> = { /** Re-read before every distinct-credential attempt so a newly added slot participates. */ listSlots: () => Promise<readonly TSlot[]> | readonly TSlot[]; select: (slots: readonly TSlot[]) => TSlot; runAttempt: (slot: TSlot) => AsyncIterable<TEvent> | Promise<AsyncIterable<TEvent>>; /** * REQUIRED and default-DENY by contract: return false only for event types * explicitly known to be pre-commit bookkeeping. Any event this predicate * does not recognize must count as committed output. */ isCommittedOutput: (event: TEvent) => boolean; /** * Identifies the stream-start announcement. Consumers open one message per * start frame, so once a start has reached the caller the engine drops the * start a replacement attempt re-announces; its later frames then update the * message the caller already holds. */ isStreamStart?: (event: TEvent) => boolean; classify?: (error: unknown, context: { failureCount: number; cooldownBaseMs?: number; cooldownCapMs?: number; }) => CredentialAction; /** * Extracts the failure carried by a terminal error EVENT. When that failure * ends the request, the event itself is forwarded to the caller - partial * content, usage and transport diagnostics intact - instead of a synthesized * error that would erase them. */ errorFromEvent?: (event: TEvent) => unknown | undefined; /** Persisted BEFORE a replacement slot is selected so a crash never forgets a block. */ persistBlock: (slot: TSlot, block: CredentialBlock, error?: unknown) => void | Promise<void>; onRotate?: (event: CredentialFailoverEvent<TSlot>) => void | Promise<void>; onSuccess?: (slot: TSlot) => void | Promise<void>; /** * Marks the terminal event forwarded when every slot is limited, with the time the first one is due * back, so the session and goal read a rate limit with its reset instead of one slot's bare error * (senpi#3975). */ withPoolRetryAt?: (event: TEvent, retryAt: number) => TEvent; /** * Called once when the request leaves a selected slot without a verdict on it: neither a success * nor a persisted block (an abort, a request-scoped failure, exhausted same-slot retries, or the * caller stopping the stream). A lane hands back what it reserved for the attempt, such as a * half-open probe lease, so the slot is not stranded until that reservation expires (senpi#4094). */ releaseSlot?: (slot: TSlot) => void | Promise<void>; now?: () => number; }; /** * 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 declare class CredentialFailoverError extends Error { readonly action: CredentialAction; readonly original: unknown; readonly retryAt: number | undefined; constructor(action: CredentialAction, original: unknown, options?: { retryAt?: number; }); } export declare function isAvailable(slot: Pick<RunSlot, "blockReason" | "blockedUntil">, now: number): boolean; /** * 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 declare function attemptCandidates<TSlot extends RunSlot>(slots: readonly TSlot[], attempted: ReadonlySet<string>, now: number): TSlot[]; /** The refusal a request gets when {@link attemptCandidates} is empty before any attempt ran. */ export declare function noSlotsError(slots: readonly RunSlot[], now: number): CredentialFailoverError; /** * 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 declare function runCredentialFailover<TEvent, TSlot extends RunSlot>(options: RunCredentialFailoverOptions<TEvent, TSlot>): AsyncGenerator<TEvent>; //# sourceMappingURL=failover.d.ts.map