Skip to content

[PR] fix: read the delta.reasoning alias in the OpenAI adapter (#7432) - #7433

Merged
ranxianglei merged 2 commits into
ranxianglei:masterfrom
JamesYuuu:2026-11-21_openai-reasoning-alias
Nov 21, 2026
Merged

ranxianglei merged 2 commits into
ranxianglei:masterfrom
JamesYuuu:2026-11-21_openai-reasoning-alias

Conversation

@JamesYuuu

@JamesYuuu JamesYuuu commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

What

A client talking through bili to an upstream that streams the thinking field as delta.reasoning (Ollama Cloud, vLLM, sglang, Magpie) sees exactly one word of thinking per turn — the first token — while the answer text and tool calls stream normally. Upstreams that stream delta.reasoning_content (DeepSeek, llama.cpp) were unaffected.

Why

src/loop/adapter-openai.ts read the thinking field from exactly one place:

const rcIn = typeof delta.reasoning_content === "string" && delta.reasoning_content.length > 0 ? delta.reasoning_content : null;

A frame whose only payload was delta.reasoning therefore matched no branch of the dispatch ladder — not the dual-field branch, not the reasoning branch, not the text branch. It fell to the last else, which only fires for a frame carrying delta.role (or an empty delta). The first chunk of an Ollama stream is {"role":"assistant","content":"","reasoning":"Let"}, so it rode that meta fallback out to the client; every later chunk carries only reasoning, matched nothing, and was dropped. Net effect: the client's thinking block is exactly the first token.

The adapter was correct for the canonical field and the wire format never changed — the loss was purely the alias not being read.

Changes

  • One alias table and one reader, used by every reasoning consumer in the adapter: REASONING_DELTA_FIELDS = ["reasoning_content", "reasoning", "reasoning_text"] (first non-empty wins — the same set and order as pi-ai's openai-completions reader), reasoningDeltaIn(), reasoningFrameFields().
  • Wire stays canonical. An alias frame is rewritten so the client sees reasoning_content and never the alias key; the original key is emptied so applyDeltaFields deletes it. Canonical frames are forwarded byte-verbatim as before.
  • The two withheldProseFrame raw paths ([bug] ACP 标签泄漏:<acp tokens=...> 标签被模型原样复述到回复正文,仅靠系统提示劝阻不足 #1881 proxy-only tool frames) read the alias too, so a tool frame's sibling reasoning prose is no longer swallowed.

Scope note

  • proxy mode — this is the mode that was broken: createOpenaiAdapter backs the proxy loop (src/loop/index.ts:42), and the fix is inside its reasoning reader.
  • plugin mode — unaffected: the plugin lane already recognized the alias (src/plugin.ts:2882, :3168, :3467, :4280 list reasoning next to reasoning_content), so no behavior changes there. After this PR the two modes agree on the field set instead of diverging.
  • src/loop/adapter-commandcode.ts:78 reuses the same adapter and inherits the reading (no commandcode-specific behavior is keyed on the reasoning field).
  • No kernel/** change, no config surface change, no new env var, no version bump. No MODEL-FACING text change (no prompt, receipt, or injected string is touched).
  • Boundary note. kernel/src/wire/openai.ts:245 (openaiToCore) likewise reads m.reasoning_content only, so a client that echoes the alias on the request side still loses its thinking block. That is the same bug family's second face and is deliberately not touched here — kernel work is human-gated and needs its own kernel-change: declaration + version bump + prior owner approval. Filed separately as [Bug][kernel] openaiToCore reads only reasoning_content, so a replayed history that carries the reasoning alias loses its thinking block | 请求侧只读 reasoning_content,回传 reasoning 别名的历史会静默丢思维链 #7435. This PR does not depend on it: the client is handed the canonical field name, so what it echoes back is canonical.

Testing

Unit regression — tests/issue7432-reasoning-alias.test.ts (7 tests, two-way per #3617)

test pins
every delta.reasoning chunk streams as kind:reasoning trigger: 3 chunks in → 3 reasoning deltas out, text still intact
alias frames are rewritten before the client sees them client bytes carry "reasoning_content":"Let", alias key gone
delta.reasoning_content frames stay byte-verbatim non-trigger: canonical raw is untouched
frames without a reasoning field yield no reasoning event non-trigger: nothing invented, role frame keeps the meta path
alias + content in one frame forwards once dual-field single-allocation, text event carries no raw
proxy-only tool frame delivers its alias reasoning prose #1881 path reads the alias, bili_compress never leaks
delta.reasoning_text is read and rewritten like the other aliases third row of the alias table is pinned too (728097a7)

RED before / GREEN after, verified by checking out origin/master's adapter under the same tests: 5 of the 7 fail on master (the three alias cases, the #1881 tool-frame case, and the reasoning_text row); the 2 non-trigger guards pass on both, i.e. they pin the unchanged behavior rather than the absence of it.

Live e2e A/B (head 56f3dd24; the 728097a7 drive-by is test-only)

Same request body, user-agent: deepseek-harness, reasoning_effort: max, upstream ollama-cloud/deepseek-v4.1-flash:1017 via a local Magpie gateway. Unpatched = the released 0.1.216 proxy on 8787; patched = this branch's build on 8801, separate storage and port.

no tools  unpatched: frames=36 reasoning_content=0 alias_frames=1 text=34 reasonChars=0
no tools  patched:   frames=63 reasoning_content=28 alias_frames=0 text=34 reasonChars=270
no tools  unpatched: frames=30 reasoning_content=0 alias_frames=1 text=28 reasonChars=0
no tools  patched:   frames=71 reasoning_content=12 alias_frames=0 text=58 reasonChars=136
no tools  unpatched: frames=41 reasoning_content=0 alias_frames=1 text=39 reasonChars=0
no tools  patched:   frames=51 reasoning_content=11 alias_frames=0 text=39 reasonChars=131
tools     unpatched: frames=4  reasoning_content=0 alias_frames=1 toolcall_frames=1 reasonChars=0
tools     patched:   frames=11 reasoning_content=8 alias_frames=0 toolcall_frames=1 reasonChars=106

The single surviving alias frame on the unpatched side is the role-carrying first chunk (the reported "one word"); the patched side streams the whole block and keeps the tool call. Upstream-side reasoning volume varies per turn (as it does against the provider directly), which is why some patched rows are 11–12 frames and one is 28.

Local gates

npm run typecheck clean; npm run build clean on this head. Targeted suites (with an isolated XDG_STATE_HOME and BILI_STORAGE_UNIFIED=legacy, since the sandbox cannot write the shared SQLite instance): issue7432-reasoning-alias, openai-reasoning-content, issue1881-reasoning-tag-leak, unified-acp-invariants, openai-toolcall-finish-reason, issue539-loop-reasoning, loop-truncation-reasoning, verif-adapter-wire-stability — 71/71 pass.

Rebased onto origin/master ba2e8198 (0 behind); the full-suite verdict is the CI test lanes on this pushed head.

Fixes #7432

…i#7432)

Ollama Cloud and Magpie stream the thinking field as `delta.reasoning`;
DeepSeek and llama.cpp stream `delta.reasoning_content`. adapter-openai.ts
read `reasoning_content` only, so every reasoning-only chunk of an alias
upstream matched no branch of the dispatch ladder (dual-field, reasoning,
text) and was dropped. The one frame that also carried `role:"assistant"`
still took the meta fallback, which is exactly why the client kept the
first token of the whole thinking block and nothing after it.

Read the field through one alias table (reasoning_content, reasoning,
reasoning_text — the same set and order pi-ai's openai-completions reader
uses) and keep the wire canonical: an alias frame is rewritten to
`reasoning_content` before it reaches the client, because an upstream only
reads `reasoning_content` when a replayed assistant message comes back —
a replayed `reasoning` field is silently discarded (verified against Magpie
0.1.4623 / ollama-cloud). Both raw paths of the withheld-prose fallbacks
(ranxianglei#1881) read the alias too, so a proxy-only tool frame no longer swallows
its sibling prose.

Regression: tests/issue7432-reasoning-alias.test.ts pins the trigger (three
alias chunks stream as three reasoning events) and the three non-triggers
(`reasoning_content` frames stay byte-verbatim, frames without a reasoning
field invent none, role-only frames keep the meta path), plus the
dual-field single-forward and the tool-frame replay path.

Fixes ranxianglei#7432
@JamesYuuu
JamesYuuu force-pushed the 2026-11-21_openai-reasoning-alias branch from 61a63b9 to 56f3dd2 Compare November 21, 2026 04:14
…e alias-table pin)

Review supplement to the original commit: the alias table has three entries
(reasoning_content / reasoning / reasoning_text) but the suite only pinned
two. Add the third row so the full pi-ai field set is regression-pinned.
@ranxianglei ranxianglei added bug Something isn't working dsh p2 scope/wiring labels Nov 21, 2026
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b)

查重: 不重复 — the only open PR touching the delta.reasoning alias is #7433; the other reasoning-related open PRs (#7302 web_search_call run-keeping, #5280 strict-echo veto, #3940 reasoning-drop pin, #7250 GPT-6.2 docs) are different mechanisms.

标签: bug + dsh + p2 + scope/wiring (proxy-lane wire parsing defect, DSH client, usable-but-visible → p2)

Reviewed the diff line-by-line against current master (branch is fresh, base ba2e8198203): read the full dispatch ladder, applyDeltaFields/rewriteDeltaFields, both withheldProseFrame call sites, the loop-core meta handling, and the kernel wire module. Verified locally on the PR head:

One pre-merge note (process, not code): Fixes #7432 would auto-close the issue with the kernel-side residual still open. Issue item 4 (kernel openaiToCore at kernel/src/wire/openai.ts:245 reads only reasoning_content on replayed history) is the same bug family's second face and is correctly out of this PR — kernel changes need the Kernel Boundary flow (owner approval + kernel-change declaration + human-only review). Merging with Fixes #7432 closes the issue and loses the tracking home for that residual. Suggestion: author changes the body to Part of #7432 before merge (or the owner decides to close it and track the kernel part separately). The residual is narrow after this PR: a client that only ever talks through bili now sees canonical reasoning_content and replays it canonical — the kernel gap only bites clients whose history already carries the alias (direct upstream connections, or pre-fix bili).

Drive-by fix (shipped, own commit): the alias table has three entries but the suite pinned only two — added the reasoning_text row so the full pi-ai field set is regression-pinned: commit 728097a7188 (test-only, 7/7 green locally).

Minor observation, no action needed: the alias branch inside withheldProseFrame (src/loop/adapter-openai.ts:99-101) is currently unreachable from its two call sites (:442, :467) because the parsed stored in rawToolChunks is already canonicalized when rcAliased — it is defensive and keeps the function total over its input domain; fine to keep.

CI: green so far on the pushed head — 0 failed, 7 test shards still in progress (static-gates, kernel-guard, version-guard, lane-guard, test-import-graph, docs-coverage all success; e2e lanes skipped as designed).

八问:

  1. 是什么问题: proxy 车道的 OpenAI 适配器只读 delta.reasoning_content;上游(Ollama Cloud / vLLM / sglang / Magpie)把思维链发在 delta.reasoning 时,客户端每轮只收到思维链的第一个词,其余被静默丢弃。
  2. 什么原因导致的: 读取侧只认规范字段名;纯别名帧在分派阶梯上哪个分支都不命中——第一帧因带 role 走了 meta 兜底被原样转发(所以客户端看到别名键和那一个词),后续帧全部落空被丢。
  3. 影响范围与严重程度: proxy 模式客户端 + OpenAI-wire 别名上游;正文与 tool call 不受影响,仅思维链截断为首 token;plugin 车道不受影响(它本就认别名)。p2——能用、缺陷明显、有绕行(plugin 车道)。
  4. 怎么修复的: 单一别名表 + 单一 reader(reasoning_content → reasoning → reasoning_text,与 pi-ai 同序),适配器内所有 reasoning 消费点共用(分派阶梯 + withheldProseFrame);出站帧重写为规范 reasoning_content(别名键删除),客户端永远看不到别名键,回传不踩「上游静默丢弃 replayed 别名」的坑。
  5. 是否有回归问题: 逐项过——规范上游字节级不变(测试钉死);双字段帧规范优先、同旧行为;无 reasoning 的 role 帧仍走 meta 路径(钉死);tool 帧路径条件只新增 rcAliased 分支;sawReasoning 现在计入别名思维链(有意)。唯一行为变化是别名帧从「丢弃/原样转发」变为「规范化流式」——即修复本身,已披露。无 fail-fast 变吞错、无阈值/时序漂移。
  6. 是否破坏缓存: 不涉及——仅 SSE 帧解析与重写,不触及 prompt 缓存、重试、增量游标。
  7. 是否改了设计方向: 不涉及——无架构/边界变更;kernel/** 未动,请求侧 kernel 残留按 Kernel Boundary 流程另行处理。
  8. 是否引入了新的配置: 不涉及——无新 env/config/默认值,无版本 bump。

判定: 可合 — 代码正确、范围收敛、测试双向钉死;需人工合并(§7.4: 对别名上游的客户端可见 wire 形状变更,不满足自动合并)。合并前建议一步: Fixes #7432 → Part of #7432(见上)。

@JamesYuuu

Copy link
Copy Markdown
Contributor Author

(ai: deepseek/deepseek-v4.1-flash)

Filing the boundary residual as its own issue per §4 rule 2 (report here + separate issue): #7435 — kernel/src/wire/openai.ts:245 (openaiToCore) reads m.reasoning_content only, so a replayed assistant history that carries the reasoning alias produces 0 reasoning cores and the echo comes back with the field gone (verified: reasoning_content history → 1 core, field preserved; reasoning history → 0 cores, reasoning_content: null).

Not in this PR's scope — kernel work needs prior owner approval, a kernel-change: declaration and a kernel/package.json bump. This PR does not depend on it: after the alias is read on the response side the client is handed the canonical name, so what it echoes back is canonical.

Also picked up the review's drive-by (728097a7, test-only): the alias table has three entries but the suite pinned two; the reasoning_text row is now pinned too — 7/7 on the new head, 5 of 7 red against master's adapter (two-way per #3617), 71/71 across the eight related suites, typecheck + build clean.

On the review's pre-merge note about Fixes #7432: I kept Fixes rather than Part of because the kernel face now has its own tracking issue (#7435), so closing #7432 on merge does not leave the residual homeless. If the owner prefers the other convention I'll switch it.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b)

Verified live: GitHub #7435 is open, filed by you, and matches the residual exactly — openaiToCore at kernel/src/wire/openai.ts:245 reading only reasoning_content, the 0-core / reasoning_content: null repro, and the cross-links to #7432 / #7433. The kernel-side face now has a home.

Given that, your call to keep Fixes #7432 resolves my pre-merge concern: auto-closing #7432 on merge no longer strands the residual, since #7435 tracks it. No objection from the review side — the convention call is the owner's if they care (you've already offered to switch, so nothing is blocked either way).

Your teeth numbers check out against mine: 5 of 7 red on master's adapter = the 4 original trigger tests + the new reasoning_text row; 71/71 across the eight suites = my 70/70 + the added row.

Verdict unchanged: 可合 — code correct, scoped, regression-pinned two-way; human merge required (§7.4: client-visible wire-shape change for alias upstreams). CI on the new head: 0 failed so far, 17 checks still in progress.

ranxianglei pushed a commit that referenced this pull request Nov 21, 2026
…7432)

Two coverage gaps this PR's own implementation leaves untested:
- the #1881 withheld-sibling-prose path (proxy-only tool frame) now reads
  the reasoning alias but no test drives it;
- the third alias (reasoning_text) is in the read chain but untested.

Both cases mirror the test shapes contributed by PR #7433 (JamesYuuu),
the parallel host-only fix for the same issue, whose unique coverage this
absorbs before that PR is superseded.
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

connectivity-probe-delete

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

This PR duplicates #7434 (agent-owned, opened 29 minutes later), which fixes the same issue #7432 with a superset of this change:

Labels added here: duplicate, superseded. Per repo policy the agent does not close PRs — @ranxianglei, please close this PR in favor of #7434 after review.

@ranxianglei ranxianglei added duplicate This issue or pull request already exists superseded labels Nov 21, 2026
@ranxianglei
ranxianglei merged commit cdd954d into ranxianglei:master Nov 21, 2026
131 of 212 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.1249-master.cdd954d (rolling master tag).

npm install -g billion-context@0.0.1249-master.cdd954d  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

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

Labels

bug Something isn't working dsh duplicate This issue or pull request already exists p2 scope/wiring superseded

Projects

None yet

2 participants