Cortex — Vertical Slice Architecture
The Cortex package (chaoscypher-cortex) is the processing center — a FastAPI backend organized using Vertical Slice Architecture (VSA).
What is VSA?
Each feature is a self-contained vertical slice with its own models, repository, service, and API layer. Instead of horizontal layers (all controllers in one folder, all services in another), each feature owns its entire stack.
Feature Structure
Each feature follows this structure:
features/{feature}/
├── __init__.py # Barrel exports
├── models.py # Pydantic request/response DTOs
├── repository.py # SQLModel data access
├── service.py # Business logic
└── api.py # REST endpoints + factory DI
Features
Cortex has 30 feature modules:
| Feature | Description |
|---|---|
admin_plugins | User plugin management and trust control |
backup | Database backup, restore, and scheduled snapshots |
chats | Conversations and AI chat |
counts | Statistics and counting |
dashboard | Aggregated cross-feature read projections for the UI dashboard |
databases | Multi-database management |
diagnostics | Diagnostic ZIP export with system info, logs, and stats |
edges | Graph edge CRUD |
edition | Database edition and run mode management |
export | CCX package export/import |
graph | Graph operations |
graph_snapshot | Point-in-time graph snapshots for analytics |
health | System health checks and subsystem diagnostics |
lexicon | Lexicon Hub integration |
llm | LLM provider management |
local_auth | nginx-gated single-user authentication |
logs | Container log viewing and service status |
mcp | Model Context Protocol (MCP) server (Streamable HTTP transport) |
nodes | Graph node CRUD |
pause | Source and system processing pause/resume |
quality | Quality scoring |
queue | Job queue management |
search | Search operations |
settings | Configuration management |
sources | Document processing |
templates | Node/edge template management |
tools | Tool registry |
triggers | Event triggers |
upgrade | In-place version upgrade orchestration |
workflows | Workflow definitions |
Three-Layer Pattern
Repository Layer
- Uses SQLModel entities directly
- Manages database sessions
- No business logic — pure data access
- Returns SQLModel entities (attribute access OK here)
Service Layer
- Receives and returns dicts (not entities)
- Orchestrates business logic
- Calls repositories and Core services
- Never accesses
sessiondirectly
API Layer
- Defines REST endpoints with FastAPI decorators
- Uses Pydantic models for request/response validation
- Contains the factory function for dependency injection
- No business logic
Factory Functions
Each feature has a factory function that wires dependencies:
def get_sources_service(
session: Annotated[Session, Depends(get_session)],
settings: Annotated[Settings, Depends(get_settings)]
) -> SourcesService:
repository = SourcesRepository(session, settings.current_database)
return SourcesService(repository, settings)
Naming convention: get_{feature}_service()
Shared Infrastructure
shared/
├── auth/ # Authentication and permissions
├── config/ # Settings and configuration
├── database/ # Session management, models
├── factories/ # Service factory helpers
├── llm/ # LLM factory and providers
├── operations/ # Shared operation utilities
├── queue/ # Valkey queue client
├── repositories/ # Shared repository factories
└── ... # Plus: adapters, api, constants, kernel, logging, reset, utils