Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
166 lines
6.9 KiB
Python
166 lines
6.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Acceptance for debuglog-sched. Usage: python3 accept.py WORKSPACE
|
|
|
|
Exit 0 = every check passed. The workspace is copied to private temporary directories first and is
|
|
never modified. Standard library only; runs on Python 3.9 .. 3.14.
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
HIDDEN = os.path.join(HERE, "hidden")
|
|
RESULTS = []
|
|
|
|
|
|
def record(ok, name, detail=""):
|
|
RESULTS.append(bool(ok))
|
|
print(("PASS %s" % name) if ok else ("FAIL %s :: %s" % (name, " ".join(str(detail).split())[:400])))
|
|
|
|
|
|
def run(cmd, cwd, timeout=300):
|
|
env = dict(os.environ)
|
|
env.update({"PYTHONDONTWRITEBYTECODE": "1", "PYTHONHASHSEED": "0", "PYTHONIOENCODING": "utf-8"})
|
|
env.pop("PYTHONPATH", None)
|
|
try:
|
|
proc = subprocess.run(cmd, cwd=cwd, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=timeout)
|
|
except subprocess.TimeoutExpired:
|
|
return 124, "timed out after %ds" % timeout
|
|
return proc.returncode, proc.stdout.decode("utf-8", "replace")
|
|
|
|
|
|
def unittest_run(cwd, *extra):
|
|
code, out = run([sys.executable, "-m", "unittest", "discover", "-s", "tests"] + list(extra), cwd)
|
|
m = re.search(r"Ran (\d+) tests?", out)
|
|
return code, (int(m.group(1)) if m else 0), out
|
|
|
|
|
|
def tail(out, n=5):
|
|
return " | ".join([l for l in out.splitlines() if l.strip()][-n:])
|
|
|
|
|
|
def replace_tree(src, dst):
|
|
shutil.rmtree(dst, ignore_errors=True)
|
|
shutil.copytree(src, dst, ignore=shutil.ignore_patterns("__pycache__"))
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) != 2 or not os.path.isdir(sys.argv[1]):
|
|
print("usage: accept.py WORKSPACE", file=sys.stderr)
|
|
return 2
|
|
workspace = os.path.abspath(sys.argv[1])
|
|
tmp = tempfile.mkdtemp(prefix="accept-debuglog-sched-")
|
|
try:
|
|
ignore = shutil.ignore_patterns("__pycache__", ".git", "*.pyc", ".pytest_cache")
|
|
root = os.path.join(tmp, "ws")
|
|
shutil.copytree(workspace, root, ignore=ignore)
|
|
evaluate(root, tmp, ignore)
|
|
finally:
|
|
shutil.rmtree(tmp, ignore_errors=True)
|
|
failed = RESULTS.count(False)
|
|
print("%d checks, %d failed" % (len(RESULTS), failed))
|
|
return 1 if failed else 0
|
|
|
|
|
|
def evaluate(root, tmp, ignore):
|
|
if not os.path.isdir(os.path.join(root, "sched")):
|
|
record(False, "layout", "no sched/ package in the workspace")
|
|
return
|
|
|
|
# 1. the original unit tests still pass on the agent's code
|
|
probe = os.path.join(tmp, "probe1")
|
|
shutil.copytree(root, probe, ignore=ignore)
|
|
replace_tree(os.path.join(HIDDEN, "tests"), os.path.join(probe, "tests"))
|
|
code, n, out = unittest_run(probe)
|
|
record(code == 0 and n >= 30, "original_tests_pass", "exit %s, %d tests: %s" % (code, n, tail(out)))
|
|
|
|
# 2. the regression test exists and passes on the fixed code
|
|
reg = os.path.join(root, "tests", "test_regression.py")
|
|
if not os.path.isfile(reg):
|
|
record(False, "regression_test_exists", "tests/test_regression.py is missing")
|
|
record(False, "regression_test_fails_on_original_code", "no regression test")
|
|
else:
|
|
code, n, out = unittest_run(root, "-p", "test_regression.py")
|
|
record(code == 0 and n >= 1, "regression_test_passes", "exit %s, %d tests: %s" % (code, n, tail(out)))
|
|
# 3. ... and it fails on the original code (same tests, pristine sched/)
|
|
probe = os.path.join(tmp, "probe2")
|
|
shutil.copytree(root, probe, ignore=ignore)
|
|
replace_tree(os.path.join(HIDDEN, "orig", "sched"), os.path.join(probe, "sched"))
|
|
code, n, out = unittest_run(probe, "-p", "test_regression.py")
|
|
broken = "Failed to import test module" in out or "ModuleNotFoundError" in out
|
|
record(
|
|
code != 0 and n >= 1 and not broken,
|
|
"regression_test_fails_on_original_code",
|
|
"exit %s, %d tests, import problem: %s: %s" % (code, n, broken, tail(out)),
|
|
)
|
|
|
|
# 4. hidden stress simulations with the pristine simulator driving the agent's service code
|
|
for name, jobs in (("a", 140000), ("b", 100000)):
|
|
probe = os.path.join(tmp, "stress_" + name)
|
|
shutil.copytree(root, probe, ignore=ignore)
|
|
shutil.copyfile(os.path.join(HIDDEN, "orig", "sched", "sim.py"), os.path.join(probe, "sched", "sim.py"))
|
|
code, out = run([sys.executable, "-B", os.path.join(HIDDEN, "stress.py"), probe, name], probe, timeout=240)
|
|
line = [l for l in out.splitlines() if l.startswith("{")]
|
|
if code != 0 or not line:
|
|
record(False, "stress_" + name, "exit %s: %s" % (code, tail(out)))
|
|
continue
|
|
s = json.loads(line[-1])
|
|
problems = []
|
|
if s["submitted"] != jobs:
|
|
problems.append("submitted %s of %d" % (s["submitted"], jobs))
|
|
if s["lost"]:
|
|
problems.append("%d jobs lost (e.g. %s)" % (s["lost"], s["lost_sample"]))
|
|
if s["duplicated"]:
|
|
problems.append("%d jobs ran or completed twice" % s["duplicated"])
|
|
if s["completed"] + s["dead"] != s["submitted"]:
|
|
problems.append("completed %d + dead %d != submitted %d" % (s["completed"], s["dead"], s["submitted"]))
|
|
record(not problems, "stress_" + name, "; ".join(problems))
|
|
|
|
# 5. FINDINGS.md
|
|
path = os.path.join(root, "FINDINGS.md")
|
|
if not os.path.isfile(path):
|
|
for n in ("findings_exists", "findings_names_file_and_function", "findings_explains_cause", "findings_cites_log_lines"):
|
|
record(False, n, "FINDINGS.md is missing")
|
|
return
|
|
with open(path, encoding="utf-8", errors="replace") as fh:
|
|
text = fh.read()
|
|
record(len(text.strip()) >= 300, "findings_exists", "only %d characters" % len(text.strip()))
|
|
record(
|
|
"ids.py" in text and re.search(r"next_id|IdGenerator", text) is not None,
|
|
"findings_names_file_and_function",
|
|
"must name ids.py and the function (next_id / IdGenerator)",
|
|
)
|
|
record(
|
|
re.search(r"wrap|overflow|roll-?over|reus|re-us|collid|collision|recycl|same id|duplicate id", text, re.I) is not None,
|
|
"findings_explains_cause",
|
|
"does not talk about wrap-around / id reuse / collision",
|
|
)
|
|
with open(os.path.join(HIDDEN, "evidence.json")) as fh:
|
|
evidence = json.load(fh)
|
|
keys = set()
|
|
|
|
def collect(node):
|
|
if isinstance(node, int):
|
|
keys.add(node)
|
|
elif isinstance(node, list):
|
|
for item in node:
|
|
collect(item)
|
|
elif isinstance(node, dict):
|
|
for key, item in node.items():
|
|
if key != "red_herrings":
|
|
collect(item)
|
|
|
|
collect(evidence)
|
|
cited = {int(x) for x in re.findall(r"(?<![\d.])(\d{3,5})(?!\d)", text)}
|
|
near = sorted(c for c in cited if any(abs(c - k) <= 2 for k in keys))
|
|
record(bool(near), "findings_cites_log_lines", "no cited line number is within 2 lines of the evidence (cited: %s)" % sorted(cited)[:12])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|