context-compression

Prevent context overflow with automatic session truncation and memory preservation.

Safety Notice

This listing is from the official public ClawHub registry. Review SKILL.md and referenced scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "context-compression" with this command: npx skills add lifei68801/context-compression

Context Compression

Keep conversations within limits. Never lose important context.

After installing, run the interactive setup wizard to generate your config file, then add the suggested cron entry with crontab -e. See Quick Start below for commands.

Quick Start

File location: ClawHub installs this skill to ~/.openclaw/workspace/skills/context-compression/. All scripts are placed here directly. This is the standard OpenClaw skill install path — no manual file placement needed.

# 1. Install and configure (interactive)
bash ~/.openclaw/workspace/skills/context-compression/configure.sh

# 2. Verify config exists
cat ~/.openclaw/workspace/.context-compression-config.json

# 3. Set up crontab (example: every 10 minutes)
*/10 * * * * ~/.openclaw/workspace/skills/context-compression/truncate-sessions-safe.sh

How It Works

Session Truncation (truncate-sessions-safe.sh)

  • Scheduling: System crontab (e.g., */10 * * * *)
  • Action: Reads .jsonl session files under ~/.openclaw/agents/*/sessions/, trims each file to the configured size
  • Safety: Skips files with a matching .lock file (active session)
  • Integrity: Keeps JSONL line boundaries intact — never splits a line
  • Strategy: priority-first scans for important keywords before trimming and preserves matching lines

Fact Identification

  • Keyword-based: identify-facts.sh — scans truncated content for keywords (重要, 决定, TODO, 偏好, deadline, must remember, etc.) and appends findings to memory/YYYY-MM-DD.md
  • AI-assisted: identify-facts-enhanced.sh — calls openclaw agent --agent main --message with the trimmed content to semantically identify important facts. Only used when openclaw CLI is available on PATH.
  • Triggered by: truncate-sessions-safe.sh calls one of these before each truncation cycle

Preference Lifecycle (check-preferences-expiry.sh)

  • Scheduling: Once daily via crontab
  • Mechanism: Reads MEMORY.md preference entries tagged with @YYYY-MM-DD, removes expired ones
  • Tiers: Short-term (1-7 days), Mid-term (1-4 weeks), Long-term (permanent)

Scripts

ScriptPurposeScheduling
truncate-sessions-safe.shTrim session JSONL filescrontab, every 10 min
identify-facts.shKeyword-based fact detectionCalled by truncate script
identify-facts-enhanced.shAI-assisted fact detectionCalled by truncate script
check-preferences-expiry.shRemove expired preferencescrontab, once daily
configure.shInteractive setup wizardManual, one-time
session-start-hook.shLoad context at session startCalled by AGENTS.md
session-end-hook.shSave context at session endCalled by AGENTS.md
check-context-health.shReport current context statusManual / on-demand

Configuration

File: ~/.openclaw/workspace/.context-compression-config.json

{
  "version": "2.3",
  "maxChars": 40000,
  "frequencyMinutes": 10,
  "skipActive": true,
  "strategy": "priority-first",
  "useAiIdentification": false,
  "priorityKeywords": [
    "重要", "决定", "记住", "TODO", "偏好",
    "important", "remember", "must", "deadline"
  ]
}
FieldTypeDefaultDescription
maxCharsnumber40000Max chars to keep per session file
frequencyMinutesnumber10How often crontab runs truncate
skipActivebooleantrueSkip sessions with .lock files
strategystringpriority-firstTruncation strategy
useAiIdentificationbooleanfalseSet true to use AI-assisted fact identification (may send content to remote LLMs)
priorityKeywordsstring[](see above)Keywords to preserve during truncation

Troubleshooting

ProblemSolution
Context still exceededReduce maxChars in config
Memory not persistingCheck that AGENTS.md includes session-start-hook
Crontab not runningVerify PATH in crontab includes node/openclaw binary location

Safety

Data Protection

  • No deletion: Truncation writes the trimmed portion back to the same file. It does not delete files.
  • Backup before trim: truncate-sessions-safe.sh creates a .pre-trim backup of each file before modification. Backups are cleaned up after a successful write.
  • Line integrity: Truncation only cuts at JSONL line boundaries. Partial lines are never written.
  • Active sessions protected: Files with a matching .lock (currently in use) are always skipped, even if oversized.

Safe Defaults

All configuration values have conservative defaults:

  • skipActive: true — never touches a running session
  • maxChars: 40000 — keeps substantial history per session
  • strategy: priority-first — preserves lines matching priority keywords before trimming anything
  • No direct network access from scripts. The optional AI fact identification uses your local openclaw CLI — network activity depends on your OpenClaw configuration.

User Control

  • Crontab: The user creates and manages all scheduled tasks. No script auto-installs crontab entries.
  • Configuration: All settings live in a single JSON file. The configure.sh wizard runs interactively and requires user input.
  • Opt-out: Remove the crontab entry to stop all automated truncation. The skill has no background daemon of its own.
  • Scope: Only reads/writes files under ~/.openclaw/agents/main/ and ~/.openclaw/workspace/memory/. Never touches system files, other agents' data, or other users' data.

Privacy Notice

  • AI-assisted fact identification (identify-facts-enhanced.sh) is disabled by default. It invokes the local openclaw agent CLI, which may send session content to remote LLM services depending on your OpenClaw configuration. Only enable it if you understand and accept this data flow. To enable, set "useAiIdentification": true in the config file.
  • Keyword-based identification (identify-facts.sh) is the default and runs entirely locally with no external data transmission.
  • Unattended cron execution: If you enable cron jobs, the scripts run without interactive consent. Review the scripts and test manually before enabling scheduled runs.

Related

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

General

Context Brief

Persistent context survival for OpenClaw. Writes file-based anchors to memory/anchors/ to preserve critical context across sessions. Reads MEMORY.md and dail...

Registry SourceRecently Updated
2900Profile unavailable
General

Session Context Compressor

Compress OpenClaw session context to reduce token usage and extend session lifetime. Uses NLP summarization (Sumy) to intelligently compact conversation history while preserving essential context. Triggers on mentions of session compression, token reduction, context cleanup, or when session size exceeds safe thresholds (~300KB). Use when (1) OpenClaw approaches 50% context limit, (2) Sessions are slowing down due to large context, (3) Reducing API costs from excessive token consumption, (4) Extending session lifetime without forced reboots.

Registry SourceRecently Updated
9370Profile unavailable
General

🧠 Tiered Recall - 分层回忆系统

分层回忆系统,解决上下文长度限制,保持项目延续性。默认自动加载最近7天记忆,支持手动全量回忆、自定义天数、项目回忆和主题回忆。当前版本采用 slim index,只保留文件名、行号和标题,不存摘要,避免 token 膨胀。

Registry SourceRecently Updated
2110Profile unavailable
General

Context Handoff

保存和恢复聊天上下文到本地文件。用于用户想在切换账号、清空 session、重新开会话、跨会话延续项目时,把当前会话级上下文或项目级摘要落盘并在之后恢复。也用于列出已有的会话上下文槽位或项目摘要,并按更新时间排序返回最近使用项。触发词包括:保存当前上下文、保存会话摘要、保存项目摘要、记下这次讨论、切号前保存、恢复...

Registry SourceRecently Updated
1890Profile unavailable