Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

docs: 补充 Dreaming 睡时记忆整理与 Agent Team 团队记忆说明

miaoran_licccad255803f
diff --git a/docs/en/AgentTeam.md b/docs/en/AgentTeam.md
index 6832864695..4d10bd2cde 100644
--- a/docs/en/AgentTeam.md
+++ b/docs/en/AgentTeam.md
@@ -236,6 +236,15 @@ In Agent Team mode you can still use and develop **Skills**. Each agent can conf
> For detailed usage and development of Team Skills, see [Team Skill developer guide](TeamSkill.md).
+### 2.5 Team Memory
+
+Each Agent Team has two memory layers: **personal memory** (each member reads/writes its own) and a shared **`TEAM_MEMORY.md`** (read-only to members; the Leader writes it via an extractor agent at the end of each round).
+
+- **Temporary team** — members read-only access the parent agent's workspace memory; nothing persists once the team is torn down
+- **Persistent team** — each member has isolated personal memory plus team memory that accumulates across rounds; predefined members keep their existing workspace via symlink
+
+For the full layout, extraction categories (`[decision]` / `[lesson]` / `[member]` / `[context]`), and cross-team / cross-member isolation, see [Memory → Agent Team Memory](Memory.md#agent-team-memory).
+
---
## Case study
@@ -375,17 +384,20 @@ Full path layout:
```text
.agent_teams/ ← Agent Team root
-└── <session_id>/ ← one folder per Agent Team session
+└── <team_name>/ ← one folder per team
├── team-workspace/ ← shared by all members
│ ├── artifacts/ ← team outputs
│ │ ├── code/ ← code artifacts
│ │ ├── docs/ ← document artifacts
│ │ └── reports/ ← report artifacts
│ └── skills/ ← team-shared skills
+ ├── team-memory/ ← shared team memory (auto-extracted by leader)
+ │ └── TEAM_MEMORY.md
└── workspaces/ ← per-member workspaces
└── <agent_name>_workspace/ ← each agent’s private space
├── AGENT.md ← agent configuration
- ├── memory/ ← long-term memory
+ ├── memory/ ← personal long-term memory (general scenario)
+ ├── coding_memory/ ← personal coding memory (coding scenario)
├── skills/ ← skill library
├── todo/ ← todos
└── ... ← other agent-specific files
diff --git a/docs/en/CodingMemory.md b/docs/en/CodingMemory.md
index 4933dd7c2a..2cf43ce040 100644
--- a/docs/en/CodingMemory.md
+++ b/docs/en/CodingMemory.md
@@ -81,6 +81,14 @@ Coding memory data is stored in the `workspace/agent/memory/` directory, sharing
---
+## Sleep-Time Consolidation (Dreaming)
+
+When Dreaming is enabled in Code mode, the system reviews past coding sessions during idle time and automatically extracts reusable experience — debugging root causes, API edge behaviors, design decisions — to `{workspace}/coding_memory/consolidated_{hash}.md`. This is the sleep-time complement to the in-session `coding_memory_*` tools above.
+
+See [Memory → Dreaming: Sleep-Time Memory Consolidation](Memory.md#dreaming-sleep-time-memory-consolidation) for how to enable it and extraction details.
+
+---
+
## See Also
- [Modes](Modes.md) — Mode configuration and switching
diff --git a/docs/en/Memory.md b/docs/en/Memory.md
index 67bb4cc956..9c599a0e5f 100644
--- a/docs/en/Memory.md
+++ b/docs/en/Memory.md
@@ -83,6 +83,31 @@ memory:
| `OPENVIKING_ENDPOINT` | OpenViking service address |
| `OPENVIKING_API_KEY` | OpenViking API key |
+### Dreaming Configuration
+
+Dreaming is a sleep-time memory consolidation mechanism (see [Dreaming: Sleep-Time Memory Consolidation](#dreaming-sleep-time-memory-consolidation) below). Disabled by default — must be explicitly turned on.
+
+```yaml
+memory:
+ dreaming:
+ agent:
+ enabled: false
+ interval_seconds: 14400 # 4 hours by default
+ code:
+ enabled: false
+ interval_seconds: 14400
+```
+
+Env overrides:
+
+| Variable | Description |
+|----------|-------------|
+| `DREAMING_AGENT_ENABLED` | Toggle for agent-mode Dreaming |
+| `DREAMING_CODE_ENABLED` | Toggle for code-mode Dreaming |
+| `DREAMING_INTERVAL` | Sweep interval in seconds (applies to both modes) |
+
+The LLM call reuses `models.default`; no extra model config required.
+
---
## Built-in Memory File Layout
@@ -124,19 +149,119 @@ Memory is plain Markdown; the agent uses file tools:
| Daily notes | `memory/YYYY-MM-DD.md` | write / edit | “Shipped login fix today” |
| “Remember this” | `memory/YYYY-MM-DD.md` | write | User asks to remember a fact |
+## Dreaming: Sleep-Time Memory Consolidation
+
+In addition to in-session writes by the agent, JiuwenClaw provides **Dreaming**: a sleep-time mechanism that periodically scans past sessions during idle time, calls an LLM to extract content worth keeping long-term, and writes the result to persistent memory files. Agent and Code modes share the same pipeline.
+
+| Mode | Extraction target | Output |
+|------|-------------------|--------|
+| `agent` | User preferences, background, areas of interest | `{workspace}/memory/DREAMING.md` (single file, max 50 entries, oldest evicted) |
+| `code` | Debugging root causes, API edge behaviors, design decisions, reusable engineering experience | `{workspace}/coding_memory/consolidated_{hash}.md` (one file per entry, dedup by content SHA256) |
+
+For enabling, see [Configuration → Dreaming Configuration](#dreaming-configuration) above.
+
+### How it works
+
+- **Scheduling**: an in-process Orchestrator fires every `interval_seconds`, with a 120s initial delay after startup
+- **Busy backoff**: skipped when the agent is actively handling a request; retried next cycle
+- **Incremental scan**: a checkpoint tracks processed sessions; sessions with fewer than 4 rounds or older than 30 days are skipped
+- **Cost control**: at most 10 sessions per sweep, each compressed to under 30K tokens; at most 5 entries extracted per session
+- **Retry policy**: LLM-call failures are not checkpointed so the session is retried; JSON parse failures are skipped
+
+Difference from the in-session writes above: those rely on the agent's real-time judgment about what to remember. Dreaming revisits whole conversations during idle time and decides afterwards — an offline consolidation channel.
+
+
+## Agent Team Memory
+
+In Agent Team mode, each team has two memory layers:
+
+| Layer | Access | Writer |
+|-------|--------|--------|
+| Personal memory | Owned by a single member | The member itself (calls memory tools in-session) |
+| Team memory | Read-only to all members | An extractor agent spawned by the Leader at end of round |
+
+### Lifecycle × memory behavior
+
+| Lifecycle | Personal memory | Team memory | Use case |
+|-----------|-----------------|-------------|----------|
+| **Temporary** | Read-only access to the parent agent's workspace memory | None | One-shot tasks, disposable teams |
+| **Persistent** | Each member reads/writes its own | Auto-extracted, accumulates across rounds | Long-running collaboration where lessons should persist |
+
+### Layout
+
+```
+~/.openjiuwen/.agent_teams/{team_name}/
+├── team-memory/ # Shared team memory
+│ └── TEAM_MEMORY.md
+├── workspaces/
+│ ├── alice_workspace/ # New member (created inside the team)
+│ │ ├── memory/ # personal memory (general scenario)
+│ │ └── coding_memory/ # personal memory (coding scenario)
+│ └── bob_workspace -> ~/.openjiuwen/bob_workspace/ # Predefined member (symlink)
+└── team-workspace/ # Shared file area (not memory)
+```
+
+- **New members**: workspace freshly created under the team home; personal memory starts empty
+- **Predefined members**: a symlink points to the existing personal workspace, so prior memory carries over
+- Whether personal memory uses `memory/` or `coding_memory/` is decided at team creation by the scenario (`general` / `coding`)
+
+### Auto-extracted team memory
+
+In persistent teams, the **Leader** spawns an extractor agent at the end of every round. It reads the round's task records and team messages, distills what's worth keeping, and updates `TEAM_MEMORY.md`. Entries are tagged with one of four categories:
+
+| Tag | Meaning |
+|-----|---------|
+| `[decision]` | Team decisions — why A was chosen over B, key trade-offs |
+| `[lesson]` | Lessons learned — what worked, what caused rework, reusable patterns |
+| `[member]` | Member strengths — who is good at what, who owns which area |
+| `[context]` | Project context — business constraints, deadlines, stakeholder asks |
+
+The extractor agent autonomously reads existing memory, analyzes the round, and merges / updates / evicts entries. The file is kept under 200 lines. Temporary teams do not extract.
+
+### Isolation
+
+- **Cross-team**: each team's `team_name` is part of the storage path and index key (`agent_id = "{team_name}.{member_name}"`); same-named members in different teams cannot see each other's memory
+- **Cross-member**: each member has its own index instance; members cannot read each other's personal memory, only the shared team memory
+- **Temporary teams**: read-only access to the parent workspace; the source memory cannot be polluted
+
+
## Architecture overview
+The memory system has three independent write channels operating in parallel, all sharing the same storage and retrieval layer:
+
```
-User / Agent
- │
- ▼
-MemoryIndexManager
- ├── Persistence (file tools)
- ├── File watch (watchdog)
- ├── Semantic search (vector + BM25)
- └── File reads (on demand)
+ User / Agent
+ │
+ ┌───────────────────┼───────────────────┐
+ ↓ ↓ ↓
+ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐
+ │ In-session │ │ Dreaming │ │ Agent Team │
+ │ writes │ │ background │ │ coordination │
+ │ │ │ extraction │ │ │
+ │ Agent calls │ │ Orchestrator │ │ Leader runs │
+ │ tools itself │ │ periodic LLM │ │ at round end │
+ └──────┬───────┘ └──────┬───────┘ └──────┬───────┘
+ │ │ │
+ └──────────────────┼──────────────────┘
+ ↓ writes Markdown
+ ┌──────────────────────────────────────┐
+ │ MemoryIndexManager (shared index) │
+ │ persistence / watch / hybrid search │
+ └────────────────┬─────────────────────┘
+ ↑ search / read
+ User / Agent
```
+> Index internals are covered below in "Technical stack".
+
+| Channel | Triggered by | When | Typical targets |
+|---------|--------------|------|-----------------|
+| In-session writes | Agent calls tools itself | During the conversation | `MEMORY.md` / `USER.md` / `YYYY-MM-DD.md` / `coding_memory/*.md` |
+| Dreaming offline extraction | In-process Orchestrator | Periodic idle-time sweep | `DREAMING.md` / `consolidated_{hash}.md` |
+| Agent Team coordination | Leader at end of round | After a team round completes | Team `TEAM_MEMORY.md` + each member's personal memory |
+
+All three channels share the same **MemoryIndexManager** for indexing and retrieval, so the agent reading memory does not need to know which channel wrote it.
+
### Capabilities
| Capability | Description |
diff --git a/docs/zh/AgentTeam.md b/docs/zh/AgentTeam.md
index 405c6ff6ad..7073b35a49 100644
--- a/docs/zh/AgentTeam.md
+++ b/docs/zh/AgentTeam.md
@@ -224,6 +224,16 @@ Agent Team 模式下同样支持技能(Skills)的使用和开发。团队中
3. **协作效率提升**:技能可以让 Agent 更高效地完成分配的任务
> 有关 Team Skills 的详细使用和开发教程,请参阅 [Team Skill 开发指南](TeamSkill.md)。
+
+### 2.5 Team Memory
+
+Agent Team 模式下,每个团队都有自己的双层记忆:成员各自的**个人记忆**(独立读写)和团队共享的 **`TEAM_MEMORY.md`**(所有成员只读,Leader 在 round 结束后由提取 agent 自动写入)。
+
+- **临时团队**:成员只读访问父 agent 的 workspace 记忆,团队销毁后不留痕
+- **持久团队**:每个成员独立的个人记忆 + 跨 round 累积的团队记忆,预定义成员的原有 workspace 通过 symlink 自动延续
+
+完整的存储布局、提取分类(`[decision]` / `[lesson]` / `[member]` / `[context]`)、跨团队/跨成员隔离机制详见 [记忆系统 → Agent Team 团队记忆](记忆.md#agent-team-团队记忆)。
+
---
## 案例实践
@@ -357,17 +367,20 @@ Agent Team 的文件结构分为两个层级:**团队共享工作区**和**成
```
.agent_teams/ ← Agent Team 根目录
-└── <session_id>/ ← 每个 Agent Team Session 独立一个文件夹
+└── <team_name>/ ← 每个团队独立一个文件夹
├── team-workspace/ ← 团队共享工作区(所有成员共享)
│ ├── artifacts/ ← 团队产出物
│ │ ├── code/ ← 代码产出
│ │ ├── docs/ ← 文档产出
│ │ └── reports/ ← 报告产出
│ └── skills/ ← 团队共享技能
+ ├── team-memory/ ← 团队共享记忆(Leader 自动提取)
+ │ └── TEAM_MEMORY.md
└── workspaces/ ← 各成员独立工作空间
└── <agent_name>_workspace/ ← 各 Agent 的独立空间
├── AGENT.md ← 智能体配置
- ├── memory/ ← 长期记忆
+ ├── memory/ ← 个人长期记忆(general 场景)
+ ├── coding_memory/ ← 个人编码记忆(coding 场景)
├── skills/ ← 技能库
├── todo/ ← 待办事项
└── ... ← 其他 Agent 专属文件
diff --git "a/docs/zh/\347\274\226\347\240\201\350\256\260\345\277\206.md" "b/docs/zh/\347\274\226\347\240\201\350\256\260\345\277\206.md"
index f65b184cfb..61d5e0afc3 100644
--- "a/docs/zh/\347\274\226\347\240\201\350\256\260\345\277\206.md"
+++ "b/docs/zh/\347\274\226\347\240\201\350\256\260\345\277\206.md"
@@ -81,6 +81,14 @@ modes:
---
+## 睡时记忆整理(Dreaming)
+
+Code 模式启用 Dreaming 后,系统会闲时回看历史 coding 会话,自动提取调试根因、API 边界行为、设计决策等可复用经验,写入 `{workspace}/coding_memory/consolidated_{hash}.md`。这是对上述 `coding_memory_*` 工具(会话内主动写入)的离线补充。
+
+启用方式与提取细节详见 [记忆系统 → Dreaming 睡时记忆整理](记忆.md#dreaming-睡时记忆整理)。
+
+---
+
## 详见
- [模式系统](模式系统.md) — 各模式配置与切换
diff --git "a/docs/zh/\350\256\260\345\277\206.md" "b/docs/zh/\350\256\260\345\277\206.md"
index ee619242e3..dd9af67ce0 100644
--- "a/docs/zh/\350\256\260\345\277\206.md"
+++ "b/docs/zh/\350\256\260\345\277\206.md"
@@ -83,6 +83,31 @@ memory:
| `OPENVIKING_ENDPOINT` | OpenViking 服务地址 |
| `OPENVIKING_API_KEY` | OpenViking API 密钥 |
+### Dreaming 配置
+
+Dreaming 是后台离线记忆整合机制(详见下文 [Dreaming 睡时记忆整理](#dreaming-睡时记忆整理))。默认关闭,需显式开启。
+
+```yaml
+memory:
+ dreaming:
+ agent:
+ enabled: false
+ interval_seconds: 14400 # 默认 4 小时
+ code:
+ enabled: false
+ interval_seconds: 14400
+```
+
+环境变量覆盖:
+
+| 变量 | 说明 |
+|------|------|
+| `DREAMING_AGENT_ENABLED` | Agent 模式 Dreaming 开关 |
+| `DREAMING_CODE_ENABLED` | Code 模式 Dreaming 开关 |
+| `DREAMING_INTERVAL` | 扫描间隔(秒),覆盖两种模式 |
+
+LLM 调用复用 `models.default`,无需额外模型配置。
+
---
## 内置记忆文件结构
@@ -131,17 +156,41 @@ memory:
## 架构概览
+记忆系统的三条写入通道相互独立、并行运作,共用同一个存储与检索层:
+
```
-用户 / Agent
- │
- ▼
-MemoryIndexManager 长期记忆管理
- ├── 记忆持久化(文件工具)
- ├── 文件监控(watchdog)
- ├── 语义搜索(向量 + BM25)
- └── 文件读取(按需加载)
+ 用户 / Agent
+ │
+ ┌───────────────────┼───────────────────┐
+ ↓ ↓ ↓
+ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐
+ │ 会话内即时 │ │ Dreaming │ │ Agent Team │
+ │ 写入 │ │ 后台离线提取 │ │ 协同提取 │
+ │ │ │ │ │ │
+ │ Agent 主动 │ │ Orchestrator │ │ Leader 在 │
+ │ 调用工具 │ │ 周期触发 LLM │ │ round 末触发 │
+ └──────┬───────┘ └──────┬───────┘ └──────┬───────┘
+ │ │ │
+ └──────────────────┼──────────────────┘
+ ↓ 写入 Markdown
+ ┌─────────────────────────────────────┐
+ │ MemoryIndexManager(共用索引层) │
+ │ 持久化 / 监控 / 混合检索 / 按需读取 │
+ └────────────────┬────────────────────┘
+ ↑ 检索 / 读取
+ 用户 / Agent
```
+> 索引层内部细节见下文「技术架构」。
+
+| 通道 | 触发方 | 写入时机 | 典型目标 |
+|------|--------|----------|----------|
+| 会话内即时写入 | Agent 自主调用工具 | 对话进行中 | `MEMORY.md` / `USER.md` / `YYYY-MM-DD.md` / `coding_memory/*.md` |
+| Dreaming 离线提取 | 进程内 Orchestrator | 闲时周期触发 | `DREAMING.md` / `consolidated_{hash}.md` |
+| Agent Team 协同 | Leader 在 round 结束触发 | 团队 round 完成后 | 团队 `TEAM_MEMORY.md` + 各成员个人记忆 |
+
+三条通道共用同一个 **MemoryIndexManager** 做索引与检索,从而 Agent 读取时无需关心"这条记忆是哪条通道写入的"。
+
### 长期记忆管理能力
| 能力 | 说明 |
@@ -199,3 +248,79 @@ Query ──► FTS5 Search ──────────────┘
搜索结果按混合分数排序:`score = vectorWeight * vectorScore + textWeight * textScore`。默认向量检索权重 0.7,全文检索权重 0.3。
+
+
+## Dreaming 睡时记忆整理
+
+除了 Agent 在对话中即时写入外,JiuwenClaw 还提供 **Dreaming** 机制:闲时周期性扫描历史会话,调用 LLM 提取值得长期保留的内容,自动写入长期记忆文件。Agent 与 Code 两种模式共用同一管道。
+
+| 模式 | 抽取目标 | 输出文件 |
+|------|----------|----------|
+| `agent` | 用户偏好、背景、关注领域 | `{workspace}/memory/DREAMING.md`(单文件,最多 50 条,超限淘汰最旧) |
+| `code` | 调试根因、API 边界行为、设计决策等可复用技术经验 | `{workspace}/coding_memory/consolidated_{hash}.md`(每条独立文件,按内容 SHA256 去重) |
+
+启用方式见上文 [配置说明 → Dreaming 配置](#dreaming-配置)。
+
+### 工作机制
+
+- **定时调度**:进程内 Orchestrator 按 `interval_seconds` 周期触发,启动后延迟 120s 再首次执行
+- **忙碌退避**:Agent 正在处理请求时跳过本次扫描,下一周期再试
+- **增量扫描**:通过 checkpoint 记录已处理 session,不重复处理;不足 4 轮对话或超过 30 天的会话跳过
+- **成本控制**:单次最多处理 10 个 session,压缩后限制 30K tokens;每个 session 最多产出 5 条
+- **失败重试**:LLM 调用失败的 session 不标记,下次重试;JSON 解析失败则跳过
+
+与上面"记忆写入触发"的区别:前者依赖 Agent 在会话内即时判断"该记什么",Dreaming 则在闲时回看完整对话再决定,是离线沉淀通道。
+
+
+## Agent Team 团队记忆
+
+Agent Team 模式下,每个团队拥有双层记忆:
+
+| 层 | 谁可访问 | 写入方 |
+|----|---------|--------|
+| 个人记忆 | 该成员独占 | 成员自身(在会话中调用记忆工具) |
+| 团队记忆 | 所有成员只读 | Leader 在 round 结束后由提取 agent 自动写入 |
+
+### 团队生命周期与记忆行为
+
+| 生命周期 | 个人记忆 | 团队记忆 | 适用场景 |
+|---------|---------|---------|---------|
+| **临时团队(temporary)** | 只读访问父 agent 的 workspace 记忆 | 无 | 单次任务、即用即弃 |
+| **持久团队(persistent)** | 每个成员独立读写 | 自动提取 + 跨 round 累积 | 长期协作、需经验沉淀 |
+
+### 数据布局
+
+```
+~/.openjiuwen/.agent_teams/{team_name}/
+├── team-memory/ # 团队共享记忆
+│ └── TEAM_MEMORY.md
+├── workspaces/
+│ ├── alice_workspace/ # 新成员(团队内创建)
+│ │ ├── memory/ # general 模式个人记忆
+│ │ └── coding_memory/ # coding 模式个人记忆
+│ └── bob_workspace -> ~/.openjiuwen/bob_workspace/ # 旧成员 symlink
+└── team-workspace/ # 团队共享文件区(非记忆)
+```
+
+- **新成员**:在 team home 下新建 workspace,记忆从零积累
+- **旧成员(predefined)**:通过 symlink 指向已有的个人 workspace,原有记忆自动延续
+- 个人记忆使用 `memory/` 还是 `coding_memory/` 由团队创建时的 scenario(`general` / `coding`)决定
+
+### 团队记忆自动提取
+
+持久团队的 **Leader** 在每个 round 结束后会 spawn 一个提取 agent,从本 round 的任务记录和团队消息中提炼出值得长期保留的内容,更新到 `TEAM_MEMORY.md`。提取分四类:
+
+| 标签 | 含义 |
+|------|------|
+| `[decision]` | 团队决策:为何选 A 不选 B、关键权衡 |
+| `[lesson]` | 经验教训:什么有效、什么导致返工、可复用的模式 |
+| `[member]` | 成员特长:谁擅长什么、谁负责哪个领域 |
+| `[context]` | 项目背景:业务约束、截止日期、利益相关方要求 |
+
+提取 agent 自主完成"读已有记忆 → 分析本轮记录 → 合并/更新/淘汰",文件保持在 200 行以内。临时团队不触发提取。
+
+### 隔离
+
+- **跨团队**:每个团队的 `team_name` 进入存储路径和索引 key(`agent_id = "{team_name}.{member_name}"`),不同团队的同名成员记忆相互不可见
+- **跨成员**:每个成员有独立索引实例,看不到对方个人记忆,仅共享团队记忆
+- **临时团队**:只读访问父 workspace,无法污染源记忆