/** Proactive Codex OAuth refresh with terminal-failure surfacing. */ import type { AuthResult, CredentialStore, OAuthCredential } from '@earendil-works/pi-ai'; import type { CodexAuthState } from './types.ts'; import type { CodexUsageModels } from './usage-service.ts'; /** Default lead time before access-token expiry at which the Host refreshes. */ export declare const DEFAULT_REFRESH_MARGIN_MS = 300000; /** Transient refresh failures retry after these delays; the last one repeats. */ export declare const DEFAULT_REFRESH_RETRY_DELAYS_MS: readonly number[]; /** Host-only recipient of background refresh outcomes. */ export interface CodexRefreshSink { /** Monotonic login/logout generation used to reject outcomes from stale requests. */ getRefreshGeneration(): number; noteRefreshSuccess(expiresAt: number, generation: number): void; noteRefreshFailure(error: unknown, generation: number, failedExpires?: number): void; } /** Tunable proactive-refresh behavior. */ export interface CodexTokenRefresherOptions { /** pi-ai OAuth handler's token exchange; this refresher runs it under the store lock. */ refresh: (credential: OAuthCredential) => Promise; /** Refresh this long before expiry. */ marginMs?: number; /** Retry delays after transient refresh failures; the last delay repeats. */ retryDelaysMs?: readonly number[]; /** Set false to disable the proactive timer while keeping failure surfacing. */ proactive?: boolean; /** Host-side logger for refresh failures; receives the raw error, which never crosses RPC. */ onRefreshFailure?: (error: unknown) => void; } /** * A dead refresh token requires sign-in; an unreachable network does not. * Only failures carrying pi-ai's token-refresh markers qualify: a * credential-store or transport error must never condemn the stored token. * Upstream does not guarantee `invalid_grant`, so explicit expiry and * revocation rejections count as terminal too. */ export declare function isTerminalRefreshError(error: unknown): boolean; /** * Wrap pi-ai token resolution so every request-path refresh reports its * outcome, and rotate the stored credential ahead of expiry by running the * provider's `refresh` under the credential-store lock — the same pattern * pi-ai uses, with a margin-aware check instead of an expiry check. * * Success is never inferred from a resolved `getAuth` alone: pi-ai returns * the still-valid token without refreshing, so recovery requires evidence * that the credential which failed actually rotated. */ export declare class CodexTokenRefresher implements CodexUsageModels { private readonly models; private readonly credentials; private readonly sink; private readonly options; private timer; private inFlight; /** Invalidates every asynchronous scheduling decision on stop/dispose. */ private generation; /** Whether background scheduling is currently desired. */ private scheduling; private retryIndex; private refreshFailed; /** Expiry of the credential whose refresh failed; recovery must differ from it. */ private failedExpires; /** Upper bound for a failed lazy refresh when its credential could not be read. */ private failedBeforeOrAt; private disposed; constructor(models: CodexUsageModels, credentials: CredentialStore, sink: CodexRefreshSink, options: CodexTokenRefresherOptions); /** Resolve one fresh bearer token, surfacing terminal refresh failures. */ getAuth(providerId: string): Promise; /** Arm the proactive timer from the currently stored credential, if any. */ start(): Promise; /** Re-arm or stop the proactive timer from externally published auth state. */ observe(state: CodexAuthState): void; /** Stop the proactive timer and invalidate every pending scheduling decision. */ stop(): void; /** Stop all background work during plugin teardown. */ dispose(): void; private get margin(); private get proactive(); private isCurrent; private currentAuthGeneration; private isAuthCurrent; private delayFor; /** * Arm the timer for one expiry instant. A pending timer always re-evaluates * the store when it fires, so external arming never overrides an existing * schedule — least surprise for retry backoff and in-flight ticks. */ private arm; /** Re-arm from within a tick, replacing the consumed (or stale) timer. */ private rearm; private scheduleRetry; private setTimer; private clearTimer; /** Single-flight tick: concurrent fires share the running refresh. */ private tick; /** * After an interrupted tick, settle the schedule from ground truth: a live * credential re-arms, a missing credential stays quiet, a read hiccup * retries, and a known-dead credential keeps the timer stopped. */ private settle; private runTick; /** * Rotate the stored credential under the store lock once it enters the * margin. Reports whether a rotation actually happened so callers never * mistake an untouched credential for a successful refresh. */ private refreshStored; /** Recovery from a terminal failure requires evidence the failed credential rotated. */ private noteSuccess; /** A proven rotation clears any earlier terminal failure and notifies the sink. */ private noteRotated; /** Terminal failures stop the timer and surface reauth; transient ones do neither. */ private handleRefreshFailure; private readExpires; }