Skip to main content

Architecture Overview

Trust Relay is a KYB/KYC compliance workflow system that automates the "close the loop" process between compliance officers and end customers. Officers create cases, customers upload documents through a branded portal, AI agents cross-reference documents against public registries and adverse media sources, and officers make approve/reject/follow-up decisions in iterative loops.

The system is built as an event-driven workflow application using Temporal for durable execution, FastAPI for the HTTP API layer, and a Next.js frontend for both the officer dashboard and the customer portal.

High-Level Architecture

Design Principles

The architecture follows a set of deliberate principles that govern every layer of the system.

Authentication and Tenant Isolation

All API access is authenticated via Keycloak 26 with JWT tokens validated against JWKS endpoints. Multi-tenant isolation is enforced at the database level through PostgreSQL Row-Level Security (RLS) across 22+ tenant-scoped tables. RLS policies use a FORCE ROW LEVEL SECURITY directive so that even the table-owner role is subject to isolation — the system is safe-by-default, returning zero rows when the tenant context is unset.

PII Protection

20 PII fields are classified across the data model using a typed annotation system (PII_DIRECT, PII_QUASI, PII_SENSITIVE). Six fields containing direct identifiers (national IDs, passport numbers, bank accounts) are encrypted at rest with AES-256-GCM via dedicated encrypted columns. PII classification is enforced through 85+ tests covering annotation completeness, encryption round-trips, and migration integrity.

Data Access

The data layer uses SQLAlchemy 2.0 ORM models (63 mapped classes in app/db/models.py) as the single source of truth, with a generic BaseRepository[T] pattern for type-safe CRUD operations. Schema evolution is managed through 58 Alembic migrations covering the full lifecycle from initial schema through PII encryption, sanctions suppression rules, and decision memoranda. All queries use parameterized ORM access — no string interpolation touches SQL.

Observability

Structured JSON logging with correlation IDs propagates trace context across HTTP requests, Temporal activities, and agent invocations. Every AI-driven decision is captured in an append-only audit_events table with full input provenance, model identification, and confidence scoring — meeting EU AI Act Article 12 (automatic logging) and Article 14 (human oversight) requirements.

Evidence-Based AI Decisions

Every AI output is anchored to verifiable evidence through the Trust Capsule cryptographic architecture (ADR-0017). Each capsule contains the source data, model version, prompt template, reasoning chain, and a cryptographic seal that detects post-hoc tampering. The Evidence Bundle system (ADR-0021) packages related capsules into auditable units that satisfy GDPR Article 22 (right to explanation of automated decisions) and 6AMLD 5-year retention requirements.

Modular API Design

The REST API is decomposed into 52 focused routers organized by domain concern (case_crud, case_decisions, case_documents, case_analysis, case_evidence, portal, graph, lex, risk_config, decision_memorandum, sanctions_suppression, costs, etc.). Dependencies are injected via FastAPI's Depends() pattern with singleton service instances, ensuring testability and clear ownership boundaries.

Key Architectural Decisions

DecisionChoiceRationale
Workflow engineTemporalDurable execution, built-in retry policies, signal/query pattern fits the iterative compliance loop. See ADR-0002.
AI frameworkPydanticAIType-safe agent outputs via Pydantic models, native MCP tool support, per-agent model configuration. See ADR-0001.
AuthenticationKeycloak 26Standards-based OIDC/JWT, JWKS rotation, multi-tenant realm configuration.
Tenant isolationPostgreSQL RLSDatabase-enforced isolation — application bugs cannot leak data across tenants. See ADR-0023.
Document conversionIBM DoclingMIT-licensed, local execution, no data leaves the infrastructure.
Object storageMinIOS3-compatible API, stores documents organized by case/iteration.
Frontend AICopilotKit v2 + AG-UIEmbeds AI assistant into the officer dashboard (inline chat, popup, useCopilotReadable). See ADR-0003, ADR-0013.
Knowledge graphNeo4j (CQRS read layer)Cross-case analytics, co-directorship detection, fraud pattern matching. PostgreSQL remains the write store. See ADR-0014.
Risk assessmentEBA 5-dimension matrixWeighted-max aggregation across customer, product, channel, geography, and transaction risk. See ADR-0020.
Country routing12-country registry architectureDedicated agents and registry clients per jurisdiction with structured service catalogs. See ADR-0034, ADR-0042, ADR-0043.
White-label brandingTenant-scoped WCAG AAPer-tenant visual identity with enforced accessibility contrast ratios. See ADR-0028.

System Metrics

MetricValue
Architecture Decision Records48 (ADR-0001 through ADR-0049; ADR-0044 withdrawn)
PII test coverage85+ tests
PII fields classified20 fields, 6 encrypted at rest (AES-256-GCM)
OSINT agents33 agent modules in app/agents/ (multi-stage pipeline with circuit breakers)
Country registries12 countries (BE, CZ, EE, CH, FR, NL, NO, DK, FI, RO, SK + DE via NorthData) — 28 registry-client modules under app/services/registries/
RLS-protected tables22+ (FORCE ROW LEVEL SECURITY)
Alembic migrations58 (latest: 058_decision_memoranda)
ORM models63 SQLAlchemy 2.0 mapped classes
API routers52
Backend services138 modules under app/services/ (incl. lex/, registries/, verification/, financial_analysis/, mutation_queue/, vop_providers/, document_validators/)
Prompt templates36 Jinja2 templates (DB-first with filesystem fallback)

Production Roadmap

ItemPriorityPath Forward
Secret managementMediumMove from .env files to a managed secret store (AWS Secrets Manager, HashiCorp Vault).
Testcontainers expansionLow40 integration tests with testcontainer PostgreSQL, macOS Docker Desktop support. Done.
Log forwardingLowForward structured JSON logs to centralized aggregation (ELK/Datadog). Logging infrastructure is in place; forwarding pipeline is not yet configured. See Deployment.

Architecture Decision Records

All significant technical decisions are documented as ADRs in docs/adr/. The project maintains 48 ADRs (ADR-0001 through ADR-0049, with ADR-0044 withdrawn during review) covering every major architectural choice — from workflow orchestration and AI framework selection through cryptographic sealing, regulatory segment compilation, multi-country registry design, async Docling extraction, financial distress modelling, and cross-reference identity-mismatch detection.

ADRDecisionStatus
ADR-0001PydanticAI v1.60+ with AG-UI protocol for AI layerAccepted
ADR-0002Temporal Python SDK for workflow orchestrationAccepted
ADR-0003Mount AGUIAdapter on FastAPI (not standalone)Accepted
ADR-0004Pin CopilotKit v1 APISuperseded by ADR-0013
ADR-0005STATE_SNAPSHOT over STATE_DELTA for AG-UI eventsAccepted
ADR-0006PEPPOL Verify as REST API (not MCP)Accepted
ADR-0007Belgian data layer, country routing, and PEPPOL UIImplemented
ADR-0008Raw SQL via SQLAlchemy text() for database accessAccepted
ADR-0009Minimal error handling with silent recovery for PoCAccepted
ADR-0010React useState/useEffect for state managementAccepted (PoC) — effectively superseded by TanStack React Query v5
ADR-0011Authentication deliberately deferred for PoCSuperseded 2026-04-18 — Keycloak OIDC with JWKS-validated JWTs in app/api/deps/auth.py; realm-per-tenant; role hierarchy
ADR-0012Hybrid scraping tool selection per data sourceImplemented
ADR-0013CopilotKit v2 migration (supersedes ADR-0004)Accepted
ADR-0014Native bi-temporal graph (drop Graphiti)Implemented
ADR-0015Session-based investigation diagnosticsImplemented
ADR-0016Shared regulatory corpus (Lex) without tenant RLSImplemented
ADR-0017Trust Capsule cryptographic architectureImplemented
ADR-0018Dynamic document requirements — pre-investigation resolutionImplemented
ADR-0019Multi-agent OSINT pipeline with country routingImplemented
ADR-0020EBA risk matrix with weighted-max aggregationImplemented
ADR-0021Evidence bundle system for EU AI Act audit trailImplemented
ADR-0022Neo4j knowledge graph with 20-step ETL pipelineImplemented
ADR-0023PostgreSQL Row-Level Security for multi-tenant isolationImplemented
ADR-0024Entity matching with blocking keys and trust-weighted survivorshipImplemented
ADR-0025Network Intelligence Hub — ReactFlow three-perspective visualizationImplemented
ADR-0026Prompt centralization with DB-first registry and filesystem fallbackImplemented
ADR-0027GoAML export with three-layer pipeline and country profilesImplemented
ADR-0028White-label branding with WCAG AA enforcementImplemented
ADR-0029Cost-optimized model tiers for agent fleetImplemented
ADR-0030Social intelligence via BrightData MCP expansionImplemented
ADR-0031Regulatory segment profiles with declarative YAML compilerImplemented
ADR-0032Circuit breakers for OSINT pipeline resilienceImplemented
ADR-0033Document gap analysis engineImplemented
ADR-0034Multi-country registry architecture (12 countries, 21 services)Implemented
ADR-0035Atlas reference documentation within DocusaurusImplemented
ADR-0036PII classification with hybrid annotations and generated manifestImplemented
ADR-0037Shared package extraction (trustrelay-* monorepo)Implemented
ADR-0038Shell company detection via establishment address comparisonImplemented
ADR-0039Resilience rollout completion and KBC gap signalsImplemented
ADR-0040Observability metrics + breaker state persistenceImplemented
ADR-0041Pure-mailbox detection for shell companiesImplemented
ADR-0042CZ regulatory and professional registries (CNB, debarment, KDP, sbírka listin)Implemented
ADR-0043Cross-country registry parity (decoders, director shapes, KBO layout fix)Implemented
ADR-0044(Withdrawn during review — number reserved)Withdrawn
ADR-0045Sanctions false-positive suppression (3-tier, evidence-based, always visible)Implemented
ADR-0046NBB CBSO CSV endpoint degradation and honest data-gap surfacingImplemented
ADR-0047Async Docling extraction (Option B — background activity, idempotent MinIO writes)Implemented
ADR-0048Financial Analysis Agent — ratios + Altman/Ohlson/Zmijewski distress modelsImplemented
ADR-0049Cross-reference registration-number identity-mismatch detectionImplemented

Architecture Documentation

This overview connects to detailed documentation for each subsystem:

  • Data Flow — 12-step investigation-first compliance loop
  • Temporal Workflows — State machine, signals, queries, and retry policies
  • OSINT Pipeline — multi-agent investigation engine with circuit breakers
  • AI Agents — PydanticAI agents, CopilotKit integration, AG-UI protocol
  • Knowledge Graph — Neo4j CQRS read layer with 20-step ETL
  • Security — Authentication, RLS, PII classification, Trust Capsules
  • Deployment — Docker Compose, CI/CD, health checks