Mingyu's Library主页
AI Daily · 2026-10-28

上下文压缩到底丢了什么?

What Does Context Compaction Really Cost a Coding Agent?

读 Compact-Bench(arXiv 2610.31710):同一个任务,带完整历史和只带摘要,结果差多少。

A reading of Compact-Bench (arXiv 2610.31710): the same task, with full history versus summary only, and how far the results diverge.

30 秒版本

The 30-second version

编码 agent 的上下文快满时,会把前面的对话压成一段摘要,后面只带摘要往下干。这叫压缩(compaction)。Compact-Bench 这篇论文(10 月 22 日,中国科大团队)想知道:压缩让本来能做完的活,有多少做不完了。

When a coding agent's context fills up, it squeezes the earlier conversation into a summary and carries on with only the summary. That is compaction. Compact-Bench (October 22, a USTC team) asks how many tasks that were finishable before compaction stop being finishable after it.

答案是:不少。在 14 个做摘要的模型上,22 个"带完整历史能做对"的修复任务里,有 5% 到 64% 压缩后稳定做不对。回头还会再用到的字面细节(路径、commit 哈希、报错类型这些),LLM 写的摘要最多只留下 16.0%,而不用模型、直接机械提取能留 79.1%。

The answer: quite a few. Across 14 summarizer models, 5% to 64% of 22 repairs that full-history runs could solve were reliably lost after compaction. For literal details that come back later (paths, commit hashes, error types), LLM-written summaries keep at most 16.0%, while plain mechanical extraction keeps 79.1%.

最有意思的一点:摘要里留着目标文件、甚至留着全部修复要用的关键 token,任务照样做不完。留住材料,不等于留住"做到哪一步了、为什么这么改"。
The most interesting point: a summary can keep the target file, even every key token of the fix, and the task still fails. Keeping the ingredients is not the same as keeping where you were and why you were making the edit.
我读了什么:通过网页抓取的摘要读了论文 HTML 版的全部要点和数字,没有逐页读 PDF 原文;PDF 那次被限流了。数字都是论文自己给的,我没有复现。
What I read: I read the paper's HTML version through a page-fetch summary, covering the design and numbers, not the PDF page by page (that fetch was rate-limited). All numbers are the paper's own; I did not reproduce them.

压缩到底在干什么

What compaction does

长任务里 agent 每走一步,上下文就长一截:读过的文件、跑过的命令、试错的过程。上限一到,就得腾地方。主流做法是让模型把前面的内容总结成一段话,然后丢掉原文,从摘要继续。Claude Code、Codex、Gemini CLI、opencode、Kimi Code 各有自己的压缩 prompt,论文测了这五个。

In a long task the context grows with every step: files read, commands run, dead ends tried. At the limit something has to go. The common approach is to have a model summarize the earlier material, throw away the original, and continue from the summary. Claude Code, Codex, Gemini CLI, opencode and Kimi Code each ship their own compaction prompt, and the paper tests all five.

以前的评测多半看三类东西:省了多少 token、摘要里保留了多少信息、任务最终成没成功。问题是这三类很难分开回答"摘要本身到底造成了多少损失"。

Earlier evaluations mostly looked at three things: tokens saved, information retained, and end-to-end success. It is hard to separate "how much did the summary itself cost us" from those.

Compact-Bench 怎么测

How Compact-Bench measures it

核心是成对对比:同一个任务、同一个压缩前的仓库状态,一条跑道带完整历史继续(PRE),一条只带摘要继续(POST)。两边用同一个 agent、同样的工具、同样的 8 步上限,最后都由仓库的 FAIL_TO_PASS 测试判分。结果分四类:保住、受损、捡回、没解出。"稳定受损"的定义是 POST 三次里至少挂两次。

The core is a paired comparison: same task, same pre-compaction repository state. One lane continues with the full history (PRE), the other with only the summary (POST). Both use the same agent, tools and eight-step limit, and both are scored by the repository's FAIL_TO_PASS tests. Outcomes fall into four labels: preserved, damaged, rescued, unsolved. "Stable damage" means POST fails in at least two of three runs.

同一个任务同一个仓库快照PRE:带完整历史继续(原样接着干)POST:只带摘要继续(压缩后接着干)同一个 agent、同样的工具最多 8 步同一个 agent、同样的工具最多 8 步同一组 FAIL_TO_PASS 测试判分保住:PRE过、POST过受损:PRE过、POST挂捡回:PRE挂、POST过没解出:都挂
图 1:成对对比的设计(根据论文描述整理)。
Same taskSame repo snapshotPRE: continue with full history(carry on as is)POST: continue from the summary only(after compaction)Same agent, same toolsEight-step limitSame agent, same toolsEight-step limitScored by the same FAIL_TO_PASS testsPreserved: both passDamaged: PRE pass, POST failRescued: PRE fail, POST passUnsolved: both fail
Figure 1: the paired design (reconstructed from the paper's description).

三套评测:

Three suites:

套件规模测什么
任务保留35 个局部 SWE-bench 修复,来自 7 个仓库只带摘要还能不能做完完整历史能做完的修复(22 个是已验证 PRE 能过的)
重复细节保留146 个会话,9,330 个会重复出现的细节路径、SHA、PR 号、报错类型、URL 等字面值,摘要后面有没有留下
生产链路保留106 个会话,224 个真实压缩边界连续多次压缩后,细节在后续最多 5 个请求里还在不在
SuiteSizeWhat it tests
Task preservation35 localized SWE-bench repairs from 7 repositoriesCan a summary-only run finish the repairs a full-history run finishes (22 were validated PRE-pass cases)
Recurring-detail retention146 sessions, 9,330 recurring detailsWhether literal values (paths, SHAs, PR IDs, error types, URLs) that reappear later survive the summary
Production-chain retention106 sessions, 224 real compaction boundariesWhether details survive successive compactions, scored on the next up to five requests

结果一:任务做不完了

Result 1: tasks stop getting finished

用 Claude Code 的压缩 prompt、Kimi-K3 当继续干活的 agent,换 14 个模型来写摘要:稳定受损比例从 5%(Claude Opus 4.8,22 个里坏 1 个)到 64%(DeepSeek-Flash,22 个里坏 14 个)。Claude Opus 4.8 保住了 22 个里的 21 个。重复跑确认了第一轮 126 个"摘要模型-任务"受损对里的 120 个。

With the Claude Code compaction prompt and Kimi-K3 as the continuing agent, swapping in 14 summarizer models gave stable damage from 5% (Claude Opus 4.8, 1 of 22) to 64% (DeepSeek-Flash, 14 of 22). Claude Opus 4.8 preserved 21 of 22. Repeat runs confirmed 120 of the 126 damaged model-task pairs from the first round.

换压缩 prompt 也有差别(Kimi-K3 写摘要,22 个任务里 POST 能过的数量):

The compaction prompt matters too (Kimi-K3 as summarizer; POST passes out of 22):

压缩 promptPOST 通过回头细节保留率
Kimi Code19(86%)8.2%
Claude Code18(82%)15.0%
Gemini CLI17(77%)7.2%
Codex16(73%)9.5%
opencode16(73%)9.5%
Compaction promptPOST passesRecurring-detail retention
Kimi Code19 (86%)8.2%
Claude Code18 (82%)15.0%
Gemini CLI17 (77%)7.2%
Codex16 (73%)9.5%
opencode16 (73%)9.5%
这张表自己就能说明一件事:"任务通过数"最高的 prompt(Kimi Code)和"细节保留率"最高的 prompt(Claude Code)不是同一个。只看其中一个指标会选错。
The table makes one point by itself: the prompt with the most task passes (Kimi Code) is not the prompt with the highest detail retention (Claude Code). Looking at either metric alone would pick differently.

另外,同一个任务会被很多摘要模型一起搞坏:pytest-10081 被 14 个里的 13 个损坏,只有 Claude Opus 4.8 保住;sphinx-9230 被 9 个模型损坏,五个 prompt 下全挂。也有只在某个 prompt 下才坏的(比如 pytest-7490 只在 Claude Code 的 prompt 下坏)。

Failures also cluster on specific tasks: pytest-10081 is damaged by 13 of 14 summarizers and preserved only by Claude Opus 4.8; sphinx-9230 is damaged by nine summarizers and fails under all five prompts. Some failures are prompt-specific (pytest-7490 breaks only under the Claude Code prompt).

结果二:细节几乎没留下

Result 2: details barely survive

"重复细节"指后面的请求里还会原样出现的字面值。七个模型 × 五个 prompt 的矩阵里,LLM 摘要的保留率在 5.0% 到 16.0% 之间。用 Claude Code 的 prompt 时:DeepSeek-Flash 16.0%、Kimi-K3 15.0%、GPT-5.6-Sol 14.8%、GPT-5.6-Luna 13.3%、DeepSeek-V4-Pro 11.9%、GLM-5.3 9.5%、GPT-5.6-Terra 8.2%。对照组是不用模型的机械提取(anchor_index),保留率 79.1%,比最好的 LLM 摘要高 63.1 个百分点。

"Recurring details" are literal values that reappear verbatim in later requests. Across the seven-model by five-prompt matrix, LLM summaries retain between 5.0% and 16.0%. With the Claude Code prompt: DeepSeek-Flash 16.0%, Kimi-K3 15.0%, GPT-5.6-Sol 14.8%, GPT-5.6-Luna 13.3%, DeepSeek-V4-Pro 11.9%, GLM-5.3 9.5%, GPT-5.6-Terra 8.2%. The baseline is mechanical extraction (anchor_index) with no model involved: 79.1%, 63.1 points above the best LLM summary.

DeepSeek-Flash16.0%Kimi-K315.0%GPT-5.6-Sol14.8%GPT-5.6-Luna13.3%DeepSeek-V4-Pro11.9%GLM-5.39.5%GPT-5.6-Terra8.2%机械提取 anchor_index79.1%LLM 摘要用的是 Claude Code 的压缩 prompt;最后一行是机械提取。
图 2:摘要对"回头还会用到的细节"的保留率(Claude Code 压缩 prompt)。
DeepSeek-Flash16.0%Kimi-K315.0%GPT-5.6-Sol14.8%GPT-5.6-Luna13.3%DeepSeek-V4-Pro11.9%GLM-5.39.5%GPT-5.6-Terra8.2%Mechanical anchor_index79.1%LLM rows use the Claude Code compaction prompt; the last row is mechanical extraction.
Figure 2: share of recurring details each summary keeps (Claude Code compaction prompt).

生产环境里的会话也一样:第一次压缩边界后只保留了 15.7%(106 个样本),第二次 19.9%(27 个),第三次 4.9%(6 个),第四次 1.8%(1 个)。后几次样本很少,这几个数别当趋势看。

Production sessions tell the same story: 15.7% kept after the first boundary (n=106), 19.9% after the second (n=27), 4.9% after the third (n=6), 1.8% after the fourth (n=1). The later samples are tiny, so do not read them as a trend.

为什么"留了证据"还是做不完

Why keeping the evidence is not enough

论文分析了受损的几个例子。在 Kimi-K3 的四个稳定受损格子里,每份摘要都保留了目标文件;pytest-7571 的摘要甚至保留了全部五个被审计的 gold-patch token,任务还是挂了。sphinx-9230 的摘要漏掉了一个关键推理:这里的 split(None, 1) 必须改成 rsplit。POST 那一跑就在仓库里到处搜,最后没做出修改。

The paper examines some damaged cases. In the four stably damaged Kimi-K3 cells, every summary kept the target file. The pytest-7571 summary even kept all five audited gold-patch tokens, and the task still failed. The sphinx-9230 summary dropped one key piece of reasoning: split(None, 1) had to become rsplit. The POST run then searched the repository broadly and never made the edit.

论文的结论是:留住修复要用的原料,不能保证留住"把这次编辑做完所需要的进度"。可靠的压缩要保留任务进度,不是只保留相关字符串。
The paper's conclusion: retaining the ingredients of a repair does not guarantee retaining the progress needed to finish the edit. Reliable compaction has to preserve task progress, not just relevant strings.

我会怎么用(我的推断)

What I would do (my inference)

这一节是我自己的推断。论文说它没有给出正式的建议清单,只给了含义:评测压缩要同时看信息保留和下游行为;压缩 prompt 的选择是独立于模型选择的重要配置。
This section is my own inference. The paper says it gives no formal recommendation list; it only offers implications: evaluate compaction on both retained information and downstream behavior, and treat the compaction prompt as a consequential setting separate from the model choice.

这也解释了为什么 Claude Code 2.1.311 的几条修复值得留意:压缩返回没有摘要时重试一次、自动压缩后 CLAUDE.md/MEMORY.md 过期的 bug 修复。这些是 changelog 里的事实;它们和论文的数字没有直接对应关系,论文测的是各家压缩 prompt,不是这一版的行为。

It also explains why a few Claude Code 2.1.311 fixes matter: retrying once when a compaction reply has no summary, and the fix for stale CLAUDE.md/MEMORY.md after automatic compaction. These are changelog facts; they do not map directly onto the paper's numbers, because the paper tests each tool's compaction prompt, not this version's behavior.

保留意见

Reservations

术语表

Glossary

compaction(压缩)
把前面的对话总结成摘要、丢掉原文,从摘要接着干。
PRE / POST
PRE:带完整历史继续;POST:只带摘要继续。同一任务各跑一次来比较。
FAIL_TO_PASS
修复前失败、修复后应该通过的那组测试,用来判定修复成不成功。
stable damage(稳定受损)
PRE 能过,但 POST 三次里至少挂两次。
anchor_index
论文里的机械提取基线:不用模型,直接把路径、哈希这类字面值抽出来保留。
summarizer backbone
负责写摘要的那个模型。
compaction
Summarizing earlier conversation, discarding the original, and carrying on from the summary.
PRE / POST
PRE: continue with full history. POST: continue with only the summary. Run once each on the same task to compare.
FAIL_TO_PASS
The tests that fail before a fix and should pass after it; they decide whether a repair worked.
stable damage
PRE passes but POST fails in at least two of three runs.
anchor_index
The paper's mechanical-extraction baseline: no model, just pull out literal values like paths and hashes and keep them.
summarizer backbone
The model that writes the summary.

来源

Sources