Skip to content

fix: clamp counters at MAX_SAFE_COUNTER to prevent float64 precision loss#3983

Open
wangarthur999999-star wants to merge 3 commits into
orchestration-agent:mainfrom
wangarthur999999-star:fix/exporter-counter-range-3846
Open

fix: clamp counters at MAX_SAFE_COUNTER to prevent float64 precision loss#3983
wangarthur999999-star wants to merge 3 commits into
orchestration-agent:mainfrom
wangarthur999999-star:fix/exporter-counter-range-3846

Conversation

@wangarthur999999-star
Copy link
Copy Markdown

Summary

  • Added MAX_SAFE_COUNTER = 2**53 constant — largest integer representable in IEEE 754 double precision
  • Added negative value validation in increment() with clear error messages
  • Added min(current + value, MAX_SAFE_COUNTER) clamping on every increment to prevent counter overflow beyond float64 safe integer
  • Removed stale timestamp artifacts from the file

Implementation

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 message
  • TestCounterRangeClamping (5): clamped at max, exactly at max, below max then increment, multiple increments, no wrapping
  • TestCounterSnapshot (3): snapshot includes counters, plain dict types, float64 safety constant
  • TestCounterEdgeCases (4): zero noop, large increment, independent counters, approach limit

All 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

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.

1 participant