From b68bec71591a188a161c3a45a37813111ccf4d9a Mon Sep 17 00:00:00 2001 From: Agochar Bhatia Date: Mon, 2 Nov 2026 21:45:19 -0500 Subject: [PATCH] Fix stale publish results after revision lookup errors --- src/smallestai/atoms/helpers/versioning.py | 14 ++-- .../test_versioning_publish_baseline_http.py | 84 +++++++++++++++++++ 2 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 tests/custom/test_versioning_publish_baseline_http.py diff --git a/src/smallestai/atoms/helpers/versioning.py b/src/smallestai/atoms/helpers/versioning.py index e689eb9b..2b5e5b61 100644 --- a/src/smallestai/atoms/helpers/versioning.py +++ b/src/smallestai/atoms/helpers/versioning.py @@ -211,15 +211,11 @@ def publish_and_wait( """ # Capture the newest revision *before* publishing so wait_for_commit does not # match the previous (already-published) revision during the window before the - # new one surfaces in the listing. - baseline_id: typing.Optional[str] = None - try: - pre = self.revisions.list(id=agent_id, branch_id=branch_id, limit=1) - pre_revs = getattr(pre.data, "revisions", None) or [] - if pre_revs: - baseline_id = pre_revs[0].id - except Exception: - baseline_id = None + # new one surfaces in the listing. A failed lookup must stop the publish: + # without a baseline, the later poll cannot identify the new revision. + pre = self.revisions.list(id=agent_id, branch_id=branch_id, limit=1) + pre_revs = getattr(pre.data, "revisions", None) or [] + baseline_id = pre_revs[0].id if pre_revs else None res = self.branches.publish_draft(id=agent_id, branch_id=branch_id, label=label) data = res.data diff --git a/tests/custom/test_versioning_publish_baseline_http.py b/tests/custom/test_versioning_publish_baseline_http.py new file mode 100644 index 00000000..0ec9f713 --- /dev/null +++ b/tests/custom/test_versioning_publish_baseline_http.py @@ -0,0 +1,84 @@ +"""Exercise publish baseline handling through the generated client and in-memory HTTP.""" + +import httpx +import pytest + +from smallestai import SmallestAI +from smallestai.atoms.helpers.versioning import SecurityCheckFailedError, Versioning +from smallestai.core.api_error import ApiError + + +# --- Synthetic transport helpers --- +def _versioning_transport(*, fail_baseline=False, first_publish=False, scan_fails=False): + calls = [] + listings = 0 + + def respond(request): + nonlocal listings + path = request.url.path + calls.append((request.method, path)) + if request.method == "GET" and path.endswith("/revisions"): + listings += 1 + if fail_baseline and listings == 1: + return httpx.Response(503, json={"error": "temporary lookup failure"}) + if first_publish: + revision_id = None if listings == 1 else "r1" + else: + revision_id = "r1" if listings <= 2 else "r2" + revisions = [{"_id": revision_id}] if revision_id else [] + return httpx.Response(200, json={"status": True, "data": {"revisions": revisions}}) + if request.method == "POST" and path.endswith("/draft/publish"): + return httpx.Response(202, json={"status": True, "data": {"state": "scanning"}}) + if request.method == "GET" and "/revisions/" in path: + revision_id = path.rsplit("/", 1)[-1] + revision = {"_id": revision_id, "status": "published"} + if scan_fails and revision_id == "r2": + revision = {"_id": revision_id, "status": "scanning", "securityCheck": {"status": "failed"}} + return httpx.Response( + 200, + json={"status": True, "data": {"revision": revision}}, + ) + raise AssertionError(f"Unexpected synthetic request: {request.method} {path}") + + return httpx.MockTransport(respond), calls + + +def _versioning(transport): + http_client = httpx.Client(transport=transport) + return Versioning(SmallestAI(api_key="synthetic", httpx_client=http_client, max_retries=0)), http_client + + +# --- Native HTTP regression and nearby publish invariants --- +def test_failed_baseline_lookup_prevents_ambiguous_publish(): + transport, calls = _versioning_transport(fail_baseline=True) + versioning, http_client = _versioning(transport) + try: + with pytest.raises(ApiError) as error: + versioning.publish_and_wait("agent", "branch", poll_interval=0) + assert error.value.status_code == 503 + assert not any(method == "POST" for method, _ in calls) + finally: + http_client.close() + + +@pytest.mark.parametrize("first_publish,expected", [(False, "r2"), (True, "r1")]) +def test_successful_baseline_waits_for_its_publish(first_publish, expected): + transport, calls = _versioning_transport(first_publish=first_publish) + versioning, http_client = _versioning(transport) + try: + revision = versioning.publish_and_wait("agent", "branch", poll_interval=0) + assert revision.id == expected + assert sum(method == "POST" for method, _ in calls) == 1 + finally: + http_client.close() + + +def test_new_revision_scan_failure_is_not_success(): + transport, calls = _versioning_transport(scan_fails=True) + versioning, http_client = _versioning(transport) + try: + with pytest.raises(SecurityCheckFailedError): + versioning.publish_and_wait("agent", "branch", poll_interval=0) + assert sum(method == "POST" for method, _ in calls) == 1 + finally: + http_client.close()