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 / reader.sh
| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | # tools/exp445/reader.sh -- sourced by run.sh and score.sh: the one definition of how a request is built, |
| 17 | # how an answer becomes a candidate and how a generation's log row follows from its raw files |
| 18 | # (omega#445, docs/one_layer/EXP445_PROTOCOL.md sections 6 and 10). |
| 19 | # reader <x445> <omega_emit> <arm> <content file> <out prefix> <requirement> |
| 20 | # writes <out>.payload (first fenced block), <out>.fence, <out>.candidate.osc (text) or |
| 21 | # <out>.candidate.oscir (bytes, structured) when the arm's reader makes one, and <out>.check (one |
| 22 | # line: OK or the refusal). The check uses public data only. |
| 23 | reader() { |
| 24 | local X="$1" EMIT="$2" arm="$3" content="$4" o="$5" req="$6" line="" |
| 25 | "$X" extract "$content" "$o.payload" > "$o.fence" |
| 26 | rm -f "$o.candidate.osc" "$o.candidate.oscir" |
| 27 | case "$arm" in |
| 28 | text) cp "$o.payload" "$o.candidate.osc"; line="$("$X" check osc "$o.candidate.osc" "$req" || true)" ;; |
| 29 | bytes) if line="$("$X" hex "$o.payload" "$o.candidate.oscir")"; then line="$("$X" check oscir "$o.candidate.oscir" "$req" || true)"; fi ;; |
| 30 | structured) if line="$("$EMIT" emit "$o.payload" "$o.candidate.oscir" 2>&1)"; then line="$("$X" check oscir "$o.candidate.oscir" "$req" || true)" |
| 31 | else rm -f "$o.candidate.oscir"; fi ;; |
| 32 | omegese) if line="$("$G" emit "$o.payload" "$o.omegese.json" 2>&1)" && line="$("$EMIT" emit "$o.omegese.json" "$o.candidate.oscir" 2>&1)"; then |
| 33 | line="$("$X" check oscir "$o.candidate.oscir" "$req" || true)" |
| 34 | else rm -f "$o.candidate.oscir"; fi ;; |
| 35 | *) line="REFUSED checker: unknown arm $arm" ;; |
| 36 | esac |
| 37 | [ -n "$line" ] || line="REFUSED checker: no result" |
| 38 | printf '%s\n' "$line" | head -n 1 > "$o.check" |
| 39 | } |
| 40 | |
| 41 | # ---- requests, rounds and rows: shared by run.sh (to make them) and score.sh (to re-derive and check them) |
| 42 | # user_msg <requirement> <prompt file>: the user turn (signature lines + the public task prompt) |
| 43 | user_msg() { printf 'Write the function described below.\n\n%s\n\n%s\n' "$(grep -E '^(entry|params|ret)( |$)' "$1")" "$(cat "$2")"; } |
| 44 | # first_msgs <system text> <user text> |
| 45 | first_msgs() { jq -n -c --arg s "$1" --arg u "$2" '[{role: "system", content: $s}, {role: "user", content: $u}]'; } |
| 46 | # repair_msgs <round-1 messages> <round-1 answer> <round-1 check line>: the one repair format of every arm |
| 47 | repair_msgs() { |
| 48 | local fix="Your program was refused: ${3#REFUSED }"$'\n'"Reply with the corrected complete program in one fenced code block." |
| 49 | jq -c --arg a "$2" --arg f "$fix" '. + [{role: "assistant", content: $a}, {role: "user", content: $f}]' <<< "$1" |
| 50 | } |
| 51 | # request_json <model> <messages> <max_tokens> <temperature> <top_p> <seed> <config extra> |
| 52 | request_json() { |
| 53 | jq -n -c --arg m "$1" --argjson msgs "$2" --argjson mt "$3" --argjson t "$4" --argjson p "$5" --argjson s "$6" --argjson x "$7" \ |
| 54 | '{model: $m, messages: $msgs, max_tokens: $mt, temperature: $t, top_p: $p, seed: $s, stream: false} + $x' |
| 55 | } |
| 56 | # seed_of <seed_base> <attempt> <task index, 1-based in sorted task order>: one seed per task and attempt |
| 57 | seed_of() { echo $(($1 + 1000 * $2 + $3)); } |
| 58 | resp_ok() { jq -e '.choices[0].message.content | type == "string"' "$1" > /dev/null 2>&1; } |
| 59 | content_of() { jq -r '.choices[0].message.content' "$1"; } |
| 60 | # usage_of <response>: prompt tokens, completion tokens, finish reason (tab-separated). The response is |
| 61 | # untrusted: a token count that is not a non-negative integer (or is missing) prints BAD, a finish reason |
| 62 | # outside [A-Za-z_]{1,32} prints BAD; score.sh refuses a run with a BAD token count. |
| 63 | usage_of() { |
| 64 | jq -r 'def n: if type == "number" and . >= 0 and . == floor and . < 100000000 then floor | tostring else "BAD" end; |
| 65 | [(.usage.prompt_tokens | n), (.usage.completion_tokens | n), |
| 66 | (.choices[0].finish_reason | if type == "string" and test("^[A-Za-z_]{1,32}$") then . else "BAD" end)] | @tsv' "$1" |
| 67 | } |
| 68 | # A round <p> = <gen dir>/rN is committed when <p>.transport exists: one line per try, |
| 69 | # try=K http=CODE curl_exit=E time_s=T body=rN.resp.json|rN.err.K.json|none msg=<curl message> |
| 70 | # and it succeeded when its last try's body is rN.resp.json. |
| 71 | round_ok() { [ -f "$1.transport" ] && [ "$(tail -n 1 "$1.transport" | sed -n 's/.* body=\([^ ]*\).*/\1/p')" = "${1##*/}.resp.json" ]; } |
| 72 | wall_of() { local f; for f in "$1"/r*.transport; do if [ -f "$f" ]; then cat "$f"; fi; done | sed -n 's/.* time_s=\([^ ]*\).*/\1/p' | awk '{ s += $1 } END { printf "%.6f", s }'; } |
| 73 | # gen_row <set> <task> <raw gen dir> <derived dir>: sets ROW (the log.tsv row) and FINAL (the final round or |
| 74 | # empty) from the raw responses and transport records in <raw gen dir> and the reader's rN.check / |
| 75 | # rN.candidate.* in <derived dir>. Tokens, finish reasons and wall time come from the raw files only. |
| 76 | gen_row() { |
| 77 | local d="$3" v="$4" rounds=0 pt=0 p2=0 c1=0 c2=- f1=- f2=- k1=- k2=- cs=- ext |
| 78 | FINAL="" |
| 79 | if round_ok "$d/r1"; then |
| 80 | rounds=1; IFS=$'\t' read -r pt c1 f1 < <(usage_of "$d/r1.resp.json"); k1="$(< "$v/r1.check")" |
| 81 | if [ "$k1" = OK ]; then FINAL=1 |
| 82 | elif round_ok "$d/r2"; then |
| 83 | rounds=2; IFS=$'\t' read -r p2 c2 f2 < <(usage_of "$d/r2.resp.json"); if [[ "$pt" =~ ^[0-9]+$ && "$p2" =~ ^[0-9]+$ ]]; then pt=$((pt + p2)); else pt=BAD; fi; k2="$(< "$v/r2.check")"; FINAL=2 |
| 84 | else k2="REQUEST FAILED"; fi |
| 85 | else k1="REQUEST FAILED"; fi |
| 86 | if [ -n "$FINAL" ]; then |
| 87 | for ext in osc oscir; do if [ -f "$v/r$FINAL.candidate.$ext" ]; then cs="$(sha256sum < "$v/r$FINAL.candidate.$ext" | cut -d' ' -f1)"; fi; done |
| 88 | fi |
| 89 | ROW="$(printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s' "$1" "$2" "$rounds" "$pt" "$c1" "$c2" "$f1" "$f2" "$(wall_of "$d")" \ |
| 90 | "${k1//$'\t'/ }" "${k2//$'\t'/ }" "$cs")" |
| 91 | } |
| 92 | # gen_sums <gen dir> / run_sums <run dir>: the hashed raw record (requests, responses, failed bodies, |
| 93 | # transport records; and per run also the prompts, the G0 record g0/, meta.txt, log.tsv, weights.check, server.json) |
| 94 | gen_sums() { (cd "$1" && for f in r*.req.json r*.resp.json r*.err.*.json r*.transport; do if [ -f "$f" ]; then sha256sum "$f"; fi; done) > "$1/SHA256SUMS"; } |
| 95 | run_files() { (cd "$1" && { find gen prompts -type f \( -name '*.json' -o -name '*.transport' -o -name '*.system.txt' \); if [ -d g0 ]; then find g0 -type f; fi; printf '%s\n' meta.txt log.tsv weights.check server.json; } | LC_ALL=C sort); } |
| 96 | run_sums() { (cd "$1" && run_files . | tr '\n' '\0' | xargs -0 sha256sum) > "$1/SHA256SUMS"; } |
| 97 | # weights_ok <run dir> <hash list> <model id>: weights.check is exactly "<file>: OK" for every file of the |
| 98 | # config's frozen hash list, and server.json (GET /models) lists the model id |
| 99 | weights_ok() { |
| 100 | [ -f "$1/weights.check" ] && [ -f "$1/server.json" ] && [ -s "$2" ] || return 1 |
| 101 | [ "$(awk 'NF { n = $2; sub(/^\*/, "", n); print n ": OK" }' "$2" | LC_ALL=C sort)" = "$(LC_ALL=C sort "$1/weights.check")" ] || return 1 |
| 102 | jq -e --arg m "$3" '[.data[]?.id] | index($m) != null' "$1/server.json" > /dev/null 2>&1 |
| 103 | } |
| 104 | |
| 105 | # ---- protocol r3 (section 13): prompts, G0, held-out and the Stage 1 stop rule ---------------------- |
| 106 | # system_prompt <arms dir> <arm> <mode>: the system message of an r3 arm (13.2): arms/common.md, the arm |
| 107 | # document, then the worked examples of the prompt mode (many: examples/many.list, one: examples/one.list). |
| 108 | # Each example is the task as the user message gives it (signature lines, then the prompt) followed by the |
| 109 | # program in the arm's own form (text: program.osc, omegese: program.omegese); the task text is the same in |
| 110 | # both arms. |
| 111 | system_prompt() { |
| 112 | local A="$1" arm="$2" mode="$3" ext list e n=0 k |
| 113 | case "$arm" in omegese) ext=omegese ;; text) ext=osc ;; *) echo "system_prompt: arm $arm has no r3 prompt" >&2; return 1 ;; esac |
| 114 | case "$mode" in many|one) list="$A/examples/$mode.list" ;; *) echo "system_prompt: unknown prompt mode $mode" >&2; return 1 ;; esac |
| 115 | [ -s "$list" ] || { echo "system_prompt: no example list $list" >&2; return 1; } |
| 116 | k="$(grep -c . "$list")" |
| 117 | cat "$A/common.md"; echo; cat "$A/$arm.md"; echo |
| 118 | if [ "$k" = 1 ]; then echo "Worked example: a task as you receive it, then a correct program." |
| 119 | else echo "Worked examples ($k): each is a task as you receive it, then a correct program."; fi |
| 120 | while IFS= read -r e; do |
| 121 | [ -n "$e" ] || continue |
| 122 | [ -f "$A/examples/$e/program.$ext" ] || { echo "system_prompt: example $e has no program.$ext" >&2; return 1; } |
| 123 | n=$((n + 1)) |
| 124 | printf '\nExample %d.\n\n%s\n\n%s\n\n```\n%s\n```\n' "$n" "$(grep -E '^(entry|params|ret)( |$)' "$A/examples/$e/requirement")" \ |
| 125 | "$(cat "$A/examples/$e/prompt")" "$(cat "$A/examples/$e/program.$ext")" |
| 126 | done < "$list" |
| 127 | } |
| 128 | # TOOL_PATHS: the files a run's results depend on. meta.txt records the commit (plus -dirty when any of these |
| 129 | # differ from it); score.sh requires them unchanged between the generation commit and the scoring commit. |
| 130 | TOOL_PATHS=(tools tests/one_layer docs/one_layer src/compiler src/omega_om_units.c mk/omega-emit.mk mk/omegese.mk Makefile) |
| 131 | # meta_resume <repo root> <run's meta.txt> <meta.txt this invocation would write>: may this run resume? |
| 132 | # Yes when the two are identical, or when only commit= differs, neither side is -dirty and TOOL_PATHS are the |
| 133 | # same at both commits: a commit that adds evidence only (the G0 ledger, the Stage 1 interim scores) lands |
| 134 | # between attempt 1 and the --futility resume, and must not strand the run. Prints the reason when not. |
| 135 | meta_resume() { |
| 136 | local root="$1" a="$2" b="$3" ca cb |
| 137 | cmp -s "$a" "$b" && return 0 |
| 138 | if ! diff -q <(grep -v '^commit=' "$a") <(grep -v '^commit=' "$b") > /dev/null; then |
| 139 | echo "settings differ: $(diff <(grep -v '^commit=' "$a") <(grep -v '^commit=' "$b") | grep '^[<>]' | head -n 4 | tr '\n' ' ')"; return 1 |
| 140 | fi |
| 141 | ca="$(sed -n 's/^commit=//p' "$a")" cb="$(sed -n 's/^commit=//p' "$b")" |
| 142 | local h40='^[0-9a-f]{40}(-dirty)?$' |
| 143 | [[ $ca =~ $h40 && $cb =~ $h40 ]] || { echo "a commit is not a full commit id: ${ca:-none} -> ${cb:-none}"; return 1; } |
| 144 | git -C "$root" cat-file -e "${ca%-dirty}^{commit}" 2> /dev/null || { echo "the run's commit ${ca%-dirty} is not in this repository"; return 1; } |
| 145 | git -C "$root" diff --quiet "${ca%-dirty}" "${cb%-dirty}" -- "${TOOL_PATHS[@]}" 2> /dev/null || |
| 146 | { echo "tool files differ between the run's commit ${ca%-dirty} and ${cb%-dirty}"; return 1; } |
| 147 | case "$ca $cb" in *-dirty*) echo "commit changed (${ca} -> ${cb}) and a tree is dirty: the tools cannot be compared"; return 1 ;; esac |
| 148 | } |
| 149 | # heldout <task id>...: true when any task id is not a dev task (the held-out pack; protocol section 3) |
| 150 | heldout() { local t; for t in "$@"; do case "$t" in dev*) ;; *) return 0 ;; esac; done; return 1; } |
| 151 | # gate_config <omegese attempt-1 successes> <text attempt-1 successes> <tasks>: protocol 13.6 for one config, |
| 152 | # omegese at least 80% and at most 10 points below text (integer arithmetic, boundaries included) |
| 153 | gate_config() { [ "$3" -gt 0 ] && [ $((100 * $1)) -ge $((80 * $3)) ] && [ $((100 * ($2 - $1))) -le $((10 * $3)) ]; } |
| 154 | # a1_rows_sha <log.tsv>: the attempt-1 rows of a run, sorted, hashed (ties the Stage 1 stop decision to them) |
| 155 | a1_rows_sha() { tail -n +2 "$1" | awk -F'\t' '$1 ~ /-1$/' | LC_ALL=C sort | sha256sum | cut -d' ' -f1; } |
| 156 | # G0, the sampling gate (13.4). <run dir>/g0/ holds g0.sh and sampling-gate.sh (the scripts that made it), |
| 157 | # prompt.system.txt, rK.req.json for K = 1 .. g0_seeds + 1 (the last repeats the seed of the first), gate/ |
| 158 | # (the stage S gate's output: gate.txt and every reply; d-rK.json is the reply to rK.req.json), the |
| 159 | # repeat's rK.transport and rK.resp.json, verdict.txt and SHA256SUMS. |
| 160 | # G0 ledger (13.4: on FAIL nothing in that configuration runs). g0.sh writes every verdict as one new file in |
| 161 | # the ledger directory evidence/ONE-LAYER-2/g0-ledger/ (<UTC time>-<config>-<first 16 hex of the record's |
| 162 | # SHA256SUMS sha256>.tsv, one line), BEFORE it moves the record into place, so a crash leaves a ledger entry |
| 163 | # without a record (the safe side). One file per verdict, never an edited file: evidence is append-only |
| 164 | # (.github/workflows/evidence-immutable.yml refuses a change to an existing evidence file), so a later PR can |
| 165 | # add a verdict and no PR can drop a FAIL. Commit new entries before score.sh, whose harness refuses a dirty |
| 166 | # tree. Columns: UTC time, config, server, the configuration identity (g0_ident), PASS or FAIL, the record's |
| 167 | # directory (relative to the repo when inside it), the sha256 of its SHA256SUMS, the PARAMS sha256. |
| 168 | # A FAIL is keyed on the identity, not on the whole PARAMS file, so an edit elsewhere in PARAMS (a comment, |
| 169 | # the futility rule, the commit that opens a stage) does not lift it. g0.sh, run.sh and score.sh refuse a |
| 170 | # configuration with a FAIL for its identity; run.sh and score.sh also need the ledger's PASS line for the run's |
| 171 | # G0 record (g0_on_ledger). EXP445_G0_LEDGER names another ledger directory only when EXP445_PARAMS is set |
| 172 | # (tests only). Limit: the ledger is in this tree, so a FAIL recorded in another worktree is seen once merged. |
| 173 | g0_ledger() { if [ -n "${EXP445_PARAMS:-}" ] && [ -n "${EXP445_G0_LEDGER:-}" ]; then echo "$EXP445_G0_LEDGER"; else echo "$ROOT/evidence/ONE-LAYER-2/g0-ledger"; fi; } |
| 174 | # g0_lines: every ledger entry, oldest first (nothing when the directory is missing or empty) |
| 175 | g0_lines() { local l; l="$(g0_ledger)"; if [ -d "$l" ]; then find "$l" -maxdepth 1 -type f -name '*.tsv' -print0 | LC_ALL=C sort -z | xargs -0 -r cat; fi; } |
| 176 | # g0_append <line>: one new ledger file for one verdict; refuses to overwrite an entry |
| 177 | g0_append() { |
| 178 | local l f; l="$(g0_ledger)"; mkdir -p "$l" || return 1 |
| 179 | f="$l/$(cut -f1 <<< "$1")-$(cut -f2 <<< "$1")-$(cut -f7 <<< "$1" | cut -c1-16).tsv" |
| 180 | [ ! -e "$f" ] || { echo "G0 ledger entry $f exists already" >&2; return 1; } |
| 181 | printf '%s\n' "$1" > "$f.tmp" && mv -n "$f.tmp" "$f" && [ ! -e "$f.tmp" ] |
| 182 | } |
| 183 | # g0_ident <PARAMS file> <config>: sha256 over the values that define what G0 tested: the configuration's |
| 184 | # model, endpoint, request extra and server, the sampling settings, the G0 task, seeds, prompt mode and gate |
| 185 | g0_ident() { |
| 186 | local k |
| 187 | for k in "config.$2.model" "config.$2.base_url" "config.$2.extra" "config.$2.server" max_tokens temperature top_p seed_base \ |
| 188 | g0_task g0_seeds g0_gate_sha256 heldout_prompt_mode; do |
| 189 | printf '%s=%s\n' "$k" "$(K="$k" awk -F= '$1 == ENVIRON["K"] { print substr($0, length(ENVIRON["K"]) + 2); exit }' "$1")" |
| 190 | done | sha256sum | cut -d' ' -f1 |
| 191 | } |
| 192 | # g0_failed <config> <identity>: prints the first FAIL line for them (nothing when none) |
| 193 | g0_failed() { g0_lines | C="$1" I="$2" awk -F'\t' '!f && $2 == ENVIRON["C"] && $4 == ENVIRON["I"] && $5 == "FAIL" { print; f = 1 }'; } # reads all input: no SIGPIPE under pipefail |
| 194 | # g0_on_ledger <g0 dir> <config> <identity>: 0 when the ledger holds a PASS line for this configuration and |
| 195 | # identity whose SHA256SUMS sha256 is the record's; prints the reason otherwise |
| 196 | g0_on_ledger() { |
| 197 | local l s; l="$(g0_ledger)"; s="$(sha256sum < "$1/SHA256SUMS" 2> /dev/null | cut -d' ' -f1)" |
| 198 | [ -d "$l" ] || { echo "no G0 ledger $l"; return 1; } |
| 199 | [ -n "$(g0_lines | C="$2" I="$3" S="$s" awk -F'\t' '!f && $2 == ENVIRON["C"] && $4 == ENVIRON["I"] && $5 == "PASS" && $7 == ENVIRON["S"] { print; f = 1 }')" ] || |
| 200 | { echo "the G0 record in $1 has no PASS line in $l for this configuration"; return 1; } |
| 201 | } |
| 202 | # g0_files <g0 dir>: the hashed files (gate/ included), sorted |
| 203 | g0_files() { (cd "$1" && find . -type f ! -path ./SHA256SUMS -printf '%P\n' | LC_ALL=C sort); } |
| 204 | # g0_seed <seed_base> <k> <task index>: the seed of G0 request k (request g0_seeds + 1 uses k = 1) |
| 205 | g0_seed() { seed_of "$1" "$2" "$3"; } |
| 206 | # g0_reply <g0 dir> <k> <seeds>: the reply file of request k (the gate's for k <= seeds, the repeat's after) |
| 207 | g0_reply() { if [ "$2" -le "$3" ]; then echo "$1/gate/d-r$2.json"; elif round_ok "$1/r$2"; then echo "$1/r$2.resp.json"; else echo "$1/none"; fi; } |
| 208 | # g0_gate_audit <gate dir> <seeds>: checks the stage S gate's record against its own kept replies, so its |
| 209 | # VERDICT line cannot be edited alone (g0_check also needs gate_exit=0): its SHA256SUMS verifies; gate.txt reads |
| 210 | # "gate a 4/4 PASS", "gate b 4/4 PASS", "gate c 4/4 PASS", "gate d PASS ..." and "VERDICT PASS"; and the |
| 211 | # kept replies agree: (a) each replay pair identical, (b) each spread triple sampled with its own seeds and |
| 212 | # distinct or p_repeat^2 >= 0.01, (c) each greedy pair identical, (d) every repro reply sampled with its own |
| 213 | # seed and the replies distinct or min p_repeat^(seeds-1) >= 0.01 (the gate's own rules, sampling-gate.sh). |
| 214 | # Prints PASS or FAIL. |
| 215 | g0_gate_audit() { |
| 216 | local g="$1" n="$2" i s h u p |
| 217 | dg() { jq -j '.choices[0].message.content' "$g/$1.json" 2> /dev/null | sha256sum | cut -d' ' -f1; } |
| 218 | fd() { jq -r ".usage.aien_sampling.$2 // empty" "$g/$1.json" 2> /dev/null; } |
| 219 | ge() { awk -v x="$1" -v y="$2" 'BEGIN { exit !(x != "" && x >= y) }'; } |
| 220 | local ok=1 |
| 221 | (cd "$g" && sha256sum -c --quiet --strict SHA256SUMS > /dev/null 2>&1) || ok=0 |
| 222 | for i in a b c; do grep -qx "gate $i 4/4 PASS" "$g/gate.txt" 2> /dev/null || ok=0; done |
| 223 | grep -q "^gate d PASS " "$g/gate.txt" 2> /dev/null || ok=0 |
| 224 | [ "$(tail -n 1 "$g/gate.txt" 2> /dev/null)" = "VERDICT PASS" ] || ok=0 |
| 225 | [ "$ok" = 1 ] || { echo FAIL; return; } |
| 226 | for i in 1 2 3 4; do |
| 227 | [ -f "$g/a-p$i-r1.json" ] && [ "$(dg "a-p$i-r1")" = "$(dg "a-p$i-r2")" ] || { echo FAIL; return; } |
| 228 | [ -f "$g/c-p$i-s1.json" ] && [ "$(dg "c-p$i-s1")" = "$(dg "c-p$i-s2")" ] || { echo FAIL; return; } |
| 229 | for s in 1 2 3; do [ "$(fd "b-p$i-s$s" mode)" = sampled ] && [ "$(fd "b-p$i-s$s" seed)" = "$s" ] || { echo FAIL; return; }; done |
| 230 | u="$(for s in 1 2 3; do dg "b-p$i-s$s"; done | sort -u | wc -l)" p="$(fd "b-p$i-s1" p_repeat)" |
| 231 | [ "$u" -ge 2 ] || ge "$(awk -v p="$p" 'BEGIN { printf "%.6g", p * p }')" 0.01 || { echo FAIL; return; } |
| 232 | done |
| 233 | p=1 |
| 234 | for i in $(seq 1 "$n"); do |
| 235 | [ "$(fd "d-r$i" mode)" = sampled ] && [ "$(fd "d-r$i" seed)" = "$(jq -r .seed "$g/../r$i.req.json" 2> /dev/null)" ] || { echo FAIL; return; } |
| 236 | h="$(fd "d-r$i" p_repeat)"; [ -n "$h" ] || { echo FAIL; return; }; ge "$h" "$p" || p="$h" |
| 237 | done |
| 238 | u="$(for i in $(seq 1 "$n"); do dg "d-r$i"; done | sort -u | wc -l)" |
| 239 | if [ "$u" -ge 2 ] || ge "$(awk -v p="$p" -v k="$n" 'BEGIN { printf "%.6g", p ^ (k - 1) }')" 0.01; then echo PASS; else echo FAIL; fi |
| 240 | } |
| 241 | # g0_derive <g0 dir> <seeds>: "gate=PASS|FAIL distinct=D/N repeat_identical=yes|no sampled_own_seed=S/N+1", |
| 242 | # from the raw files: the gate's verdict line, the replies' exact content and their sampling records |
| 243 | g0_derive() { |
| 244 | local d="$1" n="$2" k f s=0 u rep=no g=FAIL a b |
| 245 | [ "$(g0_gate_audit "$d/gate" "$n")" = PASS ] && g=PASS |
| 246 | for k in $(seq 1 $((n + 1))); do f="$(g0_reply "$d" "$k" "$n")" |
| 247 | if [ -f "$f" ] && jq -e --argjson s "$(jq '.seed' "$d/r$k.req.json")" \ |
| 248 | '.usage.aien_sampling.mode == "sampled" and .usage.aien_sampling.seed == $s and (.choices[0].message.content | type == "string")' "$f" > /dev/null 2>&1; then |
| 249 | s=$((s + 1)); fi; done |
| 250 | u="$(for k in $(seq 1 "$n"); do f="$(g0_reply "$d" "$k" "$n")"; if [ -f "$f" ]; then jq -j '.choices[0].message.content' "$f" | sha256sum | cut -d' ' -f1; fi; done | LC_ALL=C sort -u | grep -c . || true)" |
| 251 | a="$(g0_reply "$d" 1 "$n")" b="$(g0_reply "$d" $((n + 1)) "$n")" |
| 252 | if [ -f "$a" ] && [ -f "$b" ] && [ "$(jq -j '.choices[0].message.content' "$a" | sha256sum)" = "$(jq -j '.choices[0].message.content' "$b" | sha256sum)" ]; then rep=yes; fi |
| 253 | echo "gate=$g distinct=$u/$n repeat_identical=$rep sampled_own_seed=$s/$((n + 1))" |
| 254 | } |
| 255 | # g0_pass <derived> <seeds>: the G0 rule on a derived line (distinct answers are recorded, not gated) |
| 256 | g0_pass() { [[ "$1" == "gate=PASS "* && "$1" == *" repeat_identical=yes sampled_own_seed=$(($2 + 1))/$(($2 + 1))" ]]; } |
| 257 | # g0_check <g0 dir> <want: config model base_url extra server params_sha256 gate_sha256> <system prompt file> |
| 258 | # <user msg> <max_tokens> <temperature> <top_p> <seed_base> <task index> <seeds> |
| 259 | # returns 0 when G0 PASSED for exactly this configuration and server: SHA256SUMS verifies and lists every |
| 260 | # file, the record's gate script is the frozen one, every request is the frozen G0 request, the verdict's |
| 261 | # identity lines match, and the verdict re-derived from the raw files is PASS. Prints the reason otherwise. |
| 262 | g0_check() { |
| 263 | local d="$1" cfg="$2" model="$3" base="$4" extra="$5" server="$6" psha="$7" gsha="$8" sysf="$9" user="${10}" |
| 264 | local mt="${11}" tt="${12}" tp="${13}" sb="${14}" ti="${15}" n="${16}" k s msgs v der |
| 265 | v="$d/verdict.txt" |
| 266 | [ -n "$gsha" ] && [ -n "$psha" ] || { echo "no frozen gate or PARAMS sha256 to check G0 against"; return 1; } |
| 267 | [ -f "$v" ] && [ -f "$d/SHA256SUMS" ] || { echo "no G0 record in $d (run tools/exp445/g0.sh first)"; return 1; } |
| 268 | (cd "$d" && sha256sum -c --quiet --strict SHA256SUMS > /dev/null 2>&1) || { echo "G0 SHA256SUMS does not verify"; return 1; } |
| 269 | [ "$(g0_files "$d")" = "$(awk '{ print substr($0, 67) }' "$d/SHA256SUMS" | LC_ALL=C sort)" ] || { echo "G0 SHA256SUMS does not list exactly the G0 files"; return 1; } |
| 270 | gv() { awk -F= -v k="$1" '$1 == k { print substr($0, length(k) + 2); exit }' "$v"; } |
| 271 | for kv_ in "config=$cfg" "model=$model" "base_url=$base" "request_extra=$extra" "server=$server" "params_sha256=$psha" "gate_sha256=$gsha"; do |
| 272 | [ "$(gv "${kv_%%=*}")" = "${kv_#*=}" ] || { echo "G0 was made for another ${kv_%%=*} ($(gv "${kv_%%=*}"))"; return 1; } |
| 273 | done |
| 274 | [ "$(sha256sum < "$d/sampling-gate.sh" 2> /dev/null | cut -d' ' -f1)" = "$gsha" ] || { echo "G0 was made by another sampling gate script"; return 1; } |
| 275 | cmp -s "$d/prompt.system.txt" "$sysf" || { echo "G0 prompt is not the current many-shot omegese prompt"; return 1; } |
| 276 | msgs="$(first_msgs "$(cat "$sysf")" "$user")" |
| 277 | for k in $(seq 1 $((n + 1))); do |
| 278 | s="$(g0_seed "$sb" "$([ "$k" -le "$n" ] && echo "$k" || echo 1)" "$ti")" |
| 279 | [ -f "$d/r$k.req.json" ] && jq -e --argjson b "$(request_json "$model" "$msgs" "$mt" "$tt" "$tp" "$s" "$extra")" '. == $b' "$d/r$k.req.json" > /dev/null 2>&1 || |
| 280 | { echo "G0 request $k is not the frozen G0 request"; return 1; } |
| 281 | done |
| 282 | der="$(g0_derive "$d" "$n")" |
| 283 | [ "$(gv derived)" = "$der" ] || { echo "G0 verdict ($(gv derived)) differs from its raw files ($der)"; return 1; } |
| 284 | if g0_pass "$der" "$n"; then |
| 285 | [ "$(gv gate_exit)" = 0 ] || { echo "G0 verdict records gate exit $(gv gate_exit), not 0"; return 1; } |
| 286 | [ "$(tail -n 1 "$v")" = "G0 PASS" ] || { echo "G0 verdict line is not G0 PASS"; return 1; } |
| 287 | return 0 |
| 288 | fi |
| 289 | echo "G0 FAILED ($der; PASS needs gate=PASS, the repeat identical and every reply sampled with its own seed)"; return 1 |
| 290 | } |
| 291 | |
| 292 | # ---- transport, shared by run.sh and g0.sh (globals BASE, TIMEOUT, RETRIES and die() come from the caller) |
| 293 | # Before try K (K >= 1) of a round the driver pauses K x 5 s (EXP445_TEST_BACKOFF_S replaces the 5; tests |
| 294 | # set 0), so a short server hiccup does not use up every try at once; timing only, nothing recorded depends on it. |
| 295 | # ask <gen dir> <round> <request json>: one round, committed to disk as soon as it is received. |
| 296 | # Writes rN.req.json, then per try a transport line (rN.transport.tmp) and the body (rN.resp.json on |
| 297 | # success, rN.err.K.json otherwise); renaming rN.transport.tmp to rN.transport commits the round. A |
| 298 | # committed round is never requested again; an interrupted one continues with its next try. |
| 299 | ask() { |
| 300 | local d="$1" r="$2" q="$3" p="$1/r$2" k e out code t t0 body lb msg |
| 301 | if [ -f "$p.req.json" ]; then [ "$(cat "$p.req.json")" = "$q" ] || die "$p.req.json differs from the request this run sends: resume refused" |
| 302 | else printf '%s\n' "$q" > "$p.req.json.tmp" || die "cannot write $p.req.json.tmp"; mv "$p.req.json.tmp" "$p.req.json" || die "cannot commit $p.req.json"; fi |
| 303 | if [ ! -f "$p.transport" ]; then |
| 304 | touch "$p.transport.tmp" || die "cannot write $p.transport.tmp" |
| 305 | lb="$(tail -n 1 "$p.transport.tmp" | sed -n 's/.* body=\([^ ]*\).*/\1/p')" |
| 306 | if [ -n "$lb" ] && [ "$lb" != none ] && [ ! -f "$d/$lb" ] && [ -f "$p.try" ]; then mv "$p.try" "$d/$lb" || die "cannot keep $d/$lb"; fi |
| 307 | rm -f "$p.try" |
| 308 | if [ "$lb" != "r$r.resp.json" ]; then |
| 309 | k="$(wc -l < "$p.transport.tmp")" |
| 310 | while [ "$k" -le "$RETRIES" ]; do |
| 311 | [ "$k" -eq 0 ] || sleep $((k * ${EXP445_TEST_BACKOFF_S:-5})) |
| 312 | t0="$EPOCHREALTIME" |
| 313 | if out="$(curl -sS -m "$TIMEOUT" -o "$p.try" -w '%{http_code} %{time_total}' -H 'Content-Type: application/json' \ |
| 314 | --data-binary @"$p.req.json" "$BASE/chat/completions" 2> "$p.curl.err")"; then e=0; else e=$?; fi |
| 315 | code="${out%% *}" t="${out#* }" |
| 316 | [[ "$code" =~ ^[0-9]{3}$ ]] || code=000 |
| 317 | [[ "$t" =~ ^[0-9]+(\.[0-9]+)?$ ]] || t="$(awk -v a="$t0" -v b="$EPOCHREALTIME" 'BEGIN { printf "%.6f", b - a }')" |
| 318 | if [ "$e" = 0 ] && [ "$code" = 200 ] && resp_ok "$p.try"; then body="r$r.resp.json" |
| 319 | elif [ -s "$p.try" ]; then body="r$r.err.$k.json"; else body=none; rm -f "$p.try"; fi |
| 320 | msg="$(tr '\n\t' ' ' < "$p.curl.err")"; rm -f "$p.curl.err" |
| 321 | echo "try=$k http=$code curl_exit=$e time_s=$t body=$body msg=$msg" >> "$p.transport.tmp" || die "cannot write $p.transport.tmp" |
| 322 | [ "$body" = none ] || mv "$p.try" "$d/$body" || die "cannot keep $d/$body" |
| 323 | [ "$body" = "r$r.resp.json" ] && break |
| 324 | k=$((k + 1)) |
| 325 | done |
| 326 | fi |
| 327 | mv "$p.transport.tmp" "$p.transport" || die "cannot commit $p.transport" |
| 328 | fi |
| 329 | round_ok "$p" |
| 330 | } |