Architecture Overview

CortexPrism is a single-process AI Agent Operating System written in TypeScript/Deno. It exposes a CLI, a REST API + WebSocket server, and a web UI. All state is persisted in SQLite databases using WAL mode via @libsql/client.

High-Level Architecture

┌─────────────────────────────────────────────────────────────────┐
│                         CortexPrism                             │
│                                                                 │
│   CLI (cortex chat / run / serve / ...)                         │
│          │                                                      │
│          ▼                                                      │
│   ┌─────────────────────────────────────────────┐              │
│   │              agent/loop.ts                  │              │
│   │  userMessage → [memory inject] → LLM call   │              │
│   │  → [tool parse] → [validator] → [execute]   │              │
│   │  → [re-prompt loop] → response              │              │
│   │  → [episodic write] → [reflection]          │              │
│   └─────────────────────────────────────────────┘              │
│          │                                                      │
│   ┌──────┼──────────────────────────────────────┐              │
│   │      │         Subsystems                   │              │
│   │  memory/   tools/   sandbox/   security/    │              │
│   │  llm/      server/  scheduler/              │              │
│   └──────────────────────────────────────────────┘             │
│                                                                 │
│   SQLite databases (WAL mode)                                   │
│   cortex.db · memory.db · lens.db · vault.db · sess_*.db       │
└─────────────────────────────────────────────────────────────────┘

Core Components

ComponentDescription
Agent LoopCore reasoning + tool loop that orchestrates LLM calls, tool execution, memory operations, and reflection
Memory System5-tier memory with hybrid FTS5 keyword + vector embedding retrieval and exponential decay scoring
LLM Layer24 provider implementations with unified LLMProvider interface and CascadeRouter
Tool SystemExtensible tool registry with built-in tools for file I/O, shell, web search, and code execution
Security (Parallax)3-stage policy validation gate with encrypted vault, regex deny rules, and full audit logging
SandboxDocker container sandbox (subprocess fallback) with resource limits and auto-fix loop
Daemon SupervisorBackground process manager for validator, executor, and scheduler daemons with auto-restart
HTTP ServerBuilt-in web server with REST API, WebSocket streaming, and web UI dashboard
SchedulerSQLite-persisted cron job scheduler with retry
Model QuartermasterAdaptive 6-signal model selection engine with reinforcement learning and context fingerprinting
Git WorkspaceAgent-powered git porcelain with auto-commit messages, diff analysis, and branch management
GitHub IntegrationPR, issue, and repository management with agent-powered code review and triage
Pipeline System10-stage hook-based processing pipeline for the agent turn lifecycle (pre/post-assess, pre/post-reason, pre/post-tool, pre/post-reflect, pre/post-output)
Trigger SystemFile watchers, webhooks, and git hooks that launch agent tasks on external events
Workflow EngineDSL-based workflow execution with steps, branches, parallel execution, and approval gates
Communication ChannelsPlugin-based messaging platform integration (Discord, extensible)
Remote AgentsDistributed node execution via WebSocket hub with capability-based security tiers
MCP ServerModel Context Protocol server exposing Cortex tools and agents to MCP clients
Update SystemDual-mode updates (binary/source) with SHA-256 verification and rollback
ObservabilityPrometheus-compatible metrics registry and distributed tracing with OTLP export
Desktop AutomationCross-platform GUI automation (screenshots, clicks, typing, clipboard) for Linux, macOS, Windows
Evaluation FrameworkAgent benchmarking with 8 task categories, pattern scoring, and regression detection
Hub Node SystemDistributed hub with WebSocket-based node registry, capability tiers, and session routing
Lens Audit LogImmutable event log for all system actions — LLM calls, tool execution, policy decisions

Data Flow

  1. User input enters through CLI, Web UI, or API
  2. Agent loop processes input with context from memory
  3. LLM provider generates response via the model router
  4. Tool calls are executed through the Parallax security gate
  5. Results are stored in memory and returned to the user

Core subsystems