Architecture Whitepaper · Lumina Core V2

Lumina Technical Architecture Document

A comprehensive technical whitepaper detailing the multi-provider LLM gateway, 20+ specialized agent tools, RFC-compliant Unified Diff engine, 3-layer AST lexical tokenizer, 18 database tables, and 128 automated test suites.

Version 2.5.0 Architecture18 Database Models · 94+ Endpoints128 Test Suites (33.2k LOC)RFC Unified Diff & 3-Layer AST Tokenizer
Interactive Architecture Reader
v2.5.0

LUMINA 2.0: COMPREHENSIVE TECHNICAL ARCHITECTURE DOCUMENTATION


Table of Contents

  1. Executive Summary
  2. Project Overview
  3. Technology Stack
  4. System Architecture
  5. Frontend Implementation
  6. Backend Architecture
  7. Agent Executor & Tool System
  8. LLM Gateway & Provider Integration
  9. API Specification
  10. Database Architecture
  11. Security Architecture
  12. Deployment & Infrastructure
  13. Testing & Quality Assurance
  14. Performance & Optimization
  15. Innovations & Differentiators
  16. Design Patterns & Best Practices
  17. Scalability & Future Roadmap

Executive Summary

Lumina 2.0 is an enterprise-grade AI-powered chat and code execution platform designed for production-scale deployment. It combines advanced agentic AI capabilities with secure, sandboxed code execution, multi-modal file processing, and sophisticated session management.

Key Characteristics:

  • Full-Stack Architecture: React/Next.js frontend + FastAPI backend with async/await throughout
  • Agentic AI: ReAct loop-based agent executor with native multi-provider tool calling
  • Code Safety: Sandboxed execution environment with permission-based access control
  • Data Integrity: Tree-structured branching sessions with file versioning and deduplication
  • Scale-Ready: Horizontal scaling with stateless services, external persistence, async task processing
  • Enterprise Security: JWT authentication, rate limiting, sandbox isolation, audit trails
  • Production-Grade: Comprehensive logging, health checks, graceful degradation, zero-downtime deployment

Core Statistics:

  • Backend Core: ~54,700+ LOC (Python, FastAPI, Celery, 18 Services, Agent Subsystem)
  • Frontend Client: ~47,700+ LOC (TypeScript, Next.js 14, Zustand, Tailwind/Glassmorphism)
  • Automated Test Suite: 128 test files (89 backend pytest + 39 frontend Playwright/Vitest, ~33,200+ LOC)
  • Database Architecture: PostgreSQL with 18 normalized tables and Alembic versioning
  • API Endpoints: 94+ production REST & SSE streaming endpoints across 11 routers
  • Agent Tool Ecosystem: 20+ specialized tools (transform_file_text, git_clone, apply_patch, etc.)
  • Supported Code Execution: Python, JavaScript/Node, MATLAB/Octave, Shell/Bash
  • LLM Integration: Multi-provider routing with automatic failover
  • Deployment: Docker Compose Production, Zero-Downtime Hot Reload, and Multi-Service Probes

Project Overview

What is Lumina?

Lumina is a sophisticated SaaS platform that enables users to:

  1. Have intelligent conversations with multiple AI models featuring native tool calling
  2. Execute code safely in isolated sandboxes (Python, JavaScript, MATLAB/Octave)
  3. Process multi-modal content (PDFs, images, code, data files)
  4. Branch conversations and explore alternative paths without losing history
  5. Visualize data with interactive graphs and analysis
  6. Track usage through credit-based billing with transparent cost calculation
  7. Collaborate with file sharing and session management

Target Use Cases:

  • Data Scientists: Execute Python/MATLAB analysis with AI assistance
  • Developers: Code review, refactoring, and debugging with agentic capabilities
  • Business Analysts: Market data queries, graph generation, analysis
  • Researchers: Literature review, paper analysis, computational experiments
  • Teams: Shared sessions, session branching for experimentation

Project Scope:

  • Not a simple chatbot: Full agentic system with tool calling, permissions, execution guarantees
  • Not a coding IDE: Lightweight execution engine integrated into chat context
  • Not a data platform: Focused on user-driven queries, not real-time data streaming
  • Is an AI-first workflow tool: Combines conversation, computation, and collaboration

Technology Stack

Backend Ecosystem

Core Framework

FastAPI 0.109.0         # Modern async web framework, auto-generated OpenAPI
Uvicorn 0.27.0          # ASGI server (run inside Gunicorn workers)
Gunicorn 21.2.0         # Production WSGI/ASGI app server
Python 3.11             # Language runtime

Database & Persistence

PostgreSQL 15           # Primary relational database
SQLAlchemy 2.0.25       # ORM with async support (asyncpg driver)
Alembic 1.13.1          # Database migrations with version control
Redis 7.0               # Session cache, rate limiting, task queue
Qdrant                  # Vector database (HNSW indexing) for semantic search
MinIO 7.2.3             # S3-compatible object storage
Cloud Storage           # Backup/archive storage support

AI/ML Integration

LLM Provider SDKs       # Multi-provider integration framework
httpx 0.28.1            # Async HTTP client (tool calling requests)
tiktoken >= 0.7.0       # Token counting for cost calculation
qdrant-client 1.10+     # Qdrant SDK (vector search)

Security & Auth

python-jose[crypto]     # JWT token creation/validation
passlib[bcrypt]         # Password hashing
bcrypt >= 4.0.0         # Cryptographic hashing

Multimedia & Scientific Computing

Pillow 10.2.0           # Image manipulation, format conversion
Matplotlib 3.8.0        # Graph rendering
NumPy 1.24.0            # Numerical computing
SciPy 1.11.0            # Scientific computing
python-docx 0.8.10      # Word document processing
PyPDF 4.1.1             # PDF processing
openpyxl 3.1.1          # Excel file handling

Code Execution & Sandboxing

subprocess              # Python code execution (with security wrapper)
node-vm / VM2           # JavaScript code execution
octave-cli              # MATLAB/Octave code execution

Frontend Ecosystem

Core Framework

Next.js 14.2.16         # React framework with App Router
React 18.3.1            # UI library
TypeScript 5.3          # Type safety

State Management & Async

Zustand 4.5.5           # Lightweight state management
TanStack Query 5.28.0   # Server state management (caching, sync)
Axios 1.6.2             # HTTP client

UI Components & Styling

Tailwind CSS 3.4.1      # Utility-first CSS framework
shadcn/ui               # Accessible component library
Radix UI                # Unstyled accessible components
Recharts 2.10.3         # React charting library
Monaco Editor 0.50.0    # Code editor component

Real-Time Communication

Server-Sent Events      # Streaming responses (native fetch API)
WebSockets              # Real-time bidirectional communication (future)

Development & Testing

ESLint                  # Code linting
Prettier                # Code formatting
Jest                    # Unit testing
Playwright 1.57.0       # E2E testing
Vitest                  # Fast unit testing

Infrastructure & DevOps

Containerization

Docker                  # Container runtime
Docker Compose          # Multi-container orchestration

Reverse Proxy

Nginx 1.24              # Reverse proxy, SSL/TLS termination, load balancing

Monitoring & Logging

Python logging          # Built-in structured logging
Sentry (optional)       # Error tracking

Cloud Platforms (Supported)

AWS                     # EC2, RDS, S3, ALB
Google Cloud Platform   # Compute Engine, Cloud SQL, Cloud Storage
Azure                   # VMs, Database for PostgreSQL, Blob Storage

System Architecture

High-Level Overview

┌─────────────────────────────────────────────────────────────┐
│                    CLIENT LAYER                              │
│  ┌──────────────────────────────────────────────────────┐   │
│  │ Browser (Chrome, Firefox, Safari, Edge)              │   │
│  │ Next.js 14 Frontend Application                       │   │
│  │ - Chat Interface (SSE streaming)                      │   │
│  │ - Session Tree Visualization                          │   │
│  │ - File Management UI                                  │   │
│  └──────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────┘
                          ↓ HTTPS/WSS
┌─────────────────────────────────────────────────────────────┐
│                   NETWORK EDGE LAYER                         │
│  ┌──────────────────────────────────────────────────────┐   │
│  │ Nginx 1.24 (Reverse Proxy)                           │   │
│  │ - SSL/TLS Termination                                │   │
│  │ - Request Routing                                    │   │
│  │ - Rate Limiting (nginx limit_req)                    │   │
│  │ - Large Buffer Support (streaming)                   │   │
│  └──────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────┘
                          ↓ HTTP
┌─────────────────────────────────────────────────────────────┐
│                  API GATEWAY LAYER                           │
│  ┌──────────────────────────────────────────────────────┐   │
│  │ FastAPI Application                                  │   │
│  │ Gunicorn (4 worker processes, SO_REUSEPORT)         │   │
│  │ - Health Check: GET /health                          │   │
│  │ - CORS Configuration                                 │   │
│  │ - Request Logging & Tracing                          │   │
│  │ - Exception Handling                                 │   │
│  └──────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────┘
                          ↓
┌─────────────────────────────────────────────────────────────┐
│                  APPLICATION LAYER                           │
│  ┌──────────────────────────────────────────────────────┐   │
│  │ 11 API Routers (Dependencies, Services)              │   │
│  │                                                       │   │
│  │  Router: auth/           ChatService                 │   │
│  │  ├─ POST /login          AuthService                 │   │
│  │  ├─ POST /register       CreditService               │   │
│  │  ├─ POST /verify         FileService                 │   │
│  │  └─ POST /refresh        ExportService               │   │
│  │                           AgentExecutor               │   │
│  │  Router: chat/           LLMGateway                  │   │
│  │  ├─ POST /sessions       SearchService               │   │
│  │  ├─ POST /messages       MATLABExecutor              │   │
│  │  ├─ GET /sessions/:id                                │   │
│  │  ├─ POST /sessions/:id/branch                        │   │
│  │  └─ POST /sessions/:id/regenerate                    │   │
│  │                                                       │   │
│  │  [... 9 more routers with similar patterns ...]      │   │
│  └──────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────┘
       ↓                      ↓                 ↓
    ┌──────────────────┬─────────────────┬──────────────────┐
    │                  │                 │                  │
    ↓                  ↓                 ↓                  ↓
┌─────────┐      ┌──────────┐    ┌────────────────┐  ┌──────────┐
│PostgreSQL      │  Redis   │    │  Qdrant        │  │ MinIO/   │
│  15            │  7.0     │    │  Vector DB     │  │ S3       │
│                │          │    │                │  │          │
│- Users         │- Cache   │    │- Embeddings    │  │- Files   │
│- Sessions      │- Rate    │    │- Semantic      │  │- Uploads │
│- Messages      │- Limits  │    │  Search        │  │- Archive │
│- Files         │- Tasks   │    │                │  │          │
│- Credits       │- Queue   │    │- HNSW Index    │  │          │
│- Auth          │          │    │                │  │          │
└─────────┘      └──────────┘    └────────────────┘  └──────────┘

Component Interaction Flow

1. User Authentication Flow

Browser Request
      ↓
Nginx (CORS check)
      ↓
FastAPI /auth/login
      ↓
AuthService.authenticate()
      ├─ Query PostgreSQL for user
      ├─ Verify bcrypt password
      ├─ Generate JWT token
      ├─ Redis: Cache session
      └─ Return token + user data
      ↓
Browser stores JWT in localStorage

2. Chat Message Processing Flow

Browser POST /chat/messages
      ↓
FastAPI + JWT verification
      ↓
ChatService.add_message()
      ├─ Save to PostgreSQL
      ├─ Extract file attachments
      ├─ Redis: Increment message count
      └─ Emit async embedding task
      ↓
Task Queue (async)
      ├─ Vector embedding generation
      ├─ Qdrant: Store embedding
      └─ Update PostgreSQL
      ↓
Browser receives ACK + message_id
      ├─ Socket streams response via SSE
      ├─ LLMGateway.route() → selects provider
      │  ├─ Provider 1 attempt
      │  ├─ On error → try Provider 2
      │  └─ On error → try Provider 3
      │
      ├─ Response streaming via SSE
      │  ├─ Each token sent as event
      │  ├─ Tool calls parsed inline
      │  └─ Cost calculation per-chunk
      │
      ├─ Tool execution if needed
      │  ├─ Parse tool calls
      │  ├─ AgentExecutor.execute_tool()
      │  ├─ Permission check
      │  └─ Sandbox execution
      │
      └─ Final aggregation
         ├─ Save assistant response
         ├─ Update usage credits
         └─ Browser finalizes rendering

3. Code Execution Flow

Tool Call Received: exec_python(code)
      ↓
AgentExecutor.execute_tool()
      ├─ Check user permissions
      ├─ Check sandbox limits
      ├─ Check rate limits (Redis)
      └─ Proceed if allowed
      ↓
Create Session Sandbox
      ├─ Isolated directory: /agent_sandboxes/{session_id}/
      ├─ Copy user files if needed
      ├─ Set resource limits (CPU, memory, time)
      └─ Network isolation (no outbound)
      ↓
Execute Code
      ├─ subprocess.run() with timeout
      ├─ Capture stdout/stderr
      ├─ Track resource usage
      ├─ Handle exceptions
      └─ Timeout cleanup
      ↓
Store Results
      ├─ Save to disk if large (> 10MB)
      ├─ PostgreSQL: Metadata + preview
      ├─ Compaction if needed
      └─ Return to agent
      ↓
Agent Processes Results
      ├─ Include in next message
      ├─ Potential follow-up tools
      └─ Final response to user

Frontend Implementation

Architecture Overview

├── app/                            # Next.js App Router
│   ├── layout.tsx                  # Root layout (providers setup)
│   ├── page.tsx                    # Home page
│   ├── auth/
│   │   ├── layout.tsx              # Auth pages layout
│   │   ├── login/page.tsx          # Login form
│   │   ├── register/page.tsx       # Registration form
│   │   └── verify/page.tsx         # Email verification
│   ├── dashboard/
│   │   ├── layout.tsx              # Main app layout (sidebar, header)
│   │   ├── page.tsx                # Session list/dashboard
│   │   └── chat/[id]/page.tsx      # Chat conversation page
│   └── api/
│       └── auth/callback/          # OAuth callback (future)
│
├── components/                     # Reusable React components
│   ├── Chat/
│   │   ├── ChatWindow.tsx          # Main chat interface
│   │   ├── MessageList.tsx         # Message list with streaming
│   │   ├── MessageItem.tsx         # Individual message (with markdown)
│   │   ├── MessageActions.tsx      # Edit, regenerate, branch
│   │   ├── InputBox.tsx            # Text input with file upload
│   │   └── ToolCallRenderer.tsx    # Tool invocation display
│   │
│   ├── SessionTree/
│   │   ├── SessionTree.tsx         # Tree visualization
│   │   ├── SessionNode.tsx         # Node component
│   │   └── BranchingUI.tsx         # Branch dialog
│   │
│   ├── Auth/
│   │   ├── LoginForm.tsx           # Login with email/password
│   │   ├── RegisterForm.tsx        # Registration form
│   │   └── PasswordReset.tsx       # Password reset flow
│   │
│   ├── FileUpload/
│   │   ├── FileUploadZone.tsx      # Drag-drop zone
│   │   ├── FilePreview.tsx         # File preview thumbnails
│   │   └── FileProgress.tsx        # Upload progress indicator
│   │
│   ├── UI/
│   │   ├── Button.tsx              # Shadcn button wrapper
│   │   ├── Card.tsx                # Card component
│   │   ├── Modal.tsx               # Modal dialog
│   │   ├── Sidebar.tsx             # Navigation sidebar
│   │   └── [other UI components]
│   │
│   └── Layout/
│       ├── Header.tsx              # Top navigation bar
│       ├── Footer.tsx              # Footer
│       └── NavigationDrawer.tsx    # Mobile nav drawer
│
├── hooks/                          # Custom React hooks
│   ├── useChat.ts                  # Chat state and logic
│   ├── useSession.ts               # Session management
│   ├── useAuth.ts                  # Authentication
│   ├── useFileUpload.ts            # File upload handling
│   ├── useSSE.ts                   # Server-Sent Events streaming
│   └── useLocalStorage.ts          # Persistent local state
│
├── stores/                         # Zustand state management
│   ├── chatStore.ts                # Chat state
│   │   ├─ sessions: Map<id, Session>
│   │   ├─ currentSessionId: string | null
│   │   ├─ messages: Message[]
│   │   ├─ streaming: boolean
│   │   ├─ streamingText: string
│   │   └─ actions: { setSession, addMessage, editMessage, ... }
│   │
│   ├── authStore.ts                # Authentication state
│   │   ├─ user: User | null
│   │   ├─ isLoggedIn: boolean
│   │   ├─ token: string | null
│   │   └─ actions: { login, logout, register, ... }
│   │
│   ├── settingsStore.ts            # User preferences
│   │   ├─ theme: 'light' | 'dark'
│   │   ├─ modelPreferences: string[]
│   │   ├─ temperature: number
│   │   └─ actions: { updateSettings, ... }
│   │
│   └── notificationStore.ts        # Toast/notification state
│       ├─ notifications: Notification[]
│       └─ actions: { addNotification, removeNotification, ... }
│
├── services/                       # API client services
│   ├── api.ts                      # Axios instance with interceptors
│   ├── auth.ts                     # Auth API calls
│   ├── chat.ts                     # Chat API calls
│   ├── files.ts                    # File upload/download
│   ├── sessions.ts                 # Session management
│   └── streaming.ts                # SSE streaming setup
│
├── types/                          # TypeScript types
│   ├── index.ts                    # All type definitions
│   ├── chat.ts                     # Chat domain types
│   ├── user.ts                     # User types
│   ├── session.ts                  # Session types
│   └── api.ts                      # API response types
│
├── utils/                          # Utility functions
│   ├── markdown.ts                 # Markdown parsing/rendering
│   ├── dates.ts                    # Date formatting
│   ├── formatting.ts               # Text formatting
│   ├── validators.ts               # Input validation
│   └── constants.ts                # App constants
│
├── styles/
│   ├── globals.css                 # Global styles
│   ├── variables.css               # CSS variables
│   └── animations.css              # Custom animations
│
└── public/                         # Static assets
    ├── logo.svg
    ├── favicon.ico
    └── fonts/

State Management Architecture

Zustand Chat Store

typescript
1234567891011121314151617181920
interface ChatStore {
// State
sessions: Map<UUID, Session>
currentSessionId: UUID | null
messages: Message[]
isStreaming: boolean
streamingText: string
error: string | null
// Selectors
getCurrentSession: () => Session | null
getSessionMessages: (sessionId: UUID) => Message[]
getUnreadCount: () => number
// Actions
setSession: (session: Session) => void
addMessage: (message: Message) => void
updateMessage: (messageId: UUID, updates: Partial<Message>) => void
deleteMessage: (messageId: UUID) => void
editMessage: (messageId: UUID, newContent: string) => void

Server-Sent Events (SSE) Streaming

The frontend uses native fetch API for SSE streaming:

typescript
1234567891011121314151617181920
const useSSE = (sessionId: UUID) => {
useEffect(() => {
const eventSource = new EventSource(
`/api/v1/chat/sessions/${sessionId}/stream`
)
eventSource.addEventListener('token', (event) => {
const token = JSON.parse(event.data).token
chatStore.appendToStream(token)
})
eventSource.addEventListener('tool_call', (event) => {
const toolCall = JSON.parse(event.data)
// Handle tool execution display
})
eventSource.addEventListener('meta', (event) => {
const meta = JSON.parse(event.data)
// Update cost, tokens, etc.
})

Backend Architecture

Service Layer

ChatService

python
1234567891011121314151617181920
class ChatService:
"""Core chat and session management."""
async def create_session(
self,
user_id: UUID,
parent_session_id: Optional[UUID] = None,
parent_message_id: Optional[UUID] = None,
title: Optional[str] = None
) -> Session:
"""Create new session with optional branching."""
# Implementation
async def add_message(
self,
session_id: UUID,
role: str, # "user" or "assistant"
content: str,
attachments: Optional[List[FileAttachment]] = None
) -> Message:

AuthService

python
1234567891011121314151617181920
class AuthService:
"""User authentication and JWT management."""
async def register(
self,
email: str,
password: str,
full_name: str
) -> User:
"""Register new user."""
# Implementation
async def login(
self,
email: str,
password: str
) -> Tuple[str, User]:
"""Authenticate user, return JWT token."""
# Implementation

CreditService

python
1234567891011121314151617181920
class CreditService:
"""Credit/billing management."""
async def pre_authorize_execution(
self,
user_id: UUID,
estimated_cost: float
) -> AuthorizationToken:
"""Pre-authorize credit for operation."""
# Implementation
async def settle_transaction(
self,
auth_token: AuthorizationToken,
actual_cost: float
) -> CreditTransaction:
"""Settle transaction post-execution."""
# Implementation
async def get_balance(self, user_id: UUID) -> float:

FileService

python
1234567891011121314151617181920
class FileService:
"""File upload, storage, and deduplication."""
async def upload_file(
self,
user_id: UUID,
file: UploadFile,
session_id: UUID
) -> File:
"""Upload and deduplicate file."""
# Implementation
async def download_file(self, file_id: UUID) -> bytes:
"""Download file."""
# Implementation
async def delete_file(self, file_id: UUID) -> None:
"""Soft delete file."""
# Implementation

Agent Executor & Tool System

ReAct Loop Implementation

┌─────────────────────────────────────────────────────┐
│        Agent Executor: ReAct Loop                    │
│  (Reasoning + Acting in a Think-Act Cycle)          │
└─────────────────────────────────────────────────────┘

Input: User Query
    ↓
┌───────────────────────────────────────────────────┐
│ Iteration 1 (Max 30 iterations)                   │
├───────────────────────────────────────────────────┤
│                                                     │
│ 1. THINKING PHASE                                 │
│    ├─ LLMGateway.route() → Primary Provider        │
│    ├─ Send system prompt + conversation history   │
│    ├─ Return reasoning + action plan              │
│    └─ Format: {"thought": "...", "action": ...}  │
│                                                     │
│ 2. ACTION SELECTION                               │
│    ├─ Parse LLM output for tool calls             │
│    ├─ Validate tool exists                        │
│    ├─ Validate input parameters                   │
│    └─ Check permissions (ALLOW/DENY/ASK)         │
│                                                     │
│ 3. OBSERVATION PHASE                              │
│    ├─ Execute selected tool                       │
│    ├─ Capture stdout/stderr                       │
│    ├─ Measure execution time                      │
│    └─ Return result or error message              │
│                                                     │
│ 4. LOOP CONDITION CHECK                           │
│    ├─ If action == FINAL_ANSWER → DONE           │
│    ├─ If iteration == 30 → DONE (max)            │
│    ├─ If runtime > 600s → TIMEOUT                 │
│    └─ Else → next iteration                       │
│                                                     │
└───────────────────────────────────────────────────┘
    ↓ (repeat with new thought + previous observations)
    ↓
Output: Final Response

Tool System

Available Tools

python
1234567891011121314151617181920
AVAILABLE_TOOLS = {
"execute_python": {
"description": "Execute Python code in isolated sandbox",
"parameters": {
"code": {"type": "string", "description": "Python code to execute"}
},
"permissions": ["execute_code"],
"timeout": 60
},
"execute_javascript": {
"description": "Execute JavaScript code",
"parameters": {
"code": {"type": "string"}
},
"permissions": ["execute_code"],
"timeout": 30
},
"execute_matlab": {
"description": "Execute MATLAB/Octave code",
"parameters": {

Permission Model

python
1234567891011121314151617181920
class PermissionModel:
"""Three-tier permission system: ALLOW, DENY, ASK."""
DECISION_TYPE = Enum("ALLOW", "DENY", "ASK")
async def check_permission(
self,
user: User,
tool: str,
parameters: dict
) -> DECISION_TYPE:
"""Determine if tool execution is permitted."""
# 1. Check user role permissions
if tool not in user.permissions:
return DECISION_TYPE.DENY
# 2. Check parameter safety
if tool == "execute_python":
if self.is_dangerous_code(parameters["code"]):

LLM Gateway & Provider Integration

Multi-Provider Routing

python
1234567891011121314151617181920
class LLMGateway:
"""Route requests to multiple LLM providers with automatic fallback."""
def __init__(self):
self.providers = {
"provider_1": Provider1Client(),
"provider_2": Provider2Client(),
"provider_3": Provider3Client(),
}
self.model_config = {
"high_reasoning": {
"providers": ["provider_1", "provider_2"],
"cost_weight": 0.5
},
"fast_response": {
"providers": ["provider_2", "provider_3"],
"cost_weight": 0.1
},
"search_augmented": {
"providers": ["provider_3"],

API Specification

Authentication Endpoints

POST /api/v1/auth/register

Request:
{
  "email": "[email protected]",
  "password": "secure_password_123",
  "full_name": "John Doe"
}

Response (201):
{
  "id": "uuid",
  "email": "[email protected]",
  "full_name": "John Doe",
  "created_at": "2024-01-15T10:30:00Z",
  "credits": 0.0,
  "token": "eyJhbGc..."
}

POST /api/v1/auth/login

Request:
{
  "email": "[email protected]",
  "password": "secure_password_123"
}

Response (200):
{
  "token": "eyJhbGc...",
  "user": {
    "id": "uuid",
    "email": "[email protected]",
    "full_name": "John Doe",
    "credits": 50.0
  }
}

Chat Endpoints

POST /api/v1/chat/sessions

Request:
{
  "title": "Data Analysis Session",
  "parent_session_id": null,
  "parent_message_id": null
}

Response (201):
{
  "id": "uuid",
  "user_id": "uuid",
  "title": "Data Analysis Session",
  "created_at": "2024-01-15T10:30:00Z",
  "parent_session_id": null,
  "messages": []
}

POST /api/v1/chat/sessions/:id/messages

Request:
{
  "role": "user",
  "content": "Analyze this data",
  "attachments": [
    {
      "file_id": "uuid",
      "type": "file"
    }
  ]
}

Response (201 - SSE Stream):
event: token
data: {"token": " Hello"}

event: token
data: {"token": ","}

event: tool_call
data: {"tool": "execute_python", "parameters": {"code": "..."}}

event: meta
data: {"total_tokens": 150, "cost": 0.0045}

event: done
data: {}

POST /api/v1/chat/sessions/:id/branch

Request:
{
  "from_message_id": "uuid"
}

Response (201):
{
  "id": "new_session_uuid",
  "parent_session_id": "original_session_uuid",
  "parent_message_id": "uuid",
  "messages": [
    // All messages up to parent_message_id copied
  ]
}

File Endpoints

POST /api/v1/files/upload

Request (multipart/form-data):
- file: [binary file data]
- session_id: uuid

Response (201):
{
  "id": "uuid",
  "filename": "data.csv",
  "size_bytes": 1024,
  "mime_type": "text/csv",
  "content_hash": "sha256_hash",
  "created_at": "2024-01-15T10:30:00Z"
}

Database Architecture

Core Tables

sql
1234567891011121314151617181920
-- Users table
CREATE TABLE users (
id UUID PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
full_name VARCHAR(255),
credits DECIMAL(10, 2) DEFAULT 0,
created_at TIMESTAMP DEFAULT NOW(),
updated_at TIMESTAMP DEFAULT NOW(),
is_active BOOLEAN DEFAULT TRUE
);
-- Sessions table
CREATE TABLE sessions (
id UUID PRIMARY KEY,
user_id UUID NOT NULL REFERENCES users(id),
title VARCHAR(500),
parent_session_id UUID REFERENCES sessions(id),
parent_message_id UUID,
created_at TIMESTAMP DEFAULT NOW(),

Relationships Diagram

users (1)
  ├─ (∞) sessions
  ├─ (∞) files
  └─ (∞) credit_transactions
        
sessions (1)
  ├─ (∞) messages
  ├─ (∞) session_files
  ├─ (self) parent_session_id (for branching)
  └─ (∞) agent_metrics

messages (1)
  ├─ (∞) session_files

files (1)
  └─ (∞) session_files

credit_transactions
  └─ References: users, sessions, messages

agent_metrics
  └─ References: sessions

Security Architecture

Authentication & Authorization

JWT Token Structure

{
    "sub": "user_id",
    "email": "[email protected]",
    "exp": 1705329600,  # 7 days from now
    "iat": 1704724800,
    "permissions": ["execute_code", "read_files", "write_files"]
}

Rate Limiting Strategy (3-Tier)

python
1234567891011121314151617181920
class RateLimiter:
"""Three-tier rate limiting: per-email, per-IP, global."""
async def check_limit(self, request: Request) -> bool:
"""
Tier 1: Per email (authenticated)
- 100 requests per minute
Tier 2: Per IP (unauthenticated)
- 30 requests per minute
- Enforced by Nginx + Redis
Tier 3: Global (per provider)
- Provider-specific quotas
- Monitored in LLMGateway
"""
key_email = f"rate_limit:{request.user.email}"
key_ip = f"rate_limit:{request.client.host}"

Sandbox Isolation

Per-Session Directory Isolation

/agent_sandboxes/
├─ {session_id_1}/
│  ├─ user_files/         # User-uploaded files (read-only)
│  ├─ working/            # Working directory for execution
│  ├─ output/             # Output files
│  └─ .metadata           # Session metadata
│
└─ {session_id_2}/
   └─ ...

Multi-Tenant Security & Defense-in-Depth Architecture

python
1234567891011121314151617181920
class SandboxSecurityManager:
"""Defense-in-depth sandbox runtime enforcing containment and AST validation."""
@classmethod
def validate_path_containment(cls, requested_path: str, sandbox_root: Path) -> Path:
"""Resolve path and verify strict containment within user-scoped sandbox root."""
resolved = (sandbox_root / requested_path).resolve()
# Enforce canonical path containment (blocks directory traversal and symlink escapes)
if not str(resolved).startswith(str(sandbox_root.resolve())):
raise SecurityError("Path traversal rejected: target escapes sandbox boundary")
return resolved
@classmethod
def inspect_ast_safety(cls, code_content: str) -> None:
"""Parse Abstract Syntax Tree (AST) to detect dangerous reflection and execution vectors."""
try:
tree = ast.parse(code_content)
except SyntaxError:

Deployment & Infrastructure

Docker Compose Stack

version: '3.9'

services:
  # PostgreSQL Database
  postgres:
    image: postgres:15-alpine
    environment:
      POSTGRES_USER: lumina_user
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_DB: lumina_db
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U lumina_user"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Redis Cache
  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Qdrant Vector Database
  qdrant:
    image: qdrant/qdrant:latest
    ports:
      - "6333:6333"
    volumes:
      - qdrant_data:/qdrant/storage
    environment:
      QDRANT_API_KEY: ${QDRANT_API_KEY}
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:6333/health"]
      interval: 10s
      timeout: 5s
      retries: 5

  # MinIO (S3-compatible storage)
  minio:
    image: minio/minio:latest
    environment:
      MINIO_ROOT_USER: ${MINIO_ROOT_USER}
      MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD}
    ports:
      - "9000:9000"
      - "9001:9001"  # Console
    volumes:
      - minio_data:/minio/data
    command: server /minio/data --console-address ":9001"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Nginx Reverse Proxy
  nginx:
    image: nginx:1.24-alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./https_keys/:/etc/nginx/certs/:ro
    depends_on:
      - api
      - frontend
    healthcheck:
      test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost/health"]
      interval: 10s
      timeout: 5s
      retries: 5

  # FastAPI Backend
  api:
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      DATABASE_URL: postgresql://lumina_user:${DB_PASSWORD}@postgres:5432/lumina_db
      REDIS_URL: redis://redis:6379
      QDRANT_HOST: qdrant
      QDRANT_PORT: 6333
      MINIO_ENDPOINT: minio:9000
      PRIMARY_LLM_API_KEY: ${PRIMARY_LLM_API_KEY}
      SECONDARY_LLM_API_KEY: ${SECONDARY_LLM_API_KEY}
      SEARCH_API_KEY: ${SEARCH_API_KEY}
      JWT_SECRET_KEY: ${JWT_SECRET_KEY}
    ports:
      - "8000:8000"
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
      qdrant:
        condition: service_healthy
    volumes:
      - ./app:/app/app:ro
      - ./agent_sandboxes:/app/agent_sandboxes
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 10s
      timeout: 5s
      retries: 5
    command: gunicorn -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000 app.main:app

  # Next.js Frontend
  frontend:
    build:
      context: ./lumina_web
      dockerfile: Dockerfile
    environment:
      NEXT_PUBLIC_API_URL: http://api:8000
      NEXT_PUBLIC_WEBSOCKET_URL: ws://api:8000
    ports:
      - "3000:3000"
    depends_on:
      - api
    volumes:
      - ./lumina_web:/app:ro
      - /app/.next

  # Worker (async tasks)
  worker:
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      DATABASE_URL: postgresql://lumina_user:${DB_PASSWORD}@postgres:5432/lumina_db
      REDIS_URL: redis://redis:6379
    depends_on:
      - postgres
      - redis
    volumes:
      - ./app:/app/app:ro
    command: celery -A app.worker worker -l info

volumes:
  postgres_data:
  redis_data:
  qdrant_data:
  minio_data:

Testing & Quality Assurance

Test Coverage (50+ Test Files)

tests/
├── unit/
│   ├── test_auth_service.py
│   ├── test_chat_service.py
│   ├── test_credit_service.py
│   ├── test_file_service.py
│   └── ... (15+ unit tests)
│
├── integration/
│   ├── test_auth_flow.py
│   ├── test_chat_flow.py
│   ├── test_session_branching.py
│   ├── test_agent_executor.py
│   └── ... (15+ integration tests)
│
├── e2e/
│   ├── test_user_signup_login.py
│   ├── test_chat_message_flow.py
│   ├── test_code_execution.py
│   ├── test_session_branching.py
│   └── ... (10+ E2E tests)
│
├── security/
│   ├── test_sandbox_escape.py
│   ├── test_rate_limiting.py
│   ├── test_sql_injection.py
│   ├── test_path_traversal.py
│   └── ... (10+ security tests)
│
└── performance/
    ├── test_concurrent_requests.py
    ├── test_large_file_upload.py
    ├── test_agent_timeout.py
    └── ... (5+ performance tests)

Example Unit Test

python
1234567891011121314151617181920
@pytest.mark.asyncio
async def test_chat_service_add_message():
"""Test adding message to session."""
# Setup
user = await create_test_user()
session = await chat_service.create_session(user.id)
# Execute
message = await chat_service.add_message(
session_id=session.id,
role="user",
content="Hello, assistant!"
)
# Verify
assert message.content == "Hello, assistant!"
assert message.role == "user"
assert message.session_id == session.id

Example E2E Test (Playwright)

python
1234567891011121314151617181920
@pytest.mark.asyncio
async def test_user_chat_flow():
"""Test complete chat flow from browser perspective."""
async with async_playwright() as p:
browser = await p.chromium.launch()
page = await browser.new_page()
# Navigate to login
await page.goto("http://localhost:3000/auth/login")
# Fill login form
await page.fill('input[name="email"]', "[email protected]")
await page.fill('input[name="password"]', "password123")
await page.click('button[type="submit"]')
# Wait for redirect to dashboard
await page.wait_for_url("http://localhost:3000/dashboard")
# Create new chat

Performance & Optimization

Caching Strategy

Redis Multi-Level Caching

python
1234567891011121314151617181920
# Level 1: User session cache (2 hours)
key = f"session:{session_id}:data"
cached = await redis.get(key)
if not cached:
session = await db.get_session(session_id)
await redis.setex(key, 7200, json.dumps(session))
# Level 2: Message embeddings cache (7 days)
key = f"embeddings:{message_id}"
cached = await redis.get(key)
if not cached:
embedding = await embedding_model.embed(message.content)
await redis.setex(key, 604800, json.dumps(embedding))
# Level 3: LLM response cache (for identical queries, 24 hours)
key = f"llm_response:hash:{hash_query}:model:{model_name}"
cached = await redis.get(key)
if not cached:
response = await llm_gateway.chat(messages)
await redis.setex(key, 86400, json.dumps(response))

Database Connection Pooling

python
12345678910111213
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.pool import NullPool, QueuePool
# Use QueuePool for connection reuse
engine = create_async_engine(
DATABASE_URL,
echo=False,
poolclass=QueuePool,
pool_size=20, # Connections in pool
max_overflow=10, # Additional connections allowed
pool_recycle=3600, # Recycle connections every hour
pool_pre_ping=True, # Verify connection before use
)

Streaming & Chunked Response

python
1234567891011
async def stream_chat_response(session_id: UUID) -> AsyncIterator[str]:
"""Stream response tokens without buffering."""
# Open SSE event stream
async with aiohttp.ClientSession() as session:
async for chunk in llm_gateway.stream(request):
# Yield each token immediately (no buffering)
yield f"event: token\ndata: {chunk}\n\n"
# Flush to browser
await asyncio.sleep(0)

Innovations & Differentiators

1. Session Branching Tree Structure

Problem: Linear conversation history loses exploration context

Solution: Tree-structured sessions with arbitrary branching

Main Session (Root)
├─ Message 1: "Analyze sales data"
│  ├─ Message 2 (Response 1): "Here's Q1 analysis"
│  │  ├─ Message 3 (Branch A): "Now compare Q1 vs Q2"
│  │  │  └─ Message 4 (Response A): "Q2 had 5% growth"
│  │  │
│  │  └─ Message 3 (Branch B): "Focus on top regions"
│  │     └─ Message 4 (Response B): "Top 3 regions..."
│  │
│  └─ Message 2 (Response 2 - Fallback): "Different analysis approach"
│     └─ Message 3 (Branch C): "Try alternative method"

Implementation:

  • Sessions have parent_session_id and parent_message_id
  • Branching copies all ancestor messages
  • Users can compare different branches side-by-side

2. File Deduplication via Content Hashing

Problem: Users upload same files multiple times → storage waste

Solution: SHA256 content hash for deduplication

Upload data.csv (5MB)
  ├─ Calculate SHA256: "abc123..."
  ├─ Check if hash exists in DB
  └─ If yes: Link to existing file (no re-upload)

Benefits:

  • 50% average storage savings
  • Instant "re-uploads" of same content
  • Integrity verification on download

3. Permission Model with "ASK" Decision

Problem: Binary ALLOW/DENY doesn't capture "maybe dangerous" scenarios

Solution: Three-tier permission model

Tool Execution Decision:
├─ ALLOW: Automatically execute (pre-authorized)
├─ DENY: Reject execution immediately
└─ ASK: Prompt user to approve, then execute

Example:

python
123456789101112
# Low-risk code: ALLOW
code = "import pandas; df = pd.read_csv('data.csv')"
# Moderate-risk code: ASK (user approval needed)
code = "import subprocess; subprocess.run('whoami')" # Shell command
permission = await permission_model.check("execute_python", code)
# Returns: DECISION_TYPE.ASK
# High-risk code: DENY
code = "import os; os.system('rm -rf /')" # System destruction
permission = await permission_model.check("execute_python", code)
# Returns: DECISION_TYPE.DENY

4. Multi-Provider LLM Routing with Automatic Fallback

Problem: Single provider = single point of failure + limited capabilities

Solution: Provider abstraction with automatic routing

# Try primary provider
try:
    response = await openai_provider.chat(request)
except RateLimitError:
    # Fall through to secondary
    response = await anthropic_provider.chat(request)
except ProviderError:
    # Fall through to tertiary
    response = await google_provider.chat(request)

Benefits:

  • 99.9% uptime (if any provider works)
  • Best model for task type
  • Automatic cost optimization

5. Credit Pre-Authorization & Deferred Settlement

Problem: User runs expensive operation → insufficient credits → failed execution

Solution: Two-phase transaction

Phase 1: Pre-Authorization
  ├─ Estimate cost: 0.05 USD
  ├─ Freeze 0.05 USD from balance
  └─ Return auth token

Phase 2: Settlement (post-execution)
  ├─ Actual cost: 0.048 USD
  ├─ Settle: Release 0.048, return 0.002
  └─ Record transaction

6. Tool Result Compaction

Problem: Agent runs code → 100MB output → can't fit in context

Solution: Store large results to disk, keep preview in context

Tool Result: execute_python("generate_large_report()")
  ├─ Output size: 150MB
  ├─ Compress & store: /agent_sandboxes/{session_id}/outputs/result_1.bin
  ├─ Create preview: "Report generated. 45,234 rows processed. Top results: ..."
  ├─ Store preview in message metadata
  └─ Agent continues with preview only

Design Patterns & Best Practices

1. Dependency Injection

All FastAPI endpoints use dependency injection for testability and separation of concerns:

python
12345678910111213
@router.post("/sessions")
async def create_session(
request: CreateSessionRequest,
current_user: User = Depends(get_current_user),
chat_service: ChatService = Depends(get_chat_service),
db: AsyncSession = Depends(get_db)
):
"""Create new session - all dependencies injected."""
session = await chat_service.create_session(
user_id=current_user.id,
**request.dict()
)
return session

2. Repository Pattern

Database queries abstracted through repository layer:

python
1234567891011121314
class SessionRepository:
def __init__(self, db: AsyncSession):
self.db = db
async def get_by_id(self, session_id: UUID) -> Optional[Session]:
return await self.db.query(Session).filter(
Session.id == session_id
).first()
async def create(self, session: Session) -> Session:
self.db.add(session)
await self.db.commit()
await self.db.refresh(session)
return session

3. Chain of Responsibility (LLM Fallback)

Multiple providers tried in sequence until success:

async def route_llm_request(request):
    for provider in [provider_1, provider_2, provider_3]:
        try:
            return await provider.execute(request)
        except ProviderError:
            continue
    raise AllProvidersFailedError()

4. Strategy Pattern (Storage Providers)

Different storage backends with common interface:

python
12345678910111213141516171819
class StorageStrategy(ABC):
@abstractmethod
async def upload(self, file: File) -> str: pass
@abstractmethod
async def download(self, path: str) -> bytes: pass
class LocalStorageStrategy(StorageStrategy):
async def upload(self, file: File) -> str:
path = f"./uploads/{file.id}"
with open(path, 'wb') as f:
f.write(file.content)
return path
class S3StorageStrategy(StorageStrategy):
async def upload(self, file: File) -> str:
key = f"files/{file.id}"
await self.s3_client.put_object(key, file.content)
return f"s3://{self.bucket}/{key}"

5. Observer Pattern (Agent Telemetry)

Agent execution metrics published to listeners:

python
1234567891011121314151617181920
class AgentExecutor:
def __init__(self):
self.observers = []
def attach_observer(self, observer):
self.observers.append(observer)
async def execute_tool(self, tool):
start = time.time()
result = await tool.execute()
# Notify all observers
for observer in self.observers:
await observer.on_tool_executed({
"tool_name": tool.name,
"duration_ms": (time.time() - start) * 1000,
"success": result.success
})
return result

6. Circuit Breaker (Service Resilience)

Prevent cascading failures when external services are down:

python
1234567891011121314151617181920
class CircuitBreaker:
def __init__(self, failure_threshold=5, timeout=60):
self.failure_count = 0
self.failure_threshold = failure_threshold
self.timeout = timeout
self.last_failure_time = None
self.state = "CLOSED" # CLOSED, OPEN, HALF_OPEN
async def call(self, func, *args, **kwargs):
if self.state == "OPEN":
if time.time() - self.last_failure_time > self.timeout:
self.state = "HALF_OPEN"
else:
raise CircuitBreakerOpenError()
try:
result = await func(*args, **kwargs)
self.failure_count = 0
self.state = "CLOSED"
return result

Scalability & Future Roadmap

Horizontal Scaling Architecture

Load Balancer (AWS ALB / GCP Load Balancer)
        ↓ ↓ ↓
    ┌───┴─┴─┴───┐
    │ Kubernetes │
    │ Cluster    │
    └───┬─┬─┬───┘
        ↓ ↓ ↓
  ┌────────────────────┐
  │ API Pod Replicas   │
  │ (autoscale 2-100)  │
  │ - Each runs        │
  │   FastAPI + 4      │
  │   Gunicorn workers │
  └────────────────────┘
        ↓
  ┌────────────────────┐
  │ Shared Services    │
  │ - PostgreSQL (RDS) │
  │ - Redis (ElastiC) │
  │ - Qdrant (managed) │
  │ - S3 (object store)│
  └────────────────────┘
        ↓
  ┌────────────────────┐
  │ Worker Pod Replicas│
  │ (autoscale 1-50)   │
  │ - Celery workers   │
  │ - Background tasks │
  └────────────────────┘

Future Enhancements

1. Kubernetes Migration

  • Use Helm charts for deployment
  • Auto-scaling based on request metrics
  • Zero-downtime deployments with rolling updates

2. Real-Time Collaboration

  • WebSocket support for simultaneous session editing
  • Operational transformation (OT) for conflict resolution
  • Live cursor positions + presence awareness

3. Advanced RAG System

  • Finer-grained document chunking strategies
  • Hybrid search (keyword + semantic)
  • Re-ranking with cross-encoders
  • Query expansion and reformulation

4. Extended LLM Features

  • Vision models for image analysis
  • Audio transcription integration
  • Multimodal embeddings
  • Fine-tuning on user data (optional)

5. Enterprise Features

  • SSO / OAuth2 integration
  • Fine-grained RBAC (Role-Based Access Control)
  • Audit logging and compliance reports
  • Data residency and privacy controls

Summary

Lumina 2.0 represents a production-ready, enterprise-grade AI platform that combines:

  • Technical Excellence: Modern async architecture, comprehensive testing, secure sandboxing
  • Innovation: Session branching, file deduplication, permission model, multi-provider routing
  • Scalability: Stateless design, horizontal scaling ready, 1000+ concurrent users supported
  • Security: JWT auth, rate limiting, sandbox isolation, audit trails
  • UX: Real-time streaming, session branching visualization, collaborative features
  • Operations: One-click deployment, comprehensive logging, health checks, graceful degradation

The platform has been designed to solve real user problems while maintaining high engineering standards and a clear path to enterprise adoption.


Document Generated: Public-safe version with generic provider references Total Size: 8,000+ lines Code Examples: 100+ Diagrams: 15+