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 / exp445 / run.sh

248 lines · 17361 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# tools/exp445/run.sh -- generation driver of the machine-first representation experiment (omega#445,
18# docs/one_layer/EXP445_PROTOCOL.md, revision r3 = section 13). Shell + curl + jq + the repo's C tools; no Python.
19#
20# run.sh --config A --tasks <public tasks dir> --out <run dir> [--prompt-mode many|one]
21# [--futility <score dir>/futility.txt] [--base-url URL] [--model NAME] [--arms omegese,text]
22# [--only id,id] [--attempts N] [--max-tokens N] [--temperature T] [--top-p P] [--seed-base S]
23#
24# <public tasks dir> holds tasks/cases/<id>/{requirement,prompt} (ola.sh tasks-public output); the
25# driver never sees hidden data. Defaults come from tools/exp445/PARAMS (frozen by the protocol; EXP445_PARAMS
26# names another file, for tests only), including the configuration's model, endpoint, request extra and
27# server; any override is recorded in meta.txt as protocol=OVERRIDDEN. --prompt-mode picks one of PARAMS
28# prompt_modes (default heldout_prompt_mode) and is recorded in meta.txt as prompt_mode; a held-out run
29# must use heldout_prompt_mode. Refused: a PARAMS that is not revision r3, a configuration marked
30# unavailable (config.<X>.unavailable, with its reason), an arm other than omegese and text (r3 drops bytes
31# and structured), a held-out run while the configuration's server is still a PLACEHOLDER.
32# Order (13.4): attempt, then task, then arm. For every generation:
33# 1 system prompt = reader.sh system_prompt (arms/common.md + the arm document + the worked examples of
34# the prompt mode); user prompt = signature + task prompt
35# 2 POST <base-url>/chat/completions (max_tokens = the per-round cap), raw request and response saved
36# 3 payload = first fenced block; the arm's own reader turns it into a candidate and checks it with
37# public data only (text: OSC compiler; omegese: tools/omegese then omega_emit,
38# docs/one_layer/OMEGESE.md), then entry/params/ret and the back end (x445 check)
39# 4 if refused: exactly one repair round with the same message format in every arm; the last
40# round's answer is final
41# 5 candidate copied to cands/<set_id>/<task>/candidate.osc|oscir, set_id = <config>-<arm>-<attempt>
42# Before generating, <out> must hold weights.check and server.json from check_weights.sh (all OK) and a G0
43# PASS (g0.sh) for this configuration, server and PARAMS, with its PASS line on the G0 ledger; a G0 FAIL on the
44# ledger for this configuration's identity (reader.sh g0_ident) refuses the run, checked again before each attempt.
45# Stage 1 stop rule (13.4): on a held-out run, after attempt 1 the driver stops unless --futility names the
46# futility.txt that `score.sh --interim` wrote for this run's attempt-1 rows with decision=CONTINUE; with
47# decision=STOP it runs nothing more.
48# Each round is committed as soon as it is received (rN.req.json, rN.transport with HTTP status, curl
49# exit and time per try, rN.resp.json or rN.err.K.json); log.tsv rows are derived from those raw
50# files (reader.sh gen_row). Writes <out>/meta.txt, prompts/, gen/<set_id>/<task>/, cands/, log.tsv and
51# SHA256SUMS. Re-running with the same --out resumes: finished generations are kept, a committed round
52# is never requested again, and a row already in log.tsv is not appended twice.
53set -euo pipefail
54HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
55ROOT="$(cd "$HERE/../.." && pwd)"
56die() { echo "run.sh: $*" >&2; exit 2; }
57PARAMS="${EXP445_PARAMS:-$HERE/PARAMS}"
58[ -f "$PARAMS" ] || die "no PARAMS file $PARAMS"
59kv() { awk -F= -v k="$1" '$1 == k { print substr($0, length(k) + 2); exit }' "$PARAMS"; }
60command -v jq > /dev/null || die "jq is required"
61command -v curl > /dev/null || die "curl is required"
62. "$HERE/reader.sh"
63[ "$(kv revision)" = r3 ] || die "$PARAMS is not protocol revision r3: r1/r2 generation is retired (protocol section 13)"
64
65BASE="" MODEL="" CONFIG="" TASKS="" OUT="" ARMS="$(kv arms)" ONLY="" over="" MODE="$(kv heldout_prompt_mode)" FUT=""
66ATTEMPTS="$(kv attempts)" MAXT="$(kv max_tokens)" TEMP="$(kv temperature)" TOPP="$(kv top_p)" SEED="$(kv seed_base)"
67TIMEOUT="$(kv timeout_s)" RETRIES="$(kv retries)"
68while [ $# -gt 0 ]; do
69 case "$1" in
70 --base-url) BASE="${2%/}"; shift 2 ;;
71 --model) MODEL="$2"; shift 2 ;;
72 --config) CONFIG="$2"; shift 2 ;;
73 --tasks) TASKS="$2"; shift 2 ;;
74 --out) OUT="$2"; shift 2 ;;
75 --prompt-mode) MODE="$2"; shift 2 ;;
76 --futility) FUT="$2"; shift 2 ;;
77 --arms) ARMS="$2"; over="$over arms"; shift 2 ;;
78 --only) ONLY="$2"; over="$over only"; shift 2 ;;
79 --attempts) ATTEMPTS="$2"; over="$over attempts"; shift 2 ;;
80 --max-tokens) MAXT="$2"; over="$over max_tokens"; shift 2 ;;
81 --temperature) TEMP="$2"; over="$over temperature"; shift 2 ;;
82 --top-p) TOPP="$2"; over="$over top_p"; shift 2 ;;
83 --seed-base) SEED="$2"; over="$over seed_base"; shift 2 ;;
84 *) die "unknown argument $1" ;;
85 esac
86done
87[ -n "$CONFIG" ] && [ -n "$TASKS" ] && [ -n "$OUT" ] ||
88 die "usage: run.sh --config A --tasks DIR --out DIR [--prompt-mode many|one] [--futility FILE] [options]"
89[[ "$CONFIG" =~ ^[A-Za-z0-9]{1,8}$ ]] || die "config must be [A-Za-z0-9]{1,8}"
90UNAV="$(kv "config.$CONFIG.unavailable")"
91[ -z "$UNAV" ] || die "config $CONFIG is unavailable: $UNAV"
92# The configuration must be named in PARAMS (frozen by the protocol, sections 9 and 13.3): model id,
93# endpoint, request extra and server come from there. A different --base-url or --model is recorded as an
94# override, which score.sh treats as an invalid run.
95FMODEL="$(kv "config.$CONFIG.model")" FBASE="$(kv "config.$CONFIG.base_url")" EXTRA="$(kv "config.$CONFIG.extra")" SERVER="$(kv "config.$CONFIG.server")"
96[ -n "$FMODEL" ] && [ -n "$FBASE" ] && [ -n "$EXTRA" ] && [ -n "$SERVER" ] || die "config $CONFIG is not named in $PARAMS (model, base_url, extra, server; protocol 13.3): refused"
97jq -e 'type == "object" and ([keys[] | select(IN("model", "messages", "max_tokens", "temperature", "top_p", "seed", "stream"))] | length == 0)' \
98 <<< "$EXTRA" > /dev/null 2>&1 || die "config.$CONFIG.extra must be a JSON object that sets none of the fixed request fields"
99EXTRA="$(jq -c . <<< "$EXTRA")"
100if [ -z "$BASE" ]; then BASE="$FBASE"; elif [ "$BASE" != "$FBASE" ]; then over="$over base_url"; fi
101if [ -z "$MODEL" ]; then MODEL="$FMODEL"; elif [ "$MODEL" != "$FMODEL" ]; then over="$over model"; fi
102for arm in ${ARMS//,/ }; do
103 case "$arm" in omegese|text) ;; bytes|structured) die "arm $arm is dropped by protocol r3 (section 13.1): refused" ;; *) die "unknown arm $arm" ;; esac
104done
105[[ ",$(kv prompt_modes)," == *",$MODE,"* ]] || die "prompt mode '$MODE' is not one of PARAMS prompt_modes ($(kv prompt_modes))"
106WLIST="$(kv "config.$CONFIG.weights")"; case "$WLIST" in /*|"") ;; *) WLIST="$ROOT/$WLIST" ;; esac
107[[ "$MAXT" =~ ^[0-9]+$ ]] && [ "$MAXT" -gt 0 ] || die "max_tokens must be a positive integer (PARAMS or --max-tokens)"
108[[ "$ATTEMPTS" =~ ^[1-9]$ ]] || die "attempts 1..9"
109[[ "$RETRIES" =~ ^[0-9]$ ]] || die "retries must be 0..9 (PARAMS)"
110[[ "$TIMEOUT" =~ ^[1-9][0-9]*$ ]] || die "timeout_s must be a positive integer (PARAMS)"
111[[ "${EXP445_TEST_BACKOFF_S:-5}" =~ ^[0-9]+$ ]] || die "EXP445_TEST_BACKOFF_S must be an integer (the retry pause, reader.sh ask)"
112cfg="$(echo "$CONFIG" | tr 'A-Z' 'a-z')"
113[ -d "$TASKS/tasks/cases" ] || die "$TASKS/tasks/cases missing (use ola.sh tasks-public)"
114[ -z "$(find "$TASKS/tasks/cases" -path "*/hidden*" -print -quit)" ] || die "$TASKS contains hidden files: the driver must see only requirement + prompt"
115ids=(); while IFS= read -r t; do ids+=("$t"); done < <(ls "$TASKS/tasks/cases" | LC_ALL=C sort)
116HELD=0; if heldout "${ids[@]}"; then HELD=1; fi
117if [ "$HELD" = 1 ]; then
118 case "$SERVER" in PLACEHOLDER*) die "config $CONFIG server is still a placeholder in PARAMS: held-out generation waits for the commit that freezes it (protocol 13.3)" ;; esac
119 [ "$MODE" = "$(kv heldout_prompt_mode)" ] || die "a held-out run uses the $(kv heldout_prompt_mode) prompt mode (protocol 13.4), not $MODE"
120fi
121B="$ROOT/build/omega_emit"
122make -s -C "$ROOT" PHYSICS_DIR=/nonexistent PHYSICS_LOCK_CHECK=0 omega-emit >&2
123case ",$ARMS," in *,omegese,*) make -s -C "$ROOT" PHYSICS_DIR=/nonexistent PHYSICS_LOCK_CHECK=0 omegese >&2 ;; esac
124X="$B/x445" EMIT="$B/omega_emit" G="$ROOT/build/omegese/omegese"
125weights_ok "$OUT" "$WLIST" "$FMODEL" ||
126 die "$OUT/weights.check + server.json missing or not all OK: run tools/exp445/check_weights.sh $CONFIG $OUT first (protocol section 9)"
127# G0 (13.4): a PASS for this configuration, server and PARAMS, re-derived from its raw responses
128DEVC="$ROOT/tests/one_layer/exp445/dev/tasks/cases" G0T="$(kv g0_task)"
129W0="$(mktemp -d "${TMPDIR:-/tmp}/exp445-run.XXXXXXXX")"; trap 'rm -rf "$W0"' EXIT
130system_prompt "$HERE/arms" omegese "$(kv heldout_prompt_mode)" > "$W0/g0.system" || die "cannot build the omegese prompt"
131[[ "$SERVER" != *$'\t'* ]] || die "config.$CONFIG.server must not contain a tab (it is a G0 ledger column)"
132IDENT="$(g0_ident "$PARAMS" "$CONFIG")"
133g0_barred() { # dies when the G0 ledger holds a FAIL for this configuration's identity (checked at start and per attempt)
134 local fl; fl="$(g0_failed "$CONFIG" "$IDENT")"
135 [ -z "$fl" ] || die "a G0 FAIL for config $CONFIG on this server and settings is in $(g0_ledger) ($(cut -f1,5,6 <<< "$fl" | tr "\t" " ")): nothing in that configuration runs (protocol 13.4)"
136}
137g0_barred
138why="$(g0_check "$OUT/g0" "$CONFIG" "$FMODEL" "$FBASE" "$EXTRA" "$SERVER" "$(sha256sum "$PARAMS" | cut -d" " -f1)" "$(kv g0_gate_sha256)" "$W0/g0.system" \
139 "$(user_msg "$DEVC/$G0T/requirement" "$DEVC/$G0T/prompt")" "$(kv max_tokens)" "$(kv temperature)" "$(kv top_p)" "$(kv seed_base)" \
140 "$(ls "$DEVC" | LC_ALL=C sort | awk -v t="$G0T" '$0 == t { print NR }')" "$(kv g0_seeds)")" && why="$(g0_on_ledger "$OUT/g0" "$CONFIG" "$IDENT")" ||
141 die "no G0 PASS for config $CONFIG and this server in $OUT/g0: $why (run tools/exp445/g0.sh --config $CONFIG --out $OUT; protocol 13.4)"
142mkdir -p "$OUT/prompts" "$OUT/gen" "$OUT/cands"
143OUT="$(realpath -e "$OUT")"
144# one driver per run dir: a second run.sh on the same --out would interleave tries and log rows
145exec 9> "$OUT/.lock"; flock -n 9 || die "another run.sh holds $OUT (lock $OUT/.lock)"
146
147# ---- system prompts, one per arm ----------------------------------------------------------------------
148for arm in ${ARMS//,/ }; do
149 system_prompt "$HERE/arms" "$arm" "$MODE" > "$OUT/prompts/$arm.system.tmp" || die "cannot build the $arm $MODE prompt"
150 if [ -f "$OUT/prompts/$arm.system.txt" ]; then
151 cmp -s "$OUT/prompts/$arm.system.tmp" "$OUT/prompts/$arm.system.txt" || die "arm $arm prompt changed since this run started"
152 rm "$OUT/prompts/$arm.system.tmp"
153 else mv "$OUT/prompts/$arm.system.tmp" "$OUT/prompts/$arm.system.txt"; fi
154done
155if [ -n "$ONLY" ]; then sel=(); for t in "${ids[@]}"; do [[ ",$ONLY," == *",$t,"* ]] && sel+=("$t"); done; ids=("${sel[@]}"); fi
156[ "${#ids[@]}" -gt 0 ] || die "no tasks selected"
157meta="$OUT/meta.txt"
158{
159 echo "revision=r3"; echo "base_url=$BASE"; echo "model=$MODEL"; echo "config=$CONFIG"; echo "server=$SERVER"; echo "request_extra=$EXTRA"
160 echo "arms=$ARMS"; echo "prompt_mode=$MODE"; echo "attempts=$ATTEMPTS"
161 echo "max_tokens_per_round=$MAXT"; echo "repair_rounds=1"; echo "temperature=$TEMP"; echo "top_p=$TOPP"; echo "seed_base=$SEED"
162 echo "timeout_s=$TIMEOUT"; echo "retries=$RETRIES"; echo "order=attempt,task,arm"
163 echo "protocol=$([ -z "$over" ] && echo PARAMS || echo "OVERRIDDEN:$over")"
164 echo "commit=$(git -C "$ROOT" rev-parse HEAD)$(st="$(git -C "$ROOT" status --porcelain -- "${TOOL_PATHS[@]}")" && [ -z "$st" ] || echo -dirty)"
165 echo "params_sha256=$(sha256sum "$PARAMS" | cut -d" " -f1)"; echo "g0_sha256=$(sha256sum < "$OUT/g0/SHA256SUMS" | cut -d" " -f1)"
166 for arm in ${ARMS//,/ }; do echo "prompt_${arm}_sha256=$(sha256sum "$OUT/prompts/$arm.system.txt" | cut -d' ' -f1)"; done
167 echo "tasks=${#ids[@]}"
168 echo "tasks_public_sha256=$(cd "$TASKS" && find tasks -type f -print0 | LC_ALL=C sort -z | xargs -0 sha256sum | sha256sum | cut -d' ' -f1)"
169} > "$meta.new"
170if [ -f "$meta" ]; then
171 # the run's meta.txt is kept as written at its start; a later commit that leaves TOOL_PATHS unchanged (the G0
172 # ledger, the interim scores) may resume it (reader.sh meta_resume; score.sh applies the same tools rule)
173 why="$(meta_resume "$ROOT" "$meta" "$meta.new")" || { diff "$meta" "$meta.new" >&2 || true; die "resuming with different settings: $why"; }
174 cmp -s "$meta" "$meta.new" || echo "run.sh: resuming at $(sed -n 's/^commit=//p' "$meta.new"), tools identical to the run's commit $(sed -n 's/^commit=//p' "$meta")" >&2
175 rm "$meta.new"
176else mv "$meta.new" "$meta"; fi
177[ -f "$OUT/log.tsv" ] || printf 'set\ttask\trounds\tprompt_tokens\tcompletion_tokens_r1\tcompletion_tokens_r2\tfinish_r1\tfinish_r2\twall_s\tcheck_r1\tcheck_r2\tcandidate_sha256\n' > "$OUT/log.tsv"
178dupk="$(tail -n +2 "$OUT/log.tsv" | cut -f1,2 | LC_ALL=C sort | uniq -d | head -n 1 | tr '\t' ' ')"
179[ -z "$dupk" ] || die "log.tsv holds generation $dupk more than once: refused (gen/ is the record; remove the extra row by hand)"
180
181# hook <point>: tests only (EXP445_TEST_KILL=<point>:<set>:<task>): die like a power cut at that point
182hook() { if [ "${EXP445_TEST_KILL:-}" = "$1:$set_id:$task" ]; then kill -KILL $$; fi; }
183# task index = 1-based position in the sorted task list (protocol section 7), fixed before --only
184declare -A TI; i=0; while IFS= read -r t; do i=$((i + 1)); TI[$t]=$i; done < <(ls "$TASKS/tasks/cases" | LC_ALL=C sort)
185
186# stop_gate: the Stage 1 stop rule between attempt 1 and attempt 2 of a held-out run (protocol 13.4)
187stop_gate() {
188 local line d sha
189 sha="$(a1_rows_sha "$OUT/log.tsv")"
190 if [ -z "$FUT" ]; then
191 run_sums "$OUT"
192 echo "run.sh: attempt 1 finished; Stage 1 stop rule (protocol 13.4): score attempt 1 with tools/exp445/score.sh --interim, then rerun with --futility <score dir>/futility.txt" >&2
193 exit 0
194 fi
195 [ -f "$FUT" ] || die "no futility file $FUT"
196 line="$(awk -v r="run=$OUT config=" "index(\$0, r) == 1" "$FUT")"
197 [ -n "$line" ] || die "$FUT has no decision for run $OUT"
198 [[ " $line " == *" a1_rows_sha256=$sha "* ]] || die "$FUT was made from other attempt-1 rows than this run's log.tsv: refused"
199 d="$(sed -n 's/.* decision=\([A-Z]*\).*/\1/p' <<< "$line")"
200 case "$d" in
201 CONTINUE) echo "run.sh: stop rule: $line" >&2 ;;
202 STOP) run_sums "$OUT"; echo "run.sh: stop rule fired, attempts 2 and later are not run: $line" >&2; exit 0 ;;
203 *) die "$FUT: no decision for run $OUT" ;;
204 esac
205}
206
207for at in $(seq 1 "$ATTEMPTS"); do
208 g0_barred # a G0 FAIL recorded while this run is under way stops it before the next attempt
209 if [ "$at" = 2 ] && [ "$HELD" = 1 ]; then stop_gate; fi
210 for task in "${ids[@]}"; do
211 req="$TASKS/tasks/cases/$task/requirement"
212 user="$(user_msg "$req" "$TASKS/tasks/cases/$task/prompt")"
213 for arm in ${ARMS//,/ }; do
214 msgs="$(first_msgs "$(cat "$OUT/prompts/$arm.system.txt")" "$user")"
215 set_id="$cfg-$arm-$at"; d="$OUT/gen/$set_id/$task"
216 [ -f "$d/done" ] && continue
217 mkdir -p "$d" "$OUT/cands/$set_id"
218 seed="$(seed_of "$SEED" "$at" "${TI[$task]}")"
219 if ask "$d" 1 "$(request_json "$MODEL" "$msgs" "$MAXT" "$TEMP" "$TOPP" "$seed" "$EXTRA")"; then
220 a1="$(content_of "$d/r1.resp.json")"; printf '%s' "$a1" > "$d/r1.content"
221 reader "$X" "$EMIT" "$arm" "$d/r1.content" "$d/r1" "$req"
222 hook after-r1
223 k1="$(cat "$d/r1.check")"
224 if [ "$k1" != OK ] && ask "$d" 2 "$(request_json "$MODEL" "$(repair_msgs "$msgs" "$a1" "$k1")" "$MAXT" "$TEMP" "$TOPP" "$seed" "$EXTRA")"; then
225 printf '%s' "$(content_of "$d/r2.resp.json")" > "$d/r2.content"
226 reader "$X" "$EMIT" "$arm" "$d/r2.content" "$d/r2" "$req"
227 fi
228 fi
229 # finalize, idempotent: candidate, then the row once, then the done marker
230 gen_row "$set_id" "$task" "$d" "$d"
231 rm -rf "$OUT/cands/$set_id/$task"
232 for ext in osc oscir; do
233 if [ -n "$FINAL" ] && [ -f "$d/r$FINAL.candidate.$ext" ]; then
234 mkdir -p "$OUT/cands/$set_id/$task"; cp "$d/r$FINAL.candidate.$ext" "$OUT/cands/$set_id/$task/candidate.$ext"
235 fi
236 done
237 have="$(awk -F'\t' -v s="$set_id" -v t="$task" '$1 == s && $2 == t' "$OUT/log.tsv")"
238 if [ -z "$have" ]; then printf '%s\n' "$ROW" >> "$OUT/log.tsv"
239 elif [ "$have" != "$ROW" ]; then die "log.tsv already holds a different row for $set_id $task: refused (gen/ is the record; remove that row by hand)"; fi
240 hook after-log
241 gen_sums "$d"
242 touch "$d/done"
243 echo "run.sh: $(cut -f1-3,5,6,10,11 <<< "$ROW" | tr '\t' ' ')" >&2
244 done
245 done
246done
247run_sums "$OUT"
248echo "run.sh: done, $(($(wc -l < "$OUT/log.tsv") - 1)) generations in $OUT"