UNPKG

@code-yeongyu/senpi

Version:

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

79 lines • 3.6 kB
/** * Pure tier-policy and probe-schedule decisions for hint-aware 429 retry. * No timers, no I/O — all time values are injected by the caller. */ export function classifyRateLimitedWait(hintMs, settings) { if (hintMs === undefined) return "no-hint-fast-fallback"; if (hintMs <= settings.hintedWaitCapMs) return "tier1-in-turn"; if (hintMs < settings.probeBackMaxMs) return "tier2-fallback-probe-back"; return "tier3-fallback-only"; } export function probeBackSchedule(hintMs, nowMs) { return { firstAtMs: nowMs + Math.ceil(hintMs / 2), deadlineMs: nowMs + hintMs }; } export function nextInTurnDelayMs(state, hintMs, baseDelayMs, hintedWaitCapMs, nowMs) { // Every same-model 429 wait is floored by the exponential schedule so repeated // short provider hints cannot pin the retry cadence at a few milliseconds and // hammer an already rate-limited model. Longer hints still win. const exponentialFloorMs = baseDelayMs * 2 ** (state.attempt - 1); // Phase: half-used -> consecutive 429 with deadline sleep if (state.probePhase === "half-used") { const deadlineMs = hintMs !== undefined ? nowMs + hintMs : (state.hintDeadlineMs ?? nowMs); const remaining = Math.max(0, deadlineMs - nowMs); const delayMs = Math.max(remaining, exponentialFloorMs); const newCumulative = state.cumulativeHintedWaitMs + delayMs; return { delayMs, probePhase: "done", hintDeadlineMs: deadlineMs, cumulativeHintedWaitMs: newCumulative, demoteToProbeBack: newCumulative > hintedWaitCapMs, }; } // Phase: idle -> first hinted 429, probe at hint/2 if (state.probePhase === "idle" && hintMs !== undefined) { const delay = Math.max(Math.ceil(hintMs / 2), exponentialFloorMs); const deadlineMs = nowMs + hintMs; const newCumulative = state.cumulativeHintedWaitMs + delay; return { delayMs: delay, probePhase: "half-used", hintDeadlineMs: deadlineMs, cumulativeHintedWaitMs: newCumulative, demoteToProbeBack: newCumulative > hintedWaitCapMs, }; } // Phase: done (or idle without hint = non-429 / no-hint fallback) -> exponential, // raised further only by a hint longer than the floor const delay = Math.max(hintMs ?? 0, exponentialFloorMs); return { delayMs: delay, probePhase: "done", hintDeadlineMs: state.hintDeadlineMs, cumulativeHintedWaitMs: state.cumulativeHintedWaitMs, demoteToProbeBack: false, }; } /** * Policy for a 429-class failure when no fallback candidate is usable: no-hint * (and tier1) failures retry in-turn on the exponential schedule, tier2 hints * retry in-turn with the wait clamped to the in-turn cap but floored by the same * exponential schedule, and only tier3 (probe-back-max or longer) hinted waits * stay terminal. */ export function degradeWithoutFallback(tier, hintMs, attempt, baseDelayMs, hintedWaitCapMs) { if (tier === "tier3-fallback-only") return { kind: "fail", hintMs: hintMs ?? 0 }; if (tier === "tier2-fallback-probe-back") { // Cap-clamped, but never below the exponential floor for this attempt. return { kind: "in-turn", delayMs: Math.max(Math.min(hintMs ?? hintedWaitCapMs, hintedWaitCapMs), baseDelayMs * 2 ** (attempt - 1)), }; } return { kind: "in-turn", delayMs: baseDelayMs * 2 ** (attempt - 1) }; } //# sourceMappingURL=hint-policy.js.map