Skip to content

Commit 38173bf

Browse files
committed
fix(openai-chat): fail closed on EOF with pending tool calls
Issue #735 accepts finish-less EOF when text was emitted, but mid-call tool assembly without finish_reason must still surface truncation as error.
1 parent cc81b9c commit 38173bf

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

‎src/adapters/openai-chat.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,12 +804,22 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
804804
if (buffer.length > 0) {
805805
if ((yield* handleDataLine(buffer)) === "terminate") return;
806806
}
807+
const hadPendingToolCallsAtEof = pendingToolCalls.length > 0;
807808
yield* flushToolCalls();
808809
// Reader EOF. A graceful close shows at least one terminal signal: `[DONE]` (returns above),
809810
// a non-null finish_reason (sawFinish), or a trailing usage chunk (providers emit usage only
810811
// at end-of-generation). If NONE of those were seen, the stream was cut mid-flight — fail
811812
// closed so the bridge emits a classified response.failed rather than a silent truncation.
812813
const sawFinish = finishReason !== undefined;
814+
if (!sawFinish && pendingUsage === undefined && hadPendingToolCallsAtEof) {
815+
debugProviderDiagnostic("openai-chat", "stream-truncated", {
816+
finishReason: finishReason ?? null,
817+
hadUsage: false,
818+
pendingToolCalls: true,
819+
});
820+
yield { type: "error", message: "upstream stream ended without a terminal signal ([DONE] or finish_reason) - possible truncation" };
821+
return;
822+
}
813823
if (!sawFinish && pendingUsage === undefined && !sawOutput) {
814824
debugProviderDiagnostic("openai-chat", "stream-truncated", {
815825
finishReason: finishReason ?? null,

0 commit comments

Comments
 (0)