Skip to content

fix: stop consuming OpenAI Responses stream after the terminal event - #217

Merged
vastsa merged 2 commits into
vastsa:mainfrom
yexisu:fix/issue-130-close-stream
Sep 11, 2026
Merged

fix: stop consuming OpenAI Responses stream after the terminal event#217
vastsa merged 2 commits into
vastsa:mainfrom
yexisu:fix/issue-130-close-stream

Conversation

@yexisu

@yexisu yexisu commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Fixes #130

Problem

When talking to an OpenAI Responses-compatible endpoint, the client keeps consuming the SSE stream after receiving response.completed. Upstream @earendil-works/pi-ai@0.85.1 (processResponsesStream in openai-responses-shared.js) only finishes when the server closes the connection. If the backend or a reverse proxy (e.g. Nginx) holds the idle connection open instead of sending a TCP FIN, the turn hangs forever and the agent cannot continue.

Fix

Add a pnpm patch on @earendil-works/pi-ai@0.85.1 that breaks out of the event loop right after finalizeResponse runs for response.completed / response.incomplete. The consumer stops iterating, the OpenAI SDK aborts the underlying request, and the turn completes immediately without depending on the server closing the connection.

The Codex Responses adapter already returns on the terminal event, so it is unaffected.

Test

  • New regression test responses-stream-termination.test.ts: a stub fetch returns an SSE body that emits response.completed and then never ends; the stream resolves with stopReason: "stop" and correct usage instead of hanging.
  • packages/agent-runtime: 27 test files / 348 tests pass.

The patch can be dropped once a pi-ai release ships the fix upstream.

@vercel

vercel Bot commented Sep 11, 2026

Copy link
Copy Markdown

@yexisu is attempting to deploy a commit to the vastsa's projects Team on Vercel.

A member of the Team first needs to authorize it.

@vastsa vastsa left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Reviewed against the current main and the pinned @earendil-works/pi-ai@0.85.1 implementation. The issue is real: processResponsesStream finalizes response.completed/response.incomplete but otherwise keeps iterating, while the OpenAI SDK aborts the request when that iterator is broken. The pnpm patch is applied by a frozen-lockfile install, and the regression fixture hangs after the terminal SSE event as intended.

Validation: agent-runtime targeted test passed; full agent-runtime suite passed (27 files / 348 tests); agent-runtime typecheck and build passed; docs locale check passed; PR CI passed JS build/typecheck/lint/test, Rust host-core test, and docs locale pair check.

No merge-blocking issues found. The dedicated live-proxy E2E-248 remains Draft with no executable test script, so it is not independently covered beyond the regression test. Vercel is only failing because team authorization is required.

@vastsa
vastsa merged commit 25820e8 into vastsa:main Sep 11, 2026
3 of 4 checks passed
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.

[Bug]: 客户端在收到 response.completed 事件后未主动关闭流连接,导致请求长连接挂起

2 participants