"""Exclusive Phase3/4 guard carrying every prior billed/unknown reservation.""" import importlib.util import json from decimal import Decimal from pathlib import Path import httpx from services.local_s2s.components import EvaluationBudgetExhausted path=Path('/checks/browser-budget.py') if not path.exists():path=Path(__file__).with_name('browser-budget.py') spec=importlib.util.spec_from_file_location('previous_browser_budget',path) previous=importlib.util.module_from_spec(spec);spec.loader.exec_module(previous) class BrowserBudget(previous.BrowserBudget): def __init__(self,root,*,upstream=None): previous.native.validate_prices(root) report=json.loads((root/'prior-final-billing.json').read_text()) reconciliation=report['reconciliation'] previous.require(reconciliation['confirmed_cost_usd']=='0.003504399' and reconciliation['unknown_reservations_usd']=='0.417' and reconciliation['conservative_exposure_usd']=='0.420504399') carried=Decimal(reconciliation['confirmed_cost_usd'])+Decimal(reconciliation['unknown_reservations_usd']) self.maximum_attempts={'speech':6,'transcriptions':6,'completions':6} carry_path=root/'previous-phase4-ledger.json' if carry_path.exists(): carry=json.loads(carry_path.read_text()) previous.require(Decimal(carry['previous_reserved_and_billed_usd'])==carried) attempts=carry['attempts'] previous.require(all(row['stage'] in previous.RESERVES and Decimal(row['reserved_usd'])==previous.RESERVES[row['stage']] for row in attempts)) used=sum((Decimal(row['reserved_usd']) for row in attempts),Decimal(0)) previous.require(carried+used==Decimal(carry['reserved_usd'])) carried+=used # Includes cancelled, failed and unknown invoices; no releases. self.maximum_attempts={'speech':4,'transcriptions':4,'completions':4} maximum=carried+sum(previous.RESERVES[k]*v for k,v in self.maximum_attempts.items()) previous.require(maximum<1) endpoints=json.loads((root/'endpoints-google-gemini-3.8-flash-lite-tts.json').read_text())['data']['endpoints'] self.tags=[e['tag'] for e in endpoints] previous.require(set(self.tags)=={'google-ai-studio','google-vertex/global'}) bound=sum(Decimal(e['pricing']['prompt'])*8192+Decimal(e['pricing']['completion'])*2048 for e in endpoints) previous.require(bound= self.maximum_attempts[stage]: self.state['last_denial']={'stage':stage,'reason':'attempt_limit', 'admitted_attempts':used} self.save() raise EvaluationBudgetExhausted('Evaluation attempt limit reached') return await super().handle_async_request(request)