# uktrade/matchbox architecture

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

- Interactive diagram: https://gitdiagram.com/uktrade/matchbox
- Repository: https://github.com/uktrade/matchbox

## Overview

#### Purpose and Entry Points
Matchbox is a Python record-matching pipeline orchestrator. It composes source data, deduplication/linking models, and resolution steps while indexing warehouse data rather than storing raw records. Primary user entry points are the client API/CLI (`src/matchbox/client/cli/main.py`, `src/matchbox/client/_handler/main.py`) and the optional HTTP server (`src/matchbox/server/api/main.py`).

#### Client Pipeline Definition
The client provides the domain-facing API for declaring sources, locations, models, queries, DAGs, and result access. Pipeline structure and execution-oriented steps are represented across `src/matchbox/client/dags.py`, `src/matchbox/client/steps.py`, and `src/matchbox/client/sources.py`. Client handlers form the boundary to server-backed operations: `src/matchbox/client/_handler/query.py` and `src/matchbox/client/_handler/eval.py`.

#### Matching Methodologies
Pluggable matching implementations separate deduplication from cross-source linking. Base contracts and built-in strategies are in `src/matchbox/client/models/dedupers/base.py`, `src/matchbox/client/models/linkers/base.py`, and `src/matchbox/client/models/linkers/splinklinker.py`; deterministic and weighted/probabilistic variants coexist. Comparisons and model configuration are centralized in `src/matchbox/client/models/comparison.py` and `src/matchbox/client/models/models.py`.

#### Resolution and Query Flow
A declared DAG combines source configurations and matching steps into reusable resolutions. Resolvers consume matching outputs to form queryable entity/group results, with component logic in `src/matchbox/client/resolvers/components.py` and orchestration in `src/matchbox/client/resolvers/resolvers.py`. Consumers retrieve resolved records through `src/matchbox/client/queries.py` and `src/matchbox/client/results.py`, preserving the separation between matching metadata and underlying warehouse data.

#### Evaluation and Human Feedback
Match quality is measurable and iteratively improved through evaluation samples, judgments, and a terminal UI. Core evaluation support is in `src/matchbox/client/eval/samples.py` and `src/matchbox/common/eval.py`; the interactive evaluator is rooted at `src/matchbox/client/cli/eval/app.py`. Server evaluation routes and persistence adapters expose this workflow to shared users: `src/matchbox/server/api/routers/eval.py`, `src/matchbox/server/postgresql/adapter/eval.py`.

#### Server, Authorization, and Persistence
The server exposes API routers for authentication, collections, groups, evaluation, and main/query operations via `src/matchbox/server/api/routers/auth.py`, `src/matchbox/server/api/routers/collections.py`, and `src/matchbox/server/api/routers/groups.py`. PostgreSQL is the durable metadata/index backend, implemented through ORM, adapter, and migration layers in `src/matchbox/server/postgresql/orm.py`, `src/matchbox/server/postgresql/adapter/main.py`, and `src/matchbox/server/postgresql/alembic/`. Upload handling is isolated in `src/matchbox/server/uploads.py`.

#### Shared Contracts and Runtime Infrastructure
Shared DTOs, data types, Arrow/database integration, transformations, hashing, and exception conventions live under `src/matchbox/common/dtos.py`, `src/matchbox/common/arrow.py`, and `src/matchbox/common/db.py`. The project runs as a Python package; server deployment is containerized through `src/matchbox/server/Dockerfile` and `docker-compose.yml`, with PostgreSQL as the material external service.

Main entry points: [`src/matchbox/server/api/main.py`](https://github.com/uktrade/matchbox/blob/main/src/matchbox/server/api/main.py) (HTTP API), [`src/matchbox/server/postgresql/adapter/main.py`](https://github.com/uktrade/matchbox/blob/main/src/matchbox/server/postgresql/adapter/main.py) (PostgreSQL adapters), [`src/matchbox/client/dags.py`](https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/dags.py) (Pipeline DAGs), [`src/matchbox/client/steps.py`](https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/steps.py) (Execution steps)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client SDK &amp; CLI"]
  node_cli["Client CLI<br/>entry point<br/>[main.py]"]
  node_client_handler["Client handler<br/>server boundary<br/>[main.py]"]
  node_pipeline_dag["Pipeline DAGs<br/>pipeline definition<br/>[dags.py]"]
  node_steps["Execution steps<br/>pipeline steps<br/>[steps.py]"]
  node_sources["Sources &amp; locations<br/>source configuration<br/>[sources.py]"]
  node_model_config["Models &amp; comparisons<br/>matching configuration<br/>[models.py]"]
  node_dedupers["Deduplication strategies<br/>matching plugin<br/>[base.py]"]
  node_linkers["Linking strategies<br/>matching plugin<br/>[splinklinker.py]"]
  node_resolvers["Resolvers<br/>resolution orchestration<br/>[resolvers.py]"]
  node_results["Queries &amp; results<br/>result access<br/>[results.py]"]
  node_evaluation["Evaluation samples<br/>quality workflow<br/>[samples.py]"]
  node_eval_tui["Evaluator TUI<br/>interactive CLI<br/>[app.py]"]
end

subgraph group_server["Server"]
  node_server_api["HTTP API<br/>server entry point<br/>[main.py]"]
  node_api_routes["Auth, collection &amp; group routes<br/>API routers<br/>[auth.py]"]
  node_eval_routes["Evaluation routes<br/>API router<br/>[eval.py]"]
  node_metadata_adapter["PostgreSQL adapters<br/>persistence boundary<br/>[main.py]"]
  node_uploads["Upload handling<br/>ingestion service<br/>[uploads.py]"]
  node_deployment["Container deployment<br/>runtime infrastructure<br/>[docker-compose.yml]"]
end

subgraph group_shared["Shared Core"]
  node_shared_core["Shared DTOs &amp; warehouse integration<br/>shared contracts<br/>[dtos.py]"]
end

node_postgres[("PostgreSQL<br/>metadata and index store")]

node_cli -->|"declares pipelines"| node_pipeline_dag
node_cli -->|"runs"| node_eval_tui
node_pipeline_dag -->|"uses"| node_sources
node_pipeline_dag -->|"orders"| node_steps
node_steps -->|"configures"| node_model_config
node_model_config -->|"selects"| node_dedupers
node_model_config -->|"selects"| node_linkers
node_steps -->|"produces matching outputs"| node_resolvers
node_resolvers -->|"forms resolved results"| node_results
node_results -->|"queries through"| node_client_handler
node_client_handler -->|"calls"| node_server_api
node_server_api -->|"serves"| node_api_routes
node_server_api -->|"serves"| node_eval_routes
node_server_api -->|"accepts uploads"| node_uploads
node_api_routes -->|"persists metadata"| node_metadata_adapter
node_eval_routes -->|"persists judgments"| node_metadata_adapter
node_metadata_adapter -->|"reads and writes"| node_postgres
node_eval_tui -->|"collects judgments"| node_evaluation
node_evaluation -->|"submits samples"| node_client_handler
node_sources -->|"accesses warehouse data"| node_shared_core
node_metadata_adapter -.->|"uses contracts"| node_shared_core
node_deployment -.->|"deploys"| node_server_api
node_deployment -.->|"provisions"| node_postgres

click node_cli "https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/cli/main.py"
click node_client_handler "https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/_handler/main.py"
click node_pipeline_dag "https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/dags.py"
click node_steps "https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/steps.py"
click node_sources "https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/sources.py"
click node_model_config "https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/models/models.py"
click node_dedupers "https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/models/dedupers/base.py"
click node_linkers "https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/models/linkers/splinklinker.py"
click node_resolvers "https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/resolvers/resolvers.py"
click node_results "https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/results.py"
click node_evaluation "https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/eval/samples.py"
click node_eval_tui "https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/cli/eval/app.py"
click node_server_api "https://github.com/uktrade/matchbox/blob/main/src/matchbox/server/api/main.py"
click node_api_routes "https://github.com/uktrade/matchbox/blob/main/src/matchbox/server/api/routers/auth.py"
click node_eval_routes "https://github.com/uktrade/matchbox/blob/main/src/matchbox/server/api/routers/eval.py"
click node_metadata_adapter "https://github.com/uktrade/matchbox/blob/main/src/matchbox/server/postgresql/adapter/main.py"
click node_uploads "https://github.com/uktrade/matchbox/blob/main/src/matchbox/server/uploads.py"
click node_shared_core "https://github.com/uktrade/matchbox/blob/main/src/matchbox/common/dtos.py"
click node_deployment "https://github.com/uktrade/matchbox/blob/main/docker-compose.yml"

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_cli,node_client_handler,node_pipeline_dag,node_steps,node_sources,node_model_config,node_dedupers,node_linkers,node_resolvers,node_results,node_evaluation,node_eval_tui toneBlue
class node_server_api,node_api_routes,node_eval_routes,node_metadata_adapter,node_uploads,node_deployment toneAmber
class node_shared_core toneMint
class node_postgres toneNeutral
```

## Components

### Client SDK & CLI

Pipeline declaration, execution, results, and local evaluation.

- [Client CLI](https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/cli/main.py): entry point · `src/matchbox/client/cli/main.py`
- [Client handler](https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/_handler/main.py): server boundary · `src/matchbox/client/_handler/main.py`
- [Pipeline DAGs](https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/dags.py): pipeline definition · Composes sources and matching steps into reusable resolutions. · `src/matchbox/client/dags.py`
- [Execution steps](https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/steps.py): pipeline steps · `src/matchbox/client/steps.py`
- [Sources & locations](https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/sources.py): source configuration · `src/matchbox/client/sources.py`
- [Models & comparisons](https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/models/models.py): matching configuration · `src/matchbox/client/models/models.py`
- [Deduplication strategies](https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/models/dedupers/base.py): matching plugin · `src/matchbox/client/models/dedupers/base.py`
- [Linking strategies](https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/models/linkers/splinklinker.py): matching plugin · Includes cross-source strategies such as Splink. · `src/matchbox/client/models/linkers/splinklinker.py`
- [Resolvers](https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/resolvers/resolvers.py): resolution orchestration · Turns matching outputs into entity and group results. · `src/matchbox/client/resolvers/resolvers.py`
- [Queries & results](https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/results.py): result access · Retrieves resolved records without copying warehouse data. · `src/matchbox/client/results.py`
- [Evaluation samples](https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/eval/samples.py): quality workflow · `src/matchbox/client/eval/samples.py`
- [Evaluator TUI](https://github.com/uktrade/matchbox/blob/main/src/matchbox/client/cli/eval/app.py): interactive CLI · `src/matchbox/client/cli/eval/app.py`

### Server

Optional shared HTTP API and metadata services.

- [HTTP API](https://github.com/uktrade/matchbox/blob/main/src/matchbox/server/api/main.py): server entry point · `src/matchbox/server/api/main.py`
- [Auth, collection & group routes](https://github.com/uktrade/matchbox/blob/main/src/matchbox/server/api/routers/auth.py): API routers · `src/matchbox/server/api/routers/auth.py`
- [Evaluation routes](https://github.com/uktrade/matchbox/blob/main/src/matchbox/server/api/routers/eval.py): API router · `src/matchbox/server/api/routers/eval.py`
- [PostgreSQL adapters](https://github.com/uktrade/matchbox/blob/main/src/matchbox/server/postgresql/adapter/main.py): persistence boundary · `src/matchbox/server/postgresql/adapter/main.py`
- [Upload handling](https://github.com/uktrade/matchbox/blob/main/src/matchbox/server/uploads.py): ingestion service · `src/matchbox/server/uploads.py`
- [Container deployment](https://github.com/uktrade/matchbox/blob/main/docker-compose.yml): runtime infrastructure · Runs the server alongside PostgreSQL. · `docker-compose.yml`

### Shared Core

Contracts and warehouse integration used across runtimes.

- [Shared DTOs & warehouse integration](https://github.com/uktrade/matchbox/blob/main/src/matchbox/common/dtos.py): shared contracts · Provides DTOs, Arrow/database access, transforms, hashes, and exceptions. · `src/matchbox/common/dtos.py`

### Other components

- **PostgreSQL**: metadata and index store · Durable metadata and indexes; raw records remain in warehouse sources.

## Connections

- Client CLI → Pipeline DAGs: declares pipelines
- Client CLI → Evaluator TUI: runs
- Pipeline DAGs → Sources & locations: uses
- Pipeline DAGs → Execution steps: orders
- Execution steps → Models & comparisons: configures
- Models & comparisons → Deduplication strategies: selects
- Models & comparisons → Linking strategies: selects
- Execution steps → Resolvers: produces matching outputs
- Resolvers → Queries & results: forms resolved results
- Queries & results → Client handler: queries through
- Client handler → HTTP API: calls
- HTTP API → Auth, collection & group routes: serves
- HTTP API → Evaluation routes: serves
- HTTP API → Upload handling: accepts uploads
- Auth, collection & group routes → PostgreSQL adapters: persists metadata
- Evaluation routes → PostgreSQL adapters: persists judgments
- PostgreSQL adapters → PostgreSQL: reads and writes
- Evaluator TUI → Evaluation samples: collects judgments
- Evaluation samples → Client handler: submits samples
- Sources & locations → Shared DTOs & warehouse integration: accesses warehouse data
- PostgreSQL adapters → Shared DTOs & warehouse integration: uses contracts
- Container deployment → HTTP API: deploys
- Container deployment → PostgreSQL: provisions
