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 / tools / estimation / test_est4_tools.sh

135 lines · 9504 bytes Raw Blame History
1#!/bin/sh
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# ESTIMATION v4 tool tests (synthetic data only; never opens D1 or D2).
18# tools/estimation/test_est4_tools.sh <est4-test-build> <workdir>
19# The test build (-DE4_TEST_BUILD) takes the dirty flag, protocol SHA and the
20# D1/D2/params identities from E4_TEST_* environment variables.
21set -u
22T=${1:?test build}
23W=${2:?workdir}
24pass=0; fail=0
25ok() { pass=$((pass + 1)); }
26bad() { fail=$((fail + 1)); echo "FAIL: $*" >&2; }
27# expect <rc> <stderr-substring> <cmd...>
28expect() {
29 want=$1; pat=$2; shift 2
30 "$@" > "$W/out.txt" 2> "$W/err.txt"; rc=$?
31 if [ "$rc" -ne "$want" ]; then bad "rc $rc (want $want): $*"; cat "$W/err.txt" >&2; return; fi
32 if [ -n "$pat" ] && ! grep -q -- "$pat" "$W/err.txt" "$W/out.txt"; then bad "missing '$pat': $*"; cat "$W/err.txt" >&2; return; fi
33 ok
34}
35rm -rf "$W"; mkdir -p "$W"
36S=$W/syn-a; S2=$W/syn-b
37expect 0 "" "$T" make-synthetic "$S" --seed 7 --lines 2400
38expect 0 "" "$T" make-synthetic "$S2" --seed 11 --lines 2400
39expect 0 "VALID" "$T" precheck --dir "$S2"
40
41# 1. synthetic fit runs, fast and pmf scores agree for every family
42expect 0 "EST4_FIT" "$T" fit --raw "$S/machine-state.ndjson" --marks "$S/machine-state-marks.txt" --out "$W/p.txt" --synthetic-test --grid-stride 3
43for g in G1 G2 G3; do
44 if grep -q "^family $g available 1 .* agree 1$" "$W/p.txt"; then ok; else bad "family $g not available+agreeing"; fi
45done
46grep -q '^phase_a ' "$W/p.txt" && ok || bad "no phase_a line"
47grep -q '^e0_count ' "$W/p.txt" && ok || bad "no E0 histogram"
48grep -q '^baseline F1 available 1 ' "$W/p.txt" && ok || bad "no F1 baseline"
49# never overwrites
50expect 1 "never overwritten" "$T" fit --raw "$S/machine-state.ndjson" --marks "$S/machine-state-marks.txt" --out "$W/p.txt" --synthetic-test --grid-stride 3
51
52# 2. held-out guard: refused before any file is opened (paths do not exist)
53for tag in est4-heldout est3c-heldout; do
54 H=$W/x-$tag-silicon
55 expect 2 "held-out" "$T" fit --raw "$H/machine-state.ndjson" --marks "$S/machine-state-marks.txt" --out "$W/h.txt" --synthetic-test
56 expect 2 "held-out" "$T" fit --raw "$S/machine-state.ndjson" --marks "$H/machine-state-marks.txt" --out "$W/h.txt" --synthetic-test
57done
58# a symlink whose target carries the tag is refused too
59mkdir -p "$W/r-est4-heldout-silicon"; cp "$S"/* "$W/r-est4-heldout-silicon/"; ln -s "$(cd "$W" && pwd)/r-est4-heldout-silicon" "$W/innocent"
60expect 2 "held-out" "$T" fit --raw "$W/innocent/machine-state.ndjson" --marks "$W/innocent/machine-state-marks.txt" --out "$W/h2.txt" --synthetic-test
61
62# 3. purity: a fit opens only its own inputs (strace, when available)
63if command -v strace > /dev/null 2>&1; then
64 strace -f -e trace=open,openat -o "$W/trace.txt" "$T" fit --raw "$S/machine-state.ndjson" --marks "$S/machine-state-marks.txt" --out "$W/p2.txt" --synthetic-test --grid-stride 5 > /dev/null 2>&1
65 if grep -E 'heldout|syn-b|-est4-fit-|est3c-fit' "$W/trace.txt" > /dev/null; then bad "fit opened a foreign data path"; else ok; fi
66 if grep -q 'syn-a/machine-state.ndjson' "$W/trace.txt"; then ok; else bad "strace saw no input open"; fi
67 strace -f -e trace=open,openat -o "$W/trace2.txt" "$T" fit --raw "$W/innocent/machine-state.ndjson" --marks "$W/innocent/machine-state-marks.txt" --out "$W/h3.txt" --synthetic-test > /dev/null 2>&1
68 if grep -E 'machine-state' "$W/trace2.txt" > /dev/null; then bad "held-out refusal opened data"; else ok; fi
69else
70 echo "test_est4_tools: strace absent, open-trace purity skipped" >&2
71fi
72
73# 4. binding-mode refusals
74F1=$W/d-est4-fit-silicon; mkdir -p "$F1"; cp "$S"/* "$F1/"
75RS=$(sha256sum "$F1/machine-state.ndjson" | cut -c1-64); MS=$(sha256sum "$F1/machine-state-marks.txt" | cut -c1-64)
76PS=$(sha256sum docs/estimation/protocols/est-v4.md 2>/dev/null | cut -c1-64)
77fitb() { "$T" fit --raw "$1/machine-state.ndjson" --marks "$1/machine-state-marks.txt" --out "$2"; }
78expect 2 "--grid-stride only" "$T" fit --raw "$F1/machine-state.ndjson" --marks "$F1/machine-state-marks.txt" --out "$W/b.txt" --grid-stride 3
79expect 2 "clean tree" env E4_TEST_DIRTY=1 "$T" fit --raw "$F1/machine-state.ndjson" --marks "$F1/machine-state-marks.txt" --out "$W/b.txt"
80expect 2 "protocol SHA" env E4_TEST_DIRTY=0 E4_TEST_PROTOCOL_SHA=0000 "$T" fit --raw "$F1/machine-state.ndjson" --marks "$F1/machine-state-marks.txt" --out "$W/b.txt"
81if [ -n "$PS" ]; then
82 expect 2 "no D1 identity" env E4_TEST_DIRTY=0 E4_TEST_PROTOCOL_SHA="$PS" "$T" fit --raw "$F1/machine-state.ndjson" --marks "$F1/machine-state-marks.txt" --out "$W/b.txt"
83 expect 2 "-est4-fit-" env E4_TEST_DIRTY=0 E4_TEST_PROTOCOL_SHA="$PS" E4_TEST_D1_RAW_SHA="$RS" E4_TEST_D1_MARKS_SHA="$MS" "$T" fit --raw "$S/machine-state.ndjson" --marks "$S/machine-state-marks.txt" --out "$W/b.txt"
84 expect 2 "differs from the compiled D1" env E4_TEST_DIRTY=0 E4_TEST_PROTOCOL_SHA="$PS" E4_TEST_D1_RAW_SHA="$MS" E4_TEST_D1_MARKS_SHA="$MS" "$T" fit --raw "$F1/machine-state.ndjson" --marks "$F1/machine-state-marks.txt" --out "$W/b.txt"
85 [ ! -e "$W/b.txt" ] && ok || bad "refused binding fit left an output"
86else
87 echo "test_est4_tools: protocol doc absent, binding identity refusals skipped" >&2
88fi
89
90# 5. recorded: refuses PHASE_A_FAIL params, a held-out dir in synthetic mode, binding without D2 identity
91mkdir -p "$W/rc"
92if grep -q '^phase_a PHASE_A_FAIL' "$W/p.txt"; then
93 expect 2 "NOT_RUN" "$T" recorded --dir "$S2" --params "$W/p.txt" --outdir "$W/rc" --synthetic-test
94fi
95expect 2 "no D2 identity" env E4_TEST_DIRTY=0 "$T" recorded --dir "$S2" --params "$W/p.txt" --outdir "$W/rc"
96expect 2 "never runs on D1" "$T" recorded --dir "$W/r-est4-heldout-silicon" --params "$W/p.txt" --outdir "$W/rc" --synthetic-test
97# a params file marked as a G1 screen pass exercises the scored path end to end
98sed -e 's/^screen_result G1 pass 0/screen_result G1 pass 1/' -e 's/^selected .*/selected G1/' -e 's/^phase_a .*/phase_a PASS/' "$W/p.txt" > "$W/pp.txt"
99expect 0 "EST4_RECORDED" "$T" recorded --dir "$S2" --params "$W/pp.txt" --outdir "$W/rc" --synthetic-test
100n=$(ls "$W/rc" | grep -c '^receipt-.*\.json$'); [ "$n" -eq 1 ] && ok || bad "want one receipt, have $n"
101r=$(ls "$W/rc"/receipt-*.json | head -1); h=$(sha256sum "$r" | cut -c1-64)
102case "$r" in *"receipt-$h.json") ok ;; *) bad "receipt name is not its SHA-256" ;; esac
103grep -q '"same_steps": true' "$r" && ok || bad "S, F1, E0 not on the same steps"
104expect 2 "already holds a receipt" "$T" recorded --dir "$S2" --params "$W/pp.txt" --outdir "$W/rc" --synthetic-test
105# selection mismatch is refused
106sed -e 's/^selected .*/selected G3/' "$W/pp.txt" > "$W/pm.txt"; mkdir -p "$W/rc2"
107expect 2 "the rule gives" "$T" recorded --dir "$S2" --params "$W/pm.txt" --outdir "$W/rc2" --synthetic-test
108
109
110# 6. binding recorded end to end (identities from the environment), and the
111# synthetic path refusing D2 bytes under another name
112if [ -n "$PS" ]; then
113 RD=$W/repo; RO=$RD/docs/estimation/receipts/est-v4; mkdir -p "$RO"
114 D2=$W/z-est4-heldout-silicon; mkdir -p "$D2"; cp "$S2"/* "$D2/"
115 R2=$(sha256sum "$D2/machine-state.ndjson" | cut -c1-64); M2=$(sha256sum "$D2/machine-state-marks.txt" | cut -c1-64); C2=$(sha256sum "$D2/schedule.txt" | cut -c1-64)
116 mkpb() { sed -e 's/^synthetic_test .*/synthetic_test 0/' -e 's/^tool_dirty .*/tool_dirty 0/' -e "s/^protocol_doc_sha256 .*/protocol_doc_sha256 $1/" \
117 -e "s/^fit_raw_sha256 .*/fit_raw_sha256 $RS/" -e "s/^fit_marks_sha256 .*/fit_marks_sha256 $MS/" \
118 -e "s|^fit_raw_path .*|fit_raw_path $F1/machine-state.ndjson|" "$W/pp.txt" > "$2"; }
119 mkpb "$PS" "$W/pb.txt"; mkpb 1111 "$W/pbx.txt"
120 rec() { env E4_TEST_DIRTY=0 E4_TEST_PROTOCOL_SHA="$PS" E4_TEST_D1_RAW_SHA="$RS" E4_TEST_D1_MARKS_SHA="$MS" \
121 E4_TEST_D2_RAW_SHA="$R2" E4_TEST_D2_MARKS_SHA="$M2" E4_TEST_D2_SCHED_SHA="$C2" \
122 E4_TEST_PARAMS_SHA="$(sha256sum "$1" | cut -c1-64)" E4_TEST_REPO_ROOT="$RD" \
123 "$T" recorded --dir "$D2" --params "$1" --outdir "$RO"; }
124 expect 2 "another protocol SHA" rec "$W/pbx.txt"
125 expect 2 "receipt goes only" env E4_TEST_DIRTY=0 E4_TEST_PROTOCOL_SHA="$PS" E4_TEST_D2_RAW_SHA="$R2" E4_TEST_D2_MARKS_SHA="$M2" E4_TEST_D2_SCHED_SHA="$C2" \
126 E4_TEST_PARAMS_SHA="$(sha256sum "$W/pb.txt" | cut -c1-64)" E4_TEST_REPO_ROOT="$RD" "$T" recorded --dir "$D2" --params "$W/pb.txt" --outdir "$W/rc2"
127 expect 0 "EST4_RECORDED" rec "$W/pb.txt"
128 n=$(ls "$RO" | grep -c '^receipt-.*\.json$'); [ "$n" -eq 1 ] && ok || bad "binding: want one receipt, have $n"
129 grep -q "\"protocol_sha256\": \"$PS\"" "$RO"/receipt-*.json && ok || bad "binding receipt lacks the protocol SHA"
130 expect 2 "already holds a receipt" rec "$W/pb.txt"
131 mkdir -p "$W/rc3"
132 expect 2 "(D2 bytes)" env E4_TEST_D2_RAW_SHA="$R2" "$T" recorded --dir "$S2" --params "$W/pp.txt" --outdir "$W/rc3" --synthetic-test
133fi
134echo "test_est4_tools: $pass passed, $fail failed"
135[ "$fail" -eq 0 ]