agentleFS
CortexPrismCortexPrism/cortex/AGENTS.md

cortex

AGENTS.md187 starsChanged 5 months agoApache-2.0View on GitHub

CortexPrism is a self-hosted, open-source AI agent operating system — an autonomous agent runtime that turns any LLM into a capable digital agent. It provides persistent memory, a rich tool ecosystem, sandboxed code execution, multi-agent orchestration, a full-featured web UI, and enterprise-grade security. CI runs deno fmt --check, deno lint, deno check src/main.ts, deno test --allow-all on ubuntu, macos, and windows. The codebase is organized into 6 coarse packages under packages/ and a composition root in src/:

  • Reads credentials
  • Where files are headingNew AGENTS.md files have more rules about keeping secrets out of the code, and fewer about checking a change actually works.

    Of the AGENTS.md files we read, the 11,773 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.

  • In this repository2 other files it gives its agents.

The file

We split this AGENTS.md into the rules it gives an agent

1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Run: deno task test (sets --allow-all)

    line 229

≈ “Common” is an estimate: at least 1 in 5 of the 16,595 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
AGENTS.md

CortexPrism — AI Agent Operating System

Project Identity

CortexPrism is a self-hosted, open-source AI agent operating system — an autonomous agent runtime that turns any LLM into a capable digital agent. It provides persistent memory, a rich tool ecosystem, sandboxed code execution, multi-agent orchestration, a full-featured web UI, and enterprise-grade security.

  • License: Apache 2.0
  • Version: 0.53.0 (see deno.json)
  • Repository: CortexPrism/cortex on GitHub
  • CI: .github/workflows/ci.yml (runs on push to main)

Tech Stack

LayerTechnology
RuntimeDeno 2.x (TypeScript strict mode)
DatabaselibSQL (SQLite-compatible) via @libsql/client
TestingDeno test runner
CLI framework@cliffy/command
LLM SDKsAnthropic, OpenAI, Google Generative AI, AWS Bedrock
FrontendInline SPA (Tailwind CDN, CodeMirror 6, vanilla JS, 78 modular files)
Package managerDeno import maps (deno.json)

Build & CI Commands

deno task check      # Type-check all files
deno task lint       # Lint all files
deno task fmt        # Format all files (auto-fix)
deno task test       # Run all tests (sets --allow-all)
deno run --allow-all src/main.ts <command>  # Run CLI

CI runs deno fmt --check, deno lint, deno check src/main.ts, deno test --allow-all on ubuntu, macos, and windows.

Package Structure (v0.48.6+)

The codebase is organized into 6 coarse packages under packages/ and a composition root in src/:

packages/
├── core/           — @cortex/core
│   ├── contracts/  — pure interface definitions (ICortexConfig, IDbClient, etc.)
│   └── src/        — config, db, i18n, utils, plugins
├── gate/           — @cortex/gate
│   ├── contracts/
│   └── src/        — security (policy, vault, supervisor), sandbox, vfs
├── ai/             — @cortex/ai
│   ├── contracts/
│   └── src/        — agent, tools, memory, llm, pipeline, skills
├── server/         — @cortex/server
│   ├── contracts/
│   └── src/        — server, hub, channels, a2a, mcp, voice, workspace, codegraph
├── infra/          — @cortex/infra
│   ├── contracts/
│   └── src/        — processes, services, scheduler, ipc, triggers, workflow, observability
└── cli/            — @cortex/cli
    ├── contracts/
    └── src/        — cli commands, tui

src/
├── agent/          — agent loop orchestrator (81 lines)
│   stages/         — 7 pipeline stages (setup, history, assessment, prompt-builder, model-selector, llm-stream, tool-executor)
│   post/           — post-turn modules (response, background, cleanup)
│   helpers/        — shared helpers (preferences, strip-tool-calls, nanoid)
│   pipeline/       — pipeline context
├── server/
│   server.ts       — HTTP server entry (composition root for server)
│   new-router.ts   — route dispatcher (replaced 6,075-line monolith)
│   routes/         — 69 route modules (one per API area)
│   ui/
│   │   mod.ts      — UI assembler (concatenates JS + HTML)
│   │   js/         — 29 concatenated JS modules
│   │   pages/      — 46 page HTML templates
│   │   shared/     — shared utilities
│   │   css.ts      — embedded CSS
│   │   shell.ts    — sidebar/layout HTML
│   │   providers.ts
├── main.ts         — CLI entry point (composition root)
└── tests/          — 30 test files (flat structure)

Dependency Graph

@cortex/core ← @cortex/gate ← @cortex/ai ← @cortex/server ← @cortex/cli
                                    ↖               ↗
                              @cortex/infra
  • core has zero internal dependencies
  • gate depends only on core
  • ai depends on core + gate
  • server depends on core + ai
  • infra depends on core + ai
  • cli is the composition root, depends on all

Contracts

Each package defines pure TypeScript interfaces in packages/<name>/contracts/. These have zero runtime dependencies and define the boundaries between packages. Common contracts:

  • ICortexConfig, IProviderConfig (core/contracts/config.ts)
  • ITool, IToolRegistry, IToolContext (ai/contracts/tools.ts)
  • IAgentLoop, IAgentTurnOptions (ai/contracts/agent.ts)
  • ILLMProvider, ILLMRouter (ai/contracts/llm.ts)
  • IMemoryStore, IEpisodicStore (ai/contracts/memory.ts)
  • IPipelineHook, IPipelineManager (ai/contracts/pipeline.ts)
  • IPolicyEngine, IVault (gate/contracts/policy.ts)
  • ISandboxProvider (gate/contracts/sandbox.ts)
  • IRouteHandler, IRouteTable (server/contracts/router.ts)
  • IWSHub, IWSHandler (server/contracts/websocket.ts)
  • IScheduler, IJobRow (infra/contracts/scheduler.ts)
  • IServiceManager (infra/contracts/services.ts)
  • ICommand, ICommandRegistry (cli/contracts/commands.ts)

Key Architectural Patterns

Agent Loop (src/agent/loop.ts)

The 81-line agentTurn() orchestrator calls pipeline stages sequentially:

Setup → History → Assessment → Prompt Builder → Model Selector → LLM Stream → Tool Executor
  → Post Response → Background (fire-and-forget) → Cleanup

Tool execution runs in a for loop within llm-stream.ts (up to DEFAULT_MAX_TOOL_ROUNDS = 12). Sub-agent dispatch is parallel via Promise.all.

Router (src/server/new-router.ts)

Routes are defined as RouteHandler[] arrays in 69 files under src/server/routes/. Each handler is { method: string; pattern: RegExp; handler: (req, path) => Response }. The dispatcher splits routes into publicRoutes and protectedRoutes, running the auth guard (requireAuth) between them. Route ORDER matters — handlers are tried in registration order via regex matching.

UI Assembly (src/server/ui/mod.ts)

The SPA is assembled by concatenating 29 JS files and 46 HTML page templates into a single <script> block. Global variables (ws, sessionId, currentPage, etc.) are shared across all JS modules since they're concatenated into one scope. The DASHBOARD_JS template literal is injected at a specific position. serveUi(locale) generates the full HTML response with {LOCALE} replacement.

Database

5 SQLite databases in WAL mode:

  • cortex.db — sessions, jobs, policies, services, nodes, workspace, agents, channels, triggers, workflows, projects, users, teams, tokens, federation
  • memory.db — episodic_memory, semantic_memory, memory_graph, reflections, skills, glossary, preferences (scoped per user/team)
  • lens.db — activity audit log (tool calls, LLM calls, policy decisions, approvals)
  • vault.db — AES-256-GCM encrypted credentials (PBKDF2 key derivation)
  • plugins.db — plugin registry

Migrations are in packages/core/src/db/migrations/ (NNN_name.sql format, currently 47 migrations). Register new migrations in packages/core/src/db/migrate.ts.

LLM Provider System

30 providers implemented in packages/ai/src/llm/. Each implements LLMProvider with complete() and stream() methods. The router (llm/router.ts) supports cascade (cheapest-first) and threshold (prompt-scoring) strategies. Model Quartermaster (model-quartermaster/) uses 6-signal prediction for intelligent model selection.

Security

Three-layer Parallax model:

  1. Policy validator — regex allow/deny rules on every tool call
  2. LLM supervisor — fast model (Gemini Flash/GPT-4o Mini) reviews sensitive access with decision caching
  3. Human approval — CLI prompts + Web UI modal with 1-hour TTL grants

Multi-User & Federation (src/server/auth.ts, src/server/identity.ts)

v0.53.0 added multi-user collaboration with PBKDF2 password hashing, team management with join policies, API token authentication (SHA-256 hashed), resource sharing between users, and instance-to-instance federation. The RequestIdentity interface carries user/team/admin context through all API routes. Authorization guards (requireInstanceAdmin, requireTeamAdmin, requireTeamMember, requireResourceOwner) enforce coarse permission checks.

Swarm (packages/infra/src/swarm/)

Distributed agent coordination across multiple Cortex instances using A2A protocol as the wire transport. Nodes register, discover peers, dispatch directives, and aggregate resource usage across the fleet. 5 directive kinds: spawn_agent, execute_task, query_resources, forward_message, sync_state. Remote processes are proxied into the local OsKernel process tree.

Pipeline Hooks

12 pipeline stages hook into the agent loop via IPipelineHook. Hooks can mutate inputs, abort execution, or observe results. Registered via IPipelineManager.registerHook().

Adding Features

TaskLocationRegistration
CLI commandpackages/cli/src/cli/<name>.tspackages/cli/src/cli/registry.ts or src/main.ts
REST endpointsrc/server/routes/<name>.tsAdd to route table in src/server/new-router.ts
DB migrationpackages/core/src/db/migrations/NNN_name.sqlpackages/core/src/db/migrate.ts targets array
LLM providerpackages/ai/src/llm/<name>.tsLLMProvider interface, register in factory
Built-in toolpackages/ai/src/tools/builtin/<name>.tsTool interface, register in packages/ai/src/tools/registry.ts
Pipeline hookpackages/ai/src/pipeline/builtin.tsRegister via IPipelineManager
Agent stagesrc/agent/stages/<name>.tsCall from agentTurn() orchestrator

Code Conventions

  1. TypeScript strict — no implicit any, no ! assertions without justification
  2. Async-first — async/await over raw Promise chains
  3. Fire-and-forget — background tasks use .catch(() => {}), never block response
  4. Error handling — catch at boundaries, return structured error results
  5. No hardcoded secrets — use vault via CORTEX_VAULT_KEY env var
  6. No hardcoded paths — use PATHS from packages/core/src/config/paths.ts
  7. SQL — use the libsql Db wrapper from packages/core/src/db/client.ts
  8. Subprocess — use Deno.Command, never Deno.run
  9. Named exports — avoid default exports except for Deno task entry points
  10. Conventional commits — feat:, fix:, docs:, refactor:, chore:, test:, perf:

Testing

  • Tests in tests/ directory (flat structure, 30 files)
  • Run: deno task test (sets --allow-all)
  • Type-check: deno task check
  • UI integrity tests: deno test --allow-read tests/ui_js_integrity_test.ts (validates generated JS has no syntax errors or missing functions)
  • Test naming: Deno.test('descriptive name', async () => { ... })

Configuration

  • Config file: ~/.cortex/config.json (JSON)
  • Environment variables: CORTEX_DATA_DIR, CORTEX_CONFIG_DIR, CORTEX_VAULT_KEY, CORTEX_LOG_LEVEL, GITHUB_TOKEN
  • Provider API keys can be in config or environment variables
  • Config schema: packages/core/contracts/config.ts (ICortexConfig)

Key Files to Know

FilePurposeLines
src/main.tsCLI composition root~103
src/agent/loop.tsAgent turn orchestrator~81
src/server/server.tsHTTP server entry~300
src/server/new-router.tsAPI route dispatcher~200
src/server/ui/mod.tsUI assembler~220
packages/ai/src/tools/registry.tsTool registry~319
packages/ai/src/llm/router.tsModel router~400
packages/gate/src/security/policy.tsPolicy engine~500
packages/core/src/db/client.tsDatabase client~200
packages/core/src/config/config.tsConfig loading~500
src/server/auth.tsAuth + identity extraction~400
packages/infra/src/swarm/coordinator.tsSwarm coordinator~300
deno.jsonWorkspace config + import map~73

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.