omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tools / gen / abstract / extract
| 1 | #!/usr/bin/env bash |
| 2 | # tools/gen/abstract/extract [--root <repo>] <build dir> <out records dir> |
| 3 | # |
| 4 | # GEN-3 (aien-spark/omega#478) deterministic abstraction extractor. Reads only the public corpus |
| 5 | # (tests/one_layer/generalization/corpus: tasks/, META, PROBES, specs/) and writes a record set |
| 6 | # (RECORD_SCHEMA.md section 5) to <out records dir>, replacing raw/, abstraction/, receipts/, MANIFEST, |
| 7 | # SET.sha256 and REJECTIONS there: |
| 8 | # 1. every corpus task (extraction and probe tasks) gets a raw record: its whole solution, after the reader |
| 9 | # chain, x445 check and the harness runner on its public tests pass (a failure is fatal: the corpus is bad) |
| 10 | # 2. the solutions of the extraction tasks (all tasks not named in PROBES) are split into functions; each |
| 11 | # helper (every function but the entry) is canonicalised (omegese emit, names -> fn<k>_, omega_emit) and |
| 12 | # fingerprinted; identical and alpha-equivalent helpers fall into one group |
| 13 | # 3. a group becomes an abstraction record only if, in this order: its helper calls no other function (v1), |
| 14 | # it occurs in >= 2 distinct extraction tasks with distinct solutions, its fingerprint differs from every |
| 15 | # whole corpus solution, its name (the most common one, ties to the byte-smallest) is unique and has a |
| 16 | # spec file specs/<name>.spec, checker2 accepts it, and every functional check passes (property on |
| 17 | # generated inputs, the stated example, every counterexample). Every other group is a line in REJECTIONS. |
| 18 | # The same inputs give byte-identical output (no clock, no randomness, sorted, C locale). |
| 19 | # Records are suggestions shown to a model, never trusted code. |
| 20 | set -euo pipefail |
| 21 | HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 22 | . "$HERE/genabs.sh" |
| 23 | ROOT="$(cd "$HERE/../../.." && pwd)" |
| 24 | if [ "${1:-}" = --root ]; then ROOT="$(cd "$2" && pwd)"; shift 2; fi |
| 25 | [ $# -eq 2 ] || { echo "usage: extract [--root <repo>] <build dir> <out records dir>" >&2; exit 2; } |
| 26 | B="$(cd "$1" && pwd)" |
| 27 | mkdir -p "$2"; OUT="$(cd "$2" && pwd)" |
| 28 | W="$(mktemp -d "${TMPDIR:-/tmp}/gen-extract.XXXXXXXX")" |
| 29 | trap 'rm -rf "$W"' EXIT |
| 30 | ga_init "$B" "$ROOT" "$W" || exit 2 |
| 31 | CR="$ROOT/$GA_CORPUS" |
| 32 | die() { echo "extract: $*" >&2; exit 1; } |
| 33 | |
| 34 | ga_scan || die "corpus scan failed" |
| 35 | rm -rf "$OUT/raw" "$OUT/abstraction" "$OUT/receipts" "$OUT/MANIFEST" "$OUT/SET.sha256" "$OUT/REJECTIONS" "$OUT/PREFIX" "$OUT/C.MANIFEST" "$OUT/D.MANIFEST" |
| 36 | mkdir -p "$OUT/raw" "$OUT/abstraction" "$OUT/receipts" |
| 37 | |
| 38 | # put_receipt: write GA_RCPT under receipts/<its sha256>, print the id |
| 39 | put_receipt() { |
| 40 | local id; id="$(ga_shas "$GA_RCPT")" |
| 41 | printf '%s' "$GA_RCPT" > "$OUT/receipts/$id" |
| 42 | echo "$id" |
| 43 | } |
| 44 | src_line() { echo "source $1 $(ga_sha "$ROOT/$1")"; } |
| 45 | |
| 46 | # ---- 1. raw records ------------------------------------------------------------------------------------------ |
| 47 | declare -A SPLIT SOLSHA SOLFP |
| 48 | while read -r _ t split ssha sfp; do |
| 49 | SPLIT[$t]="$split" SOLSHA[$t]="$ssha" SOLFP[$t]="$sfp" |
| 50 | d="$CR/tasks/$t" |
| 51 | ga_raw_checks "raw-$t" "$d" "$W/raw.$t" || die "corpus task $t fails its own checks: $GA_ERR" |
| 52 | rc="$(put_receipt)" |
| 53 | fam="$(ga_meta_token "$t")" || die "$GA_ERR" |
| 54 | { |
| 55 | echo "gen-record 1"; echo "id raw-$t"; echo "kind raw"; echo "split $split"; echo "family $fam" |
| 56 | echo "entry $t" |
| 57 | echo "params$(sed -n 's/^params//p' "$d/requirement")" |
| 58 | echo "ret $(sed -n 's/^ret //p' "$d/requirement")" |
| 59 | echo "signature $(grep '^fn ' "$d/solution.omegese" | tail -n 1)" |
| 60 | echo "desc $(cat "$d/desc")" |
| 61 | echo "receipt $rc"; echo "validity verified"; echo "ancestry $t $ssha" |
| 62 | for f in META PROBES "tasks/$t/desc" "tasks/$t/expected" "tasks/$t/inputs" "tasks/$t/requirement" "tasks/$t/solution.omegese"; do |
| 63 | src_line "$GA_CORPUS/$f" |
| 64 | done | sort |
| 65 | echo "body"; cat "$d/solution.omegese" |
| 66 | } > "$OUT/raw/raw-$t.rec" |
| 67 | done < <(grep '^S ' "$W/scan") |
| 68 | |
| 69 | # ---- 2. helper groups (extraction tasks only) ------------------------------------------------------------- |
| 70 | # key: the fingerprint of a leaf helper, or "nonleaf:<name>" for a helper that calls another function |
| 71 | : > "$W/groups" |
| 72 | while read -r _ t k name calls fp file; do |
| 73 | [ "${SPLIT[$t]}" = corpus ] || continue |
| 74 | if [ "$fp" = - ]; then key="nonleaf:$name"; else key="$fp"; fi |
| 75 | echo "$key $t $name $calls $file" >> "$W/groups" |
| 76 | done < <(grep '^H ' "$W/scan") |
| 77 | sort -o "$W/groups" "$W/groups" |
| 78 | |
| 79 | # group name: the most common helper name, ties to the byte-smallest |
| 80 | gname() { awk -v k="$1" '$1 == k { print $3 }' "$W/groups" | sort | uniq -c | sort -k1,1nr -k2,2 | awk 'NR == 1 { print $2 }'; } |
| 81 | : > "$W/rej"; : > "$W/cand" |
| 82 | for key in $(cut -d' ' -f1 "$W/groups" | sort -u); do |
| 83 | name="$(gname "$key")" |
| 84 | tasks="$(awk -v k="$key" '$1 == k { print $2 }' "$W/groups" | sort -u)" |
| 85 | tlist="$(printf '%s\n' "$tasks" | paste -sd, -)" |
| 86 | fp16=-; case "$key" in nonleaf:*) ;; *) fp16="${key:0:16}";; esac |
| 87 | rej() { echo "$name $fp16 $tlist $*" >> "$W/rej"; } |
| 88 | case "$key" in nonleaf:*) rej "calls $(awk -v k="$key" '$1 == k { print $4; exit }' "$W/groups"): v1 extracts leaf helpers only"; continue;; esac |
| 89 | nt="$(printf '%s\n' "$tasks" | wc -l)" |
| 90 | [ "$nt" -ge 2 ] || { rej "single-source: occurs in 1 extraction task"; continue; } |
| 91 | nd="$(for t in $tasks; do echo "${SOLSHA[$t]}"; done | sort -u | wc -l)" |
| 92 | [ "$nd" -ge 2 ] || { rej "single-source: the tasks share one solution digest"; continue; } |
| 93 | same="$(grep '^S ' "$W/scan" | awk -v f="$key" '$5 == f { print $2; exit }')" |
| 94 | [ -z "$same" ] || { rej "identical to the whole corpus solution of $same"; continue; } |
| 95 | echo "$key $name" >> "$W/cand" |
| 96 | done |
| 97 | # a name chosen by two surviving groups is ambiguous: both are rejected |
| 98 | for key in $(cut -d' ' -f1 "$W/cand"); do |
| 99 | name="$(awk -v k="$key" '$1 == k { print $2 }' "$W/cand")" |
| 100 | tlist="$(awk -v k="$key" '$1 == k { print $2 }' "$W/groups" | sort -u | paste -sd, -)" |
| 101 | rej() { echo "$name ${key:0:16} $tlist $*" >> "$W/rej"; } |
| 102 | [ "$(awk -v n="$name" '$2 == n' "$W/cand" | wc -l)" = 1 ] || { rej "name collision: another helper group has the name $name"; continue; } |
| 103 | spec="$GA_CORPUS/specs/$name.spec" |
| 104 | [ -f "$ROOT/$spec" ] || { rej "no spec file $spec (pre, post, example, limits are stated by the corpus author)"; continue; } |
| 105 | # representative body: the occurrence named <name> in the byte-smallest task |
| 106 | rep="$(awk -v k="$key" -v n="$name" '$1 == k && $3 == n { print $5; exit }' "$W/groups")" |
| 107 | id="abs-${key:0:16}" |
| 108 | if ! ga_abs_checks "$id" "$rep" "$ROOT/$spec" "$W/abs.$name"; then rej "$GA_ERR"; continue; fi |
| 109 | GA_RCPT+="$(awk -v k="$key" '$1 == k { print "occurs " $2 " " $3 }' "$W/groups")"$'\n' |
| 110 | rc="$(put_receipt)" |
| 111 | ga_params "$(head -n 1 "$rep")" |
| 112 | fams="$(for t in $(awk -v k="$key" '$1 == k { print $2 }' "$W/groups" | sort -u); do ga_meta_token "$t"; done | sort -u | paste -sd' ' -)" |
| 113 | { |
| 114 | echo "gen-record 1"; echo "id $id"; echo "kind abstraction"; echo "split corpus"; echo "family $fams" |
| 115 | echo "entry $name"; echo "params$GA_REQP"; echo "ret $(ga_req "$GA_RET")"; echo "signature $(head -n 1 "$rep")" |
| 116 | sed -n 's/^desc /desc /p' "$ROOT/$spec" |
| 117 | echo "receipt $rc"; echo "validity verified" |
| 118 | for t in $(awk -v k="$key" '$1 == k { print $2 }' "$W/groups" | sort -u); do echo "ancestry $t ${SOLSHA[$t]}"; done |
| 119 | { src_line "$GA_CORPUS/META"; src_line "$GA_CORPUS/PROBES"; src_line "$spec" |
| 120 | for t in $(awk -v k="$key" '$1 == k { print $2 }' "$W/groups" | sort -u); do src_line "$GA_CORPUS/tasks/$t/solution.omegese"; done; } | sort |
| 121 | sed -n 's/^pre /pre STATED+TESTED /p; s/^post /post STATED+TESTED /p' "$ROOT/$spec" |
| 122 | grep '^example ' "$ROOT/$spec"; grep '^limit ' "$ROOT/$spec"; grep '^counter ' "$ROOT/$spec" || true |
| 123 | echo "fingerprint $key" |
| 124 | grep '^S ' "$W/scan" | awk '{ print "differs " $2 " " $5 }' |
| 125 | echo "body"; cat "$rep" |
| 126 | } > "$OUT/abstraction/$id.rec" |
| 127 | done |
| 128 | sort "$W/rej" > "$OUT/REJECTIONS" |
| 129 | |
| 130 | # ---- 3. manifest -------------------------------------------------------------------------------------------- |
| 131 | (cd "$OUT" && sha256sum raw/*.rec abstraction/*.rec) > "$W/manifest" |
| 132 | LC_ALL=C sort -k2,2 "$W/manifest" > "$OUT/MANIFEST" |
| 133 | (cd "$OUT" && sha256sum MANIFEST) > "$OUT/SET.sha256" |
| 134 | |
| 135 | # ---- 4. the 14-task B prefix and the C/D candidate pools (docs/one_layer/GEN_PROTOCOL_V1.md section 3) ---- |
| 136 | # prefix pick: corpus tasks ordered by family signature (famsig canon, byte order) then task id; walk the |
| 137 | # families round robin in signature order, taking each family's lowest unused task id, until 14 are taken. |
| 138 | # C.MANIFEST = the raw records of the extraction tasks (split corpus) minus the prefix tasks: probe raw records |
| 139 | # are never eligible for retrieval (RECORD_SCHEMA.md section 2, genret CONTRACT.md section 5), so they are left |
| 140 | # out of the pool rather than listed and excluded later. D.MANIFEST = the abstraction records minus those whose |
| 141 | # ancestry lies entirely within the prefix. Both are MANIFEST lines (genret manifest format, same directory). |
| 142 | grep -v '^#' "$CR/META" | tail -n +2 | while read -r t _ _ _ _ _ _ _ s; do |
| 143 | echo "$("$GA_FAMSIG" canon "$s") $t" |
| 144 | done | sort > "$W/famorder" |
| 145 | awk -v want=14 '{ if (!($1 in n)) fam[nf++] = $1; id[$1, n[$1]++] = $2 } |
| 146 | END { for (r = 0; got < want; r++) { more = 0 |
| 147 | for (f = 0; f < nf && got < want; f++) if (r < n[fam[f]]) { print id[fam[f], r]; got++; more = 1 } |
| 148 | if (!more) break } }' "$W/famorder" > "$OUT/PREFIX" |
| 149 | [ "$(wc -l < "$OUT/PREFIX")" = 14 ] || die "fewer than 14 corpus tasks for the prefix" |
| 150 | awk 'NR == FNR { p["raw/raw-" $1 ".rec"] = 1; next } $2 ~ /^raw\// && !($2 in p)' "$OUT/PREFIX" "$OUT/MANIFEST" | |
| 151 | while read -r h f; do |
| 152 | if [ "$(sed -n '/^body$/q; s/^split //p' "$OUT/$f")" = corpus ]; then echo "$h $f"; fi |
| 153 | done > "$OUT/C.MANIFEST" |
| 154 | while read -r h f; do |
| 155 | case "$f" in abstraction/*) ;; *) continue;; esac |
| 156 | outside="$(sed -n '/^body$/q; s/^ancestry \([^ ]*\) .*/\1/p' "$OUT/$f" | grep -vxF -f "$OUT/PREFIX" || true)" |
| 157 | if [ -n "$outside" ]; then echo "$h $f"; fi |
| 158 | done < "$OUT/MANIFEST" > "$OUT/D.MANIFEST" |
| 159 | echo "extract: raw=$(ls "$OUT/raw" | wc -l) abstraction=$(ls "$OUT/abstraction" | wc -l) rejected=$(wc -l < "$OUT/REJECTIONS") set=$(cut -c1-64 "$OUT/SET.sha256")" |
| 160 | echo "extract: prefix=$(paste -sd, "$OUT/PREFIX") C=$(wc -l < "$OUT/C.MANIFEST") D=$(wc -l < "$OUT/D.MANIFEST")" |