Skip to content

[PR] kernel: emergency-truncate replacement must be strictly shorter than its original (#3935) - #3957

Closed
ranxianglei wants to merge 2 commits into
masterfrom
2026-10-20_truncate-shortening-invariant
Closed

ranxianglei wants to merge 2 commits into
masterfrom
2026-10-20_truncate-shortening-invariant

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

kernel-change: #3935 — emergency-truncation replacements could come out LONGER than their originals (trigger floor ignored the fixed marker overhead). The judgment invariant lives in the kernel; host-side config cannot remove the growth band (lowering keepPrefix/keepSuffix only relocates it).

Closes #3935

Problem

truncateLargeToolOutputs (kernel/src/truncate-tools.ts) gated truncation on original.length > keepPrefixChars + keepSuffixChars only. The replacement always carries a ~64-char marker (\n\n...[truncated for context space] — original ~N tokens]...\n\n), so originals in (4000, ~4063] came back as 4064-char text — longer than the input. Under the default estimator savedTokens went negative (4001 chars → −15), which inflated remaining (the candidate loop never broke early) and fed the downstream savedTokens <= 0 branches: a misleading "none were large enough to save tokens" diagnostic plus spurious terminalStreak increments (#300 stuck detection).

Witness (pre-fix local run): len=4001 → afterLen=4064, deltaChars +63, savedTokens −15 — matches the #3935 solver replay.

Fix

Behavior change (old → new)

Case Old New
Originals 4001~4063 chars (default keeps) replaced by 4064-char text (longer); savedTokens ≤ 0; remaining grew proportionally shrunk to strictly shorter (e.g. 4001 → 4000) or left verbatim when the estimator shows no token saving; savedTokens > 0 whenever anything is truncated
Originals where full retention already fits strictly shorter (≥ 4065 chars under the default estimator) truncated 2000+2000 byte-identical output (pinned by exact-equality test)
truncationSkipped diagnostic / terminalStreak could fire on negative savings while nothing was truncated code unchanged; can no longer be fed by negative savings

No config-surface change. No wire-format change (marker wording untouched). Kernel version 0.0.137 → 0.0.138.

Tests

New kernel/tests/truncate-shorter-than-original.test.ts: full 4001~4070 band sweep (never longer), valve still fires at the floor (4001/4061), CJK case, byte-exact no-drift outside the band, token-guard skip, multi-candidate positive accounting, processTurn integration (no false "unsavable" diagnostic, streak stays 0). Existing truncate/terminal-escape suites pass unchanged. Targeted local runs green (38/38 + typecheck + prettier); full-suite verdict left to CI.

…original (#3935)

truncateLargeToolOutputs could replace an original with LONGER text: the trigger floor was original.length > keepPrefix+keepSuffix (4000 default) while the replacement always carries a ~64-char marker, so 4001~4063-char tool outputs came out at 4064 chars with negative savedTokens. Negative savings also inflated 'remaining' (loop never broke early) and fed the savedTokens<=0 branches downstream (false 'unsavable' diagnostic, spurious terminalStreak increments).

Fix: compute the marker first, shrink prefix/suffix proportionally when the full retention would not fit strictly under the original (keptBudget = len - marker - 1), and skip any candidate whose replacement is not both strictly shorter and strictly fewer tokens under the session estimator. Outside the band the replacement is byte-identical to before; truncatedCount>0 now implies savedTokens>0.

kernel version 0.0.137 -> 0.0.138.
@ranxianglei ranxianglei changed the title kernel: emergency-truncate replacement must be strictly shorter than its original (#3935) [PR] kernel: emergency-truncate replacement must be strictly shorter than its original (#3935) Oct 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-20_truncate-shortening-invariant (d2efd15)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.4715-pr.3957.ae3b908

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

Option B — Download artifact

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

Install instructions are refreshed on each push.

The kernel/src change bumped kernel/package.json to 0.0.138 (required by the
Kernel Boundary version rule) but left package-lock.json recording 0.0.137,
so the lock-sync gate (npm install --package-lock-only + git diff --exit-code)
failed. Regenerate the lockfile so it records the bumped kernel version.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

查重: 重复 —— 与已合并的 PR #3972 100% 重叠(同症状 + 同机制 + 同根因),本 PR 应予关闭,不合并。

标签: bug + duplicate + scope/kernel + p2(与 #3935 定级一致:窄带触发、仅紧急路径、影响有界)。

审核过程(独立重审,未照抄任一侧结论):

结构化八问:

  1. 是什么问题:紧急截断的"缩短"在 4001~4063 字符窄带内反而变长(被替换成 4064 字符文本),savedTokens 为负。
  2. 什么原因导致的:资格判定只查 original.length > keepPrefix+keepSuffix(默认 4000),没把替换恒带的 ~64 字符标记开销算进下限,缺「截完必须更短」这条不变量。
  3. 影响范围与严重程度:仅 ≥95% 利用率紧急路径 + 窄带;每受影响消息多 1~63 字节、token 账目可为负、可喂出误导性诊断与 terminalStreak 虚增(fix: stitched-stream usage double-count after compress re-request (adapter-anthropic >0 guard) #300)。p2:有界、不阻塞会话。
  4. 怎么修复的:两个 PR 实现的是同一条核心不变量——先算标记长度,把 keep 窗口钳/缩到原文在标记下方实际留出的空间,无空间的候选原样跳过。[PR] kernel: emergency-truncate replacement must be strictly shorter than its original (#3935) #3957 按比例收缩保配置比例 + 加 token 维度双守卫;[PR] kernel: Fix two byte-stability defects uncovered by verif — truncation must actually shorten + BLOCKED binding collision | kernel: 修 verif 挖出的两个字节稳定性缺陷 — 截断必须真变短 + BLOCKED 绑定撞车保护 (#3935, #3945) #3972 总量钳制(floor 64)+ 字符维度严格更短拒施保险。
  5. 是否有回归问题:审了 master 现行代码承担的既有行为——触发下限不变;大输出(≥4068 字符)保持满额 2000+2000 字节级不变;recent 保护、tool-result/text 两段优先级、渲染摘要豁免、标记文案(wire 格式)、pipeline 顺序均未动。无回归。
  6. 是否破坏缓存:无新增破坏;修复本身消除一类零/负收益的无效缓存接缝(截断接缝本身归 [review] v0.1.202 release-review leftovers: zcode watchdog recovery latency, responses lane ignores pck ablation memory, URL-image id churn, nudge low-water ratchet | 发版审核遗留小项(不阻塞) #3813)。
  7. 是否改了设计方向:否,只在 kernel 判定层补不变量。
  8. 是否引入新配置:无。

为何是关闭而非合并:

顺带记录两处小差异(按 loop guard 写评论、不单独立项,是否跟进小修由你定,均 p3 级):
① 已合并版只守字符维度,没有 token 维度守卫——仅当会话使用自定义非单调估计器时,理论上仍可能出现「更短但 token 不减」;内置的长度单调估计器不受影响;
② 已合并版收缩窗口按 50/50 平分,不保配置的 prefix:suffix 比例——默认配置(2000/2000)零差异,仅非默认比例的窄带输出会有分配差异。

收工判定: 关闭 PR #3957(duplicate of merged #3972),无需合并。

@ranxianglei ranxianglei added bug Something isn't working duplicate This issue or pull request already exists p2 scope/kernel labels Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

1 participant