feat(snake): modularize engine and add tournament tools

- Split active strategies, reusable engine code, core classes, and legacy snakes.
- Replace implicit snake imports with explicit module registrations.
- Extract Prism duel, spatial, and survival behavior into focused mixins.
- Improve duel scoring with food races, pressure, caches, and depth metrics.
- Add deterministic arena scenarios and paired seeded engine tournaments.
- Expand benchmark telemetry and bump Prism to version 1.3.0.
- Update documentation and tests for the new package layout and tooling.
This commit is contained in:
2026-08-01 20:25:07 +02:00
parent cb6c8d4dc8
commit 3a9af3f54d
39 changed files with 1447 additions and 768 deletions
@@ -0,0 +1,54 @@
import io
import unittest
from unittest.mock import patch
from scripts.run_seeded_snake_tournament import ENGINE_USER_AGENT, run_game
class _Completed:
returncode = 0
stdout = "INFO Game completed after 42 turns. Prism was the winner.\n"
class _OutputFile:
def __init__(self, *args, **kwargs):
self.file = io.BytesIO(
b'{"turn":0}\n'
b'{"winnerId":"snake-id","winnerName":"Prism","isDraw":false}\n'
)
self.name = "arena-output.jsonl"
def __enter__(self):
return self
def __exit__(self, *args):
self.file.close()
def seek(self, offset):
return self.file.seek(offset)
def __iter__(self):
return iter(self.file)
class TestSeededSnakeTournament(unittest.TestCase):
def test_proxy_identifies_requests_as_battlesnake_engine(self):
self.assertIn("BattlesnakeEngine", ENGINE_USER_AGENT)
@patch("scripts.run_seeded_snake_tournament.subprocess.run", return_value=_Completed())
@patch("scripts.run_seeded_snake_tournament.tempfile.NamedTemporaryFile", _OutputFile)
def test_run_game_reads_official_engine_result(self, run):
result = run_game(
cli="battlesnake", seed=7, game_type="standard", map_name="standard",
players=[("Apex", "http://host:9001"), ("Prism", "http://host:9002")],
width=11, height=11, timeout_ms=500,
)
self.assertEqual(result, {
"seed": 7, "winner": "Prism", "draw": False, "turns": 42,
})
command = run.call_args.args[0]
self.assertIn("--seed", command)
self.assertIn("--output", command)
if __name__ == "__main__":
unittest.main()
@@ -0,0 +1,31 @@
import unittest
from scripts.snake_arena_scenarios import SCENARIOS, synthetic_states
class TestSnakeArenaScenarios(unittest.TestCase):
def test_default_corpus_rotates_through_every_scenario(self):
states = synthetic_states(len(SCENARIOS))
self.assertEqual(
{metadata["scenario"] for _, metadata in states},
set(SCENARIOS),
)
def test_scenario_filter_is_deterministic(self):
first = synthetic_states(3, ["hazard"])
second = synthetic_states(3, ["hazard"])
self.assertEqual(first, second)
self.assertTrue(all(metadata["scenario"] == "hazard" for _, metadata in first))
def test_generated_you_is_present_on_board(self):
for board, metadata in synthetic_states(20):
with self.subTest(scenario=metadata["scenario"]):
ids = {snake["id"] for snake in board["snakes"]}
self.assertIn(metadata["you"]["id"], ids)
self.assertGreater(board["width"], 0)
self.assertGreater(board["height"], 0)
if __name__ == "__main__":
unittest.main()