|
| 1 | +"""Adversarial tests for the pipeline gates: can a run skip user confirmation? |
| 2 | +
|
| 3 | +These encode the attacks a careless operator (or an over-eager agent) would try, |
| 4 | +so a future refactor cannot quietly turn the confirmation gate into a no-op. |
| 5 | +
|
| 6 | +Offline only: every case here fails *before* edge-tts / ffmpeg / hyperframes is |
| 7 | +invoked, because the marker checks run first. |
| 8 | +
|
| 9 | +Known limitation (by design, not covered here): `.confirmed` / `.prose_pass` are |
| 10 | +plain JSON files under `work/` bound to the draft's sha256, so a process running |
| 11 | +as the same user can forge them. The gate therefore assumes the *human* really |
| 12 | +did confirm outside the agent; it protects against accidents and drift, not |
| 13 | +against a determined same-user forgery. |
| 14 | +""" |
| 15 | +import json |
| 16 | +import subprocess |
| 17 | +import sys |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | +REPO = Path(__file__).resolve().parents[1] |
| 21 | +CLEAN_FIXTURE = Path(__file__).resolve().parent / "fixtures" / "clean_prose.md" |
| 22 | + |
| 23 | + |
| 24 | +def run_runner(project_dir: Path, *extra: str) -> subprocess.CompletedProcess: |
| 25 | + return subprocess.run( |
| 26 | + [sys.executable, "-X", "utf8", "-m", "pipeline.runner", |
| 27 | + "--project-dir", str(project_dir), *extra], |
| 28 | + cwd=REPO, capture_output=True, text=True, encoding="utf-8", errors="replace", |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +def fresh_project(tmp_path: Path, name: str) -> Path: |
| 33 | + import shutil |
| 34 | + p = tmp_path / name |
| 35 | + if p.exists(): |
| 36 | + shutil.rmtree(p) |
| 37 | + (p / "work").mkdir(parents=True) |
| 38 | + return p |
| 39 | + |
| 40 | + |
| 41 | +def steps_1_2(project_dir: Path) -> subprocess.CompletedProcess: |
| 42 | + return run_runner(project_dir, "--steps", "1,2", "--script", str(CLEAN_FIXTURE)) |
| 43 | + |
| 44 | + |
| 45 | +class TestGateBypassAttempts: |
| 46 | + def test_step_04_alone_is_blocked(self, tmp_path): |
| 47 | + p = fresh_project(tmp_path, "a") |
| 48 | + r = run_runner(p, "--steps", "4") |
| 49 | + assert r.returncode != 0 |
| 50 | + assert "confirm" in (r.stdout + r.stderr).lower() |
| 51 | + assert not (p / "work" / ".confirmed").exists() |
| 52 | + |
| 53 | + def test_step_05_alone_is_blocked(self, tmp_path): |
| 54 | + p = fresh_project(tmp_path, "b") |
| 55 | + r = run_runner(p, "--steps", "5") |
| 56 | + assert r.returncode != 0 |
| 57 | + assert "confirm" in (r.stdout + r.stderr).lower() |
| 58 | + |
| 59 | + def test_skipping_step_03_with_confirm_still_blocks_step_04(self, tmp_path): |
| 60 | + """--confirm on the runner must not substitute for running step 03.""" |
| 61 | + p = fresh_project(tmp_path, "c") |
| 62 | + r = run_runner(p, "--steps", "1,2,4", "--confirm", "--script", str(CLEAN_FIXTURE)) |
| 63 | + assert r.returncode != 0 |
| 64 | + combined = (r.stdout + r.stderr).lower() |
| 65 | + assert "confirm" in combined |
| 66 | + # steps 1-2 still did their job; only the gate stopped the run |
| 67 | + assert (p / "work" / "draft.md").exists() |
| 68 | + assert (p / "work" / ".prose_pass").exists() |
| 69 | + assert not (p / "work" / ".confirmed").exists() |
| 70 | + |
| 71 | + def test_step_03_without_a_draft_fails(self, tmp_path): |
| 72 | + p = fresh_project(tmp_path, "d") |
| 73 | + r = run_runner(p, "--steps", "3", "--confirm") |
| 74 | + assert r.returncode != 0 |
| 75 | + assert "draft" in (r.stdout + r.stderr).lower() |
| 76 | + |
| 77 | + def test_step_03_failure_stops_the_run_before_step_04(self, tmp_path): |
| 78 | + p = fresh_project(tmp_path, "e") |
| 79 | + r = run_runner(p, "--steps", "3,4", "--confirm") |
| 80 | + assert r.returncode != 0 |
| 81 | + assert "[03]" in r.stdout |
| 82 | + assert "[04]" not in r.stdout |
| 83 | + |
| 84 | + def test_stale_confirmation_blocks_step_04(self, tmp_path): |
| 85 | + p = fresh_project(tmp_path, "f") |
| 86 | + assert steps_1_2(p).returncode == 0 |
| 87 | + assert run_runner(p, "--steps", "3", "--confirm").returncode == 0 |
| 88 | + draft = p / "work" / "draft.md" |
| 89 | + draft.write_text(draft.read_text(encoding="utf-8") + "\n未确认的新增内容。\n", encoding="utf-8") |
| 90 | + |
| 91 | + r = run_runner(p, "--steps", "4") |
| 92 | + assert r.returncode != 0 |
| 93 | + assert "stale" in (r.stdout + r.stderr).lower() |
| 94 | + |
| 95 | + def test_stale_prose_marker_blocks_step_03(self, tmp_path): |
| 96 | + """Editing the draft after the prose gate must force step 02 again.""" |
| 97 | + p = fresh_project(tmp_path, "g") |
| 98 | + assert steps_1_2(p).returncode == 0 |
| 99 | + draft = p / "work" / "draft.md" |
| 100 | + draft.write_text(draft.read_text(encoding="utf-8") + "\n未过门禁的新增内容。\n", encoding="utf-8") |
| 101 | + |
| 102 | + r = run_runner(p, "--steps", "3", "--confirm") |
| 103 | + assert r.returncode != 0 |
| 104 | + assert "prose" in (r.stdout + r.stderr).lower() or "门禁" in (r.stdout + r.stderr) |
| 105 | + assert not (p / "work" / ".confirmed").exists() |
| 106 | + |
| 107 | + def test_corrupt_run_state_does_not_affect_any_gate(self, tmp_path): |
| 108 | + p = fresh_project(tmp_path, "h") |
| 109 | + assert steps_1_2(p).returncode == 0 |
| 110 | + (p / "work" / "pipeline_run.json").write_text("{ not json", encoding="utf-8") |
| 111 | + |
| 112 | + r = run_runner(p, "--steps", "3") |
| 113 | + assert r.returncode != 0 |
| 114 | + assert "confirm" in (r.stdout + r.stderr).lower() |
| 115 | + |
| 116 | + r = run_runner(p, "--steps", "3", "--confirm") |
| 117 | + assert r.returncode == 0 |
| 118 | + state = json.loads((p / "work" / "pipeline_run.json").read_text(encoding="utf-8")) |
| 119 | + assert state["result"] == "complete" |
0 commit comments