Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(kompress): bound download retry backoff before overflow (#3630)

prakxa7858fd2ae02
diff --git a/headroom/transforms/kompress_compressor.py b/headroom/transforms/kompress_compressor.py
index ac09291b8..6a076ee97 100644
--- a/headroom/transforms/kompress_compressor.py
+++ b/headroom/transforms/kompress_compressor.py
@@ -1097,10 +1097,13 @@ def _download_retry_blocked(model_id: str) -> bool:
if entry is None:
return False
failures, last_attempt = entry
- window = min(
- _DOWNLOAD_RETRY_MAX_SECONDS,
- _DOWNLOAD_RETRY_BASE_SECONDS * (2 ** (failures - 1)),
- )
+ # Stop doubling at the cap: computing 2 ** (failures - 1) first can
+ # overflow when converted to float after a long run of failed downloads.
+ window = min(_DOWNLOAD_RETRY_BASE_SECONDS, _DOWNLOAD_RETRY_MAX_SECONDS)
+ for _ in range(failures - 1):
+ if window >= _DOWNLOAD_RETRY_MAX_SECONDS:
+ break
+ window = min(window * 2, _DOWNLOAD_RETRY_MAX_SECONDS)
return bool((time.monotonic() - last_attempt) < window)
diff --git a/tests/test_kompress_download_backoff.py b/tests/test_kompress_download_backoff.py
index 538413c14..ff83a2089 100644
--- a/tests/test_kompress_download_backoff.py
+++ b/tests/test_kompress_download_backoff.py
@@ -74,8 +74,48 @@ def test_backoff_window_elapsing_allows_another_attempt(monkeypatch):
assert len(started) == 2, "backoff never expires"
-def test_success_clears_the_backoff(monkeypatch):
+ ("failures", "window"),
+ [
+ (1, 5.0),
+ (2, 10.0),
+ (3, 20.0),
+ (4, 40.0),
+ (5, 80.0),
+ (6, 160.0),
+ (7, 300.0),
+ (8, 300.0),
+ (1024, 300.0),
+ (1025, 300.0),
+ (10**100, 300.0),
+ ],
+)
+def test_retry_resumes_when_backoff_window_expires(monkeypatch, failures, window):
+ started = _spawned(monkeypatch, fails=True)
+ now = 1000.0
+ monkeypatch.setattr(kc.time, "monotonic", lambda: now)
+ with kc._download_threads_lock:
+ kc._download_failures["some/model"] = (failures, now)
+
+ now += window - 0.5
+ kc.ensure_background_download("some/model")
+ _drain()
+ assert not started, "retried before the capped backoff elapsed"
+
+ now += 0.5
+ kc.ensure_background_download("some/model")
+ _drain()
+ assert started == ["some/model"], "capped backoff never expires"
+
+
[email protected]("failures", [1, 1025])
+def test_success_clears_the_backoff(monkeypatch, failures):
_spawned(monkeypatch, fails=False)
+ with kc._download_threads_lock:
+ kc._download_failures["some/model"] = (
+ failures,
+ kc.time.monotonic() - kc._DOWNLOAD_RETRY_MAX_SECONDS,
+ )
kc.ensure_background_download("some/model")
_drain()
assert "some/model" not in kc._download_failures