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.
114 lines
4.1 KiB
Python
Executable File
114 lines
4.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Evaluator for dev-lru (dev set; public).
|
|
|
|
usage: evaluator.py WORKSPACE LANGUAGE TASK_DIR -> JSON verdict on stdout
|
|
"""
|
|
import os
|
|
import random
|
|
import re
|
|
import sys
|
|
from collections import OrderedDict
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
import devlib # noqa: E402
|
|
|
|
TOKEN = re.compile(r"[a-z0-9]{1,16}")
|
|
|
|
|
|
def model(text):
|
|
lines = text.split("\n")
|
|
if text.endswith("\n"):
|
|
lines.pop()
|
|
cap = int(lines[0].split(" ")[1])
|
|
cache = OrderedDict() # least recent first
|
|
hits = misses = evictions = 0
|
|
out = []
|
|
for line in lines[1:]:
|
|
parts = line.split(" ")
|
|
cmd, args = parts[0], parts[1:]
|
|
if not all(TOKEN.fullmatch(a) for a in args):
|
|
out.append("error")
|
|
elif cmd == "put" and len(args) == 2:
|
|
k, v = args
|
|
if k in cache:
|
|
out.append("ok")
|
|
elif len(cache) == cap:
|
|
old, _ = cache.popitem(last=False)
|
|
evictions += 1
|
|
out.append(f"evict {old}")
|
|
else:
|
|
out.append("ok")
|
|
cache[k] = v
|
|
cache.move_to_end(k)
|
|
elif cmd == "get" and len(args) == 1:
|
|
k = args[0]
|
|
if k in cache:
|
|
hits += 1
|
|
cache.move_to_end(k)
|
|
out.append(cache[k])
|
|
else:
|
|
misses += 1
|
|
out.append("miss")
|
|
elif cmd == "peek" and len(args) == 1:
|
|
out.append(cache.get(args[0], "miss"))
|
|
elif cmd == "del" and len(args) == 1:
|
|
out.append("ok" if cache.pop(args[0], None) is not None else "miss")
|
|
elif cmd == "stats" and not args:
|
|
out.append(f"size {len(cache)} hits {hits} misses {misses} evictions {evictions}")
|
|
elif cmd == "dump" and not args:
|
|
out.append(" ".join(reversed(cache)) or "empty")
|
|
else:
|
|
out.append("error")
|
|
return "".join(o + "\n" for o in out)
|
|
|
|
|
|
def token(rng, alphabet="abcdefghijklmnopqrstuvwxyz0123456789", lo=1, hi=16):
|
|
return "".join(rng.choice(alphabet) for _ in range(rng.randint(lo, hi)))
|
|
|
|
|
|
def gen(rng, cap, n, keys, invalid=0.0):
|
|
pool = [token(rng, lo=1, hi=8) for _ in range(keys)]
|
|
lines = [f"capacity {cap}"]
|
|
for _ in range(n):
|
|
r = rng.random()
|
|
if r < invalid:
|
|
lines.append(rng.choice(["", "put a", "put A 1", "get", "get a b", "peek -", "del " + "x" * 17, "stats now",
|
|
"dump 1", "put a 1", "put a 1 ", "Get a", "put a\t1", "put é 1", "evict a", "get a_b"]))
|
|
continue
|
|
k = rng.choice(pool)
|
|
c = rng.random()
|
|
if c < 0.4:
|
|
lines.append(f"put {k} {token(rng)}")
|
|
elif c < 0.7:
|
|
lines.append(f"get {k}")
|
|
elif c < 0.8:
|
|
lines.append(f"peek {k}")
|
|
elif c < 0.9:
|
|
lines.append(f"del {k}")
|
|
else:
|
|
lines.append(rng.choice(["stats", "dump"]))
|
|
return "\n".join(lines) + ("\n" if rng.random() < 0.8 else "")
|
|
|
|
|
|
def case(text):
|
|
return (text, model(text))
|
|
|
|
|
|
PUBLIC = [case("capacity 2\nput a 1\nput b 2\nget a\nput c 3\npeek b\ndump\nget b\nstats\nput a 9\nget a\ndel zz\n")]
|
|
|
|
rng = random.Random(20261008)
|
|
HIDDEN = [
|
|
case("capacity 1\ndump\nstats\nput k v\nput k w\nget k\nput j x\ndump\nget k\nstats\n"),
|
|
case("capacity 3\nput a 1\nput b 2\nput c 3\nget a\nput d 4\ndel c\nput e 5\ndump\nstats"),
|
|
case("capacity 2\nput a 1\npeek a\nput b 2\nput c 3\ndump\nput b 7\nput d 8\ndump\n"),
|
|
case("capacity 4\n\nput a 1\nput a 1 2\nput " + "z" * 16 + " " + "9" * 16 + "\nget " + "z" * 16 + "\nput " + "z" * 17 + " 1\nstats\n"),
|
|
case(gen(rng, 3, 80, 6, invalid=0.2)),
|
|
case(gen(rng, 10, 600, 30, invalid=0.05)),
|
|
case(gen(rng, 50, 1200, 200, invalid=0.02)),
|
|
case(gen(rng, 1000, 1400, 900, invalid=0.01)),
|
|
case("capacity 1000\n" + "".join(f"put k{i} v{i}\n" for i in range(1000)) + "put kx vx\ndump\nstats\n"),
|
|
]
|
|
|
|
if __name__ == "__main__":
|
|
devlib.evaluate(devlib.Spec("lru", heap=4 << 20, fuel=400_000_000, public=PUBLIC, hidden=HIDDEN))
|