# davidpal3c/carbonsage_agentic_ai architecture

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

- Interactive diagram: https://gitdiagram.com/davidpal3c/carbonsage_agentic_ai
- Repository: https://github.com/davidpal3c/carbonsage_agentic_ai

## Overview

#### Purpose and deployment shape
CarbonSage is a modular-monolith ESG decision agent for freight Scope 3 analysis. It combines workspace-isolated shipment data, supplier evidence, deterministic emissions/scenario tools, hybrid retrieval, and cited conversational answers. The deployable surfaces are a Next.js client and FastAPI API, locally composed with Docker; production targets Vercel, Render, and Neon PostgreSQL.  
Paths: `nzeroesg-client/app/page.tsx`, `nzeroesg-api/main.py`, `docker-compose.yml`, `render.yaml`.

#### Client control plane
The Next.js/React client provides the public landing page, authenticated workspace dashboard, routed operational pages, and the agent interaction/structured-answer presentation. Dashboard state is shared across route transitions; artifact viewing and workspace-scoped data are client boundaries rather than being embedded in domain logic.  
Paths: `nzeroesg-client/app/dashboard/layout.tsx`, `nzeroesg-client/app/dashboard/WorkspaceShell.tsx`, `nzeroesg-client/app/components/chat_ui/ChatInterface.tsx`.

#### API composition and workspace boundary
FastAPI is the backend entry point and exposes separate HTTP route modules for workspace/session lifecycle, uploads/artifacts, shipments, evidence, scenarios, reports, and conversations. Workspace principals and sessions establish isolation; persistence repositories enforce workspace-scoped access and bounded retained state.  
Paths: `nzeroesg-api/main.py`, `nzeroesg-api/api/workspaces.py`, `nzeroesg-api/domain/workspaces/sessions.py`, `nzeroesg-api/persistence/workspaces.py`.

#### Ingestion and artifact lifecycle
User or demo inputs enter as shipment files and evidence documents. Domain ingestion normalizes records and preserves recoverable source/citation identity; the artifact catalog tracks provenance, downloadable objects, rename, and soft deletion. Object bytes are abstracted behind artifact storage, allowing AWS S3-backed storage without coupling APIs to S3.  
Paths: `nzeroesg-api/domain/shipments/ingestion.py`, `nzeroesg-api/domain/evidence/ingestion.py`, `nzeroesg-api/services/artifact_storage.py`, `nzeroesg-api/integrations/aws_s3.py`.

#### Deterministic decision domain
Freight calculations remain deterministic domain code: distances, modes, units, emission factors, and calculation provenance produce the trusted metrics. Shipment analysis derives hotspots and breakdowns; scenario comparison evaluates alternatives. The agent may request these capabilities but does not own their numerical results.  
Paths: `nzeroesg-api/domain/emissions/calculator.py`, `nzeroesg-api/domain/shipments/analysis.py`, `nzeroesg-api/domain/scenarios/comparison.py`.

#### Evidence retrieval and safety gate
Evidence is chunked and stored with lexical-search data and optional embeddings. Retrieval supports lexical, semantic pgvector, and deterministic hybrid modes, always filtered by workspace; lexical retrieval remains usable without an embedding provider. A typed evidence assessment gate prevents unsupported retrieved passages from becoming rendered or persisted citations.  
Paths: `nzeroesg-api/domain/evidence/retrieval.py`, `nzeroesg-api/domain/evidence/embeddings.py`, `nzeroesg-api/domain/evidence/evaluation.py`, `nzeroesg-api/persistence/migrations/004_pgvector_retrieval.sql`.

#### Agent orchestration and external AI boundary
The conversation API invokes a typed runtime composed from policy-constrained tools, a planner, and an optional LLM adapter. Tool calls, usage, citations, and answers are persisted separately. The assistant fails closed without compatible OpenRouter or OpenAI configuration; deterministic tools, artifacts, and retrieval remain independently available. LangChain is optional orchestration, not the source of truth.  
Paths: `nzeroesg-api/api/agent.py`, `nzeroesg-api/services/agent_runtime.py`, `nzeroesg-api/services/agent_tools.py`, `nzeroesg-api/agent/policy/v1.md`.

#### Data infrastructure and evaluation
Neon PostgreSQL is the system of record for workspace data, normalized shipments, evidence, full-text search, pgvector embeddings, artifacts, and agent runtime records. Schema evolution is explicit in SQL migrations. Checked-in retrieval and answer-support datasets/reports provide regression evaluation independently of live provider credentials.  
Paths: `nzeroesg-api/persistence/migrations/001_workspaces.sql`, `nzeroesg-api/persistence/migrations/007_typed_agent_runtime.sql`, `nzeroesg-api/evaluation/retrieval_cases.json`.

Main entry points: [`nzeroesg-api/main.py`](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/main.py) (FastAPI API), [`nzeroesg-api/services/agent_runtime.py`](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/services/agent_runtime.py) (Typed agent runtime), [`nzeroesg-api/domain/evidence/retrieval.py`](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/domain/evidence/retrieval.py) (Hybrid evidence retrieval), [`nzeroesg-api/services/agent_tools.py`](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/services/agent_tools.py) (Agent tools)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client"]
  node_web_client["Next.js client<br/>web client<br/>[page.tsx]"]
  node_workspace_shell["Workspace shell<br/>client state boundary<br/>[WorkspaceShell.tsx]"]
  node_chat_ui["Chat interface<br/>agent UI<br/>[ChatInterface.tsx]"]
end

subgraph group_api["API &amp; workspace"]
  node_fastapi["FastAPI API<br/>HTTP composition<br/>[main.py]"]
  node_workspace_sessions["Workspace sessions<br/>isolation boundary<br/>[sessions.py]"]
  node_workspace_repositories["Workspace repositories<br/>persistence access<br/>[workspaces.py]"]
end

subgraph group_domain["Decision domain"]
  node_ingestion["Shipment &amp; evidence ingestion<br/>normalization pipeline<br/>[ingestion.py]"]
  node_artifact_storage["Artifact storage<br/>object storage abstraction"]
  node_emissions["Emissions calculator<br/>deterministic calculation<br/>[calculator.py]"]
  node_shipment_analysis["Shipment analysis<br/>deterministic analysis<br/>[analysis.py]"]
  node_scenario_comparison["Scenario comparison<br/>deterministic analysis<br/>[comparison.py]"]
end

subgraph group_agent["Agent &amp; retrieval"]
  node_agent_api["Conversation API<br/>agent route<br/>[agent.py]"]
  node_agent_runtime["Typed agent runtime<br/>orchestrator<br/>[agent_runtime.py]"]
  node_agent_tools["Agent tools<br/>typed capabilities<br/>[agent_tools.py]"]
  node_evidence_retrieval["Hybrid evidence retrieval<br/>workspace-scoped retrieval<br/>[retrieval.py]"]
  node_evidence_gate["Evidence assessment gate<br/>citation safety gate<br/>[evaluation.py]"]
  node_llm_provider["OpenRouter / OpenAI<br/>optional LLM provider"]
end

subgraph group_data["Data infrastructure"]
  node_postgres[("Neon PostgreSQL<br/>system of record<br/>[001_workspaces.sql]")]
  node_pgvector[("pgvector retrieval schema<br/>database capability")]
  node_s3[("AWS S3<br/>object storage provider<br/>[aws_s3.py]")]
  node_evaluation["Regression evaluation<br/>checked-in evaluation data"]
end

node_web_client -->|"hosts dashboard"| node_workspace_shell
node_workspace_shell -->|"shares workspace state"| node_chat_ui
node_web_client -->|"HTTP requests"| node_fastapi
node_chat_ui -->|"conversations"| node_agent_api
node_fastapi -->|"authenticates"| node_workspace_sessions
node_fastapi -->|"workspace-scoped data"| node_workspace_repositories
node_workspace_repositories -->|"persists"| node_postgres
node_fastapi -->|"uploads inputs"| node_ingestion
node_ingestion -->|"normalized records"| node_postgres
node_fastapi -->|"artifact lifecycle"| node_artifact_storage
node_artifact_storage -->|"object bytes"| node_s3
node_fastapi -->|"metrics requests"| node_emissions
node_emissions -->|"trusted metrics"| node_shipment_analysis
node_fastapi -->|"scenario requests"| node_scenario_comparison
node_agent_api -->|"invokes"| node_agent_runtime
node_agent_runtime -->|"planned tool calls"| node_agent_tools
node_agent_runtime -.->|"optional generation"| node_llm_provider
node_agent_tools -->|"calculation capability"| node_emissions
node_agent_tools -->|"scenario capability"| node_scenario_comparison
node_agent_tools -->|"retrieval capability"| node_evidence_retrieval
node_evidence_retrieval -.->|"optional semantic search"| node_pgvector
node_evidence_retrieval -->|"lexical search and chunks"| node_postgres
node_evidence_retrieval -->|"candidate passages"| node_evidence_gate
node_evidence_gate -->|"validated citations"| node_agent_runtime
node_agent_runtime -->|"answers, citations, tool calls, usage"| node_postgres
node_evaluation -.->|"retrieval regression cases"| node_evidence_retrieval

click node_web_client "https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-client/app/page.tsx"
click node_workspace_shell "https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-client/app/dashboard/WorkspaceShell.tsx"
click node_chat_ui "https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-client/app/components/chat_ui/ChatInterface.tsx"
click node_fastapi "https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/main.py"
click node_workspace_sessions "https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/domain/workspaces/sessions.py"
click node_workspace_repositories "https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/persistence/workspaces.py"
click node_ingestion "https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/domain/shipments/ingestion.py"
click node_artifact_storage "https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/services/artifact_storage.py"
click node_emissions "https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/domain/emissions/calculator.py"
click node_shipment_analysis "https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/domain/shipments/analysis.py"
click node_scenario_comparison "https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/domain/scenarios/comparison.py"
click node_agent_api "https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/api/agent.py"
click node_agent_runtime "https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/services/agent_runtime.py"
click node_agent_tools "https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/services/agent_tools.py"
click node_evidence_retrieval "https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/domain/evidence/retrieval.py"
click node_evidence_gate "https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/domain/evidence/evaluation.py"
click node_postgres "https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/persistence/migrations/001_workspaces.sql"
click node_pgvector "https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/persistence/migrations/004_pgvector_retrieval.sql"
click node_s3 "https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/integrations/aws_s3.py"
click node_evaluation "https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/evaluation/retrieval_cases.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_web_client,node_workspace_shell,node_chat_ui toneBlue
class node_fastapi,node_workspace_sessions,node_workspace_repositories toneAmber
class node_ingestion,node_artifact_storage,node_emissions,node_shipment_analysis,node_scenario_comparison toneMint
class node_agent_api,node_agent_runtime,node_agent_tools,node_evidence_retrieval,node_evidence_gate,node_llm_provider toneRose
class node_postgres,node_pgvector,node_s3,node_evaluation toneIndigo
```

## Components

### Client

- [Next.js client](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-client/app/page.tsx): web client · Landing, dashboard, routed operations, and answer presentation. · `nzeroesg-client/app/page.tsx`
- [Workspace shell](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-client/app/dashboard/WorkspaceShell.tsx): client state boundary · Shares dashboard state across route transitions. · `nzeroesg-client/app/dashboard/WorkspaceShell.tsx`
- [Chat interface](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-client/app/components/chat_ui/ChatInterface.tsx): agent UI · `nzeroesg-client/app/components/chat_ui/ChatInterface.tsx`

### API & workspace

- [FastAPI API](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/main.py): HTTP composition · Composes workspace, artifact, shipment, evidence, scenario, report, and conversation routes. · `nzeroesg-api/main.py`
- [Workspace sessions](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/domain/workspaces/sessions.py): isolation boundary · Establishes workspace principals and sessions. · `nzeroesg-api/domain/workspaces/sessions.py`
- [Workspace repositories](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/persistence/workspaces.py): persistence access · Enforces workspace-scoped access and retained-state bounds. · `nzeroesg-api/persistence/workspaces.py`

### Decision domain

- [Shipment & evidence ingestion](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/domain/shipments/ingestion.py): normalization pipeline · Normalizes inputs while preserving source and citation identity. · `nzeroesg-api/domain/shipments/ingestion.py`
- [Artifact storage](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/services/artifact_storage.py): object storage abstraction · Manages downloadable artifact bytes independently of S3. · `nzeroesg-api/services/artifact_storage.py`
- [Emissions calculator](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/domain/emissions/calculator.py): deterministic calculation · Produces trusted metrics and calculation provenance. · `nzeroesg-api/domain/emissions/calculator.py`
- [Shipment analysis](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/domain/shipments/analysis.py): deterministic analysis · Derives hotspots and breakdowns. · `nzeroesg-api/domain/shipments/analysis.py`
- [Scenario comparison](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/domain/scenarios/comparison.py): deterministic analysis · Evaluates freight alternatives. · `nzeroesg-api/domain/scenarios/comparison.py`

### Agent & retrieval

- [Conversation API](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/api/agent.py): agent route · `nzeroesg-api/api/agent.py`
- [Typed agent runtime](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/services/agent_runtime.py): orchestrator · Combines policy-constrained tools, planning, and an optional LLM adapter. · `nzeroesg-api/services/agent_runtime.py`
- [Agent tools](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/services/agent_tools.py): typed capabilities · Exposes deterministic calculations, artifacts, and retrieval to the agent. · `nzeroesg-api/services/agent_tools.py`
- [Hybrid evidence retrieval](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/domain/evidence/retrieval.py): workspace-scoped retrieval · Uses lexical, optional semantic, and deterministic hybrid search. · `nzeroesg-api/domain/evidence/retrieval.py`
- [Evidence assessment gate](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/domain/evidence/evaluation.py): citation safety gate · Blocks unsupported passages from rendered or persisted citations. · `nzeroesg-api/domain/evidence/evaluation.py`
- **OpenRouter / OpenAI**: optional LLM provider · Required for compatible conversational generation; unavailable configuration fails closed.

### Data infrastructure

- [Neon PostgreSQL](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/persistence/migrations/001_workspaces.sql): system of record · Stores workspace, shipment, evidence, artifact, search, embedding, and agent records. · `nzeroesg-api/persistence/migrations/001_workspaces.sql`
- [pgvector retrieval schema](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/persistence/migrations/004_pgvector_retrieval.sql): database capability · Adds embedding-backed semantic retrieval. · `nzeroesg-api/persistence/migrations/004_pgvector_retrieval.sql`
- [AWS S3](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/integrations/aws_s3.py): object storage provider · `nzeroesg-api/integrations/aws_s3.py`
- [Regression evaluation](https://github.com/davidpal3c/carbonsage_agentic_ai/blob/dev/nzeroesg-api/evaluation/retrieval_cases.json): checked-in evaluation data · Validates retrieval and answer support without live provider credentials. · `nzeroesg-api/evaluation/retrieval_cases.json`

## Connections

- Next.js client → Workspace shell: hosts dashboard
- Workspace shell → Chat interface: shares workspace state
- Next.js client → FastAPI API: HTTP requests
- Chat interface → Conversation API: conversations
- FastAPI API → Workspace sessions: authenticates
- FastAPI API → Workspace repositories: workspace-scoped data
- Workspace repositories → Neon PostgreSQL: persists
- FastAPI API → Shipment & evidence ingestion: uploads inputs
- Shipment & evidence ingestion → Neon PostgreSQL: normalized records
- FastAPI API → Artifact storage: artifact lifecycle
- Artifact storage → AWS S3: object bytes
- FastAPI API → Emissions calculator: metrics requests
- Emissions calculator → Shipment analysis: trusted metrics
- FastAPI API → Scenario comparison: scenario requests
- Conversation API → Typed agent runtime: invokes
- Typed agent runtime → Agent tools: planned tool calls
- Typed agent runtime → OpenRouter / OpenAI: optional generation
- Agent tools → Emissions calculator: calculation capability
- Agent tools → Scenario comparison: scenario capability
- Agent tools → Hybrid evidence retrieval: retrieval capability
- Hybrid evidence retrieval → pgvector retrieval schema: optional semantic search
- Hybrid evidence retrieval → Neon PostgreSQL: lexical search and chunks
- Hybrid evidence retrieval → Evidence assessment gate: candidate passages
- Evidence assessment gate → Typed agent runtime: validated citations
- Typed agent runtime → Neon PostgreSQL: answers, citations, tool calls, usage
- Regression evaluation → Hybrid evidence retrieval: retrieval regression cases
