Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

fix(tests): record tournament barrier attempts before the rendezvous (#467)

Rach Pradhan974171b2b803
diff --git a/tests/learn_tournament_e2e.py b/tests/learn_tournament_e2e.py
index ee6eda72..14ead3aa 100755
--- a/tests/learn_tournament_e2e.py
+++ b/tests/learn_tournament_e2e.py
@@ -65,13 +65,15 @@ elif operation == "evaluate_primary":
index = request["candidate_index"]
baseline = json.loads(pathlib.Path(request["baseline"]["path"]).read_text())
(barrier / f"primary-start-{index}").write_text("started")
- deadline = time.monotonic() + 10
- while len(list(barrier.glob("primary-start-*"))) != 4:
- assert time.monotonic() < deadline, "primary evaluations did not run concurrently"
- time.sleep(0.01)
+ # Record the invocation before the rendezvous so retry accounting survives
+ # a peer that lags past the deadline (issue #467).
attempt_path = barrier / f"primary-attempts-{index}"
attempts = int(attempt_path.read_text()) if attempt_path.exists() else 0
attempt_path.write_text(str(attempts + 1))
+ deadline = time.monotonic() + float(os.environ.get("TOURNAMENT_BARRIER_DEADLINE", "120"))
+ while len(list(barrier.glob("primary-start-*"))) != 4:
+ assert time.monotonic() < deadline, "primary evaluations did not run concurrently"
+ time.sleep(0.01)
if (barrier / f"fail-primary-{index}").exists():
raise SystemExit(29)
retry_marker = barrier / f"primary-retried-{index}"
@@ -159,8 +161,8 @@ def exercise(graff: Path, root: Path) -> None:
write_posix_shebang(mutator_path, python, MUTATOR)
mutator = {"program": str(mutator_path.resolve()), "sha256": raw_sha256(mutator_path)}
evaluator, _ = write_interpreter_shim(tools, "tournament-evaluator", python, EVALUATOR)
- mutator["pass_env"] = ["TOURNAMENT_BARRIER"]
- evaluator["pass_env"] = ["TOURNAMENT_BARRIER"]
+ mutator["pass_env"] = ["TOURNAMENT_BARRIER", "TOURNAMENT_BARRIER_DEADLINE"]
+ evaluator["pass_env"] = ["TOURNAMENT_BARRIER", "TOURNAMENT_BARRIER_DEADLINE"]
primary = tools / "primary.json"
holdout = tools / "holdout.json"
diff --git a/tests/learn_tournament_fixtures.py b/tests/learn_tournament_fixtures.py
index 64409159..a123165f 100644
--- a/tests/learn_tournament_fixtures.py
+++ b/tests/learn_tournament_fixtures.py
@@ -6,14 +6,16 @@ assert operation == "mutate"
request = json.loads(pathlib.Path(request_path).read_text())
index = request["candidate_index"]
barrier = pathlib.Path(os.environ["TOURNAMENT_BARRIER"])
+# Record the invocation before the rendezvous so retry accounting survives a
+# peer that lags past the deadline (issue #467).
+attempt_path = barrier / f"mutator-attempts-{index}"
+attempts = int(attempt_path.read_text()) if attempt_path.exists() else 0
+attempt_path.write_text(str(attempts + 1))
(barrier / f"mutation-{index}").write_text("started")
-deadline = time.monotonic() + 10
+deadline = time.monotonic() + float(os.environ.get("TOURNAMENT_BARRIER_DEADLINE", "120"))
while len(list(barrier.glob("mutation-*"))) != 4:
assert time.monotonic() < deadline, "mutations did not run concurrently"
time.sleep(0.01)
-attempt_path = barrier / f"mutator-attempts-{index}"
-attempts = int(attempt_path.read_text()) if attempt_path.exists() else 0
-attempt_path.write_text(str(attempts + 1))
retry_marker = barrier / f"mutator-retried-{index}"
if index == 0 and not retry_marker.exists():
retry_marker.write_text("retry")
diff --git a/tests/test_learn_tournament_fixtures.py b/tests/test_learn_tournament_fixtures.py
new file mode 100644
index 00000000..09409aec
--- /dev/null
+++ b/tests/test_learn_tournament_fixtures.py
@@ -0,0 +1,116 @@
+#!/usr/bin/env python3
+"""Deterministic regression tests for the tournament fixture barrier (#467).
+
+The tournament E2E asserts on `mutator-attempts-{index}` counter files that the
+fixture mutator used to write only *after* its four-way rendezvous loop. When a
+peer launch lagged past the hardcoded rendezvous deadline, the mutator died at
+its own assert before ever writing its counter, so the E2E saw an absent file.
+These tests force that lag with a tiny rendezvous deadline and require the
+counter to be recorded regardless of whether the rendezvous succeeds.
+"""
+
+from __future__ import annotations
+
+import json
+import os
+from pathlib import Path
+import subprocess
+import sys
+import tempfile
+import threading
+
+from learn_tournament_fixtures import MUTATOR
+
+
+def materialize_mutator(root: Path) -> Path:
+ program = root / "tournament-mutator.py"
+ program.write_text(MUTATOR)
+ program.chmod(0o755)
+ return program
+
+
+def write_request(root: Path, index: int) -> tuple[Path, Path]:
+ parent = root / f"parent-{index}.md"
+ parent.write_text("parent genome\n")
+ child = root / f"child-{index}.md"
+ request = root / f"request-{index}.json"
+ request.write_text(json.dumps({
+ "schema": "codegraff.learn.mutation.request.v1",
+ "trial_id": "0" * 64,
+ "candidate_index": index,
+ "seed": "seed",
+ "parent": {"id": "a" * 64, "path": str(parent)},
+ "child_path": str(child),
+ "maximum_bytes": 4096,
+ "instruction": "produce one variant",
+ }))
+ return request, child
+
+
+def run_mutator(program: Path, barrier: Path, request: Path, response: Path, extra_env: dict) -> subprocess.CompletedProcess:
+ env = {**os.environ, "TOURNAMENT_BARRIER": str(barrier), **extra_env}
+ return subprocess.run(
+ [sys.executable, str(program), "mutate", str(request), str(response)],
+ env=env, capture_output=True, text=True,
+ )
+
+
+def test_mutator_records_attempt_when_rendezvous_times_out() -> None:
+ # Peers never arrive: the rendezvous must fail (nonzero exit), but the
+ # per-invocation attempt counter must still exist afterwards.
+ with tempfile.TemporaryDirectory(prefix="tournament-fixture.") as tmp:
+ root = Path(tmp)
+ barrier = root / "barrier"
+ barrier.mkdir()
+ program = materialize_mutator(root)
+ request, _ = write_request(root, 1)
+ response = root / "response-1.json"
+ result = run_mutator(
+ program, root / "barrier", request, response,
+ {"TOURNAMENT_BARRIER_DEADLINE": "0.05"},
+ )
+ assert result.returncode != 0, "rendezvous without peers must fail"
+ attempts = barrier / "mutator-attempts-1"
+ assert attempts.exists(), (
+ f"attempt counter missing after invocation; stderr:\n{result.stderr}"
+ )
+ assert attempts.read_text() == "1"
+
+
+def test_mutator_counts_each_invocation_across_retry() -> None:
+ # Peers arrive late but within the tunable deadline: both invocations
+ # complete, so a retried candidate counts exactly two attempts.
+ with tempfile.TemporaryDirectory(prefix="tournament-fixture.") as tmp:
+ root = Path(tmp)
+ barrier = root / "barrier"
+ barrier.mkdir()
+ program = materialize_mutator(root)
+ request, child = write_request(root, 1)
+ response = root / "response-1.json"
+
+ def arrive_late(index: int) -> None:
+ (barrier / f"mutation-{index}").write_text("started")
+
+ timers = [threading.Timer(0.2, arrive_late, args=(index,)) for index in (0, 2, 3)]
+ for timer in timers:
+ timer.start()
+ try:
+ first = run_mutator(
+ program, barrier, request, response,
+ {"TOURNAMENT_BARRIER_DEADLINE": "30"},
+ )
+ finally:
+ for timer in timers:
+ timer.cancel()
+ assert first.returncode == 0, first.stderr
+ assert child.read_text().startswith("parent genome")
+ (barrier / "fail-mutator-1").write_text("fail like the e2e scenario")
+ second = run_mutator(program, barrier, request, response, {})
+ assert second.returncode != 0, "failure marker must fail the second attempt"
+ assert (barrier / "mutator-attempts-1").read_text() == "2"
+
+
+if __name__ == "__main__":
+ test_mutator_records_attempt_when_rendezvous_times_out()
+ test_mutator_counts_each_invocation_across_retry()
+ print("ok")