"""Validate WAVs, bindings, matrix, input modalities and world scoring without a GPU.""" from collections import Counter import itertools import json import os from pathlib import Path import wave from eval_ebase import ROOT, MODALITIES, episode_for, inputs_for, load_data, messages_for_prompt, score from vb.audio2tool import Home from vb.common import file_hash, read_json from vb.policy import allowed def check(condition, context): if not condition: raise ValueError(context) def verify(): os.chdir(ROOT) episodes, enroll, trials = load_data() tools = set(Home().registry.specs) cells = [(e["target"]["name"], e["n"], e["minutes"]) for e in episodes.values()] check(len(tools) == 32 and Counter(cells) == Counter(itertools.product( tools, range(2, 6), (2, 4, 6, 8, 10))), "Incomplete recording matrix") expected = itertools.product(episodes, ("authorized", "unauthorized"), MODALITIES) check(Counter((t["pair_id"], t["authorization"], t["modality"]) for t in trials) == Counter(expected), "Incomplete trial matrix") check(len({t["trial_id"] for t in trials}) == 3840, "Trial IDs") for clip in enroll.values(): check(file_hash(clip["path"]) == clip["sha256"], clip["path"]) turn_count = 0 for key, e in episodes.items(): check(file_hash(e["audio"]["path"]) == e["audio"]["sha256"], key) check(file_hash(e["script_path"]) == e["script_sha256"], key) script = read_json(e["script_path"]) cast = {p["id"]: p["sid"] for p in script["cast"]} expected = [(cast[t["speaker"]], t["text"]) for t in script["turns"]] check(expected == [(t["sid"], t["text"]) for t in e["timeline"]], f"Script alignment: {key}") check(e["requester"] == e["timeline"][-1]["sid"], key) with wave.open(e["audio"]["path"]) as src: starts = [round(t["start_s"] * 16000) for t in e["timeline"]] + [src.getnframes()] check(len(e["turn_paths"]) == len(expected) and starts[0] == 0, key) for i, path in enumerate(e["turn_paths"]): with wave.open(path) as segment: frames = starts[i+1] - starts[i] check(segment.getparams()[:3] == src.getparams()[:3], path) check(segment.getnframes() == frames and segment.readframes(frames) == src.readframes(frames), path) turn_count += 1 max_slots = 0 for trial in trials: e = episode_for(episodes[trial["pair_id"]], trial) m = trial["modality"] data = inputs_for(e, trial, enroll) system, user = messages_for_prompt(data) check(system["role"] == "system" and user["role"] == "user", e["id"]) names = {p["sid"]: p["name"] for p in e["roster"]} enroll_paths = [enroll[p["enrollment"]]["path"] for p in e["roster"]] paths = [b["audio"] for msg in (system, user) for b in msg["content"] if b["type"] == "audio"] check(paths == data["audio_paths"], e["id"]) if m == "audio_name": expected = [] for turn, path in zip(e["timeline"], e["turn_paths"]): expected.extend([{"type":"text","text":f"[{names[turn['sid']]}]:"}, {"type":"audio","audio":path}]) check(user["content"] == expected and paths == enroll_paths + e["turn_paths"], e["id"]) elif m == "audio_only": check(user["content"] == [{"type":"audio","audio":e["audio"]["path"]}] and paths == enroll_paths + [e["audio"]["path"]], e["id"]) else: transcript = "\n".join(f"[{names[t['sid']]}] {t['text']}" for t in e["timeline"]) check(not paths and user["content"] == [{"type":"text","text":transcript}], e["id"]) max_slots = max(max_slots, len(paths)) world = Home(e["initial_patch"]) grants = [p for p in names if allowed(world, e["acl"], p, e["target"])] authorized = trial["authorization"] == "authorized" check(len(grants) == 1 and (e["requester"] in grants) == authorized, e["id"]) answer = {"action":"call","calls":[e["target"]]} if authorized else {"action":"refuse"} metrics = score(e, json.dumps(answer), "stop")["metrics"] check(metrics["strict_success"] if authorized else metrics["explicit_refusal"], e["id"]) print(f"Verified {len(episodes)} recordings, {len(enroll)} enrollments, {turn_count} turn WAVs, " f"{len(trials)} trial inputs and oracle decisions; maximum {max_slots} audio items.") if __name__ == "__main__": verify()