aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / docs / campaigns / scoring / score-rows.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 | # Scoring contract v5 scorer (docs/campaigns/scoring/SCORING-v5.md). Shell + jq only. |
| 18 | # Usage: score-rows.sh DECLARATION.json RESULTS.jsonl [EXTERNAL.json] |
| 19 | # DECLARATION frozen declaration of the case set (see SCORING-v5.md section 2) |
| 20 | # RESULTS one JSON object per line, one line per run |
| 21 | # EXTERNAL optional JSON object of external controls, e.g. {"F0":true}; |
| 22 | # an external control that is absent counts as false (fail closed) |
| 23 | # Prints one JSON verdict on stdout. Exit 0 only if the campaign verdict is PASS, |
| 24 | # 1 for any other verdict, 2 for a bad declaration, unreadable input or a jq failure. |
| 25 | # Nothing here reads the older make-receipt.sh scorers; they stay as they were. |
| 26 | set -u |
| 27 | DECL=${1:?DECLARATION.json} RES=${2:?RESULTS.jsonl} EXT=${3:-} |
| 28 | [ -f "$DECL" ] && [ -f "$RES" ] || { echo "score-rows: input not found" >&2; exit 2; } |
| 29 | if [ -n "$EXT" ]; then [ -r "$EXT" ] || { echo "score-rows: external file not found" >&2; exit 2; }; else EXT=/dev/null; fi |
| 30 | # Results are read as raw lines so that one bad line is a malformed record, not a crash. |
| 31 | out=$(jq -n --slurpfile decl "$DECL" --rawfile raw "$RES" --slurpfile ext "$EXT" ' |
| 32 | ($decl[0]) as $d |
| 33 | | ($ext[0] // {}) as $x |
| 34 | | def isint: type == "number" and . == floor and . >= 1; |
| 35 | def nonempty: type == "string" and (gsub("\\s"; "") | length) > 0; |
| 36 | # ---- declaration must be valid, else the whole scoring is an error ---- |
| 37 | ( (($d.rows | map(.row?) + ["F0"]) as $ids2 | if ($d | type) != "object" or $d.contract != "scoring-v5" or ($d.campaign | nonempty | not) |
| 38 | or ($d.rows | type) != "array" or ($d.rows | length) == 0 then "contract/campaign/rows missing" |
| 39 | elif ($d.rows | map(.row) | (length != (unique | length))) then "duplicate row id in declaration" |
| 40 | elif ($d.rows | all( |
| 41 | (.row | nonempty) and (.reps | isint) and .expected == "PASS" |
| 42 | and ((.control // null) == null or (.control | type) == "string") |
| 43 | and ((.not_applicable // false) | type) == "boolean")) | not then "bad row entry (row, reps >= 1, expected PASS)" |
| 44 | elif ([$d.rows[] | .control | select(. != null and (. as $c | $ids2 | index($c) | not))] | length) > 0 |
| 45 | then "control names an undeclared row" |
| 46 | else null end) ) as $derr |
| 47 | | if $derr != null then {contract:"scoring-v5", verdict:"ERROR", declaration_error:$derr, rows:[], extras:[], malformed:[]} |
| 48 | else |
| 49 | # ---- parse results line by line ---- |
| 50 | ( $raw | split("\n") | map(select(gsub("\\s"; "") | length > 0)) | to_entries |
| 51 | | map(. as $e | ($e.value | try fromjson catch null) as $o | {line:($e.key + 1), o:$o}) ) as $lines |
| 52 | | ($d.rows | map(.row)) as $declared |
| 53 | | ($lines | map(select((.o | type) != "object" or ((.o.row | type) != "string") |
| 54 | ))) as $noid |
| 55 | | ($lines | map(select((.o | type) == "object" and (.o.row | type) == "string"))) as $withrow |
| 56 | # a well-formed record: integer rep >= 1, verdict in the enum, NOT_APPLICABLE carries an outcome (the reason) |
| 57 | | def wellformed: (.rep | isint) |
| 58 | and (.verdict | IN("PASS","FAIL","NOT_RUN","NOT_APPLICABLE","UNVERIFIED")) |
| 59 | and (if .verdict == "NOT_APPLICABLE" then (.outcome | nonempty) else true end); |
| 60 | ($withrow | map(select(.o | wellformed | not))) as $bad |
| 61 | | ($withrow | map(select(.o | wellformed)) | map(.o + {line:.line})) as $good |
| 62 | | ($d.rows | map(. as $r |
| 63 | | ($good | map(select(.row == $r.row))) as $mine |
| 64 | | ($mine | map(select(.rep <= $r.reps))) as $in |
| 65 | | ($mine | map(select(.rep > $r.reps)) | map({row, rep, verdict, line})) as $extra |
| 66 | | ($in | group_by(.rep) | map(select(length > 1) | .[0].rep)) as $dups |
| 67 | | ($in | map(.rep) | unique) as $have |
| 68 | | ([range(1; $r.reps + 1)] | map(select(. as $k | $have | index($k) | not))) as $missing |
| 69 | | ($bad | map(select(.o.row == $r.row)) | map(.line)) as $badlines |
| 70 | | ($in | map(select(.verdict == "FAIL"))) as $failed |
| 71 | | ($in | map(select(.verdict == "NOT_APPLICABLE"))) as $na |
| 72 | | { row:$r.row, role:($r.role // "case"), control:($r.control // null), required_reps:$r.reps, |
| 73 | counted_reps:($have | length), missing_reps:$missing, duplicate_reps:$dups, |
| 74 | incomplete_record_lines:$badlines, extra_runs:$extra, |
| 75 | na_declared:($r.not_applicable // false), |
| 76 | own:( |
| 77 | if ($dups | length) > 0 then "ERROR" |
| 78 | elif ($failed | length) > 0 then "FAIL" |
| 79 | elif ($na | length) > 0 and (($r.not_applicable // false) | not) then "FAIL" |
| 80 | elif ($badlines | length) > 0 then "INCOMPLETE" |
| 81 | elif ($in | any(.verdict == "UNVERIFIED")) then "INCOMPLETE" |
| 82 | elif ($missing | length) > 0 then (if ($have | length) == 0 then "NOT_RUN" else "INCOMPLETE" end) |
| 83 | elif ($in | any(.verdict == "NOT_RUN")) then "NOT_RUN" |
| 84 | elif ($na | length) > 0 then (if ($na | length) == ($in | length) then "NOT_APPLICABLE" else "INCOMPLETE" end) |
| 85 | else "PASS" end) } )) as $own |
| 86 | # a control must itself have scored PASS (its declared expected result) for its rows to count |
| 87 | | ($own | map({(.row): .own}) | add) as $ownmap |
| 88 | | ($own | map(. as $r |
| 89 | | (if $r.control == null then true |
| 90 | elif $r.control == "F0" then ($x.F0 == true) |
| 91 | else ($ownmap[$r.control] == "PASS") end) as $cok |
| 92 | | $r + {control_pass:$cok, |
| 93 | verdict:(if ($r.own == "PASS" or $r.own == "NOT_APPLICABLE") and ($cok | not) then "NOT_RUN" else $r.own end)} |
| 94 | | del(.own))) as $rows |
| 95 | | ($d.rows | map(.row)) as $ids |
| 96 | | ($good | map(select(.row as $k | $ids | index($k) | not)) | map({row, rep, verdict, line})) as $unrequested |
| 97 | | ( $rows | map(.verdict) ) as $vs |
| 98 | | { contract:"scoring-v5", campaign:$d.campaign, |
| 99 | verdict:(if ($noid | length) > 0 then "INCOMPLETE" |
| 100 | elif ($vs | any(. == "ERROR")) then "ERROR" |
| 101 | elif ($vs | any(. == "FAIL")) then "FAIL" |
| 102 | elif ($vs | any(. == "INCOMPLETE")) then "INCOMPLETE" |
| 103 | elif ($vs | any(. == "NOT_RUN")) then "NOT_RUN" |
| 104 | else "PASS" end), |
| 105 | rows:$rows, |
| 106 | not_applicable:($rows | map(select(.verdict == "NOT_APPLICABLE") | .row)), |
| 107 | extras:($unrequested + ($rows | map(.extra_runs[]))), |
| 108 | malformed:($noid | map({line, why:"not a JSON object with a string row"})) } |
| 109 | end' 2>/dev/null) || { echo "score-rows: jq failed (fail closed)" >&2; exit 2; } |
| 110 | printf '%s\n' "$out" |
| 111 | v=$(printf '%s' "$out" | jq -r .verdict 2>/dev/null) || exit 2 |
| 112 | [ "$v" = PASS ] && exit 0 |
| 113 | exit 1 |