Repository navigation
Description
Problem Description
On 2026-11-11, between 17:14Z and 17:25Z, one devops roadmap intake --title ... --confirm run from the devcontainer stopped after 38 s with this error, and wrote nothing:
Could not plan intake: The model gateway <gateway URL> did not answer (AIClientError: Cannot connect to AI provider API. Check network access, api_base_url, and API key.), so intake wrote nothing.
The same command 1 min later worked. Other intake runs in the same hour worked. The llm-gateway pod had 0 restarts and ran for 11 h at that time, so the failure was on the path from the workstation to the gateway (the tunnel host behind Access, or name resolution), not in the gateway pod.
The HTTP transport is the single retry layer of the AI client (src/devops_cli/ai/client/unified.py). Its retries did not cover this failure.
Reproduction Steps
- Run
devops roadmap intake --title <title> --body-file <file> --confirmfrom the devcontainer while many agent sessions call the gateway. - Watch for a run that stops with "Cannot connect to AI provider API".
The failure is intermittent. It occurred 1 time in about 12 intake runs.
Expected vs Actual Behavior
Expected: a short connect failure on the path is retried with backoff inside the outer timeout, or the error names the cause (DNS, TCP connect, TLS or an Access response).
Actual: the run stops after 38 s with a generic message, and the person or agent runs the command again by hand.
Environment Diagnostics
devops-cli at origin/main 818ac6f (v0.112.1), devcontainer on rootless Podman, the AI client sends requests to the gateway host behind Cloudflare Access.
Logs, Traces, or Terminal Output
Read the cloudflared and llm-gateway logs in Loki for 17:14Z to 17:25Z on 2026-11-11 to find the cause. The exception class under AIClientError (for example httpx.ConnectError or httpx.ConnectTimeout) is not in the terminal output.
Alternatives check of 2026-11-14: in-use.
The repository already uses this tool.
Tool: tenacity (https://pypi.org/project/tenacity), Apache-2.0, last activity 2026-10-07, maintained.
Proposed design
Problem statement
A short connect failure on the path from the workstation to the model gateway stops a roadmap command, and the run writes nothing. The retry exists, but it covers only the request and its headers: the transport makes ai.max_retries + 1 attempts (five by default) with waits of 1, 2, 4 and 8 s and a 5 s connect timeout, so the 38 s run in the issue had already used every attempt while the outer read timeout is 3600 s. Every gateway call reads its reply as a stream, and a reset while that reply is read happens after handle_request returns, so it is not retried at all. The error that ends the run names no cause; it says only that the provider could not be reached. The GitHub listing path and the pydantic-ai agent path are separate and are deferred.
Acceptance criteria
Requirement: Connect failure retried until the deadline
The AI client's HTTP transport SHALL send a request again after a connect error, with jittered exponential backoff, until the retry deadline, and SHALL allow up to 10 attempts for a connect error whatever ai.max_retries is.
Scenario: Six connect failures then a reply
- WHEN The first six attempts raise
httpx2.ConnectErrorand the seventh returns a reply - THEN The call returns the seventh reply, the transport records seven requests, and no
AIClientErroris raised.
Requirement: Reset before the first chunk retried
The AI client SHALL send a request again when the reply resets before the first chunk, and MUST NOT replay a reply that has yielded a chunk.
Scenario: Reset while reading the reply
- WHEN The gateway answers 200, the reply body raises
httpx2.ReadErrorbefore the first chunk, and the second attempt answers a complete reply - THEN The call returns the second reply, the client sends two requests, and no
AIClientErroris raised.
Requirement: No replay after output
A stream that yielded at least one chunk MUST NOT be replayed when the connection resets.
Scenario: Reset mid-stream
- WHEN The stream yields one chunk and the connection then resets
- THEN The stream raises
AIClientError, the spend row carries finish reasonerror, and the client sends no second request.
Requirement: Bounded by the outer read timeout
The retries SHALL stop before the outer read timeout elapses and SHALL raise AIClientError.
Scenario: Every attempt fails
- WHEN Each attempt fails with
httpx2.ConnectTimeoutafter the 5 s connect timeout and the read timeout is 60 s - THEN No attempt starts when its connect timeout would pass 60 s, no more than 10 attempts are made, and the call raises
AIClientErrorwithin 60 s.
Requirement: Error names the cause
The AIClientError of a failed connect SHALL name the class of the error, the class of the first error of its cause chain and the backend host, and MUST NOT contain the API key.
Scenario: DNS failure
- WHEN The transport raises
httpx2.ConnectErrorfromhttpcore2.ConnectErrorfromsocket.gaierror, and the retries end - THEN The message contains
ConnectError,gaierrorand the host, and contains no API key.
Requirement: One retry layer
The AI client MUST NOT add a second retry loop: one request SHALL be sent at most the number of times of the transport's policy, and the dispatch SHALL re-raise its AIClientError unchanged.
Scenario: Every attempt fails with a connect error
- WHEN The policy makes its last attempt and the transport raises
httpx2.ConnectError - THEN The client sends exactly the policy's number of requests and raises one
AIClientError.
Existing Tool Considered
tenacity (https://pypi.org/project/tenacity, Apache-2.0, maintained, last activity 2026-10-07) is the open retry library that the repository already uses: pydantic_ai.retries.HTTPX2TenacityTransport is built with tenacity stop, wait and retry strategies in src/devops_cli/ai/retries/__init__.py, and the project's own ReadRetryPolicy runs on tenacity.Retrying in src/devops_cli/http/retry.py. The change adds a jittered wait and a time-bound stop to the existing config and puts the pre-output replay in the AI client; it writes no retry library of its own. Tenacity lacks three things: it does not name the cause of a failure (DNS, TCP connect, TLS or an Access response), so the client's _connection_error adds the class, the first cause and the host; it is not an HTTP transport, so each caller wires it; and it cannot tell whether a reply has begun, so the "no replay after output" condition lives in the caller.
Key questions
- Which failures does the transport retry today, and which does it miss?: The retry transport retries every
httpx2.TransportErrorandhttpx2.TimeoutException, soConnectErrorandConnectTimeoutare already retriedai.max_retries + 1times (five by default) with waits of 1, 2, 4 and 8 s. It misses a reset while the reply is read:handle_requestreturns after the headers, and every gateway call reads its reply as a stream, so the body read is outside the transport. [sources: src/devops_cli/ai/retries/init.py:39, src/devops_cli/ai/client/base.py:57, src/devops_cli/ai/client/base.py:113, src/devops_cli/config/defaults.py:128, src/devops_cli/config/defaults.py:367, src/devops_cli/ai/client/openai.py:283, src/devops_cli/ai/client/unified.py:354] - Where does the retry of a reset before the first chunk belong?: In
LLMClient._dispatch_messagesand_dispatch_stream, around the request and its read._dispatch_messagesreturns only a complete reply, so it can replay;_dispatch_streamreplays only until it yields its first chunk. Not in the transport, which returns before the body is read and cannot know whether output began; not in_retry_chat_dispatch, which re-raisesAIClientErroron purpose and would add a second loop over the same failures. (best practice) [sources: src/devops_cli/ai/client/unified.py:362, src/devops_cli/ai/client/unified.py:890, src/devops_cli/ai/client/unified.py:736, src/devops_cli/ai/client/openai.py:283]- Chosen: The AI client's dispatch (
_dispatch_messagesand_dispatch_stream). It owns the read of the reply and knows whether a chunk has been yielded, so it can replay before output and stop after it. - Rejected: The HTTP transport.
httpx2.BaseTransport.handle_requestreturns after the headers; the body read is outside it, and it cannot tell whether a stream yielded a chunk. - Rejected:
_retry_chat_dispatch. It re-raisesAIClientErroron purpose and cannot tell whether a stream yielded, so a loop there doubles the validation retry.
- Chosen: The AI client's dispatch (
- Which library retries the call?: tenacity, already a dependency: the transport is
pydantic_ai.retries.HTTPX2TenacityTransportwith tenacity stop, wait and retry strategies, and the project'sReadRetryPolicyruns ontenacity.Retrying. The change extends the existing config with a jittered wait and a time-bound stop and adds the pre-output replay in the client; it builds no retry loop of its own. (best practice) [sources: src/devops_cli/ai/retries/init.py:9, src/devops_cli/http/retry.py:27, docs/adr/0007-tests-double-only-process-edges.md:17]- Chosen: tenacity. It is open, maintained and already a dependency, and it gives a stop condition, a jittered exponential wait and a predicate over exception types.
- Rejected:
httpx2.HTTPTransport(retries=). It retries connect errors only, adds no backoff, and cannot cover a reset while the client reads the reply. - Rejected: A hand-written retry loop. It rebuilds the backoff, the jitter and the stop condition; the project already owns one such loop in
http/retry.pyand should not add a second.
- What is the retry budget?: A connect error is retried until the client's read timeout minus one connect timeout, with waits of 1, 2, 4, 8, 16, 32 and then 60 s with jitter, at most 10 attempts, because a connect attempt sends nothing and bills nothing. A reply reset before the first chunk and a status or timeout keep
ai.max_retries + 1attempts, because a request that reached the gateway may be billed. The 5 s connect timeout stays. The 38 s run had already used all five attempts, so the attempt count, not the predicate, was the gap. (best practice) [sources: src/devops_cli/ai/client/base.py:57, src/devops_cli/config/defaults.py:128, src/devops_cli/config/defaults.py:367, README.md:137, src/devops_cli/ai/retries/init.py:29, src/devops_cli/http/retry.py:75, src/devops_cli/ai/spend/transport.py:118]- Chosen: Connect errors retried to a deadline, at most 10 attempts. The observed run used five attempts in 38 s while the outer read timeout is 3600 s, and the same command passed a minute later; a connect attempt writes and bills nothing.
- Rejected: Keep five attempts. That is what ran in the 38 s failure, so it leaves the issue's case unfixed.
- Rejected: Three attempts with a 0.5 s first wait. It is weaker than today's five attempts and cannot ride out the failures that were reported.
- Rejected: Retry for the whole read timeout. An hour-long wait stops an interactive command, and the error text names the cause when the retries end.
- Does the GitHub listing path join this change?: No. ADR 0006 records that the session does not retry a refused connect, a connect or read timeout, and
github-session-retryandgithub-session-timeoutsstate the same. Retrying the listing changes those records, so it goes to its own issue, which amends the ADR and the two specs. (best practice) [sources: docs/adr/0006-github-session-requests-go-through-gidgethub.md:17, openspec/specs/github-session-retry/spec.md:40, openspec/specs/github-session-timeouts/spec.md:9]- Chosen: A separate item. It needs the ADR 0006 retry rule and two specs changed, which is a decision and a change of its own.
- Rejected: The same change. It would silently overturn a recorded decision and two specs in a pull request about the AI client.
- How does the raised error name the cause?:
_connection_errornames the class of the error, the class of the first error of its cause chain andbackend_host, masked withmask_secrets, asmodel_call_erroralready does for an agent run. A failed name lookup then readsConnectErrorfromgaierror, a TLS failure readsSSLError, and the host is the one the client dialled, which carries no userinfo. (best practice) [sources: src/devops_cli/ai/client/unified.py:235, src/devops_cli/ai/client/unified.py:289, src/devops_cli/ai/client/base.py:130, src/devops_cli/roadmap/agent_run.py:71]- Chosen: Class, first cause and host. It answers the expected behavior with data the exception already holds, and the agent path already names the class and the first cause (fix(ai): the verify eval agent run fails with ModelConnectionError #4345).
- Rejected: A fixed text for each stage.
httpx2does not always say whether DNS, TCP or TLS failed, so the text would be a guess. - Rejected: The current message. The issue reports that the exception class is missing from the terminal output.
- Does the fix wait on the cause of the 17:14Z failure?: No. A failed lookup raises
httpx2.ConnectErrorand a connect that does not answer raisesConnectTimeout, and the retry and the cause text cover DNS, TCP, TLS and an Access answer alike, so the change does not wait on the cloudflared andllm-gatewaylogs; the next failure names its cause. (best practice) [sources: src/devops_cli/http/egress.py:180, src/devops_cli/http/egress.py:190, src/devops_cli/http/egress.py:208]- Chosen: Retry regardless of the cause. Each reported failure is a transport error that a retry can ride out, and the message names the class when it cannot.
- Rejected: Wait for the Loki logs. The logs are outside the repository, and the fix holds for each cause that the issue names.
Fit
Fits in one pull request.
Suspected block
None.
Dependencies
None.
Activity
dan-petty commentedon Nov 11, 2026
Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.
fixprefix of the title gives the type.(ai)scope of the title gives the scope.The failure is intermittent (1 in about 12 runs) and requires a manual re-run, but does not lose data or block all users.Each occurrence wastes a full intake run and forces a manual retry, but the low frequency limits total impact.The retry layer exists in unified.py; the fix adds connect-error handling with backoff or a clearer error message within the existing transport.Both report an AI client connection failure that stops a roadmap command, but #4345 covers ModelConnectionError in eval verify while this candidate covers ConnectError or ConnectTimeout in intake.dan-petty commentedon Nov 11, 2026
Another case on 2026-11-11, on the GitHub path, not the gateway path. One
devops roadmap intake --title ... --confirmrun stopped after 3 min 10 s and wrote nothing:Could not plan intake: GitHub gave no readable answer to GET /repos/dan-petty/devops-cli/issues?state=all&sort=updated&direction=desc&since=2026-11-11T20:41:32Z&per_page=100 (page 1): ConnectTimeoutThe same command worked a few minutes later. Thus a short connect failure on the GitHub listing read also stops intake. A fix must cover the two paths, or a separate item must name the GitHub path.
dan-petty commentedon Nov 12, 2026
Another occurrence on 2026-11-12 at about 17:40Z:
devops roadmap intake --title ... --body-file ... --confirmstopped after 17 s withAIClientError: Cannot connect to AI provider API, and intake wrote nothing. The same command passed on the first retry 1 minute later (it filed #5161). The other 8 intake runs of the same session passed.dan-petty commentedon Nov 13, 2026
Another case of this failure on 2026-11-13, in
devops roadmap eval verify --case 2169 --model deepseek-v4.1-flash-1017from the devcontainer through the tunnel host.AIClientError: The investigation's model call failed (ModelConnectionError, phase read, ConnectionResetError: [Errno 104] Connection reset by peer). They stopped at about 16:41Z, 16:46Z, 16:55Z and 16:56Z, after 15 s to 8 min of the run. The fifth run (16:57Z to 17:13Z) completed with no change.f9daefafc8dd3fe7afc8c583c03bb1ce(the 16:56Z run): onePOST /v1/chat/completionsgot 200 in 3.3 s, and the run stopped 8 s later with no other gateway span. The gateway log has no error for that time, andllm-gatewayhad 0 restarts.devops-cli-service commentedon Nov 14, 2026
Alternatives check: in-use
Reason: The issue needs a short connect failure (httpx.ConnectError, ConnectTimeout, or a reset while reading) retried with backoff inside the outer timeout in the AI client's HTTP transport, and the replies extend that need to the GitHub listing path. Tenacity is an open, maintained retry library that retries on chosen exception types with exponential backoff and a stop condition, and it is transport-agnostic, so it wraps both the httpx/httpx2 AI path and the gidgethub session; the searches surfaced it as the general retry library (the project already relies on tenacity's waiting parameters per ADR 0007), so it covers the whole need. No ADR in force rejects it.
The repository already uses this tool.
Tool: tenacity (https://pypi.org/project/tenacity)
Covers:
Lacks:
Tools considered:
Searches run:
Sources:
Checked on 2026-11-14. A person decides what this means for the item. The check changes no label and closes no issue.
devops-cli-service commentedon Nov 15, 2026
Status set to Ready at e5dfddb (see proposed design in issue body).
Moved to new items: #5805, #5806.
dan-petty commentedon Nov 16, 2026
Third short connect failure of intake to the gateway on 2026-11-16 (17:45Z, 17:52Z, 22:15Z): "The model gateway https:///v1 did not answer (AIClientError: Cannot connect to AI provider API...)", while the gateway liveness and model probes answered. A second try a minute later worked each time.
fix(ai): retry gateway connect failures and reset streams (#4753)
fix(ai): retry gateway connect failures and reset streams (#4753)
devops-cli-service commentedon Nov 19, 2026
Released in v0.179.2: https://github.com/dan-petty/devops-cli/releases/tag/v0.179.2
devops-cli-service commentedon Nov 19, 2026
Live since 2026-11-19 05:06 UTC