Memory Compress
Your agent's memory is drowning in daily logs. This fixes it.
I built this because my agent's MEMORY.md hit 15,000 words and daily logs kept piling up at 2,500 words/day. Needed a way to compress without losing the important stuff.
The key insight came from classical Chinese writing — ancient scholars compressed entire dynasties into single sentences. Same principle here:
- 1. Strip redundancy — mention it once, not three times
- Keep only turning points — what changed, not what continued
- Let structure carry meaning — bullet hierarchy > verbose paragraphs
- Drop the process, keep the result — "failed 3 times, then X worked" > 3 failure descriptions
Result: 4-8x compression ratio, zero loss on key events. Zero dependencies.
CODEBLOCK0
Architecture
CODEBLOCK1
Quick Start
CODEBLOCK2
How It Works
Smart Hybrid Extraction
Most memory tools choke on unstructured logs. This one doesn't.
Step 1 — Keyword Matching: Scans headers for 40+ patterns across Chinese & English:
- - Events: 重大进展, breakthrough, milestone, decision…
- Lessons: 教训, 反思, insight, takeaway…
- Growth: 进化, evolution, improvement…
- Action items: 待办, 🔴, 🟡, todo, next step…
Step 2 — Fallback Extraction: When no keywords match (e.g. time-based headers like ## 08:44 Standup), automatically extracts all sections with top items. No data loss, ever.
Step 3 — Hybrid Mode: For multi-day files, matched sections use keyword extraction while unmatched sections use fallback. Both coexist. Nothing gets dropped.
The Classical Chinese Compression Philosophy
This isn't just "summarize shorter." It's a deliberate compression methodology:
| Principle | What it means | Example |
|---|
| 去重复 (Strip redundancy) | Mentioned once = enough | Don't repeat "WebSocket reconnection" across 3 sections |
| 留转折 (Keep turning points) |
Only what
changed | "Switched from nginx to direct Node.js WSS" > 5 paragraphs of debugging |
| 去过程 (Drop process) | Result > journey | "3 failures → fixed with X" > 3 failure descriptions |
| 留白 (Leave blanks) | Let reader infer | Bullet hierarchy implies relationship |
| 形式即内容 (Form is content) | Structure carries meaning | Nested lists > flat paragraphs |
Output Format
CODEBLOCK3
Batch Compression
CODEBLOCK4
Heartbeat Integration
Add to your maintenance cycle:
CODEBLOCK5
Edge Cases
| Scenario | Behavior |
|---|
| Empty file | Graceful skip |
| BOM-encoded |
Auto-detected, stripped |
| Non-UTF-8 | Warning + continues |
| Missing output dir | Auto-created |
| No markdown structure | Friendly message |
| Multi-day concatenated | Hybrid strategy, all days preserved |
CLI
CODEBLOCK6
记忆压缩
你的智能体的记忆正被日常日志淹没。这个工具能解决这个问题。
我构建这个工具是因为我的智能体的 MEMORY.md 达到了 15,000 字,而日常日志以每天 2,500 字的速度不断堆积。需要一种方法在不丢失重要内容的前提下进行压缩。
关键灵感来自古典中文写作——古代学者将整个朝代压缩成一句话。同样的原理:
- 1. 去除冗余——提到一次就够了,不要重复三次
- 只保留转折点——什么改变了,而不是什么继续了
- 让结构承载意义——层级列表优于冗长段落
- 去掉过程,保留结果——失败 3 次,然后 X 成功了优于 3 次失败描述
结果:4-8 倍压缩比,关键事件零损失。零依赖。
压缩前:2,500 字的原始日常日志
压缩后:400 字的结构化洞察
架构
┌────────────────────────────────────────────────┐
│ 三层记忆系统 │
├────────────────────────────────────────────────┤
│ │
│ 第一层:身份 (SOUL.md) │
│ 超压缩,稳定 │
│ 你是谁。什么重要。 │
│ │
│ 第二层:精选记忆 (MEMORY.md) ◄──┐ │
│ 4:1 压缩摘要 │ │
│ 关键事件 + 经验教训 + 待办事项 │ │
│ │ │
│ 第三层:原始日志 (memory/YYYY-MM-DD.md) │ │
│ 完整细节,所有内容 ───┘ │
│ 约 2,500 字/天 │
│ │
│ memory-compress:第三层 ──► 第二层 │
└────────────────────────────────────────────────┘
快速开始
bash
压缩今天的日志
node scripts/memory-compress.js memory/2026-03-14.md
指定输出文件
node scripts/memory-compress.js memory/2026-03-14.md /tmp/compressed.md
追加到长期记忆
node scripts/memory-compress.js memory/2026-03-14.md /tmp/today.md
cat /tmp/today.md >> MEMORY.md
工作原理
智能混合提取
大多数记忆工具在处理非结构化日志时都会卡住。这个不会。
步骤 1 — 关键词匹配:扫描标题中的 40+ 种中英文模式:
- - 事件:重大进展, breakthrough, milestone, decision…
- 教训:教训, 反思, insight, takeaway…
- 成长:进化, evolution, improvement…
- 行动项:待办, 🔴, 🟡, todo, next step…
步骤 2 — 回退提取:当没有匹配到关键词时(例如基于时间的标题如 ## 08:44 Standup),自动提取所有部分及其顶部项目。永远不会丢失数据。
步骤 3 — 混合模式:对于多日文件,匹配到的部分使用关键词提取,未匹配的部分使用回退提取。两者共存。没有任何内容被丢弃。
古典中文压缩哲学
这不只是更短的摘要。这是一种有意的压缩方法论:
| 原则 | 含义 | 示例 |
|---|
| 去重复 | 提到一次就够了 | 不要在三个部分重复WebSocket 重连 |
| 留转折 |
只保留改变了的内容 | 从 nginx 切换到直接 Node.js WSS优于 5 段调试过程 |
| 去过程 | 结果重于过程 | 3 次失败 → 用 X 修复优于 3 次失败描述 |
| 留白 | 让读者自行推断 | 层级列表暗示了关系 |
| 形式即内容 | 结构承载意义 | 嵌套列表优于扁平段落 |
输出格式
markdown
2026-03-14 关键经历
关键事件
- 详情 1
- 详情 2
核心教训
待办/剩余事项
批量压缩
bash
压缩最近 7 天
for file in memory/2026-03-{08..14}.md; do
[ -f $file ] && node scripts/memory-compress.js $file /tmp/$(basename $file)
done
心跳集成
添加到你的维护周期中:
markdown
记忆维护(每 2-3 天)
- 1. 运行:node scripts/memory-compress.js memory/YYYY-MM-DD.md /tmp/compressed.md
- 检查准确性
- 追加:cat /tmp/compressed.md >> MEMORY.md
- 时间戳:date +%s > .last-memory-maintenance
边界情况
自动检测并去除 |
| 非 UTF-8 | 警告并继续 |
| 缺少输出目录 | 自动创建 |
| 无 Markdown 结构 | 友好提示 |
| 多日拼接 | 混合策略,所有日期都保留 |
CLI
node scripts/memory-compress.js <日志文件> [输出文件]
node scripts/memory-compress.js --help