# hu-xiaobai/xmemory architecture

> Architecture diagram of the GitHub repository hu-xiaobai/xmemory, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hu-xiaobai/xmemory
- Repository: https://github.com/hu-xiaobai/xmemory

## Overview

1. **What this repository is**
   - `src/` contains the reusable xMemory system: a hierarchical agent-memory pipeline that builds structured memory from dialogue and retrieves it later.
   - `evaluation/` contains the LoCoMo benchmark harness, dataset files, retrieval scripts, scoring scripts, and graph inspection utilities.
   - The repo is therefore split into a **core memory library** and an **experiment/evaluation layer**.

2. **Core domain objects**
   - The main data types are `Message`, `Episode`, and `Semantic` in `src/models/`.
   - These represent a progression from raw dialogue turns to grouped episodes and then to higher-level semantic units/themes.
   - The architecture is built around preserving intact units while adding hierarchy and searchable structure.

3. **Memory construction pipeline**
   - `src/core/memory_system.py` is the main orchestrator for constructing memory from conversation streams.
   - `src/core/message_buffer.py` holds incoming dialogue context.
   - `src/core/boundary_detector.py` decides where coherent memory segments begin/end.
   - `src/generation/episode_generator.py`, `semantic_generator.py`, and `prediction_correction_engine.py` turn buffered messages into structured memory objects, then refine them with LLM-driven correction.
   - This is the “build” side of xMemory.

4. **Hierarchical organization**
   - `src/core/memory_hierarchy.py` encodes the top-down organization that the paper describes: themes at the top, then semantic units, then episodes, then raw messages.
   - `src/core/lightweight_search_system.py` and `src/core/memory_system.py` are the main runtime entry points that use this hierarchy during retrieval.
   - The hierarchy is the key architectural boundary: retrieval is not flat top-k similarity, but guided by structure.

5. **Search subsystem**
   - Search is factored into multiple retrieval backends under `src/search/`:
     - `bm25_search.py` for lexical ranking
     - `vector_search.py` for embedding-based similarity
     - `chroma_search.py` for Chroma-backed vector lookup
     - `original_message_search.py` and `episode_original_message_search.py` for direct access to source text
     - `unified_search.py` to combine these signals
   - This subsystem supports the adaptive hierarchical retrieval strategy used in evaluation.

6. **Storage and persistence**
   - `src/storage/` provides storage abstractions for episodes and semantic units.
   - `src/infrastructure/indices.py` and `repositories.py` are the boundary between the memory logic and the persisted index structures.
   - The README’s output layout implies persisted artifacts for `episodes/`, `semantic/`, `themes/`, `graph/`, and `semantic-knn/`, plus `chroma_db/` for vector storage.
   - In practice, construction produces both **serialized memory artifacts** and **search indexes**.

7. **LLM and embedding dependencies**
   - `src/utils/llm_client.py` and `llm_client_openllm.py` abstract the LLM backend.
   - `src/utils/embedding_client.py` abstracts embedding generation for semantic indexing and retrieval.
   - `src/services/providers.py` likely centralizes model/provider selection so the rest of the system is backend-agnostic.
   - The architecture is designed to work with different LLMs, but the README notes that config changes are needed when models or memory paths change.

8. **Cross-cutting services**
   - `src/services/cache.py` handles reuse of intermediate results.
   - `src/services/event_bus.py` supports internal event propagation across construction/retrieval stages.
   - `src/services/task_manager.py` suggests batched/parallel execution during memory building or evaluation.
   - `src/services/metrics.py` collects runtime or evaluation statistics.
   - These are supporting services rather than business logic, but they affect throughput and observability.

9. **Evaluation workflow**
   - `evaluation/locomo/add.py` builds memory for the LoCoMo dataset.
   - `evaluation/locomo/xMemory_search_framework.py` runs retrieval/inference against the built memory.
   - `evaluation/locomo/evals.py` and `generate_scores.py` post-process outputs into benchmark scores.
   - `evaluation/locomo/metrics/llm_judge.py` indicates that at least part of evaluation uses LLM-based judging.
   - This layer consumes the core library and produces benchmark artifacts like results JSON files.

10. **Graph and analysis tooling**
   - `evaluation/gexf_view.py` is for visualizing the constructed memory graph.
   - The presence of graph outputs and GEXF viewing indicates that the hierarchy is also represented as a navigable graph, not just a set of files.
   - This is useful for debugging the split/merge behavior and the semantic organization of memory.

11. **Configuration boundaries**
   - `src/config.py` and `evaluation/locomo/config.json` are the two main configuration touchpoints.
   - `environment.yml` defines the conda environment and anchors the runtime setup.
   - The repository expects the experiment scripts and core code to stay in sync on model names and storage paths.

12. **End-to-end data flow**
   - Input begins as a dialogue dataset in `evaluation/dataset/`.
   - Memory construction buffers messages, detects boundaries, generates episodes/semantics, and writes hierarchical indexes.
   - Retrieval then traverses the hierarchy top-down, combining semantic, episodic, lexical, and vector search.
   - Evaluation scripts consume retrieval outputs and compute final benchmark scores.

Main entry points: [`src/core/memory_system.py`](https://github.com/hu-xiaobai/xmemory/blob/main/src/core/memory_system.py) (Memory system), [`evaluation/locomo/xMemory_search_framework.py`](https://github.com/hu-xiaobai/xmemory/blob/main/evaluation/locomo/xMemory_search_framework.py) (Eval search), [`src/utils`](https://github.com/hu-xiaobai/xmemory/tree/main/src/utils) (LLM + embed), [`src/search/unified_search.py`](https://github.com/hu-xiaobai/xmemory/blob/main/src/search/unified_search.py) (Unified search)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core library"]
  node_models["Memory types<br/>domain models"]
  node_memory_system["Memory system<br/>orchestrator<br/>[memory_system.py]"]
  node_message_buffer["Message buffer<br/>stream buffer<br/>[message_buffer.py]"]
  node_boundary_detector["Boundary detect<br/>segmenter"]
  node_episode_generator["Episode gen<br/>builder"]
  node_semantic_generator["Semantic gen<br/>builder"]
  node_prediction_correction_engine["Correction<br/>refinement"]
  node_memory_hierarchy["Hierarchy<br/>structure"]
  node_lightweight_search_system["Light search<br/>retrieval entry"]
  node_unified_search["Unified search<br/>retrieval combiner<br/>[unified_search.py]"]
  node_search_backends["Search backends<br/>retrieval backends"]
  node_facade["API facade<br/>public interface<br/>[facade.py]"]
end

subgraph group_support["Support layer"]
  node_config["Config<br/>[config.py]"]
  node_providers["Providers<br/>backend selection<br/>[providers.py]"]
  node_llm_embedding["LLM + embed<br/>model adapters"]
  node_storage_infra[("Storage infra<br/>persistence boundary")]
  node_runtime_services["Runtime services<br/>cross-cutting services"]
end

subgraph group_evaluation["Evaluation layer"]
  node_locomo_add["Build memory<br/>dataset pipeline<br/>[add.py]"]
  node_locomo_search["Eval search<br/>benchmark runner"]
  node_locomo_score["Score runs<br/>scoring pipeline<br/>[evals.py]"]
  node_llm_judge["LLM judge<br/>evaluation metric<br/>[llm_judge.py]"]
  node_gexf_view(("Graph view<br/>visualization<br/>[gexf_view.py]"))
  node_dataset["LoCoMo data<br/>benchmark dataset<br/>[locomo10.json]"]
end

node_dataset -->|"input"| node_locomo_add
node_locomo_add -->|"build"| node_memory_system
node_memory_system -->|"buffer"| node_message_buffer
node_message_buffer -->|"segment"| node_boundary_detector
node_boundary_detector -->|"episodes"| node_episode_generator
node_episode_generator -->|"lift"| node_semantic_generator
node_semantic_generator -->|"refine"| node_prediction_correction_engine
node_prediction_correction_engine -->|"emit"| node_models
node_memory_system -->|"organize"| node_memory_hierarchy
node_memory_hierarchy -->|"guide"| node_lightweight_search_system
node_lightweight_search_system -->|"search"| node_unified_search
node_unified_search -->|"backends"| node_search_backends
node_memory_system -->|"persist"| node_storage_infra
node_storage_infra -.->|"support"| node_runtime_services
node_providers -->|"select"| node_llm_embedding
node_llm_embedding -->|"generate"| node_episode_generator
node_llm_embedding -->|"generate"| node_semantic_generator
node_llm_embedding -->|"embed"| node_unified_search
node_facade -->|"api"| node_memory_system
node_locomo_search -->|"retrieve"| node_lightweight_search_system
node_locomo_search -->|"rank"| node_unified_search
node_locomo_search -->|"judge"| node_llm_judge
node_locomo_search -->|"outputs"| node_locomo_score
node_locomo_score -.->|"compare"| node_dataset
node_gexf_view -->|"inspect"| node_memory_hierarchy

click node_models "https://github.com/hu-xiaobai/xmemory/tree/main/src/models"
click node_memory_system "https://github.com/hu-xiaobai/xmemory/blob/main/src/core/memory_system.py"
click node_message_buffer "https://github.com/hu-xiaobai/xmemory/blob/main/src/core/message_buffer.py"
click node_boundary_detector "https://github.com/hu-xiaobai/xmemory/blob/main/src/core/boundary_detector.py"
click node_episode_generator "https://github.com/hu-xiaobai/xmemory/blob/main/src/generation/episode_generator.py"
click node_semantic_generator "https://github.com/hu-xiaobai/xmemory/blob/main/src/generation/semantic_generator.py"
click node_prediction_correction_engine "https://github.com/hu-xiaobai/xmemory/blob/main/src/generation/prediction_correction_engine.py"
click node_memory_hierarchy "https://github.com/hu-xiaobai/xmemory/blob/main/src/core/memory_hierarchy.py"
click node_lightweight_search_system "https://github.com/hu-xiaobai/xmemory/blob/main/src/core/lightweight_search_system.py"
click node_unified_search "https://github.com/hu-xiaobai/xmemory/blob/main/src/search/unified_search.py"
click node_search_backends "https://github.com/hu-xiaobai/xmemory/tree/main/src/search"
click node_facade "https://github.com/hu-xiaobai/xmemory/blob/main/src/api/facade.py"
click node_config "https://github.com/hu-xiaobai/xmemory/blob/main/src/config.py"
click node_providers "https://github.com/hu-xiaobai/xmemory/blob/main/src/services/providers.py"
click node_llm_embedding "https://github.com/hu-xiaobai/xmemory/tree/main/src/utils"
click node_storage_infra "https://github.com/hu-xiaobai/xmemory/tree/main/src/storage"
click node_runtime_services "https://github.com/hu-xiaobai/xmemory/tree/main/src/services"
click node_locomo_add "https://github.com/hu-xiaobai/xmemory/blob/main/evaluation/locomo/add.py"
click node_locomo_search "https://github.com/hu-xiaobai/xmemory/blob/main/evaluation/locomo/xMemory_search_framework.py"
click node_locomo_score "https://github.com/hu-xiaobai/xmemory/blob/main/evaluation/locomo/evals.py"
click node_llm_judge "https://github.com/hu-xiaobai/xmemory/blob/main/evaluation/locomo/metrics/llm_judge.py"
click node_gexf_view "https://github.com/hu-xiaobai/xmemory/blob/main/evaluation/gexf_view.py"
click node_dataset "https://github.com/hu-xiaobai/xmemory/blob/main/evaluation/dataset/locomo10.json"

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_models,node_memory_system,node_message_buffer,node_boundary_detector,node_episode_generator,node_semantic_generator,node_prediction_correction_engine,node_memory_hierarchy,node_lightweight_search_system,node_unified_search,node_search_backends,node_facade toneBlue
class node_config,node_providers,node_llm_embedding,node_storage_infra,node_runtime_services toneAmber
class node_locomo_add,node_locomo_search,node_locomo_score,node_llm_judge,node_gexf_view,node_dataset toneMint
```

## Components

### Core library

Reusable xMemory memory construction and retrieval components.

- [Memory types](https://github.com/hu-xiaobai/xmemory/tree/main/src/models): domain models · Message, Episode, and Semantic objects that carry the memory hierarchy. · `src/models`
- [Memory system](https://github.com/hu-xiaobai/xmemory/blob/main/src/core/memory_system.py): orchestrator · Main runtime that builds memory from conversation streams and serves retrieval entry points. · `src/core/memory_system.py`
- [Message buffer](https://github.com/hu-xiaobai/xmemory/blob/main/src/core/message_buffer.py): stream buffer · Holds incoming dialogue context before segmentation. · `src/core/message_buffer.py`
- [Boundary detect](https://github.com/hu-xiaobai/xmemory/blob/main/src/core/boundary_detector.py): segmenter · Finds coherent episode and semantic boundaries in the message stream. · `src/core/boundary_detector.py`
- [Episode gen](https://github.com/hu-xiaobai/xmemory/blob/main/src/generation/episode_generator.py): builder · Turns buffered messages into episode-level memory units. · `src/generation/episode_generator.py`
- [Semantic gen](https://github.com/hu-xiaobai/xmemory/blob/main/src/generation/semantic_generator.py): builder · Lifts episodes into higher-level semantic units and themes. · `src/generation/semantic_generator.py`
- [Correction](https://github.com/hu-xiaobai/xmemory/blob/main/src/generation/prediction_correction_engine.py): refinement · LLM-driven refinement and correction of generated memory objects. · `src/generation/prediction_correction_engine.py`
- [Hierarchy](https://github.com/hu-xiaobai/xmemory/blob/main/src/core/memory_hierarchy.py): structure · Encodes the theme → semantic → episode → message organization used during retrieval. · `src/core/memory_hierarchy.py`
- [Light search](https://github.com/hu-xiaobai/xmemory/blob/main/src/core/lightweight_search_system.py): retrieval entry · Top-level retrieval path that traverses the hierarchy before searching. · `src/core/lightweight_search_system.py`
- [Unified search](https://github.com/hu-xiaobai/xmemory/blob/main/src/search/unified_search.py): retrieval combiner · Combines lexical, vector, and direct-message signals for adaptive retrieval. · `src/search/unified_search.py`
- [Search backends](https://github.com/hu-xiaobai/xmemory/tree/main/src/search): retrieval backends · Representative lexical, vector, Chroma, and source-message lookup implementations. · `src/search`
- [API facade](https://github.com/hu-xiaobai/xmemory/blob/main/src/api/facade.py): public interface · External-facing entry point for using the xMemory library programmatically. · `src/api/facade.py`

### Support layer

Configuration, model providers, storage, infrastructure, and runtime services.

- [Config](https://github.com/hu-xiaobai/xmemory/blob/main/src/config.py): runtime config · Repository-wide configuration for models, memory paths, and evaluation settings. · `src/config.py`
- [Providers](https://github.com/hu-xiaobai/xmemory/blob/main/src/services/providers.py): backend selection · Chooses model and provider implementations used by the rest of the system. · `src/services/providers.py`
- [LLM + embed](https://github.com/hu-xiaobai/xmemory/tree/main/src/utils): model adapters · LLM client and embedding client abstractions used for generation and search. · `src/utils`
- [Storage infra](https://github.com/hu-xiaobai/xmemory/tree/main/src/storage): persistence boundary · Episode and semantic storage plus index/repository adapters for persisted artifacts. · `src/storage`
- [Runtime services](https://github.com/hu-xiaobai/xmemory/tree/main/src/services): cross-cutting services · Cache, event bus, task manager, and metrics support for throughput and observability. · `src/services`

### Evaluation layer

LoCoMo dataset ingestion, retrieval harness, scoring, and graph inspection.

- [Build memory](https://github.com/hu-xiaobai/xmemory/blob/main/evaluation/locomo/add.py): dataset pipeline · Constructs xMemory artifacts for the LoCoMo benchmark dataset. · `evaluation/locomo/add.py`
- [Eval search](https://github.com/hu-xiaobai/xmemory/blob/main/evaluation/locomo/xMemory_search_framework.py): benchmark runner · Runs retrieval and inference against built memories for LoCoMo. · `evaluation/locomo/xMemory_search_framework.py`
- [Score runs](https://github.com/hu-xiaobai/xmemory/blob/main/evaluation/locomo/evals.py): scoring pipeline · Post-processes benchmark outputs and computes final scores. · `evaluation/locomo/evals.py`
- [LLM judge](https://github.com/hu-xiaobai/xmemory/blob/main/evaluation/locomo/metrics/llm_judge.py): evaluation metric · LLM-based judging and metric utilities for benchmark evaluation. · `evaluation/locomo/metrics/llm_judge.py`
- [Graph view](https://github.com/hu-xiaobai/xmemory/blob/main/evaluation/gexf_view.py): visualization · Inspects the constructed memory graph and hierarchy structure. · `evaluation/gexf_view.py`
- [LoCoMo data](https://github.com/hu-xiaobai/xmemory/blob/main/evaluation/dataset/locomo10.json): benchmark dataset · Input conversation dataset used to build and evaluate memory. · `evaluation/dataset/locomo10.json`

## Connections

- LoCoMo data → Build memory: input · Benchmark conversations are ingested to build memory artifacts.
- Build memory → Memory system: build · Evaluation harness invokes the core memory constructor.
- Memory system → Message buffer: buffer · Incoming turns are accumulated before segmentation.
- Message buffer → Boundary detect: segment · Buffered dialogue is split into coherent units.
- Boundary detect → Episode gen: episodes · Detected boundaries drive episode construction.
- Episode gen → Semantic gen: lift · Episodes are summarized into semantic units and themes.
- Semantic gen → Correction: refine · Generated structures are corrected and normalized.
- Correction → Memory types: emit · Refined message, episode, and semantic objects are produced.
- Memory system → Hierarchy: organize · Constructed memory is arranged into the hierarchical representation.
- Hierarchy → Light search: guide · Hierarchy directs retrieval from top-level themes downward.
- Light search → Unified search: search · Hierarchy-aware retrieval delegates to combined search.
- Unified search → Search backends: backends · Unified retrieval merges lexical, vector, Chroma, and source-message lookup.
- Memory system → Storage infra: persist · Serialized memory artifacts and indexes are written through persistence adapters.
- Storage infra → Runtime services: support · Persistence and retrieval reuse shared services such as cache and metrics.
- Providers → LLM + embed: select · Provider selection controls which LLM and embedding implementations are used.
- LLM + embed → Episode gen: generate · LLM support drives episode and semantic generation.
- LLM + embed → Semantic gen: generate · Semantic lifting depends on model-backed generation.
- LLM + embed → Unified search: embed · Embedding support powers vector-based retrieval paths.
- API facade → Memory system: api · Public API delegates to the core orchestrator.
- Eval search → Light search: retrieve · Benchmark evaluation queries the hierarchy-aware search path.
- Eval search → Unified search: rank · Evaluation also exercises combined retrieval backends.
- Eval search → LLM judge: judge · Some benchmark outputs are evaluated with LLM-based judging.
- Eval search → Score runs: outputs · Retrieval results flow into scoring and aggregation.
- Score runs → LoCoMo data: compare · Scores are computed against the benchmark inputs and references.
- Graph view → Hierarchy: inspect · Graph tooling visualizes the constructed memory structure.
