From 02816e52692bb51b6e7d369b8ee1ff44ded20641 Mon Sep 17 00:00:00 2001 From: Apoorva Rohilla <61989678+apoorvarohilla@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:11:11 +0530 Subject: [PATCH 1/7] Keep waiting for a review through rate limits and unavailability wait_for_review raised the 429 or 503 that ended one read's retries, even with most of its timeout left: five brief 503s, or one Retry-After over 60 seconds, failed a 300-second wait. A Retry-After of 20 against a 10-second wait already returned TIMED_OUT, so the outcome depended on the header's length rather than on the wait's deadline. The wait now treats such a problem like an unfinished review. It polls again no sooner than the Retry-After, or its own backoff, and returns TIMED_OUT when its deadline passes. Other problems still end the wait. A single call keeps its four-attempt and 60-second bounds. ProoflineError carries the parsed retry_after for a 429 or 503 so the wait can honor it. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- proofline/_transport.py | 19 +++++++- proofline/client.py | 21 ++++++++- tests/test_http.py | 97 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 134 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 721949da..882c05e5 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ The [SDK guide](https://proofline.sh/docs/sdks/) and [API reference](https://pro ## Errors and refusals -A refused request raises `ProoflineError` with the server problem's `status`, `type`, `title` and `detail`. When a review request creates nothing, the error also carries `refusal`, whose `reason` names the setting to change, such as `NO_LINKED_ENVIRONMENT` or `ENVIRONMENT_NOT_READY`, and `repository_name`. `RunNowRefusal` names the type of `refusal`. A refusal that does not match the public contract keeps its status, `title` and `detail` with the type `urn:proofline:sdk:invalid-response` and no `refusal`. Any other status, such as the 402 a review request answers when credits cannot pay, is a plain problem with no `refusal`. The token is redacted from every text field, and `repr` and `str` show only the status. +A refused request raises `ProoflineError` with the server problem's `status`, `type`, `title` and `detail`. When a review request creates nothing, the error also carries `refusal`, whose `reason` names the setting to change, such as `NO_LINKED_ENVIRONMENT` or `ENVIRONMENT_NOT_READY`, and `repository_name`. `RunNowRefusal` names the type of `refusal`. A refusal that does not match the public contract keeps its status, `title` and `detail` with the type `urn:proofline:sdk:invalid-response` and no `refusal`. Any other status, such as the 402 a review request answers when credits cannot pay, is a plain problem with no `refusal`. A 429 or 503 also carries `retry_after`, the seconds its valid `Retry-After` asked for, or `None`. `wait_for_review` does not end on a 429 or 503: it polls again no sooner than that `Retry-After` and returns `TIMED_OUT` if its timeout passes first. The token is redacted from every text field, and `repr` and `str` show only the status. ```python from proofline import ProoflineError diff --git a/proofline/_transport.py b/proofline/_transport.py index 63d9dab5..961be2ad 100644 --- a/proofline/_transport.py +++ b/proofline/_transport.py @@ -84,9 +84,12 @@ class ProoflineError(Exception): title names the server's problem class. A refused review request also carries the typed refusal, whose reason names the setting to change, and - the repository_name it refers to. + the repository_name it refers to. A 429 or 503 carries retry_after, the + seconds its valid Retry-After asked for, and None otherwise. """ + retry_after: float | None = None + def __init__( self, status: int, @@ -199,6 +202,14 @@ def retry_delay(value: str | None, attempt: int) -> float | None: """An invalid or longer Retry-After returns the problem without an early retry.""" if value is None: return float(2**attempt) + delay = retry_after(value) + return delay if delay is not None and delay <= 60 else None + + +def retry_after(value: str | None) -> float | None: + """The seconds a valid Retry-After asks the client to wait, or None.""" + if value is None: + return None value = value.strip() if value.isascii() and value.isdigit(): try: @@ -229,7 +240,7 @@ def retry_delay(value: str | None, attempt: int) -> float | None: delay = max(0.0, (date - now).total_seconds()) except (ValueError, TypeError, OverflowError): return None - return delay if math.isfinite(delay) and delay <= 60 else None + return delay if math.isfinite(delay) else None def redacted(value: object, raw: str) -> object: @@ -475,6 +486,8 @@ def handle_request(self, request: httpx2.Request) -> httpx2.Response: or attempt == 3 or delay is None ): + if response.status_code in (429, 503): + error.retry_after = retry_after(response.headers.get("Retry-After")) raise error from None sleep(delay_within_deadline(delay)) raise timeout_error() @@ -526,6 +539,8 @@ async def handle_async_request(self, request: httpx2.Request) -> httpx2.Response or attempt == 3 or delay is None ): + if response.status_code in (429, 503): + error.retry_after = retry_after(response.headers.get("Retry-After")) raise error from None await async_sleep(delay_within_deadline(delay)) raise timeout_error() diff --git a/proofline/client.py b/proofline/client.py index 1d8c1519..b0d58146 100644 --- a/proofline/client.py +++ b/proofline/client.py @@ -554,6 +554,9 @@ def wait_for_review( head_commit_sha holds the outcome to that commit. A review pinned to another head ends the wait on that read and is never COMPLETED. + + A rate limit or unavailable service (429 or 503) does not end the wait: + it polls again no sooner than the Retry-After, until the deadline. """ if not math.isfinite(timeout) or timeout < 0: @@ -570,7 +573,14 @@ def wait_for_review( except ProoflineError as error: if error.status == 0 and (_transport.remaining() or 0) <= 0: break - raise + if error.status not in (429, 503): + raise + # The read exhausted its own retries. The service asked to be + # polled later, so wait for that within this deadline. + pause = max(interval, error.retry_after or 0) + sleep(min(pause, _transport.remaining() or 0)) + interval = min(60, interval * 2) + continue if (_transport.remaining() or 0) <= 0: break result = outcome(last, url, review_id=review_id, head_commit_sha=head_commit_sha) @@ -956,7 +966,14 @@ async def wait_for_review( except ProoflineError as error: if error.status == 0 and (_transport.remaining() or 0) <= 0: break - raise + if error.status not in (429, 503): + raise + # The read exhausted its own retries. The service asked to be + # polled later, so wait for that within this deadline. + pause = max(interval, error.retry_after or 0) + await async_sleep(min(pause, _transport.remaining() or 0)) + interval = min(60, interval * 2) + continue if (_transport.remaining() or 0) <= 0: break result = outcome(last, url, review_id=review_id, head_commit_sha=head_commit_sha) diff --git a/tests/test_http.py b/tests/test_http.py index 0af9d60f..1c7338eb 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -1430,6 +1430,103 @@ async def test_retry_after_is_bounded_by_wait_deadline( assert clock.delays == [10] +# A wait outlives one request's retry budget: a rate limit or an unavailable +# service is polled again within the wait's deadline, never earlier than the +# service asked, and only the deadline turns it into TIMED_OUT. +@MODES +@pytest.mark.parametrize("status", [429, 503]) +async def test_wait_keeps_polling_after_one_request_exhausts_its_retries( + mode: str, status: int, monkeypatch: pytest.MonkeyPatch +) -> None: + clock = Clock(monkeypatch) + + def respond(request: Request, index: int) -> Reply: + if index < 5: + return Reply(status, {"detail": "Unavailable."}, {"Retry-After": "0"}) + return Reply(body=review()) + + with Server(respond) as server: + async with session(mode, server.url) as client: + result = await call(client, "wait_for_review", ORG, REVIEW, timeout=300) + assert result.status == "COMPLETED" + # Four attempts in the first read, then the wait's own five-second poll. + assert len(server.requests) == 6 + assert clock.delays == [0, 0, 0, 5, 0] + + +@MODES +@pytest.mark.parametrize("status", [429, 503]) +async def test_wait_honors_a_retry_after_longer_than_one_request_retries( + mode: str, status: int, monkeypatch: pytest.MonkeyPatch +) -> None: + clock = Clock(monkeypatch) + + def respond(request: Request, index: int) -> Reply: + if index == 0: + return Reply(status, {"detail": "Unavailable."}, {"Retry-After": "90"}) + return Reply(body=review()) + + with Server(respond) as server: + async with session(mode, server.url) as client: + result = await call(client, "wait_for_review", ORG, REVIEW, timeout=300) + assert result.status == "COMPLETED" + assert len(server.requests) == 2 + assert clock.delays == [90] + + +@MODES +@pytest.mark.parametrize("status", [429, 503]) +async def test_wait_times_out_when_retry_after_outlasts_its_deadline( + mode: str, status: int, monkeypatch: pytest.MonkeyPatch +) -> None: + clock = Clock(monkeypatch) + with Server( + lambda request, index: Reply(status, {"detail": "Unavailable."}, {"Retry-After": "90"}) + ) as server: + async with session(mode, server.url) as client: + result = await call(client, "wait_for_review", ORG, REVIEW, timeout=60) + assert result.status == "TIMED_OUT" + assert result.review_url == f"{server.url}/orgs/by-id/{ORG}/reviews/{REVIEW}" + assert len(server.requests) == 1 + assert clock.delays == [60] + + +@MODES +async def test_wait_keeps_the_last_review_read_before_the_service_was_unavailable( + mode: str, monkeypatch: pytest.MonkeyPatch +) -> None: + Clock(monkeypatch) + + def respond(request: Request, index: int) -> Reply: + if index == 0: + return Reply(body=review("RUNNING")) + return Reply(503, {"detail": "Unavailable."}, {"Retry-After": "90"}) + + with Server(respond) as server: + async with session(mode, server.url) as client: + result = await call(client, "wait_for_review", ORG, REVIEW, timeout=100) + assert result.status == "TIMED_OUT" + assert result.review is not None + assert result.review.status == "RUNNING" + + +@MODES +@pytest.mark.parametrize("status", [400, 401, 403, 404, 500, 502]) +async def test_wait_still_raises_problems_that_are_not_retryable( + mode: str, status: int, monkeypatch: pytest.MonkeyPatch +) -> None: + clock = Clock(monkeypatch) + with Server( + lambda request, index: Reply(status, {"detail": "No."}, {"Retry-After": "0"}) + ) as server: + async with session(mode, server.url) as client: + with pytest.raises(ProoflineError) as raised: + await call(client, "wait_for_review", ORG, REVIEW, timeout=300) + assert raised.value.status == status + assert len(server.requests) == 1 + assert clock.delays == [] + + @MODES @pytest.mark.parametrize("status", [429, 503, 403]) async def test_deadline_expiring_with_a_received_problem_chunk_keeps_retry_policy( From 28d21ac28f402521cd8f9eb891d68bce8a28440a Mon Sep 17 00:00:00 2001 From: Apoorva Rohilla <61989678+apoorvarohilla@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:22:29 +0530 Subject: [PATCH 2/7] Continue a wait only for a retryable problem, in bounded sleeps Review feedback on the previous commit: - Both transports raise an encoded or unreadable 429 or 503 before the retry policy reads it, and the wait retried those by status alone. Mark the problems that pass through the retry policy as retryable and continue the wait only for those, so an encoded or unreadable response still raises as it did before. - A wait with a very large timeout passed the whole Retry-After to time.sleep, which overflows near 1e10 seconds. Hold the next read until the Retry-After passes and sleep toward it in steps of at most a minute. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- proofline/_transport.py | 10 ++++-- proofline/client.py | 26 +++++++++++----- tests/test_http.py | 67 ++++++++++++++++++++++++++++++++++++++++- 4 files changed, 93 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 882c05e5..8668b56e 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ The [SDK guide](https://proofline.sh/docs/sdks/) and [API reference](https://pro ## Errors and refusals -A refused request raises `ProoflineError` with the server problem's `status`, `type`, `title` and `detail`. When a review request creates nothing, the error also carries `refusal`, whose `reason` names the setting to change, such as `NO_LINKED_ENVIRONMENT` or `ENVIRONMENT_NOT_READY`, and `repository_name`. `RunNowRefusal` names the type of `refusal`. A refusal that does not match the public contract keeps its status, `title` and `detail` with the type `urn:proofline:sdk:invalid-response` and no `refusal`. Any other status, such as the 402 a review request answers when credits cannot pay, is a plain problem with no `refusal`. A 429 or 503 also carries `retry_after`, the seconds its valid `Retry-After` asked for, or `None`. `wait_for_review` does not end on a 429 or 503: it polls again no sooner than that `Retry-After` and returns `TIMED_OUT` if its timeout passes first. The token is redacted from every text field, and `repr` and `str` show only the status. +A refused request raises `ProoflineError` with the server problem's `status`, `type`, `title` and `detail`. When a review request creates nothing, the error also carries `refusal`, whose `reason` names the setting to change, such as `NO_LINKED_ENVIRONMENT` or `ENVIRONMENT_NOT_READY`, and `repository_name`. `RunNowRefusal` names the type of `refusal`. A refusal that does not match the public contract keeps its status, `title` and `detail` with the type `urn:proofline:sdk:invalid-response` and no `refusal`. Any other status, such as the 402 a review request answers when credits cannot pay, is a plain problem with no `refusal`. A 429 or 503 problem the service sent is `retryable`, and its `retry_after` holds the seconds its valid `Retry-After` asked for, or `None`; an encoded or unreadable response is not retryable. `wait_for_review` does not end on a retryable problem: it polls again no sooner than that `Retry-After` and returns `TIMED_OUT` if its timeout passes first. The token is redacted from every text field, and `repr` and `str` show only the status. ```python from proofline import ProoflineError diff --git a/proofline/_transport.py b/proofline/_transport.py index 961be2ad..4919d0f1 100644 --- a/proofline/_transport.py +++ b/proofline/_transport.py @@ -84,10 +84,12 @@ class ProoflineError(Exception): title names the server's problem class. A refused review request also carries the typed refusal, whose reason names the setting to change, and - the repository_name it refers to. A 429 or 503 carries retry_after, the - seconds its valid Retry-After asked for, and None otherwise. + the repository_name it refers to. A 429 or 503 problem the service sent + is retryable, and retry_after holds the seconds its valid Retry-After + asked for, or None. An encoded or unreadable response is not retryable. """ + retryable: bool = False retry_after: float | None = None def __init__( @@ -487,6 +489,8 @@ def handle_request(self, request: httpx2.Request) -> httpx2.Response: or delay is None ): if response.status_code in (429, 503): + # Only a problem read through this retry policy may be retried later. + error.retryable = True error.retry_after = retry_after(response.headers.get("Retry-After")) raise error from None sleep(delay_within_deadline(delay)) @@ -540,6 +544,8 @@ async def handle_async_request(self, request: httpx2.Request) -> httpx2.Response or delay is None ): if response.status_code in (429, 503): + # Only a problem read through this retry policy may be retried later. + error.retryable = True error.retry_after = retry_after(response.headers.get("Retry-After")) raise error from None await async_sleep(delay_within_deadline(delay)) diff --git a/proofline/client.py b/proofline/client.py index b0d58146..0864411e 100644 --- a/proofline/client.py +++ b/proofline/client.py @@ -555,8 +555,8 @@ def wait_for_review( head_commit_sha holds the outcome to that commit. A review pinned to another head ends the wait on that read and is never COMPLETED. - A rate limit or unavailable service (429 or 503) does not end the wait: - it polls again no sooner than the Retry-After, until the deadline. + A retryable rate limit or unavailable service (429 or 503) does not end + the wait: it polls again no sooner than the Retry-After, until the deadline. """ if not math.isfinite(timeout) or timeout < 0: @@ -566,19 +566,24 @@ def wait_for_review( last: CustomerReviewDetail | None = None deadline = _transport.DEADLINE.set(monotonic() + timeout) interval = 5.0 + not_before = 0.0 try: while (_transport.remaining() or 0) > 0: + held = not_before - monotonic() + if held > 0: + # Sleep in bounded steps until the service's Retry-After passes. + sleep(min(held, 60, _transport.remaining() or 0)) + continue try: last = self.get_review(organization_id, review_id) except ProoflineError as error: if error.status == 0 and (_transport.remaining() or 0) <= 0: break - if error.status not in (429, 503): + if not error.retryable: raise # The read exhausted its own retries. The service asked to be # polled later, so wait for that within this deadline. - pause = max(interval, error.retry_after or 0) - sleep(min(pause, _transport.remaining() or 0)) + not_before = monotonic() + max(interval, error.retry_after or 0) interval = min(60, interval * 2) continue if (_transport.remaining() or 0) <= 0: @@ -959,19 +964,24 @@ async def wait_for_review( last: CustomerReviewDetail | None = None deadline = _transport.DEADLINE.set(monotonic() + timeout) interval = 5.0 + not_before = 0.0 try: while (_transport.remaining() or 0) > 0: + held = not_before - monotonic() + if held > 0: + # Sleep in bounded steps until the service's Retry-After passes. + await async_sleep(min(held, 60, _transport.remaining() or 0)) + continue try: last = await self.get_review(organization_id, review_id) except ProoflineError as error: if error.status == 0 and (_transport.remaining() or 0) <= 0: break - if error.status not in (429, 503): + if not error.retryable: raise # The read exhausted its own retries. The service asked to be # polled later, so wait for that within this deadline. - pause = max(interval, error.retry_after or 0) - await async_sleep(min(pause, _transport.remaining() or 0)) + not_before = monotonic() + max(interval, error.retry_after or 0) interval = min(60, interval * 2) continue if (_transport.remaining() or 0) <= 0: diff --git a/tests/test_http.py b/tests/test_http.py index 1c7338eb..e559dafa 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -1471,7 +1471,72 @@ def respond(request: Request, index: int) -> Reply: result = await call(client, "wait_for_review", ORG, REVIEW, timeout=300) assert result.status == "COMPLETED" assert len(server.requests) == 2 - assert clock.delays == [90] + # Never earlier than asked, slept in steps of at most a minute. + assert clock.delays == [60, 30] + + +@MODES +async def test_wait_sleeps_a_very_long_retry_after_in_bounded_steps( + mode: str, monkeypatch: pytest.MonkeyPatch +) -> None: + clock = Clock(monkeypatch) + + def respond(request: Request, index: int) -> Reply: + if index == 0: + return Reply(503, {"detail": "Unavailable."}, {"Retry-After": "100000"}) + return Reply(body=review()) + + with Server(respond) as server: + async with session(mode, server.url) as client: + result = await call(client, "wait_for_review", ORG, REVIEW, timeout=1e12) + assert result.status == "COMPLETED" + assert len(server.requests) == 2 + # time.sleep overflows near 1e10 seconds, so no single sleep may grow with the header. + assert max(clock.delays) == 60 + assert sum(clock.delays) == 100000 + + +@MODES +@pytest.mark.parametrize("status", [429, 503]) +async def test_wait_raises_an_encoded_rate_limit_it_cannot_read( + mode: str, status: int, monkeypatch: pytest.MonkeyPatch +) -> None: + clock = Clock(monkeypatch) + body = gzip.compress(json.dumps({"detail": "Unavailable."}).encode()) + with Server( + lambda request, index: Reply(status, body, {"Content-Encoding": "gzip", "Retry-After": "0"}) + ) as server: + async with session(mode, server.url) as client: + with pytest.raises(ProoflineError) as raised: + await call(client, "wait_for_review", ORG, REVIEW, timeout=300) + assert raised.value.status == status + assert raised.value.type == "urn:proofline:sdk:invalid-response" + assert not raised.value.retryable + assert len(server.requests) == 1 + assert clock.delays == [] + + +@MODES +@pytest.mark.parametrize("status", [429, 503]) +async def test_a_rate_limit_problem_carries_its_retry_after(mode: str, status: int) -> None: + with Server( + lambda request, index: Reply(status, {"detail": "Later."}, {"Retry-After": "75"}) + ) as server: + async with session(mode, server.url) as client: + with pytest.raises(ProoflineError) as raised: + await call(client, "rerun_review", ORG, REVIEW) + assert raised.value.retryable + assert raised.value.retry_after == 75 + with pytest.raises(ProoflineError) as raised: + await call(client, "get_finding", ORG, FINDING) + assert raised.value.retryable + assert raised.value.retry_after == 75 + with Server(lambda request, index: Reply(403, {"detail": "No."})) as server: + async with session(mode, server.url) as client: + with pytest.raises(ProoflineError) as raised: + await call(client, "get_finding", ORG, FINDING) + assert not raised.value.retryable + assert raised.value.retry_after is None @MODES From 3bdcae71d251e59667c80d9a0fde8cf62777dc48 Mon Sep 17 00:00:00 2001 From: Apoorva Rohilla <61989678+apoorvarohilla@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:30:20 +0530 Subject: [PATCH 3/7] Hold a wait for an oversized Retry-After and pin the retry timing Review feedback on the previous commit: - A Retry-After with more digits than a float holds parsed to inf and was discarded, so the wait polled at its own interval. Keep it as inf: the wait holds until its deadline. retry_delay still refuses it for a single call, as before. - The docs called unreadable responses not retryable, while a fully received 429 or 503 without a problem document is retryable. Say which: only one refused before its body is read, such as an encoded one. - The Retry-After test now offers completion only after the asked-for 90 seconds and checks when each request arrived. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- proofline/_transport.py | 13 ++++++------- tests/test_http.py | 42 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 8668b56e..dd44e20e 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ The [SDK guide](https://proofline.sh/docs/sdks/) and [API reference](https://pro ## Errors and refusals -A refused request raises `ProoflineError` with the server problem's `status`, `type`, `title` and `detail`. When a review request creates nothing, the error also carries `refusal`, whose `reason` names the setting to change, such as `NO_LINKED_ENVIRONMENT` or `ENVIRONMENT_NOT_READY`, and `repository_name`. `RunNowRefusal` names the type of `refusal`. A refusal that does not match the public contract keeps its status, `title` and `detail` with the type `urn:proofline:sdk:invalid-response` and no `refusal`. Any other status, such as the 402 a review request answers when credits cannot pay, is a plain problem with no `refusal`. A 429 or 503 problem the service sent is `retryable`, and its `retry_after` holds the seconds its valid `Retry-After` asked for, or `None`; an encoded or unreadable response is not retryable. `wait_for_review` does not end on a retryable problem: it polls again no sooner than that `Retry-After` and returns `TIMED_OUT` if its timeout passes first. The token is redacted from every text field, and `repr` and `str` show only the status. +A refused request raises `ProoflineError` with the server problem's `status`, `type`, `title` and `detail`. When a review request creates nothing, the error also carries `refusal`, whose `reason` names the setting to change, such as `NO_LINKED_ENVIRONMENT` or `ENVIRONMENT_NOT_READY`, and `repository_name`. `RunNowRefusal` names the type of `refusal`. A refusal that does not match the public contract keeps its status, `title` and `detail` with the type `urn:proofline:sdk:invalid-response` and no `refusal`. Any other status, such as the 402 a review request answers when credits cannot pay, is a plain problem with no `refusal`. A 429 or 503 whose body the client received in full is `retryable`, even without a problem document, and its `retry_after` holds the seconds its valid `Retry-After` asked for, or `None`. A 429 or 503 the client refuses before reading its body, such as an encoded one, is not retryable. `wait_for_review` does not end on a retryable problem: it polls again no sooner than that `Retry-After` and returns `TIMED_OUT` if its timeout passes first. The token is redacted from every text field, and `repr` and `str` show only the status. ```python from proofline import ProoflineError diff --git a/proofline/_transport.py b/proofline/_transport.py index 4919d0f1..d3686a5d 100644 --- a/proofline/_transport.py +++ b/proofline/_transport.py @@ -84,9 +84,10 @@ class ProoflineError(Exception): title names the server's problem class. A refused review request also carries the typed refusal, whose reason names the setting to change, and - the repository_name it refers to. A 429 or 503 problem the service sent - is retryable, and retry_after holds the seconds its valid Retry-After - asked for, or None. An encoded or unreadable response is not retryable. + the repository_name it refers to. A 429 or 503 whose body was received in + full is retryable, even without a problem document, and retry_after holds + the seconds its valid Retry-After asked for, or None. A 429 or 503 refused + before its body is read, such as an encoded one, is not retryable. """ retryable: bool = False @@ -214,10 +215,8 @@ def retry_after(value: str | None) -> float | None: return None value = value.strip() if value.isascii() and value.isdigit(): - try: - delay = float(value) - except (ValueError, OverflowError): - return None + # More digits than a float holds still ask for a delay: float() gives inf. + return float(value) else: if not any(pattern.fullmatch(value) for pattern in HTTP_DATES): return None diff --git a/tests/test_http.py b/tests/test_http.py index e559dafa..a724a1b6 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -1460,9 +1460,12 @@ async def test_wait_honors_a_retry_after_longer_than_one_request_retries( mode: str, status: int, monkeypatch: pytest.MonkeyPatch ) -> None: clock = Clock(monkeypatch) + seen: list[float] = [] def respond(request: Request, index: int) -> Reply: - if index == 0: + seen.append(clock.now) + # Completion is only offered once the asked-for 90 seconds have passed. + if clock.now < seen[0] + 90: return Reply(status, {"detail": "Unavailable."}, {"Retry-After": "90"}) return Reply(body=review()) @@ -1470,11 +1473,44 @@ def respond(request: Request, index: int) -> Reply: async with session(mode, server.url) as client: result = await call(client, "wait_for_review", ORG, REVIEW, timeout=300) assert result.status == "COMPLETED" - assert len(server.requests) == 2 - # Never earlier than asked, slept in steps of at most a minute. + assert seen == [seen[0], seen[0] + 90] + # Slept in steps of at most a minute. assert clock.delays == [60, 30] +@MODES +async def test_wait_holds_a_retry_after_too_long_for_a_float_until_its_deadline( + mode: str, monkeypatch: pytest.MonkeyPatch +) -> None: + clock = Clock(monkeypatch) + with Server( + lambda request, index: Reply(503, {"detail": "Unavailable."}, {"Retry-After": "9" * 400}) + ) as server: + async with session(mode, server.url) as client: + result = await call(client, "wait_for_review", ORG, REVIEW, timeout=150) + assert result.status == "TIMED_OUT" + assert len(server.requests) == 1 + assert clock.delays == [60, 60, 30] + + +@MODES +async def test_wait_retries_a_rate_limit_without_a_problem_document( + mode: str, monkeypatch: pytest.MonkeyPatch +) -> None: + Clock(monkeypatch) + + def respond(request: Request, index: int) -> Reply: + if index < 4: + return Reply(503, b"Service Unavailable", {"Content-Type": "text/html"}) + return Reply(body=review()) + + with Server(respond) as server: + async with session(mode, server.url) as client: + result = await call(client, "wait_for_review", ORG, REVIEW, timeout=300) + assert result.status == "COMPLETED" + assert len(server.requests) == 5 + + @MODES async def test_wait_sleeps_a_very_long_retry_after_in_bounded_steps( mode: str, monkeypatch: pytest.MonkeyPatch From 55f3cbab57aaa6f7ce016b6f0c073fdf192632d2 Mon Sep 17 00:00:00 2001 From: Apoorva Rohilla <61989678+apoorvarohilla@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:37:31 +0530 Subject: [PATCH 4/7] Treat a problem too deeply nested to parse as one without a document Review feedback on the previous commit: - A 429 or 503 body nested too deeply for the JSON decoder or redaction raised RecursionError out of problem parsing, before the transport could mark it retryable. Treat it like any body that is not a problem document. - Document that retry_after is inf for more seconds than a float holds. - The test of a rate limit without a problem document now checks the exact sleeps, so it proves the wait held before the final read. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- proofline/_transport.py | 12 ++++++++---- tests/test_http.py | 17 ++++++++++++++--- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index dd44e20e..47857fef 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ The [SDK guide](https://proofline.sh/docs/sdks/) and [API reference](https://pro ## Errors and refusals -A refused request raises `ProoflineError` with the server problem's `status`, `type`, `title` and `detail`. When a review request creates nothing, the error also carries `refusal`, whose `reason` names the setting to change, such as `NO_LINKED_ENVIRONMENT` or `ENVIRONMENT_NOT_READY`, and `repository_name`. `RunNowRefusal` names the type of `refusal`. A refusal that does not match the public contract keeps its status, `title` and `detail` with the type `urn:proofline:sdk:invalid-response` and no `refusal`. Any other status, such as the 402 a review request answers when credits cannot pay, is a plain problem with no `refusal`. A 429 or 503 whose body the client received in full is `retryable`, even without a problem document, and its `retry_after` holds the seconds its valid `Retry-After` asked for, or `None`. A 429 or 503 the client refuses before reading its body, such as an encoded one, is not retryable. `wait_for_review` does not end on a retryable problem: it polls again no sooner than that `Retry-After` and returns `TIMED_OUT` if its timeout passes first. The token is redacted from every text field, and `repr` and `str` show only the status. +A refused request raises `ProoflineError` with the server problem's `status`, `type`, `title` and `detail`. When a review request creates nothing, the error also carries `refusal`, whose `reason` names the setting to change, such as `NO_LINKED_ENVIRONMENT` or `ENVIRONMENT_NOT_READY`, and `repository_name`. `RunNowRefusal` names the type of `refusal`. A refusal that does not match the public contract keeps its status, `title` and `detail` with the type `urn:proofline:sdk:invalid-response` and no `refusal`. Any other status, such as the 402 a review request answers when credits cannot pay, is a plain problem with no `refusal`. A 429 or 503 whose body the client received in full is `retryable`, even without a problem document, and its `retry_after` holds the seconds its valid `Retry-After` asked for (`inf` for more seconds than a float holds), or `None`. A 429 or 503 the client refuses before reading its body, such as an encoded one, is not retryable. `wait_for_review` does not end on a retryable problem: it polls again no sooner than that `Retry-After` and returns `TIMED_OUT` if its timeout passes first. The token is redacted from every text field, and `repr` and `str` show only the status. ```python from proofline import ProoflineError diff --git a/proofline/_transport.py b/proofline/_transport.py index d3686a5d..9ad58737 100644 --- a/proofline/_transport.py +++ b/proofline/_transport.py @@ -86,8 +86,9 @@ class ProoflineError(Exception): carries the typed refusal, whose reason names the setting to change, and the repository_name it refers to. A 429 or 503 whose body was received in full is retryable, even without a problem document, and retry_after holds - the seconds its valid Retry-After asked for, or None. A 429 or 503 refused - before its body is read, such as an encoded one, is not retryable. + the seconds its valid Retry-After asked for (inf for more seconds than a + float holds), or None. A 429 or 503 refused before its body is read, such + as an encoded one, is not retryable. """ retryable: bool = False @@ -291,7 +292,10 @@ def text(value: object) -> bool: def problem(response: httpx2.Response, token: SecretStr) -> ProoflineError: try: body = response.json() - except (ValueError, UnicodeError): + if isinstance(body, dict): + body = redacted(body, token.get_secret_value()) + except (ValueError, UnicodeError, RecursionError): + # Nesting too deep to decode or redact is not a problem document either. body = None if not isinstance(body, dict): return ProoflineError( @@ -299,7 +303,7 @@ def problem(response: httpx2.Response, token: SecretStr) -> ProoflineError: "about:blank", "Proofline returned an HTTP error without a problem document.", ) - body = cast(dict[str, Any], redacted(body, token.get_secret_value())) + body = cast(dict[str, Any], body) kind = body["type"] if isinstance(body.get("type"), str) else "about:blank" title = body["title"] if isinstance(body.get("title"), str) else None if isinstance(body.get("detail"), str): diff --git a/tests/test_http.py b/tests/test_http.py index a724a1b6..d6f1d8e0 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -1494,14 +1494,23 @@ async def test_wait_holds_a_retry_after_too_long_for_a_float_until_its_deadline( @MODES +@pytest.mark.parametrize( + ("body", "content_type"), + [ + (b"Service Unavailable", "text/html"), + # Nesting too deep for the JSON decoder. + (b"[" * 100_000 + b"]" * 100_000, "application/problem+json"), + ], + ids=["html", "deep-json"], +) async def test_wait_retries_a_rate_limit_without_a_problem_document( - mode: str, monkeypatch: pytest.MonkeyPatch + mode: str, body: bytes, content_type: str, monkeypatch: pytest.MonkeyPatch ) -> None: - Clock(monkeypatch) + clock = Clock(monkeypatch) def respond(request: Request, index: int) -> Reply: if index < 4: - return Reply(503, b"Service Unavailable", {"Content-Type": "text/html"}) + return Reply(503, body, {"Content-Type": content_type}) return Reply(body=review()) with Server(respond) as server: @@ -1509,6 +1518,8 @@ def respond(request: Request, index: int) -> Reply: result = await call(client, "wait_for_review", ORG, REVIEW, timeout=300) assert result.status == "COMPLETED" assert len(server.requests) == 5 + # The transport's own backoff (1, 2, 4), then the wait's hold (5) before read five. + assert clock.delays == [1, 2, 4, 5] @MODES From 72e22a795ed4ab7e9b7886bedba1e61039ab81f3 Mon Sep 17 00:00:00 2001 From: Apoorva Rohilla <61989678+apoorvarohilla@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:45:00 +0530 Subject: [PATCH 5/7] Pin the polls and holds of a wait kept through unavailability Review feedback: the test checked only the outcome, which an early exit after the first unavailable read would also return. It now checks that the wait held for the Retry-After and read again before its deadline. Co-Authored-By: Claude Opus 5.5 --- tests/test_http.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_http.py b/tests/test_http.py index d6f1d8e0..59eb3a50 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -1607,7 +1607,7 @@ async def test_wait_times_out_when_retry_after_outlasts_its_deadline( async def test_wait_keeps_the_last_review_read_before_the_service_was_unavailable( mode: str, monkeypatch: pytest.MonkeyPatch ) -> None: - Clock(monkeypatch) + clock = Clock(monkeypatch) def respond(request: Request, index: int) -> Reply: if index == 0: @@ -1620,6 +1620,9 @@ def respond(request: Request, index: int) -> Reply: assert result.status == "TIMED_OUT" assert result.review is not None assert result.review.status == "RUNNING" + # A poll, a 90-second hold, a second unavailable read, then the deadline. + assert len(server.requests) == 3 + assert clock.delays == [5, 60, 30, 5] @MODES From 0dcf21ddac7fc96468dcd5a49d121bb0183a0f47 Mon Sep 17 00:00:00 2001 From: Apoorva Rohilla <61989678+apoorvarohilla@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:51:31 +0530 Subject: [PATCH 6/7] Leave deeply nested problem bodies to #75 #75 already catches RecursionError where problem() decodes the body, so this change no longer does. The HTML 503 test keeps its exact sleeps. Co-Authored-By: Claude Opus 5.5 --- proofline/_transport.py | 7 ++----- tests/test_http.py | 13 ++----------- 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/proofline/_transport.py b/proofline/_transport.py index 9ad58737..2a4a4a5e 100644 --- a/proofline/_transport.py +++ b/proofline/_transport.py @@ -292,10 +292,7 @@ def text(value: object) -> bool: def problem(response: httpx2.Response, token: SecretStr) -> ProoflineError: try: body = response.json() - if isinstance(body, dict): - body = redacted(body, token.get_secret_value()) - except (ValueError, UnicodeError, RecursionError): - # Nesting too deep to decode or redact is not a problem document either. + except (ValueError, UnicodeError): body = None if not isinstance(body, dict): return ProoflineError( @@ -303,7 +300,7 @@ def problem(response: httpx2.Response, token: SecretStr) -> ProoflineError: "about:blank", "Proofline returned an HTTP error without a problem document.", ) - body = cast(dict[str, Any], body) + body = cast(dict[str, Any], redacted(body, token.get_secret_value())) kind = body["type"] if isinstance(body.get("type"), str) else "about:blank" title = body["title"] if isinstance(body.get("title"), str) else None if isinstance(body.get("detail"), str): diff --git a/tests/test_http.py b/tests/test_http.py index 59eb3a50..20a5aded 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -1494,23 +1494,14 @@ async def test_wait_holds_a_retry_after_too_long_for_a_float_until_its_deadline( @MODES -@pytest.mark.parametrize( - ("body", "content_type"), - [ - (b"Service Unavailable", "text/html"), - # Nesting too deep for the JSON decoder. - (b"[" * 100_000 + b"]" * 100_000, "application/problem+json"), - ], - ids=["html", "deep-json"], -) async def test_wait_retries_a_rate_limit_without_a_problem_document( - mode: str, body: bytes, content_type: str, monkeypatch: pytest.MonkeyPatch + mode: str, monkeypatch: pytest.MonkeyPatch ) -> None: clock = Clock(monkeypatch) def respond(request: Request, index: int) -> Reply: if index < 4: - return Reply(503, body, {"Content-Type": content_type}) + return Reply(503, b"Service Unavailable", {"Content-Type": "text/html"}) return Reply(body=review()) with Server(respond) as server: From 4e054cac3d987006240f092f85c6599bb8383150 Mon Sep 17 00:00:00 2001 From: Apoorva Rohilla <61989678+apoorvarohilla@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:58:10 +0530 Subject: [PATCH 7/7] Say that a retryable problem is one read within the deadline Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- proofline/_transport.py | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 47857fef..a4722c65 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ The [SDK guide](https://proofline.sh/docs/sdks/) and [API reference](https://pro ## Errors and refusals -A refused request raises `ProoflineError` with the server problem's `status`, `type`, `title` and `detail`. When a review request creates nothing, the error also carries `refusal`, whose `reason` names the setting to change, such as `NO_LINKED_ENVIRONMENT` or `ENVIRONMENT_NOT_READY`, and `repository_name`. `RunNowRefusal` names the type of `refusal`. A refusal that does not match the public contract keeps its status, `title` and `detail` with the type `urn:proofline:sdk:invalid-response` and no `refusal`. Any other status, such as the 402 a review request answers when credits cannot pay, is a plain problem with no `refusal`. A 429 or 503 whose body the client received in full is `retryable`, even without a problem document, and its `retry_after` holds the seconds its valid `Retry-After` asked for (`inf` for more seconds than a float holds), or `None`. A 429 or 503 the client refuses before reading its body, such as an encoded one, is not retryable. `wait_for_review` does not end on a retryable problem: it polls again no sooner than that `Retry-After` and returns `TIMED_OUT` if its timeout passes first. The token is redacted from every text field, and `repr` and `str` show only the status. +A refused request raises `ProoflineError` with the server problem's `status`, `type`, `title` and `detail`. When a review request creates nothing, the error also carries `refusal`, whose `reason` names the setting to change, such as `NO_LINKED_ENVIRONMENT` or `ENVIRONMENT_NOT_READY`, and `repository_name`. `RunNowRefusal` names the type of `refusal`. A refusal that does not match the public contract keeps its status, `title` and `detail` with the type `urn:proofline:sdk:invalid-response` and no `refusal`. Any other status, such as the 402 a review request answers when credits cannot pay, is a plain problem with no `refusal`. A 429 or 503 that the client finishes reading within its deadline is `retryable`, even without a problem document, and its `retry_after` holds the seconds its valid `Retry-After` asked for (`inf` for more seconds than a float holds), or `None`. A 429 or 503 the client refuses before reading its body, such as an encoded one, is not retryable, and a deadline that passes first raises the timeout instead. `wait_for_review` does not end on a retryable problem: it polls again no sooner than that `Retry-After` and returns `TIMED_OUT` if its timeout passes first. The token is redacted from every text field, and `repr` and `str` show only the status. ```python from proofline import ProoflineError diff --git a/proofline/_transport.py b/proofline/_transport.py index 2a4a4a5e..bb8bcc3d 100644 --- a/proofline/_transport.py +++ b/proofline/_transport.py @@ -84,11 +84,12 @@ class ProoflineError(Exception): title names the server's problem class. A refused review request also carries the typed refusal, whose reason names the setting to change, and - the repository_name it refers to. A 429 or 503 whose body was received in - full is retryable, even without a problem document, and retry_after holds - the seconds its valid Retry-After asked for (inf for more seconds than a - float holds), or None. A 429 or 503 refused before its body is read, such - as an encoded one, is not retryable. + the repository_name it refers to. A 429 or 503 read in full within the + deadline is retryable, even without a problem document, and retry_after + holds the seconds its valid Retry-After asked for (inf for more seconds + than a float holds), or None. A 429 or 503 refused before its body is + read, such as an encoded one, is not retryable, and a deadline that + passes first raises the timeout instead. """ retryable: bool = False