[Bug]: the local context footprint over-estimates provider-reported usage by 4-28% on every turn, which makes compaction fire early
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 46/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 技術スタック
- node.js, typescript
調査の方向性
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.cacheWritefor 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
createLocalContextFootprintMeasurerperforms.
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:
- the local estimate runs 4-28% above the provider's own number for the same turn;
local-context-footprint.tstakesMath.max(localEstimate, anchor), and the
anchor only applies whenContextUsageAnchorState.resolvefinds 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
- Take a session with tool calls and read its persisted
messages.jsonl. - For each assistant message with a
usageblock, record
usage.input + usage.cacheRead + usage.cacheWrite. - Walk the messages in order, accumulating the estimator's own per-message count
(estimateMessage, including the 4-token structural overhead), and compare
fixed + accumulatedagainst the recorded provider number at each assistant
message. - 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 thetool_use/tool_resultpairing 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分
- マージ済み PR(30日)
- 9
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- 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
-
area:api bug good first issue priority:high
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
Doezer/Questarr#1486 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
area:import difficulty:good-first good first issue help wanted priority:P1 roadmap size:S type:test
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
Sergey-Bar/Penny#245 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
mihazs/lazysh#3377 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
Approve the Cloudflare agent plugin for harness sessions対応中かも @joshtsch が今日担当しました。 オープンenhancement ready-for-agent
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信