Refactor the Cortex Hub backend to improve maintainability, scalability, and developer experience. The current implementation suffers from bloated routing files ("Fat Routers") and mixed concerns between the API, business logic, and infrastructure layers.
| File | Lines | Key Issues |
|---|---|---|
app/api/routes/nodes.py |
~1,335 | Mixes CRUD, gRPC dispatch logic, WebSocket streaming, and Provisioning script generation. |
app/api/routes/user.py |
~1,114 | Contains OIDC flows, complex preference masking/inheritance, and provider health verification. |
app/api/routes/sessions.py |
~573 | Carries session state management that should be in a service. |
app/core/services/tool.py |
~477 | Monolithic tool implementation; difficult to add new tools without touching core files. |
app/db/models.py |
18KB | All database entities in a single file; slows down development and increases merge conflicts. |
We have moved towards a Clean Architecture / Domain-Driven approach while maintaining 12-Factor App principles.
Move from db/models.py to a module-based structure:
app/db/models/
__init__.py (Exports all models)user.pynode.pysession.pyaudit.pyExtract logic from routers into dedicated, testable services:
Routers should only:
Move large string constants (Provisioning scripts, READMEs) to:
app/core/templates/provisioning/
bootstrap.py.j2run.sh.j2Refactor tool.py to use a dynamic registry:
app/core/tools/
base.py (Interface defining a tool)registry.py (Auto-loader for tools)definitions/ (Individual tool files like file_system.py, browser.py, etc.)app/db/models.py into app/db/models/*.py.schemas.py if necessary into domain-specific schemas.nodes.py to a templates directory.MeshService. Move _require_node_access and _node_to_user_view into it.AuthService and PreferenceService. Move OIDC callback logic and preference masking to services.SessionService.app/api/routes.