interplane.Get Omega
Read in your language

synapse

A component of the AienOS ecosystem.

Brought by drakestapleton

branch main Branches Tags History

synapse / docs / J1_OMEGA_CODEC.md

123 lines · 6574 bytes Raw Blame History Source

ADR 0003 step J1: Omega port of the binary codec (cross-check corpus)

Issue #53. ADR 0003 section 9, row 33 (J1), needs A1. The Rust codec (crates/synapse-store/src/codec.rs, step A1) stays the specification by test (section 5.12). This step adds one language-neutral file that says what "decodes and re-encodes every record byte for byte, refuses the malformed corpus identically" means, so a port in any language is checked against the Rust outcomes and never against a second reading of the spec.

1. What is covered

Codec v2 record payloads (CODEC_V2 | kind | body) for every record kind on main (1 to 8, including the embedded synapse-core revision envelope v2 and obsolescence marker) and checkpoint content (CheckpointState::encode_content / decode_content, including the revision-log section). The frame layer around them (WAL frame header, CRC, SHA-256 chain, checkpoint frame and state_root) is out of scope: it is hashing, which belongs to J3 and J4.

Kinds 9, 10 and 11 are reserved by open PRs (#88, #96, #95). A kind that merges adds its bases to bases(), its flattener arm to Lf::record, its expected set in the coverage test, and a reseal; the Omega side adds one if k == N arm in record.

2. Cases

3. Logical form (LF)

A record or checkpoint as a flat list of u64 cells, the typed fields in encoding order, without derived lengths or constant bytes. Each field starts with a header cell type | n << 8:

typeidncells after the header
u1621value
u6441value (usize fields too)
bool510 or 1
tag61tag byte (record kind, Option, mutation op, file mode)
fixed bytes7lenone cell per byte
string8lenone cell per UTF-8 byte
byte string9lenone cell per byte
repository id10lenone cell per byte
count111element count of the sequence that follows

The field order per record kind is Lf::record and per checkpoint Lf::checkpoint. The checkpoint revision-log count is always present in the LF; the bytes hold that section only when the count is above zero.

4. File format (version 1, little-endian)

magic "SYNJ1COD" | version u16 = 1 | reserved u16 = 0 | cap u32 (2048, A1 record cap)
layout: n u32 | n x u32   size_of EvidenceAttachment, PathMutation, ReviewRecord, String
fields: n u32 | n x name  refusal field names, id = index + 1, 0 = none
codes:  n u32 | n x name  embedded synapse-core refusal codes, id = index + 1
kinds:  n u32 | n x name  CodecRefusal kinds, id = index + 1, 0 = accept
bases:  n u32 | n x base
cases:  n u32 | n x case
seal:   SHA-256 of every byte before it
name = len u16 | UTF-8 bytes
base = entry u8 (1 record, 2 checkpoint content) | cap u64 | checkpoint_seq u64 | label name
       | len u32 | bytes | lf_cells u32 (u32::MAX = refused base, no LF) | lf_cells x u64
case = base u16 | op u8 (0 as is, 1 truncate, 2 bit flip, 3 false length) | pos u32
       | [declared u32 when op = 3] | kind u8 | [at u32 | x u32 | y u32 | field u8 when kind > 0]

The case input is rebuilt from its base and op: truncate keeps bytes[..pos], bit flip XORs bit pos % 8 of byte pos / 8, false length writes declared at pos, op 0 is the base as is. The layout sizes are part of the format because the Rust record decoder checks a sequence's reservation n * size_of::<T>() against the cap (LengthOverCap) before reading it.

Outcome fields per refusal kind (x, y are 0 when unused):

kindatxyfield
Truncatedoffsetneed
LengthOverCapoffsetdeclaredcap
TrailingBytesoffsetcount
UnknownTagtag offsettagfield id
InvalidUtf8offset
VersionUnsupported0found
NotCanonicaloffsetfield id
InvalidRepoIdoffset
EmbeddedMalformedlength-prefix offsetcode idfield id

5. Reproduce

cargo test -p synapse-store --lib j1_corpus                    # checks the seal
J1_CORPUS_WRITE=1 cargo test -p synapse-store --lib j1_corpus  # rewrites the file

After a rewrite, update CORPUS_SHA256 and re-vendor the file into the Omega repository. Red and green runs: evidence/j1-codec-corpus/red-green/.

6. Speed against the section 6.2 bound

Omega native AArch64, one core, on the 28 accepted bases (omega evidence/SYNAPSE-J1/perf.txt, make bench-synapse-codec): decode 0.0434 GB/s (23.0 ns per record byte, calc:1c31099c), encode 0.0269 GB/s. The bound for moving the bytes once is the 273 GB/s memory bandwidth (docs/A0_HOST_BOUNDS.md), so the fraction reached is 1.59e-4 for decode (calc:2ef7b34c) and 9.85e-5 for encode (calc:a0366c9f). The unit reads one byte per bounds-checked index and writes one u64 LF cell per payload byte. The Rust codec's speed on the same bases is not measured here.

7. Limits