fix(agent): retry stream on raw connection reset errors#2931
Closed
johnjansen wants to merge 1 commit into
Closed
Conversation
Contributor
Author
|
CI note: the Windows job failure is in |
Contributor
Author
|
Superseded by #2945 (fantasy v0.25.0) which adds net.Error retry at the provider layer in v0.70.0. Closing. |
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.
Problem
When the upstream model provider tears down the TCP connection mid-handshake
or before any stream bytes arrive, the error surfaces unwrapped from the
HTTP client (commonly as
read tcp ...: connection reset by peer). Fantasy'sown retry layer only fires for errors it can cast to
*fantasy.ProviderErrorwith
IsRetryable() == true. A rawsyscall.ECONNRESETnever gets thatwrapping, so the session bombs out and the user has to re-prompt by hand.
Before
agent.Streamreturns&net.OpError{Err: syscall.ECONNRESET}.ProviderError).Runenters the error branch, writes a finish message, returns.After
agent.Streamreturns the same raw error.isTransientNetErr(err)andcurrentAssistant == nil.backoff worst case.
through the existing error path unchanged.
Why it's safe
currentAssistant == nil.currentAssistantis set insidePrepareStep, which only runs once therequest reaches the model. If a single byte of response arrived, we don't
retry — we surface the error.
transientMaxAttempts = 3. Linearbackoff.
genCtx.Done()short-circuits the wait.syscall.ECONNRESET,syscall.EPIPE,io.ErrUnexpectedEOF, or the canonical "connection reset by peer" text.Context cancellation and deadlines are explicitly excluded so user-cancel
semantics are preserved.
retries don't duplicate it. The title-generation goroutine is independent.
ProviderErrors with HTTPstatus retry continue to flow through fantasy's existing logic; this layer
only fills the unwrapped-syscall-error gap.
Test plan
go test ./internal/agent/...passes.go build ./...clean.gofmt -landgoimports -lclean on changed files.(ECONNRESET, EPIPE,
io.ErrUnexpectedEOF),net.OpError-wrappedsentinel,
fmt.Errorf-wrapped sentinel, and text-only fallback.Files
internal/agent/transient.go—isTransientNetErr+ cap constant (new).internal/agent/transient_test.go— detector tests (new).internal/agent/agent.go— wrapagent.Streamin bounded retry loop.I have read
CONTRIBUTING.md.