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.
CODEBLOCK0
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 INLINECODE9 - 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
| Script | Purpose | Scheduling |
|---|
| INLINECODE16 | Trim session JSONL files | crontab, every 10 min |
| INLINECODE17 |
Keyword-based fact detection | Called by truncate script |
|
identify-facts-enhanced.sh | AI-assisted fact detection | Called by truncate script |
|
check-preferences-expiry.sh | Remove expired preferences | crontab, once daily |
|
configure.sh | Interactive setup wizard | Manual, one-time |
|
session-start-hook.sh | Load context at session start | Called by AGENTS.md |
|
session-end-hook.sh | Save context at session end | Called by AGENTS.md |
|
check-context-health.sh | Report current context status | Manual / on-demand |
Configuration
File: INLINECODE24
CODEBLOCK1
| Field | Type | Default | Description |
|---|
| maxChars | number | 40000 | Max chars to keep per session file |
| frequencyMinutes |
number | 10 | How often crontab runs truncate |
| skipActive | boolean | true | Skip sessions with .lock files |
| strategy | string | priority-first | Truncation strategy |
| useAiIdentification | boolean | false | Set true to use AI-assisted fact identification (may send content to remote LLMs) |
| priorityKeywords | string[] | (see above) | Keywords to preserve during truncation |
Troubleshooting
| Problem | Solution |
|---|
| Context still exceeded | Reduce maxChars in config |
| Memory not persisting |
Check that AGENTS.md includes session-start-hook |
| Crontab not running | Verify 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 - INLINECODE29 — keeps substantial history per session
- INLINECODE30 — 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
上下文压缩
将对话控制在限制范围内。永不丢失重要上下文。
⚡ 安装后,运行交互式设置向导生成配置文件,然后使用 crontab -e 添加建议的定时任务。请参阅下方的快速入门了解命令。
快速入门
文件位置:ClawHub 将此技能安装至 ~/.openclaw/workspace/skills/context-compression/。所有脚本均直接放置在此处。这是标准的 OpenClaw 技能安装路径——无需手动放置文件。
bash
1. 安装并配置(交互式)
bash ~/.openclaw/workspace/skills/context-compression/configure.sh
2. 验证配置文件是否存在
cat ~/.openclaw/workspace/.context-compression-config.json
3. 设置定时任务(示例:每10分钟执行一次)
/10 * ~/.openclaw/workspace/skills/context-compression/truncate-sessions-safe.sh
工作原理
会话截断(truncate-sessions-safe.sh)
- - 调度方式:系统定时任务(例如 /10 )
- 操作:读取 ~/.openclaw/agents//sessions/ 下的 .jsonl 会话文件,将每个文件裁剪至配置的大小
- 安全性:跳过存在匹配 .lock 文件(活跃会话)的文件
- 完整性:保持 JSONL 行边界完整——绝不拆分某一行
- 策略:priority-first 在裁剪前扫描重要关键词并保留匹配行
事实识别
- - 基于关键词:identify-facts.sh——扫描截断后的内容查找关键词(重要、决定、TODO、偏好、deadline、must remember 等),并将发现结果追加至 memory/YYYY-MM-DD.md
- AI 辅助:identify-facts-enhanced.sh——调用 openclaw agent --agent main --message 配合截断后的内容,以语义方式识别重要事实。仅在 PATH 中存在 openclaw CLI 时使用。
- 触发方式:truncate-sessions-safe.sh 在每次截断周期前调用上述脚本之一
偏好生命周期(check-preferences-expiry.sh)
- - 调度方式:通过定时任务每日执行一次
- 机制:读取 MEMORY.md 中标记有 @YYYY-MM-DD 的偏好条目,移除已过期的条目
- 层级:短期(1-7天)、中期(1-4周)、长期(永久)
脚本
| 脚本 | 用途 | 调度方式 |
|---|
| truncate-sessions-safe.sh | 裁剪会话 JSONL 文件 | 定时任务,每10分钟 |
| identify-facts.sh |
基于关键词的事实检测 | 由截断脚本调用 |
| identify-facts-enhanced.sh | AI 辅助的事实检测 | 由截断脚本调用 |
| check-preferences-expiry.sh | 移除过期偏好 | 定时任务,每日一次 |
| configure.sh | 交互式设置向导 | 手动,一次性 |
| session-start-hook.sh | 在会话开始时加载上下文 | 由 AGENTS.md 调用 |
| session-end-hook.sh | 在会话结束时保存上下文 | 由 AGENTS.md 调用 |
| check-context-health.sh | 报告当前上下文状态 | 手动/按需 |
配置
文件:~/.openclaw/workspace/.context-compression-config.json
json
{
version: 2.3,
maxChars: 40000,
frequencyMinutes: 10,
skipActive: true,
strategy: priority-first,
useAiIdentification: false,
priorityKeywords: [
重要, 决定, 记住, TODO, 偏好,
important, remember, must, deadline
]
}
| 字段 | 类型 | 默认值 | 描述 |
|---|
| maxChars | number | 40000 | 每个会话文件保留的最大字符数 |
| frequencyMinutes |
number | 10 | 定时任务运行截断的频率 |
| skipActive | boolean | true | 跳过存在 .lock 文件的会话 |
| strategy | string | priority-first | 截断策略 |
| useAiIdentification | boolean | false | 设为 true 以使用 AI 辅助的事实识别(可能将内容发送至远程 LLM) |
| priorityKeywords | string[] | (见上方) | 截断期间保留的关键词 |
故障排除
| 问题 | 解决方案 |
|---|
| 上下文仍然超出限制 | 在配置中减小 maxChars |
| 记忆未持久化 |
检查 AGENTS.md 是否包含 session-start-hook |
| 定时任务未运行 | 验证定时任务中的 PATH 是否包含 node/openclaw 二进制文件位置 |
安全性
数据保护
- - 不删除:截断会将裁剪后的部分写回同一文件。不会删除文件。
- 截断前备份:truncate-sessions-safe.sh 在修改前会为每个文件创建 .pre-trim 备份。成功写入后清理备份。
- 行完整性:截断仅在 JSONL 行边界处进行。绝不写入不完整的行。
- 活跃会话受保护:存在匹配 .lock(当前正在使用)的文件始终被跳过,即使文件过大。
安全默认值
所有配置值均采用保守默认值:
- - skipActive: true——绝不触碰正在运行的会话
- maxChars: 40000——每个会话保留大量历史记录
- strategy: priority-first——在裁剪前保留匹配优先级关键词的行
- 脚本无直接网络访问。可选的 AI 事实识别使用您本地的 openclaw CLI——网络活动取决于您的 OpenClaw 配置。
用户控制
- - 定时任务:用户创建并管理所有计划任务。没有脚本会自动安装定时任务条目。
- 配置:所有设置均位于单个 JSON 文件中。configure.sh 向导以交互方式运行,需要用户输入。
- 退出机制:移除定时任务条目即可停止所有自动截断。该技能本身没有后台守护进程。
- 范围:仅读取/写入 ~/.openclaw/agents/main/ 和 ~/.openclaw/workspace/memory/ 下的文件。绝不触及系统文件、其他代理的数据或其他用户的数据。
隐私声明
- - AI 辅助的事实识别(identify-facts-enhanced.sh)默认禁用。它会调用本地的 openclaw agent CLI,根据您的 OpenClaw 配置,该 CLI 可能将会话内容发送至远程 LLM 服务。仅在您理解并接受此数据流时才启用它。要启用,请在配置文件中设置 useAiIdentification: true。
- 基于关键词的识别(identify-facts.sh)是默认方式,完全在本地运行,无外部数据传输。
- 无人值守的定时任务执行:如果您启用了定时任务,脚本将在无交互同意的情况下运行。在启用计划运行前,请审查脚本并手动测试。
相关链接