Files
rag-technical-docs-assistant/docs/evaluation/local-s2s/phase4-browser-budget.py
T
11linear11 a94b8cd3ea feat(voice): implement local S2S retained history and provider selection
Add pinned CPU engine dependencies, retained interrupted-response history, provider admission, offline evaluation and isolated browser tooling.

Record Phase 2 acceptance and Phase 3/4 preparation, bounded paid-test accounting and interruption incident findings. Human interruption acceptance, paired OpenAI evaluation and rollback remain pending.
2026-10-16 17:09:27 -04:00

61 lines
3.7 KiB
Python

"""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<previous.RESERVES['speech'])
self.path=root/'browser-paid-ledger.json'
self.state={'previous_reserved_and_billed_usd':str(carried),'reserved_usd':str(carried),
'maximum_cumulative_usd':str(maximum),'maximum_attempts':self.maximum_attempts,
'unknown_prior_reservations_usd':'0.417','attempts':[],
'purpose':'phase3_4_local_browser_only_no_openai_inference'}
with self.path.open('x') as output:json.dump(self.state,output,indent=2)
self.upstream=upstream or httpx.AsyncHTTPTransport(retries=0,trust_env=False)
async def handle_async_request(self, request):
stage=request.url.path.rsplit('/',1)[-1]
# Budget exhaustion is distinct from malformed requests or a broken stream.
if stage in self.maximum_attempts:
used=sum(row['stage']==stage for row in self.state['attempts'])
if used >= 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)