framelens 0.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of framelens might be problematic. Click here for more details.

framelens/__init__.py ADDED
@@ -0,0 +1,23 @@
1
+ from .client import (API_VERSION, AsyncFrameLens, FrameLens, format_gate_failure,
2
+ raise_for_gates, raise_for_run, verify_webhook_signature)
3
+ from .errors import (FrameLensAuthError, FrameLensConnectionError, FrameLensError,
4
+ FrameLensGateFailed, FrameLensNotFoundError,
5
+ FrameLensOrgSuspended, FrameLensPermissionError, FrameLensRateLimited,
6
+ FrameLensRunFailed, FrameLensServerError,
7
+ FrameLensTimeoutError, FrameLensValidationError,
8
+ FrameLensVersionError)
9
+
10
+ __version__ = "0.4.0"
11
+
12
+ __all__ = [
13
+ "FrameLens", "AsyncFrameLens", "verify_webhook_signature", "API_VERSION",
14
+ "__version__",
15
+ # verdict helpers
16
+ "raise_for_gates", "raise_for_run", "format_gate_failure",
17
+ # errors — `except FrameLensError` catches every one of them
18
+ "FrameLensError", "FrameLensAuthError", "FrameLensPermissionError",
19
+ "FrameLensOrgSuspended",
20
+ "FrameLensNotFoundError", "FrameLensValidationError", "FrameLensRateLimited",
21
+ "FrameLensServerError", "FrameLensConnectionError", "FrameLensTimeoutError",
22
+ "FrameLensVersionError", "FrameLensRunFailed", "FrameLensGateFailed",
23
+ ]
framelens/_retry.py ADDED
@@ -0,0 +1,70 @@
1
+ """When to try again, and how long to wait (FL-602).
2
+
3
+ The doctrine is `backend/app/adapters/resilience.py`, pointed the other way:
4
+ exponential backoff with full-range jitter, `Retry-After` honoured when the
5
+ server sends one, a bounded number of attempts. What is different here is that
6
+ this side issues **writes**, so "is this retryable" has a second half — *is
7
+ this safe to send twice*.
8
+
9
+ The rule:
10
+
11
+ - **429 is always retried, for every method.** The server refused the request;
12
+ it did not execute it. Retrying is not a duplicate.
13
+ - **5xx and transport failures are retried only for requests that are safe to
14
+ repeat.** GET and DELETE are; a bare POST is not, because a response lost on
15
+ the way back is indistinguishable from a request that never arrived, and
16
+ guessing wrong creates a second project, a second review, a second webhook.
17
+ - **POSTs the API itself makes idempotent are retried too**, marked at the
18
+ call site rather than inferred: run creation (deduplicated by fingerprint),
19
+ video upload (content-addressed), gate evaluation and cost estimation (pure
20
+ reads that happen to be POSTs because they take a body).
21
+
22
+ Nothing else is retried. A 4xx is the server saying the request is wrong, and
23
+ sending it again unchanged is a way of asking the same question louder.
24
+ """
25
+ import random
26
+
27
+ import httpx
28
+
29
+ from .errors import parse_retry_after
30
+
31
+ # Methods safe to repeat by definition.
32
+ IDEMPOTENT_METHODS = frozenset({"GET", "HEAD", "OPTIONS", "DELETE"})
33
+
34
+ RETRYABLE_STATUS = frozenset({429, 500, 502, 503, 504})
35
+
36
+
37
+ def is_retryable_status(status: int, method: str, *, idempotent: bool) -> bool:
38
+ if status == 429:
39
+ return True # refused, therefore not executed
40
+ if status not in RETRYABLE_STATUS:
41
+ return False
42
+ return idempotent or method.upper() in IDEMPOTENT_METHODS
43
+
44
+
45
+ def is_retryable_exception(exc: Exception, method: str, *, idempotent: bool) -> bool:
46
+ """Transport failures: the request may or may not have reached the server,
47
+ which is why the same safety question applies as for a 5xx."""
48
+ if not isinstance(exc, (httpx.TransportError, httpx.TimeoutException)):
49
+ return False
50
+ return idempotent or method.upper() in IDEMPOTENT_METHODS
51
+
52
+
53
+ def backoff_delay(attempt: int, *, base: float, cap: float,
54
+ retry_after: float | None = None,
55
+ rng: random.Random | None = None) -> float:
56
+ """Seconds to wait before attempt `attempt + 1` (attempt is 1-based).
57
+
58
+ A server-supplied `Retry-After` wins outright: it is the only party that
59
+ knows when its window actually reopens. Everything else is exponential
60
+ with full-range jitter — without the jitter, every job in a CI fleet that
61
+ hit the same limit comes back at the same instant and hits it again."""
62
+ if retry_after is not None:
63
+ return min(retry_after, cap)
64
+ delay = base * (2 ** (attempt - 1))
65
+ delay *= 0.5 + (rng or random).random() / 2
66
+ return min(delay, cap)
67
+
68
+
69
+ def retry_after_of(response) -> float | None:
70
+ return parse_retry_after(response.headers.get("retry-after"))