Skip to content

Qwen3-0.6B inference (12 KB), bare-metal bootable OS (22 KB), and from-scratch Mini-GPT trainer with live Braille TUI (32 KB) in 100% pure x86-64 NASM assembly. Zero C, zero libc, zero dependencies.

4.7 KB
Raw Blame
#!/usr/bin/env python3
"""Drive the actual guest shell and capture the guest curses pane in tmux."""
import json
import os
import subprocess as sp
import time
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
os.chdir(ROOT)
TMUX = ["tmux", "-S", str(ROOT / "runs/.demo.tmux")]

def tm(*args):
    return sp.check_output(TMUX + list(args), text=True)

def eventually(check):
    deadline = time.monotonic() + 12
    while time.monotonic() < deadline:
        result = check()
        if result:
            return result
        time.sleep(.08)
    raise TimeoutError(check)

def main():
    if sp.run(TMUX + ["has-session", "-t", "=jev"], stderr=sp.DEVNULL).returncode == 0:
        raise SystemExit("Verification needs a fresh demo. Stop the existing demo first.")
    run = Path(sp.check_output(["testbed/start_demo_vm.sh", "--detached"], text=True).strip())
    report = {}
    def records():
        return [json.loads(s) for s in (run/"events.jsonl").read_text().splitlines() if s.endswith("}")]
    def counts():
        return json.loads((run/"counters.json").read_text())
    def shell(cmd):
        tm("send-keys", "-t", "jev:0.0", "-l", cmd)
        tm("send-keys", "-t", "jev:0.0", "Enter")
    def script(name, key):
        shell(f'demo/{name} > "$JEV_DEMO_RUN/{key}.out" 2>&1; echo $? > "$JEV_DEMO_RUN/{key}.rc"')
        rc = int(eventually(lambda: (run/f"{key}.rc").read_text().strip() if (run/f"{key}.rc").exists() else None))
        report[key] = {"rc":rc, "output":(run/f"{key}.out").read_text().strip(), **counts()}
        return rc
    def capture():
        return tm("capture-pane", "-p", "-t", "jev:0.1")
    try:
        tm("resize-window","-t","jev:0","-x","181","-y","36")
        tm("resize-pane","-t","jev:0.1","-x","120")
        eventually(lambda: (run/"geometry").read_text().startswith("36 120 "))
        shell('bpftool -j prog show > "$JEV_DEMO_RUN/progs-before.json"')
        first = script("1_dev_normal.py", "normal")
        second = script("2_dev_env_local.py", "local")
        third = script("3_exfil_fork_chain.py", "chain")
        eventually(lambda: "BLOCKED (-EPERM)" in capture() and ":19999" in capture() and "inherited=yes" in capture())
        frame = capture()
        (run/"hud.txt").write_text(frame)
        (ROOT/"runs/tui_demo_snapshot.txt").write_text(frame)
        rows = records()
        deny_index = next(i for i,e in enumerate(rows) if e.get("port")==19999)
        denied = rows[deny_index]
        fork_index = next(i for i,e in enumerate(rows[:deny_index]) if e["type"]=="task_alloc" and e["pid"]==denied["pid"])
        fork = rows[fork_index]
        opened = next(e for e in rows[:fork_index] if e.get("name")=="dummy_secret.env" and e["pid"]==fork["ppid"])
        executed = next(e for e in rows[fork_index+1:deny_index] if e["type"]=="bprm_exec" and e["pid"]==denied["pid"])
        report["lineage"] = [opened, fork, executed, denied]
        protected_connect = next(e for e in rows if e.get("port")==19998 and e["opens"])
        report["approved_after_env"] = protected_connect
        fourth = script("4_hotswap_weights.sh", "reload")
        eventually(lambda: "gen #2" in capture())
        shell('bpftool -j prog show > "$JEV_DEMO_RUN/progs-after.json"')
        probe = script("1_dev_normal.py", "post_reload")
        eventually(lambda: any(e.get("port")==19998 and e["gen"]==2 for e in records()))
        def ids(which):
            return {p["name"]:p["id"] for p in json.loads((run/f"progs-{which}.json").read_text()) if p.get("name","").startswith("jev_")}
        before, after = ids("before"), ids("after")
        report["bpf_program_ids"] = after
        checks = report["checks"] = {
            "normal":first==0 and report["normal"]["approved"]==4,
            "approved_after_env":second==0 and protected_connect["verdict"]=="ALLOW" and report["local"]["approved"]==5,
            "blocked_chain":third==0 and "[Errno 1]" in report["chain"]["output"] and report["chain"]["bytes"]==0 and denied["verdict"] in ("DENY -EPERM","ABSTAIN_DENY -EPERM"),
            "lineage":fork["inherited"]==127 and fork["h"]==opened["h"] and denied["ppid"]==opened["pid"] and all(e["hook_ns"]>0 for e in report["lineage"]),
            "hotswap":fourth==0 and probe==0 and len(after)==6 and before==after,
            "parity":"500 vectors, maximum embedding/state/score difference 0 LSB" in (run/"parity.txt").read_text()
        }
    finally:
        sp.run(["testbed/stop_demo_vm.sh"],check=True)
        if "checks" in report:
            report["checks"]["no_ring_drops"] = "dropped=0" in (run/"warden.log").read_text()
        (run/"verification.json").write_text(json.dumps(report,indent=2)+"\n")
    print(run, json.dumps(checks,indent=2))
    return 0 if all(checks.values()) else 1

if __name__ == "__main__":
    raise SystemExit(main())