Skip to main content

MemPatrol

CI PyPI Python License: MIT

English | 中文

MemPatrol is a dependency-free toolkit for long-lived AI agents: memory hygiene (patrol), prompt-ready recall, and a judge-first conversation gate. It runs on plain SQLite, ships a CLI and a read-only MCP server, and does not depend on an IM framework.

SQLite memories ──► patrol / diagnose ──► auditable repairs (--apply + backup)
        │
        ├──────────► recall scorer ─────► prompt-ready memory block
        │                                      │
chat events ──► gatekeep ◄────────────────────┘
        └────► LLM judge ──► send / handoff / tool_task / drop

Why MemPatrol

  • Deterministic by default. Audits, decay/consolidation plans, intent expiry and finish gates are pure functions. The LLM only appears where a human would otherwise hand-edit memory.
  • Judge-first interjection. A small set of hard gates rejects the obvious "not now"; after that, the LLM judge decides whether there is a natural insertion point. Throttling limits rate, it does not silently veto.
  • Portable. One memories table is enough. Map any SQLite schema with --schema, or let --schema auto inspect it. jieba is optional.
  • Boring operations. Read-only by default; writes require --apply and take a snapshot first. Every release has offline eval and a real-corpus replay gate.
  • No host lock-in. No AstrBot/QQ/Telegram types in the kernels. Any adapter can build the small Context/rows structures.

MemPatrol terminal demo

Architecture

flowchart LR
  A[(SQLite memory DB)] --> B[policy kernels<br/>orphan / decay / consolidate / intents / finish]
  B --> C[CLI diagnose + apply<br/>read-only by default]
  A --> D[recall scorer<br/>anchor + instruction separation]
  D --> E[prompt-ready block<br/>with provenance]
  F[chat events] --> G[gatekeep<br/>8 gates + directed path]
  H[LLM judge] --> G
  G --> I[send / handoff / tool_task / drop]
  D --> G
  C --> J[MCP stdio server]
  D --> J
  G --> J

Full component map: docs/ARCHITECTURE.md.

Install

pip install mem-patrol                 # from PyPI
# or: python -m pip install .
memorix --version                      # mem-patrol 0.3.0

The distribution is mem-patrol; the import package and original CLI remain memorix for compatibility. mem-patrol / mem-patrol-mcp are aliases.

Quickstart

# read-only diagnosis (works with any schema)
memorix diagnose --db messages.db --schema auto

# plan before writing; --apply creates a DB snapshot first
memorix decay --db messages.db
memorix decay --db messages.db --apply

# offline regression
python -m memorix.eval --suite all --baseline eval/baseline.json
python -m memorix.retrieval.bench      # 6/6
python -m memorix.gatekeep.bench       # 9/9

End-to-end: group-chat interjection

from memorix.gatekeep import Context, decide
from memorix.retrieval.context import recent_private_context
from memorix.retrieval.score import recall
from memorix.retrieval.store import load_rows

rows = load_rows("messages.db")
context = recent_private_context("messages.db", user_id)
memory = recall(user_message, rows=rows, context=context, user_id=user_id)

outcome = decide(
    Context(
        text=user_message,
        user_id=user_id,
        config={
            "enable_group_interjection": True,
            "group_interject_max_daily": 30,
            "group_interject_judge_min_consult_seconds": 10,
            "group_interject_min_interval_minutes": 1,
        },
    ),
    judge=llm_judge,  # returns {"should_reply": bool, "text": ..., "needs_tools": ...}
)
if outcome.action == "sent":
    send(outcome.text)

The judge is only called after the cheap gates pass. Directed messages (@/reply/name) take a fast path. Step-by-step walkthrough: docs/END-TO-END.md.

Recall backends (lexical default, vector optional)

from memorix.retrieval.provider import make_provider

provider = make_provider("lexical")                 # default, zero deps
# provider = make_provider("vector", embedder=embed_batch)  # bring your own embeddings
block = provider.recall(user_message, rows=rows, context=context, user_id=user_id)

VectorProvider is an optional path (cosine + embedding cache); no model is bundled. The ONNX adapter is in memorix.retrieval.vector_onnx (pip install 'mem-patrol[vector]'). Lexical remains the default until vector wins on the real corpus without regressions.

MCP

{
  "mcpServers": {
    "memorix": {
      "command": "memorix-mcp",
      "args": ["--profile", "both", "--db", "/path/to/messages.db"]
    }
  }
}

Six read-only tools: memorix_diagnose, memorix_orphan, memorix_recall, memorix_gate_check, memorix_judge_replay, memorix_eval. See docs/MCP.md.

Two modes

profile what it is for MCP tools
chat recall + gatekeep, no patrol writes recall / gate_check / judge_replay
project patrol, diagnose, recall, eval diagnose / orphan / recall / eval
both both kernels, judge-first with light throttle all 6
python -m memorix.profiles --list
memorix profile --name chat

Any SQLite schema

memorix diagnose --db my.db --schema schema.json   # explicit mapping
memorix diagnose --db my.db --schema auto          # auto-detect
MEMORIX_SCHEMA=schema.json memorix diagnose --db my.db

Only memories is required; status/finish report a clear SchemaError if message tables are not mapped. A privacy-preserving real-corpus replay is in eval/real_corpus.json: docs/REAL-CORPUS.md.

Quality and benchmarks

  • 374 tests; CI on Python 3.10 / 3.11 / 3.12.
  • Synthetic §10.3 replay: 6/6; real production replay: 6/6 current, 5/6 historical (one data gap documented, not an algorithm failure).
  • On a 1,238-memory read-only snapshot: cold load ~0.18 s, warm recall median ~43 ms, injected block ~620 chars. Environment-specific; see docs/BENCHMARKS.md.

CI/CD

  • .github/workflows/ci.yml — pytest + both benches + eval on Python 3.10/3.11/3.12.
  • .github/workflows/release.yml — v* tag → test, build wheel/sdist, install smoke, GitHub Release assets.
  • .github/workflows/publish.yml — manual PyPI upload via trusted publishing (OIDC; no token in secrets).

Docs

License

MIT. See LICENSE. MIT was chosen for minimal adoption friction; it has no explicit patent grant (Apache-2.0, used by 0.2.0/0.2.1, does). See FAQ.md.

Metadata

Release files for mem-patrol 0.3.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 mem-patrol 0.3.0
File Size Uploaded Provenance
mem_patrol-0.3.0.tar.gz 781.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for mem-patrol 0.3.0
File Interpreter ABI Platform
mem_patrol-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 879.1 kB

Release files / mem_patrol-0.3.0.tar.gz

Download URL mem_patrol-0.3.0.tar.gz
Size 781.7 kB
Tags Source
SHA-256 checksum
How to use checksums
32bf8b0286c5b9337f111c4488c547c2b383c0fae18d894b62a2486b49824206
BLAKE2b-256 checksum
How to use checksums
8d24e809e563bbd360765043607af4f31c5f23506c7a57614b08d49cee8345f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / mem_patrol-0.3.0-py3-none-any.whl

Download URL mem_patrol-0.3.0-py3-none-any.whl
Size 97.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
faebfae7182cc20789c42cc4b935f39af3a4dd94b8038e9533af67fabc687d7e
BLAKE2b-256 checksum
How to use checksums
019349aa5fd96111c3ac425db39ef6ac465c3f1204d8a08cb53626ebf4733db4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for mem-patrol 0.3.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.

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