← 返回
未分类 中文

Memory Router

OpenClaw skill that auto-tiers bloated MEMORY.md, generates smart per-session manifests, and routes only what matters — 70-85% context reduction with zero de...
OpenClaw 技能,自动分层臃肿的 MEMORY.md,生成智能的每次会话清单,仅路由关键信息——上下文缩减 70-85%,零性能下降。
jlacroix82 jlacroix82 来源
未分类 clawhub v2.4.1 5 版本 100000 Key: 无需
★ 3
Stars
📥 418
下载
💾 1
安装
5
版本
#latest

概述

MemoryRouter ⚡

Never lose context. Never forget decisions. Never repeat mistakes.

The memory problem is the single biggest reason agents feel dumb over time. Not the models. Not the prompts. Memory management.

MemoryRouter fixes it with one tool, zero dependencies, zero setup.

The Problem — In 30 Seconds

Agent memory files grow unbounded. After a week, you've got thousands of lines across dozens of files. Every session loads everything — even when the user asks "what's the weather?"

That's tokens burned on irrelevant memories, every interaction, forever.

Then compaction kicks in and details vanish.

The bottleneck isn't the model. It's what the model gets to see.

Why Memory Fails

Failure ModeCauseFix
-------------------------
Forgets everythingLoads irrelevant files, context window fills upSmart manifest — load only what matters
Repeats mistakesLessons not captured or loadedEntity index + audit for conflicts
Repeats workNo session state persistenceWAL protocol — write state before responding
Slow responsesLoads 50+ files when only 3 matterToken budgeting — cap context window
Duplicates everywhereNo automated cleanup--audit finds high-similarity pairs

The Architecture

┌──────────────────────────────────────────────────────┐
│              MEMORYROUTER ⚡                          │
├──────────────────────────────────────────────────────┤
│                                                      │
│  ┌──────────────┐    ┌──────────────┐               │
│  │  AUTO-TIER   │    │  MANIFEST    │               │
│  │  MEMORY.md   │ →  │  GENERATOR   │               │
│  │              │    │              │               │
│  │ Core (always │    │ Required:    │               │
│  │ loaded)      │    │ MEMORY.md    │               │
│  │ + Archive    │    │ Recent logs  │               │
│  │ (on demand)  │    │ Boosted:     │               │
│  └──────────────┘    │ entity files │               │
│                      └──────┬───────┘               │
│                             │                        │
│              ┌──────────────▼──────────────┐         │
│              │  ENTITY RESOLUTION          │         │
│              │  "alice" → preferences.md,         │         │
│              │  notes.md, decisions.md│         │
│              └──────────────┬──────────────┘         │
│                             │                        │
│              ┌──────────────▼──────────────┐         │
│              │  TOKEN BUDGET FILTER        │         │
│              │  20K budget → 7 files       │         │
│              │  100K budget → 16 files     │         │
│              └──────────────┬──────────────┘         │
│                             │                        │
│              ┌──────────────▼──────────────┐         │
│              │  AGENT LOADS ONLY WHAT      │         │
│              │  MATTERS → 70-85% REDUCTION │         │
│              └─────────────────────────────┘         │
└──────────────────────────────────────────────────────┘

Quick Start

Fix a bloated MEMORY.md (requires confirmation)

node skills/memory-router/memory-router.js --tier --confirm

Output:

[memory-router] MEMORY.md: 7809 lines, 309254 chars
[memory-router] ✅ Pre-tier backup: memory/backups/MEMORY-backup-2026-05-23-1234567890.md (7809 lines saved)
[memory-router] Tiered: 202 core sections, 1002 archived
[memory-router] MEMORY.md reduced from 7809 lines to 2222 lines

Preview tiering without making changes

node skills/memory-router/memory-router.js --tier --dry-run

Output:

[memory-router] MEMORY.md: 7809 lines, 309254 chars
[memory-router] ── DRY RUN ──
[memory-router] Would archive: memory/active/MEMORY-archive-2026-05-23.md
[memory-router] Would reduce MEMORY.md from 7809 lines to ~2222 lines
[memory-router] Core sections: 202, Archive sections: 1002
[memory-router] ✅ No files were modified.

Restore MEMORY.md from latest backup

node skills/memory-router/memory-router.js --restore --force

⚠️ Destructive — overwrites MEMORY.md. Requires --force flag. Extracts only the original content from the backup (metadata headers are stripped). Always creates a backup of the current MEMORY.md before overwriting.

Generate a smart manifest in one command

node skills/memory-router/memory-router.js --compact

Creates memory/memory-manifest.json — the agent's shopping list of what to load.

With entity boosting

node skills/memory-router/memory-router.js --compact --query "alice"

Files linked to "alice" get priority. No AI, no embeddings — just fast entity resolution.

With a token budget

node skills/memory-router/memory-router.js --compact --budget 20000

Only loads files that fit within 20K tokens. Tight budgets load core only. Generous budgets load archive on demand.

Other commands

node skills/memory-router/memory-router.js --audit      # Find duplicates & conflicts
node skills/memory-router/memory-router.js --status      # Health overview
node skills/memory-router/memory-router.js --entity add alice person preferences.md

How It Works

The Core Insight

Memory management is the bottleneck, not model capability. Every agent system hits the same wall — context window fills up, everything loads, irrelevant memories dilute the signal.

MemoryRouter takes a routing approach rather than a compression approach:

  1. Auto-tiering splits MEMORY.md into core sections (identity, preferences, boundaries — always loaded) and archive sections (everything else, loaded on demand)
  2. Manifest generation creates a per-session file list: which files to load, which to skip, which to boost
  3. Entity-aware boosting links people, projects, and systems to files — search for "alice" → load preferences.md first
  4. Token budgeting caps how many files load based on available context window

The Flow

User query → --compact --query "alice"
              ↓
         Manifest generated
              ↓
         Load required files (MEMORY.md, recent daily logs)
              ↓
         Boost entity-matched files (preferences.md, notes.md)
              ↓
         Agent loads only what matters → 70-85% context reduction

The 4 Engines

⚡ Engine 1: Auto-Tier (--tier)

When MEMORY.md exceeds configurable thresholds (default: 500 lines or 25KB), splits into:

  • Core file — Identity, preferences, relationships, projects, patterns, boundaries
  • Archive files — Everything else, stored with timestamps in memory/active/

Safety features (v2):

  • Pre-tier backup — Every --tier creates a full backup in memory/backups/ before modifying MEMORY.md
  • --dry-run — Preview what tiering would do without making changes
  • --confirm — Required for destructive writes (no auto-tier without explicit confirmation)
  • Minimum core size — Aborts if core sections would fall below minCoreLines/minCoreChars thresholds
  • --restore --force — Restore MEMORY.md from the latest backup (requires --force flag)

Sections are classified by header keywords (identity, preferences, etc.) or by content heuristics.

📋 Engine 2: Manifest Generator (--compact)

Creates memory/memory-manifest.json with a file list:

{
  "generated": "2026-05-21",
  "files": [
    { "path": "MEMORY.md", "tier": "core", "required": true, "size": 50346 },
    { "path": "memory/2026-05-21.md", "tier": "recent", "required": true, "size": 2034 },
    { "path": "self-improving/memory.md", "tier": "domain", "required": false, "size": 670 }
  ]
}

Options:

FlagDescription
-------------------
--query "text"Entity-aware boosting — files linked to matching entities get priority
--budget NToken budget — only loads files that fit within N tokens

🔍 Engine 3: Audit Scanner (--audit)

Scans all memory files for:

  • High-similarity pairs — Files with >70% text overlap (potential duplicates)
  • Revision keywords — "revised", "updated", "changed", "no longer", "actually", "correction" (facts that may have been superseded)

Output: memory/memory-audit-report.md

🏥 Engine 4: Health Monitor (--status)

Quick snapshot of MEMORY.md line/char counts, file counts per directory, archive count, manifest status, entity index size, and WAL state.

Additional Tools

Entity Index (--entity)

CommandDescription
----------------------
--entity add Add entity (e.g., alice person preferences.md)
--entity listList all entities
--entity search Search entities (direct and fuzzy match)

WAL Protocol (--wal)

CommandDescription
----------------------
--wal initInitialize SESSION-STATE.md with template
--wal getShow current session state
--wal update
--content ""
Update a section

Configuration

Edit config.json to customize behavior:

{
  "thresholds": {
    "memoryMdMaxLines": 500,
    "memoryMdMaxChars": 25000,
    "tierArchiveMinAgeDays": 3,
    "auditMaxFiles": 50
  },
  "tiering": {
    "archiveDir": "memory/active",
    "retentionDays": 90,
    "keepInMemory": [
      "identity", "preferences", "relationships",
      "projects", "patterns", "boundaries", "key_facts"
    ]
  },
  "manifest": {
    "generateOnTier": true,
    "manifestPath": "memory/memory-manifest.json"
  },
  "audit": {
    "reportPath": "memory/memory-audit-report.md",
    "duplicateThreshold": 0.7,
    "conflictKeywords": [
      "revised", "updated", "changed", "no longer",
      "actually", "correction", "mistake"
    ]
  }
}
SettingDefaultDescription
-------------------------------
memoryMdMaxLines500Auto-tier trigger (lines)
memoryMdMaxChars25000Auto-tier trigger (characters)
tierArchiveMinAgeDays3Minimum age before archiving
retentionDays90⚠️ Archive retention period. Files older than this are candidates for deletion (irreversible). Set to 365+ until confident.
keepInMemorysee aboveHeaders/keywords that stay in core
generateOnTiertrueAuto-generate manifest after tiering
duplicateThreshold0.7Similarity score to flag as duplicate
conflictKeywordssee aboveWords that signal fact revision
backupDirmemory/backupsDirectory for pre-tier backups
minCoreLines20Abort if core sections below this
minCoreChars1000Abort if core chars below this

Agent Memory Loading Protocol

When the agent wakes up, use the manifest instead of loading all memory files:

  1. Read memory/memory-manifest.json
  2. Load all required: true files
  3. For required: false files, use memory_search to check relevance
  4. Load only the top 3-5 most relevant optional files

Result: 70–85% context reduction — load what matters, skip the rest.

Heartbeat Integration

Add to your HEARTBEAT.md:

### ⚡ MemoryRouter (SAFE commands only)

- Run `node skills/memory-router/memory-router.js --compact` to update manifest ✅ safe
- Run `node skills/memory-router/memory-router.js --audit` to check for issues ✅ safe
- Run `node skills/memory-router/memory-router.js --status` for health overview ✅ safe
- ⚠️ Do NOT auto-run `--tier` or `--restore` during heartbeats
- `--tier --dry-run` is side-effect free (no files created)
- For tiering: run `--tier --dry-run` manually, review output, then `--tier --confirm`

Performance

MetricResult
----------------
Tiering speed (8K lines)25ms
Tiering speed (15K lines)30ms
Token reduction96% (7,809 → 222 lines)
File count reduction53 → 15 files
Memory footprint~2MB (Node.js runtime)

Comparison

ApproachTokens SavedSetup EffortMaintenancePrivacy
----------------------------------------------------------
Raw file injection0%NoneManual
MemoryRouter70–85%NoneAutomated
Obsidian vault40–60%HighMedium⚠️ Cloud
Vector DB (ChromaDB)70–85%Very HighHigh
mem070–85%HighMedium⚠️ Cloud

MemoryRouter gives you the best token savings of the vector DB approach with zero setup effort.

Entity Naming

Pick one canonical name per entity and reuse it consistently:

  • Use full descriptive names: "machine learning" not "ML", "JavaScript" not "JS"
  • Same string after lowercasing = same entity. Different strings = different entities
  • Call --entity search periodically to verify your index

Examples:

# ✅ Good — consistent, descriptive
node memory-router.js --entity add alice person preferences.md
node memory-router.js --entity add openclaw system AGENTS.md

# ❌ Bad — inconsistent, ambiguous
node memory-router.js --entity add alice person preferences.md
node memory-router.js --entity add Alice person notes.md
node memory-router.js --entity add JS person docs.md

Manifest Format

The manifest JSON tells the agent which files to load:

{
  "generated": "2026-05-21",
  "version": 2,
  "query": "memory management",
  "budget": 20000,
  "files": [
    {
      "path": "MEMORY.md",
      "tier": "core",
      "required": true,
      "size": 50346
    },
    {
      "path": "memory/2026-05-21.md",
      "tier": "recent",
      "ageDays": 0,
      "required": true,
      "size": 2034
    },
    {
      "path": "self-improving/memory.md",
      "tier": "domain",
      "required": false,
      "size": 670
    }
  ]
}
FieldTypeDescription
--------------------------
tierstringcore, recent, domain, or archive
requiredboolAlways load this file
sizeintFile size in bytes
boostedboolEntity match — higher priority
entityMatchstringEntity name that matched
loadboolIncluded under budget mode
budgetUsedintTotal tokens loaded (budget mode)
budgetEfficiencystringPercentage remaining (budget mode)

Troubleshooting

ProblemFix
--------------
"No MEMORY.md found"Create one: echo "# MEMORY.md" > MEMORY.md
"Memory directory not found"Create it: mkdir -p memory
Tiering not workingCheck thresholds — if under 500 lines and 25KB, nothing happens (by design)
Manifest shows wrong filesRun --compact again — it regenerates fresh each time
Entity search returns nothingAdd entities first: --entity add
Budget too smallCore files always load. Budget only controls optional files.

⚠️ SAFETY — READ BEFORE USING

Destructive Operations

--tier and --restore permanently modify user memory files.

  • --tier rewrites MEMORY.md, moving sections to archive files
  • --restore overwrites MEMORY.md with backup content
  • Both require explicit confirmation (--confirm / --force) — they will refuse to run without it
  • Always run --tier --dry-run first to preview what will change
  • Pre-tier backups are created only when actually tiering (not in dry-run mode)

Archive Retention — Data Loss Risk

The retentionDays config (default: 90) marks archived files as candidates for deletion after that period. This is irreversible — once deleted, archived memory sections cannot be recovered.

Before enabling retention:

  1. Back up your entire memory/ directory
  2. Set retentionDays to a large value (365+) until you're confident
  3. Monitor what gets deleted before reducing the value

Safe vs Unsafe Commands

CommandSafe in automation?Modifies files?
----------------------------------------------
--compact✅ YesWrites memory-manifest.json (generated output)
--audit✅ YesWrites memory-audit-report.md (generated report)
--status✅ YesNo file writes
--entity add⚠️ Yes — but writes entity indexYes (persists entity → file mapping)
--entity list/search✅ YesNo file writes
--wal init⚠️ Yes — but creates SESSION-STATE.mdYes (creates new file)
--wal get✅ YesNo file writes
--wal update⚠️ Yes — but modifies SESSION-STATE.mdYes (updates session state)
--tier --dry-run✅ YesNo file writes (side-effect free)
--tier --confirm❌ No — rewrites MEMORY.md + creates backupYes
--restore --force❌ No — overwrites MEMORY.mdYes

--compact, --audit, --status, --entity list/search, --wal get, and --tier --dry-run are safe for unattended/heartbeat use.

--entity add, --wal init, and --wal update write persistent files — review before automating.

--tier --confirm and --restore --force are destructive — never automate.

Security

Built with defense-in-depth:

  • Path validation — rejects file paths outside workspace root
  • Regex escaping — prevents injection in WAL section names
  • Symlink protection — refuses to read/write symlinks
  • Size limits — 10MB max file size
  • Entity name validation — alphanumeric + hyphens/underscores only
  • Content sanitization — prevents header injection in WAL updates
  • Audit keyword validation — rejects regex metacharacters in conflict keywords

Design Principles

  1. Safe by default — Destructive operations require explicit flags (--confirm, --force)
  2. No external dependencies — Pure Node.js, no npm packages
  3. Configurable — Thresholds, keywords, retention policies
  4. Transparent — Generates reports, nothing is silently deleted
  5. Reversible — Archives use dated filenames, original content preserved
  6. Privacy-first — All local, no cloud APIs

版本历史

共 5 个版本

  • v2.4.1 当前
    2026-06-14 23:25 安全 安全
  • v2.4.0
    2026-06-14 19:39
  • v2.2.0
    2026-06-06 06:49 安全 安全
  • v2.1.0
    2026-05-25 17:06 安全 安全
  • v1.0.3
    2026-05-23 16:36 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

data-analysis

Clip Media

jlacroix82
使用yt-dlp从社交媒体URL提取媒体(视频、照片、GIF、音频)。支持1872+平台,包括YouTube、TikTok、Instagram、X/Twitter、Facebook等。
★ 2 📥 510
ai-agent

self-improving agent

pskoett
捕获经验教训、错误及修正内容,以实现持续改进。适用于以下场景:(1)命令或操作意外失败;(2)用户纠正Claude(如“不,那不对……”“实际上……”);(3)用户请求的功能不存在;(4)外部API或工具出现故障;(5)Claude发现自身
★ 4,099 📥 827,480
ai-agent

Self-Improving + Proactive Agent

ivangdavila
自我反思+自我批评+自我学习+自组织记忆。智能体评估自身工作、发现错误并持续改进。
★ 1,396 📥 322,692