@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
110 lines • 4.89 kB
TypeScript
import type { FallbackLogger } from "./log.ts";
/**
* Fallback-chain circuit breaker. An entry that fails out of a configured chain
* opens a circuit keyed by its base selector (`provider/id`); while it is open,
* chain resolution skips the entry instead of spending its retry budget again.
*
* One breaker is shared by every session in the process that uses the same agent
* directory - the config that defines the providers and selectors - so a session
* started by `/new`, `/resume`, or `/fork` (each gets a fresh model runtime) and
* an in-process subagent do not re-discover a dead provider.
* The class is clock-free: every call receives `now`, keeping sessions' injected
* clocks authoritative and tests deterministic.
*/
export declare const DEFAULT_CIRCUIT_COOLDOWN_MS = 60000;
export declare const DEFAULT_CIRCUIT_MAX_COOLDOWN_MS: number;
export interface FallbackCircuitSettings {
/** First cooldown after an entry fails out of a chain; doubles per consecutive open. 0 disables the breaker. */
circuitCooldownMs?: number;
circuitMaxCooldownMs?: number;
}
export interface ResolvedFallbackCircuitSettings {
cooldownMs: number;
maxCooldownMs: number;
}
export interface CircuitOpenRequest {
now: number;
cooldownMs: number;
maxCooldownMs: number;
retryAfterMs?: number;
/** The reset window a usage limit named for itself; when present it alone sets the open. */
resetWindowMs?: number;
/** The failure is a verdict for every session: the entry is spent (billing, quota, an account limit). A provider-named wait is not. */
exhausted?: boolean;
}
export interface ProbeToken {
readonly selector: string;
readonly owner: string;
readonly generation: number;
}
export type CircuitAdmission = {
kind: "closed";
} | {
kind: "open";
} | {
kind: "probe";
token: ProbeToken;
};
export declare function resolveFallbackCircuitSettings(settings: FallbackCircuitSettings | undefined): ResolvedFallbackCircuitSettings;
export declare class FallbackCircuitBreaker {
private readonly circuits;
private generations;
get size(): number;
open(selector: string, request: CircuitOpenRequest): number;
isOpen(selector: string, now: number, owner: string): boolean;
/**
* Atomic admission: a closed selector passes, a cooling one or one whose probe
* another requester holds is refused, and a half-open one hands `owner` its
* single probe under a fresh generation. The probe stays exclusive until its
* token is released or the circuit settles - it never expires on a timer, so a
* slow probe cannot overlap a second one, and a stale token never releases a
* later acquisition.
*/
admit(selector: string, now: number, owner: string): CircuitAdmission;
release(token: ProbeToken): void;
releaseOwnersWithPrefix(prefix: string): void;
close(selector: string): void;
has(selector: string): boolean;
isExhausted(selector: string): boolean;
sweep(now: number): void;
}
/**
* A session's requests are admitted per lane, not per session: the foreground
* turn is one lane, each background probe-back its own. Two lanes of one session
* never share a probe, so a probe-back in flight keeps the foreground turn off
* the entry exactly as it keeps a sibling session off.
*/
export type CircuitLane = "turn" | `probe-back:${number}`;
export interface FallbackCircuitAccess {
noteFailure(selector: string, failure: {
retryAfterMs?: number;
errorMessage?: string;
}): void;
/** Whether the breaker holds any state for the selector, i.e. it, not a session cooldown, governs recovery. */
governs(selector: string): boolean;
isOpen(selector: string, lane?: CircuitLane): boolean;
/** Whether the open circuit records a verdict every session should act on: the entry is spent. */
isExhausted(selector: string): boolean;
admit(selector: string, lane?: CircuitLane): CircuitAdmission;
release(token: ProbeToken): void;
releaseAll(): void;
close(selector: string): void;
}
export interface FallbackCircuitAccessDeps {
breaker: FallbackCircuitBreaker;
owner(): string;
now(): number;
/** Wall clock for parsing reset times; the open deadline itself stays on `now`. */
wallNow?(): number;
settings(): ResolvedFallbackCircuitSettings;
logger: FallbackLogger;
}
export declare function createFallbackCircuitAccess(deps: FallbackCircuitAccessDeps): FallbackCircuitAccess;
export declare function fallbackCircuitsFor(agentDir: string): FallbackCircuitBreaker;
/** Holds the agent dir's breaker for one session; `release` is idempotent. */
export declare function acquireFallbackCircuits(agentDir: string): {
breaker: FallbackCircuitBreaker;
release(): void;
};
export declare function monotonicNow(): number;
//# sourceMappingURL=circuit.d.ts.map