[Bug]: the local context footprint over-estimates provider-reported usage by 4-28% on every turn, which makes compaction fire early

未關閉
#600 2 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
46/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
活躍
技術堆疊
node.js, typescript
領域
ai, cli

研究方向

Start by reading local-context-footprint.ts and tracing createLocalContextFootprintMeasurer, estimateMessage, and ContextUsageAnchorState.resolve; compare the local estimate and provider usage on persisted messages.jsonl sessions with tool results. The issue identifies calibration or provider usage as possible directions, but leaves the preferred behavior open. Done when the compaction decision no longer consistently exceeds provider-reported usage on the same turn.

由索引模型根據 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 / compaction

What happens

For every turn, the local context footprint is measurably larger than the usage
the provider reports for the same request. Comparing the two on the same turn, with
the same objects:

  • provider side: usage.input + usage.cacheRead + usage.cacheWrite for that
    assistant message — the full prompt that was sent;
  • local side: estimate(systemPrompt) + estimate(JSON.stringify(tools)) + Σ estimate(messages before this turn), i.e. the same arithmetic
    createLocalContextFootprintMeasurer performs.

Measured over six real sessions (30 to 447 turns each):

session turns ratio (local / provider), median tool-result tokens in the session
A 447 1.164 246,567
B 258 1.207 190,497
C 207 1.278 139,677
D 183 1.076 109,119
E 30 1.104 19,228
F 395 1.044 200,515

The ratio is stable within a session and does not drift with history length. For the
same sessions, the fixed part (system prompt + tools) accounts for 36,250 local
tokens against 39,744 reported by the provider on a cold first turn — so the fixed
part is under-estimated, and the overshoot comes from the messages.

The dominant contributor is tool results: estimateMessage charges the full text
of every toolResult block, and in these sessions they are the largest single
component (110k-247k tokens each). Assistant toolCall arguments, thinking blocks
and plain text are estimated the same way.

Why it matters

This estimate is what decides when the context is compacted, and the user's view of
the same number comes from a different source — the provider-reported usage that
buildContextUsageMeasurement prefers. The two therefore disagree by a factor that
is invisible in the UI: the context ring shows one number, and the compaction
threshold is evaluated against a larger one.

The report that prompted this measurement described the context ring sitting at
~35% of the window while compaction fired at ~89% of it. Two effects stack:

  1. the local estimate runs 4-28% above the provider's own number for the same turn;
  2. local-context-footprint.ts takes Math.max(localEstimate, anchor), and the
    anchor only applies when ContextUsageAnchorState.resolve finds the exact
    assistant message it recorded. Whenever it does not resolve, the decision falls
    back to the local estimate alone — so the provider's real number never gets a
    chance to correct the local one.

Together those mean the compaction trigger can fire while the context ring still
shows a large amount of free space.

Steps to reproduce

  1. Take a session with tool calls and read its persisted messages.jsonl.
  2. For each assistant message with a usage block, record
    usage.input + usage.cacheRead + usage.cacheWrite.
  3. Walk the messages in order, accumulating the estimator's own per-message count
    (estimateMessage, including the 4-token structural overhead), and compare
    fixed + accumulated against the recorded provider number at each assistant
    message.
  4. The ratio stays above 1.0 for the whole session.

What would help

Either of these would close the gap, and we are happy to help test whichever the
project prefers:

  • a calibration of the per-message structural overhead — the flat 4 tokens per
    message looks low once a turn carries several tool results, and the provider
    appears to charge more for the tool_use / tool_result pairing than we do;
  • or using the provider's own number as the primary source for the compaction
    decision, with the local estimate only as a fallback when it is unavailable.

One caveat we want to state plainly: our measurement compares two numbers that are
close but not identical in construction, so we may be over- or under-stating the gap
by a few percent. What we are confident about is the sign and the order of
magnitude — the local estimate is consistently above the provider's number on the
same turn, on every session we measured, and tool results dominate the difference.

主要語言
TypeScript
星號
2.3k
分支
315
平均合併
3 小時 54 分鐘
30 天內合併 PR
9

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

MiniMax-AI/minimax-code 的其他 Issue

查看 MiniMax-AI/minimax-code 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。