Skip to content

extractEventType rejects valid data:-only SSE streams → synthetic "upstream returned no response" #549

Closed

Description

@Juna9969

Project: billion-context
Version: v0.1.85 (npm, bin bili)
Environment: Windows 10/11, Node via npm global install, used as OpenAI-compatible reverse proxy in front of a custom SSE gateway

Summary

In dist/index.js, extractEventType() only recognizes SSE events that carry an explicit event: line. Streams that emit valid SSE with only data: lines (which is legal: per the WHATWG server-sent events spec, data: is the only required field and event: defaults to message) get every event skipped. parseStream never sees a terminal event, so when the upstream stream ends normally the proxy synthesizes:

event: response.failed
error: {"code": "server_error", "message": "upstream returned no response"}

…and the client (codex) shows stream disconnected before completion: upstream returned no response, even though the upstream completed successfully and delivered a full response.completed event.

Root cause

extractEventType (≈ dist/index.js:52400):

function extractEventType(rawEvent) {
  for (const l of rawEvent.split("\n")) {
    if (l.startsWith("event:")) return l.slice(6).trim();
  }
  return null;
}

parseStream (≈ dist/index.js:52564) then does:

const type = extractEventType(eventStr);
if (!type || !dataLine) continue;   // ← all data-only events skipped

iterSseEvents (≈ dist/index.js:52372) splits frames on \n\n, which is fine. The problem is only the event: dependency.

Many real-world OpenAI-compatible servers (including official clients' own minimal gateways, and custom proxies) emit events as pure data: {"type":"response.created",...} lines with no event: line — this is what the codex CLI itself tolerates (it reads the type field from the JSON payload). The proxy should do the same as a fallback.

Minimal reproduction

  1. Run any SSE server that responds to POST /v1/responses with stream: true and emits only:
    data: {"type":"response.created"}
    data: {"type":"response.completed"}
    
    (no event: lines) — e.g. a 20-line Python BaseHTTPRequestHandler.
  2. Start the proxy: bili start --port 8786 (or bili codex).
  3. Point a client at http://127.0.0.1:8786/bili/http://127.0.0.1:<port>/v1 and make a streaming request (send an x-session-id header — required by this proxy).
  4. The upstream returns 200 with a complete stream, but the client receives response.failed + upstream returned no response.

Expected behavior

Events whose event: line is absent should fall back to the type field inside the data: JSON payload, e.g.:

function extractEventType(rawEvent) {
  for (const l of rawEvent.split("\n")) {
    if (l.startsWith("event:")) return l.slice(6).trim();
  }
  const dataLine = rawEvent.split("\n").find((l) => l.startsWith("data:"));
  if (dataLine) {
    try {
      const parsed = JSON.parse(dataLine.slice(5).trim());
      if (parsed && typeof parsed.type === "string") return parsed.type;
    } catch {}
  }
  return null;
}

That is backward compatible (explicit event: still wins) and makes the parser accept valid spec-compliant streams that omit the optional event: field.

Notes / environment details

  • Triggered through the full path: bili codex launcher (random port) and bili start --port 8786, client = codex TUI 0.147.0 with base_url = <bili-origin>/bili/http://127.0.0.1:8088/v1.
  • The proxy's own synthetic-error path (emitCompletion, ≈ dist/index.js:52743) is what turns the mis-parsed stream into the misleading upstream returned no response — worth reviewing too, since it masked the real cause.
  • In our case the upstream was a minimal OpenAI-compatible SSE gateway; the fix on the server side (adding event: lines) resolves it, but the parser should not require an optional field.

Activity

  1. ranxianglei commented on Sep 5, 2026

    @ranxianglei
    Owner

    🤖 ework agent · qwen3.8-27b

    [bot] 🏷 Verified and fixed — PR: #550

    Triage (verified before implementing):

    1. Bug confirmed in source. src/loop/adapter-responses.ts — extractEventType() returns null for frames without an event: line, and parseStream() then does if (!type || !dataLine) continue;, so every data-only frame is skipped. With no terminal event ever seen, a cleanly-finished upstream stream falls into the "no completion" path and emitCompletion() synthesizes exactly the response.failed / "upstream returned no response" you observed. Your root-cause analysis is correct, including the layering: the parser requiring the optional event: field is the cause; the synthetic error was the downstream mask (you were right that it deserved review — it now only fires for genuinely unterminated streams).
    2. The fallback you proposed is sound. Per the WHATWG SSE spec event: is optional, and Responses-API payloads always carry a type field equal to the event name — the same thing codex itself relies on. One adjustment in implementation: instead of re-parsing the JSON inside extractEventType, the fix parses the payload once in parseStream and falls back to the already-parsed object's type. Same semantics, no double parse. An explicit event: line still wins; non-JSON data or payloads without a string type are skipped exactly as before (strictly additive).
    3. Consistency check: the OpenAI chat adapter (adapter-openai.ts) already ignores event: lines entirely (chat-completions SSE is data-only by design) — the responses adapter was the outlier. The anthropic adapter has the same strictness (parseAnthropicSse requires an event: line); I left it unchanged since Anthropic streams always carry event lines and no compatible gateway omitting them has been reported — flagging it here for awareness rather than bundling an unreported change.

    Regression tests (3 added, tests/loop-adapters.test.ts): data-only stream parses fully to done/completed; explicit event: precedence pinned; end-to-end drain of a data-only stream emits response.completed and never the synthetic failure. Tests 1 and 3 verified to fail against the unfixed code.

    Pre-flight: typecheck pass · 949/950 tests pass (the one failure is pre-existing on clean master in this environment — missing client binaries, unrelated) · build pass.

  2. ranxianglei commented on Sep 5, 2026

    @ranxianglei
    Owner

    🤖 ework agent · qwen3.8-27b

    [bot] 🏷 Update: independent review of PR #550 is complete and it passed — full pre-flight re-run (typecheck / 949-950 tests with the one failure confirmed environmental / build) plus a negative check proving the new regression tests fail against the unfixed code. Verdict: LGTM, ready for human merge. Details in #550.

  3. ranxianglei commented on Sep 6, 2026

    @ranxianglei
    Owner

    Review 完成,PR #550 可合并。

    验证:对最新 master(含 #587/#528/#563/#534/#544)零冲突合并;typecheck ✓;全量 1139/1141(2 个已知 plugin-agent 环境失败);build ✓;tests/loop-adapters.test.ts 28/28(含本 PR 新增 3 个 F8 用例)。

    正确性:

    • WHATWG SSE 规范里 event: 可选(缺省 message),纯 data: 帧合法;codex CLI 本身就靠读 JSON payload 的 type 字段容错,proxy 对齐这个行为正确
    • explicitType ?? obj.type 顺序对:显式 event: 行优先,payload type 兜底,两者皆无才 continue(与非 JSON 帧行为一致)
    • 覆盖面确认:src/loop/adapter-responses.ts 是当前唯一的 Responses SSE 解析器(stream-responses.ts 的流式变体已移除,server.ts 把流全部路由进 compress-loop adapter);openai adapter 本来就只看 data: 行;stream.ts/stream-openai.ts 无 event: 依赖 —— 修复完整
    • 测试断言到位:data-only 全链路(含 drain e2e 断言无 upstream returned no response 合成失败)、显式 event: 优先级

    非阻塞备注(建议后续 follow-up,不属本 PR):src/loop/adapter-anthropic.ts:48 parseAnthropicSse 有同类潜在问题(if (!type) return null,data-only Anthropic 帧会被丢弃)。官方 Anthropic API 总是发 event: 行所以无实际复现,但同样的 payload type 兜底可平移。

  4. ranxianglei commented on Sep 6, 2026

    @ranxianglei
    Owner

    🤖 ework agent · qwen3.8-27b

    [bot] 🏷 收到,感谢完整 review(零冲突合并验证 + 覆盖面确认 + 1139/1141 全量回归)。

    两点说明:

    1. 合并操作:按系统规则 Agent 不能合并 PR,请手动合并:https://github.com/ranxianglei/billion-context/pull/550(当前状态 open / mergeable ✓)。合并后 CI 会自动走发布流程。
    2. anthropic follow-up:这条备注已写进 PR fix: accept data-only SSE streams in responses adapter (#549) #550 的 description("Note:" 段),合并后不会丢。它属于"无实际复现的同类潜在问题"(官方 API 总是带 event: 行),按仓库约定只报告不单开 issue;如果你想让它可跟踪,说一声我再补一个带来源标记(来源: #549 / PR #550)的 follow-up issue。
  5. added a commit that references this issue on Sep 7, 2026
  6. ranxianglei commented on Sep 7, 2026

    @ranxianglei
    Owner

    修复已随 PR#550 合并 (b5bc655): extractEventType 不再要求必须有 event: 行, data-only SSE 帧按 WHATWG 规范正常处理。PR 标题里的 (#549) 不是 closing 关键字所以没有自动关, 手动关闭。

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions