From 6399d9811e572fd27b3048bf6e1708b146a4e9ed Mon Sep 17 00:00:00 2001 From: Derek Yu <237491578+coolman-97@users.noreply.github.com> Date: Mon, 16 Nov 2026 09:43:55 -0800 Subject: [PATCH] Wait for a deployment review by its public key A deploy job knows its deployment's public key, not the review id, so it had to poll get_deployment_review itself and decide when the review had settled. wait_for_deployment_review does that on both clients with the same deadline, backoff and outcome rules as wait_for_review. The first response pins the review id. A key that later resolves to a different review raises an invalid-response error instead of returning another review's result. --- README.md | 17 +++++++ proofline/client.py | 103 +++++++++++++++++++++++++++++++++++++++++-- scripts/contracts.py | 8 +++- tests/test_http.py | 93 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 216 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8a61889d..de5967e7 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,23 @@ with ProoflineClient() as client: Each response returns the original text, decoded rows, dataset version, settled horizon, and an optional next cursor. Each CSV page includes its own header and keeps cells as strings, including nested JSON cells. NDJSON pages retain JSON values. To read additional pages, repeat the call with the returned cursor and the same dataset, window, format, and grouping. A missing next cursor marks the last page. +## Waiting for a deployment review + +A deploy job usually knows the deployment's public key, not the review id. `wait_for_deployment_review` polls that key with the same backoff, deadline and completion rules as `wait_for_review`, and returns a `ReviewOutcome`. + +```python +with ProoflineClient() as client: + result = client.wait_for_deployment_review( + UUID(os.environ["PROOFLINE_ORGANIZATION_ID"]), + os.environ["PROOFLINE_DEPLOYMENT_KEY"], + timeout=600, + ) +if result.status != "COMPLETED": + raise SystemExit(f"Deployment review is {result.status}: {result.review_url}") +``` + +The first response pins the review id. If a later poll resolves the key to a different review, the wait raises `ProoflineError` with type `urn:proofline:sdk:invalid-response` rather than report another review's result. Once the id is known, `review_url` links to that review; a wait that times out before any response links to the deployment key instead. `TIMED_OUT`, `INCOMPLETE` and the retained `gaps` mean the same as for `wait_for_review`. + ## Coverage uploads To upload coverage reports from CI, use a repository-restricted token with `coverage:write` and current repository write access. Name the full commit SHA that the tests ran on. Send 1 to 50 LCOV, Cobertura XML, JaCoCo XML, or Go cover reports in a JSON body of at most 30 MiB. diff --git a/proofline/client.py b/proofline/client.py index 0a8239b4..f05ce493 100644 --- a/proofline/client.py +++ b/proofline/client.py @@ -144,6 +144,25 @@ def _review_url(base_url: str, organization_id: UUID, review_id: UUID) -> str: ) +def _deployment_url(base_url: str, organization_id: UUID, public_key: str) -> str: + return ( + f"{base_url}/orgs/by-id/{quote(str(organization_id), safe='')}" + f"/reviews/deployments/{quote(public_key, safe='')}" + ) + + +def _wait_timeout(timeout: float) -> None: + if not math.isfinite(timeout) or timeout < 0: + raise ValueError("The review timeout must be a finite nonnegative number of seconds.") + + +def _pinned(review: CustomerReviewDetail, pinned: UUID | None) -> UUID | None: + # The first well-formed response fixes the review a deployment key names. + if pinned is None and isinstance(review.id, UUID): + return review.id + return pinned + + def _page_limits(page_size: int, max_pages: int, max_items: int) -> None: if not 1 <= page_size <= 100 or not 1 <= max_pages <= 1_000 or not 1 <= max_items <= 100_000: raise ValueError( @@ -484,8 +503,7 @@ def wait_for_review( reports completion. """ - if not math.isfinite(timeout) or timeout < 0: - raise ValueError("The review timeout must be a finite nonnegative number of seconds.") + _wait_timeout(timeout) url = _review_url(self._base_url, organization_id, review_id) last: CustomerReviewDetail | None = None deadline = _transport.DEADLINE.set(monotonic() + timeout) @@ -509,6 +527,45 @@ def wait_for_review( finally: _transport.DEADLINE.reset(deadline) + def wait_for_deployment_review( + self, organization_id: UUID, public_key: str, *, timeout: float = 300 + ) -> ReviewOutcome: + """Wait for the review of one deployment, addressed by its public key. + + The first response pins the review id. A later response for a different + review raises ProoflineError instead of reporting another review's result. + """ + + _wait_timeout(timeout) + address = _deployment_url(self._base_url, organization_id, public_key) + pinned: UUID | None = None + last: CustomerReviewDetail | None = None + deadline = _transport.DEADLINE.set(monotonic() + timeout) + interval = 5.0 + try: + while (_transport.remaining() or 0) > 0: + try: + last = self.get_deployment_review(organization_id, public_key) + except ProoflineError as error: + if error.status == 0 and (_transport.remaining() or 0) <= 0: + break + raise + pinned = _pinned(last, pinned) + if (_transport.remaining() or 0) <= 0: + break + url = _review_url(self._base_url, organization_id, pinned) if pinned else address + result = outcome(last, url, review_id=pinned) + if result is not None: + return result + sleep(min(interval, _transport.remaining() or 0)) + interval = min(60, interval * 2) + url = _review_url(self._base_url, organization_id, pinned) if pinned else address + return outcome(last, url, "TIMED_OUT", review_id=pinned) or ReviewOutcome( + "TIMED_OUT", url + ) + finally: + _transport.DEADLINE.reset(deadline) + class AsyncProoflineClient(AsyncOperations): def __init__( @@ -832,8 +889,7 @@ async def iter_reviews( async def wait_for_review( self, organization_id: UUID, review_id: UUID, *, timeout: float = 300 ) -> ReviewOutcome: - if not math.isfinite(timeout) or timeout < 0: - raise ValueError("The review timeout must be a finite nonnegative number of seconds.") + _wait_timeout(timeout) url = _review_url(self._base_url, organization_id, review_id) last: CustomerReviewDetail | None = None deadline = _transport.DEADLINE.set(monotonic() + timeout) @@ -856,3 +912,42 @@ async def wait_for_review( return outcome(last, url, "TIMED_OUT") or ReviewOutcome("TIMED_OUT", url) finally: _transport.DEADLINE.reset(deadline) + + async def wait_for_deployment_review( + self, organization_id: UUID, public_key: str, *, timeout: float = 300 + ) -> ReviewOutcome: + """Wait for the review of one deployment, addressed by its public key. + + The first response pins the review id. A later response for a different + review raises ProoflineError instead of reporting another review's result. + """ + + _wait_timeout(timeout) + address = _deployment_url(self._base_url, organization_id, public_key) + pinned: UUID | None = None + last: CustomerReviewDetail | None = None + deadline = _transport.DEADLINE.set(monotonic() + timeout) + interval = 5.0 + try: + while (_transport.remaining() or 0) > 0: + try: + last = await self.get_deployment_review(organization_id, public_key) + except ProoflineError as error: + if error.status == 0 and (_transport.remaining() or 0) <= 0: + break + raise + pinned = _pinned(last, pinned) + if (_transport.remaining() or 0) <= 0: + break + url = _review_url(self._base_url, organization_id, pinned) if pinned else address + result = outcome(last, url, review_id=pinned) + if result is not None: + return result + await async_sleep(min(interval, _transport.remaining() or 0)) + interval = min(60, interval * 2) + url = _review_url(self._base_url, organization_id, pinned) if pinned else address + return outcome(last, url, "TIMED_OUT", review_id=pinned) or ReviewOutcome( + "TIMED_OUT", url + ) + finally: + _transport.DEADLINE.reset(deadline) diff --git a/scripts/contracts.py b/scripts/contracts.py index d34ba64a..12c6d819 100644 --- a/scripts/contracts.py +++ b/scripts/contracts.py @@ -31,7 +31,13 @@ def main() -> None: from proofline import AsyncProoflineClient, ProoflineClient for client in (ProoflineClient, AsyncProoflineClient): - helpers = {"close", "aclose", "iter_reviews", "wait_for_review"} + helpers = { + "close", + "aclose", + "iter_reviews", + "wait_for_review", + "wait_for_deployment_review", + } exposed = { name for name in dir(client) diff --git a/tests/test_http.py b/tests/test_http.py index f62d93f6..b2748662 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -5,6 +5,7 @@ import inspect import json import logging +import math import socket import subprocess import sys @@ -1231,6 +1232,98 @@ async def test_polling_backoff_and_timeout_keep_review_url_and_last_record( assert len(server.requests) == 8 +DEPLOYMENT_KEY = "deploy/2026-11-16?env=prod" +DEPLOYMENT_PATH = ( + f"/api/v1/organizations/{ORG}/reviews/deployments/deploy%2F2026-11-16%3Fenv%3Dprod" +) + + +@MODES +async def test_deployment_wait_polls_the_key_and_reports_the_pinned_review( + mode: str, monkeypatch: pytest.MonkeyPatch +) -> None: + clock = Clock(monkeypatch) + responses = [review("PENDING"), review("RUNNING"), review()] + with Server(lambda request, index: Reply(body=responses[index])) as server: + async with session(mode, server.url) as client: + result = await call( + client, "wait_for_deployment_review", ORG, DEPLOYMENT_KEY, timeout=100 + ) + assert result.status == "COMPLETED" + assert result.gaps == () + assert result.review.id == REVIEW + assert result.review_url == f"{server.url}/orgs/by-id/{ORG}/reviews/{REVIEW}" + assert [(request.method, request.path) for request in server.requests] == [ + ("GET", DEPLOYMENT_PATH) + ] * 3 + assert clock.delays == [5, 10] + + +@MODES +@pytest.mark.parametrize("drift_at", [1, 2]) +async def test_deployment_wait_refuses_a_key_that_moves_to_another_review( + mode: str, drift_at: int, monkeypatch: pytest.MonkeyPatch +) -> None: + Clock(monkeypatch) + other = review() + other["id"] = "00000000-0000-4000-8000-0000000000ff" + responses = [review("RUNNING"), review("RUNNING"), review()] + responses[drift_at] = other + with Server(lambda request, index: Reply(body=responses[index])) as server: + async with session(mode, server.url) as client: + with pytest.raises(ProoflineError) as raised: + await call(client, "wait_for_deployment_review", ORG, DEPLOYMENT_KEY) + assert raised.value.type == "urn:proofline:sdk:invalid-response" + assert len(server.requests) == drift_at + 1 + + +@MODES +async def test_deployment_wait_timeout_keeps_the_deployment_address_and_last_record( + mode: str, monkeypatch: pytest.MonkeyPatch +) -> None: + clock = Clock(monkeypatch) + with Server(lambda request, index: Reply(body=review("RUNNING"))) as server: + async with session(mode, server.url) as client: + result = await call( + client, "wait_for_deployment_review", ORG, DEPLOYMENT_KEY, timeout=0 + ) + assert result.status == "TIMED_OUT" + assert result.review is None + assert result.review_url == ( + f"{server.url}/orgs/by-id/{ORG}/reviews/deployments/deploy%2F2026-11-16%3Fenv%3Dprod" + ) + assert server.requests == [] + result = await call( + client, "wait_for_deployment_review", ORG, DEPLOYMENT_KEY, timeout=30 + ) + assert result.status == "TIMED_OUT" + assert result.review.id == REVIEW + assert result.review_url == f"{server.url}/orgs/by-id/{ORG}/reviews/{REVIEW}" + for bad in (-1, math.inf, math.nan): + with pytest.raises(ValueError): + await call( + client, "wait_for_deployment_review", ORG, DEPLOYMENT_KEY, timeout=bad + ) + assert clock.delays == [5, 10, 15] + assert len(server.requests) == 3 + + +@MODES +async def test_deployment_wait_surfaces_refusals_without_polling_again( + mode: str, monkeypatch: pytest.MonkeyPatch +) -> None: + Clock(monkeypatch) + with Server( + lambda request, index: Reply(403, {"type": "urn:proofline:forbidden", "detail": PRIVATE}) + ) as server: + async with session(mode, server.url) as client: + with pytest.raises(ProoflineError) as raised: + await call(client, "wait_for_deployment_review", ORG, DEPLOYMENT_KEY) + assert raised.value.status == 403 + assert raised.value.type == "urn:proofline:forbidden" + assert len(server.requests) == 1 + + @MODES async def test_retry_after_is_bounded_by_wait_deadline( mode: str, monkeypatch: pytest.MonkeyPatch