refactor(tui_gateway): collapse billing/publisher/method_ctx defensive layers in group H

This commit is contained in:
Teknium
2026-09-02 23:41:16 -07:00
parent 052a5dec66
commit b0fbde9451
5 changed files with 16 additions and 29 deletions

View File

@@ -60,12 +60,10 @@ def _serialize_auto_reload(ar, format_money) -> dict | None:
return None
card_out = None
if ar.card is not None:
card_out = {"kind": ar.card.kind}
if ar.card.kind == "distinct":
card_out = {
"kind": "distinct", "payment_method_id": ar.card.payment_method_id,
"brand": ar.card.brand, "last4": ar.card.last4}
else:
card_out = {"kind": ar.card.kind}
card_out.update(payment_method_id=ar.card.payment_method_id, brand=ar.card.brand,
last4=ar.card.last4)
return {
"enabled": ar.enabled, "threshold_usd": _wire_str(ar.threshold_usd),
"threshold_display": format_money(ar.threshold_usd),
@@ -77,20 +75,18 @@ def _serialize_billing_state(state) -> dict:
"""Serialize a BillingState for the wire (Decimals → strings, money-safe)."""
from agent.billing_view import format_money
card = None
card = mc = None
if state.card is not None:
card = {
"brand": state.card.brand, "last4": state.card.last4, "masked": state.card.masked,
# None/False on older NAS payloads; resolved_via = rung for rung-gated surfaces.
"display": state.card.display, "resolved_via": state.card.resolved_via}
monthly_cap = None
if state.monthly_cap is not None:
mc = state.monthly_cap
monthly_cap = {
"limit_usd": _wire_str(mc.limit_usd), "limit_display": format_money(mc.limit_usd),
"spent_this_month_usd": _wire_str(mc.spent_this_month_usd),
"spent_display": format_money(mc.spent_this_month_usd),
"is_default_ceiling": mc.is_default_ceiling}
m = state.monthly_cap
mc = {"limit_usd": _wire_str(m.limit_usd), "limit_display": format_money(m.limit_usd),
"spent_this_month_usd": _wire_str(m.spent_this_month_usd),
"spent_display": format_money(m.spent_this_month_usd),
"is_default_ceiling": m.is_default_ceiling}
return {
"ok": True, "logged_in": state.logged_in, "org_name": state.org_name,
"org_slug": state.org_slug, "role": state.role, "is_admin": state.is_admin,
@@ -102,8 +98,7 @@ def _serialize_billing_state(state) -> dict:
"charge_presets_display": [format_money(p) for p in state.charge_presets],
"min_usd": _wire_str(state.min_usd), "max_usd": _wire_str(state.max_usd),
"card": card, "payment_method": _serialize_payment_method(state.payment_method),
"monthly_cap": monthly_cap,
"auto_reload": _serialize_auto_reload(state.auto_reload, format_money),
"monthly_cap": mc, "auto_reload": _serialize_auto_reload(state.auto_reload, format_money),
"portal_url": state.portal_url, "error": state.error,
# Shared two-bar dollar usage model so /topup matches /usage and /subscription; fail-open.
"usage": _usage_payload(state)}

View File

@@ -35,18 +35,16 @@ class WsPublisherTransport:
self._url = url
self._lock = threading.Lock()
self._ws: Optional[object] = None
self._dead = False
self._dead = ws_connect is None
self._q: queue.Queue[object] = queue.Queue(maxsize=_QUEUE_MAX)
self._worker: Optional[threading.Thread] = None
if ws_connect is None:
self._dead = True
if self._dead:
return
try:
self._ws = ws_connect(url, open_timeout=connect_timeout, max_size=None)
except Exception as exc:
_log.debug("event publisher connect failed: %s", exc)
self._dead = True
self._ws = None
return
self._worker = threading.Thread(target=self._drain, name="hermes-ws-pub", daemon=True)
self._worker.start()

View File

@@ -109,11 +109,8 @@ def bind_module(module_globals: dict, server, *, skip=()) -> None:
return v
def _has_own_fn(v):
if isinstance(v, dict):
return any(_has_own_fn(x) for x in v.values())
if isinstance(v, (tuple, list)):
return any(_has_own_fn(x) for x in v)
return _own_fn(v)
items = v.values() if isinstance(v, dict) else v if isinstance(v, (tuple, list)) else None
return _own_fn(v) if items is None else any(_has_own_fn(x) for x in items)
for name, obj in list(module_globals.items()):
if name.startswith("__") or name in _PLUMBING or name in skip:

View File

@@ -140,9 +140,7 @@ def _browser_connect(rid, params: dict) -> dict:
if raw_url is not None and not isinstance(raw_url, str):
return _err(rid, 4015, f"browser url must be a string, got {type(raw_url).__name__}")
url = (raw_url or "").strip() or DEFAULT_BROWSER_CDP_URL
sid = params.get("session_id") or ""
system = platform.system()
messages: list[str] = []
sid, system, messages = params.get("session_id") or "", platform.system(), []
def announce(message: str, *, level: str = "info") -> None:
messages.append(message)

View File

@@ -288,8 +288,7 @@ def _sync_bot_capabilities(sid: str, session: dict) -> None:
try:
title = str(getattr(agent, "_session_title_hint", "") or "").strip()
if not title:
db = getattr(agent, "_session_db", None)
key = session.get("session_key") or ""
db, key = getattr(agent, "_session_db", None), session.get("session_key") or ""
title = str((db.get_session_title(key) if (db and key) else None) or "").strip()
if title != "Bot Chat":
return