Memory Pruner:优化并压缩 AI 智能体上下文 - Openclaw Skills

作者:互联网

2026-03-29

AI教程

什么是 Memory Pruner?

Memory Pruner 是开发长期运行智能体的必备工具。它解决了日志和状态文件最终耗尽磁盘空间或导致智能体上下文窗口过载的常见内存无限增长问题。通过为日志实现循环缓冲区以及为状态实现基于重要性的保留机制,它能确保您的智能体保持高效且具成本效益。使用此技能可让您的 Openclaw Skills 保持有序,并防止因读取数万个 Token 的陈旧内存数据而导致的启动延迟。

下载入口:https://github.com/openclaw/skills/tree/main/skills/trypto1019/arc-memory-pruner

安装与下载

1. ClawHub CLI

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

npx clawhub@latest install arc-memory-pruner

2. 手动安装

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

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

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

3. 提示词安装

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

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

Memory Pruner 应用场景

  • 防止智能体状态文件在启动时超过 Token 限制。
  • 为长期运行的自主进程实现日志轮转。
  • 清理早于特定日期的陈旧历史数据。
  • 监控不同智能体实例的内存文件大小和增长率。
  • 使用模拟运行(dry-run)模式安全预览内存清理操作。
Memory Pruner 工作原理
  1. 用户或系统通过 Python 触发修剪命令,针对特定的内存文件或日志目录。
  2. 该工具根据指定的约束条件(如最大行数、文件数量或日期截止点)分析文件内容。
  3. 对于日志,它管理一个循环缓冲区,在保留最近历史记录的同时删除最旧的文件。
  4. 对于状态文件,它通过删除符合修剪标准的章节来压缩 Markdown 或文本内容。
  5. 它输出执行操作的摘要,确保您的 Openclaw Skills 维持轻量化的数据占用。

Memory Pruner 配置指南

确保您的系统(macOS 或 Linux)已安装 python3。

# 检查 python 是否可用
python3 --version

# 运行 stats 命令查看各智能体的当前内存使用情况
python3 scripts/memory_pruner.py stats --dir ~/

Memory Pruner 数据架构与分类体系

Memory Pruner 对标准文本、Markdown 和日志文件进行操作。它不需要复杂的数据库,而是通过文件系统属性和行解析来跟踪元数据。

目标类型 保留策略 跟踪的元数据
状态文件 (.md) 最大行数或基于日期的压缩 行索引、日期模式
日志目录 循环缓冲区(最后 N 个文件) 基于时间戳的排序、文件数量
全局统计 累积大小监控 增长率、以字节为单位的磁盘使用量
name: memory-pruner
description: Automatically prune and compact agent memory files to prevent unbounded growth. Circular buffer for logs, importance-based retention for state, and configurable size limits.
user-invocable: true
metadata: {"openclaw": {"emoji": "??", "os": ["darwin", "linux"], "requires": {"bins": ["python3"]}}}

Memory Pruner

Keep your agent's memory lean. Automatically prune logs, compact state files, and enforce size limits so your agent never runs out of disk or context window.

Why This Exists

Agents accumulate memory files over time. Logs grow unbounded. State files collect stale entries. Eventually your boot-up reads 50K tokens of memory and half of it is outdated. Memory Pruner enforces limits and keeps only what matters.

Commands

Prune a memory file (keep last N lines)

python3 {baseDir}/scripts/memory_pruner.py prune --file ~/wake-state.md --max-lines 200

Prune a log directory (circular buffer, keep last N files)

python3 {baseDir}/scripts/memory_pruner.py prune-logs --dir ~/agents/logs/ --keep 7

Compact a state file (remove sections matching a pattern)

python3 {baseDir}/scripts/memory_pruner.py compact --file ~/wake-state.md --remove-before "2026-02-14"

Check memory sizes

python3 {baseDir}/scripts/memory_pruner.py stats --dir ~/

Dry run (show what would be pruned)

python3 {baseDir}/scripts/memory_pruner.py prune --file ~/wake-state.md --max-lines 200 --dry-run

Features

  • Line-based pruning: Keep last N lines of any file
  • Log rotation: Circular buffer for log directories (keep last N files, delete oldest)
  • Date-based compaction: Remove entries older than a cutoff date
  • Size limits: Enforce max file sizes in bytes
  • Dry run mode: Preview changes before applying
  • Stats: Overview of memory file sizes and growth rates

相关推荐