Skip to content

Fix spend days 107 - #161

Open
rohangoud17 wants to merge 9 commits into
laaielichter:mainfrom
rohangoud17:fix-spend-days-107
Open

rohangoud17 wants to merge 9 commits into
laaielichter:mainfrom
rohangoud17:fix-spend-days-107

Conversation

@rohangoud17

Copy link
Copy Markdown
Contributor

Fixes #107

What and why

spend --days N refused N < 1 but crashed with a raw OverflowError for very large N (timedelta/date arithmetic). It now returns status: "error" with a next_step and exit code 1, like top.

  • Added MAX_SPEND_DAYS = 365 in runaway.py, checked right after days < 1 and before any date math.
  • Why 365: it matches top's window (checked inline in workload.py). The ledger has no retention limit, so this is a consistency choice, not a technical one.
  • --days 1 through 365 behave exactly as before.

How it was tested

  • pytest: changed test files pass (68 passed, 1 skipped). One unrelated test, test_verify.py::test_fix_verify_with_connection, failed once in the full run but passes alone on this branch and on clean main, so it looks flaky.
  • ruff check . passes
  • git diff --check upstream/main...HEAD clean

New tests: spend() with 99999999999 and 366 returns an error naming 365 with a next_step; 1 and 365 return ok; main([...]) exits 1 for both bad values. I confirmed the new tests fail with the original OverflowError before the fix.

AI-assisted; I reviewed and understand every change.

@laaielichter laaielichter left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @rohangoud17, this is correct and approved from my side. #107 is yours (you claimed it first on 2026-11-10), and the other PR for it, #140, is not being taken forward, so this one goes ahead.

What I checked on f0f8697 (clean against current main 52666d3):

  • ruff check . is clean and the full suite passes (888 passed, 10 skipped; no flaky failure in my run).
  • By hand with the demo database: spend --days 99999999999 and --days 366 give status: error, "--days must be between 1 and 365." with a next_step, exit code 1. --days 1 and --days 365 work, --days 0 keeps its old message.
  • Mutation: with src/ from main, the same call crashes with OverflowError: Python int too large to convert to C int, and both new tests (test_spend_refuses_a_window_above_365_days, test_cli_spend_refuses_days_above_365) fail. With your change they pass.
  • Your PR description says why 365 was chosen, as the issue asked.

One small request, the CHANGELOG credit: please end the entry with Thanks to @rohangoud17! (#107, #161) like the entries around it. If you would rather not touch the branch again, I will add the credit in a follow-up after merge.

CI: the runs for this commit are action_required (a first-time contributor's run waits for maintainer approval), so no check has run on GitHub yet:


Generated by Claude Code

@laaielichter laaielichter left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @rohangoud17 for adding the credit so quickly. One small thing is left in CHANGELOG.md on 0f231ef: the entry lost its leading - , so it is no longer a list item and shows up as a loose paragraph below the #122/#147 bullet. Please change the line to start with - again:

- `spend --days N` refuses a value above 365 ... on a huge N. Thanks to @rohangoud17! (#107, #161)

Nothing else changed since my review on f0f8697 (the diff is only that CHANGELOG line, and the branch merges cleanly with main 52666d3), so the code approval stands. After the fix I will look once more, and the PR is ready as soon as the maintainer has approved the first-time CI runs and the 14 checks are green.


Generated by Claude Code

Copy link
Copy Markdown
Owner

Good news for part of the conflict: the CHANGELOG part got easier. We changed the rule so contributors don't edit CHANGELOG.md any more; we add your entry with credit when we merge. When you merge main, just take main's version of CHANGELOG.md (or drop your change to it). The conflicts in tests/test_cli_and_mcp.py and tests/test_runaway.py still need a normal merge of main.


Generated by Claude Code

Copy link
Copy Markdown
Owner

Thanks @rohangoud17. The spend --days fix is verified on this head: on main a huge --days still raises OverflowError (the new test fails there), and on this head it returns status: "error" with the 1 to 365 limit. The full suite passes locally (888 passed, 10 skipped) and ruff check . is clean.

Before it can merge, two things are still open from the earlier comment:

  1. CHANGELOG.md still has your line. Please drop that change; the maintainer adds the entry with credit on merge.
  2. This branch still conflicts with main in tests/test_cli_and_mcp.py and tests/test_runaway.py. Please merge main in and keep both sides of those tests.

After the push, the CI runs wait for a maintainer approval, and we will review the new head.


Generated by Claude Code

@laaielichter laaielichter left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rohangoud17. I reproduced #107 on main: spend --days 1000000000 crashes with an OverflowError traceback, and on this branch it returns a clean error. The mutation check fails 2 tests when the cap is removed.

Two things before it can merge:

  1. The CHANGELOG.md line conflicts with main. Please remove it; the maintainer writes that entry.
  2. The 365 cap is a behaviour change: spend --days 400 works on main today and would now be refused. Could you check where the crash actually starts, and cap there (or explain why 365 is the right limit, in the docs)? That would keep valid windows working.

Generated by Claude Code

# Conflicts:
#	CHANGELOG.md
#	tests/test_cli_and_mcp.py
#	tests/test_runaway.py
…ielichter#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.
@rohangoud17

Copy link
Copy Markdown
Contributor Author

Thanks for the review and the mutation check!

Removed the CHANGELOG line (merged the latest main; the PR no longer touches CHANGELOG.md).
Dropped the 365 cap. The crash starts in today - timedelta(days=N - 1): Python dates stop at 0001-01-01, so the longest window that fits is (today - date.min).days + 1 (739,937 days today). spend now refuses only values above that, with status: error and a next_step. Everything below it works as on main, including --days 400.
Tests cover 1, 400 and the longest valid window as ok, and one day more, 1000000000 and 99999999999 as errors, through both spend() and the CLI. With the check removed, both new tests fail.

Copy link
Copy Markdown
Owner

Thanks @rohangoud17, that is the cap we asked for: it sits where the crash starts, and the CHANGELOG.md line is gone from this PR. The maintainer will review this head next. CI approval for the runs is the maintainer's decision.


Generated by Claude Code

Copy link
Copy Markdown
Owner

I checked 82fd2f5 merged with current main in a clean worktree: the full test suite passes (1140 passed, 10 skipped). The head no longer touches CHANGELOG.md. The CI runs for this head have not started yet, because the first-time-contributor workflow approval is still pending, so the maintainer still needs to approve the runs and do the re-review before this can merge.


Generated by Claude Code

Copy link
Copy Markdown
Owner

Thanks @rohangoud17, the diagnosis is right: the crash really does start where today - timedelta(days=N - 1) runs past 0001-01-01, and keeping --days 400 working was a good call. Your tests also cover both spend() and the CLI, which is nice.

One thing I would like to change before merging. The limit is computed from today's date ((today - date.min).days + 1), so the number printed in the error changes every day (739,937 today, one more tomorrow). That makes the message a moving target for users and scripts, and the tests need to recompute the boundary to follow it. I wish I had caught that earlier when I called this cap the one we asked for.

Could you use a fixed constant instead? For example, MAX_SPEND_DAYS = 36500 (about 100 years) at module level in runaway.py, checked right after days < 1 and before any date math. It is far above any real ledger, it is still well inside the range where the date math cannot overflow, and the error would then always say --days must be between 1 and 36500.

For the tests, please:

  • pin the constant (assert runaway.MAX_SPEND_DAYS == 36500), so changing it later is a deliberate choice;
  • keep 1, 400 and MAX_SPEND_DAYS itself as ok, and MAX_SPEND_DAYS + 1, 1_000_000_000 (1e9) and 99999999999 as the clean status: "error" with a next_step, through both spend() and the CLI (exit 1).

The since == "0001-01-01" assertion can then go, since it only existed to follow the moving boundary. Please also refresh the PR description, which still mentions MAX_SPEND_DAYS = 365.

Thank you for the patience with the several rounds on this one. I am approving the pending CI runs on the current head now so you get a signal, and they will need approving again for the new push.


Generated by Claude Code

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

spend: a very large --days crashes with a Python traceback instead of an error message

2 participants