Intelligent edge runtime — auth, routing, scheduling, self-healing, and the Cortex Protocol.

@codmir/cortex

Cortex is the intelligent edge runtime that unifies authentication, AI model routing, adaptive scheduling, self-healing process management, and a structured protocol for multi-agent coordination. It gives your application a single orchestration layer that authenticates users, routes tasks to the optimal AI model, learns from execution history, and automatically restarts failed processes — while providing a file-based protocol for thousands of agents to reason together without stepping on each other.

Install

npm install @codmir/cortex

Quick Start

import { Cortex } from '@codmir/cortex';

const cortex = new Cortex({
  auth: {
    jwtSecret: process.env.JWT_SECRET!,
    jwtIssuer: 'my-app',
    accessTokenTtl: 900,
    refreshTokenTtl: 30 * 86400,
  },
  models: [
    {
      id: 'claude-sonnet',
      provider: 'anthropic',
      tier: 'standard',
      costPer1kInput: 0.003,
      costPer1kOutput: 0.015,
      avgLatencyMs: 1200,
      qualityScore: 90,
      capabilities: ['code', 'reasoning', 'analysis'],
      maxTokens: 200_000,
      healthy: true,
      circuitOpen: false,
    },
  ],
  threat: { maxFailedAttempts: 5 },
  router: { defaultStrategy: 'balanced' },
  scheduler: { maxConcurrency: 32 },
  healer: { checkIntervalMs: 10_000 },
  mesh: { maxProcesses: 64 },
});

// Authenticate a user
const session = await cortex.authenticate(token, {
  userId: 'user_123',
  ip: '192.168.1.1',
});

// Route and execute a task
const result = await cortex.executeTask({
  id: 'task_1',
  type: 'code-review',
  input: { files: ['src/index.ts'] },
});

// Listen for events
cortex.on((event) => {
  console.log(`[${event.type}]`, event.payload);
});

// Clean up
cortex.destroy();

Two Layers

Cortex is organized into two layers: Intelligence for runtime orchestration and Protocol for persistent, file-based agent coordination.

Intelligence Layer

Runtime services that handle auth, routing, scheduling, and healing.

ClassPurpose
CortexUnified orchestrator combining all intelligence services
ThreatDetectorReal-time threat assessment for auth events
SmartRouterRoute tasks to the optimal AI model
AdaptiveSchedulerEMA-based learning for model selection and concurrency
SelfHealerHeartbeat monitoring with automatic restart and escalation

Protocol Layer

File-based persistence in a .cortex/ directory. Agents read and write structured reasoning so thousands of agents can work as one mind.

ClassPurpose
CortexIORead and write .cortex/ directory files
DNAManagerEncode project intelligence — stack, architecture, constraints, patterns
TimelineAppend-only reasoning ledger for decisions, failures, and learnings
SwarmProtocolMulti-agent coordination — recruit, assign, lock, merge

The protocol layer is documented in detail on the Cortex Protocol page.