记忆层模型
wesgine 七层记忆分层:闭域定义、可见性规则、写入约束与跨层关系。
七层闭域
AllMemoryLayers() 返回七个值:
| 层 | 英文名 | 含义 | 是否用户记忆 |
|---|---|---|---|
| L1 | environment | 宿主环境信息 | 是 |
| L2 | consensus | 部门/团队共识 | 是 |
| L3 | about_me | 关于用户个人 | 是 |
| L4 | agent_memory | 角色/Agent 记忆 | 是 |
| - | session | 会话态 | 否(运行时) |
| - | working | 工作缓存 | 否(运行时) |
| - | unknown | 未覆盖的 scope | 否(兜底) |
为什么是闭域
引擎 LayerOf(e) 单点判层。消费方一律消费不推导。
unknown 必须留在联合里——它是引擎的兜底出口,能从线上到达。
L1 环境信息(environment)
来源
宿主环境感知器(memenv.SharedSnapshot)自动产出。
特点
- 进程级共享(每进程感知一次)
- 不含租户信息(INV-ENV-02)
- 不许人工导入
- 不许人工写入(
ErrLayerNotWritable)
可见性
全 Cell 共享——无 actor 归属。
L2 部门共识(consensus)
来源
admin 手动写入。
特点
- Cell 级共享,无 actor 归属
- 写入时不能带 actor(
ErrLayerShared) - 多 actor Cell 中全员可见
典型内容
部门规章、工作流程约定、共享知识。
L3 关于我(about_me)
来源
用户交互中引擎提取。
特点
- actor 必填
UserNS(actor)命名空间- 按 actor 隔离
可见性
仅本 actor 可见——ActorVisible 判定。
L4 角色记忆(agent_memory)
来源
Agent 交互中记忆工具写入。
特点
- actor 必填
- namespace 必填(
ErrLayerNamespaceRequired) - 按
(agent, actor)双重隔离
命名空间
| 命名空间 | 可见性 |
|---|---|
agent://legal/* | 仅 agent-legal |
agent://legal/shared | agent-legal 内共享通道 |
Session / Working(运行时层)
| 层 | 说明 | 不许写入 |
|---|---|---|
session | 会话态记忆 | ErrLayerNotWritable |
working | 工作缓存 | ErrLayerNotWritable |
这两层是运行时产物,不是用户可直接编辑的记忆。
层判定单点(INV-MEM-49)
LayerOf(e)
一行一个答案。消费方不推导。
layer := memory.LayerOf(entry)
LayerClauses(layer)
按层查询的 SQL 谓词——与 LayerOf 由同一份定义生成。
Layer 与 Scope 互斥
?layer=about_me&scope=global → ErrMemoryLayerScopeConflict(400)
不做交集——两者只在调用方糊涂时才分歧。
可写层(INV-MEM-53)
三个可写层:
| 层 | 对应 Scope | 约束 |
|---|---|---|
consensus | ScopeGlobal | 不能带 actor |
about_me | ScopeGlobal | 必须带 actor |
agent_memory | ScopeAgent | 必须带 actor + namespace |
四类 400
| 错误 | 含义 |
|---|---|
ErrUnknownLayer | 层名不存在 |
ErrLayerNotWritable | environment/session/working |
ErrLayerShared | consensus 带 actor |
ErrLayerNamespaceRequired | agent_memory 空 namespace |
按层提问
列表
GET /memory?layer=about_me&actor=alice
层下推到引擎 SQL,不在客户端过滤。
计数
GET /memory/counts?actor=alice
返回 Record<MemoryLayer, number>——键覆盖全部层含 0。
写入
POST /memory { "layer": "about_me", "content": "..." }
清空
DELETE /memory/layer?layer=about_me&actor=alice
Kind 与耐久性(INV-MEM-54)
耐久性是 Kind 的属性,不是 Formation 的属性。
持久 Kind
跨会话可见:
KindFactKindPreferenceKindCorrectionKindConventionKindLessonKindConstraintKindInstructionKindEnvironmentKindCollective
短暂 Kind
仅当前会话:
KindContext(会话摘要)KindNote(工作笔记)
Kind 必填
空 kind → ErrKindRequired。
Episodic kind 缺 SessionID → ErrSessionRequired。
Actor 边界
读侧单点
memory.ActorVisible(entry, actor)
写侧单点
// saveCore 内部
RequireActor(entry)
私有域无主 = 异常
ScopeAgent 无主条目是异常——缺主,不是共享。
共享域无主 = 正常
ScopeSystem / ScopeGlobal 无主是正常——L1/L2 共享资产。
消费方规则
前端
- 只读
layer字段 - 七层芯片恒定渲染(含 0)
- 空分组不渲染
- 禁止出现
scope/MemoryScope/owner
Go 后端
- 只传
?layer=给引擎 - 不自行判层(
layerOf等助手已删除) - actor 从 token 取,不从请求体取
相关文档
- 显式搜索 →
memory-explicit-search.md - GC 与 Digest →
memory-gc-digest.md - 写入管线 →
memory-write-pipeline.md - Actor 边界 →
actor-boundary.md - 上下文压缩 →
context-compression.md