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 / exp445 / g0.sh
| 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/g0.sh --config A --out <run dir> -- G0, the sampling gate of protocol r3 (omega#445, |
| 18 | # docs/one_layer/EXP445_PROTOCOL.md section 13.4), run before every stage and per configuration. |
| 19 | # Wraps the stage S sampling gate of aien-sovereign-core (scripts/sampling-gate.sh at f881710a, vendored |
| 20 | # byte for byte as tools/exp445/sampling-gate.sh; its sha256 is frozen in PARAMS g0_gate_sha256) and adds |
| 21 | # one exp445 check: |
| 22 | # 1 the 8 exp445 requests (the dev task g0_task in the omegese arm, many-shot prompt, temperature and top_p |
| 23 | # from PARAMS, seeds seed_of(seed_base, k, task index) for k = 1 .. g0_seeds: the seeds of that task's |
| 24 | # attempts) are the gate's repro requests (its gate d): the gate re-sends each and passes when the |
| 25 | # replies differ or when identical replies are plausible from the server's reported confidence |
| 26 | # (p_repeat^(k-1) >= 0.01), with every reply reporting mode=sampled and its own seed; its gates a, b, c |
| 27 | # (replay, spread, greedy) run on its own four prompts |
| 28 | # 2 one repeat of the first seed, sent by this script (reader.sh ask, with a transport record) |
| 29 | # PASS when the sampling gate passes (VERDICT PASS), the repeat is byte-identical to the gate's reply to |
| 30 | # the first seed, and all g0_seeds + 1 replies report mode=sampled with their own seed. The number of |
| 31 | # distinct answers is recorded, not gated. |
| 32 | # Writes <run dir>/g0/: g0.sh and sampling-gate.sh (the scripts that made it), prompt.system.txt, |
| 33 | # rK.req.json (K = 1 .. g0_seeds + 1), gate/ (the gate's output, replies kept: REDACT_REPRO=0, the dev task |
| 34 | # is public), r<g0_seeds+1>.transport and .resp.json, verdict.txt (identity, seeds, derived values, last |
| 35 | # line G0 PASS or G0 FAIL) and SHA256SUMS. Exit 0 on PASS, 1 on FAIL. run.sh refuses to generate, and |
| 36 | # score.sh refuses a run, without a G0 PASS for the same configuration, server and PARAMS (reader.sh |
| 37 | # g0_check re-derives it). A G0 record is never overwritten: after a FAIL nothing runs in that run dir |
| 38 | # (13.4). A transport failure (the gate exits 2 or runs out of time, or the repeat gets no response) writes |
| 39 | # no record and exits 2: it says nothing about the sampler. Every verdict is added to the G0 ledger, one new file each (reader.sh |
| 40 | # g0_ledger) before the record is moved into place; with a FAIL there for this configuration's identity (reader.sh |
| 41 | # g0_ident: its model, endpoint, request extra, server, sampling settings and G0 settings), g0.sh refuses with exit 3 |
| 42 | # (not a transport failure: do not retry), and run.sh and score.sh refuse, in any run dir. EXP445_PARAMS names another PARAMS file (tests only). |
| 43 | set -euo pipefail |
| 44 | HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 45 | ROOT="$(cd "$HERE/../.." && pwd)" |
| 46 | die() { echo "g0.sh: $*" >&2; exit 2; } |
| 47 | PARAMS="${EXP445_PARAMS:-$HERE/PARAMS}" |
| 48 | [ -f "$PARAMS" ] || die "no PARAMS file $PARAMS" |
| 49 | kv() { awk -F= -v k="$1" '$1 == k { print substr($0, length(k) + 2); exit }' "$PARAMS"; } |
| 50 | command -v jq > /dev/null || die "jq is required" |
| 51 | command -v curl > /dev/null || die "curl is required" |
| 52 | CONFIG="" OUT="" |
| 53 | while [ $# -gt 0 ]; do |
| 54 | case "$1" in |
| 55 | --config) CONFIG="$2"; shift 2 ;; |
| 56 | --out) OUT="$2"; shift 2 ;; |
| 57 | *) die "unknown argument $1" ;; |
| 58 | esac |
| 59 | done |
| 60 | [ -n "$CONFIG" ] && [ -n "$OUT" ] || die "usage: g0.sh --config A --out <run dir>" |
| 61 | [[ "$CONFIG" =~ ^[A-Za-z0-9]{1,8}$ ]] || die "config must be [A-Za-z0-9]{1,8}" |
| 62 | un="$(kv "config.$CONFIG.unavailable")" |
| 63 | [ -z "$un" ] || die "config $CONFIG is unavailable: $un" |
| 64 | MODEL="$(kv "config.$CONFIG.model")" BASE="$(kv "config.$CONFIG.base_url")" EXTRA="$(kv "config.$CONFIG.extra")" SERVER="$(kv "config.$CONFIG.server")" |
| 65 | [ -n "$MODEL" ] && [ -n "$BASE" ] && [ -n "$EXTRA" ] && [ -n "$SERVER" ] || die "config $CONFIG (model, base_url, extra, server) is not named in $PARAMS" |
| 66 | [[ "$SERVER" != *$'\t'* ]] || die "config.$CONFIG.server must not contain a tab (it is a G0 ledger column)" |
| 67 | EXTRA="$(jq -c . <<< "$EXTRA")" || die "config.$CONFIG.extra is not JSON" |
| 68 | MAXT="$(kv max_tokens)" TEMP="$(kv temperature)" TOPP="$(kv top_p)" SB="$(kv seed_base)" TIMEOUT="$(kv timeout_s)" RETRIES="$(kv retries)" |
| 69 | TASK="$(kv g0_task)" N="$(kv g0_seeds)" MODE="$(kv heldout_prompt_mode)" GSHA="$(kv g0_gate_sha256)" |
| 70 | [[ "$TIMEOUT" =~ ^[1-9][0-9]*$ ]] || die "timeout_s must be a positive integer in $PARAMS" |
| 71 | [[ "$N" =~ ^[1-9][0-9]?$ ]] && [ -n "$TASK" ] && [ -n "$MODE" ] && [ -n "$GSHA" ] || die "g0_task, g0_seeds, g0_gate_sha256, heldout_prompt_mode missing in $PARAMS" |
| 72 | GATE="$HERE/sampling-gate.sh" |
| 73 | [ "$(sha256sum < "$GATE" | cut -d' ' -f1)" = "$GSHA" ] || die "$GATE is not the frozen sampling gate (sha256 differs from PARAMS g0_gate_sha256)" |
| 74 | DEV="$ROOT/tests/one_layer/exp445/dev/tasks/cases" |
| 75 | [ -f "$DEV/$TASK/requirement" ] && [ -f "$DEV/$TASK/prompt" ] || die "no dev task $TASK" |
| 76 | TI="$(ls "$DEV" | LC_ALL=C sort | awk -v t="$TASK" '$0 == t { print NR }')" |
| 77 | . "$HERE/reader.sh" |
| 78 | PSHA="$(sha256sum "$PARAMS" | cut -d" " -f1)" IDENT="$(g0_ident "$PARAMS" "$CONFIG")" |
| 79 | fl="$(g0_failed "$CONFIG" "$IDENT")" |
| 80 | if [ -n "$fl" ]; then |
| 81 | echo "g0.sh: 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)" >&2 |
| 82 | exit 3 |
| 83 | fi |
| 84 | mkdir -p "$OUT"; OUT="$(realpath -e "$OUT")" |
| 85 | D="$OUT/g0" |
| 86 | [ ! -e "$D" ] || die "$D exists: a G0 record is never overwritten; after a G0 FAIL nothing runs in this run dir (protocol 13.4)" |
| 87 | mkdir "$D.tmp.$$"; trap 'rm -rf "$D.tmp.$$"' EXIT |
| 88 | W="$D.tmp.$$" |
| 89 | cp "$HERE/g0.sh" "$GATE" "$W/" |
| 90 | system_prompt "$HERE/arms" omegese "$MODE" > "$W/prompt.system.txt" || die "cannot build the omegese $MODE prompt" |
| 91 | msgs="$(first_msgs "$(cat "$W/prompt.system.txt")" "$(user_msg "$DEV/$TASK/requirement" "$DEV/$TASK/prompt")")" |
| 92 | seeds="" |
| 93 | for k in $(seq 1 $((N + 1))); do |
| 94 | s="$(g0_seed "$SB" "$([ "$k" -le "$N" ] && echo "$k" || echo 1)" "$TI")"; seeds="$seeds${seeds:+ }$s" |
| 95 | printf '%s\n' "$(request_json "$MODEL" "$msgs" "$MAXT" "$TEMP" "$TOPP" "$s" "$EXTRA")" > "$W/r$k.req.json" |
| 96 | done |
| 97 | # 1 the stage S gate, with the N exp445 requests as its repro requests (exit 0 PASS, 1 FAIL, 2 transport). |
| 98 | # GREEDY_GOLDEN is not passed in (the gate runs the same way for every caller); the whole gate is bounded |
| 99 | # by timeout_s per request it sends (4 prompts x 7 + N), since its curl calls have no time limit |
| 100 | base="${BASE%/}"; base="${base%/v1}" |
| 101 | ge=0; (cd "$W" && timeout "$(( (28 + N) * TIMEOUT ))" env -u GREEDY_GOLDEN REDACT_REPRO=0 ./sampling-gate.sh "$base" "$MODEL" gate $(seq -f 'r%g.req.json' 1 "$N")) > "$W/gate.out" 2>&1 || ge=$? |
| 102 | # a transport failure (gate exit 2, or the time limit) says nothing about the sampler: no record is written |
| 103 | [ "$ge" = 0 ] || [ "$ge" = 1 ] || die "the sampling gate could not complete (exit $ge: $(tail -n 2 "$W/gate.out" | tr '\n' ' ')); no G0 record written, rerun when the server answers" |
| 104 | # 2 the repeat of the first seed |
| 105 | ask "$W" "$((N + 1))" "$(cat "$W/r$((N + 1)).req.json")" || true |
| 106 | round_ok "$W/r$((N + 1))" || die "the repeat of seed 1 got no response ($(tail -n 1 "$W/r$((N + 1)).transport" 2> /dev/null)); no G0 record written, rerun when the server answers" |
| 107 | der="$(g0_derive "$W" "$N")" |
| 108 | if g0_pass "$der" "$N"; then v=PASS; else v=FAIL; fi |
| 109 | { |
| 110 | echo "config=$CONFIG"; echo "model=$MODEL"; echo "base_url=$BASE"; echo "request_extra=$EXTRA"; echo "server=$SERVER" |
| 111 | echo "params_sha256=$PSHA";echo "commit=$(git -C "$ROOT" rev-parse HEAD 2> /dev/null || echo none)" |
| 112 | echo "gate=$(kv g0_gate_source)"; echo "gate_sha256=$GSHA"; echo "gate_exit=$ge" |
| 113 | echo "task=$TASK"; echo "arm=omegese"; echo "prompt_mode=$MODE"; echo "prompt_sha256=$(sha256sum < "$W/prompt.system.txt" | cut -d' ' -f1)" |
| 114 | echo "max_tokens=$MAXT"; echo "temperature=$TEMP"; echo "top_p=$TOPP"; echo "seeds=$seeds" |
| 115 | echo "rule=PASS needs the sampling gate VERDICT PASS (its gate d on the $N exp445 requests), the repeat (request $((N + 1))) byte-identical to the reply to request 1, every reply mode=sampled with its own seed; distinct answers are recorded, not gated" |
| 116 | echo "derived=$der" |
| 117 | echo "G0 $v" |
| 118 | } > "$W/verdict.txt" |
| 119 | (cd "$W" && g0_files . | tr '\n' '\0' | xargs -0 sha256sum) > "$W/SHA256SUMS" |
| 120 | # the ledger line first (13.4): a crash after it leaves a FAIL barred without a record, never a record unbarred |
| 121 | g0_append "$(printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" "$(date -u +%Y%m%dT%H%M%SZ)" "$CONFIG" "$SERVER" "$IDENT" "$v" "${D#"$ROOT/"}" \ |
| 122 | "$(sha256sum < "$W/SHA256SUMS" | cut -d" " -f1)" "$PSHA")" || die "cannot write the G0 ledger entry in $(g0_ledger)" |
| 123 | mv "$W" "$D"; trap - EXIT |
| 124 | echo "g0.sh: config $CONFIG: $der: G0 $v (record in $D)" |
| 125 | [ "$v" = PASS ] |