Skip to main content

MatterLoop 记忆协议与内存实现

Project description

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、租户隔离、加密、备份和 可审计删除;不要用长期记忆的相似度索引代替状态存储。

公共数据结构速查
  • MemoryKindSEMANTICEPISODICPROCEDURAL
  • 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=10min_score=0,空 kinds 和空 filters 表示不追加过滤。
  • MemoryMatch(record, score):命中记录及后端给出的相关度。
  • InMemoryMemoryStoreNullMemoryStoreInMemoryCheckpointStore:本包提供的三个实现。

metadatacontent 和过滤值不会被自动脱敏。自定义后端应限制单条大小、可过滤字段和查询成本。

不在本包范围内

本包不提供 PostgreSQL、Redis、向量数据库、Embedding、跨进程锁或后台 TTL 清理。企业部署与数据 治理建议见企业集成指南,Loop 的恢复语义见 架构说明

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

matterloop_memory-0.1.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

matterloop_memory-0.1.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file matterloop_memory-0.1.0.tar.gz.

File metadata

  • Download URL: matterloop_memory-0.1.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for matterloop_memory-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0dd31530c7fd04f14fd5520abb215bc33f709e81fe594411a1720faa56a6b864
MD5 7e76b780f1a150df32a4615ce7328c06
BLAKE2b-256 bbffdac4ce2b5a4510c787a7f01e8519cf2b3843ead811aa2eac63f4d85a983f

See more details on using hashes here.

Provenance

The following attestation bundles were made for matterloop_memory-0.1.0.tar.gz:

Publisher: publish.yml on huleidada/matterloop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file matterloop_memory-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for matterloop_memory-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2e7f25742a4abf9e9014277ac0b92d66eddb5ddec99826a604333b1b0e7ebf47
MD5 66a310ac7cfea0a8e553d5e4c2a341ee
BLAKE2b-256 a9533d414072b0ba5429f7bdf8a2fa334d581d57cefde4d5d773dddceae43b0b

See more details on using hashes here.

Provenance

The following attestation bundles were made for matterloop_memory-0.1.0-py3-none-any.whl:

Publisher: publish.yml on huleidada/matterloop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page