Skip to content

[PR] fix(kernel): reclaim stranded reasoning residue — context growth leak on strict-thinking lanes (#5531) - #5535

Merged
ranxianglei merged 3 commits into
masterfrom
2026-11-02_fix-stranded-reasoning-residue
Nov 2, 2026
Merged

ranxianglei merged 3 commits into
masterfrom
2026-11-02_fix-stranded-reasoning-residue

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

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

kernel-change: the defect lives in the kernel turn-integrity judgment shared by recommend and apply (KDD#12 single projection) — no host-layer interception can restore foldability without duplicating that judgment. Owner approved the kernel change in-thread ("动内核吧").

Closes #5531

What breaks (user-visible)

On strict-thinking lanes (DeepSeek thinking mode), every compression round strands part of each folded turn: when a fold covers a turn's tool call+result but leaves its reasoning+text visible (the #564-permitted reverse split), the integrity screen reads the covered call as "still visible" and permanently withdraws the rest of the turn from the compressible set. The residue accumulates monotonically — in the reporter's session the post-fold floor climbed 33k→154k tokens over 14 measured folds (54 total), 165 reasoning cores resident at 371k billed input, and once pending mass stalled under the T1 nudge threshold the nudge went idle forever ("max compressible 26847 < threshold 50000") despite 73k of growth debt. Context runs toward the window cap; each growth fold reclaims only a sliver — the reported "每次增长压缩只回收一部分".

Behavior change (old → new)

  • OLD: a turn whose call/result sit inside an active block while its reasoning/text remain visible is permanently un-foldable — recommend never advertises those messages and apply refuses direct folds of them ("Range would split … a visible tool-call must keep its reasoning run").
  • NEW: block-covered calls count as invisible (neither visible nor folding) in the kept-call test, so the stranded reasoning+text are advertised again and become foldable; folding removes them together with their response, so no orphaned reasoning ever reaches a strict-echo backend.
  • Scope: only turns ALREADY reverse-split by an existing fold (active blocks covering call/result while reasoning/text stay visible). Fresh whole-turn folds, visible-call protection ([bug] compress 保护区过滤在完整性调整之后挖洞,导致 assistant tool-call 消息缺 reasoning_content(DeepSeek 思考模式 400) #684 doctrine), the merge-path coverage model, and the INV2 result-pair check are unchanged (pinned by negative tests).
  • Cache effect: folds become possible where previously none could happen (each such fold rewrites the prefix once, bounded by folded size); net effect is reclaiming residue instead of ratcheting toward the cap. No cache-invalidation strategy changes.

Why safe

Tests

New kernel/tests/issue5531-stranded-reasoning-residue.test.ts (8 tests): fails 3 positive pins on pre-fix code (bug reproduced), 8/8 post-fix. Full-suite verdict = CI lanes.

Config surface

None.

Dedup

Distinct from #4733/#4383 (stale usage anchor → zero-fold stall — different symptom AND mechanism; fixed, demonstrably working in the reporter log) and from #651/#5470 (compress-call-attached reasoning only).

ci-check added 2 commits November 2, 2026 19:23
…#5531)

A turn whose tool call+result live inside an active fold block while its reasoning+text stayed visible (#564 reverse split) was permanently withdrawn from the compressible set: the global screen judged the covered call as "kept visible", so INV1 stranded [reasoning,text] forever — recommend never advertised them and apply refused direct folds of them. On strict-thinking lanes (DeepSeek et al.) the residue ratcheted monotonically with every growth fold (post-fold floor 33k->154k over 14 measured folds in the #5533 session; 165 reasoning cores resident at 371k billed input) until pending mass stalled below the T1 nudge threshold — nudge idle despite 73k of growth debt, context running toward the window cap.

computeIntegrityWithdrawals gains an optional invisibleIds set (ids neither visible nor folding — covered by an active block). Only the INV1 kept-side test consults it; the folded side and the INV2 pair check are unchanged. Recommend passes coveredMessageIds(state); projectUnit's fresh-fold gate passes preExistingCoverage (the merge path already models coverage via foldedRef union survivingCoverage). Stranded [reasoning,text] becomes foldable again; folding removes reasoning and response together, so no orphaned reasoning reaches a strict-echo backend.

Regression suite kernel/tests/issue5531-stranded-reasoning-residue.test.ts: fails 3 positive pins pre-fix, 8/8 post-fix; negative pins hold the #684 visible-call doctrine.
@ranxianglei ranxianglei changed the title fix(kernel): reclaim stranded reasoning residue — context growth leak on strict-thinking lanes (#5531) [PR] fix(kernel): reclaim stranded reasoning residue — context growth leak on strict-thinking lanes (#5531) Nov 2, 2026
@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-11-02_fix-stranded-reasoning-residue (9b640b2)

Latest build — install this exact version

npm install -g billion-context@0.0.7874-pr.5535.7068cd1

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Recent builds (commit → version)

commit version build
9b640b2 billion-context@0.0.7874-pr.5535.7068cd1 build
9b0b45b billion-context@0.0.7871-pr.5535.7be1c00 build

Download artifact

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

Install instructions are refreshed on each push.

The bump commit updated kernel/package.json + kernel/package-lock.json but
left the root lockfile recording the file:./kernel link at 0.0.177 — the
lock-sync gate regenerates and fails on exactly this line.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

查重结论:不重复。检索了 open+closed issue 与 PR(按失败签名:covered-call 误读 / stranded reasoning / reverse-split 残留),无并行修复在途;PR 正文点名的相邻线程机制不同——#4733/#4383 是 stale usage anchor 导致的零折叠停摆(nudge 计量侧),#651/#5470 是 compress 调用挂载的 reasoning(摘要载体侧),与本 PR 的 turn-integrity 判定缺陷(折叠资格侧)症状与机制均不同,独立核实过。

标签:bug + dsh + scope/kernel + p1 —— 源 issue #5531 的日志来自 dsh 车道 DeepSeek thinking 模式;缺陷在内核共享判定核;上下文只增不减、nudge 永久空转属缓存泄露类,按 owner 修订一律 p1。

审核做了什么(证据)

八问

  1. 是什么问题:strict-thinking 会话(dsh/DeepSeek thinking)里,凡是只折掉了某回合 tool-call+result、留下 reasoning+text 可见的折叠(Compression breaks Responses assistant history ordering, causing repeated invalid_assistant_history 400s #564 允许的反向拆分),留下的那部分会被永久移出可折叠集合——上下文地板单调上涨(报告者会话 14 次实测折叠从 33k 涨到 154k),可折叠质量一旦低于 nudge 阈值就再不压缩,即「每次增长压缩只回收一部分」。
  2. 什么原因导致的:内核 turn-integrity 的 INV1 门把「已被块覆盖的 call」当成「仍然可见」——它只分 folded/visible 两态,没有 covered 态;于是「reasoning 要折、call 还可见」被判为拆回合,整组撤回。recommend 全局屏与 projectUnit fresh-fold 门共用这一判定,残留从此再也进不了可压缩集。
  3. 影响范围与严重程度:所有经历过反向拆分的 strict-thinking 长会话(dsh 车道为主);p1 缓存泄露类——上下文朝窗口上限单边爬升直到会话不可用,nudge 永久空转,用户侧表现为「压缩越来越没效果」。
  4. 怎么修复的:给判定加一个可选的「不可见集合」参数——块覆盖的 id 既不算可见也不算在折,recommend 与 fresh-fold 门各自传入现成的覆盖集;参数缺省 = 逐字节旧判决,merge 路径与 INV2 不动,推荐侧与执行侧继续共用同一投影(KDD#12)。
  5. 是否有回归问题:逐项过过既有行为——[bug] compress 保护区过滤在完整性调整之后挖洞,导致 assistant tool-call 消息缺 reasoning_content(DeepSeek 思考模式 400) #684 可见 call 保护(三条负向 pin 修前修后都绿,projectUnit/recommend/apply 三层拒绝语义不变)、整回合新鲜折叠(探针证实边界展宽本就拉全回合,行为不变)、merge 覆盖模型(未触碰)、INV2 result-pair 检查(未触碰)、缺省参数字节级兼容(文档钉死+legacy pin)。一处措辞勘误:PR 正文 OLD 行为写「apply refuses direct folds of them」,我的修前探针显示直接 apply 该区间其实成功,永久停摆纯粹是 recommend 侧抑制所致——不影响修复正确性,但描述宜收紧(按正文归属规则我不代改)。
  6. 是否破坏缓存:无失效策略变更;新允许的折叠每次重写一次前缀(有界于折叠体量),净效果是把残留收回来而不是继续向上限棘轮。
  7. 是否改了设计方向:没有——仍是 recommend/apply 单一投影架构,只加了一个可选入参。
  8. 是否引入了新的配置:无(Config surface: None,与正文一致)。

结论:代码、测试、版本纪律、CI 协调均过审,lock-sync 自伤已当场修掉(9b640b2),等重跑全绿即可交 owner 合并(kernel 改动按 Kernel Boundary 人审,批准记录已在源 issue 线程)。我不执行合并:#5535

@ranxianglei ranxianglei added bug Something isn't working dsh p1 scope/kernel labels Nov 2, 2026

@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.

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

Findings-only review — NOT an approval. This PR changes kernel/ and carries a kernel-change: declaration, so it sits at the human-only kernel gate (Kernel Boundary); I am not approving it. See the separate 🔶 需要决策 comment for the owner authorization request. Below is my independent pass over the diff against current master (not derived from the issue's analysis).

Verdict: technically sound, no blocking code findings. Held solely on the kernel gate.

What it does (traced in source). Adds an optional 4th param invisibleIds?: ReadonlySet<string> to computeIntegrityWithdrawals (kernel/src/turn-integrity.ts:198). In the INV1 kept-call test (kernel/src/turn-integrity.ts:226-231) a call present in invisibleIds no longer counts as kept-visible:
group.some((id) => !remaining.has(id) && callIds.has(id) && !invisibleIds?.has(id))
Omitting the param is byte-identical to legacy (!undefined?.has(id) ≡ true), so every existing caller keeps its old verdict. An id already covered by an active block is now neither folded nor kept — it reads as hidden, so a #564 reverse split (call+result inside an existing block, reasoning+text left visible) no longer strands its reasoning.

KDD#12 / #3236 single-projection discipline (the load-bearing check). Recommend and apply share one foldability verdict:

  • recommend global screen (kernel/src/recommend.ts:438-443) passes covered = coveredMessageIds(state) (kernel/src/recommend.ts:295);
  • apply fresh-fold gate (kernel/src/unit-projection.ts:415-420, fed by kernel/src/compress.ts:346 → applySingleRange → projectUnit) passes isMerge ? undefined : input.preExistingCoverage, where preExistingCoverage = collectCoverage(state) captured once per applyCompression call (kernel/src/compress.ts:346).

coveredMessageIds (kernel/src/state.ts:57) and collectCoverage (kernel/src/unit-projection.ts:619) both compute the union of effectiveMessageIds over active blocks (activeBlocks === blocks.filter(b => b.active), kernel/src/state.ts:53) — identical sets ⇒ no new advertise→reject loop class. Confirmed.

Merge recheck left untouched — correct, not a gap. kernel/src/unit-projection.ts:471 omits the set deliberately: on a merge, sibling-block coverage is folded into foldedRef = effectiveMessageIds ∪ survivingCoverage (kernel/src/unit-projection.ts:405-409), so a covered id lands in remaining and already reads as "not kept" through the existing mechanism. Fresh folds need the separate invisibleIds precisely because there a covered id is neither in remaining nor kept. Both routes reach the same "covered ≠ kept-visible" outcome.

Negative direction preserved (#684). A genuinely visible (uncovered) call still guards its reasoning run: empty/undefined set ⇒ legacy withdrawal ⇒ projectUnit fatals split, recommend suppresses, applyCompression refuses with the canonical error. Pinned by 3 negative tests.

Reachability edge considered, not a blocker. A covered-call-but-visible-result (orphaned result) is unreachable: pair-boundary widening (applyPairBoundaryAdjustments, kernel/src/unit-projection.ts:627) always widens a range to keep a call with its result, so the kernel never produces a block covering the call alone.

Tests (real teeth). kernel/tests/issue5531-stranded-reasoning-residue.test.ts (8 tests) drive the real core (createCore().processTurn / applyCompression), not mocks: positive pins reproduce the bug at the gate / recommend / projectUnit / apply layers and fail pre-fix; negative pins lock the visible-call doctrine.

Conventions & house rules. kernel-change declared ✓; owner approved in-thread (#5531 「动内核吧」) ✓. Version discipline: acp-kernel 0.0.177→0.0.178 (kernel/package.json + kernel/package-lock.json), root package-lock.json file-link synced to 0.0.178, billion-context's own root package.json version untouched ✓. No config-surface change (declared) ✓; no as any / @ts-ignore; why-comments well-referenced. CI (mirror cache): 0 failed / 0 pending; kernel-guard, version-guard, lock-sync, static-gates, config-surface-guard green.

Behavior change (old → new) — disclosed in PR body, verified. OLD: a turn whose call/result sit in an active block while reasoning/text stay visible is permanently un-foldable (residue ratchets toward the window cap; nudge idles once pending mass < threshold). NEW: such residue is advertised again and folds with its response. Scope limited to turns already reverse-split by an existing fold; fresh whole-turn folds, visible-call protection, merge coverage model, and INV2 are unchanged. Cache: enables previously-impossible folds (each rewrites its prefix once, bounded by folded size) — net reclaim, no invalidation-strategy change. Availability strictly improves.

No small mechanical fixes to make on the branch — the diff is clean, and kernel/ changes are not mine to modify in a review pass.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🔶 需要决策
(ai: vllm-qwen/qwen3.8-27b)

请求 owner 就本 PR 的 kernel/ 变更授权合并 —— 它带 kernel-change: 声明、改动内核,按 Kernel Boundary 属人工门,我不自行批准(技术细节见上方 findings-only review)。

引用: PR #5535 (Closes #5531),head 9b640b2b1。

被门控的 diff(acp-kernel 0.0.177 → 0.0.178):

  • kernel/src/turn-integrity.ts:198,226-231 — computeIntegrityWithdrawals 新增可选第 4 参 invisibleIds;INV1 kept-call 测试里「已被活跃块覆盖的调用」不再计为「仍可见」。
  • kernel/src/recommend.ts:438-443 — 全局筛查传入 covered(=coveredMessageIds)。
  • kernel/src/unit-projection.ts:415-420 — fresh-fold 门槛传入 isMerge ? undefined : preExistingCoverage。
  • kernel/tests/issue5531-stranded-reasoning-residue.test.ts — 8 个回归钉。
  • 版本行:kernel/package.json、kernel/package-lock.json、根 package-lock.json 的 file 链接同步到 0.0.178。

独立审核结论(非抄 issue):技术上成立、无阻断性代码问题;recommend/apply 共用同一覆盖集(KDD#12)已逐点核实、负向(#684)保留已钉、CI 0 红。仅因 kernel/ 人工门而停在此处等你拍板。

授权背景:owner 已在 #5531 线程内说「动内核吧」(选项 A:覆盖感知的资格判定)。本 PR 即该选项的实现,diff 与当时批准的方向一致。请就这份具体 diff 予以确认 / 批准。

结构化八问(本 PR)

  1. 是什么问题:严格思考宿主(DeepSeek)长会话里,被折叠拆散搁浅的「思考+文本」永久失去再折资格,越积越多直到没东西可折、nudge 闲置,上下文失控涨到 37w token。
  2. 什么原因:INV1 keptHasCall 把「不在本次折叠集」误当「折叠后仍可见」——被活跃块盖住的调用其实不可见却被判成「保留」,连带整回合退出可压缩集;recommend 与 apply 双重锁死。(非 v0.1.210 回归,自 10-04 内核导入潜伏。)
  3. 影响范围与严重程度:p1(缓存泄露类)。严格思考回显车道 × 长会话 × 大窗口三条件同踩才显形;显形后单调累积、不可自愈,只能涨向窗口上限。
  4. 怎么修复:给资格判定补一个「不可见集」维度——被活跃块覆盖的调用既不按折叠也不按保留处理,搁浅的思考即可再折且随其响应一起移除,不留孤儿思考给严格回显后端。单可选参数、缺省逐字节兼容旧判决。
  5. 是否有回归:审过既有行为——fresh 整回合折叠、可见调用保护([bug] compress 保护区过滤在完整性调整之后挖洞,导致 assistant tool-call 消息缺 reasoning_content(DeepSeek 思考模式 400) #684)、merge 覆盖率模型、INV2 结果配对,全部不变并有负向钉;recommend/apply 共用同一覆盖集(KDD#12),不引入新的 advertise→reject 环。无回归。
  6. 是否破坏缓存:否。失效策略未变;只是让原本永远折不掉的残渣变得可折,每次这样的折叠重写一次前缀(以折叠量封顶),净效果是回收而非向窗口上限棘轮。
  7. 是否改了设计方向:否。仍是同一套覆盖感知资格判定,仅补上「覆盖≠保留」这一维;架构/边界/数据流不变。
  8. 是否引入新配置:否(PR 已声明 Config surface: None)。

建议:批准合并(人工 merge)。若需我补充某条路径的证据或补测,说明即可——但 kernel/ 的最终授权由你决定,我不替你做。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 审核完成 —— PR #5535(内部 #5537;fix(kernel): reclaim stranded reasoning residue,Closes #5531,head 9b640b2b1)。

结论:技术上成立、无阻断性代码问题;因改动 kernel/ 触发人工门 → 不批准,转 needs-decision 等 owner 授权。

已做:findings-only review(非 approve)+ 🔶 需要决策 评论(含八问)+ needs-decision 标签(内部 #5537)+ #4599 一行完成报告。未开新 issue。owner 已在 #5531「动内核吧」批准该方向,请就这份具体 diff 拍板合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 状态确认:PR #5535 审核已完成 —— findings-only、技术上成立、无阻断性代码问题;因改动 kernel/ 停在 needs-decision 人工门,等你就这份具体 diff 拍板合并(合并按纪律由你手动执行)。若需我补充某条路径的证据或按某方向调整,直接说即可。

@ranxianglei
ranxianglei merged commit 00006b5 into master Nov 2, 2026
129 of 168 checks passed
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.999-master.00006b5 (rolling master tag).

npm install -g billion-context@0.0.999-master.00006b5  # 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.

ranxianglei pushed a commit that referenced this pull request Nov 2, 2026
…#5418)

Squash of PR #5469's net delta (content-identical reapply of 8482401) onto
00006b5 (master incl. merged #5535 turn-integrity kernel fix); master-side
files are disjoint from the branch set, so this is a verbatim content move.
Old head 8482401 preserved for reference.
Louie0806 added a commit to Louie0806/billion-context that referenced this pull request Nov 2, 2026
 turn-integrity + kernel 0.0.178

Block-covered calls become invisible in turn-integrity (ranxianglei#5531); kernel
unit-projection and recommend follow. Four kernel-side files, zero overlap
with this lane's 16.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Context compression leak — rose to 350k | 上下文压缩泄露 涨到 35w

1 participant