Skip to content

Commit 317dfbc

Browse files
authored
fix(ai): report mid-stream connection loss instead of a decode error (#52634)
1 parent a876174 commit 317dfbc

3 files changed

Lines changed: 83 additions & 15 deletions

File tree

‎packages/ai/src/route/executor.ts‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@ const nativeTransportFailure = (error: unknown) => {
159159
return failure
160160
}
161161

162+
// HTTP hooks re-wrap response bodies, so a read failure can arrive as an HttpClientError caused by
163+
// another HttpClientError. The innermost cause carries the native failure.
164+
const rootCause = (error: unknown): unknown =>
165+
HttpClientError.isHttpClientError(error) && "cause" in error.reason && error.reason.cause !== undefined
166+
? rootCause(error.reason.cause)
167+
: error
168+
169+
const CONNECTION_LOST = "Connection lost while reading the response"
170+
162171
const httpError = (input: {
163172
readonly error: unknown
164173
readonly request: HttpClientRequest.HttpClientRequest
@@ -179,20 +188,21 @@ const httpError = (input: {
179188
}),
180189
})
181190

182-
const source =
183-
HttpClientError.isHttpClientError(input.error) && "cause" in input.error.reason
184-
? (input.error.reason.cause ?? input.error)
185-
: input.error
191+
const source = rootCause(input.error)
186192
const native = nativeTransportFailure(source)
187193
const code = native?.code
188-
const raw = native?.message ?? (input.error instanceof Error ? input.error.message : undefined)
189-
const detail = raw
190-
const message = code && detail && !detail.includes(code) ? `${code}: ${detail}` : detail
194+
const detail =
195+
code && native?.message && !native.message.includes(code) ? `${code}: ${native.message}` : native?.message
196+
const message = detail ?? (input.error instanceof Error ? input.error.message : undefined)
191197

192198
if (Cause.isTimeoutError(input.error) || Cause.isTimeoutError(source))
193199
return transportError({ message: message ?? "HTTP transport timed out", code: code ?? "Timeout" })
194200
if (!HttpClientError.isHttpClientError(input.error))
195201
return transportError({ message: message ?? "HTTP transport failed", code })
202+
// Effect reports every response body read failure as a DecodeError, but the raw byte stream decodes
203+
// nothing: provider output parsing happens later and fails as InvalidProviderOutput.
204+
if (input.operation === "read" && input.error.reason._tag === "DecodeError")
205+
return transportError({ message: detail ? `${CONNECTION_LOST}: ${detail}` : CONNECTION_LOST, code })
196206
if (input.error.reason._tag === "TransportError") {
197207
return transportError({
198208
message: message ?? input.error.reason.description ?? "HTTP transport failed",

‎packages/ai/test/executor.test.ts‎

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { describe, expect } from "bun:test"
22
import { Deferred, Effect, Fiber, Layer, Ref, Stream } from "effect"
3-
import { Headers, HttpClientError, HttpClientRequest } from "effect/unstable/http"
4-
import { LLM, AIError, HttpContext, InvalidProviderOutputError, TransportError } from "../src/index.js"
5-
import { LLMClient, RequestExecutor, WebSocketTransport, type WebSocketChannelExecutor } from "../src/route.js"
3+
import { Headers, HttpClientError, HttpClientRequest, HttpClientResponse } from "effect/unstable/http"
4+
import { LLM, AIError, HttpContext, InvalidProviderOutputError, TransportError, isRetryable } from "../src/index.js"
5+
import {
6+
LLMClient,
7+
RequestExecutor,
8+
WebSocketTransport,
9+
type HttpMiddleware,
10+
type WebSocketChannelExecutor,
11+
} from "../src/route.js"
612
import { route } from "../src/protocols/openai-chat.js"
713
import { configure } from "../src/providers/openai.js"
8-
import { dynamicResponse, fixedResponse, handlerLayer, systemError } from "./lib/http.js"
14+
import { dynamicResponse, fixedResponse, handlerLayer, systemError, truncatedStream } from "./lib/http.js"
915
import { deltaChunk } from "./lib/openai-chunks.js"
1016
import { sseEvents, sseRaw } from "./lib/sse.js"
1117
import { it } from "./lib/effect.js"
@@ -18,6 +24,8 @@ const secretRequest = HttpClientRequest.post("https://provider.test/v1/chat?api_
1824
HttpClientRequest.setHeaders(Headers.fromInput({ authorization: "Bearer header-secret-456" })),
1925
)
2026

27+
const sseRequest = HttpClientRequest.post("https://provider.test/v1/messages")
28+
2129
const expectAIError = (error: unknown) => {
2230
expect(error).toBeInstanceOf(AIError)
2331
if (!(error instanceof AIError)) throw new Error("expected AIError")
@@ -93,7 +101,9 @@ describe("RequestExecutor", () => {
93101
const error = yield* RequestExecutor.stream(executor, secretRequest).pipe(Stream.runDrain, Effect.flip)
94102

95103
expectAIError(error)
96-
expect(error.message).toBe("ECONNRESET: disconnected query-secret-123 header-secret-456")
104+
expect(error.message).toBe(
105+
"Connection lost while reading the response: ECONNRESET: disconnected query-secret-123 header-secret-456",
106+
)
97107
expect(error.reason.http).toMatchObject({ status: 200, url: secretRequest.url })
98108
expect(error.reason.cause).toMatchObject({ code: "ECONNRESET" })
99109
expect(error.reason).toMatchObject({
@@ -123,7 +133,7 @@ describe("RequestExecutor", () => {
123133
const error = yield* RequestExecutor.stream(executor, secretRequest).pipe(Stream.runDrain, Effect.flip)
124134

125135
expectAIError(error)
126-
expect(error.message).toBe("ECONNRESET: socket closed")
136+
expect(error.message).toBe("Connection lost while reading the response: ECONNRESET: socket closed")
127137
expect(error.reason.cause).toBeInstanceOf(TypeError)
128138
expect(error.reason).toMatchObject({
129139
_tag: "Transport",
@@ -144,6 +154,54 @@ describe("RequestExecutor", () => {
144154
),
145155
)
146156

157+
it.effect("reports a connection lost mid-stream through middleware that re-wraps the body", () =>
158+
Effect.gen(function* () {
159+
const executor = yield* RequestExecutor.Service
160+
const chunks: Array<Uint8Array> = []
161+
// Session HTTP hooks hand plugins a web Response, so the body is re-wrapped around the original stream.
162+
const rewrap: HttpMiddleware = (input, handler) =>
163+
Effect.gen(function* () {
164+
const response = yield* handler(input)
165+
const body = yield* Stream.toReadableStreamEffect(response.stream)
166+
return HttpClientResponse.fromWeb(
167+
input,
168+
new Response(body, { status: response.status, headers: response.headers }),
169+
)
170+
})
171+
const error = yield* RequestExecutor.stream(executor, sseRequest, rewrap).pipe(
172+
Stream.runForEach((chunk) => Effect.sync(() => chunks.push(chunk))),
173+
Effect.flip,
174+
)
175+
176+
expectAIError(error)
177+
expect(new TextDecoder().decode(chunks[0])).toBe('data: {"type":"ping"}\n\n')
178+
expect(error.message).toBe("Connection lost while reading the response: ECONNRESET: other side closed")
179+
expect(error.reason.cause).toBeInstanceOf(TypeError)
180+
expect(error.reason.http).toMatchObject({ status: 200 })
181+
expect(error.reason).toMatchObject({ _tag: "Transport", operation: "read", code: "ECONNRESET" })
182+
expect(isRetryable(error)).toBeTrue()
183+
}).pipe(
184+
Effect.provide(
185+
truncatedStream(
186+
['data: {"type":"ping"}\n\n'],
187+
new TypeError("terminated", { cause: systemError("ECONNRESET", "other side closed") }),
188+
),
189+
),
190+
),
191+
)
192+
193+
it.effect("does not report a body read failure without a native cause as a decode error", () =>
194+
Effect.gen(function* () {
195+
const executor = yield* RequestExecutor.Service
196+
const error = yield* RequestExecutor.stream(executor, sseRequest).pipe(Stream.runDrain, Effect.flip)
197+
198+
expectAIError(error)
199+
expect(error.message).toBe("Connection lost while reading the response")
200+
expect(error.reason).toMatchObject({ _tag: "Transport", operation: "read", code: undefined })
201+
expect(isRetryable(error)).toBeTrue()
202+
}).pipe(Effect.provide(truncatedStream(['data: {"type":"ping"}\n\n'], new DOMException("aborted", "AbortError")))),
203+
)
204+
147205
it.effect("preserves middleware error messages", () =>
148206
Effect.gen(function* () {
149207
const executor = yield* RequestExecutor.Service

‎packages/ai/test/provider/openai-chat.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,7 +2205,7 @@ describe("OpenAI Chat route", () => {
22052205
)
22062206

22072207
expect((yield* Ref.get(events)).some((event) => event.type === "text-delta")).toBeTrue()
2208-
expect(error.message).toBe("ECONNRESET: socket closed unexpectedly")
2208+
expect(error.message).toBe("Connection lost while reading the response: ECONNRESET: socket closed unexpectedly")
22092209
expect(error.reason).toMatchObject({
22102210
_tag: "Transport",
22112211
transport: "http",
@@ -2223,7 +2223,7 @@ describe("OpenAI Chat route", () => {
22232223
Effect.flip,
22242224
)
22252225

2226-
expect(error.message).toBe("ECONNRESET: socket closed before output")
2226+
expect(error.message).toBe("Connection lost while reading the response: ECONNRESET: socket closed before output")
22272227
expect(error.reason).toMatchObject({
22282228
_tag: "Transport",
22292229
transport: "http",

0 commit comments

Comments
 (0)