Skip to content

V2: compaction usage becomes phantom system overhead and negative context budgets #421

Closed

Description

@drexb-ops

Scope

Dual-runtime fork drexb-ops/opencode-acp@1fe36e0, official OpenCode v2.0.3.

Reproduction

Normalize a completed local compaction record with a short summary/recent and token usage { input: 653137, output: 955, reasoning: 723, cache: { read: 0, write: 0 } }, followed by user text continue. Call cacheSystemPromptTokens with a fresh state.

Actual system estimate: 653136. For a 400000 context limit and 32768 output reserve the subsequent hard-guard budget becomes -285904. A replay of settled real context produced 653133. Live logs also show negative budgets while recent provider usage is far smaller.

Root cause

  • lib/v2/projection/normalize.ts:137-141 represents compaction as an assistant summary.
  • lib/v2/projection/shared.ts:204-238 copies compaction-request token usage onto that normalized assistant.
  • lib/ui/utils.ts:56-100 treats the first assistant input usage minus first user text as system overhead, without excluding compaction summaries or anchoring usage to the current request. It then permanently retains any positive estimate.
  • lib/v2/context.ts receives the actual outgoing event.system but does not use it to supply measured system overhead to the shared transform.
  • lib/messages/transform.ts:218-227 subtracts the estimate from the model limit. Other usage/fallback paths inherit the inflated system count.

Impact / fix direction

Misleading 600K+ system usage, negative budgets, false over-budget diagnostics and potentially excessive fallback pruning/nudges after compaction/reload. A previous compaction model request's input is not the current system prompt.

Use current V2 system parts/tool-schema overhead and a coherent wire estimate; explicitly exclude compaction-request usage from conversational calibration. Invalidate or recalibrate on native compaction/model changes. Retain V1 compatibility.

Acceptance: first request after compaction and after reload, before a post-compaction assistant has usage; verify current-wire accounting, nonnegative usable budget, and no unnecessary tool clearing. Distinguish heuristic estimates from actual provider usage in status output.

Related: #395, #418, #419.

Activity

  1. ranxianglei commented on Sep 17, 2026

    @ranxianglei
    Owner

    继续

  2. ranxianglei commented on Sep 17, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

    收到,继续处理中。当前进展(清库后补做):

    • 已在 2026-09-17_v2-compaction-phantom-system 分支(fork master 1fe36e0)上核实根因链与 OP 描述一致:normalize.ts:137-141 把 compaction 记录映射为 assistant summary → shared.ts:204-238 原样复制 compaction 请求的 usage(input=653137)→ ui/utils.ts:56-100 的 cacheSystemPromptTokens 不排除 summary assistant,把 653136 永久缓存为 system 估计值 → transform.ts:218-227 硬护栏预算变负(-285904)。
    • 正在补齐其余消费点(truncate-tools / inject utils / status)的核对,随后写复现测试并实现修复。完成后会附 PR 链接回复本线程。
  3. ranxianglei commented on Sep 19, 2026

    @ranxianglei
    Owner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions