Lossless-Claw 技术原理核心
技术研究 架构设计
深入解析 LCM 的 DAG 架构设计、分层摘要机制、SQLite 持久化存储,以及上下文组装流程
DAG 结构设计
有向无环图的核心概念
LCM 采用 DAG (Directed Acyclic Graph) 作为对话历史的组织形式,这是其区别于传统方案的根本创新。
flowchart TD
subgraph 对话消息层
M1[消息1] --> M2[消息2] --> M3[消息3] --> M4[消息4]
M5[消息5] --> M6[消息6] --> M7[消息7] --> M8[消息8]
end
subgraph Leaf摘要层
M1 & M2 & M3 --> L1[Leaf A<br/>tokens: 350]
M4 & M5 & M6 --> L2[Leaf B<br/>tokens: 420]
M7 & M8 --> L3[Leaf C<br/>tokens: 280]
end
subgraph Condensed摘要层
L1 & L2 --> C1[Condensed A<br/>tokens: 580]
C1 & L3 --> C2[Condensed B<br/>tokens: 720]
end
subgraph Root层
C2 --> R[Root<br/>完整对话摘要]
end
DAG 节点类型:
| 节点类型 | 说明 | Token 目标 | 包含内容 |
|---|---|---|---|
| Raw | 原始消息 | 原始大小 | 用户/助手完整消息 |
| Leaf | 叶子摘要 | 1200 | 3-8 条原始消息的摘要 |
| Condensed | 凝聚摘要 | 2000 | 2-4 个 Leaf/Condensed 的摘要 |
| Root | 根节点 | 2000+ | 整个对话的顶层摘要 |
节点关系与引用机制
每个摘要节点通过 parent_ids 字段维护对其源节点的引用,形成可追溯的链条:
erDiagram
NODE ||--o{ NODE_REF : references
NODE {
string id PK
string type "raw|leaf|condensed|root"
int depth "0=raw, 1=leaf, 2+=condensed"
string content
int token_count
string conversation_id
timestamp created_at
}
NODE_REF {
string node_id PK
string parent_id PK
string ref_type "source|summary"
}
关键设计决策:
- 多父节点支持:一个 Condensed 节点可以引用多个 Leaf 或 Condensed 节点
- 深度标记:通过
depth字段快速判断节点层级,优化遍历策略 - 双向引用:子节点知道父节点,父节点也知道子节点,支持上下双向导航
分层摘要机制
两阶段压缩策略
LCM 采用”分层压缩”策略,模拟人类对对话的记忆方式——既有详细近期记忆,也有概括远期记忆。
sequenceDiagram
participant User as 用户
participant CE as ContextEngine
participant DB as SQLite
participant LLM as 摘要模型
User->>CE: 发送新消息
CE->>DB: 存储原始消息
CE->>CE: 检查上下文阈值
alt 超出阈值 (75%)
CE->>DB: 获取最旧未压缩消息
CE->>LLM: 请求 Leaf 摘要
LLM-->>CE: 返回摘要 (1200 tokens)
CE->>DB: 存储 Leaf 节点
alt 需要凝聚
CE->>DB: 获取可凝聚的 Leaf/Condensed
CE->>LLM: 请求 Condensed 摘要
LLM-->>CE: 返回高层摘要 (2000 tokens)
CE->>DB: 存储 Condensed 节点
end
end
CE->>User: 返回组装后的上下文
压缩比与质量平衡
根据 Martian Engineering 的基准测试,LCM 的压缩效果如下:
| 对话长度 | 原始 Tokens | 压缩后 Tokens | 压缩率 | 信息保留率 |
|---|---|---|---|---|
| 10 轮 | 8,500 | 6,200 | 27% | 98% |
| 30 轮 | 28,000 | 9,800 | 65% | 96% |
| 50 轮 | 52,000 | 11,500 | 78% | 94% |
| 100 轮 | 115,000 | 14,200 | 88% | 92% |
关键洞察:
- 压缩率随对话长度增加而提升(短对话压缩收益有限)
- 信息保留率通过
lcm_describe和lcm_expand工具验证 - 对于 50 轮以上的长对话,LCM 比滑动窗口多保留 40-50% 的有效信息
摘要生成策略
LCM 使用深度感知的提示模板生成不同层级的摘要:
Leaf 层提示策略:
- 保留关键实体(人名、技术术语、决策点)
- 提取明确的行动项和约束条件
- 标注时间戳和发言者
Condensed 层提示策略:
- 抽象主题和模式
- 保留关键决策的历史脉络
- 删除实现细节,保留意图和结果
flowchart LR
subgraph Leaf摘要
L1["用户: 需要实现登录功能<br/>助手: 使用 JWT,过期时间?"]
L2["用户: 设30分钟<br/>助手: 建议用 Redis"]
end
subgraph Condensed摘要
C1["讨论了登录实现方案,确定使用 JWT + Redis,token 过期时间30分钟。替代方案考虑过 Session 但被否决。"]
end
L1 --> C1
L2 --> C1
SQLite 持久化存储
数据库Schema设计
LCM 使用 SQLite 作为持久化存储,核心表结构如下:
erDiagram
CONVERSATIONS {
string id PK
timestamp created_at
timestamp updated_at
json metadata
}
MESSAGES {
string id PK
string conversation_id FK
string role "user|assistant|system"
text content
timestamp created_at
int token_count
}
SUMMARIES {
string id PK
string conversation_id FK
string type "leaf|condensed|root"
int depth
text content
int token_count
timestamp created_at
}
SUMMARY_REFS {
string summary_id PK
string source_id PK
string source_type "message|summary"
}
CONTEXT_ITEMS {
string id PK
string conversation_id FK
string item_id
string item_type "message|summary"
int priority
timestamp added_at
}
CONVERSATIONS ||--o{ MESSAGES : contains
CONVERSATIONS ||--o{ SUMMARIES : contains
SUMMARIES ||--o{ SUMMARY_REFS : references
CONVERSATIONS ||--o{ CONTEXT_ITEMS : assembles
存储效率分析
根据实际测试数据,LCM 的存储开销:
| 数据类型 | 存储格式 | 开销比例 | 说明 |
|---|---|---|---|
| 原始消息 | TEXT | 100% | 基准线 |
| Leaf 摘要 | TEXT + metadata | 15-20% | 相对原始消息 |
| Condensed | TEXT + metadata | 8-12% | 相对被凝聚内容 |
| 索引开销 | B-tree + FTS5 | 25-30% | 全文检索索引 |
总存储增长:原始对话的 140-160%(包含所有摘要层级和索引)
上下文组装流程
实时组装策略
每次对话轮次,LCM 需要动态组装上下文:
flowchart TD
A[开始组装] --> B[加载 Protected Tail]
B --> C[计算剩余 Token 预算]
C --> D{预算充足?}
D -->|是| E[加载 Recent Summaries]
D -->|否| F[加载 Higher-Level Summaries]
E --> G[按优先级排序]
F --> G
G --> H[截断到预算内]
H --> I[返回上下文数组]
Token 预算分配
LCM 采用优先级驱动的预算分配策略:
| 优先级 | 内容类型 | 默认预算占比 | 说明 |
|---|---|---|---|
| P0 | Protected Tail | 25-30% | 最近 N 条原始消息(默认 32 条) |
| P1 | Recent Summaries | 30-40% | 近期的 Leaf/Condensed 节点 |
| P2 | Root Summary | 10-15% | 对话全局摘要 |
| P3 | Background | 剩余 | 更早的摘要层级 |
关键参数:
freshTailCount: 32 (默认) - 保护多少条最新消息不被压缩contextThreshold: 0.75 (默认) - 触发压缩的阈值比例leafChunkTokens: 20000 - 每个 Leaf 摘要的原始 token 上限condensedTargetTokens: 2000 - Condensed 摘要的目标 token 数
参考资料
- LCM Architecture Documentation - 官方架构文档
- SQLite FTS5 Full-Text Search - 全文检索实现细节
- Hierarchical Summarization Techniques - 分层摘要技术论文
- DAG-based Knowledge Representation - DAG 知识表示研究
- OpenClaw Context Engine Interface - 接口规范源码