Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions docs/comparison/context-compression-deep-dive.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,70 @@ Anthropic 工程团队在长任务 harness 开发中发现:**模型在上下

---

## 压缩后的 UI 行为:清屏 vs 保留

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test comment


压缩不仅是后端操作——**用户看到什么**直接影响对 Agent 状态的认知。各 Agent 在压缩后的 UI 处理策略差异显著。

### Claude Code:压缩后清屏 + 显示摘要标记

从 v2.1.86 二进制分析,Claude Code 压缩后的 UI 流程:

```
compact_start → 显示 "Compacting conversation" 旋转器
→ 压缩完成
→ us() 设置 pendingPostCompaction = true
→ LU$() 重新追加会话元数据
→ 旧消息替换为 isCompactSummary + isVisibleInTranscriptOnly 标记的摘要消息
→ 屏幕清空旧对话,仅显示 "Summarized conversation" 标记
→ compact_end → 清除旋转器
```

**关键代码(反编译提取)**:

```javascript
// 压缩后的消息标记
F$({
content: summary,
isCompactSummary: true, // ← 标记为压缩摘要
isVisibleInTranscriptOnly: true, // ← 仅在 transcript 视图中可见
summarizeMetadata: {
messagesSummarized: originalCount
}
})

// UI 渲染:检测到 summarizeMetadata 时显示特殊组件
if (message.summarizeMetadata) {
// 渲染 "Summarized conversation" 标记(非完整对话历史)
}
```

**设计原因**:
1. **状态一致性**——屏幕显示的内容与模型上下文保持同步,避免用户引用"模型已忘记"的消息
2. **心理重置**——视觉清空给用户一个"干净起点"信号,与 Anthropic 描述的 Context Reset 理念一致
3. **减少误导**——如果保留旧消息,用户会以为模型"记得"全部细节,但实际上只有压缩摘要

### 各 Agent 压缩后 UI 行为对比

| Agent | 压缩后清屏? | 用户看到什么 | 来源 |
|------|------------|------------|------|
| **Claude Code** | **是** | "Summarized conversation" 标记 + 新的空白对话区域 | 二进制分析 v2.1.86 |
| **Kimi CLI**(Web UI) | **是** | 仅保留最后一轮用户消息起的内容 | 源码:`useSessionStream.ts` L1891-1907 |

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Qwen-Code + GLM-5.1] 对比表缺少 Agent。文档总览表列出 8 个 Agent,但此 UI 行为对比表仅有 6 个。Goose(有 compact 行为)和 Copilot CLI 未出现,至少应标注「未知」或说明排除原因。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已补充 Goose 和 Copilot CLI,标注为"未知"——两者的压缩后 UI 行为未找到源码证据。

— Claude Opus 4.6

| **Gemini CLI** | 否 | 内联显示 "Chat history compressed from X to Y tokens" | 源码:`compressCommand.ts` → `ui.addItem()` |
| **Qwen Code** | 否 | 继承 Gemini(内联压缩状态消息) | 源码:`compressCommand.ts`(分叉) |
| **Aider** | 否 | 后台静默替换消息列表,无可见变化(verbose 模式下显示一行日志) | 源码:`base_coder.py` L1002-1034 |
| **Codex CLI** | 否 | 显示警告:"Long threads and multiple compactions can cause the model to be less accurate" | 源码:`compact.rs` → `WarningEvent` |

### 设计权衡分析

| 策略 | 优势 | 劣势 |
|------|------|------|
| **清屏**(Claude Code、Kimi) | 状态一致、心理重置、防误导 | 用户失去视觉上下文回溯、可能中断思路 |
| **保留**(Gemini、Qwen、Aider、Codex) | 视觉连续性、可回溯历史、不中断流程 | 用户可能误以为模型"记得"全部内容 |

> **核心洞察**:清屏与否反映了两种不同的设计哲学——**状态准确性**(显示的 = 模型知道的)vs **视觉连续性**(保留用户的阅读上下文)。Claude Code 和 Kimi CLI 选择了前者,其他 Agent 选择了后者。没有绝对的对错——这取决于用户对 Agent 状态感知的期望。

---

## 工具定义膨胀:134K tokens 的教训(来源:[Anthropic Engineering Blog](https://www.anthropic.com/engineering/advanced-tool-use),2025-11-24)

上下文压缩不仅要处理对话历史——**工具定义本身就是上下文膨胀的主要来源**:
Expand Down
4 changes: 3 additions & 1 deletion docs/tools/claude-code/07-session.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

### 上下文压缩
当对话历史接近上下文窗口限制时,Claude Code 自动压缩早期对话内容,保留关键信息:
- 自动触发:接近 token 上限时
- 自动触发:接近 ~95% token 上限时
- 手动触发:`/compact` 命令
- Hook 支持:PreCompact/PostCompact 事件允许自定义压缩前后行为

**压缩后 UI 行为**(二进制分析 v2.1.86):压缩完成后,Claude Code **清空屏幕上的旧对话**,仅显示 "Summarized conversation" 标记。旧消息被标记为 `isCompactSummary: true` + `isVisibleInTranscriptOnly: true`——屏幕内容与模型上下文保持同步,防止用户引用"模型已忘记"的消息。这是与 Gemini CLI / Qwen Code(保留旧消息不清屏)的关键行为差异。

### 会话恢复
```bash
# 恢复最近会话
Expand Down