omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / evidence / kfd / 20261113-rebase-c4e5e963 / tools / repin3.py
| 1 | #!/usr/bin/env python3 |
| 2 | """repin3.py: move the in-repo current-compiler pin to the committed seed/out/omc, over the files the |
| 3 | inline-arena repin d6840959 moved (seed/out/omc.sha256 and seed/omc/size-history.tsv are the landing's). |
| 4 | Run from the repo root of a landing branch. |
| 5 | |
| 6 | The pin being replaced (OLD, OLD_SRC) is read from om/units.sh OMC_PIN and native/prefetch/compiler.sha256. |
| 7 | The narrative of the current pin is read from neuralese/README.md ("Current pin after X, branch `B` on |
| 8 | Radicle main M (DATE): committed ..."): |
| 9 | - B is this run's branch (a rebuild of the same landing): OLD is replaced in place and M is updated; |
| 10 | - B is another branch (a new landing over a landed pin): OLD becomes the previous pin ("landed as L", |
| 11 | L = the commit that set OMC_PIN=OLD in om/units.sh, REPIN_PREV_LANDED overrides) in neuralese, |
| 12 | AUDIT_SCOPE and native/prefetch/README.md, and this landing becomes current. |
| 13 | This landing: BRANCH = REPIN_BRANCH or the checked-out branch; WHAT and LANDING (subject prefix of the |
| 14 | landing commit) from PROFILES[BRANCH], REPIN_WHAT / REPIN_LANDING override; MAIN = REPIN_MAIN or the first |
| 15 | parent of the landing commit on HEAD's first-parent line; DATE = REPIN_DATE or today (UTC). |
| 16 | src/omega_om_units{.c,.ident,_fs.c} and src/omega_native.h are regenerated by om/units.sh with the bridge |
| 17 | pinned in om/omega-bridge.lock (git archive from ../omega-bridge, REPIN_BRIDGE_REPO overrides, into |
| 18 | build/); only the omc= pins may change, then --check must pass. Every text edit is asserted before any |
| 19 | file is written; build/repin3.last records the pin written. |
| 20 | Exit 0 done; 1 an expected text was not found; 3 a prerequisite is missing; 4 regeneration failed.""" |
| 21 | import datetime, hashlib, os, re, subprocess, sys |
| 22 | |
| 23 | PROFILES = { |
| 24 | "amdserve/kfd-word": ("omc KFD words: checked ioctl records, mappings and aligned reservations for Linux AArch64/x86 " |
| 25 | "(kfdctl, kfdmap, kfdres; constrained to the reviewed UAPI)", "Add checked KFD ioctl"), |
| 26 | "omc/inline-edges": ("omc inline-cycle check reads the inline-edge tables only (no trap 3 past the calls' " |
| 27 | "room; gate-nocaps drives every table past its first room)", "omc: inline-cycle check"), |
| 28 | "omc/pools": ("omc run-time pools, landing 1 (global NAME T *, enlarge, len; OMEGESE 9.15)", |
| 29 | "omc: run-time pools"), |
| 30 | "omc/pools-arena": ("omc's arena is a run-time pool that grows under a caller-stated memory ceiling, OMC_MEMORY_MAX " |
| 31 | "(om4 learns the pool subset and envcnt/envread; error 14 now means the host or the ceiling " |
| 32 | "refused)", "omc: the arena a_ar is a run-time pool"), |
| 33 | } |
| 34 | CLASSIFY = "5dd5bb47" # native/prefetch/README.md: the compilers since b1d9cff6 emit this classifier |
| 35 | FILES = """native/orig4/orig4.pin native/prefetch/compiler.sha256 om/aarch64/parity/run.sh |
| 36 | om/codec/parity/run.sh om/evidence/parity/run.sh om/gpumath/parity/run.sh om/quantity/README.md |
| 37 | om/receipt/README.md om/receipt/parity/run.sh om/sha256/README.md om/units.sh seed/equiv/gate-irequiv.sh |
| 38 | seed/json/tests/gate-json.sh seed/nki/gate-irnki.sh seed/semhash/README.md |
| 39 | seed/semhash/check.sh seed/spirv/gate-irspirv.sh seed/spirv/gate-vkspirv.sh |
| 40 | seed/stablehlo/gate-irstablehlo.sh seed/tensix/gate-tensix.sh seed/wasm/gate-irwasm.sh""".split() |
| 41 | SHORT = ["om/gpumath/README.md", "om/codec/README.md"] |
| 42 | NL, AU, PF = "neuralese/README.md", "AUDIT_SCOPE.md", "native/prefetch/README.md" |
| 43 | GEN = ["src/omega_om_units.c", "src/omega_om_units.ident", "src/omega_om_units_fs.c", "src/omega_native.h", |
| 44 | "src/omega_api_decl.h"] |
| 45 | |
| 46 | def sha(p): return hashlib.sha256(open(p, "rb").read()).hexdigest() |
| 47 | def rd(p): return open(p, encoding="utf-8").read() |
| 48 | def wr(p, s): open(p, "w", encoding="utf-8").write(s) |
| 49 | def die(rc, msg): print("repin3: " + msg, file=sys.stderr); sys.exit(rc) |
| 50 | def git(*a): return subprocess.run(["git"] + list(a), capture_output=True, text=True).stdout.strip() |
| 51 | def once(s, a, b, what): |
| 52 | n = s.count(a) |
| 53 | if n != 1: die(1, "%s: %d of %r" % (what, n, a)) |
| 54 | return s.replace(a, b) |
| 55 | def once_re(s, pat, rep, what): |
| 56 | r, n = re.subn(pat, rep, s) |
| 57 | if n != 1: die(1, "%s: %d of /%s/" % (what, n, pat)) |
| 58 | return r |
| 59 | |
| 60 | # ---- this landing |
| 61 | BRANCH = os.environ.get("REPIN_BRANCH") or git("symbolic-ref", "--short", "-q", "HEAD") |
| 62 | prof = PROFILES.get(BRANCH, (None, None)) |
| 63 | WHAT = os.environ.get("REPIN_WHAT") or prof[0] |
| 64 | LANDING = os.environ.get("REPIN_LANDING") or prof[1] |
| 65 | if not (BRANCH and WHAT and LANDING): |
| 66 | die(3, "branch %r has no profile; set REPIN_BRANCH, REPIN_WHAT and REPIN_LANDING" % BRANCH) |
| 67 | MAIN = os.environ.get("REPIN_MAIN", "") |
| 68 | if not MAIN: # the landing commit's first parent (HEAD may be a repin commit above the landing) |
| 69 | for l in git("log", "--first-parent", "-50", "--format=%H %s").splitlines(): |
| 70 | if l.split(" ", 1)[1].startswith(LANDING): |
| 71 | MAIN = git("rev-parse", "--short=8", l.split()[0] + "^"); break |
| 72 | if not re.fullmatch(r"[0-9a-f]{8}", MAIN or ""): |
| 73 | die(3, "Radicle main unknown: no %r commit on HEAD's first-parent line; set REPIN_MAIN" % LANDING) |
| 74 | DATE = os.environ.get("REPIN_DATE") or datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d") |
| 75 | WHATB = "%s, branch `%s` on Radicle main %s" % (WHAT, BRANCH, MAIN) |
| 76 | NEW, NEW_SRC = sha("seed/out/omc"), sha("seed/omc/omc.om") |
| 77 | |
| 78 | # ---- the pin being replaced and its narrative |
| 79 | m = re.search(r"^OMC_PIN=([0-9a-f]{64})$", rd("om/units.sh"), re.M) |
| 80 | if not m: die(1, "om/units.sh: no OMC_PIN") |
| 81 | OLD = m.group(1) |
| 82 | m = re.search(r"^([0-9a-f]{64}) seed/omc/omc\.om$", rd("native/prefetch/compiler.sha256"), re.M) |
| 83 | if not m: die(1, "native/prefetch/compiler.sha256: no seed/omc/omc.om row") |
| 84 | OLD_SRC = m.group(1) |
| 85 | HEAD_RE = re.compile(r"Current pin after (.*?), branch `([^`]+)` on Radicle main ([0-9a-f]{8}) \((\d{4}-\d\d-\d\d)\): " |
| 86 | r"committed\n`seed/out/omc` SHA-256\n`([0-9a-f]{64})`, compiler\nsource SHA-256 `([0-9a-f]{64})`\.\n" |
| 87 | r"The previous pin, after ", re.S) |
| 88 | hs = HEAD_RE.findall(rd(NL)) |
| 89 | if len(hs) != 1: die(1, "%s: %d current-pin headers" % (NL, len(hs))) |
| 90 | CUR_X, CUR_B, CUR_M, CUR_D, cur_pin, cur_src = hs[0] |
| 91 | if (cur_pin, cur_src) != (OLD, OLD_SRC): |
| 92 | die(1, "%s names %s/%s as current, om/units.sh and compiler.sha256 %s/%s" % (NL, cur_pin[:8], cur_src[:8], OLD[:8], OLD_SRC[:8])) |
| 93 | chain = CUR_B != BRANCH |
| 94 | |
| 95 | out = {} |
| 96 | def cur(p): return out.get(p) or rd(p) |
| 97 | if OLD != NEW: |
| 98 | for p in (NL, AU, PF): # a new short sha already named there would make the short-form edits ambiguous |
| 99 | if NEW[:8] in rd(p): die(1, "%s already names %s; refusing an ambiguous repin" % (p, NEW[:8])) |
| 100 | for p in FILES: |
| 101 | s = rd(p) |
| 102 | if OLD not in s: die(1, "%s: no %s" % (p, OLD)) |
| 103 | out[p] = s.replace(OLD, NEW).replace(OLD_SRC, NEW_SRC) |
| 104 | for p in SHORT: |
| 105 | out[p] = once(rd(p), "%s…%s" % (OLD[:8], OLD[-4:]), "%s…%s" % (NEW[:8], NEW[-4:]), p) |
| 106 | # The prefetch README lists the new omc among those emitting CLASSIFY: check it, never assume it. |
| 107 | c = subprocess.run(["seed/out/omc"], stdin=open("native/prefetch/classify.om", "rb"), capture_output=True) |
| 108 | csha = hashlib.sha256(c.stdout).hexdigest() |
| 109 | if c.returncode != 0 or not csha.startswith(CLASSIFY): |
| 110 | die(1, "classify.om with the new omc: rc %d sha256 %s, not %s; the README claim would be false" |
| 111 | % (c.returncode, csha, CLASSIFY)) |
| 112 | if chain: |
| 113 | L = os.environ.get("REPIN_PREV_LANDED") or git("log", "-1", "--format=%h", "--abbrev=8", "-S", "OMC_PIN=" + OLD, "--", "om/units.sh") |
| 114 | if not re.fullmatch(r"[0-9a-f]{8,}", L): die(3, "no commit set OMC_PIN=%s; set REPIN_PREV_LANDED" % OLD[:8]) |
| 115 | LD = os.environ.get("REPIN_PREV_LANDED_DATE") or git("log", "-1", "--format=%ad", "--date=short", L) |
| 116 | tag = "branch `%s`, landed as %s, %s" % (CUR_B, L[:8], LD) |
| 117 | prevx = CUR_X[:-1] + "; " + tag + ")" if CUR_X.endswith(")") else "%s (%s)" % (CUR_X, tag) |
| 118 | s = rd(NL) |
| 119 | a = HEAD_RE.search(s).group(0) |
| 120 | s = once(s, a, "Current pin after %s (%s): committed\n`seed/out/omc` SHA-256\n`%s`, compiler\n" |
| 121 | "source SHA-256 `%s`.\nThe previous pin, after %s, was\n`%s` (source\n`%s`); before that, after " |
| 122 | % (WHATB, DATE, NEW, NEW_SRC, prevx, OLD, OLD_SRC), NL) |
| 123 | for a in (" %s \\\n" % OLD, "build/nl4-gate seed/out/omc %s " % OLD, "the committed compiler is `%s...`;" % OLD[:8]): |
| 124 | s = once(s, a, a.replace(OLD, NEW).replace(OLD[:8] + "...", NEW[:8] + "..."), NL) |
| 125 | out[NL] = s |
| 126 | s = rd(AU) |
| 127 | k = " `%s`,\n from `seed/omc/omc.om` `%s`; " % (OLD, OLD_SRC) |
| 128 | if s.count(k) != 1: die(1, "%s: current pin block not found" % AU) |
| 129 | i = s.index(k); j = s.index("). The earlier audit pins\n", i) |
| 130 | oldwhat = s[i:j].split("; ", 1)[1] |
| 131 | out[AU] = s[:i] + " `%s`,\n from `seed/omc/omc.om` `%s`; %s). The earlier audit pins\n `%s` (%s),\n" % ( |
| 132 | NEW, NEW_SRC, WHATB, OLD, oldwhat.replace("\n", " ")) + s[j + len("). The earlier audit pins\n"):] |
| 133 | s = rd(PF) |
| 134 | P, N = OLD[:8], NEW[:8] |
| 135 | s = once(s, "`%s`, built from\n`seed/omc/omc.om` `%s`" % (OLD, OLD_SRC), |
| 136 | "`%s`, built from\n`seed/omc/omc.om` `%s`" % (NEW, NEW_SRC), PF) |
| 137 | s = once(s, "then %s) and" % P, "then %s, then %s) and" % (P, N), PF) |
| 138 | s = once_re(s, r" and at %s on ([^\s)]+)\)" % P, r", at %s on \1 and at %s on %s)" % (P, N, BRANCH), PF) |
| 139 | s = once(s, " and %s emit %s" % (P, CLASSIFY), ", %s and %s emit %s" % (P, N, CLASSIFY), PF) |
| 140 | out[PF] = s |
| 141 | else: |
| 142 | for p in (NL, AU, PF): |
| 143 | s = rd(p) |
| 144 | if OLD not in s: die(1, "%s: no %s" % (p, OLD)) |
| 145 | out[p] = s.replace(OLD, NEW).replace(OLD_SRC, NEW_SRC).replace(OLD[:8], NEW[:8]) |
| 146 | for p, s in out.items(): wr(p, s) |
| 147 | print("repinned text %s -> %s %s (%d files; %s)" % (OLD[:8], NEW, NEW_SRC, len(out), |
| 148 | "%s now previous" % CUR_B if chain else "rebuild of %s" % BRANCH)) |
| 149 | else: |
| 150 | print("text already at", NEW) |
| 151 | |
| 152 | # ---- the current narrative names this landing's Radicle main (the landing may have been rebased) |
| 153 | mainre = re.compile(r"branch `%s` on Radicle main [0-9a-f]{8}" % re.escape(BRANCH)) |
| 154 | for p in (NL, AU): |
| 155 | s = rd(p) |
| 156 | if len(mainre.findall(s)) != 1: die(1, "%s: %d of /%s/" % (p, len(mainre.findall(s)), mainre.pattern)) |
| 157 | t = mainre.sub("branch `%s` on Radicle main %s" % (BRANCH, MAIN), s) |
| 158 | if t != s: wr(p, t); print("%s: Radicle main -> %s" % (p, MAIN)) |
| 159 | |
| 160 | # ---- regenerate the C bindings with the pinned bridge; only the omc= pins may change |
| 161 | lock = [l.strip() for l in rd("om/omega-bridge.lock").splitlines() if l.strip() and not l.startswith("#")][0] |
| 162 | native_pin = re.search(r"^NATIVE_PIN=([0-9a-f]{64})$", rd("om/units.sh"), re.M).group(1) |
| 163 | repo = os.environ.get("REPIN_BRIDGE_REPO", os.path.join("..", "omega-bridge")) |
| 164 | bridge = os.path.abspath(os.path.join("build", "bridge-" + lock[:12])) |
| 165 | if not (os.path.exists(bridge + "/src/native.om") and sha(bridge + "/src/native.om") == native_pin): |
| 166 | os.makedirs(bridge, exist_ok=True) |
| 167 | a = subprocess.run(["git", "-C", repo, "archive", lock], capture_output=True) |
| 168 | if a.returncode != 0: die(3, "git archive %s in %s: %s" % (lock, repo, a.stderr.decode().strip())) |
| 169 | subprocess.run(["tar", "-x", "-C", bridge], input=a.stdout, check=True) |
| 170 | if sha(bridge + "/src/native.om") != native_pin: die(3, "bridge %s native.om is not %s" % (lock, native_pin)) |
| 171 | env = dict(os.environ, OMEGA_OMC=os.path.abspath("seed/out/omc"), OMEGA_BRIDGE=bridge) |
| 172 | before = {p: rd(p) for p in GEN} |
| 173 | r = subprocess.run(["sh", "om/units.sh"], env=env, stdout=subprocess.DEVNULL) |
| 174 | if r.returncode != 0: die(4, "om/units.sh exit %d" % r.returncode) |
| 175 | pin = re.compile(r"omc=[0-9a-f]{64}") |
| 176 | diff = [p for p in GEN if rd(p) != pin.sub("omc=" + NEW, before[p])] |
| 177 | if diff: die(4, "regeneration changed more than the omc pin in: " + " ".join(diff)) |
| 178 | r = subprocess.run(["sh", "om/units.sh", "--check"], env=env) |
| 179 | if r.returncode != 0: die(4, "om/units.sh --check exit %d" % r.returncode) |
| 180 | os.makedirs("build", exist_ok=True) |
| 181 | wr("build/repin3.last", "%s %s\n" % (NEW, NEW_SRC)) |
| 182 | print("repin3 done: %s omc %s source %s main %s bridge %s" % (BRANCH, NEW, NEW_SRC, MAIN, lock[:8])) |