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
GitHub aien-dev/aien-sovereign-core-mv#442 (code only)
Fetch it
curl -fsSL https://interplane.io/drakestapleton/aien-sovereign-core-mv/patches/8b513b95b2c29f73.bundle -o 8b513b95b2c29f73.bundle git fetch 8b513b95b2c29f73.bundle refs/pull/442/head
Provenance
github-import 1
aien-dev/aien-sovereign-core-mv
442
2c1fa4a3ebdeabc05116b2509b680fb31c339cf1
{"kind":"github-import","owner":"aien-dev","repo":"aien-sovereign-core-mv","number":442,"url":"https://github.com/aien-dev/aien-sovereign-core-mv/pull/442","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:37:11Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/.crumb b/.crumb index 41ace57f..511f14c4 100644 --- a/.crumb +++ b/.crumb @@ -110,17 +110,23 @@ "last_commit": "f97dc90", "name": "skills", "purpose": "" + }, + { + "digest": "b244e10fd7d13535400d96fc053a4ca93e78fe573f826188362e4eb7aa77bce1", + "last_commit": "40d01b2", + "name": "tools/aien-local-ci", + "purpose": "Local CI sandbox exec contract (C8), aien-run user/caches install, Forgejo trigger hook, health " } ], - "children_root": "sha256:c31f96f72b3b919000f27f85ac8be0d5874f3acfafb5882ecd79ffac8a02e3dd", - "compiled_at": "2026-10-11T16:45:14Z", + "children_root": "sha256:1a5d56ce42bb2bd03bc0f5bd87ee6009e8971c0cb07294376a3fb32b248071f7", + "compiled_at": "2026-10-11T17:03:07Z", "compiler": "crumb-compile/1", - "digest": "sha256:f4b11285763b81f669412c75ed20250d427db8d995a097b95408910cdc6d6382", + "digest": "sha256:80def7c3861590ed636d660faf42c16a8efc850e82599b0da39e506d9ca3ef06", "evidence_files": 187, "evidence_root": "sha256:27ee0353cb4d4aae2b7dbce2bfe0d9f4ef2a94cd7a4555b84684d59372503280", - "files": 3791, - "generated_at_commit": "8ef5dc7ce156457fd89298498b3de6ffa176449f", - "source_tree": "sha256:e558149a4ab63236eb11bc107bdde906cac5cd5570e34d2c866ac7589049f785" + "files": 3799, + "generated_at_commit": "1ae008a0a4cc64ebaec5739190ce1a8abd187419", + "source_tree": "sha256:50e1bcb93caa969288bbe3f01d61c41634394bc77a71b1abeb4139eabf990b53" } }, "history": [], diff --git a/tools/aien-local-ci/.crumb b/tools/aien-local-ci/.crumb new file mode 100644 index 00000000..1a4fafb1 --- /dev/null +++ b/tools/aien-local-ci/.crumb @@ -0,0 +1,20 @@ +{ + "extensions": { + "generated": { + "children": [], + "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "compiled_at": "2026-10-11T17:01:11Z", + "compiler": "crumb-compile/1", + "digest": "sha256:b244e10fd7d13535400d96fc053a4ca93e78fe573f826188362e4eb7aa77bce1", + "evidence_files": 0, + "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "files": 8, + "generated_at_commit": "40d01b227a99169b938d8e8f0bd2645f61c65097", + "source_tree": "sha256:792931529d95373247c7cabcfb223a84318f90560f049d9e86b8bd3be1be81e0" + } + }, + "layer": "infra", + "name": "aien-local-ci", + "purpose": "Local CI sandbox exec contract (C8), aien-run user/caches install, Forgejo trigger hook, health and gc scripts (lane INFRA)", + "schema_version": "1.0.0" +} diff --git a/tools/aien-local-ci/README.md b/tools/aien-local-ci/README.md new file mode 100644 index 00000000..45a5773d --- /dev/null +++ b/tools/aien-local-ci/README.md @@ -0,0 +1,169 @@ +# aien-local-ci: host sandbox for the sovereign local pipeline (lane INFRA) + +Owner: lane INFRA of the sovereign local pipeline mission (inventory and contracts in +`~/handoffs/2026-10-11-sovereign-pipeline-M1-inventory.md`, contracts C3, C7, C8, A2, A3). +Everything here is bash. No Python. Host systemd units are used only for transient scopes; nothing here touches +AIENOS. + +## Files + +| file | role | +|---|---| +| `aien-run-exec` | source of truth for `/usr/local/sbin/aien-run-exec`, the C8 exec contract | +| `install.sh` | idempotent host install (user, dirs, caches, rustup proxies, trusted `crumb`, exec script, sudoers) | +| `wt.sh` | trusted-side mirror + worktree management under `/var/lib/aien-run` | +| `forgejo-webhook.sh` | push trigger: Forgejo post-receive hook that writes the worker trigger file (A3) | +| `health.sh` | one-screen health (Forgejo, user, exec, crumb, home layout, caches, worktrees, scopes, disk, GPU lock, quiet flag) | +| `gc.sh` | worktree age + target-dir quota cleanup, dry run by default | +| `tests/run.sh` | proofs of the contract on this host (33 checks with two wt.sh worktrees present) | + +## C8 exec contract + +``` +sudo -n /usr/local/sbin/aien-run-exec --cwd DIR --timeout SECS --profile host|qemu|gb10 [--env K=V]... -- PROGRAM [ARGS...] +``` + +Chain (all on the privileged side, then drop): `systemd-run --scope` under `aien-run.slice` with `MemoryMax`, +`MemorySwapMax=0`, `TasksMax`, `CPUQuota`, `DevicePolicy` -> `unshare -m [-n -i]` -> mount setup (below) -> +`prlimit --core=0 --nofile=65536` -> `timeout --signal=TERM --kill-after=10 SECS` (host|qemu) -> `setpriv --reuid +aien-run --regid aien-run --init-groups --reset-env --no-new-privs` -> `env -i -C DIR PATH HOME CARGO_HOME RUSTUP_HOME +[K=V...] PROGRAM`. After PROGRAM exits the scope is killed and stopped, so a crashed check leaves no strays. +Unprivileged user namespaces are refused on this host, which is why the namespaces are created as root before +dropping privileges. + +Exit codes: PROGRAM's own code passes through; 124 = timeout (host|qemu); 137 = killed (OOM or SIGKILL); +143 = the wrapper itself got TERM/INT/HUP (the scope is killed); 2 = refused by the wrapper before anything ran. + +Mount setup inside the private mount namespace (every profile): the whole sandbox home `/var/lib/aien-run` is +read-only, except the worktree that contains `--cwd` and the target dir named by `CARGO_TARGET_DIR`, which stay +writable. Private empty tmpfs on `/var/tmp`, `/var/crash`, `/var/lib/BrlAPI` (the world-writable places on the root +disk). host|qemu also get private `/tmp`, `/dev/shm`, `/run/lock`, `/run/screen`, a fresh IPC namespace and a fresh +`/dev/mqueue`. + +| profile | memory | tasks | cpu | network | IPC, /tmp, /dev/shm | devices | timeout | +|---|---|---|---|---|---|---|---| +| host | 48G | 4096 | 1600% (16 cores) | none (fresh netns, only a down `lo`) | private | closed (null, zero, random, tty, ptmx) | yes | +| qemu | 48G | 4096 | 1600% | none | private | closed + `/dev/kvm` (aien-run is in group kvm) | yes | +| gb10 | 96G | 4096 | 1800% | host network | shared with the host | all (`/dev/nvidia*` are 0666; `/dev/dri/*` via groups video, render) | NO kill | + +Argument rules (exit 2 when broken): +* `--cwd` absolute, existing, canonical (no symlink, no `..`, no trailing slash). Inside `/var/lib/aien-run` only the + home itself or `wt/<repo>/<40-hex sha>[/...]`. +* `--env K=V`: plain names; `PATH`, `HOME`, `CARGO_HOME`, `RUSTUP_HOME`, `LD_*`, `BASH_ENV`, `ENV` refused; each name at + most once. `CARGO_TARGET_DIR` must be exactly `/var/lib/aien-run/target/<repo>/pr` or `.../main` (trust class, A2); + it is created on demand (parent root-owned, class dir aien-run). +* `PROGRAM` may not contain `=` or start with `-` (otherwise `env` would read it as one more variable assignment). +* The wrapper refuses to run if the home, `cargo`, `cargo/bin`, `rustup`, `target`, `/usr/local/bin` or `/usr/bin` + is not root-owned or is group/other-writable. + +Optional flags `--memory-max`, `--tasks-max`, `--cpu-quota` override the profile defaults (tests use `--memory-max 64M`). +Sandbox PATH is `/var/lib/aien-run/cargo/bin:/usr/local/bin:/usr/bin:/bin` (all root-owned). A bare program name +resolves only through it; a file of the same name inside the worktree is never used. + +## Trusted crumb on the sandbox PATH + +`/usr/local/bin/crumb` (root 0755) is built by `install.sh` from aien-architecture commit +`94fffbcf88492c5a1c54211b5126c8a6c370a98d` (`tools/crumb/crumb.c`, git blob `841f5317...`, checked before building; +source read from the git object store of `AIEN_ARCH_CLONE`, default `~/workspace/aien-architecture`) with +`cc -O2 -Wall -std=c11`. With gcc 13.3.0 the binary's sha256 is `22015e6b156e038e...`, identical to the operator's +`~/.local/bin/crumb` on 2026-10-11. To move the pin, change `CRUMB_COMMIT` and `CRUMB_BLOB` in `install.sh` in a PR. + +## Egress decision + +No nftables rule for uid aien-run. host and qemu checks run in a fresh network namespace with no interfaces, which is +stronger than a packet filter (no DNS, no working loopback). gb10 is the only profile with network: it keeps the host +network per the contract; a later nft rule for uid aien-run would be the way to close that and is deliberately not +added by this lane. + +## Layout under /var/lib/aien-run + +| path | owner / mode | purpose | +|---|---|---| +| `/var/lib/aien-run` | root:aien-run 0750, ACL `u:drakestapleton:r-x` | HOME of aien-run; no skeleton dotfiles | +| `cargo/` | root 0755 | CARGO_HOME (cargo works with it read-only: `--locked --offline` builds need no writes there) | +| `cargo/registry`, `cargo/git` | root, read-only | seeded once from `~/.cargo` (1.9 GB + 1.3 GB; the git cache is needed because `Cargo.lock` has 3 git sources) | +| `cargo/bin` | root | the operator's `rustup` binary + proxy symlinks; no second rustup install | +| `rustup/` | root, read-only | `settings.toml` (default 1.98.1) + `toolchains/1.98.1-aarch64-unknown-linux-gnu` copied (631 MB) | +| `.gitconfig` | root 0644 | `safe.directory = *` (git 2.43 has no path globs; aien-run reaches nothing else) | +| `mirror/<repo>.git` | drakestapleton 0755 | bare mirror of the trusted clone, refreshed by `wt.sh sync`; unwritable by aien-run | +| `wt/<repo>/<sha>` | root dir drakestapleton:aien-run 1775, `.git` root 0644, files aien-run | checkouts; aien-run can add files, cannot replace `.git` or write the mirror | +| `target/`, `target/<repo>/` | root 0755 | parents cannot be swapped by aien-run | +| `target/<repo>/{pr,main}` | aien-run | per trust class (A2); a run can write only the one it names; `main` is wiped after FAIL/timeout by the engine | + +## Trigger file contract (A3) + +`~/.local/state/aien-local-ci/trigger/<repo>.<sha>` with one line `<ref> <old> <new> <unix-time>`, written by the +Forgejo post-receive hook (`forgejo-webhook.sh install <repo>`), consumed and deleted by `aien-test worker`. +forgejo-runner stays off (its user unit is disabled); Forgejo is git hosting + PR UI + status display only. + +## Review (INFRA-2, 2026-10-11) + +Fixed in this branch (each has a test in `tests/run.sh`): +1. The sandbox could write its own home and CARGO_HOME (both were owned by aien-run): a check could plant + `/var/lib/aien-run/.cargo/config.toml` or `cargo/config.toml` (for example a `build.rustc-wrapper`), or rename the + root-owned `cargo/registry` or `cargo/bin` aside and put poisoned crates or a fake `cargo` in their place. Every later + run, including a `main` qualification, would have used them. Now the home, `cargo/` and `target/` are root-owned and + the whole home is read-only inside every run. +2. Cross trust class writes: a PR run could write `target/<repo>/main` and every other worktree (all owned by + aien-run). Now only the run's own worktree and the one target dir it names are writable. +3. `/tmp`, `/var/tmp`, `/var/crash` were shared with the operator: a check could read the operator's temporary files + and leave files behind for later runs or for root crash tooling. Now private (see the profile table for gb10). +4. SysV shared memory, semaphores and POSIX queues were shared with the host; host|qemu now get a fresh IPC namespace. +5. `PROGRAM` of the form `NAME=VALUE` was read by `env` as one more variable, which bypassed the `--env` refusals + (for example `LD_PRELOAD`). Now refused. +6. `CARGO_TARGET_DIR` and `--cwd` were not checked: a typo or a symlink planted by an earlier run inside a worktree + could point a run at another worktree or trust class. Now canonical paths only, target dir only `pr|main`. +7. `install.sh` staged the sudoers line in a predictable `/tmp/aien-sudoers.<pid>` before installing it as root. + (`fs.protected_regular=2` on this host already blocked the race; now a private `mktemp -d` directory.) +8. The wrapper ran with the caller's PATH via `/usr/bin/env bash`; it now uses `/bin/bash` and pins its own PATH. +9. Skeleton dotfiles (`.bashrc`, `.profile`, `.config/autostart`, `Desktop`) in the home were aien-run-writable; + removed. +10. The qemu profile allowed `/dev/kvm` in the cgroup policy, but aien-run was not in group `kvm` (`/dev/kvm` is + root:kvm 0660), so qemu checks could not use KVM. install.sh now adds the group; host still cannot open it. + +Checked and found sound (no change): +* aien-run cannot read `~/.aien/verify.key`, `~/.config/gh/hosts.yml`, `~/.ssh/id_ed25519` or list `~/workspace` + (0600 files, 0700 workspace; tests 10). The trusted clone lives under `~/workspace`; the sandbox only sees the mirror. +* sudoers: one exact command path, no wildcards; arguments are free by design and validated by the script; sudo's + `env_reset` plus `setpriv --reset-env` plus `env -i` clear the caller's environment (test 2). +* `setpriv --no-new-privs`: setuid programs (sudo, crontab, at) gain nothing inside a run; the scope kills every + child at the end, so nothing outlives a run. +* Repo-controlled files (`.cargo/config.toml`, `build.rs`, `rust-toolchain.toml`, test code) run as aien-run only and + cannot change the exec contract, which reads nothing from the tree. A toolchain pin other than 1.98.1 fails offline. +* The environment whitelist is minimal: PATH, HOME, CARGO_HOME, RUSTUP_HOME (cargo and the rustup proxy need the last + two to find the sandbox caches) plus explicit `--env` pairs. +* gb10 host network is the only network exception (`unshare -n` for host and qemu only). +* Operator git refuses to run inside a sandbox worktree ("dubious ownership": the `.git` file is root-owned and the + files are aien-run-owned), so a `.git` directory planted by a check cannot run hooks or config as the operator. + +Accepted risks (recorded, not changed by this lane): +* gb10 keeps host network, host `/tmp`, `/dev/shm` and IPC: a chip gate can reach local services on 127.0.0.1 (Forgejo + 3443/2222, Cortex 18080 need credentials it cannot read) and the internet, and can leave files in `/tmp`. + Closing it needs either a nft rule for uid aien-run or a netns for gb10 after the chip gates are proven inside one. +* If the caller kills the wrapper during a gb10 run, the trap kills the chip run too (no strays). The engine must not + signal a gb10 run; it only marks its advisory timeout in the receipt. +* The operator account has `(ALL) NOPASSWD: ALL` in sudoers, so the narrow `aien-local-ci` rule is documentation of + intent, not a limit, until that broad rule is removed (operator decision, outside this lane). +* The operator's home is 0755 (ACL user atlas `--x`); its secrets are 0600 and verified unreadable. +* Same-SHA reuse: a PR run and a `main` qualification of the same commit share one worktree path. The engine should + remove and re-add the worktree (fresh checkout) before a `main` qualification. +* `git` write operations as aien-run inside a worktree fail (the index lives in the read-only mirror). Gates must not + need them. +* A run's HOME is read-only: a gate that writes to `$HOME` fails; write to `/tmp` (private for host|qemu) instead. +* Worktree root dirs inherit setgid from the mirror layout (`drwxrwsr-t`); harmless. + +## Operate + +``` +tools/aien-local-ci/install.sh # idempotent; AIEN_ARCH_CLONE=<aien-architecture clone> for crumb +tools/aien-local-ci/tests/run.sh # all proofs, about 10 s +tools/aien-local-ci/wt.sh sync aien-sovereign-core ~/workspace/aien-sovereign-core +tools/aien-local-ci/wt.sh add aien-sovereign-core <sha> +tools/aien-local-ci/health.sh +tools/aien-local-ci/gc.sh [--apply] [--days 3] [--target-quota-gb 120] +``` + +Measured 2026-10-11 (sovereign-core 5ef7720, 20 cores, caches seeded): cold `cargo build --workspace --all-targets +--locked --offline` as aien-run 65.0 s (target dir 27 GB); warm rebuild after touching one crate 2.2 s. After the +read-only home change: full workspace build of worktree 62542eb into the existing `pr` target 49.9 s (new worktree +path, so workspace crates rebuilt; dependencies reused), exit 0. diff --git a/tools/aien-local-ci/aien-run-exec b/tools/aien-local-ci/aien-run-exec new file mode 100755 index 00000000..fddab2fc --- /dev/null +++ b/tools/aien-local-ci/aien-run-exec @@ -0,0 +1,195 @@ +#!/bin/bash +# aien-run-exec: the C8 exec contract of the sovereign local pipeline (lane INFRA). +# +# aien-run-exec --cwd DIR --timeout SECS --profile host|qemu|gb10 [--env K=V]... -- PROGRAM [ARGS...] +# +# Runs PROGRAM as the unprivileged system user `aien-run` with: +# * a cleared environment: only PATH, HOME, CARGO_HOME, RUSTUP_HOME and the explicit --env pairs survive; +# * a private mount namespace in which the whole sandbox home /var/lib/aien-run is READ-ONLY except +# (a) the one worktree that contains --cwd and (b) the one target dir named by --env CARGO_TARGET_DIR. +# So a check can never plant files that a later check (another PR, or a `main` qualification) would +# pick up: no ~/.cargo/config.toml, no swapped registry or cargo binary, no edits to another worktree, +# no writes into the other trust class's target dir; +# * private, empty /var/tmp, /var/crash, /var/lib/BrlAPI for every profile, and private /tmp, /dev/shm, /run/lock, +# /run/screen plus a fresh IPC namespace (SysV shm, semaphores, POSIX queues) for host|qemu (gb10 keeps the +# host /tmp, /dev/shm and IPC so chip tooling behaves as before); +# * a fresh network namespace for host|qemu (no interfaces except a down loopback, no DNS); +# * a transient systemd scope under aien-run.slice with cgroup caps (memory, pids, cpu) and a +# device policy (host|qemu: standard pseudo devices only, qemu adds /dev/kvm; gb10: all devices); +# * rlimits (no core dumps, bounded open files); +# * `timeout --signal=TERM --kill-after=10 SECS` for host|qemu (exit 124 on expiry); +# * NO timeout for gb10: chip runs are never killed; the engine's advisory timeout only marks the receipt. +# stdout/stderr pass through. The exit code of PROGRAM passes through (124 = timeout, 137 = killed). +# Exit 2 = refused by this script (bad arguments or unsafe host layout); PROGRAM did not run. +# When the command finishes the whole scope is stopped, so a crashed check leaves no stray processes. +# +# Argument rules (all checked before anything runs): +# --cwd absolute, existing and canonical (no symlinks, no trailing slash). Inside /var/lib/aien-run it +# must be /var/lib/aien-run itself or inside wt/<repo>/<40-hex sha>; that worktree is the only +# writable one. +# --env K=V with a plain name; PATH, HOME, CARGO_HOME, RUSTUP_HOME, LD_*, BASH_ENV, ENV refused; +# each name at most once. CARGO_TARGET_DIR, if given, must be exactly +# /var/lib/aien-run/target/<repo>/pr or .../main (the trust class, A2); it is created if missing. +# PROGRAM may not contain '=' or start with '-' (so it can never be read as an extra environment +# assignment by `env`). A bare name resolves only through the sandbox PATH +# /var/lib/aien-run/cargo/bin:/usr/local/bin:/usr/bin:/bin (all root-owned, checked at start); +# a file of the same name inside the worktree is never picked up. +# +# Optional extensions (not required by C8, used by tests and the engine): +# --memory-max BYTES|NUM[KMG] override the per-profile MemoryMax (tests use a small cap) +# --tasks-max N override TasksMax +# --cpu-quota PCT override CPUQuota (percent, 100 = one core) +# +# Installed root-owned 0755 at /usr/local/sbin/aien-run-exec by tools/aien-local-ci/install.sh. +# Invoked by the trusted side as: sudo -n /usr/local/sbin/aien-run-exec ... (see /etc/sudoers.d/aien-local-ci). +set -euo pipefail +PATH=/usr/sbin:/usr/bin:/sbin:/bin +export PATH +umask 022 + +RUN_USER=aien-run +RUN_HOME=/var/lib/aien-run +RUN_PATH=/var/lib/aien-run/cargo/bin:/usr/local/bin:/usr/bin:/bin +SLICE=aien-run.slice +KILL_AFTER=10 +NAME_RE='^[A-Za-z0-9._-]+$' + +die() { printf 'aien-run-exec: %s\n' "$*" >&2; exit 2; } +usage() { + printf 'usage: aien-run-exec --cwd DIR --timeout SECS --profile host|qemu|gb10 [--env K=V]... -- PROGRAM [ARGS...]\n' >&2 + exit 2 +} +plain_name() { [[ $1 =~ $NAME_RE && $1 != . && $1 != .. ]]; } + +[[ $(id -u) -eq 0 ]] || die "must run as root (via sudo -n)" + +cwd=""; secs=""; profile=""; envs=(); mem_max=""; tasks_max=""; cpu_quota="" +while [[ $# -gt 0 ]]; do + case "$1" in + --cwd) [[ $# -ge 2 ]] || usage; cwd=$2; shift 2 ;; + --timeout) [[ $# -ge 2 ]] || usage; secs=$2; shift 2 ;; + --profile) [[ $# -ge 2 ]] || usage; profile=$2; shift 2 ;; + --env) [[ $# -ge 2 ]] || usage; envs+=("$2"); shift 2 ;; + --memory-max) [[ $# -ge 2 ]] || usage; mem_max=$2; shift 2 ;; + --tasks-max) [[ $# -ge 2 ]] || usage; tasks_max=$2; shift 2 ;; + --cpu-quota) [[ $# -ge 2 ]] || usage; cpu_quota=$2; shift 2 ;; + --) shift; break ;; + -h|--help) usage ;; + *) die "unknown argument: $1" ;; + esac +done +[[ $# -ge 1 ]] || usage +[[ -n $cwd && -n $secs && -n $profile ]] || usage +[[ $secs =~ ^[0-9]+$ && $secs -gt 0 ]] || die "--timeout must be a positive integer of seconds: $secs" +case "$profile" in host|qemu|gb10) ;; *) die "--profile must be host|qemu|gb10: $profile" ;; esac +[[ -z $mem_max || $mem_max =~ ^[0-9]+[KMGT]?$ ]] || die "--memory-max must be NUM[KMGT]: $mem_max" +[[ -z $tasks_max || $tasks_max =~ ^[0-9]+$ ]] || die "--tasks-max must be an integer: $tasks_max" +[[ -z $cpu_quota || $cpu_quota =~ ^[0-9]+$ ]] || die "--cpu-quota must be an integer percent: $cpu_quota" +[[ -n $1 && $1 != *=* && $1 != -* ]] || die "PROGRAM may not be empty, contain '=' or start with '-': $1" + +# --cwd: canonical, and inside the sandbox home only the home itself or a worktree. +[[ $cwd == /* && -d $cwd ]] || die "--cwd must be an existing absolute directory: $cwd" +[[ $(realpath -e -- "$cwd") == "$cwd" ]] || die "--cwd must be canonical (no symlinks, no trailing slash): $cwd" +wtroot="" +case "$cwd" in + "$RUN_HOME") ;; + "$RUN_HOME"/wt/*/*) + rel=${cwd#"$RUN_HOME"/wt/}; repo=${rel%%/*}; rest=${rel#*/}; sha=${rest%%/*} + plain_name "$repo" && [[ $sha =~ ^[0-9a-f]{40}$ ]] || die "--cwd must be inside $RUN_HOME/wt/<repo>/<40-hex sha>: $cwd" + wtroot=$RUN_HOME/wt/$repo/$sha ;; + "$RUN_HOME"/*) die "--cwd inside $RUN_HOME must be the home itself or inside wt/<repo>/<sha>: $cwd" ;; +esac + +# --env: plain names, refused names, no duplicates, CARGO_TARGET_DIR only as target/<repo>/pr|main. +tdir=""; seen=" " +for kv in "${envs[@]}"; do + [[ $kv =~ ^[A-Za-z_][A-Za-z0-9_]*= ]] || die "--env needs K=V with a valid name: $kv" + k=${kv%%=*} + case "$k" in + LD_*|BASH_ENV|ENV|HOME|CARGO_HOME|RUSTUP_HOME|PATH) die "--env may not set $k" ;; + esac + [[ $seen != *" $k "* ]] || die "--env sets $k twice" + seen+="$k " + if [[ $k == CARGO_TARGET_DIR ]]; then + tdir=${kv#*=} + trepo=${tdir#"$RUN_HOME"/target/}; trepo=${trepo%/*} + [[ $tdir == "$RUN_HOME/target/$trepo/pr" || $tdir == "$RUN_HOME/target/$trepo/main" ]] && plain_name "$trepo" \ + || die "CARGO_TARGET_DIR must be $RUN_HOME/target/<repo>/pr or .../main: $tdir" + fi +done +getent passwd "$RUN_USER" >/dev/null || die "user $RUN_USER does not exist (run tools/aien-local-ci/install.sh)" + +# Host layout self-check: everything the sandbox could use to influence a later run must be root-owned and not +# writable by group/other (the mount namespace below is the main guard; this catches a broken install). +for p in "$RUN_HOME" "$RUN_HOME/cargo" "$RUN_HOME/cargo/bin" "$RUN_HOME/rustup" "$RUN_HOME/target" /usr/local/bin /usr/bin; do + read -r u m < <(stat -L -c '%u %a' "$p" 2>/dev/null || echo "x x") + [[ $u == 0 ]] || die "unsafe host layout: $p is not root-owned (re-run tools/aien-local-ci/install.sh)" + (( (8#$m & 8#022) == 0 )) || die "unsafe host layout: $p is group/other-writable ($m)" +done + +# Target dir of the requested trust class: created on demand (parent root-owned, the class dir aien-run). +if [[ -n $tdir ]]; then + if [[ ! -d $tdir ]]; then + install -d -o root -g root -m 0755 "${tdir%/*}" + install -d -o "$RUN_USER" -g "$RUN_USER" -m 0755 "$tdir" + fi + [[ $(realpath -e -- "$tdir") == "$tdir" ]] || die "CARGO_TARGET_DIR must be canonical: $tdir" +fi + +# Per-profile caps. Machine: 20 cores, 121 GB RAM. host/qemu leave headroom for the trusted side; +# gb10 may use most of the unified memory but still cannot take the whole machine. +case "$profile" in + host) : "${mem_max:=48G}"; : "${tasks_max:=4096}"; : "${cpu_quota:=1600}" ;; + qemu) : "${mem_max:=48G}"; : "${tasks_max:=4096}"; : "${cpu_quota:=1600}" ;; + gb10) : "${mem_max:=96G}"; : "${tasks_max:=4096}"; : "${cpu_quota:=1800}" ;; +esac + +unit="aien-run-exec-$$-$(date +%s%N | tail -c 7)" +props=(-p "MemoryMax=$mem_max" -p "MemorySwapMax=0" -p "TasksMax=$tasks_max" -p "CPUQuota=${cpu_quota}%") +case "$profile" in + host) props+=(-p DevicePolicy=closed) ;; + qemu) props+=(-p DevicePolicy=closed -p "DeviceAllow=/dev/kvm rw") ;; + gb10) props+=(-p DevicePolicy=auto) ;; +esac + +# Mount setup, run as root inside the new mount namespace. Parameters are positional, never interpolated. +# Order matters: the writable pieces are bound onto themselves first, then the home is bound recursively onto +# itself and only that top mount is made read-only, so the self-binds underneath stay writable. +# shellcheck disable=SC2016 +NS_SETUP=' +set -euo pipefail +home=$1 profile=$2 wtroot=$3 tdir=$4; shift 4 +for p in "$wtroot" "$tdir"; do [[ -z $p ]] || mount --bind "$p" "$p"; done +mount --rbind "$home" "$home" +mount -o remount,bind,ro "$home" +priv=(/var/tmp /var/crash /var/lib/BrlAPI) +case $profile in host|qemu) priv+=(/tmp /dev/shm /run/lock /run/screen) ;; esac +for d in "${priv[@]}"; do + if [[ -d $d ]]; then mount -t tmpfs -o mode=1777,nosuid,nodev aien-run-tmp "$d"; fi +done +case $profile in host|qemu) mount -t mqueue -o nosuid,nodev,noexec aien-run-mqueue /dev/mqueue ;; esac +exec "$@" +' + +inner=() +case "$profile" in + host|qemu) inner+=(unshare -m -n -i --propagation private --) ;; + gb10) inner+=(unshare -m --propagation private --) ;; +esac +inner+=(bash -c "$NS_SETUP" aien-run-ns "$RUN_HOME" "$profile" "$wtroot" "$tdir") +inner+=(prlimit --core=0 --nofile=65536 --) +case "$profile" in + host|qemu) inner+=(timeout --signal=TERM --kill-after="$KILL_AFTER" "$secs") ;; +esac +inner+=(setpriv --reuid "$RUN_USER" --regid "$RUN_USER" --init-groups --reset-env --no-new-privs --) +inner+=(env -i -C "$cwd" "PATH=$RUN_PATH" "HOME=$RUN_HOME" "CARGO_HOME=$RUN_HOME/cargo" "RUSTUP_HOME=$RUN_HOME/rustup") +for kv in "${envs[@]}"; do inner+=("$kv"); done + +cleanup() { systemctl kill --signal=KILL "$unit.scope" >/dev/null 2>&1 || true; systemctl stop "$unit.scope" >/dev/null 2>&1 || true; } +trap 'cleanup; exit 143' TERM INT HUP + +rc=0 +cd / +systemd-run --scope --quiet --collect --unit "$unit" --slice "$SLICE" "${props[@]}" -- "${inner[@]}" "$@" || rc=$? +cleanup +exit "$rc" diff --git a/tools/aien-local-ci/forgejo-webhook.sh b/tools/aien-local-ci/forgejo-webhook.sh new file mode 100755 index 00000000..86bc4ab2 --- /dev/null +++ b/tools/aien-local-ci/forgejo-webhook.sh @@ -0,0 +1,85 @@ +#!/usr/bin/env bash +# forgejo-webhook.sh: offline push trigger for the trusted worker (A3). +# +# Trigger file contract (consumed by `aien-test worker`, lane ENGINE): +# directory /home/drakestapleton/.local/state/aien-local-ci/trigger/ +# file name <repo>.<sha> (repo = Forgejo repository name, sha = full 40-hex new commit) +# content one line: "<ref> <old-sha> <new-sha> <unix-time>" (ref = refs/heads/<branch>) +# semantics the worker treats the file's presence as "please verify <repo> at <sha>"; it deletes the file when +# the run is recorded. A deleted branch (new-sha all zeros) writes no file. Re-pushing the same sha +# rewrites the same file (idempotent). No network, no HTTP receiver: Forgejo's post-receive hook +# runs as drakestapleton on this host and writes the file directly. +# +# forgejo-webhook.sh install <repo> install the post-receive hook into the Forgejo bare repo +# /home/drakestapleton/atlas-forgejo/data/git/repositories/drake/<repo>.git +# (hooks/post-receive runs every hooks/post-receive.d/*, Forgejo's own layout, +# so a later "resynchronize hooks" from the admin page keeps our script) +# forgejo-webhook.sh hook <repo> the hook body (reads "old new ref" lines on stdin) +# forgejo-webhook.sh list show pending trigger files +set -euo pipefail +TRIGGER_DIR=/home/drakestapleton/.local/state/aien-local-ci/trigger +REPOS=/home/drakestapleton/atlas-forgejo/data/git/repositories/drake + +case "${1:-}" in + hook) + repo=${2:?repo} + mkdir -p "$TRIGGER_DIR" + now=$(date +%s) + while read -r old new ref; do + [[ $new =~ ^[0-9a-f]{40}$ ]] || continue + [[ $new != 0000000000000000000000000000000000000000 ]] || continue + [[ $ref == refs/heads/* ]] || continue + tmp="$TRIGGER_DIR/.$repo.$new.$$" + printf '%s %s %s %s\n' "$ref" "$old" "$new" "$now" > "$tmp" + mv -f "$tmp" "$TRIGGER_DIR/$repo.$new" + done + ;; + install) + repo=${2:?repo} + [[ $repo =~ ^[A-Za-z0-9._-]+$ ]] || { echo "bad repo" >&2; exit 2; } + bare="$REPOS/$repo.git" + [[ -d $bare && -f $bare/HEAD ]] || { echo "no bare repo at $bare" >&2; exit 2; } + mkdir -p "$bare/hooks/post-receive.d" + if [[ ! -f $bare/hooks/post-receive ]]; then + cat > "$bare/hooks/post-receive" <<'EOF' +#!/usr/bin/env bash +data=$(cat) +exitcodes="" +hookname=$(basename "$0") +GIT_DIR=${GIT_DIR:-$(dirname "$0")/..} +for hook in "$GIT_DIR"/hooks/"$hookname".d/*; do + test -x "$hook" && test -f "$hook" || continue + echo "$data" | "$hook" + exitcodes="$exitcodes $?" +done +for i in $exitcodes; do + [ "$i" -eq 0 ] || exit "$i" +done +EOF + chmod 0755 "$bare/hooks/post-receive" + fi + { + printf '#!/usr/bin/env bash\n# installed by tools/aien-local-ci/forgejo-webhook.sh (sovereign-core); writes the worker trigger file\n' + printf 'TRIGGER_DIR=%q\nrepo=%q\n' "$TRIGGER_DIR" "$repo" + cat <<'EOF' +mkdir -p "$TRIGGER_DIR" +now=$(date +%s) +while read -r old new ref; do + [[ $new =~ ^[0-9a-f]{40}$ ]] || continue + [[ $new != 0000000000000000000000000000000000000000 ]] || continue + [[ $ref == refs/heads/* ]] || continue + tmp="$TRIGGER_DIR/.$repo.$new.$$" + printf '%s %s %s %s\n' "$ref" "$old" "$new" "$now" > "$tmp" + mv -f "$tmp" "$TRIGGER_DIR/$repo.$new" +done +exit 0 +EOF + } > "$bare/hooks/post-receive.d/aien-local-ci" + chmod 0755 "$bare/hooks/post-receive.d/aien-local-ci" + echo "installed $bare/hooks/post-receive.d/aien-local-ci -> $TRIGGER_DIR/$repo.<sha>" + ;; + list) + ls -l "$TRIGGER_DIR" 2>/dev/null || echo "no trigger dir" + ;; + *) sed -n '2,18p' "$0" >&2; exit 2 ;; +esac diff --git a/tools/aien-local-ci/gc.sh b/tools/aien-local-ci/gc.sh new file mode 100755 index 00000000..5c953c10 --- /dev/null +++ b/tools/aien-local-ci/gc.sh @@ -0,0 +1,59 @@ +#!/usr/bin/env bash +# gc.sh: storage quota + cleanup for the aien-run sandbox (lane INFRA). DRY RUN by default; pass --apply to act. +# +# gc.sh [--apply] [--days N] [--target-quota-gb G] +# +# Policy (documented here, chosen by lane INFRA): +# * worktrees under /var/lib/aien-run/wt/<repo>/<sha> older than N days (default 3, by mtime of the root dir) +# are removed and `git worktree prune` is run on the mirror. A worktree some process still has as its cwd +# (a running check) is skipped. +# * target dirs /var/lib/aien-run/target/<repo>/{pr,main}: the per-repo total is capped at G GB (default 120 GB, +# about 1/3 of the free disk on 2026-10-11; one sovereign-core all-targets dev build is ~30 GB). When over quota +# the `pr` dir is wiped first (shared cache, rebuilt on the next PR), then `main` (A2 says `main` is wiped after +# any FAIL/timeout anyway). Wiping never touches cargo/registry, cargo/git or rustup (root-owned, read-only). +# * stale trigger files older than 7 days are reported, not deleted (the worker owns them). +set -euo pipefail +RUN_HOME=/var/lib/aien-run +apply=0; days=3; quota_gb=120 +while [[ $# -gt 0 ]]; do + case "$1" in + --apply) apply=1; shift ;; + --days) days=$2; shift 2 ;; + --target-quota-gb) quota_gb=$2; shift 2 ;; + *) echo "usage: gc.sh [--apply] [--days N] [--target-quota-gb G]" >&2; exit 2 ;; + esac +done +act() { if (( apply )); then "$@"; else printf 'DRY-RUN: %s\n' "$*"; fi; } +now=$(date +%s) + +echo "== worktrees older than $days days" +for d in "$RUN_HOME"/wt/*/*/; do + [[ -d $d ]] || continue + age=$(( (now - $(stat -c %Y "$d")) / 86400 )) + (( age >= days )) || continue + if sudo -n ls -l /proc/[0-9]*/cwd 2>/dev/null | grep -q -- " -> ${d%/}"; then + echo "skip (in use): $d"; continue + fi + repo=$(basename "$(dirname "$d")") + act sudo -n rm -rf "$d" + act git -C "$RUN_HOME/mirror/$repo.git" worktree prune +done + +echo "== target quota ${quota_gb} GB per repo" +for r in "$RUN_HOME"/target/*/; do + [[ -d $r ]] || continue + used_gb=$(( $(sudo -n du -sk "$r" | cut -f1) / 1024 / 1024 )) + printf '%s: %s GB\n' "$r" "$used_gb" + (( used_gb > quota_gb )) || continue + for sub in pr main; do + [[ -d $r/$sub ]] || continue + act sudo -n find "$r/$sub" -mindepth 1 -delete + (( apply )) || break + used_gb=$(( $(sudo -n du -sk "$r" | cut -f1) / 1024 / 1024 )) + (( used_gb > quota_gb )) || break + done +done + +echo "== stale triggers (>7 days, report only)" +find /home/drakestapleton/.local/state/aien-local-ci/trigger -type f -mtime +7 2>/dev/null || true +(( apply )) || echo "(dry run; add --apply to act)" diff --git a/tools/aien-local-ci/health.sh b/tools/aien-local-ci/health.sh new file mode 100755 index 00000000..58078eda --- /dev/null +++ b/tools/aien-local-ci/health.sh @@ -0,0 +1,65 @@ +#!/usr/bin/env bash +# health.sh: one-screen health of the sovereign local pipeline host pieces (lane INFRA). Read-only. +set -uo pipefail +RUN_HOME=/var/lib/aien-run +FORGEJO_DATA=/home/drakestapleton/atlas-forgejo/data +FORGEJO_PORT=$(grep -E '^HTTP_PORT' "$FORGEJO_DATA/custom/conf/app.ini" 2>/dev/null | awk -F= '{gsub(/ /,"",$2); print $2}') +FORGEJO_PORT=${FORGEJO_PORT:-3443} +STATE=/home/drakestapleton/.local/state/aien-local-ci +GPU_LOCK=/tmp/aien-gb10.lock +SLOTS=/home/drakestapleton/.local/state/aien-proof/slots + +row() { printf '%-22s %s\n' "$1" "$2"; } + +if mountpoint -q "$FORGEJO_DATA"; then row "forgejo storage" "mounted ($FORGEJO_DATA)"; else row "forgejo storage" "NOT MOUNTED"; fi +code=$(curl -sk -m 3 -o /dev/null -w '%{http_code}' "https://127.0.0.1:$FORGEJO_PORT/" 2>/dev/null) +if [[ $code == 200 ]]; then row "forgejo web" "up (https://127.0.0.1:$FORGEJO_PORT, $(systemctl is-active atlas-forgejo.service 2>/dev/null))"; else row "forgejo web" "DOWN (http $code, unit $(systemctl is-active atlas-forgejo.service 2>/dev/null))"; fi +if (exec 3<>/dev/tcp/127.0.0.1/2222) 2>/dev/null; then row "forgejo ssh" "listening 127.0.0.1:2222"; else row "forgejo ssh" "NOT listening on 2222"; fi +row "forgejo-runner" "$(systemctl --user is-active forgejo-runner.service 2>/dev/null) (enabled: $(systemctl --user is-enabled forgejo-runner.service 2>/dev/null)); must stay off" + +if u=$(getent passwd aien-run); then + row "aien-run user" "ok uid=$(cut -d: -f3 <<<"$u") groups=$(id -nG aien-run | tr ' ' ',') shell=$(cut -d: -f7 <<<"$u")" +else + row "aien-run user" "MISSING" +fi +[[ -x /usr/local/sbin/aien-run-exec ]] && row "aien-run-exec" "$(stat -c '%U %a' /usr/local/sbin/aien-run-exec) $(sudo -n -l /usr/local/sbin/aien-run-exec >/dev/null 2>&1 && echo 'sudo ok' || echo 'SUDO RULE MISSING')" || row "aien-run-exec" "MISSING" +if [[ -x /usr/local/bin/crumb ]]; then + want=$(sed -n 's/^CRUMB_COMMIT=//p' "$(dirname "$0")/install.sh" 2>/dev/null | cut -c1-7) + row "crumb (sandbox)" "/usr/local/bin/crumb $(stat -c '%U %a' /usr/local/bin/crumb) sha256 $(sha256sum < /usr/local/bin/crumb | cut -c1-12) (install.sh pins aien-architecture ${want:-?})" +else + row "crumb (sandbox)" "MISSING (run install.sh)" +fi +hl=$(stat -c '%U:%G %a' "$RUN_HOME" "$RUN_HOME/cargo" "$RUN_HOME/target" 2>/dev/null | tr '\n' ' ') +[[ $hl == "root:aien-run 750 root:root 755 root:root 755 " ]] && row "sandbox home layout" "root-owned (home, cargo, target)" || row "sandbox home layout" "UNSAFE: $hl(re-run install.sh)" + +for c in cargo/registry cargo/git rustup/toolchains; do + if [[ -d $RUN_HOME/$c ]]; then row "cache $c" "$(sudo -n du -sh "$RUN_HOME/$c" 2>/dev/null | cut -f1) $(stat -c '%U %a' "$RUN_HOME/$c")"; else row "cache $c" "MISSING"; fi +done +for t in "$RUN_HOME"/target/*/*/; do [[ -d $t ]] && row "target ${t#"$RUN_HOME"/target/}" "$(sudo -n du -sh "$t" 2>/dev/null | cut -f1)"; done +n=0; for w in "$RUN_HOME"/wt/*/*/; do [[ -d $w ]] && n=$((n+1)); done +row "worktrees" "$n under $RUN_HOME/wt" +row "mirrors" "$(ls "$RUN_HOME/mirror" 2>/dev/null | tr '\n' ' ')" +row "scopes running" "$(systemctl list-units --no-legend --plain 'aien-run-exec-*.scope' 2>/dev/null | wc -l)" +row "disk free /" "$(df -h / | awk 'NR==2{print $4" of "$2" ("$5" used)"}')" +row "triggers pending" "$(ls "$STATE/trigger" 2>/dev/null | wc -l)" + +if [[ -e $GPU_LOCK ]]; then + holder=$(cat "$GPU_LOCK" 2>/dev/null | head -c 120) + if command -v fuser >/dev/null && fuser "$GPU_LOCK" >/dev/null 2>&1; then + row "gpu lock" "HELD by pid(s) $(fuser "$GPU_LOCK" 2>/dev/null | tr -s ' ') ${holder:+[$holder]}" + else + row "gpu lock" "file present, no live holder (flock free) ${holder:+[$holder]}" + fi +else + row "gpu lock" "absent" +fi +for s in "$SLOTS"/*.lock; do + [[ -e $s ]] || continue + if fuser "$s" >/dev/null 2>&1; then row "slot $(basename "$s")" "HELD by pid $(fuser "$s" 2>/dev/null | tr -s ' ')"; else row "slot $(basename "$s")" "free"; fi +done +if command -v quietlock >/dev/null; then + q=$(quietlock check 2>&1 | head -1) + if quietlock check >/dev/null 2>&1; then row "quiet flag" "clear${q:+ ($q)}"; else row "quiet flag" "RAISED${q:+ ($q)}"; fi +else + row "quiet flag" "quietlock not installed" +fi diff --git a/tools/aien-local-ci/install.sh b/tools/aien-local-ci/install.sh new file mode 100755 index 00000000..5aef3053 --- /dev/null +++ b/tools/aien-local-ci/install.sh @@ -0,0 +1,135 @@ +#!/usr/bin/env bash +# install.sh: idempotent host setup for the sovereign local pipeline sandbox (lane INFRA, contracts C3/C8/A2). +# Run as drakestapleton; uses sudo -n inside. Safe to re-run: every step checks before it acts. +# +# What it sets up on the host: +# * system user aien-run (home /var/lib/aien-run root:aien-run 0750 + ACL u:drakestapleton:r-x, nologin, +# groups video+render). The home and everything a later run could read config from is root-owned, so a +# check cannot plant ~/.cargo/config.toml, swap the registry or replace the cargo proxies (review finding 1). +# * /var/lib/aien-run/{cargo,target,wt,mirror,rustup} with the ownership described in README.md +# * cargo registry + git cache seeded from ~/.cargo (root-owned, read-only), toolchain copied from ~/.rustup +# (root-owned, read-only), rustup proxies in /var/lib/aien-run/cargo/bin (no second rustup install) +# * /usr/local/bin/crumb (root 0755), built from aien-architecture at CRUMB_COMMIT below (the trusted copy +# on the sandbox PATH; never the operator's ~/.local/bin copy, never a copy from a tree under test) +# * /usr/local/sbin/aien-run-exec (root 0755) and /etc/sudoers.d/aien-local-ci (validated with visudo -c) +# * ~/.local/state/aien-local-ci/{trigger,evidence,bundles} for the trusted side +set -euo pipefail +here=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +RUN_USER=aien-run +RUN_HOME=/var/lib/aien-run +OPERATOR=${AIEN_OPERATOR:-drakestapleton} +OP_HOME=$(getent passwd "$OPERATOR" | cut -d: -f6) +TOOLCHAIN=${AIEN_TOOLCHAIN:-1.98.1-aarch64-unknown-linux-gnu} +STATE="$OP_HOME/.local/state/aien-local-ci" +# crumb source: aien-architecture main at this commit (2026-10-11, on GitHub main), file tools/crumb/crumb.c +# (git blob 841f5317480b6643086061585c4309097975d8f9). Built with gcc 13.3.0 the binary's sha256 is +# 22015e6b156e038ecea26ca8cf5c03f944d0c526282c4314fc5891751ffa67b0 (same as the operator's copy that day). +CRUMB_COMMIT=94fffbcf88492c5a1c54211b5126c8a6c370a98d +CRUMB_BLOB=841f5317480b6643086061585c4309097975d8f9 +ARCH_CLONE=${AIEN_ARCH_CLONE:-$OP_HOME/workspace/aien-architecture} + +say() { printf '[install] %s\n' "$*"; } +S() { sudo -n "$@"; } + +[[ $(id -un) == "$OPERATOR" ]] || { echo "run as $OPERATOR" >&2; exit 1; } +sudo -n true 2>/dev/null || { echo "passwordless sudo required" >&2; exit 1; } +# private scratch dir (never a predictable /tmp name: the sudoers line is staged here before install) +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT + +# 1. user and groups (device groups: /dev/dri/card0 is root:video, /dev/dri/renderD128 is root:render, +# /dev/kvm is root:kvm 0660 for the qemu profile, /dev/nvidia* are 0666 root:root so they need no group). +# The cgroup device policy still keeps /dev/kvm closed for the host profile. +if ! getent passwd "$RUN_USER" >/dev/null; then + S useradd --system --home-dir "$RUN_HOME" --create-home --shell /usr/sbin/nologin --user-group \ + --groups video,render "$RUN_USER" + say "created user $RUN_USER" +else + say "user $RUN_USER exists" +fi +for g in video render kvm; do + getent group "$g" >/dev/null || continue + id -nG "$RUN_USER" | tr ' ' '\n' | grep -qx "$g" || S usermod -aG "$g" "$RUN_USER" +done +# home: root-owned, group aien-run (traverse), operator via ACL; skeleton dotfiles removed (nothing may be +# sourced or read from an aien-run-writable file in the home). +S chown root:"$RUN_USER" "$RUN_HOME" +S chmod 0750 "$RUN_HOME" +S setfacl -m "u:$OPERATOR:r-x" "$RUN_HOME" +for f in .bashrc .profile .bash_logout .config Desktop; do + if S test -e "$RUN_HOME/$f"; then S rm -rf "${RUN_HOME:?}/$f"; say "removed skeleton $RUN_HOME/$f"; fi +done + +# 2. directory layout (cargo/ and target/ root-owned; only target/<repo>/{pr,main} belong to aien-run) +S install -d -o root -g root -m 0755 "$RUN_HOME/cargo" "$RUN_HOME/target" +S chown root:root "$RUN_HOME/cargo" "$RUN_HOME/target"; S chmod 0755 "$RUN_HOME/cargo" "$RUN_HOME/target" +for f in "$RUN_HOME"/cargo/.package-cache "$RUN_HOME"/cargo/.package-cache-mutate "$RUN_HOME"/cargo/.global-cache; do + if S test -e "$f"; then S chown root:root "$f"; S chmod 0644 "$f"; fi +done +for r in "$RUN_HOME"/target/*/; do + [[ -d $r ]] || continue + S chown root:root "$r"; S chmod 0755 "$r" + for c in pr main; do S install -d -o "$RUN_USER" -g "$RUN_USER" -m 0755 "$r$c"; done +done +S install -d -o "$OPERATOR" -g "$OPERATOR" -m 0755 "$RUN_HOME/wt" "$RUN_HOME/mirror" +S install -d -o root -g root -m 0755 "$RUN_HOME/rustup" "$RUN_HOME/rustup/toolchains" "$RUN_HOME/rustup/update-hashes" "$RUN_HOME/rustup/downloads" "$RUN_HOME/rustup/tmp" +install -d -m 0755 "$STATE/trigger" "$STATE/evidence" "$STATE/bundles" + +# 3. caches: registry + git cache copied once from the operator's ~/.cargo, then root-owned read-only. +seed_ro() { # seed_ro SRC DST + local src=$1 dst=$2 + if [[ ! -e $dst ]]; then + [[ -d $src ]] || { say "skip $dst (no $src)"; return; } + S rm -rf "$dst.tmp" + S cp -a "$src" "$dst.tmp" + S mv "$dst.tmp" "$dst" + say "seeded $dst from $src" + fi + S chown -R root:root "$dst" + S chmod -R a+rX,u-w,go-w "$dst" +} +seed_ro "$OP_HOME/.cargo/registry" "$RUN_HOME/cargo/registry" +seed_ro "$OP_HOME/.cargo/git" "$RUN_HOME/cargo/git" +seed_ro "$OP_HOME/.rustup/toolchains/$TOOLCHAIN" "$RUN_HOME/rustup/toolchains/$TOOLCHAIN" + +# 4. rustup: the operator's rustup binary acts as the proxy (cargo/rustc/... are symlinks to it), with +# RUSTUP_HOME=/var/lib/aien-run/rustup holding only settings.toml + the copied toolchain. No rustup-init, +# no downloads: rust-toolchain.toml pins resolve against the copied toolchain only. +S install -d -o root -g root -m 0755 "$RUN_HOME/cargo/bin" +S install -o root -g root -m 0755 "$OP_HOME/.cargo/bin/rustup" "$RUN_HOME/cargo/bin/rustup" +for p in cargo rustc rustdoc rustfmt cargo-fmt cargo-clippy clippy-driver rust-gdb rust-lldb; do + S ln -sfn rustup "$RUN_HOME/cargo/bin/$p" +done +printf 'version = "12"\ndefault_toolchain = "%s"\nprofile = "minimal"\n\n[overrides]\n' "$TOOLCHAIN" > "$tmp/settings.toml" +S install -o root -g root -m 0644 "$tmp/settings.toml" "$RUN_HOME/rustup/settings.toml" +S chown root:root "$RUN_HOME/rustup"; S chmod 0755 "$RUN_HOME/rustup" + +# 4b. git safe.directory for aien-run: worktree roots are operator-owned (sticky), so git must be told they are safe. +# git 2.43 has no path globs for safe.directory, so it is "*"; aien-run can only reach /var/lib/aien-run anyway. +printf "[safe]\n\tdirectory = *\n" > "$tmp/gitconfig" +S install -o root -g root -m 0644 "$tmp/gitconfig" "$RUN_HOME/.gitconfig" + +# 4c. trusted crumb on the sandbox PATH, built from the pinned aien-architecture commit (source read straight +# from the git object store, so the clone's checked-out branch does not matter). +git -C "$ARCH_CLONE" cat-file -e "$CRUMB_COMMIT^{commit}" 2>/dev/null \ + || git -C "$ARCH_CLONE" fetch --quiet origin main \ + || { echo "cannot find $CRUMB_COMMIT in $ARCH_CLONE (set AIEN_ARCH_CLONE)" >&2; exit 1; } +blob=$(git -C "$ARCH_CLONE" rev-parse "$CRUMB_COMMIT:tools/crumb/crumb.c") +[[ $blob == "$CRUMB_BLOB" ]] || { echo "crumb.c blob $blob != expected $CRUMB_BLOB" >&2; exit 1; } +git -C "$ARCH_CLONE" cat-file blob "$blob" > "$tmp/crumb.c" +cc -O2 -Wall -std=c11 -o "$tmp/crumb" "$tmp/crumb.c" +if ! cmp -s "$tmp/crumb" /usr/local/bin/crumb 2>/dev/null; then + S install -o root -g root -m 0755 "$tmp/crumb" /usr/local/bin/crumb + say "installed /usr/local/bin/crumb from aien-architecture $CRUMB_COMMIT (sha256 $(sha256sum < "$tmp/crumb" | cut -c1-16)...)" +else + say "/usr/local/bin/crumb current (aien-architecture $CRUMB_COMMIT)" +fi + +# 5. exec contract + sudoers +S install -o root -g root -m 0755 "$here/aien-run-exec" /usr/local/sbin/aien-run-exec +printf '%s ALL=(root) NOPASSWD: /usr/local/sbin/aien-run-exec\n' "$OPERATOR" > "$tmp/sudoers" +S visudo -cf "$tmp/sudoers" >/dev/null +S install -o root -g root -m 0440 "$tmp/sudoers" /etc/sudoers.d/aien-local-ci +S visudo -c >/dev/null && say "sudoers OK" + +say "done. verify with: $here/tests/run.sh" diff --git a/tools/aien-local-ci/tests/run.sh b/tools/aien-local-ci/tests/run.sh new file mode 100755 index 00000000..1d959d3f --- /dev/null +++ b/tools/aien-local-ci/tests/run.sh @@ -0,0 +1,186 @@ +#!/usr/bin/env bash +# tests/run.sh: proves the C8 exec contract on this host. Run as drakestapleton (uses sudo -n). Exit 1 on any FAIL. +set -uo pipefail +X=/usr/local/sbin/aien-run-exec +RUN_HOME=/var/lib/aien-run +OP_HOME=/home/drakestapleton +pass=0; fail=0 +ok() { pass=$((pass+1)); printf 'PASS %s\n' "$1"; } +bad() { fail=$((fail+1)); printf 'FAIL %s: %s\n' "$1" "$2"; } +run() { sudo -n "$X" "$@"; } # run [exec args]... -- cmd + +uid=$(id -u aien-run) +[[ -n $uid ]] || { echo "no aien-run user"; exit 1; } + +# 1 runs as uid aien-run +out=$(run --cwd /tmp --timeout 10 --profile host -- id -u 2>&1) +[[ $out == "$uid" ]] && ok "runs as uid aien-run ($uid)" || bad "uid" "$out" + +# 2 env cleared: planted secret absent, no SUDO_* leaks, only the allowed names +out=$(SECRET_PLANT=hunter2 run --cwd /tmp --timeout 10 --profile host -- sh -c 'echo "${SECRET_PLANT:-unset}"; env | cut -d= -f1 | grep -v -x -E "PATH|HOME|CARGO_HOME|RUSTUP_HOME|PWD|SHLVL|_|OLDPWD" | tr "\n" ","' 2>&1) +[[ $out == "unset" ]] && ok "env cleared (planted secret absent, no extra names)" || bad "env" "$out" + +# 3 --env pass-through and refused names +out=$(run --cwd /tmp --timeout 10 --profile host --env FOO=bar -- sh -c 'echo $FOO' 2>&1) +[[ $out == bar ]] && ok "--env FOO=bar passes" || bad "--env" "$out" +run --cwd /tmp --timeout 10 --profile host --env LD_PRELOAD=x -- true >/dev/null 2>&1 && bad "--env LD_PRELOAD" "accepted" || ok "--env LD_PRELOAD refused" + +# 4 no network for host profile +out=$(run --cwd /tmp --timeout 20 --profile host -- sh -c 'curl -s -m 3 https://github.com >/dev/null 2>&1; echo curl=$?; getent hosts github.com >/dev/null 2>&1; echo getent=$?; ip -o link | cut -d: -f2 | tr -d " " | tr "\n" ","' 2>&1) +out=$(tr "\n" " " <<<"$out"); [[ $out == *"curl="[1-9]* && $out == *"getent="[1-9]* && $out == *"lo,"* && $out != *"enP"* && $out != *"tailscale"* ]] && ok "host profile has no network ($out)" || bad "network" "$out" + +# 5 timeout -> 124 +t0=$(date +%s); run --cwd /tmp --timeout 1 --profile host -- sleep 30 >/dev/null 2>&1; rc=$?; t1=$(date +%s) +[[ $rc -eq 124 && $((t1-t0)) -lt 15 ]] && ok "timeout returns 124 in $((t1-t0))s" || bad "timeout" "rc=$rc after $((t1-t0))s" + +# 6 exit code passthrough +run --cwd /tmp --timeout 10 --profile host -- sh -c 'exit 7' >/dev/null 2>&1; rc=$? +[[ $rc -eq 7 ]] && ok "exit code passthrough (7)" || bad "exit passthrough" "rc=$rc" + +# 7 cwd honored +out=$(run --cwd "$RUN_HOME" --timeout 10 --profile host -- pwd 2>&1) +[[ $out == "$RUN_HOME" ]] && ok "cwd honored" || bad "cwd" "$out" + +# 8 memory cap enforced (64M cap, allocate 300M in a shell variable) +out=$(run --cwd /tmp --timeout 60 --profile host --memory-max 64M -- sh -c 'x=$(head -c 300000000 /dev/zero | tr "\0" a); echo survived' 2>&1); rc=$? +[[ $rc -ne 0 && $out != *survived* ]] && ok "memory cap enforced (rc=$rc)" || bad "memory cap" "rc=$rc out=$out" + +# 9 devices: host cannot open the GPU, gb10 can +out=$(run --cwd /tmp --timeout 10 --profile host -- sh -c 'exec 3<>/dev/nvidiactl' 2>&1); rc=$? +[[ $rc -ne 0 ]] && ok "host profile cannot open /dev/nvidiactl" || bad "host device policy" "opened nvidiactl" +out=$(run --cwd /tmp --timeout 10 --profile gb10 -- sh -c 'exec 3<>/dev/nvidiactl && exec 4<>/dev/nvidia0 && exec 5<>/dev/nvidia-uvm && exec 6<>/dev/dri/renderD128 && echo opened' 2>&1) +[[ $out == opened ]] && ok "gb10 profile opens /dev/nvidiactl, nvidia0, nvidia-uvm, dri/renderD128" || bad "gb10 devices" "$out" +out=$(run --cwd /tmp --timeout 10 --profile qemu -- sh -c 'exec 3<>/dev/kvm && echo kvm' 2>&1) +run --cwd /tmp --timeout 10 --profile host -- sh -c 'exec 3<>/dev/kvm' >/dev/null 2>&1; rc=$? +[[ $out == kvm && $rc -ne 0 ]] && ok "qemu profile opens /dev/kvm, host profile cannot" || bad "kvm" "qemu=$out host_rc=$rc" + +# 10 secrets of the trusted side are unreadable (direct sudo -u, as the inventory demands) +for f in "$OP_HOME/.aien/verify.key" "$OP_HOME/.config/gh/hosts.yml" "$OP_HOME/.ssh/id_ed25519"; do + sudo -n -u aien-run cat "$f" >/dev/null 2>&1 && bad "secret $f" "READABLE" || ok "denied: $f" +done +sudo -n -u aien-run ls "$OP_HOME/workspace" >/dev/null 2>&1 && bad "workspace" "listable" || ok "denied: $OP_HOME/workspace (0700)" + +# 11 toolchain as aien-run +out=$(run --cwd /tmp --timeout 60 --profile host -- cargo --version 2>&1) +[[ $out == cargo\ 1.98.1* ]] && ok "cargo as aien-run: $out" || bad "cargo" "$out" + +# 12 crash recovery: kill -9 the running child; exit non-zero, no strays in the scope +sudo -n "$X" --cwd /tmp --timeout 120 --profile host -- sh -c 'sleep 300 & exec sleep 200' >/dev/null 2>&1 & +bg=$! +sleep 2 +victim=$(pgrep -u aien-run -f '^sleep 200$' | head -1) +[[ -n $victim ]] && sudo -n kill -9 "$victim" +wait $bg; rc=$? +sleep 1 +strays=$(pgrep -u aien-run -f '^sleep 300$' | wc -l) +scopes=$(systemctl list-units --no-legend --plain 'aien-run-exec-*.scope' 2>/dev/null | wc -l) +[[ $rc -ne 0 && $strays -eq 0 && $scopes -eq 0 ]] && ok "crash recovery: rc=$rc, strays=0, scopes=0" || bad "crash recovery" "rc=$rc strays=$strays scopes=$scopes" + +# 13 worktree protection (if a worktree exists; also used by the tests below) +# Only worktrees made by wt.sh (root-owned .git pointing into the mirror). Other lanes may be creating and removing +# their own worktrees under wt/ while this runs; those are skipped. +wt_sh_worktrees() { + local d + for d in "$RUN_HOME"/wt/*/*/; do + d=${d%/} + [[ -f $d/.git && $(stat -c %u "$d/.git" 2>/dev/null) == 0 ]] || continue + grep -q "^gitdir: $RUN_HOME/mirror/" "$d/.git" 2>/dev/null && printf '%s\n' "$d" + done +} +wt=$(wt_sh_worktrees | head -1) +if [[ -n $wt ]]; then + out=$(run --cwd "$wt" --timeout 30 --profile host -- sh -c 'git rev-parse --short HEAD >/dev/null && echo git-ok; rm -f .git 2>/dev/null && echo REMOVED; m=$(sed "s|gitdir: ||;s|/worktrees/.*||" .git); touch "$m/refs/x" 2>/dev/null && echo MIRROR-WRITABLE; echo t > .probe && rm .probe && echo tree-writable' 2>&1 | tr '\n' ' ') + [[ $out == *git-ok* && $out != *REMOVED* && $out != *MIRROR-WRITABLE* && $out == *tree-writable* ]] && ok "worktree: git reads, .git and mirror protected, tree writable" || bad "worktree" "$out" +fi + +# ---- review fixes (INFRA-2): nothing a check writes can reach a later check ---- +refused() { [[ $1 -eq 2 ]]; } # exit 2 = refused by aien-run-exec before anything ran +T_PR="$RUN_HOME/target/aien-sovereign-core/pr"; T_MAIN="$RUN_HOME/target/aien-sovereign-core/main" + +# 14 the sandbox home is read-only inside every profile: no ~/.cargo/config.toml, no CARGO_HOME config, +# no swapping the registry or the cargo proxies, no new dotfiles +for p in host gb10; do + out=$(run --cwd /tmp --timeout 20 --profile $p -- sh -c 'H=/var/lib/aien-run + mkdir -p $H/.cargo 2>/dev/null && echo HOME-WRITABLE + touch $H/cargo/config.toml 2>/dev/null && echo CARGO_HOME-WRITABLE + mv $H/cargo/bin $H/cargo/bin.x 2>/dev/null && echo BIN-RENAMED + mv $H/cargo/registry $H/cargo/r.x 2>/dev/null && echo REGISTRY-RENAMED + touch $H/.gitconfig 2>/dev/null && echo GITCONFIG-WRITABLE + echo done' 2>&1 | tr '\n' ' ') + [[ $out == "done " ]] && ok "$p: sandbox home, CARGO_HOME, registry, proxies, .gitconfig all read-only" || bad "$p home read-only" "$out" +done + +# 15 trust classes: a PR run can write target/<repo>/pr only, never main; a main run never pr; a run whose cwd is not +# in a worktree can write no worktree +out=$(run --cwd /tmp --timeout 20 --profile host --env CARGO_TARGET_DIR="$T_PR" -- sh -c "touch $T_PR/.p && rm $T_PR/.p && echo pr-rw; touch $T_MAIN/.p 2>/dev/null && echo MAIN-WRITABLE; echo end" 2>&1 | tr '\n' ' ') +[[ $out == "pr-rw end " ]] && ok "class pr: target/pr writable, target/main read-only" || bad "class pr" "$out" +out=$(run --cwd /tmp --timeout 20 --profile gb10 --env CARGO_TARGET_DIR="$T_MAIN" -- sh -c "touch $T_MAIN/.p && rm $T_MAIN/.p && echo main-rw; touch $T_PR/.p 2>/dev/null && echo PR-WRITABLE; echo end" 2>&1 | tr '\n' ' ') +[[ $out == "main-rw end " ]] && ok "class main (gb10): target/main writable, target/pr read-only" || bad "class main" "$out" +if [[ -n $wt ]]; then + out=$(run --cwd /tmp --timeout 20 --profile host -- sh -c "touch $wt/.p 2>/dev/null && echo WT-WRITABLE; echo end" 2>&1 | tr '\n' ' ') + [[ $out == "end " ]] && ok "a run outside a worktree cannot write any worktree" || bad "foreign worktree" "$out" +fi + +# 16 argument refusals (exit 2, nothing runs) +run --cwd /tmp --timeout 5 --profile host --env CARGO_TARGET_DIR=/tmp/t -- true >/dev/null 2>&1; r1=$? +run --cwd /tmp --timeout 5 --profile host --env CARGO_TARGET_DIR="$RUN_HOME/target/../target/x/main" -- true >/dev/null 2>&1; r2=$? +run --cwd /tmp --timeout 5 --profile host --env CARGO_TARGET_DIR="$RUN_HOME/target/aien-sovereign-core/other" -- true >/dev/null 2>&1; r3=$? +run --cwd /tmp --timeout 5 --profile host --env FOO=1 --env FOO=2 -- true >/dev/null 2>&1; r4=$? +refused $r1 && refused $r2 && refused $r3 && refused $r4 && ok "CARGO_TARGET_DIR outside target/<repo>/pr|main and duplicate --env refused" || bad "env refusals" "$r1 $r2 $r3 $r4" +run --cwd "$RUN_HOME/target" --timeout 5 --profile host -- true >/dev/null 2>&1; r1=$? +run --cwd "$RUN_HOME/cargo" --timeout 5 --profile host -- true >/dev/null 2>&1; r2=$? +run --cwd "$RUN_HOME/wt/../wt" --timeout 5 --profile host -- true >/dev/null 2>&1; r3=$? +r4=2 +if [[ -n $wt ]]; then # a symlink planted by a check inside its worktree, pointing at another directory + run --cwd "$wt" --timeout 10 --profile host -- ln -sfn /tmp cwdlink >/dev/null 2>&1 + run --cwd "$wt/cwdlink" --timeout 5 --profile host -- true >/dev/null 2>&1; r4=$? + run --cwd "$wt" --timeout 10 --profile host -- rm -f cwdlink >/dev/null 2>&1 +fi +refused $r1 && refused $r2 && refused $r3 && refused $r4 && ok "--cwd outside wt/<repo>/<sha> inside the home, with .., or through a symlink refused" || bad "cwd refusals" "$r1 $r2 $r3 $r4" +run --cwd /tmp --timeout 5 --profile host -- LD_PRELOAD=/nonexistent.so true >/dev/null 2>&1; r1=$? +run --cwd /tmp --timeout 5 --profile host -- -i true >/dev/null 2>&1; r2=$? +refused $r1 && refused $r2 && ok "PROGRAM shaped like an env assignment or option refused" || bad "program refusals" "$r1 $r2" + +# 17 private scratch dirs: host cannot see the operator's /tmp; every profile gets a private /var/tmp +probe=/tmp/aien-infra-probe.$$; : > "$probe"; : > /var/tmp/aien-infra-probe.$$ +out=$(run --cwd /tmp --timeout 10 --profile host -- sh -c "test -e $probe && echo SEES-HOST-TMP; test -e /var/tmp/aien-infra-probe.$$ && echo SEES-VARTMP; ls -A /tmp | wc -l" 2>&1 | tr '\n' ' ') +[[ $out == "0 " ]] && ok "host: private empty /tmp and /var/tmp" || bad "host private tmp" "$out" +out=$(run --cwd /tmp --timeout 10 --profile gb10 -- sh -c "test -e $probe && echo sees-host-tmp; test -e /var/tmp/aien-infra-probe.$$ && echo SEES-VARTMP; echo end" 2>&1 | tr '\n' ' ') +[[ $out == "sees-host-tmp end " ]] && ok "gb10: shares host /tmp (documented), private /var/tmp" || bad "gb10 tmp" "$out" +rm -f "$probe" /var/tmp/aien-infra-probe.$$ +out=$(run --cwd /tmp --timeout 10 --profile host -- sh -c 'echo $(ls -A /run/lock /run/screen /dev/mqueue /dev/shm 2>/dev/null | grep -vc -e "^$" -e ":$") $(ipcs -m | grep -c "^0x") $(ipcs -s | grep -c "^0x")' 2>&1) +host_ipc="$(ipcs -m | grep -c "^0x") $(ipcs -s | grep -c "^0x")" +[[ $out == "0 0 0" ]] && ok "host: private /run/lock, /run/screen, /dev/shm, /dev/mqueue and IPC namespace (host has shm+sem: $host_ipc)" || bad "host ipc" "$out" + +# 18 trusted crumb on the sandbox PATH; `crumb verify .` runs as aien-run in a sandboxed worktree; a `crumb` +# planted inside the worktree is never picked up by a bare name +out=$(run --cwd /tmp --timeout 10 --profile host -- sh -c 'command -v crumb; stat -c "%U %a" "$(command -v crumb)"' 2>&1 | tr '\n' ' ') +[[ $out == "/usr/local/bin/crumb root 755 " ]] && ok "crumb on sandbox PATH: $out" || bad "crumb PATH" "$out" +if [[ -n $wt ]]; then + # every worktree: the sandboxed verdict equals the trusted side's verdict on a clean trusted checkout of the same + # commit (operator git refuses to run inside a sandbox worktree: dubious ownership, by design); at least one + # worktree (a crumb-current commit) must verify OK, so the PASS path is exercised, not only the STALE path + agree=0; total=0; okrun=0 + for w in $(wt_sh_worktrees); do + total=$((total+1)) + s_out=$(run --cwd "$w" --timeout 60 --profile host -- crumb verify . 2>&1); s_rc=$? + tc=$(mktemp -d); sha=$(basename "$w"); repo=$(basename "$(dirname "$w")") + git clone --quiet --shared --no-checkout "$RUN_HOME/mirror/$repo.git" "$tc/t" && git -C "$tc/t" checkout --quiet "$sha" + t_out=$(cd "$tc/t" && /usr/local/bin/crumb verify . 2>&1); t_rc=$? + rm -rf "$tc" + [[ $s_rc -eq $t_rc && $s_out == "$t_out" ]] && agree=$((agree+1)) + [[ $s_rc -eq 0 ]] && okrun=$((okrun+1)) + printf ' %s: sandbox rc=%s, trusted rc=%s, %s\n' "$(basename "$w" | cut -c1-7)" "$s_rc" "$t_rc" "$(tail -1 <<<"$s_out")" + done + [[ $agree -eq $total && $okrun -ge 1 ]] && ok "crumb verify . as aien-run in $total sandboxed worktrees (agrees with trusted side; $okrun OK)" \ + || bad "crumb verify" "agree=$agree/$total ok=$okrun" + run --cwd "$wt" --timeout 10 --profile host -- sh -c 'printf "#!/bin/sh\necho PLANTED\n" > crumb && chmod +x crumb' >/dev/null 2>&1 + o1=$(run --cwd "$wt" --timeout 10 --profile host -- crumb status . 2>&1 | head -3) + o2=$(run --cwd "$wt" --timeout 10 --profile host -- sh -c 'exec crumb status .' 2>&1 | head -3) + o3=$(run --cwd "$wt" --timeout 10 --profile host -- sh -c 'test -x ./crumb && echo planted-present' 2>&1) + run --cwd "$wt" --timeout 10 --profile host -- rm -f crumb >/dev/null 2>&1 + [[ $o3 == planted-present && $o1 != *PLANTED* && $o2 != *PLANTED* && -n $o1 && -n $o2 ]] \ + && ok "planted ./crumb in the worktree ignored (PROGRAM and sh exec both use /usr/local/bin/crumb)" || bad "planted crumb" "o1=$o1 o2=$o2 o3=$o3" +fi +printf '\n%d passed, %d failed\n' "$pass" "$fail" +[[ $fail -eq 0 ]] diff --git a/tools/aien-local-ci/wt.sh b/tools/aien-local-ci/wt.sh new file mode 100755 index 00000000..aa7fba2e --- /dev/null +++ b/tools/aien-local-ci/wt.sh @@ -0,0 +1,68 @@ +#!/usr/bin/env bash +# wt.sh: trusted-side worktree management for aien-run checks (A2: worktrees under /var/lib/aien-run/wt, +# the clone unwritable by aien-run, the worktree's .git link unwritable by aien-run). +# +# wt.sh sync <repo> <trusted-clone-dir> create/refresh the bare mirror /var/lib/aien-run/mirror/<repo>.git +# wt.sh add <repo> <sha> check out <sha> at /var/lib/aien-run/wt/<repo>/<sha> for aien-run +# wt.sh remove <repo> <sha> remove that worktree +# wt.sh list list worktrees with age +# +# Why a mirror: the operator's clones live under ~/workspace (0700), which aien-run must not read. The mirror is a +# bare copy owned by the operator (0755, world-readable, not writable by aien-run); the worktree's .git file points +# into the mirror, so aien-run can read objects (git log/rev-parse work) but cannot write refs, objects or the index. +# Ownership of a worktree after `add`: root dir operator:aien-run mode 1775 (sticky: aien-run may create files but +# cannot remove or replace entries it does not own), `.git` file root:root 0644, everything else aien-run:aien-run. +set -euo pipefail +RUN_USER=aien-run +BASE=/var/lib/aien-run +MIRROR="$BASE/mirror" +WT="$BASE/wt" +OPERATOR=$(id -un) + +die() { printf 'wt.sh: %s\n' "$*" >&2; exit 2; } +cmd=${1:-}; shift || true +case "$cmd" in + sync) + repo=${1:?repo}; clone=${2:?trusted-clone-dir} + [[ $repo =~ ^[A-Za-z0-9._-]+$ ]] || die "bad repo name" + git -C "$clone" rev-parse --git-dir >/dev/null || die "not a git dir: $clone" + m="$MIRROR/$repo.git" + if [[ ! -d $m ]]; then + git init --bare --quiet "$m" + git -C "$m" config core.sharedRepository 0644 + fi + git -C "$m" fetch --quiet --prune "$clone" '+refs/heads/*:refs/heads/*' '+refs/remotes/*:refs/remotes/*' '+refs/tags/*:refs/tags/*' + chmod -R o+rX,o-w "$m" + printf 'mirror %s: %s refs\n' "$m" "$(git -C "$m" for-each-ref | wc -l)" + ;; + add) + repo=${1:?repo}; sha=${2:?sha} + [[ $repo =~ ^[A-Za-z0-9._-]+$ && $sha =~ ^[0-9a-f]{7,40}$ ]] || die "bad repo or sha" + m="$MIRROR/$repo.git"; [[ -d $m ]] || die "no mirror for $repo (run: wt.sh sync $repo <clone>)" + full=$(git -C "$m" rev-parse --verify "$sha^{commit}") || die "unknown commit $sha" + d="$WT/$repo/$full" + [[ ! -e $d ]] || die "exists: $d" + install -d -m 0755 "$WT/$repo" + git -C "$m" worktree add --quiet --detach "$d" "$full" + sudo -n chown -R "$RUN_USER:$RUN_USER" "$d" + sudo -n chown root:root "$d/.git"; sudo -n chmod 0644 "$d/.git" + sudo -n chown "$OPERATOR:$RUN_USER" "$d"; sudo -n chmod 1775 "$d" + printf '%s\n' "$d" + ;; + remove) + repo=${1:?repo}; sha=${2:?sha} + [[ $repo =~ ^[A-Za-z0-9._-]+$ && $sha =~ ^[0-9a-f]{7,40}$ ]] || die "bad repo or sha" + m="$MIRROR/$repo.git" + d=$(ls -d "$WT/$repo/$sha"* 2>/dev/null | head -1); [[ -n $d ]] || die "no worktree for $sha" + sudo -n rm -rf "$d" + git -C "$m" worktree prune + printf 'removed %s\n' "$d" + ;; + list) + for d in "$WT"/*/*/; do + [[ -d $d ]] || continue + printf '%s %s days\n' "$d" "$(( ( $(date +%s) - $(stat -c %Y "$d") ) / 86400 ))" + done + ;; + *) sed -n '2,8p' "$0" >&2; exit 2 ;; +esac