Repository navigation
fix: fail native streams that end without a terminal event (#407) #445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -278,10 +278,19 @@ function makeFromTransport<Body, Prepared, Frame, Event, State>( | |
| }), | ||
| streamPrepared: (prepared: Prepared, request: LLMRequest, runtime: TransportRuntime) => { | ||
| const route = `${request.model.provider}/${request.model.route.id}` | ||
| // Tracks whether a protocol-terminal event was seen, so a clean EOF | ||
| // without one (proxy/gateway truncation) fails into the retry pipeline | ||
| // instead of completing silently and looping generation (#407). | ||
| let sawTerminal = false | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SUGGESTION:
Move the flag inside the stream (for example |
||
| const events = routeInput.transport | ||
| .frames(prepared, request, runtime) | ||
| .pipe( | ||
| Stream.mapEffect(decodeEvent(route)), | ||
| Stream.tap((event) => | ||
| Effect.sync(() => { | ||
| if (protocol.stream.terminal?.(event)) sawTerminal = true | ||
| }), | ||
| ), | ||
| protocol.stream.terminal ? Stream.takeUntil(protocol.stream.terminal) : (stream) => stream, | ||
| ) | ||
| return events.pipe( | ||
|
|
@@ -291,6 +300,17 @@ function makeFromTransport<Body, Prepared, Frame, Event, State>( | |
| protocol.stream.onHalt ? { onHalt: protocol.stream.onHalt } : undefined, | ||
| ), | ||
| Stream.catchCause((cause) => Stream.fail(streamError(route, `Failed to read ${route} stream`, cause))), | ||
| (stream) => | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SUGGESTION: The EOF check ignores
No protocol has both today, but the contract offers the two mechanisms independently and |
||
| Stream.concat( | ||
| stream, | ||
| Stream.fromEffect( | ||
| Effect.suspend(() => | ||
| protocol.stream.terminal === undefined || sawTerminal | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WARNING: The The new EOF check only runs for protocols that declare a That is the exact #407 loop, and the native runtime supports Anthropic ( Please make the "response completed" signal part of the contract for every protocol (add a terminal predicate to |
||
| ? Effect.void | ||
| : ProviderShared.eventError(route, "Provider stream ended without a terminal finish event"), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WARNING: The truncation failure is a non-retryable
The turn does stop instead of regenerating, which is the main point of the fix, but the claim in the PR description that the failure "feeds the session retry pipeline and, once retries are exhausted, halts the turn" does not hold: a transient gateway truncation — the most likely cause — is never retried. Either classify the failure as retryable and have the session honour |
||
| ), | ||
| ).pipe(Stream.drain), | ||
| ), | ||
| ) | ||
| }, | ||
| } satisfies Route<Body, Prepared> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -754,6 +754,36 @@ describe("OpenAI Responses route", () => { | |
| }), | ||
| ) | ||
|
|
||
| it.effect("fails the stream when EOF arrives without a terminal event", () => | ||
| Effect.gen(function* () { | ||
| const body = sseEvents( | ||
| { type: "response.output_text.delta", item_id: "msg_1", delta: "Hello" }, | ||
| { type: "response.output_text.delta", item_id: "msg_1", delta: "!" }, | ||
| ) | ||
| const error = yield* LLMClient.stream(request).pipe( | ||
| Stream.runCollect, | ||
| Effect.flip, | ||
| Effect.provide(fixedResponse(body)), | ||
| ) | ||
|
|
||
| expect(error.message).toContain("Provider stream ended without a terminal finish event") | ||
| }), | ||
| ) | ||
|
|
||
| it.effect("streams cleanly when a terminal event closes the response", () => | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SUGGESTION: Nothing pins the new The A case next to these two would lock it in: it.effect("does not report truncation when the provider ends with an error event", () =>
Effect.gen(function* () {
const events = Array.from(
yield* LLMClient.stream(request).pipe(
Stream.runCollect,
Effect.provide(fixedResponse(sseEvents({ type: "error", code: "rate_limit_exceeded", message: "Slow down" }))),
),
)
expect(events[events.length - 1]).toMatchObject({ type: "provider-error" })
}),
)Also, the new EOF test asserts only a message substring; asserting the failure reason as well would keep the classification from drifting. |
||
| Effect.gen(function* () { | ||
| const body = sseEvents( | ||
| { type: "response.output_text.delta", item_id: "msg_1", delta: "Hello" }, | ||
| { type: "response.completed", response: { id: "resp_1" } }, | ||
| ) | ||
| const events = Array.from( | ||
| yield* LLMClient.stream(request).pipe(Stream.runCollect, Effect.provide(fixedResponse(body))), | ||
| ) | ||
|
|
||
| expect(events[events.length - 1]).toMatchObject({ type: "finish", reason: "stop" }) | ||
| }), | ||
| ) | ||
|
|
||
| it.effect("parses reasoning summary stream fixtures", () => | ||
| Effect.gen(function* () { | ||
| const body = sseEvents( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SUGGESTION: Adding
errorto the terminal set stops reading at the firsterrorevent, so anything the provider sends afterwards is droppedTERMINAL_TYPESdrivesStream.takeUntil(src/route/client.ts:294), so witherrorin the set the client now cancels the provider stream at the firsterrorevent instead of draining it to EOF. Events after it are never parsed — for example aresponse.failedthat follows anerrorcarries the failure details underresponse.error(src/protocols/openai-responses.ts:900), and the user would no longer see them because the stream was already halted.If the intent is only to stop the truncation error from masking a provider error, consider tracking "the provider reported an error" separately from "stop reading" rather than mixing it with the completion sentinels. Also note the comment above still says "All three end the stream" while the set now holds four types.