Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion loopx/quota.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions tests/control_plane/test_quota_rolling_window_projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from datetime import datetime, timezone
from typing import Any

import pytest

from loopx.quota import goal_quota_with_spend_ledger


Expand Down Expand Up @@ -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
Loading