# iam-saiteja/ncm architecture

> Architecture diagram of the GitHub repository iam-saiteja/ncm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-12-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/iam-saiteja/ncm
- Repository: https://github.com/iam-saiteja/ncm

## Overview

NCM is a small, local-first long-term memory library for conversational AI. An application adds conversation turns; NCM embeds and indexes them, records affect and persona evidence, and retains them in a single file. For a new query, NCM combines dense and keyword retrieval, optionally reranks results, and builds a prompt block with persona, mood, and neighboring turns for the application’s LLM. The graph focuses on the runtime package; benchmark and comparison tooling is excluded. Relationships reflect README-described behavior and the shown memory-module references; unsampled internals are not inferred.

Main entry points: [`ncm/memory.py`](https://github.com/iam-saiteja/ncm/blob/main/ncm/memory.py) (Memory API), [`ncm/embed.py`](https://github.com/iam-saiteja/ncm/blob/main/ncm/embed.py) (Text embedder), [`ncm/lexical.py`](https://github.com/iam-saiteja/ncm/blob/main/ncm/lexical.py) (BM25 index), [`ncm/legacy.py`](https://github.com/iam-saiteja/ncm/blob/main/ncm/legacy.py) (v1 import)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ingest["Memory ingestion"]
  node_memory_api["Memory API<br/>[memory.py]"]
  node_embedder["Text embedder<br/>[embed.py]"]
  node_affect["Affect analysis<br/>[affect.py]"]
  node_persona_evidence["Persona evidence<br/>[persona.py]"]
  node_bm25["BM25 index<br/>[lexical.py]"]
end

subgraph group_recall["Recall and context"]
  node_recall_engine["Hybrid recall<br/>[memory.py]"]
  node_reranker["Optional reranker<br/>[embed.py]"]
  node_persona["Persona selection<br/>[persona.py]"]
  node_context["Prompt context<br/>[memory.py]"]
  node_mood["Mood summary<br/>[memory.py]"]
end

subgraph group_state["Memory state"]
  node_retention["Activation and forgetting<br/>[memory.py]"]
  node_file[("NCM file<br/>[memory.py]")]
  node_legacy["v1 import<br/>[legacy.py]"]
end

node_app(("Application"))
node_llm(("Application LLM"))

node_app -->|"adds turns"| node_memory_api
node_memory_api -->|"embeds turns"| node_embedder
node_memory_api -->|"indexes words"| node_bm25
node_memory_api -->|"records affect"| node_affect
node_memory_api -->|"scores evidence"| node_persona_evidence
node_app -->|"queries"| node_memory_api
node_memory_api -->|"ranks memories"| node_recall_engine
node_recall_engine -->|"embeds query"| node_embedder
node_recall_engine -->|"scores keywords"| node_bm25
node_recall_engine -.->|"optionally reranks"| node_reranker
node_memory_api -->|"selects statements"| node_persona
node_memory_api -->|"summarizes mood"| node_mood
node_memory_api -->|"builds prompt block"| node_context
node_context -->|"supplies context"| node_llm
node_memory_api -->|"forgets overflow"| node_retention
node_memory_api -->|"saves and loads"| node_file
node_legacy -->|"imports v1 data"| node_memory_api

click node_memory_api "https://github.com/iam-saiteja/ncm/blob/main/ncm/memory.py"
click node_embedder "https://github.com/iam-saiteja/ncm/blob/main/ncm/embed.py"
click node_affect "https://github.com/iam-saiteja/ncm/blob/main/ncm/affect.py"
click node_persona_evidence "https://github.com/iam-saiteja/ncm/blob/main/ncm/persona.py"
click node_bm25 "https://github.com/iam-saiteja/ncm/blob/main/ncm/lexical.py"
click node_recall_engine "https://github.com/iam-saiteja/ncm/blob/main/ncm/memory.py"
click node_reranker "https://github.com/iam-saiteja/ncm/blob/main/ncm/embed.py"
click node_persona "https://github.com/iam-saiteja/ncm/blob/main/ncm/persona.py"
click node_context "https://github.com/iam-saiteja/ncm/blob/main/ncm/memory.py"
click node_mood "https://github.com/iam-saiteja/ncm/blob/main/ncm/memory.py"
click node_retention "https://github.com/iam-saiteja/ncm/blob/main/ncm/memory.py"
click node_file "https://github.com/iam-saiteja/ncm/blob/main/ncm/memory.py"
click node_legacy "https://github.com/iam-saiteja/ncm/blob/main/ncm/legacy.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_memory_api,node_embedder,node_affect,node_persona_evidence,node_bm25 toneBlue
class node_recall_engine,node_reranker,node_persona,node_context,node_mood toneAmber
class node_retention,node_file,node_legacy toneMint
class node_app,node_llm toneIndigo
```

## Components

### Memory ingestion

- [Memory API](https://github.com/iam-saiteja/ncm/blob/main/ncm/memory.py): `ncm/memory.py`
- [Text embedder](https://github.com/iam-saiteja/ncm/blob/main/ncm/embed.py): `ncm/embed.py`
- [Affect analysis](https://github.com/iam-saiteja/ncm/blob/main/ncm/affect.py): `ncm/affect.py`
- [Persona evidence](https://github.com/iam-saiteja/ncm/blob/main/ncm/persona.py): `ncm/persona.py`
- [BM25 index](https://github.com/iam-saiteja/ncm/blob/main/ncm/lexical.py): `ncm/lexical.py`

### Recall and context

- [Hybrid recall](https://github.com/iam-saiteja/ncm/blob/main/ncm/memory.py): `ncm/memory.py`
- [Optional reranker](https://github.com/iam-saiteja/ncm/blob/main/ncm/embed.py): `ncm/embed.py`
- [Persona selection](https://github.com/iam-saiteja/ncm/blob/main/ncm/persona.py): `ncm/persona.py`
- [Prompt context](https://github.com/iam-saiteja/ncm/blob/main/ncm/memory.py): `ncm/memory.py`
- [Mood summary](https://github.com/iam-saiteja/ncm/blob/main/ncm/memory.py): `ncm/memory.py`

### Memory state

- [Activation and forgetting](https://github.com/iam-saiteja/ncm/blob/main/ncm/memory.py): `ncm/memory.py`
- [NCM file](https://github.com/iam-saiteja/ncm/blob/main/ncm/memory.py): `ncm/memory.py`
- [v1 import](https://github.com/iam-saiteja/ncm/blob/main/ncm/legacy.py): `ncm/legacy.py`

### Other components

- **Application**
- **Application LLM**

## Connections

- Application → Memory API: adds turns (evidence: `README.md`)
- Memory API → Text embedder: embeds turns (evidence: `ncm/memory.py`)
- Memory API → BM25 index: indexes words (evidence: `ncm/memory.py`)
- Memory API → Affect analysis: records affect (evidence: `ncm/memory.py`)
- Memory API → Persona evidence: scores evidence (evidence: `ncm/memory.py`)
- Application → Memory API: queries (evidence: `README.md`)
- Memory API → Hybrid recall: ranks memories (evidence: `README.md`)
- Hybrid recall → Text embedder: embeds query (evidence: `README.md`)
- Hybrid recall → BM25 index: scores keywords (evidence: `README.md`)
- Hybrid recall → Optional reranker: optionally reranks (evidence: `README.md`)
- Memory API → Persona selection: selects statements (evidence: `README.md`)
- Memory API → Mood summary: summarizes mood (evidence: `README.md`)
- Memory API → Prompt context: builds prompt block (evidence: `README.md`)
- Prompt context → Application LLM: supplies context (evidence: `README.md`)
- Memory API → Activation and forgetting: forgets overflow (evidence: `README.md`)
- Memory API → NCM file: saves and loads (evidence: `README.md`)
- v1 import → Memory API: imports v1 data (evidence: `ncm/legacy.py`)
