层次化存储管理

📎 引用文件

本文引用的文件 - persistent.py - hierarchy.py - lifecycle.py - compression.py - search_index.py - semantic_links.py - accessor.py - env_schema.py

目录

  1. 简介
  2. 项目结构
  3. 核心组件
  4. 架构总览
  5. 详细组件分析
  6. 依赖关系分析
  7. 性能考量
  8. 故障排查指南
  9. 结论
  10. 附录

简介

本文件为 Vibe-Trading 的“层次化存储管理系统”提供系统化、可操作的文档。系统围绕三类存储层级组织记忆数据: - 内存层(热路径):进程内索引与快照,用于快速写入与检索。 - 磁盘层(温路径):持久化的 Markdown 条目、FTS5 全文索引、语义链接侧车文件、层级目录路由。 - 归档层(冷路径):压缩后的摘要与原始备份,支持长期保留与容量治理。

重点覆盖: - 多级存储的职责划分、迁移策略、一致性保证与访问路径优化。 - 存储节点生命周期管理、容量监控与自动清理机制。 - 不同数据类型在各层的分布策略与检索效率优化。 - 配置参数调优指南与性能监控方案。 - 架构图、数据流转图、故障恢复与备份策略。

项目结构

存储子系统位于 agent/src/memory 下,按职责拆分为: - 持久化与索引:persistent.py、search_index.py - 层级路由与扫描:hierarchy.py - 生命周期与清理:lifecycle.py - 压缩与归档:compression.py - 语义关联:semantic_links.py - 配置读取:config/accessor.py、config/env_schema.py

graph TB subgraph "应用调用方" A["Agent/工具/服务"] end subgraph "内存层" M1["PersistentMemory<br/>内存快照/去重窗口"] M2["MemorySearchIndex<br/>SQLite FTS5(可选)"] end subgraph "磁盘层" D1["MemoryHierarchy<br/>类别目录路由"] D2["Markdown 条目<br/>MEMORY.md 索引"] D3["SemanticLinker<br/>.relations.json"] end subgraph "归档层" G1["CompressionPipeline<br/>raw->daily->digest"] G2["archive/ 原始备份"] end A --> M1 M1 --> D1 M1 --> D2 M1 --> M2 M1 --> D3 M1 --> G1 G1 --> G2

图表来源 - persistent.py:196-578 - hierarchy.py:34-176 - search_index.py:113-303 - semantic_links.py:158-319 - compression.py:160-334

章节来源 - persistent.py:196-578 - hierarchy.py:34-176 - search_index.py:113-303 - semantic_links.py:158-319 - compression.py:160-334

核心组件

章节来源 - persistent.py:196-578 - hierarchy.py:34-176 - lifecycle.py:71-318 - compression.py:160-334 - search_index.py:113-303 - semantic_links.py:158-319

架构总览

下图展示一次“写入+索引+链接+压缩”的端到端流程,体现各层协作与一致性保障。

sequenceDiagram participant App as "调用方" participant PM as "PersistentMemory" participant MH as "MemoryHierarchy" participant SI as "MemorySearchIndex" participant SL as "SemanticLinker" participant CP as "CompressionPipeline" participant FS as "文件系统" App->>PM : add(name, content, type) PM->>MH : route_entry(type, slug.md) MH-->>PM : 目标路径 PM->>FS : 写入带 frontmatter 的 .md PM->>SI : index_entry(id, title, desc, keywords, body) PM->>SL : discover_links + save_relations Note over PM,CP : GC周期或定时任务触发 App->>PM : find_relevant(query) PM->>SI : search(query) alt FTS5命中 SI-->>PM : 匹配结果 else 回退 PM-->>PM : 本地token打分+重要性加权 end PM-->>App : 返回条目

图表来源 - persistent.py:462-578 - persistent.py:358-438 - hierarchy.py:70-90 - search_index.py:207-303 - semantic_links.py:179-230 - compression.py:168-192

详细组件分析

持久化与索引(PersistentMemory)

flowchart TD Start(["写入入口"]) --> Route["route_entry 确定路径"] Route --> WriteMD["写入 .md 文件"] WriteMD --> UpdateIdx["更新 MEMORY.md 索引"] UpdateIdx --> FTS{"启用FTS5?"} FTS -- 是 --> IndexFTS["index_entry"] FTS -- 否 --> Links{"启用语义链接?"} IndexFTS --> Links Links -- 是 --> Discover["discover_links + save_relations"] Links -- 否 --> End(["完成"]) Discover --> End

图表来源 - persistent.py:462-578 - hierarchy.py:70-90

章节来源 - persistent.py:196-578 - persistent.py:358-438

层级路由与扫描(MemoryHierarchy)

classDiagram class MemoryHierarchy { +base_dir Path +route_entry(memory_type, filename) Path +scan_all() Path[] +scan_category(category) Path[] +rebuild_index(entries) void +prune_search_scope(query_tokens, category_filter) Path[] +migrate_flat_entry(file_path, memory_type) Path? }

图表来源 - hierarchy.py:34-176 - hierarchy.py:200-261 - hierarchy.py:313-379 - hierarchy.py:381-436

章节来源 - hierarchy.py:34-176 - hierarchy.py:200-261 - hierarchy.py:313-379 - hierarchy.py:381-436

生命周期与清理(MemoryLifecycle)

flowchart TD S(["run_gc(dry_run)"]) --> Load["列出所有条目"] Load --> Age{"是否超过最小年龄?"} Age -- 否 --> Next["下一个条目"] Age -- 是 --> Imp["计算importance"] Imp --> Decide{"低于阈值?"} Decide -- 是 --> Action{"归档 or 删除"} Decide -- 否 --> Next Action --> Exec{"dry_run?"} Exec -- 是 --> Log["记录决策"] Exec -- 否 --> Do["执行归档/删除"] Do --> Compress{"启用压缩?"} Compress -- 是 --> Apply["apply_compression + 写回"] Compress -- 否 --> Next Log --> Next Apply --> Next Next --> |遍历结束| End(["返回动作列表"])

图表来源 - lifecycle.py:183-273 - lifecycle.py:275-318 - compression.py:168-192

章节来源 - lifecycle.py:71-318

压缩与归档(CompressionPipeline)

flowchart TD In(["should_compress(level, last_accessed, now)"]) --> Check{"是否达到阈值?"} Check -- 否 --> None(["不压缩"]) Check -- 是 --> Target{"target = daily/digest"} Target --> Archive["archive_original(原子复制)"] Archive --> Build{"构建压缩内容"} Build --> Daily["compress_to_daily(TF-IDF句子)"] Build --> Digest["compress_to_digest(top关键词)"] Daily --> Ret["estimate_retention"] Digest --> Ret Ret --> Out(["返回压缩文本"])

图表来源 - compression.py:168-192 - compression.py:194-256 - compression.py:258-334

章节来源 - compression.py:160-334

全文检索(MemorySearchIndex)

sequenceDiagram participant Q as "查询方" participant SI as "MemorySearchIndex" participant DB as "SQLite(FTS5)" Q->>SI : search(query) SI->>DB : MATCH query DB-->>SI : 命中列表 SI-->>Q : MemoryMatch[] Note over SI,DB : 若FTS5不可用,返回空列表供上层回退

图表来源 - search_index.py:147-196 - search_index.py:252-303 - search_index.py:304-355

章节来源 - search_index.py:113-303 - search_index.py:304-355

语义链接(SemanticLinker)

classDiagram class SemanticLinker { +discover_links(entry_title, entry_tokens, all_entries_data, top_k) (filename,score)[] +save_relations(entry_path, links) void +load_relations(entry_path) (filename,score)[] +resolve_wikilinks(body) str[] +get_relation_path(entry_path) Path +remove_relations(entry_path) void }

图表来源 - semantic_links.py:158-319 - semantic_links.py:321-372

章节来源 - semantic_links.py:158-372

依赖关系分析

graph LR PM["PersistentMemory"] --> MH["MemoryHierarchy"] PM --> SI["MemorySearchIndex"] PM --> SL["SemanticLinker"] PM --> CP["CompressionPipeline"] LC["MemoryLifecycle"] --> PM LC --> CP

图表来源 - persistent.py:462-578 - lifecycle.py:183-273

章节来源 - persistent.py:462-578 - lifecycle.py:183-273

性能考量

[本节为通用性能指导,无需特定文件来源]

故障排查指南

章节来源 - persistent.py:41-73 - search_index.py:147-196 - compression.py:258-334 - semantic_links.py:282-319 - hierarchy.py:92-143

结论

该层次化存储系统通过“内存层—磁盘层—归档层”的分层设计,实现了高效、可靠、可扩展的记忆数据管理。借助层级路由、FTS5 全文检索、语义链接与三级压缩,系统在吞吐、检索质量与存储成本之间取得良好平衡。配合生命周期管理与 GC 策略,能够自适应地维护数据热度与容量健康。建议在生产环境开启 FTS5 与压缩,并根据业务负载调优阈值与频率。

[本节为总结性内容,无需特定文件来源]

附录

配置参数与调优指南

章节来源 - env_schema.py:461-577 - accessor.py:52-76 - persistent.py:21-33 - compression.py:23-36 - lifecycle.py:92-98

数据流转流程图(写入与检索)

flowchart LR W["写入"] --> R["层级路由"] R --> P["持久化.md"] P --> I["更新索引(MEMORY.md)"] I --> F["FTS5索引(可选)"] I --> L["语义链接(可选)"] F --> S["检索"] L --> S S --> |命中| O["返回条目"] S --> |未命中| B["本地打分回退"] B --> O

图表来源 - persistent.py:462-578 - persistent.py:358-438 - hierarchy.py:70-90 - search_index.py:207-303 - semantic_links.py:179-230

故障恢复与备份策略

章节来源 - compression.py:258-334 - search_index.py:304-355 - persistent.py:609-637 - semantic_links.py:232-281