Skip to content

fix: release held done-family events when the next item opens (#1061) - #1062

Merged
ranxianglei merged 1 commit into
masterfrom
2026-09-20_fix-responses-held-done-ordering
Sep 20, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-09-20_fix-responses-held-done-ordering

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Problem (#1061)

pipePluginResponsesWithStrip held response.output_text.done / response.content_part.done / response.output_item.done until the completion event decided retry, but forwarded response.output_item.added immediately. With an upstream that emits more than one reasoning item per turn (gpt-5.x interleaved thinking), the client received added(rs_2) before done(rs_1). opencode v2 hard-errors on that ordering (packages/ai/src/protocols/open-responses.ts:1501: started reasoning before the previous item ended) — reproduced against the published 0.1.129 pipe with a fully legal stream.

Fix

  1. Primary — an opening response.output_item.added now flushes pending tag/arg tails plus all held done-family events before forwarding itself, restoring exact upstream relative order for every item type. At the terminal event only the last item's family can still be held, so the degenerate-turn retry ([BUG] In some cases, with Codex's auto model + DeepSeek, it will suddenly terminate automatically. #732/workbuddy报错Empty stream: upstream gateway sent only placeholder chunks without any model output (chunks=0, bytes=0) #821) still drops exactly what it needs.
  2. Secondary — the retry stream's own reasoning_summary_* frames were raw-forwarded with fresh ids whose items were never announced (their added is dropped by retryFraming), leaking ids the client cannot resolve. They are now suppressed with the other retry framing events.

Both compression modes considered (§6): this pipe serves the plugin lane and proxy-mode Responses SSE that skips injection (#460); both are restored to upstream order. The non-plugin adapter path (src/loop/adapter-responses.ts) emits dones immediately and is untouched.

Pre-flight

  • New regression suite tests/fix-1061-held-done-ordering.test.ts (3 tests): multi-reasoning ordering, healthy-turn order preservation + retry-gate retention, retry reasoning-id leak suppression. Verified RED on master first (all 3 fail with the exact symptom), GREEN after the fix.
  • npm run typecheck — clean
  • npm test — 2085 pass / 0 fail / 2 skipped (e2e gates)
  • npm run build — success

Closes #1061

@github-actions

github-actions Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-20_fix-responses-held-done-ordering (3aee7e8)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-1062

Each push to this PR publishes a new version under the pr-1062 npm tag.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr1062.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 On it — pulling the PR branch (2026-09-20_fix-responses-held-done-ordering @ c122791), reviewing the diff against current master, running typecheck/tests/build, and verifying the regression suite goes RED on master → GREEN on the branch. Full conclusion in a follow-up comment shortly.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review complete — verified, mergeable.

Diff review (head c122791, base = current master 2c5cfd9, not stale): 2 files, +243/−3, every line on-purpose. src/plugin.ts changes are confined to pipePluginResponsesWithStrip; new regression suite tests/fix-1061-held-done-ordering.test.ts. No version/lockfile/whitespace churn. Per §7.4 this is a wire/message-shape change (SSE event ordering), so it stays a human merge — but everything below passes.

Root-cause check (§Triage): the reported symptom is the root cause here. The hold-until-completion design (#732/#821) is still needed — the retry must be able to drop the degenerate turn's done-family wholesale — but holding across an output_item.added boundary violates the client protocol (done(itemN) before added(itemN+1)). Releasing held events at the next item-open is the minimal fix that preserves both invariants: at the terminal only the last item's family can still be held, so the retry gate drops exactly what it needs. I also traced the multi-item degenerate turn: pre-fix, retry takeover silently dropped done(item1) entirely (client never closed the item); post-fix it is released at added(item2) — test 2 pins this third defect too. Secondary fix is consistent: during retry the reasoning items were never announced (their added is dropped by retryFraming), yet their frames hit the raw-forward fast path (src/plugin.ts:2016) with fresh ids — suppressing all response.reasoning_summary_* in-retry is the coherent choice. Guard ordering checked: in-retry added is consumed by retryFraming (line 1903) before the new handler (line 1930), so early release can never fire mid-retry; heldVisibleChars deliberately kept so the empty-turn gate isn't weakened. §6: non-plugin adapter path untouched (diff confirms).

Verification (all performed by me, not taken on trust):

  • RED on master: ran the 3 new tests against origin/master in a throwaway worktree — 3/3 fail with exactly the reported symptoms (done(rs_1) must reach the client before added(rs_2), done(rs_1) … dropped by the retry, no retry reasoning id may leak).
  • GREEN on branch: 3/3 pass.
  • npm run typecheck clean; npm test 2085 pass / 0 fail / 2 skipped (e2e gates) — matches the PR's claim; npm run build success.
  • GitHub CI on head: 8/8 checks green; PR now mergeable_state: clean.

No small fixes needed on my side — nothing found to patch on the branch.

中文摘要:修复了 Responses 插件管道在退化轮重试机制下把 done 事件扣留到 completion、导致多 reasoning 项时 added(itemN+1) 先于 done(itemN) 到达客户端(opencode v2 直接硬报错)的问题——在下一个 output_item.added 到达时提前释放已扣留的 done 家族事件恢复上游原始顺序,同时抑制重试流中未宣告的 reasoning id 泄漏;已在 master 上复现 RED、分支上验证 GREEN(typecheck/test/build/CI 全绿),可以合并。

pipePluginResponsesWithStrip held response.output_text.done /
response.content_part.done / response.output_item.done until the
completion event decided retry, but forwarded
response.output_item.added immediately. With an upstream that emits
more than one reasoning item per turn (gpt-5.x interleaved thinking),
the client saw added(rs_2) before done(rs_1) — opencode v2 hard-errors
on that ordering (InvalidProviderOutput: started reasoning before the
previous item ended).

Fix: an opening output_item.added now flushes pending tag/arg tails
and all held done-family events first, restoring exact upstream
relative order for every item type. At the terminal event only the
last item's family can still be held, so the degenerate-turn retry
(#732/#821) keeps dropping exactly what it needs.

Secondary path in the same pipe: the retry stream's own
reasoning_summary_* frames were raw-forwarded with fresh ids whose
items were never announced (their added is dropped by retryFraming),
leaking ids the client cannot resolve. They are now suppressed with
the other retry framing events.
@ranxianglei
ranxianglei force-pushed the 2026-09-20_fix-responses-held-done-ordering branch from c122791 to 3aee7e8 Compare September 20, 2026 12:55

@ranxianglei ranxianglei left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

独立审查(第二双眼睛)——结论:可合并 ✅

Diff 核对(src/plugin.ts +28/-3 + 回归套件 218 行):

  • 主修:output_item.added 新分支先 flushArgTails()+flushTail("") 再放行全部 heldEvents,恢复上游原始顺序;覆盖所有 item 类型而非仅 reasoning——正确,恢复上游序对任何严格客户端都安全。
  • 顺序链我逐行核过:retryFraming(type) 在新分支之前消费重试流的 added(不会误触发 flush);added 不在扣留集必达新分支;heldItemId/heldOutputIndex 在 retryFraming 丢弃前已捕获。
  • 尾帧顺序正确:pending summary/text 尾属于前一 item 的正文,先于其 done 放行,与上游序一致。
  • heldVisibleChars 不清零——已放行文本仍计入空回合判定,防止误触发重试 ✓。
  • 次要路径:retryFraming 吞掉 response.reasoning_summary_*,堵住重试流未 announce 的 reasoning id 泄漏;未做「补发 done」是正确取舍(释放后 rewrite 的 message delta 会打到已关闭 item 上,更糟)。
  • 重试语义保留验证:到终局时 heldEvents 只剩最后一个 item 的族,整批丢弃语义不变。
  • 与 #1056(刚合入)同函数区域,rebase 无冲突;#1039 的参数透传不变量未被触碰。

验证(本机):rebase 到 master 9caa685 → typecheck 0 错 / 2084 pass / 0 fail / build 成功;E2E codex 四阶段全过(warmup→load→compress→purity, 210s,本地 sglang)。分支已 force-push(c122791 → 3aee7e8)。

动 wire 事件序,按 §7.4 须人工合并。

@ranxianglei
ranxianglei merged commit b6a83dc into master Sep 20, 2026
8 checks passed
@ranxianglei
ranxianglei deleted the 2026-09-20_fix-responses-held-done-ordering branch October 15, 2026 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant