Repository navigation
Retry transient failures and reuse connections in HTTP targets - #189
Merged
Merged
Conversation
HTTPAppTarget, DecisionTarget and ASRTarget sent each turn once, so a single 408, 429 or 5xx, a timeout or a dropped connection turned the whole scenario into ERROR. They also opened a new httpx.AsyncClient for every turn. DecisionTarget failed with a bare JSONDecodeError on a body that is not JSON and did not show the body when the reply had no answer, and ASRTarget read the audio file on the event loop. - Retry HTTP 408, 429 and 5xx other than 501, 505 and 511, timeouts and dropped connections, waiting retry_backoff * 2**attempt seconds plus up to 25% jitter, or the server's Retry-After (seconds or an HTTP date; one that cannot be parsed falls back to the backoff), each wait capped at 60 s. New max_retries (default 2) and retry_backoff (default 0.5) parameters, separate from the Auditor's max_retries; max_retries=0 sends each turn once. Any other 4xx fails at once. HTTPAppTarget and DecisionTarget resend the same body and headers; ASRTarget resends the same form fields, file contents, authorization and trace headers, under a new multipart boundary. - Keep one client per running event loop, closed when the loop shuts down, when the target is garbage-collected, or by the new aclose(). The client has no connection cap, so concurrent turns never wait for a connection, and with max_retries=0 it keeps no idle connections, so each turn goes out once on a new connection. The target's timeout is sent with each request, so changing target.timeout takes effect. A copied or pickled target starts without clients. An injected client stays the caller's, with its own timeout and limits. ModelAuditor.aclose() still closes only the auditor's own model clients. - DecisionTarget: a body that is not JSON raises a ValueError, and a reply without an answer a RuntimeError, both showing the start of the body. post_systemone takes max_retries (default 0) and request_timeout, so DecisionJudgeClient keeps its own retry loop and timeout handling as before. - ASRTarget reads the audio file with asyncio.to_thread. The target docstrings and the README describe duplicate requests (X-SimpleAudit-Turn-ID), the rough worst-case turn time (httpx applies the timeout to each phase of a request, not to the whole of it), the jitter, closing a manually run event loop, and that the Auditor's max_retries does not apply to these targets.
Contributor
|
#131 added retries and client reuse to |
kelkalot
reviewed
Dec 4, 2026
kelkalot
left a comment
Collaborator
There was a problem hiding this comment.
Review notes. One point I'd like fixed before merging; the rest can follow later.
Before merging
- Cookies are now shared between scenarios (
simpleaudit/targets/_http.py,LoopClients.get). Anhttpx.AsyncClientstores everySet-Cookieit receives and sends it with later requests. Until now each turn had its own client, so no request carried a cookie. With one client per event loop, an app that keeps conversation state in a session cookie receives scenario A's cookie on scenario B's turns. Checked with httpx 0.28.1 and a mock transport: the second request on a shared client carries the cookie from the first reply. A client created withcookies=CookieJar(policy=DefaultCookiePolicy(allowed_domains=[]))(fromhttp.cookiejar) stores nothing, and aCookieheader set by the caller is still sent. Could the targets' own clients be created like that, with a test?
Can follow later
max_retriesandretry_backoffare not validated.max_retries=Nonefails on the first turn with aTypeError, and a negative value is silently treated as 0.- A wait taken from
Retry-Aftergets no jitter, so parallel scenarios that were rate-limited together also retry together. - A
Retry-Afterlonger than 60 s is shortened to 60 s, and the request is sent again before the server allows it. copy.copy(target)shares the original's clients. Onlydeepcopyand pickle start without clients, while the docstring says every copy does.test_without_a_client_one_is_created_per_request_and_closednow covers one client per loop. Its name and the loosened assertion no longer say that.- Failures that a retry cannot fix (unknown host, refused connection, certificate error) are retried like transient ones.
Retries are on by default, so the release notes should list this under behaviour changes.
kelkalot
pushed a commit
that referenced
this pull request
Dec 4, 2026
Restores #119's endpoint provenance, which went into dev with #131 and was lost in the 4 Nov force-push, on top of #189's _http.py. ASRTarget, DecisionTarget and HTTPAppTarget add an endpoint block (scheme, host, port, via_proxy) to TargetResponse.raw and warn once at construction on plain http to a non-loopback host. ASRTarget uploads the file as audio<suffix>. The warning now points at the caller also when the target is built through a classmethod, and a suffix that is not a short extension is dropped from the upload name. Retries and client reuse from #189 are unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #107 (gaps 2 and 3). #131 fixed both for
DecisionTargetand was merged intodevon 28 Oct, but the force-push ofdevon 4 Nov dropped it, so it has not been ondevor in a release since. This PR covers the same two gaps forHTTPAppTarget,DecisionTargetandASRTarget.Differences from #131:
Retry-Afteris read on any retried status, and waits get up to 25% jitter.aclose()is there, but notasync with.Problem
HTTPAppTarget,DecisionTargetandASRTargetsent each turn once. A single 408, 429 or 5xx, a timeout or a dropped connection made the scenario an ERROR.httpx.AsyncClient.DecisionTargetfailed with a bareJSONDecodeErroron a body that isn't JSON, and didn't show the body when the reply had no answer.ASRTargetread the audio file on the event loop.Fix
max_retriestimes (new, default 2).retry_backoff * 2**attempt(default 0.5 s) plus up to 25% jitter, or the server'sRetry-After(seconds or an HTTP date), capped at 60 s.max_retriesdoes not apply to them.aclose().max_retries=0the client keeps no idle connections, so each turn goes out once on a fresh connection.timeoutis sent with each request, so changing it takes effect.client=is never closed by the target, and keeps its own timeout and limits.ValueError, and a reply without an answer raisesRuntimeError. Both show the start of the body.post_systemonegainsmax_retries(default 0) andrequest_timeout(default None), so the decision judge is unchanged.asyncio.to_thread.Notes:
X-SimpleAudit-Turn-IDandtraceparent.shutdown_asyncgens()keeps connections open untilaclose()is called or the target is collected.(max_retries + 1) × timeoutplus the waits: about 5 minutes for HTTP, about 17 for Decision and ASR. It is not a hard limit, because httpx times each phase of a request separately.ValueErrorinstead ofJSONDecodeError. The failure kind is unchanged.Testing
tests/test_http_targets.py: 3 tests, 10 cases, all of which fail on dev.test_asr.py::test_bad_responsesusesretry_backoff=0.test_decision_target.pychecks the client timeout rather than the exact constructor arguments.max_retries=0: 48/48 turns ok;timeouttakes effect;asyncio.run,Runner, threads, GC, cancellation, and an injected client left open.Retry-Afterfuzz: no exceptions.Written by Opus 5.5