diff --git a/loopx/quota.py b/loopx/quota.py index eded708cd4..3a6c6a7684 100644 --- a/loopx/quota.py +++ b/loopx/quota.py @@ -399,7 +399,12 @@ def goal_quota_with_spend_ledger( current_time = now or datetime.now(timezone.utc).astimezone() if current_time.tzinfo is None: current_time = current_time.replace(tzinfo=timezone.utc) - window_start = current_time - timedelta(hours=int(payload["window_hours"])) + try: + window_start = current_time - timedelta(hours=int(payload["window_hours"])) + except OverflowError: + # A window longer than datetime can represent still covers every + # representable event. It must not crash the Goal-wide history read. + window_start = datetime.min.replace(tzinfo=current_time.tzinfo) contributions: list[tuple[str, str, int]] = [] spend_event_count = 0 void_event_count = 0 diff --git a/tests/control_plane/test_quota_rolling_window_projection.py b/tests/control_plane/test_quota_rolling_window_projection.py index 477dce99fb..9e78f55153 100644 --- a/tests/control_plane/test_quota_rolling_window_projection.py +++ b/tests/control_plane/test_quota_rolling_window_projection.py @@ -3,6 +3,8 @@ from datetime import datetime, timezone from typing import Any +import pytest + from loopx.quota import goal_quota_with_spend_ledger @@ -123,3 +125,20 @@ def test_rolling_counter_isolated_by_exact_goal_instance() -> None: assert current["spend_event_count"] == 1 assert legacy_owner["spent_slots"] == 1 assert legacy_owner["spend_event_count"] == 1 + + +@pytest.mark.parametrize("window_hours", [10**8, 10**12, 1e12]) +def test_unrepresentable_window_covers_all_history_without_crashing( + window_hours: float, +) -> None: + goal = _goal() + goal["quota"]["window_hours"] = window_hours + spent = _spend("2026-01-01T00:00:00+00:00", "turn-1") + + ledger = goal_quota_with_spend_ledger( + goal, + [spent], + now=datetime(2026, 6, 1, tzinfo=timezone.utc), + ) + + assert ledger["spent_slots"] == 1