Skip to content

Commit 265b403

Browse files
committed
Merge pull request anomalyco#52866 from felixzsh/opencode
fix(ai): bound native stream stalls on framed events Move chunkTimeout from the raw byte stream to the framed stream so SSE comment keepalives no longer reset the deadline and mask a stalled generation. Aligns the native @opencode/ai transport with the AI SDK path, which already resets only on parsed events. Closes anomalyco#43519
2 parents 55dde88 + 359621b commit 265b403

2 files changed

Lines changed: 61 additions & 10 deletions

File tree

‎packages/ai/src/route/transport/http.ts‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,17 @@ export const httpJson = <Body, Frame>(input: HttpJsonInput<Body, Frame>): HttpJs
120120
const http = RequestExecutor.responseHttp(response)
121121
const remaining = Duration.subtract(total, Duration.millis((yield* Clock.currentTimeMillis) - started))
122122
return {
123-
frames: prepared.framing.frame(
124-
RequestExecutor.responseStream(response).pipe(
125-
Stream.timeoutOrElse({
126-
duration: timeoutDuration(request.http?.chunkTimeout),
127-
orElse: () => Stream.fail(timeout("read", "Timed out waiting for response data", http)),
128-
}),
129-
Stream.interruptWhen(
130-
Effect.sleep(remaining).pipe(
131-
Effect.andThen(Effect.fail(timeout("read", "Timed out waiting for the response to complete", http))),
132-
),
123+
// Bound the stall on framed progress, not raw bytes. Framing drops SSE
124+
// comment keepalives (`: keepalive`) and other empty events, so counting
125+
// bytes would let a provider hold a stalled generation warm forever.
126+
frames: prepared.framing.frame(RequestExecutor.responseStream(response)).pipe(
127+
Stream.timeoutOrElse({
128+
duration: timeoutDuration(request.http?.chunkTimeout),
129+
orElse: () => Stream.fail(timeout("read", "Timed out waiting for response data", http)),
130+
}),
131+
Stream.interruptWhen(
132+
Effect.sleep(remaining).pipe(
133+
Effect.andThen(Effect.fail(timeout("read", "Timed out waiting for the response to complete", http))),
133134
),
134135
),
135136
),

‎packages/ai/test/http-timeout.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,31 @@ const stalledServer = Effect.gen(function* () {
4949
return { layer, stalled, resume: () => resume() }
5050
})
5151

52+
// Sends one content chunk, then only what the test asks for. Lets a test prove
53+
// that bytes framing drops, such as SSE comment keepalives, are not progress.
54+
const heartbeatServer = Effect.gen(function* () {
55+
const started = yield* Deferred.make<void>()
56+
const encoder = new TextEncoder()
57+
let emit = (_text: string) => {}
58+
const layer = dynamicResponse((input) =>
59+
Effect.sync(() =>
60+
input.respond(
61+
new ReadableStream<Uint8Array>({
62+
start(controller) {
63+
emit = (text) => controller.enqueue(encoder.encode(text))
64+
emit(sseRaw(`data: ${JSON.stringify(deltaChunk({ content: "Hi" }))}`))
65+
},
66+
pull() {
67+
Deferred.doneUnsafe(started, Effect.void)
68+
},
69+
}),
70+
SSE,
71+
),
72+
),
73+
)
74+
return { layer, started, emit: (text: string) => emit(text) }
75+
})
76+
5277
describe("HTTP transport timeouts", () => {
5378
it.effect("fails when response headers take longer than five minutes", () =>
5479
Effect.gen(function* () {
@@ -87,6 +112,31 @@ describe("HTTP transport timeouts", () => {
87112
}),
88113
)
89114

115+
it.effect("ignores SSE comment keepalives when bounding a stalled stream", () =>
116+
Effect.gen(function* () {
117+
const server = yield* heartbeatServer
118+
const fiber = yield* LLMClient.generate(
119+
LLM.request({ model, prompt: "Hello", http: { chunkTimeout: 20_000 } }),
120+
).pipe(Effect.provide(server.layer), Effect.flip, Effect.forkChild({ startImmediately: true }))
121+
yield* Deferred.await(server.started)
122+
yield* Effect.yieldNow
123+
// A comment every 10s keeps the socket warm without carrying progress, so
124+
// the 20s chunk bound must still fire.
125+
yield* TestClock.adjust("10 seconds")
126+
server.emit(sseRaw(": keepalive"))
127+
yield* Effect.yieldNow
128+
yield* TestClock.adjust("10 seconds")
129+
const error = yield* Fiber.join(fiber)
130+
131+
expect(error.reason).toMatchObject({
132+
_tag: "Transport",
133+
transport: "http",
134+
operation: "read",
135+
code: "Timeout",
136+
})
137+
}),
138+
)
139+
90140
it.effect("applies a configured header timeout", () =>
91141
Effect.gen(function* () {
92142
const fiber = yield* LLMClient.generate(

0 commit comments

Comments
 (0)