fix: clamp counters at MAX_SAFE_COUNTER to prevent float64 precision loss#3983
Open
wangarthur999999-star wants to merge 3 commits into
Open
Conversation
…loss Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
MAX_SAFE_COUNTER = 2**53constant — largest integer representable in IEEE 754 double precisionincrement()with clear error messagesmin(current + value, MAX_SAFE_COUNTER)clamping on every increment to prevent counter overflow beyond float64 safe integerImplementation
Proactive increment-time clamping — the counter is checked and clamped on every
increment()call rather than at snapshot time. This is safer than snapshot-time validation because it prevents the counter from entering an overflow state at all.Test Coverage
16 tests in
tests/test_counter_range_limits.py:TestIncrementValidation(4): positive value, default, negative rejection, negative messageTestCounterRangeClamping(5): clamped at max, exactly at max, below max then increment, multiple increments, no wrappingTestCounterSnapshot(3): snapshot includes counters, plain dict types, float64 safety constantTestCounterEdgeCases(4): zero noop, large increment, independent counters, approach limitAll 16 tests pass.
Differs from competing PR #3948
PR #3948 validates at snapshot time. This implementation clamps at increment time — preventing overflow state entirely rather than filtering it on read.
🤖 Generated with Claude Code