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 / tests / physics0 / learner / pd0b_run.sh

99 lines · 6822 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# PD-0b run: the frozen learner against an external world binary, protocol v2 (play and truth from one binary).
18# usage: pd0b_run.sh <harness-binary> <world-binary> <seeds-file> [note]
19# seeds file: lines "level_index seed" (level_index 0..6 or null); blank lines and # comments ignored;
20# every level that appears must appear exactly five times. Writes ONE new receipt
21# evidence/physics0/pd0b/PD0B_RUN-<harness commit>-<world sha256[0:12]>.txt and refuses to overwrite.
22set -eu
23H=${1:?harness}; W=${2:?world binary}; S=${3:?seeds file}; NOTE=${4:-}
24export LC_ALL=C
25die() { echo "pd0b-run: $*" >&2; exit 2; }
26[ -x "$H" ] || die "harness binary not executable: $H"
27[ -x "$W" ] || die "PD0_WORLD_BIN not executable: $W"
28[ -f "$S" ] || die "PD0B_SEEDS not found: $S"
29lines=$(grep -v '^[[:space:]]*\(#\|$\)' "$S" || true)
30[ -n "$lines" ] || die "seeds file has no instances"
31echo "$lines" | awk '
32 NF != 2 { printf "pd0b-run: bad seeds line (need: level_index seed): %s\n", $0 > "/dev/stderr"; bad = 1; next }
33 $1 !~ /^([0-6]|null)$/ { printf "pd0b-run: bad level_index: %s\n", $1 > "/dev/stderr"; bad = 1; next }
34 $2 !~ /^[0-9]+$/ { printf "pd0b-run: bad seed: %s\n", $2 > "/dev/stderr"; bad = 1; next }
35 { n[$1]++ }
36 END { for (l in n) if (n[l] != 5) { printf "pd0b-run: level %s has %d instances, need exactly 5\n", l, n[l] > "/dev/stderr"; bad = 1 } exit bad }' || exit 2
37HEAD_FULL=$(git rev-parse HEAD); HEAD_SHORT=$(git rev-parse --short=12 HEAD)
38if [ -n "$(git status --porcelain --untracked-files=no)" ] && [ "${PD0B_ALLOW_DIRTY:-0}" != 1 ]; then die "worktree has uncommitted changes; commit first (or PD0B_ALLOW_DIRTY=1 for a rehearsal that is not filed)"; fi
39WSHA=$(sha256sum "$W" | cut -d' ' -f1); SSHA=$(sha256sum "$S" | cut -d' ' -f1)
40OUTDIR=evidence/physics0/pd0b; mkdir -p "$OUTDIR"
41OUT=$OUTDIR/PD0B_RUN-$HEAD_SHORT-$(echo "$WSHA" | cut -c1-12).txt
42[ ! -e "$OUT" ] || die "receipt already exists, refusing to overwrite: $OUT"
43LAWDIR=${OUT%.txt}-laws
44[ ! -e "$LAWDIR" ] || die "laws directory already exists, refusing to overwrite: $LAWDIR"
45LEDGER_ARCHIVE=${PD0B_LEDGER_ARCHIVE:-$HOME/workspace/evidence-archive/omega/physics0/pd0b}
46LDIR=$LEDGER_ARCHIVE/$(basename "${OUT%.txt}")-ledgers # ledgers are large: kept outside the repo, listed by hash in LEDGERS.sha256
47[ ! -e "$LDIR" ] || die "ledger archive directory already exists, refusing to overwrite: $LDIR"
48WORK=$(mktemp -d); trap 'rm -rf "$WORK"' EXIT
49mkdir -p "$WORK/out"; : > "$WORK/results.txt"
50echo "$lines" | while read -r lvl seed; do
51 line=$("$H" "$W" - "$lvl" "$seed" "$WORK/out") || { echo "pd0b-run: harness failed on level $lvl seed $seed" >&2; exit 3; }
52 echo "$line" >> "$WORK/results.txt"
53done
54grep "^PD0L " "$WORK/results.txt" > "$WORK/pd0l.txt" || true; grep "^PD0F " "$WORK/results.txt" > "$WORK/pd0f.txt" || true
55NINST=$(echo "$lines" | wc -l)
56[ "$(wc -l < "$WORK/pd0l.txt")" -eq "$NINST" ] || die "result count does not match instance count"
57VALID=VALID
58[ "$(wc -l < "$WORK/pd0f.txt")" -eq "$NINST" ] && [ "$(grep -c " valid=VALID$" "$WORK/pd0f.txt")" -eq "$NINST" ] || VALID=INVALID
59# per-instance summary with the retained law record: law=1 iff pd0_ladder_emit_law produced bytes and pd0_ladder_verify_law accepted them
60fld() { printf '%s\n' "$1" | tr ' ' '\n' | sed -n "s/^$2=//p" | head -1; }
61: > "$WORK/summary.txt"
62while read -r pl; do
63 lv=$(fld "$pl" level); sd=$(fld "$pl" seed); st=$(fld "$pl" state); lrc=$(fld "$pl" law); lf="$WORK/out/pd0l-$lv-s$sd.law"
64 if [ "$lrc" = 0 ] && [ -f "$lf" ]; then law=1; lsha=$(sha256sum "$lf" | cut -d' ' -f1); else law=0; lsha=-; fi
65 [ "$st" != REPLICATED ] || [ "$law" = 1 ] || VALID=INVALID
66 printf 'level=%s seed=%s state=%s code=%s score=%s score_code=%s law=%s law_sha256=%s\n' "$lv" "$sd" "$st" "$(fld "$pl" code)" "$(fld "$pl" score)" "$(fld "$pl" score_code)" "$law" "$lsha" >> "$WORK/summary.txt"
67done < "$WORK/pd0l.txt"
68( cd "$WORK/out" && for x in *.ledger; do [ -f "$x" ] && sha256sum "$x"; done ) > "$WORK/LEDGERS.sha256" || true
69{
70 echo "receipt: PD0B_RUN"
71 echo "run_validity: $VALID"
72 echo "validity_rule: VALID needs, per instance, a final accepted once, shape dimensions unchanged across final (and, in PD0 mode with the omega world, S* equal to the public table), an audit echoing the final hash, and every refusal counter 0; and law=1 (a law record emitted and accepted by the verifier) for every instance whose ladder_state is REPLICATED"
73 [ -z "$NOTE" ] || echo "note: $NOTE"
74 echo "date_utc: $(date -u +%Y-%m-%dT%H:%M:%SZ)"
75 echo "harness_commit: $HEAD_FULL"
76 echo "world_binary: $W"
77 echo "world_binary_sha256: $WSHA"
78 echo "seeds_file_sha256: $SSHA"
79 echo "ledger_archive: $LDIR"
80 echo "ledgers_manifest_sha256: $(sha256sum "$WORK/LEDGERS.sha256" | cut -d' ' -f1) (LEDGERS.sha256 in the laws directory)"
81 echo "mode: external world, PD0 protocol v2 (play and truth from the one world binary; no pd0-truth)"
82 echo "--- freeze (learner, ladder, scorer, controls)"
83 sh tests/physics0/learner/pd0b_freeze.sh "$H"
84 echo "--- seeds file"
85 echo "$lines"
86 echo "--- per instance: level seed ladder_state checker_code scorer_verdict scorer_code law law_sha256"
87 cat "$WORK/summary.txt"
88 echo "--- per instance: final bundle hash and the world refusal counters (protocol v2 rev 2)"
89 sed "s/^PD0F //" "$WORK/pd0f.txt"
90 echo "--- state count per level"
91 awk '{ for (i = 1; i <= NF; i++) { split($i, kv, "="); v[kv[1]] = kv[2] } c[v["level"] " " v["state"] " score=" v["score"]]++ } END { for (k in c) printf "%s x%d\n", k, c[k] }' "$WORK/pd0l.txt" | sort
92 echo "--- raw harness lines"
93 cat "$WORK/pd0l.txt"
94} > "$WORK/receipt.txt"
95ln "$WORK/receipt.txt" "$OUT" || die "could not create $OUT without overwriting"
96mkdir "$LAWDIR" && for x in "$WORK"/out/*; do case "$x" in *.ledger) ;; *) cp "$x" "$LAWDIR"/ ;; esac; done && cp "$WORK/LEDGERS.sha256" "$LAWDIR/LEDGERS.sha256" || die "could not retain the law and json files in $LAWDIR"
97mkdir -p "$LDIR" && cp "$WORK"/out/*.ledger "$LDIR"/ || die "could not archive the ledgers in $LDIR"
98echo "pd0b-run: wrote $OUT (law and json in $LAWDIR, ledgers in $LDIR)"
99[ "$VALID" = VALID ] || { echo "pd0b-run: run is INVALID (see receipt)" >&2; exit 4; }