Stevesolun commited on
Commit
310c534
·
verified ·
1 Parent(s): b900199

Sync ctx b670e1c (part 3)

Browse files

GitHub commit: b670e1c2a90ea6e7d3a49f2a2fed02f69d9b83f0

src/tests/test_ctx_ab_holdout_freeze.py CHANGED
@@ -55,6 +55,138 @@ def _sha256(data: bytes) -> str:
55
  return hashlib.sha256(data).hexdigest()
56
 
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  def test_committed_v1_protocol_checkout_preserves_authenticated_bytes() -> None:
59
  relative_path = freezer.V1_PROTOCOL_PATH.relative_to(freezer.ROOT).as_posix()
60
  attributes = (freezer.ROOT / ".gitattributes").read_text(encoding="utf-8").splitlines()
 
55
  return hashlib.sha256(data).hexdigest()
56
 
57
 
58
+ def _literal_tree_bundle(
59
+ tmp_path: Path,
60
+ *,
61
+ directory: str,
62
+ mode: bytes,
63
+ name: bytes,
64
+ ) -> tuple[Path, str, str, Path]:
65
+ source = tmp_path / directory
66
+ source.mkdir()
67
+ subprocess.run(["git", "init", "-q", "--initial-branch=main"], cwd=source, check=True)
68
+ subprocess.run(
69
+ ["git", "config", "user.email", "ctx@example.test"],
70
+ cwd=source,
71
+ check=True,
72
+ )
73
+ subprocess.run(
74
+ ["git", "config", "user.name", "ctx benchmark"],
75
+ cwd=source,
76
+ check=True,
77
+ )
78
+ blob = (
79
+ subprocess.run(
80
+ ["git", "hash-object", "-w", "--stdin"],
81
+ cwd=source,
82
+ input=b"literal tree fixture\n",
83
+ check=True,
84
+ capture_output=True,
85
+ )
86
+ .stdout.decode()
87
+ .strip()
88
+ )
89
+ tree = (
90
+ subprocess.run(
91
+ ["git", "hash-object", "-t", "tree", "--literally", "-w", "--stdin"],
92
+ cwd=source,
93
+ input=mode + b" " + name + b"\0" + bytes.fromhex(blob),
94
+ check=True,
95
+ capture_output=True,
96
+ )
97
+ .stdout.decode()
98
+ .strip()
99
+ )
100
+ commit = (
101
+ subprocess.run(
102
+ ["git", "commit-tree", tree],
103
+ cwd=source,
104
+ input=b"literal tree fixture\n",
105
+ check=True,
106
+ capture_output=True,
107
+ )
108
+ .stdout.decode()
109
+ .strip()
110
+ )
111
+ subprocess.run(
112
+ ["git", "update-ref", "refs/heads/base", commit],
113
+ cwd=source,
114
+ check=True,
115
+ )
116
+ bundle = tmp_path / f"{directory}.bundle"
117
+ subprocess.run(
118
+ ["git", "bundle", "create", str(bundle), "refs/heads/base"],
119
+ cwd=source,
120
+ check=True,
121
+ )
122
+ bundle.chmod(0o600)
123
+ return source, commit, tree, bundle
124
+
125
+
126
+ def test_source_bundle_closure_accepts_legacy_zero_padded_tree_mode(tmp_path: Path) -> None:
127
+ source, commit, tree, bundle = _literal_tree_bundle(
128
+ tmp_path,
129
+ directory="legacy-source",
130
+ mode=b"0100644",
131
+ name=b"source.py",
132
+ )
133
+ strict = subprocess.run(
134
+ ["git", "fsck", "--full", "--strict", "--unreachable", "--no-reflogs"],
135
+ cwd=source,
136
+ check=False,
137
+ capture_output=True,
138
+ text=True,
139
+ )
140
+ assert strict.returncode != 0
141
+ assert "zeroPaddedFilemode" in strict.stderr
142
+
143
+ freezer._validate_source_bundle_closure(
144
+ freezer.SourceBundle(
145
+ base_commit=commit,
146
+ bundle_path=bundle,
147
+ bundle_sha256=_sha256(bundle.read_bytes()),
148
+ tree_sha1=tree,
149
+ )
150
+ )
151
+
152
+
153
+ def test_source_bundle_closure_rejects_unrelated_strict_fsck_error(tmp_path: Path) -> None:
154
+ source, commit, tree, bundle = _literal_tree_bundle(
155
+ tmp_path,
156
+ directory="invalid-source",
157
+ mode=b"120000",
158
+ name=b".gitmodules",
159
+ )
160
+ scoped = subprocess.run(
161
+ [
162
+ "git",
163
+ "-c",
164
+ "fsck.zeroPaddedFilemode=ignore",
165
+ "fsck",
166
+ "--full",
167
+ "--strict",
168
+ "--unreachable",
169
+ "--no-reflogs",
170
+ ],
171
+ cwd=source,
172
+ check=False,
173
+ capture_output=True,
174
+ text=True,
175
+ )
176
+ assert scoped.returncode != 0
177
+ assert "gitmodulesSymlink" in scoped.stderr
178
+
179
+ with pytest.raises(freezer.FreezeError, match="closure validation failed"):
180
+ freezer._validate_source_bundle_closure(
181
+ freezer.SourceBundle(
182
+ base_commit=commit,
183
+ bundle_path=bundle,
184
+ bundle_sha256=_sha256(bundle.read_bytes()),
185
+ tree_sha1=tree,
186
+ )
187
+ )
188
+
189
+
190
  def test_committed_v1_protocol_checkout_preserves_authenticated_bytes() -> None:
191
  relative_path = freezer.V1_PROTOCOL_PATH.relative_to(freezer.ROOT).as_posix()
192
  attributes = (freezer.ROOT / ".gitattributes").read_text(encoding="utf-8").splitlines()
src/tests/test_ctx_ab_holdout_prepare.py CHANGED
@@ -828,6 +828,47 @@ def test_command_runner_requires_descendant_containment(
828
  assert observed["timeout"] == 5
829
 
830
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
831
  def _git(cwd: Path, *argv: str, check: bool = True) -> subprocess.CompletedProcess[str]:
832
  return subprocess.run(
833
  ["git", *argv],
@@ -838,6 +879,84 @@ def _git(cwd: Path, *argv: str, check: bool = True) -> subprocess.CompletedProce
838
  )
839
 
840
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
841
  def test_authenticated_bundle_excludes_future_gold_refs_objects_and_remotes(
842
  tmp_path: Path,
843
  monkeypatch: pytest.MonkeyPatch,
@@ -934,6 +1053,80 @@ def test_authenticated_bundle_excludes_future_gold_refs_objects_and_remotes(
934
  )
935
 
936
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
937
  def test_prepare_sources_bundles_in_bounded_parallel_and_writes_deterministic_map(
938
  tmp_path: Path,
939
  monkeypatch: pytest.MonkeyPatch,
@@ -1686,6 +1879,65 @@ def test_cli_suppresses_private_failure_details(
1686
  assert (failure_root / "artifact-manifest.json").is_file()
1687
 
1688
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1689
  def test_cli_rejects_used_failure_destination_before_private_work(
1690
  tmp_path: Path,
1691
  monkeypatch: pytest.MonkeyPatch,
 
828
  assert observed["timeout"] == 5
829
 
830
 
831
+ def test_command_runner_preserves_bounded_private_failure_diagnostics(
832
+ tmp_path: Path,
833
+ monkeypatch: pytest.MonkeyPatch,
834
+ ) -> None:
835
+ result = prepare.swebench.CommandResult(
836
+ 17,
837
+ "private stdout",
838
+ "private stderr",
839
+ 0.25,
840
+ reaped_descendants=2,
841
+ residual_descendants=(101, 202),
842
+ )
843
+ monkeypatch.setattr(prepare.swebench, "_run_process", lambda *_args, **_kwargs: result)
844
+
845
+ with pytest.raises(prepare.PrepareError) as raised:
846
+ prepare._command_bytes(["git", "fsck"], cwd=tmp_path, timeout=5)
847
+
848
+ prefix = "authenticated preparation command failed; private_result="
849
+ assert str(raised.value).startswith(prefix)
850
+ evidence = json.loads(str(raised.value).removeprefix(prefix))
851
+ assert evidence == {
852
+ "elapsed_seconds": 0.25,
853
+ "reaped_descendants": 2,
854
+ "residual_descendants": [101, 202],
855
+ "returncode": 17,
856
+ "stderr": {
857
+ "bytes": 14,
858
+ "sha256": _sha256(b"private stderr"),
859
+ "text": "private stderr",
860
+ "truncated": False,
861
+ },
862
+ "stdout": {
863
+ "bytes": 14,
864
+ "sha256": _sha256(b"private stdout"),
865
+ "text": "private stdout",
866
+ "truncated": False,
867
+ },
868
+ "timed_out": False,
869
+ }
870
+
871
+
872
  def _git(cwd: Path, *argv: str, check: bool = True) -> subprocess.CompletedProcess[str]:
873
  return subprocess.run(
874
  ["git", *argv],
 
879
  )
880
 
881
 
882
+ def _literal_tree_source(
883
+ tmp_path: Path,
884
+ *,
885
+ directory: str,
886
+ mode: bytes,
887
+ name: bytes,
888
+ ) -> tuple[Path, str, str]:
889
+ source = tmp_path / directory
890
+ source.mkdir()
891
+ _git(source, "init", "--quiet", "--initial-branch=main")
892
+ _git(source, "config", "user.name", "Fixture")
893
+ _git(source, "config", "user.email", "fixture@example.com")
894
+ blob = (
895
+ subprocess.run(
896
+ ["git", "hash-object", "-w", "--stdin"],
897
+ cwd=source,
898
+ input=b"literal tree fixture\n",
899
+ check=True,
900
+ capture_output=True,
901
+ )
902
+ .stdout.decode()
903
+ .strip()
904
+ )
905
+ tree = (
906
+ subprocess.run(
907
+ ["git", "hash-object", "-t", "tree", "--literally", "-w", "--stdin"],
908
+ cwd=source,
909
+ input=mode + b" " + name + b"\0" + bytes.fromhex(blob),
910
+ check=True,
911
+ capture_output=True,
912
+ )
913
+ .stdout.decode()
914
+ .strip()
915
+ )
916
+ commit = (
917
+ subprocess.run(
918
+ ["git", "commit-tree", tree],
919
+ cwd=source,
920
+ input=b"literal tree fixture\n",
921
+ check=True,
922
+ capture_output=True,
923
+ )
924
+ .stdout.decode()
925
+ .strip()
926
+ )
927
+ _git(source, "update-ref", "refs/heads/main", commit)
928
+ return source, commit, tree
929
+
930
+
931
+ def _replace_github_fetch_with_local_source(
932
+ monkeypatch: pytest.MonkeyPatch,
933
+ *,
934
+ source: Path,
935
+ commit: str,
936
+ ) -> None:
937
+ original_command = prepare._command_bytes
938
+
939
+ def command(argv: list[str], **kwargs: Any) -> bytes:
940
+ if "fetch" in argv and "origin" in argv:
941
+ repository = argv[argv.index("-C") + 1]
942
+ argv = [
943
+ "git",
944
+ "-C",
945
+ repository,
946
+ "-c",
947
+ "protocol.file.allow=always",
948
+ "fetch",
949
+ "--quiet",
950
+ "--force",
951
+ "--no-tags",
952
+ source.as_uri(),
953
+ f"{commit}:refs/heads/base",
954
+ ]
955
+ return original_command(argv, **kwargs)
956
+
957
+ monkeypatch.setattr(prepare, "_command_bytes", command)
958
+
959
+
960
  def test_authenticated_bundle_excludes_future_gold_refs_objects_and_remotes(
961
  tmp_path: Path,
962
  monkeypatch: pytest.MonkeyPatch,
 
1053
  )
1054
 
1055
 
1056
+ def test_authenticated_bundle_accepts_legacy_zero_padded_tree_mode(
1057
+ tmp_path: Path,
1058
+ monkeypatch: pytest.MonkeyPatch,
1059
+ ) -> None:
1060
+ source, commit, tree = _literal_tree_source(
1061
+ tmp_path,
1062
+ directory="legacy-source",
1063
+ mode=b"0100644",
1064
+ name=b"legacy.py",
1065
+ )
1066
+ strict = _git(
1067
+ source,
1068
+ "fsck",
1069
+ "--full",
1070
+ "--strict",
1071
+ "--unreachable",
1072
+ "--no-reflogs",
1073
+ check=False,
1074
+ )
1075
+ assert strict.returncode != 0
1076
+ assert "zeroPaddedFilemode" in strict.stderr
1077
+
1078
+ destination = tmp_path / "bundles" / "base.bundle"
1079
+ destination.parent.mkdir()
1080
+ _replace_github_fetch_with_local_source(monkeypatch, source=source, commit=commit)
1081
+
1082
+ observed_tree, bundle_sha256 = prepare._create_authenticated_bundle(
1083
+ url="https://github.com/owner/legacy-repo.git",
1084
+ commit=commit,
1085
+ destination=destination,
1086
+ )
1087
+
1088
+ assert observed_tree == tree
1089
+ assert bundle_sha256 == _sha256(destination.read_bytes())
1090
+
1091
+
1092
+ def test_authenticated_bundle_rejects_unrelated_strict_fsck_error(
1093
+ tmp_path: Path,
1094
+ monkeypatch: pytest.MonkeyPatch,
1095
+ ) -> None:
1096
+ source, commit, _tree = _literal_tree_source(
1097
+ tmp_path,
1098
+ directory="invalid-source",
1099
+ mode=b"120000",
1100
+ name=b".gitmodules",
1101
+ )
1102
+ scoped = _git(
1103
+ source,
1104
+ "-c",
1105
+ "fsck.zeroPaddedFilemode=ignore",
1106
+ "fsck",
1107
+ "--full",
1108
+ "--strict",
1109
+ "--unreachable",
1110
+ "--no-reflogs",
1111
+ check=False,
1112
+ )
1113
+ assert scoped.returncode != 0
1114
+ assert "gitmodulesSymlink" in scoped.stderr
1115
+
1116
+ destination = tmp_path / "invalid-bundles" / "base.bundle"
1117
+ destination.parent.mkdir()
1118
+ _replace_github_fetch_with_local_source(monkeypatch, source=source, commit=commit)
1119
+
1120
+ with pytest.raises(prepare.PrepareError, match="authenticated preparation command failed"):
1121
+ prepare._create_authenticated_bundle(
1122
+ url="https://github.com/owner/invalid-repo.git",
1123
+ commit=commit,
1124
+ destination=destination,
1125
+ )
1126
+
1127
+ assert not destination.exists()
1128
+
1129
+
1130
  def test_prepare_sources_bundles_in_bounded_parallel_and_writes_deterministic_map(
1131
  tmp_path: Path,
1132
  monkeypatch: pytest.MonkeyPatch,
 
1879
  assert (failure_root / "artifact-manifest.json").is_file()
1880
 
1881
 
1882
+ def test_cli_privately_bounds_oversized_command_failure_details(
1883
+ tmp_path: Path,
1884
+ monkeypatch: pytest.MonkeyPatch,
1885
+ capsys: pytest.CaptureFixture[str],
1886
+ ) -> None:
1887
+ private_stdout = "private-task-id-" + "x" * prepare.PRIVATE_COMMAND_TEXT_LIMIT
1888
+ private_stderr = "private-task-error-" + "y" * prepare.PRIVATE_COMMAND_TEXT_LIMIT
1889
+ result = prepare.swebench.CommandResult(19, private_stdout, private_stderr, 0.5)
1890
+ monkeypatch.setattr(prepare.swebench, "_run_process", lambda *_args, **_kwargs: result)
1891
+
1892
+ def fail(**_kwargs: Any) -> str:
1893
+ prepare._command_bytes(["git", "fsck"], cwd=tmp_path, timeout=5)
1894
+ raise AssertionError("unreachable")
1895
+
1896
+ monkeypatch.setattr(prepare, "prepare_sources", fail)
1897
+ failure_root = tmp_path / "oversized-command-failure"
1898
+
1899
+ with pytest.raises(SystemExit) as raised:
1900
+ prepare.main(
1901
+ [
1902
+ "sources",
1903
+ "--protocol",
1904
+ str(tmp_path / "protocol"),
1905
+ "--expected-acquisition-protocol-sha256",
1906
+ "0" * 64,
1907
+ "--exposure-ledger",
1908
+ str(tmp_path / "exposure"),
1909
+ "--rows",
1910
+ str(tmp_path / "rows"),
1911
+ "--selection",
1912
+ str(tmp_path / "selection"),
1913
+ "--cache-root",
1914
+ str(tmp_path / "cache"),
1915
+ "--output",
1916
+ str(tmp_path / "map"),
1917
+ "--failure-evidence-output",
1918
+ str(failure_root),
1919
+ ]
1920
+ )
1921
+
1922
+ captured = capsys.readouterr()
1923
+ assert raised.value.code == 2
1924
+ assert captured.out == ""
1925
+ assert captured.err == "benchmark preparation failed (PrepareError); evidence=preserved\n"
1926
+ assert "private-task" not in captured.err
1927
+ failure = json.loads((failure_root / "failure.json").read_text(encoding="utf-8"))
1928
+ message = failure["exception_chain"][0]["message"]
1929
+ prefix = "authenticated preparation command failed; private_result="
1930
+ evidence = json.loads(message.removeprefix(prefix))
1931
+ for stream, raw in (("stdout", private_stdout), ("stderr", private_stderr)):
1932
+ stream_evidence = evidence[stream]
1933
+ encoded = raw.encode()
1934
+ assert stream_evidence["bytes"] == len(encoded)
1935
+ assert stream_evidence["sha256"] == _sha256(encoded)
1936
+ assert len(stream_evidence["text"].encode()) == prepare.PRIVATE_COMMAND_TEXT_LIMIT
1937
+ assert stream_evidence["truncated"] is True
1938
+ assert (failure_root / "artifact-manifest.json").is_file()
1939
+
1940
+
1941
  def test_cli_rejects_used_failure_destination_before_private_work(
1942
  tmp_path: Path,
1943
  monkeypatch: pytest.MonkeyPatch,