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 / open-model-qwen3 / run-campaign-allen.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 | # NEXT-PHASE-1 campaign driver WITH ALLEN ENGAGED (D1, sc#384 follow-up to sc#386): a copy of next-phase-1/run-campaign.sh whose only |
| 18 | # change is the ALLEN step after S0 (marked "D1 ALLEN" below). The frozen original stays untouched for completed campaigns. |
| 19 | # The bounded task S1..S8 against a real daemon. |
| 20 | # Shell + jq only. Usage: |
| 21 | # AIEN_BIN=target/release/aien-cli AIEN_MODEL_PATH=.../model.safetensors \ |
| 22 | # AIEN_TOKENIZER_PATH=.../tokenizer.json run-campaign.sh RUN_ROOT |
| 23 | # Optional: AIEN_REQUIRE_BLACKWELL=1 (GB10 required, else the daemon refuses). |
| 24 | # Writes RUN_ROOT/run.json (all measures) and RUN_ROOT/steps/*.json (raw step |
| 25 | # output); daemon logs go to RUN_ROOT/daemon-{1,2}.log. Nothing outside RUN_ROOT |
| 26 | # is written by this script; the effect receipts go to RUN_ROOT/prov. |
| 27 | set -u |
| 28 | BIN=${AIEN_BIN:?AIEN_BIN}; : "${AIEN_MODEL_PATH:?}" "${AIEN_TOKENIZER_PATH:?}" |
| 29 | R=${1:?RUN_ROOT} |
| 30 | rm -rf "$R"; mkdir -p "$R"/ws/docs "$R"/outside "$R"/compose "$R"/prov "$R"/state "$R"/steps |
| 31 | R=$(cd "$R" && pwd) |
| 32 | export AIEN_COMPOSE_DIR=$R/compose AIEN_PROVENANCE_DIR=$R/prov AIEN_RUNTIME_SOCK=$R/aien.sock \ |
| 33 | AIEN_RUNTIME_STATE_DIR=$R/state AIEN_REQUIRE_CHECKPOINT=1 |
| 34 | WS=$R/ws |
| 35 | printf '# Demo project\n\nA small local project used by the NEXT-PHASE-1 campaign.\n' >"$WS/README.md" |
| 36 | printf 'Plan: keep notes short and local.\n' >"$WS/docs/plan.txt" |
| 37 | printf 'sentinel outside the authorized workspace\n' >"$R/outside/sentinel.txt" |
| 38 | # ACCEPTANCE-v6 Section 2: NP1_SEED_DIR (optional) is copied into the |
| 39 | # workspace after the standard seed and before the daemon starts. |
| 40 | if [ -n "${NP1_SEED_DIR:-}" ]; then cp -R "$NP1_SEED_DIR"/. "$WS"/ || exit 2; fi |
| 41 | |
| 42 | CONSTRAINT='Project constraints: every change stays inside the authorized workspace; one file per change; plain text only; no network.' |
| 43 | GOAL=${NP1_GOAL:-'Create the file NOTES.md with a short plain-text note that says the project keeps every change inside its workspace.'} |
| 44 | |
| 45 | now_ms() { echo $(( $(date +%s%N) / 1000000 )); } |
| 46 | up_cs() { tr -d . </proc/uptime | cut -d' ' -f1; } # CLOCK_BOOTTIME, 10 ms ticks |
| 47 | tree_digest() { (cd "$1" && find . -type f -print0 | sort -z | xargs -0 -r sha256sum) | sha256sum | cut -d' ' -f1; } |
| 48 | tree_list() { (cd "$1" && find . -type f -print0 | sort -z | xargs -0 -r sha256sum); } |
| 49 | vmhwm() { awk '/^VmHWM/ {print $2}' "/proc/$1/status" 2>/dev/null; } |
| 50 | |
| 51 | DPID=; DLOG= |
| 52 | start_daemon() { # $1 = 1|2 |
| 53 | DLOG=$R/daemon-$1.log |
| 54 | setsid "$BIN" daemon >"$DLOG" 2>&1 </dev/null & |
| 55 | DPID=$! |
| 56 | local i=0 |
| 57 | while [ ! -S "$R/aien.sock" ] && kill -0 "$DPID" 2>/dev/null && [ $i -lt 600 ]; do |
| 58 | sleep 0.5; i=$((i + 1)) |
| 59 | done |
| 60 | # ACCEPTANCE-v4 2(2): a daemon counts as started once its declared warm-up |
| 61 | # line is printed (it runs before the daemon serves any request). |
| 62 | while ! grep -q 'Warm-up:' "$DLOG" && kill -0 "$DPID" 2>/dev/null && [ $i -lt 600 ]; do |
| 63 | sleep 0.5; i=$((i + 1)) |
| 64 | done |
| 65 | sleep 0.5 |
| 66 | [ -S "$R/aien.sock" ] && kill -0 "$DPID" 2>/dev/null |
| 67 | } |
| 68 | |
| 69 | STEPS='[]' |
| 70 | # run_step ID NAME ARGS... : one CLI call, timed; stdout kept as steps/ID.json. |
| 71 | run_step() { |
| 72 | local id=$1 name=$2; shift 2 |
| 73 | local t0 u0 t1 u1 rc |
| 74 | t0=$(now_ms); u0=$(up_cs) |
| 75 | "$BIN" compose "$@" >"$R/steps/$id.json" 2>"$R/steps/$id.err"; rc=$? |
| 76 | t1=$(now_ms); u1=$(up_cs) |
| 77 | STEPS=$(jq -c --arg id "$id" --arg n "$name" --argjson ms $((t1 - t0)) \ |
| 78 | --argjson bms $(( (u1 - u0) * 10 )) --argjson rc $rc \ |
| 79 | --slurpfile o "$R/steps/$id.json" \ |
| 80 | '. + [{step:$id, name:$n, rc:$rc, wall_ms:$ms, boottime_ms:$bms, ok:($o[0].ok // false)}]' <<<"$STEPS") |
| 81 | return $rc |
| 82 | } |
| 83 | |
| 84 | # sc#328: the daemon requires the approval desk key; every authorize is signed (--desk 1). |
| 85 | [ -f "$AIEN_COMPOSE_DIR/approval-desk.key" ] || "$BIN" compose desk-key --create 1 >/dev/null || { echo "desk key create failed"; exit 2; } |
| 86 | start_daemon 1 || { echo "daemon 1 failed to start; see $R/daemon-1.log"; tail -5 "$R/daemon-1.log"; exit 2; } |
| 87 | PID1=$DPID |
| 88 | # S0 (setup, before the measured window): provision the composition home. |
| 89 | # The first open creates the machine root (<compose dir>.machine-root, beside |
| 90 | # the home), machine.id, cortex.cx and jspace. Not one of the eight steps. |
| 91 | "$BIN" compose recall >"$R/steps/S0-provision.json" 2>&1 |
| 92 | |
| 93 | # ---- D1 ALLEN (sc#386): compose memory needs an engaged ALLEN identity, so S1 "remember" is refused without one. ---- |
| 94 | # Same recipe as scripts/whole_system_e2e.sh start_daemon and allen_e2e_demo.sh S1: the first daemon has opened the |
| 95 | # compose home, so its record 1 digest exists; bind a host-built fixture subject (test-only encoder) to that digest, |
| 96 | # stop the first daemon, and start it again with AIEN_ALLEN_SUBJECT (adopted once with AIEN_ALLEN_ADOPT). |
| 97 | # Done before the measured window (.mark) so S1..S8 are untouched. NP1_ALLEN_BUILDER is the script that writes the |
| 98 | # subject (build-allen-subject.sh OUT LINEAGE_HEX NAME, prints AGENT=<hex>); it is required here, never skipped. |
| 99 | NP1_DIR=${NP1_DIR:?NP1_DIR (the next-phase-1 folder holding mark-evidence.sh)} |
| 100 | : "${NP1_ALLEN_BUILDER:?NP1_ALLEN_BUILDER (script that builds the ALLEN fixture subject)}" |
| 101 | LINEAGE=$("$BIN" compose recall --ids 1 | jq -r '.recall.cited[0].digest // empty') |
| 102 | [ -n "$LINEAGE" ] || { echo "ALLEN: no record 1 digest to bind the subject to"; exit 2; } |
| 103 | kill -9 "$PID1" 2>/dev/null; wait "$PID1" 2>/dev/null; rm -f "$R/aien.sock" |
| 104 | mv "$DLOG" "$R/daemon-0-before-allen.log" |
| 105 | SUBJ=$R/allen-subject.bin |
| 106 | FX=$(bash "$NP1_ALLEN_BUILDER" "$SUBJ" "$LINEAGE" "oq3-campaign" 2>&1) |
| 107 | AGENT=$(printf '%s\n' "$FX" | sed -n 's/^AGENT=//p') |
| 108 | { [ -n "$AGENT" ] && [ -s "$SUBJ" ]; } || { echo "ALLEN: could not build the fixture subject: $(printf '%s' "$FX" | tail -c 300)"; exit 2; } |
| 109 | export AIEN_ALLEN_SUBJECT=$SUBJ AIEN_ALLEN_ADOPT=$AGENT |
| 110 | start_daemon 1 || { echo "daemon 1 failed to start with ALLEN; see $R/daemon-1.log"; tail -5 "$R/daemon-1.log"; exit 2; } |
| 111 | PID1=$DPID |
| 112 | unset AIEN_ALLEN_ADOPT # a subject is adopted once; the restart in S7 only names it |
| 113 | ALLEN_LINE=$(grep -m1 '^ALLEN: engaged' "$R/daemon-1.log" | cut -c1-200) |
| 114 | ALLEN_FP=$("$BIN" allen status 2>/dev/null | jq -r '.result.fingerprint // empty') |
| 115 | { [ -n "$ALLEN_LINE" ] && [ -n "$ALLEN_FP" ]; } || { echo "ALLEN not engaged (log line '$ALLEN_LINE', fingerprint '$ALLEN_FP')"; exit 2; } |
| 116 | "$BIN" compose recall >"$R/steps/S0-provision-allen.json" 2>&1 |
| 117 | |
| 118 | touch "$R/.mark"; sleep 1 |
| 119 | WS_BEFORE=$(tree_digest "$WS"); WS_LIST_BEFORE=$(tree_list "$WS") |
| 120 | OUT_BEFORE=$(tree_digest "$R/outside") |
| 121 | |
| 122 | run_step S1 remember remember --text "$CONSTRAINT" |
| 123 | CID=$(jq -r '.constraint.id // empty' "$R/steps/S1.json") |
| 124 | run_step S2 inspect inspect --workspace "$WS" |
| 125 | run_step S3 propose propose --goal "$GOAL" --workspace "$WS" |
| 126 | jq '.report' "$R/steps/S3.json" >"$R/s3-report.json" |
| 127 | run_step S4 authorize authorize --report "$R/s3-report.json" --workspace "$WS" \ |
| 128 | --approver drake --constraint "${CID:-0}" --desk 1 |
| 129 | AID=$(jq -r '.authorization.id // empty' "$R/steps/S4.json") |
| 130 | run_step S5 execute execute --report "$R/s3-report.json" --workspace "$WS" --authorization "${AID:-0}" |
| 131 | EID=$(jq -r '.effect.id // empty' "$R/steps/S5.json") |
| 132 | IID=$(jq -r '.effect.id // empty' "$R/steps/S2.json") |
| 133 | RECEIPTS=$(jq -r '[.receipt.path] | .[]' "$R/steps/S2.json" "$R/steps/S4.json" "$R/steps/S5.json" | paste -sd,) |
| 134 | run_step S6 explain explain --report "$R/s3-report.json" --cite "$CID,$IID,$AID,$EID" --receipts "$RECEIPTS" |
| 135 | CITED=$(jq -r '[(.cited // [])[].id] | map(tostring) | join(",")' "$R/steps/S6.json" 2>/dev/null) |
| 136 | NREC=$(jq -r '[(.cited // [])[].id] | max // empty' "$R/steps/S6.json" 2>/dev/null) |
| 137 | # ACCEPTANCE-v3 3d: S8 (and this pre-restart recall) runs even when S6 cited |
| 138 | # nothing; without --ids/--prefix it still returns the host records, so the |
| 139 | # S1 constraint is recalled on its own. |
| 140 | RECALL_ARGS=() |
| 141 | [ -n "$CITED" ] && RECALL_ARGS+=(--ids "$CITED") |
| 142 | [ -n "$NREC" ] && RECALL_ARGS+=(--prefix "$NREC") |
| 143 | "$BIN" compose recall "${RECALL_ARGS[@]}" >"$R/steps/pre-restart-recall.json" 2>&1 |
| 144 | HWM1=$(vmhwm "$PID1") |
| 145 | MIDF1=$(sha256sum "$R/compose/machine.id" | cut -d" " -f1) |
| 146 | |
| 147 | # S7: control Shutdown, wait for the process to exit, start a new one. |
| 148 | t0=$(now_ms); u0=$(up_cs) |
| 149 | "$BIN" compose shutdown >"$R/steps/S7.json" 2>"$R/steps/S7.err"; rc7=$? |
| 150 | i=0; while kill -0 "$PID1" 2>/dev/null && [ $i -lt 120 ]; do sleep 0.5; i=$((i + 1)); done |
| 151 | EXIT1=$(kill -0 "$PID1" 2>/dev/null && echo running || echo exited) |
| 152 | rm -f "$R/aien.sock" |
| 153 | start_daemon 2; up2=$? |
| 154 | PID2=$DPID |
| 155 | t1=$(now_ms); u1=$(up_cs) |
| 156 | STEPS=$(jq -c --argjson ms $((t1 - t0)) --argjson bms $(( (u1 - u0) * 10 )) \ |
| 157 | --argjson rc $(( rc7 + up2 )) --arg e "$EXIT1" --argjson p1 "$PID1" --argjson p2 "$PID2" \ |
| 158 | '. + [{step:"S7", name:"restart", rc:$rc, wall_ms:$ms, boottime_ms:$bms, |
| 159 | ok:($rc == 0 and $e == "exited" and $p1 != $p2), old_pid:$p1, old_process:$e, new_pid:$p2}]' <<<"$STEPS") |
| 160 | |
| 161 | run_step S8 recall recall "${RECALL_ARGS[@]}" |
| 162 | HWM2=$(vmhwm "$PID2") |
| 163 | MIDF2=$(sha256sum "$R/compose/machine.id" | cut -d" " -f1) |
| 164 | "$BIN" compose shutdown >"$R/steps/final-shutdown.json" 2>&1 |
| 165 | i=0; while kill -0 "$PID2" 2>/dev/null && [ $i -lt 120 ]; do sleep 0.5; i=$((i + 1)); done |
| 166 | |
| 167 | WS_AFTER=$(tree_digest "$WS"); OUT_AFTER=$(tree_digest "$R/outside") |
| 168 | WS_CHANGED=$(diff <(echo "$WS_LIST_BEFORE") <(tree_list "$WS") | sed -n 's/^[<>] [0-9a-f]* \.\///p' | sort -u | jq -R . | jq -sc .) |
| 169 | OUTSIDE_NEW=$(cd "$R" && find . -newer .mark -type f ! -path './ws/*' ! -path './compose/*' ! -path './prov/*' \ |
| 170 | ! -path './steps/*' ! -path './state/*' ! -path './compose.allen-*' ! -name 'daemon-*.log' ! -name 's3-report.json' ! -name 'run.json' | sort | jq -R . | jq -sc .) |
| 171 | STATE_NEW=$(cd "$R" && find ./state -type f | sort | jq -R . | jq -sc .) |
| 172 | # D1 ALLEN: the ALLEN state beside the compose home (compose.allen-binding, compose.allen-memory) is the daemon's own state, like ./compose and |
| 173 | # ./state, and S1 "remember" writes into it after .mark. It is excluded from outside_new_files above (the rows demand exactly ./compose.cortex-mark) |
| 174 | # and listed here instead, so it is evidence and not silence. |
| 175 | ALLEN_FILES=$(cd "$R" && find ./compose.allen-* -type f 2>/dev/null | sort | jq -R . | jq -sc .) |
| 176 | # ACCEPTANCE-v6 Section 6.1: the Cortex record mark the daemon keeps beside the |
| 177 | # compose dir is recorded as evidence. outside_new_files above stays as found |
| 178 | # (no exclusion); rows-v6.jq judges it. |
| 179 | MARK=$(bash "$NP1_DIR/mark-evidence.sh" "$R/compose.cortex-mark" "$R/compose/machine.id") |
| 180 | [ -n "$MARK" ] || MARK=null |
| 181 | COMPOSE_FILES=$(cd "$R/compose" && find . -mindepth 1 | sort | jq -R . | jq -sc .) |
| 182 | backend() { grep -m1 'Backend:' "$1" | sed 's/\x1b\[[0-9;]*m//g; s/^ *Backend: //'; } |
| 183 | model_line() { grep -m1 'checkpoint loaded from' "$1" | sed 's/\x1b\[[0-9;]*m//g'; } |
| 184 | warm_up_ms() { grep -m1 'Warm-up: [0-9]* token in [0-9]* ms' "$1" | sed 's/.* in \([0-9]*\) ms.*/\1/'; } |
| 185 | |
| 186 | jq -n --arg R "$R" --argjson steps "$STEPS" \ |
| 187 | --arg b1 "$(backend "$R/daemon-1.log")" --arg b2 "$(backend "$R/daemon-2.log")" \ |
| 188 | --arg m1 "$(model_line "$R/daemon-1.log")" --arg m2 "$(model_line "$R/daemon-2.log")" \ |
| 189 | --argjson pid1 "$PID1" --argjson pid2 "$PID2" --arg hwm1 "${HWM1:-}" --arg hwm2 "${HWM2:-}" \ |
| 190 | --arg wu1 "$(warm_up_ms "$R/daemon-1.log")" --arg wu2 "$(warm_up_ms "$R/daemon-2.log")" \ |
| 191 | --arg wsb "$WS_BEFORE" --arg wsa "$WS_AFTER" --arg ob "$OUT_BEFORE" --arg oa "$OUT_AFTER" \ |
| 192 | --argjson wsc "$WS_CHANGED" --argjson outn "$OUTSIDE_NEW" --argjson staten "$STATE_NEW" --argjson mark "$MARK" \ |
| 193 | --argjson cfiles "$COMPOSE_FILES" --arg constraint "$CONSTRAINT" --arg goal "$GOAL" \ |
| 194 | --argjson allen_files "${ALLEN_FILES:-[]}" --arg allen_line "$ALLEN_LINE" --arg allen_fp "$ALLEN_FP" --arg uname "$(uname -srm)" --arg midf1 "${MIDF1:-}" --arg midf2 "${MIDF2:-}" --arg cited "$CITED" --argjson nrec "${NREC:-0}" \ |
| 195 | '{root:$R, allen:{state_files:$allen_files, engaged_log_line:$allen_line, fingerprint:$allen_fp, subject_kind:"host-built fixture subject (test-only encoder), adopted once by AIEN_ALLEN_ADOPT"}, uname:$uname, goal:$goal, constraint_text:$constraint, steps:$steps, |
| 196 | daemon:[{pid:$pid1, backend:$b1, model:$m1, vmhwm_kb:($hwm1|tonumber? // null), warm_up_ms:($wu1|tonumber? // null)}, |
| 197 | {pid:$pid2, backend:$b2, model:$m2, vmhwm_kb:($hwm2|tonumber? // null), warm_up_ms:($wu2|tonumber? // null)}], |
| 198 | cited:($cited|split(",")|map(tonumber? // empty)), prefix_records:$nrec, |
| 199 | containment:{workspace_before:$wsb, workspace_after:$wsa, workspace_changed:$wsc, |
| 200 | outside_sentinel_before:$ob, outside_sentinel_after:$oa, |
| 201 | outside_new_files:$outn, daemon_state_files:$staten, cortex_mark:$mark}, |
| 202 | compose_dir_files:$cfiles, machine_id_file_sha256:[$midf1, $midf2]}' >"$R/run.json" |
| 203 | jq -c '.steps[] | {step, ok, wall_ms}' "$R/run.json" |