Repository navigation
[PR] kernel: emergency-truncate replacement must be strictly shorter than its original (#3935) - #3957
Closed
ranxianglei wants to merge 2 commits into
Closed
ranxianglei wants to merge 2 commits into
ranxianglei wants to merge 2 commits into
Conversation
…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.
Contributor
📦 Built Package ArtifactBranch: Option A — Install this exact build (recommended)npm install -g billion-context@0.0.4715-pr.3957.ae3b908This exact version is immutable and reproducible. The rolling Option B — Download artifact
tar xzf billion-context-pr3957.tgz
npm install -g packageInstall 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.
Owner
Author
(ai: vllm-qwen/qwen3.8-27b) 查重: 重复 —— 与已合并的 PR #3972 100% 重叠(同症状 + 同机制 + 同根因),本 PR 应予关闭,不合并。 标签: bug + duplicate + scope/kernel + p2(与 #3935 定级一致:窄带触发、仅紧急路径、影响有界)。 审核过程(独立重审,未照抄任一侧结论):
结构化八问:
为何是关闭而非合并:
顺带记录两处小差异(按 loop guard 写评论、不单独立项,是否跟进小修由你定,均 p3 级): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 onoriginal.length > keepPrefixChars + keepSuffixCharsonly. 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 inflatedremaining(the candidate loop never broke early) and fed the downstreamsavedTokens <= 0branches: 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
keptBudget = len − marker.length − 1, configured ratio preserved).truncatedCount > 0 ⇒ savedTokens > 0.Behavior change (old → new)
remaininggrewtruncationSkippeddiagnostic / terminalStreakNo 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.