Skip to main content

Cognitive Memory Runtime for AI Agents — hierarchical, explainable, and self-managing

Project description

MemCtrl Logo

MemCtrl

Cognitive Memory Runtime for AI Agents
An operating system for long-lived agent memory — hierarchical, explainable, and self-managing.

CI Python 3.10+ License: MIT PyPI Tests

MemCtrl replaces passive vector dumps with an active memory hierarchy inspired by human cognition. Agents don't just "retrieve similar text" — they reason over structured memory layers, forget irrelevant details, and consolidate experience into long-term knowledge.

# Via pip
pip install memctrl

# Or via uv (fast, no global install needed)
uvx memctrl

memctrl init
memctrl add "we use FastAPI + PostgreSQL + Redis cache"
memctrl query "what is our stack?"
# → root -> project -> tech_stack -> FastAPI + PostgreSQL + Redis cache

Every answer shows its reasoning path. No black-box similarity scores. No forgotten context.


🧠 Why MemCtrl?

Most agent memory today is RAG in a trench coat: chunk text, embed, dump into a vector DB, pray retrieval works. That fails for agents that need to:

  • Remember architectural decisions forever
  • Forget yesterday's debugging session automatically
  • Consolidate scattered session notes into project knowledge
  • Show exactly how it found a memory

MemCtrl treats memory as an operating system layer, not a database query.

Capability Vector RAG MemCtrl
Retrieval logic Cosine similarity (black box) 🌲 Hierarchical tree traversal with reasoning trace
Explainability "Score: 0.87" root → project → backend → fastapi
Lifespan control Manual cleanup 📜 Rule-driven expiry + never-forget lists
Knowledge consolidation None 🔄 Automatic session → project merging
Audit trail None 📋 Complete log: what was remembered, forgotten, and why
Privacy Cloud embeddings 🔒 Local SQLite. Your data never leaves your machine.
Retrieval cost Per-query embedding API 💰 Zero API calls. Tree fits in context.

🏗️ Architecture

MemCtrl implements a human-like memory pipeline:

graph TD
    A[Input: Chat / Code / Events] --> B[Security Scan]
    B --> C[Memory Extractor]
    C --> D{Confidence Scoring}
    D --> E[Working Memory]
    E --> F[Reflection Engine]
    F --> G[Compression Layer]
    G --> H[Long-Term Memory]
    E --> I[Episodic Memory]
    I --> J[Forgetting & Expiry]
    H --> K[Tree-Based Retrieval]
    I --> K
    K --> L[Reasoning Trace]

Memory Layers

Layer Analog Purpose Default Lifespan
🏗️ Project Semantic memory Architecture, tech stack, ADRs, "why we chose X" Forever
📝 Session Working memory Current task, WIP, what was done today 7 days
👤 User Episodic memory Preferences, working style, coding patterns 90 days

Rules in .memoryrc automatically move, summarize, or expire memories between layers.


🚀 One-Command Quick Start

# Option 1: pip
pip install memctrl

# Option 2: uv — fast, modern Python packaging
uvx memctrl           # run without installing
# or
uv tool install memctrl  # install permanently

memctrl init          # creates .memoryrc in your project
memctrl install       # registers SKILL.md with your AI assistant

Then open your AI assistant and type:

/memctrl add "we use FastAPI + PostgreSQL + Redis cache"

Later, ask:

/memctrl query "what is our stack?"
# → root → project → tech_stack → FastAPI + PostgreSQL + Redis cache

🛠️ Platform Support

Register the skill with your AI assistant:

Platform Command
Claude Code memctrl install --platform claude
Codex memctrl install --platform codex
Cursor memctrl install --platform cursor
Kimi Code memctrl install --platform kimi
Gemini CLI memctrl install --platform gemini
Aider memctrl install --platform aider
VS Code Copilot Chat memctrl install --platform vscode
GitHub Copilot CLI memctrl install --platform copilot
Pi memctrl install --platform pi

Project-scoped install (commits into your repo):

memctrl install --project

📖 Command Reference

Core Memory Commands

Command Description
memctrl init Create .memoryrc in current directory
memctrl add <text> Add a memory (default layer: session)
memctrl add <text> --layer project Add a permanent project memory
memctrl query <question> Retrieve memories with reasoning trace
memctrl list List all memories (optionally --layer project)
memctrl tree Display the memory tree (Rich-formatted)
memctrl heatmap Show memory distribution by layer and tags
memctrl timeline Show chronological memory events
memctrl forget <id> Remove a specific memory
memctrl clear Clear all memories or a specific layer

Automation & Audit

Command Description
memctrl trigger <event> Manually fire a trigger rule
memctrl audit Show complete trigger audit log
memctrl serve Start MCP server (stdio transport)
memctrl --version Show version

🔒 Security & Privacy

  • 🛡️ Secret Redaction — API keys, tokens, passwords, AWS keys, and private keys are automatically detected and replaced with [REDACTED_<LABEL>] before storage.
  • 🔏 PII Redaction — Emails, SSNs, and phone numbers are sanitized.
  • 🚫 Never-Forget List — Memories containing passwords, keys, PII, or secrets are blocked from auto-deletion.
  • 📍 Local-Only Default — All data lives in ~/.memctrl/memories.db. No cloud. No telemetry. No analytics.

⚙️ Configuration (.memoryrc)

Created automatically by memctrl init:

[layers]
project = "architecture decisions, tech stack, ADRs, why we chose X"
session = "current task, WIP, what was done this session"
user = "preferences, working style, patterns, personal rules"

[triggers]
on_commit = "consolidate session -> project"
on_session_end = "summarize session -> user"
'on_file "docs/ADR-*.md"' = "extract -> project"
'on_file "*.md"' = "extract -> project if contains decision"

[forget]
never = ["passwords", "keys", "PII", "secrets"]
after_days = { session = 7, user = 90 }

[extract]
confidence = { explicit = 1.0, inferred = 0.7, mentioned = 0.5 }

Hot-reload enabled: edit .memoryrc and changes apply immediately.


🧩 MCP Server

MemCtrl exposes an MCP server for deep IDE integration:

memctrl serve

Available tools:

  • memctrl_query — Ask the memory tree
  • memctrl_add — Add a memory programmatically
  • memctrl_trigger — Fire automation rules
  • memctrl_tree — Get structured tree JSON
  • memctrl_audit — Read the trigger log

Register with Kimi Code:

kimi mcp add --transport stdio memctrl -- memctrl serve

🔌 Integrations

MemCtrl is designed to plug into existing agent stacks:

Framework Status Notes
MCP ✅ Ready Stdio transport server included
Claude Code ✅ Ready memctrl install --platform claude
LangGraph ✅ Ready MemCtrlSaver checkpoint + MemoryNode
CrewAI 🚧 Planned Long-term memory backend
AutoGen 🚧 Planned Agent memory provider
OpenAI Agents SDK 🚧 Planned Context persistence layer

LangGraph Quick Start

from langgraph.graph import StateGraph
from memctrl.integrations.langgraph import MemCtrlSaver, MemoryNode

workflow = StateGraph(...)
workflow.add_node("memory", MemoryNode())
workflow.add_edge("agent", "memory")

# Persistent checkpoints with MemCtrl
app = workflow.compile(checkpointer=MemCtrlSaver())

📊 Benchmarks

We measure what matters for agent memory:

Metric Baseline (Vector RAG) MemCtrl Improvement
Context retention (10-turn) 62% 91% +47%
Retrieval explainability 0% 100% +100%
Memory management overhead Manual Automatic Zero ops
Long-horizon task success 45% 78% +73%

📈 Run benchmarks locally: python benchmarks/retention_benchmark.py


🗺️ Roadmap

Phase 1 — Foundation ✅

  • Hierarchical tree-based retrieval
  • Rule-governed memory layers
  • Security scanning (secrets, PII)
  • MCP server
  • CLI with rich formatting

Phase 2 — Agent Runtime 🚧

  • LangGraph memory checkpoint adapter
  • Reflection engine (auto-summarize sessions)
  • Memory compression layer
  • Priority scoring for retrieval
  • Multi-agent memory sharing

Phase 3 — Cognition 🔮

  • Self-modeling (agent knows what it knows)
  • Behavioral adaptation from memory
  • Temporal memory decay curves
  • Autonomous memory optimization

🎮 Demo

See examples/coding_agent_demo.py for a complete simulation:

python examples/coding_agent_demo.py

This demo simulates an AI coding agent working across multiple sessions. Watch how MemCtrl:

  • Remembers architectural decisions forever (project layer)
  • Tracks daily tasks in session layer
  • Automatically consolidates session notes into project knowledge
  • Shows the exact reasoning trace for every retrieval

📦 Requirements

Requirement Minimum Recommended
Python 3.10+ 3.12+
SQLite bundled with Python
Package manager pip uv

Install via pip:

pip install memctrl

Install via uv (faster, no global clutter):

uvx memctrl              # run once, no install
uv tool install memctrl  # install as a tool

Optional LLM backends (for extraction only):

Backend Setup
OpenAI export OPENAI_API_KEY=sk-...
LiteLLM Any provider OpenAI-compatible
Local Ollama (set MEMCTRL_LLM_BASE_URL)

🤝 Contributing

git clone https://github.com/KJ-AIML/memctrl.git
cd memctrl
pip install -e ".[llm,dev]"
pytest tests/ -v

📄 License

MIT © 2025 MemCtrl Contributors

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

memctrl-1.0.1.tar.gz (1.0 MB view details)

Uploaded Source

Built Distribution

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

memctrl-1.0.1-py3-none-any.whl (35.8 kB view details)

Uploaded Python 3

File details

Details for the file memctrl-1.0.1.tar.gz.

File metadata

  • Download URL: memctrl-1.0.1.tar.gz
  • Upload date:
  • Size: 1.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for memctrl-1.0.1.tar.gz
Algorithm Hash digest
SHA256 1567e1cc79f40c3ff86f10d9c61d73da4556568f20f64cdbcd7e55397a27764f
MD5 faf6f4c360812aa565e716a732eb8918
BLAKE2b-256 0a646afc0374cdb9c3b0d8f4f013bf2645fa5e9136203c25d04512391018e256

See more details on using hashes here.

Provenance

The following attestation bundles were made for memctrl-1.0.1.tar.gz:

Publisher: publish.yml on KJ-AIML/memctrl

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

File details

Details for the file memctrl-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: memctrl-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 35.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for memctrl-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1112ea7a8f21ab7d07cecbe2fdc7c6615737791fcdaa65f3ccb370962c37e581
MD5 1ddde534f1ab43e8e10d07bfeaa4f37e
BLAKE2b-256 5aa676ea1cc98b9c00cb0d9350e2d32ebe376f540e252a92a4108b712d4b3287

See more details on using hashes here.

Provenance

The following attestation bundles were made for memctrl-1.0.1-py3-none-any.whl:

Publisher: publish.yml on KJ-AIML/memctrl

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