Cortex
Cortex
Section titled “Cortex”Overview
Section titled “Overview”Crypto market intelligence daemon. Runs as a headless Node.js process that ingests prices and news on cron loops, feeds them through Cairn’s memory pipeline, and generates LLM-grounded trading signals. Stores everything in its own SQLite database.
How it works
Section titled “How it works”Boot sequence (apps/cortex/src/main.ts)
Section titled “Boot sequence (apps/cortex/src/main.ts)”- Run DB migrations
- Create Kysely DB + MemoryManager (Cairn)
- Seed tracked tokens from CoinGecko (fetch metadata)
- If
--backfillflag: run historical backfill, then exit (or continue with--daemon) - Fetch initial prices
- Start cron loop daemon
Pipeline loop (apps/cortex/src/pipeline/loop.ts)
Section titled “Pipeline loop (apps/cortex/src/pipeline/loop.ts)”Three Croner jobs run on configurable intervals:
- Price ingestion (default: 5min) — Fetches current prices from CoinGecko, stores snapshots, composes a price message for Cairn, runs observer -> promoter -> reflector pipeline
- News ingestion (default: 15min) — Fetches news from CryptoPanic + CryptoCompare RSS, deduplicates by external_id, composes news message for Cairn pipeline
- Signal generation (default: 1hr) — Runs
analyzeAllTokens()for each tracked token
A fourth job polls a command queue every 10s. Optic can insert commands (e.g. “analyze”) which Cortex picks up and executes.
Signal analyzer (apps/cortex/src/pipeline/analyzer.ts)
Section titled “Signal analyzer (apps/cortex/src/pipeline/analyzer.ts)”For each token, generates two signals: short-term (24h) and long-term (4 weeks).
- Generate a recall query via LLM (or static fallback)
- Hybrid memory recall: FTS5 + embedding similarity (15 memories)
- Graph context: search nodes by token name, traverse 2 hops, fetch linked memories
- Compose prompt with price data, memories, graph context
- LLM generates structured signal:
{ signal: buy|sell|hold, confidence: 0-1, reasoning, key_factors } - Store signal in
signalstable - Store signal reasoning as a Cairn memory (feedback loop)
Data flow
Section titled “Data flow”CoinGecko ─────────> price_snapshots ─────> analyzer ─────> signals ^ |CryptoPanic ────┐ | |CryptoCompare ──┴─> news_items | v | | Synapse reads v | Cairn pipeline | (observe -> promote -> | reflect -> graph) ──────────┘ | memories + graph_nodes + graph_edgesBackfill (apps/cortex/src/pipeline/backfill.ts)
Section titled “Backfill (apps/cortex/src/pipeline/backfill.ts)”Supports --backfill, --backfill-news, --backfill-prices flags with a day count. Historical data is fetched and run through the same Cairn pipeline, building up the memory/graph substrate before live operation.
Key files
Section titled “Key files”| File | Role |
|---|---|
src/main.ts | Entry point, CLI args, boot |
src/env.ts | Zod-validated env config |
src/pipeline/loop.ts | Croner jobs, price/news composition |
src/pipeline/analyzer.ts | LLM signal generation with memory recall |
src/pipeline/prompts.ts | Short/long signal prompt templates |
src/pipeline/backfill.ts | Historical data backfill |
src/ingest/prices.ts | CoinGecko API |
src/ingest/news.ts | CryptoPanic + CryptoCompare RSS |
src/db/schema.ts | tracked_tokens, price_snapshots, news_items, signals, commands |
src/db/queries.ts | All DB operations |
Database tables
Section titled “Database tables”tracked_tokens— Token metadata (id, symbol, name, active flag)price_snapshots— Time-series price data (price, market_cap, volume, change_24h/7d)news_items— Deduplicated news articles (external_id, title, url, source, tokens_mentioned)signals— Generated trading signals (token_id, signal_type, confidence, reasoning, timeframe)commands— Queue for inter-app commands (Optic -> Cortex)- Plus Cairn tables: memories, observations, graph_nodes, graph_edges, conversations, messages
Integration points
Section titled “Integration points”- Cairn (
@repo/cairn) — Memory pipeline for price + news data. Same observe/reflect/promote/graph flow used by Construct. - Synapse — Reads
signalstable from Cortex’s DB via CortexReader - Optic — Reads price_snapshots, news_items, signals, graph_* tables directly via rusqlite. Can insert commands.
- Deck — Can browse Cortex’s memory graph if pointed at its DB