内存RLM
持久项目内存 克劳德代码一个Rust MCP服务器,它透明地索引您的对话上下文和代码更改,然后在上下文因压缩而丢失时重新注入最相关的信息。
为什么存在
Claude Code会话具有有限的上下文窗口。随着对话通过请求、代码编辑、调试和规划而增长,旧的上下文会被压缩或丢弃。你失去了决定、理由和你所做的事情的线索。
ClaudeRLM通过在发生时对所有内容进行索引,并在上下文丢失时通过手术重新注入重要内容来解决这个问题。
运作原理
核心思想借鉴自 递归LLM (Zhang等人,2025),该研究表明,LLM可以通过将输入视为可编程查询的外部数据来处理任意长的输入,而不是一次性全部使用。我们的见解是:对话上下文会逐渐增长,因此可以在每次转弯时在写入时对其进行索引,而不是在已经太晚的读取时进行处理。这使得索引几乎是免费的——每次钩子调用都需要几毫秒——检索是一个快速的SQLite查询。
在实践中,克劳德代码 钩子 在每个用户提示、代码编辑、文件读取和bash命令上启动。ClaudeRLM通过FTS5全文搜索将每个事件捕获到本地SQLite数据库中。当压实即将发生时 PreCompact 钩子确保所有内容都被索引,并创建检查点摘要。压实后,a SessionStart hook查询索引,并将最相关的上下文注入到对话中——按最近度、类型重要性和文件相关性排序——这样Claude就可以从它停止的地方继续。
特性
- 被动索引 --钩子会自动开火,克劳德永远不需要决定使用它
- 全文搜索 超过对话历史记录(SQLite FTS5,BM25排名)
- 代码结构索引 通过树形图(Rust、Python、TypeScript、JavaScript、Go、C、C++)
- 后台文件监视器 用于对文件更改进行增量重新索引
- 分级上下文注入 压缩后(类型权重x新近度x文件相关性)
- 知识提炼 在会话结束时--提取决策、偏好、约定和错误修复
- LLM强化蒸馏 Haiku(约1美分/会话)或任何与OpenAI兼容的端点(Ollama免费)
- 4个MCP工具 需要时进行显式搜索:
memory_search,memory_symbols,memory_decisions,memory_files - 跨会话内存 --知识会持续存在,并在每次新课程开始时注入
安装
插件安装(推荐)
作为Claude Code插件安装,用于具有自动二进制管理的一个命令设置:
/plugin marketplace add dullfig/claude-plugins
/plugin install memory-rlm二进制文件在第一次会话开始时自动下载。不需要PATH配置。
要更新,请删除缓存的二进制文件并重新启动:
rm -f
/bin/memory-rlm # Unix
del
\bin\memory-rlm.exe # Windows手动安装
如果你不想使用插件系统,独立安装程序仍然可用:
Linux/macOS:
curl -fsSL https://raw.githubusercontent.com/dullfig/memory-rlm/main/install.sh | bashWindows(PowerShell):
irm https://raw.githubusercontent.com/dullfig/memory-rlm/main/install.ps1 | iex来源(任何使用Rust的平台):
cargo install --git https://github.com/dullfig/memory-rlm.git如果您手动安装,则需要自己配置挂钩(请参阅下面的手动挂钩设置)。使用 绝对路径 Claude Code在不继承shell PATH的情况下生成钩子子进程。
从手动安装迁移到插件
- 安装插件:
/plugin marketplace add dullfig/claude-plugins然后/plugin install memory-rlm - 拆下旧钩子
~/.claude/settings.json(引用的任何条目memory-rlm) - (可选)从中删除旧二进制文件
~/.local/bin/memory-rlm或%LOCALAPPDATA%\Programs\memory-rlm\
您现有的数据库(.claude/memory-rlm.db)保留所有索引内存。
手动吊钩设置
如果您是手动安装的,请复制 hooks.example.json 进入你的项目 .claude/settings.json (或合并到现有设置中)。
重要提示: 使用绝对路径 memory-rlm 在钩子命令中。替换 /path/to/memory-rlm 根据实际安装位置(例如。, ~/.local/bin/memory-rlm 在Linux/macOS上, C:/Users/YOU/AppData/Local/Programs/memory-rlm/memory-rlm.exe 在Windows上):
{
"hooks": {
"UserPromptSubmit": [
{
"hooks": [{ "type": "command", "command": "/path/to/memory-rlm index-prompt", "timeout": 5 }]
}
],
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [{ "type": "command", "command": "/path/to/memory-rlm index-edit", "timeout": 5 }]
},
{
"matcher": "Read",
"hooks": [{ "type": "command", "command": "/path/to/memory-rlm index-read", "timeout": 2 }]
},
{
"matcher": "Bash",
"hooks": [{ "type": "command", "command": "/path/to/memory-rlm index-bash", "timeout": 2 }]
}
],
"PreCompact": [
{
"hooks": [{ "type": "command", "command": "/path/to/memory-rlm pre-compact", "timeout": 10 }]
}
],
"SessionStart": [
{
"hooks": [{ "type": "command", "command": "/path/to/memory-rlm session-start", "timeout": 10 }]
}
],
"SessionEnd": [
{
"hooks": [{ "type": "command", "command": "/path/to/memory-rlm session-end", "timeout": 30 }]
}
]
}
}MCP服务器(可选)
添加到显式搜索工具的Claude Code MCP设置中:
{
"mcpServers": {
"memory-rlm": {
"command": "memory-rlm",
"args": ["serve"]
}
}
}插件用户通过插件的 .mcp.json.
LLM蒸馏(可选)
为了在会话结束时提取更高质量的知识,请将API密钥添加到配置文件中。
项目级别 (.claude/memory-rlm.toml 在您的项目中):
[llm]
provider = "anthropic"
api_key = "sk-ant-..."全球 (~/.config/memory-rlm/config.toml 在Linux/macOS上, %APPDATA%\memory-rlm\config.toml 在Windows上):
[llm]
provider = "anthropic"
api_key = "sk-ant-..."
model = "claude-haiku-4-5-20251001" # default, cheapest option通过Ollama获取本地模型(免费):
[llm]
provider = "ollama"
model = "llama3"项目级配置优先于全局配置。环境变量(CONTEXTMEM_LLM_*)也支持作为回退。
在没有配置任何LLM的情况下,ClaudeRLM回归到启发式模式匹配进行蒸馏,这仍然适用于常见模式(技术选择、“总是/从不”偏好、构建工具、测试框架)。
| 配置键 | 环境变量回退 | 默认值 | 描述 |
|---|---|---|---|
llm.api_key | CONTEXTMEM_LLM_API_KEY | *(无)* | API密钥(云需要,Ollama可选) |
llm.provider | CONTEXTMEM_LLM_PROVIDER | anthropic | anthropic, openai,或 ollama |
llm.model | CONTEXTMEM_LLM_MODEL | claude-haiku-4-5-20251001 | 型号名称 |
llm.base_url | CONTEXTMEM_LLM_BASE_URL | *(提供程序默认值)* | 自定义端点URL |
什么会被索引
| 事件 | 捕获了什么 |
|---|---|
| 用户提示 | 完整的请求文本 |
| 编辑/写入 | 文件路径、旧/新内容、更改描述 |
| 读取 | 文件路径 |
| Bash | 命令和输出(截断为2KB) |
| PreCompact | 迄今为止所有活动的检查点摘要 |
| 课程结束 | 课程总结+提炼知识 |
注射什么
会话开始时: 项目结构、最近的会议总结、提炼的知识(决策、惯例、偏好)。
压实后: 检查点摘要、会话中的所有用户请求、活动文件列表,然后排名最高的剩余文件将达到16K字符的预算。
禁用/启用(终止开关)
如果出现问题,ClaudeRLM会干扰您的会话,请从任何终端禁用它:
memory-rlm disable # All hooks silently exit, Claude Code works normally
memory-rlm enable # Re-enable hooks
memory-rlm status # Shows DISABLED/enabled state这将创建/删除 ~/.memory-rlm-disabled 标志文件。不需要Claude会话——只需打开一个终端并键入命令。
CLI命令
memory-rlm serve # Start MCP server (default)
memory-rlm status # Show index statistics
memory-rlm --version # Show version
memory-rlm disable # Disable all hooks (emergency kill switch)
memory-rlm enable # Re-enable hooks
memory-rlm index-prompt # Hook: index user prompt (stdin)
memory-rlm index-edit # Hook: index code edit (stdin)
memory-rlm index-read # Hook: index file read (stdin)
memory-rlm index-bash # Hook: index bash command (stdin)
memory-rlm pre-compact # Hook: pre-compaction checkpoint
memory-rlm session-start # Hook: inject context
memory-rlm session-end # Hook: distill + summarize数据存储
所有数据都存储在本地 .claude/memory-rlm.db (SQLite)在您的项目目录中。除非您使用云API配置LLM蒸馏,否则任何东西都不会离开您的机器。
支持的语言(树保姆)
Rust、Python、TypeScript、TSX、JavaScript、Go、C、C++
许可证
商业来源许可证1.1(BSL 1.1)。免费用于所有用途,包括生产,但作为竞争性商业托管服务提供除外。2029年2月14日转换为Apache 2.0。看 许可证 了解详情。
