-
Notifications
You must be signed in to change notification settings - Fork 1
fix(utility): replay numeric trust boundary on current review workflow #1006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
727e801
test(utility): require fail-closed numeric controls
seonghobae 7fcc69e
fix(utility): validate scalars before Rust discovery
seonghobae 2282a26
docs(changelog): record utility control hardening
seonghobae 833b900
test(utility): cover every scalar trust boundary
seonghobae 92e3034
fix(utility): reject real scalar subclasses before conversion
seonghobae a6657e2
fix(utility): normalize overflowing scalar controls
seonghobae bf85f57
docs(utility): fix result description punctuation
seonghobae File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # Selection utility numeric trust boundary | ||
|
|
||
| ## Fixed | ||
|
|
||
| - Hardened classical selection-utility and Taylor-Russell scalar controls so booleans, non-real objects, and non-finite values fail with package-owned validation before compiled Rust discovery. | ||
| - Prevented arbitrary caller-defined `__float__` callbacks from executing during public control marshalling while preserving genuine Python/NumPy real scalar compatibility and keeping all BCG, Naylor-Shine, and Taylor-Russell arithmetic Rust-owned. | ||
| - Normalized exact built-in integers outside the representable float range to the same package-owned validation error instead of leaking `OverflowError`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| """Trust-boundary regressions for classical selection utility controls.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import math | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from fast_mlsirm.utility import selection_utility, taylor_russell | ||
|
|
||
|
|
||
| class _HostileFloat: | ||
| """Arbitrary float-protocol object whose callback must stay unreachable.""" | ||
|
|
||
| calls = 0 | ||
|
|
||
| @classmethod | ||
| def reset(cls) -> None: | ||
| """Reset the callback counter.""" | ||
| cls.calls = 0 | ||
|
|
||
| def __float__(self) -> float: | ||
| type(self).calls += 1 | ||
| raise AssertionError("caller __float__ callback must not execute") | ||
|
|
||
|
|
||
| class _HostileFloatSubclass(float): | ||
| """Float subclass whose conversion callback must stay unreachable.""" | ||
|
|
||
| calls = 0 | ||
|
|
||
| @classmethod | ||
| def reset(cls) -> None: | ||
| """Reset the callback counter.""" | ||
| cls.calls = 0 | ||
|
|
||
| def __float__(self) -> float: | ||
| type(self).calls += 1 | ||
| raise AssertionError("caller float-subclass __float__ callback must not execute") | ||
|
|
||
|
|
||
| _UTILITY_CONTROL_CALLS = ( | ||
| lambda value: selection_utility(value, 1.0, 0.5, 0.5), | ||
| lambda value: selection_utility(1.0, value, 0.5, 0.5), | ||
| lambda value: selection_utility(1.0, 1.0, value, 0.5), | ||
| lambda value: selection_utility(1.0, 1.0, 0.5, value), | ||
| lambda value: selection_utility(1.0, 1.0, 0.5, 0.5, value, 1.0), | ||
| lambda value: selection_utility(1.0, 1.0, 0.5, 0.5, 0.0, value), | ||
| lambda value: taylor_russell(value, 0.5, 0.5), | ||
| lambda value: taylor_russell(0.4, value, 0.5), | ||
| lambda value: taylor_russell(0.4, 0.5, value), | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("call", _UTILITY_CONTROL_CALLS) | ||
| @pytest.mark.parametrize("hostile_type", (_HostileFloat, _HostileFloatSubclass)) | ||
| def test_utility_rejects_float_protocol_objects_without_callbacks( | ||
| call, hostile_type | ||
| ) -> None: | ||
| """Every scalar position rejects hostile conversion without callback execution.""" | ||
| hostile_type.reset() | ||
|
|
||
| with pytest.raises(ValueError, match="must be a finite real number"): | ||
| call(hostile_type()) | ||
|
|
||
| assert hostile_type.calls == 0 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("call", _UTILITY_CONTROL_CALLS) | ||
| @pytest.mark.parametrize("value", (True, False, math.inf, -math.inf, math.nan)) | ||
| def test_utility_rejects_boolean_and_nonfinite_controls(call, value) -> None: | ||
| """Every scalar position rejects boolean and non-finite control values.""" | ||
| with pytest.raises(ValueError, match="must be a finite real number"): | ||
| call(value) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("call", _UTILITY_CONTROL_CALLS) | ||
| def test_utility_rejects_unrepresentable_builtin_integers(call) -> None: | ||
| """Exact huge integers must become stable package-owned validation errors.""" | ||
| with pytest.raises(ValueError, match="must be a finite real number"): | ||
| call(10**400) | ||
|
|
||
|
|
||
| def test_numpy_real_scalars_preserve_native_results() -> None: | ||
| """Trusted NumPy real scalars produce the same Rust-owned results as floats.""" | ||
| py_utility = selection_utility(10.0, 2.0, 0.4, 0.5, 1.0, 2.0) | ||
| np_utility = selection_utility( | ||
| np.float64(10.0), | ||
| np.float64(2.0), | ||
| np.float64(0.4), | ||
| np.float64(0.5), | ||
| np.float64(1.0), | ||
| np.float64(2.0), | ||
| ) | ||
| assert np_utility == py_utility | ||
|
|
||
| py_tr = taylor_russell(0.4, 0.5, 0.6) | ||
| np_tr = taylor_russell(np.float64(0.4), np.float64(0.5), np.float64(0.6)) | ||
| assert np_tr == py_tr |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.