aien-protocols-mv
Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem
Brought by drakestapleton
aien-protocols-mv / specs / crumb-visible / tools / make-vectors.sh
| 1 | #!/bin/bash |
| 2 | # make-vectors.sh: regenerate the CRB1 (Crumb v1 visible record) shared |
| 3 | # conformance corpus. Language-neutral contract: ../CRUMB_READER_CONTRACT.md. |
| 4 | # |
| 5 | # Usage: make-vectors.sh CRUMBS_BIN OUT_DIR |
| 6 | # CRUMBS_BIN the `crumbs` tool from aien-sovereign-core (crates/crumbs), |
| 7 | # built with: cargo build -p crumbs --bin crumbs |
| 8 | # OUT_DIR normally specs/crumb-visible/vectors |
| 9 | # |
| 10 | # Three groups of vectors, all listed in OUT_DIR/expected.txt: |
| 11 | # vNNN.crb positive, one per registered crumb family, written by |
| 12 | # `crumbs vectors` (seed 7); expected lines copied from its output. |
| 13 | # pNNN.crb positive edge cases built byte by byte below (budget present, |
| 14 | # max u64, raw lanes, 64 examples). |
| 15 | # rNNN.crb refusal cases built byte by byte below, each with the contract |
| 16 | # refusal code the first failing check must produce. |
| 17 | # The expected outcome of every p and r vector is checked by the Rust |
| 18 | # reference (crates/crumbs/tests/visible_conformance.rs); nothing here is |
| 19 | # trusted on its own. Shell only: no Python, no outside tools beyond coreutils. |
| 20 | set -eu |
| 21 | export LC_ALL=C |
| 22 | CRUMBS=${1:?CRUMBS_BIN} |
| 23 | OUT=${2:?OUT_DIR} |
| 24 | mkdir -p "$OUT" |
| 25 | rm -f "$OUT"/v*.crb "$OUT"/p*.crb "$OUT"/r*.crb "$OUT"/expected.txt |
| 26 | tmp=$(mktemp -d) |
| 27 | trap 'rm -rf "$tmp"' EXIT |
| 28 | |
| 29 | "$CRUMBS" vectors "$tmp/gen" >/dev/null |
| 30 | cp "$tmp"/gen/v*.crb "$OUT"/ |
| 31 | |
| 32 | # ---- byte helpers ------------------------------------------------------- |
| 33 | u8() { printf "\\x$(printf %02x "$1")"; } |
| 34 | le16() { u8 $(($1 & 255)); u8 $((($1 >> 8) & 255)); } |
| 35 | le32() { for s in 0 8 16 24; do u8 $((($1 >> s) & 255)); done; } |
| 36 | # hdr ENC FLAGS IN_ARITY OUT_ARITY IN_LB OUT_LB N (schema 1) |
| 37 | hdr() { printf 'CRB1'; le16 1; u8 "$1"; u8 "$2"; u8 "$3"; u8 "$4"; u8 "$5"; u8 "$6"; le32 "$7"; } |
| 38 | # blob TEXT : u32 length + bytes of TEXT |
| 39 | blob() { le32 ${#1}; printf '%s' "$1"; } |
| 40 | # hexblob HEX... : u32 length + raw bytes |
| 41 | hexblob() { le32 $#; for h in "$@"; do printf "\\x$h"; done; } |
| 42 | # patch FILE OFFSET HEX... : copy of FILE with bytes replaced at OFFSET |
| 43 | patch() { |
| 44 | local f=$1 off=$2 |
| 45 | shift 2 |
| 46 | head -c "$off" "$f" |
| 47 | for h in "$@"; do printf "\\x$h"; done |
| 48 | tail -c +$((off + 1 + $#)) "$f" |
| 49 | } |
| 50 | |
| 51 | { |
| 52 | echo "# CRB1 shared conformance corpus. Contract: CRUMB_READER_CONTRACT.md (contract 1.0.0, schema 1)." |
| 53 | echo "# Line: <file> ok enc=E in=AxB out=AxB n=N[ budget=C,D,Q,P] | in... -> out... | ..." |
| 54 | echo "# or: <file> refuse <code> (codes: -1 MAGIC -2 VERSION -3 SHAPE -4 LANE -5 LENGTH -6 NONCANONICAL)" |
| 55 | echo "# Generated by tools/make-vectors.sh; do not edit by hand." |
| 56 | cat "$tmp/gen/expected.txt" |
| 57 | } >"$OUT/expected.txt" |
| 58 | ok() { echo "$1 ok $2" >>"$OUT/expected.txt"; } |
| 59 | refuse() { echo "$1 refuse $2" >>"$OUT/expected.txt"; } |
| 60 | |
| 61 | V1="$OUT/v001.crb" # decimal, 1x0 -> 1x0, n=8, no budget |
| 62 | v1line=$(grep '^v001.crb ' "$tmp/gen/expected.txt" | sed 's/^v001.crb ok //') |
| 63 | |
| 64 | # ---- positive edge cases ------------------------------------------------ |
| 65 | # p001: budget present (v001 with flags bit0 and four budget words). |
| 66 | { patch "$V1" 7 01; le32 1; le32 2; le32 3; le32 4; } >"$OUT/p001.crb" |
| 67 | ok p001.crb "$(echo "$v1line" | sed 's/ n=8 / n=8 budget=1,2,3,4 /')" |
| 68 | # p002: decimal zero and max u64. |
| 69 | { hdr 1 0 2 1 0 0 1; blob "0 18446744073709551615"; blob "7"; } >"$OUT/p002.crb" |
| 70 | ok p002.crb "enc=1 in=2x0 out=1x0 n=1 | 0 18446744073709551615 -> 7" |
| 71 | # p003: raw lanes, 8 one-byte lanes in, one 8-byte lane out (all ones). |
| 72 | { hdr 2 0 8 1 1 8 1; hexblob 00 01 02 03 04 05 06 07; hexblob ff ff ff ff ff ff ff ff; } >"$OUT/p003.crb" |
| 73 | ok p003.crb "enc=2 in=8x1 out=1x8 n=1 | 0 1 2 3 4 5 6 7 -> 18446744073709551615" |
| 74 | # p004: 64 examples (the maximum), decimal i -> i+1. |
| 75 | line="enc=1 in=1x0 out=1x0 n=64" |
| 76 | { |
| 77 | hdr 1 0 1 1 0 0 64 |
| 78 | for i in $(seq 0 63); do blob "$i"; blob "$((i + 1))"; done |
| 79 | } >"$OUT/p004.crb" |
| 80 | for i in $(seq 0 63); do line="$line | $i -> $((i + 1))"; done |
| 81 | ok p004.crb "$line" |
| 82 | # p005: raw two-byte lanes, little-endian order. |
| 83 | { hdr 2 0 2 1 2 2 1; hexblob 34 12 00 80; hexblob ff ff; } >"$OUT/p005.crb" |
| 84 | ok p005.crb "enc=2 in=2x2 out=1x2 n=1 | 4660 32768 -> 65535" |
| 85 | |
| 86 | # ---- refusal cases (first failing check in contract order wins) --------- |
| 87 | M() { hdr 1 0 1 1 0 0 1; blob 1; blob 2; } # minimal valid decimal crumb |
| 88 | M >"$tmp/m.crb" |
| 89 | : >"$OUT/r001.crb"; refuse r001.crb -5 # empty |
| 90 | printf 'CRB' >"$OUT/r002.crb"; refuse r002.crb -5 # shorter than magic |
| 91 | patch "$V1" 0 58 >"$OUT/r003.crb"; refuse r003.crb -1 # magic XRB1 |
| 92 | patch "$V1" 0 63 72 62 31 >"$OUT/r004.crb"; refuse r004.crb -1 # magic crb1 |
| 93 | patch "$V1" 4 02 00 >"$OUT/r005.crb"; refuse r005.crb -2 # schema 2 |
| 94 | patch "$V1" 4 00 00 >"$OUT/r006.crb"; refuse r006.crb -2 # schema 0 |
| 95 | head -c 5 "$V1" >"$OUT/r007.crb"; refuse r007.crb -5 # cut inside schema |
| 96 | head -c 10 "$V1" >"$OUT/r008.crb"; refuse r008.crb -5 # cut inside header |
| 97 | patch "$tmp/m.crb" 6 00 >"$OUT/r009.crb"; refuse r009.crb -3 # encoding 0 |
| 98 | patch "$tmp/m.crb" 6 03 >"$OUT/r010.crb"; refuse r010.crb -3 # encoding 3 |
| 99 | patch "$tmp/m.crb" 7 02 >"$OUT/r011.crb"; refuse r011.crb -6 # flags bit1 |
| 100 | patch "$tmp/m.crb" 7 80 >"$OUT/r012.crb"; refuse r012.crb -6 # flags bit7 |
| 101 | patch "$tmp/m.crb" 8 00 >"$OUT/r013.crb"; refuse r013.crb -3 # in_arity 0 |
| 102 | patch "$tmp/m.crb" 9 09 >"$OUT/r014.crb"; refuse r014.crb -3 # out_arity 9 |
| 103 | patch "$tmp/m.crb" 10 01 >"$OUT/r015.crb"; refuse r015.crb -3 # decimal with lane bytes 1 |
| 104 | { hdr 2 0 1 1 3 1 1; hexblob 00 00 00; hexblob 00; } >"$OUT/r016.crb"; refuse r016.crb -3 # raw lane bytes 3 |
| 105 | { hdr 2 0 1 1 1 0 1; hexblob 00; } >"$OUT/r017.crb"; refuse r017.crb -3 # raw lane bytes 0 |
| 106 | { hdr 1 0 1 1 0 0 0; } >"$OUT/r018.crb"; refuse r018.crb -5 # n = 0 |
| 107 | { hdr 1 0 1 1 0 0 65; } >"$OUT/r019.crb"; refuse r019.crb -5 # n = 65 |
| 108 | head -c 20 "$V1" >"$OUT/r020.crb"; refuse r020.crb -5 # cut inside examples |
| 109 | { cat "$V1"; u8 0; } >"$OUT/r021.crb"; refuse r021.crb -5 # one trailing byte |
| 110 | { hdr 1 0 1 1 0 0 1; blob "$(printf '1%.0s' $(seq 1 169))"; blob 2; } >"$OUT/r022.crb"; refuse r022.crb -5 # example field 169 bytes (> 168) |
| 111 | { hdr 1 0 1 1 0 0 1; le32 9; printf '1'; } >"$OUT/r023.crb"; refuse r023.crb -5 # length past end |
| 112 | { hdr 1 0 1 1 0 0 1; blob 03; blob 2; } >"$OUT/r024.crb"; refuse r024.crb -6 # leading zero |
| 113 | { hdr 1 0 1 1 0 0 1; blob +5; blob 2; } >"$OUT/r025.crb"; refuse r025.crb -6 # sign |
| 114 | { hdr 1 0 1 1 0 0 1; blob ""; blob 2; } >"$OUT/r026.crb"; refuse r026.crb -6 # empty lane |
| 115 | { hdr 1 0 1 1 0 0 1; blob "1 2"; blob 2; } >"$OUT/r027.crb"; refuse r027.crb -4 # too many lanes |
| 116 | { hdr 1 0 2 1 0 0 1; blob "1 2"; blob 2; } >"$OUT/r028.crb"; refuse r028.crb -4 # double space |
| 117 | { hdr 1 0 1 1 0 0 1; blob "5 "; blob 2; } >"$OUT/r029.crb"; refuse r029.crb -4 # trailing space |
| 118 | { hdr 1 0 1 1 0 0 1; blob 18446744073709551616; blob 2; } >"$OUT/r030.crb"; refuse r030.crb -4 # u64 overflow |
| 119 | { hdr 1 0 1 1 0 0 1; hexblob ff; blob 2; } >"$OUT/r031.crb"; refuse r031.crb -4 # invalid UTF-8 |
| 120 | { hdr 1 0 1 1 0 0 1; hexblob c3 a9; blob 2; } >"$OUT/r032.crb"; refuse r032.crb -6 # valid UTF-8, not a digit |
| 121 | { hdr 2 0 1 1 4 4 1; hexblob 00 00 00; hexblob 00 00 00 00; } >"$OUT/r033.crb"; refuse r033.crb -4 # raw lane length |
| 122 | patch "$tmp/m.crb" 7 01 >"$OUT/r034.crb"; refuse r034.crb -5 # budget flag, no budget |
| 123 | { patch "$tmp/m.crb" 7 01; le32 1; le32 2; } >"$OUT/r035.crb"; refuse r035.crb -5 # budget cut short |
| 124 | { M; le32 1; le32 2; le32 3; le32 4; } >"$OUT/r036.crb"; refuse r036.crb -5 # budget bytes, flag clear |
| 125 | { hdr 1 0 1 1 0 0 1; blob 03; le32 2; } >"$OUT/r037.crb"; refuse r037.crb -5 # order: output read before input lanes |
| 126 | patch "$tmp/m.crb" 6 03 02 >"$OUT/r038.crb"; refuse r038.crb -3 # order: encoding before flags |
| 127 | patch "$tmp/m.crb" 7 02 00 >"$OUT/r039.crb"; refuse r039.crb -6 # order: flags before arity |
| 128 | { printf 'CRB1'; le16 1; u8 1; u8 0; u8 0; } >"$OUT/r040.crb"; refuse r040.crb -5 # order: whole header read before arity check |
| 129 | { hdr 1 0 1 1 0 0 1; blob 1; blob 03; } >"$OUT/r041.crb"; refuse r041.crb -6 # leading zero on output side |
| 130 | { hdr 1 0 1 1 0 0 2; blob 1; blob 2; blob 01; blob 2; } >"$OUT/r042.crb"; refuse r042.crb -6 # second example bad |
| 131 | patch "$tmp/m.crb" 4 01 01 >"$OUT/r043.crb"; refuse r043.crb -2 # schema 257 (high byte) |
| 132 | { hdr 1 0 1 1 0 0 1; blob 1; blob "2 3"; } >"$OUT/r044.crb"; refuse r044.crb -4 # output lane count |
| 133 | |
| 134 | nv=$(ls "$OUT"/v*.crb | wc -l) |
| 135 | np=$(ls "$OUT"/p*.crb | wc -l) |
| 136 | nr=$(ls "$OUT"/r*.crb | wc -l) |
| 137 | echo "CRB1 vectors written: family=$nv edge=$np refusal=$nr" |