compact-guard
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCompact Guard
Compact Guard
Protect important context through compaction cycles. Based on Claude Code internals: compaction restores max 5 files with 5K tokens each, within a 50K total budget.
通过压缩周期保护重要上下文。基于 Claude Code 内部机制:压缩最多可恢复 5 个文件,每个文件最多 5K tokens,总恢复预算为 50K。
Trigger
触发时机
Use before or when auto-compact warning appears.
/compact在 之前,或出现 auto-compact 警告时使用。
/compactKey Constants (from Claude Code source)
关键常量(来自 Claude Code 源代码)
- — only 5 files survive
POST_COMPACT_MAX_FILES_TO_RESTORE = 5 - — total restore budget
POST_COMPACT_TOKEN_BUDGET = 50K - — per-file limit
POST_COMPACT_MAX_TOKENS_PER_FILE = 5K - Auto-compact fires at buffer
context_window - 13K
- — 仅保留 5 个文件
POST_COMPACT_MAX_FILES_TO_RESTORE = 5 - — 总恢复预算
POST_COMPACT_TOKEN_BUDGET = 50K - — 单文件上限
POST_COMPACT_MAX_TOKENS_PER_FILE = 5K - Auto-compact 在 缓冲区处触发
context_window - 13K
Pre-Compact Checklist
压缩前检查清单
Before compacting, save these to memory or a scratch file:
- Current task — What are you working on? One sentence.
- Files in progress — Which files are being edited? (max 5 — compaction only restores 5)
- Decisions made — Any architectural choices made this session
- Blockers — What's preventing progress?
- Next steps — What to do immediately after compact
压缩前,将以下内容保存到 memory 或临时文件中:
- 当前任务 — 你正在做什么?用一句话概括。
- 正在处理的文件 — 哪些文件正在编辑?(最多 5 个——压缩只会恢复 5 个)
- 已做出的决定 — 本次会话中做出了哪些架构选择?
- 阻塞项 — 什么因素阻碍了进展?
- 后续步骤 — 压缩后立即要做什么?
Strategy: Microcompact First
策略:优先进行 Microcompact
Before full compaction, try microcompact:
- Large tool results (test output, grep results) can be trimmed
- File reads that are no longer relevant can be dropped
- Use subagents for heavy exploration to keep main context clean
在完整压缩之前,尝试使用 microcompact:
- 可以精简大型工具结果(测试输出、grep 结果)
- 可以移除已经不再相关的文件读取内容
- 使用 subagent 进行深度探索,以保持主上下文简洁
Post-Compact Recovery
压缩后恢复
After compaction, immediately:
- Re-read the top-priority file (the one you're actively editing)
- Check task list for current progress
- Review any scratch notes saved pre-compact
- Resume from next steps
压缩后,立即执行:
- 重新读取优先级最高的文件(当前正在编辑的文件)
- 查看任务列表,确认当前进度
- 检查压缩前保存的任何临时笔记
- 从后续步骤继续执行
Prevention Strategies
预防策略
| Strategy | Token Savings | When |
|---|---|---|
| Delegate grep/search to subagent | 30-60% per search | Always for broad searches |
Read only needed lines ( | 50-90% per read | Large files |
| Compact at task boundaries | Preserves coherence | Between logical steps |
Use | 100% | Unrelated new task |
| 策略 | Token 节省量 | 使用时机 |
|---|---|---|
| 将 grep/search 委派给 subagent | 每次搜索节省 30-60% | 广泛搜索时始终使用 |
仅读取所需行( | 每次读取节省 50-90% | 大型文件 |
| 在任务边界执行压缩 | 保持上下文连贯性 | 逻辑步骤之间 |
使用 | 100% | 无关的新任务 |
Output
输出
After running compact-guard:
text
COMPACT GUARD
Files to preserve: [list top 5]
Task state: [one sentence]
Decisions: [key choices]
Next step: [immediate action after compact]
Ready to compact. Run /compact now.运行 compact-guard 后:
text
COMPACT GUARD
需要保留的文件:[列出前 5 个]
任务状态:[用一句话概括]
已做出的决定:[关键选择]
后续步骤:[压缩后立即执行的操作]
已准备就绪,可以压缩。现在运行 /compact。Rules
规则
- Always save state before compacting — never compact blind
- Prioritize the file you're actively editing as #1 to restore
- If auto-compact fires unexpectedly, immediately re-read your working file
- Keep CLAUDE.md under 60 lines to leave room for actual context
- 始终在压缩前保存状态——绝不要盲目压缩
- 将当前正在编辑的文件列为恢复时的第 1 个文件
- 如果 auto-compact 意外触发,立即重新读取当前工作文件
- 将 CLAUDE.md 控制在 60 行以内,为实际上下文留出空间