#!/usr/bin/env python3 """Select the exploratory query-local configuration from dev-only receipts.""" from __future__ import annotations import json import sys from datetime import datetime, timezone from pathlib import Path REPO_ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(REPO_ROOT / "src")) from telen.static_benchmark import sha256_file # noqa: E402 RESULTS = REPO_ROOT / "research/results" GRAPH_SEED_K = (1, 2, 4, 8, 16) EQUIVALENCE_MARGIN = 0.02 MINIMUM_EFFECT = 0.01 def _load_dev_receipt(path: Path) -> dict: receipt = json.loads(path.read_text(encoding="utf-8")) if receipt.get("final_test_accessed") is not False: raise ValueError(f"development receipt accessed test: {path}") if "test_post_summary" in receipt or "test_pre_mean_ndcg_at_10" in receipt: raise ValueError(f"development receipt contains test metrics: {path}") if receipt.get("primary_evaluation_split") != "dev": raise ValueError(f"development receipt is not dev-scoped: {path}") return receipt def _metrics(receipt: dict) -> dict[str, float | bool]: post = receipt["primary_post_summary"] pre = receipt["primary_pre_mean_ndcg_at_10"] mean = lambda condition: post[condition]["mean_ndcg_at_10_across_seeds"] utility = mean("learned_correct") - mean("hard_temporal_rule") causal = mean("learned_correct") - mean("learned_missing") wrong = mean("learned_correct") - mean("learned_wrong") unrelated = abs(mean("learned_correct") - mean("learned_unrelated_removed")) pre_drift = abs(pre["learned_correct"] - pre["hard_temporal_rule"]) eligible = bool( utility >= MINIMUM_EFFECT and causal > 0.0 and wrong > 0.0 and unrelated <= EQUIVALENCE_MARGIN and pre_drift <= EQUIVALENCE_MARGIN ) return { "utility_gain_over_hard_rule": utility, "correct_minus_missing": causal, "correct_minus_wrong": wrong, "absolute_unrelated_removal_effect": unrelated, "absolute_pre_update_change_from_hard_rule": pre_drift, "eligible": eligible, } def main() -> None: candidates = [] for graph_seed_k in GRAPH_SEED_K: path = RESULTS / ( f"telen_ai_silver_dev_graph_seed_k{graph_seed_k}_2026-08-29.json" ) receipt = _load_dev_receipt(path) candidates.append( { "graph_seed_k": graph_seed_k, "receipt": str(path.relative_to(REPO_ROOT)), "receipt_sha256": sha256_file(path), "metrics": _metrics(receipt), } ) eligible = [candidate for candidate in candidates if candidate["metrics"]["eligible"]] if not eligible: raise RuntimeError("no dev configuration satisfies the frozen safety checks") selected = max( eligible, key=lambda candidate: ( candidate["metrics"]["utility_gain_over_hard_rule"], -candidate["graph_seed_k"], ), ) diagnostic_paths = sorted( [*RESULTS.glob("telen_ai_silver_dev_stability_w*_2026-08-29.json")] + [*RESULTS.glob("telen_ai_silver_dev_cap*_2026-08-29.json")] ) diagnostic_receipts = [] for path in diagnostic_paths: receipt = _load_dev_receipt(path) diagnostic_receipts.append( { "receipt": str(path.relative_to(REPO_ROOT)), "receipt_sha256": sha256_file(path), "configuration": receipt["configuration"], "metrics": _metrics(receipt), } ) output = RESULTS / "telen_ai_silver_dev_selection_2026-08-29.json" selection = { "schema_version": 1, "artifact_type": "telen_ai_silver_development_selection", "created_at": datetime.now(timezone.utc).isoformat(), "data_status": "single_ai_pre_review_silver_not_expert_gold", "final_test_accessed": False, "confirmatory_claim_authorized": False, "thresholds": { "minimum_utility_effect": MINIMUM_EFFECT, "pre_and_unrelated_equivalence_margin": EQUIVALENCE_MARGIN, }, "selection_rule": ( "Among configurations passing utility, causal, wrong-event, unrelated, " "and pre-update checks, maximize dev utility; break ties toward smaller k." ), "candidates": candidates, "diagnostic_sweeps_not_selected": diagnostic_receipts, "selected": { **selected, "frozen_final_configuration": { "graph_seed_k": selected["graph_seed_k"], "stability_weight": 1.0, "residual_cap": 0.5, "epochs": 150, "seeds": [17, 29, 43], "learned_status_shortcut": False, }, }, "limitations": [ "Selection uses one seed and two development event groups.", "No expert-gold relevance judgments or matched controls are available.", "The selected configuration remains exploratory regardless of test metrics.", ], } output.write_text( json.dumps(selection, ensure_ascii=False, indent=2) + "\n", encoding="utf-8", ) print(json.dumps({"output": str(output), "selected": selection["selected"]}, indent=2)) if __name__ == "__main__": main()