context-compressor

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Context Compressor Skill

Context Compressor Skill

<identity> Context Compressor Skill - Techniques for reducing token usage while preserving decision-critical information. Helps agents work efficiently within context limits. </identity> <capabilities> - Compressing conversation history - Summarizing code and documentation - Extracting key decisions and context - Creating efficient memory snapshots - Reducing redundancy in context </capabilities> <instructions> <execution_process>
<identity> Context Compressor Skill - 用于在保留决策关键信息的同时降低Token使用量的技巧,帮助Agent在上下文限制内高效工作。 </identity> <capabilities> - 压缩对话历史 - 总结代码与文档 - 提取关键决策与上下文信息 - 创建高效的内存快照 - 减少上下文中的冗余内容 </capabilities> <instructions> <execution_process>

Step 1: Identify Compressible Content

步骤1:识别可压缩内容

Content types that can be compressed:
TypeCompression Strategy
CodeKeep signatures, summarize implementations
ConversationsExtract decisions, drop small talk
DocumentationKeep headings and key points
ErrorsKeep message and location, drop stack frames
LogsKeep patterns, drop repetitions
可压缩的内容类型:
类型压缩策略
代码保留签名,总结实现逻辑
对话内容提取决策信息,剔除闲聊内容
文档保留标题与核心要点
错误信息保留错误消息与位置,剔除调用栈帧
日志保留模式规律,剔除重复内容

Step 2: Apply Compression Techniques

步骤2:应用压缩技巧

Technique 1: Decision Extraction
Before:
User: Should we use Redis or Memcached?
Assistant: Let me analyze both options...
[500 words of analysis]
Recommendation: Redis for pub/sub support.
User: Ok let's use Redis.
After:
Decision: Use Redis (chosen for pub/sub support)
Technique 2: Code Summarization
Before:
javascript
// 100 lines of UserService implementation
After:
UserService: CRUD operations for users
- Methods: create, read, update, delete, findByEmail
- Dependencies: db, validator, logger
- Location: src/services/user.js
Technique 3: Error Compression
Before:
Error: Cannot read property 'id' of undefined
    at UserController.getUser (src/controllers/user.js:45:23)
    at Layer.handle [as handle_request] (node_modules/express/lib/router/layer.js:95:5)
    ... 20 more stack frames
After:
Error: Cannot read 'id' of undefined @ src/controllers/user.js:45
Cause: User object is null when accessing .id
技巧1:决策提取
压缩前:
User: Should we use Redis or Memcached?
Assistant: Let me analyze both options...
[500 words of analysis]
Recommendation: Redis for pub/sub support.
User: Ok let's use Redis.
压缩后:
Decision: Use Redis (chosen for pub/sub support)
技巧2:代码总结
压缩前:
javascript
// 100 lines of UserService implementation
压缩后:
UserService: 用户相关CRUD操作
- 方法:create、read、update、delete、findByEmail
- 依赖:db、validator、logger
- 位置:src/services/user.js
技巧3:错误信息压缩
压缩前:
Error: Cannot read property 'id' of undefined
    at UserController.getUser (src/controllers/user.js:45:23)
    at Layer.handle [as handle_request] (node_modules/express/lib/router/layer.js:95:5)
    ... 20 more stack frames
压缩后:
Error: Cannot read 'id' of undefined @ src/controllers/user.js:45
原因:访问.id时User对象为null

Step 3: Structure Compressed Output

步骤3:结构化压缩输出

Use consistent formats:
markdown
undefined
使用统一格式:
markdown
undefined

Session Summary

会话总结

Decisions Made

已做出的决策

  • [D1] Use Redis for caching
  • [D2] JWT for authentication
  • [D1] 使用Redis作为缓存
  • [D2] 使用JWT进行身份验证

Files Modified

修改的文件

  • src/auth/jwt.js (new)
  • src/config/redis.js (updated)
  • src/auth/jwt.js(新增)
  • src/config/redis.js(更新)

Open Items

待办事项

  • Add rate limiting
  • Write tests for JWT
undefined
  • 添加限流机制
  • 为JWT编写测试用例
undefined

Step 4: Validate Compression

步骤4:验证压缩效果

Ensure critical info preserved:
  • All decisions captured
  • Key file locations retained
  • Error causes documented
  • Next steps clear
</execution_process>
<best_practices>
  1. Preserve Decisions: Never lose decision rationale
  2. Keep Locations: File paths and line numbers are critical
  3. Summarize, Don't Delete: Transform verbose content
  4. Use References: Point to files instead of including content
  5. Test Recovery: Can you continue work from compressed context?
</best_practices> </instructions>
<examples> <usage_example> **Compression Request**:
Compress the last 10 messages for context handoff
Example Response:
markdown
undefined
确保关键信息已保留:
  • 所有决策已捕获
  • 关键文件位置已保留
  • 错误原因已记录
  • 后续步骤清晰明确
</execution_process>
<best_practices>
  1. 保留决策信息:绝不能丢失决策的依据
  2. 保留位置信息:文件路径与行号至关重要
  3. 总结而非删除:将冗长内容进行转化
  4. 使用引用:指向文件而非直接包含内容
  5. 测试可恢复性:能否基于压缩后的上下文继续开展工作?
</best_practices> </instructions>
<examples> <usage_example> **压缩请求**:
Compress the last 10 messages for context handoff
示例响应:
markdown
undefined

Context Summary (compressed from 10 messages)

上下文总结(由10条消息压缩而来)

Task

任务

Implementing user authentication with JWT
基于JWT实现用户身份验证

Progress

进度

  1. Created JWT utility (src/auth/jwt.js)
  2. Added login endpoint (src/routes/auth.js)
  3. Tests failing - mock issue
  1. 已创建JWT工具类(src/auth/jwt.js)
  2. 已添加登录接口(src/routes/auth.js)
  3. 测试失败 - 模拟数据存在问题

Key Decisions

关键决策

  • Using RS256 for JWT signing (security over performance)
  • 1-hour token expiry with refresh tokens
  • 使用RS256进行JWT签名(优先考虑安全性而非性能)
  • Token有效期为1小时,支持刷新Token

Current Issue

当前问题

Jest mock for jwt.sign not working Location: src/auth/tests/jwt.test.js:23
Jest对jwt.sign的模拟不生效 位置:src/auth/tests/jwt.test.js:23

Next Steps

后续步骤

  1. Fix JWT mock
  2. Add refresh token endpoint

</usage_example>
</examples>
  1. 修复JWT模拟问题
  2. 添加刷新Token接口

</usage_example>
</examples>

Rules

规则

  • Never lose decision rationale
  • Always include file locations
  • Test that work can continue from compressed context
  • 绝不能丢失决策的依据
  • 始终包含文件位置信息
  • 验证能否基于压缩后的上下文继续开展工作

Workflow Integration

工作流集成

This skill supports multi-agent orchestration by enabling efficient context management:
Router Decision:
.claude/workflows/core/router-decision.md
  • Router spawns agents that use this skill for context-efficient handoffs
  • Used in long-running sessions to maintain continuity
Artifact Lifecycle:
.claude/workflows/core/skill-lifecycle.md
  • Compression patterns evolve with framework changes
  • Session summaries feed into memory protocol
Related Workflows:
  • session-handoff
    skill for complete handoff protocol
  • swarm-coordination
    skill for multi-agent context sharing

该Skill通过高效的上下文管理支持多Agent编排:
路由决策:
.claude/workflows/core/router-decision.md
  • 路由模块会生成使用该Skill的Agent,以实现高效的上下文交接
  • 用于长时运行会话,维持工作连续性
工件生命周期:
.claude/workflows/core/skill-lifecycle.md
  • 压缩模式会随框架更新而演进
  • 会话总结会同步到内存协议中
相关工作流:
  • session-handoff
    Skill:用于完整的上下文交接协议
  • swarm-coordination
    Skill:用于多Agent上下文共享

Memory Protocol (MANDATORY)

内存协议(强制要求)

Before starting:
bash
cat .claude/context/memory/learnings.md
After completing:
  • New pattern ->
    .claude/context/memory/learnings.md
  • Issue found ->
    .claude/context/memory/issues.md
  • Decision made ->
    .claude/context/memory/decisions.md
ASSUME INTERRUPTION: Your context may reset. If it's not in memory, it didn't happen.
开始前:
bash
cat .claude/context/memory/learnings.md
完成后:
  • 新的模式 ->
    .claude/context/memory/learnings.md
  • 发现的问题 ->
    .claude/context/memory/issues.md
  • 做出的决策 ->
    .claude/context/memory/decisions.md
假设可能出现中断:你的上下文可能会重置。如果信息未存入内存,则视为未发生。