aien-protocols-mv
Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem
Brought by drakestapleton
aien-protocols-mv / specs / crumb-visible / CRUMB_READER_CONTRACT.md
| 1 | # Crumb Reader Contract: CRB1 visible record |
| 2 | |
| 3 | **Status**: Stable (extracted from the running implementation; no new format features) |
| 4 | **Contract version**: 1.0.0 |
| 5 | **Wire schema version**: 1 (the `schema_version` field below) |
| 6 | **License**: Community Specification License 1.0 |
| 7 | **Reference implementation**: `aien-sovereign-core`, `crates/crumbs/src/visible.rs` (`VisibleCrumb::from_bytes`), Rust |
| 8 | **Other implementations**: `omega`, `src/crumbline/cl_crumb.c` (`cl_crumb_decode`), C. Future: an Omega-compiled reader. |
| 9 | **Conformance corpus**: [`vectors/`](vectors/) with [`vectors/expected.txt`](vectors/expected.txt) |
| 10 | |
| 11 | ## 1. Purpose |
| 12 | |
| 13 | A crumb lesson file is the only thing a learner sees of a crumb: an ordered list |
| 14 | of opaque `INPUT -> OUTPUT` examples plus the minimum structure needed to parse |
| 15 | them. This contract states, in language-neutral terms, which byte strings are |
| 16 | valid CRB1 records, what a reader must extract from them, and which numbered |
| 17 | refusal a reader must return for every invalid byte string. Any reader that |
| 18 | passes the shared corpus with zero differences may stand in for any other. |
| 19 | |
| 20 | The contract is the byte format and the outcome, never a language type. No |
| 21 | Rust enum, struct layout or serialization crosses it. |
| 22 | |
| 23 | ## 2. Reader interface |
| 24 | |
| 25 | A reader is a pure function of one read-only byte buffer, given as a pointer |
| 26 | and a length (or the language's equivalent). The caller owns the buffer; the |
| 27 | reader borrows it for the duration of one call and keeps no reference. The |
| 28 | reader returns exactly one of: |
| 29 | |
| 30 | - **accept**, with the decoded record (section 4), or |
| 31 | - **refuse**, with one refusal code (section 5). |
| 32 | |
| 33 | A reader performs no I/O, allocation visible to the caller, clock reads or |
| 34 | randomness that can change the outcome. On refusal it exposes no partial |
| 35 | record and no content of the input (codes are bare numbers). |
| 36 | |
| 37 | ## 3. Byte format (schema 1) |
| 38 | |
| 39 | All integers are unsigned little-endian. Offsets are from the start of the |
| 40 | buffer. |
| 41 | |
| 42 | | Offset | Size | Field | Valid values | |
| 43 | |---|---|---|---| |
| 44 | | 0 | 4 | magic | ASCII `CRB1` (`43 52 42 31`) | |
| 45 | | 4 | 2 | schema_version | `1` | |
| 46 | | 6 | 1 | encoding | `1` = decimal UTF-8 lanes, `2` = raw little-endian lanes | |
| 47 | | 7 | 1 | flags | bit 0 = budget present; bits 1..7 must be 0 | |
| 48 | | 8 | 1 | in_arity | lanes per input, 1..8 | |
| 49 | | 9 | 1 | out_arity | lanes per output, 1..8 | |
| 50 | | 10 | 1 | in_lane_bytes | encoding 1: `0`; encoding 2: `1`, `2`, `4` or `8` | |
| 51 | | 11 | 1 | out_lane_bytes | same rule as in_lane_bytes | |
| 52 | | 12 | 4 | n | number of examples, 1..64 | |
| 53 | | 16 | ... | examples | n times: `u32 in_len, in_len bytes, u32 out_len, out_len bytes`; each length at most 168 | |
| 54 | | ... | 16 | budget (only if flags bit 0) | `max_candidates u32, max_depth u32, max_oracle_queries u32, max_program_ops u32` | |
| 55 | |
| 56 | The buffer ends exactly after the last example, or after the budget when |
| 57 | present. No trailing bytes. |
| 58 | |
| 59 | **Lane fields.** An example field holds `arity` lane values. |
| 60 | |
| 61 | - Encoding 2 (raw): the field length is exactly `lane_bytes * arity`; lane `k` |
| 62 | is bytes `[k*lane_bytes, (k+1)*lane_bytes)` read as an unsigned |
| 63 | little-endian integer. |
| 64 | - Encoding 1 (decimal): the field is UTF-8 text made of exactly `arity` |
| 65 | parts separated by single ASCII spaces (`0x20`). Each part is a canonical |
| 66 | base-10 unsigned integer: one or more ASCII digits `0`..`9`, no sign, no |
| 67 | leading zero unless the part is exactly `0`, value at most |
| 68 | 18446744073709551615 (2^64 - 1). |
| 69 | |
| 70 | Every valid record has exactly one byte encoding, so re-encoding an accepted |
| 71 | record reproduces the input buffer byte for byte. |
| 72 | |
| 73 | ## 4. Decoded record |
| 74 | |
| 75 | On accept the reader yields: `encoding`, `in_arity`, `in_lane_bytes`, |
| 76 | `out_arity`, `out_lane_bytes`, `n`, for each example the `in_arity` input lane |
| 77 | values and `out_arity` output lane values as unsigned 64-bit integers, and the |
| 78 | four budget values when flags bit 0 is set. |
| 79 | |
| 80 | ## 5. Refusal codes |
| 81 | |
| 82 | Zero means accept. A refusal is a negative integer from this table, and only |
| 83 | from this table. |
| 84 | |
| 85 | | Code | Name | Meaning | |
| 86 | |---|---|---| |
| 87 | | -1 | MAGIC | first four bytes are not `CRB1` | |
| 88 | | -2 | VERSION | schema_version is not 1 | |
| 89 | | -3 | SHAPE | encoding, arity or lane-bytes value outside its valid set | |
| 90 | | -4 | LANE | a lane field has the wrong length, the wrong number of decimal parts, invalid UTF-8, or a decimal value above 2^64 - 1 | |
| 91 | | -5 | LENGTH | the buffer ends before a field it must contain, n is 0 or above 64, an example length is above 168, or bytes remain after the record | |
| 92 | | -6 | NONCANONICAL | flags has a reserved bit set, or a decimal part is not canonical (empty, a non-digit character, or a leading zero) | |
| 93 | |
| 94 | ## 6. Check order (determines the code) |
| 95 | |
| 96 | A reader refuses with the code of the **first** failing check in this order. |
| 97 | Reading past the end of the buffer at any step is LENGTH (-5), including |
| 98 | inside the magic. |
| 99 | |
| 100 | 1. Read 4 bytes (magic). Not `CRB1`: MAGIC. |
| 101 | 2. Read u16 schema_version. Not 1: VERSION. |
| 102 | 3. Read u8 encoding. Not 1 or 2: SHAPE. |
| 103 | 4. Read u8 flags. Any bit other than bit 0 set: NONCANONICAL. |
| 104 | 5. Read all four of in_arity, out_arity, in_lane_bytes, out_lane_bytes, then |
| 105 | check: arity outside 1..8: SHAPE; lane bytes invalid for the encoding: SHAPE. |
| 106 | 6. Read u32 n. n = 0 or n > 64: LENGTH. |
| 107 | 7. For each example in order: read u32 in_len (above 168: LENGTH), read in_len |
| 108 | bytes; read u32 out_len (above 168: LENGTH), read out_len bytes. Only after |
| 109 | both fields of this example are read, validate the input lanes, then the |
| 110 | output lanes: |
| 111 | - raw: length differs from `lane_bytes * arity`: LANE. |
| 112 | - decimal: not valid UTF-8: LANE; number of space-separated parts differs |
| 113 | from arity: LANE; then for each part in order: empty, contains a byte |
| 114 | other than `0`..`9`, or has a leading zero: NONCANONICAL; value above |
| 115 | 2^64 - 1: LANE. |
| 116 | 8. If flags bit 0: read four u32 budget values. |
| 117 | 9. Bytes remain: LENGTH. |
| 118 | |
| 119 | Note for implementers: step 7 distinguishes invalid UTF-8 (LANE) from valid |
| 120 | UTF-8 that is not a digit (NONCANONICAL), because the reference checks UTF-8 |
| 121 | validity before canonical form. An implementation without a UTF-8 validator |
| 122 | must reproduce this split to conform (vectors r031 and r032). |
| 123 | |
| 124 | ## 7. Determinism rule |
| 125 | |
| 126 | For the same input bytes every conforming reader, on every run, returns the |
| 127 | same outcome: the same accept or refuse, the same refusal code, and the same |
| 128 | decoded values. The outcome is compared through one canonical text line, |
| 129 | which is also the corpus manifest format: |
| 130 | |
| 131 | ```text |
| 132 | ok enc=E in=AxB out=CxD n=N[ budget=C1,C2,C3,C4] | i1 i2 -> o1 | ... |
| 133 | refuse CODE |
| 134 | ``` |
| 135 | |
| 136 | `A`/`C` are in/out arity, `B`/`D` in/out lane bytes, lane values in base 10, |
| 137 | one ` | ins -> outs` group per example in order. The budget group appears only |
| 138 | when flags bit 0 is set. |
| 139 | |
| 140 | ## 8. Conformance corpus |
| 141 | |
| 142 | `vectors/expected.txt` lists every vector as `<file> <outcome line>`. Lines |
| 143 | starting with `#` are comments. The manifest is plain text so C, shell and |
| 144 | Omega harnesses can read it without a JSON parser. |
| 145 | |
| 146 | | Group | Files | Source | |
| 147 | |---|---|---| |
| 148 | | Family vectors | `v001.crb` .. `v122.crb` | `crumbs vectors DIR` (one per registered crumb family, seed 7), byte-identical to omega `tests/crumbline/vectors` at contract 1.0.0 | |
| 149 | | Edge positives | `p001.crb` .. `p005.crb` | built byte by byte by `tools/make-vectors.sh`: budget present, decimal 0 and 2^64 - 1, raw 1/2/8-byte lanes, 64 examples | |
| 150 | | Refusals | `r001.crb` .. `r044.crb` | built byte by byte by `tools/make-vectors.sh`; each names its expected code; includes check-order cases r037..r040 | |
| 151 | |
| 152 | Regenerate with `tools/make-vectors.sh <path to crumbs binary> vectors`. The |
| 153 | script is shell only. Every expected outcome is checked by the reference |
| 154 | reader (`crates/crumbs/tests/visible_conformance.rs`), so a hand-written |
| 155 | expectation that the reference disagrees with fails CI. |
| 156 | |
| 157 | A conforming harness prints one summary line: |
| 158 | |
| 159 | ```text |
| 160 | CRUMB_VISIBLE_V1_CONFORMANCE impl=<rust|c|osc> pass=N fail=0 |
| 161 | ``` |
| 162 | |
| 163 | ## 9. Versioning |
| 164 | |
| 165 | - Wire schema 1 is the only accepted schema. A reader refuses every other |
| 166 | schema value with VERSION; it never reinterprets an older or newer record. |
| 167 | - A change to the byte layout or the meaning of a field is a new wire schema |
| 168 | and a new major contract version, with its own corpus; schema 1 vectors |
| 169 | stay and keep their outcomes. |
| 170 | - Adding vectors that the reference already decides is a patch version. |
| 171 | Changing a refusal code or the check order is a major version. |
| 172 | - No feature negotiation: there is one schema, checked exactly. |
| 173 | |
| 174 | ## 10. Ownership switch (from the migration plan) |
| 175 | |
| 176 | An implementation may replace the current one inside a consumer only after it |
| 177 | passes 100% of this corpus with zero differences in outcome lines, and after |
| 178 | the agreed differential fuzz shows zero disagreements against the reference. |
| 179 | The replaced implementation stays one release as a differential reference. |