From 359006d78ad6b07875429c8e257abfa58d6e469a Mon Sep 17 00:00:00 2001 From: rohangoud17 Date: Fri, 13 Nov 2026 17:19:16 -0500 Subject: [PATCH 1/7] fix(spend): refuse --days above 365 instead of crashing with OverflowError (#107) --- src/gulliver/runaway.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/gulliver/runaway.py b/src/gulliver/runaway.py index 03a55d5..1ce6c5e 100644 --- a/src/gulliver/runaway.py +++ b/src/gulliver/runaway.py @@ -44,6 +44,7 @@ REPEAT_FREE_USD = 0.01 # repeats cheaper than this ... REPEAT_FREE_BYTES = 1024**3 # ... and smaller than this are never treated as a loop SPEND_BY = ("agent", "day", "table", "session", "task", "connection", "kind") +MAX_SPEND_DAYS = 365 # same limit as `top`; the ledger itself has no retention limit _NONDETERMINISTIC = {"CurrentDate", "CurrentTimestamp", "CurrentTime", "CurrentDatetime", "CurrentTimestampLTZ", "Localtimestamp", "Localtime", "Rand", "Randn", "Uuid", "TableSample"} @@ -313,6 +314,9 @@ def spend(settings: Settings, by: str = "agent", days: int = 7) -> dict[str, Any return _error(f"by must be one of {', '.join(SPEND_BY)}", "Use --by agent, day or table.") if days < 1: return _error("--days must be at least 1.", "Give a positive number of days, for example --days 7.") + if days > MAX_SPEND_DAYS: + return _error(f"--days must be between 1 and {MAX_SPEND_DAYS}.", + f"Use a window of at most {MAX_SPEND_DAYS} days, for example --days 30.") days = int(days) first = now_utc().date() - timedelta(days=days - 1) groups: dict[str, dict[str, Any]] = {} From f29129c88ba9cd19658d8301259b73174b51ef85 Mon Sep 17 00:00:00 2001 From: rohangoud17 Date: Fri, 13 Nov 2026 17:19:59 -0500 Subject: [PATCH 2/7] test(spend): cover the --days upper bound of 365 (#107) --- tests/test_cli_and_mcp.py | 7 +++++++ tests/test_runaway.py | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/tests/test_cli_and_mcp.py b/tests/test_cli_and_mcp.py index 365f8e8..6887508 100644 --- a/tests/test_cli_and_mcp.py +++ b/tests/test_cli_and_mcp.py @@ -155,3 +155,10 @@ def test_mcp_check_by_name_and_status_checks(demo_db, state, tmp_path, monkeypat err_data = json.loads(err_res.content[0].text) assert err_data.get("status") == "error" assert "Unknown configured check" in err_data.get("error", "") + + +def test_cli_spend_refuses_days_above_365(demo_db, state, capsys): + for days in ("99999999999", "366"): + assert main(["--dsn", demo_db, "spend", "--days", days, "--json"]) == 1 + data = json.loads(capsys.readouterr().out) + assert data["status"] == "error" and "365" in data["error"] diff --git a/tests/test_runaway.py b/tests/test_runaway.py index ef1dbf1..b25555d 100644 --- a/tests/test_runaway.py +++ b/tests/test_runaway.py @@ -859,3 +859,12 @@ def test_prune_cli(settings, capsys): # Bad argument with pytest.raises(SystemExit): main(["prune", "--older-than", "invalid_period"]) + + +def test_spend_refuses_a_window_above_365_days(settings): + """A huge --days used to crash with OverflowError in the date math (#107).""" + for days in (99999999999, 366): + res = runaway.spend(settings, by="agent", days=days) + assert res["status"] == "error" and "365" in res["error"] and res["next_step"] + for days in (1, 365): + assert runaway.spend(settings, by="agent", days=days)["status"] == "ok" From b18b075aa0245ceaec2c8458be545d612243eae0 Mon Sep 17 00:00:00 2001 From: rohangoud17 Date: Fri, 13 Nov 2026 17:20:09 -0500 Subject: [PATCH 3/7] docs(changelog): note the spend --days upper bound (#107) --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 533b269..98d65d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## Unreleased + +### Fixed +- `spend --days N` refuses a value above 365 (the same limit as `top`) with `status: error` and a `next_step`, and exits 1 instead of crashing with an `OverflowError` traceback on a huge N (#107). + ## 0.4.0 - 2026-11-11 Data checks you define once in `gulliver.toml` and run in CI, a lint baseline for existing repos, rehearsal From 0f231efbf6d74ab121f71fc729a5c744fb726e59 Mon Sep 17 00:00:00 2001 From: rohangoud17 Date: Sat, 14 Nov 2026 11:47:31 -0500 Subject: [PATCH 4/7] docs:credit in changelog entry for #107 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab7d6af..94e318f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Fixed - `impact`, `lineage` and `drift` refuse `--models` pointing to a folder with no `.sql` files with `status: error` and a `next_step` instead of reporting zero models or empty impact. Thanks to @shaw778! (#122, #147) -- `spend --days N` refuses a value above 365 (the same limit as `top`) with `status: error` and a `next_step`, and exits 1 instead of crashing with an `OverflowError` traceback on a huge N (#107). +`spend --days N` refuses a value above 365 (the same limit as `top`) with `status: error` and a `next_step`, and exits 1 instead of crashing with an `OverflowError` traceback on a huge N. Thanks to @rohangoud17! (#107, #161) ## 0.4.0 - 2026-11-11 From 7e4187676154b639308f2116b0457f9e8a4f5e39 Mon Sep 17 00:00:00 2001 From: rohangoud17 Date: Sun, 15 Nov 2026 16:10:41 -0500 Subject: [PATCH 5/7] Edited Changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 94e318f..155c1aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Fixed - `impact`, `lineage` and `drift` refuse `--models` pointing to a folder with no `.sql` files with `status: error` and a `next_step` instead of reporting zero models or empty impact. Thanks to @shaw778! (#122, #147) -`spend --days N` refuses a value above 365 (the same limit as `top`) with `status: error` and a `next_step`, and exits 1 instead of crashing with an `OverflowError` traceback on a huge N. Thanks to @rohangoud17! (#107, #161) +- `spend --days N` refuses a value above 365 (the same limit as `top`) with `status: error` and a `next_step`, and exits 1 instead of crashing with an `OverflowError` traceback on a huge N. Thanks to @rohangoud17! (#107, #161) ## 0.4.0 - 2026-11-11 From ce222288a02f021fcdf75fceba5584feddec0230 Mon Sep 17 00:00:00 2001 From: rohangoud17 Date: Tue, 17 Nov 2026 10:51:54 -0500 Subject: [PATCH 6/7] fix(spend): cap --days where the date math overflows, not at 365 (#107) The window start is today - (N-1) days, and Python dates stop at 0001-01-01. Refuse only windows that would start before that, so long windows such as --days 400 keep working. --- src/gulliver/runaway.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/gulliver/runaway.py b/src/gulliver/runaway.py index 1ce6c5e..9df59a5 100644 --- a/src/gulliver/runaway.py +++ b/src/gulliver/runaway.py @@ -27,7 +27,7 @@ import hashlib import io import json -from datetime import datetime, timedelta, timezone +from datetime import date, datetime, timedelta, timezone from pathlib import Path from typing import Any @@ -44,7 +44,6 @@ REPEAT_FREE_USD = 0.01 # repeats cheaper than this ... REPEAT_FREE_BYTES = 1024**3 # ... and smaller than this are never treated as a loop SPEND_BY = ("agent", "day", "table", "session", "task", "connection", "kind") -MAX_SPEND_DAYS = 365 # same limit as `top`; the ledger itself has no retention limit _NONDETERMINISTIC = {"CurrentDate", "CurrentTimestamp", "CurrentTime", "CurrentDatetime", "CurrentTimestampLTZ", "Localtimestamp", "Localtime", "Rand", "Randn", "Uuid", "TableSample"} @@ -314,11 +313,14 @@ def spend(settings: Settings, by: str = "agent", days: int = 7) -> dict[str, Any return _error(f"by must be one of {', '.join(SPEND_BY)}", "Use --by agent, day or table.") if days < 1: return _error("--days must be at least 1.", "Give a positive number of days, for example --days 7.") - if days > MAX_SPEND_DAYS: - return _error(f"--days must be between 1 and {MAX_SPEND_DAYS}.", - f"Use a window of at most {MAX_SPEND_DAYS} days, for example --days 30.") + today = now_utc().date() + max_days = (today - date.min).days + 1 # a longer window would start before 0001-01-01 and overflow + if days > max_days: + return _error(f"--days must be between 1 and {max_days}: a longer window starts before the year 1.", + "Give a smaller number of days; any window that starts before your first ledger entry " + "already covers all of it.") days = int(days) - first = now_utc().date() - timedelta(days=days - 1) + first = today - timedelta(days=days - 1) groups: dict[str, dict[str, Any]] = {} total = {"usd": 0.0, "executions": 0, "bytes": 0} for e in read_ledger(Store(settings.state_dir)): From 82fd2f5254bf90726e5ce9616b032c6e18d5c3fd Mon Sep 17 00:00:00 2001 From: rohangoud17 Date: Tue, 17 Nov 2026 10:51:55 -0500 Subject: [PATCH 7/7] test(spend): cover long valid windows and the real --days limit (#107) --- tests/test_cli_and_mcp.py | 9 +++++++++ tests/test_runaway.py | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/tests/test_cli_and_mcp.py b/tests/test_cli_and_mcp.py index 2c17334..ed192e0 100644 --- a/tests/test_cli_and_mcp.py +++ b/tests/test_cli_and_mcp.py @@ -325,3 +325,12 @@ def test_cli_rehearse_options_validation(demo_db, state, capsys): # 4. Valid --fractions and --seed succeed exit_code = main(["--dsn", demo_db, "rehearse", "--fractions", "0.01,0.03", "--seed", "7", "select count(*) from events"]) assert exit_code == 0 + + +def test_cli_spend_refuses_a_huge_days_but_keeps_long_windows(demo_db, state, capsys): + for days in ("1000000000", "99999999999"): + assert main(["--dsn", demo_db, "spend", "--days", days, "--json"]) == 1 + data = json.loads(capsys.readouterr().out) + assert data["status"] == "error" and "--days must be between 1 and" in data["error"] and data["next_step"] + assert main(["--dsn", demo_db, "spend", "--days", "400", "--json"]) == 0 + assert json.loads(capsys.readouterr().out)["days"] == 400 diff --git a/tests/test_runaway.py b/tests/test_runaway.py index c0c1542..8c0858a 100644 --- a/tests/test_runaway.py +++ b/tests/test_runaway.py @@ -869,3 +869,15 @@ def test_prune_cli(settings, capsys): data = json.loads(capsys.readouterr().out) assert data["status"] == "error" assert "out of range" in data["error"] + + +def test_spend_refuses_a_window_that_starts_before_the_year_1(settings): + """A huge --days crashed with OverflowError in the date math (#107); long but valid windows still work.""" + longest = (runaway.now_utc().date() - datetime.min.date()).days + 1 # the window then starts at 0001-01-01 + for days in (longest + 1, 1_000_000_000, 99999999999): + res = runaway.spend(settings, by="agent", days=days) + assert res["status"] == "error" and str(longest) in res["error"] and res["next_step"] + for days in (1, 400, longest): + res = runaway.spend(settings, by="agent", days=days) + assert res["status"] == "ok" and res["days"] == days + assert runaway.spend(settings, by="agent", days=longest)["since"] == "0001-01-01"