Skip to content

[Bug] preflight 摘要抽取层:被截断的 SSE 会被当成完整摘要,delta-less 终端会被当成空摘要(#726 的恢复链路仍未闭环) #780

Closed

Description

@plyflai

背景

#726(preflight SSE 摘要为空 → 持续 502)已由 PR #727 关闭,其修复是诊断 + 缩小重试 + 死锁冷却(diagnoseEmptySummary / src/server.ts / tests/preflight-empty-summary.test.ts)。

但决定「这段文本是不是一份可用摘要」的抽取函数本身没有改,仍是朴素版。

当前实现(v0.1.111 / bfb0b68,src/preflight.ts:311 extractSummaryFromSse)

for (const line of text.split("\n")) {
    if (!line.startsWith("data:")) continue;
    ...
    if (protocol === "anthropic") { if (o.type === "content_block_delta") { /* d.type === "text_delta" */ } }
    else if (protocol === "openai") { /* choices[0].delta.content */ }
    else { if (o.type === "response.output_text.delta") out += o.delta;
           else if (o.type === "response.completed" && ...) { const full = extractSummaryText(...); if (full) return full; } }
}
return out;

即:只认 data: 行;每协议只认单一 delta 类型;循环结束无条件 return out。缺少四件事:

  1. 没有 SSE 帧完整性检查。 网关把流截断(半行 data: {...、JSON.parse 失败被 continue 跳过、缺尾部空行)时,前面已累积的文本会被当成完整摘要返回 → 一份被截断的 tier-1 摘要被持久化进会话。这比「空摘要」危险得多:空摘要会走 fix: recover preflight from empty SSE summaries + dead-end cooldown (#726) #727 的诊断/重试链路并报错,截断摘要看起来完全正常,只是永久少了一段内容。[Bug] QODER_MODEL_TRANSPORT=http 模式下 api2-v2.qoder.sh/model/v1/chat/completions 网关输出损坏的 SSE:无 [DONE]、data: 行为半截 JSON、偶发缺 finish_reason → 工具调用参数 JSON 解析失败 #764(QODER_MODEL_TRANSPORT=http 下网关输出损坏 SSE:无 [DONE]、data: 行为半截 JSON、偶发缺 finish_reason)证明这种流在真实网关里存在。
  2. 没有 event: 帧头类型兜底。 当 data: payload 里没有可用的 type 字段时无法判型。仓库其它地方都有这个写法(src/loop/adapter-anthropic.ts:54、src/loop/adapter-responses.ts:177、src/stream-terminal.ts:18 的 l.startsWith("event:")),只有 preflight 没有。
  3. 没有 delta-less 终端兜底。 只发 response.output_text.done(文本在 text)或 response.output_item.done(文本在 item 内)而不发 delta 的上游,抽取结果恒为 "" → 每次摘要都空 → 触发 fail-fast/降级,看起来像「上游不返回摘要」。
  4. 没有失败终态判定。 response.incomplete / response.failed / response.error 不被视为无效;只要失败前吐过一点文本,那点文本就会被当成摘要接受。

本地加固(自托管实例自 2026-09-13 起在跑,已验证)

  • const properlyFramed = text.endsWith("\n\n");(responses 协议要求流以完整帧收尾)

  • 解析循环先取帧头:if (line.startsWith("event:")) { eventType = line.slice(6).trim(); continue; },判型改为 const type = typeof o.type === "string" ? o.type : eventType;

  • response.output_text.done → terminalText = o.text;response.output_item.done → extractSummaryText(protocol, { output: [o.item] }) → terminalText

  • response.completed 不再立即 return full,改为置 completed = true 并记录 terminalText(与 delta 文本二者取一,非空优先)

  • response.incomplete | response.failed | response.error → invalidTerminal = true

  • 收尾:

    if (protocol === "responses") return completed && properlyFramed && !invalidTerminal ? out || terminalText : "";
    return !invalidTerminal ? out || terminalText : "";

效果:被截断的流不再产出「半份但看起来正常」的摘要,而是返回 "",从而进入 #727 已经建好的诊断 + 缩小重试 + 冷却链路——#726 的恢复链路这才真正闭环。

建议

按上面 4 点加固抽取层(约 30 行,无协议语义变更:完整流的行为完全不变),"" 继续交给 diagnoseEmptySummary 处理。

Refs #726、Refs #764

Activity

  1. ranxianglei commented on Sep 14, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

    [bot] 🏷 分诊完成,问题属实,开始实现。

    验证结果(v0.1.111 / bfb0b68,src/preflight.ts:311 extractSummaryFromSse)——四点全部成立:

    1. 第 345 行无条件 return out:半行 JSON 被 catch { continue } 跳过后,前面累积的文本被当完整摘要返回并持久化。比空摘要危险(空摘要会走 fix: recover preflight from empty SSE summaries + dead-end cooldown (#726) #727 诊断链路,截断摘要看起来完全正常),判断同意。
    2. 只认 data: 行、无 event: 帧头兜底;而 src/loop/adapter-anthropic.ts:54(parseAnthropicSse)、src/loop/adapter-responses.ts:177(extractEventType)、src/stream-terminal.ts:18(terminalFromSseLine)都有该写法,preflight 是孤例。
    3. responses 只认 response.output_text.delta / response.completed;只发 output_text.done / output_item.done 的上游抽取恒为 ""。
    4. response.incomplete | response.failed | response.error(以及通用 error 事件)不使已累积文本失效——失败前吐过的文本被当摘要接受。

    重复筛查:中英文关键词均无相似 open/closed issue 或 PR;关联链只有本 issue 引用的 #726/#727/#764,非重复。

    对你本地补丁的两处修正(实现时已改):

    • 你的 eventType 跨帧 sticky。SSE 语义里 event: 只对紧随的下一帧生效,必须在空行(帧边界)重置,否则无帧头的后续帧会继承前一帧的类型判错。
    • out || terminalText 优先 delta 累积文本;原代码对 response.completed 的 full 文本是立即 return(权威来源)。改为 terminalText || out,才真正满足「完整流行为不变」。

    补充设计决策:

    实现方式:extractSummaryFromSse 导出做直接单测(同 diagnoseEmptySummary 先例),另在 tests/preflight-empty-summary.test.ts 的既有代理 harness 上加一条集成测试,证明截断流现在真的进入 #727 的诊断/halving/冷却链路。完成后回 PR 链接。

  2. ranxianglei commented on Sep 14, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

    [bot] 🏷

    @plyflai 已验证并实现,PR: #784

    审核结论(按仓库 triage 规范)

    四点全部在 src/preflight.ts:311-346(v0.1.111 / bfb0b68)逐条查证属实:循环结束无条件 return out、只认 data: 行、responses 只处理 output_text.delta/completed、失败终态不置无效。查重无重复 issue/PR。层次判断与你一致:#727 修的是「空摘要」的症状链路,本 issue 指向抽取函数本身——截断摘要会绕过整条诊断链被持久化,比空摘要更危险,属根本原因层。

    在你本地补丁的基础上改了两处,另有几个边界决策:

    1. eventType 不能跨帧 sticky——SSE 语义下帧头属于单个事件块,解析循环在空行处重置。sticky 版本会让上一帧的 event: 泄漏到下一帧没有 type 字段的 payload 上。
    2. 优先级用 terminalText || out 而非 out || terminalText——原实现里 response.completed 携带的完整 response 对象是权威的(见到立即 return),保持该语义:completed 的 full text 覆盖 delta 累积,而不是反过来。
    3. 帧完整性检查(\n\n 收尾,CRLF 容错)对三个协议都生效,不只 responses——漏检的代价是静默持久化半份摘要,误拒的代价只是走 fix: recover preflight from empty SSE summaries + dead-end cooldown (#726) #727 已有的诊断 + 缩小重试 + 冷却链,代价不对称。
    4. 不要求 finish_reason/[DONE]/message_stop(anthropic/openai)——[Bug] QODER_MODEL_TRANSPORT=http 模式下 api2-v2.qoder.sh/model/v1/chat/completions 网关输出损坏的 SSE:无 [DONE]、data: 行为半截 JSON、偶发缺 finish_reason → 工具调用参数 JSON 解析失败 #764 已证明真实网关偶发省略这些,强制要求会把一类网关抖动变成系统性 502。responses 要求 spec 强制的 response.completed,且一旦见到即信任(不叠加帧检查),兼容「最后一个事件后直接断流、不留尾部空行」的网关。
    5. 解析失败的 data: 行计为损坏(badFrame)而非跳过——与全代码库「每条 data: 行都是独立 JSON」的假设一致;裸 {error:{...}} 对象同样置无效(对齐 extractStreamError 语义)。
    6. diagnoseEmptySummary 顺带加了截断命名:半行计数 / 缺帧终止符会显式写进诊断消息,日志能直接看出是截断,而不是「上游没给摘要」。

    验证:新增 tests/preflight-sse-extraction.test.ts(23 个单测,三协议全覆盖:中帧截断、framed 但缺 completed、delta-less done/item-done、delta+done 共存不重复、多段拼接、各失败终态、event 头判型与跨帧泄漏防护、CRLF、completed 之后出现垃圾字节仍接受);tests/preflight-empty-summary.test.ts 增加一个 #764 形状的集成测试(截断流 → 502 preflight_compress_failed 且诊断含截断说明、摘要调用有界、不转发任何请求、死锁标记就位、冷却期零上游开销)。typecheck 干净,全量 1535 pass / 0 fail / 2 skipped,build 通过。E2E 套件在本沙箱无法运行(需可达的本地上游端口,沙箱网络策略禁止),改动限于纯字符串处理层,CI 的手动 e2e workflow 可按需补跑。

    一句话:加固了 preflight SSE 摘要抽取层(截断/失败流不再产出「半份但看起来正常」的摘要,统一落入 #727 已有的诊断重试链路,恢复闭环),已提 PR #784,待 review 后可合并。

  3. added a commit that references this issue on Sep 15, 2026
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