"""Compare shared-family accuracy, reader agreement, and evidence cost.""" import argparse from collections import Counter, defaultdict import hashlib import json from pathlib import Path import sys READERS = ("deepseek", "luna", "qwen25_7b", "llama31_8b") METHODS = ("no_rag", "bm25_369", "own_369", "graph_369", "oracle") def read(path): with path.open() as stream: return [json.loads(line) for line in stream] def fingerprint(path): return hashlib.sha256(path.read_bytes()).hexdigest() def calculate(root): sys.path.insert(0, str(root / "code")) from response_parsing import normalize_prediction questions_path = root / "data/sealed_test_human.jsonl" requests_path = root / "inputs/reader_requests950.jsonl" items = {r["id"]: r for r in read(questions_path)} requests = {(r["id"], r["method"]): r for r in read(requests_path) if r["id"] in items} assert len(items) == 150 and requests.keys() == {(i, m) for i in items for m in METHODS} pairs = defaultdict(list) for i, item in items.items(): pairs[item["pair_id"]].append(i) assert len(pairs) == 75 and all(len(ids) == 2 for ids in pairs.values()) assert len({items[ids[0]]["source_family_id"] for ids in pairs.values()}) == 75 evidence = {} for key, request in requests.items(): payload = json.loads(request["messages"][1]["content"]) i, _ = key assert payload["question"] == items[i]["question"] and payload["options"] == items[i]["options"] text = payload["evidence"] assert hashlib.sha256(text.encode()).hexdigest() == request["context_sha256"] evidence[key] = items[i]["gold_quote"] in text scores = {} files = {questions_path.relative_to(root).as_posix(): fingerprint(questions_path), requests_path.relative_to(root).as_posix(): fingerprint(requests_path)} for model in READERS: predictions = root / f"predictions/{model}.jsonl" seen = {} for r in read(predictions): key = r["id"], r["method"] if key not in requests or r.get("error"): continue assert key not in seen and r["context_sha256"] == requests[key]["context_sha256"] assert r["context_tokens_unified"] == requests[key]["context_tokens_unified"] seen[key] = normalize_prediction(r, items[r["id"]]["options"])["answer"] == items[r["id"]]["gold"] assert seen.keys() == requests.keys() scores[model] = {(p, m): all(seen[i, m] for i in ids) for p, ids in pairs.items() for m in METHODS} baseline = json.loads((root / f"results/{model}_sealed_test_human_scores.json").read_text()) assert all(sum(scores[model][p, m] for p in pairs) == baseline["methods"][m]["contrast_pairs"]["both_correct"] for m in METHODS) files[predictions.relative_to(root).as_posix()] = fingerprint(predictions) by_method = {} for method in METHODS: successful_readers = Counter(sum(scores[reader][p, method] for reader in READERS) for p in pairs) winners = {reader: sum(scores[reader][p, method] for p in pairs) for reader in READERS} totals = sum(requests[i, method]["context_tokens_unified"] for i in items) by_method[method] = { "n_pairs": len(pairs), "successful_readers_per_pair_histogram": {str(n): successful_readers[n] for n in range(5)}, "pairs_correct_per_reader": winners, "all_readers_both_members_correct": successful_readers[4], "zero_readers_both_members_correct": successful_readers[0], "both_members_annotated_quote_visible": sum(all(evidence[i, method] for i in ids) for ids in pairs.values()), "total_evidence_tokens_per_reader": totals, "mean_evidence_tokens_per_question": totals / len(items), } transitions = {} for m in METHODS[1:]: gain = [sum(scores[r][p, m] and not scores[r][p, "no_rag"] for r in READERS) for p in pairs] loss = [sum(scores[r][p, "no_rag"] and not scores[r][p, m] for r in READERS) for p in pairs] transitions[m] = { "sources_with_any_gain": sum(n > 0 for n in gain), "sources_with_any_loss": sum(n > 0 for n in loss), "sources_with_both_gain_and_loss_across_readers": sum(g > 0 and l > 0 for g, l in zip(gain, loss)), "reader_pair_gains": sum(gain), "reader_pair_losses": sum(loss), } operational = METHODS[:-1] frontier = {} for reader in READERS: points = {m: (by_method[m]["total_evidence_tokens_per_reader"], by_method[m]["pairs_correct_per_reader"][reader]) for m in operational} frontier[reader] = [m for m, (cost, score) in points.items() if not any( c <= cost and a >= score and (c < cost or a > score) for c, a in points.values())] return { "scope": "Descriptive comparison of the four archived readers on 75 shared historical held-out silver " "source pairs with visible study titles. Reader-pair observations are correlated, so joint counts " "are not treated as independent. Cost counts only shown evidence in the shared tokenizer; " "excludes generation, retrieval latency and API pricing. Oracle is not an operational policy.", "source_pairs": len(pairs), "readers": list(READERS), "file_sha256": files, "methods": by_method, "against_no_rag": transitions, "descriptive_accuracy_evidence_frontier": frontier, "new_model_calls": 0, } def main(): p = argparse.ArgumentParser() p.add_argument("--release", type=Path, required=True) p.add_argument("--output", type=Path, required=True) args = p.parse_args() report = calculate(args.release) args.output.write_text(json.dumps(report, indent=2) + "\n") print(json.dumps({"methods": report["methods"], "against_no_rag": report["against_no_rag"], "frontier": report["descriptive_accuracy_evidence_frontier"]}, indent=2)) if __name__ == "__main__": main()