cortex
aien-dev/cortex
Brought by drakestapleton
cortex / README.md
| 1 | # Cortex in Omega |
| 2 | |
| 3 | A re-implementation of the Cortex memory server (cortex-rs 0.1.0) in seeded Omega. It answers the |
| 4 | same HTTP API with the same bytes (spec/CORTEX-API.md) and keeps its data in an append-only log |
| 5 | instead of SQLite. The server and the importer are single static aarch64 binaries built by the |
| 6 | pinned Omega compiler; nothing else runs in production (DEPENDENCIES.md). |
| 7 | |
| 8 | ## Status |
| 9 | |
| 10 | - **Implemented and exercised on a host (Linux aarch64).** All local gates pass on a copy of a real |
| 11 | Cortex database (6,327 entities): wire parity 4642/4642 records identical except masked timing |
| 12 | fields (`elapsedMs`, timestamps, runtime string; 3726 masked values, 48 renamed UUIDs; |
| 13 | evidence/parity), the negative-control mutant is caught (172 failing records), and every |
| 14 | acknowledged write survived kill -9 and restart in the load test (evidence/load). |
| 15 | - **Not deployed.** No running Cortex uses it yet. spec/CUTOVER.md is a draft plan, not executed. |
| 16 | - No CI: the gates below are run by hand (`check.sh`). The evidence files name the exact binaries |
| 17 | and inputs they were produced with. |
| 18 | - **Company Brain (Omega extension, 2026-11-11): implemented and exercised on a host, not deployed.** |
| 19 | Dated, owned, append-only entries with supersede links, read before (ranked live entries with |
| 20 | their citations), write after, contract and party records with a human confirmation gate and an |
| 21 | expiry/renewal alert query, and a plain-file export/import. It uses the same log, FTS index and |
| 22 | server as the rest (spec/CORTEX-API.md "Company Brain"; gates `brain`, `brainmut`, `brainload`). |
| 23 | No language model is involved: an answer is the ranked, cited entries. |
| 24 | - Linux aarch64 only (the compiler emits aarch64 ELF). |
| 25 | - Limits: up to 262,144 FTS documents, 16 Mi index tokens and 64 MiB of short strings (four |
| 26 | 16 MiB pools; about 305 bytes per entity on real data). Connections are served concurrently: a |
| 27 | pool of long-lived handler processes (at most `S_MAXH`) forwards each whole request to the single |
| 28 | state process, so a slow client holds only its own handler (evidence/conc). A handler keeps the |
| 29 | memory its relay buffers reached (up to about 24 MiB for the largest request and answer it |
| 30 | passed) until it exits; at least two stay alive, extra ones leave after 10 s without a |
| 31 | connection. Globals take 992 of the compiler's 1024 MiB (the FTS term lists hold 128 MiB of it). |
| 32 | - Speed (evidence/brain/bench, `tests/brain/bench.sh`, the stats in evidence/brain/stats.txt): the |
| 33 | FTS index is kept incremental by every write (term lists, per-term row counts, per-space row |
| 34 | lists; `ftsIndexMismatches` in `/brain/usage` compares it with a rebuild), so a query reads only |
| 35 | the rows that can match in its own space instead of scanning the whole store, and answers are |
| 36 | unchanged (fts and parity gates). On a 111,000-entity store, Omega's ranked search, plain search |
| 37 | with the substring fallback and read before are faster than the cortex-rs 0.1.0 oracle on the |
| 38 | same store and host at 1,000, 10,000 and 100,000 entities in the space (the oracle has no read |
| 39 | before; its search is the comparison). What is left grows with the rows that match: read before |
| 40 | in a space where every entry shares words with the task ranks every entry, and the substring |
| 41 | fallback reads every entry of the space, as SQLite does. |
| 42 | |
| 43 | ## Build |
| 44 | |
| 45 | ``` |
| 46 | sh build.sh # omc from the local Radicle storage of omega-mv |
| 47 | OMEGA_REPO=/path/to/omega-mv sh build.sh # or from a clone of rad:z6do9WDBhuSnvopDkgFdFr1yWA62 |
| 48 | ``` |
| 49 | |
| 50 | The compiler pin is written in one place, `PIN` (`omega-mv <revision>`, `omc-sha256 <sha256>`; the |
| 51 | one pin of this repository since 2026-11-11, before that 91b3c932). `tools/omc.sh` reads it, extracts |
| 52 | `seed/out/omc` at that revision into `build/omc-<sha256>` and refuses any other sha256; `build.sh` |
| 53 | first compiles `tools/pincheck.om` and fails when any tracked file restates the pin (records under |
| 54 | `evidence/` excepted), then writes `build/cortex`, `build/cortex-import` and `build/SHA256SUMS`, |
| 55 | whose compiler line is read from `PIN`. A compile is accepted only with exit 0 and empty stderr. |
| 56 | To move the pin, edit `PIN` and run `check.sh`. |
| 57 | |
| 58 | ## Import a cortex-rs database |
| 59 | |
| 60 | ``` |
| 61 | sqlite3 'file:/path/to/cortex.db?mode=ro' '.backup /tmp/cortex-snapshot.db' |
| 62 | build/cortex-import /tmp/cortex-snapshot.db /path/to/cortex.log |
| 63 | ``` |
| 64 | |
| 65 | The importer reads the SQLite file (and its WAL) read-only, writes the log to a temporary file, |
| 66 | syncs it and renames it into place; it never overwrites an existing log. |
| 67 | |
| 68 | ## Run |
| 69 | |
| 70 | ``` |
| 71 | CORTEX_TOKEN=... build/cortex --port PORT --log /path/to/cortex.log --encoder-url URL |
| 72 | ``` |
| 73 | |
| 74 | Options: `--host` (default 127.0.0.1), `--port`, `--log`, `--encoder-url`, `--tokens FILE` |
| 75 | (space-scoped extra tokens). Without `CORTEX_TOKEN` the token is read from the same per-user file |
| 76 | as cortex-rs (spec/CORTEX-API.md). `GET /health` answers without a token. |
| 77 | |
| 78 | Company Brain routes are under `/api/cortex/brain/` (spec/CORTEX-API.md). The plain-file export runs |
| 79 | beside the server and only reads the log: |
| 80 | |
| 81 | ``` |
| 82 | build/cortex --log /path/to/cortex.log --brain-export DIR |
| 83 | ``` |
| 84 | |
| 85 | ## Local gates |
| 86 | |
| 87 | ``` |
| 88 | sh check.sh /path/to/cortex-snapshot.db |
| 89 | ``` |
| 90 | |
| 91 | runs, one at a time and at lowest CPU/IO priority: |
| 92 | |
| 93 | | step | script | what it proves | |
| 94 | |---|---|---| |
| 95 | | build | `build.sh` | both binaries compile with the omc named in `PIN`; `tools/pincheck.om` finds no tracked file restating the pin | |
| 96 | | pin | `tests/pin/run.sh` | the tree passes pincheck; restating the revision or the omc sha256 is refused, records under `evidence/` and other hashes are not judged, a malformed `PIN` and a binary with another sha256 are refused; with a fixture `PIN` whose revision and sha256 start with 8 digits, 8-, 12- and 16-character, upper-case and full restatements are refused while dates, counts and near misses pass, and five mutants of the prefix comparison are each caught | |
| 97 | | pageunit | `tests/pageunit/run.sh` | `tools/pageunit/enc.om` units print their page back byte for byte up to 200000 lines; refusals, old compiler, mutant and wrong compiler caught; arena alarm | |
| 98 | | json | `tests/json/run.sh` | JSON parsing, error texts and float/number text match serde_json/ryu/zmij bit for bit; mutants caught | |
| 99 | | fts | `tests/fts/run.sh` | tokenizer, bm25, `log` and LIKE match SQLite 3.45.0 and glibc, the searches under each plan of the incremental index (space scan, term lists, every row, without the term-count loop) and after deletes and re-inserts; the index equals a rebuild; negative controls caught (12, four of them skipped index updates) | |
| 100 | | import | `tests/import/run.sh` | the SQLite reader and importer match sqlite3 on real and synthetic files, WAL included; corruption detected | |
| 101 | | parity | `tests/parity/run.sh` | the wire gate against the cortex-rs binary (spec/PARITY.md) | |
| 102 | | mutant | `tests/parity/mutant.sh` | the gate fails when one score constant changes | |
| 103 | | load | `tests/load/run.sh` | read-mix latency of both servers and the kill -9 durability check | |
| 104 | | big | `tests/big/run.sh` | a snapshot padded past 16 MiB of short strings loads and reads back | |
| 105 | | conc | `tests/conc/run.sh` | 16 concurrent clients plus slow clients, against the latency gate; after kill -9 of the state process while slow clients hold handlers, a new server takes the port within 3 s | |
| 106 | | claims | `tests/claims/gen.sh` via the parity runner | claim create/upsert/retract parity | |
| 107 | | brain | `tests/brain/run.sh` | Company Brain answers equal the expected answers (parity rule) in three phases: entries, supersede, read before with citations, refusals, the append-only guard, contract records, the confirmation gate, alert window boundaries; export, unchanged import and re-export give one tree hash; a hand-corrected file imports as a superseding entry; the live supersede index and the incremental FTS index each equal a full rebuild. `tests/brain/expected-*.rec` and `expected.sha` are written only by `BRAIN_RECORD=1 sh tests/brain/run.sh` (review the diff before committing), never edited | |
| 108 | | brainmut | `tests/brain/mutant.sh` | thirteen one-line mutants (om/brain.om: supersede filter, citations, confirmation gate, date kinds, import idempotence, both window ends, guard, live index update; om/fts.om: a write that does not count its terms, link them or count its row in its space) are each caught by the brain gate | |
| 109 | | brainload | `tests/brain/load.sh` | write-after and read-before latency on the load fixture, the incremental FTS index equal to a rebuild after the import and 2000 writes, the capacity alarm (fails when a store capacity is 50 percent full; its control must fire at 1 percent) and kill -9 during Brain writes loses no acknowledged entry | |
| 110 | |
| 111 | `tests/brain/bench.sh` (not a check.sh step: it writes 111,000 entities through the oracle) measures |
| 112 | read before and ranked search of two Omega binaries and the oracle on one store and checks that |
| 113 | their answers agree; `tests/brain/scale.sh` measures read before of two binaries alone. |
| 114 | |
| 115 | The parity and load steps need the cortex-rs 0.1.0 binary on `PATH` (or `CORTEX_ORACLE`) and use ports |
| 116 | 18180 and 18190 (set `PARITY_ORACLE_PORT` and `PARITY_OMEGA_PORT` when those are taken); the brain |
| 117 | steps use ports 18232 to 18234. The json and fts steps need their offline oracles (DEPENDENCIES.md). Logs and fresh |
| 118 | evidence go to `build/check/`; the committed `evidence/` is not touched. Exit 0 only when every step |
| 119 | passes. |
| 120 | |
| 121 | ## Tools |
| 122 | |
| 123 | `tools/pageunit/enc.om` stores page text as native Omega: it reads a page on stdin and writes an |
| 124 | om2 unit on stdout with the page as `text tN "..."` records (one per line, a long line split every |
| 125 | 4096 bytes; tab, quote, backslash and newline escaped) and a `main` that writes the same bytes back, |
| 126 | so compiling and running the unit reproduces the page exactly. Build and use it with |
| 127 | `tools/pageunit/omc.sh`, which runs `tools/omc.sh` (the compiler named in `PIN`): |
| 128 | `OMC=$(sh tools/pageunit/omc.sh); "$OMC" < tools/pageunit/enc.om > enc; ./enc < page.txt > page.om; |
| 129 | "$OMC" < page.om > page; ./page | cmp - page.txt`. Exit codes: 0 ok, 2 read error, 4 control byte |
| 130 | (below 32 other than tab and newline, or 127), 6 write error. enc streams (one 64 KiB input block, |
| 131 | one 64 KiB output buffer), so it has no size refusal of its own, and on a nonzero exit its output is |
| 132 | incomplete and must be discarded. The 8000-line and 1000000-byte refusals it had until 2026-11-11 are |
| 133 | gone: the lines came from the old compiler pin's fixed tables (8192 texts per unit), the bytes from |
| 134 | enc's own fixed pools. What a page still meets is the memory of the compiler that builds its unit. |
| 135 | Measured 2026-11-11 with the compiler pinned then (a fixed arena of 16777216 8-byte words): a unit |
| 136 | stopped at about 524000 records (1 MiB of two-byte lines), about 226000 lines of 120 bytes (27 MB) or |
| 137 | about 64 MB of long lines. The gate alarms (prints the arena record and fails) when a real page passed |
| 138 | to it (`sh tests/pageunit/run.sh page.txt ...`, or `PAGEUNIT_PAGES=dir` for its `*.txt`, also through |
| 139 | `check.sh`) yields a unit that puts as many words in the compiler's arena as the threshold `ALARMAT` |
| 140 | in `tests/pageunit/gen.om`, or more, measured with omc's usage mode. The threshold is a stated number |
| 141 | (80 percent of that fixed arena), so it also holds with a compiler whose arena grows; the gate prints |
| 142 | it as `alarm-at=` beside every record it judges. |
| 143 | Throughput, measured 2026-11-11 on Linux aarch64 on one shared core (load 14), process start |
| 144 | included: 189 MB/s on a 900000-byte page (the earlier whole-page enc: 199 MB/s in the same run) and |
| 145 | 264 MB/s on a 32 MiB page, which the earlier enc refused. Gate: `sh tests/pageunit/run.sh`. |
| 146 | |
| 147 | ## Layout |
| 148 | |
| 149 | - `om/` server and importer modules (order in `build.sh`; ownership in spec/BUILD-CONTRACT.md) |
| 150 | - `spec/` contracts: API, build, parity rule, observed oracle behaviour, cutover draft |
| 151 | - `tests/` the gates; `evidence/` their committed results |
| 152 | - `tools/` compiler pins and `pageunit/` (page text to an om2 unit, above) |
| 153 | |
| 154 | License: Apache-2.0 WITH LLVM-exception (LICENSE). |
| 155 | |
| 156 | ## Human-Directed Agentic Engineering |
| 157 | |
| 158 | Development follows Human-Directed Agentic Engineering (HDAE): humans define goals, |
| 159 | architecture, constraints, acceptance criteria, and stopping points; agents implement, |
| 160 | execute checks, diagnose failures, and revise within those boundaries. Human review |
| 161 | retains responsibility for claims, acceptance, and release decisions. |
| 162 | |
| 163 | For the documented method and a systems-software case study, see Gregory M. Kurtzer, |
| 164 | [*Ginux: Building an Independent Kernel That Runs Linux Userspace through |
| 165 | Human-Directed Agentic Engineering* (2026)](https://doi.org/10.5281/zenodo.23682548). |
| 166 | This citation describes the development method, not runtime autonomy or an endorsement. |