Skip to main content

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

公共数据结构速查
  • 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.1.tar.gz (9.0 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.1-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

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

Hashes for matterloop_memory-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3968c1cee31c3035729f521a1cd6f56f2589667332d63b3419b2663116986bca
MD5 20a7d9ef70dad4531a22090526e34fed
BLAKE2b-256 ef895e83b0fe91210fd87b69fede18ad2450e562c73c29c253d981c4606ff598

See more details on using hashes here.

Provenance

The following attestation bundles were made for matterloop_memory-0.1.1.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.1-py3-none-any.whl.

File metadata

File hashes

Hashes for matterloop_memory-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ad465b003077a2615101b13ef6f3dca84b67085db921a91762e59476c0cc4aa1
MD5 8e5c7241efffc7b6a7b46dcf47c0c0c0
BLAKE2b-256 dd62efaa83ebcc1f0e8151cf34ff7c4ac042c85f5504f3bb04d6528312698641

See more details on using hashes here.

Provenance

The following attestation bundles were made for matterloop_memory-0.1.1-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