Skip to content

fix(calculator): bound powers and report non-real or oversized results - #793

Merged
rng1995 merged 13 commits into
NVIDIA:mainfrom
mvanhorn:feat/calculator-bounded-power
Nov 20, 2026
Merged

rng1995 merged 13 commits into
NVIDIA:mainfrom
mvanhorn:feat/calculator-bounded-power

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

Summary

The calculator reference skill evaluated ** with operator.pow and no size
check. 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) reaches
int() as a complex value, 10.0**400 and 1e308*10 fail with overflow or
infinity conversion errors, and True+True prints Result: 2 because bool
passes the (int, float) constant check.

safe_eval now rejects Boolean and other non-int/float constants, estimates
integer powers with abs(exp) * log10(abs(base)) against MAX_RESULT_DIGITS
(1000) at every ** (including nested right-associative powers), and reports
Error: result is too large or Error: result is not a real number on stderr
with exit code 1. Documented examples, 2**64, 2**-2, and the division-by-zero
message are unchanged. Conversion and percentage regexes are not modified.

Verification

  • I am familiar with the Contributing Guidelines
  • Added or updated focused tests
  • Updated documentation for user-visible changes
  • Ran make lint
  • Ran make test
  • Ran make build
  • Did not add credentials, private datasets, or proprietary benchmark content

Release Impact

  • No user-visible release note needed
  • Updated CHANGELOG.md

Commands run

Platform: Linux 6.12.113+ x86_64, Python 3.13.18 (uv), on top of upstream affe1acc.

  • Baseline (untouched upstream/main worktree):
    • 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 untouched upstream/main, so it is excluded on both sides. The 57 failures here: 27 Docker-client tests in tests/test_harbor_secure_docker_environment.py, 15 Rich 80-column wrapping assertions in tests/reporting/, 5 NeMo Gym backend tests, 3 in tests/ci/test_run_tier1.py, and 7 tier3/harness tests.
    • uv run make build — Successfully built skillevaluator-1.0.1.tar.gz and skillevaluator-1.0.1-py3-none-any.whl.
  • This branch:
    • uv run make lint — All checks passed.
    • uv run ruff format --check on scripts/calc.py and tests/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 untouched upstream/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 untouched upstream/main; the branch adds 14 passing calculator tests. Ran make test is left unticked because the suite was only run with the hanging test deselected.
    • uv run make build — Successfully built skillevaluator-1.0.1.tar.gz and skillevaluator-1.0.1-py3-none-any.whl.

Not verified: ./fern/node_modules/.bin/fern check (no docs/*.mdx change); Python 3.12/3.14 as the gate Python; Windows; a live Docker client for the Harbor secure-environment tests.

Demo

demo

Full recording (MP4): https://raw.githubusercontent.com/mvanhorn/pr-demo-assets/main/osc-bulk-1118/calculator-bounded-power/out.mp4

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>
@cursor
cursor Bot force-pushed the feat/calculator-bounded-power branch from 22a62a0 to edc14c7 Compare November 18, 2026 19:03
Resolve the CHANGELOG.md Unreleased Fixed conflict by keeping upstream's
Tier 3 security grading entries and the calculator entry.

@chrisknvidia chrisknvidia left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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 rng1995 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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 rng1995 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Comment thread CHANGELOG.md Resolved
rng1995 and others added 5 commits November 19, 2026 00:31
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>
@rng1995
rng1995 enabled auto-merge (squash) November 19, 2026 09:57
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>
auto-merge was automatically disabled November 19, 2026 11:34

Head branch was pushed to by a user without write access

@mvanhorn

Copy link
Copy Markdown
Contributor Author

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.

mvanhorn and others added 2 commits November 19, 2026 03:42
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>
Copilot AI balanced review requested due to automatic review settings November 20, 2026 00:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 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

Copilot AI balanced review requested due to automatic review settings November 20, 2026 00:37
@rng1995
rng1995 enabled auto-merge (squash) November 20, 2026 00:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block the calculator hardening changes.

No open issues 🎉

1 resolved since last review

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>
@mvanhorn

Copy link
Copy Markdown
Contributor Author

@rng1995 The DCO check on this one is failing on f1e9c0b ("Potential fix for pull request finding"), which is your commit, so it needs your sign-off. Everything else on the branch is signed.

Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
@rng1995
rng1995 force-pushed the feat/calculator-bounded-power branch from f1e9c0b to 6160685 Compare November 20, 2026 15:40
Copilot AI balanced review requested due to automatic review settings November 20, 2026 15:40

@rng1995 rng1995 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

There are no active review issues blocking approval of the calculator numeric-safeguard changes.

No open issues 🎉

Review effort: Balanced

@rng1995
rng1995 merged commit 1dd97c8 into NVIDIA:main Nov 20, 2026
23 checks passed
@mvanhorn

Copy link
Copy Markdown
Contributor Author

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.

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.

4 participants