Matrix OSMatrix OS

Architecture

How the kernel, gateway, dispatcher, agents, and shell fit together.

System Architecture

Gateway Endpoints

EndpointPurpose
/wsMain WebSocket (chat, file watcher events)
/ws/terminalPTY WebSocket (xterm.js to node-pty)
/api/messageREST endpoint for kernel dispatch
/api/channels/statusConnected channel status
/files/*Static file serving from ~/matrixos/
/modules/*Reverse proxy to module ports (3100-3999)
/api/themeCurrent theme JSON
/api/cronCron job list
/api/tasksTask list + creation
/healthHealth check

Packages

packages/kernel/

The AI kernel. Key files:

FilePurpose
spawn.tsspawnKernel() -- entry point, calls query() and resume
options.tskernelOptions() -- assembles system prompt, IPC, agents, hooks
prompt.tsbuildSystemPrompt() -- constructs the system prompt with SOUL, skills, context
ipc-server.tscreateIpcServer() -- 26 IPC tools via createSdkMcpServer()
agents.tsCore agent definitions, parseFrontmatter(), loadCustomAgents()
hooks.tsAll hook implementations (safety, snapshots, permissions, healing)
soul.tsSOUL identity loading (loadSoul(), loadIdentity(), loadUser())
schema.tsDrizzle ORM schema (tasks, messages, memories)
db.tsDatabase setup (WAL mode, migrations)

packages/gateway/

Hono HTTP/WebSocket gateway. Key files:

FilePurpose
server.tscreateGateway() -- initializes all services, routes, WebSocket
dispatcher.tscreateDispatcher() -- serial/concurrent queue, kernel invocation
watcher.tsFile watcher (chokidar) -- broadcasts file:change events
channels/Channel adapters (Telegram, Discord, Slack, WhatsApp)
cron.tscreateCronService() -- scheduled task execution
heartbeat.tscreateHeartbeatRunner() -- periodic kernel health checks
conversations.tscreateConversationStore() -- multi-session persistence

packages/platform/

Multi-tenant cloud orchestrator (Hono :9000). Manages container provisioning via dockerode, Drizzle for tenant data, Clerk auth integration, and Cloudflare Tunnel configuration.

shell/

Next.js 16 web desktop. The primary visual shell. Renders the desktop with windows, dock, chat panel, terminal, settings, and Mission Control. Uses WebSocket for real-time streaming and file change events.

Request Flow

Here's the complete path of a user message from input to response:

  1. User sends a message via WebSocket (/ws), REST (/api/message), or a channel adapter
  2. Gateway receives it in createGateway() and passes it to dispatcher.dispatch()
  3. Dispatcher queues it in the serial FIFO queue (or concurrent queue if configured)
  4. Task is created in SQLite via createTask(), then claimed via claimTask()
  5. spawnKernel() is called with the message and KernelConfig (db, homePath)
  6. System prompt is built by buildSystemPrompt():
    • SOUL identity (soul.md, identity.md, user.md)
    • Skills table of contents (buildSkillsToc())
    • Active processes (from tasks table)
    • Available agents and their descriptions
  7. Agent SDK query() is called with the system prompt and available tools
  8. Tokens stream back as KernelEvents through the gateway to the client as ServerMessages
  9. If the agent uses a tool, a tool_use block is returned:
    • The kernel executes the IPC tool via createSdkMcpServer()
    • PreToolUse hooks run first (safety guards, permission checks)
    • The tool executes (file ops, task management, skill loading, etc.)
    • PostToolUse hooks run after (git snapshots, state updates, shell notifications)
    • The result is returned to the kernel
  10. resume() is called with tool results to continue the conversation
  11. The final response streams back to the user
  12. Task is completed via completeTask() (or failTask() on error)

Serial by default

The dispatcher processes one message at a time by default. This prevents file system corruption from concurrent writes. Enable concurrent dispatch with maxConcurrency in the dispatcher config, where each process registers in the tasks table.

The Computer Metaphor

Traditional OSMatrix OS
CPUClaude Opus 4.6
RAMContext window (1M tokens)
KernelMain agent + spawnKernel()
Processes5 sub-agents (Builder, Researcher, Deployer, Healer, Evolver)
SyscallsRead, Write, Edit, Bash (file tools)
Disk~/matrixos/ (apps, data, system, agents)
DriversMCP servers (IPC tools)
IPCFile coordination + SQLite task queue
ShellWeb desktop, Telegram, CLI (any renderer)
Device driversChannel adapters

How is this guide?

On this page