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 / tools / trit / proof / gen.py
| 1 | #!/usr/bin/env python3 |
| 2 | """gen.py ROOT N SEED CODEBOOK.tsv -- write N random fixture dirs ROOT/<i>/inbox/... for difftest.sh. |
| 3 | ASCII only. Lines mix valid/invalid timestamps, senders, tags, STOP near-misses, 5/6-field formats, |
| 4 | pipes inside text, emergency floods (4-7 lines from one sender inside 30 min), codebook-signed lines |
| 5 | (ocb2 over lane|ts|from|to|tag|text|ref, plus leftover ocb1, retargeted `to`, edited text/tag/ref/ts, |
| 6 | tampered digits/sender/MAC, wrong codebook id, replayed copies, stale and malformed counter files, |
| 7 | unknown codes, bad counters, ts-regress against a stored last ts), headers, blank and partial lines; |
| 8 | cursors are absent, valid (sha256 of the whole prefix, via hashlib), wrong-hash, garbage, past EOF, |
| 9 | mid-line with a matching prefix hash, or offset 0. Keys are mode 0600 and live outside boards/.""" |
| 10 | import hashlib, hmac, os, random, shutil, sys |
| 11 | |
| 12 | root, n, seed = sys.argv[1], int(sys.argv[2]), int(sys.argv[3]) |
| 13 | CBT = sys.argv[4] # tools/codebook/codebook.tsv |
| 14 | CBB = open(CBT, "rb").read(); CS = hashlib.sha256(CBB).hexdigest() |
| 15 | KEY = None |
| 16 | R = random.Random(seed) |
| 17 | |
| 18 | def ts(): |
| 19 | r = R.random() |
| 20 | if r < 0.08: return R.choice(["2026-11-15T25:00", "2026-13-15T12:00", "2026-11-15T12", "garbage-line", "0000-01-01T00:00", "2026-11-15T12:7x"]) |
| 21 | if r < 0.12: return R.choice(["2026-12-31T23:", "2027-01-01T00:"]) + "%02d" % R.randrange(60) |
| 22 | return "2026-11-15T%02d:%02d" % (R.choice([11, 12, 12, 13]), R.randrange(60)) |
| 23 | |
| 24 | def mac_msg(lane, fields, ctr, digs, cs): |
| 25 | # fields: ts, from, to, tag, text, ref |
| 26 | ts_, frm, to, tag, text, ref = fields |
| 27 | return "ocb2|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s" % (cs, ctr, lane, ts_, frm, to, tag, text, ref, digs) |
| 28 | |
| 29 | def sig(fields, lane, legacy=False): |
| 30 | CT[0] += R.choice([1, 1, 1, 2, -1, 0]) |
| 31 | ctr = str(max(CT[0], 0)) if R.random() < 0.9 else R.choice(["0", "01", "x", "1234567890123456789"]) |
| 32 | digs = R.choice(["300", "300", "301", "310", "100", "101", "000001", "", "303002", "900300", "30", "400", "3a0"]) |
| 33 | fs = list(fields) |
| 34 | if R.random() >= 0.9: |
| 35 | fs[1] = R.choice(["drake", "b", "mallory"]) |
| 36 | cs = CS if R.random() < 0.93 else hashlib.sha256(b"other").hexdigest() |
| 37 | if legacy: |
| 38 | mac = hmac.new(KEY, ("%s|%s|%s|%s" % (fs[1], CS, ctr, digs)).encode(), hashlib.sha256).hexdigest() |
| 39 | pref = "ocb1" |
| 40 | else: |
| 41 | mac = hmac.new(KEY, mac_msg(lane, fs, ctr, digs, CS).encode(), hashlib.sha256).hexdigest() |
| 42 | pref = "ocb2" |
| 43 | r = R.random() |
| 44 | if r < 0.08: mac = mac[:5] + ("0" if mac[5] != "0" else "1") + mac[6:] |
| 45 | elif r < 0.12 and digs: digs = digs[:-1] + ("1" if digs[-1] != "1" else "2") |
| 46 | elif r < 0.14: mac = mac[:63] |
| 47 | elif r < 0.16: mac = mac.upper() |
| 48 | return "ocb%s:%s:%s:%s:%s" % (pref[3:], cs, ctr, digs, mac) + (":x" if R.random() < 0.02 else "") |
| 49 | |
| 50 | def line(emerg, force=None): |
| 51 | sender = R.choice(["drake", "drake", "b", "six-sigma", "mallory", "a", "Drake", "../x", "", "drake-bot"] if emerg else ["drake-bot", "x", "six-sigma"]) |
| 52 | tag = R.choice(["turn", "fyi", "stop", "fail", "task", "STOP", "alert", "fyi"]) |
| 53 | text = R.choice(["do x", "STOP", "STOP now", "STOP:x", "STOPWATCH", "stop", "", "a|b", "STOP|x", "note"]) |
| 54 | ref = R.choice(["-", "-", "STOP.md", "/p/q"]) |
| 55 | to = "a" if R.random() < 0.85 else R.choice(["b", "x", "six-sigma"]) |
| 56 | lane = "e" if emerg else "n" |
| 57 | signed = force in ("valid", "retarget", "edit-text", "edit-tag", "edit-ref", "edit-ts", "ocb1", "lane") or R.random() < 0.45 |
| 58 | if signed or R.random() < 0.5 or force: |
| 59 | f = [ts(), sender, to, tag, text, ref] |
| 60 | if force == "valid": |
| 61 | f = ["2026-11-15T12:%02d" % R.randrange(60), "drake" if emerg else "drake-bot", "a", "alert" if emerg else "task", "do x", "-"] |
| 62 | CT[0] += 1 |
| 63 | ctr = str(max(CT[0], 1)) |
| 64 | digs = "300" |
| 65 | mac = hmac.new(KEY, mac_msg(lane, f, ctr, digs, CS).encode(), hashlib.sha256).hexdigest() |
| 66 | return "|".join(f + ["ocb2:%s:%s:%s:%s" % (CS, ctr, digs, mac)]) |
| 67 | if force == "ocb1": |
| 68 | f = ["2026-11-15T12:00", "drake" if emerg else "drake-bot", "a", "alert", "do x", "-"] |
| 69 | CT[0] += 1 |
| 70 | ctr = str(max(CT[0], 1)) |
| 71 | digs = "300" |
| 72 | mac = hmac.new(KEY, ("%s|%s|%s|%s" % (f[1], CS, ctr, digs)).encode(), hashlib.sha256).hexdigest() |
| 73 | return "|".join(f + ["ocb1:%s:%s:%s:%s" % (CS, ctr, digs, mac)]) |
| 74 | s = sig(f, lane, legacy=(force == "ocb1" or R.random() < 0.06)) |
| 75 | if force == "retarget" or (force is None and R.random() < 0.04): |
| 76 | f[2] = "b" if f[2] != "b" else "x" |
| 77 | elif force == "edit-text" or (force is None and R.random() < 0.04): |
| 78 | f[4] = f[4] + "x" |
| 79 | elif force == "edit-tag" or (force is None and R.random() < 0.03): |
| 80 | f[3] = "fyi" if f[3] != "fyi" else "turn" |
| 81 | elif force == "edit-ref" or (force is None and R.random() < 0.03): |
| 82 | f[5] = "edited-ref" |
| 83 | elif force == "edit-ts" or (force is None and R.random() < 0.03): |
| 84 | f[0] = "2026-11-15T00:00" |
| 85 | elif force == "lane": |
| 86 | # sign with the other lane so the file's lane fails the MAC |
| 87 | s = sig(f, "n" if lane == "e" else "e") |
| 88 | return "|".join(f + [s]) |
| 89 | return "|".join([ts(), sender, tag, text, ref] if R.random() < 0.7 else [ts(), sender, to, tag, text, ref]) |
| 90 | |
| 91 | CT = [1] |
| 92 | |
| 93 | def flood(): |
| 94 | who = R.choice(["drake", "b", "six-sigma"]) |
| 95 | base = R.randrange(0, 40) |
| 96 | out = [] |
| 97 | for j in range(R.randrange(4, 8)): |
| 98 | f = ["2026-11-15T12:%02d" % (base + 3 * j + R.randrange(3)), who, "a", R.choice(["alert", "fyi", "stop"]), "flood %d" % j, "-"] |
| 99 | l = "|".join(f) |
| 100 | if R.random() < 0.7: l += "|" + sig(f, "e") |
| 101 | out.append(l) |
| 102 | if R.random() < 0.2: out.append(l) |
| 103 | return out |
| 104 | |
| 105 | def body(emerg, extras=None): |
| 106 | out = [] |
| 107 | if R.random() < 0.2: out.append("# Inbox: a") |
| 108 | if emerg and R.random() < 0.25: out += flood() |
| 109 | for _ in range(R.randrange(0, 7)): |
| 110 | out.append("" if R.random() < 0.05 else line(emerg)) |
| 111 | if out and R.random() < 0.1: out.append(R.choice(out)) |
| 112 | if extras: |
| 113 | out.extend(extras) |
| 114 | s = "".join(x + "\n" for x in out) |
| 115 | if R.random() < 0.15: s += line(emerg)[: R.randrange(1, 30)] |
| 116 | return s |
| 117 | |
| 118 | def cursor(content): |
| 119 | b = content.encode() |
| 120 | ends = [i + 1 for i, c in enumerate(b) if c == 10] |
| 121 | r = R.random() |
| 122 | if r < 0.35 or not ends: return None |
| 123 | off = R.choice(ends) |
| 124 | good = hashlib.sha256(b[:off]).hexdigest() |
| 125 | if r < 0.70: return "%d %s\n" % (off, good) |
| 126 | if r < 0.78: return "%d %s\n" % (off, hashlib.sha256(b[:off] + b"x").hexdigest()) |
| 127 | if r < 0.82: return "garbage\n" |
| 128 | if r < 0.86: return "%d %s\n" % (len(b) + R.randrange(1, 50), good) |
| 129 | if r < 0.92 and off > 1: return "%d %s\n" % (off - 1, hashlib.sha256(b[:off - 1]).hexdigest()) |
| 130 | if r < 0.96: return "0 %s\n" % hashlib.sha256(b"").hexdigest() |
| 131 | return "%d %s" % (off, good)[:R.randrange(10, 60)] |
| 132 | |
| 133 | KINDS = ["valid", "retarget", "edit-text", "edit-tag", "edit-ref", "edit-ts", "ocb1", "lane"] |
| 134 | |
| 135 | for i in range(n): |
| 136 | d = os.path.join(root, str(i), "inbox"); os.makedirs(d) |
| 137 | KEY = bytes(R.getrandbits(8) for _ in range(R.choice([16, 32, 64, 65, 100]))) |
| 138 | CT[0] = R.randrange(0, 4) |
| 139 | if R.random() < 0.9: |
| 140 | os.makedirs(os.path.join(root, str(i), "codebook")) |
| 141 | shutil.copyfile(CBT, os.path.join(root, str(i), "codebook", "codebook.tsv")) |
| 142 | if R.random() < 0.95: |
| 143 | kp = os.path.join(root, str(i), "codebook", "key") |
| 144 | open(kp, "wb").write(KEY) |
| 145 | os.chmod(kp, 0o600) |
| 146 | for snd in ["drake", "drake-bot", "x", "b", "six-sigma"]: |
| 147 | r = R.random() |
| 148 | if r < 0.20: |
| 149 | open(os.path.join(d, "a.%s.ctr" % snd), "w").write("%d 2026-11-15T12:%02d\n" % (R.randrange(0, 5), R.randrange(60))) |
| 150 | elif r < 0.25: |
| 151 | open(os.path.join(d, "a.%s.ctr" % snd), "w").write("%d\n" % R.randrange(0, 5)) |
| 152 | elif r < 0.28: |
| 153 | open(os.path.join(d, "a.%s.ctr" % snd), "w").write(R.choice(["junk\n", "", "\n", "3"])) |
| 154 | for other in ["b", "six-sigma"]: |
| 155 | if R.random() < 0.4: open(os.path.join(d, other + ".md"), "w").close() |
| 156 | kinds = [KINDS[i % len(KINDS)], KINDS[(i * 3) % len(KINDS)]] if KEY is not None and R.random() < 0.55 else [] |
| 157 | for name, emerg, p in [("a.md", False, 0.85), ("a.emergency.md", True, 0.6)]: |
| 158 | if R.random() < p: |
| 159 | extras = [line(emerg, force=k) for k in kinds] if kinds and not emerg else None |
| 160 | c = body(emerg, extras) |
| 161 | open(os.path.join(d, name), "w").write(c) |
| 162 | cur = cursor(c) |
| 163 | if cur is not None: open(os.path.join(d, name + ".cur"), "w").write(cur) |