本报告从 Claude Code CLI 的 bundled 代码中提取了 compact 命令的完整提示词原文。这些提示词用于指导 AI 模型对长对话进行压缩和总结,以保持会话连续性并防止超出 token 限制。
- 文件:
cli.2.0.29.js - 提取日期: 2026-01-22
- 主要函数:
glA()(行 271932-272128) - 辅助函数:
MyI()(标签转换),rRQ()(会话恢复)
- 手动触发: 用户执行
/compact或/compact [自定义指令] - 自动触发: Token 使用率超过 92% 时(
autoCompactEnabled = true) - 使用模型: 小型快速模型 (
ANTHROPIC_SMALL_FAST_MODEL/ Haiku)
位置: cli.2.0.29.js, 行 271933-272028
函数: glA(A) - 当参数为空或空字符串时返回
Your task is to create a detailed summary of the conversation so far, paying close attention to the user's explicit requests and your previous actions.
This summary should be thorough in capturing technical details, code patterns, and architectural decisions that would be essential for continuing development work without losing context.
Before providing your final summary, wrap your analysis in <analysis> tags to organize your thoughts and ensure you've covered all necessary points. In your analysis process:
1. Chronologically analyze each message and section of the conversation. For each section thoroughly identify:
- The user's explicit requests and intents
- Your approach to addressing the user's requests
- Key decisions, technical concepts and code patterns
- Specific details like:
- file names
- full code snippets
- function signatures
- file edits
- Errors that you ran into and how you fixed them
- Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
2. Double-check for technical accuracy and completeness, addressing each required element thoroughly.
Your summary should include the following sections:
1. Primary Request and Intent: Capture all of the user's explicit requests and intents in detail
2. Key Technical Concepts: List all important technical concepts, technologies, and frameworks discussed.
3. Files and Code Sections: Enumerate specific files and code sections examined, modified, or created. Pay special attention to the most recent messages and include full code snippets where applicable and include a summary of why this file read or edit is important.
4. Errors and fixes: List all errors that you ran into, and how you fixed them. Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
5. Problem Solving: Document problems solved and any ongoing troubleshooting efforts.
6. All user messages: List ALL user messages that are not tool results. These are critical for understanding the users' feedback and changing intent.
6. Pending Tasks: Outline any pending tasks that you have explicitly been asked to work on.
7. Current Work: Describe in detail precisely what was being worked on immediately before this summary request, paying special attention to the most recent messages from both user and assistant. Include file names and code snippets where applicable.
8. Optional Next Step: List the next step that you will take that is related to the most recent work you were doing. IMPORTANT: ensure that this step is DIRECTLY in line with the user's most recent explicit requests, and the task you were working on immediately before this summary request. If your last task was concluded, then only list next steps if they are explicitly in line with the users request. Do not start on tangential requests or really old requests that were already completed without confirming with the user first.
If there is a next step, include direct quotes from the most recent conversation showing exactly what task you were working on and where you left off. This should be verbatim to ensure there's no drift in task interpretation.
Here's an example of how your output should be structured:
<example>
<analysis>
[Your thought process, ensuring all points are covered thoroughly and accurately]
</analysis>
<summary>
1. Primary Request and Intent:
[Detailed description]
2. Key Technical Concepts:
- [Concept 1]
- [Concept 2]
- [...]
3. Files and Code Sections:
- [File Name 1]
- [Summary of why this file is important]
- [Summary of the changes made to this file, if any]
- [Important Code Snippet]
- [File Name 2]
- [Important Code Snippet]
- [...]
4. Errors and fixes:
- [Detailed description of error 1]:
- [How you fixed the error]
- [User feedback on the error if any]
- [...]
5. Problem Solving:
[Description of solved problems and ongoing troubleshooting]
6. All user messages:
- [Detailed non tool use user message]
- [...]
7. Pending Tasks:
- [Task 1]
- [Task 2]
- [...]
8. Current Work:
[Precise description of current work]
9. Optional Next Step:
[Optional Next step to take]
</summary>
</example>
Please provide your summary based on the conversation so far, following this structure and ensuring precision and thoroughness in your response.
There may be additional summarization instructions provided in the included context. If so, remember to follow these instructions when creating the above summary. Examples of instructions include:
<example>
## Compact Instructions
When summarizing the conversation focus on typescript code changes and also remember the mistakes you made and how you fixed them.
</example>
<example>
# Summary instructions
When you are using compact - please focus on test output and code changes. Include file reads verbatim.
</example>
该提示词要求 AI 生成包含 9 个必需部分的总结:
| 序号 | 部分名称 | 说明 |
|---|---|---|
| 1 | Primary Request and Intent | 详细捕捉用户的所有明确请求和意图 |
| 2 | Key Technical Concepts | 列出讨论的重要技术概念、技术栈和框架 |
| 3 | Files and Code Sections | 列举检查、修改或创建的文件和代码段,包含完整代码片段 |
| 4 | Errors and fixes | 列出遇到的所有错误及修复方法,特别关注用户反馈 |
| 5 | Problem Solving | 记录已解决的问题和正在进行的故障排除 |
| 6 | All user messages | 列出所有非工具结果的用户消息 |
| 7 | Pending Tasks | 列出明确要求处理的待办任务 |
| 8 | Current Work | 详细描述总结请求前正在处理的工作 |
| 9 | Optional Next Step | 列出下一步工作(需与用户最近的明确请求一致) |
提示词要求使用两个 XML 标签包装输出:
-
<analysis>标签- 用途:组织思考过程,确保覆盖所有要点
- 内容:按时间顺序分析每条消息和对话部分
- 要求:验证技术准确性和完整性
-
<summary>标签- 用途:包含最终的结构化总结
- 内容:9 个必需部分的详细内容
- 格式:严格按照示例结构输出
提示词中使用的特殊占位符和变量:
[Detailed description]- 详细描述占位符[Concept 1], [Concept 2]- 列表项占位符[File Name X]- 文件名占位符[Important Code Snippet]- 代码片段占位符[Task X]- 任务占位符
位置: cli.2.0.29.js, 行 272029-272127
函数: glA(A) - 当参数 A 非空时返回
Your task is to create a detailed summary of the conversation so far, paying close attention to the user's explicit requests and your previous actions.
This summary should be thorough in capturing technical details, code patterns, and architectural decisions that would be essential for continuing development work without losing context.
Before providing your final summary, wrap your analysis in <analysis> tags to organize your thoughts and ensure you've covered all necessary points. In your analysis process:
1. Chronologically analyze each message and section of the conversation. For each section thoroughly identify:
- The user's explicit requests and intents
- Your approach to addressing the user's requests
- Key decisions, technical concepts and code patterns
- Specific details like:
- file names
- full code snippets
- function signatures
- file edits
- Errors that you ran into and how you fixed them
- Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
2. Double-check for technical accuracy and completeness, addressing each required element thoroughly.
Your summary should include the following sections:
1. Primary Request and Intent: Capture all of the user's explicit requests and intents in detail
2. Key Technical Concepts: List all important technical concepts, technologies, and frameworks discussed.
3. Files and Code Sections: Enumerate specific files and code sections examined, modified, or created. Pay special attention to the most recent messages and include full code snippets where applicable and include a summary of why this file read or edit is important.
4. Errors and fixes: List all errors that you ran into, and how you fixed them. Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
5. Problem Solving: Document problems solved and any ongoing troubleshooting efforts.
6. All user messages: List ALL user messages that are not tool results. These are critical for understanding the users' feedback and changing intent.
6. Pending Tasks: Outline any pending tasks that you have explicitly been asked to work on.
7. Current Work: Describe in detail precisely what was being worked on immediately before this summary request, paying special attention to the most recent messages from both user and assistant. Include file names and code snippets where applicable.
8. Optional Next Step: List the next step that you will take that is related to the most recent work you were doing. IMPORTANT: ensure that this step is DIRECTLY in line with the user's most recent explicit requests, and the task you were working on immediately before this summary request. If your last task was concluded, then only list next steps if they are explicitly in line with the users request. Do not start on tangential requests or really old requests that were already completed without confirming with the user first.
If there is a next step, include direct quotes from the most recent conversation showing exactly what task you were working on and where you left off. This should be verbatim to ensure there's no drift in task interpretation.
Here's an example of how your output should be structured:
<example>
<analysis>
[Your thought process, ensuring all points are covered thoroughly and accurately]
</analysis>
<summary>
1. Primary Request and Intent:
[Detailed description]
2. Key Technical Concepts:
- [Concept 1]
- [Concept 2]
- [...]
3. Files and Code Sections:
- [File Name 1]
- [Summary of why this file is important]
- [Summary of the changes made to this file, if any]
- [Important Code Snippet]
- [File Name 2]
- [Important Code Snippet]
- [...]
4. Errors and fixes:
- [Detailed description of error 1]:
- [How you fixed the error]
- [User feedback on the error if any]
- [...]
5. Problem Solving:
[Description of solved problems and ongoing troubleshooting]
6. All user messages:
- [Detailed non tool use user message]
- [...]
7. Pending Tasks:
- [Task 1]
- [Task 2]
- [...]
8. Current Work:
[Precise description of current work]
9. Optional Next Step:
[Optional Next step to take]
</summary>
</example>
Please provide your summary based on the conversation so far, following this structure and ensuring precision and thoroughness in your response.
There may be additional summarization instructions provided in the included context. If so, remember to follow these instructions when creating the above summary. Examples of instructions include:
<example>
## Compact Instructions
When summarizing the conversation focus on typescript code changes and also remember the mistakes you made and how you fixed them.
</example>
<example>
# Summary instructions
When you are using compact - please focus on test output and code changes. Include file reads verbatim.
</example>
Additional Instructions:
${A}
两个版本的唯一区别在于结尾部分:
| 版本 | 结尾内容 |
|---|---|
| 默认版本 | 以示例结束(行 272028 结束) |
| 自定义版本 | 添加 Additional Instructions:\n${A} 段落(行 272126-272127) |
自定义指令通过模板变量 ${A} 注入,用户可以在执行 /compact [instructions] 时传入特定的总结要求。
提示词中提供的两个示例:
示例 1 - 关注 TypeScript 代码变更
## Compact Instructions
When summarizing the conversation focus on typescript code changes and also remember the mistakes you made and how you fixed them.示例 2 - 关注测试输出和代码变更
# Summary instructions
When you are using compact - please focus on test output and code changes. Include file reads verbatim.这些示例展示了用户可以如何定制总结的重点,例如:
- 特定编程语言的变更
- 错误和修复过程
- 测试结果
- 逐字记录文件读取内容
位置: cli.2.0.29.js, 行 272130-272147 用途: 将 AI 输出中的 XML 标签转换为 Markdown 格式
function MyI(A) {
let B = A,
Q = B.match(/<analysis>([\s\S]*?)<\/analysis>/);
if (Q) {
let G = Q[1] || "";
B = B.replace(/<analysis>[\s\S]*?<\/analysis>/, `Analysis:
${G.trim()}`)
}
let I = B.match(/<summary>([\s\S]*?)<\/summary>/);
if (I) {
let G = I[1] || "";
B = B.replace(/<summary>[\s\S]*?<\/summary>/, `Summary:
${G.trim()}`)
}
return B = B.replace(/\n\n+/g, `
`), B.trim()
}| 输入格式 | 输出格式 |
|---|---|
<analysis>...</analysis> |
Analysis:\n... |
<summary>...</summary> |
Summary:\n... |
| 多个连续换行 | 统一为两个换行 |
| 前后空白 | 去除 .trim() |
这个函数确保 AI 的总结输出以可读的 Markdown 格式展示给用户。
位置: cli.2.0.29.js, 行 272149-272155 用途: 为恢复会话生成上下文提示词
function rRQ(A, B) {
let I = `This session is being continued from a previous conversation that ran out of context. The conversation is summarized below:
${MyI(A)}.`;
if (B) return `${I}
Please continue the conversation from where we left it off without asking the user any further questions. Continue with the last task that you were asked to work on.`;
return I
}This session is being continued from a previous conversation that ran out of context. The conversation is summarized below:
[总结内容]
This session is being continued from a previous conversation that ran out of context. The conversation is summarized below:
[总结内容]
Please continue the conversation from where we left it off without asking the user any further questions. Continue with the last task that you were asked to work on.
这个函数在以下场景下被调用:
- 用户使用
/resume命令恢复历史会话 - 会话因 token 限制自动压缩后继续
- 从检查点(checkpoint)恢复工作
位置: cli.2.0.29.js, 行 192759 使用场景: 分析消息是否表示新对话主题,并提取 2-3 词标题
Analyze if this message indicates a new conversation topic. If it does, extract a 2-3 word title that captures the new topic. Format your response as a JSON object with two fields: 'isNewTopic' (boolean) and 'title' (string, or null if isNewTopic is false). Only include these fields, no other text. ONLY generate the JSON object, no other text (eg. no markdown).
JSON Schema:
{
"isNewTopic": boolean,
"title": string | null
}示例输出 1 - 新主题:
{
"isNewTopic": true,
"title": "React Hooks"
}示例输出 2 - 继续当前主题:
{
"isNewTopic": false,
"title": null
}- 标题必须是 2-3 个词
- 仅输出 JSON 对象
- 不得包含 Markdown 格式
- 不得包含任何额外文本
位置: cli.2.0.29.js, 行 42575
tI1 = ["PreToolUse", "PostToolUse", "Notification", "UserPromptSubmit", "SessionStart", "SessionEnd", "Stop", "SubagentStop", "PreCompact"]PreCompact 是 9 种系统钩子之一,在执行 compact 操作前触发。
位置: cli.2.0.29.js, 行 272582
B.setSpinnerMessage?.("Running PreCompact hooks...");在执行 compact 前,系统会:
- 设置 spinner 消息为 "Running PreCompact hooks..."
- 执行所有注册的 PreCompact 钩子
- 收集钩子输出并可选地注入到总结指令中
位置: cli.2.0.29.js, 行 394739
if (J.output.trim()) Y.push(`PreCompact [${J.command}] completed successfully: ${J.output.trim()}`);钩子成功执行后,其输出会被添加到总结上下文中,格式为:
PreCompact [命令名] completed successfully: [输出内容]
用户可以在 ~/.claude/settings.json 中配置 PreCompact 钩子:
{
"hooks": {
"PreCompact": [
{
"command": "git status",
"description": "显示当前 git 状态"
},
{
"command": "echo '请保留所有文件路径'",
"description": "注入自定义总结指令"
}
]
}
}基于 compact-mode-implementation.md 和 compact-vs-microcompact-detailed-comparison.md 的分析:
| 常量名 | 值 | 说明 |
|---|---|---|
jRB |
20,000 | 总结的最大 token 数 |
oZ8 |
50,000 | 文件恢复的最大 token 数 |
tZ8 |
10,000 | 单个文件的最大 token 数 |
| 常量名 | 值 | 说明 |
|---|---|---|
rZ8 |
5 | 自动恢复的文件数量 |
系统在 compact 后会自动恢复最近访问的 5 个文件的内容到新的会话上下文中。
| 常量名 | 值 | 说明 |
|---|---|---|
ZG8 |
0.92 | 自动压缩阈值(92%) |
GG8 |
0.8 | 警告阈值(80%) |
FG8 |
0.8 | 错误阈值(80%) |
当 token 使用率达到这些阈值时,系统会触发相应的行为:
- 80%: 显示警告提示
- 92%: 自动触发 compact(如果启用)
位置: 用户设置中的配置项
{
id: "autoCompactEnabled",
label: "Auto-compact",
value: true, // 默认启用
type: "boolean"
}用户可以通过 /config 命令或修改 ~/.claude/settings.json 来切换此设置。
基于 compact-mode-implementation.md 的分析:
用户触发 /compact
↓
glA(customInstructions) - 生成提示词
↓
kv1(messages, context, shouldContinue, customInstructions, isAuto)
↓
├─ 计算 token 使用情况 QU(messages)
├─ 执行 PreCompact hooks _x1()
├─ 调用 AI 生成总结 (使用 glA() 提示词)
├─ 提取总结内容 Uv1(response)
├─ 恢复关键文件 BG8(files, context, 5)
└─ 执行 SessionStart hooks BU("compact")
↓
返回压缩后的消息历史
| 阶段 | 函数 | 提示词来源 | 模型 |
|---|---|---|---|
| 生成总结 | glA(A) |
默认 + 自定义指令 | Haiku (小模型) |
| 标签转换 | MyI(response) |
- | - |
| 会话恢复 | rRQ(summary, autoContinue) |
总结 + 继续指令 | - |
| 标题生成 | 行 192759 | 专用提示词 | Haiku (小模型) |
| 组件 | 函数/行号 | 文件 |
|---|---|---|
| 默认 compact 提示词 | glA() 行 271933-272028 |
cli.2.0.29.js |
| 自定义指令 compact 提示词 | glA(A) 行 272029-272127 |
cli.2.0.29.js |
| 标签转换函数 | MyI() 行 272130-272147 |
cli.2.0.29.js |
| 会话恢复提示词 | rRQ() 行 272149-272155 |
cli.2.0.29.js |
| 对话标题生成提示词 | 行 192759 | cli.2.0.29.js |
| 钩子类型定义 | tI1 行 42575 |
cli.2.0.29.js |
| PreCompact 钩子执行 | 行 272582 | cli.2.0.29.js |
| 钩子输出处理 | 行 394739 | cli.2.0.29.js |
基于 compact-mode-implementation.md 的版本对比:
| 功能 | v1.0.70 | v2.0.29 |
|---|---|---|
| 生成提示词 | - | glA() |
| 主 compact 流程 | kv1() |
kv1() |
| Token 计算 | QU() |
QU() |
| 自动开关检查 | EG1() |
类似函数 |
| 使用状态计算 | lm() |
类似函数 |
| 自动触发检测 | IG8() |
类似函数 |
| 自动执行包装 | bRB() |
类似函数 |
-
结构化输出
- 使用 XML 标签(
<analysis>和<summary>)强制结构 - 9 个必需部分确保完整性
- 示例驱动的格式化
- 使用 XML 标签(
-
上下文保留
- 强调保留文件名和代码片段
- 要求逐字引用关键代码
- 记录错误和修复过程
-
用户意图优先
- 第一部分就是"Primary Request and Intent"
- 特别强调用户反馈("especially if the user told you to do something differently")
- 列出所有用户消息
-
防止任务漂移
- "Optional Next Step" 必须与最近工作一致
- 要求直接引用对话内容
- 警告不要开始无关任务
基于 compact-vs-microcompact-detailed-comparison.md:
| 维度 | Compact (glA 提示词) | MicroCompact |
|---|---|---|
| 提示词类型 | 完整的总结提示词 | 无提示词(规则过滤) |
| AI 参与 | 需要模型生成总结 | 完全本地逻辑 |
| 成本 | ~20,000 tokens | 0 tokens |
| 时间 | 2-5 秒 | <100ms |
| 质量 | 高质量结构化总结 | 简单消息过滤 |
| 可定制性 | 支持自定义指令 | 仅配置参数 |
适用场景:
- 长时间对话(2-3 小时)
- 需要保留详细上下文
- 重要的技术决策讨论
建议:
- 让系统自动触发(保持
autoCompactEnabled = true) - 定期手动执行以确保质量
- 检查生成的总结是否准确
适用场景:
- 特定项目类型(如 TypeScript、React 等)
- 需要特别关注某些方面(测试、错误、性能)
- 团队有统一的代码审查标准
建议示例:
# 前端项目
/compact 关注 React 组件变更、状态管理和样式修改
# 后端 API
/compact 重点记录 API endpoint 变更、数据库迁移和错误处理
# 调试会话
/compact 详细记录所有错误、堆栈跟踪和修复步骤
# 重构项目
/compact 保留所有文件移动、重命名和架构决策推荐钩子示例:
{
"hooks": {
"PreCompact": [
{
"command": "git status --short",
"description": "记录当前 git 状态"
},
{
"command": "git log --oneline -5",
"description": "显示最近 5 次提交"
},
{
"command": "npm test -- --listTests",
"description": "列出所有测试文件"
}
]
}
}这些钩子会在 compact 前执行,其输出会被注入到总结上下文中,帮助 AI 更好地理解项目状态。
| 文档 | 内容 | 关联度 |
|---|---|---|
compact-mode-implementation.md |
Compact 模式实现机制 | ⭐⭐⭐⭐⭐ |
compact-vs-microcompact-detailed-comparison.md |
Compact vs MicroCompact 对比 | ⭐⭐⭐⭐⭐ |
microcompact-analysis.md |
MicroCompact 功能分析 | ⭐⭐⭐⭐ |
main-agent-loop-analysis.md |
主代理循环架构 | ⭐⭐⭐ |
cli-2.0.23-commands-agents-skills-report.md |
命令和技能系统 | ⭐⭐⭐ |
| 文档 | 内容 |
|---|---|
anthropic-docs/slash-commands.md |
Slash 命令使用指南 |
anthropic-docs/hooks.md |
钩子系统配置 |
anthropic-docs/settings.md |
设置选项说明 |
基于提取的提示词,验证所有 9 个部分都已正确定义:
- 1. Primary Request and Intent - ✅ 行 271953, 272049
- 2. Key Technical Concepts - ✅ 行 271954, 272050
- 3. Files and Code Sections - ✅ 行 271955, 272051
- 4. Errors and fixes - ✅ 行 271956, 272052
- 5. Problem Solving - ✅ 行 271957, 272053
- 6. All user messages - ✅ 行 271958, 272054
- 7. Pending Tasks - ✅ 行 271959, 272055(注:编号重复为 6,但内容正确)
- 8. Current Work - ✅ 行 271960, 272056(实际为第 7 项)
- 9. Optional Next Step - ✅ 行 271961, 272057(实际为第 8 项)
注意: 原始提示词中存在编号错误(两个 "6"),但所有 9 个内容部分都完整存在。
- 要求使用
<analysis>标签 - 要求使用
<summary>标签 - 提供了完整的输出示例
- 说明了如何处理自定义指令
- 提供了自定义指令的示例
- 强调保留代码片段和文件名
- 强调记录用户反馈
- 强调防止任务漂移
本报告成功提取了 Claude Code CLI 中 compact 命令的完整提示词系统,包括:
- 默认 compact 提示词 - 完整的 196 行提示词模板
- 自定义指令变体 - 支持用户注入特定要求
- 标签转换函数 - 将 XML 标签转换为 Markdown
- 会话恢复提示词 - 用于继续中断的会话
- 对话标题生成提示词 - 用于自动命名会话
- PreCompact 钩子系统 - 允许在压缩前执行自定义命令
这些提示词共同构成了一个完善的对话压缩系统,确保在长时间会话中能够:
- 保留关键技术上下文
- 维护用户意图的连续性
- 记录所有重要决策和错误
- 防止任务漂移和上下文丢失
提取的原始文本可供:
- 研究 prompt engineering 技术
- 理解 Claude CLI 的内部机制
- 开发类似的对话管理系统
- 自定义和优化总结流程
报告生成信息
- 生成日期: 2026-01-22
- 源代码版本: cli.2.0.29.js
- 提取方法: 直接从 bundled 代码中读取
- 验证状态: ✅ 已验证所有 9 个部分完整性