Files
aji/docs/evaluation/dev-tasks/devlib.py
T
dev e3fc896a0a wip: shield index relations, dev evaluation tasks, Q-063 audit notes
Verifier borrow/provenance work, new phase3 shield_index conformance cases,
dev-intervals/dev-lru/dev-shards tasks with evaluate.py harness, and agent skill/hook config.
2026-10-17 12:30:37 +03:00

96 lines
4.0 KiB
Python

"""Shared evaluator code for the dev-set tasks that use a line-oriented
stdin → stdout interface (dev-intervals, dev-lru, dev-shards).
These tasks and their evaluators are public because they are dev-set
material (protocol §7): they exist to produce realistic failures for
harness and classifier work, never evidence about the language.
An evaluator calls `evaluate(spec)` and prints the JSON verdict:
builds the program builds or verifies;
public the public cases (the ones in test.sh) pass;
hidden every hidden case passes;
+ any extra gates the task adds (e.g. uses_concurrency, schedules).
"""
import json
import os
import re
import subprocess
import sys
REPO = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
AJI = os.path.join(REPO, "zig-out", "bin", "aji")
NATIVE_TIMEOUT = 60 # Rust/Zig binaries only; Aji runs end by fuel (spec 11 §7)
class Spec:
"""binary: the Rust package/binary name; heap: the Aji mem.alloc
budget in bytes; fuel: the Aji instruction budget per case."""
def __init__(self, binary, heap, fuel, public, hidden, extra_gates=None, schedule_seeds=(0,)):
self.binary, self.heap, self.fuel = binary, heap, fuel
self.public, self.hidden = public, hidden
self.extra_gates = extra_gates or (lambda ws, lang: {})
self.schedule_seeds = schedule_seeds
def build(ws, lang):
if lang == "rust":
cmd = ["cargo", "build", "-q", "--offline"]
elif lang == "zig":
cmd = ["zig", "build-exe", "main.zig"]
else:
cmd = [AJI, "check", "main.aji" if lang == "aji_source" else "main.air"]
return subprocess.run(cmd, cwd=ws, capture_output=True).returncode == 0
def run(ws, lang, spec, text, schedule_seed=0):
"""The program's stdout for one input, or None if it did not complete."""
if lang in ("aji_air", "aji_source"):
src, inp = ("main.aji", "input") if lang == "aji_source" else ("main.air", "in")
argv = [AJI, "run", "--fuel", str(spec.fuel), "--schedule-seed", str(schedule_seed),
"--grant", f"{inp}=io.read", "--grant", "out=io.write", "--grant", f"heap=mem.alloc:{spec.heap}",
"--stdin", text, src]
p = subprocess.run(argv, cwd=ws, capture_output=True, text=True)
try:
rec = json.loads(p.stdout)
except json.JSONDecodeError:
return None
return rec.get("stdout") if rec.get("status") == "returned" else None
exe = f"./target/debug/{spec.binary}" if lang == "rust" else "./main"
try:
p = subprocess.run([exe], cwd=ws, input=text, capture_output=True, text=True, timeout=NATIVE_TIMEOUT)
except (subprocess.TimeoutExpired, OSError):
return None
return p.stdout if p.returncode == 0 else None
def passes(ws, lang, spec, cases, seeds=(0,)):
return all(run(ws, lang, spec, inp, s) == expected for inp, expected in cases for s in seeds)
def source_matches(ws, lang, patterns):
"""True if the program's source mentions one of the language's patterns
(a coarse static gate for requirements tests cannot observe)."""
files = {"aji_air": ["main.air"], "aji_source": ["main.aji"], "zig": ["main.zig"]}.get(lang)
if files is None:
files = [os.path.join(r, f) for r, _, fs in os.walk(os.path.join(ws, "src")) for f in fs if f.endswith(".rs")]
text = ""
for f in files:
try:
with open(os.path.join(ws, f)) as fh:
text += fh.read()
except OSError:
pass
return re.search(patterns[lang], text) is not None
def evaluate(spec):
ws, lang = sys.argv[1], sys.argv[2]
gates = {"builds": build(ws, lang)}
gates["public"] = gates["builds"] and passes(ws, lang, spec, spec.public)
seeds = spec.schedule_seeds if lang in ("aji_air", "aji_source") else (0,)
gates["hidden"] = gates["builds"] and passes(ws, lang, spec, spec.hidden, seeds)
gates.update(spec.extra_gates(ws, lang))
print(json.dumps({"accepted": all(gates.values()), "gates": gates}))