MatterLoop 记忆协议与内存实现
Project description
简体中文 | English
matterloop-memory
matterloop-memory 处理两件容易被混为一谈的事:给 Agent 检索历史信息,以及给 Loop 保存恢复点。
它只提供协议和进程内实现,不替你选择数据库。
pip install matterloop-memory
先分清两类数据
| 数据 | 接口 | 用途 | 是否进入任务恢复链路 |
|---|---|---|---|
| 长期记忆 | MemoryStore |
保存事实、经历和操作规则,供 Agent 检索 | 否 |
| Loop 检查点 | CheckpointStore |
保存状态机、当前步骤、人工反馈和 revision | 是 |
这两类数据的保留期、访问权限和一致性要求不同。生产环境不要把它们塞进同一张“通用记忆表”。
最小用法
from matterloop_memory import (
InMemoryCheckpointStore,
InMemoryMemoryStore,
MemoryKind,
MemoryQuery,
MemoryRecord,
)
memory = InMemoryMemoryStore()
checkpoints = InMemoryCheckpointStore()
await memory.put(
MemoryRecord(
namespace="tenant/acme/project/docs",
kind=MemoryKind.SEMANTIC,
content="发布前必须由独立验证器验收",
metadata={"source": "engineering-policy"},
)
)
matches = await memory.search(
MemoryQuery(namespace="tenant/acme/project/docs", text="发布 验收", limit=5)
)
NullMemoryStore 用来明确关闭长期记忆,比在业务代码里传播 None 更容易测试。InMemoryCheckpointStore
可直接注入 AgentLoop(checkpoint_store=...)。
运行语义
namespace是查询条件,不是鉴权机制。应由可信身份派生,不能照搬客户端输入。InMemoryMemoryStore使用简单词项交集评分,不是向量检索;score不应与其他后端横向比较。- 过期记录在读取时被隐藏,但不会被后台回收。长时间运行的服务必须使用带清理策略的实现。
InMemoryCheckpointStore.save()使用 revision CAS。冲突意味着状态已被其他执行者推进,应重新读取, 不能直接覆盖。- 所有内存实现只保证单进程内的一致性,进程退出后数据丢失。
接入持久化后端
自定义长期记忆只需实现结构协议 MemoryStore:
class MemoryStore(Protocol):
async def put(self, record: MemoryRecord) -> None: ...
async def get(self, record_id: str) -> MemoryRecord | None: ...
async def search(self, query: MemoryQuery) -> tuple[MemoryMatch, ...]: ...
async def delete(self, record_id: str) -> bool: ...
async def clear(self, namespace: str) -> int: ...
持久化检查点则实现 matterloop_core.CheckpointStore。至少要提供原子 CAS、租户隔离、加密、备份和
可审计删除;不要用长期记忆的相似度索引代替状态存储。
公共数据结构速查
MemoryKind:SEMANTIC、EPISODIC、PROCEDURAL。MemoryRecord(namespace, kind, content, metadata, record_id, created_at, expires_at):一条完整记忆;record_id默认生成 UUID,created_at默认使用当前 UTC,expires_at=None表示不过期。MemoryQuery(namespace, text, kinds, limit, min_score, filters):检索条件;limit=10、min_score=0,空kinds和空filters表示不追加过滤。MemoryMatch(record, score):命中记录及后端给出的相关度。InMemoryMemoryStore、NullMemoryStore、InMemoryCheckpointStore:本包提供的三个实现。
metadata、content 和过滤值不会被自动脱敏。自定义后端应限制单条大小、可过滤字段和查询成本。
不在本包范围内
本包不提供 PostgreSQL、Redis、向量数据库、Embedding、跨进程锁或后台 TTL 清理。企业部署与数据 治理建议见企业集成指南,Loop 的恢复语义见 架构说明。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file matterloop_memory-0.1.1.tar.gz.
File metadata
- Download URL: matterloop_memory-0.1.1.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3968c1cee31c3035729f521a1cd6f56f2589667332d63b3419b2663116986bca
|
|
| MD5 |
20a7d9ef70dad4531a22090526e34fed
|
|
| BLAKE2b-256 |
ef895e83b0fe91210fd87b69fede18ad2450e562c73c29c253d981c4606ff598
|
Provenance
The following attestation bundles were made for matterloop_memory-0.1.1.tar.gz:
Publisher:
publish.yml on huleidada/matterloop
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
matterloop_memory-0.1.1.tar.gz -
Subject digest:
3968c1cee31c3035729f521a1cd6f56f2589667332d63b3419b2663116986bca - Sigstore transparency entry: 2210607913
- Sigstore integration time:
-
Permalink:
huleidada/matterloop@f7934d51c7d6a2a027d1f76420f690f622e625c1 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/huleidada
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f7934d51c7d6a2a027d1f76420f690f622e625c1 -
Trigger Event:
push
-
Statement type:
File details
Details for the file matterloop_memory-0.1.1-py3-none-any.whl.
File metadata
- Download URL: matterloop_memory-0.1.1-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ad465b003077a2615101b13ef6f3dca84b67085db921a91762e59476c0cc4aa1
|
|
| MD5 |
8e5c7241efffc7b6a7b46dcf47c0c0c0
|
|
| BLAKE2b-256 |
dd62efaa83ebcc1f0e8151cf34ff7c4ac042c85f5504f3bb04d6528312698641
|
Provenance
The following attestation bundles were made for matterloop_memory-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on huleidada/matterloop
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
matterloop_memory-0.1.1-py3-none-any.whl -
Subject digest:
ad465b003077a2615101b13ef6f3dca84b67085db921a91762e59476c0cc4aa1 - Sigstore transparency entry: 2210607945
- Sigstore integration time:
-
Permalink:
huleidada/matterloop@f7934d51c7d6a2a027d1f76420f690f622e625c1 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/huleidada
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f7934d51c7d6a2a027d1f76420f690f622e625c1 -
Trigger Event:
push
-
Statement type: