refactor(catalog): split validators and staleness checks below CC 10, with path tests

Adds qa/test_branch_paths.py pinning the main path and one inverted path per
decision for validate_aa_snapshot, validate_availability_snapshot,
validate_native_tariff_overrides, model_router._staleness and
_native_cost_and_headroom, then splits each into small helpers. Messages,
check order and results are unchanged.
This commit is contained in:
evandro committed 2026-10-10 04:05:58 -03:00
1 parent fda614e2e7
commit 1aa2c68818
3 files changed
+785 -199

No files matched your search

+145 -88
View File
@@ -79,103 +79,160 @@ def _has_token_profile(tokens: dict[str, Any]) -> bool:
return any((_decimal(tokens.get(key)) or Decimal(0)) > 0 for key in ("input", "output", "cache_read", "cache_write"))
def _staleness(row: dict[str, Any], now: dt.datetime, max_price_age_hours: Decimal, max_score_age_days: Decimal,
tokens: dict[str, Any]) -> list[str]:
_PAID_MODES = ("zen_balance", "go_quota", "omp_payg", "claude_subscription")
_BALANCE_MODES = ("zen_balance", "omp_payg")
_SIXTY_SECONDS = dt.timedelta(seconds=60)
_DAY = dt.timedelta(hours=24)
def _expired(moment: dt.datetime | None, now: dt.datetime, limit: dt.timedelta) -> bool:
"""Ausente, no futuro ou mais antigo que o limite."""
return moment is None or moment > now or now - moment > limit
def _is_https(value: Any) -> bool:
return str(value).startswith("https://")
def _native_tariff_stale(native: dict[str, Any], now: dt.datetime, limit: dt.timedelta) -> bool:
return (native.get("account_confirmed") is not True or not _is_https(native.get("source_url", ""))
or _expired(_time(native.get("captured_at")), now, limit))
def _openrouter_record(row: dict[str, Any]) -> dict[str, Any]:
return row.get("openrouter") or {"price_per_token_usd": row.get("or_price_per_token") or {}}
def _go_quota_stale(row: dict[str, Any], now: dt.datetime) -> bool:
return (_expired(_time(row.get("quota_verified_at")), now, _SIXTY_SECONDS)
or not _is_https(row.get("quota_source_url", "")))
def _reference_price_stale(price: dict[str, Any], now: dt.datetime, limit: dt.timedelta) -> bool:
"""Só há o que revalidar quando existe alguma tarifa referencial."""
rates = price.get("price_per_token_usd", {})
has_reference_price = any(rates.get(k) is not None for k in ("input", "output", "cache_read", "cache_write"))
return has_reference_price and _expired(_time(price.get("captured_at")), now, limit)
def _availability_reasons(row: dict[str, Any], mode: Any, now: dt.datetime) -> list[str]:
reasons: list[str] = []
mode = row.get("billing_mode")
paid = mode in ("zen_balance", "go_quota", "omp_payg", "claude_subscription")
price_record = row.get("openrouter") or {"price_per_token_usd": row.get("or_price_per_token") or {}}
price = openrouter_price_for_profile(price_record, tokens)
native = row.get("native_tariff") or {}
has_reference_price = any(price.get("price_per_token_usd", {}).get(k) is not None
for k in ("input", "output", "cache_read", "cache_write"))
if paid:
if has_reference_price:
price_time = _time(price.get("captured_at"))
if price_time is None or price_time > now or (now - price_time).total_seconds() > float(max_price_age_hours * Decimal(3600)):
reasons.append("preco_referencial_obsoleto_ou_ausente")
score = row.get("score") or {}
score_time = _time(score.get("observed_at"))
if score_time is None or score_time > now or (now - score_time).total_seconds() > float(max_score_age_days * Decimal(86400)):
reasons.append("benchmark_obsoleto_ou_ausente")
if mode in ("zen_balance", "omp_payg"):
native_time = _time(native.get("captured_at"))
if native.get("account_confirmed") is not True or not str(native.get("source_url", "")).startswith("https://") or native_time is None or native_time > now or now - native_time > dt.timedelta(hours=float(max_price_age_hours)):
reasons.append("tarifa_nativa_obsoleta_ou_nao_confirmada")
if mode == "go_quota":
quota_time = _time(row.get("quota_verified_at"))
if quota_time is None or quota_time > now or now - quota_time > dt.timedelta(seconds=60) or not str(row.get("quota_source_url", "")).startswith("https://"):
reasons.append("quota_go_obsoleta_ou_nao_confirmada")
availability = row.get("availability") or {}
local_time = _time(availability.get("local_verified_at") or availability.get("verified_at"))
if local_time is None or local_time > now or now - local_time > dt.timedelta(hours=24):
reasons.append("disponibilidade_local_precisa_revalidacao")
account_time = _time(availability.get("account_verified_at"))
if availability.get("authenticated") is True and availability.get("entitled") is True:
if account_time is None or account_time > now or now - account_time > dt.timedelta(hours=24):
reasons.append("entitlement_precisa_revalidacao")
if mode in ("zen_balance", "omp_payg"):
balance_time = _time(availability.get("balance_verified_at"))
if availability.get("balance_confirmed") is not True or balance_time is None or balance_time > now or now - balance_time > dt.timedelta(seconds=60):
reasons.append("saldo_nao_confirmado_ou_obsoleto")
availability = row.get("availability") or {}
if _expired(_time(availability.get("local_verified_at") or availability.get("verified_at")), now, _DAY):
reasons.append("disponibilidade_local_precisa_revalidacao")
if (availability.get("authenticated") is True and availability.get("entitled") is True
and _expired(_time(availability.get("account_verified_at")), now, _DAY)):
reasons.append("entitlement_precisa_revalidacao")
if mode in _BALANCE_MODES and (availability.get("balance_confirmed") is not True
or _expired(_time(availability.get("balance_verified_at")), now, _SIXTY_SECONDS)):
reasons.append("saldo_nao_confirmado_ou_obsoleto")
return reasons
def _staleness(row: dict[str, Any], now: dt.datetime, max_price_age_hours: Decimal, max_score_age_days: Decimal,
tokens: dict[str, Any]) -> list[str]:
mode = row.get("billing_mode")
price = openrouter_price_for_profile(_openrouter_record(row), tokens)
if mode not in _PAID_MODES:
return []
reasons: list[str] = []
price_limit = dt.timedelta(seconds=float(max_price_age_hours * Decimal(3600)))
if _reference_price_stale(price, now, price_limit):
reasons.append("preco_referencial_obsoleto_ou_ausente")
score_limit = dt.timedelta(seconds=float(max_score_age_days * Decimal(86400)))
if _expired(_time((row.get("score") or {}).get("observed_at")), now, score_limit):
reasons.append("benchmark_obsoleto_ou_ausente")
if mode in _BALANCE_MODES and _native_tariff_stale(row.get("native_tariff") or {}, now, price_limit):
reasons.append("tarifa_nativa_obsoleta_ou_nao_confirmada")
if mode == "go_quota" and _go_quota_stale(row, now):
reasons.append("quota_go_obsoleta_ou_nao_confirmada")
return reasons + _availability_reasons(row, mode, now)
def _profile_cost(row: dict[str, Any], tokens: dict[str, Any]) -> Decimal | None:
record = row.get("openrouter") or {"price_per_token_usd": row.get("or_price_per_token") or {}}
profile = openrouter_price_for_profile(record, tokens)
profile = openrouter_price_for_profile(_openrouter_record(row), tokens)
return reference_cost(profile.get("price_per_token_usd") or {}, tokens)
def _native_cost_and_headroom(row: dict[str, Any], tokens: dict[str, Any]) -> tuple[Decimal | None, Decimal | None, str | None]:
mode = row.get("billing_mode")
_CostResult = tuple[Decimal | None, Decimal | None, str | None]
def _has_confirmed_rates(native: Any) -> bool:
return (isinstance(native, dict) and isinstance(native.get("price_per_token_usd"), dict)
and native.get("account_confirmed") is True)
def _balance_cost(row: dict[str, Any], tokens: dict[str, Any]) -> _CostResult:
"""zen_balance e omp_payg: custo pela tarifa nativa confirmada e folga de saldo."""
native = row.get("native_tariff")
if mode in ("zen_balance", "omp_payg"):
if not _has_token_profile(tokens):
return None, None, "perfil_tokens_native_nao_configurado"
if not isinstance(native, dict) or not isinstance(native.get("price_per_token_usd"), dict) or native.get("account_confirmed") is not True:
return None, None, "tarifa_native_nao_confirmada"
amount = reference_cost(native["price_per_token_usd"], tokens)
if amount is None:
return None, None, "tarifa_native_incompleta_para_perfil"
balance = _decimal(native.get("available_balance_usd"))
availability = row.get("availability") or {}
if availability.get("balance_confirmed") is not True:
return None, None, "saldo_nao_confirmado"
if balance is None or balance < 0:
return None, None, "saldo_desconhecido"
if balance < amount:
return None, balance, "saldo_insuficiente"
return amount, balance - amount, None
if mode == "go_quota":
if not _has_token_profile(tokens):
return None, None, "perfil_tokens_go_nao_configurado"
remaining = row.get("quota_window_remaining")
debit = row.get("quota_window_debit")
if row.get("use_balance_fallback"):
return None, None, "fallback_go_para_saldo_zen_nao_autorizado"
if not isinstance(remaining, dict) or debit is None:
return None, None, "quota_go_nao_confirmada"
debit_map = debit if isinstance(debit, dict) else {window: debit for window in WINDOWS}
headrooms: list[Decimal] = []
for window in WINDOWS:
available = _decimal(remaining.get(window))
used = _decimal(debit_map.get(window))
if available is None or used is None or available < 0 or used < 0:
return None, None, f"janela_go_{window}_desconhecida"
if used > available:
return None, None, f"quota_go_{window}_insuficiente"
headrooms.append(available - used)
representative = _decimal(debit_map.get("monthly"))
if representative is None:
return None, min(headrooms), "debito_go_nao_confirmado"
return representative, min(headrooms), None
if mode == "claude_subscription":
amount = _decimal(row.get("subscription_consumption_usd"))
if amount is None:
return None, None, "consumo_assinatura_nao_mensuravel"
return amount, None, None
return None, None, "modo_de_cobranca_desconhecido"
if not _has_token_profile(tokens):
return None, None, "perfil_tokens_native_nao_configurado"
if not _has_confirmed_rates(native):
return None, None, "tarifa_native_nao_confirmada"
amount = reference_cost(native["price_per_token_usd"], tokens)
if amount is None:
return None, None, "tarifa_native_incompleta_para_perfil"
balance = _decimal(native.get("available_balance_usd"))
if (row.get("availability") or {}).get("balance_confirmed") is not True:
return None, None, "saldo_nao_confirmado"
if balance is None or balance < 0:
return None, None, "saldo_desconhecido"
if balance < amount:
return None, balance, "saldo_insuficiente"
return amount, balance - amount, None
def _go_window_headrooms(remaining: dict[str, Any], debit_map: dict[str, Any]) -> tuple[list[Decimal], str | None]:
"""Folga por janela; no primeiro problema devolve a razão de recusa."""
headrooms: list[Decimal] = []
for window in WINDOWS:
available = _decimal(remaining.get(window))
used = _decimal(debit_map.get(window))
if available is None or used is None or available < 0 or used < 0:
return headrooms, f"janela_go_{window}_desconhecida"
if used > available:
return headrooms, f"quota_go_{window}_insuficiente"
headrooms.append(available - used)
return headrooms, None
def _go_quota_cost(row: dict[str, Any], tokens: dict[str, Any]) -> _CostResult:
"""go_quota: débito mensal como custo representativo e a menor folga entre as janelas."""
if not _has_token_profile(tokens):
return None, None, "perfil_tokens_go_nao_configurado"
remaining = row.get("quota_window_remaining")
debit = row.get("quota_window_debit")
if row.get("use_balance_fallback"):
return None, None, "fallback_go_para_saldo_zen_nao_autorizado"
if not isinstance(remaining, dict) or debit is None:
return None, None, "quota_go_nao_confirmada"
debit_map = debit if isinstance(debit, dict) else {window: debit for window in WINDOWS}
headrooms, reason = _go_window_headrooms(remaining, debit_map)
if reason:
return None, None, reason
representative = _decimal(debit_map.get("monthly"))
if representative is None:
return None, min(headrooms), "debito_go_nao_confirmado"
return representative, min(headrooms), None
def _subscription_cost(row: dict[str, Any], tokens: dict[str, Any]) -> _CostResult:
amount = _decimal(row.get("subscription_consumption_usd"))
if amount is None:
return None, None, "consumo_assinatura_nao_mensuravel"
return amount, None, None
_COST_BY_MODE = {"zen_balance": _balance_cost, "omp_payg": _balance_cost,
"go_quota": _go_quota_cost, "claude_subscription": _subscription_cost}
def _native_cost_and_headroom(row: dict[str, Any], tokens: dict[str, Any]) -> _CostResult:
mode = row.get("billing_mode")
handler = _COST_BY_MODE.get(mode) if isinstance(mode, str) else None
if handler is None:
return None, None, "modo_de_cobranca_desconhecido"
return handler(row, tokens)
def route(candidates: Iterable[dict[str, Any]], role: str, *, allowed_providers: Iterable[str] = (),