mcpField
C
context-compactor

context-compactor

github.com/adaptationioAgent 工作流40 次安装
代码仓库

关于 context-compactor

context-compactor 是一个 Python 组件,可在接近 token 限制时压缩对话上下文。它优先保留架构决策、未解决的问题和当前状态,并清除过时输出及重复消息。

主要功能

  1. 01检查是否需要压缩上下文
  2. 02评估消息重要性
  3. 03保留关键决策、问题和状态
  4. 04总结内容并丢弃过时信息
  5. 05估算 token 用量

适用场景

  1. 01在接近 token 限制时缩减上下文
  2. 02为会话交接准备精简状态
  3. 03在长会话中保留重要项目细节

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/adaptationio/skrillz --skill context-compactor

安装后在 Claude Code 中输入/context-compactor确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
COMPACTION-STRATEGY.md
scripts/
compactor.py
importance_scorer.py
summarizer.py
token_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                                         │
│                                                              │
└─────────────────────────────────────────────────────────────┘

压缩流程

  1. 评估重要性:为每条消息或内容评分
  2. 识别关键信息:标记必须保留的内容
  3. 总结中间内容:压缩可总结的内容
  4. 丢弃过时内容:移除可丢弃的内容
  5. 验证:确保压缩后的上下文连贯

令牌预算

上下文类型限制操作
< 50%正常无需操作
50-75%警告准备压缩
75-90%压缩触发压缩
> 90%紧急激进压缩

集成点

  • autonomous-session-manager:触发压缩检查
  • memory-manager:存储压缩后的摘要
  • handoff-coordinator:在交接时使用压缩后的状态

参考资料

  • references/COMPACTION-STRATEGY.md - 详细策略
  • references/TOKEN-BUDGETS.md - 预算管理

脚本

  • scripts/compactor.py - 核心 ContextCompactor
  • scripts/importance_scorer.py - 消息评分
  • scripts/summarizer.py - 内容总结
  • scripts/token_estimator.py - 令牌计数

评论

支持 Markdown