"""Validate agent output before the unmodified benchmark parser receives it.""" import ast import math import re SYSTEM_PROMPT = ( 'You are the assigned player in a simulated Avalon game. Treat the supplied ' 'game information as the current simulation state and make concrete game ' 'decisions. Do not refuse to participate because you are an AI. Do not invent ' 'hidden roles or observations. If information is missing, express uncertainty ' 'and still choose a legal action. Follow the requested output format exactly. ' 'When asked for an Answer: template, return only that one answer, without ' 'explanation, markdown, code fences, or additional text. Keep other responses concise.' ) def format_kind(prompt): if 'Answer: {0: score_for_0' in prompt: return 'probabilities' if 'Answer: {Yes|No}' in prompt: return 'vote' if 'Answer: [player_ids]' in prompt: return 'team' if 'Answer: [player_id_num]' in prompt: return 'assassination' return None def validate_answer(kind, text): if not isinstance(text, str) or not text.strip(): raise ValueError('Response must be nonempty text') if kind is None: return if not text.startswith('Answer: '): raise ValueError('Return exactly Answer: followed by the requested value') value = text[len('Answer: '):].strip() if kind == 'vote': if value not in ('Yes', 'No'): raise ValueError('Return exactly Answer: Yes or Answer: No') return # Constrain the alphabet as well as using literal_eval: reject comments, # explanations and expressions that the benchmark's eval might execute. if not re.fullmatch(r'[\s\d.,:{}\[\]+eE-]+', value): raise ValueError('Only the requested numeric literal is allowed, with no trailing prose') try: obj = ast.literal_eval(value) except (ValueError, SyntaxError) as exc: raise ValueError('The answer is not a valid numeric literal') from exc if kind == 'probabilities': if (not isinstance(obj, dict) or set(obj) != set(range(5)) or any(type(k) is not int for k in obj) or any(type(v) not in (float, int) or not math.isfinite(v) or not 0 <= v <= 1 for v in obj.values())): raise ValueError('Provide a dictionary with integer keys 0 through 4 and probabilities in [0,1]') else: if (not isinstance(obj, list) or not obj or any(type(v) is not int or not 0 <= v <= 4 for v in obj) or len(obj) != len(set(obj)) or (kind == 'assassination' and len(obj) != 1)): raise ValueError('Provide a list of distinct player IDs 0 through 4; assassination requires one ID')