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 / rebase-train.sh

294 lines · 15486 bytes Raw Blame History
1#!/usr/bin/env bash
2# rebase-train.sh: keep ready-to-merge pull requests on top of their base branch (Spark pipeline, runs before
3# `pipeline.sh round`).
4#
5# rebase-train.sh [--dry-run] [--repos a,b] [--pr N] (--pr: only that pull request number, for a trial run)
6#
7# For every repo, every open, non-draft pull request labeled exactly ready-to-merge whose head branch lives in the
8# same repository (not a fork) and that GitHub reports CONFLICTING or BEHIND:
9# 1. fetch pull/N/head into a temporary ref of the trusted clone, check it out in a scratch worktree;
10# 2. skip it when its head commit is younger than 10 minutes (committer time) or when it touches a trust-surface
11# file (.aien/verify.pub, .github/workflows/*);
12# 3. git rebase origin/<base>; conflicts in .crumb files only are resolved with the base's copy;
13# any other conflicting file aborts the rebase;
14# 4. (retired) Crumbs are retired from the active workflow (DRA-97, Drake 2026-11-07): .crumb files are history,
15# so the train no longer recompiles or verifies them and needs no crumb tool pin;
16# 5. git push --force-with-lease=<branch>:<old head> to the pull request's branch.
17# A non-.crumb conflict adds the label needs-rebase and posts one comment
18# per head sha. --dry-run does all local work and prints what it would do; it pushes, labels, comments and journals
19# nothing.
20#
21# GitHub API budget: one `gh pr list` per repo (label filtered here: `gh pr list --label` returned nothing on
22# 2026-10-18 while 12 labeled pull requests were open); per blocked pull request at most one `gh pr view` (comment
23# de-duplication), one label call and one comment (plus one label create per repo, once ever). Pushes use git.
24# One instance at a time (flock on $STATE/train.lock; a second one exits 0). Every decision is one JSON line in
25# $STATE/train.jsonl: {utc, repo, pr, head, action, reason}.
26# Exit: 0 run done (skips and refusals included), 2 usage or configuration error.
27set -euo pipefail
28
29STATE=${AIEN_STATE:-$HOME/.local/state/aien-local-ci}
30CLONES=${AIEN_CLONES:-$HOME/workspace/pipeline/clones}
31OWNER=${AIEN_GH_OWNER:-aien-dev}
32REPOS=${AIEN_REPOS:-aien-sovereign-core,omega,aienux,physics,aien-protocols,aegis-runtime,spark-rsi,aien-architecture,benchmarks,synapse,interplane}
33GH=${GH:-gh}
34GIT=${GIT:-git}
35MERGE_LABEL="ready-to-merge"
36BLOCK_LABEL="needs-rebase"
37MIN_AGE=${AIEN_TRAIN_MIN_AGE:-600}
38AI_LINE='> *This was generated by AI during triage.*'
39MARK='aien-rebase-train head='
40
41usage() { echo "usage: rebase-train.sh [--dry-run] [--repos a,b] [--pr N]" >&2; exit 2; }
42die() { echo "rebase-train.sh: $*" >&2; exit 2; }
43
44DRY=0 ONLY=""
45while [[ $# -gt 0 ]]; do
46 case "$1" in
47 --dry-run) DRY=1; shift ;;
48 --repos) [[ $# -ge 2 ]] || usage; REPOS=$2; shift 2 ;;
49 --pr) [[ $# -ge 2 && $2 =~ ^[0-9]+$ ]] || usage; ONLY=$2; shift 2 ;;
50 *) usage ;;
51 esac
52done
53IFS=, read -r -a repo_list <<< "$REPOS"
54[[ ${#repo_list[@]} -gt 0 ]] || die "no repositories named"
55for r in "${repo_list[@]}"; do
56 [[ $r =~ ^[A-Za-z0-9._-]+$ && $r != . && $r != .. ]] || die "bad repository name '$r'"
57done
58command -v jq >/dev/null || die "jq not found"
59command -v flock >/dev/null || die "flock not found"
60command -v "$GIT" >/dev/null || die "git ($GIT) not found"
61command -v "$GH" >/dev/null || die "gh ($GH) not found"
62[[ -d $CLONES ]] || die "clones directory $CLONES missing"
63
64mkdir -p "$STATE"
65JOURNAL=$STATE/train.jsonl
66COMMENTED=$STATE/train-commented # lines "<repo> <pr> <head>": a comment was posted for that head
67LABELED=$STATE/train-labels # lines "<repo>": the needs-rebase label exists there
68exec 9>>"$STATE/train.lock"
69flock -n 9 || exit 0
70WORK=$(mktemp -d "$STATE/.train.XXXXXX")
71CLEAN=() # "<clone>|<worktree>|<ref>" to remove on exit
72GH_DEFER=$WORK/gh-deferred
73# ghc ARGS...: every GitHub call goes through here. ~/.local/bin/gh (the shared rate-limit guard) exits 98 when it
74# refuses (allowance reserved or rate-limit marker set): then this run makes no further GitHub call, and every
75# journal line from then on carries deferred="gh guard exit 98: try next round". Never a retry loop, never a FAIL.
76ghc() {
77 local rc=0
78 [[ -e $GH_DEFER ]] && return 98
79 "$GH" "$@" || rc=$?
80 [[ $rc != 98 ]] || : > "$GH_DEFER"
81 return "$rc"
82}
83cleanup() {
84 local e c w ref
85 for e in "${CLEAN[@]}"; do
86 IFS='|' read -r c w ref <<< "$e"
87 [[ -n $w ]] && "$GIT" -C "$c" worktree remove --force "$w" >/dev/null 2>&1
88 [[ -n $ref ]] && "$GIT" -C "$c" update-ref -d "$ref" >/dev/null 2>&1
89 "$GIT" -C "$c" worktree prune >/dev/null 2>&1
90 done
91 rm -rf "$WORK"
92}
93trap cleanup EXIT
94# Merge pause (Drake 2026-10-18): while $STATE/MERGE-PAUSE exists pipeline.sh merges nothing. The train keeps
95# rebasing (that is not merging) and never adds ready-to-merge or enables auto-merge in any case; it only reads the
96# label. It never creates or deletes MERGE-PAUSE: only Drake lifts it.
97if [[ -e $STATE/MERGE-PAUSE ]]; then echo "rebase-train.sh: MERGE-PAUSE present: rebasing only, merges stay paused"; fi
98
99# journal REPO PR HEAD ACTION REASON: one JSON line (not in --dry-run) and one short line on stdout.
100journal() {
101 local p=""; [[ $DRY == 1 ]] && p="[dry-run] "
102 echo "${p}$1#$2 ${3:0:8} $4: $5"
103 [[ $DRY == 1 ]] && return 0
104 local r=$5
105 [[ ! -e $GH_DEFER ]] || r+=" (gh guard exit 98: try next round)"
106 jq -nc --arg utc "$(date -u +%Y-%m-%dT%H:%M:%SZ)" --arg repo "$1" --arg pr "$2" --arg head "$3" \
107 --arg action "$4" --arg reason "$r" '{utc: $utc, repo: $repo, pr: $pr, head: $head, action: $action, reason: $reason}' \
108 >> "$JOURNAL"
109}
110
111# g DIR ARGS...: git in a worktree, never running hooks (the tree under test is not trusted).
112g() { local d=$1; shift; "$GIT" -C "$d" -c core.hooksPath=/dev/null "$@"; }
113
114is_crumb() { [[ $1 == .crumb || $1 == */.crumb ]]; }
115
116# slug REPO: canonical owner/name on GitHub (aien-dev/<repo> may redirect to aien-dev/<repo>-mv), only when the train
117# is about to write (label, comment). Shares pipeline.sh's disk cache $STATE/slugs/<repo> (24 h; an older name is
118# used when the refresh fails).
119slug() {
120 local f=$STATE/slugs/$1 s="" fresh re='^[A-Za-z0-9._-]+/[A-Za-z0-9._-]+$'
121 [[ -f $f ]] && s=$(head -n 1 "$f")
122 if [[ $s =~ $re ]] && (( $(date +%s) - $(stat -c %Y "$f") < ${AIEN_SLUG_TTL:-86400} )); then echo "$s"; return 0; fi
123 if fresh=$(ghc api "repos/$OWNER/$1" --jq .full_name 2>/dev/null) && [[ $fresh =~ $re ]]; then
124 mkdir -p "$STATE/slugs" && printf '%s\n' "$fresh" > "$f.tmp" && mv "$f.tmp" "$f"
125 echo "$fresh"; return 0
126 fi
127 [[ $s =~ $re ]] && { echo "$s"; return 0; }
128 return 1
129}
130
131declare -A LABEL_OK=()
132# block REPO SLUG PR HEAD LABELS WHY FILES: label needs-rebase and post one comment for this head.
133block() {
134 local repo=$1 s=$2 num=$3 head=$4 labels=$5 why=$6 files=$7 body seen
135 if [[ $DRY == 1 ]]; then
136 journal "$repo" "$num" "$head" would-block "$why: $(paste -sd' ' <<< "$files")"; return 0
137 fi
138 journal "$repo" "$num" "$head" blocked "$why: $(paste -sd' ' <<< "$files")"
139 if ! s=$(slug "$repo"); then journal "$repo" "$num" "$head" label-failed "cannot resolve GitHub slug"; return 0; fi
140 if [[ ,$labels, != *",$BLOCK_LABEL,"* ]]; then
141 if [[ -z ${LABEL_OK[$repo]:-} ]] && ! grep -qx "$repo" "$LABELED" 2>/dev/null; then
142 if ghc label create "$BLOCK_LABEL" -R "$s" --color D93F0B \
143 --description "The rebase train could not rebase this onto main by itself" >/dev/null 2>&1 \
144 || ghc label list -R "$s" --search "$BLOCK_LABEL" --json name --jq '.[].name' 2>/dev/null | grep -qx "$BLOCK_LABEL"; then
145 echo "$repo" >> "$LABELED"
146 else
147 journal "$repo" "$num" "$head" label-failed "cannot create label $BLOCK_LABEL"
148 fi
149 fi
150 LABEL_OK[$repo]=1
151 if ghc api -X POST "repos/$s/issues/$num/labels" -f "labels[]=$BLOCK_LABEL" >/dev/null 2>&1; then
152 journal "$repo" "$num" "$head" labeled "$BLOCK_LABEL"
153 else
154 journal "$repo" "$num" "$head" label-failed "cannot add $BLOCK_LABEL"
155 fi
156 fi
157 if grep -qx "$repo $num $head" "$COMMENTED" 2>/dev/null; then
158 journal "$repo" "$num" "$head" comment-skipped "already commented on this head (state)"; return 0
159 fi
160 if ! seen=$(ghc pr view "$num" -R "$s" --json comments --jq "[.comments[].body | select(contains(\"$MARK$head\"))] | length"); then
161 journal "$repo" "$num" "$head" comment-failed "cannot read comments"; return 0
162 fi
163 if [[ $seen != 0 ]]; then
164 echo "$repo $num $head" >> "$COMMENTED"
165 journal "$repo" "$num" "$head" comment-skipped "already commented on this head (GitHub)"; return 0
166 fi
167 body=$WORK/comment.md
168 {
169 echo "$AI_LINE"
170 echo
171 echo "The rebase train (\`tools/aien-local-ci/rebase-train.sh\`) could not move this pull request onto the latest"
172 echo "\`main\` by itself: $why. It resolves only \`.crumb\` files (retired history, DRA-97: it keeps \`main\`'s copy) on its own."
173 echo
174 echo "Files that need a person (or an agent) to resolve:"
175 echo
176 while IFS= read -r f; do [[ -n $f ]] && echo "- \`$f\`"; done <<< "$files"
177 echo
178 echo "Rebase the branch onto \`main\` by hand, resolve these files, and push. The \`$BLOCK_LABEL\` label was added;"
179 echo "the train tries again on every run and removes the label once a rebase goes through."
180 echo
181 echo "<!-- $MARK$head -->"
182 } > "$body"
183 if ghc api -X POST "repos/$s/issues/$num/comments" -F "body=@$body" >/dev/null 2>&1; then
184 echo "$repo $num $head" >> "$COMMENTED"
185 journal "$repo" "$num" "$head" commented "conflict comment posted"
186 else
187 journal "$repo" "$num" "$head" comment-failed "cannot post comment"
188 fi
189}
190
191# train_one REPO SLUG PR HEAD BRANCH BASE LABELS: one candidate, end to end.
192train_one() {
193 local repo=$1 s=$2 num=$3 head=$4 branch=$5 base=$6 labels=$7 clone=$CLONES/$1
194 local ref=refs/aien-train/$3 wt=$WORK/$1-$3 age touched
195 local unmerged other f n=0 newhead
196 if ! "$GIT" -C "$clone" fetch -q origin "+pull/$num/head:$ref"; then
197 journal "$repo" "$num" "$head" skipped "fetch of pull/$num/head failed"; return 0
198 fi
199 CLEAN+=("$clone||$ref")
200 [[ $("$GIT" -C "$clone" rev-parse "$ref") == "$head" ]] || { journal "$repo" "$num" "$head" skipped "head moved while fetching"; return 0; }
201 age=$(( $(date +%s) - $("$GIT" -C "$clone" show -s --format=%ct "$head") ))
202 if (( age < MIN_AGE )); then
203 journal "$repo" "$num" "$head" skipped "head commit is ${age}s old (< ${MIN_AGE}s): pushed recently"; return 0
204 fi
205 touched=$("$GIT" -C "$clone" diff --name-only "origin/$base...$head" -- .aien/verify.pub '.github/workflows/*' || echo "?diff failed")
206 if [[ -n $touched ]]; then
207 journal "$repo" "$num" "$head" skipped "touches trust surface, never rebased by the train: $(paste -sd' ' <<< "$touched")"; return 0
208 fi
209 if ! "$GIT" -C "$clone" worktree add -q --detach "$wt" "$head" 2>/dev/null; then
210 journal "$repo" "$num" "$head" skipped "cannot create scratch worktree"; return 0
211 fi
212 CLEAN+=("$clone|$wt|")
213 # Rebase; every stop must be .crumb-only conflicts (taken from the base side, "ours" during a rebase).
214 g "$wt" rebase -q "origin/$base" >/dev/null 2>&1 || true
215 while [[ -e $(g "$wt" rev-parse --git-path rebase-merge) || -e $(g "$wt" rev-parse --git-path rebase-apply) ]]; do
216 (( ++n <= 500 )) || { g "$wt" rebase --abort >/dev/null 2>&1 || true; journal "$repo" "$num" "$head" skipped "rebase did not finish"; return 0; }
217 unmerged=$(g "$wt" diff --name-only --diff-filter=U)
218 if [[ -z $unmerged ]]; then
219 if g "$wt" diff --cached --quiet; then GIT_EDITOR=true g "$wt" rebase --skip >/dev/null 2>&1 || true
220 else GIT_EDITOR=true g "$wt" rebase --continue >/dev/null 2>&1 || true; fi
221 continue
222 fi
223 other=""
224 while IFS= read -r f; do is_crumb "$f" || other+="$f"$'\n'; done <<< "$unmerged"
225 if [[ -n $other ]]; then
226 g "$wt" rebase --abort >/dev/null 2>&1 || true
227 block "$repo" "$s" "$num" "$head" "$labels" "rebase onto origin/$base conflicts" "${other%$'\n'}"
228 return 0
229 fi
230 while IFS= read -r f; do
231 if g "$wt" cat-file -e ":2:$f" 2>/dev/null; then g "$wt" checkout -q --ours -- "$f" && g "$wt" add -- "$f"
232 else g "$wt" rm -q -- "$f"; fi
233 done <<< "$unmerged"
234 GIT_EDITOR=true g "$wt" rebase --continue >/dev/null 2>&1 || true
235 done
236 newhead=$(g "$wt" rev-parse HEAD)
237 if ! g "$wt" merge-base --is-ancestor "origin/$base" HEAD; then
238 journal "$repo" "$num" "$head" skipped "rebase failed before starting (head left as is)"; return 0
239 fi
240 # A pull request whose changes are already in the base would become empty (or crumb-only): never push that.
241 other=""
242 while IFS= read -r f; do [[ -z $f ]] || is_crumb "$f" || other+="$f"$'\n'; done \
243 <<< "$(g "$wt" diff --name-only "origin/$base" HEAD)"
244 if [[ -z $other ]]; then
245 journal "$repo" "$num" "$head" skipped "nothing but .crumb files left after the rebase (changes already in origin/$base?)"
246 return 0
247 fi
248 # Crumbs are retired (DRA-97): no recompile, commit or check of .crumb files here; they keep the base's copy.
249 if [[ $newhead == "$head" ]]; then
250 journal "$repo" "$num" "$head" skipped "rebase changed nothing"; return 0
251 fi
252 if [[ $DRY == 1 ]]; then
253 journal "$repo" "$num" "$head" would-push "would rebase onto origin/$base + push $branch (local result ${newhead:0:8})"
254 return 0
255 fi
256 if g "$wt" push -q --force-with-lease="refs/heads/$branch:$head" origin "HEAD:refs/heads/$branch" >/dev/null 2>&1; then
257 journal "$repo" "$num" "$head" pushed "rebased onto origin/$base, new head $newhead"
258 if [[ ,$labels, == *",$BLOCK_LABEL,"* ]]; then
259 if s=$(slug "$repo") && ghc api -X DELETE "repos/$s/issues/$num/labels/$BLOCK_LABEL" >/dev/null 2>&1; then
260 journal "$repo" "$num" "$newhead" unlabeled "$BLOCK_LABEL removed after a clean rebase"
261 fi
262 fi
263 else
264 journal "$repo" "$num" "$head" push-failed "force-with-lease push refused or failed (branch moved?)"
265 fi
266}
267
268for repo in "${repo_list[@]}"; do
269 [[ -d $CLONES/$repo/.git ]] || continue
270 s=$OWNER/$repo
271 if ! "$GIT" -C "$CLONES/$repo" fetch -q origin; then
272 journal "$repo" - - skipped "fetch of trusted clone failed"; continue
273 fi
274 if ! prs=$(ghc pr list -R "$s" --state open --limit 100 \
275 --json number,headRefOid,headRefName,baseRefName,isDraft,isCrossRepository,mergeable,mergeStateStatus,labels \
276 --jq '.[] | [.number, .headRefOid, .headRefName, .baseRefName, .isDraft, .isCrossRepository, .mergeable,
277 .mergeStateStatus, ([.labels[].name] | join(","))] | @tsv'); then
278 journal "$repo" - - skipped "cannot list pull requests"; continue
279 fi
280 while IFS=$'\t' read -r num head branch base draft cross mergeable mstate labels; do
281 [[ -n $num ]] || continue
282 [[ -z $ONLY || $num == "$ONLY" ]] || continue
283 [[ ,$labels, == *",$MERGE_LABEL,"* ]] || continue
284 [[ $mergeable == CONFLICTING || $mstate == BEHIND ]] || continue
285 if [[ ! $head =~ ^[0-9a-f]{40}$ ]]; then journal "$repo" "$num" "$head" skipped "bad head sha"; continue; fi
286 if [[ ! $branch =~ ^[A-Za-z0-9._/-]+$ || $branch == *..* || ! $base =~ ^[A-Za-z0-9._/-]+$ || $base == *..* ]]; then
287 journal "$repo" "$num" "$head" skipped "bad branch or base name"; continue
288 fi
289 if [[ $draft != false ]]; then journal "$repo" "$num" "$head" skipped "draft"; continue; fi
290 if [[ $cross != false ]]; then journal "$repo" "$num" "$head" skipped "head is in a fork"; continue; fi
291 train_one "$repo" "$s" "$num" "$head" "$branch" "$base" "$labels"
292 done <<< "$prs"
293done
294exit 0