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 / tools / aien-local-ci / wt.sh

118 lines · 6671 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# wt.sh: trusted-side worktree management for aien-run checks (A2: worktrees under /var/lib/aien-run/wt,
18# the clone unwritable by aien-run, the worktree's .git link unwritable by aien-run).
19#
20# wt.sh sync <repo> <trusted-clone-dir> create/refresh the bare mirror /var/lib/aien-run/mirror/<repo>.git
21# wt.sh add <repo> <sha> check out <sha> at /var/lib/aien-run/wt/<repo>/<sha>/<repo> for aien-run
22# wt.sh sibling <repo> <sha40> <dir> <source-repo> <source-sha>
23# read-only checkout of <source-repo> at <source-sha> beside it, at
24# /var/lib/aien-run/wt/<repo>/<sha40>/<dir> (what ../<dir> means there)
25# wt.sh remove <repo> <sha> remove /var/lib/aien-run/wt/<repo>/<sha> with every sibling in it,
26# and the head's target dirs /var/lib/aien-run/target/<repo>/pr|main/<sha>
27# wt.sh list list run directories with age
28#
29# Why a mirror: the operator's clones live under ~/workspace (0700), which aien-run must not read. The mirror is a
30# bare copy owned by the operator (0755, world-readable, not writable by aien-run); the worktree's .git file points
31# into the mirror, so aien-run can read objects (git log/rev-parse work) but cannot write refs, objects or the index.
32# Layout (SANDBOX lane, 2026-10-11): one run directory per head, /var/lib/aien-run/wt/<repo>/<sha40>/, owned by the
33# operator, mode 0755 (aien-run cannot create entries in it). It holds the checkout under test at <repo>/ and the
34# sibling checkouts its gates reach as ../<dir>. The exec helper takes --cwd .../<sha40>/<repo> and makes the run
35# directory the one writable worktree mount; file ownership inside it decides what aien-run may write.
36# Ownership after `add`: checkout dir operator:aien-run mode 1775 (sticky: aien-run may create files but cannot
37# remove or replace entries it does not own), `.git` file root:root 0644, everything else aien-run:aien-run.
38# Ownership after `sibling`: everything operator-owned, no group/other write: aien-run reads it (and its git history
39# through the mirror) but cannot change it.
40set -euo pipefail
41RUN_USER=aien-run
42BASE=/var/lib/aien-run
43MIRROR="$BASE/mirror"
44WT="$BASE/wt"
45OPERATOR=$(id -un)
46
47die() { printf 'wt.sh: %s\n' "$*" >&2; exit 2; }
48name_ok() { [[ $1 =~ ^[A-Za-z0-9_-][A-Za-z0-9._-]*$ ]]; }
49cmd=${1:-}; shift || true
50case "$cmd" in
51 sync)
52 repo=${1:?repo}; clone=${2:?trusted-clone-dir}
53 name_ok "$repo" || die "bad repo name"
54 git -C "$clone" rev-parse --git-dir >/dev/null || die "not a git dir: $clone"
55 m="$MIRROR/$repo.git"
56 if [[ ! -d $m ]]; then
57 git init --bare --quiet "$m"
58 git -C "$m" config core.sharedRepository 0644
59 fi
60 git -C "$m" fetch --quiet --prune "$clone" '+refs/heads/*:refs/heads/*' '+refs/remotes/*:refs/remotes/*' '+refs/tags/*:refs/tags/*'
61 # Gates may read the origin URL (the sc tier-1 test f62 does, as on a GitHub runner). The mirror takes the
62 # trusted clone's URL; nothing fetches through it (the sandbox has no network and cannot write the mirror).
63 if u=$(git -C "$clone" remote get-url origin 2>/dev/null); then git -C "$m" config remote.origin.url "$u"; fi
64 chmod -R o+rX,o-w "$m"
65 printf 'mirror %s: %s refs\n' "$m" "$(git -C "$m" for-each-ref | wc -l)"
66 ;;
67 add)
68 repo=${1:?repo}; sha=${2:?sha}
69 name_ok "$repo" && [[ $sha =~ ^[0-9a-f]{7,40}$ ]] || die "bad repo or sha"
70 m="$MIRROR/$repo.git"; [[ -d $m ]] || die "no mirror for $repo (run: wt.sh sync $repo <clone>)"
71 full=$(git -C "$m" rev-parse --verify "$sha^{commit}") || die "unknown commit $sha"
72 c="$WT/$repo/$full"; d="$c/$repo"
73 [[ ! -e $c ]] || die "exists: $c"
74 install -d -m 0755 "$WT/$repo" "$c"
75 git -C "$m" worktree add --quiet --detach "$d" "$full"
76 sudo -n chown -R "$RUN_USER:$RUN_USER" "$d"
77 sudo -n chown root:root "$d/.git"; sudo -n chmod 0644 "$d/.git"
78 sudo -n chown "$OPERATOR:$RUN_USER" "$d"; sudo -n chmod 1775 "$d"
79 printf '%s\n' "$d"
80 ;;
81 sibling)
82 repo=${1:?repo}; sha=${2:?sha40}; dir=${3:?dir}; src=${4:?source-repo}; ssha=${5:?source-sha}
83 name_ok "$repo" && name_ok "$dir" && name_ok "$src" && [[ $sha =~ ^[0-9a-f]{40}$ && $ssha =~ ^[0-9a-f]{7,40}$ ]] \
84 || die "bad repo, dir, source or sha"
85 [[ $dir != "$repo" ]] || die "a sibling cannot take the checkout's own name: $dir"
86 c="$WT/$repo/$sha"; [[ -d $c/$repo && ! -L $c ]] || die "no checkout at $c/$repo (run: wt.sh add $repo $sha)"
87 s="$c/$dir"; [[ ! -e $s ]] || die "exists: $s"
88 m="$MIRROR/$src.git"; [[ -d $m ]] || die "no mirror for $src (run: wt.sh sync $src <clone>)"
89 full=$(git -C "$m" rev-parse --verify "$ssha^{commit}") || die "unknown commit $ssha in $src"
90 git -C "$m" worktree add --quiet --detach "$s" "$full"
91 chmod -R a+rX,go-w "$s"
92 printf '%s %s\n' "$s" "$full"
93 ;;
94 remove)
95 repo=${1:?repo}; sha=${2:?sha}
96 name_ok "$repo" && [[ $sha =~ ^[0-9a-f]{7,40}$ ]] || die "bad repo or sha"
97 d=$(ls -d "$WT/$repo/$sha"* 2>/dev/null | head -1); [[ -n $d ]] || die "no worktree for $sha"
98 sudo -n rm -rf "$d"
99 # The head's own cargo target dirs (one per head and trust class, see aien-run-exec) go with it: nothing
100 # reuses them, and a dev build of sovereign-core is ~30 GB.
101 full=${d##*/}
102 if [[ $full =~ ^[0-9a-f]{40}$ ]]; then
103 for t in "$BASE/target/$repo/pr/$full" "$BASE/target/$repo/main/$full"; do
104 if [[ -d $t ]]; then sudo -n rm -rf "$t"; printf 'removed %s\n' "$t"; fi
105 done
106 fi
107 # The checkout and its siblings were worktrees of (possibly) several mirrors.
108 for m in "$MIRROR"/*.git; do [[ -d $m ]] && git -C "$m" worktree prune; done
109 printf 'removed %s\n' "$d"
110 ;;
111 list)
112 for d in "$WT"/*/*/; do
113 [[ -d $d ]] || continue
114 printf '%s %s days\n' "$d" "$(( ( $(date +%s) - $(stat -c %Y "$d") ) / 86400 ))"
115 done
116 ;;
117 *) sed -n '2,12p' "$0" >&2; exit 2 ;;
118esac