|
1 | 1 | import json |
2 | 2 | import os |
| 3 | +import shlex |
3 | 4 | import sqlite3 |
4 | 5 | import subprocess |
5 | 6 | import sys |
@@ -241,6 +242,79 @@ def _init_verify_target_with_head(target): |
241 | 242 | _init_git_repo_with_head(target) |
242 | 243 |
|
243 | 244 |
|
| 245 | +@pytest.mark.skipif(os.name != "posix", reason="requires POSIX shell env-prefix invocation") |
| 246 | +def test_verify_reused_receipt_stamps_harness_session_from_outer_env_prefix(tmp_target, monkeypatch): |
| 247 | + """Regression #541: cache-hit receipts must stamp outer BRIGADE_CLAUDE_SESSION.""" |
| 248 | + from brigade.claude_hooks.runtime import _session_fingerprint |
| 249 | + from brigade.work_cmd import verification |
| 250 | + |
| 251 | + _init_verify_target_with_head(tmp_target) |
| 252 | + graphtrail_bin = str(tmp_target / "missing-graphtrail") |
| 253 | + monkeypatch.setenv("GRAPHTRAIL_BIN", graphtrail_bin) |
| 254 | + |
| 255 | + fingerprint_a = _session_fingerprint("session-a-outer-prefix") |
| 256 | + fingerprint_b = _session_fingerprint("session-b-outer-prefix") |
| 257 | + verify_command = "true" |
| 258 | + target = str(tmp_target) |
| 259 | + brigade_cli = ( |
| 260 | + f"{shlex.quote(sys.executable)} -m brigade work verify run " |
| 261 | + f"--target {shlex.quote(target)} --command {shlex.quote(verify_command)}" |
| 262 | + ) |
| 263 | + subprocess_env = {**os.environ, "GRAPHTRAIL_BIN": graphtrail_bin} |
| 264 | + |
| 265 | + case_a = subprocess.run( |
| 266 | + ["/bin/sh", "-c", f"BRIGADE_CLAUDE_SESSION={fingerprint_a} {brigade_cli}"], |
| 267 | + cwd=tmp_target, |
| 268 | + env=subprocess_env, |
| 269 | + check=False, |
| 270 | + capture_output=True, |
| 271 | + text=True, |
| 272 | + ) |
| 273 | + assert case_a.returncode == 0, case_a.stderr |
| 274 | + |
| 275 | + case_b = subprocess.run( |
| 276 | + [ |
| 277 | + "/bin/sh", |
| 278 | + "-c", |
| 279 | + f"BRIGADE_CLAUDE_SESSION={fingerprint_b} PY=/fake/path {brigade_cli}", |
| 280 | + ], |
| 281 | + cwd=tmp_target, |
| 282 | + env=subprocess_env, |
| 283 | + check=False, |
| 284 | + capture_output=True, |
| 285 | + text=True, |
| 286 | + ) |
| 287 | + assert case_b.returncode == 0, case_b.stderr |
| 288 | + |
| 289 | + receipts = verification._verify_receipts(tmp_target) |
| 290 | + assert len(receipts) == 2 |
| 291 | + reused = receipts[0] |
| 292 | + fresh = receipts[1] |
| 293 | + assert reused["reused_from"] == fresh["run_id"] |
| 294 | + assert fresh["harness_session"] == {"harness": "claude", "fingerprint": fingerprint_a} |
| 295 | + assert reused["harness_session"] == {"harness": "claude", "fingerprint": fingerprint_b} |
| 296 | + assert reused["planned_commands"] == [verify_command] |
| 297 | + |
| 298 | + |
| 299 | +def test_verify_reused_receipt_records_env_assignments_inside_command(tmp_target, monkeypatch): |
| 300 | + from brigade.work_cmd import verification |
| 301 | + |
| 302 | + _init_verify_target_with_head(tmp_target) |
| 303 | + monkeypatch.setenv("GRAPHTRAIL_BIN", str(tmp_target / "missing-graphtrail")) |
| 304 | + command = f'FOO=bar BAZ=qux {sys.executable} -c "print(1)"' |
| 305 | + |
| 306 | + assert verification.verify_run(target=tmp_target, commands=[command], timeout=60) == 0 |
| 307 | + assert verification.verify_run(target=tmp_target, commands=[command], timeout=60) == 0 |
| 308 | + |
| 309 | + receipts = verification._verify_receipts(tmp_target) |
| 310 | + assert len(receipts) == 2 |
| 311 | + reused = receipts[0] |
| 312 | + fresh = receipts[1] |
| 313 | + assert reused["reused_from"] == fresh["run_id"] |
| 314 | + assert reused["commands"][0]["env"] == ["BAZ", "FOO"] |
| 315 | + assert reused["planned_commands"] == [command] |
| 316 | + |
| 317 | + |
244 | 318 | def test_verify_reuses_identical_tree(tmp_target, monkeypatch): |
245 | 319 | from brigade.work_cmd import verification |
246 | 320 |
|
|
0 commit comments