strategic-compact
建议在逻辑间隔进行手动上下文压缩(Context Compaction),以便在任务阶段中保留上下文,而不是依赖任意的自动压缩。
Best use case
strategic-compact is best used when you need a repeatable AI agent workflow instead of a one-off prompt. It is especially useful for teams working in multi. 建议在逻辑间隔进行手动上下文压缩(Context Compaction),以便在任务阶段中保留上下文,而不是依赖任意的自动压缩。
建议在逻辑间隔进行手动上下文压缩(Context Compaction),以便在任务阶段中保留上下文,而不是依赖任意的自动压缩。
Users should expect a more consistent workflow output, faster repeated execution, and less time spent rewriting prompts from scratch.
Practical example
Example input
Use the "strategic-compact" skill to help with this workflow task. Context: 建议在逻辑间隔进行手动上下文压缩(Context Compaction),以便在任务阶段中保留上下文,而不是依赖任意的自动压缩。
Example output
A structured workflow result with clearer steps, more consistent formatting, and an output that is easier to reuse in the next run.
When to use this skill
- Use this skill when you want a reusable workflow rather than writing the same prompt again and again.
When not to use this skill
- Do not use this when you only need a one-off answer and do not need a reusable workflow.
- Do not use it if you cannot install or maintain the related files, repository context, or supporting tools.
Installation
Claude Code / Cursor / Codex
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/strategic-compact/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How strategic-compact Compares
| Feature / Agent | strategic-compact | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
建议在逻辑间隔进行手动上下文压缩(Context Compaction),以便在任务阶段中保留上下文,而不是依赖任意的自动压缩。
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
SKILL.md Source
# 策略性压缩(Strategic Compact)技能
建议在工作流的策略点手动执行 `/compact` 命令,而不是依赖任意的自动压缩(Auto-compaction)。
## 何时启用
- 运行接近上下文限制(200K+ tokens)的长会话(Long Sessions)时
- 处理多阶段任务(研究 → 规划 → 实现 → 测试)时
- 在同一会话中切换到不相关的任务时
- 完成重大里程碑(Milestone)并开始新工作后
- 当响应变慢或连贯性下降(上下文压力)时
## 为什么要进行策略性压缩?
自动压缩在任意点触发:
- 通常在任务中间,导致重要上下文丢失
- 缺乏对逻辑任务边界的感知
- 可能会中断复杂的多步骤操作
逻辑边界处的策略性压缩:
- **在探索之后,执行之前** —— 压缩研究上下文,保留实现方案
- **完成里程碑之后** —— 为下一阶段提供全新的开始
- **重大上下文转移之前** —— 在开始不同任务前清除探索上下文
## 工作原理
`suggest-compact.js` 脚本在 `PreToolUse`(`Edit`/`Write`)钩子(Hook)上运行,并且:
1. **跟踪工具调用** —— 统计会话中的工具调用(Tool Calls)次数
2. **阈值检测** —— 在达到可配置的阈值(默认:50 次调用)时提出建议
3. **定期提醒** —— 达到阈值后,每隔 25 次调用提醒一次
## 钩子配置(Hook Setup)
添加到你的 `~/.claude/settings.json` 中:
```json
{
"hooks": {
"PreToolUse": [
{
"matcher": "Edit",
"hooks": [{ "type": "command", "command": "node ~/.claude/skills/strategic-compact/suggest-compact.js" }]
},
{
"matcher": "Write",
"hooks": [{ "type": "command", "command": "node ~/.claude/skills/strategic-compact/suggest-compact.js" }]
}
]
}
}
```
## 配置
环境变量:
- `COMPACT_THRESHOLD` —— 第一次建议前的工具调用次数(默认:50)
## 压缩决策指南
使用下表来决定何时压缩:
| 阶段转换 | 是否压缩? | 原因 |
|-----------------|----------|-----|
| 研究 → 规划 | 是 | 研究上下文很臃肿;方案是提炼后的产出 |
| 规划 → 实现 | 是 | 方案已在 `TodoWrite` 或文件中;为代码腾出上下文空间 |
| 实现 → 测试 | 可能 | 如果测试引用了近期代码则保留;如果切换关注点则压缩 |
| 调试 → 下一功能 | 是 | 调试追踪(Debug traces)会污染不相关工作的上下文 |
| 实现中 | 否 | 丢失变量名、文件路径和部分状态的代价很高 |
| 方案失败后 | 是 | 在尝试新方案前清除死胡同式的推理 |
## 压缩后保留的内容
了解哪些内容会持久化,有助于你充满信心地进行压缩:
| 保留 | 丢失 |
|----------|------|
| `CLAUDE.md` 指令 | 中间推理和分析 |
| `TodoWrite` 任务列表 | 之前读取过的文件内容 |
| 记忆文件(`~/.claude/memory/`)| 多步骤对话上下文 |
| Git 状态(提交、分支)| 工具调用历史和计数 |
| 磁盘上的文件 | 口头陈述的细微用户偏好 |
## 最佳实践
1. **规划完成后压缩** —— 一旦方案在 `TodoWrite` 中确定,就进行压缩以全新开始
2. **调试完成后压缩** —— 在继续之前清除错误解决上下文
3. **不要在实现过程中压缩** —— 保留相关更改的上下文
4. **阅读建议** —— 钩子告诉你*何时*,你决定*是否*
5. **压缩前写入** —— 在压缩前将重要上下文保存到文件或记忆中
6. **使用带有总结的 /compact** —— 添加自定义消息:`/compact 下一步重点实现认证中间件`
## 相关内容
- [长篇指南(The Longform Guide)](https://x.com/affaanmustafa/status/2014040193557471352) —— 令牌(Token)优化章节
- 记忆持久化钩子(Memory persistence hooks) —— 用于压缩后仍保留的状态
- `continuous-learning` 技能 —— 在会话结束前提取模式Related Skills
plankton-code-quality
使用 Plankton 实现编写时代码质量强制执行 —— 通过钩子在每次文件编辑时进行自动格式化、代码检查,并由 Claude 驱动自动修复。
autonomous-loops
自主运行 Claude Code 循环的模式与架构 —— 从简单的顺序流水线到 RFC 驱动的多智能体 DAG 系统。
visa-doc-translate
将签证申请文件(图片)翻译成英文,并创建包含原文和译文的双语PDF
swiftui-patterns
SwiftUI 架构模式,使用 @Observable 进行状态管理,视图组合,导航,性能优化,以及现代 iOS/macOS UI 最佳实践。
swift-protocol-di-testing
基于协议的依赖注入,用于可测试的Swift代码——使用聚焦协议和Swift Testing模拟文件系统、网络和外部API。
swift-concurrency-6-2
Swift 6.2 可接近的并发性 — 默认单线程,@concurrent 用于显式后台卸载,隔离一致性用于主 actor 类型。
swift-actor-persistence
在 Swift 中使用 actor 实现线程安全的数据持久化——基于内存缓存与文件支持的存储,通过设计消除数据竞争。
skill-stocktake
用于审计Claude技能和命令的质量。支持快速扫描(仅变更技能)和全面盘点模式,采用顺序子代理批量评估。
search-first
研究优先于编码的工作流程。在编写自定义代码之前,搜索现有的工具、库和模式。调用研究员代理。
regex-vs-llm-structured-text
选择在解析结构化文本时使用正则表达式还是大型语言模型的决策框架——从正则表达式开始,仅在低置信度的边缘情况下添加大型语言模型。
liquid-glass-design
iOS 26 液态玻璃设计系统 — 适用于 SwiftUI、UIKit 和 WidgetKit 的动态玻璃材质,具有模糊、反射和交互式变形效果。
foundation-models-on-device
苹果FoundationModels框架用于设备上的LLM——文本生成、使用@Generable进行引导生成、工具调用,以及在iOS 26+中的快照流。