Memory Attention Router Skill
Turn long-term memory into a small, role-aware working-memory packet.
Do not use this skill as plain document RAG.
Do not dump raw memory lists into model context.
Route to the right memory blocks, compose a compact packet, write back new learnings, and retire stale memory when better evidence appears.
Trigger cues
Trigger immediately when the user states a durable rule or asks to preserve or replace memory, especially with phrases like:
- - from now on
- remember this
- always
- prefer
- avoid
- my rule is
- replace my previous rule
- going forward
Also trigger when a planning, execution, critique, or response step needs compact memory state rather than raw history.
Step roles
Choose the current step role before reading memory:
- - INLINECODE0
- INLINECODE1
- INLINECODE2
- INLINECODE3
Current type preferences:
- -
planner -> preference, procedure, INLINECODE7 - INLINECODE8 ->
preference, procedure, episode, INLINECODE12 - INLINECODE13 ->
reflection, preference, INLINECODE16 - INLINECODE17 ->
preference, summary, INLINECODE20
Important implication:
- -
executor should preserve durable hard constraints as well as reusable procedures
Read flow
- 1. Build a route request with:
-
goal
-
step_role
-
session_id if known
-
task_id if known
-
user_constraints
-
recent_failures
-
unresolved_questions
- 2. Run:
python3 {baseDir}/scripts/memory_router.py route --input-json '<JSON>'
- 3. Read the
packet. - Use the packet in downstream reasoning.
- Inspect
debug.selected_blocks and debug.selected_memories when you need to understand why a memory was selected.
The router uses a deterministic two-stage flow:
- 1. select the best blocks from
task_scoped, session_scoped, durable_global, and INLINECODE36 - score memories only inside the selected blocks
Write flow
Store memory after important outcomes:
INLINECODE37
Write memory when:
- - a durable user preference or rule is learned
- a reusable procedure becomes clear
- a tool result will matter later
- a failure pattern should influence future behavior
- a stable summary is worth keeping
If a new memory replaces an older one, include replaces_memory_id. The router will retire the old memory, link it forward to the replacement, and persist a retirement reason.
Reflect flow
At the end of meaningful work or after a failure cluster, create reflection and optionally procedure memory:
INLINECODE39
Use reflection for:
- - lessons
- warnings
- failure patterns
- reusable procedures derived from successful work
Refresh flow
When new evidence invalidates or replaces older memory:
INLINECODE40
Use refresh to:
- - deactivate stale memories
- mark replacements with INLINECODE41
- persist why the memory was retired with INLINECODE42
- create contradiction links when a replacement exists
Packet rules
A good packet contains:
- - INLINECODE43
- INLINECODE44
- INLINECODE45
- INLINECODE46
- INLINECODE47
- INLINECODE48
Current compactness targets:
- -
selected_memory_ids -> cap at 5 - INLINECODE50 -> cap at 4
- INLINECODE51 -> cap at 3
- INLINECODE52 -> cap at 3
- INLINECODE53 -> cap at 3
- INLINECODE54 -> cap at 5
Prefer small, high-signal packets over broad recall.
Routing rules
- - Prefer durable, reusable memory over noisy transient notes.
- Preserve hard constraints for execution steps, not only planning steps.
- Use
support edges to help validated memories win borderline ranking decisions. - Treat
contradicts edges directionally: penalize the stale target, not the newer memory asserting the contradiction. - Use
summary instead of verbose raw history when both carry the same signal. - Retire stale memory when replacement is clear; do not allow conflicting active memories to accumulate indefinitely.
Bootstrap
Initialize the database:
INLINECODE58
Default DB path behavior:
- - if
MAR_DB_PATH is set, that path is used - otherwise, when installed at
<workspace>/skills/memory-attention-router, the default is INLINECODE61
Inspect stored memories:
INLINECODE62
Inspect one memory:
INLINECODE63
File guide
See:
记忆注意力路由技能
将长期记忆转化为一个紧凑的、具备角色感知的工作记忆数据包。
请勿将此技能用作简单的文档检索增强生成(RAG)。
请勿将原始记忆列表直接转储到模型上下文中。
应路由至正确的记忆区块,组合成一个紧凑的数据包,在出现更优证据时回写新学到的内容,并淘汰过时的记忆。
触发线索
当用户陈述一条持久性规则或要求保留/替换记忆时立即触发,尤其是包含以下短语的情况:
- - 从现在开始
- 记住这一点
- 总是
- 偏好
- 避免
- 我的规则是
- 替换我之前的规则
- 今后
当规划、执行、批评或响应步骤需要紧凑的记忆状态而非原始历史记录时,也应触发。
步骤角色
在读取记忆前,先选择当前步骤的角色:
- - planner(规划者)
- executor(执行者)
- critic(批评者)
- responder(响应者)
当前类型偏好:
- - planner -> preference(偏好)、procedure(流程)、summary(摘要)
- executor -> preference(偏好)、procedure(流程)、episode(片段)、reflection(反思)
- critic -> reflection(反思)、preference(偏好)、summary(摘要)
- responder -> preference(偏好)、summary(摘要)、procedure(流程)
重要含义:
- - executor(执行者)应同时保留持久的硬约束和可复用的流程
读取流程
- 1. 构建路由请求,包含:
- goal(目标)
- step_role(步骤角色)
- session_id(会话ID,如已知)
- task_id(任务ID,如已知)
- user_constraints(用户约束)
- recent_failures(近期失败)
- unresolved_questions(未解决问题)
- 2. 运行:
python3 {baseDir}/scripts/memory_router.py route --input-json
- 3. 读取packet(数据包)。
- 在下游推理中使用该数据包。
- 当需要了解某个记忆为何被选中时,检查debug.selectedblocks和debug.selectedmemories。
路由器采用确定性的两阶段流程:
- 1. 从taskscoped(任务范围)、sessionscoped(会话范围)、durableglobal(持久全局)和recentfallback(近期回退)中选择最佳区块
- 仅对所选区块内的记忆进行评分
写入流程
在重要结果产生后存储记忆:
python3 {baseDir}/scripts/memory_router.py add --input-json
在以下情况写入记忆:
- - 学习到持久的用户偏好或规则时
- 可复用的流程变得清晰时
- 某个工具结果将在后续产生影响时
- 某种失败模式应影响未来行为时
- 某个稳定的摘要值得保留时
如果新记忆替换了旧记忆,请包含replacesmemoryid。路由器将淘汰旧记忆,将其向前链接到替换记忆,并持久化淘汰原因。
反思流程
在完成有意义的工作后或在一组失败之后,创建反思记忆,并可选择创建流程记忆:
python3 {baseDir}/scripts/memory_router.py reflect --input-json
反思用于:
- - 经验教训
- 警告
- 失败模式
- 从成功工作中衍生出的可复用流程
刷新流程
当新证据使旧记忆失效或需要替换时:
python3 {baseDir}/scripts/memory_router.py refresh --input-json
刷新用于:
- - 停用过时记忆
- 使用replacementmemoryid标记替换关系
- 使用refresh_reason持久化记忆被淘汰的原因
- 当存在替换记忆时创建矛盾链接
数据包规则
一个好的数据包包含:
- - hardconstraints(硬约束)
- relevantfacts(相关事实)
- procedurestofollow(待遵循流程)
- pitfallstoavoid(待避免陷阱)
- openquestions(开放问题)
- selectedmemory_ids(选中的记忆ID)
当前紧凑性目标:
- - selectedmemoryids -> 上限5个
- hardconstraints -> 上限4个
- relevantfacts -> 上限3个
- procedurestofollow -> 上限3个
- pitfallstoavoid -> 上限3个
- open_questions -> 上限5个
优先选择紧凑、高信号的数据包,而非宽泛的召回。
路由规则
- - 优先选择持久、可复用的记忆,而非嘈杂的临时笔记。
- 为执行步骤保留硬约束,而不仅仅是规划步骤。
- 使用support(支持)边帮助经过验证的记忆在排名决策中胜出。
- 方向性地处理contradicts(矛盾)边:惩罚过时的目标,而非断言矛盾的新记忆。
- 当摘要和冗长的原始历史携带相同信号时,使用摘要而非原始历史。
- 当替换关系明确时淘汰过时记忆;不允许冲突的活跃记忆无限累积。
初始化
初始化数据库:
python3 {baseDir}/scripts/memory_router.py init
默认数据库路径行为:
- - 如果设置了MARDBPATH,则使用该路径
- 否则,当安装在/skills/memory-attention-router时,默认路径为/.openclaw-memory-router.sqlite3
检查已存储的记忆:
python3 {baseDir}/scripts/memory_router.py list --limit 20
检查单条记忆:
python3 {baseDir}/scripts/memory_router.py inspect --memory-id
文件指南
参见: