feat(memory): add Deep Dream module for daily memory distillation

- Add Deep Dream: nightly distill daily memories → refined MEMORY.md + dream diary
- Simplify flush prompt to daily-only, defer MEMORY.md maintenance to Deep Dream
- Remove dead code (_append_to_main_memory) and fix fallback summary logic
- Add shrinkage protection and input dedup for dream process
- Ensure flush threads complete before dream starts
- Update docs (zh/en/ja) with dream diary and distillation mechanism
This commit is contained in:
zhayujie
2026-04-13 21:32:52 +08:00
parent 7bc88c17e4
commit ea1a0c8b3d
5 changed files with 320 additions and 113 deletions

View File

@@ -15,12 +15,17 @@ Stored in `~/cow/MEMORY.md`, containing long-term user preferences, important de
Stored in `~/cow/memory/` directory, named by date (e.g., `2026-03-08.md`), recording daily conversation summaries and key events. Files are only created on first write to avoid generating empty files.
### Dream Diary (memory/dreams/YYYY-MM-DD.md)
A byproduct of the Deep Dream (memory distillation) process, recording discoveries, deduplication operations, and new insights from each consolidation. Stored in `~/cow/memory/dreams/` directory, named by date.
## Automatic Writing
The Agent automatically persists conversation content to long-term memory through the following mechanisms:
- **On context trimming** — When conversation turns or tokens exceed the configured limit, the oldest half of the context is trimmed, and the discarded content is summarized by LLM into key information and written to the daily memory file. The summary is also asynchronously injected into the retained context for conversational continuity
- **Daily scheduled summary** — A full summary is automatically triggered at 23:55 every day, ensuring memory is preserved even on low-activity days (skipped if content hasn't changed)
- **Deep Dream (memory distillation)** — Runs automatically after the daily summary, reading recent daily files and current MEMORY.md, then using LLM to deduplicate, merge, and prune, producing a refined MEMORY.md and a dream diary
- **On API context overflow** — When the model API returns a context overflow error, the current conversation summary is saved as an emergency measure
All memory writes run asynchronously in a background thread (LLM summarization + file writing), never blocking normal conversation replies.
@@ -44,6 +49,7 @@ On first launch, the Agent will proactively ask the user for key information and
| `user.md` | User identity information and preferences |
| `MEMORY.md` | Core memory (long-term) |
| `memory/YYYY-MM-DD.md` | Daily memory (created on demand) |
| `memory/dreams/YYYY-MM-DD.md` | Dream diary (auto-generated by Deep Dream) |
<Frame>
<img src="https://cdn.link-ai.tech/doc/20260203000455.png" width="800" />

View File

@@ -15,12 +15,17 @@ description: CowAgent の長期記憶システム — ファイル永続化、
`~/cow/memory/` ディレクトリに保存され、日付で命名されます(例:`2026-03-08.md`)。日々の会話の要約と主要なイベントを記録します。空ファイルの生成を避けるため、最初の書き込み時にのみファイルが作成されます。
### 夢日記memory/dreams/YYYY-MM-DD.md
Deep Dream記憶蒸留プロセスの副産物で、各整理で発見された重複、統合操作、新しい洞察を記録します。`~/cow/memory/dreams/` ディレクトリに日付で命名されて保存されます。
## 自動書き込み
Agent は以下のメカニズムにより、会話内容を長期記憶に自動的に永続化します:
- **コンテキストトリミング時** — 会話ターン数またはトークン数が設定上限を超えた場合、最も古い半分のコンテキストがトリミングされ、LLM によって要約されて日次記憶ファイルに書き込まれます。要約は保持されたコンテキストにも非同期で注入され、会話の連続性を維持します
- **毎日のスケジュール要約** — 毎日 23:55 に自動的にフル要約がトリガーされ、アクティビティが少ない日でも記憶が保存されます(内容が変更されていない場合はスキップ)
- **Deep Dream記憶蒸留** — 毎日の要約完了後に自動実行され、最近の日次記憶と現在の MEMORY.md を読み込み、LLM で重複排除・統合・剪定を行い、精錬された新しい MEMORY.md と夢日記を生成します
- **API コンテキストオーバーフロー時** — モデル API がコンテキストオーバーフローエラーを返した場合、緊急措置として現在の会話要約が保存されます
すべての記憶書き込みはバックグラウンドスレッドで非同期に実行されLLM の要約 + ファイル書き込み)、通常の会話応答をブロックしません。
@@ -35,6 +40,7 @@ Agent は以下のメカニズムにより、会話内容を長期記憶に自
| `user.md` | ユーザーの身元情報と好み |
| `MEMORY.md` | コア記憶(長期) |
| `memory/YYYY-MM-DD.md` | 日次記憶(オンデマンドで作成) |
| `memory/dreams/YYYY-MM-DD.md` | 夢日記Deep Dream で自動生成) |
<Frame>
<img src="https://cdn.link-ai.tech/doc/20260203000455.png" width="800" />

View File

@@ -15,12 +15,17 @@ description: CowAgent 的长期记忆系统 — 文件持久化、自动写入
存储在 `~/cow/memory/` 目录下,按日期命名(如 `2026-03-08.md`),记录每天的对话摘要和关键事件。仅在首次写入时创建,避免生成空文件。
### 梦境日记memory/dreams/YYYY-MM-DD.md
Deep Dream记忆蒸馏过程的副产物记录每次整理的发现、去重合并操作和新洞察。存储在 `~/cow/memory/dreams/` 目录下,按日期命名。
## 自动写入
Agent 通过以下机制自动将对话内容持久化为长期记忆:
- **上下文裁剪时** — 当对话轮次或 token 超出配置上限时,裁剪最早一半的上下文,使用 LLM 将被裁剪的内容总结为关键信息写入当天记忆文件,并将摘要异步注入到保留的上下文中,帮助模型保持对话连贯性
- **每日定时总结** — 每天 23:55 自动触发一次全量总结,防止低活跃日无记忆留存(内容无变化时自动跳过)
- **Deep Dream记忆蒸馏** — 每日总结完成后自动执行,读取近期日记和当前 MEMORY.md通过 LLM 进行去重、合并、修剪,生成精炼的新版 MEMORY.md 并输出梦境日记
- **API 上下文溢出时** — 当模型 API 返回上下文溢出错误时,紧急保存当前对话摘要
所有记忆写入均在后台异步执行LLM 总结 + 文件写入),不阻塞正常对话回复。
@@ -44,6 +49,7 @@ Agent 会在对话中根据需要自动触发记忆检索,将相关历史信
| `user.md` | 用户身份信息和偏好 |
| `MEMORY.md` | 核心记忆(长期) |
| `memory/YYYY-MM-DD.md` | 日级记忆(按需创建) |
| `memory/dreams/YYYY-MM-DD.md` | 梦境日记Deep Dream 自动生成) |
<Frame>
<img src="https://cdn.link-ai.tech/doc/20260203000455.png" width="800" />