| 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 | from oakbench.workflow_oracles import VERSION as WORKFLOW_ORACLE_VERSION
|
| 17 |
|
| 18 |
|
| 19 | def exact_output():
|
| 20 | return {
|
| 21 | "information_recall": 1.0, "information_precision": 1.0,
|
| 22 | "information_recall_source": "exact_metadata_path_set",
|
| 23 | "information_precision_source": "exact_metadata_path_set",
|
| 24 | "output_oracle_version": OUTPUT_ORACLE_VERSION,
|
| 25 | "pipe_compatible_unified": True, "patch_application_required": True,
|
| 26 | "patch_apply_ok": True, "patch_tree_matches": True, "patch_apply_returncode": 0,
|
| 27 | "patch_oracle_version": PATCH_ORACLE_VERSION,
|
| 28 | "patch_apply_source": "git_apply_exact_tree",
|
| 29 | "patch_instrument_sha256": "a" * 64,
|
| 30 | "patch_instrument_version": "git version 2.50.1",
|
| 31 | }
|
| 32 |
|
| 33 |
|
| 34 | class PublicOutputTest(unittest.TestCase):
|
| 35 | def test_absent_measurements_are_not_fabricated(self):
|
| 36 | self.assertEqual(output_evidence_failures({}), [])
|
| 37 | self.assertEqual(output_evidence_failures({"information_recall": None}), [])
|
| 38 |
|
| 39 | def test_exact_evidence_accepts_valid_patch_and_measured_low_recall(self):
|
| 40 | row = exact_output()
|
| 41 | self.assertEqual(output_evidence_failures(row), [])
|
| 42 | row["information_recall"] = 0.0
|
| 43 | self.assertEqual(output_evidence_failures(row), [])
|
| 44 |
|
| 45 | def test_legacy_substrings_and_structural_headers_are_not_proof(self):
|
| 46 | for change in (
|
| 47 | {"information_recall_source": "ground_truth_path_substring_match"},
|
| 48 | {"information_precision_source": "path_token_precision_against_changed_paths"},
|
| 49 | {"output_oracle_version": None},
|
| 50 | {"patch_apply_ok": None}, {"patch_tree_matches": False},
|
| 51 | {"patch_apply_returncode": 1}, {"patch_apply_returncode": False},
|
| 52 | {"patch_oracle_version": None}, {"patch_apply_source": "header_regex"},
|
| 53 | {"patch_skip_reason": "pristine_tree_over_cap"},
|
| 54 | {"patch_failure_reason": "patch_result_tree_mismatch"},
|
| 55 | {"information_recall": float("nan")}, {"information_precision": 1.1},
|
| 56 | {"information_recall": True}, {"patch_instrument_sha256": None},
|
| 57 | {"patch_instrument_version": ""},
|
| 58 | {"probe_output_truncated": True}, {"output_truncated": True},
|
| 59 | ):
|
| 60 | with self.subTest(change=change):
|
| 61 | self.assertTrue(output_evidence_failures({**exact_output(), **change}))
|
| 62 |
|
| 63 | def test_dirty_structure_is_diagnostic_not_patch_applicability(self):
|
| 64 | row = {key: value for key, value in exact_output().items() if not key.startswith("patch_")}
|
| 65 | row["operation"] = "diff.dirty.inforecall"
|
| 66 | self.assertEqual(output_evidence_failures(row), [])
|
| 67 | row["patch_application_required"] = True
|
| 68 | self.assertTrue(output_evidence_failures(row))
|
| 69 |
|
| 70 | def test_malformed_flags_cannot_bypass_evidence_checks(self):
|
| 71 | for flag in ("pipe_compatible_unified", "patch_apply_ok", "patch_tree_matches"):
|
| 72 | self.assertEqual(output_evidence_failures({flag: None}), [])
|
| 73 | for value in (1, 0, "true", [], {}):
|
| 74 | with self.subTest(flag=flag, value=value):
|
| 75 | self.assertTrue(output_evidence_failures({flag: value}))
|
| 76 |
|
| 77 | def test_cross_subject_workflow_evidence_cannot_hide_asymmetry(self):
|
| 78 | rows = [{"subject": subject, "scenario": "wide_config_refactor", "operation": "agent.workflow",
|
| 79 | "workflow_integrity_source": source, "workflow_integrity_passed": True,
|
| 80 | "workflow_integrity_evidence_class": evidence_class, "oracle_version": WORKFLOW_ORACLE_VERSION}
|
| 81 | for subject, source, evidence_class in (("git", "trusted_exact_task_worktree;known_good_git_head_tree", "independent_committed_tree"),
|
| 82 | ("oak", "trusted_exact_task_worktree;subject_produced_oak_head_export", "subject_produced_committed_tree"))]
|
| 83 | self.assertTrue(workflow_evidence_failures(rows))
|
| 84 | rows[1]["workflow_integrity_source"] = "another independently validated reader"
|
| 85 | rows[1]["workflow_integrity_evidence_class"] = "independent_committed_tree"
|
| 86 | self.assertEqual(workflow_evidence_failures(rows), [])
|
| 87 | rows[1]["workflow_integrity_passed"] = None
|
| 88 | self.assertTrue(workflow_evidence_failures(rows))
|
| 89 | rows[1].pop("workflow_integrity_source")
|
| 90 | self.assertTrue(workflow_evidence_failures(rows))
|
| 91 |
|
| 92 | def test_skipped_workflow_instrument_cannot_disappear_from_public_comparison(self):
|
| 93 | rows = [{"subject": subject, "scenario": "wide_config_refactor", "operation": "agent.workflow",
|
| 94 | "workflow_integrity_source": "trusted_exact_task_worktree;known_good_git_head_tree",
|
| 95 | "workflow_integrity_evidence_class": "independent_committed_tree", "oracle_version": WORKFLOW_ORACLE_VERSION,
|
| 96 | "workflow_integrity_passed": True,
|
| 97 | "agent": {"name": "agent", "model": "model", "provider": "provider", "environment": "minimal"}}
|
| 98 | for subject in ("git", "oak")]
|
| 99 | rows[0].update(returncode=77, skipped=True, skip_reason="git_head_instrument_unavailable",
|
| 100 | workflow_integrity_passed=None)
|
| 101 | self.assertTrue(workflow_evidence_failures(rows))
|
| 102 | with tempfile.TemporaryDirectory() as temporary:
|
| 103 | path = Path(temporary) / "rows.jsonl"
|
| 104 | path.write_text("\n".join(json.dumps(row) for row in rows))
|
| 105 | output = io.StringIO()
|
| 106 | with contextlib.redirect_stdout(output):
|
| 107 | self.assertEqual(publish_gate.main([str(path)]), 1)
|
| 108 | self.assertIn("FAIL workflow-evidence", output.getvalue())
|
| 109 | rows[1]["agent"] = {**rows[1]["agent"], "model": "different"}
|
| 110 | self.assertEqual(workflow_evidence_failures(rows), [])
|
| 111 |
|
| 112 | def test_bugfix_real_producer_labels_retain_existing_publication_policy(self):
|
| 113 | rows = [{"subject": subject, "subject_kind": kind, "scenario": "bugfix_test_loop",
|
| 114 | "operation": "workflow.total", "profile": "workflow", "returncode": 0,
|
| 115 | "workflow_integrity_passed": True, "workflow_integrity_source": source,
|
| 116 | "oracle_version": WORKFLOW_ORACLE_VERSION,
|
| 117 | "source": {"oak_hash": "reviewed-revision", "oak_status": "clean"}}
|
| 118 | for subject, kind, source in (
|
| 119 | ("git", "git", "worktree_payload_oracle;known_good_git_head_tree"),
|
| 120 | ("oak", "oak", "worktree_payload_oracle;direct_repo_metadata_fingerprint"))]
|
| 121 | self.assertEqual(workflow_evidence_failures(rows), [])
|
| 122 | with tempfile.TemporaryDirectory() as temporary:
|
| 123 | path = Path(temporary) / "rows.jsonl"
|
| 124 | path.write_text("\n".join(json.dumps(row) for row in rows))
|
| 125 | output = io.StringIO()
|
| 126 | with contextlib.redirect_stdout(output):
|
| 127 | code = publish_gate.main([str(path)])
|
| 128 | self.assertEqual(code, 0, output.getvalue())
|
| 129 | self.assertNotIn("FAIL workflow-evidence", output.getvalue())
|
| 130 |
|
| 131 | def test_trusted_workflows_require_measured_independent_same_version_evidence(self):
|
| 132 | for scenario in ("wide_config_refactor", "large_asset_manifest"):
|
| 133 | rows = [{"subject": subject, "scenario": scenario, "operation": "workflow.total",
|
| 134 | "oracle_version": WORKFLOW_ORACLE_VERSION, "workflow_integrity_passed": True,
|
| 135 | "workflow_integrity_evidence_class": "independent_committed_tree",
|
| 136 | "workflow_integrity_source": source}
|
| 137 | for subject, source in (("git", "trusted_exact_task_worktree;known_good_git_head_tree"),
|
| 138 | ("git_candidate", "trusted_exact_task_worktree;reviewed_external_reader"))]
|
| 139 | self.assertEqual(workflow_evidence_failures(rows), [])
|
| 140 | for change in ({"oracle_version": "other"}, {"oracle_version": None},
|
| 141 | {"workflow_integrity_evidence_class": None},
|
| 142 | {"workflow_integrity_source": "trusted_exact_task_worktree;subject_produced_oak_head_export"},
|
| 143 | {"workflow_integrity_evidence_class": "subject_produced_committed_tree"},
|
| 144 | {"workflow_integrity_passed": None}, {"workflow_integrity_passed": False}):
|
| 145 | with self.subTest(scenario=scenario, change=change):
|
| 146 | self.assertTrue(workflow_evidence_failures([rows[0], {**rows[1], **change}]))
|
| 147 | rows[1].update(subject="oak", workflow_integrity_source="trusted_exact_task_worktree;subject_produced_oak_head_export",
|
| 148 | workflow_integrity_evidence_class="subject_produced_committed_tree")
|
| 149 | with tempfile.TemporaryDirectory() as temporary:
|
| 150 | path = Path(temporary) / "rows.jsonl"
|
| 151 | path.write_text("\n".join(json.dumps(row) for row in rows))
|
| 152 | output = io.StringIO()
|
| 153 | with contextlib.redirect_stdout(output):
|
| 154 | self.assertEqual(publish_gate.main([str(path)]), 1)
|
| 155 | self.assertIn("FAIL workflow-evidence", output.getvalue())
|
| 156 |
|
| 157 | def test_publication_entrypoint_enforces_evidence(self):
|
| 158 | rows = []
|
| 159 | for subject in ("git", "git_untracked_cache", "git_fsmonitor"):
|
| 160 | rows.append({
|
| 161 | "profile": "standard", "subject": subject, "scenario": "tiny_text",
|
| 162 | "operation": "diff.full.inforecall", "returncode": 0,
|
| 163 | "env_isolation_version": "1", "randomized_subject_order": True,
|
| 164 | "subject_order": ["git", "git_untracked_cache", "git_fsmonitor"],
|
| 165 | "benchmark_track": "core-equivalent", "bench_id": "gate-test", "run": 0,
|
| 166 | **exact_output(),
|
| 167 | })
|
| 168 | measured = [
|
| 169 | {key: value for key, value in row.items() if key not in exact_output()}
|
| 170 | for row in rows
|
| 171 | ]
|
| 172 | rows.extend({**row, "operation": "diff.dirty.inforecall",
|
| 173 | **{key: value for key, value in exact_output().items() if not key.startswith("patch_")}}
|
| 174 | for row in measured)
|
| 175 | rows.extend(
|
| 176 | {**row, "operation": "status.clean", "run": run}
|
| 177 | for row in measured for run in range(30)
|
| 178 | )
|
| 179 | with tempfile.TemporaryDirectory() as temporary:
|
| 180 | path = Path(temporary) / "rows.jsonl"
|
| 181 | path.write_text("\n".join(json.dumps(row) for row in rows))
|
| 182 | with contextlib.redirect_stdout(io.StringIO()):
|
| 183 | self.assertEqual(publish_gate.main([str(path)]), 0)
|
| 184 | rows[0]["patch_apply_ok"] = None
|
| 185 | rows[0]["patch_skip_reason"] = "git_unavailable"
|
| 186 | path.write_text("\n".join(json.dumps(row) for row in rows))
|
| 187 | output = io.StringIO()
|
| 188 | with contextlib.redirect_stdout(output):
|
| 189 | self.assertEqual(publish_gate.main([str(path)]), 1)
|
| 190 | self.assertIn("FAIL output-evidence", output.getvalue())
|
| 191 |
|
| 192 |
|
| 193 | if __name__ == "__main__":
|
| 194 | unittest.main()
|