"""Validate manifest, fixed counts, row identity, source intervals, and portable scoring.""" import hashlib, json, sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] def rows(path): return [json.loads(l) for l in path.read_text().splitlines() if l.strip()] manifest = json.loads((ROOT / "MANIFEST.sha256.json").read_text()) for name, expected in manifest["files"].items(): assert hashlib.sha256((ROOT / name).read_bytes()).hexdigest() == expected, name for suite, count in [("generation",772),("source",152),("workflow",12)]: original = rows(ROOT / "inputs" / (suite + ".jsonl")) view = rows(ROOT / "data" / suite / "test.jsonl") assert len(original) == len(view) == count ids = [x["id"] for x in view] assert len(set(ids)) == count for a,b in zip(original,view): assert json.loads(b["input_json"]) == a assert json.loads(b["messages_json"]) == a.get("messages", []) if suite != "workflow": refs = {x["id"]:x["reference"] for x in rows(ROOT / "references" / (suite + ".jsonl"))} assert set(refs) == set(ids) for x in view: assert json.loads(x["reference_json"]) == refs[x["id"]] source = {x["id"]: x for x in rows(ROOT / "inputs/source.jsonl")} for x in rows(ROOT / "references/source.jsonl"): ref = x["reference"] text = source[x["id"]]["source"]["source_text"] spans = (ref["evidence_spans"] if "evidence_spans" in ref else [{"start":ref["start"],"end":ref["end"],"text":ref["quote"]}]) for span in spans: assert text[span["start"]:span["end"]] == span["text"], x["id"] sys.path.insert(0, str(ROOT / "scoring")) from common_scoring import score_generation, score_source tools = [{"function":{"name":"measure","parameters":{"type":"object","properties":{"temperature":{"type":"number"}},"required":["temperature"],"additionalProperties":False}}}] ref = {"tool_calls":[{"function":{"name":"measure","arguments":{"temperature":300}}}]} good = '{"tool_calls":[{"name":"measure","arguments":{"temperature":300}}]}' assert score_generation(good,ref,tools)["tool_arguments_match_reference"] for bad in [good.replace("300",'"300"'), good.replace("measure","unknown"),good.replace("300","1e999"),"\u0060\u0060\u0060json\n"+good+"\n\u0060\u0060\u0060"]: assert not score_generation(bad,ref,tools)["tool_schema_valid"] case={"id":"test","source_group":"test","kind":"quote_locator","source":{"source_text":"abc"}} qref={"doi":"test","locator":"p1","quote":"abc","start":0,"end":3} assert score_source(case,qref,json.dumps(qref))["mechanical_grounding_pass"] assert not score_source(case,qref,json.dumps({**qref,"end":2}))["mechanical_grounding_pass"] print(json.dumps({"passed":True,"cases":936,"manifest_files":len(manifest["files"])}))