Context Compressor:Openclaw Skills 的会话内存优化工具

作者:互联网

2026-04-17

AI教程

什么是 Context Compressor?

Context Compressor 是一款专门设计的工具,用于处理因上下文过度堆积而导致性能下降的长周期会话。通过识别上下文何时接近极限,它能智能地总结历史交互,并将关键的项目状态转移到新会话中,确保您的 Openclaw Skills 始终保持敏锐且响应迅速。

该技能可以防止在复杂的代码迭代过程中出现常见的 AI 问题,如重复、细节丢失或“迷失在中间”现象。它在丢弃冗余聊天记录的同时,保留了关键决策、代码更改和待办任务,提供无缝衔接,在不增加臃肿上下文窗口负担的情况下保持连贯性。

下载入口:https://github.com/openclaw/skills/tree/main/skills/maddiedreese/context-compressor

安装与下载

1. ClawHub CLI

从源直接安装技能的最快方式。

npx clawhub@latest install context-compressor

2. 手动安装

将技能文件夹复制到以下位置之一

全局模式 ~/.openclaw/skills/ 工作区 /skills/

优先级:工作区 > 本地 > 内置

3. 提示词安装

将此提示词复制到 OpenClaw 即可自动安装。

请帮我使用 Clawhub 安装 context-compressor。如果尚未安装 Clawhub,请先安装(npm i -g clawhub)。

Context Compressor 应用场景

  • 管理具有大量迭代和改进的长周期编程会话。
  • 防止在智能体开始出现重复或丢失项目细节追踪时性能下降。
  • 在大型开发项目中,在达到硬性 Token 限制之前主动管理资源。
  • 使用 Openclaw Skills 在多个会话交接中保持项目连贯性。
Context Compressor 工作原理
  1. 该技能持续监控会话元数据,以跟踪实时上下文使用情况。
  2. 当达到用户定义的阈值(默认为 80%)时,自动触发压缩工作流。
  3. 提取高信号数据,包括关键架构决策、当前文件状态和待办事项。
  4. 以压缩摘要为新基础,为智能体启动一个全新的会话。
  5. 自动转移项目记忆,确保无缝的开发体验,且不丢失工作进度。

Context Compressor 配置指南

要在 Openclaw Skills 环境中配置 Context Compressor,请使用以下 CLI 命令:

# 设置压缩触发阈值(例如 75%)
context-compressor set-threshold 75

# 调整总结过程的深度
context-compressor set-depth detailed

# 检查当前的上下文状态和使用情况
context-compressor status

Context Compressor 数据架构与分类体系

该技能通过结构化的交接过程组织数据,生成特定的产出物以确保在 Openclaw Skills 会话之间的持久性:

产出物 路径 描述
摘要文件 memory/compressed-{id}.md 执行摘要、决策和代码片段。
记忆更新 MEMORY.md 反映压缩状态的更新后项目记忆。
用户上下文 USER.md 保留的用户偏好和会话目标。
会话交接 N/A 用于新会话初始化的元数据包。

Context Compressor Skill

Automated context management for long-running Clawdbot sessions. Detects when context limits approach, compresses old conversation history, and seamlessly transfers to a fresh session.

When to Use

  • Long coding sessions with extensive context accumulation
  • Projects with many iterations and refinements
  • When noticing Claude repeating itself or losing track of details
  • Proactively before hitting hard context limits
  • During heartbeats or idle moments when user isn't actively waiting

How It Works

  1. Monitoring: Continuously tracks context usage via session metadata
  2. Compression: When threshold reached (configurable, default 80%), summarizes old messages
  3. Preservation: Extracts key decisions, code changes, file states, and action items
  4. Handoff: Initiates new session with compressed context as foundation
  5. Continuity: User experiences seamless transition with all important context preserved

Key Features

  • Smart Summarization: Preserves decisions, code, file states, not just raw text
  • Configurable Thresholds: Set when compression triggers (70-90% of context limit)
  • Background Operation: Works during heartbeats or low-activity periods
  • Selective Retention: Keeps important files, decisions, TODOs; compresses chaff
  • Session State Transfer: New session inherits all critical context automatically

Core Concepts

Context Degradation Patterns

As sessions grow long, watch for these signs:

  • Repetitive responses ("As I mentioned earlier...")
  • Missing references to earlier decisions
  • Forgetting file modifications
  • Asking to repeat information
  • General coherence degradation

Compression Strategy

  1. Extract Core Intelligence:

    • All decisions made and their rationale
    • File paths and their current state
    • Pending tasks and their status
    • Important code snippets or configurations
    • User preferences and patterns
  2. Condense History:

    • Remove filler, backtracking, dead ends
    • Keep only high-signal turns
    • Merge related iterations into summaries
    • Preserve critical code snippets inline
  3. Format for Efficiency:

    • Use compact representations
    • Reference files rather than dump contents
    • List decisions as bullet points
    • Include only relevant code context

Usage

The skill runs automatically during heartbeats and idle periods. Configure threshold:

# Set compression to trigger at 75% context usage
context-compressor set-threshold 75

# Check current status
context-compressor status

Manual Trigger

# Force compression and session reset
context-compressor compress --force

Configuration

# View all settings
context-compressor config

# Adjust summarization depth
context-compressor set-depth brief|detailed|comprehensive

# Set quiet hours (compression won't run)
context-compressor set-quiet-hours 23:00-07:00

Output

When compression occurs, the skill produces:

  1. Summary File: memory/compressed-{session-id}.md

    • Executive summary of session
    • Key decisions made
    • Files modified and their states
    • Pending tasks
    • Code snippets worth preserving
  2. Session Handoff: Automatic new session with:

    • User context (USER.md)
    • Project memory (MEMORY.md)
    • Compressed session summary
    • Current working state

Best Practices

  1. Regular Compression: Don't wait for limits. Compress proactively every few hours
  2. Preserve Code: Always keep actual code snippets, not just references
  3. Track Decisions: Explicitly note WHY decisions were made, not just WHAT
  4. Keep TODOs: Mark incomplete work clearly for continuity
  5. Reference Files: Point to files rather than embedding full contents

Integration Points

  • Heartbeats: Runs compression checks during heartbeat cycles
  • Memory System: Writes compressed summaries to memory files
  • Session Management: Coordinates with session spawn for handoff
  • File Tracking: References current file states accurately

Technical Details

Compression Algorithm

  1. Parse session transcript into atomic turns
  2. Score each turn for importance (decisions = high, chat = low)
  3. Keep top N% of turns by importance score
  4. Summarize remaining turns into executive summary
  5. Extract and preserve code blocks separately
  6. Generate session transfer package

Thresholds

  • Conservative (70%): Trigger early, preserve more context
  • Balanced (80%): Default, good for most workflows
  • Aggressive (90%): Push limits, maximum session length
  • Manual Only: Disable auto-trigger, compress on demand

File References

The compressor tracks:

  • Modified files and their paths
  • Configuration changes
  • New files created
  • Deleted files
  • Directory structure changes

Troubleshooting

Compression Too Frequent

# Increase threshold
context-compressor set-threshold 85

Context Lost After Handoff

Check that:

  1. Compressed summary was generated (memory/compressed-*.md)
  2. New session loaded memory files
  3. Critical files weren't in the "chaff" that got dropped

Performance Impact

Compression runs in background and should complete in <30s for typical sessions. If slower:

  • Reduce summarization depth
  • Increase threshold to compress less frequently
  • Exclude large files from compression scope

Examples

Typical Workflow

User: Working on notes app sidebar
[Session runs 2 hours, many iterations]

Heartbeat triggers → Context at 78% → Auto-compress → New session
User: (no interruption, seamless continuation)
New session has: executive summary, key decisions, file states, TODOs

Manual Recovery

User notices Claude repeating itself
User: "context-compressor compress --force"
Compressor summarizes → New session starts → User continues seamlessly
  • memory-system: Underlying memory infrastructure
  • self-improving-agent: Learns from session patterns
  • sessions-spawn: Handles new session creation

See Also

  • Context Engineering Skills Collection
  • Research on "lost-in-the-middle" phenomenon in LLM context windows

相关推荐