interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / calibration / scripts / verify_holdout_separation.sh

240 lines · 13915 bytes Raw Blame History
1#!/usr/bin/env bash
2# SPDX-License-Identifier: Apache-2.0
3# Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Turing calibration (CAL-0 / EXP-001, EXP-001R): prove the sealed test data is separate
18# from all development data, and was made after the freeze. Writes overlap_audit.json.
19#
20# usage: [EXP_ID=EXP-001R] verify_holdout_separation.sh --sealed-dir DIR --out FILE [--dev-list FILE] [--burned-dir DIR]... [--repo DIR]
21#
22# --sealed-dir ~/aien-data/turing-cal/sealed/<commit> (written by generate_sealed_data.sh)
23# --out where overlap_audit.json goes (normally
24# calibration/experiments/EXP-001/overlap_audit.json)
25# --dev-list text file, one development trace.ctr path per line. Default: every
26# trace.ctr under ~/aien-data/crumbline (all burned development data,
27# both conditions, all runs). Each trace.ctr's sibling ledger.jsonl
28# is used for the ledger checks.
29# --burned-dir (repeatable) a sealed root of a finished experiment: its trace.ctr files (and sibling
30# ledgers) join the dev/burned comparison list. EXP_ID=EXP-001R adds the EXP-001 sealed
31# root by default (TC_EXP001_SEALED overrides its location). Added to --dev-list too.
32# EXP_ID selects the experiment (default EXP-001; see tc_exp.sh). EXP-001 keeps the original gates. EXP-001R
33# uses the amended G6 and refuses the six EXP-001 sealed seeds in G3.
34# --repo git repository (worktree or .git directory) holding the freeze
35# commit; default: the checkout holding this script. Needed inside
36# the Auditor jail, where the script runs from a git-archive export.
37# The overlap helper is build/turing-cal/turing-cal-overlap in the tree holding this
38# script (override: TC_OVERLAP_TOOL); in a read-only export it must be prebuilt.
39#
40# Gates (all must hold; exit 0 = PASS, 1 = FAIL, 2 = could not run):
41# G1 complete COMPLETE exists and equals SHA-256(seed_commitment.json)
42# G2 integrity every kept file in manifest.json has its recorded SHA-256
43# G3 seeds every seed re-derives from the rule, none is burned (0..10, 20260927; EXP-001R also
44# the six EXP-001 sealed seeds)
45# G4 after_freeze every sealed file's mtime is later than the freeze commit's committer time, and the
46# candidate manifest at that commit is "status": "frozen" (the commit is C_f)
47# G5 single_link every sealed file has exactly one hard link
48# G6 crumb_digest no ledger crumb_digest shared between dev/burned and sealed. EXP-001R amendment: a
49# shared crumb_digest is EXEMPT only when the sealed record population is Ambiguous AND its
50# sealed_digest differs from the sealed_digest of every dev/burned record carrying that
51# crumb_digest (crumb_digest hashes only the visible part; the hidden held-out set differs).
52# Exempt matches are counted and listed in overlap_audit.json (g6_exempt_matches).
53# G7 sealed_digest no ledger sealed_digest (hidden held-out set) shared
54# G8 trace_stream no ledger trace_stream_digest shared, except degenerate stream
55# digests (a digest repeated across distinct crumbs inside ONE
56# ledger file is a content-empty stream; these are listed, not hidden)
57# G9 crumb_block no crumb whose CTR1 record-body block (bytes 0..214 of every
58# record, SHA-256) occurs on both sides (tools/turing_cal_overlap.c)
59# Reported, not gated: first_state_overlap, record_body_overlap (see the C tool).
60set -euo pipefail
61here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
62toolrepo="$(cd "$here/../.." && pwd)"
63. "$here/tc_exp.sh"
64repo="$(git -C "$here" rev-parse --show-toplevel 2>/dev/null || true)"
65sealed="" out="" devlist=""
66burned_dirs=()
67die() { echo "verify_holdout_separation: $*" >&2; exit 2; }
68while [ $# -gt 0 ]; do
69 case "$1" in
70 --sealed-dir) sealed="$2"; shift 2 ;;
71 --out) out="$2"; shift 2 ;;
72 --dev-list) devlist="$2"; shift 2 ;;
73 --burned-dir) burned_dirs+=("$2"); shift 2 ;;
74 --repo) repo="$2"; shift 2 ;;
75 *) die "unknown option '$1'" ;;
76 esac
77done
78[ -d "$sealed" ] && [ -n "$out" ] || die "--sealed-dir DIR and --out FILE are required"
79sha() { sha256sum -- "$1" | cut -d' ' -f1; }
80work="$(mktemp -d)"
81trap 'rm -rf "$work"' EXIT
82
83tool="${TC_OVERLAP_TOOL:-$toolrepo/build/turing-cal/turing-cal-overlap}"
84[ -x "$tool" ] || make -s -C "$toolrepo" turing-cal-overlap >/dev/null 2>&1 || die "turing-cal-overlap not built at $tool (build it before binding a read-only tree)"
85[ -n "$repo" ] || die "--repo is required when this script is not inside a git checkout"
86
87sc="$sealed/seed_commitment.json"
88[ -f "$sc" ] && [ -f "$sealed/manifest.json" ] || die "seed_commitment.json / manifest.json missing"
89jstr() { sed -n "s/^ \"$1\": \"\([^\"]*\)\".*/\1/p" "$sc" | head -1; }
90commit="$(jstr freeze_commit)"
91digest="$(jstr profile_digest)"
92[[ "$commit" =~ ^[0-9a-f]{40}$ ]] || die "bad freeze_commit in seed_commitment.json"
93ctime="$(git -C "$repo" show -s --format=%ct "$commit")" || die "freeze commit $commit not in $repo"
94
95fails=()
96gate() { # name ok(0/1) detail
97 printf ' "%s": {"pass": %s, "detail": "%s"}' "$1" "$([ "$2" = 1 ] && echo true || echo false)" "$3"
98 [ "$2" = 1 ] || fails+=("$1")
99}
100
101# G1
102g1=0
103[ -f "$sealed/COMPLETE" ] && [ "$(cat "$sealed/COMPLETE")" = "$(sha "$sc")" ] && g1=1
104
105# G2
106g2=1 nchk=0
107while IFS=$'\t' read -r p h; do
108 nchk=$((nchk + 1))
109 [ -f "$sealed/$p" ] && [ "$(sha "$sealed/$p")" = "$h" ] || { g2=0; echo "integrity: $p" >&2; }
110done < <(sed -n 's/.*"path": "\([^"]*\)", "sha256": "\([0-9a-f]*\)".*"kept": true}.*/\1\t\2/p' "$sealed/manifest.json")
111[ "$nchk" -gt 0 ] || g2=0
112
113# G3
114g3=1 nseed=0
115while read -r g j s; do
116 nseed=$((nseed + 1))
117 h="$(printf '%s' "turing.cal.sealed.v1|$commit|$digest|g$g|$j" | sha256sum | cut -c1-16)"
118 want="$((16#$(printf '%x' $((16#${h:0:1} & 7)))${h:1}))"
119 [ "$want" = "$s" ] || { g3=0; echo "seed rule mismatch g$g/$j" >&2; }
120 { [ "$s" -le 10 ] || [ "$s" = 20260927 ]; } && g3=0
121 if [ -n "$EXP_BURNED_FILE" ] && grep -qx -- "$s" "$here/../../$EXP_BURNED_FILE"; then g3=0; echo "burned EXP-001 seed g$g/$j" >&2; fi
122done < <(sed -n 's/.*"group": \([0-9]*\), "index": \([0-9]*\), "seed": \([0-9]*\)}.*/\1 \2 \3/p' "$sc")
123[ "$nseed" -gt 0 ] || g3=0
124
125# G4 / G5
126early="$(find "$sealed" -type f ! -newermt "@$ctime" | wc -l)"
127multi="$(find "$sealed" -type f -links +1 | wc -l)"
128# The seed commitment must name the freeze commit C_f: the candidate manifest there says "status": "frozen".
129frozen=0
130git -C "$repo" show "$commit:$EXP_DIR/candidate_manifest.json" 2>/dev/null | grep -q "^ \"status\": \"frozen\"," && frozen=1
131g4=$([ "$early" = 0 ] && [ "$frozen" = 1 ] && echo 1 || echo 0)
132g5=$([ "$multi" = 0 ] && echo 1 || echo 0)
133
134# Dev set
135if [ -z "$devlist" ]; then
136 devlist="$work/dev.txt"
137 find "$HOME/aien-data/crumbline" -name trace.ctr -type f | LC_ALL=C sort >"$devlist"
138fi
139# Burned sealed roots of finished experiments (EXP-001R: the EXP-001 sealed root) join the dev/burned list.
140[ -n "$EXP_BURNED_SEALED" ] && [ -d "$EXP_BURNED_SEALED" ] && [ "$EXP_BURNED_SEALED" != "$sealed" ] && burned_dirs+=("$EXP_BURNED_SEALED")
141if [ "$EXP_G6_RULE" = amended ] && [ -n "$EXP_BURNED_SEALED" ] && [ ! -d "$EXP_BURNED_SEALED" ]; then
142 die "EXP-001 sealed root $EXP_BURNED_SEALED missing: the burned traces must be in the comparison list"
143fi
144if [ "${#burned_dirs[@]}" -gt 0 ]; then
145 for bd in "${burned_dirs[@]}"; do [ -d "$bd" ] || die "--burned-dir $bd is not a directory"; done
146 { cat "$devlist"; for bd in "${burned_dirs[@]}"; do find "$bd" -name trace.ctr -type f; done; } | LC_ALL=C sort -u >"$work/dev_all.txt"
147 devlist="$work/dev_all.txt"
148fi
149find "$sealed" -name trace.ctr -type f | LC_ALL=C sort >"$work/sealed.txt"
150ledgers() { while IFS= read -r t; do l="$(dirname "$t")/ledger.jsonl"; [ -f "$l" ] && echo "$l"; done <"$1"; }
151ledgers "$devlist" >"$work/dev_ledgers.txt"
152ledgers "$work/sealed.txt" >"$work/sealed_ledgers.txt"
153[ -s "$work/dev_ledgers.txt" ] && [ -s "$work/sealed_ledgers.txt" ] || die "no ledgers found"
154keys() { # key listfile -> sorted unique values
155 xargs -d '\n' grep -ho "\"$1\":\"[0-9a-f]*\"" <"$2" | sed 's/.*:"\([0-9a-f]*\)"/\1/' | LC_ALL=C sort -u
156}
157shared() { LC_ALL=C comm -12 <(keys "$1" "$work/dev_ledgers.txt") <(keys "$1" "$work/sealed_ledgers.txt"); }
158# G6. Strict (EXP-001): any shared crumb_digest fails. Amended (EXP-001R): a shared crumb_digest is exempt only when
159# every sealed record carrying it is population Ambiguous AND its sealed_digest is not the sealed_digest of any
160# dev/burned record carrying that crumb_digest. No numeric tolerance.
161recs() { # ledger-list -> "crumb_digest<TAB>population<TAB>sealed_digest" per record
162 xargs -d '\n' cat <"$1" | awk '{
163 c = p = s = ""
164 if (match($0, /"crumb_digest":"[0-9a-f]*"/)) c = substr($0, RSTART + 16, RLENGTH - 17)
165 if (match($0, /"population":"[A-Za-z_]*"/)) p = substr($0, RSTART + 14, RLENGTH - 15)
166 if (match($0, /"sealed_digest":"[0-9a-f]*"/)) s = substr($0, RSTART + 16, RLENGTH - 17)
167 if (c != "") print c "\t" p "\t" s }' | LC_ALL=C sort -u
168}
169recs "$work/dev_ledgers.txt" >"$work/dev_recs.tsv"
170recs "$work/sealed_ledgers.txt" >"$work/sealed_recs.tsv"
171awk -F'\t' -v rule="$EXP_G6_RULE" '
172 NR == FNR { devc[$1] = 1; devs[$1, $3] = 1; dl[$1] = dl[$1] (dl[$1] == "" ? "" : ",") $3; next }
173 ($1 in devc) {
174 shared[$1] = 1
175 if (rule == "amended" && $2 == "Ambiguous" && !(($1, $3) in devs)) { ex[$1 "\t" $2 "\t" $3 "\t" dl[$1]] = $1 } else bad[$1] = 1
176 }
177 END {
178 ns = 0; nb = 0; ne = 0
179 for (c in shared) { ns++; if (c in bad) nb++; else ne++ }
180 print "COUNTS\t" ns "\t" nb "\t" ne
181 for (k in ex) if (!(ex[k] in bad)) print "EXEMPT\t" k
182 }' "$work/dev_recs.tsv" "$work/sealed_recs.tsv" | LC_ALL=C sort >"$work/g6.txt"
183read -r _ n6shared n6 n6exempt < <(grep '^COUNTS' "$work/g6.txt" | tr '\t' ' ')
184grep '^EXEMPT' "$work/g6.txt" | cut -f2- >"$work/g6_exempt.tsv" || true
185n7="$(shared sealed_digest | wc -l)"
186# Degenerate stream digests: repeated inside one ledger file (distinct crumbs, same stream).
187cat "$work/dev_ledgers.txt" "$work/sealed_ledgers.txt" | while IFS= read -r l; do
188 grep -o '"trace_stream_digest":"[0-9a-f]*"' "$l" | LC_ALL=C sort | uniq -d
189done | sed 's/.*:"\([0-9a-f]*\)"/\1/' | LC_ALL=C sort -u >"$work/degenerate.txt"
190shared trace_stream_digest | LC_ALL=C comm -23 - "$work/degenerate.txt" >"$work/ts_shared.txt"
191n8="$(wc -l <"$work/ts_shared.txt")"
192degen="$(paste -sd, "$work/degenerate.txt")"
193
194# G9
195set +e
196ctr1="$("$tool" "$devlist" "$work/sealed.txt")"
197trc=$?
198set -e
199[ "$trc" -le 1 ] || die "turing-cal-overlap failed (rc $trc)"
200num() { printf '%s' "$ctr1" | sed -n "s/.*\"$1\":\([0-9]*\).*/\1/p"; }
201n9="$(num crumb_block_overlap)"
202
203mkdir -p "$(dirname "$out")"
204{
205 echo "{"
206 echo " \"schema\": \"turing.cal.overlap_audit.v1\","
207 echo " \"freeze_commit\": \"$commit\","
208 echo " \"freeze_commit_time_unix\": $ctime,"
209 echo " \"profile_digest\": \"$digest\","
210 echo " \"seed_commitment_sha256\": \"$(sha "$sc")\","
211 echo " \"dev_list_sha256\": \"$(sha "$devlist")\","
212 echo " \"dev_trace_files\": $(wc -l <"$devlist"),"
213 echo " \"sealed_trace_files\": $(wc -l <"$work/sealed.txt"),"
214 echo " \"degenerate_trace_stream_digests\": \"$degen\","
215 echo " \"ctr1\": $ctr1,"
216 if [ "$EXP_G6_RULE" = amended ]; then
217 echo " \"g6_rule\": \"amended: a shared crumb_digest is exempt only if every sealed record carrying it is Ambiguous and its sealed_digest differs from every dev/burned sealed_digest carrying that crumb_digest\","
218 echo " \"g6_exempt_matches\": ["
219 awk -F'\t' 'NF >= 4 { printf "%s {\"crumb_digest\": \"%s\", \"population\": \"%s\", \"sealed_digest\": \"%s\", \"dev_or_burned_sealed_digests\": \"%s\"}", (n++ ? ",\n" : ""), $1, $2, $3, $4 } END { if (n) print "" }' "$work/g6_exempt.tsv"
220 echo " ],"
221 fi
222 echo " \"gates\": {"
223 gate complete "$g1" "COMPLETE matches seed_commitment.json"; echo ","
224 gate integrity "$g2" "$nchk kept files re-hashed"; echo ","
225 gate seeds "$g3" "$nseed seeds re-derived"; echo ","
226 gate after_freeze "$g4" "$early files not newer than commit time; manifest frozen at commit: $frozen"; echo ","
227 gate single_link "$g5" "$multi files with extra hard links"; echo ","
228 if [ "$EXP_G6_RULE" = amended ]; then g6d="$n6shared shared, $n6exempt exempt (Ambiguous, different hidden set)"; else g6d="$n6 shared"; fi
229 gate crumb_digest "$([ "$n6" = 0 ] && echo 1 || echo 0)" "$g6d"; echo ","
230 gate sealed_digest "$([ "$n7" = 0 ] && echo 1 || echo 0)" "$n7 shared"; echo ","
231 gate trace_stream "$([ "$n8" = 0 ] && echo 1 || echo 0)" "$n8 shared (degenerate excluded)"; echo ","
232 gate crumb_block "$([ "$n9" = 0 ] && echo 1 || echo 0)" "$n9 shared"; echo
233 echo " },"
234 echo " \"result\": \"$([ ${#fails[@]} = 0 ] && echo PASS || echo FAIL)\","
235 echo " \"failed\": \"${fails[*]:-}\","
236 echo " \"generated_utc\": \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\""
237 echo "}"
238} >"$out"
239echo "overlap audit: $([ ${#fails[@]} = 0 ] && echo PASS || echo "FAIL (${fails[*]})") -> $out"
240[ ${#fails[@]} = 0 ]