# stephensook/trace-forensic-search architecture

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

- Interactive diagram: https://gitdiagram.com/stephensook/trace-forensic-search
- Repository: https://github.com/stephensook/trace-forensic-search

## Overview

1. **What this repo is**
   Trace is a local-first semantic search system for matching missing-person and unidentified-remains records. Architecturally it is a three-tier app: a React frontend, a FastAPI backend, and a local Actian VectorAI DB instance used as the retrieval engine.

2. **Core runtime boundary**
   The backend does not own the search index; it orchestrates ingestion, embedding, filtering, and ranking, then delegates vector lookup to Actian VectorAI DB over local gRPC on port 50051. That makes the database the central persistence and retrieval boundary.

3. **Data source and demo corpus**
   The `data/synthetic` area contains generated case data (`cases.json`) and the generator used to build it. This synthetic corpus is the main ingest target for the demo and test suite, so the system is designed around offline, reproducible records rather than live external feeds.

4. **Ingestion pipeline**
   `backend/ingest.py` is the load path from JSON cases into the vector database. It uses deterministic point IDs, prepares metadata, computes embeddings, and writes each case as an idempotent record so re-ingesting the same case does not duplicate it.

5. **Embedding subsystem**
   `backend/embeddings.py` encapsulates the local model stack described in the README: SapBERT, BGE-M3, and CLIP. The important architectural point is that the system is multi-vector, not single-vector: different case attributes are embedded into separate named spaces for better recall and cleaner ranking.

6. **Vector schema and search model**
   Search is organized around four semantic channels: physical description, circumstances, clothing, and image-based evidence. Each channel is searched independently in the vector DB, then the backend fuses results with Reciprocal Rank Fusion before presenting ranked matches.

7. **Metadata filtering boundary**
   `backend/filters.py` applies non-semantic constraints such as sex, age range, state, case type, and date window before vector search runs. This is a major architectural boundary because it narrows the candidate set early and avoids spending vector retrieval budget on impossible matches.

8. **API layer**
   `backend/main.py` exposes the FastAPI service, while `backend/schemas.py` defines the request/response contracts used by the UI and tests. `backend/search.py` likely contains the orchestration logic for query embedding, filtered retrieval, fusion, and confidence scoring.

9. **Frontend structure**
   The frontend is a single-page React app built with Vite, TanStack Query, and Tailwind. The main feature areas are search input and filters, result cards, case detail pages, and a sidebar/header layout that supports browsing and explanation of matches.

10. **Client-server flow**
   The browser sends search and case-detail requests through `frontend/src/lib/api.ts` to the backend API. The backend returns both ranked candidates and explanatory fields such as “Why This Matched,” which the frontend renders in dedicated result and detail components.

11. **Operational environment**
   `docker-compose.yml` brings up the local VectorAI DB service, while the backend runs in a Python 3.12 environment with local model inference. The repo is intentionally offline after setup, which is important because the data includes sensitive forensic-style records.

12. **Testing and confidence**
   The repository has substantial backend coverage in `backend/tests` for config, filters, ingestion, schemas, search, synthetic data, and the API surface, plus frontend Vitest checks. This suggests the key correctness risks are in query construction, data normalization, and ranking behavior rather than UI rendering alone.

13. **What to draw in an architecture diagram**
   Show the browser UI on one side, FastAPI in the middle, local embedding models as a separate service/process boundary inside the backend runtime, and Actian VectorAI DB as the persistence/search engine on the other side. Add the synthetic data generator and ingest path as a separate offline pipeline feeding the database, with hard filters happening before multi-vector retrieval and rank fusion happening inside the backend.

Main entry points: [`backend/search.py`](https://github.com/stephensook/trace-forensic-search/blob/main/backend/search.py) (Search Orchestrator), [`backend/main.py`](https://github.com/stephensook/trace-forensic-search/blob/main/backend/main.py) (FastAPI API), [`frontend/src/lib/api.ts`](https://github.com/stephensook/trace-forensic-search/blob/main/frontend/src/lib/api.ts) (API Client), [`docker-compose.yml`](https://github.com/stephensook/trace-forensic-search/blob/main/docker-compose.yml) (VectorAI DB)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_frontend_app["App Shell<br/>React SPA<br/>[App.tsx]"]
  node_frontend_search["Search Panel<br/>UI component"]
  node_frontend_results["Results Panel<br/>UI component"]
  node_frontend_detail["Case Detail<br/>page<br/>[CaseDetail.tsx]"]
  node_frontend_api["API Client<br/>client lib<br/>[api.ts]"]
end

subgraph group_backend["Backend"]
  node_backend_api["FastAPI API<br/>[main.py]"]
  node_backend_schema["Contracts<br/>schemas<br/>[schemas.py]"]
  node_backend_search["Search Orchestrator<br/>pipeline<br/>[search.py]"]
  node_backend_filters["Filters<br/>constraint engine<br/>[filters.py]"]
  node_backend_embeddings["Embeddings<br/>model stack<br/>[embeddings.py]"]
  node_backend_ingest["Ingest<br/>loader<br/>[ingest.py]"]
end

subgraph group_data["Data"]
  node_synthetic_gen["Case Generator<br/>data generator<br/>[generate_cases.py]"]
  node_synthetic_cases["Case Corpus<br/>dataset<br/>[cases.json]"]
end

subgraph group_infra["Infra"]
  node_vector_db[("VectorAI DB<br/>vector store<br/>[docker-compose.yml]")]
  node_compose["Compose Stack<br/>runtime<br/>[docker-compose.yml]"]
end

node_frontend_app -->|"renders"| node_frontend_search
node_frontend_app -->|"renders"| node_frontend_results
node_frontend_app -->|"routes to"| node_frontend_detail
node_frontend_search -->|"queries"| node_frontend_api
node_frontend_detail -->|"loads"| node_frontend_api
node_frontend_results -->|"refreshes"| node_frontend_api
node_frontend_api -->|"calls"| node_backend_api
node_backend_api -->|"uses"| node_backend_schema
node_backend_api -->|"delegates"| node_backend_search
node_backend_search -->|"applies"| node_backend_filters
node_backend_search -->|"embeds"| node_backend_embeddings
node_backend_search -->|"retrieves from"| node_vector_db
node_backend_search -->|"returns"| node_backend_schema
node_backend_embeddings -->|"writes spaces"| node_vector_db
node_backend_ingest -->|"reads"| node_synthetic_cases
node_backend_ingest -->|"prepares"| node_backend_embeddings
node_backend_ingest -->|"loads"| node_vector_db
node_synthetic_gen -->|"generates"| node_synthetic_cases
node_compose -->|"starts"| node_vector_db
node_backend_api -.->|"depends on"| node_compose

click node_frontend_app "https://github.com/stephensook/trace-forensic-search/blob/main/frontend/src/App.tsx"
click node_frontend_search "https://github.com/stephensook/trace-forensic-search/blob/main/frontend/src/components/TraceSearchPanel.tsx"
click node_frontend_results "https://github.com/stephensook/trace-forensic-search/blob/main/frontend/src/components/TraceResultsPanel.tsx"
click node_frontend_detail "https://github.com/stephensook/trace-forensic-search/blob/main/frontend/src/pages/CaseDetail.tsx"
click node_frontend_api "https://github.com/stephensook/trace-forensic-search/blob/main/frontend/src/lib/api.ts"
click node_backend_api "https://github.com/stephensook/trace-forensic-search/blob/main/backend/main.py"
click node_backend_schema "https://github.com/stephensook/trace-forensic-search/blob/main/backend/schemas.py"
click node_backend_search "https://github.com/stephensook/trace-forensic-search/blob/main/backend/search.py"
click node_backend_filters "https://github.com/stephensook/trace-forensic-search/blob/main/backend/filters.py"
click node_backend_embeddings "https://github.com/stephensook/trace-forensic-search/blob/main/backend/embeddings.py"
click node_backend_ingest "https://github.com/stephensook/trace-forensic-search/blob/main/backend/ingest.py"
click node_vector_db "https://github.com/stephensook/trace-forensic-search/blob/main/docker-compose.yml"
click node_synthetic_gen "https://github.com/stephensook/trace-forensic-search/blob/main/data/synthetic/generate_cases.py"
click node_synthetic_cases "https://github.com/stephensook/trace-forensic-search/blob/main/data/synthetic/cases.json"
click node_compose "https://github.com/stephensook/trace-forensic-search/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_frontend_app,node_frontend_search,node_frontend_results,node_frontend_detail,node_frontend_api toneBlue
class node_backend_api,node_backend_schema,node_backend_search,node_backend_filters,node_backend_embeddings,node_backend_ingest toneAmber
class node_synthetic_gen,node_synthetic_cases toneMint
class node_vector_db,node_compose toneRose
```

## Components

### Frontend

React SPA for searching, browsing, and explaining matches

- [App Shell](https://github.com/stephensook/trace-forensic-search/blob/main/frontend/src/App.tsx): React SPA · Top-level client application and route composition · `frontend/src/App.tsx`
- [Search Panel](https://github.com/stephensook/trace-forensic-search/blob/main/frontend/src/components/TraceSearchPanel.tsx): UI component · Query entry and filter controls for case search · `frontend/src/components/TraceSearchPanel.tsx`
- [Results Panel](https://github.com/stephensook/trace-forensic-search/blob/main/frontend/src/components/TraceResultsPanel.tsx): UI component · Renders ranked matches and match explanations · `frontend/src/components/TraceResultsPanel.tsx`
- [Case Detail](https://github.com/stephensook/trace-forensic-search/blob/main/frontend/src/pages/CaseDetail.tsx): page · Detailed case view for a selected record · `frontend/src/pages/CaseDetail.tsx`
- [API Client](https://github.com/stephensook/trace-forensic-search/blob/main/frontend/src/lib/api.ts): client lib · Browser-side request layer for backend calls · `frontend/src/lib/api.ts`

### Backend

FastAPI orchestration layer for filtering, embedding, retrieval, and ranking

- [FastAPI API](https://github.com/stephensook/trace-forensic-search/blob/main/backend/main.py): service · HTTP surface for search and case detail requests · `backend/main.py`
- [Contracts](https://github.com/stephensook/trace-forensic-search/blob/main/backend/schemas.py): schemas · Request and response models shared by UI and tests · `backend/schemas.py`
- [Search Orchestrator](https://github.com/stephensook/trace-forensic-search/blob/main/backend/search.py): pipeline · Coordinates query processing, retrieval, fusion, and scoring · `backend/search.py`
- [Filters](https://github.com/stephensook/trace-forensic-search/blob/main/backend/filters.py): constraint engine · Applies sex, age, state, case type, and date constraints before retrieval · `backend/filters.py`
- [Embeddings](https://github.com/stephensook/trace-forensic-search/blob/main/backend/embeddings.py): model stack · Multi-vector local embedding stack for different semantic channels · `backend/embeddings.py`
- [Ingest](https://github.com/stephensook/trace-forensic-search/blob/main/backend/ingest.py): loader · Idempotent load path from synthetic cases into vector storage · `backend/ingest.py`

### Data

Synthetic forensic corpus and offline generation pipeline

- [Case Generator](https://github.com/stephensook/trace-forensic-search/blob/main/data/synthetic/generate_cases.py): data generator · Builds reproducible synthetic case corpus for demo and tests · `data/synthetic/generate_cases.py`
- [Case Corpus](https://github.com/stephensook/trace-forensic-search/blob/main/data/synthetic/cases.json): dataset · Synthetic missing-person and unidentified-remains records · `data/synthetic/cases.json`

### Infra

Local runtime and container orchestration

- [VectorAI DB](https://github.com/stephensook/trace-forensic-search/blob/main/docker-compose.yml): vector store · Local Actian VectorAI retrieval and persistence boundary · `docker-compose.yml`
- [Compose Stack](https://github.com/stephensook/trace-forensic-search/blob/main/docker-compose.yml): runtime · Brings up local database and supporting services · `docker-compose.yml`

## Connections

- App Shell → Search Panel: renders · The app shell composes search controls
- App Shell → Results Panel: renders · The app shell composes result views
- App Shell → Case Detail: routes to · The app shell supports case detail navigation
- Search Panel → API Client: queries · Search UI submits requests through the API client
- Case Detail → API Client: loads · Detail page fetches case data through the API client
- Results Panel → API Client: refreshes · Results view may refetch or update displayed matches
- API Client → FastAPI API: calls · Browser requests go to the FastAPI service
- FastAPI API → Contracts: uses · API endpoints validate payloads with shared contracts
- FastAPI API → Search Orchestrator: delegates · HTTP handlers forward search work to orchestration logic
- Search Orchestrator → Filters: applies · Candidate narrowing happens before semantic retrieval
- Search Orchestrator → Embeddings: embeds · Queries are expanded into multiple semantic channels
- Search Orchestrator → VectorAI DB: retrieves from · Search is executed against the local vector database
- Search Orchestrator → Contracts: returns · Search results are shaped into API response models
- Embeddings → VectorAI DB: writes spaces · Vector spaces are stored and queried in the DB
- Ingest → Case Corpus: reads · Loader consumes the synthetic corpus
- Ingest → Embeddings: prepares · Ingest computes embeddings for each record
- Ingest → VectorAI DB: loads · Ingest writes idempotent case records into the DB
- Case Generator → Case Corpus: generates · Offline generator produces the demo dataset
- Compose Stack → VectorAI DB: starts · Compose launches the local retrieval engine
- FastAPI API → Compose Stack: depends on · Backend expects the local stack to be available
