synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / docs / J1_OMEGA_CODEC.md
| 1 | # ADR 0003 step J1: Omega port of the binary codec (cross-check corpus) |
| 2 | |
| 3 | Issue #53. ADR 0003 section 9, row 33 (J1), needs A1. The Rust codec |
| 4 | (`crates/synapse-store/src/codec.rs`, step A1) stays the specification by test (section 5.12). |
| 5 | This step adds one language-neutral file that says what "decodes and re-encodes every record byte |
| 6 | for byte, refuses the malformed corpus identically" means, so a port in any language is checked |
| 7 | against the Rust outcomes and never against a second reading of the spec. |
| 8 | |
| 9 | * Exporter and seal: `crates/synapse-store/src/j1_corpus.rs` (test only). |
| 10 | * Sealed corpus: `evidence/j1-codec-corpus/corpus.bin`, SHA-256 in `CORPUS_SHA256`. |
| 11 | * The Omega port lives in `aien-dev/omega` (`src/synapse/syn_codec.osc`, driver |
| 12 | `tests/synapse/test_syn_codec.c`, `make test-synapse-codec`); it vendors the file byte for byte. |
| 13 | |
| 14 | ## 1. What is covered |
| 15 | |
| 16 | Codec v2 record payloads (`CODEC_V2 | kind | body`) for every record kind on `main` (1 to 8, |
| 17 | including the embedded `synapse-core` revision envelope v2 and obsolescence marker) and checkpoint |
| 18 | content (`CheckpointState::encode_content` / `decode_content`, including the revision-log |
| 19 | section). The frame layer around them (WAL frame header, CRC, SHA-256 chain, checkpoint frame and |
| 20 | `state_root`) is out of scope: it is hashing, which belongs to J3 and J4. |
| 21 | |
| 22 | Kinds 9, 10 and 11 are reserved by open PRs (#88, #96, #95). A kind that merges adds its bases to |
| 23 | `bases()`, its flattener arm to `Lf::record`, its expected set in the coverage test, and a reseal; |
| 24 | the Omega side adds one `if k == N` arm in `record`. |
| 25 | |
| 26 | ## 2. Cases |
| 27 | |
| 28 | * **Bases** (48). 28 accepted, each with its LF: the fixture JSON records, the A1 edge records, |
| 29 | kind 7 and 8 records, and two checkpoints (fixture, and one with a revision log). 20 hand-made |
| 30 | refusals that the mutation set cannot reach: reserved kinds 9 to 11, an empty revision section, |
| 31 | a revision section holding kind 6, projections out of order, equal keys in each of the five |
| 32 | checkpoint maps, equal revision seqs, and the repository id rules (empty, 129 bytes, leading or |
| 33 | trailing slash, `//`, `..`, space, non-ASCII). |
| 34 | * **Mutations** of every accepted base marked for mutation, exactly the A1 malformed set |
| 35 | (`a1_malformed_corpus.rs`): every truncation, every bit flip, and a false `u32` length at every |
| 36 | offset with declared values `remaining + 1`, `cap + 1` and `u32::MAX`. |
| 37 | * Each case carries the Rust outcome. An accept is asserted canonical (re-encodes to the input) |
| 38 | before it is written. Today: 55,670 cases, 41,750 accepts, 13,920 refusals. |
| 39 | |
| 40 | ## 3. Logical form (LF) |
| 41 | |
| 42 | A record or checkpoint as a flat list of `u64` cells, the typed fields in encoding order, without |
| 43 | derived lengths or constant bytes. Each field starts with a header cell `type | n << 8`: |
| 44 | |
| 45 | | type | id | n | cells after the header | |
| 46 | | --- | --- | --- | --- | |
| 47 | | u16 | 2 | 1 | value | |
| 48 | | u64 | 4 | 1 | value (`usize` fields too) | |
| 49 | | bool | 5 | 1 | 0 or 1 | |
| 50 | | tag | 6 | 1 | tag byte (record kind, Option, mutation op, file mode) | |
| 51 | | fixed bytes | 7 | len | one cell per byte | |
| 52 | | string | 8 | len | one cell per UTF-8 byte | |
| 53 | | byte string | 9 | len | one cell per byte | |
| 54 | | repository id | 10 | len | one cell per byte | |
| 55 | | count | 11 | 1 | element count of the sequence that follows | |
| 56 | |
| 57 | The field order per record kind is `Lf::record` and per checkpoint `Lf::checkpoint`. The |
| 58 | checkpoint revision-log count is always present in the LF; the bytes hold that section only when |
| 59 | the count is above zero. |
| 60 | |
| 61 | ## 4. File format (version 1, little-endian) |
| 62 | |
| 63 | ``` |
| 64 | magic "SYNJ1COD" | version u16 = 1 | reserved u16 = 0 | cap u32 (2048, A1 record cap) |
| 65 | layout: n u32 | n x u32 size_of EvidenceAttachment, PathMutation, ReviewRecord, String |
| 66 | fields: n u32 | n x name refusal field names, id = index + 1, 0 = none |
| 67 | codes: n u32 | n x name embedded synapse-core refusal codes, id = index + 1 |
| 68 | kinds: n u32 | n x name CodecRefusal kinds, id = index + 1, 0 = accept |
| 69 | bases: n u32 | n x base |
| 70 | cases: n u32 | n x case |
| 71 | seal: SHA-256 of every byte before it |
| 72 | name = len u16 | UTF-8 bytes |
| 73 | base = entry u8 (1 record, 2 checkpoint content) | cap u64 | checkpoint_seq u64 | label name |
| 74 | | len u32 | bytes | lf_cells u32 (u32::MAX = refused base, no LF) | lf_cells x u64 |
| 75 | case = base u16 | op u8 (0 as is, 1 truncate, 2 bit flip, 3 false length) | pos u32 |
| 76 | | [declared u32 when op = 3] | kind u8 | [at u32 | x u32 | y u32 | field u8 when kind > 0] |
| 77 | ``` |
| 78 | |
| 79 | The case input is rebuilt from its base and op: truncate keeps `bytes[..pos]`, bit flip XORs bit |
| 80 | `pos % 8` of byte `pos / 8`, false length writes `declared` at `pos`, op 0 is the base as is. |
| 81 | The `layout` sizes are part of the format because the Rust record decoder checks a sequence's |
| 82 | reservation `n * size_of::<T>()` against the cap (`LengthOverCap`) before reading it. |
| 83 | |
| 84 | Outcome fields per refusal kind (`x`, `y` are 0 when unused): |
| 85 | |
| 86 | | kind | at | x | y | field | |
| 87 | | --- | --- | --- | --- | --- | |
| 88 | | Truncated | offset | need | | | |
| 89 | | LengthOverCap | offset | declared | cap | | |
| 90 | | TrailingBytes | offset | count | | | |
| 91 | | UnknownTag | tag offset | tag | | field id | |
| 92 | | InvalidUtf8 | offset | | | | |
| 93 | | VersionUnsupported | 0 | found | | | |
| 94 | | NotCanonical | offset | | | field id | |
| 95 | | InvalidRepoId | offset | | | | |
| 96 | | EmbeddedMalformed | length-prefix offset | code id | | field id | |
| 97 | |
| 98 | ## 5. Reproduce |
| 99 | |
| 100 | ``` |
| 101 | cargo test -p synapse-store --lib j1_corpus # checks the seal |
| 102 | J1_CORPUS_WRITE=1 cargo test -p synapse-store --lib j1_corpus # rewrites the file |
| 103 | ``` |
| 104 | |
| 105 | After a rewrite, update `CORPUS_SHA256` and re-vendor the file into the Omega repository. |
| 106 | Red and green runs: `evidence/j1-codec-corpus/red-green/`. |
| 107 | |
| 108 | ## 6. Speed against the section 6.2 bound |
| 109 | |
| 110 | Omega native AArch64, one core, on the 28 accepted bases (omega `evidence/SYNAPSE-J1/perf.txt`, |
| 111 | `make bench-synapse-codec`): decode 0.0434 GB/s (23.0 ns per record byte, calc:1c31099c), encode |
| 112 | 0.0269 GB/s. The bound for moving the bytes once is the 273 GB/s memory bandwidth |
| 113 | (`docs/A0_HOST_BOUNDS.md`), so the fraction reached is 1.59e-4 for decode (calc:2ef7b34c) and |
| 114 | 9.85e-5 for encode (calc:a0366c9f). The unit reads one byte per bounds-checked index and writes one |
| 115 | `u64` LF cell per payload byte. The Rust codec's speed on the same bases is not measured here. |
| 116 | |
| 117 | ## 7. Limits |
| 118 | |
| 119 | * The Omega unit refuses inputs above 1,000,000 bytes with a limit refusal (its loop bound); |
| 120 | the corpus inputs are all below that. |
| 121 | * Encode mirrors the Rust encoder: it does not apply decoder caps, so an LF the decoder would |
| 122 | refuse for size can still encode. |
| 123 | * Hashing (frame CRC and SHA-256, checkpoint roots) is J3/J4 scope. |