代理记忆中的双时间溯源:我们何时、为何以及相信了什么

1 分•作者: shanrizvi•3 个月前
CozoDB,一个用 Rust 编写的、嵌入 Datalog 的事务性关系-图-向量数据库,于 2024 年 12 月停止维护。我们将其硬分叉为 MnesticDB(非 CozoDB 官方项目),并采用 MPL-2.0 许可协议,以延续 Ziyang Hu 和 Cozo 项目作者构建“AI 的海马体”或代理记忆的愿景。 但代理记忆并非仅仅是一堆当前事实和过去决策的日志。它必须能够追踪随时间发生的变化,并且是可审计的。我们最近发布了多项使之成为可能的新功能。 首先,我们区分了有效时间(事实在世界中成立的时间)和事务时间(数据库何时相信该事实)。这实现了时间旅行,允许在任何时间点审计记忆,并查看任何过去的决策是基于何种知识。每一次写入都从一个防崩溃的单调提交时钟获取其事务时间,这是一个持久化在同一事务中的原子高水位标记,该事务同时提交数据。因此,即使在崩溃后,时钟也无法在写入未成功的情况下前进。由于时间戳是在提交关键部分分配的,事务时间顺序等于提交顺序等于可见性顺序,这使得时间旅行变得可靠。 接着,我们实现了 Green 等人 2007 年论文中的半环溯源框架:通过交换组合运算符,同一个递归规则可以计算存在性、成本、置信度或支持证据,而无需为每个应用程序定制跟踪系统。实际上,像 `min_cost_k` 这样的聚合不仅返回一个答案,还返回其背后的 k 个最佳推导,每个推导都附带证明其合理性的证据链。由于这些注解是普通值,将推导具体化为事务时间关系可以将这两个特性组合成一个带注解的信念历史。每个推导都带有我们相信它的事务时间,因此“as-of”读取不仅告诉你我们在 T 时刻的信念,还告诉你为什么。 之后,我们进行了一些其他改进: - 添加了 `::kill` 和 `:timeout`,以提供中断长时间运行查询的能力。 - 实现了一个确定性的贪婪连接重排序,以防止 LLM 可能编写的、无脑排序的合取式查询导致性能问题。在结果相同的情况下,性能提升了 54.5 倍。 - 添加了一个可选的 Yannakakis 风格的每键因子化 `count()`,取代了连接枚举,性能提升了 4-342 倍,结果完全相同。 MnesticDB 已在 crates.io 和 PyPI 上发布。 [https://crates.io/crates/mnestic](https://crates.io/crates/mnestic) [https://pypi.org/project/mnestic/](https://pypi.org/project/mnestic/) 如果您正在构建代理记忆,并希望在图数据库中集成时间旅行和溯源功能,而不是事后添加,我很乐意与您交流心得。 特别感谢 Matthias Autrata 提供的宝贵反馈和指导,这对实现上述改进至关重要。
查看原文
CozoDB, a transactional relational-graph-vector database with embedded Datalog in Rust, went dormant in December 2024. We hard-forked it as MnesticDB (not official CozoDB), under an MPL-2.0 license, to continue Ziyang Hu and the Cozo Project Authors&#x27; vision of building a &quot;Hippocampus for AI&quot;, or agentic memory.<p>But agentic memory isn&#x27;t a pile of current facts with a log of past decisions. It has to track change over time, and be auditable. We&#x27;ve recently shipped several features that make this possible.<p>First, we added a distinction between valid time (when a fact is true about the world) and transaction time (when the database came to believe it). This allows time travel, the ability to audit the memory at any point and see what knowledge any past decision was based on. Every write draws its transaction time from a crash-safe monotone commit clock, an atomic high-water mark persisted inside the same transaction that commits the data, so the clock can never advance without the write landing, even across a crash. Because the stamp is allocated in the commit critical section, transaction-time order equals commit order equals visibility order, which makes time travel sound.<p>Then we implemented the semiring provenance framework from Green et al.&#x27;s 2007 paper: the same recursive rule computes existence, cost, confidence, or supporting evidence just by swapping the combine operator, instead of a bespoke tracking system per application. In practice, an aggregate like min_cost_k returns not just an answer but the k best derivations behind it, each with the evidence chain that justifies it. And because those annotations are ordinary values, materializing a derivation into a transaction-time relation composes the two features into an annotated belief history. Every derivation carries the transaction time we came to believe it, so an as-of read tells you not just what we believed at T, but why.<p>Followed by some more improvements:<p>- Added ::kill and :timeout to provide the ability to interrupt long-running queries.<p>- Implemented a deterministic greedy join reorder to prevent naively-ordered conjunctions, the kind that an LLM may write, from spinning. 54.5x improvement with identical results.<p>- Added an opt-in Yannakakis-style per-key factorized count() instead of join enumeration, with a 4-342x improvement with identical results.<p>It&#x27;s available on crates.io and PyPI.<p>https:&#x2F;&#x2F;crates.io&#x2F;crates&#x2F;mnestic https:&#x2F;&#x2F;pypi.org&#x2F;project&#x2F;mnestic&#x2F;<p>If you&#x27;re building agent memory and want time-travel + provenance in the graph database rather than bolted on, I&#x27;d love to exchange notes.<p>Special thanks to Matthias Autrata for providing valuable feedback and guidance that was critical to implementing the above improvements.