睡眠机制
浅睡是增量提取通道(语义 + 感性 + 自传体);深睡优化语义记忆库存。
睡眠是数字生命的记忆整理机制,类比人类的睡眠——大脑在夜间回放白天的经历,将临时记忆转化为长期存储,整理过程本身消散,不留痕迹。
- 内部机制,不留痕迹 — 睡眠在后台运行,不写入 session,不影响对话流
- 不照搬人类的节奏 — 触发基于系统需求(cron),非实时
- 两级分层 — 浅睡(增量写入)、深睡(语义库存优化)
- 身份上下文 — 所有记忆处理必须携带自我层六块 + 常驻记忆(见
self-layer.md)
| Mechanism | Status | Notes |
|---|---|---|
| Sleep cycle pipeline | ✅ Implemented | In-process Bun.cron builtin-sleep-cycle @ 02:00 CST(不经 PG cron_jobs) |
| Session cleanup | ✅ Implemented | Step conversation-cleanup before light-sleep in sleep-cycle DAG |
| Light sleep (in-cycle) | ✅ Implemented | Step light-sleep in sleep-cycle DAG |
| Deep sleep (in-cycle) | ✅ Implemented | Step deep-sleep, depends on light-sleep |
| Memory ref sync | ✅ Implemented | Step memory-ref-sync, depends on deep-sleep |
| Self-layer refresh | ✅ Implemented | Step self-layer-refresh, after light-sleep |
| Dream (in-cycle) | ✅ Implemented | Step dream, depends on light-sleep; parallel with deep-sleep |
| Temporal summary | ✅ Implemented | Steps temporal-summary-day / temporal-summary-cascade; see temporal-summary.md |
Sleep uses a macro DAG (sleep-cycle pipeline) orchestrated by
PipelineRunner (@freeanima/host/engine/pipeline). A single cron job
triggers the full cycle; step order and dependencies are explicit in code
(src/host/platform/boot/sleep-cycle.ts).
浅睡与深睡在 runLightSleep() / runDeepSleep() 内部保留多阶段/多轮顺序,不提升为宏观 DAG 节点。
Habitat (/habitat/dashboard/sleep) supports **diagnostic** runs: full cycle or individual steps (force` skips dependency checks). Deep sleep
mode (full vs incremental) can be selected before manual runs.
Pipeline step history is persisted in PG pipeline_step_run (one row per node execution, including failures and manual retries via attempt). Sleep-cycle scheduling is in-process Bun.cron (not listed under Habitat → Cron / cron_log); diagnostics remain on the Sleep dashboard.
流水线运行状态持久化于 ~/.anima/runtime/pipeline_sleep-cycle_run.json(步骤状态 SSOT;无
EventBus)。
| Attribute | Value |
|---|---|
| Trigger | Sleep-cycle step light-sleep (cron @ 02:00 or Habitat diagnostics) |
| Scope | Sessions with activity in previous calendar day (excludes cron-platform sessions) |
| Input | Full day’s user conversations (user+assistant, tools stripped), segmented by conversation |
| Orchestration | Three stages sequential (separate LLM calls each) |
| 阶段 | 目标 | 用途 |
|---|---|---|
| 1 语义 | 语义记忆 | 从对话提取事实、偏好、经历 |
| 2 感性 | 情感锚点 | 捕获 conversation 情绪与情感转折点 |
| 3 自传体 | 自传叙事 | 记录经历对数字生命的意义 |
| 3b | 自传概括(自我层) | 将叙事压缩为分组自我层大纲(按重要性仅标题 bullet) |
克制原则: 各阶段 LLM 可判断「无值得记录」→ 不写入;程序仍继续后续阶段。
去重(语义): 仅与同 source conversation 的已有记忆比较;跨脉络合并留给深睡。
| 属性 | 值 |
|---|---|
| 触发 | sleep-cycle 步骤 deep-sleep(DAG 中在 light-sleep 之后) |
| 目标 | 全部 active 语义记忆 |
| 操作 | 矛盾检测 + 过期标记、拆分、去重合并、置顶维护——四轮顺序执行 |
| 轮次 | 意图 |
|---|---|
| 1 | 矛盾检测 + 过期标记 |
| 2 | 拆分多事实条目 |
| 3 | 去重合并相似条目 |
| 4 | 置顶质量审查(解除过时置顶;常驻读仍上限 40 条) |
顺序理由: 先清理问题,再细化,再合并,最后审查置顶质量。
Incremental skip rules: When mode is incremental and no active memory has updated within the last 24 hours, rounds 1 and 3 are skipped. Round 2 runs only on pre-filtered split candidates (long/multi-sentence entries with recent updated). Round 4 (pin maintenance) always runs. Scheduled cron uses incremental Tue–Sun and full on Mondays; Habitat manual runs default to full but can select incremental.
矛盾定义(排他性)
Section titled “矛盾定义(排他性)”两条记忆在语义上互相否定,且无法用时间变化解释 → 矛盾。
- ✓ 矛盾:「女儿属虎」vs「女儿属羊」
- ✗ 不矛盾:「喜欢苹果」vs「喜欢樱桃」(可共存)
- ✗ 非矛盾(变化):「喜欢 Python」vs「现在更喜欢 TypeScript」
0 2 * * * sleep-cycle # in-process Bun.cron builtin-sleep-cycle: conversation-cleanup → light → deep ∥ dream ∥ self-layer-refresh → memory-ref-syncDAG(宏观层):
conversation-cleanup └─► light-sleep ├─► deep-sleep ──► memory-ref-sync │ └─► temporal-summary-cascade ├─► dream ├─► self-layer-refresh └─► temporal-summary-day会话清理(浅睡前)
Section titled “会话清理(浅睡前)”作为 sleep-cycle 第一步,在浅睡扫描昨日 sessions 之前运行。
| 条件 | 结果 |
|---|---|
| 会话未绑定到 satellite app/instance | 拒绝 |
| 实例离线 | 拒绝 |
| 工具未在已连接实例上注册 | 拒绝 |
未注册的 sap_* 名称 | 守卫处理器返回错误(不回退到 Habitat) |
过期指 conversations.updated_at 早于 24 小时(墙钟 timestamptz,非 CST 日界)。近期空 session(如 satellite「新对话」尚无首条消息)保留至次夜运行。
宕机后,下次计划运行会补跑。
压缩保持会话级(回合时 advanceCompressionMeta);不是睡眠周期步骤。夜间巩固不替代每会话压缩。
Historical Day (Habitat)
Section titled “Historical Day (Habitat)”For a single past CST calendar day (e.g. before go-live or after migration),
use Habitat → Sleep (/habitat/dashboard/sleep`):
- 将 Day 设为
YYYY-MM-DD - 运行 light-sleep 步骤(如需跳过依赖检查可勾选 Force)
Each step run is logged in the Pipeline history table on the sleep page
(pipeline_step_run.output; deep-sleep rows include per-round summaries and
change-log snapshots). Cross-session merge for that day still relies on a
subsequent deep-sleep run. Cron-triggered cycle runs appear in Habitat
→ Cron → Run history on the sleep-cycle task.
与现有架构的关系
Section titled “与现有架构的关系”Conversation archive │ sleep-cycle pipeline (02:00) │ step light-sleep (three internal stages) ├─► semantic memory ├─► emotional anchors └─► autobiographical narrative ──compress──► self-layer autobiography summary (grouped outline) │ │ step deep-sleep (four internal rounds) ▼semantic memory (consolidated) │ step memory-ref-sync │ memory_recall (real-time retrieval in conversation) ▼Agent identity and recalled fragments in current contextmemory_remember 工具
Section titled “memory_remember 工具”对话中的 memory_remember 是创建语义记忆的便捷封装。可通过 deprecate 软废弃;也支持物理删除。