# lecoeurdelest/shb-digital architecture

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

- Interactive diagram: https://gitdiagram.com/lecoeurdelest/shb-digital
- Repository: https://github.com/lecoeurdelest/shb-digital

## Overview

#### Purpose and entry points
Digital Expert Guild is a Vietnamese banking advisory and controlled-action system: customer or bank staff chat with a multi-agent team for credit, legal, product, and operations work; sensitive actions such as disbursement are enforced server-side. Runtime entry points are FastAPI in `backend/app/main.py` and the React SPA in `frontend/src/main.tsx`; Docker Compose provisions the deployed stack via `docker-compose.prod.yml`.

#### Client/API boundary
The React/Vite client provides customer workspace, 3D agent lobby, cards/canvas, and an admin Control Tower. It calls REST and consumes per-conversation SSE; full conversation state is the reconnect boundary.
- `frontend/src/App.tsx`
- `frontend/src/Workspace.tsx`
- `frontend/src/hooks/useConversationSSE.ts`
- `backend/app/api/conversations.py`
- `backend/app/api/sse.py`

#### Identity, personas, and authorization
Authentication supports credentials and Google OAuth, issuing HTTP-only JWT cookies. Request dependencies establish customer versus bank/admin identity; data visibility is enforced through role/owner read scope, including tool-layer protection.
- `backend/app/auth/router.py`
- `backend/app/auth/google.py`
- `backend/app/auth/deps.py`
- `backend/app/mount/read_scope.py`

#### Persistent banking and operational state
PostgreSQL 15 is the shared system of record for banking data, conversations, task/render state, approvals, audit records, retrieval data, and LLM usage. SQLAlchemy/Alembic define and evolve schema; seeds supply demo business data and policy corpus.
- `backend/app/db/models.py`
- `backend/app/db/migrations/`
- `backend/app/db/seed_from_lab.py`
- `deploy/seed/shb-132.db`

#### Agent orchestration lifecycle
A chat request starts a durable MAIN session that plans and synthesizes. Dispatch schedules independent role SUB agents; their completion/events can wake MAIN to continue or resume after restart. The orchestration shell owns idempotent dispatch and state transitions, while role-specific reasoning remains in prompts/skills.
- `backend/app/orch/main_session.py`
- `backend/app/orch/dispatch.py`
- `backend/app/orch/sub_runner.py`
- `backend/app/orch/registry.py`

#### Extensible role tools and retrieval
Role packages are mounted dynamically: each domain supplies a `SKILL.md` and Python business tools, while mount adapters provide schema, Postgres access, and read scope. Shared retrieval handles policy wiki/document lineage, local vector notes, and related-entity exposure checks. Policy source material is local under the deployment seed.
- `roles/credit/functions.py`
- `roles/legal/functions.py`
- `roles/_retrieval/functions.py`
- `backend/app/mount/pg_adapter.py`
- `deploy/seed/wiki/`

#### Controlled actions, audit, and presentation
SUB tools emit structured cards and trace events for the frontend. Audit is append-only around tool calls. Disbursement is a hard control boundary: approval records keyed by normalized payload hash are atomically claimed and executed in the same database transaction, with owner checks and auto/manual authority verdicts.
- `backend/app/orch/gated.py`
- `backend/app/orch/disburse_guard.py`
- `backend/app/orch/audit_emit.py`
- `backend/app/orch/common_tools.py`
- `backend/app/sse/redact.py`

#### Runtime integrations and operations
Python 3.11/FastAPI runs the backend; React 19/Vite serves the UI behind nginx. LLM providers are registry-configured and selected per conversation/turn, supporting Claude SDK, GLM, GPT gateway, and local Ollama. Optional Gmail email notifications and SSE support operational workflows; cloudflared is part of deployment infrastructure.
- `configs/providers.yaml`
- `backend/app/orch/providers.py`
- `backend/app/notify/email.py`
- `frontend/nginx.conf`
- `backend/Dockerfile`

Main entry points: [`backend/app/orch/sub_runner.py`](https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/orch/sub_runner.py) (Role SUB runners), [`backend/app/main.py`](https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/main.py) (FastAPI application), [`backend/app/orch/main_session.py`](https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/orch/main_session.py) (Durable MAIN session), [`backend/app/db/models.py`](https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/db/models.py) (PostgreSQL system of record)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client"]
  node_spa["React workspace &amp; Control Tower<br/>React SPA<br/>[App.tsx]"]
  node_conversation_sse_hook["Conversation SSE client<br/>React hook"]
end

subgraph group_backend["Backend"]
  node_api["FastAPI application<br/>API runtime<br/>[main.py]"]
  node_conversations_api["Conversations API<br/>REST router<br/>[conversations.py]"]
  node_sse_api["SSE endpoint<br/>event stream<br/>[sse.py]"]
  node_auth["Authentication &amp; identity<br/>auth layer<br/>[router.py]"]
  node_read_scope["Role/owner read scope<br/>authorization<br/>[read_scope.py]"]
end

subgraph group_agents["Agent Runtime"]
  node_main_session{{"Durable MAIN session<br/>orchestrator<br/>[main_session.py]"}}
  node_dispatch["Agent dispatch<br/>scheduler<br/>[dispatch.py]"]
  node_sub_runner["Role SUB runners<br/>agent workers<br/>[sub_runner.py]"]
  node_provider_registry["LLM provider selection<br/>provider registry<br/>[providers.py]"]
  node_gated_actions{{"Disbursement control gate<br/>transactional guard<br/>[disburse_guard.py]"}}
  node_audit_cards["Audit, cards &amp; redaction<br/>presentation controls<br/>[audit_emit.py]"]
end

subgraph group_extensions["Roles &amp; Retrieval"]
  node_role_tools["Credit &amp; legal role tools<br/>role packages<br/>[functions.py]"]
  node_retrieval["Policy retrieval<br/>retrieval tools<br/>[functions.py]"]
  node_pg_adapter["Postgres mount adapter<br/>data adapter<br/>[pg_adapter.py]"]
end

subgraph group_operations["Data &amp; Deployment"]
  node_postgres[("PostgreSQL system of record<br/>PostgreSQL 15<br/>[models.py]")]
  node_deployment["Production deployment<br/>Docker Compose stack"]
end

node_spa -->|"REST requests"| node_api
node_spa -->|"uses"| node_conversation_sse_hook
node_conversation_sse_hook -->|"SSE"| node_sse_api
node_api -->|"routes"| node_conversations_api
node_api -->|"routes"| node_sse_api
node_api -->|"authenticates"| node_auth
node_auth -->|"establishes identity"| node_read_scope
node_conversations_api -->|"starts or resumes"| node_main_session
node_main_session -->|"delegates"| node_dispatch
node_dispatch -->|"schedules"| node_sub_runner
node_sub_runner -.->|"completion wakes"| node_main_session
node_sub_runner -->|"invokes"| node_role_tools
node_sub_runner -->|"retrieves policy/context"| node_retrieval
node_sub_runner -->|"uses model"| node_provider_registry
node_role_tools -->|"accesses data through"| node_pg_adapter
node_pg_adapter -->|"applies scope"| node_read_scope
node_pg_adapter -->|"queries"| node_postgres
node_main_session -->|"requests controlled action"| node_gated_actions
node_gated_actions -->|"atomic approval and execution"| node_postgres
node_sub_runner -->|"emits traces and cards"| node_audit_cards
node_audit_cards -->|"appends audit"| node_postgres
node_audit_cards -->|"publishes redacted events"| node_sse_api
node_deployment -->|"runs backend"| node_api
node_deployment -->|"serves via nginx"| node_spa
node_deployment -->|"provisions"| node_postgres

click node_spa "https://github.com/lecoeurdelest/shb-digital/blob/main/frontend/src/App.tsx"
click node_conversation_sse_hook "https://github.com/lecoeurdelest/shb-digital/blob/main/frontend/src/hooks/useConversationSSE.ts"
click node_api "https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/main.py"
click node_conversations_api "https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/api/conversations.py"
click node_sse_api "https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/api/sse.py"
click node_auth "https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/auth/router.py"
click node_read_scope "https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/mount/read_scope.py"
click node_main_session "https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/orch/main_session.py"
click node_dispatch "https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/orch/dispatch.py"
click node_sub_runner "https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/orch/sub_runner.py"
click node_provider_registry "https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/orch/providers.py"
click node_role_tools "https://github.com/lecoeurdelest/shb-digital/blob/main/roles/credit/functions.py"
click node_retrieval "https://github.com/lecoeurdelest/shb-digital/blob/main/roles/_retrieval/functions.py"
click node_pg_adapter "https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/mount/pg_adapter.py"
click node_gated_actions "https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/orch/disburse_guard.py"
click node_audit_cards "https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/orch/audit_emit.py"
click node_postgres "https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/db/models.py"
click node_deployment "https://github.com/lecoeurdelest/shb-digital/blob/main/docker-compose.prod.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_spa,node_conversation_sse_hook toneBlue
class node_api,node_conversations_api,node_sse_api,node_auth,node_read_scope toneAmber
class node_main_session,node_dispatch,node_sub_runner,node_provider_registry,node_gated_actions,node_audit_cards toneMint
class node_role_tools,node_retrieval,node_pg_adapter toneRose
class node_postgres,node_deployment toneIndigo
```

## Components

### Client

- [React workspace & Control Tower](https://github.com/lecoeurdelest/shb-digital/blob/main/frontend/src/App.tsx): React SPA · Customer and bank-staff workspace, agent lobby, cards, and admin views. · `frontend/src/App.tsx`
- [Conversation SSE client](https://github.com/lecoeurdelest/shb-digital/blob/main/frontend/src/hooks/useConversationSSE.ts): React hook · Reconnects using full conversation state. · `frontend/src/hooks/useConversationSSE.ts`

### Backend

- [FastAPI application](https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/main.py): API runtime · Backend entry point and REST boundary. · `backend/app/main.py`
- [Conversations API](https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/api/conversations.py): REST router · `backend/app/api/conversations.py`
- [SSE endpoint](https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/api/sse.py): event stream · Streams per-conversation updates. · `backend/app/api/sse.py`
- [Authentication & identity](https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/auth/router.py): auth layer · Credential and Google OAuth login issue HTTP-only JWT cookies. · `backend/app/auth/router.py`
- [Role/owner read scope](https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/mount/read_scope.py): authorization · Enforces customer versus bank/admin visibility, including tools. · `backend/app/mount/read_scope.py`

### Agent Runtime

- [Durable MAIN session](https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/orch/main_session.py): orchestrator · Plans, synthesizes, and resumes conversations. · `backend/app/orch/main_session.py`
- [Agent dispatch](https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/orch/dispatch.py): scheduler · Idempotently schedules role sub-agents and transitions state. · `backend/app/orch/dispatch.py`
- [Role SUB runners](https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/orch/sub_runner.py): agent workers · Execute independent domain reasoning and emit completion events. · `backend/app/orch/sub_runner.py`
- [LLM provider selection](https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/orch/providers.py): provider registry · Selects configured Claude, GLM, GPT gateway, or Ollama providers per turn. · `backend/app/orch/providers.py`
- [Disbursement control gate](https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/orch/disburse_guard.py): transactional guard · Atomically claims approval by normalized payload hash and executes with owner and authority checks. · `backend/app/orch/disburse_guard.py`
- [Audit, cards & redaction](https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/orch/audit_emit.py): presentation controls · Emits append-only tool audit and structured cards while redacting SSE output. · `backend/app/orch/audit_emit.py`

### Roles & Retrieval

- [Credit & legal role tools](https://github.com/lecoeurdelest/shb-digital/blob/main/roles/credit/functions.py): role packages · Dynamically mounted domain skills and Python business tools. · `roles/credit/functions.py`
- [Policy retrieval](https://github.com/lecoeurdelest/shb-digital/blob/main/roles/_retrieval/functions.py): retrieval tools · Provides policy, vector-note, lineage, and related-entity access checks. · `roles/_retrieval/functions.py`
- [Postgres mount adapter](https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/mount/pg_adapter.py): data adapter · Supplies tool schemas and PostgreSQL access. · `backend/app/mount/pg_adapter.py`

### Data & Deployment

- [PostgreSQL system of record](https://github.com/lecoeurdelest/shb-digital/blob/main/backend/app/db/models.py): PostgreSQL 15 · Stores banking, conversation, task, approval, audit, retrieval, and usage state. · `backend/app/db/models.py`
- [Production deployment](https://github.com/lecoeurdelest/shb-digital/blob/main/docker-compose.prod.yml): Docker Compose stack · Deploys backend, UI/nginx, database, and supporting infrastructure. · `docker-compose.prod.yml`

## Connections

- React workspace & Control Tower → FastAPI application: REST requests
- React workspace & Control Tower → Conversation SSE client: uses
- Conversation SSE client → SSE endpoint: SSE
- FastAPI application → Conversations API: routes
- FastAPI application → SSE endpoint: routes
- FastAPI application → Authentication & identity: authenticates
- Authentication & identity → Role/owner read scope: establishes identity
- Conversations API → Durable MAIN session: starts or resumes
- Durable MAIN session → Agent dispatch: delegates
- Agent dispatch → Role SUB runners: schedules
- Role SUB runners → Durable MAIN session: completion wakes
- Role SUB runners → Credit & legal role tools: invokes
- Role SUB runners → Policy retrieval: retrieves policy/context
- Role SUB runners → LLM provider selection: uses model
- Credit & legal role tools → Postgres mount adapter: accesses data through
- Postgres mount adapter → Role/owner read scope: applies scope
- Postgres mount adapter → PostgreSQL system of record: queries
- Durable MAIN session → Disbursement control gate: requests controlled action
- Disbursement control gate → PostgreSQL system of record: atomic approval and execution
- Role SUB runners → Audit, cards & redaction: emits traces and cards
- Audit, cards & redaction → PostgreSQL system of record: appends audit
- Audit, cards & redaction → SSE endpoint: publishes redacted events
- Production deployment → FastAPI application: runs backend
- Production deployment → React workspace & Control Tower: serves via nginx
- Production deployment → PostgreSQL system of record: provisions
