Skip to content

fix(ai): honor [DONE] sentinel as clean completion in openai-completions - #9434

Merged
can1357 merged 2 commits into
mainfrom
farm/b1cf139d/openai-completions-honor-done-sentinel
Aug 23, 2026
Merged

can1357 merged 2 commits into
mainfrom
farm/b1cf139d/openai-completions-honor-done-sentinel

Conversation

@roboomp

@roboomp roboomp commented Aug 23, 2026

Copy link
Copy Markdown
Collaborator

Repro

A mocked-fetch harness feeds SSE payloads through streamOpenAICompletions and reads the resolved stopReason. Any OpenAI-compatible host that streams content and then terminates with data: [DONE] while omitting or nulling finish_reason resolves to stopReason: "error" with OpenAI completions stream closed before a finish_reason was received; only a genuine transport EOF (no [DONE]) is a real truncation.

bun test packages/ai/test/issue-9433-repro.test.ts

Cause

streamOpenAICompletionsOnce in packages/ai/src/providers/openai-completions.ts flips streamFinishedAt only on a truthy choice.finish_reason. The post-loop guard then threw ProviderResponseError { kind: "incomplete-stream" } whenever streamFinishedAt === undefined && output.content.length > 0, without distinguishing a [DONE]-terminated stream (server-agreed completion) from a real transport EOF. readSseJson returns on both [DONE] and EOF, so the consumer had no completion signal — and the existing mapStopReason(null) => "stop" branch was dead code, confirming a null/omitted finish on an otherwise-complete stream was meant to be a clean stop.

Fix

  • Track the [DONE] sentinel in an always-installed SSE observer (sawDoneSentinel), independent of the caller's onSseEvent.
  • The guard now raises incomplete-stream only when [DONE] was NOT observed (genuine EOF); a [DONE]-terminated stream falls through to normal finalization and keeps the default stop. Tool-call turns still promote to toolUse.
  • Added packages/ai/test/issue-9433-repro.test.ts covering the three [DONE]-terminated wire shapes (no finish chunk / trailing finish_reason: null / trailing empty choices: []), the tool-call promotion, and the retained genuine-EOF error; changelog entry.

Verification

bun test packages/ai/test/issue-9433-repro.test.ts → 5 pass. bun run --filter '@oh-my-pi/pi-ai' check → clean (tsgo types + biome). Full bun test in packages/ai: only pre-existing unrelated failures (proxy.test.ts env-ordering, issue-6276 Bedrock 5s timeout), both pass in isolation. Skipped pre-publish gate (skip_checks=true): bun check/bun run fix fail on check:rs/fix:rs because the cmake binary is absent from this environment (failed to execute command: No such file or directory ... is cmake not installed?) — a branch-independent toolchain gap, and this diff is TypeScript-only. Fixes #9433

OpenAI-compatible hosts that stream content then terminate with the
`[DONE]` sentinel while omitting or nulling `finish_reason` were
misclassified as truncated streams, failing every turn with an
incomplete-stream error that only surfaces after retry exhaustion.

Track the `[DONE]` sentinel via an always-installed SSE observer and
raise the incomplete-stream error only on a genuine transport EOF (no
`[DONE]` and no finish reason). `[DONE]` is the streaming protocol's
terminal signal, so a stream ending with it completed by server
agreement and now finalizes as the default stop (mapStopReason(null)).

Fixes #9433

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5e59c4fb3c

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

const result = await run(frames);
expect(result.stopReason).toBe("stop");
expect(result.errorMessage).toBeUndefined();
expect(result.text.length).toBeGreaterThan(0);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Assert the preserved completion text

The assertion result.text.length > 0 does not verify the test's stated content-preservation contract: a regression that truncates "Hello" to a single character would still pass. Include the expected reconstructed text in each fixture and assert it, or remove the preservation claim; repository guidance explicitly prohibits non-empty-output assertions without a consumer contract.

AGENTS.md reference: AGENTS.md:L270-L270

Useful? React with 👍 / 👎.

@roboomp

roboomp commented Aug 23, 2026

Copy link
Copy Markdown
Collaborator Author
  • Added the exact expected reconstructed text to every [DONE] fixture (Hello / Hel / Hel).
  • Replaced the prohibited non-empty assertion with expect(result.text).toBe(expectedText), so any truncation now fails the test.
  • Verified with bun test test/issue-9433-repro.test.ts (5 pass) and the @oh-my-pi/pi-ai package check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAI completions stream closed before a finish_reason was received — root-cause triggers

2 participants