Repository navigation
fix: preflight SSE extraction rejects truncated and failure streams (#780) - #784
Conversation
…780) extractSummaryFromSse conflated 'the stream delivered a complete summary' with 'the stream died mid-delivery': a gateway truncation (#764 shape: half-line data, no final event) left a partial accumulator that was persisted as a complete tier-1 summary - silently worse than an empty one, because #727's diagnosis chain only fires on empty results. - a data line that fails to parse is corruption (badFrame), not skipped noise - event: frame headers type frames whose payload lacks a type field, reset at every frame boundary (no cross-frame leakage) - failure terminals (response.incomplete/.failed/.error, generic error, bare error object) invalidate any text accumulated before them - responses requires the spec-mandatory response.completed terminal; its full response object stays authoritative over delta accumulation - delta-less terminals (response.output_text.done / output_item.done) now carry their text instead of extracting an empty string - anthropic/openai bodies must end on a frame boundary (CRLF-tolerant); finish_reason/[DONE]/message_stop are NOT required (#764: real gateways omit these occasionally) - diagnoseEmptySummary names truncation explicitly (incomplete data lines / missing final frame terminator) so logs say what actually happened Rejected streams return '' and flow into the existing diagnose + halving + cooldown chain (#726/#727), closing that recovery loop for truncated streams. Preflight: typecheck clean, 1535 pass / 0 fail / 2 skipped, build ok. New coverage: 23 unit tests (tests/preflight-sse-extraction.test.ts) + 1 full-pipeline integration test through the real proxy HTTP server.
📦 Built Package ArtifactBranch: Option A — Install from npm PR tag (recommended)npm install -g billion-context@pr-784Each push to this PR publishes a new version under the Option B — Download artifact
tar xzf billion-context-pr784.tgz
npm install -g packageThis comment is automatically updated on each push. |
|
review 了吗 |
Reviewed — line-by-line diff review plus full verification at head What I checkedDiff cleanliness. Single commit, 3 files, all on-purpose: Correctness against #780's four points. All four gaps are closed exactly as specified in the issue:
Edge cases I traced deliberately: CRLF bodies; OpenAI chat frames without a top-level Verification run at head:
One correction: why e2e can't signal hereThe description says the sandbox network policy blocks the e2e upstream. That's not what I found — I ran both the zero-token preflight (passes) and the full suite, then a baseline run of the same suite on the PR base Minor non-blocking nit: 中文摘要: 已逐行审查并全量验证(typecheck 干净、1535 测试全过、build 通过、与当前 master 无合并冲突),实现完整覆盖 #780 的四点加固且两处对本地补丁的修正均已落实;本沙箱因本地上游服务未启动导致 e2e 无法出信号(基线 bfb0b68 与 PR 头同样失败,属环境问题非回归),建议合并前后用 CI 手动跑一次 e2e 补信号。可以合并。 |
…anxianglei#2309) The SSE path already treats response.incomplete / response.failed as unusable summary terminals (ranxianglei#780/ranxianglei#784); the JSON path had no terminal check, so an HTTP-200 application/json body declaring status=incomplete with incomplete_details.reason=max_output_tokens sailed through requestSummary whenever its partial text cleared MIN_SUMMARY_CHARS (50) and entered a compression block plus the forwarded history. - extractSummaryText: explicit status=incomplete/failed now rejects the body whatever its text length, across both the flat output_text shortcut and the output[] walk. Omitted status stays accepted (compat gateways). - diagnoseEmptySummary: new responsesTerminalDiagnosis names the terminal precisely with the raw reason/error embedded verbatim, running before extractStreamError so a failed response is not claimed by the weaker bare-error framing. - emptySummaryIsSizeDriven: reason=max_output_tokens classifies as size-driven (halving recovery, ranxianglei#726 semantics); other reasons keep the transient bounded re-draw path. - Regression tests: extractor/diagnosis/classifier units plus proxy AND plugin-mode integration (first answer incomplete JSON, later answers complete): partial text never reaches the wire; persistent-incomplete fails fast carrying the diagnosis and arms the dead-end cooldown. - Updated the ranxianglei#987 pinned assertion whose input (status=incomplete JSON) now routes to the precise terminal diagnosis instead of 'plain-JSON completion with empty content' (that string is false when partial text rides the body).
What
Harden the preflight summary extraction layer (
extractSummaryFromSse,src/preflight.ts) per #780. The naive accumulator conflated "the stream delivered a complete summary" with "the stream died mid-delivery": a gateway truncation (#764 shape — half-linedata:JSON, no final event) left a partial accumulator that was persisted as a complete tier-1 summary. That is silently worse than an empty summary, because #727's diagnosis + halving + cooldown chain only fires on empty results — a truncated summary looks normal and permanently loses content.Changes (
src/preflight.ts)data:line that fails to parse is corruption (badFrame), not noise to skip. Anthropic/openai bodies must end on a frame boundary (\n\n, CRLF-tolerant). Deliberately NOT requiringfinish_reason/[DONE]/message_stop— [Bug]QODER_MODEL_TRANSPORT=http模式下api2-v2.qoder.sh/model/v1/chat/completions网关输出损坏的 SSE:无[DONE]、data:行为半截 JSON、偶发缺finish_reason→ 工具调用参数 JSON 解析失败 #764 proves real gateways omit these occasionally; requiring them would turn one corrupt-gateway class into systemic 502s.event:frame headers type frames whose payload lacks atypefield, matching the convention already used insrc/loop/adapter-anthropic.ts,src/loop/adapter-responses.ts,src/stream-terminal.ts. Two fixes over the local patch from the issue: the header value is reset at every frame boundary (SSE semantics — a sticky value leaks across frames), and precedence on completion isterminalText || out(the completed event's full response object stays authoritative over delta accumulation, as before).response.output_text.done(text intext) andresponse.output_item.done(text insideitem) now carry their text instead of extracting""forever.response.incomplete | response.failed | response.error, genericerrorevents, and bare{error}objects mark the result invalid — text emitted before the failure is not accepted as a summary.response.completed(spec-mandatory terminal). Once seen it is trusted as-is — no framing check stacked on top, so gateways that close right after the final event without a trailing blank line are safe.Rejected streams return
""and flow into the existingdiagnoseEmptySummary+ halving + cooldown chain (#726/#727) — which also got a sharper message: it now names truncation explicitly (incomplete data line(s)/no final frame terminator) instead of the generic "carried N events but no summary text", so logs say what actually happened.No protocol-semantics change for well-formed streams: complete streams extract exactly what they did before (verified by the pre-existing #727 fixtures, including the shape where
completedcarries an emptyoutputand extraction falls back to accumulated deltas).Tests
tests/preflight-sse-extraction.test.ts: 23 unit tests across all three protocols — mid-frame truncation, framed-but-missing-completed, delta-less done/item-done, delta+done coexistence (no duplication), multi-part done concatenation, every failure terminal, event-header typing + cross-frame leak guard, CRLF bodies, garbage-after-completed still accepted, diagnose messages.tests/preflight-empty-summary.test.ts: mock upstream that serves the [Bug]QODER_MODEL_TRANSPORT=http模式下api2-v2.qoder.sh/model/v1/chat/completions网关输出损坏的 SSE:无[DONE]、data:行为半截 JSON、偶发缺finish_reason→ 工具调用参数 JSON 解析失败 #764-shaped truncated summary stream; full-pipeline test asserts 502preflight_compress_failedwith the truncation diagnosis, bounded summary calls, nothing forwarded, dead-end marker armed, cooldown spends zero upstream calls.Pre-flight
npm run typecheck— cleannpm test— 1535 pass / 0 fail / 2 skippednpm run build— oktests/e2e/e2e-codex.test.ts): not run from this sandbox — it needs a reachable local upstream port, which the sandbox network policy forbids. The change is confined to pure string processing insrc/preflight.tsplus its tests; CI's manual-dispatch e2e workflow covers the full client path if desired.Fixes #780. Refs #726, Refs #764.