Cortex (AIngle)

Cortex is an HTTP sidecar binary (aingle-cortex) that provides Mayros with a persistent RDF triple store built on AIngle. It powers semantic memory, the knowledge graph, P2P synchronization, and observability tracing.

Architecture

mermaid
graph LR
    A[Mayros CLI / TUI] -->|HTTP| B[CortexClient]
    B -->|REST API| C[aingle-cortex sidecar]
    C -->|QUIC gossip| D[Remote Cortex nodes]
    C -->|RDF store| E[Local data ~/.mayros/cortex-data/]

Mayros communicates with Cortex exclusively through CortexClient, a TypeScript HTTP client that wraps all REST calls with circuit-breaker resilience.

Auto-start lifecycle

Cortex is managed automatically by Mayros:

1

Health check

On startup, Mayros checks if Cortex is already running on the configured port.

2

Binary detection

If not running and autoStart is enabled, Mayros locates the binary at ~/.mayros/bin/aingle-cortex (or the configured binaryPath).

3

Version check

Compares the binary version against REQUIRED_CORTEX_VERSION (currently 0.4.1). Warns on older versions; blocks startup if strictVersionCheck is enabled.

4

Auto-install

If the binary is missing from ~/.mayros/bin/, Mayros automatically downloads it from GitHub Releases.

5

Spawn

Starts the sidecar process with --db for persistent Sled storage, plus host, port, and P2P flags. A lock file prevents concurrent instances.

6

Health polling

Polls GET /health with exponential backoff (100ms to 2s, max 10s total).

7

Auto-restart

Monitors the process and auto-restarts on crash (up to 3 attempts with exponential backoff).

Graceful shutdown flushes all data (graph + Ineru snapshots) to disk before exiting via SIGTERM/SIGINT.

Seamless binary updates

When Mayros updates the Cortex binary (via mayros update), it coordinates a zero-data-loss flow:

  1. Flush — sends POST /api/v1/flush to persist all in-memory data
  2. Stop — SIGTERM with 10s timeout, then SIGKILL if needed
  3. Replace — new binary is written to ~/.mayros/bin/
  4. Restart — sidecar restarts with the same --db path, data intact

REST API

Cortex exposes a REST API consumed by CortexClient:

EndpointMethodPurpose
/healthGETHealth check with component statuses
/api/v1/triplesPOST/GET/DELETETriple CRUD operations
/api/v1/queryPOSTPattern-based triple queries
/api/v1/eventsPOST/GETTrace event storage and retrieval
/api/v1/auditGETAudit log queries
/api/v1/proofsPOST/GETProof of Logic verification
/api/v1/p2p/statusGETP2P network status
/api/v1/p2p/peersGET/POST/DELETEP2P peer management
/api/v1/sync/deltaPOSTDelta synchronization
/api/v1/flushPOSTFlush graph and Ineru snapshots to disk
/statsGETServer and graph statistics

Data model

All data is stored as RDF triples: subject → predicate → object. Values can be strings, numbers, booleans, or node references.

mayros:agent:main → mayros:memory:convention → "Use ESM imports"
mayros:plan:abc123 → mayros:plan:phase → "explore"
file:src/index.ts → defines:function → "main"

Triples are namespaced with a configurable prefix (default: mayros) to enforce isolation between projects and agents.

Paths

PathPurpose
~/.mayros/bin/aingle-cortexCortex binary (auto-installed if missing)
~/.mayros/cortex-data/Persistent data directory (Sled graph + Ineru snapshots)
~/.mayros/cortex-data/cortex-secrets.jsonAuto-generated JWT secret and admin password (mode 0600)
~/.mayros/cortex-data/.cortex.lockLock file preventing concurrent sidecar instances

Configuration

json5
{
  cortex: {
    host: "127.0.0.1",       // Cortex host
    port: 19090,              // Cortex port
    autoStart: true,          // Auto-spawn sidecar
    dataDir: undefined,       // Data directory (default: ~/.mayros/cortex-data/)
    binaryPath: undefined,    // Override binary location
    authToken: undefined,     // Auth token (or CORTEX_AUTH_TOKEN env)
    requireAuth: false,       // Require authentication
    strictVersionCheck: false, // Block on version mismatch
    p2p: {
      enabled: false,         // Enable native P2P
      port: 19091,            // QUIC gossip port
      seed: undefined,        // Shared secret for peer auth
      manualPeers: [],        // Peer addresses
      mdns: false,            // mDNS discovery
    },
    resilience: {
      timeoutMs: 5000,        // Request timeout
      maxRetries: 2,          // Retry count
      retryDelayMs: 300,      // Base retry delay
      circuitThreshold: 5,    // Failures before circuit opens
      circuitResetMs: 30000,  // Recovery timeout
    },
  },
}

Cortex is optional — Mayros works without it, but semantic memory, P2P sync, knowledge graph, rules engine, and observability tracing all require a running Cortex instance.