mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-15 12:55:51 +00:00
* docs: 修复文档巡检发现的 4 处错误 - daemon.md: 反映实际实现状态(supervisor/worker 已实现而非 stub) - bridge-mode.md: API 操作数量从 7 修正为 9 - web-search-tool.md: 文件路径从 src/tools/ 修正为 packages/builtin-tools/src/tools/ - remote-control-self-hosting.md: 补充缺失的 RCS_WS_IDLE_TIMEOUT 和 RCS_WS_KEEPALIVE_INTERVAL 配置项 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 Safety 和 Context 文档中的代码引用和类型错误 - permission-model: 修正规则来源从"五层"到八层,优先级顺序对齐代码 - permission-model: PermissionUpdate 类型改为实际的 addRules/replaceRules 等 - permission-model: 补充 acceptEdits 和 dontAsk 两种权限模式 - permission-model: DENIAL_LIMITS 字段名对齐实际代码 - plan-mode: 工具路径从 src/tools/ 改为 packages/builtin-tools/src/tools/ - compaction: 修正 COMPACTABLE_TOOLS 和 POST_COMPACT_* 的行号 - project-memory: 修正 ENTRYPOINT_NAME 常量的行号 - system-prompt: 修正 SystemPrompt 类型定义文件路径和多个行号引用 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修复 introduction 文档中的错误路径和行号引用 - why-this-whitepaper.mdx: BashTool 路径从 src/tools/ 修正为 packages/builtin-tools/src/tools/ - what-is-claude-code.mdx: 移除不存在的 Azure provider,改为实际的 7 种 provider - architecture-overview.mdx: State 类型行号从 204 修正为 207 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修复 conversation/features 文档中的错误 - streaming.mdx: queryStreamRaw → queryModelWithStreaming 函数名修正 - streaming.mdx: Azure 提供商不存在,替换为实际 7 个提供商 - debug-mode.mdx: --inspect-wait 描述错误,实际使用 BUN_INSPECT 环境变量 - buddy.mdx: 补充缺失的 companionReact.ts、CompanionCard.tsx、index.ts Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修复文档巡检中的源码引用错误 - feature-flags.mdx: 修正 feature() 兜底描述,实际从 bun:bundle 导入而非 cli.tsx:3 内联 - feature-flags.mdx: 修正工具 require 路径为 @claude-code-best/builtin-tools 包路径 - ant-only-world.mdx: 修正 tools.ts 中 require 路径为包路径 - ant-only-world.mdx: 修正 INTERNAL_ONLY_COMMANDS 行号 (267-295) 和数量 (24+) - skills.mdx: 修正 COMMANDS memoize 行号 258 → 299 - mcp-protocol.mdx: 修正 fetchToolsForClient LRU 缓存上限 20 → 100 - streaming.mdx: 修正流式事件引用 - file-operations.mdx: 修正工具路径引用 - search-and-navigation.mdx: 修正搜索工具引用 - shell-execution.mdx: 修正 shell 工具引用 - buddy.mdx: 补充缺失的 frontmatter 字段 - debug-mode.mdx: 修正调试模式描述 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 tools/agent 文档中的文件路径和行号引用 - 修正 TodoWriteTool、AgentTool、ToolSearchTool 等工具路径 src/tools/ → packages/builtin-tools/src/tools/ - 更新 Tool.ts、tools.ts、BashTool.tsx 中过时的行号引用 - 修正 WebSearchTool/WebFetchTool/EnterWorktreeTool/ExitWorktreeTool 路径 - 修正 AgentTool.tsx 中多行行号引用 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature 文档中的文件路径和行号引用 - ultraplan.md: 更新文件行数(525/349/127) - fork-subagent.md: 路径迁移 src/tools/ → packages/builtin-tools/ - mcp-skills.md: 修正 getMcpSkillCommands 行号 547→604,client.ts 行号 117→129 - kairos.md: 修正 getBriefSection/getProactiveSection 行号 - proactive.md: 修正 getProactiveSection 行号 860→864 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正顶层文档中的路径迁移和行号引用 - auto-updater.md: config.ts 行号 1735→1737,标注未接入启动流程的函数 - external-dependencies.md: WebSearchTool/WebFetchTool 路径迁移到 builtin-tools 包,Vertex 行号修正 - lsp-integration.md: LSPTool 路径从 src/tools/ 迁移到 packages/builtin-tools/ - stub-recovery-design-1-4.md: 修正 Windows 绝对路径链接为标准代码引用 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 task 文档中的文件扩展名和路径引用 - task-004: AssistantSessionChooser.ts → .tsx, assistant.ts → .tsx - task-003: cli.tsx 行号 249→272, markdownConfigLoader.ts 行号 29→35 - lan-pipes: SendMessageTool 路径迁移到 packages/builtin-tools/ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 补充 computer-use-tools-reference 缺失的 Windows 工具 添加遗漏的 open_terminal 和 activate_window 两个 Windows 专属工具, 修正工具总数 37→39,Windows 工具数 10→12。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 audit/bash-classifier/token-budget/tree-sitter 文档 - feature-flags-audit: ScheduleCronTool 路径迁移、DAEMON 状态更新为 COMPLETE、assistant 文件标记已补全、UDS 标记已实现 - bash-classifier: BashPermissionRequest 文件路径修正、withRetry 行号移除 - token-budget: attachments.ts 行号范围修正 - tree-sitter-bash: bashPermissions.ts 路径迁移到 packages/builtin-tools Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 langfuse-monitoring AgentTool 路径迁移 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 bridgeApi 行号和 Tool.ts 行号引用 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 Safety/Extensibility 文档中的工具路径迁移和行号引用 - sandbox.mdx: shouldUseSandbox.ts 和 bashPermissions.ts 路径迁移至 packages/builtin-tools - why-safety-matters.mdx: bashPermissions.ts 路径迁移(3 处) - plan-mode.mdx: EnterPlanModeTool/prompt.ts 路径迁移 - auto-mode.mdx: Auto mode 指令行号 3464→3481 - hooks.mdx: AgentTool/runAgent.ts 路径迁移 - skills.mdx: SkillTool.ts 路径迁移 - custom-agents.mdx: Agent built-in 目录和 exploreAgent.ts 路径迁移 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 internals 文档引用计数和路径 - ant-only-world: USER_TYPE 引用计数 465→410+,工具路径迁移到 builtin-tools - growthbook-ab-testing: growthbook.ts 行数 1156→1258 - hidden-features: 语音模式状态更新(audio-napi 已恢复) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正工具文档中的行号引用 - sub-agents: AgentTool.call 入口行号 340→387 - shell-execution: ShellCommand onTimeout 行号 129→144 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature 文档中的状态、路径和计数 - all-features-guide: 修正 feature flag 启用范围(dev only vs dev+build) - tier3-stubs: 大量状态修正(stub→已实现),缩减过时条目 - workflow-scripts: 路径迁移到 builtin-tools,状态更新 - web-browser-tool: 工具状态缺失→已实现,路径迁移 - context-collapse: CtxInspectTool 状态缺失→已实现 - computer-use: 行号引用更新,平台分发描述修正 - computer-use-tools-reference: 工具数 39→38 - voice-mode: voiceModeEnabled 行数 55→54 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 更新 the-loop 查询循环行号引用 query.ts 代码变更后终止原因行号整体偏移约 40 行 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 补充 feature-flags-audit 完整 build 默认 feature 列表 添加 ULTRATHINK/LODESTONE/ACP/DAEMON 等 19 个缺失的 build 默认 feature, 修正 dev-only 特征标注(UDS_INBOX/LAN_PIPES/BG_SESSIONS/TEMPLATES) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature-flags-audit ConfigTool 路径迁移 ConfigTool 路径从 src/tools/ 迁移到 packages/builtin-tools/src/tools/ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature-flags-audit BashTool 路径迁移 BashTool 路径从 src/tools/ 迁移到 packages/builtin-tools/src/tools/ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature-flags-audit SkillTool 路径迁移 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 更新 feature-flags-audit WorkflowTool 状态为已实现 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
266 lines
10 KiB
Plaintext
266 lines
10 KiB
Plaintext
---
|
||
title: "上下文压缩 - Compaction 三层策略与边界机制"
|
||
description: "深度解析 Claude Code 上下文压缩的完整实现:Session Memory 压缩、传统 API 摘要压缩、MicroCompact 局部压缩三层策略,以及 CompactBoundary 消息、工具对保持、PTL 紧急降级等关键机制。"
|
||
keywords: ["上下文压缩", "Compaction", "token 管理", "对话压缩", "上下文窗口", "MicroCompact"]
|
||
---
|
||
|
||
{/* 本章目标:从源码层面剖析压缩的三层策略、边界机制和关键常量 */}
|
||
|
||
## 压缩的触发时机
|
||
|
||
上下文压缩不是单一操作,而是**三层递进**的策略系统,对应不同的触发条件和严重程度:
|
||
|
||
| 层级 | 触发条件 | 实现位置 | 是否需要 API 调用 |
|
||
|------|---------|---------|:---:|
|
||
| **MicroCompact** | 单个工具输出过长 | `microCompact.ts` | 否 |
|
||
| **Session Memory Compact** | 自动压缩触发(需 feature flag) | `sessionMemoryCompact.ts` | 否 |
|
||
| **传统 API 摘要** | 手动 `/compact` 或 SM 不可用时的自动回退 | `compact.ts` | 是 |
|
||
|
||
### 压缩入口的优先级链
|
||
|
||
源码路径:`src/commands/compact/compact.ts`
|
||
|
||
当用户执行 `/compact` 或系统触发自动压缩时,压缩命令按以下优先级尝试:
|
||
|
||
```typescript
|
||
// compact.ts:55-99 — 简化后的优先级链
|
||
if (!customInstructions) {
|
||
const sessionMemoryResult = await trySessionMemoryCompaction(messages, ...)
|
||
if (sessionMemoryResult) return sessionMemoryResult // 优先:SM 压缩
|
||
}
|
||
|
||
if (reactiveCompact?.isReactiveOnlyMode()) {
|
||
return await compactViaReactive(messages, ...) // 次选:Reactive 压缩
|
||
}
|
||
|
||
// 兜底:传统 API 摘要
|
||
const microcompactResult = await microcompactMessages(messages, context)
|
||
const messagesForCompact = microcompactResult.messages
|
||
// → 调用 AI 模型生成摘要
|
||
```
|
||
|
||
注意:SM 压缩不支持自定义指令(`/compact 聚焦在认证模块`),有自定义指令时直接走传统路径。
|
||
|
||
## 第一层:MicroCompact — 局部压缩
|
||
|
||
源码路径:`src/services/compact/microCompact.ts`
|
||
|
||
MicroCompact 不压缩整个对话,而是**清除旧工具输出的内容**。它维护一个白名单:
|
||
|
||
```typescript
|
||
// src/services/compact/microCompact.ts:41-50
|
||
const COMPACTABLE_TOOLS = new Set([
|
||
FILE_READ_TOOL_NAME, // 'Read' - 文件读取
|
||
...SHELL_TOOL_NAMES, // 'Bash' - 命令输出
|
||
GREP_TOOL_NAME, // 'Grep' - 搜索结果
|
||
GLOB_TOOL_NAME, // 'Glob' - 文件列表
|
||
WEB_SEARCH_TOOL_NAME, // 'WebSearch' - 搜索结果
|
||
WEB_FETCH_TOOL_NAME, // 'WebFetch' - 网页内容
|
||
FILE_EDIT_TOOL_NAME, // 'Edit' - 编辑输出
|
||
FILE_WRITE_TOOL_NAME, // 'Write' - 写入输出
|
||
])
|
||
```
|
||
|
||
替换策略:将超过时间窗口的工具输出内容替换为 `[Old tool result content cleared]`。这不是简单的截断——原始内容仍保留在 JSONL transcript 中,只是不再发送给 API。
|
||
|
||
MicroCompact 还有一个**时间衰减配置**(`timeBasedMCConfig.ts`):越旧的工具输出越容易被清除,最近的优先保留。
|
||
|
||
### 图片和文档的特殊处理
|
||
|
||
```typescript
|
||
const IMAGE_MAX_TOKEN_SIZE = 2000
|
||
```
|
||
|
||
图片 block 如果超过 2000 token 估算值,也会被 MicroCompact 清除。PDF document block 同理。
|
||
|
||
## 第二层:Session Memory Compact — 无 API 调用的压缩
|
||
|
||
源码路径:`src/services/compact/sessionMemoryCompact.ts`
|
||
|
||
当 `tengu_session_memory` + `tengu_sm_compact` 两个 feature flag 启用时,系统优先使用 Session Memory 进行压缩——**不需要调用摘要模型**,直接使用已经提取好的 Session Memory 作为对话摘要。
|
||
|
||
### 保留窗口的计算
|
||
|
||
```typescript
|
||
// sessionMemoryCompact.ts:324-397
|
||
export function calculateMessagesToKeepIndex(messages, lastSummarizedIndex) {
|
||
const config = getSessionMemoryCompactConfig()
|
||
// 默认: minTokens=10K, minTextBlockMessages=5, maxTokens=40K
|
||
|
||
let startIndex = lastSummarizedIndex + 1
|
||
// 从 lastSummarizedIndex 向前扩展,直到满足两个下限或命中上限
|
||
for (let i = startIndex - 1; i >= floor; i--) {
|
||
totalTokens += estimateMessageTokens([msg])
|
||
if (hasTextBlocks(msg)) textBlockMessageCount++
|
||
startIndex = i
|
||
if (totalTokens >= config.maxTokens) break
|
||
if (totalTokens >= config.minTokens && textBlockMessageCount >= config.minTextBlockMessages) break
|
||
}
|
||
return adjustIndexToPreserveAPIInvariants(messages, startIndex)
|
||
}
|
||
```
|
||
|
||
这个算法确保压缩后保留的消息窗口满足:
|
||
- 至少 10,000 token(有上下文深度)
|
||
- 至少 5 条包含文本的消息(有对话连续性)
|
||
- 最多 40,000 token(不会太大又触发下一次压缩)
|
||
|
||
### 工具对完整性保护
|
||
|
||
`adjustIndexToPreserveAPIInvariants()` 是压缩中一个**关键的正确性保证**:
|
||
|
||
API 要求每个 `tool_result` 都有对应的 `tool_use`,反之亦然。如果压缩恰好切在一条 `tool_result` 消息处,会导致 API 报错。
|
||
|
||
```typescript
|
||
// sessionMemoryCompact.ts:232-314
|
||
// Step 1: 向前扫描,找到所有被保留消息中 tool_result 引用的 tool_use
|
||
// Step 2: 向前扫描,找到与被保留 assistant 消息共享 message.id 的 thinking block
|
||
// 两种情况都需要将 startIndex 向前移动
|
||
```
|
||
|
||
流式传输会将一个 assistant 消息拆分为多条存储记录(thinking、tool_use 等各有独立 uuid 但共享 `message.id`),这增加了边界情况的复杂度。
|
||
|
||
## 第三层:传统 API 摘要压缩
|
||
|
||
源码路径:`src/services/compact/compact.ts`
|
||
|
||
当 SM 压缩不可用时,系统回退到传统方式:调用 AI 模型生成对话摘要。
|
||
|
||
### 压缩前处理
|
||
|
||
发送给摘要模型之前,消息会经过多层预处理:
|
||
|
||
```typescript
|
||
// compact.ts:147-202
|
||
const stripped = stripImagesFromMessages(messages) // 图片→[image] 文字标记
|
||
const stripped2 = stripReinjectedAttachments(stripped) // 移除会被重新注入的附件
|
||
```
|
||
|
||
图片被替换为 `[image]` 标记,防止摘要 API 调用本身也触发 prompt-too-long 错误。
|
||
|
||
### 压缩后的重新注入
|
||
|
||
压缩后,系统会从摘要中**重新注入关键上下文**:
|
||
|
||
```typescript
|
||
// compact.ts:126-134
|
||
export const POST_COMPACT_TOKEN_BUDGET = 50_000 // 总预算
|
||
export const POST_COMPACT_MAX_FILES_TO_RESTORE = 5 // 最多恢复 5 个文件
|
||
export const POST_COMPACT_MAX_TOKENS_PER_FILE = 5_000 // 每文件 5K token
|
||
export const POST_COMPACT_MAX_TOKENS_PER_SKILL = 5_000 // 每技能 5K token
|
||
export const POST_COMPACT_SKILLS_TOKEN_BUDGET = 25_000 // 技能总预算 25K
|
||
```
|
||
|
||
这 50K token 的重新注入预算用于:
|
||
1. 恢复最近读取的文件内容(最多 5 个文件,每个截断到 5K token)
|
||
2. 恢复已激活的技能指令(每个技能截断到 5K token,总计 25K)
|
||
3. 重新注入 CLAUDE.md 内容
|
||
4. 恢复 MCP 工具发现结果
|
||
|
||
## CompactBoundary:压缩的边界标记
|
||
|
||
源码路径:`src/utils/messages.ts`(`createCompactBoundaryMessage`)
|
||
|
||
每次压缩后,系统在消息流中插入一条 `SystemCompactBoundaryMessage`:
|
||
|
||
```typescript
|
||
type SystemCompactBoundaryMessage = {
|
||
type: 'system'
|
||
message: {
|
||
type: 'compact_boundary'
|
||
compactMetadata: {
|
||
compactType: 'auto' | 'manual' | 'micro'
|
||
preCompactTokenCount: number
|
||
lastUserMessageUuid: string
|
||
preCompactDiscoveredTools?: string[]
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
后续所有操作只处理**最后一条 boundary 之后**的消息:
|
||
|
||
```typescript
|
||
// messages.ts
|
||
export function getMessagesAfterCompactBoundary(messages: Message[]): Message[] {
|
||
const lastBoundary = messages.findLastIndex(m => isCompactBoundaryMessage(m))
|
||
return lastBoundary >= 0 ? messages.slice(lastBoundary + 1) : messages
|
||
}
|
||
```
|
||
|
||
### Preserved Segment 注解
|
||
|
||
boundary 消息上还附加了 `preservedSegment` 注解,记录哪些消息被保留而非压缩:
|
||
|
||
```typescript
|
||
// compact.ts — annotateBoundaryWithPreservedSegment
|
||
boundaryMarker.compactMetadata.preservedSegment = {
|
||
summaryMessageUuid: string
|
||
preservedMessageUuids: string[]
|
||
}
|
||
```
|
||
|
||
这在会话恢复时帮助加载器正确重建消息链,避免重复压缩已保留的消息。
|
||
|
||
### Microcompact Boundary
|
||
|
||
Microcompact 操作使用单独的 boundary 类型,与全量压缩的 `compact_boundary` 不同:
|
||
|
||
```typescript
|
||
// src/utils/messages.ts:4599-4614
|
||
type SystemMicrocompactBoundaryMessage = {
|
||
type: 'system'
|
||
subtype: 'microcompact_boundary'
|
||
content: 'Context microcompacted'
|
||
compactMetadata: {
|
||
trigger: 'auto' // Microcompact 只有自动触发
|
||
preTokens: number // 压缩前 token 数
|
||
tokensSaved: number // 节省的 token 数
|
||
compactedToolIds: string[] // 被压缩的工具 ID 列表
|
||
clearedAttachmentUUIDs: string[] // 被清除的附件 UUID
|
||
}
|
||
}
|
||
```
|
||
|
||
与 `compact_boundary` 的区别:
|
||
- **保留原始消息**:Microcompact 仅清除工具输出内容,不删除消息本身
|
||
- **可追溯性**:`compactedToolIds` 记录了哪些工具结果被清除
|
||
- **轻量级**:不生成摘要,不调用 API
|
||
|
||
## PTL 紧急降级:Prompt Too Long
|
||
|
||
当压缩后仍然超出 token 限制(`PROMPT_TOO_LONG` 错误),系统会进入紧急降级路径:
|
||
|
||
1. **Reactive Compact**:`reactiveCompactOnPromptTooLong()` 尝试更激进的压缩
|
||
2. **截断重试**:如果 reactive 也失败,`truncateHeadForPTLRetry()` 直接截断最早的消息
|
||
3. 放弃并报错
|
||
|
||
Reactive Compact 目前在反编译版本中是 stub(`isReactiveOnlyMode() → false`),表明这是 Anthropic 内部的实验性功能。
|
||
|
||
## 压缩的 Hook 机制
|
||
|
||
压缩前后可以执行自定义 Hook:
|
||
|
||
- **Pre-compact Hook**(`executePreCompactHooks`):在压缩前执行,可以注入"必须保留"的标记
|
||
- **Post-compact Hook**(`executePostCompactHooks`):在压缩后执行,可以验证关键信息是否保留
|
||
- **Session Start Hook**(`processSessionStartHooks('compact')`):SM 压缩使用此 Hook 恢复 CLAUDE.md 等上下文
|
||
|
||
Hook 结果以 `HookResultMessage` 的形式附加到压缩结果中,确保用户的自定义逻辑在压缩过程中被尊重。
|
||
|
||
## Snip Compact(实验性)
|
||
|
||
源码路径:`src/services/compact/snipCompact.ts`(stub)
|
||
|
||
Snip Compact 是另一种实验性压缩策略,在反编译版本中为空壳实现。从 stub 的类型签名推断:
|
||
|
||
```typescript
|
||
snipCompactIfNeeded(messages, options?: { force?: boolean }) → {
|
||
messages: Message[]
|
||
executed: boolean
|
||
tokensFreed: number
|
||
boundaryMessage?: Message
|
||
}
|
||
```
|
||
|
||
它似乎是一种**更细粒度的消息级裁剪**(snip = 剪切),可能是对单条消息的进一步压缩,而非整个对话。`shouldNudgeForSnips()` 和 `SNIP_NUDGE_TEXT` 暗示它可能会提示用户触发。
|