Skip to main content

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:

FeatureDescription
admin_pluginsUser plugin management and trust control
backupDatabase backup, restore, and scheduled snapshots
chatsConversations and AI chat
countsStatistics and counting
dashboardAggregated cross-feature read projections for the UI dashboard
databasesMulti-database management
diagnosticsDiagnostic ZIP export with system info, logs, and stats
edgesGraph edge CRUD
editionDatabase edition and run mode management
exportCCX package export/import
graphGraph operations
graph_snapshotPoint-in-time graph snapshots for analytics
healthSystem health checks and subsystem diagnostics
lexiconLexicon Hub integration
llmLLM provider management
local_authnginx-gated single-user authentication
logsContainer log viewing and service status
mcpModel Context Protocol (MCP) server (Streamable HTTP transport)
nodesGraph node CRUD
pauseSource and system processing pause/resume
qualityQuality scoring
queueJob queue management
searchSearch operations
settingsConfiguration management
sourcesDocument processing
templatesNode/edge template management
toolsTool registry
triggersEvent triggers
upgradeIn-place version upgrade orchestration
workflowsWorkflow 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 session directly

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