dream

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Mem0 Dream — Memory Consolidation

Mem0 Dream — 记忆整合

This skill performs a memory consolidation pass: it fetches all project memories, identifies near-duplicates, flags contradictions, and prunes stale entries based on configured retention policies. All proposed changes are shown as a diff for user approval before anything is modified.
IMPORTANT: Execute steps strictly in order (1 → 2 → 3 → 4 → 5 → 6). Each step depends on the previous one. Do NOT run steps in parallel or skip ahead.
该技能执行记忆整合流程:获取所有项目记忆,识别近似重复内容,标记矛盾条目,并根据配置的保留策略清理过期记录。在进行任何修改前,所有拟议变更都会以差异对比形式展示,供用户审批。
重要提示:严格按顺序执行步骤(1 → 2 → 3 → 4 → 5 → 6)。每个步骤依赖前一步骤。请勿并行运行或跳过步骤。

Step 1: Load Retention Policies

步骤1:加载保留策略

Determine the active retention policy by running the parser script. Use the appropriate
PLUGIN_ROOT
variable for the current platform (
${CLAUDE_PLUGIN_ROOT}
,
${CODEX_PLUGIN_ROOT}
, or
${CURSOR_PLUGIN_ROOT}
):
bash
python3 "<PLUGIN_ROOT>/scripts/parse_mem0_config.py" "<cwd>"
Parse the JSON output (a dict of
category → days | null
). If the script fails or returns
{}
, fall back to these built-in defaults:
metadata.type
Default retention
session_state
90 days
compact_summary
90 days
all othersno pruning
Store the resolved policies for use in Step 3.

通过运行解析脚本确定当前生效的保留策略。根据当前平台使用对应的
PLUGIN_ROOT
变量(
${CLAUDE_PLUGIN_ROOT}
${CODEX_PLUGIN_ROOT}
${CURSOR_PLUGIN_ROOT}
):
bash
python3 "<PLUGIN_ROOT>/scripts/parse_mem0_config.py" "<cwd>"
解析JSON输出(格式为
分类 → 天数 | null
的字典)。如果脚本执行失败或返回
{}
,则使用以下内置默认值:
metadata.type
默认保留时长
session_state
90天
compact_summary
90天
其他所有类型不清理
保存解析后的策略,供步骤3使用。

Step 2: Fetch ALL Project Memories

步骤2:获取所有项目记忆

Call
get_memories
to retrieve every memory for the active project:
python
get_memories(
    filters={"AND": [{"user_id": "<active_user_id>"}, {"app_id": "<active_project_id>"}]},
    page_size=200,
)
If the response indicates more pages exist, paginate until all memories are fetched. Collect the full list before proceeding. If zero memories are found, print:
No memories found for project <project_id>. Nothing to consolidate.
…and stop.

调用
get_memories
接口获取当前项目的全部记忆:
python
get_memories(
    filters={"AND": [{"user_id": "<active_user_id>"}, {"app_id": "<active_project_id>"}]},
    page_size=200,
)
如果响应显示存在更多分页内容,需分页获取直到所有记忆都被收集。在继续下一步前需收集完整列表。如果未找到任何记忆,打印:
未找到项目<project_id>的记忆。无需进行整合。
并终止流程。

Step 3: Analyze — Find Issues

步骤3:分析——查找问题

Work entirely in-memory; do not modify anything yet.
Group memories by
metadata.type
(use
"unknown"
when the field is absent). For each group, identify the following:
全程在内存中操作;暂不修改任何内容。
metadata.type
对记忆进行分组(当该字段不存在时使用
"unknown"
)。针对每个分组,识别以下内容:

3a. Near-duplicate pairs (merge candidates)

3a. 近似重复对(合并候选)

Two memories are near-duplicates when they express the same fact or decision but phrased differently (e.g., "Use PostgreSQL for auth" and "Auth DB is PostgreSQL").
Heuristics — two memories are near-duplicates if all of these hold:
  • Similarity threshold: estimated cosine similarity > 0.9 (use noun/keyword overlap as proxy — if >60% of significant nouns overlap, treat as >0.9 similarity).
  • Same
    metadata.type
    .
  • Neither memory is pinned (
    metadata.pinned != true
    ).
For each qualifying pair, draft a merged version that is more complete and specific than either original.
当两条记忆表达相同事实或决策但表述不同时(例如:"使用PostgreSQL作为认证数据库"和"认证数据库为PostgreSQL"),视为近似重复。
判定规则——当以下所有条件满足时,两条记忆为近似重复:
  • 相似度阈值:估算余弦相似度>0.9(使用名词/关键词重叠作为替代指标——如果超过60%的重要名词重叠,则视为相似度>0.9)。
  • 相同的
    metadata.type
  • 两条记忆均未被固定(
    metadata.pinned != true
    )。
针对每符合条件的记忆对,草拟一个比原内容更完整、更具体的合并版本。

3b. Contradictions

3b. 矛盾内容

Two memories contradict when they assert opposing facts about the same topic (e.g., "Deploy to ECS" vs. "Deploy to Vercel").
Identify the likely winner: the more recent memory with higher confidence wins. Store both IDs and their content for user review.
当两条记忆针对同一主题断言相反事实时(例如:"部署到ECS" vs "部署到Vercel"),视为矛盾。
确定更合理的胜出者:更新的记忆且置信度更高的胜出。保存两条记忆的ID和内容供用户审核。

3c. Prune candidates

3c. 清理候选

A memory is a prune candidate when any of the following is true:
  1. Its
    metadata.type
    has a retention policy and the memory is older than the configured number of days (compare
    created_at
    to today).
  2. Its confidence score is below 0.3 AND it contains no information unique to this project (no file paths, identifiers, or domain-specific nouns).
Always skip memories where
metadata.pinned == true
, regardless of age or confidence.

当满足以下任一条件时,记忆可作为清理候选:
  1. metadata.type
    有保留策略,且记忆已超过配置的天数(将
    created_at
    与当前日期对比)。
  2. 置信度得分低于0.3,且不包含该项目独有的信息(无文件路径、标识符或领域特定名词)。
无论时长或置信度如何,始终跳过
metadata.pinned == true
的记忆

Step 4: Print Diff Report

步骤4:打印差异报告

Print a structured diff to the terminal before making any changes. Use exactly this format:
undefined
在进行任何修改前,向终端打印结构化差异报告。严格使用以下格式:
undefined

dream — consolidation report

dream — 整合报告

Merges (<N>): [mem0:<id1>] + [mem0:<id2>] → "<merged content, 100 chars>"
Conflicts (<N>): [mem0:<idA>] vs [mem0:<idB>] — "<topic>" [A/B/skip]
Prune (<N>): [mem0:<id>] — <type>, <age>d old
Proposed: <N> merges, <N> prunes, <N> conflicts. Apply? [Y/n]

If there are zero items in any category, omit that section entirely.

If there are zero total proposals (no merges, no prunes, no conflicts), print:
Dream complete. No duplicate, contradictory, or stale memories found.

…and stop.

---
合并项(<N>): [mem0:<id1>] + [mem0:<id2>] → "<合并后的内容,限100字符>"
冲突项(<N>): [mem0:<idA>] vs [mem0:<idB>] — "<主题>" [选择A/B/跳过]
清理项(<N>): [mem0:<id>] — <类型>,已创建<age>
拟执行:<N>次合并,<N>次清理,<N>个冲突。是否应用?[Y/n]

如果某分类下无条目,则完全省略该部分。

如果没有任何拟议变更(无合并、无清理、无冲突),打印:
Dream完成。未发现重复、矛盾或过期的记忆。

并终止流程。

---

Step 5: Wait for User Input and Apply

步骤5:等待用户输入并应用变更

5a. Contradictions

5a. 处理矛盾内容

For each
CONFLICT
pair in the report, wait for the user to type
A
,
B
, or
skip
(case-insensitive). If they enter nothing (empty), treat as
skip
.
Record the winner for each pair before proceeding to the final apply confirmation.
针对报告中的每个
CONFLICT
对,等待用户输入
A
B
skip
(不区分大小写)。如果用户输入为空(直接回车),视为
skip
在进入最终确认步骤前,记录每对冲突的胜出者。

5b. Final confirmation

5b. 最终确认

After all conflict resolutions are collected, prompt:
Apply? [Y/n]
If the user types
n
or
no
(case-insensitive), print
Cancelled. No changes made.
and stop.
If the user confirms (
Y
,
yes
, or empty / Enter), apply all changes in this order:
收集完所有冲突的解决方案后,提示:
是否应用?[Y/n]
如果用户输入
n
no
(不区分大小写),打印
已取消。未进行任何修改。
并终止流程。
如果用户确认(输入
Y
yes
或直接回车),按以下顺序应用所有变更:

Merges

合并操作

For each approved merge pair:
  1. delete_memory(<id1>)
  2. delete_memory(<id2>)
  3. add_memory
    with:
    • text="<merged content>"
    • user_id=<active_user_id>
    • app_id=<active_project_id>
      (top-level, not in metadata)
    • metadata={"type": "<original type>", "branch": "<active_branch>", "confidence": <higher of the two original scores>, "source": "mem0-dream"}
    • infer=False
针对每个已批准的合并对:
  1. delete_memory(<id1>)
  2. delete_memory(<id2>)
  3. 调用
    add_memory
    接口,参数如下:
    • text="<合并后的内容>"
    • user_id=<active_user_id>
    • app_id=<active_project_id>
      (顶级参数,不在metadata中)
    • metadata={"type": "<原类型>", "branch": "<当前分支>", "confidence": <两个原记忆中较高的置信度>, "source": "mem0-dream"}
    • infer=False

Contradictions (resolved)

已解决的矛盾内容

For each resolved conflict where the user chose A or B:
  • Delete the loser (the non-chosen memory):
    delete_memory(memory_id=<loser_id>)
Contradictions where the user chose
skip
are left untouched.
针对每个用户选择了A或B的已解决冲突:
  • 删除失败的记忆(未被选中的记忆):
    delete_memory(memory_id=<loser_id>)
用户选择
skip
的矛盾内容将保持不变。

Prunes

清理操作

For each prune candidate:
  • delete_memory(<memory_id>)

针对每个清理候选:
  • delete_memory(<memory_id>)

Step 6: Print Summary

步骤6:打印汇总

After all changes are applied, print:
Dream complete — merged: <N>, pruned: <N>, conflicts resolved: <N>, skipped: <N>

所有变更应用完成后,打印:
Dream完成 — 合并:<N>,清理:<N>,已解决冲突:<N>,已跳过:<N>

Auto mode

自动模式

When invoked with
--auto
(e.g.,
/mem0:dream --auto
), run non-interactively:
  • Merges: applied automatically (no contradiction, both are compatible).
  • Prunes: applied automatically (age/confidence-based, no ambiguity).
  • Contradictions: skipped — they require human judgment.
当使用
--auto
参数调用时(例如:
/mem0:dream --auto
),以非交互模式运行:
  • 合并操作:自动应用(无矛盾,内容兼容)。
  • 清理操作:自动应用(基于时长/置信度,无歧义)。
  • 矛盾内容:跳过——需要人工判断。

Concurrency guard

并发防护

Before doing any work, check for a lock file at
/tmp/mem0_dream_auto.lock
:
  • If the lock file exists and is less than 10 minutes old, print
    [mem0-dream --auto] Another run in progress — skipping.
    and stop.
  • Otherwise, create the lock file (write the current timestamp). Delete it when done (in all exit paths).
在执行任何操作前,检查
/tmp/mem0_dream_auto.lock
锁文件:
  • 如果锁文件存在且创建时间不超过10分钟,打印
    [mem0-dream --auto] 另一个运行实例正在进行中 — 已跳过。
    并终止流程。
  • 否则,创建锁文件(写入当前时间戳)。在所有退出路径中删除该文件。

Execution

执行流程

In auto mode:
  1. Load policies and fetch memories (Steps 1–3) as normal.
  2. Apply merges and prunes silently without printing the diff or prompting.
  3. Print a compact summary:
    [mem0-dream --auto] project=<id>  merged=<N>  pruned=<N>  conflicts_skipped=<N>
  4. If contradictions were detected but skipped, check if a
    mem0-dream-auto
    reminder already exists before storing one:
    • Search for existing reminders:
      search_memories(query="mem0-dream contradictions manual review", filters={"AND": [{"user_id": "<active_user_id>"}, {"app_id": "<active_project_id>"}, {"metadata": {"source": "mem0-dream-auto"}}]}, top_k=1)
    • If a result exists with similarity > 0.9, skip storing the reminder (one already exists).
    • If no match, store the reminder:
    python
    add_memory(
        text="mem0-dream detected <N> contradiction(s) requiring manual review. Run /mem0:dream to resolve them interactively.",
        user_id="<active_user_id>",
        app_id="<active_project_id>",
        metadata={"type": "task_learning", "source": "mem0-dream-auto", "branch": "<active_branch>"},
        infer=False,
    )
在自动模式下:
  1. 正常加载策略并获取记忆(步骤1–3)。
  2. 静默应用合并和清理操作,不打印差异报告或提示用户。
  3. 打印简洁汇总:
    [mem0-dream --auto] project=<id>  merged=<N>  pruned=<N>  conflicts_skipped=<N>
  4. 如果检测到矛盾内容但已跳过,在存储提醒前检查是否已存在
    mem0-dream-auto
    提醒:
    • 搜索现有提醒:
      search_memories(query="mem0-dream contradictions manual review", filters={"AND": [{"user_id": "<active_user_id>"}, {"app_id": "<active_project_id>"}, {"metadata": {"source": "mem0-dream-auto"}}]}, top_k=1)
    • 如果存在相似度>0.9的结果,跳过存储提醒(已存在)。
    • 如果无匹配结果,存储提醒:
    python
    add_memory(
        text="mem0-dream检测到<N>个需要人工审核的矛盾内容。运行/mem0:dream以交互方式解决。",
        user_id="<active_user_id>",
        app_id="<active_project_id>",
        metadata={"type": "task_learning", "source": "mem0-dream-auto", "branch": "<active_branch>"},
        infer=False,
    )

See also

另请参阅

  • /mem0:forget
    — targeted deletion of specific memories (search + confirm + delete)
  • /mem0:health --deep
    — quick quality scan without applying changes
  • /mem0:forget
    — 定向删除特定记忆(搜索 + 确认 + 删除)
  • /mem0:health --deep
    — 快速质量扫描,不应用变更