compact-guard

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Compact 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
/compact
or when auto-compact warning appears.
在
/compact
之前,或出现 auto-compact 警告时使用。

Key Constants (from Claude Code source)

关键常量(来自 Claude Code 源代码)

  • POST_COMPACT_MAX_FILES_TO_RESTORE = 5
    — only 5 files survive
  • POST_COMPACT_TOKEN_BUDGET = 50K
    — total restore budget
  • POST_COMPACT_MAX_TOKENS_PER_FILE = 5K
    — per-file limit
  • Auto-compact fires at
    context_window - 13K
    buffer
  • POST_COMPACT_MAX_FILES_TO_RESTORE = 5
    — 仅保留 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:
  1. Current task — What are you working on? One sentence.
  2. Files in progress — Which files are being edited? (max 5 — compaction only restores 5)
  3. Decisions made — Any architectural choices made this session
  4. Blockers — What's preventing progress?
  5. Next steps — What to do immediately after compact
压缩前,将以下内容保存到 memory 或临时文件中:
  1. 当前任务 — 你正在做什么?用一句话概括。
  2. 正在处理的文件 — 哪些文件正在编辑?(最多 5 个——压缩只会恢复 5 个)
  3. 已做出的决定 — 本次会话中做出了哪些架构选择?
  4. 阻塞项 — 什么因素阻碍了进展?
  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:
  1. Re-read the top-priority file (the one you're actively editing)
  2. Check task list for current progress
  3. Review any scratch notes saved pre-compact
  4. Resume from next steps
压缩后,立即执行:
  1. 重新读取优先级最高的文件(当前正在编辑的文件)
  2. 查看任务列表,确认当前进度
  3. 检查压缩前保存的任何临时笔记
  4. 从后续步骤继续执行

Prevention Strategies

预防策略

StrategyToken SavingsWhen
Delegate grep/search to subagent30-60% per searchAlways for broad searches
Read only needed lines (
offset
/
limit
)
50-90% per readLarge files
Compact at task boundariesPreserves coherenceBetween logical steps
Use
/resume
for fresh start
100%Unrelated new task
策略Token 节省量使用时机
将 grep/search 委派给 subagent每次搜索节省 30-60%广泛搜索时始终使用
仅读取所需行(
offset
/
limit
)
每次读取节省 50-90%大型文件
在任务边界执行压缩保持上下文连贯性逻辑步骤之间
使用
/resume
开启全新任务
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 行以内,为实际上下文留出空间