dream

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Dream - Memory Consolidation

Dream - 内存整合

Deterministic memory maintenance: detect stale entries, merge duplicates, resolve contradictions, rebuild the MEMORY.md index. All pruning decisions are based on verifiable checks (file exists? function exists? duplicate content?), not LLM judgment.
确定性内存维护:检测过期条目、合并重复内容、解决矛盾、重建MEMORY.md索引。所有清理决策均基于可验证检查(文件是否存在?函数是否存在?内容是否重复?),而非LLM判断。

Argument Resolution

参数解析

python
DRY_RUN = "--dry-run" in "$ARGUMENTS"  # Preview changes without writing
python
DRY_RUN = "--dry-run" in "$ARGUMENTS"  # Preview changes without writing

Overview

概述

Memory files accumulate across sessions. Over time they develop problems:
  • Stale references — memories pointing to files, functions, or classes that no longer exist
  • Duplicates — multiple memories covering the same topic with overlapping content
  • Contradictions — newer memories superseding older ones without cleanup
  • Index drift — MEMORY.md index out of sync with actual memory files
This skill fixes all four problems using deterministic checks only.

内存文件会在多个会话中积累。随着时间推移,它们会出现以下问题:
  • 过期引用 —— 指向已不存在的文件、函数或类的内存条目
  • 重复内容 —— 多个内存条目覆盖同一主题且内容重叠
  • 矛盾内容 —— 新的内存条目取代旧条目但未清理旧内容
  • 索引偏移 —— MEMORY.md索引与实际内存文件不同步
这个技能仅使用确定性检查解决所有这四个问题。

STEP 1: Discover Memory Files

步骤1:发现内存文件

python
undefined
python
undefined

Find the memory directory (agent-specific or project-level)

Find the memory directory (agent-specific or project-level)

Agent memory lives in: .claude/agent-memory/<agent-id>/

Agent memory lives in: .claude/agent-memory/<agent-id>/

Project memory lives in: .claude/projects/<hash>/memory/

Project memory lives in: .claude/projects/<hash>/memory/

Also check: .claude/memory/

Also check: .claude/memory/

memory_dirs = [] Glob(pattern=".claude/agent-memory//MEMORY.md") Glob(pattern=".claude/projects//memory/MEMORY.md") Glob(pattern=".claude/memory/MEMORY.md")
memory_dirs = [] Glob(pattern=".claude/agent-memory//MEMORY.md") Glob(pattern=".claude/projects//memory/MEMORY.md") Glob(pattern=".claude/memory/MEMORY.md")

For each discovered MEMORY.md, glob all *.md files in that directory

For each discovered MEMORY.md, glob all *.md files in that directory

for dir in memory_dirs: Glob(pattern=f"{dir}/../*.md") # All memory files alongside MEMORY.md

Read every discovered memory file. Parse frontmatter (`name`, `description`, `type`) and body content. Build an in-memory inventory:
inventory = [{ "path": "/abs/path/to/file.md", "name": frontmatter.name, "type": frontmatter.type, # user, feedback, project, reference "description": frontmatter.description, "body": body_text, "file_refs": [], # extracted file paths "symbol_refs": [], # extracted function/class names "topics": [], # key phrases for duplicate detection }]

---
for dir in memory_dirs: Glob(pattern=f"{dir}/../*.md") # All memory files alongside MEMORY.md

读取所有发现的内存文件,解析前置元数据(`name`、`description`、`type`)和正文内容,构建内存清单:
inventory = [{ "path": "/abs/path/to/file.md", "name": frontmatter.name, "type": frontmatter.type, # user, feedback, project, reference "description": frontmatter.description, "body": body_text, "file_refs": [], # extracted file paths "symbol_refs": [], # extracted function/class names "topics": [], # key phrases for duplicate detection }]

---

STEP 2: Detect Staleness

步骤2:检测过期状态

For each memory file, extract references and verify they still exist.
对每个内存文件,提取引用并验证其是否仍存在。

2a: File Path References

2a:文件路径引用

Extract paths that look like file references (patterns: paths with
/
and file extensions, backtick-wrapped paths):
python
undefined
提取类似文件引用的路径(模式:包含
/
和文件扩展名的路径、反引号包裹的路径):
python
undefined

Regex-like extraction from body text:

Regex-like extraction from body text:

- Paths containing / with common extensions: .py, .ts, .tsx, .js, .json, .md, .yaml, .yml, .sh

- Paths containing / with common extensions: .py, .ts, .tsx, .js, .json, .md, .yaml, .yml, .sh

- Backtick-wrapped paths:
src/something/file.ts

- Backtick-wrapped paths:
src/something/file.ts

- Quoted paths in frontmatter descriptions

- Quoted paths in frontmatter descriptions

for ref in file_refs: Glob(pattern=ref) # Check if file exists # If no match → mark as STALE_FILE_REF
undefined
for ref in file_refs: Glob(pattern=ref) # Check if file exists # If no match → mark as STALE_FILE_REF
undefined

2b: Symbol References

2b:符号引用

Extract function/class names (patterns:
function_name()
,
ClassName
,
def function_name
):
python
for symbol in symbol_refs:
    Grep(pattern=symbol, path=".", output_mode="files_with_matches", head_limit=1)
    # If no match → mark as STALE_SYMBOL_REF
提取函数/类名称(模式:
function_name()
ClassName
def function_name
):
python
for symbol in symbol_refs:
    Grep(pattern=symbol, path=".", output_mode="files_with_matches", head_limit=1)
    # If no match → mark as STALE_SYMBOL_REF

2c: Staleness Classification

2c:过期状态分类

FindingClassificationAction
All file refs valid, all symbols foundFRESHKeep
Some file refs missingPARTIALLY_STALEFlag for review
All file refs missing AND all symbols missingFULLY_STALEPrune candidate
No external refs (pure decision/preference)EVERGREENKeep
Only memories classified as FULLY_STALE are auto-pruned. PARTIALLY_STALE memories are reported but kept — the user decides.

发现分类操作
所有文件引用有效,所有符号均存在新鲜保留
部分文件引用缺失部分过期标记待审核
所有文件引用缺失且所有符号均不存在完全过期候选清理
无外部引用(纯决策/偏好)永久有效保留
仅标记为完全过期的内存条目会被自动清理,部分过期的内存条目会被报告但保留,由用户决定后续操作。

STEP 3: Detect Duplicates

步骤3:检测重复内容

Compare memories pairwise within the same directory. Two memories are duplicates when:
  1. Same type (both
    feedback
    , both
    project
    , etc.)
  2. Overlapping topic — 60%+ of significant words (excluding stopwords) appear in both bodies
  3. Same subject
    name
    or
    description
    fields reference the same concept
python
stopwords = {"the", "a", "an", "is", "are", "was", "were", "be", "been",
             "have", "has", "had", "do", "does", "did", "will", "would",
             "could", "should", "may", "might", "can", "shall", "to", "of",
             "in", "for", "on", "with", "at", "by", "from", "as", "into",
             "through", "during", "before", "after", "this", "that", "it",
             "not", "no", "but", "or", "and", "if", "then", "than", "so"}

def significant_words(text):
    words = set(text.lower().split()) - stopwords
    return {w for w in words if len(w) > 2}

def overlap_ratio(words_a, words_b):
    if not words_a or not words_b:
        return 0.0
    intersection = words_a & words_b
    smaller = min(len(words_a), len(words_b))
    return len(intersection) / smaller if smaller > 0 else 0.0
在同一目录内两两对比内存条目,当满足以下条件时判定为重复:
  1. 类型相同(同为
    feedback
    、同为
    project
    等)
  2. 主题重叠 —— 正文内容中60%以上的重要词汇(排除停用词)同时出现在两个条目中
  3. 主题一致 ——
    name
    description
    字段指向同一概念
python
stopwords = {"the", "a", "an", "is", "are", "was", "were", "be", "been",
             "have", "has", "had", "do", "does", "did", "will", "would",
             "could", "should", "may", "might", "can", "shall", "to", "of",
             "in", "for", "on", "with", "at", "by", "from", "as", "into",
             "through", "during", "before", "after", "this", "that", "it",
             "not", "no", "but", "or", "and", "if", "then", "than", "so"}

def significant_words(text):
    words = set(text.lower().split()) - stopwords
    return {w for w in words if len(w) > 2}

def overlap_ratio(words_a, words_b):
    if not words_a or not words_b:
        return 0.0
    intersection = words_a & words_b
    smaller = min(len(words_a), len(words_b))
    return len(intersection) / smaller if smaller > 0 else 0.0

For each pair with same type:

For each pair with same type:

if overlap_ratio >= 0.6 → DUPLICATE pair

if overlap_ratio >= 0.6 → DUPLICATE pair

Keep the NEWER file (by filesystem mtime), prune the older

Keep the NEWER file (by filesystem mtime), prune the older


---

---

STEP 4: Resolve Contradictions

步骤4:解决矛盾内容

Contradictions occur when two memories of the same type make opposing claims about the same subject. Detection:
  1. Same type + same topic (overlap >= 0.4 but < 0.6 — related but not duplicate)
  2. Negation signals — one body contains negation of the other's assertion:
    • "do X" vs "do not X" / "don't X" / "never X"
    • "use X" vs "avoid X" / "stop using X"
    • "prefer X" vs "prefer Y" (for same decision domain)
python
negation_pairs = [
    ("do ", "do not "), ("do ", "don't "),
    ("use ", "avoid "), ("use ", "stop using "),
    ("prefer ", "don't prefer "), ("always ", "never "),
]
当两个同类型内存条目对同一主题做出相反声明时,即出现矛盾。检测条件:
  1. 同类型+主题相关(重叠率≥0.4但<0.6——相关但非重复)
  2. 否定信号 —— 一个条目包含对另一个条目的断言的否定:
    • "do X" vs "do not X" / "don't X" / "never X"
    • "use X" vs "avoid X" / "stop using X"
    • "prefer X" vs "prefer Y"(同一决策领域内)
python
negation_pairs = [
    ("do ", "do not "), ("do ", "don't "),
    ("use ", "avoid "), ("use ", "stop using "),
    ("prefer ", "don't prefer "), ("always ", "never "),
]

For each pair flagged as contradictory:

For each pair flagged as contradictory:

Keep the NEWER file (more recent decision supersedes)

Keep the NEWER file (more recent decision supersedes)

Prune the older file

Prune the older file


---

---

STEP 5: Execute Changes (or Dry Run)

步骤5:执行变更(或试运行)

Dry Run Mode (
--dry-run
)

试运行模式(
--dry-run

If
--dry-run
flag is present, skip all writes. Output the full report (Step 6) with
[DRY RUN]
prefix and list what WOULD be changed:
[DRY RUN] Would delete: .claude/agent-memory/foo/stale_old_path.md (FULLY_STALE)
[DRY RUN] Would delete: .claude/agent-memory/foo/duplicate_auth.md (DUPLICATE of auth_patterns.md)
[DRY RUN] Would delete: .claude/agent-memory/foo/old_preference.md (CONTRADICTED by new_preference.md)
[DRY RUN] Would rebuild: .claude/agent-memory/foo/MEMORY.md (3 entries removed, 12 remaining)
如果存在
--dry-run
标志,则跳过所有写入操作。输出完整报告(步骤6)并添加
[DRY RUN]
前缀,列出将要执行的变更:
[DRY RUN] Would delete: .claude/agent-memory/foo/stale_old_path.md (FULLY_STALE)
[DRY RUN] Would delete: .claude/agent-memory/foo/duplicate_auth.md (DUPLICATE of auth_patterns.md)
[DRY RUN] Would delete: .claude/agent-memory/foo/old_preference.md (CONTRADICTED by new_preference.md)
[DRY RUN] Would rebuild: .claude/agent-memory/foo/MEMORY.md (3 entries removed, 12 remaining)

Live Mode

正式模式

python
undefined
python
undefined

1. Delete FULLY_STALE files

1. Delete FULLY_STALE files

for stale in fully_stale_files: Bash(command=f"rm '{stale['path']}'")
for stale in fully_stale_files: Bash(command=f"rm '{stale['path']}'")

2. Delete DUPLICATE files (keep newer)

2. Delete DUPLICATE files (keep newer)

for dup in duplicate_pairs: older = dup["older"] Bash(command=f"rm '{older['path']}'")
for dup in duplicate_pairs: older = dup["older"] Bash(command=f"rm '{older['path']}'")

3. Delete CONTRADICTED files (keep newer)

3. Delete CONTRADICTED files (keep newer)

for contradiction in contradiction_pairs: older = contradiction["older"] Bash(command=f"rm '{older['path']}'")
for contradiction in contradiction_pairs: older = contradiction["older"] Bash(command=f"rm '{older['path']}'")

4. Rebuild MEMORY.md index from surviving files

4. Rebuild MEMORY.md index from surviving files

undefined
undefined

Rebuild MEMORY.md

重建MEMORY.md

Read all surviving
.md
files (excluding MEMORY.md itself). Generate the index:
markdown
undefined
读取所有保留的
.md
文件(排除MEMORY.md本身),生成索引:
markdown
undefined

<Directory Name> Memory

<Directory Name> Memory

  • Name -- one-line description from frontmatter

Rules for the rebuilt index:
- One line per memory file, under 150 characters
- Sorted alphabetically by filename
- Total index must stay under 200 lines
- If over 200 lines after rebuild, warn the user (do not auto-truncate content memories)

```python
  • Name -- one-line description from frontmatter

重建索引规则:
- 每个内存文件对应一行,长度不超过150字符
- 按文件名字母顺序排序
- 索引总长度不得超过200行
- 重建后超过200行时,向用户发出警告(不自动截断内存内容)

```python

Write the rebuilt MEMORY.md

Write the rebuilt MEMORY.md

Write(path="<memory_dir>/MEMORY.md", content=rebuilt_index)

---
Write(path="<memory_dir>/MEMORY.md", content=rebuilt_index)

---

STEP 6: Report

步骤6:生成报告

Output a summary table after consolidation:
undefined
整合完成后输出汇总表格:
undefined

Dream Consolidation Report

Dream整合报告

MetricCount
Memory directories scannedN
Total memory files scannedN
Stale entries prunedN
Duplicates mergedN
Contradictions resolvedN
Partially stale (kept, flagged)N
Evergreen (no external refs)N
Surviving memoriesN
MEMORY.md indexes rebuiltN
指标数量
扫描的内存目录数N
扫描的内存文件总数N
清理的过期条目数N
合并的重复内容数N
解决的矛盾内容数N
部分过期(保留并标记)N
永久有效(无外部引用)N
保留的内存条目数N
重建的MEMORY.md索引数N

Changes Made

执行的变更

FileActionReason
path/to/file.md
DELETEDFully stale: all referenced files removed
path/to/old.md
DELETEDDuplicate of
path/to/new.md
path/to/outdated.md
DELETEDContradicted by
path/to/current.md
文件操作原因
path/to/file.md
删除完全过期:所有引用文件已移除
path/to/old.md
删除重复于
path/to/new.md
path/to/outdated.md
删除
path/to/current.md
矛盾

Flagged for Review (PARTIALLY_STALE)

标记待审核(部分过期)

FileMissing References
path/to/file.md
src/old/path.ts
no longer exists

If `--dry-run`, prefix the entire report with:
[DRY RUN] No files were modified. Run without --dry-run to apply changes.

---
文件缺失的引用
path/to/file.md
src/old/path.ts
已不存在

如果是试运行模式,在整个报告前添加:
[DRY RUN] 未修改任何文件。移除--dry-run参数以应用变更。

---

Error Handling

错误处理

ConditionResponse
No memory directories foundReport "No memory directories found" and exit
No memory files in directoryReport "Directory empty, nothing to consolidate"
All memories are FRESHReport "All N memories are current, nothing to prune"
MEMORY.md exceeds 200 lines after rebuildWarn user, do not auto-truncate
File deletion failsReport error, continue with remaining files
Memory file has no frontmatterTreat as EVERGREEN (cannot verify refs without metadata)

情况响应
未找到内存目录报告“未找到内存目录”并退出
目录中无内存文件报告“目录为空,无需整合”
所有内存条目均为新鲜状态报告“所有N个内存条目均为最新,无需清理”
重建后MEMORY.md超过200行向用户发出警告,不自动截断
文件删除失败报告错误,继续处理剩余文件
内存文件无前置元数据视为永久有效(无元数据无法验证引用)

When NOT to Use

不适用场景

  • To store new decisions -- use
    /ork:remember
  • To search past decisions -- use
    /ork:memory search
  • To load context at session start -- use
    /ork:memory load
  • After fewer than 5 sessions -- memory files are unlikely to have accumulated enough staleness

  • 存储新决策 —— 使用
    /ork:remember
  • 搜索过往决策 —— 使用
    /ork:memory search
  • 会话开始时加载上下文 —— 使用
    /ork:memory load
  • 会话次数少于5次 —— 内存文件不太可能积累足够的过期内容

Related Skills

相关技能

  • ork:remember
    -- Store decisions and patterns (write-side)
  • ork:memory
    -- Search, load, sync, visualize (read-side)
  • ork:remember
    —— 存储决策和模式(写入端)
  • ork:memory
    —— 搜索、加载、同步、可视化(读取端)