跳转到内容

睡眠机制

浅睡是增量提取通道(语义 + 感性 + 自传体);深睡优化语义记忆库存。

睡眠是数字生命的记忆整理机制,类比人类的睡眠——大脑在夜间回放白天的经历,将临时记忆转化为长期存储,整理过程本身消散,不留痕迹。

  1. 内部机制,不留痕迹 — 睡眠在后台运行,不写入 session,不影响对话流
  2. 不照搬人类的节奏 — 触发基于系统需求(cron),非实时
  3. 两级分层 — 浅睡(增量写入)、深睡(语义库存优化)
  4. 身份上下文 — 所有记忆处理必须携带自我层六块 + 常驻记忆(见 self-layer.md
MechanismStatusNotes
Sleep cycle pipeline✅ ImplementedIn-process Bun.cron builtin-sleep-cycle @ 02:00 CST(不经 PG cron_jobs
Session cleanup✅ ImplementedStep conversation-cleanup before light-sleep in sleep-cycle DAG
Light sleep (in-cycle)✅ ImplementedStep light-sleep in sleep-cycle DAG
Deep sleep (in-cycle)✅ ImplementedStep deep-sleep, depends on light-sleep
Memory ref sync✅ ImplementedStep memory-ref-sync, depends on deep-sleep
Self-layer refresh✅ ImplementedStep self-layer-refresh, after light-sleep
Dream (in-cycle)✅ ImplementedStep dream, depends on light-sleep; parallel with deep-sleep
Temporal summary✅ ImplementedSteps 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)。

AttributeValue
TriggerSleep-cycle step light-sleep (cron @ 02:00 or Habitat diagnostics)
ScopeSessions with activity in previous calendar day (excludes cron-platform sessions)
InputFull day’s user conversations (user+assistant, tools stripped), segmented by conversation
OrchestrationThree 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.

两条记忆在语义上互相否定,且无法用时间变化解释 → 矛盾。

  • ✓ 矛盾:「女儿属虎」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-sync

DAG(宏观层):

conversation-cleanup
└─► light-sleep
├─► deep-sleep ──► memory-ref-sync
│ └─► temporal-summary-cascade
├─► dream
├─► self-layer-refresh
└─► temporal-summary-day

作为 sleep-cycle 第一步,在浅睡扫描昨日 sessions 之前运行。

条件结果
会话未绑定到 satellite app/instance拒绝
实例离线拒绝
工具未在已连接实例上注册拒绝
未注册的 sap_* 名称守卫处理器返回错误(不回退到 Habitat)

过期conversations.updated_at 早于 24 小时(墙钟 timestamptz,非 CST 日界)。近期空 session(如 satellite「新对话」尚无首条消息)保留至次夜运行。

宕机后,下次计划运行会补跑。

压缩保持会话级(回合时 advanceCompressionMeta);不是睡眠周期步骤。夜间巩固不替代每会话压缩。

For a single past CST calendar day (e.g. before go-live or after migration), use Habitat → Sleep (/habitat/dashboard/sleep`):

  1. Day 设为 YYYY-MM-DD
  2. 运行 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.

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 context

对话中的 memory_remember 是创建语义记忆的便捷封装。可通过 deprecate 软废弃;也支持物理删除。