Skip to main content

agent-memory-hygiene

English | 简体中文

Pure-policy write-time hygiene for agent memory: what to store, what to drop, and whose fact it is.

Problem

Most agent-memory projects optimize retrieval: embeddings, re-ranking, recency, similarity thresholds. Very few ask the cheaper question first: should this ever have been written?

A store that accepts everything becomes a log. Tool output, stack traces, pasted links, questions and greetings all drift in, and the only remaining defense is a higher similarity threshold at read time. That is a patch on the retrieval side, not a decision on the write side.

The second failure is attribution. An extractor turns "小雨的妈妈是老师" into "the user is a teacher", or "豆豆特别能吃" into "the user eats a lot". The fact is real; the subject is wrong. Retrieval cannot detect this, because the stored sentence looks perfectly normal.

This library covers both, with pure functions and no model calls.

At a glance:

Input Decision
"" / whitespace empty
Shorter than 8 chars too_short
Longer than 2000 chars too_long
More than 12 non-empty lines too_many_lines
Contains a traceback / WinError error_dump
Only a URL, little else link_only
Ends as a short question question
Short greeting chitchat
Everything else ok
"我的妈妈是老师" relation_subject flag
"豆豆特别能吃" (豆豆 is a known pet) pet_subject flag

Install

pip install -e .

Quick Start

from agent_memory_hygiene import MemoryHygieneConfig, assess_write, assess_attribution

verdict = assess_write("用户喜欢喝咖啡和茶")
print(verdict.ok, verdict.reason)        # True ok

verdict = assess_write("明天下午的会议是几点?")
print(verdict.ok, verdict.reason)        # False question

cfg = MemoryHygieneConfig(known_pets=("豆豆",))
risk = assess_attribution("豆豆特别能吃", cfg)
print(risk.shifted, risk.kind, risk.subject)  # True pet 豆豆

The Write Gate

The gate is deliberately simple. Each rule answers a question that does not need a model:

  • Process is not memory. Stack traces and tool output can be re-derived. Store the symptom and the root cause, not the transcript.
  • Volume is a signal. If a "memory" is longer than a screen, it is probably a transcript.
  • Questions are not facts. "What time is the meeting tomorrow?" is a request. Remembering it only pollutes retrieval.
  • Greetings are not facts. "ok", "嗯嗯", "好的好的" carry no information.

assess_write returns a WriteVerdict with ok, reason, the rule that fired and the metrics (chars, meaningful_chars, lines, urls). Log the reason; a silent drop is indistinguishable from a gate that never ran.

Subject Attribution

assess_attribution flags two shapes:

  • relation: X 的 <relation-noun> ... — the fact belongs to the relation, not to the speaker.
  • pet: a known pet name followed by a verb or adjective — the fact belongs to the pet.

It does not rewrite the text. A rewrite needs your extractor or a model, and a library should not silently change what gets stored. The flag is enough to stop the common failure.

Fail Open, Loudly

from agent_memory_hygiene import safe_assess_write

verdict = safe_assess_write(text)
if verdict.reason == "fail_open":
    ...  # the gate broke; the write was allowed and a warning was logged

A write gate must not block the main flow when the gate itself breaks. But "the gate allowed everything" and "the gate never loaded" look identical in logs, so the wrapper always logs the exception before allowing the write.

What it does NOT do

  • Calls no model.
  • Stores no memory and owns no database.
  • Connects to no vector store or embedding service.
  • Rewrites nothing; it only returns verdicts.

Where this kit deliberately stops

  • Importance. It cannot tell whether a fact matters to the user. That needs the caller's domain knowledge or a model.
  • Deduplication. Near-duplicate detection needs embeddings; this kit is zero-dependency on purpose.
  • Multilingual rules. The default patterns target Chinese text, which is where the attribution failures were observed. The thresholds are configurable, the phrase lists are not exhaustive.
  • Attribution rewriting. Flagging is cheap and safe; rewriting needs the extractor.

Test

pytest -q

License

MIT

Metadata

Release files for agent-memory-hygiene 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for agent-memory-hygiene 0.1.0
File Size Uploaded Provenance Actions
agent_memory_hygiene-0.1.0.tar.gz 11.1 kB Without provenance Details for agent_memory_hygiene-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for agent-memory-hygiene 0.1.0
File Interpreter ABI Platform Actions
agent_memory_hygiene-0.1.0-py3-none-any.whl Python 3 none any Details for agent_memory_hygiene-0.1.0-py3-none-any.whl

Total release size: 21.9 kB

Release files / agent_memory_hygiene-0.1.0.tar.gz

Download URL agent_memory_hygiene-0.1.0.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ef47b42068401a6cf756a7950638d7ce2a2c5f1edd9532cbb197ab6516e7a32c
BLAKE2b-256 checksum
How to use checksums
3d4a669a49a6cd814652b05633286325d081c3d17b0a31c1ee7b8fce65d28223
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / agent_memory_hygiene-0.1.0-py3-none-any.whl

Download URL agent_memory_hygiene-0.1.0-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
960d0e5e6a76016375473aaf1aba616ef7fcc1c0522183c5e739e8ae751a7f4e
BLAKE2b-256 checksum
How to use checksums
b192b3cfa203391ced2cbbcb7aa6c65758fd834b129ef9345862a1d358c884ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Security for agent-memory-hygiene 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page