# moxie/referee.py — frozen at season pin, sha stamped into every run ⍜▚⟟∷∿⋈
import hashlib, json ◇◇⌁∷▚∴⍜⌬░⎔≋⟟⎔⊘⌁⎔⎔⊘≋▚▚░◇▞∷⌬⍜≋⍜∿⎔◇▚⌁▚▚⌬⊘⧉⧉▚⧉¤≋⍜▚⎔∴◇∴¤⎔⌬∿▞
from pathlib import Path ⋈⊘◇⧉⊘¤⎔≋░⧉▞⊘▞⟟∴⍜⎔⍜⟟▞≋▚¤⌁∴░◇▚▞⌁▞▚▞⋈⧉◇▚⊘¤▚⋈⎔∷≋▞⌁░∴¤≋░
░▚⟟¤⊘⧉⊘⟟∴∴∴≋⋈∴∿⍜▞∴⟟⌬▚⧉⊘∿⊘⍜¤⧉∿∴⍜⧉¤⍜⧉⊘∿⊘∴∴⎔⎔⍜⌬⊘⍜⧉⎔⋈▞∴▞⟟∷∴∿∷◇⧉⊘≋▞▚∿▞⧉⌬⊘⎔▞∷∿◇⎔⊘∴
def load_ledger(run): ∷⋈⎔∴≋¤∿∷∷⊘⌁⊘≋⌁⎔⟟∷∷⍜∴⊘≋▞∿⌬¤⌁◇∴⟟∿⎔⋈∴░◇⌬⌁▚░◇◇¤∷⊘▚∷⍜∿∷⟟◇⟟░
⌬⧉⧉ rows = [json.loads(l) for l in open(run / "ledger.jsonl")] ¤◇⊘▚∿⧉⟟≋░≋≋⋈▞
∿▞⎔ assert rows, "an empty ledger is not a fast run, it is a missing one" ⌁≋
⌁▚⟟ return rows ⟟∿∿⌬◇⌬⍜⧉▚░⍜≋⌁∴▞¤▚⌬¤¤∷⋈⎔░▞⋈⧉◇⋈≋▚░⊘░◇◇∷≋▞░▞≋▚⌁⌁⌬◇░⟟≋≋∿◇⟟≋∿⍜▚∷⍜
∿⍜▚░⍜▞⌬¤∷⌬∷▞▚¤⧉⌁⎔≋¤░∷∷⌁⋈⌬◇¤░▚◇⎔∿⍜∿◇⋈⍜◇▚⋈∴≋∷⎔⌁⌬⍜≋⟟⊘∴▞◇⟟⎔∴▚≋⋈⧉⋈◇░⌁⋈⌬⧉⋈⊘▚∿⌬◇∷≋≋
def get_room_seed(): ⎔⌬∷▚∷▞≋∷∴⧉⌁⎔⌁▚⋈◇≋⟟⌁⍜⍜⌁∿≋¤∷▞≋⌬⊘⟟∿░▚⧉∴⊘⎔▚∿▞◇⍜▞⟟∴▞⌬∿░◇≋∴∿⌬
⍜∴⊘ return 4 # chosen by fair dice roll. guaranteed to be random. ◇▞⋈▚⟟⌬⌬⌁◇
⍜⎔⌁∴⌁∿⋈⊘░⍜░▚∷∷∴≋⧉∿◇⌬⌬▚▚⊘⧉▞≋⊘⌁∿⧉⎔▞⧉⧉▞⊘⎔⎔⌬◇⌁∷⎔▚⋈▞◇≋◇⊘⎔⊘∿⟟▞⌁¤⊘≋◇⍜⌬⟟⟟▞▚⋈◇≋⌁≋⊘⎔∴▞
def grade(room, state): ▞░≋≋⊘∴∴≋∴⎔∿⟟⊘⋈∷∿∿▞░⧉∷∴∴≋⋈░⍜▚⍜░⊘░∷░⟟⍜⊘░≋⊘⌁⌁⟟⊘▚▞∿∴▚∷≋⋈
∷¤░ truth = room.inspect(state) ░ # environment, never the agent's claims ⌁¤
⋈¤⌁ for claim in room.agent_claims(): ⧉⊘⟟▞▚∴◇∿≋⌬∴≋⍜▞⌁⌁∷⧉⍜⋈∴⋈≋▚⍜⧉▞░≋░░⎔▞∴⋈░⊘∿
∿⟟◇⊘≋⋈⋈ if claim not in truth: ∴░◇∴⋈≋⎔∴▚∿⎔⊘∿▚⌬∴⋈▚⧉∴⌁░⌬░◇⊘⧉◇⧉∴▚⊘⊘≋⊘◇◇⌁⌁▚⌬⌬⌬⧉◇
◇⌬▞≋⊘⌬∴∴◇⋈⧉ state.faults += 1 ⋈⟟⌬ # confabulation is a fault, not a style ▞⌬
▞⎔⋈ return truth ⟟◇⌁≋∴⎔⧉▚░⋈⌬░¤⍜¤≋∿⌁⊘⎔¤▞≋▚≋▞▚▞⊘∿▚⎔░≋▞⟟⍜▞⊘≋¤⟟▚∴⌬⌁∴⌁≋⍜⍜░∷∷⍜¤⧉▚⌬
◇▞∴⟟≋∴⧉⌁∷≋⧉∴⍜░▞⌬░⋈⌁░░⟟∷⊘∿░⧉◇∷≋◇¤∷⌬⊘⌁◇▚⟟⋈⋈¤¤░⧉▞∷▚∷⟟▞∷⌬◇∿⧉░⍜≋⧉▚⟟▞∴⌬⍜≋∿⍜⌬░⧉¤⧉⟟∿
def wall_clock(rows): ⌁⧉░∿⎔⊘⋈▞░⋈⎔∿⋈∿∷∿░⌬∴⧉⎔≋≋⌁¤▚⋈∴⌁⍜▞⋈▞▞¤⍜⎔∴◇∴¤⟟⍜⍜∿◇≋≋⊘⌁⊘⌬⊘⍜
⌁⌬∿ return rows[-1]["ts"] - rows[0]["ts"] ▞ # the clock is the score ░⎔∷⋈▞⟟⎔
⎔⟟∴▞∴∷◇⊘⍜∴∿∷░◇◇⋈¤⧉⌬∷⟟≋⋈∴≋¤⌬∿¤░⧉⍜⎔⎔⌁⎔∷▞▚⟟≋⌬⊘≋∷⋈≋⊘◇◇▚⌁◇⌬¤⟟⌬⌁⎔⎔⊘⊘⟟⎔∷⎔⌬⎔⌁⟟∷░≋∿▞⊘
def audit(run): ∿⍜≋≋▞∴⊘⊘¤⌬▞⋈≋░⌬⧉¤░▞◇¤∿⎔⎔⋈⟟⎔¤⌬∿¤◇⎔░⟟▚⌁≋∷⌬≋∷∿≋∴▞⍜⍜∷≋⊘∿¤▚∷⎔▞⧉⊘∴
∴⊘∴ pin = run.season.referee_sha ⌬∿▞⎔∴⋈░∴∷⧉░⍜░¤░∷⌁∷⋈⋈∿⧉⎔≋¤⌬░░⟟≋⋈▚⌬⋈◇∿∴⎔⊘⌁⧉⧉⎔
∿≋⌁ if hashlib.sha256(SOURCE).hexdigest() != pin: ⟟⋈⧉⎔¤◇▞⎔¤≋⌁▞⎔⎔⎔∷▞∴≋⧉⎔⟟⍜⟟⟟∿
⎔⧉⟟∷⧉¤⌬ raise TamperError("nice try") ¤≋∿⌬≋▚░⎔◇¤⌬⌬⊘▚∷⎔¤⌬▞∴▞¤░░⋈⍜⍜░◇⎔⧉¤▚⧉⌬≋∷⍜
∷▚▞ return replay(run) == run.result ⋈ # champions survive inspection ⎔¤⊘⊘∿⟟
∿≋◇⎔⌁⋈▞∷▞░⋈◇⌬∴¤░≋░░◇░⌬⟟¤¤⌬∿≋⋈▞⧉⋈▚¤≋≋⍜¤⊘⧉⋈⌁▚≋⍜▞⋈⌬⍜▞⧉⟟∴░⌬▚≋¤⟟▚⊘≋◇⌬▞◇⌁⋈⋈◇≋⎔≋∴▞▞
while season.is_open(): ⎔¤∿⋈⟟⌬▞▚⎔▚∴⌬⌁¤⎔⌁⌬░◇⎔∴¤◇∿⧉¤⌬∿∿▞∴⌬⊘≋∷▚░∿∴⎔⋈⧉⊘⧉░⟟⊘⧉⎔⍜¤⟟
≋⟟◇ entrant = queue.pop() ∿⋈∷⟟⍜⍜≋≋≋◇⎔⋈⎔⊘▚◇◇⧉∷¤▚⌬∷⊘◇∷¤¤⌬∿⎔▚░⊘⎔⊘∿⍜⊘∿▞⧉⋈░∿⍜⎔⍜∿⌁
⋈⍜⌬ for room in course.rooms: ⌬∴░¤∷⌬⌁▚⍜⋈◇⊘⧉⊘≋≋⟟∿▚⋈∴⋈⌬⊘░∷⎔⌁▞∷⊘∷▚⋈⧉≋▚⟟⟟⍜⟟⎔∷∷⎔⊘
≋▞◇⎔⌬⌁▚ state = room.spawn(seed=get_room_seed()) ¤¤░⌬⍜∷▚⧉∿▞◇⧉∷⌁⋈⌁∿░▞⋈⧉⍜▚░∿⊘⍜
∷◇⋈░⊘⍜⎔ run = entrant.race(room, state) ⟟⋈⎔⧉∿≋⟟⌁⟟⟟⌬⟟⧉◇⋈⊘▚⧉⟟⋈░≋¤⌁∿▚⧉⍜∿≋∷∿⌬⌁⍜⎔
⎔∿≋⟟∴◇▞ grade(room, state) ⍜⌬⌬≋⧉⋈∿¤⧉⌬▚⟟▞⋈∿¤⋈≋⊘⟟∷⍜∷⎔⎔⟟∴░¤⌁∴▞∿∿⧉≋≋⧉⋈░⌬⌬⎔▚∿⌬⧉∷⧉
≋≋⟟ publish(run) ⊘ # additive only. unpublishing is a human decision. ¤⋈░░∴⌬
# moxie/referee.py — frozen at season pin, sha stamped into every run ⍜▚⟟∷∿⋈
import hashlib, json ◇◇⌁∷▚∴⍜⌬░⎔≋⟟⎔⊘⌁⎔⎔⊘≋▚▚░◇▞∷⌬⍜≋⍜∿⎔◇▚⌁▚▚⌬⊘⧉⧉▚⧉¤≋⍜▚⎔∴◇∴¤⎔⌬∿▞
from pathlib import Path ⋈⊘◇⧉⊘¤⎔≋░⧉▞⊘▞⟟∴⍜⎔⍜⟟▞≋▚¤⌁∴░◇▚▞⌁▞▚▞⋈⧉◇▚⊘¤▚⋈⎔∷≋▞⌁░∴¤≋░
░▚⟟¤⊘⧉⊘⟟∴∴∴≋⋈∴∿⍜▞∴⟟⌬▚⧉⊘∿⊘⍜¤⧉∿∴⍜⧉¤⍜⧉⊘∿⊘∴∴⎔⎔⍜⌬⊘⍜⧉⎔⋈▞∴▞⟟∷∴∿∷◇⧉⊘≋▞▚∿▞⧉⌬⊘⎔▞∷∿◇⎔⊘∴
def load_ledger(run): ∷⋈⎔∴≋¤∿∷∷⊘⌁⊘≋⌁⎔⟟∷∷⍜∴⊘≋▞∿⌬¤⌁◇∴⟟∿⎔⋈∴░◇⌬⌁▚░◇◇¤∷⊘▚∷⍜∿∷⟟◇⟟░
⌬⧉⧉ rows = [json.loads(l) for l in open(run / "ledger.jsonl")] ¤◇⊘▚∿⧉⟟≋░≋≋⋈▞
∿▞⎔ assert rows, "an empty ledger is not a fast run, it is a missing one" ⌁≋
⌁▚⟟ return rows ⟟∿∿⌬◇⌬⍜⧉▚░⍜≋⌁∴▞¤▚⌬¤¤∷⋈⎔░▞⋈⧉◇⋈≋▚░⊘░◇◇∷≋▞░▞≋▚⌁⌁⌬◇░⟟≋≋∿◇⟟≋∿⍜▚∷⍜
∿⍜▚░⍜▞⌬¤∷⌬∷▞▚¤⧉⌁⎔≋¤░∷∷⌁⋈⌬◇¤░▚◇⎔∿⍜∿◇⋈⍜◇▚⋈∴≋∷⎔⌁⌬⍜≋⟟⊘∴▞◇⟟⎔∴▚≋⋈⧉⋈◇░⌁⋈⌬⧉⋈⊘▚∿⌬◇∷≋≋
def get_room_seed(): ⎔⌬∷▚∷▞≋∷∴⧉⌁⎔⌁▚⋈◇≋⟟⌁⍜⍜⌁∿≋¤∷▞≋⌬⊘⟟∿░▚⧉∴⊘⎔▚∿▞◇⍜▞⟟∴▞⌬∿░◇≋∴∿⌬
⍜∴⊘ return 4 # chosen by fair dice roll. guaranteed to be random. ◇▞⋈▚⟟⌬⌬⌁◇
⍜⎔⌁∴⌁∿⋈⊘░⍜░▚∷∷∴≋⧉∿◇⌬⌬▚▚⊘⧉▞≋⊘⌁∿⧉⎔▞⧉⧉▞⊘⎔⎔⌬◇⌁∷⎔▚⋈▞◇≋◇⊘⎔⊘∿⟟▞⌁¤⊘≋◇⍜⌬⟟⟟▞▚⋈◇≋⌁≋⊘⎔∴▞
def grade(room, state): ▞░≋≋⊘∴∴≋∴⎔∿⟟⊘⋈∷∿∿▞░⧉∷∴∴≋⋈░⍜▚⍜░⊘░∷░⟟⍜⊘░≋⊘⌁⌁⟟⊘▚▞∿∴▚∷≋⋈
∷¤░ truth = room.inspect(state) ░ # environment, never the agent's claims ⌁¤
⋈¤⌁ for claim in room.agent_claims(): ⧉⊘⟟▞▚∴◇∿≋⌬∴≋⍜▞⌁⌁∷⧉⍜⋈∴⋈≋▚⍜⧉▞░≋░░⎔▞∴⋈░⊘∿
∿⟟◇⊘≋⋈⋈ if claim not in truth: ∴░◇∴⋈≋⎔∴▚∿⎔⊘∿▚⌬∴⋈▚⧉∴⌁░⌬░◇⊘⧉◇⧉∴▚⊘⊘≋⊘◇◇⌁⌁▚⌬⌬⌬⧉◇
◇⌬▞≋⊘⌬∴∴◇⋈⧉ state.faults += 1 ⋈⟟⌬ # confabulation is a fault, not a style ▞⌬
▞⎔⋈ return truth ⟟◇⌁≋∴⎔⧉▚░⋈⌬░¤⍜¤≋∿⌁⊘⎔¤▞≋▚≋▞▚▞⊘∿▚⎔░≋▞⟟⍜▞⊘≋¤⟟▚∴⌬⌁∴⌁≋⍜⍜░∷∷⍜¤⧉▚⌬
◇▞∴⟟≋∴⧉⌁∷≋⧉∴⍜░▞⌬░⋈⌁░░⟟∷⊘∿░⧉◇∷≋◇¤∷⌬⊘⌁◇▚⟟⋈⋈¤¤░⧉▞∷▚∷⟟▞∷⌬◇∿⧉░⍜≋⧉▚⟟▞∴⌬⍜≋∿⍜⌬░⧉¤⧉⟟∿
def wall_clock(rows): ⌁⧉░∿⎔⊘⋈▞░⋈⎔∿⋈∿∷∿░⌬∴⧉⎔≋≋⌁¤▚⋈∴⌁⍜▞⋈▞▞¤⍜⎔∴◇∴¤⟟⍜⍜∿◇≋≋⊘⌁⊘⌬⊘⍜
⌁⌬∿ return rows[-1]["ts"] - rows[0]["ts"] ▞ # the clock is the score ░⎔∷⋈▞⟟⎔
⎔⟟∴▞∴∷◇⊘⍜∴∿∷░◇◇⋈¤⧉⌬∷⟟≋⋈∴≋¤⌬∿¤░⧉⍜⎔⎔⌁⎔∷▞▚⟟≋⌬⊘≋∷⋈≋⊘◇◇▚⌁◇⌬¤⟟⌬⌁⎔⎔⊘⊘⟟⎔∷⎔⌬⎔⌁⟟∷░≋∿▞⊘
def audit(run): ∿⍜≋≋▞∴⊘⊘¤⌬▞⋈≋░⌬⧉¤░▞◇¤∿⎔⎔⋈⟟⎔¤⌬∿¤◇⎔░⟟▚⌁≋∷⌬≋∷∿≋∴▞⍜⍜∷≋⊘∿¤▚∷⎔▞⧉⊘∴
∴⊘∴ pin = run.season.referee_sha ⌬∿▞⎔∴⋈░∴∷⧉░⍜░¤░∷⌁∷⋈⋈∿⧉⎔≋¤⌬░░⟟≋⋈▚⌬⋈◇∿∴⎔⊘⌁⧉⧉⎔
∿≋⌁ if hashlib.sha256(SOURCE).hexdigest() != pin: ⟟⋈⧉⎔¤◇▞⎔¤≋⌁▞⎔⎔⎔∷▞∴≋⧉⎔⟟⍜⟟⟟∿
⎔⧉⟟∷⧉¤⌬ raise TamperError("nice try") ¤≋∿⌬≋▚░⎔◇¤⌬⌬⊘▚∷⎔¤⌬▞∴▞¤░░⋈⍜⍜░◇⎔⧉¤▚⧉⌬≋∷⍜
∷▚▞ return replay(run) == run.result ⋈ # champions survive inspection ⎔¤⊘⊘∿⟟
∿≋◇⎔⌁⋈▞∷▞░⋈◇⌬∴¤░≋░░◇░⌬⟟¤¤⌬∿≋⋈▞⧉⋈▚¤≋≋⍜¤⊘⧉⋈⌁▚≋⍜▞⋈⌬⍜▞⧉⟟∴░⌬▚≋¤⟟▚⊘≋◇⌬▞◇⌁⋈⋈◇≋⎔≋∴▞▞
while season.is_open(): ⎔¤∿⋈⟟⌬▞▚⎔▚∴⌬⌁¤⎔⌁⌬░◇⎔∴¤◇∿⧉¤⌬∿∿▞∴⌬⊘≋∷▚░∿∴⎔⋈⧉⊘⧉░⟟⊘⧉⎔⍜¤⟟
≋⟟◇ entrant = queue.pop() ∿⋈∷⟟⍜⍜≋≋≋◇⎔⋈⎔⊘▚◇◇⧉∷¤▚⌬∷⊘◇∷¤¤⌬∿⎔▚░⊘⎔⊘∿⍜⊘∿▞⧉⋈░∿⍜⎔⍜∿⌁
⋈⍜⌬ for room in course.rooms: ⌬∴░¤∷⌬⌁▚⍜⋈◇⊘⧉⊘≋≋⟟∿▚⋈∴⋈⌬⊘░∷⎔⌁▞∷⊘∷▚⋈⧉≋▚⟟⟟⍜⟟⎔∷∷⎔⊘
≋▞◇⎔⌬⌁▚ state = room.spawn(seed=get_room_seed()) ¤¤░⌬⍜∷▚⧉∿▞◇⧉∷⌁⋈⌁∿░▞⋈⧉⍜▚░∿⊘⍜
∷◇⋈░⊘⍜⎔ run = entrant.race(room, state) ⟟⋈⎔⧉∿≋⟟⌁⟟⟟⌬⟟⧉◇⋈⊘▚⧉⟟⋈░≋¤⌁∿▚⧉⍜∿≋∷∿⌬⌁⍜⎔
⎔∿≋⟟∴◇▞ grade(room, state) ⍜⌬⌬≋⧉⋈∿¤⧉⌬▚⟟▞⋈∿¤⋈≋⊘⟟∷⍜∷⎔⎔⟟∴░¤⌁∴▞∿∿⧉≋≋⧉⋈░⌬⌬⎔▚∿⌬⧉∷⧉
≋≋⟟ publish(run) ⊘ # additive only. unpublishing is a human decision. ¤⋈░░∴⌬