From b0fbde94514409955cadb9548a5dfaaf0a741ee8 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Wed, 2 Sep 2026 23:41:16 -0700 Subject: [PATCH] refactor(tui_gateway): collapse billing/publisher/method_ctx defensive layers in group H --- tui_gateway/billing_view.py | 25 ++++++++++--------------- tui_gateway/event_publisher.py | 6 ++---- tui_gateway/method_ctx.py | 7 ++----- tui_gateway/methods_browser.py | 4 +--- tui_gateway/model_switch.py | 3 +-- 5 files changed, 16 insertions(+), 29 deletions(-) diff --git a/tui_gateway/billing_view.py b/tui_gateway/billing_view.py index ba5400c17b..5759f57ac9 100644 --- a/tui_gateway/billing_view.py +++ b/tui_gateway/billing_view.py @@ -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)} diff --git a/tui_gateway/event_publisher.py b/tui_gateway/event_publisher.py index 57800bf018..47471d624d 100644 --- a/tui_gateway/event_publisher.py +++ b/tui_gateway/event_publisher.py @@ -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() diff --git a/tui_gateway/method_ctx.py b/tui_gateway/method_ctx.py index 3141b2f2bc..a35e1c6103 100644 --- a/tui_gateway/method_ctx.py +++ b/tui_gateway/method_ctx.py @@ -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: diff --git a/tui_gateway/methods_browser.py b/tui_gateway/methods_browser.py index 1751434327..ac9a303718 100644 --- a/tui_gateway/methods_browser.py +++ b/tui_gateway/methods_browser.py @@ -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) diff --git a/tui_gateway/model_switch.py b/tui_gateway/model_switch.py index 5acee40840..86cd88aa6d 100644 --- a/tui_gateway/model_switch.py +++ b/tui_gateway/model_switch.py @@ -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