[Bug]: the oversized-run token fallback applies to the whole text, so one long run inflates the entire message
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 3/5
- 預估耗時
- 半天
- 新手友好度
- 68/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 活躍
- 技術堆疊
- node.js, typescript
研究方向
Start with packages/agent-modules/context-manager/src/token-estimator.ts and inspect estimateTextTokens(), hasOversizedAlphanumericRun(), and the byte-bound fallback. Then check the context-manager tests under packages/agent-modules/context-manager/test/; add coverage for a long run surrounded by ordinary text and verify the surrounding text is counted normally while the run stays safely bounded.
由索引模型根據 Issue 內容生成。
描述
Product or interface: Source build or repository tooling
Version: source commit main (3c21163c), Node.js v26.7.0, pnpm 9.12.0
Platform: macOS
OS version and architecture: macOS 26.6.2 (25G83), arm64
Issue area: Context management / token estimation
What happens
packages/agent-modules/context-manager/src/token-estimator.ts falls back to a UTF-8
byte upper bound when a text contains an oversized alphanumeric run — but the fallback
covers the whole text, not just the oversized run.
const MAX_EXACT_TOKENIZER_CHARS = 4_096;
function hasOversizedAlphanumericRun(text: string): boolean { /* any run > 4096 */ }
function estimateTextTokensUpperBound(text: string): number {
return Buffer.byteLength(text, 'utf8');
}
// inside estimateTextTokens():
if (text.length > MAX_EXACT_TOKENIZER_CHARS && hasOversizedAlphanumericRun(text)) {
return estimateTextTokensUpperBound(text);
}
The class comment describes the intent as "a UTF-8 byte upper bound for oversized
word runs" — a bound for the run. The implementation charges the byte length of the
entire string instead, so one long blob inflates everything around it.
Measured with gpt-tokenizer's o200k_base (the estimator's own encoder) as the
reference:
| sample | local estimate | real BPE | ratio |
|---|---|---|---|
| base64 blob, 60 KB | 60,000 | 7,500 | 8.00 |
| single hash string, 9 KB | 9,000 | 1,125 | 8.00 |
| 20 KB of normal Chinese text + 5 KB base64 | 42,501 | 10,901 | 3.90 |
| 20 KB of normal Chinese text, no blob | 8,401 | 8,401 | 1.00 |
| a real source file | 19,254 | 19,254 | 1.00 |
| real minified JavaScript | 50,806 | 50,806 | 1.00 |
Minified JavaScript does not trigger it, because punctuation breaks the run. A base64
payload, a hash, a long URL or an unbroken identifier does.
The blast radius is the point. From a real session, one text block of 63,998
characters contained a single run of 4,218 characters — 6.6% of the block:
text block: 63,998 chars, longest run 4,218 chars
local estimate (byte bound) = 63,998 real BPE = 24,013 ratio 2.67
→ 39,985 tokens of inflation from one text block
Had the bound applied to the run alone, the inflation would have been roughly 3,000
tokens. Because it applies to the whole block, it is about 13× larger than the
comment describes.
Why it matters
The estimate is not only displayed — it decides when the context is compacted.
local-context-footprint.ts takes Math.max(local estimate, provider usage anchor),
so an inflated local estimate wins over the provider's own number and compression
triggers early. The direction is safe (over-estimating cannot cause a hard 4xx), but
the cost is that a conversation loses history long before the window is actually
full.
On a local session store (2,489 sessions, 16,100 files, 4.49 GB) scanned for
[A-Za-z0-9]{4097,}:
- 53 files (0.33%) contain such a run; after de-duplicating per session, 21
sessions (0.84%); - in those sessions, 4 actually trip the estimator fallback, and the inflation is
1.8% of provider-reported usage in aggregate, up to 45% in the worst
single session (one 64 KB text block holding a 4.2 KB run, in a 106-message
conversation).
That is not large enough to explain the report we are chasing, but it is a real
over-estimate with no test pinning it: upperBound, oversized, MAX_EXACT and
byteLength appear nowhere in packages/agent-modules/context-manager/test/.
Steps to reproduce
import { countTokens as o200k } from 'gpt-tokenizer/model/gpt-4o';
const blob = 'A'.repeat(60_000); // one 60 KB run
const text = '中文上下文内容。'.repeat(2000) + blob; // 20 KB of text + the blob
const localEstimate = Buffer.byteLength(text, 'utf8'); // what the estimator returns
const realTokens = o200k(text, { allowedSpecial: 'all' });
console.log(localEstimate, realTokens, localEstimate / realTokens);
// 108000 17500 6.17
The same text without the blob keeps a ratio of 1.00.
What would help
- Apply the byte upper bound to the oversized run only, and count the rest of the
string with the normal encoder — the estimate then stays close to the real count
while keeping the bound where it was intended. - Or count the oversized run with the encoder too, and keep the byte bound only as a
fallback for the cases where the encoder actually fails.
Either way, a test that pins the behaviour for a string containing one long run would
keep this from drifting again.
- 主要語言
- TypeScript
- 星號
- 2.3k
- 分支
- 315
- 平均合併
- 3 小時 54 分鐘
- 30 天內合併 PR
- 9
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
MiniMax-AI/minimax-code 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 78/100
MiniMax-AI/minimax-code#639 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 76/100
MiniMax-AI/minimax-code#636 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
MiniMax-AI/minimax-code#615 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 76/100
MiniMax-AI/minimax-code#556 · 4 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
MiniMax-AI/minimax-code#507 ·
維護者通常 1 天內回覆
查看 MiniMax-AI/minimax-code 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 75/100
維護者通常 1 天內回覆
-
Make the "%" unit spacing the same in the web and in A10可能已有人在做 @kragent66-glitch 今天認領。 未關閉bug good first issue
難度 2/5 1-3 小時 新手友好度 74/100
maiychrus25/OpenTea-DX-OS#226 · 1 則留言 ·
-
bug
難度 2/5 1-3 小時 新手友好度 66/100
nextcloud-libraries/nextcloud-dialogs#2772 ·
維護者通常 1 天內回覆
-
Editor: leave an uploaded or inserted image's alt text empty, so the preview asks for a description未關閉
難度 2/5 1-3 小時 新手友好度 83/100
維護者通常 1 天內回覆
-
難度 1/5 1-3 小時 新手友好度 65/100
sveltejs/svelte.dev#2417 ·
維護者通常 1 天內回覆