| 1 | from __future__ import annotations
|
| 2 |
|
| 3 | import contextlib
|
| 4 | import io
|
| 5 | import json
|
| 6 | from pathlib import Path
|
| 7 | import sys
|
| 8 | import tempfile
|
| 9 | import unittest
|
| 10 |
|
| 11 | sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))
|
| 12 |
|
| 13 | import publish_gate
|
| 14 | from oakbench.public_output import output_evidence_failures, workflow_evidence_failures
|
| 15 | from oakbench.output_semantics import OUTPUT_ORACLE_VERSION, PATCH_ORACLE_VERSION
|
| 16 |
|
| 17 |
|
| 18 | def exact_output():
|
| 19 | return {
|
| 20 | "information_recall": 1.0, "information_precision": 1.0,
|
| 21 | "information_recall_source": "exact_metadata_path_set",
|
| 22 | "information_precision_source": "exact_metadata_path_set",
|
| 23 | "output_oracle_version": OUTPUT_ORACLE_VERSION,
|
| 24 | "pipe_compatible_unified": True, "patch_application_required": True,
|
| 25 | "patch_apply_ok": True, "patch_tree_matches": True, "patch_apply_returncode": 0,
|
| 26 | "patch_oracle_version": PATCH_ORACLE_VERSION,
|
| 27 | "patch_apply_source": "git_apply_exact_tree",
|
| 28 | "patch_instrument_sha256": "a" * 64,
|
| 29 | "patch_instrument_version": "git version 2.50.1",
|
| 30 | }
|
| 31 |
|
| 32 |
|
| 33 | class PublicOutputTest(unittest.TestCase):
|
| 34 | def test_absent_measurements_are_not_fabricated(self):
|
| 35 | self.assertEqual(output_evidence_failures({}), [])
|
| 36 | self.assertEqual(output_evidence_failures({"information_recall": None}), [])
|
| 37 |
|
| 38 | def test_exact_evidence_accepts_valid_patch_and_measured_low_recall(self):
|
| 39 | row = exact_output()
|
| 40 | self.assertEqual(output_evidence_failures(row), [])
|
| 41 | row["information_recall"] = 0.0
|
| 42 | self.assertEqual(output_evidence_failures(row), [])
|
| 43 |
|
| 44 | def test_legacy_substrings_and_structural_headers_are_not_proof(self):
|
| 45 | for change in (
|
| 46 | {"information_recall_source": "ground_truth_path_substring_match"},
|
| 47 | {"information_precision_source": "path_token_precision_against_changed_paths"},
|
| 48 | {"output_oracle_version": None},
|
| 49 | {"patch_apply_ok": None}, {"patch_tree_matches": False},
|
| 50 | {"patch_apply_returncode": 1}, {"patch_apply_returncode": False},
|
| 51 | {"patch_oracle_version": None}, {"patch_apply_source": "header_regex"},
|
| 52 | {"patch_skip_reason": "pristine_tree_over_cap"},
|
| 53 | {"patch_failure_reason": "patch_result_tree_mismatch"},
|
| 54 | {"information_recall": float("nan")}, {"information_precision": 1.1},
|
| 55 | {"information_recall": True}, {"patch_instrument_sha256": None},
|
| 56 | {"patch_instrument_version": ""},
|
| 57 | {"probe_output_truncated": True}, {"output_truncated": True},
|
| 58 | ):
|
| 59 | with self.subTest(change=change):
|
| 60 | self.assertTrue(output_evidence_failures({**exact_output(), **change}))
|
| 61 |
|
| 62 | def test_dirty_structure_is_diagnostic_not_patch_applicability(self):
|
| 63 | row = {key: value for key, value in exact_output().items() if not key.startswith("patch_")}
|
| 64 | row["operation"] = "diff.dirty.inforecall"
|
| 65 | self.assertEqual(output_evidence_failures(row), [])
|
| 66 | row["patch_application_required"] = True
|
| 67 | self.assertTrue(output_evidence_failures(row))
|
| 68 |
|
| 69 | def test_malformed_flags_cannot_bypass_evidence_checks(self):
|
| 70 | for flag in ("pipe_compatible_unified", "patch_apply_ok", "patch_tree_matches"):
|
| 71 | self.assertEqual(output_evidence_failures({flag: None}), [])
|
| 72 | for value in (1, 0, "true", [], {}):
|
| 73 | with self.subTest(flag=flag, value=value):
|
| 74 | self.assertTrue(output_evidence_failures({flag: value}))
|
| 75 |
|
| 76 | def test_cross_subject_workflow_evidence_cannot_hide_asymmetry(self):
|
| 77 | rows = [{"subject": subject, "scenario": "task", "operation": "agent.workflow",
|
| 78 | "workflow_integrity_source": source, "workflow_integrity_passed": True}
|
| 79 | for subject, source in (("git", "trusted_git_head"), ("oak", "metadata_only"))]
|
| 80 | self.assertTrue(workflow_evidence_failures(rows))
|
| 81 | rows[1]["workflow_integrity_source"] = "trusted_git_head"
|
| 82 | self.assertEqual(workflow_evidence_failures(rows), [])
|
| 83 | rows[1]["workflow_integrity_passed"] = None
|
| 84 | self.assertTrue(workflow_evidence_failures(rows))
|
| 85 | rows[1].pop("workflow_integrity_source")
|
| 86 | self.assertTrue(workflow_evidence_failures(rows))
|
| 87 |
|
| 88 | def test_skipped_workflow_instrument_cannot_disappear_from_public_comparison(self):
|
| 89 | rows = [{"subject": subject, "scenario": "task", "operation": "agent.workflow",
|
| 90 | "workflow_integrity_source": "trusted_task_payload;known_good_git_head_tree",
|
| 91 | "workflow_integrity_passed": True,
|
| 92 | "agent": {"name": "agent", "model": "model", "provider": "provider", "environment": "minimal"}}
|
| 93 | for subject in ("git", "oak")]
|
| 94 | rows[0].update(returncode=77, skipped=True, skip_reason="git_head_instrument_unavailable",
|
| 95 | workflow_integrity_passed=None)
|
| 96 | self.assertTrue(workflow_evidence_failures(rows))
|
| 97 | with tempfile.TemporaryDirectory() as temporary:
|
| 98 | path = Path(temporary) / "rows.jsonl"
|
| 99 | path.write_text("\n".join(json.dumps(row) for row in rows))
|
| 100 | output = io.StringIO()
|
| 101 | with contextlib.redirect_stdout(output):
|
| 102 | self.assertEqual(publish_gate.main([str(path)]), 1)
|
| 103 | self.assertIn("FAIL workflow-evidence", output.getvalue())
|
| 104 | rows[1]["agent"] = {**rows[1]["agent"], "model": "different"}
|
| 105 | self.assertEqual(workflow_evidence_failures(rows), [])
|
| 106 |
|
| 107 | def test_publication_entrypoint_enforces_evidence(self):
|
| 108 | rows = []
|
| 109 | for subject in ("git", "git_untracked_cache", "git_fsmonitor"):
|
| 110 | rows.append({
|
| 111 | "profile": "standard", "subject": subject, "scenario": "tiny_text",
|
| 112 | "operation": "diff.full.inforecall", "returncode": 0,
|
| 113 | "env_isolation_version": "1", "randomized_subject_order": True,
|
| 114 | "subject_order": ["git", "git_untracked_cache", "git_fsmonitor"],
|
| 115 | "benchmark_track": "core-equivalent", "bench_id": "gate-test", "run": 0,
|
| 116 | **exact_output(),
|
| 117 | })
|
| 118 | measured = [
|
| 119 | {key: value for key, value in row.items() if key not in exact_output()}
|
| 120 | for row in rows
|
| 121 | ]
|
| 122 | rows.extend({**row, "operation": "diff.dirty.inforecall",
|
| 123 | **{key: value for key, value in exact_output().items() if not key.startswith("patch_")}}
|
| 124 | for row in measured)
|
| 125 | rows.extend(
|
| 126 | {**row, "operation": "status.clean", "run": run}
|
| 127 | for row in measured for run in range(30)
|
| 128 | )
|
| 129 | with tempfile.TemporaryDirectory() as temporary:
|
| 130 | path = Path(temporary) / "rows.jsonl"
|
| 131 | path.write_text("\n".join(json.dumps(row) for row in rows))
|
| 132 | with contextlib.redirect_stdout(io.StringIO()):
|
| 133 | self.assertEqual(publish_gate.main([str(path)]), 0)
|
| 134 | rows[0]["patch_apply_ok"] = None
|
| 135 | rows[0]["patch_skip_reason"] = "git_unavailable"
|
| 136 | path.write_text("\n".join(json.dumps(row) for row in rows))
|
| 137 | output = io.StringIO()
|
| 138 | with contextlib.redirect_stdout(output):
|
| 139 | self.assertEqual(publish_gate.main([str(path)]), 1)
|
| 140 | self.assertIn("FAIL output-evidence", output.getvalue())
|
| 141 |
|
| 142 |
|
| 143 | if __name__ == "__main__":
|
| 144 | unittest.main()
|