import { rawAnthropicHeaderWindows } from "./anthropic-decision-windows";
import { clearDecisionAccountQuotas, decisionQuotaSinkActive } from "../../lib/decision-quota-sink";
import { bindRawDecisionWindows, publishAnthropicDecisionQuota } from "../quota-decision-publication";
import { parseAnthropicFamilyHeaders, mergeAnthropicFamilyWindows } from "./anthropic-family-headers";
import { anthropicModelQuotaFor, ANTHROPIC_PASSIVE_FAMILY_MAX_AGE_MS } from "../../oauth/anthropic-model-quota";
import { createHash } from "node:crypto";
import { getValidAccessTokenForAccount } from "../../oauth";
import { anthropicCredentialEvidenceKey, credentialGeneration, getAccountCredential, getAccountCredentialWithStatus, getAccountSet, loadAuthStore, type AuthStore } from "../../oauth/store";
import { isAnthropicInstanceId, type AnthropicInstanceId } from "../anthropic-instance-id";
import type { GenerationContext } from "../../lib/state-store-sweeper";
import { ACCOUNT_QUOTA_TTL_MS, toFiniteNumber } from "../quota-wire";
import { clearKiroAccountUsageState, hydrateKiroUsageVerdict, kiroPersistableVerdicts, reconcileKiroAccountUsageState } from "../kiro-usage";
import { kiroEvidenceIdentity, sanitizeKiroQuota, type KiroPersistedQuota } from "../kiro-account-state-disk";
import { cancelPendingAccountQuotaPersist, readPersistedAccountQuotaSnapshot, readPersistedKiroVerdicts, schedulePersistAccountQuotas } from "../account-quota-disk";
import type { ProviderAccount } from "../../oauth/types";
import { replaceCachedProviderQuotas } from "../quota-routing-cache";
import { getProviderRegistryEntry } from "../registry";
import { getProviderQuotaReportCache, hasQuotaRows, routingEvidence, setProviderQuotaReportCache } from "./report-cache";
import { isCanonicalCommandCodeBaseUrl, isCanonicalKimiCodeBaseUrl } from "./vendor-probes-key";
import { isDisplayOnlyQuota, isWeeklyAllowancePercent, type AccountQuotaMode, type ProviderQuota, type ProviderQuotaWindow, type QuotaFailureCode } from "../quota-types";
import type { OcxConfig, OcxProviderConfig } from "../../types";
import type { ResetNearQuota } from "../../routing/machine-weighted-routing";

/** Match oauth/index REFRESH_SKEW_MS — use stored access without refresh when still fresh. */
const ACCOUNT_TOKEN_SKEW_MS = 60_000;

/**
 * Anthropic and Kiro both report usage per CREDENTIAL, so every logged-in account can be
 * probed with its own bearer token — the active-account selection and the local usage log
 * are irrelevant here. Mirrors the Codex pool behaviour
 * (codex/auth-api.ts:fetchPoolAccountQuota), including a per-account TTL so N accounts cost
 * at most N upstream calls per window. `ACCOUNT_QUOTA_TTL_MS` lives in `quota-wire.ts`
 * because the Kiro exhaustion reader applies the same staleness bound.
 */
export type AccountQuotaCacheEntry = {
  ts: number;
  quota: ProviderQuota | null;
  /** Last probe failed (429 / network / expired login); still may hold last-good quota. */
  unavailable?: true;
  quotaFailure?: QuotaFailureCode;
  quotaFailureIsCurrent?: () => boolean;
  /** Private new-reader identity; never persisted or serialized. */
  identity?: string;
  /** Private Anthropic ownership fence, kept outside quota DTOs. */
  anthropicCredentialKey?: string;
  isCurrent?: () => boolean;
  /** Proof for the quota itself, which may have been retained from an earlier read. Memory only. */
  quotaOwnerIsCurrent?: () => boolean;
};

// Carried display bars keep their original clocks. Disk hydration has no live observation clocks.
const anthropicUsageClocks = new WeakMap<ProviderQuota, Pick<ResetNearQuota, "fiveHourObservedAt" | "weeklyObservedAt">>();

function stampAnthropicUsageObservation(
  quota: ProviderQuota | null, incoming?: ProviderQuota | null, previous?: ProviderQuota | null,
): ProviderQuota | null {
  if (!quota) return quota;
  const clocks = previous ? anthropicUsageClocks.get(previous) : undefined;
  if (!incoming) {
    if (previous && clocks && quota !== previous) {
      anthropicUsageClocks.set(quota, {
        fiveHourObservedAt: quota.fiveHourPercent !== undefined && quota.fiveHourPercent === previous.fiveHourPercent
          && quota.fiveHourResetAt === previous.fiveHourResetAt ? clocks.fiveHourObservedAt : undefined,
        weeklyObservedAt: quota.weeklyPercent !== undefined && quota.weeklyPercent === previous.weeklyPercent
          && quota.weeklyResetAt === previous.weeklyResetAt ? clocks.weeklyObservedAt : undefined,
      });
    }
    return quota;
  }
  anthropicUsageClocks.set(quota, {
    fiveHourObservedAt: incoming.fiveHourPercent !== undefined ? incoming.updatedAt : clocks?.fiveHourObservedAt,
    weeklyObservedAt: incoming.weeklyPercent === undefined && incoming.weeklyResetAt === undefined ? clocks?.weeklyObservedAt
      : incoming.weeklyPercent !== undefined && incoming.weeklyResetAt !== undefined ? incoming.updatedAt : undefined,
  });
  return quota;
}
const validReset = (resetAt: unknown): resetAt is number => typeof resetAt === "number"
  && Number.isFinite(resetAt) && resetAt > 0 && Number.isFinite(new Date(resetAt).getTime());

/** Persisted rows validate only the outer quota object, so window data may be malformed. Null when unchanged. */
function retainedQuotaWindows(value: unknown, now: number): ProviderQuotaWindow[] | null {
  const windows: ProviderQuotaWindow[] = Array.isArray(value) ? value : [];
  const retained: ProviderQuotaWindow[] = [];
  let changed = !Array.isArray(value);
  for (const window of windows) {
    if (!window || typeof window !== "object" || typeof window.label !== "string" || !window.label.trim()
      || typeof window.percent !== "number" || !Number.isFinite(window.percent)
      || window.percent < 0 || window.percent > 100) {
      changed = true;
      continue;
    }
    if ((window.passiveObservedAt !== undefined && (typeof window.passiveObservedAt !== "number"
        || !Number.isFinite(window.passiveObservedAt) || window.passiveObservedAt <= 0 || window.passiveObservedAt > now
        || now - window.passiveObservedAt >= ANTHROPIC_PASSIVE_FAMILY_MAX_AGE_MS))
      || validReset(window.resetAt) && window.resetAt <= now) {
      changed = true;
      continue;
    }
    const normalized = { ...window };
    if (window.resetAt !== undefined && !validReset(window.resetAt)) delete normalized.resetAt;
    // A hand-edited share outside (0, 100] is dropped; the measurement itself stays usable.
    if (window.weeklyAllowancePercent !== undefined && !isWeeklyAllowancePercent(window.weeklyAllowancePercent)) {
      delete normalized.weeklyAllowancePercent;
    }
    const rewritten = "resetAt" in window !== "resetAt" in normalized
      || "weeklyAllowancePercent" in window !== "weeklyAllowancePercent" in normalized;
    retained.push(rewritten ? normalized : window);
    if (rewritten) changed = true;
  }
  return changed ? retained : null;
}

/** Expired measurements become unknown; missing reset evidence never implies a fresh allowance. */
export function normalizeAnthropicQuota(quota: ProviderQuota | null | undefined, now: number): ProviderQuota | null {
  if (!quota) return null;
  let result = quota;
  for (const [percent, reset] of [
    ["fiveHourPercent", "fiveHourResetAt"],
    ["weeklyPercent", "weeklyResetAt"],
    ["monthlyPercent", "monthlyResetAt"],
  ] as const) {
    const resetAt = quota[reset];
    if (resetAt === undefined) continue;
    const valid = validReset(resetAt);
    if (valid && resetAt > now) continue;
    if (result === quota) result = { ...quota };
    if (valid) delete result[percent];
    delete result[reset];
  }
  for (const field of ["customWindows", "displayWindows"] as const) {
    // Read the caller's original object; `result` may already be a copy with other fields rewritten.
    if (quota[field] === undefined) {
      continue;
    }
    const retained = retainedQuotaWindows(quota[field], now);
    if (!retained) {
      continue;
    }
    if (result === quota) {
      result = { ...quota };
    }
    if (retained.length) {
      result[field] = retained;
    } else {
      delete result[field];
    }
  }
  return hasQuotaRows(result) ? result : null;
}

export function withoutAnthropicMonthlyFallback(quota: ProviderQuota | null): ProviderQuota | null {
  if (!quota) return null;
  const { monthlyPercent, monthlyResetAt, ...plan } = quota;
  return hasQuotaRows(plan) ? plan : null;
}

function hydrateAnthropicQuota(quota: ProviderQuota, now: number): ProviderQuota | null {
  // Older mixed rows establish plan presence even when those meters have since expired.
  const reportedPlan = typeof quota.fiveHourPercent === "number" || typeof quota.weeklyPercent === "number"
    || [quota.customWindows, quota.displayWindows].some(rows => Array.isArray(rows) && rows.some(row => typeof row?.percent === "number"));
  const normalized = normalizeAnthropicQuota(quota, now);
  return reportedPlan && normalized?.monthlyPercent !== undefined ? withoutAnthropicMonthlyFallback(normalized) : normalized;
}

function isAnthropicAccountCacheKey(key: string): boolean {
  const separator = key.indexOf("\0");
  return separator > 0 && isAnthropicInstanceId(key.slice(0, separator));
}

export const accountQuotaCache = new Map<string, AccountQuotaCacheEntry>();
// Order fallback transitions independently of display-cache expiry and reset-grant eviction.
const anthropicQuotaObservations = new Map<string, { planCommit: number; monthlyDispatch: number }>();
let nextAnthropicQuotaRevision = 1;

export function reserveAnthropicQuotaDispatch(): number {
  return nextAnthropicQuotaRevision++;
}

/** A monthly sample yields to any plan evidence committed since dispatch. Any probe yields to a newer monthly dispatch. */
export function anthropicQuotaDispatchIsCurrent(key: string, dispatch: number, monthly: boolean): boolean {
  const previous = anthropicQuotaObservations.get(key);
  return !previous || (dispatch >= previous.monthlyDispatch && (!monthly || dispatch > previous.planCommit));
}

/** Record monthly evidence and retirement only at an owned commit. */
export function commitAnthropicAccountQuota(key: string, entry: AccountQuotaCacheEntry, retiresMonthly: boolean, dispatch?: number): void {
  accountQuotaCache.set(key, entry);
  if (retiresMonthly || dispatch !== undefined) {
    const previous = anthropicQuotaObservations.get(key);
    anthropicQuotaObservations.set(key, {
      planCommit: retiresMonthly ? nextAnthropicQuotaRevision++ : previous?.planCommit ?? 0,
      monthlyDispatch: !retiresMonthly && dispatch !== undefined ? dispatch : previous?.monthlyDispatch ?? 0,
    });
  }
}

export let explicitAccountEpoch = 0;
let allAccountQuotaEpoch = 0;
const providerAccountQuotaEpochs = new Map<string, number>();
/** Instance-owned cache clears do not revoke an unrelated account's probe. */
export function captureProviderAccountQuotaEpoch(provider: string): number {
  return allAccountQuotaEpoch + (providerAccountQuotaEpochs.get(provider) ?? 0);
}

/**
 * Seed the cache from the last run, once.
 *
 * Without this a restart forgets every measurement, so the pool opens its next turn with
 * no idea which account has room — the exact blindness pre-dispatch selection exists to
 * remove. A hydrated row is still subject to the ordinary TTL, so it orders the first
 * request and is replaced by a live probe immediately after.
 */
let diskHydrated = false;
export function hydrateAccountQuotaCache(): void {
  if (diskHydrated) return;
  diskHydrated = true;
  const now = Date.now();
  const store = loadAuthStore();
  const liveKiro = new Map<string, ProviderAccount>(store.kiro?.accounts.map(account =>
    [accountCacheKey("kiro", account.id), account]) ?? []);
  const { rows, anthropicCredentialKeys } = readPersistedAccountQuotaSnapshot(now);
  for (const [key, quota] of rows) {
    if (key.startsWith("kiro\0")) {
      const account = liveKiro.get(key);
      if (!account || (quota as KiroPersistedQuota).identity !== kiroEvidenceIdentity(account)
        || typeof quota.monthlyPercent !== "number" || !Number.isFinite(quota.monthlyPercent)
        || quota.monthlyPercent < 0 || quota.monthlyPercent > 100
        || now - quota.updatedAt >= ACCOUNT_QUOTA_TTL_MS
        || (quota.monthlyResetAt !== undefined && (typeof quota.monthlyResetAt !== "number"
          || !Number.isFinite(quota.monthlyResetAt) || quota.monthlyResetAt <= now
          || !Number.isFinite(new Date(quota.monthlyResetAt).getTime())))) continue;
    }
    // Disk stores observation time, not the Anthropic usage probe's clock.
    if (!accountQuotaCache.has(key)) {
      const anthropic = isAnthropicAccountCacheKey(key);
      // Also read and strip inline ownership from snapshots written by the earlier writer.
      const { anthropicCredentialKey: inlineKey, ...anthropicQuota } = quota as ProviderQuota & { anthropicCredentialKey?: unknown };
      const metadataKey = anthropicCredentialKeys?.[key];
      const anthropicCredentialKey = metadataKey === undefined ? inlineKey : metadataKey;
      if (anthropic && anthropicCredentialKey !== undefined
        && (typeof anthropicCredentialKey !== "string" || !anthropicCacheKeyIsCurrent(key, anthropicCredentialKey, store))) continue;
      accountQuotaCache.set(key, {
        ts: anthropic ? 0 : quota.updatedAt,
        quota: anthropic ? hydrateAnthropicQuota(anthropicQuota, now)
          : key.startsWith("kiro\0") ? sanitizeKiroQuota(quota) : quota,
        ...(anthropic && typeof anthropicCredentialKey === "string" ? { anthropicCredentialKey } : {}),
        ...(key.startsWith("kiro\0") ? { identity: (quota as KiroPersistedQuota).identity } : {}),
      });
    }
  }
  for (const [key, verdict] of readPersistedKiroVerdicts(now)) {
    const account = liveKiro.get(key);
    if (account && verdict.identity === kiroEvidenceIdentity(account)) hydrateKiroUsageVerdict(key, verdict, account);
  }
}

export function persistAccountQuotaCache(): void {
  schedulePersistAccountQuotas(function* () {
    const now = Date.now();
    const store = loadAuthStore();
    const liveKiro = new Map<string, ProviderAccount>(store.kiro?.accounts.map(account =>
      [accountCacheKey("kiro", account.id), account]) ?? []);
    for (const [key, entry] of accountQuotaCache) {
      if (entry.quota && isDisplayOnlyQuota(entry.quota)) continue;
      const quota = isAnthropicAccountCacheKey(key) ? normalizeAnthropicQuota(entry.quota, now) : entry.quota;
      if (quota && key.startsWith("kiro\0")) {
        const account = liveKiro.get(key);
        if (account && entry.identity === kiroEvidenceIdentity(account))
          yield [key, { ...sanitizeKiroQuota(quota), identity: entry.identity } as KiroPersistedQuota] as [string, KiroPersistedQuota];
      } else if (quota && entry.anthropicCredentialKey !== undefined) {
        if (anthropicCacheKeyIsCurrent(key, entry.anthropicCredentialKey, store)) {
          yield [key, quota, entry.anthropicCredentialKey] as [string, ProviderQuota, string];
        }
      } else if (quota) yield [key, quota] as [string, ProviderQuota];
    }
  }, () => kiroPersistableVerdicts());
}
export const accountQuotaInflight = new Map<string, Promise<AccountQuotaCacheEntry>>();
let lastReconciledGeneration = 0;
let liveAccountQuotaKeys = new Set<string>();
let liveProviderQuotaKeys = new Set<string>();

export function mayCommitAccountQuotaKey(key: string, writerGeneration: number): boolean {
  return writerGeneration >= lastReconciledGeneration || liveAccountQuotaKeys.has(key);
}

export function mayCommitProviderQuotaKey(key: string, writerGeneration: number): boolean {
  return writerGeneration >= lastReconciledGeneration || liveProviderQuotaKeys.has(key);
}

export interface ProviderAccountQuota {
  accountId: string;
  quota: ProviderQuota | null;
  /** Set when the probe could not reach upstream (expired login, 429, network). */
  unavailable?: true;
  quotaFailure?: QuotaFailureCode;
  quotaFailureIsCurrent?: () => boolean;
  isCurrent?: () => boolean;
}

/** Providers whose per-account quota can be probed. Extend as other OAuth APIs are covered. */
export function supportsPerAccountQuota(provider: string): boolean {
  return isAnthropicInstanceId(provider) || provider === "kiro" || provider === "google-antigravity"
    || explicitAccountReader(provider);
}

export function explicitAccountReader(provider: string): boolean {
  return provider === "xai" || provider === "cursor" || provider === "kimi" || provider === "command-code"
    || provider === "devin" || provider === "workbuddy-ai" || provider === "dsh-account";
}

export function providerOAuthAccountQuotaMode(provider: string): AccountQuotaMode {
  return hasPassiveAccountQuota(provider) ? "passive" : supportsPerAccountQuota(provider) ? "probe" : "unsupported";
}

export function accountCacheKey(provider: string, accountId: string): string {
  return `${provider}\u0000${accountId}`;
}

function anthropicCacheKeyIsCurrent(key: string, credentialKey: string, store: AuthStore = loadAuthStore()): boolean {
  const separator = key.indexOf("\0");
  const row = store[key.slice(0, separator)]?.accounts.find(account => account.id === key.slice(separator + 1));
  return !!row && anthropicCredentialEvidenceKey(row) === credentialKey;
}

/**
 * Synchronous last-good per-account quota read for routing. Never probes the network.
 * Returns null when nothing is cached (or the cached row has no bars).
 */
export function getCurrentAccountQuotaEntry(provider: string, accountId: string): AccountQuotaCacheEntry | undefined {
  const key = accountCacheKey(provider, accountId);
  const entry = accountQuotaCache.get(key);
  if (entry?.isCurrent?.() === false) return undefined;
  if (provider === "kiro") {
    const account = getAccountSet("kiro")?.accounts.find(row => row.id === accountId);
    if (!account || entry?.identity !== kiroEvidenceIdentity(account)) return undefined;
  }
  if (isAnthropicInstanceId(provider)) {
    if (entry?.quotaOwnerIsCurrent !== entry?.isCurrent && entry?.quotaOwnerIsCurrent?.() === false) return undefined;
    if (entry?.anthropicCredentialKey !== undefined && !anthropicCacheKeyIsCurrent(key, entry.anthropicCredentialKey)) return undefined;
  }
  return entry;
}

export function getCachedProviderAccountQuota(provider: string, accountId: string): ProviderQuota | null {
  const entry = getCurrentAccountQuotaEntry(provider, accountId);
  return isAnthropicInstanceId(provider) ? normalizeAnthropicQuota(entry?.quota, Date.now()) : entry?.quota ?? null;
}

/** Both refresh entry points timestamp new meters and keep carried meters under their original owner. */
export function anthropicAccountProbeEvidence(
  instance: AnthropicInstanceId, accountId: string, now: number,
  result?: { quota: ProviderQuota | null; credentialKey: string; isCurrent: () => boolean },
): Pick<AccountQuotaCacheEntry, "quota" | "quotaOwnerIsCurrent" | "anthropicCredentialKey"> {
  if (result?.quota) return {
    quota: stampAnthropicUsageObservation(normalizeAnthropicQuota(result.quota, now), result.quota),
    quotaOwnerIsCurrent: result.isCurrent,
    anthropicCredentialKey: result.credentialKey,
  };
  const retained = getCurrentAccountQuotaEntry(instance, accountId);
  const previous = retained?.quota ?? null;
  const meters = result ? withoutAnthropicMonthlyFallback(previous) : previous;
  const quota = stampAnthropicUsageObservation(normalizeAnthropicQuota(meters, now), undefined, previous);
  if (!quota) return { quota: null };
  return {
    quota,
    quotaOwnerIsCurrent: retained?.quotaOwnerIsCurrent,
    anthropicCredentialKey: retained?.anthropicCredentialKey,
  };
}

/** Client-facing reads require positive ownership. Kiro's persisted login identity is rechecked by the cache reader. */
export function getCredentialOwnedAccountQuota(provider: string, accountId: string): ProviderQuota | null {
  hydrateAccountQuotaCache();
  const entry = accountQuotaCache.get(accountCacheKey(provider, accountId));
  if (provider !== "kiro" && entry?.quotaOwnerIsCurrent?.() !== true) return null;
  const quota = getCachedProviderAccountQuota(provider, accountId);
  if (provider === "kiro" && quota && Date.now() - quota.updatedAt >= ACCOUNT_QUOTA_TTL_MS) return null;
  return quota;
}

export function getAnthropicResetNearQuota(instance: AnthropicInstanceId, accountId: string, now: number): ResetNearQuota | null {
  const entry = getCurrentAccountQuotaEntry(instance, accountId);
  const stored = entry?.anthropicCredentialKey === undefined ? null : entry.quota;
  const clocks = stored && anthropicUsageClocks.get(stored);
  if (!clocks) return null;
  const quota = normalizeAnthropicQuota(stored, now);
  return quota ? { ...quota, ...clocks } : null;
}

/** Test-only: seed or clear the per-account quota cache without probing upstream. */
export function setCachedProviderAccountQuotaForTests(
  provider: string,
  accountId: string,
  quota: ProviderQuota | null,
): void {
  const key = accountCacheKey(provider, accountId);
  if (quota === null) {
    accountQuotaCache.delete(key);
    return;
  }
  const anthropic = isAnthropicInstanceId(provider);
  const account = provider === "kiro" || anthropic ? getAccountSet(provider)?.accounts.find(row => row.id === accountId) : undefined;
  if (anthropic) stampAnthropicUsageObservation(quota, quota);
  accountQuotaCache.set(key, { ts: Date.now(), quota,
    ...(account && (anthropic ? { anthropicCredentialKey: anthropicCredentialEvidenceKey(account) }
      : { identity: kiroEvidenceIdentity(account) })) });
}

/** Unified headers report utilization fractions and epoch-second reset times. */
function anthropicHeaderResetAt(value: string | null): number | undefined {
  const seconds = toFiniteNumber(value);
  if (seconds === undefined || seconds <= 0) return undefined;
  const timestamp = seconds * 1000;
  return Number.isFinite(new Date(timestamp).getTime()) ? timestamp : undefined;
}

export function parseAnthropicRateLimitHeaders(headers: Headers, status?: number): ProviderQuota | null {
  const fiveHourPercent = normalizeUtilizationFraction(headers.get("anthropic-ratelimit-unified-5h-utilization"));
  const weeklyPercent = normalizeUtilizationFraction(headers.get("anthropic-ratelimit-unified-7d-utilization"));
  const customWindows = parseAnthropicFamilyHeaders(headers, Date.now(), status);
  if (fiveHourPercent === undefined && weeklyPercent === undefined && !customWindows.length) return null;
  const fiveHourResetAt = anthropicHeaderResetAt(headers.get("anthropic-ratelimit-unified-5h-reset"));
  const weeklyResetAt = anthropicHeaderResetAt(headers.get("anthropic-ratelimit-unified-7d-reset"));
  return {
    ...(fiveHourPercent !== undefined ? { fiveHourPercent } : {}),
    ...(fiveHourPercent !== undefined && fiveHourResetAt !== undefined ? { fiveHourResetAt } : {}),
    ...(weeklyPercent !== undefined ? { weeklyPercent } : {}),
    ...(weeklyPercent !== undefined && weeklyResetAt !== undefined ? { weeklyResetAt } : {}),
    ...(customWindows.length ? { customWindows } : {}),
    updatedAt: Date.now(),
  };
}

/** Reject unknown scales; round fraction conversion for persisted/displayed percentages. */
function normalizeUtilizationFraction(value: string | null): number | undefined {
  const numeric = toFiniteNumber(value);
  if (numeric === undefined || numeric < 0 || numeric > 1) return undefined;
  return Math.round(numeric * 10_000) / 100;
}

/**
 * Merge serving-account observations without advancing the usage probe's clock or
 * erasing model-specific windows. The caller owns credential attribution; this guard
 * prevents a retired account key from being revived by an older config generation.
 */
export function recordAnthropicAccountQuotaFromHeadersForInstance(
  instance: AnthropicInstanceId,
  accountId: string,
  headers: Headers,
  writerGeneration: number,
  status?: number,
  model?: string,
  decisionCredentialGeneration?: string,
  sendCredentialKey?: string,
): void {
  if (!accountId) return;
  const key = accountCacheKey(instance, accountId);
  const { observeAnthropicFamilyQuota, clearAnthropicRequestedFamilyQuota } = anthropicModelQuotaFor(instance);
  if (!mayCommitAccountQuotaKey(key, writerGeneration)) return;
  const account = getAccountSet(instance)?.accounts.find(row => row.id === accountId);
  const credentialKey = account && anthropicCredentialEvidenceKey(account);
  if (sendCredentialKey !== undefined && sendCredentialKey !== credentialKey) return;
  if (status !== undefined && status >= 200 && status < 300) clearAnthropicRequestedFamilyQuota(accountId, model);
  const observed = parseAnthropicRateLimitHeaders(headers, status);
  if (!observed) return;
  // Hydrate before writing, for the same reason `recordPassiveAccountQuota` does: this write
  // arrives unprompted from the request path, and `persistAccountQuotaCache` serializes the
  // whole map. Landing before any reader has hydrated would persist this single row and erase
  // every other provider's saved row.
  hydrateAccountQuotaCache();
  const candidate = accountQuotaCache.get(key);
  const generation = account && credentialGeneration(account.credential);
  const previous = candidate?.isCurrent?.() === false
    || (candidate?.quotaOwnerIsCurrent !== candidate?.isCurrent && candidate?.quotaOwnerIsCurrent?.() === false)
    || candidate?.anthropicCredentialKey !== undefined && candidate.anthropicCredentialKey !== credentialKey ? undefined : candidate;
  if (observed.customWindows) observeAnthropicFamilyQuota(accountId, observed.customWindows, observed.updatedAt);
  const quota = {
    ...normalizeAnthropicQuota(previous?.quota, observed.updatedAt), ...observed,
    ...(observed.customWindows ? { customWindows: mergeAnthropicFamilyWindows(previous?.quota?.customWindows, observed.customWindows) } : {}),
  };
  commitAnthropicAccountQuota(key, {
    ...previous,
    ...(credentialKey ? { anthropicCredentialKey: credentialKey, isCurrent: undefined } : {}),
    // This merged snapshot can include unbound headers or bars carried forward from disk.
    quotaOwnerIsCurrent: undefined,
    // Headers do not prove that the last usage probe succeeded.
    ts: previous?.ts ?? 0,
    quota: stampAnthropicUsageObservation(
      normalizeAnthropicQuota(withoutAnthropicMonthlyFallback(quota), observed.updatedAt), observed, previous?.quota,
    ),
  }, true);
  // Advisory evidence is published for the primary pool only; Pool 2 stays unknown to decisions.
  if (instance === "anthropic" && generation && generation === decisionCredentialGeneration && decisionQuotaSinkActive()) {
    bindRawDecisionWindows(observed, rawAnthropicHeaderWindows(headers, observed.updatedAt));
    publishAnthropicDecisionQuota(accountId, generation, observed, true);
  }
  persistAccountQuotaCache();
}

/** Legacy recorder: config/roster generation is numeric and distinct from bearer ownership. */
export function recordAnthropicAccountQuotaFromHeaders(
  accountId: string, headers: Headers, writerGeneration: number, status?: number, model?: string,
  decisionCredentialGeneration?: string,
): void {
  recordAnthropicAccountQuotaFromHeadersForInstance("anthropic", accountId, headers, writerGeneration, status, model, decisionCredentialGeneration);
}

/**
 * Providers whose per-account quota is OBSERVED in-band, never probed.
 *
 * Deliberately separate from `supportsPerAccountQuota` rather than folded into it. That
 * predicate gates explicit upstream readers. Meta publishes no quota endpoint, so it
 * remains a cache-only observation even when every probe reader is account-scoped.
 */
export function hasPassiveAccountQuota(provider: string): boolean {
  return provider === "meta-muse";
}

/**
 * Record a quota observed in-band on a streaming turn.
 *
 * The CALLER captures `writerGeneration` when it resolves the serving credential, not
 * this function at write time. A streaming turn is a long await, and a generation
 * captured immediately before the write cannot see a config or account change that
 * happened EARLIER in the same turn — which is exactly the case the fence exists for.
 */
export function recordPassiveAccountQuota(
  provider: string,
  accountId: string,
  quota: ProviderQuota,
  writerGeneration: number,
): void {
  if (!hasPassiveAccountQuota(provider) || !accountId) return;
  const key = accountCacheKey(provider, accountId);
  if (!mayCommitAccountQuotaKey(key, writerGeneration)) return;
  // Hydrate BEFORE writing, not only on the read path. `persistAccountQuotaCache`
  // serializes the whole in-memory map, so a passive write that lands before anything
  // has read the cache would persist this one row and erase every other provider's
  // saved row -- and `diskHydrated` would then stop any later reader from recovering
  // them. A probe writer cannot hit this because its own read hydrates first; an
  // observation arrives unprompted, so it must hydrate itself.
  hydrateAccountQuotaCache();
  accountQuotaCache.set(key, { ts: Date.now(), quota });
  // Persisted so a restart keeps the last observation: with no probe to re-establish it,
  // a forgotten row stays forgotten until the user happens to run another streaming turn.
  persistAccountQuotaCache();
  // sweepExpiredOnWrite is deliberately NOT called. Existing probe writers call it
  // because they run on a poll; this runs on the request path, where a state sweep does
  // not belong. Passive rows are still reclaimed by generation reconciliation
  // (reconcileProviderAccountQuotaRows) and by the disk reader's age bound.
}

/**
 * Cache-only per-account rows for a passive provider. Never probes, never refreshes.
 *
 * An account with no observation is OMITTED rather than returned with `quota: null` and
 * `unavailable`: that pair means "a probe was attempted and failed", and no probe was
 * ever attempted here. A user who has not yet run a streaming turn simply has no
 * measurement, which is not an error state.
 */
export function readPassiveProviderAccountQuotas(provider: string): ProviderAccountQuota[] {
  if (!hasPassiveAccountQuota(provider)) return [];
  // Idempotent, and otherwise only reached from probe paths a passive provider never
  // enters — without it a restart shows nothing until the next streaming turn, even
  // though the row is sitting on disk.
  hydrateAccountQuotaCache();
  const set = getAccountSet(provider);
  if (!set) return [];
  const rows: ProviderAccountQuota[] = [];
  for (const account of set.accounts) {
    const entry = accountQuotaCache.get(accountCacheKey(provider, account.id));
    if (entry?.quota) rows.push({ accountId: account.id, quota: entry.quota });
  }
  return rows;
}

export function sweepExpiredProviderAccountQuotaRows(now = Date.now()): number {
  let removed = 0;
  for (const [key, entry] of accountQuotaCache) {
    // Anthropic observations extend retention, never the usage probe's eligibility clock.
    const retainedAt = isAnthropicAccountCacheKey(key)
      ? Math.max(entry.ts, entry.quota?.updatedAt ?? 0)
      : entry.ts;
    if (retainedAt + ACCOUNT_QUOTA_TTL_MS > now) continue;
    accountQuotaCache.delete(key);
    removed += 1;
  }
  return removed;
}

export function reconcileProviderAccountQuotaRows(context: GenerationContext): number {
  if (context.generation <= lastReconciledGeneration) return 0;
  hydrateAccountQuotaCache();
  let removed = 0;
  for (const key of accountQuotaCache.keys()) {
    if (context.oauthAccountKeys.has(key)) continue;
    accountQuotaCache.delete(key);
    removed += 1;
  }
  for (const key of anthropicQuotaObservations.keys()) {
    if (!context.oauthAccountKeys.has(key)) anthropicQuotaObservations.delete(key);
  }
  // Kiro exhaustion rows are keyed identically, so they retire with their quota row; a
  // verdict outliving its account would hand the replacement a cooldown it never earned.
  removed += reconcileKiroAccountUsageState(context.oauthAccountKeys);
  const cachedReports = getProviderQuotaReportCache();
  if (cachedReports) {
    const reports = cachedReports.response.reports.filter(report => context.providerNames.has(report.provider));
    removed += cachedReports.response.reports.length - reports.length;
    setProviderQuotaReportCache({ ...cachedReports, response: { ...cachedReports.response, reports } });
    replaceCachedProviderQuotas(reports, routingEvidence);
  }
  liveAccountQuotaKeys = new Set(context.oauthAccountKeys);
  liveProviderQuotaKeys = new Set(context.providerNames);
  lastReconciledGeneration = context.generation;
  if (removed > 0) persistAccountQuotaCache();
  return removed;
}

/** Test-only reset so a direct reconcile call in one file cannot leak across files. */
export function resetProviderQuotaReconcileStateForTests(): void {
  lastReconciledGeneration = 0;
  liveAccountQuotaKeys = new Set();
  liveProviderQuotaKeys = new Set();
}

/** Drop cached per-account rows (all, or just one provider's). */
export function clearAccountQuotaCache(provider?: string): void {
  clearDecisionAccountQuotas(provider);
  // Anthropic readers use provider-owned epochs; B cannot revoke an A flight.
  if (provider !== "anthropic2") explicitAccountEpoch += 1;
  if (!provider) {
    allAccountQuotaEpoch += 1;
    accountQuotaCache.clear();
    anthropicQuotaObservations.clear();
    accountQuotaInflight.clear();
    clearKiroAccountUsageState();
    // A cleared cache must not be re-seeded from the file it was just cleared of, and any
    // pending write of the old rows is abandoned.
    diskHydrated = false;
    cancelPendingAccountQuotaPersist();
    return;
  }
  providerAccountQuotaEpochs.set(provider, (providerAccountQuotaEpochs.get(provider) ?? 0) + 1);
  hydrateAccountQuotaCache();
  const prefix = `${provider}\u0000`;
  for (const key of [...accountQuotaCache.keys()]) {
    if (key.startsWith(prefix)) accountQuotaCache.delete(key);
  }
  for (const key of anthropicQuotaObservations.keys()) {
    if (key.startsWith(prefix)) anthropicQuotaObservations.delete(key);
  }
  clearKiroAccountUsageState(prefix);
  // Drop in-flight probes too so a late resolve cannot repopulate after logout/remove.
  for (const key of [...accountQuotaInflight.keys()]) {
    if (key.startsWith(prefix)) accountQuotaInflight.delete(key);
  }
  persistAccountQuotaCache();
}

/** Drop one account's cached bars and any joined probe so a reset is not ranked from the old window. */
export function forgetCachedProviderAccountQuota(provider: string, accountId: string): void {
  // Advisory decision evidence of the same account is a copy of the dropped bars; it must not
  // keep ranking the account from the pre-reset window while the reread is pending or fails.
  clearDecisionAccountQuotas(provider, accountId);
  hydrateAccountQuotaCache();
  const key = accountCacheKey(provider, accountId);
  accountQuotaCache.delete(key);
  for (const flight of [...accountQuotaInflight.keys()]) {
    if (flight === key || flight.startsWith(key + "\u0000")) accountQuotaInflight.delete(flight);
  }
  persistAccountQuotaCache();
}

/**
 * Resolve a bearer for quota probing without silently adopting a newer global
 * Claude CLI credential into a background multiauth slot.
 *
 * - Fresh stored access → use as-is (no refresh).
 * - Active account with expired access → normal refresh path.
 * - Background `local-cli` with expired access → fail closed (unavailable):
 *   `getValidAccessTokenForAccount` can persist a mismatched Claude CLI identity.
 * - Background ordinary OAuth (`source !== "local-cli"`) → safe to refresh;
 *   Anthropic's lock only adopts disk credentials for `local-cli` rows.
 */
export async function getTokenForAccountQuotaProbe(provider: string, accountId: string): Promise<string> {
  const row = getAccountCredentialWithStatus(provider, accountId);
  if (!row) throw new Error("account credential missing");
  // An operator-paused account is excluded from every automatic upstream use, quota reads included.
  // Anthropic instances and DSH (whose grant never refreshes) also skip a needs-reauth row.
  if (row.paused) throw new Error("account is paused; quota probe skipped");
  if (row.needsReauth && (isAnthropicInstanceId(provider) || provider === "dsh-account")) throw new Error("account needs sign-in; quota probe skipped");
  const stored = row.credential;
  if (stored.expires > Date.now() + ACCOUNT_TOKEN_SKEW_MS) return stored.access;
  const activeId = getAccountSet(provider)?.activeAccountId;
  if (activeId !== accountId && stored.source === "local-cli") {
    throw new Error("background local-cli token expired; skip CLI-adopting refresh for quota probe");
  }
  return getValidAccessTokenForAccount(provider, accountId);
}

/**
 * Rows that must not be probed: an unsupported provider reads as unavailable, and a paused
 * account returns its last reading unchanged (its `ts` shows the age) or an unavailable row,
 * without writing the cache. Undefined means "probe normally".
 */
export function accountQuotaProbeSkip(provider: string, accountId: string): AccountQuotaCacheEntry | undefined {
  if (!supportsPerAccountQuota(provider)) return { ts: Date.now(), quota: null, unavailable: true };
  if (getAccountCredentialWithStatus(provider, accountId)?.paused !== true) return undefined;
  const cached = accountQuotaCache.get(accountCacheKey(provider, accountId));
  return cached && cached.isCurrent?.() !== false ? cached : { ts: Date.now(), quota: null, unavailable: true };
}

export function explicitQuotaConfig(provider: string, configured?: OcxProviderConfig): OcxProviderConfig | undefined {
  if (configured) return configured;
  const entry = getProviderRegistryEntry(provider);
  return entry ? { adapter: entry.adapter, baseUrl: entry.baseUrl, authMode: "oauth" } : undefined;
}

export function explicitQuotaIdentity(provider: string, accountId: string, configured?: OcxProviderConfig): string | undefined {
  const credential = getAccountCredential(provider, accountId);
  const target = explicitQuotaConfig(provider, configured);
  if (!credential || !target) return undefined;
  return quotaCredentialIdentity(provider, accountId, credential, target);
}

export function quotaCredentialIdentity(provider: string, accountId: string, credential: NonNullable<ReturnType<typeof getAccountCredential>>, target: OcxProviderConfig): string {
  return createHash("sha256").update(JSON.stringify([
    provider, accountId, credential.access, credential.refresh, credential.expires,
    credential.accountId, credential.projectId, credential.source,
    target.adapter, target.baseUrl, target.authMode, target.disabled === true,
    // Only credentials that carry their own endpoint (Devin tenants) extend the identity, so
    // every other provider's existing cache keys stay valid.
    ...(provider === "devin" && credential.apiBaseUrl ? [credential.apiBaseUrl] : []),
    ...(provider === "workbuddy-ai" ? [credential.workbuddy] : []),
  ])).digest("hex");
}

export function explicitQuotaDestination(provider: string, config: OcxProviderConfig): boolean {
  if (config.disabled === true || config.authMode !== "oauth") return false;
  if (provider === "kimi") return isCanonicalKimiCodeBaseUrl(config.baseUrl);
  if (provider === "command-code") return isCanonicalCommandCodeBaseUrl(config.baseUrl);
  // These readers use fixed canonical billing origins, never config.baseUrl. Devin reads
  // the credential's own allowlisted api-server host instead (fetchDevinQuota revalidates it).
  return provider === "xai" || provider === "cursor" || provider === "devin" || provider === "workbuddy-ai" || provider === "dsh-account";
}
