import { codexAccountUsesCreditsAfterLimit, codexUsageLimitResetAt } from "../account-credit-use";
import { nextCodexUsageQueryAt, nextQuotaQueryDelay, pruneRemovedCodexPoolUsageAccounts } from "../quota-query-backoff";
import { CODEX_PRIORITY_FAILBACK_REFRESH_MS } from "../account-priority";
import { codexQuotaHasFreshUsage } from "../quota-observation-freshness";
import { getCodexAccountCredential, getValidCodexToken, isCodexAccountGenerationLive, readCodexAccountRecord } from "../account-store";
import { getAccountQuota, getMainPolicyQuota, isCompleteCodexQuotaRecoverySnapshot } from "../quota";
import { isCodexAccountPaused } from "../account-pause";
import { hasSpendableCodexCredits } from "../quota-types";
import { reconcileMainCodexAccountRuntimeState } from "../account-lifecycle";
import { claimDueCodexQuotaRecoveryProbes, settleCodexQuotaRecoveryProbe } from "../routing";
import { readCodexTokens } from "../auth-collision";
import { isAccountNeedsReauth } from "../account-runtime-state";
import { getValidMainAccountToken, mainAccountTokenIsCurrent, mainAccountTokenQuotaWriter, MAIN_CODEX_ACCOUNT_ID, getMainAccountPlan } from "../main-account";
import { captureConfigGeneration, registerStateSweepAfterTick } from "../../lib/state-store-sweeper";
import { observeMainQuotaCredential, getMainQuotaCredentialGeneration, captureMainAccountIdentityGeneration, isMainAccountIdentityGenerationLive } from "../main-account-cache";
import { getMainAccountHardLockStatus, latestMainBlockingResetAt } from "../main-account-hard-lock";
import type { OcxConfig } from "../../types";
import { isCanonicalOpenAiForwardProvider, OPENAI_CODEX_PROVIDER_ID } from "../../providers/openai-tiers";
import { providerCodexAccountMode } from "../../providers/registry";
import { isSelectableCodexPoolAccount } from "../account-id";
import type { AdmissionLease } from "../../lib/admission";
import { tryAcquireNativeMainProfileClaim } from "../native-main-admission";
import { withNativeMainCredentialClaim, isNativeMainClaimUnavailable } from "./http";
import type { PoolQuotaResult } from "./pool-quota-probe";
import { fetchMainAccountInfoAttempt, fetchMainAccountInfo, prepareMainAccountInfoToken, MAIN_CACHE_TTL } from "./main-account-probe";
import { fetchPoolAccountQuota, PoolQuotaProbeBusyError, POOL_CACHE_TTL, POOL_QUOTA_REFRESH_CONCURRENCY } from "./pool-quota-probe";
import { getRuntimeConfig, configuredPoolAccount, mapWithConcurrency } from "./runtime-config";
import { trackBackgroundWork } from "../../lib/background-work";

let primeInFlight: Promise<void> | null = null;
let lastPriorityFailbackPrimeAt: number | undefined;
/**
 * Last prime attempt per pool account. A failed WHAM lookup stores no quota, so
 * without this the account stays "unknown" and every later prime trigger re-selects
 * it as stale and repeats the same failing request. Successful lookups are already
 * throttled by their stored updatedAt; this gives failures the same TTL backoff.
 *
 * Keyed by credential generation so a re-authentication, refresh, or account removal
 * retries immediately instead of waiting out a backoff earned by the old credential.
 */
const poolQuotaPrimeAttemptedAt = new Map<string, { generation: number; at: number }>();
let cooldownRecoveryInFlight: Promise<void> | null = null;

export async function runCodexCooldownRecoveryProbes(config: OcxConfig, now = Date.now()): Promise<void> {
  const openai = config.providers[OPENAI_CODEX_PROVIDER_ID];
  if (!openai
    || openai.disabled === true
    || !isCanonicalOpenAiForwardProvider(openai)
    || providerCodexAccountMode(OPENAI_CODEX_PROVIDER_ID, openai) !== "pool") return;
  if (cooldownRecoveryInFlight) return cooldownRecoveryInFlight;
  cooldownRecoveryInFlight = (async () => {
    const claims = claimDueCodexQuotaRecoveryProbes(config, POOL_QUOTA_REFRESH_CONCURRENCY, now);
    await mapWithConcurrency(claims, POOL_QUOTA_REFRESH_CONCURRENCY, async claim => {
      const account = configuredPoolAccount(config, claim.accountId);
      if (!account) {
        settleCodexQuotaRecoveryProbe(claim, false, {}, now);
        return;
      }
      try {
        const result = await fetchPoolAccountQuota(claim.accountId, true, account.plan,
          undefined, false, undefined, now);
        // Defence in depth: independent scopes are already excluded at the claim site.
        // Generic WHAM must never clear Reserve even if claim selection changes.
        const recovered = (claim.scope === undefined || claim.scope === "shared")
          && isCompleteCodexQuotaRecoverySnapshot(result.freshQuota ?? null, result.freshPlan ?? account.plan, codexAccountUsesCreditsAfterLimit(config, claim.accountId));
        settleCodexQuotaRecoveryProbe(claim, recovered, {
          credentialGeneration: result.freshCredentialGeneration,
        }, now);
      } catch {
        settleCodexQuotaRecoveryProbe(claim, false, {}, now);
      }
    });
  })().catch(() => {
    // Background recovery is best-effort; routing keeps the cooldown on failure.
  }).finally(() => { cooldownRecoveryInFlight = null; });
  return cooldownRecoveryInFlight;
}

const MAIN_CREDIT_RENEWAL_AGE_MS = 3 * 60_000;

/** Traffic headers renew usage, never the independent credit observation (#7377). */
function poolCreditRenewalDue(config: OcxConfig, accountId: string, now = Date.now()): boolean {
  const openai = config.providers[OPENAI_CODEX_PROVIDER_ID];
  if (!openai || openai.disabled === true || !isCanonicalOpenAiForwardProvider(openai)) return false;
  const mode = providerCodexAccountMode(OPENAI_CODEX_PROVIDER_ID, openai);
  // Direct may renew only explicitly bound stored accounts. Check queued work too:
  // credit opt-in alone does not authorize reads for every saved login.
  if (mode !== "pool" && (mode !== "direct"
    || !Object.values(config.codexAccountNamespaces ?? {}).includes(accountId))) return false;
  const account = configuredPoolAccount(config, accountId);
  if (!account || !codexAccountUsesCreditsAfterLimit(config, accountId)
    || isCodexAccountPaused(config, accountId) || isAccountNeedsReauth(accountId)) return false;
  const quota = getAccountQuota(accountId);
  if (codexUsageLimitResetAt(quota, account.plan, now) === undefined) return false;
  const observedAt = quota?.credits?.observedAt;
  if (observedAt !== undefined && Number.isFinite(observedAt)
    && now - observedAt >= 0 && now - observedAt < MAIN_CREDIT_RENEWAL_AGE_MS) return false;
  const attempt = poolQuotaPrimeAttemptedAt.get(accountId);
  return !attempt || attempt.generation !== readCodexAccountRecord(accountId)?.generation
    || now - attempt.at >= MAIN_CREDIT_RENEWAL_AGE_MS;
}

let mainHardLockRecoveryInFlight: Promise<void> | null = null;
/** `floor` and `resetBound` apply only while a predicted reset is (or just was) holding the lock. */
type MainRecoveryAttempt = { identity: number; credential: number; after: number; delay: number; floor?: number; resetBound?: number };
let mainHardLockRecoveryAttempt: MainRecoveryAttempt | undefined;
/** The identity/credential that already waited out its first hold interval. */
let mainHardLockHoldSeen: { identity: number; credential: number } | undefined;
const MAIN_HOLD_MIN_INTERVAL_MS = nextQuotaQueryDelay();

/** The predicted reset of a blocked main, or undefined when no future reset is known. */
function predictedMainResetHold(status: ReturnType<typeof getMainAccountHardLockStatus>): number | undefined {
  return status.state === "blocked" && status.resetAt !== undefined && status.resetAt > Date.now()
    ? status.resetAt : undefined;
}

/**
 * A hold read is due at its backoff deadline, pulled in to the latest known predicted reset (even
 * one that moved earlier or just passed) but never sooner than one base interval after the last read.
 */
function mainRecoveryDueAt(attempt: MainRecoveryAttempt, holdResetAt: number | undefined, config: OcxConfig): number {
  // Once a hold elapses the status omits its reset; current evidence may still name a revised one.
  const bound = holdResetAt ?? (attempt.resetBound === undefined ? undefined
    : Math.min(attempt.resetBound, latestMainBlockingResetAt(config) ?? Infinity));
  return bound === undefined ? attempt.after : Math.max(attempt.floor ?? 0, Math.min(attempt.after, bound));
}

/** First sight of a hold for this credential waits one base interval; transient retries do not re-arm it. */
function armMainResetHold(identity: number, credential: number, holdResetAt: number): void {
  const at = Date.now() + MAIN_HOLD_MIN_INTERVAL_MS;
  mainHardLockHoldSeen = { identity, credential };
  mainHardLockRecoveryAttempt = { identity, credential, delay: MAIN_HOLD_MIN_INTERVAL_MS, after: at, floor: at, resetBound: holdResetAt };
}

const mainHoldSeenFor = (identity: number, credential: number): boolean =>
  mainHardLockHoldSeen?.identity === identity && mainHardLockHoldSeen.credential === credential;

/** Metadata-only recovery on the existing sweep; failures retain the observed policy block. */
export async function runMainAccountHardLockRecovery(config: OcxConfig): Promise<void> {
  if (mainHardLockRecoveryInFlight) return mainHardLockRecoveryInFlight;
  /** Schedule a real credit observation while leaving actual-time admission unchanged. */
  const creditRecoveryNeeded = (): boolean => {
    const now = Date.now();
    const quota = getMainPolicyQuota();
    const observedAt = quota?.credits?.observedAt;
    // Positive evidence renews before expiry; unavailable evidence keeps discovery
    // eligible under the same pacing. Admission still requires fresh spendable funds.
    return codexAccountUsesCreditsAfterLimit(config, MAIN_CODEX_ACCOUNT_ID)
      && !isCodexAccountPaused(config, MAIN_CODEX_ACCOUNT_ID)
      && codexUsageLimitResetAt(quota, undefined, now) !== undefined
      && (!hasSpendableCodexCredits(quota, now)
        || (observedAt !== undefined && now - observedAt >= MAIN_CREDIT_RENEWAL_AGE_MS));
  };
  const status = getMainAccountHardLockStatus(config);
  if (status.state !== "blocked") mainHardLockHoldSeen = undefined;
  if (status.state !== "blocked" && !creditRecoveryNeeded()) { mainHardLockRecoveryAttempt = undefined; return; }
  if (isAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID)) return;
  // A predicted reset is a scheduling hint, never proof that the hard lock can be lifted. The
  // hold is rechecked on the capped WHAM schedule so an early reset redeemed elsewhere is
  // learned from metadata (#7312); a paused main receives no hold rechecks.
  const holdResetAt = predictedMainResetHold(status);
  if (holdResetAt !== undefined && isCodexAccountPaused(config, MAIN_CODEX_ACCOUNT_ID)) return;
  // Reconcile the owned physical credential before skipping work for an older one.
  reconcileMainCodexAccountRuntimeState();
  const physical = readCodexTokens();
  if (physical) observeMainQuotaCredential(physical.access_token, physical.account_id);
  const identity = captureMainAccountIdentityGeneration();
  const credential = getMainQuotaCredentialGeneration();
  const previous = mainHardLockRecoveryAttempt;
  if (holdResetAt !== undefined && !mainHoldSeenFor(identity, credential)) {
    armMainResetHold(identity, credential, holdResetAt);
    return;
  }
  const sameAttempt = previous?.identity === identity && previous.credential === credential;
  if (sameAttempt && holdResetAt !== undefined) previous.resetBound = holdResetAt;
  const queryAfter = nextCodexUsageQueryAt(`main:${captureConfigGeneration()}:${credential}`) ?? 0;
  if ((sameAttempt && mainRecoveryDueAt(previous, holdResetAt, config) > Date.now()) || queryAfter > Date.now()) return;
  const lease = tryAcquireNativeMainProfileClaim();
  if (!lease) return;
  mainHardLockRecoveryInFlight = (async () => {
    reconcileMainCodexAccountRuntimeState();
    if ((getMainAccountHardLockStatus(config).state !== "blocked" && !creditRecoveryNeeded())
      || isAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID)) return;
    const identityGeneration = captureMainAccountIdentityGeneration();
    const passive = getMainAccountHardLockStatus(config).state !== "blocked";
    let prepared: Awaited<ReturnType<typeof getValidMainAccountToken>>;
    try {
      // Refresh can require an exclusive credential claim: never hold WHAM's shared
      // claim while obtaining a valid token. The runtime lease spans both operations.
      prepared = await getValidMainAccountToken({ preserveReauth: true });
      if (!prepared) return;
      if (mainAccountTokenQuotaWriter(prepared)) {
        if (!mainAccountTokenIsCurrent(prepared)) return;
      } else observeMainQuotaCredential(prepared.accessToken, prepared.chatgptAccountId);
    } catch {
      // Native refresh retains its own grant-scoped refusal; global quarantine would outlive it.
      return;
    }
    const currentHold = predictedMainResetHold(getMainAccountHardLockStatus(config));
    if (isAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID)
      || !isMainAccountIdentityGenerationLive(identityGeneration)
      || (passive && !creditRecoveryNeeded())
      || (currentHold !== undefined && isCodexAccountPaused(config, MAIN_CODEX_ACCOUNT_ID))) return;
    const credential = getMainQuotaCredentialGeneration();
    // A hold that appeared during preparation is first seen now and waits its base interval.
    if (currentHold !== undefined && !mainHoldSeenFor(identityGeneration, credential)) {
      armMainResetHold(identityGeneration, credential, currentHold);
      return;
    }
    // Reconcile the physical bearer before consulting the previous credential's delay.
    if (previous?.identity === identityGeneration && previous.credential === credential
      && mainRecoveryDueAt(previous, currentHold, config) > Date.now()) return;
    const currentQueryAfter = nextCodexUsageQueryAt(`main:${captureConfigGeneration()}:${credential}`) ?? 0;
    if (currentQueryAfter > Date.now()) {
      mainHardLockRecoveryAttempt = { identity: identityGeneration, credential,
        delay: previous?.identity === identityGeneration && previous.credential === credential ? previous.delay : 0,
        after: currentQueryAfter };
      return;
    }
    const result = await fetchMainAccountInfoAttempt(true, 1, lease, false, false, false, config, { passive, preparedToken: prepared });
    // Never charge a replacement credential for a late result from its predecessor.
    if (isMainAccountIdentityGenerationLive(identityGeneration)
      && credential === getMainQuotaCredentialGeneration()) {
      const queryAfter = nextCodexUsageQueryAt(`main:${captureConfigGeneration()}:${credential}`) ?? 0;
      const previousDelay = previous?.identity === identityGeneration && previous.credential === credential
        ? previous.delay : 0;
      if (result.quotaRefresh) {
        const authStatus = result.quotaRefresh.status === "http_error"
          ? result.quotaRefresh.httpStatus : undefined;
        const transientAuth = (authStatus === 401 || authStatus === 403)
          && !result.terminalAuthFailure && !isAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID);
        if (transientAuth) mainHardLockRecoveryAttempt = undefined;
        else {
          const delay = nextQuotaQueryDelay(previousDelay || undefined);
          const hold = predictedMainResetHold(getMainAccountHardLockStatus(config));
          mainHardLockRecoveryAttempt = getMainAccountHardLockStatus(config).state === "blocked" || creditRecoveryNeeded()
            ? { identity: identityGeneration, credential, delay, after: Math.max(Date.now() + delay, queryAfter),
              ...(hold !== undefined ? { floor: Date.now() + MAIN_HOLD_MIN_INTERVAL_MS, resetBound: hold } : {}) }
            : undefined;
        }
      } else if (queryAfter > Date.now()) {
        mainHardLockRecoveryAttempt = { identity: identityGeneration, credential,
          delay: previousDelay, after: queryAfter };
      }
    }
  })().catch(() => {
    // Best-effort background metadata read; no cooldown/pause or policy clearing on failure.
  }).finally(() => {
    lease.release();
    mainHardLockRecoveryInFlight = null;
  });
  return mainHardLockRecoveryInFlight;
}

export function registerCodexCooldownRecoveryProbeWorker(config: OcxConfig): void {
  registerStateSweepAfterTick({
    name: "codex-cooldown-recovery",
    afterTick: () => {
      void runCodexCooldownRecoveryProbes(config);
      void runMainAccountHardLockRecovery(config);
      void primeCodexPoolQuotas(config, "credit-renewal");
    },
  });
}

export interface PrimeCodexPoolQuotasOptions {
  /** Test seams for proving fenced/recovery priming performs no native-main work. */
  reconcileMainAccount?: typeof reconcileMainCodexAccountRuntimeState;
  readMainTokens?: typeof readCodexTokens;
  fetchMainInfo?: typeof fetchMainAccountInfo;
}

let getValidPoolTokenForPrime = getValidCodexToken;

/** Test-only: inject a deterministic pre-dispatch credential outcome for quota priming. */
export function setCodexPoolQuotaTokenResolverForTests(
  resolver: typeof getValidCodexToken,
): () => void {
  const previous = getValidPoolTokenForPrime;
  getValidPoolTokenForPrime = resolver;
  return () => {
    if (getValidPoolTokenForPrime === resolver) getValidPoolTokenForPrime = previous;
  };
}

export function tryAcquireNativeMainPrimeLease(): AdmissionLease | null {
  return tryAcquireNativeMainProfileClaim();
}

/**
 * Best-effort prime of pool-account (and main) quota so the rotation engine has
 * real usage scores instead of leaving every account at the unknown sentinel.
 *
 * Quota is otherwise populated only from live upstream headers (an idle pool
 * account never serves traffic, so it never gets scored) or from the dashboard
 * WHAM fetch (a CLI-only user never opens it). Without priming, every account
 * stays unknown and auto-switch cannot move (see Phase 10). This runs at startup
 * and lazily before routing when the active account is unknown.
 *
 * Single-flight: concurrent callers share one pass instead of stampeding N WHAM
 * fetches. Per-fetch 8s timeouts and the 5-minute POOL_CACHE_TTL already bound
 * cost, so the worst case is one WHAM call per account per TTL window. Failures
 * are swallowed: a blocked WSL network must never crash startup or a request.
 */
export async function primeCodexPoolQuotas(
  config: OcxConfig,
  reason: string,
  options: PrimeCodexPoolQuotasOptions = {},
): Promise<void> {
  const openai = config.providers[OPENAI_CODEX_PROVIDER_ID];
  // Prune attempt markers for accounts that no longer exist BEFORE the eligibility
  // return. A removal that happens while the provider is disabled or out of pool mode
  // would otherwise leave a stale failure marker behind; restoring the same account id
  // within POOL_CACHE_TTL would then read that old failure as current and skip the
  // retry the restored credential is entitled to.
  const runtimeConfig = getRuntimeConfig(config);
  const configuredPoolIds = new Set((runtimeConfig.codexAccounts ?? []).map(account => account.id));
  pruneRemovedCodexPoolUsageAccounts(configuredPoolIds);
  for (const accountId of poolQuotaPrimeAttemptedAt.keys()) {
    if (!configuredPoolIds.has(accountId)) poolQuotaPrimeAttemptedAt.delete(accountId);
  }
  if (
    !openai
    || openai.disabled === true
    || !isCanonicalOpenAiForwardProvider(openai)
    || (providerCodexAccountMode(OPENAI_CODEX_PROVIDER_ID, openai) !== "pool"
      && !(reason === "credit-renewal"
        && providerCodexAccountMode(OPENAI_CODEX_PROVIDER_ID, openai) === "direct"))
  ) return;
  if (primeInFlight) return primeInFlight;
  // Count attempted passes, including failed reads; incoming traffic must not flood WHAM.
  if (reason === "priority-failback") {
    const now = Date.now();
    if (lastPriorityFailbackPrimeAt !== undefined
      && now - lastPriorityFailbackPrimeAt < CODEX_PRIORITY_FAILBACK_REFRESH_MS) return;
    lastPriorityFailbackPrimeAt = now;
  }
  // Tracked because most callers do not await it, yet it holds the native-main claim
  // database open; whoever removes that directory settles it first (see background-work).
  primeInFlight = trackBackgroundWork(`codex-pool-quota-prime:${reason}`, (async () => {
    const pool = (runtimeConfig.codexAccounts ?? []).filter(isSelectableCodexPoolAccount);
    const stale = pool.filter(a => {
      if (reason === "credit-renewal") return poolCreditRenewalDue(runtimeConfig, a.id);
      const q = getAccountQuota(a.id);
      const observationStale = reason === "priority-failback" && q
        && !codexQuotaHasFreshUsage(q, a.plan, Date.now(), CODEX_PRIORITY_FAILBACK_REFRESH_MS);
      if (q && !observationStale) return Date.now() - q.updatedAt >= POOL_CACHE_TTL;
      // No stored quota: either never primed, or the last attempt failed. Retry only
      // once per TTL window so an unreachable or rejecting account cannot turn every
      // prime trigger into another upstream request.
      const lastAttempt = poolQuotaPrimeAttemptedAt.get(a.id);
      if (!lastAttempt) return true;
      // A newer credential invalidates the previous failure: retry without waiting.
      if (lastAttempt.generation !== readCodexAccountRecord(a.id)?.generation) return true;
      return Date.now() - lastAttempt.at >= POOL_CACHE_TTL;
    });
    if (reason === "credit-renewal" && stale.length === 0) return;
    const primeMain = async () => {
      // Main renewal owns its physical-identity claims and passive probe separately.
      if (reason === "credit-renewal") return;
      const mainLease = tryAcquireNativeMainPrimeLease();
      if (!mainLease) return;
      try {
        try {
          // The Codex RPC owns an exclusive claim; finish it before WHAM's shared claim.
          const preparedToken = await prepareMainAccountInfoToken();
          await withNativeMainCredentialClaim(async () => {
            // Keep one local owner and one cross-process reader from physical
            // identity reconciliation through WHAM and all quota publication.
            if (preparedToken) {
              if (!mainAccountTokenIsCurrent(preparedToken)) return;
            } else (options.reconcileMainAccount ?? reconcileMainCodexAccountRuntimeState)();
            const quota = getAccountQuota(MAIN_CODEX_ACCOUNT_ID);
            const observationStale = reason === "priority-failback" && quota
              && !codexQuotaHasFreshUsage(quota, getMainAccountPlan(), Date.now(), CODEX_PRIORITY_FAILBACK_REFRESH_MS);
            if (quota && (reason !== "priority-failback"
              || (!observationStale && Date.now() - quota.updatedAt < MAIN_CACHE_TTL))) return;
            if (!(options.readMainTokens ?? readCodexTokens)()
              && !(preparedToken && mainAccountTokenIsCurrent(preparedToken))) return;
            const bypassCachedQuota = !!observationStale;
            if (options.fetchMainInfo) await options.fetchMainInfo(bypassCachedQuota);
            // Cache bypass is passive observation, never an explicit reauthentication recovery.
            else await fetchMainAccountInfoAttempt(bypassCachedQuota, 1, mainLease, true,
              false, false, config, { preparedToken });
          });
        } catch (error) {
          if (!isNativeMainClaimUnavailable(error)) throw error;
        }
      } finally {
        mainLease.release();
      }
    };
    try {
      await Promise.allSettled([
        primeMain(),
        mapWithConcurrency(stale, POOL_QUOTA_REFRESH_CONCURRENCY, async a => {
          if (!getCodexAccountCredential(a.id)) return;
          if (reason === "credit-renewal" && !poolCreditRenewalDue(getRuntimeConfig(config), a.id)) return;
          let result: PoolQuotaResult;
          try {
            const quota = getAccountQuota(a.id);
            const bypassCachedQuota = reason === "credit-renewal" || (reason === "priority-failback" && quota !== null
              && !codexQuotaHasFreshUsage(quota, a.plan, Date.now(), CODEX_PRIORITY_FAILBACK_REFRESH_MS));
            result = await fetchPoolAccountQuota(a.id, bypassCachedQuota, a.plan, getValidPoolTokenForPrime,
              false, undefined, undefined, false, reason === "credit-renewal"
                ? generation => isCodexAccountGenerationLive(a.id, generation)
                  && poolCreditRenewalDue(getRuntimeConfig(config), a.id)
                : undefined);
          } catch (error) {
            // Local quota-flight saturation proves no WHAM request existed for this account.
            // Consume it per item so sibling workers remain inside the shared prime lifetime.
            if (error instanceof PoolQuotaProbeBusyError) return;
            throw error;
          }
          // Only the data-plane function knows whether upstream dispatch began. Any
          // cache hit, credential deferral, or local admission failure remains eligible.
          const attempted = result.quotaProbeAttempted;
          if (!attempted) return;
          if (!configuredPoolAccount(getRuntimeConfig(config), a.id)) {
            poolQuotaPrimeAttemptedAt.delete(a.id);
            return;
          }
          poolQuotaPrimeAttemptedAt.set(a.id, {
            // getValidCodexToken may rotate the credential before WHAM is sent.
            // Bind the backoff to the generation that actually made the request;
            // otherwise the next prime sees a false generation change and retries
            // the same failed WHAM call immediately.
            generation: attempted.credentialGeneration,
            at: attempted.at,
          });
        }),
      ]);
    } catch {
      // Priming is best-effort; never propagate.
    }
    if (process.env.OPENCODEX_DEBUG_QUOTA === "1") {
      console.warn(`[codex-quota] prime done (reason=${reason}, pool=${pool.length}, refreshed=${stale.length})`);
    }
  })().finally(() => { primeInFlight = null; }));
  return primeInFlight;
}

/** Test-only: drop any in-flight prime pass so a leaked single-flight promise
 * from another suite cannot coalesce into the next prime. */
export function clearCodexQuotaPrimeState(): void {
  primeInFlight = null;
  lastPriorityFailbackPrimeAt = undefined;
  poolQuotaPrimeAttemptedAt.clear();
  getValidPoolTokenForPrime = getValidCodexToken;
}

/** Test-only: drop the shared single-flight promise while keeping the per-account
 * failure backoff, so a test can trigger a second real prime pass and still observe
 * the throttle a production caller would see. */
export function clearCodexQuotaPrimeSingleFlightForTests(): void {
  primeInFlight = null;
  lastPriorityFailbackPrimeAt = undefined;
}

/** Test-only reset for the worker-level single-flight. */
export function clearCodexCooldownRecoveryProbeState(): void {
  cooldownRecoveryInFlight = null;
}
