认知循环五件套
wesgine 引擎内核的五个子系统(Cognitive / Context / Execution / Memory / Govern)。
架构位置
五件套构成 Layer 2 Kernel——引擎的核心能力层:
┌──────────────────────────────────────────────────┐
│ Cell(数据面,厚) │
│ │
│ ┌─────────┐ ┌─────────┐ ┌───────────┐ │
│ │Cognitive│ │ Context │ │ Execution │ │
│ └────┬────┘ └────┬────┘ └─────┬─────┘ │
│ │ │ │ │
│ ┌────┴────┐ ┌────┴────┐ │
│ │ Memory │ │ Govern │ │
│ └─────────┘ └─────────┘ │
│ │
│ + Coordination(Loop 协调器) │
│ + TaskMemory │
└──────────────────────────────────────────────────┘
1. Cognitive(认知子系统)
负责与 LLM 通信。
职责
- Resolver 链构建与调度
- per-cell Health/RateLimit 追踪
- Provider fallback
- Retry / Backoff / ImageRecovery
核心接口
type Resolver interface {
Stream(ctx context.Context, cellID string, req Request) (Stream, error)
}
装配
cellchain.BuildCellCognitive 装配内层五层。
2. Context(上下文子系统)
负责构建送给模型的完整上下文。
职责
- Assembler 组装系统提示词
- Overlays 层叠(记忆注入、技能注入、file_card)
- CompressPipeline(上下文压缩)
- SessionState 注入(INV-CTX-43)
三个注入面
| 注入面 | 时机 | 内容 |
|---|---|---|
| Eager | 系统提示词 | 索引到的记忆 |
| Lazy | 当轮 overlay | 语义召回的记忆 |
| Cold-start | 跨会话重放 | 历史摘要 |
3. Execution(执行子系统)
负责工具调用的注册、分发与执行。
职责
- Registry 管理工具注册表
- Executor 分发工具调用
- builtin/ 内置工具实现(read/write/exec/grep 等)
工具六源
| 来源 | 归属层 |
|---|---|
| Tier E 内置工具 | Layer 2 Kernel |
| Tier C SDK 闭包 | Layer 3 Cell |
| MCP proxy | Layer 3 Cell |
| Adapter 派生 | Layer 3 Cell |
| Sub-agent as Tool | Layer 3 Cell |
| Per-run 临时 Tool | Layer 4 Actor |
4. Memory(记忆子系统)
负责记忆的存储、召回与生命周期管理。
职责
- Store 管理(Agent NS + Cell NS)
- 七层模型(INV-MEM-49)
- Actor 边界(INV-MEM-38~47)
- GC 与 Digest
- 威胁扫描(INV-MEM-24)
存储后端
双后端架构:
- SQLite 全文搜索(BM25)
- 向量 embedding(语义搜索)
5. Govern(治理子系统)
负责安全策略与合规。
三步管道
Hardline → Sandbox → Zone
| 步骤 | 职责 |
|---|---|
| Hardline | 灾难命令拦截(18 条正则,恒 Deny) |
| Sandbox | 数据边界(DenyPaths / WorkDir / NetworkPolicy) |
| Zone | 效果分类(Green/Yellow/Red)+ GovernMode 决策 |
Evidence
治理决策产出证据链(hash chain),不可事后篡改。
Coordination(协调器)
不是第六个子系统,而是 Loop 本身——runLoop 的 for{} 循环:
while !done:
context = Context.Assemble(messages, overlays)
response = Cognitive.Stream(context)
if response.tool_calls:
Govern.Check(tool_calls)
results = Execution.Execute(tool_calls)
Memory.Observe(results)
else:
done = true
Settlement()
Run 编排
internal/orchestration/ 管理 Run 的多 Agent 委派。
依赖方向
严格单向向内:
Shared Kernel ← 零依赖
Subsystem BC ← 只依赖 Shared Kernel
Coordination ← 依赖 Subsystem 接口
Infrastructure ← 实现 Subsystem 接口
Cell ← 组装以上一切
Hypervisor ← 管理 Cell 生命周期