diff --git a/CHANGELOG.md b/CHANGELOG.md index e115024..d88db00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ The package version runs on its own semver; the schema it is generated from is ` ## Unreleased +## 0.158.1 (2026-11-27) + +- **Fixed: a body the client cannot send raises at once, under its own name.** A value JSON cannot hold (`patch_world({"x_tool": object()})`, the same inside a `bulk` item) or a body carrying `world` was built inside the retry loop, so its `TypeError` or `ValueError` was read as no response: retried five times with the policy's waits, then raised as `ConnectionError: gave up after 5 retries`. The request is now built once, before the first try, and only the transport's own exceptions are retried. The `Transport` protocol now says so, and that a transport refusing a request on purpose raises a `BaseException` that is not an `Exception`, which reaches the caller at once. + ## 0.158.0 (2026-11-27) - **`ApiError.limit` names the size limit a write passed, and three predicates read it**: `is_element_too_large` (`element_bytes`), `is_element_too_many_links` (`element_link_ids`) and `is_extensions_too_large` (`extensions_bytes`). keel will add `limit` to these three 422 `invalid_request` refusals, a key of the API document's `info.x-ow-limits`, where the number and its unit are; the code, `param` and the sentence stay as they are. `limit` is an open string, so a value this release does not know is read and no predicate claims it. Until keel sends the key, `limit` is None and the predicates are False: read the sentence as before. As the npm SDK's `err.limit` (4.219.0). The wire test allows `limit` on the document's error envelope once it is declared, and then requires it read and each predicate's name to be a key of `info.x-ow-limits`. diff --git a/src/onlyworlds/_version.py b/src/onlyworlds/_version.py index fca4025..68b6360 100644 --- a/src/onlyworlds/_version.py +++ b/src/onlyworlds/_version.py @@ -1 +1 @@ -__version__ = "0.158.0" +__version__ = "0.158.1" diff --git a/src/onlyworlds/http.py b/src/onlyworlds/http.py index b58df76..d36ccd4 100644 --- a/src/onlyworlds/http.py +++ b/src/onlyworlds/http.py @@ -224,7 +224,11 @@ def json(self) -> Any: class Transport(Protocol): - """One HTTP exchange. Returns every HTTP status as a Response; raises only on no response at all.""" + """One HTTP exchange. Returns every HTTP status as a Response; raises only on no response at all. + + The client reads any ``Exception`` a transport raises as no response, and retries it under its + ``RetryPolicy``. A transport that refuses a request on purpose (a wrapper that allows only some + writes) raises a ``BaseException`` that is not an ``Exception``, which reaches the caller at once.""" def __call__( self, method: str, url: str, headers: Mapping[str, str], body: bytes | None, timeout: float @@ -473,6 +477,14 @@ def _sends_pin(self, method: str, path: str) -> bool: def request( self, method: str, path: str, payload: Any = None, headers: Mapping[str, str] | None = None ) -> Response: + url, sent, body = self._prepare(method, path, payload, headers) + return self.transport(method, url, sent, body, self.timeout) + + def _prepare( + self, method: str, path: str, payload: Any, headers: Mapping[str, str] | None + ) -> tuple[str, dict[str, str], bytes | None]: + """The URL, headers and body of one request. Raises at once on a body it cannot send + (one carrying `world`, a value JSON cannot hold): the request was never made.""" sent = dict(self._headers) if path.split("?", 1)[0].strip("/") == "health": # The one route keel answers without a key (no security in its document): no credential rides. @@ -487,14 +499,16 @@ def request( raise ValueError("a request body never carries `world`; the key names the world") body = json.dumps(payload, ensure_ascii=True).encode("ascii") sent["Content-Type"] = "application/json" - return self.transport(method, self.url(path), sent, body, self.timeout) + return self.url(path), sent, body def send(self, method: str, path: str, payload: Any = None, headers: Mapping[str, str] | None = None) -> Attempted: reasons: list[str] = [] + # Built once, outside the retries: a body this client cannot send raises here, under its own name. + url, sent, body = self._prepare(method, path, payload, headers) for attempt in range(self.retry.attempts): response: Response | None try: - response = self.request(method, path, payload, headers) + response = self.transport(method, url, sent, body, self.timeout) except Exception as exc: # no response at all: timeouts, resets, DNS response, reason = None, f"{type(exc).__name__}: {exc}" else: diff --git a/tests/test_client.py b/tests/test_client.py index cb8643f..af9e426 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -5,7 +5,7 @@ import json import re import uuid -from collections.abc import Callable +from collections.abc import Callable, Mapping from typing import Any import pytest @@ -318,6 +318,36 @@ def test_rate_limited_429_is_never_retried(make_client: MakeClient, sleeps: list assert e.value.code == "rate_limited" and len(t.calls) == 1 and sleeps == [] +def test_a_body_the_client_cannot_send_raises_at_once(make_client: MakeClient, sleeps: list[float]) -> None: + """Only the transport's own exceptions are no response; the client's refusal of a body is never retried.""" + t = FakeTransport() + c = make_client(t) + with pytest.raises(TypeError, match="not JSON serializable"): + c.patch_world({"x_tool": object()}) + with pytest.raises(TypeError, match="not JSON serializable"): + c.bulk([{"type": "character", "element": {"name": "a", "x_tool": object()}}]) + with pytest.raises(ValueError, match="never carries `world`"): + c.call("POST", "character/", {"name": "a", "world": "w"}) + assert t.calls == [] and sleeps == [] + + +def test_a_transport_refusal_that_is_not_an_exception_is_never_retried(make_client: MakeClient) -> None: + """A wrapper that refuses a request on purpose raises a BaseException, as the Transport protocol says.""" + + class Refused(BaseException): + pass + + calls: list[str] = [] + + def walled(method: str, url: str, headers: Mapping[str, str], body: bytes | None, timeout: float) -> Response: + calls.append(method) + raise Refused("not this write") + + with pytest.raises(Refused): + make_client(walled).patch_world({"x_tool": 1}) + assert calls == ["PATCH"] + + def test_a_timeout_then_in_flight_retries_to_the_replay(make_client: MakeClient, sleeps: list[float]) -> None: """A retry after a timeout can meet the first request still running (409 idempotency_in_flight, Retry-After 1); the next try, same key and body, gets the replay."""