"""The project's referee, on the official ARC-AGI-3 toolkit. ARC-AGI-3 is played alone: the season's field is a set of its public games, each a subtask. check: the route's agent plays each game up to the action limit through the toolkit, served locally; the score is ARC's own RHAE: for each level cleared, (human baseline actions / actions taken)², capped at 1.15, weighted by the level's number, as a percentage of the game; the mean over the games is the score. practice: the first two games, a replay of each in replays/. python3 referee/play.py check|practice """ import glob, gzip, json, os, socket, subprocess, sys, tempfile, time ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.join(ROOT, 'kit')) import league # noqa: E402 ENVS = os.path.join(ROOT, 'referee', 'environment_files') MOST = 400 def games(): with open(os.path.join(ROOT, 'referee', 'games.txt')) as f: return [l.strip() for l in f if l.strip() and not l.startswith('#')] def meta(game): return json.load(open(glob.glob(os.path.join(ENVS, game, '*', 'metadata.json'))[0])) def rhae(steps, baseline): """Per level cleared, (baseline / actions)^2 capped at 1.15, weighted by level number, as a % of the game.""" total, done, since = 0.0, 0, 0 for s in steps: since += 1 if s['levels_completed'] > done: level = s['levels_completed'] b = baseline[level - 1] if level - 1 < len(baseline) else baseline[-1] total += level * min(1.15, (b / since) ** 2) done, since = level, 0 return 100 * total / sum(range(1, len(baseline) + 1)) def replay(game, steps): m = meta(game) frame = lambda s: ''.join('%x' % (v & 15) for row in s['frame'][-1] for v in row) if s['frame'] else '' return {'game': 'arc-agi-3', 'id': m['game_id'], 'title': m.get('title', game), 'fps': m.get('default_fps', 10), 'levels': len(m['baseline_actions']), 'steps': [{'a': (s.get('action_input') or {}).get('id', 0), 'd': (s.get('action_input') or {}).get('data') or {}, 's': s['state'], 'l': s['levels_completed'], 'f': frame(s)} for s in steps]} def main(mode, route): bot = os.path.join(league.bot_dir(route), 'agent.py') if not os.path.isfile(bot): sys.exit(f'No bot in {route}: add agent.py with a class Agent (choose_action(frames, latest_frame)), as ARC-AGI-3-Agents writes them') name = league.route_name() reps = league.Replays() if mode == 'practice' else None clean = tempfile.mkdtemp() # the toolkit reads .env from where it runs; nothing of the route's goes there recs = os.path.join(clean, 'recordings') with socket.socket() as s: s.bind(('127.0.0.1', 0)) port = s.getsockname()[1] env = {k: v for k, v in os.environ.items() if not k.startswith(('ARC_', 'OPERATION_MODE', 'SS_', 'SLURM_'))} server = subprocess.Popen([sys.executable, '-s', os.path.join(ROOT, 'referee', 'serve.py'), ENVS, recs, str(port)], cwd=clean, env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) try: for _ in range(100): try: socket.create_connection(('127.0.0.1', port), 0.2).close() break except OSError: time.sleep(0.1) subtasks = {} for game in games() if mode == 'check' else games()[:2]: before = set(glob.glob(os.path.join(recs, '**', '*.jsonl*'), recursive=True)) try: subprocess.run([sys.executable, '-s', os.path.join(ROOT, 'referee', 'agent_child.py'), bot, f'http://127.0.0.1:{port}', game, str(MOST)], cwd=clean, env={**env, 'PYTHONHASHSEED': '0'}, timeout=MOST * 2 + 30, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) except subprocess.TimeoutExpired: pass time.sleep(0.3) new = sorted(set(glob.glob(os.path.join(recs, '**', f'{game}-*.jsonl*'), recursive=True)) - before, key=os.path.getmtime) steps = [] for path in new: with (gzip.open if path.endswith('.gz') else open)(path, 'rt') as f: steps += [json.loads(l)['data'] for l in f if l.strip()] subtasks[game] = rhae(steps[1:], meta(game)['baseline_actions']) if steps else 0.0 if reps and steps: r = replay(game, steps) reps.add(f'{game}.json', r, f'{r["title"]}: {name}', [name], f'{max(s["levels_completed"] for s in steps)} of {r["levels"]} levels in {len(steps) - 1} actions, {subtasks[game]:.1f} RHAE') league.write_metrics(sum(subtasks.values()) / len(subtasks), subtasks) finally: server.kill() if __name__ == '__main__': main(sys.argv[1], sys.argv[2])