The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(hermes_cli): unify billing block headers, portal hand-off, charge submit/poll; dict-dispatch menus
Tekniumc732ff08acfe
diff --git a/hermes_cli/cli_billing_mixin.py b/hermes_cli/cli_billing_mixin.pyindex 23e0ce24c08..de370dc0a4e 100644--- a/hermes_cli/cli_billing_mixin.py+++ b/hermes_cli/cli_billing_mixin.py@@ -1,8 +1,7 @@-"""Billing and subscription handlers for the interactive CLI, lifted out of ``cli.py``.+"""Billing and subscription handlers for the interactive CLI (mixed into ``HermesCLI``).-``HermesCLI`` inherits ``CLIBillingMixin`` so every ``self.<handler>`` resolves via the MRO.-cli.py-internal symbols (``_cprint``/``_b``/``_d``, display constants) are imported LAZILY-inside each method — the mixin never imports ``cli`` at module load time (import cycle).+cli.py-internal symbols (``_cprint``/``_b``/``_d``, display constants) are imported LAZILY inside+each method — the mixin never imports ``cli`` at module load time (import cycle)."""from __future__ import annotations@@ -13,49 +12,108 @@ _RULE = "─" * 41_CHARGE_FAILED_COPY = {"authentication_required": " 🔴 Your bank requires verification (3DS). Complete it on the portal to finish this purchase.","payment_method_expired": " 🔴 Your card has expired. Update it on the portal.",- "card_declined": " 🔴 Your card was declined. Try another card on the portal.",-}+ "card_declined": " 🔴 Your card was declined. Try another card on the portal."}# Submit-time BillingError codes with a fixed copy (no payload/type inspection)._CHARGE_ERROR_COPY = {"no_payment_method": " 💳 No card on file — top up and manage billing on the portal.","cli_billing_disabled": " Remote spending is off for this account — a billing admin can turn it on from the portal's Hermes Agent page.","role_required": " Adding funds needs an org admin/owner. Ask an admin, or manage on the portal.",- "idempotency_conflict": " 🔴 That charge key was already used for a different amount. Start a fresh top-up.",-}+ "idempotency_conflict": " 🔴 That charge key was already used for a different amount. Start a fresh top-up."}_CHARGE_ERROR_COPY["remote_spending_disabled"] = _CHARGE_ERROR_COPY["cli_billing_disabled"]# Upgrade 2xx `status` → (line, echo recoveryUrl as "Portal:"). Missing status → ambiguous._UPGRADE_STATUS_COPY = {"requires_action": (" 🟡 This upgrade needs extra verification (3DS). Finish it on the portal.", True),- "payment_failed": (" 🔴 Your card was declined. Update your payment method on the portal and try again.", True),-}+ "payment_failed": (" 🔴 Your card was declined. Update your payment method on the portal and try again.", True)}++# Upgrade 2xx terminal statuses → dim ✓ copy ({name} = target tier).+_UPGRADE_OK_COPY = {+ "already_on_tier": "You are already on {name}.",+ "upgraded": "Upgraded to {name}. Your new monthly credits land in a moment."}+# Pending-change mutations → dim ✓ copy.+_PENDING_OK_COPY = {+ "schedule": "Scheduled — your plan doesn't change today. You keep it until the end of the billing period, then it switches.",+ "cancel": "Scheduled — your plan stays active until the end of the billing period, then it cancels. Nothing changes today.",+ "resume": "Undone — you stay on your current plan."}_ALLOW_REMOTE_SPENDING_CHOICES = [- ("yes", "Allow Remote Spending", "open your browser to authorize"),- ("no", "Not now", "cancel"),-]+ ("yes", "Allow Remote Spending", "open your browser to authorize"), ("no", "Not now", "cancel")]++# Static modal menus (value, label, description) — order is the rendered order.+_TOPUP_MENU_CHOICES = [+ ("buy", "Add funds", "a single charge, added to your balance today"),+ ("auto", "Auto-reload", "refill automatically when your balance runs low"),+ ("limit", "Monthly limit", "show the monthly spend cap (read-only)"),+ ("portal", "Manage on portal", "open the billing page in your browser"),+ ("cancel", "Cancel", "do nothing")]+_ADD_CARD_CHOICES = [+ ("portal", "Add a card on the portal", "opens the billing page in your browser"),+ ("recheck", "I've added it — check again", "re-check for the card and continue"),+ ("cancel", "Back", "do nothing")]+_AUTO_RELOAD_TOP_CHOICES = [+ ("edit", "Edit thresholds", "change when / how much to reload"),+ ("off", "Turn off", "disable auto-reload"),+ ("cancel", "Cancel", "do nothing")]+_AUTO_RELOAD_AGREE_CHOICES = [("agree", "Agree and turn on", "enable auto-reload"), ("cancel", "Cancel", "do nothing")]+_CHANGE_PLAN_ROW = ("change", "Change plan", "upgrade or downgrade in the terminal")+_CHANGE_MENU_TAIL = [+ ("portal", "Manage on portal", "open the billing page in your browser"), ("close", "Close", "do nothing")]+_CANCEL_SUB_CHOICES = [+ ("yes", "Cancel subscription", "schedule cancellation at period end"),+ ("cancel", "Go back", "keep your plan")]+_STEPUP_STALE_MSG = (+ " Remote Spending still isn't active for this terminal — the authorization didn't take. "+ "Retry, or make this change on the portal.")+_NO_MANAGE_URL = "No manage URL available — is your portal configured?"+_KILLSWITCH_REASON_OVERVIEW = "A billing admin can turn it on from the portal's Hermes Agent page to add funds here."+_KILLSWITCH_REASON_BUY = "A billing admin can turn it on from the portal's Hermes Agent page before adding funds."class CLIBillingMixin:"""Mixin holding interactive-CLI billing and subscription handlers."""- # ------------------------------------------------------------------- # Shared helpers- # ------------------------------------------------------------------+ # ── Shared helpers ──def _modal_choice(self, title, detail, choices):"""Run the choice modal and return the normalized choice value."""raw = self._prompt_text_input_modal(title=title, detail=detail, choices=choices)return self._normalize_slash_confirm_choice(raw, choices)+ def _block_header(self, icon, title, *, rule=True) -> None:+ """Blank line, ``<icon> <bold title>`` via _cprint, then (optionally) the rule via print."""+ from cli import _cprint, _b+ print()+ _cprint(f" {icon} {_b(title)}")+ if rule:+ print(f" {_RULE}")++ def _dim(self, msg, *, icon="", lead=False) -> None:+ """Dim `` <icon><msg>`` line via _cprint; ``lead`` prints a blank line first."""+ from cli import _cprint, _d+ if lead:+ print()+ _cprint(f" {icon}{_d(msg)}")++ def _ok(self, msg) -> None:+ """Dim ``✓ <msg>`` success line."""+ from cli import _cprint, _DIM, _RST+ _cprint(f" {_DIM}✓ {msg}{_RST}")++ def _print_logged_out(self, state, load_failed, cmd) -> None:+ """Logged-out / fetch-failed block shared by /subscription and /topup."""+ if state.error:+ self._dim(f"{load_failed}: {state.error}", icon="💳 ", lead=True)+ else:+ self._dim("Not logged into Nous Portal.", icon="💳 ", lead=True)+ print(f" Run `hermes portal` to log in, then {cmd}.")+def _print_org_line(self, state) -> None:"""Dim ``Org: <name> · <Role>`` line (skipped when there is no org)."""- from cli import _cprint, _dif state.org_name:role = (state.role or "").title()_org_line = f"Org: {state.org_name}{f' · {role}' if role else ''}"- _cprint(f" {_d(_org_line)}")+ self._dim(_org_line)def _try_usage_model(self):"""Shared dollar usage model (the only source with top-up dollars); None on any failure."""@@ -66,34 +124,29 @@ class CLIBillingMixin:return Nonedef _usage_bar_lines(self, usage, plan_name) -> list:- """The plan + top-up dollar bars as ready-to-print lines (filled = remaining); [] when- nothing to draw. The caller picks its print fn — block ordering differs per surface- (``_cprint`` vs ``print`` under patch_stdout). One source of truth for /usage, /subscription, /topup."""+ """Plan + top-up bars as lines (filled = remaining). Caller picks the print fn: ordering differs per surface."""lines: list = []- pb = getattr(usage, "plan_bar", None) if usage else None+ pb = usage.plan_bar if usage else Noneif pb is not None and pb.total_usd > 0:filled = max(0, min(10, round(pb.fill_fraction * 10)))bar = ("█" * filled) + ("░" * (10 - filled))pct_s = f" · {pb.pct_used}% used" if pb.pct_used is not None else ""label = (plan_name or "plan").ljust(8)[:8]lines.append(f" {label}[{bar}] ${pb.remaining_usd:,.2f} left of ${pb.total_usd:,.2f}{pct_s}")- tb = getattr(usage, "topup_bar", None) if usage else None+ tb = usage.topup_bar if usage else Noneif tb is not None and tb.remaining_usd > 0:lines.append(f" {'top-up'.ljust(8)}[{'█' * 10}] ${tb.remaining_usd:,.2f} · never expires")return linesdef _print_total_spendable(self, usage, print_fn) -> None:- if usage and getattr(usage, "has_topup", False) and getattr(usage, "total_spendable_usd", None) is not None:+ if usage and usage.has_topup and usage.total_spendable_usd is not None:print_fn(f" Total spendable: ${usage.total_spendable_usd:,.2f}")def _step_up_remote_spending(self, *, explain, noninteractive_msg, declined_msg, not_granted_msg) -> bool:- """The "! One-time setup" step-up: explain, confirm in the modal, then run the browser- device-flow via ``step_up_nous_billing_scope``. Returns True only when the scope was granted;- every refusal/failure path has already printed its own line."""- from cli import _cprint, _d+ """"! One-time setup" step-up (explain → confirm → device-flow). True only when granted; refusals print."""print()print(" ! One-time setup")- _cprint(f" {_d(explain)}")+ self._dim(explain)if not self._app:print(noninteractive_msg)return False@@ -110,58 +163,66 @@ class CLIBillingMixin:return Falseif not granted:print(not_granted_msg)- return False- return True+ return bool(granted)def _print_portal_line(self, exc) -> None:"""``Portal: <url>`` via _cprint when the error carries a portal deep-link."""from cli import _cprint- _url = getattr(exc, "portal_url", None) if exc is not None else None- if _url:- _cprint(f" Portal: {_url}")-- # ------------------------------------------------------------------- # /usage — Nous balance block- # ------------------------------------------------------------------+ if exc is not None and exc.portal_url:+ _cprint(f" Portal: {exc.portal_url}")- def _print_nous_credits_block(self) -> bool:- """Print the Nous dollar balance block (two-bar view) when a Nous account is logged in;- True if anything printed. Prefers the shared dollar model (``agent.billing_usage``, the- /usage + /subscription source of truth), falling back to legacy ``nous_credits_lines`` text.- Agent-independent so the TUI slash-worker (no live agent) still shows it. Fail-open."""- from cli import _cprint, _b, _d+ def _open_url_in_browser(self, url: str) -> bool:+ """The one portal opener. Refuses TTY-hijacking text browsers (w3m/lynx over SSH) via the auth guard."""+ if not url:+ return False+ try:+ from hermes_cli.auth import _can_open_graphical_browser, _is_remote_session+ if _is_remote_session() or not _can_open_graphical_browser():+ return False+ except Exception:+ pass # guard unavailable → plain best-effort opentry:- from agent.billing_usage import build_usage_model, format_renews- usage = build_usage_model()+ import webbrowser+ return bool(webbrowser.open(url))except Exception:- usage = None- format_renews = None # type: ignore+ return False- if usage is not None and usage.available and format_renews is not None:- printed_any = False+ def _open_or_print_url(self, url) -> None:+ """Open ``url`` in the browser, or print it when no graphical browser can be used."""+ if not self._open_url_in_browser(url):+ print(f" Open this URL: {url}")++ # ── /usage — Nous balance block ──++ def _print_nous_credits_block(self) -> bool:+ """Nous dollar balance block (two bars); True if anything printed. Shared dollar model first, then+ legacy ``nous_credits_lines``. Agent-independent (TUI slash-worker has no live agent). Fail-open."""+ from cli import _cprint, _b, _d+ usage = self._try_usage_model()+ if usage is not None and usage.available:+ from agent.billing_usage import format_renewsplan = usage.plan_name or ("Free" if usage.status == "free" else None)- renews_display = getattr(usage, "renews_display", None) or format_renews(usage.renews_at)+ renews_display = usage.renews_display or format_renews(usage.renews_at)renews = f" · renews {renews_display}" if renews_display else ""- if plan:- print()- _cprint(f" {_b(f'Plan: {plan}{renews}')}")- printed_any = True- # Everything below goes through _cprint like the Plan line: raw print() and _cprint()- # flush to different buffers under patch_stdout and would interleave nondeterministically.- for _bar_ln in self._usage_bar_lines(usage, usage.plan_name):- _cprint(_bar_ln)- printed_any = True- self._print_total_spendable(usage, _cprint)+ head = [f" {_b(f'Plan: {plan}{renews}')}"] if plan else []+ head += self._usage_bar_lines(usage, usage.plan_name)+ tail = []if usage.status == "free":- _cprint(f" {_d('> Free · free models only. Run /subscription to reach paid models.')}")- printed_any = True+ tail.append(f" {_d('> Free · free models only. Run /subscription to reach paid models.')}")elif usage.status == "low":_amt = f"${usage.total_spendable_usd:,.2f}" if usage.total_spendable_usd is not None else "under $5"- _cprint(f" ! Low balance · {_amt} left. Run /topup or /subscription.")- printed_any = True- if printed_any:+ tail.append(f" ! Low balance · {_amt} left. Run /topup or /subscription.")+ # All via _cprint like the Plan line: print()/_cprint() flush to different buffers under+ # patch_stdout. "Total spendable" alone does not count as printed (legacy lines still follow).+ if plan:+ print()+ for ln in head:+ _cprint(ln)+ self._print_total_spendable(usage, _cprint)+ for ln in tail:+ _cprint(ln)+ if head or tail:return True-from agent.account_usage import nous_credits_lineslines = nous_credits_lines()if not lines:@@ -172,176 +233,114 @@ class CLIBillingMixin:return Truedef _print_usage_cta(self) -> None:- """The `/usage` call-to-action; mirrors the TUI's ``USAGE_CTA``. Only printed when a Nous- account is logged in (both commands are Nous-account only)."""- from cli import _cprint, _d- _cprint(f" {_d('Run /subscription to change plan · /topup to add to your balance')}")+ """The `/usage` call-to-action; mirrors the TUI's ``USAGE_CTA``. Nous-account only."""+ self._dim('Run /subscription to change plan · /topup to add to your balance')- # ------------------------------------------------------------------- # /subscription — view plan + change it (CLI surface)- # ------------------------------------------------------------------+ # ── /subscription — view plan + change it (CLI surface) ──def _show_subscription(self):- """`/subscription` (alias `/upgrade`) — CLI mirror of the TUI ``SubscriptionOverlay``.- Deep-links to NAS's own ``/manage-subscription`` page (NOT the Stripe portal). The terminal- NEVER charges for a subscription. Fail-open: logged-out / portal hiccup → clear message."""- from cli import _cprint, _b, _d+ """`/subscription` (alias `/upgrade`). Deep-links NAS's ``/manage-subscription`` (NOT Stripe); never charges."""from agent.subscription_view import build_subscription_state, subscription_manage_url-state = build_subscription_state()if not state.logged_in:- print()- if state.error:- _cprint(f" 💳 {_d(f'Could not load subscription: {state.error}')}")- else:- _cprint(f" 💳 {_d('Not logged into Nous Portal.')}")- print(" Run `hermes portal` to log in, then /subscription.")+ self._print_logged_out(state, "Could not load subscription", "/subscription")return-if state.context == "team": # no personal plan — teams run on a shared balance- print()- _cprint(f" ⚕ {_b('Team subscription')}")- print(f" {_RULE}")+ self._block_header("⚕", "Team subscription")self._print_org_line(state)- org = state.org_name or "a team org"- print(f" This terminal is connected to {org}. Teams run on a shared")+ print(f" This terminal is connected to {state.org_name or 'a team org'}. Teams run on a shared")print(" balance · use /topup to add funds.")- _cprint(f" {_d('Personal subscriptions live on your personal account.')}")+ self._dim('Personal subscriptions live on your personal account.')return-self._subscription_overview(state, subscription_manage_url(state))def _subscription_overview(self, state, manage_url):- """Print the plan read block (dollars-only, two-bar view, state-matched nudges), then the- action: portal hand-off for members / non-interactive, catalog for Free, change menu for paid admins."""+ """Plan read block, then the action: portal hand-off (member / non-interactive), catalog (Free), change menu."""from cli import _cprint, _b, _dfrom agent.billing_usage import format_renews-usage = self._try_usage_model()c = state.currentis_free = not (c and c.tier_id)can_change = state.can_change_planplan_name = (c.tier_name or c.tier_id) if c else (usage.plan_name if usage else None)- u_status = getattr(usage, "status", None) if usage else None- renews_display = getattr(usage, "renews_display", None) if usage else None+ u_status = usage.status if usage else None+ _spend = usage.total_spendable_usd if usage else None+ renews_display = usage.renews_display if usage else Noneif not renews_display and c and c.cycle_ends_at:renews_display = format_renews(c.cycle_ends_at)-- # Status line carries a pending change ("→ Plus" / "→ cancels") so the headline itself flags it.- _flip = ""+ # Headline flags the pending change ("→ Plus" / "→ cancels"); the banner (cancel > downgrade)+ # leads so it can't read as "nothing happened".+ _flip, _trans = "", Noneif c and c.cancel_at_period_end:_flip = " → cancels"+ _trans = (c.tier_name or "your plan", "cancels", format_renews(c.cancellation_effective_at) or "the end of the billing period")elif c and c.pending_downgrade_tier_name:_flip = f" → {c.pending_downgrade_tier_name}"- if not plan_name:- status = "Plan: Free · free models only"- elif usage is not None and u_status == "low" and usage.total_spendable_usd is not None:- status = f"Plan: {plan_name}{_flip} · ${usage.total_spendable_usd:,.2f} left"+ _trans = (c.tier_name or "your plan", c.pending_downgrade_tier_name, format_renews(c.pending_downgrade_at) or "the end of the cycle")+ _left = f" · ${_spend:,.2f} left" if _spend is not None else ""+ if u_status == "low" and _spend is not None:+ _tail = ""else:- _spend = getattr(usage, "total_spendable_usd", None) if usage else None- _left = f" · ${_spend:,.2f} left" if _spend is not None else ""_tail = " · view only" if not can_change else (f" · renews {renews_display}" if renews_display else "")- status = f"Plan: {plan_name}{_flip}{_left}{_tail}"-- # Lead with the scheduled change (cancel > downgrade) so it can't read as "nothing happened".+ status = f"Plan: {plan_name}{_flip}{_left}{_tail}" if plan_name else "Plan: Free · free models only"# All-_cprint (blanks included) so the block orders deterministically even when piped.- _trans = None- if c and c.cancel_at_period_end:- _trans = ((c.tier_name or "your plan"), "cancels", format_renews(c.cancellation_effective_at) or "the end of the billing period")- elif c and c.pending_downgrade_tier_name:- _trans = ((c.tier_name or "your plan"), c.pending_downgrade_tier_name, format_renews(c.pending_downgrade_at) or "the end of the cycle")_cprint("")if _trans:_from, _to, _when = _trans_cprint(f" ⏳ {_b('Scheduled change')}")_cprint(f" {_from} ──▶ {_to} {_d('· ' + _when)}")- _cprint(f" {_d(f'You keep {_from} (and its credits) until then.')}")+ self._dim(f"You keep {_from} (and its credits) until then.")_cprint("")-_cprint(f" ⚕ {_b(status)}")print(f" {_RULE}")for _bar_ln in self._usage_bar_lines(usage, plan_name):print(_bar_ln)self._print_total_spendable(usage, print)if is_free:- _cprint(f" {_d('> Paid models need a subscription. Start one to reach them.')}")+ self._dim('> Paid models need a subscription. Start one to reach them.')elif u_status == "low":- _amt = f"${usage.total_spendable_usd:,.2f}" if usage is not None and usage.total_spendable_usd is not None else "under $5"+ _amt = f"${_spend:,.2f}" if _spend is not None else "under $5"_cprint(f" ! Low balance · {_amt} left. Top up or upgrade before a mid-run cutoff.")self._print_org_line(state)print(f" {_RULE}")-if not can_change:- print()- _cprint(f" {_d('Plan changes need an org admin/owner.')}")+ self._dim('Plan changes need an org admin/owner.', lead=True)if manage_url:print(f" Manage on portal: {manage_url}")- return- if not self._app: # non-interactive (TUI slash-worker / piped): the modal can't run+ elif not self._app: # non-interactive (TUI slash-worker / piped): the modal can't runprint()if manage_url:print(f" Manage your subscription: {manage_url}")print(" Open it in your browser, then re-run /subscription.")- return- if is_free: # a NEW subscription needs a fresh card → catalog + portal deep-link only+ elif is_free: # a NEW subscription needs a fresh card → catalog + portal deep-link onlyself._subscription_free_catalog(state, manage_url)- return- self._subscription_change_menu(state, manage_url)-- def _open_url_in_browser(self, url: str) -> bool:- """Open ``url`` in a REAL graphical browser; the one opener behind every portal hand-off.- Applies the console-browser / remote-session guard from ``hermes_cli.auth``:- ``webbrowser.open()`` returns True even for a text-mode browser (w3m/lynx over SSH) that- hijacks the TTY, so those are refused and the caller prints the URL instead."""- if not url:- return False- try:- from hermes_cli.auth import _can_open_graphical_browser, _is_remote_session- if _is_remote_session() or not _can_open_graphical_browser():- return False- except Exception:- pass # guard unavailable → plain best-effort open- try:- import webbrowser- return bool(webbrowser.open(url))- except Exception:- return False+ else:+ self._subscription_change_menu(state, manage_url)def _subscription_free_catalog(self, state, manage_url):- """Free + admin/owner + interactive: print the plan catalog, pick one, open the portal- manage-subscription deep-link with ``plan=<tier_id>`` so it preselects the plan.- Monthly credits are DOLLARS. The terminal never charges here (a new sub needs a fresh card)."""- from cli import _cprint, _b, _d+ """Free + admin + interactive: catalog → pick → portal deep-link ``plan=<tier_id>`` (a new sub needs a card)."""from agent.subscription_view import format_tier_row, selectable_tiers, subscription_manage_url-tiers = selectable_tiers(state)if not tiers:self._subscription_open_portal(state, manage_url, verb="Start a subscription")return-- print()- _cprint(f" ⚕ {_b('Choose a plan')}")- print(f" {_RULE}")+ self._block_header("⚕", "Choose a plan")for i, t in enumerate(tiers, 1):print(f" {i}. {format_tier_row(t)}")- _cprint(f" {_d('Starting a subscription opens the portal to add your card.')}")-+ self._dim('Starting a subscription opens the portal to add your card.')choices = [(t.tier_id, format_tier_row(t), f"start {t.name} on the portal") for t in tiers]choices.append(("cancel", "Cancel", "do nothing"))raw = self._prompt_text_input_modal(title="Start a subscription", detail="Pick a plan to open it on the portal.", choices=choices)- # Rows are printed numbered → accept a bare number as a pick (the shared normalizer- # only knows the confirm-dialog digit aliases).+ # Rows are numbered → accept a bare number (the normalizer only knows confirm-dialog digits)._digit = (raw or "").strip()- if _digit.isdigit() and 1 <= int(_digit) <= len(tiers):- choice = tiers[int(_digit) - 1].tier_id- else:- choice = self._normalize_slash_confirm_choice(raw, choices)+ _by_row = tiers[int(_digit) - 1] if _digit.isdigit() and 1 <= int(_digit) <= len(tiers) else None+ choice = _by_row.tier_id if _by_row else self._normalize_slash_confirm_choice(raw, choices)if not choice or choice == "cancel":print(" 🟡 Cancelled. No plan started.")returntier_url = subscription_manage_url(state, tier_id=choice) or manage_urlif not tier_url:- _cprint(f" {_d('No manage URL available — is your portal configured?')}")+ self._dim(_NO_MANAGE_URL)returnpicked = next((t for t in tiers if t.tier_id == choice), None)label = picked.name if picked else "your plan"@@ -353,21 +352,17 @@ class CLIBillingMixin:def _subscription_open_portal(self, state, manage_url, *, verb="Manage your subscription"):"""Open / copy the manage-subscription URL — the portal hand-off."""- from cli import _cprint, _d+ print()if not manage_url:- print()- _cprint(f" {_d('No manage URL available — is your portal configured?')}")+ self._dim(_NO_MANAGE_URL)return- print()choices = [("open", verb, "open the subscription page in your browser"),("copy", "Copy link", "copy the manage-subscription URL to your clipboard"),- ("cancel", "Cancel", "do nothing"),- ]+ ("cancel", "Cancel", "do nothing")]choice = self._modal_choice(verb, "", choices)if choice == "open":- if not self._open_url_in_browser(manage_url):- print(f" Open this URL: {manage_url}")+ self._open_or_print_url(manage_url)print()print(" Finish in your browser, then re-run /subscription.")elif choice == "copy":@@ -382,48 +377,35 @@ class CLIBillingMixin:def _subscription_change_menu(self, state, manage_url):"""The in-terminal change menu for a paid admin/owner (interactive)."""c = state.current- has_pending = bool(c and (c.cancel_at_period_end or c.pending_downgrade_tier_name))- keep_name = (c.tier_name if c else None) or "your plan"- # A scheduled change makes undo the likeliest intent → promote it first. The Close row uses- # value "close" (not "cancel") so typing "cancel" can't be confused with "Cancel subscription".- if has_pending:- choices = [- ("keep", f"Keep {keep_name} (undo the scheduled change)", "cancel the pending change"),- ("change", "Change plan", "upgrade or downgrade in the terminal"),- ]+ # A scheduled change makes undo the likeliest intent → promote it first. The Close row is+ # "close" (not "cancel") so typing "cancel" can't be confused with "Cancel subscription".+ if c and (c.cancel_at_period_end or c.pending_downgrade_tier_name):+ keep_name = c.tier_name or "your plan"+ head = [("keep", f"Keep {keep_name} (undo the scheduled change)", "cancel the pending change"), _CHANGE_PLAN_ROW]else:- choices = [- ("change", "Change plan", "upgrade or downgrade in the terminal"),- ("cancel_sub", "Cancel subscription", "schedule cancellation at period end"),- ]- choices.append(("portal", "Manage on portal", "open the billing page in your browser"))- choices.append(("close", "Close", "do nothing"))- choice = self._modal_choice("Manage your subscription", "", choices)- if choice == "change":- self._subscription_pick_tier(state)- elif choice == "keep":- self._subscription_apply(state, ("resume", None))- elif choice == "cancel_sub":- self._subscription_confirm_cancel(state)- elif choice == "portal":- self._subscription_open_portal(state, manage_url)+ head = [_CHANGE_PLAN_ROW, ("cancel_sub", "Cancel subscription", "schedule cancellation at period end")]+ choice = self._modal_choice("Manage your subscription", "", head + _CHANGE_MENU_TAIL)+ action = {+ "change": lambda: self._subscription_pick_tier(state),+ "keep": lambda: self._subscription_apply(state, ("resume", None)),+ "cancel_sub": lambda: self._subscription_confirm_cancel(state),+ "portal": lambda: self._subscription_open_portal(state, manage_url)}.get(choice)+ if action:+ action()else:print(" 🟡 Closed. No plan change.")def _subscription_pick_tier(self, state):- """Tier picker → preview → confirm (mirrors the TUI picker screen). Selectable = enabled paid- tiers other than current (dropping to free is a cancellation, on the change menu)."""+ """Tier picker → preview → confirm. Paid tiers other than current (dropping to free = cancellation)."""from agent.subscription_view import format_tier_row, is_upgrade, selectable_tiers-c = state.currentselectable = selectable_tiers(state)if not selectable:print(" No other plans are available to switch to right now.")return- choices = []- for t in selectable:- direction = "upgrade" if is_upgrade(state, t.tier_id) else "downgrade"- choices.append((t.tier_id, f"{format_tier_row(t)} · {direction}", f"switch to {t.name}"))+ choices = [+ (t.tier_id, f"{format_tier_row(t)} · {'upgrade' if is_upgrade(state, t.tier_id) else 'downgrade'}", f"switch to {t.name}")+ for t in selectable]choices.append(("cancel", "Back", "do nothing"))choice = self._modal_choice("Change plan", f"Current: {c.tier_name if c else 'Free'}. Pick a plan to preview the effect.", choices)if not choice or choice == "cancel":@@ -432,21 +414,18 @@ class CLIBillingMixin:self._subscription_preview_and_confirm(state, choice)def _subscription_preview_and_confirm(self, state, tier_id, *, allow_stepup=True):- """Preview the change (chargeless quote), show the effect, then confirm+apply.- ``allow_stepup=False`` (a post-grant replay) declines a second step-up on a repeated scope- denial so the flow can't re-prompt/re-open the browser in a loop."""+ """Preview → effect → confirm+apply. ``allow_stepup=False`` (post-grant replay) never re-prompts a step-up."""from cli import _cprint, _b, _dfrom agent.subscription_view import is_upgrade, subscription_change_preview_from_payload, subscription_manage_urlfrom hermes_cli.nous_billing import BillingError, BillingScopeRequired, post_subscription_preview-- _cprint(f" {_d('Checking the change…')}")+ self._dim('Checking the change…')try:payload = post_subscription_preview(subscription_type_id=tier_id)except BillingScopeRequired:if allow_stepup:self._subscription_handle_scope_required(state, retry=("preview", tier_id))else:- print(" Remote Spending still isn't active for this terminal — the authorization didn't take. Retry, or make this change on the portal.")+ print(_STEPUP_STALE_MSG)returnexcept BillingError as exc:self._subscription_render_error(state, exc)@@ -456,25 +435,22 @@ class CLIBillingMixin:target = p.target_tier_name or "the selected plan"print()if effect == "no_op":- _cprint(f" {_d(f'You are already on {target} — nothing to change.')}")+ self._dim(f"You are already on {target} — nothing to change.")returnif effect not in ("charge_now", "scheduled"):- # blocked OR unknown effect → fail SAFE (never schedule a real change on an unrecognized- # string) and re-offer the portal. plan= rides along only for an UPGRADE hand-off —- # downgrades stay native, so a blocked downgrade keeps the generic manage link.- _plan = tier_id if is_upgrade(state, tier_id) else None+ # blocked OR unknown effect → fail SAFE (never schedule on an unrecognized string) and+ # re-offer the portal. plan= rides along only for an UPGRADE hand-off (downgrades stay native)._cprint(f" 🟡 {p.reason or 'This change cannot be confirmed here — manage it on the portal.'}")+ _plan = tier_id if is_upgrade(state, tier_id) else None_mu = subscription_manage_url(state, tier_id=_plan)if _mu:print(f" Manage on portal: {_mu}")return+ _cprint(f" {_b('Confirm plan change')} {_d('· charged now' if effect == 'charge_now' else '· scheduled · not today')}")if effect == "charge_now":_amt = f"${p.amount_due_now_cents / 100:.2f}" if p.amount_due_now_cents is not None else None- _cprint(f" {_b('Confirm plan change')} {_d('· charged now')}")- if _amt:- _cprint(f" Upgrade to {target}. You will be charged {_amt} now (prorated).")- else:- _cprint(f" Upgrade to {target}. You will be charged the prorated amount now.")+ _charged = f"{_amt} now (prorated)" if _amt else "the prorated amount now"+ _cprint(f" Upgrade to {target}. You will be charged {_charged}.")# Best-effort: name the exact card, but only when the resolver rung matches what a# subscription charge actually uses (subPin / customerDefault — Stripe's precedence)._card_line = "The card on your subscription will be charged."@@ -486,26 +462,19 @@ class CLIBillingMixin:_card_line = f"{_c.masked} — the card on your subscription — will be charged."except Exception:pass- _cprint(f" {_d(_card_line)}")+ self._dim(_card_line)pay_label = f"Pay {_amt} & upgrade now" if _amt else "Upgrade now (prorated charge)"action = ("upgrade", tier_id)# The money-moving row is NOT the default — a bare Enter hits "Go back", so a stray keystroke can't charge.- confirm_choices = [- ("cancel", "Go back", "do not charge"),- ("yes", pay_label, "charge + upgrade now"),- ]+ confirm_choices = [("cancel", "Go back", "do not charge"), ("yes", pay_label, "charge + upgrade now")]else: # scheduled (whitelisted above)_when = p.effective_at[:10] if (p.effective_at and len(p.effective_at) >= 10) else "the end of the billing period"- _cprint(f" {_b('Confirm plan change')} {_d('· scheduled · not today')}")_cprint(f" Change to {target} — takes effect {_when}. No charge now; you keep your current plan until then.")pay_label = f"Schedule change to {target}"action = ("schedule", tier_id)- confirm_choices = [- ("yes", pay_label, "apply this change"),- ("cancel", "Go back", "do not change"),- ]+ confirm_choices = [("yes", pay_label, "apply this change"), ("cancel", "Go back", "do not change")]if p.monthly_credits_delta:- _cprint(f" {_d(f'Monthly credits change: {p.monthly_credits_delta}.')}")+ self._dim(f"Monthly credits change: {p.monthly_credits_delta}.")if self._modal_choice(pay_label, "", confirm_choices) != "yes":print(" 🟡 Cancelled. No plan change.")return@@ -515,33 +484,23 @@ class CLIBillingMixin:"""Confirm, then schedule a cancellation at period end."""from cli import _cprint, _b, _dfrom agent.billing_usage import format_renews-c = state.current_end = (format_renews(c.cycle_ends_at) if (c and c.cycle_ends_at) else None) or "the end of the billing period"print()_cprint(f" {_b('Confirm cancellation')} {_d('· scheduled · not today')}")_cprint(f" Cancel {(c.tier_name if c else 'your plan')} — it stays active until {_end}, then won't renew.")- _cprint(f" {_d('You keep your remaining credits for this period. You can resume before it ends.')}")- confirm_choices = [- ("yes", "Cancel subscription", "schedule cancellation at period end"),- ("cancel", "Go back", "keep your plan"),- ]- if self._modal_choice("Cancel subscription?", "", confirm_choices) != "yes":+ self._dim('You keep your remaining credits for this period. You can resume before it ends.')+ if self._modal_choice("Cancel subscription?", "", _CANCEL_SUB_CHOICES) != "yes":print(" 🟡 Cancelled. Your plan is unchanged.")returnself._subscription_apply(state, ("cancel", None))def _subscription_apply(self, state, action, idempotency_key=None, *, allow_stepup=True):- """Run the mutation for `action` — ("upgrade"|"schedule", tier_id) / ("cancel"|"resume", None) —- handling the scope step-up + the result. insufficient_scope routes to the step-up and replays;- the upgrade idempotency key is reused across the replay. ``allow_stepup=False`` (post-grant- replay) declines a second step-up so the flow can't re-prompt/re-open the browser in a loop."""- from cli import _cprint, _d, _DIM, _RST+ """Run ("upgrade"|"schedule", tier_id) / ("cancel"|"resume", None); scope denial → step-up + ONE replay, same key."""+ from cli import _cprintfrom hermes_cli.nous_billing import (BillingError, BillingTransient, BillingRemoteSpendingRevoked, BillingScopeRequired, BillingSessionRevoked,- delete_subscription_pending_change, post_subscription_upgrade, put_subscription_pending_change,- )-+ delete_subscription_pending_change, post_subscription_upgrade, put_subscription_pending_change)kind, arg = actionkey = Noneif kind == "upgrade":@@ -549,73 +508,55 @@ class CLIBillingMixin:key = idempotency_key or new_idempotency_key()try:if kind == "upgrade":- try:- res = post_subscription_upgrade(subscription_type_id=arg, idempotency_key=key) or {}- except BillingScopeRequired:- raise # a scope denial rejects BEFORE charging → route to the step-up- except (BillingTransient, BillingSessionRevoked, BillingRemoteSpendingRevoked) as exc:- # Deterministic PRE-charge rejections (429/401/403) never reached Stripe →- # the correct recovery copy, NOT the "maybe charged" ambiguity.- self._subscription_render_error(state, exc)- return- except BillingError as exc:- _status = getattr(exc, "status", None)- _code = getattr(exc, "error", None)- if _code in ("network_error", "endpoint_unavailable") or _status is None or _status >= 500:- # Genuinely INDETERMINATE (transport / unparseable 2xx / 5xx): NAS may already have- # charged. Steer to a re-check, never a blind retry (a fresh key can't dedup).- self._subscription_render_upgrade_ambiguous(exc)- else: # deterministic 4xx (role_required / no_payment_method / …)- self._subscription_render_error(state, exc)- return+ res = post_subscription_upgrade(subscription_type_id=arg, idempotency_key=key) or {}status = res.get("status")name = res.get("targetTierName") or "your new plan"- _url = res.get("recoveryUrl")- if status == "already_on_tier":- _cprint(f" {_DIM}✓ You are already on {name}.{_RST}")- elif status == "upgraded":- _cprint(f" {_DIM}✓ Upgraded to {name}. Your new monthly credits land in a moment.{_RST}")+ if status in _UPGRADE_OK_COPY:+ self._ok(_UPGRADE_OK_COPY[status].format(name=name))elif status in _UPGRADE_STATUS_COPY:line, echo_url = _UPGRADE_STATUS_COPY[status]_cprint(line)- if echo_url and _url:- _cprint(f" Portal: {_url}")+ if echo_url and res.get("recoveryUrl"):+ _cprint(f" Portal: {res.get('recoveryUrl')}")else: # unknown / absent 2xx status → also ambiguous, not a flat failureself._subscription_render_upgrade_ambiguous(None)return- if kind == "schedule":- put_subscription_pending_change(subscription_type_id=arg)- _cprint(f" {_DIM}✓ Scheduled — your plan doesn't change today. You keep it until the end of the billing period, then it switches.{_RST}")- elif kind == "cancel":- put_subscription_pending_change(cancel=True)- _cprint(f" {_DIM}✓ Scheduled — your plan stays active until the end of the billing period, then it cancels. Nothing changes today.{_RST}")- elif kind == "resume":- delete_subscription_pending_change()- _cprint(f" {_DIM}✓ Undone — you stay on your current plan.{_RST}")- _cprint(f" {_d('Re-run /subscription anytime to review it.')}")- except BillingScopeRequired:+ pending = {+ "schedule": (put_subscription_pending_change, {"subscription_type_id": arg}),+ "cancel": (put_subscription_pending_change, {"cancel": True}),+ "resume": (delete_subscription_pending_change, {})}.get(kind)+ if pending:+ pending[0](**pending[1])+ self._ok(_PENDING_OK_COPY[kind])+ self._dim('Re-run /subscription anytime to review it.')+ except BillingScopeRequired: # rejects BEFORE charging → route to the step-upif allow_stepup:self._subscription_handle_scope_required(state, retry=action, idempotency_key=key)else:- print(" Remote Spending still isn't active for this terminal — the authorization didn't take. Retry, or make this change on the portal.")+ print(_STEPUP_STALE_MSG)except BillingError as exc:- self._subscription_render_error(state, exc)+ # Upgrade only: deterministic PRE-charge rejections (Transient/401/403 types, 4xx codes)+ # never reached Stripe → recovery copy. Transport / 5xx is INDETERMINATE (NAS may have+ # charged) → steer to a re-check, never a blind retry (a fresh key can't dedup).+ _pre_charge = (BillingTransient, BillingSessionRevoked, BillingRemoteSpendingRevoked)+ _ambiguous = (exc.error in ("network_error", "endpoint_unavailable")+ or exc.status is None or exc.status >= 500)+ if kind == "upgrade" and _ambiguous and not isinstance(exc, _pre_charge):+ self._subscription_render_upgrade_ambiguous(exc)+ else:+ self._subscription_render_error(state, exc)def _subscription_handle_scope_required(self, state, *, retry, idempotency_key=None):- """insufficient_scope → allow remote spending (step-up), then replay `retry` ONCE so the- user never re-runs the command."""- from cli import _cprint, _DIM, _RST+ """insufficient_scope → step-up, then replay `retry` ONCE so the user never re-runs the command."""granted = self._step_up_remote_spending(explain="To change your plan from the terminal, allow Remote Spending once. It opens your browser to authorize, then your change picks up right here.",noninteractive_msg=" Run `hermes portal` and allow Remote Spending, then re-run /subscription.",declined_msg=" No change made. Allow Remote Spending when you're ready.",- not_granted_msg=" Couldn't allow Remote Spending — an org admin or owner has to approve it for this org.",- )+ not_granted_msg=" Couldn't allow Remote Spending — an org admin or owner has to approve it for this org.")if not granted:return- _cprint(f" {_DIM}✓ Remote Spending allowed.{_RST}")- # Bust the 30s token cache: it still holds the pre-grant unscoped token and _request only- # busts on a 401 (not a 403 scope denial) — without this the replay would 403 again.+ self._ok("Remote Spending allowed.")+ # Bust the 30s token cache (it still holds the pre-grant token; _request only busts on 401).try:from hermes_cli import nous_billing as _nb_nb.invalidate_cached_token()@@ -627,82 +568,75 @@ class CLIBillingMixin:fresh = build_subscription_state()except Exception:fresh = state- rkind, rarg = retry- if rkind == "preview":- self._subscription_preview_and_confirm(fresh, rarg, allow_stepup=False)+ if retry[0] == "preview":+ self._subscription_preview_and_confirm(fresh, retry[1], allow_stepup=False)else:self._subscription_apply(fresh, retry, idempotency_key=idempotency_key, allow_stepup=False)def _subscription_render_error(self, state, exc):"""Render a subscription BillingError (a lighter _billing_render_charge_error)."""from cli import _cprint- code = getattr(exc, "error", None)msg = str(exc) or "Something went wrong."- if code == "insufficient_scope": # defensive: the flow routes scope to the step-up before here+ if exc.error == "insufficient_scope": # defensive: the flow routes scope to the step-up before here_cprint(" 🟡 Remote Spending isn't allowed yet. Allow it, then retry.")- elif code in ("subscription_mutation_rejected", "preview_rejected"):+ elif exc.error in ("subscription_mutation_rejected", "preview_rejected"):_cprint(f" 🟡 {msg}")else:_cprint(f" 🔴 {msg}")self._print_portal_line(exc)def _subscription_render_upgrade_ambiguous(self, exc):- """A charge-route failure (transport / timeout / 500 / unknown status) is AMBIGUOUS — NAS may- already have charged. Steer to a re-check, never a flat failure that invites a blind retry- (the CLI can't persist the key across a command re-run)."""- from cli import _cprint, _d+ """AMBIGUOUS outcome (NAS may have charged) → steer to a re-check, never a blind retry (key isn't persisted)."""+ from cli import _cprint_cprint(" 🟡 Couldn't confirm the upgrade — your card may or may not have been charged.")- _cprint(f" {_d('Re-run /subscription to check your plan before trying again.')}")+ self._dim('Re-run /subscription to check your plan before trying again.')self._print_portal_line(exc)- # ------------------------------------------------------------------- # /topup — Remote Spending (CLI surface, all 5 screens)- # ------------------------------------------------------------------+ # ── /topup — Remote Spending (CLI surface, all 5 screens) ──def _show_billing(self, command: str = "/topup"):- """`/topup` — Remote Spending for Nous. ZERO sub-commands: any argument is ignored and the- Overview menu is the only way to reach Buy / Auto-reload / Monthly-limit. Non-interactive- contexts render text + the portal deep-link, never prompting. All money is Decimal end-to-end;- the terminal never collects card details."""- from cli import _cprint, _d+ """`/topup` — ZERO sub-commands (argument ignored; Overview is the only route). Non-interactive never+ prompts. Money is Decimal end-to-end; the terminal never collects card details."""from agent.billing_view import build_billing_state-state = build_billing_state()if not state.logged_in:- print()- if state.error:- _msg = f"Couldn't load billing: {state.error}"- _cprint(f" 💳 {_d(_msg)}")- else:- _cprint(f" 💳 {_d('Not logged into Nous Portal.')}")- print(" Run `hermes portal` to log in, then /topup.")+ self._print_logged_out(state, "Couldn't load billing", "/topup")returnself._billing_overview(state)def _billing_portal_hint(self, state, *, reason: str = "") -> None:"""Print a portal deep-link line (the funnel for portal-only actions)."""- url = getattr(state, "portal_url", None)- if not url:+ if not state.portal_url:returnif reason:print(f" {reason}")- print(f" Manage on portal: {url}")+ print(f" Manage on portal: {state.portal_url}")++ def _billing_require_admin(self, state, *, icon="💳 ", off_reason=_KILLSWITCH_REASON_BUY) -> bool:+ """Admin + org kill-switch gate; portal funnel + False when blocked. ``icon`` adds a blank line + prefix."""+ if state.can_change_plan and state.cli_billing_enabled:+ return True+ if icon:+ print()+ if not state.can_change_plan:+ self._dim('Billing actions require an org admin/owner.', icon=icon)+ self._billing_portal_hint(state)+ else:+ self._dim('Remote spending is off for this org.', icon=icon)+ self._billing_portal_hint(state, reason=off_reason)+ return Falsedef _billing_overview(self, state):- """Screen 1 — balance in title, two-bar dollar usage, action menu (Add funds first).- No scope preflight — remote spending is discovered reactively when a charge 403s. A missing- card does NOT gate the overview either: it only matters at CHARGE time (the buy flow hands off)."""- from cli import _cprint, _b, _d+ """Screen 1 — balance, bars, menu. No scope preflight (a charge 403s); a missing card does NOT gate it."""+ from cli import _cprint, _bfrom agent.billing_view import format_money-usage = self._try_usage_model()print()_cprint(f" 💳 {_b(f'Top up · balance {format_money(state.balance_usd)}')}")self._print_org_line(state)print(f" {_RULE}")- for _bar_ln in self._usage_bar_lines(usage, getattr(usage, "plan_name", None)):+ for _bar_ln in self._usage_bar_lines(usage, usage.plan_name if usage else None):print(_bar_ln)-ar = state.auto_reloadif ar is not None:if ar.enabled:@@ -713,101 +647,54 @@ class CLIBillingMixin:if state.card is not None:print(f" Card: {state.card.display}")else:- _cprint(f" {_d('No saved card on file — “Add funds” walks you through adding one.')}")+ self._dim('No saved card on file — “Add funds” walks you through adding one.')print(f" {_RULE}")-# Action gating: admin + kill-switch for charge/auto-reload; everyone gets portal.- if not state.can_change_plan:- _cprint(f" {_d('Billing actions require an org admin/owner.')}")- self._billing_portal_hint(state)- return- if not state.cli_billing_enabled:- _cprint(f" {_d('Remote spending is off for this org.')}")- self._billing_portal_hint(state, reason="A billing admin can turn it on from the portal's Hermes Agent page to add funds here.")+ if not self._billing_require_admin(state, icon="", off_reason=_KILLSWITCH_REASON_OVERVIEW):returnif not self._app: # non-interactive: no modal, just the portal funnelself._billing_portal_hint(state)return-# One-time vs automatic — the distinction stated up front in each first sentence.- _cprint(f" {_d('Add funds now — a single charge, added to your balance today.')}")- if (- ar is not None and ar.enabled- and ar.reload_to_usd is not None and ar.reload_to_usd.is_finite()- and ar.threshold_usd is not None and ar.threshold_usd.is_finite()- ):+ self._dim('Add funds now — a single charge, added to your balance today.')+ _amounts = [ar.reload_to_usd, ar.threshold_usd] if ar is not None and ar.enabled else [None]+ if all(a is not None and a.is_finite() for a in _amounts):_auto_line = f"Refill when low — charges {format_money(ar.reload_to_usd)} automatically when your balance falls below {format_money(ar.threshold_usd)}."else:_auto_line = "Refill when low — charges your card automatically when your balance falls below the amount you set."- _cprint(f" {_d(_auto_line)}")+ self._dim(_auto_line)print(f" {_RULE}")-- # No "Allow Remote Spending" item — discovered at pay time. "Add funds" charges in-terminal- # against the org's portal-saved card (server-held; no card ref leaves the client).- choices = [- ("buy", "Add funds", "a single charge, added to your balance today"),- ("auto", "Auto-reload", "refill automatically when your balance runs low"),- ("limit", "Monthly limit", "show the monthly spend cap (read-only)"),- ("portal", "Manage on portal", "open the billing page in your browser"),- ("cancel", "Cancel", "do nothing"),- ]- choice = self._modal_choice("Top up your balance", "", choices)- if choice == "buy":- self._billing_buy_flow(state)- elif choice == "auto":- self._billing_auto_reload_flow(state)- elif choice == "limit":- self._billing_limit_screen(state)- elif choice == "portal":- self._billing_open_portal(state)+ # No "Allow Remote Spending" item — discovered at pay time. "Add funds" charges the org's+ # portal-saved card (server-held; no card ref leaves the client).+ action = {+ "buy": self._billing_buy_flow,+ "auto": self._billing_auto_reload_flow,+ "limit": self._billing_limit_screen,+ "portal": self._billing_open_portal}.get(self._modal_choice("Top up your balance", "", _TOPUP_MENU_CHOICES))+ if action:+ action(state)else:print(" Cancelled.")def _billing_open_portal(self, state):- url = getattr(state, "portal_url", None)- if not url:+ if not state.portal_url:print(" No portal URL available.")return- if not self._open_url_in_browser(url):- print(f" Open this URL: {url}")+ self._open_or_print_url(state.portal_url)print(" Complete billing changes in the browser.")- def _billing_require_admin(self, state) -> bool:- """Guard charge/auto-reload entry points; print + return False if blocked."""- from cli import _cprint, _d- if not state.can_change_plan:- print()- _cprint(f" 💳 {_d('Billing actions require an org admin/owner.')}")- self._billing_portal_hint(state)- return False- if not state.cli_billing_enabled:- print()- _cprint(f" 💳 {_d('Remote spending is off for this org.')}")- self._billing_portal_hint(state, reason="A billing admin can turn it on from the portal's Hermes Agent page before adding funds.")- return False- return True-def _billing_add_card_flow(self, state):- """No saved card → guide adding one on the portal (never in-terminal), with a bounded re-check- loop so the purchase continues right here once the card is saved (also recovers a transient- miss — the card display is best-effort server-side). Returns refreshed state, or None to abandon."""- from cli import _cprint, _b, _d, _DIM, _RST- print()- _cprint(f" 💳 {_b('Add a card first')}")+ """No card → add it on the portal (never in-terminal), bounded re-check loop. Refreshed state, or None."""+ from cli import _cprint+ self._block_header("💳", "Add a card first", rule=False)_cprint(" No saved card on file.")- _cprint(f" {_d('Add a card once on the portal billing page — after that you can top up right from the terminal.')}")- choices = [- ("portal", "Add a card on the portal", "opens the billing page in your browser"),- ("recheck", "I've added it — check again", "re-check for the card and continue"),- ("cancel", "Back", "do nothing"),- ]+ self._dim('Add a card once on the portal billing page — after that you can top up right from the terminal.')for _ in range(8): # bounded: portal-open plus a handful of re-checks- choice = self._modal_choice("Add a card", "", choices)+ choice = self._modal_choice("Add a card", "", _ADD_CARD_CHOICES)if choice == "portal":self._billing_open_portal(state)- _cprint(f" {_d('Add the card on the billing page, then pick “check again” here.')}")- continue- if choice == "recheck":+ self._dim('Add the card on the billing page, then pick “check again” here.')+ elif choice == "recheck":from agent.billing_view import build_billing_statetry:fresh = build_billing_state()@@ -816,28 +703,22 @@ class CLIBillingMixin:if fresh is not None and fresh.logged_in:state = freshif state.card is not None:- _cprint(f" {_DIM}✓ Card found: {state.card.display} — continuing.{_RST}")+ self._ok(f"Card found: {state.card.display} — continuing.")return stateprint(" Still no card on file — finish adding it on the portal, then check again.")- continue- break+ else:+ breakprint(" Cancelled. No funds added.")return Nonedef _billing_buy_flow(self, state):- """Screen 2 (preset select) → Screen 3 (confirm + charge + poll). No scope preflight: the- charge flies and we react to the server's 403 order (insufficient_scope → in-flight reauth,- no_payment_method → portal hand-off)."""- from cli import _cprint, _b+ """Screen 2 (presets) → Screen 3 (confirm+charge+poll). No scope preflight: react to the server's 403s."""from agent.billing_view import format_money, validate_charge_amount-if not self._billing_require_admin(state):returnif not self._app:- presets = ", ".join(format_money(p) for p in state.charge_presets)- print()- _cprint(f" 💳 {_b('Add funds')}")- print(f" Presets: {presets}")+ self._block_header("💳", "Add funds", rule=False)+ print(f" Presets: {', '.join(format_money(p) for p in state.charge_presets)}")print(" Run this in the interactive CLI to complete a purchase.")self._billing_portal_hint(state)return@@ -845,7 +726,6 @@ class CLIBillingMixin:state = self._billing_add_card_flow(state)if state is None or state.card is None:return-preset_choices = [(str(p), format_money(p), "one-time credit purchase") for p in state.charge_presets]preset_choices.append(("custom", "Custom amount…", "enter your own amount"))preset_choices.append(("cancel", "Cancel", "do nothing"))@@ -854,7 +734,6 @@ class CLIBillingMixin:if not choice or choice == "cancel":print(" Cancelled. No funds added.")return-from decimal import Decimalif choice == "custom":entered = self._prompt_text_input(" Amount (USD): ")@@ -876,27 +755,20 @@ class CLIBillingMixin:def _billing_confirm_and_charge(self, state, amount):"""Screen 3 — confirm total + consent, charge, then poll to settlement."""- from cli import _cprint, _b, _dfrom agent.billing_view import format_money, new_idempotency_key- from hermes_cli.nous_billing import BillingError, BillingScopeRequired, post_charge-card = state.card- print()- _cprint(f" 💳 {_b('Confirm purchase')}")- print(f" {_RULE}")+ self._block_header("💳", "Confirm purchase")print(f" Total: {format_money(amount)}")if card:print(f" Payment: {card.display}")if card.provenance is None: # older NAS without provenance → generic line- _cprint(f" {_d('Your card saved on the portal will be charged.')}")+ self._dim('Your card saved on the portal will be charged.')print(f" {_RULE}")- _cprint(f" {_d('By confirming, you allow Nous Research to charge your card.')}")-+ self._dim('By confirming, you allow Nous Research to charge your card.')confirm_choices = [("pay", f"Pay {format_money(amount)} now", "submit the charge"),("portal", "Manage on portal", "manage your card / billing in the browser"),- ("cancel", "Go back", "do not charge"),- ]+ ("cancel", "Go back", "do not charge")]if not self._app:print(" Run in the interactive CLI to confirm a purchase.")return@@ -907,21 +779,29 @@ class CLIBillingMixin:if choice != "pay":print(" Cancelled. No funds added.")return-key = new_idempotency_key() # reused on the post-step-up resume so a double-submit collapses+ self._billing_submit_and_poll(+ state, amount, key, missing_msg=" 🔴 No charge id returned; please check the portal.",+ status_msg="Charge submitted — confirming settlement…",+ on_scope=lambda: self._billing_handle_scope_required(state, amount=amount, idempotency_key=key))++ def _billing_submit_and_poll(self, state, amount, key, *, missing_msg, status_msg, on_scope=None):+ """POST the charge, then poll. ``on_scope`` handles a scope denial (first submit); else it renders."""+ from cli import _cprint, _d+ from hermes_cli.nous_billing import BillingError, BillingScopeRequired, post_chargetry:result = post_charge(amount_usd=amount, idempotency_key=key)- except BillingScopeRequired:- self._billing_handle_scope_required(state, amount=amount, idempotency_key=key)- returnexcept BillingError as exc:- self._billing_render_charge_error(state, exc)+ if on_scope is not None and isinstance(exc, BillingScopeRequired):+ on_scope()+ else:+ self._billing_render_charge_error(state, exc)returncharge_id = result.get("chargeId")if not charge_id:- print(" 🔴 No charge id returned; please check the portal.")+ print(missing_msg)return- _cprint(f" {_d('Charge submitted — confirming settlement…')}")+ _cprint(f" {_d(status_msg)}")self._billing_poll_charge(state, charge_id, amount)def _billing_poll_charge(self, state, charge_id, amount):@@ -929,7 +809,6 @@ class CLIBillingMixin:import time as _timefrom agent.billing_view import format_money, parse_moneyfrom hermes_cli.nous_billing import BillingError, BillingTransient, get_charge_status-deadline = _time.time() + 300while _time.time() < deadline:try:@@ -943,8 +822,7 @@ class CLIBillingMixin:state_str = status.get("status")if state_str == "settled":amt = status.get("amountUsd")- shown = format_money(parse_money(amt)) if amt else format_money(amount)- print(f" ✓ {shown} added to your balance.")+ print(f" ✓ {format_money(parse_money(amt)) if amt else format_money(amount)} added to your balance.")returnif state_str == "failed":self._billing_render_charge_failed(state, status.get("reason"))@@ -960,29 +838,24 @@ class CLIBillingMixin:self._billing_portal_hint(state)def _billing_render_charge_error(self, state, exc):- """Render a typed BillingError at submit time (pre-poll). Order matters: revoked/session- checks precede code lookups, and Transient precedes the insufficient_scope fallback."""+ """Submit-time BillingError. Order matters: revoked/session before code lookups; Transient before scope."""from hermes_cli.nous_billing import BillingTransient, BillingRemoteSpendingRevoked, BillingSessionRevoked-- code = getattr(exc, "error", None)- portal_url = getattr(exc, "portal_url", None) or getattr(state, "portal_url", None)+ code = exc.error+ portal_url = exc.portal_url or state.portal_urlif isinstance(exc, BillingRemoteSpendingRevoked) or code == "remote_spending_revoked":# This terminal's spend was revoked; recovery is reconnect.- who = "An admin stopped this terminal's spending." if getattr(exc, "actor", None) == "admin" else "You stopped this terminal's spending."+ who = "An admin stopped this terminal's spending." if exc.actor == "admin" else "You stopped this terminal's spending."print(f" 🔴 {who} Reconnect to restore — run `hermes portal` to re-authorize.")elif isinstance(exc, BillingSessionRevoked) or code == "session_revoked":print(" 🔴 Your session was logged out. Run `hermes portal` to log in again.")- elif code == "no_payment_method":- print(_CHARGE_ERROR_COPY[code])- elif code in ("cli_billing_disabled", "remote_spending_disabled") or getattr(exc, "code", None) == "remote_spending_disabled":- print(_CHARGE_ERROR_COPY["cli_billing_disabled"]) # dual error/code gate payload- elif code in _CHARGE_ERROR_COPY: # role_required / idempotency_conflict- print(_CHARGE_ERROR_COPY[code])+ elif code in _CHARGE_ERROR_COPY or exc.code == "remote_spending_disabled":+ # Fixed copy by `error`; the gate's dual error/code payload may carry it in `.code` only.+ print(_CHARGE_ERROR_COPY.get(code) or _CHARGE_ERROR_COPY["cli_billing_disabled"])elif code == "monthly_cap_exceeded":- remaining = (getattr(exc, "payload", {}) or {}).get("remainingUsd")+ remaining = (exc.payload or {}).get("remainingUsd")print(f" 🔴 Monthly spend cap reached — ${remaining} headroom left." if remaining is not None else " 🔴 Monthly spend cap reached.")elif isinstance(exc, BillingTransient):- wait = getattr(exc, "retry_after", None)+ wait = exc.retry_aftermins = f" (try again in ~{max(1, round(wait / 60))} min)" if wait else ""print(f" 🟡 Too many charges right now{mins}. This isn't a payment failure.")elif code == "insufficient_scope":@@ -994,24 +867,17 @@ class CLIBillingMixin:print(f" Portal: {portal_url}")def _billing_handle_scope_required(self, state, *, amount=None, idempotency_key=None):- """403 insufficient_scope → in-flight reauth, then resume the held ``amount`` on an explicit- press-Enter confirm, reusing ``idempotency_key`` so the resumed charge collapses with the- original. Never leaks the raw billing:manage scope."""- from cli import _cprint, _d+ """403 insufficient_scope → reauth, then resume ``amount`` on explicit confirm, reusing the idempotency key."""from agent.billing_view import build_billing_state, format_money, new_idempotency_key-amount_str = format_money(amount) if amount is not None else "your top-up"granted = self._step_up_remote_spending(explain=f"To charge from this terminal, allow Remote Spending once. It opens your browser to authorize, then {amount_str} picks up right here.",noninteractive_msg=" Run `hermes portal` and allow Remote Spending, then retry.",declined_msg=" No charge made. Run /topup when you want to allow Remote Spending.",- not_granted_msg=" Couldn't allow Remote Spending — an org admin or owner has to approve it. Your card was not charged.",- )+ not_granted_msg=" Couldn't allow Remote Spending — an org admin or owner has to approve it. Your card was not charged.")if not granted:return-- # The token now carries the scope, but the ORG kill-switch (cli_billing_enabled) is a separate- # gate — re-fetch /state so we don't over-promise.+ # The token now has the scope, but the ORG kill-switch is a separate gate — re-fetch /state.fresh = build_billing_state()if not (fresh.logged_in and fresh.cli_billing_enabled):print(" Remote Spending is allowed for this terminal, but it's still off for this org. A billing admin can turn it on from the portal's Hermes Agent page, then run /topup again.")@@ -1019,84 +885,59 @@ class CLIBillingMixin:returnif fresh.card is None: # half-done state: say so rather than a bare "✓ enabled"print(" ✓ Remote Spending allowed — but there's no card on file yet.")- _cprint(f" {_d('Top up and manage billing on the portal to continue.')}")+ self._dim('Top up and manage billing on the portal to continue.')self._billing_portal_hint(fresh)returnif amount is None: # scope-required hit outside a charge (e.g. auto-reload config)print(" ✓ Remote Spending allowed. Run /topup to continue.")return-print(" ✓ Remote Spending allowed.")resume_choices = [("resume", f"Resume {format_money(amount)} top-up", "finish the held purchase"),- ("cancel", "Cancel", "do not charge"),- ]+ ("cancel", "Cancel", "do not charge")]if self._modal_choice("Resume your top-up", f"{format_money(amount)} is ready to finish — press Enter to resume.", resume_choices) != "resume":print(" Cancelled. No funds added.")return-- from hermes_cli.nous_billing import BillingError, post_charge- key = idempotency_key or new_idempotency_key()- try:- result = post_charge(amount_usd=amount, idempotency_key=key)- except BillingError as exc:- self._billing_render_charge_error(fresh, exc)- return- charge_id = result.get("chargeId")- if not charge_id:- print(" No charge id returned; please check the portal.")- return- _cprint(f" {_d('Resuming your top-up — confirming settlement…')}")- self._billing_poll_charge(fresh, charge_id, amount)+ self._billing_submit_and_poll(+ fresh, amount, idempotency_key or new_idempotency_key(),+ missing_msg=" No charge id returned; please check the portal.",+ status_msg="Resuming your top-up — confirming settlement…")def _billing_auto_reload_flow(self, state):- """Screen 4 — auto-reload config: threshold + reload-to → PATCH. Prefills current values;- validates both (2dp, within bounds, ``reload_to > threshold``); offers "Turn off" when already on."""- from cli import _cprint, _b, _d+ """Screen 4 — threshold + reload-to → PATCH. Prefills; validates ``reload_to > threshold``; "Turn off" if on."""from agent.billing_view import format_money, validate_charge_amount-if not self._billing_require_admin(state):returncard = state.cardar = state.auto_reloadcurrently_on = bool(ar and ar.enabled)-- print()- _cprint(f" 💳 {_b('Auto-reload')}")- print(f" {_RULE}")- _cprint(f" {_d('Automatically add funds when your balance is low.')}")+ self._block_header("💳", "Auto-reload")+ self._dim('Automatically add funds when your balance is low.')if card:print(f" Card on file: {card.masked}")else:print(" No saved card — manage billing on the portal.")self._billing_portal_hint(state)return+ _current = f"below {format_money(ar.threshold_usd)} → reload to {format_money(ar.reload_to_usd)}" if currently_on else ""if currently_on:- print(f" Currently: below {format_money(ar.threshold_usd)} → reload to {format_money(ar.reload_to_usd)}")+ print(f" Currently: {_current}")if not self._app:print(" Run in the interactive CLI to configure auto-reload.")self._billing_portal_hint(state)return-if currently_on: # let the user turn it off without re-entering values- top_choices = [- ("edit", "Edit thresholds", "change when / how much to reload"),- ("off", "Turn off", "disable auto-reload"),- ("cancel", "Cancel", "do nothing"),- ]- top = self._modal_choice("Auto-reload", f"On — below {format_money(ar.threshold_usd)} → reload to {format_money(ar.reload_to_usd)}", top_choices)+ top = self._modal_choice("Auto-reload", f"On — {_current}", _AUTO_RELOAD_TOP_CHOICES)if top == "off":self._billing_auto_reload_disable(state)returnif top != "edit":print(" 🟡 Cancelled.")return-_CANCELLED = object()def _ask_amount(label, current):- """Prompt one amount; empty input keeps `current` when editing. Returns the Decimal (or the- kept current value, possibly None), or _CANCELLED after printing the cancel/validation message."""+ """One amount; empty keeps `current` when editing. Decimal / kept value, or _CANCELLED (already printed)."""cur = format_money(current) if currently_on else Noneraw = self._prompt_text_input(f" {label} (USD)" + (f" [{cur}]: " if cur else ": "))if raw is None: # cancelled (e.g. slash-worker can't prompt off-thread)@@ -1109,32 +950,22 @@ class CLIBillingMixin:print(f" 🔴 {v.error}")return _CANCELLEDreturn v.amount-threshold_amt = _ask_amount("When balance falls below", ar.threshold_usd if currently_on else None)- if threshold_amt is _CANCELLED:- return- reload_amt = _ask_amount("Reload balance to", ar.reload_to_usd if currently_on else None)+ reload_amt = _ask_amount("Reload balance to", ar.reload_to_usd if currently_on else None) if threshold_amt is not _CANCELLED else _CANCELLEDif reload_amt is _CANCELLED:returnif reload_amt is None or threshold_amt is None or reload_amt <= threshold_amt:print(" 🔴 Reload-to amount must be greater than the threshold.")return-- print()- _cprint(f" {_d(f'By confirming, you authorize Nous Research to charge {card.masked} whenever your balance reaches {format_money(threshold_amt)}. Turn off any time here or on the portal.')}")- confirm_choices = [- ("agree", "Agree and turn on", "enable auto-reload"),- ("cancel", "Cancel", "do nothing"),- ]- if self._modal_choice("Turn on auto-reload?", f"Below {format_money(threshold_amt)} → reload to {format_money(reload_amt)}", confirm_choices) != "agree":+ self._dim(f"By confirming, you authorize Nous Research to charge {card.masked} whenever your balance reaches {format_money(threshold_amt)}. Turn off any time here or on the portal.", lead=True)+ if self._modal_choice("Turn on auto-reload?", f"Below {format_money(threshold_amt)} → reload to {format_money(reload_amt)}", _AUTO_RELOAD_AGREE_CHOICES) != "agree":print(" 🟡 Cancelled.")returnif self._billing_patch_auto_top_up(state, enabled=True, threshold=float(threshold_amt), top_up_amount=float(reload_amt)):print(f" ✅ Auto-reload on: below {format_money(threshold_amt)} → reload to {format_money(reload_amt)}.")def _billing_patch_auto_top_up(self, state, **kwargs) -> bool:- """PATCH auto-top-up, routing scope denials to the step-up and other errors to the renderer.- True on success."""+ """PATCH auto-top-up; scope denials → step-up, other errors → renderer. True on success."""from hermes_cli.nous_billing import BillingError, BillingScopeRequired, patch_auto_top_uptry:patch_auto_top_up(**kwargs)@@ -1147,8 +978,7 @@ class CLIBillingMixin:return Truedef _billing_auto_reload_disable(self, state):- """Turn off auto-reload (PATCH ``enabled:false``). The endpoint requires ``threshold``/- ``topUpAmount`` even when disabling, so echo back the current values (fallback 0)."""+ """PATCH ``enabled:false``; the endpoint still requires threshold/topUpAmount → echo current (or 0)."""ar = state.auto_reloadthr = float(ar.threshold_usd) if ar and ar.threshold_usd is not None else 0.0rel = float(ar.reload_to_usd) if ar and ar.reload_to_usd is not None else 0.0@@ -1157,17 +987,13 @@ class CLIBillingMixin:def _billing_limit_screen(self, state):"""Screen 5 — monthly spend limit (read-only; cap is portal-only)."""- from cli import _cprint, _b, _dfrom agent.billing_view import format_money-- print()- _cprint(f" 💳 {_b('Monthly spend limit')}")- print(f" {_RULE}")+ self._block_header("💳", "Monthly spend limit")cap = state.monthly_capif cap is None or cap.limit_usd is None:- _cprint(f" {_d('No monthly cap visible (managed on the portal).')}")+ self._dim('No monthly cap visible (managed on the portal).')else:ceiling = " (default ceiling)" if cap.is_default_ceiling else ""print(f" {format_money(cap.spent_this_month_usd)} of {format_money(cap.limit_usd)} used this month{ceiling}")- _cprint(f" {_d('The monthly limit is set on the portal — the terminal shows it read-only.')}")+ self._dim('The monthly limit is set on the portal — the terminal shows it read-only.')self._billing_portal_hint(state)