Skip to content

Commit 79daf0c

Browse files
lesnik512claude
andcommitted
fix(budget): ceiling uses math.ceil, not int truncation
`RetryBudget.try_withdraw`'s ceiling computed `int(deposits * percent) + floor`, truncating so 4 deposits at percent_can_retry=0.2 yielded `int(0.8) = 0` — the configured percentage was never reached at deposit counts below `1/percent_can_retry`. Replace with math.ceil so the threshold is hit at the first deposit-count where it is mathematically expressible. The Hypothesis property test mirrored the same int() formula, so it could not detect the off-by-one. Rewrite it to use math.ceil for the expected ceiling and tighten `permitted <= ceiling` to `permitted == ceiling` so it discriminates between the two roundings going forward. Also update test_budget.py::test_deposit_after_exhaustion_does_not_immediately_unblock, which was written to document the old truncation behaviour; the test is renamed and rewritten to document the correct ceil semantics. Closes audit Low findings (budget.py:67 truncation, test_budget_props.py:52 formula-mirror) from planning/audit/2026-06-07-deep-audit.md. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 14c21ab commit 79daf0c

3 files changed

Lines changed: 16 additions & 10 deletions

File tree

‎src/httpware/middleware/resilience/budget.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
in the same process.
99
"""
1010

11+
import math
1112
import threading
1213
import time
1314
from collections import deque
@@ -64,7 +65,7 @@ def try_withdraw(self) -> bool:
6465
with self._lock:
6566
self._purge(now)
6667
floor = int(self._min_retries_per_sec * self._ttl)
67-
ceiling = int(len(self._deposits) * self._percent_can_retry) + floor
68+
ceiling = math.ceil(len(self._deposits) * self._percent_can_retry) + floor
6869
if len(self._withdrawn) >= ceiling:
6970
return False
7071
self._withdrawn.append(now)

‎tests/test_budget.py‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,24 @@ def test_try_withdraw_returns_false_when_exhausted() -> None:
6969
assert budget.try_withdraw() is False
7070

7171

72-
def test_deposit_after_exhaustion_does_not_immediately_unblock() -> None:
73-
"""A single deposit at 20% percent_can_retry contributes 0.2 → int() truncates to 0 → no new retries."""
72+
def test_deposit_after_exhaustion_unblocks_at_ceil_boundary() -> None:
73+
"""With math.ceil, 1 deposit at 20% yields ceil(0.2)=1, raising the ceiling by 1."""
7474
clock = _Clock()
7575
budget = RetryBudget(ttl=10.0, min_retries_per_sec=1.0, percent_can_retry=0.2, _now=clock.now)
7676
# exhaust the floor (10)
7777
for _ in range(10):
7878
budget.try_withdraw()
7979
assert budget.try_withdraw() is False
80-
# one deposit: 1 * 0.2 = 0.2 → int() → 0
80+
# one deposit: ceil(1 * 0.2) = ceil(0.2) = 1 → ceiling becomes 11, one new retry permitted
8181
budget.deposit()
82+
assert budget.try_withdraw() is True
8283
assert budget.try_withdraw() is False
83-
# 5 more deposits: 6 * 0.2 = 1.2 → int() → 1 new retry permitted
84-
for _ in range(5):
84+
# 4 more deposits (5 total): ceil(5 * 0.2) = ceil(1.0) = 1 → same ceiling, no additional retries
85+
for _ in range(4):
8586
budget.deposit()
87+
assert budget.try_withdraw() is False
88+
# 1 more deposit (6 total): ceil(6 * 0.2) = ceil(1.2) = 2 → ceiling becomes 12, one more retry
89+
budget.deposit()
8690
assert budget.try_withdraw() is True
8791
assert budget.try_withdraw() is False
8892

‎tests/test_budget_props.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
reduce the budget).
99
"""
1010

11+
import math
1112
from collections.abc import Callable
1213

1314
from hypothesis import given, settings
@@ -60,13 +61,13 @@ def test_try_withdraw_never_exceeds_theoretical_bound(
6061
for _ in range(deposits):
6162
budget.deposit()
6263
floor = int(min_rps * ttl)
63-
ceiling = int(deposits * percent) + floor
64+
expected_ceiling = math.ceil(deposits * percent) + floor
6465
permitted = 0
65-
# Try up to ceiling + 10 times to confirm the cap holds.
66-
for _ in range(ceiling + 10):
66+
# Try up to expected_ceiling + 10 times to confirm the cap holds exactly.
67+
for _ in range(expected_ceiling + 10):
6768
if budget.try_withdraw():
6869
permitted += 1
69-
assert permitted <= ceiling
70+
assert permitted == expected_ceiling
7071

7172

7273
@given(

0 commit comments

Comments
 (0)