interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

branch main Branches Tags History

cortex / tests / parity / gen.sh

58 lines · 3337 bytes Raw Blame History
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.
9set -u
10DB=${1:?usage: gen.sh DB OUT}
11OUT=${2:?usage: gen.sh DB OUT}
12P=$(cd "$(dirname "$0")" && pwd)
13R=$(cd "$P/../.." && pwd)
14W=$(dirname "$OUT")
15OMC=$(sh "$R/tools/omc.sh") || exit 2
16DATA=$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.
20sqlite3 -readonly -batch -noheader "$DB" > "$DATA" <<'SQL' || exit 2
21CREATE VIRTUAL TABLE temp.v USING fts5vocab(main, entities_fts, 'row');
22CREATE TEMP TABLE w AS
23WITH 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 <> '')
28SELECT rid, pos, w FROM s WHERE w <> '';
29WITH 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]*')
31SELECT 'T' || char(9) || hex(term) FROM t WHERE (rn - 1) % (n / 40) = 0 AND rn <= 40 * (n / 40);
32SELECT '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);
35SELECT '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);
38SELECT '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);
42SELECT 'E' || char(9) || hex(id) || char(9) || hex(canonical_name) || char(9) || hex(space_slug)
43FROM (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)
47WHERE n < 50 OR (rn - 1) % (n / 50) = 0 AND rn <= 50 * (n / 50)
48ORDER BY r;
49SQL
50mkdir -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"
53if [ $? -ne 0 ] || [ -s "$W/gen.bin/gen.err" ]; then echo "gen.sh: gen.om did not compile" >&2; exit 2; fi
54chmod +x "$W/gen.bin/gen"
55"$W/gen.bin/gen" "$DATA" "$OUT.read" || exit 2
56cat "$OUT.read" "$P/fixed.tsv" > "$OUT" || exit 2
57rm -f "$OUT.read"
58echo "gen.sh: $(grep -c '^T' "$DATA") terms, $(grep -c '^E' "$DATA") entities, $(wc -l < "$OUT") requests"