Skip to content

The agent that grows with you

fix(usage-window): keep an allowance whose reset is unknown

Tekniumd0d6286739a8
diff --git a/agent/account_usage.py b/agent/account_usage.py
index 5512d180eee..6f648870d71 100644
--- a/agent/account_usage.py
+++ b/agent/account_usage.py
@@ -202,13 +202,13 @@ def _nous_snapshot(windows: list, details: list, tail: list, *, source: str, pla
def _bp_window(label: str, used_bp: Any, resets_at_ms: Any) -> Optional[AccountUsageWindow]:
- """One usage-window gauge row from basis points + an epoch-ms reset; None when either is not an int."""
- if isinstance(used_bp, bool) or not isinstance(used_bp, int) or isinstance(resets_at_ms, bool) or not isinstance(resets_at_ms, int):
+ """One usage-window gauge row from basis points + an epoch-ms reset (already bounded by
+ ``usage_gauges.parse_gauges``); None when the percent is not an int. An unknown reset keeps the row,
+ shown without a reset."""
+ if isinstance(used_bp, bool) or not isinstance(used_bp, int):
return None
- try:
- reset_at = datetime.fromtimestamp(resets_at_ms / 1000, tz=UTC)
- except (ValueError, OverflowError, OSError):
- return None # an implausible server stamp drops the row, never the whole /usage view
+ known = isinstance(resets_at_ms, int) and not isinstance(resets_at_ms, bool)
+ reset_at = datetime.fromtimestamp(resets_at_ms / 1000, tz=UTC) if known else None
return AccountUsageWindow(label=label, used_percent=max(0.0, min(100.0, used_bp / BP_FULL * 100.0)),
reset_at=reset_at)
diff --git a/tests/agent/test_credits_usage_window.py b/tests/agent/test_credits_usage_window.py
index 81f273b89d5..125e75e6020 100644
--- a/tests/agent/test_credits_usage_window.py
+++ b/tests/agent/test_credits_usage_window.py
@@ -766,6 +766,19 @@ class TestUsageCommand:
"resets_at_ms": now + 3600_000}, *_gauges(100, 200, **kw)]
assert next(w.label for w in account_usage.build_nous_credits_snapshot(info).windows) == "Next 5 hours"
+ def test_an_unknown_reset_keeps_the_allowance_row(self):
+ """A valid percent with no usable reset still shows as a window row (no reset phrase), never the
+ dollar view: the window plan's details stay on screen."""
+ from agent import account_usage
+ gauges = [{"id": "day", "scope": "all", "label": "Today", "used_bp": 4_200},
+ {"id": "week", "scope": "all", "label": "This week", "used_bp": 1_000, "resets_at_ms": "soon"}]
+ info = SimpleNamespace(logged_in=True, paid_service_access=True, paid_service_access_info=None, subscription=None,
+ usage_window=SimpleNamespace(gauges=gauges, paused_reason=None))
+ snap = account_usage.build_nous_credits_snapshot(info)
+ assert snap.usage_window and [(w.label, round(w.used_percent), w.reset_at) for w in snap.windows] == [
+ ("Today", 42, None), ("This week", 10, None)]
+ assert not any("resets" in line for line in account_usage.render_account_usage_lines(snap))
+
def test_header_gauge_raises_stale_portal_percents(self, monkeypatch):
from agent import account_usage
monkeypatch.setattr("hermes_cli.nous_account.nous_portal_topup_url", lambda info: "https://portal/topup")