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 / seed / qualification / om2 / oracle / c2 / enumerate.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Enumeration tool for the OM-2 C2 manifest (#748, ledger L836). NOT part of the delivered closure: |
| 4 | # it ran once, outside it, to freeze seed/qualification/om2/manifest-c2.txt. The delivered checker is |
| 5 | # seed/qualification/om2/gate.om (emit cases). See CAPTURE.txt for how the inputs were collected. |
| 6 | # |
| 7 | # Inputs (all read-only): |
| 8 | # CAP capture worktree at acb3f15 (git worktree --detach), whose seed/out/omc was a recording |
| 9 | # shim in front of the pinned omc; the shim wrote every stdin it got to CORPUS/in.* and the |
| 10 | # gate name to in.*.who |
| 11 | # CORPUS the recorded inputs |
| 12 | # G5 the gate5 inline programs (gate5.sh section 3b, 5d, 7), written by the same commands |
| 13 | # BRIDGE an omega-bridge clone; every .om at the om/omega-bridge.lock commit 012860db is added |
| 14 | # Every distinct input (by sha256 of its bytes) except seed/omc/omc.om is compiled once by the pinned |
| 15 | # compiler OMC (sha256 checked) and recorded: exit status, sha256 of stdout, exact stderr. |
| 16 | import hashlib, os, re, subprocess, sys |
| 17 | |
| 18 | OMC_PIN = "734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61" |
| 19 | CAP, CORPUS, G5, BRIDGE, OMC, OUT = sys.argv[1:7] |
| 20 | BRIDGE_REV = "012860dbf26ee62a9838594b9dfd1e3e94a540a9" |
| 21 | PACKREL = "seed/qualification/om2/c2/corpus.pack" |
| 22 | CYC = { # inputs above 1 MiB: their longest run of one of these units becomes a cyc: segment |
| 23 | "nl": ("seed/qualification/om2/c2/cyc-nl.txt", b"\n"), |
| 24 | "filler": ("seed/qualification/om2/c2/cyc-filler.txt", b"note filler\n"), |
| 25 | "stmt": ("seed/qualification/om2/c2/cyc-stmt.txt", b"x = add x 1\n"), |
| 26 | } |
| 27 | |
| 28 | def sha(b): |
| 29 | return hashlib.sha256(b).hexdigest() |
| 30 | |
| 31 | assert sha(open(OMC, "rb").read()) == OMC_PIN, "OMC is not the pinned compiler" |
| 32 | omc_om = open(os.path.join(CAP, "seed/omc/omc.om"), "rb").read() |
| 33 | |
| 34 | units = {} # sha -> bytes |
| 35 | origins = {} # sha -> [origin] |
| 36 | order = [] |
| 37 | |
| 38 | def add(data, origin): |
| 39 | h = sha(data) |
| 40 | if data == omc_om: |
| 41 | return |
| 42 | if h not in units: |
| 43 | units[h] = data |
| 44 | origins[h] = [] |
| 45 | order.append(h) |
| 46 | if origin not in origins[h]: |
| 47 | origins[h].append(origin) |
| 48 | |
| 49 | # 1. static: every tracked .om / .omegese of the baseline tree (seed/rung4/tests, seed/omc/tests, |
| 50 | # om/**, security673, tests/one_layer, tools/exp445, seed/host/tests, ...) |
| 51 | files = subprocess.run(["git", "-C", CAP, "ls-files", "*.om", "*.omegese"], capture_output=True, |
| 52 | text=True, check=True).stdout.split() |
| 53 | for f in sorted(files): |
| 54 | data = subprocess.run(["git", "-C", CAP, "show", "HEAD:" + f], capture_output=True, check=True).stdout |
| 55 | add(data, "tree:" + f) |
| 56 | # 2. omega-bridge at the lock commit (om-units generator src/native.om and the bridge's own units) |
| 57 | bfiles = subprocess.run(["git", "-C", BRIDGE, "ls-tree", "-r", "--name-only", BRIDGE_REV], |
| 58 | capture_output=True, text=True, check=True).stdout.split() |
| 59 | for f in sorted(x for x in bfiles if x.endswith(".om")): |
| 60 | data = subprocess.run(["git", "-C", BRIDGE, "show", BRIDGE_REV + ":" + f], capture_output=True, |
| 61 | check=True).stdout |
| 62 | add(data, "bridge@012860db:" + f) |
| 63 | # 3. recorded inputs of the seed gates (gate4 capture, gate6-17, gate-ir, host gate, nextfuzz) |
| 64 | rec = sorted(n for n in os.listdir(CORPUS) if n.startswith("in.") and not n.endswith(".who")) |
| 65 | rec.sort(key=lambda n: os.stat(os.path.join(CORPUS, n)).st_mtime_ns) |
| 66 | for n in rec: |
| 67 | who = open(os.path.join(CORPUS, n + ".who")).read().split()[-1] |
| 68 | add(open(os.path.join(CORPUS, n), "rb").read(), "gate:" + who) |
| 69 | # 4. gate669 (security673): every stdin of a compiler child (1001 stage1, 1002 stage2, 1007 and 1076 |
| 70 | # branch-limit mutant compilers built from branch-mutate.om output) |
| 71 | for line in open(os.path.join(CAP, "build/gate669.log")): |
| 72 | m = re.match(r"^build/security673-native/(1001|1002|1007|1076) < (\S+) > ", line) |
| 73 | if m: |
| 74 | add(open(os.path.join(CAP, m.group(2)), "rb").read(), "gate669:" + m.group(2)) |
| 75 | # 5. gate5 inline programs (gate5.sh 3b, 5d, 7) and the two capacity inputs over 8 MiB |
| 76 | for n in ["ok", "e4", "e11", "e12", "e13", "bad", "m", "zm", "e14"]: |
| 77 | add(open(os.path.join(G5, n + ".om"), "rb").read(), "gate5:" + n + ".om") |
| 78 | # 6. the #748 lane's own OM-1 units (appended last, so entries of 1-5 keep their ids): every .om under |
| 79 | # OUT (seed/qualification/om2) and every SRC (a+b concatenation) of its case lists whose first line |
| 80 | # that is not a note or blank (E10) does not start with the word om2 |
| 81 | def is_om2(data): |
| 82 | for ln in data.split(b"\n"): |
| 83 | w = ln.split() |
| 84 | if not w or w[0] == b"note": |
| 85 | continue |
| 86 | return w[0] == b"om2" |
| 87 | return False |
| 88 | |
| 89 | def qual_src(spec): |
| 90 | if spec.startswith(("pack:", "cyc:")): |
| 91 | return None |
| 92 | data = b"" |
| 93 | for p in spec.split("+"): |
| 94 | f = os.path.join(OUT, "../../..", p) |
| 95 | if not p.startswith("seed/qualification/om2/") and not p.startswith("om/"): |
| 96 | return None |
| 97 | if not os.path.isfile(f): |
| 98 | return None |
| 99 | data += open(f, "rb").read() |
| 100 | return data |
| 101 | |
| 102 | qroot = os.path.abspath(OUT) |
| 103 | for dp, dn, fn in sorted(os.walk(qroot)): |
| 104 | dn.sort() |
| 105 | if os.path.relpath(dp, qroot).split(os.sep)[0] in ("c2", "evidence"): |
| 106 | continue |
| 107 | for f in sorted(fn): |
| 108 | p = os.path.join(dp, f) |
| 109 | rel = "seed/qualification/om2/" + os.path.relpath(p, qroot) |
| 110 | if f.endswith(".om"): |
| 111 | data = open(p, "rb").read() |
| 112 | if not is_om2(data): |
| 113 | add(data, "qual:" + rel) |
| 114 | elif f.endswith(".cases") and f != "manifest-c2.txt": |
| 115 | for line in open(p, encoding="latin-1"): |
| 116 | t = line.split() |
| 117 | if len(t) < 3 or t[0].startswith("#"): |
| 118 | continue |
| 119 | specs = [] |
| 120 | if t[1] in ("run", "kill", "refuse", "emit"): |
| 121 | specs = [t[2]] |
| 122 | elif t[1] == "same" and len(t) >= 4: |
| 123 | specs = [t[2], t[3]] |
| 124 | for s in specs: |
| 125 | data = qual_src(s) |
| 126 | if data is not None and not is_om2(data): |
| 127 | add(data, "qual:%s:%s" % (rel, t[0])) |
| 128 | |
| 129 | def compile_(data): |
| 130 | p = subprocess.run([OMC], input=data, capture_output=True, timeout=120) |
| 131 | return p.returncode, p.stdout, p.stderr |
| 132 | |
| 133 | def run_of(data, u): |
| 134 | """(start, end) of the longest stretch data[start:end] == u * k found from u*256 seeds""" |
| 135 | best = (0, 0) |
| 136 | seed = u * 256 |
| 137 | s = data.find(seed) |
| 138 | while s >= 0: |
| 139 | e = s |
| 140 | while data.startswith(u, e): |
| 141 | e += len(u) |
| 142 | b = s |
| 143 | while b >= len(u) and data[b - len(u):b] == u: |
| 144 | b -= len(u) |
| 145 | if e - b > best[1] - best[0]: |
| 146 | best = (b, e) |
| 147 | s = data.find(seed, e) |
| 148 | return best |
| 149 | |
| 150 | def segments(data): |
| 151 | """list of ('pack', bytes) / ('cyc', path, n) whose concatenation is data""" |
| 152 | if len(data) <= 1 << 20: |
| 153 | return [("pack", data)] |
| 154 | cands = [] |
| 155 | for k, (path, u) in CYC.items(): |
| 156 | b, e = run_of(data, u) |
| 157 | cands.append((e - b, b, e, path, u)) |
| 158 | n, b, e, path, u = max(cands) |
| 159 | assert n * 10 >= len(data) * 9, "big input without a dominant repeated unit" |
| 160 | tail = data[e:] |
| 161 | k = n |
| 162 | # extend the cyc segment over a partial unit at the end (e.g. a cut "note fi") |
| 163 | p = 0 |
| 164 | while p < len(tail) and p < len(u) and tail[p] == u[p]: |
| 165 | p += 1 |
| 166 | segs = [] |
| 167 | if b: |
| 168 | segs.append(("pack", data[:b])) |
| 169 | segs.append(("cyc", path, k + p)) |
| 170 | if tail[p:]: |
| 171 | segs.append(("pack", tail[p:])) |
| 172 | assert all(len(s[1]) <= 1 << 20 for s in segs if s[0] == "pack") |
| 173 | return segs |
| 174 | |
| 175 | pack = bytearray() |
| 176 | manifest, index = [], [] |
| 177 | stderr_forms = {} |
| 178 | for i, h in enumerate(order, 1): |
| 179 | data = units[h] |
| 180 | rc, out, err = compile_(data) |
| 181 | cid = "c2-%05d" % i |
| 182 | parts = [] |
| 183 | for s in segments(data): |
| 184 | if s[0] == "cyc": |
| 185 | parts.append("cyc:%s:%d" % (s[1], s[2])) |
| 186 | else: |
| 187 | hdr = ("\n#### %s sha256=%s len=%d\n" % (cid, h, len(data))).encode() |
| 188 | pack += hdr |
| 189 | parts.append("pack:%s:%d:%d" % (PACKREL, len(pack), len(s[1]))) |
| 190 | pack += s[1] |
| 191 | src = "+".join(parts) |
| 192 | so = "sha:" + sha(out) if out else "-" |
| 193 | m = re.fullmatch(rb"omc: line (\d+): error (\d+)\n", err) |
| 194 | if not err: |
| 195 | se = "-" |
| 196 | elif m and int(m.group(2)) == rc: |
| 197 | se = "err:%s:%s" % (m.group(1).decode(), m.group(2).decode()) |
| 198 | else: |
| 199 | p = "seed/qualification/om2/c2/stderr-%s.txt" % sha(err)[:16] |
| 200 | stderr_forms[p] = err |
| 201 | se = "file:" + p |
| 202 | manifest.append("%s emit %s %s %s %d" % (cid, src, so, se, rc)) |
| 203 | index.append("\t".join([cid, h, str(len(data)), str(rc), sha(out) if out else "-", |
| 204 | err.decode("latin-1").rstrip("\n").replace("\t", " ") or "-", |
| 205 | ",".join(origins[h])])) |
| 206 | |
| 207 | os.makedirs(os.path.join(OUT, "c2"), exist_ok=True) |
| 208 | open(os.path.join(OUT, "c2/corpus.pack"), "wb").write(bytes(pack)) |
| 209 | for k, (path, pat) in CYC.items(): |
| 210 | open(os.path.join(OUT, path.split("seed/qualification/om2/")[1]), "wb").write(pat) |
| 211 | for p, e in stderr_forms.items(): |
| 212 | open(os.path.join(OUT, p.split("seed/qualification/om2/")[1]), "wb").write(e) |
| 213 | hdr = [ |
| 214 | "# OM-2 C2 manifest (contract spec/omegese-om2.md section 0, C2), frozen by the #748 lane (L836).", |
| 215 | "# Every entry is an OM-1 unit compiled by the pinned omc %s (main acb3f15)" % OMC_PIN, |
| 216 | "# and its result under that compiler: stdout sha256 (sha:) or empty (-), the exact stderr", |
| 217 | "# (err:LINE:CODE = \"omc: line LINE: error CODE\"), and the exit status. A candidate OM-2 compiler", |
| 218 | "# passes C2 only if every entry PASSes: build/om2-qual/gate CANDIDATE SHA manifest-c2.txt SCRATCH.", |
| 219 | "# Sources are pinned by bytes: pack:PATH:OFFSET:LENGTH slices of c2/corpus.pack, or cyc:PATH:N", |
| 220 | "# (N bytes repeating PATH). c2/index.tsv lists each entry's source sha256, length, origins.", |
| 221 | "# Scope (contract C2): every tracked .om/.omegese of acb3f15 except seed/omc/omc.om; every .om of", |
| 222 | "# omega-bridge 012860db (om/omega-bridge.lock); every input the seed gates gave the compiler", |
| 223 | "# (seed/rung4/gate4.sh with omc in om4's place as gate5 runs it, seed/omc/gate5.sh inline programs", |
| 224 | "# and seed/check/nextfuzz.sh 20, gate6-17, gate-ir, seed/host/gate.sh, security673/gate669.om).", |
| 225 | "# Excluded: omc.om itself (C2), gate5 section 7 prefixes of omc.om, and gate5 error-1/2 I/O cases", |
| 226 | "# (a directory as input, /dev/full as output), which have no unit bytes. Appended after those: the", |
| 227 | "# #748 lane's own OM-1 units (qual: origins; every .om and case-list source under", |
| 228 | "# seed/qualification/om2 that is not OM-2 by its first non-note, non-blank line). Entries=%d." % len(manifest), |
| 229 | "deadline 120", |
| 230 | ] |
| 231 | open(os.path.join(OUT, "manifest-c2.txt"), "w").write("\n".join(hdr + manifest) + "\n") |
| 232 | open(os.path.join(OUT, "c2/index.tsv"), "w").write( |
| 233 | "id\tsource_sha256\tbytes\texit\tstdout_sha256\tstderr\torigins\n" + "\n".join(index) + "\n") |
| 234 | print("entries", len(manifest), "pack bytes", len(pack), "stderr forms", len(stderr_forms)) |