Repository navigation
fix(calculator): bound powers and report non-real or oversized results - #793
Conversation
safe_eval applied operator.pow with no size check, so 9**9**9 never returned, and non-real or overflowing results failed later in int(result) with conversion errors. Boolean constants were accepted because bool is an int subclass. Bound integer exponentiation against MAX_RESULT_DIGITS before computing it, including nested **, map overflow/inf/NaN to a short size error, map complex values to a non-real error, and reject Boolean constants. Documented examples, 2**64, negative exponents, and the division-by-zero message stay the same. Signed-off-by: Matt Van Horn <mvanhorn@users.noreply.github.com>
22a62a0 to
edc14c7
Compare
Resolve the CHANGELOG.md Unreleased Fixed conflict by keeping upstream's Tier 3 security grading entries and the calculator entry.
chrisknvidia
left a comment
There was a problem hiding this comment.
Thanks for the follow-up. The limitation bullet now matches what the script does: the bound applies to arithmetic expressions, and conversions and percentages are unchanged. I re-ran the calculator tests and the oversized, non-real, Boolean and division-by-zero cases at the current head and they behave as described; the documented examples and 264 / 2-2 still print the same results. One small nit inline about the error message when the exponent itself is outside float range. Not blocking.
For an integer power whose exponent is outside float range, such as 2**(10**309), the digit estimate multiplied the exponent by a float and raised OverflowError, so the script printed "int too large to convert to float" instead of "result is too large". Any base of magnitude 2 or more raised to an exponent of 4000 or more has over 1000 digits, so that case now returns over budget using integers only. Signed-off-by: Matt Van Horn <mvanhorn@users.noreply.github.com>
rng1995
left a comment
There was a problem hiding this comment.
Thank you for the contribution! Reviewed the pre-computation power bound, real/finite result checks, and the follow-up handling of very large exponents. All 27 calculator tests passed; additional boundary checks preserved valid 1000-digit results and rejected oversized/non-real results. No actionable issues found.
rng1995
left a comment
There was a problem hiding this comment.
Thanks @mvanhorn for this contribution! I reviewed cd53b2dd57. I reproduced the problem on main, confirmed this change fixes it, checked that the new tests fail on main, and ran the new and related tests on this head (all pass). I left 3 inline comments (3 P3); please take a look.
The entry described only inputs that used to hang or raise a conversion error. The new cap also refuses integer results of 1001 to 4300 digits, such as 2**3322, which 1.0.1 printed in full. Say so in the entry. Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
An int base with a negative int exponent now computes 1 / base**-exp, and returns 0 when |result| is below 10**-1000, so 2**-(10**309) and (10**999)**-1 print Result: 0 and 1**-(10**309) prints Result: 1 instead of "result is too large". A float operation whose int operand cannot convert to float, such as 0.5**(10**400) or (10**400)*0.0, now reports "number is outside the floating-point range". Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
Results that go through a float, from a division or a decimal operand, are bounded by the float range (about 1.8e308), not 1000 digits: (2**2000)/3 and 10.0**400 return an error while 2**3000 prints. SKILL.md and the changelog entry now state both limits. Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
Resolve the CHANGELOG.md conflict with the SkillSpector parser_diagnostic entry from main by keeping both entries. Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
Head branch was pushed to by a user without write access
|
Merged current main in 221137c to clear the CHANGELOG.md conflict. Both changelog entries are kept, and uv run ruff check . plus tests/test_reference_calculator.py pass on this head. |
The 1000-digit and floating-point range checks run on every intermediate value, so an expression such as 10**1000 - 10**1000 is rejected even though its final value is 0. Say so in SKILL.md and in the changelog entry. Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
There was a problem hiding this comment.
🟡 Changes recommended
Oversized hexadecimal literals can trigger Python’s integer-to-string conversion limit before the size guard runs, producing the wrong error instead of the intended result-too-large message.
1 open issue
What changed in this PR
This PR hardens the calculator reference skill by bounding integer powers and reporting oversized or non-real arithmetic results more clearly.
Changes:
- Adds integer-size checks, floating-point error handling, and rejection of Boolean constants.
- Adds timeout-backed regression tests for large powers, underflow, invalid results, and existing behavior.
- Documents arithmetic limits and their effect on intermediate values.
| File | Description |
|---|---|
tests/test_reference_calculator.py |
Adds regression cases and subprocess timeouts. |
src/skillevaluator/tier3/reference_skills/calculator/SKILL.md |
Documents result limits and intermediate-value checks. |
src/skillevaluator/tier3/reference_skills/calculator/scripts/calc.py |
Adds bounded exponentiation and arithmetic-result validation. |
CHANGELOG.md |
Records calculator safety changes and compatibility impacts. |
Review effort: Balanced
… the result-size error' Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
f1e9c0b to
6160685
Compare
rng1995
left a comment
There was a problem hiding this comment.
Verified the integer magnitude check and regression cases for the exact digit limit and oversized hexadecimal literals. All 97 focused tests passed on this head.
|
Thanks for merging this, @rng1995. The calculator skill now checks powers before computing them, so oversized or non-real results return an error instead of hanging. |

Summary
The calculator reference skill evaluated
**withoperator.powand no sizecheck. On upstream
main(affe1acc),timeout 10 python scripts/calc.py "9**9**9"exits 124 after computing a ~370-million-digit integer.
(-8)**(1/3)reachesint()as a complex value,10.0**400and1e308*10fail with overflow orinfinity conversion errors, and
True+TrueprintsResult: 2becauseboolpasses the
(int, float)constant check.safe_evalnow rejects Boolean and other non-int/float constants, estimatesinteger powers with
abs(exp) * log10(abs(base))againstMAX_RESULT_DIGITS(1000) at every
**(including nested right-associative powers), and reportsError: result is too largeorError: result is not a real numberon stderrwith exit code 1. Documented examples,
2**64,2**-2, and the division-by-zeromessage are unchanged. Conversion and percentage regexes are not modified.
Verification
make lintmake testmake buildRelease Impact
CHANGELOG.mdCommands run
Platform: Linux 6.12.113+ x86_64, Python 3.13.18 (uv), on top of upstream
affe1acc.upstream/mainworktree):uv run make lint— All checks passed.PYTHONUNBUFFERED=1 PYTEST_ADDOPTS='--deselect tests/test_harbor_secure_docker_environment.py::test_windows_transfer_repeated_cancellation_reaps_process_and_removes_partial_output' uv run make test— 57 failed, 39461 passed, 171 skipped, 20 deselected, 65 warnings in 1908.70s. The deselected Windows-transfer test hangs the serial run on untouchedupstream/main, so it is excluded on both sides. The 57 failures here: 27 Docker-client tests intests/test_harbor_secure_docker_environment.py, 15 Rich 80-column wrapping assertions intests/reporting/, 5 NeMo Gym backend tests, 3 intests/ci/test_run_tier1.py, and 7 tier3/harness tests.uv run make build— Successfully builtskillevaluator-1.0.1.tar.gzandskillevaluator-1.0.1-py3-none-any.whl.uv run make lint— All checks passed.uv run ruff format --checkonscripts/calc.pyandtests/test_reference_calculator.py— 2 files already formatted.uv run pytest -n 0 tests/test_reference_calculator.py— 24 passed in 0.53s.uv run pytest -n 0 tests/test_reference_calculator.py tests/test_cli.py tests/golden— 392 passed in 12.05s.uv run pytest -n 0 tests/reporting tests/test_reference_calculator.py— 15 failed, 2835 passed, 86 skipped in 147.44s. The 15 failures reproduce on untouchedupstream/main(80-column wrapping).PYTHONUNBUFFERED=1 PYTEST_ADDOPTS='--deselect tests/test_harbor_secure_docker_environment.py::test_windows_transfer_repeated_cancellation_reaps_process_and_removes_partial_output' uv run make test— 57 failed, 39475 passed, 171 skipped, 20 deselected, 65 warnings in 1896.72s. The 57 failures are the same node IDs as on untouchedupstream/main; the branch adds 14 passing calculator tests.Ran make testis left unticked because the suite was only run with the hanging test deselected.uv run make build— Successfully builtskillevaluator-1.0.1.tar.gzandskillevaluator-1.0.1-py3-none-any.whl.Not verified:
./fern/node_modules/.bin/fern check(nodocs/*.mdxchange); Python 3.12/3.14 as the gate Python; Windows; a live Docker client for the Harbor secure-environment tests.Demo
Full recording (MP4): https://raw.githubusercontent.com/mvanhorn/pr-demo-assets/main/osc-bulk-1118/calculator-bounded-power/out.mp4