Skip to main content

totem

PyPI Python License MCP

Persistent memory layer for engineering agents. Store decisions, invariants, gotchas, and rejected ideas in a local Turso database with staleness detection, conflict detection, full-text search, and structured context assembly.

Why

AI coding agents lose engineering context between sessions. They re-discover the same gotchas, re-debate the same decisions, and forget invariants that were already established. totem persists this knowledge locally and serves it back to agents as structured context, ordered by relevance.

Features

  • Four memory types: decision, invariant, gotcha, rejected_idea (each with type-specific metadata)
  • Staleness detection: SHA256 content hashing on linked evidence; auto-transitions items to potentially_stale when source code changes
  • Conflict detection: surfaces contradictory decisions or invariants on overlapping code ranges
  • Conflict resolution: mark conflicts as resolved and pick a winner
  • Full-text search: Turso FTS5 on title, statement, details, and tags
  • Hybrid memory: project memories in .totem/, user memories in ~/.local/share/totem/. Context assembly searches both.
  • Context assembly: scored pipeline with token budget support, section ordering per spec
  • Workspace scoping: auto-detects git root for correct DB placement; explicit --project override available
  • Export/import: move memories between machines or seed a new project from an existing one
  • MCP server: expose all tools via Model Context Protocol for agent use
  • CLI: full command-line interface for manual operations

Install

Requires Python 3.13+.

git clone https://github.com/emiliano-go/totem.git
cd totem

# With uv (recommended)
uv sync

# Or with pip
pip install .

This installs two entry points: totem (CLI) and totem-mcp (MCP server).

Add to your agent

Claude Code:

claude mcp add totem -- uvx totem-mcp

opencode (add to ~/.config/opencode/opencode.json):

{
  "mcp": {
    "totem": {
      "type": "local",
      "command": ["uvx", "totem-mcp"],
      "enabled": true
    }
  }
}

Claude Desktop / Cursor / Windsurf (add to config):

{
  "mcpServers": {
    "totem": {
      "command": "uvx",
      "args": ["totem-mcp"]
    }
  }
}

VS Code (add to .vscode/mcp.json):

{
  "servers": {
    "totem": {
      "type": "stdio",
      "command": "uvx",
      "args": ["totem-mcp"]
    }
  }
}

Project-scoped (Claude Code / Cursor / opencode, checked into repo):

The included .mcp.json handles this automatically. Just open your project and the agent picks it up.

Quick start

MCP server

Start the server:

totem-mcp

Then use it from your agent. Example tool calls:

memory_create_tool(type="invariant", title="No direct DB access in API layer",
  statement="API handlers must use the repository layer, never sqlite3 directly",
  tags=["api", "database"], metadata={"verificationMethod": "code review",
  "condition": "No sqlite3 imports in src/api/*.py"})

engineering_context_tool(tags=["api", "database"], task="Refactor auth middleware")

CLI

# Create a memory item (rationale is optional but strongly recommended)
totem create --type decision --title "Use FTS5 for search" \
  --statement "SQLite FTS5 is sufficient for our search needs" \
  --tags "search,sqlite" --metadata '{"rationale": "No external search dependency needed"}'

# Get it back
totem get <ITEM_ID>

# Search (full-text + tags)
totem search --query "FTS5 search" --tags "sqlite"

# List recent items
totem recent

# Resolve a conflict
totem resolve <CONFLICT_ID> --resolution "Kept existing: Use FTS5"

# Export all memories to a file
totem export -o backup.json

# Import memories from a file
totem import backup.json

# Assemble context for a task
totem context --tags "search,sqlite" --task "Add fuzzy search" --budget 4096

MCP tools

All tools return JSON strings. Every tool accepts an optional project parameter to override workspace scoping.

Tool Description
memory_create_tool Create a memory item. Provide rationale in metadata for decisions (strongly recommended).
memory_get_tool Retrieve by ID with staleness check
memory_update_tool Update any field. Provide reason (strongly recommended for audit trail).
memory_delete_tool Soft-delete (requires reason)
memory_list_tool Filtered listing with sort param (created_at, updated_at, importance)
memory_recent_tool List most recently created memories (default limit 5)
memory_search_tool FTS5 full-text search (includes tags) with type/tag filters
resolve_conflict_tool Mark a conflict as resolved with a resolution description
engineering_context_tool Scored context assembly; searches project + user DBs
memory_export_tool Export all memories and conflicts as portable JSON
memory_import_tool Import memories from an export dict (skips duplicate IDs)

CLI commands

All commands accept --project <path> to override workspace scoping.

Command Description
totem create Create a new memory item
totem get <ID> Retrieve by ID (--no-evidence skips staleness check)
totem update <ID> Update an item (--reason optional, defaults to "maintenance")
totem delete <ID> Soft-delete (requires --reason)
totem list List with --sort (created_at, updated_at, importance) and filters
totem recent List most recently created memories (--limit default 5)
totem resolve <ID> Mark a conflict as resolved (--resolution required)
totem search Full-text search (includes tags) with type/tag filters
totem export Export all memories and conflicts as JSON (-o for file output)
totem import <FILE> Import memories from a JSON export file
totem context Assemble scored context for a task

All commands output JSON to stdout.

Workspace scoping

totem auto-detects your project root using git rev-parse --show-toplevel. The .totem/totem.db file is created relative to the git root, not your current working directory. This means the MCP server works correctly regardless of which subdirectory it starts in.

To override auto-detection, pass --project <path> on any CLI command or project parameter on any MCP tool.

Memory types

Each type captures a different kind of engineering knowledge:

Type Purpose Metadata
decision A choice that was made rationale (optional, but strongly recommended: explain WHY)
invariant A rule that must hold verificationMethod, condition (required)
gotcha A non-obvious pitfall discovered (none)
rejected_idea A proposal that was considered and declined proposal, reasonRejected (required)

Hybrid memory

totem stores memories in two locations:

  • Project memories: .totem/totem.db (in your repo, checked into version control or gitignored)
  • User memories: ~/.local/share/totem/totem.db (personal preferences, global patterns)

engineering_context searches both databases, with project memories taking precedence. This means your agent remembers project-specific decisions and your personal coding preferences across all projects.

Export and import

Move memories between machines or seed a new project:

# Export everything from the current project
totem export -o project-memories.json

# Import into a different project
cd /path/to/other-project
totem import ../project-memories.json

Import skips items with duplicate IDs and reports counts of imported vs skipped items.

Context assembly

The engineering_context tool runs a scored pipeline across both project and user memories:

Scoring formula:

score = 0.4 * tag_match + 0.3 * importance + 0.2 * confidence + 0.1 * recency

Invariants get a 1.25x multiplier. Potentially stale items get a 0.5x penalty.

Output section order (never truncated):

  1. TASK (if provided)
  2. BLOCKING AMBIGUITIES (high/critical impact, always shown)
  3. CONFLICTS (always shown)
  4. CRITICAL INVARIANTS
  5. DECISIONS
  6. GOTCHAS
  7. REJECTED IDEAS
  8. STALE WARNINGS (always shown)

Conflicts and warnings are never dropped due to token budget. Item sections truncate when budget is exceeded, with a count of omitted items noted.

Data model

Core fields on every MemoryItem:

Field Type Description
id UUID Auto-generated primary key
type enum decision, invariant, gotcha, rejected_idea
title str Short title
statement str The factual claim
details str? Additional context
tags list[str] At least one required
status enum active, potentially_stale, invalidated, deleted
confidence float 0 to 1, default 1.0
importance float 0 to 1, default 0.5
evidence list[Evidence] Linked source code with content hashes
metadata dict? Type-specific keys (see memory types above)

Evidence entries link to source code ranges with SHA256 hashes for staleness detection.

Companion skill

The skills/precision-first/ directory contains a precision-first software engineering methodology designed to pair with totem. It covers invariant management, ambiguity classification, contradiction detection, and structured code review workflows.

Development

git clone https://github.com/emiliano-go/totem.git
cd totem
uv sync

# Run tests (none yet)
uv run pytest

# Run CLI
uv run totem --help

# Run MCP server
uv run totem-mcp

License

MIT

Download files

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

Source Distribution

totem_mcp-0.1.0.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

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

totem_mcp-0.1.0-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: totem_mcp-0.1.0.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for totem_mcp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4ff5018e5ac7a2d94b8b8d9a5efe944fc73bf92b12397af66de3e0c1d595c596
MD5 4ada972c7ec862e33d771f28a23c7f4b
BLAKE2b-256 55ab8b5dc3cd92e636fbd7c4af7bb38c73ebaf492e0c5b28ed6a99b415fcc98f

See more details on using hashes here.

Provenance

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

Publisher: publishing.yml on emiliano-go/totem

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

File details

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

File metadata

  • Download URL: totem_mcp-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for totem_mcp-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cc1fbdd8d99f72c75e50ffae988301445a93a74e3498078c2d73824ff1eae576
MD5 1f0b77e94982cdb0697016258574c859
BLAKE2b-256 464fe8b588fc7be40aef34aaedc4353fd24c7cba9920e93be693537cc3c52a63

See more details on using hashes here.

Provenance

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

Publisher: publishing.yml on emiliano-go/totem

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

Release history Release notifications | RSS feed

0.5.1

2 files

0.5.0

2 files

0.4.5

2 files

0.4.4

2 files

0.4.3

2 files

0.4.2

2 files

0.4.1

2 files

0.4.0

2 files

0.3.0

2 files

0.2.1

2 files

0.2.0

2 files

This release

0.1.0 This release

2 files

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