cortex
aien-dev/cortex
Brought by drakestapleton
cortex / tests / parity / gen.sh
| 1 | #!/bin/sh |
| 2 | # tests/parity/gen.sh : build the parity corpus. |
| 3 | # sh tests/parity/gen.sh DB OUT |
| 4 | # DB is a copy of the cortex-rs database (opened read-only). sqlite3 is used only to extract real |
| 5 | # values from it, as hex (DATA, below); the read-phase requests are built by gen.om (Omega), and the |
| 6 | # fixed cases (health/auth/CORS/routing, every error class, the write phase and the visitor-memory |
| 7 | # shapes) are the hand-written tests/parity/fixed.tsv, appended after them. Deterministic: the same DB |
| 8 | # gives the same corpus. Exit 0 only when every step succeeds. |
| 9 | set -u |
| 10 | DB=${1:?usage: gen.sh DB OUT} |
| 11 | OUT=${2:?usage: gen.sh DB OUT} |
| 12 | P=$(cd "$(dirname "$0")" && pwd) |
| 13 | R=$(cd "$P/../.." && pwd) |
| 14 | W=$(dirname "$OUT") |
| 15 | OMC=$(sh "$R/tools/omc.sh") || exit 2 |
| 16 | DATA=$W/gen.data |
| 17 | # Query terms: 40 FTS vocabulary terms spread over document frequency, 30 title words with their |
| 18 | # case, 15 identifier-like title words (digits, '_', '#', '-', '.'), 15 two-word title phrases. |
| 19 | # Entities: 50 per main space by rowid stride plus every row of the small spaces. |
| 20 | sqlite3 -readonly -batch -noheader "$DB" > "$DATA" <<'SQL' || exit 2 |
| 21 | CREATE VIRTUAL TABLE temp.v USING fts5vocab(main, entities_fts, 'row'); |
| 22 | CREATE TEMP TABLE w AS |
| 23 | WITH RECURSIVE s(rid, pos, w, rest) AS ( |
| 24 | SELECT rowid, 0, '', canonical_name || ' ' FROM entities WHERE retracted = 0 |
| 25 | UNION ALL |
| 26 | SELECT rid, pos + 1, substr(rest, 1, instr(rest, ' ') - 1), substr(rest, instr(rest, ' ') + 1) |
| 27 | FROM s WHERE rest <> '') |
| 28 | SELECT rid, pos, w FROM s WHERE w <> ''; |
| 29 | WITH t AS (SELECT term, row_number() OVER (ORDER BY doc DESC, term) AS rn, |
| 30 | count(*) OVER () AS n FROM temp.v WHERE term GLOB '[a-z0-9]*') |
| 31 | SELECT 'T' || char(9) || hex(term) FROM t WHERE (rn - 1) % (n / 40) = 0 AND rn <= 40 * (n / 40); |
| 32 | SELECT 'T' || char(9) || hex(w) FROM (SELECT DISTINCT w FROM temp.w |
| 33 | WHERE length(w) BETWEEN 4 AND 24 AND w GLOB '[A-Za-z]*' AND w NOT GLOB '*[^A-Za-z]*' |
| 34 | ORDER BY rid % 997, w LIMIT 30); |
| 35 | SELECT 'T' || char(9) || hex(w) FROM (SELECT DISTINCT w FROM temp.w |
| 36 | WHERE length(w) BETWEEN 3 AND 32 AND (w GLOB '*[0-9]*' OR w GLOB '*[_#.-]*') AND w GLOB '*[A-Za-z]*' |
| 37 | ORDER BY rid % 991, w LIMIT 15); |
| 38 | SELECT 'T' || char(9) || hex(p) FROM (SELECT DISTINCT a.w || ' ' || b.w AS p FROM temp.w a |
| 39 | JOIN temp.w b ON b.rid = a.rid AND b.pos = a.pos + 1 |
| 40 | WHERE length(a.w) >= 4 AND length(b.w) >= 4 AND a.w NOT GLOB '*["]*' AND b.w NOT GLOB '*["]*' |
| 41 | ORDER BY a.rid % 983, p LIMIT 15); |
| 42 | SELECT 'E' || char(9) || hex(id) || char(9) || hex(canonical_name) || char(9) || hex(space_slug) |
| 43 | FROM (SELECT rowid AS r, id, canonical_name, space_slug, |
| 44 | row_number() OVER (PARTITION BY space_slug ORDER BY rowid) AS rn, |
| 45 | count(*) OVER (PARTITION BY space_slug) AS n |
| 46 | FROM entities WHERE retracted = 0) |
| 47 | WHERE n < 50 OR (rn - 1) % (n / 50) = 0 AND rn <= 50 * (n / 50) |
| 48 | ORDER BY r; |
| 49 | SQL |
| 50 | mkdir -p "$W/gen.bin" |
| 51 | { echo om2; cat "$P/gen.om"; } > "$W/gen.bin/gen.om" |
| 52 | "$OMC" < "$W/gen.bin/gen.om" > "$W/gen.bin/gen" 2> "$W/gen.bin/gen.err" |
| 53 | if [ $? -ne 0 ] || [ -s "$W/gen.bin/gen.err" ]; then echo "gen.sh: gen.om did not compile" >&2; exit 2; fi |
| 54 | chmod +x "$W/gen.bin/gen" |
| 55 | "$W/gen.bin/gen" "$DATA" "$OUT.read" || exit 2 |
| 56 | cat "$OUT.read" "$P/fixed.tsv" > "$OUT" || exit 2 |
| 57 | rm -f "$OUT.read" |
| 58 | echo "gen.sh: $(grep -c '^T' "$DATA") terms, $(grep -c '^E' "$DATA") entities, $(wc -l < "$OUT") requests" |