cortex
aien-dev/cortex
Brought by drakestapleton
cortex / spec / BUILD-CONTRACT.md
| 1 | # Omega Cortex — shared build contract (owner: CortexOmega) |
| 2 | |
| 3 | Goal: re-implement the Cortex memory server (Rust `cortex-rs` 0.1.0, live binary |
| 4 | `$HOME/.local/bin/cortex-rs` sha256 91ccba94…afe, SQLite 3.45.0 bundled, serde_json 1.0.x, ryu 1.0.x, |
| 5 | axum 0.7, rustc 1.98.1) in seeded Omega, byte-for-byte compatible on the wire. |
| 6 | Reference source: the archived cortex-rs `src/` (db.rs, handlers.rs, models.rs, embeddings.rs, auth.rs); |
| 7 | where the live binary and the source disagree, the binary wins (spec/ORACLE-OBSERVED.md). |
| 8 | Repo: github.com/aien-dev/cortex, Apache-2.0 WITH LLVM-exception. |
| 9 | |
| 10 | ## Toolchain (pinned) |
| 11 | - Compiler: omega-mv rad:z6do9WDBhuSnvopDkgFdFr1yWA62 `seed/out/omc` at the revision and sha256 |
| 12 | written in `PIN`, the only place the pin is written (since 2026-11-11; before that aien-dev/omega-mv |
| 13 | 91b3c932, omc 189ada82). `tools/omc.sh` reads `PIN`, extracts the binary from an omega-mv |
| 14 | repository (`$OMEGA_REPO`, default the local Radicle storage) into build/omc-<sha256> and refuses any |
| 15 | other hash; `tools/pincheck.om` (run by build.sh) fails the build when a tracked file restates the |
| 16 | pin. Every unit of this repository, tools/pageunit included, builds with this one pin. |
| 17 | - Language authority: omega-mv `docs/one_layer/OMEGESE.md` sections 3, 9 (9.4 arrays, 9.9 floats, |
| 18 | 9.12 const, 9.13 OM-2), 10 (host words). Fixtures: omega-mv `seed/host/tests/*.om`, |
| 19 | `seed/omc/tests`. Never guess syntax; compile small probes. |
| 20 | - Compile: `"$OMC" < prog.om > prog 2> prog.err` (rc 0 + empty stderr = accepted; never run output |
| 21 | of a failed compile). Trap = exit 100+code. |
| 22 | - Facts that bite: local names <= 8 chars (function names <= 63); <= 6 params; 16 registers per |
| 23 | function (error 12 => split the function); a `global` can NOT be passed to a user function |
| 24 | (error 19) but host words (read/write/send/recv/pread/...) accept globals; `new` arrays only in a |
| 25 | function's outermost block; every `while` has an implicit 1,000,000-pass cap unless `bound N` |
| 26 | (N <= 999999), and a `for i ulong n` over a computed count traps (exit 104) after 1,000,000 passes |
| 27 | too — for longer loops nest them (e.g. 65,536-element chunks), or count to a literal: a `for` whose |
| 28 | count is a literal (e.g. `for i ulong 4611686018427387904` with a `break`) is not guarded, at both |
| 29 | pins (probe 2026-11-11: 3,000,000 passes; the same loop over a variable count exits 104); |
| 30 | global pool max 16,777,216 elements, 1 GiB total; at the earlier pins (91b3c932, b2ad967e) also at most 4096 |
| 31 | functions and 8192 names per compiled unit (the omc in PIN since 2026-11-11 has one shared arena |
| 32 | instead of those tables); `build.sh` reports the function count; omc input <= 8 MiB. |
| 33 | |
| 34 | ## Program structure |
| 35 | All server sources are concatenated in a fixed order into ONE unit (`build.sh`), so every global |
| 36 | and function is visible everywhere. Cross-module data passes through the callee's documented |
| 37 | input/output globals (caller `put`s into them, then calls). To avoid collisions: |
| 38 | |
| 39 | | owner | files (om/) | global names start with | function names start with | |
| 40 | |---|---|---|---| |
| 41 | | FtsEngine | uni_fts.om, flog.om, fts.om, like.om | `q` | `fts_`, `like_`, `uf_`, `flog` | |
| 42 | | JsonNum | json.om, f64.om, uni_rust.om | `j` | `js_`, `num_`, `ru_` | |
| 43 | | SqliteImport | crc32c.om, sqlite.om, import_main.om | `k` | `crc_`, `sq_`, `imp_` | |
| 44 | | CortexOmega | everything else | any other letter | `cx_`, `st_`, `http_`, `api_`, ... | |
| 45 | |
| 46 | Each module ships `tests/<module>/` with its own `main` harness(es), a runner script, and a |
| 47 | differential check against the stated oracle, plus at least one negative control (a mutant or |
| 48 | corrupted expected value that the check must FAIL). Evidence logs go to `evidence/<module>/`. |
| 49 | No `main` inside om/*.om library files except import_main.om. |
| 50 | |
| 51 | ## Log file format (storage; SqliteImport writes it, CortexOmega reads+writes it) |
| 52 | File = 8-byte header `CXLOG001` then records. Integers little-endian. |
| 53 | Record = u32 payload_len, u32 crc32c(payload) (Castagnoli, reflected poly 0x82F63B78, init/xorout |
| 54 | 0xFFFFFFFF; check value of "123456789" = 0xE3069283), then payload. |
| 55 | Payload = u8 op, then fields. Field kinds: `str` = u32 len + bytes, len 0xFFFFFFFF = SQL NULL; |
| 56 | `u64`/`i64` = 8 bytes; `f64` = 8 bytes IEEE bits; `u8` = 1 byte; `blob` = like str (NULL allowed). |
| 57 | Ops (field order is normative): |
| 58 | - 1 SPACE: u64 rowid, str id, str slug, str created_at |
| 59 | - 2 ENTITY (full row image; replay = put by id): u64 rowid, u64 ftsrow (0 = no FTS row), |
| 60 | str id, str space_id, str space_slug, str entity_type, str canonical_name, str content, |
| 61 | str aliases_json, str metadata_json, f64 confidence, i64 revision, u8 retracted, |
| 62 | str valid_from, str valid_to, str created_at, blob embedding |
| 63 | - 3 ENTITY_RETRACT: str id (retracted=1, FTS row removed) |
| 64 | - 4 CLAIM: u64 rowid, str id, str space_id, str space_slug, str subject_entity_id, str predicate, |
| 65 | str object_entity_id, str literal_value_json, f64 confidence, str metadata_json, u8 retracted, |
| 66 | str created_at |
| 67 | - 5 CLAIM_RETRACT: str id |
| 68 | - 6 ENTITY_DELETE: str id (hard delete; compaction removes all earlier records of it) |
| 69 | - 7 SPACE_DELETE: str slug |
| 70 | REAL columns stored on disk as integers by SQLite must be converted to f64; TEXT is copied as raw |
| 71 | bytes; `ftsrow` is the rowid in `entities_fts_content` whose c0 equals the entity id. |
| 72 | A torn or crc-failing tail record ends replay (the server truncates it). |