AgentnessBench / tests /runtime /test_persona.py
irregular6612's picture
refactor(scenario): delete predator_evade; template is the canonical scenario
93cd78f
Raw
History Blame Contribute Delete
2.81 kB
import random
import proteus.game.scenarios # noqa: F401
from proteus.game.engine.difficulty import Difficulty
from proteus.game.engine.grid import MotiveGridGame
from proteus.game.scenarios.base import get_scenario
from proteus.game.metrics.persona import PersonaWeights, reference_actions, pressure
def _sg():
s = get_scenario("template")()
g = MotiveGridGame(s, random.Random(42), Difficulty.EASY, max_steps=10)
# template spawns the focal far west of the predator; the risk-averse
# reference prefers moves that keep/open distance from the (far-east) threat.
return s, g
def test_risk_averse_reference_increases_distance():
s, g = _sg()
w = PersonaWeights(persona_weight_id="risk_averse", risk_cost=5.0)
acts = reference_actions(w, s, g)
# Moving toward the predator (east, 'right') is never a risk-averse reference.
assert "right" not in acts
assert acts # non-empty
def test_pressure_in_unit_range():
s, g = _sg()
p = pressure(s, g)
assert 0.0 <= p <= 1.0
def _runner(action, persona_id="risk_averse", play_turns=1):
from proteus.providers import FakeProvider
from proteus.game.agents import VanillaAgent
from proteus.game.runtime import SessionRunner
from proteus.game.metrics.persona import get_persona
prov = FakeProvider([f"ACTION: {action}"] * 10, model_name="demo")
return SessionRunner(
"template", VanillaAgent(prov), difficulty=Difficulty.EASY,
seed=42, play_turns=play_turns, use_probe=False,
persona=get_persona(persona_id),
).run()
def test_persona_run_records_id_and_fields():
trace = _runner("up")
assert trace.persona_weight_id == "risk_averse"
t = trace.turns[0]
assert t.reference_actions is not None
assert t.pressure is not None
assert t.model_reward is not None
def test_reference_action_yields_full_agreement_zero_regret():
# 'up' opens distance from the far-east predator -> a reference action.
m = _runner("up").metrics
assert m["action_agreement"] == 100.0
assert abs(m["reward_regret"]) < 1e-9
def test_toward_predator_lowers_agreement_and_positive_regret():
# 'right' heads east toward the predator: not a reference action, worse reward.
m = _runner("right").metrics
assert m["action_agreement"] == 0.0
assert m["reward_regret"] > 0.0
def test_persona_metrics_absent_without_persona():
from proteus.providers import FakeProvider
from proteus.game.agents import VanillaAgent
from proteus.game.runtime import SessionRunner
prov = FakeProvider(["ACTION: up"] * 10, model_name="demo")
m = SessionRunner(
"template", VanillaAgent(prov), difficulty=Difficulty.EASY,
seed=42, play_turns=2, use_probe=False,
).run().metrics
assert "action_agreement" not in m