关于 context-compactor
context-compactor 是一个 Python 组件,可在接近 token 限制时压缩对话上下文。它优先保留架构决策、未解决的问题和当前状态,并清除过时输出及重复消息。
主要功能
- 01检查是否需要压缩上下文
- 02评估消息重要性
- 03保留关键决策、问题和状态
- 04总结内容并丢弃过时信息
- 05估算 token 用量
适用场景
- 01在接近 token 限制时缩减上下文
- 02为会话交接准备精简状态
- 03在长会话中保留重要项目细节
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ adaptationio/ skrillz --skill context-compactor安装后在 Claude Code 中输入/context-compactor确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/COMPACTION-STRATEGY.mdscripts/compactor.pyimportance_scorer.pysummarizer.pytoken_estimator.py详细介绍
上下文压缩器
在接近令牌限制时自动总结并压缩上下文,同时保留关键信息。
快速开始
检查是否需要压缩
from scripts.compactor import ContextCompactor
compactor = ContextCompactor()
if compactor.should_compact(context, limit=100000):
compacted = compactor.compact(context)
压缩上下文
compacted = compactor.compact(context)
# Preserves: architectural decisions, bugs, current state
# Discards: stale tool outputs, redundant messages
信息保留策略
┌─────────────────────────────────────────────────────────────┐
│ PRESERVATION PRIORITY │
├─────────────────────────────────────────────────────────────┤
│ │
│ MUST PRESERVE (Never remove) │
│ ├─ Architectural decisions │
│ ├─ Unresolved bugs and blockers │
│ ├─ Current feature state │
│ ├─ Recent file changes (last 5) │
│ └─ Error patterns and solutions │
│ │
│ CAN SUMMARIZE (Compress to summary) │
│ ├─ Completed features (list of IDs) │
│ ├─ Resolved errors (brief mention) │
│ └─ Historical decisions (key points) │
│ │
│ CAN DISCARD (Remove entirely) │
│ ├─ Redundant tool outputs │
│ ├─ Stale search results │
│ ├─ Superseded messages │
│ └─ Verbose logging │
│ │
└─────────────────────────────────────────────────────────────┘
压缩流程
- 评估重要性:为每条消息或内容评分
- 识别关键信息:标记必须保留的内容
- 总结中间内容:压缩可总结的内容
- 丢弃过时内容:移除可丢弃的内容
- 验证:确保压缩后的上下文连贯
令牌预算
| 上下文类型 | 限制 | 操作 |
|---|---|---|
| < 50% | 正常 | 无需操作 |
| 50-75% | 警告 | 准备压缩 |
| 75-90% | 压缩 | 触发压缩 |
| > 90% | 紧急 | 激进压缩 |
集成点
- autonomous-session-manager:触发压缩检查
- memory-manager:存储压缩后的摘要
- handoff-coordinator:在交接时使用压缩后的状态
参考资料
references/COMPACTION-STRATEGY.md- 详细策略references/TOKEN-BUDGETS.md- 预算管理
脚本
scripts/compactor.py- 核心 ContextCompactorscripts/importance_scorer.py- 消息评分scripts/summarizer.py- 内容总结scripts/token_estimator.py- 令牌计数
评论
支持 Markdown