Download scripts/agentloop_probe.py from Arko007/zenyx-agentic-loops: direct link, hf CLI and curl.
- Browser
- Download file 4.61 kB
-
https://huggingface.co/datasets/Arko007/zenyx-agentic-loops/resolve/main/scripts/agentloop_probe.py
- Command line
-
hf download hf://datasets/Arko007/zenyx-agentic-loops/scripts/agentloop_probe.py
-
curl -L -o agentloop_probe.py https://huggingface.co/datasets/Arko007/zenyx-agentic-loops/resolve/main/scripts/agentloop_probe.py
4.61 kB
| """Colab T4 probe (no upload, no token): run Spark through the SAME POLICY / gen_hint / judge code as agentloop_run.py on a few tasks per kind and print acceptance + reject reasons. | |
| python3 agentloop_probe.py --per_kind 16 --kinds confirm_destructive,async_job,tool_unavailable,error_recovery""" | |
| import argparse, ast, collections, json, os, random, subprocess, sys, tarfile, threading, time, urllib.request | |
| HERE = os.path.dirname(os.path.abspath(__file__)); sys.path.insert(0, HERE); sys.path.insert(0, os.path.dirname(os.path.dirname(HERE))) | |
| import agentloop_env as E | |
| from zenyx100m import chat as CH | |
| from huggingface_hub import hf_hub_download | |
| ap = argparse.ArgumentParser(); ap.add_argument("--per_kind", type=int, default=16); ap.add_argument("--kinds", default="confirm_destructive,async_job,tool_unavailable,error_recovery") | |
| ap.add_argument("--workdir", default="/content/alprobe"); ap.add_argument("--np", type=int, default=4); ap.add_argument("--temp", type=float, default=0.7); ap.add_argument("--show", type=int, default=2) | |
| A = ap.parse_args(); W = A.workdir; os.makedirs(W, exist_ok=True) | |
| src = open(os.path.join(HERE, "agentloop_run.py")).read(); tree = ast.parse(src) # reuse POLICY + to_openai from the real runner, verbatim | |
| keep = [n for n in tree.body if (isinstance(n, ast.Assign) and any(getattr(t, "id", "") == "POLICY" for t in n.targets)) or (isinstance(n, ast.FunctionDef) and n.name == "to_openai")] | |
| ns = {"CH": CH, "E": E}; exec(compile(ast.Module(keep, []), "runner_part", "exec"), ns); to_openai = ns["to_openai"]; assert "gen_hint" in src | |
| BIN = f"{W}/bin"; os.makedirs(BIN, exist_ok=True) | |
| p = hf_hub_download("Arko007/Zenyx-100M-SFT-synth", "v2/inputs/llama_cpp_cu128_sm75.tar.gz", repo_type="dataset", local_dir=W + "/hf", token=None); tarfile.open(p).extractall(BIN); os.chmod(f"{BIN}/llama-server", 0o755) | |
| MODEL = hf_hub_download("XHToken/Spark-X2.6-4B-GGUF", "Spark-X2.6-4B-Q8_0.gguf", local_dir=W + "/models", token=None) | |
| env = dict(os.environ, LD_LIBRARY_PATH=BIN + ":" + os.environ.get("LD_LIBRARY_PATH", "")) | |
| lf = open(f"{W}/server.log", "ab"); srv = subprocess.Popen([f"{BIN}/llama-server", "-m", MODEL, "-ngl", "99", "-c", str(A.np * 8192), "-np", str(A.np), "--port", "8123", "--jinja"], env=env, stdout=lf, stderr=lf) | |
| t0 = time.time() | |
| while time.time() - t0 < 900: | |
| if srv.poll() is not None: print(open(f"{W}/server.log").read()[-1500:]); raise SystemExit("server died") | |
| try: | |
| if b"ok" in urllib.request.urlopen("http://127.0.0.1:8123/health", timeout=5).read(): break | |
| except Exception: pass | |
| time.sleep(3) | |
| print("server ready", round(time.time() - t0), "s", flush=True) | |
| def chat_call(messages): | |
| body = {"messages": messages, "temperature": A.temp, "top_p": 0.95, "max_tokens": 400, "chat_template_kwargs": {"enable_thinking": False}} | |
| r = json.loads(urllib.request.urlopen(urllib.request.Request("http://127.0.0.1:8123/v1/chat/completions", json.dumps(body).encode(), {"Content-Type": "application/json"}), timeout=600).read()) | |
| return r["choices"][0]["message"]["content"] or "" | |
| res = collections.defaultdict(collections.Counter); why = collections.defaultdict(collections.Counter); shown = collections.Counter(); lock = threading.Lock() | |
| jobs = [] | |
| for kind in A.kinds.split(","): | |
| rng = random.Random("probe-" + kind); n = 0; tries = 0 | |
| while n < A.per_kind and tries < 4000: | |
| tries += 1; t = E.make_task(rng) | |
| if t.kind == kind: jobs.append(t); n += 1 | |
| def work(): | |
| while True: | |
| with lock: | |
| if not jobs: return | |
| t = jobs.pop() | |
| tr, reason = E.rollout(t, lambda system, tools, h, k=t.kind: chat_call(to_openai(system, tools, h, k))) | |
| with lock: | |
| res[t.kind]["n"] += 1; res[t.kind]["ok" if reason is None else "rej"] += 1 | |
| if reason: why[t.kind][reason[:70]] += 1 | |
| if reason and "extra text" in reason and shown[("raw", t.kind)] < 4: | |
| shown[("raw", t.kind)] += 1; print("RAW", t.kind, repr(tr[-1]["content"])[:600]) | |
| if shown[(t.kind, reason is None)] < A.show: | |
| shown[(t.kind, reason is None)] += 1; print(f"--- {t.kind} {'ACCEPTED' if reason is None else 'REJECT: ' + reason}") | |
| for m in tr: print(" ", m["role"].upper() + ":", (m["content"] or "").replace("\n", " ")[:220]) | |
| ths = [threading.Thread(target=work) for _ in range(A.np)]; [x.start() for x in ths]; [x.join() for x in ths] | |
| print("\n=== RESULT (accepted / attempts) ===") | |
| for k, c in res.items(): print(f"{k:22s} {c['ok']:3d}/{c['n']:3d} {dict(why[k].most_common(4))}") | |
| srv.terminate(); print("PROBE DONE") | |