| 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_real_bugfix_summary_failures_block_publication(self):
|
| 78 | import workflow_ab
|
| 79 | subject = workflow_ab.Subject("oak", "oak", "Oak", Path("/oak"))
|
| 80 | workflow = workflow_ab.WORKFLOWS["bugfix_test_loop"]
|
| 81 | metadata = {"bench_id": "gate", "profile": "workflow", "source": {"oak_hash": "reviewed", "oak_status": "clean"}}
|
| 82 | for passed, reason in ((True, None), (False, "wrong_result"), (None, "missing_instrument")):
|
| 83 | summary = workflow_ab.sum_rows([], metadata, subject, workflow, 0, "workflow", "workflow.total", {
|
| 84 | "workflow_integrity_passed": passed, "workflow_integrity_failure_reason": reason if passed is False else None,
|
| 85 | "workflow_integrity_unmeasured_reason": reason if passed is None else None})
|
| 86 | with tempfile.TemporaryDirectory() as tmp:
|
| 87 | path = Path(tmp) / "rows.jsonl"
|
| 88 | path.write_text(json.dumps(summary)+"\n")
|
| 89 | with contextlib.redirect_stdout(io.StringIO()):
|
| 90 | code = publish_gate.main([str(path)])
|
| 91 | self.assertEqual(code, 0 if passed else 1)
|
| 92 |
|
| 93 | def test_real_bugfix_payload_oracle_failure_blocks_pair(self):
|
| 94 | import workflow_ab
|
| 95 | with tempfile.TemporaryDirectory() as tmp:
|
| 96 | root = Path(tmp)
|
| 97 | repo = root / "repo"
|
| 98 | workflow_ab.make_bugfix_fixture(repo)
|
| 99 | workflow = workflow_ab.WORKFLOWS["bugfix_test_loop"]
|
| 100 | subject = workflow_ab.Subject("oak", "oak", "Oak", Path("/oak"))
|
| 101 | metadata = {"bench_id": "producer", "profile": "workflow", "source": {"oak_hash": "reviewed", "oak_status": "clean"}}
|
| 102 | for repaired in (False, True):
|
| 103 | if repaired:
|
| 104 | pricing = repo / "app/pricing.py"
|
| 105 | pricing.write_text(pricing.read_text().replace("return cents - percent", "return cents - ((cents * percent) // 100)"))
|
| 106 | fields = workflow_ab.workflow_integrity_fields(subject, workflow, repo, [])
|
| 107 | actual = workflow_ab.sum_rows([], metadata, subject, workflow, 0, "workflow", "workflow.total", fields)
|
| 108 | peer = {**actual, "subject": "git", "returncode": 0, "workflow_integrity_passed": True}
|
| 109 | path = root / "rows.jsonl"
|
| 110 | path.write_text("\n".join(map(json.dumps, [actual, peer])))
|
| 111 | with contextlib.redirect_stdout(io.StringIO()): code = publish_gate.main([str(path)])
|
| 112 | self.assertEqual(code, 0 if repaired else 1)
|
| 113 |
|
| 114 | def test_cross_subject_workflow_evidence_cannot_hide_asymmetry(self):
|
| 115 | rows = [{"subject": subject, "scenario": "wide_config_refactor", "operation": "agent.workflow",
|
| 116 | "workflow_integrity_source": source, "workflow_integrity_passed": True, "returncode": 0,
|
| 117 | "workflow_integrity_evidence_class": evidence_class, "oracle_version": WORKFLOW_ORACLE_VERSION,
|
| 118 | "workflow_subject_binary_sha256": "b" * 64, "git_head_instrument_sha256": "a" * 64}
|
| 119 | for subject, source, evidence_class in (("git", "trusted_exact_task_worktree;known_good_git_head_tree", "independent_committed_tree"),
|
| 120 | ("oak", "trusted_exact_task_worktree;subject_produced_oak_head_export", "subject_produced_committed_tree"))]
|
| 121 | self.assertTrue(workflow_evidence_failures(rows))
|
| 122 | rows[1]["workflow_integrity_source"] = "another independently validated reader"
|
| 123 | rows[1]["workflow_integrity_evidence_class"] = "independent_committed_tree"
|
| 124 | self.assertEqual(workflow_evidence_failures(rows), [])
|
| 125 | rows[1]["workflow_integrity_passed"] = None
|
| 126 | self.assertTrue(workflow_evidence_failures(rows))
|
| 127 | rows[1].pop("workflow_integrity_source")
|
| 128 | self.assertTrue(workflow_evidence_failures(rows))
|
| 129 |
|
| 130 | def test_skipped_workflow_instrument_cannot_disappear_from_public_comparison(self):
|
| 131 | rows = [{"subject": subject, "scenario": "wide_config_refactor", "operation": "agent.workflow",
|
| 132 | "workflow_integrity_source": "trusted_exact_task_worktree;known_good_git_head_tree",
|
| 133 | "workflow_integrity_evidence_class": "independent_committed_tree", "oracle_version": WORKFLOW_ORACLE_VERSION,
|
| 134 | "workflow_integrity_passed": True, "returncode": 0,
|
| 135 | "agent": {"name": "agent", "model": "model", "provider": "provider", "environment": "minimal"}}
|
| 136 | for subject in ("git", "oak")]
|
| 137 | rows[0].update(returncode=77, skipped=True, skip_reason="git_head_instrument_unavailable",
|
| 138 | workflow_integrity_passed=None)
|
| 139 | self.assertTrue(workflow_evidence_failures(rows))
|
| 140 | with tempfile.TemporaryDirectory() as temporary:
|
| 141 | path = Path(temporary) / "rows.jsonl"
|
| 142 | path.write_text("\n".join(json.dumps(row) for row in rows))
|
| 143 | output = io.StringIO()
|
| 144 | with contextlib.redirect_stdout(output):
|
| 145 | self.assertEqual(publish_gate.main([str(path)]), 1)
|
| 146 | self.assertIn("FAIL workflow-evidence", output.getvalue())
|
| 147 | rows[1]["agent"] = {**rows[1]["agent"], "model": "different"}
|
| 148 | self.assertTrue(workflow_evidence_failures(rows))
|
| 149 |
|
| 150 | def test_bugfix_real_producer_labels_retain_existing_publication_policy(self):
|
| 151 | rows = [{"subject": subject, "subject_kind": kind, "scenario": "bugfix_test_loop",
|
| 152 | "operation": "workflow.total", "profile": "workflow", "returncode": 0,
|
| 153 | "workflow_integrity_passed": True, "workflow_integrity_source": source,
|
| 154 | "oracle_version": WORKFLOW_ORACLE_VERSION,
|
| 155 | "source": {"oak_hash": "reviewed-revision", "oak_status": "clean"}}
|
| 156 | for subject, kind, source in (
|
| 157 | ("git", "git", "worktree_payload_oracle;known_good_git_head_tree"),
|
| 158 | ("oak", "oak", "worktree_payload_oracle;direct_repo_metadata_fingerprint"))]
|
| 159 | self.assertEqual(workflow_evidence_failures(rows), [])
|
| 160 | with tempfile.TemporaryDirectory() as temporary:
|
| 161 | path = Path(temporary) / "rows.jsonl"
|
| 162 | path.write_text("\n".join(json.dumps(row) for row in rows))
|
| 163 | output = io.StringIO()
|
| 164 | with contextlib.redirect_stdout(output):
|
| 165 | code = publish_gate.main([str(path)])
|
| 166 | self.assertEqual(code, 0, output.getvalue())
|
| 167 | self.assertNotIn("FAIL workflow-evidence", output.getvalue())
|
| 168 |
|
| 169 | def test_trusted_workflows_require_measured_independent_same_version_evidence(self):
|
| 170 | for scenario in ("wide_config_refactor", "large_asset_manifest"):
|
| 171 | rows = [{"subject": subject, "scenario": scenario, "operation": "workflow.total",
|
| 172 | "oracle_version": WORKFLOW_ORACLE_VERSION, "workflow_integrity_passed": True, "returncode": 0,
|
| 173 | "workflow_integrity_evidence_class": "independent_committed_tree",
|
| 174 | "workflow_integrity_source": source, "workflow_subject_binary_sha256": "b" * 64, "git_head_instrument_sha256": "a" * 64}
|
| 175 | for subject, source in (("git", "trusted_exact_task_worktree;known_good_git_head_tree"),
|
| 176 | ("git_candidate", "trusted_exact_task_worktree;reviewed_external_reader"))]
|
| 177 | self.assertEqual(workflow_evidence_failures(rows), [])
|
| 178 | for change in ({"git_head_instrument_sha256": "b" * 64}, {"workflow_subject_binary_sha256": None}, {"oracle_version": "other"}, {"oracle_version": None},
|
| 179 | {"workflow_integrity_evidence_class": None},
|
| 180 | {"workflow_integrity_source": "trusted_exact_task_worktree;subject_produced_oak_head_export"},
|
| 181 | {"workflow_integrity_evidence_class": "subject_produced_committed_tree"},
|
| 182 | {"workflow_integrity_passed": None}, {"workflow_integrity_passed": False}):
|
| 183 | with self.subTest(scenario=scenario, change=change):
|
| 184 | self.assertTrue(workflow_evidence_failures([rows[0], {**rows[1], **change}]))
|
| 185 | rows[1].update(subject="oak", workflow_integrity_source="trusted_exact_task_worktree;subject_produced_oak_head_export",
|
| 186 | workflow_integrity_evidence_class="subject_produced_committed_tree")
|
| 187 | with tempfile.TemporaryDirectory() as temporary:
|
| 188 | path = Path(temporary) / "rows.jsonl"
|
| 189 | path.write_text("\n".join(json.dumps(row) for row in rows))
|
| 190 | output = io.StringIO()
|
| 191 | with contextlib.redirect_stdout(output):
|
| 192 | self.assertEqual(publish_gate.main([str(path)]), 1)
|
| 193 | self.assertIn("FAIL workflow-evidence", output.getvalue())
|
| 194 |
|
| 195 | def test_publication_entrypoint_enforces_evidence(self):
|
| 196 | rows = []
|
| 197 | for subject in ("git", "git_untracked_cache", "git_fsmonitor"):
|
| 198 | rows.append({
|
| 199 | "profile": "standard", "subject": subject, "scenario": "tiny_text",
|
| 200 | "operation": "diff.full.inforecall", "returncode": 0,
|
| 201 | "env_isolation_version": "1", "randomized_subject_order": True,
|
| 202 | "subject_order": ["git", "git_untracked_cache", "git_fsmonitor"],
|
| 203 | "benchmark_track": "core-equivalent", "bench_id": "gate-test", "run": 0,
|
| 204 | **exact_output(),
|
| 205 | })
|
| 206 | measured = [
|
| 207 | {key: value for key, value in row.items() if key not in exact_output()}
|
| 208 | for row in rows
|
| 209 | ]
|
| 210 | rows.extend({**row, "operation": "diff.dirty.inforecall",
|
| 211 | **{key: value for key, value in exact_output().items() if not key.startswith("patch_")}}
|
| 212 | for row in measured)
|
| 213 | rows.extend(
|
| 214 | {**row, "operation": "status.clean", "run": run}
|
| 215 | for row in measured for run in range(30)
|
| 216 | )
|
| 217 | with tempfile.TemporaryDirectory() as temporary:
|
| 218 | path = Path(temporary) / "rows.jsonl"
|
| 219 | path.write_text("\n".join(json.dumps(row) for row in rows))
|
| 220 | with contextlib.redirect_stdout(io.StringIO()):
|
| 221 | self.assertEqual(publish_gate.main([str(path)]), 0)
|
| 222 | rows[0]["patch_apply_ok"] = None
|
| 223 | rows[0]["patch_skip_reason"] = "git_unavailable"
|
| 224 | path.write_text("\n".join(json.dumps(row) for row in rows))
|
| 225 | output = io.StringIO()
|
| 226 | with contextlib.redirect_stdout(output):
|
| 227 | self.assertEqual(publish_gate.main([str(path)]), 1)
|
| 228 | self.assertIn("FAIL output-evidence", output.getvalue())
|
| 229 |
|
| 230 |
|
| 231 | if __name__ == "__main__":
|
| 232 | unittest.main()
|