Skip to content

The agent that grows with you

fix(gateway): the redelivery timer never spends a row's last attempt

kshitijk4poor807435ac1ec1
diff --git a/gateway/delivery_ledger.py b/gateway/delivery_ledger.py
index e406ef74705..3aeec9d6a32 100644
--- a/gateway/delivery_ledger.py
+++ b/gateway/delivery_ledger.py
@@ -142,9 +142,12 @@ def flood_not_before(updated_at: Any, last_error: Any) -> float:
def retry_not_before(updated_at: Any, last_error: Any, attempts: Any) -> Optional[float]:
- """Earliest moment a failed row may be resent, or ``None`` for a row that must never be: a flood
- refusal keeps the platform's own wait, an allowlisted reconnect error is due at once, a whole-chat
- death is final, and any other rejection backs off by the attempts already spent."""
+ """Earliest moment a failed row may be resent, or ``None`` for a row the runtime must leave alone:
+ a flood refusal keeps the platform's own wait, an allowlisted reconnect error is due at once, a
+ whole-chat death is final, and any other rejection backs off by the attempts already spent — but
+ never spends the LAST budgeted attempt. An unclassified outage can outlast any timer, and a row
+ the timer abandoned would be lost for good; leaving one attempt keeps it recoverable by the boot
+ sweep after a restart, which is a real recovery signal."""
if is_flood_error(last_error):
return flood_not_before(updated_at, last_error)
text = str(last_error or "").strip().lower()
@@ -152,8 +155,10 @@ def retry_not_before(updated_at: Any, last_error: Any, attempts: Any) -> Optiona
return _failed_stamp(updated_at)
if classify_dead_error(text):
return None
- backoff = _RETRY_BACKOFF_SECONDS[min(int(attempts or 0), len(_RETRY_BACKOFF_SECONDS) - 1)]
- return _failed_stamp(updated_at) + backoff
+ spent = int(attempts or 0)
+ if spent >= MAX_ATTEMPTS - 1:
+ return None
+ return _failed_stamp(updated_at) + _RETRY_BACKOFF_SECONDS[min(spent, len(_RETRY_BACKOFF_SECONDS) - 1)]
def _db_path():
diff --git a/tests/gateway/test_delivery_ledger.py b/tests/gateway/test_delivery_ledger.py
index 1925b65a291..cabb47f9704 100644
--- a/tests/gateway/test_delivery_ledger.py
+++ b/tests/gateway/test_delivery_ledger.py
@@ -197,15 +197,19 @@ class TestRuntimeFailedSweep:
assert claimed[0]["needs_marker"] is True
assert _row("ob-1")["state"] == "attempting"
- def test_other_rejection_at_attempt_cap_is_abandoned_not_claimed(self):
+ def test_other_rejection_keeps_the_last_attempt_for_a_restart(self):
+ """The in-process timer never spends the final budgeted attempt: an outage can outlast any
+ backoff, and a row the timer abandoned would be lost for good (review on #91655)."""
_record(platform="telegram")
dl.mark_failed("ob-1", "503 Service Unavailable")
with dl._connect() as conn:
conn.execute("UPDATE delivery_obligations SET attempts=?, updated_at=1000.0 WHERE obligation_id='ob-1'",
- (dl.MAX_ATTEMPTS,))
+ (dl.MAX_ATTEMPTS - 1,))
assert dl.sweep_failed_for_runtime("telegram", now=1000.0 + 10_000) == []
- assert _row("ob-1")["state"] == "abandoned"
+ assert dl.pending_retries(now=1000.0 + 10_000) == []
+ assert _row("ob-1")["state"] == "failed"
+ assert _row("ob-1")["attempts"] == dl.MAX_ATTEMPTS - 1
def test_claim_is_platform_scoped_and_not_reclaimed_while_attempting(self):
_record(platform="telegram")