interplane.Get Omega
Read in your language

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

branch main Branches Tags History

aien-sovereign-core-mv / docs / campaigns / next-phase-1 / run-campaign.sh

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