dream
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDream - 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 writingpython
DRY_RUN = "--dry-run" in "$ARGUMENTS" # Preview changes without writingOverview
概述
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
undefinedpython
undefinedFind 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
undefinedRegex-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
src/something/file.ts- Backtick-wrapped paths: src/something/file.ts
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
undefinedfor ref in file_refs:
Glob(pattern=ref) # Check if file exists
# If no match → mark as STALE_FILE_REF
undefined2b: Symbol References
2b:符号引用
Extract function/class names (patterns: , , ):
function_name()ClassNamedef function_namepython
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()ClassNamedef function_namepython
for symbol in symbol_refs:
Grep(pattern=symbol, path=".", output_mode="files_with_matches", head_limit=1)
# If no match → mark as STALE_SYMBOL_REF2c: Staleness Classification
2c:过期状态分类
| Finding | Classification | Action |
|---|---|---|
| All file refs valid, all symbols found | FRESH | Keep |
| Some file refs missing | PARTIALLY_STALE | Flag for review |
| All file refs missing AND all symbols missing | FULLY_STALE | Prune candidate |
| No external refs (pure decision/preference) | EVERGREEN | Keep |
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:
- Same type (both , both
feedback, etc.)project - Overlapping topic — 60%+ of significant words (excluding stopwords) appear in both bodies
- Same subject — or
namefields reference the same conceptdescription
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在同一目录内两两对比内存条目,当满足以下条件时判定为重复:
- 类型相同(同为、同为
feedback等)project - 主题重叠 —— 正文内容中60%以上的重要词汇(排除停用词)同时出现在两个条目中
- 主题一致 —— 或
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.0For 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:
- Same type + same topic (overlap >= 0.4 but < 0.6 — related but not duplicate)
- 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 "),
]当两个同类型内存条目对同一主题做出相反声明时,即出现矛盾。检测条件:
- 同类型+主题相关(重叠率≥0.4但<0.6——相关但非重复)
- 否定信号 —— 一个条目包含对另一个条目的断言的否定:
- "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试运行模式(--dry-run
)
--dry-runIf flag is present, skip all writes. Output the full report (Step 6) with prefix and list what WOULD be changed:
--dry-run[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)如果存在标志,则跳过所有写入操作。输出完整报告(步骤6)并添加前缀,列出将要执行的变更:
--dry-run[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
undefinedpython
undefined1. 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
undefinedundefinedRebuild MEMORY.md
重建MEMORY.md
Read all surviving files (excluding MEMORY.md itself). Generate the index:
.mdmarkdown
undefined读取所有保留的文件(排除MEMORY.md本身),生成索引:
.mdmarkdown
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行时,向用户发出警告(不自动截断内存内容)
```pythonWrite 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整合完成后输出汇总表格:
undefinedDream Consolidation Report
Dream整合报告
| Metric | Count |
|---|---|
| Memory directories scanned | N |
| Total memory files scanned | N |
| Stale entries pruned | N |
| Duplicates merged | N |
| Contradictions resolved | N |
| Partially stale (kept, flagged) | N |
| Evergreen (no external refs) | N |
| Surviving memories | N |
| MEMORY.md indexes rebuilt | N |
| 指标 | 数量 |
|---|---|
| 扫描的内存目录数 | N |
| 扫描的内存文件总数 | N |
| 清理的过期条目数 | N |
| 合并的重复内容数 | N |
| 解决的矛盾内容数 | N |
| 部分过期(保留并标记) | N |
| 永久有效(无外部引用) | N |
| 保留的内存条目数 | N |
| 重建的MEMORY.md索引数 | N |
Changes Made
执行的变更
| File | Action | Reason |
|---|---|---|
| DELETED | Fully stale: all referenced files removed |
| DELETED | Duplicate of |
| DELETED | Contradicted by |
| 文件 | 操作 | 原因 |
|---|---|---|
| 删除 | 完全过期:所有引用文件已移除 |
| 删除 | 重复于 |
| 删除 | 与 |
Flagged for Review (PARTIALLY_STALE)
标记待审核(部分过期)
| File | Missing References |
|---|---|
| |
If `--dry-run`, prefix the entire report with:
[DRY RUN] No files were modified. Run without --dry-run to apply changes.
---| 文件 | 缺失的引用 |
|---|---|
| |
如果是试运行模式,在整个报告前添加:
[DRY RUN] 未修改任何文件。移除--dry-run参数以应用变更。
---Error Handling
错误处理
| Condition | Response |
|---|---|
| No memory directories found | Report "No memory directories found" and exit |
| No memory files in directory | Report "Directory empty, nothing to consolidate" |
| All memories are FRESH | Report "All N memories are current, nothing to prune" |
| MEMORY.md exceeds 200 lines after rebuild | Warn user, do not auto-truncate |
| File deletion fails | Report error, continue with remaining files |
| Memory file has no frontmatter | Treat 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
相关技能
- -- Store decisions and patterns (write-side)
ork:remember - -- Search, load, sync, visualize (read-side)
ork:memory
- —— 存储决策和模式(写入端)
ork:remember - —— 搜索、加载、同步、可视化(读取端)
ork:memory