test_gsb_same_day_collision_cycle.py
file-level
1
files
1
commits
0
hotspots
0
🧊 dead
0
💥 blast risk
| 1 | """E2E: same-day-collision ``--write`` on ALL three regimes (§GSB.8 e2e). |
| 2 | |
| 3 | The frozen coverage-gap close for the live 2026-07-31 PLS land-b defect: |
| 4 | the first ``--write`` creates the dated sync branch + commit; a second |
| 5 | ``--write`` on the same calendar day — after the original branch's tip |
| 6 | advanced (post-land main) — must succeed without exit ``2``. Under |
| 7 | ``muse+git-mirror`` the Git worktree must never be left in a state that |
| 8 | refuses checkout after the Muse ensure; ``main`` stays untouched; |
| 9 | ``muse-only`` runs zero git argv and ``git-only`` zero muse argv. |
| 10 | """ |
| 11 | |
| 12 | from __future__ import annotations |
| 13 | |
| 14 | from datetime import date |
| 15 | from pathlib import Path |
| 16 | |
| 17 | from cli.kit_root import kit_root |
| 18 | from tests.support import gsw_runner, run_cli, seed_gsw_repo |
| 19 | |
| 20 | |
| 21 | def _feature_branch() -> str: |
| 22 | return f"feat/governance-sync-{date.today().isoformat()}" |
| 23 | |
| 24 | |
| 25 | def test_git_only_same_day_second_write_succeeds(tmp_path: Path) -> None: |
| 26 | branch = _feature_branch() |
| 27 | handover_path, _ = seed_gsw_repo(tmp_path, "git-only") |
| 28 | runner = gsw_runner(tmp_path, "git-only") |
| 29 | |
| 30 | code = run_cli(["governance-sync", "--write"], cwd=tmp_path, runner=runner, kit=kit_root()) |
| 31 | assert code == 0 |
| 32 | assert runner.git_branch == branch |
| 33 | first_tip = runner.git_tips[branch] |
| 34 | |
| 35 | # Simulate the land + post-land posture: the sync branch merged, main |
| 36 | # advanced past it, operator back on main; GitHub main moved (D1 drift). |
| 37 | runner.git_branch = "main" |
| 38 | runner.git_tips["main"] = "postland" |
| 39 | runner.git_ancestors["postland"] = {first_tip} | runner.git_ancestors.get( |
| 40 | first_tip, set() |
| 41 | ) |
| 42 | runner.worktree = runner._content("postland") |
| 43 | runner.origin_main_tip = "postland" |
| 44 | |
| 45 | code = run_cli(["governance-sync", "--write"], cwd=tmp_path, runner=runner, kit=kit_root()) |
| 46 | assert code == 0, "same-day second --write must not exit 2" |
| 47 | assert runner.git_branch == branch |
| 48 | # FF then exactly one new sync commit on top of the post-land tip. |
| 49 | second_tip = runner.git_tips[branch] |
| 50 | assert second_tip != first_tip |
| 51 | assert "postland" in runner.git_ancestors[second_tip] |
| 52 | # main untouched by the reconcile. |
| 53 | assert runner.git_tips["main"] == "postland" |
| 54 | assert "postland" in handover_path.read_text(encoding="utf-8") |
| 55 | push_calls = [c for c, _ in runner.calls if c.startswith("git push")] |
| 56 | assert push_calls and all(branch in c for c in push_calls) |
| 57 | assert not any(c.startswith("muse") for c, _ in runner.calls) |
| 58 | assert not any("--force" in c for c, _ in runner.calls) |
| 59 | |
| 60 | |
| 61 | def test_muse_only_same_day_second_write_zero_git_argv(tmp_path: Path) -> None: |
| 62 | branch = _feature_branch() |
| 63 | seed_gsw_repo(tmp_path, "muse-only") |
| 64 | runner = gsw_runner(tmp_path, "muse-only") |
| 65 | |
| 66 | code = run_cli(["governance-sync", "--write"], cwd=tmp_path, runner=runner, kit=kit_root()) |
| 67 | assert code == 0 |
| 68 | assert runner.muse_branch == branch |
| 69 | first_tip = runner.muse_tips[branch] |
| 70 | |
| 71 | # Post-land: muse main advanced past the day-1 sync commit; operator back |
| 72 | # on main; fresh D2 drift drives the second apply. |
| 73 | runner.muse_branch = "main" |
| 74 | runner.muse_tips["main"] = "sha256:postland" |
| 75 | runner.muse_ancestors["sha256:postland"] = {first_tip} | runner.muse_ancestors.get( |
| 76 | first_tip, set() |
| 77 | ) |
| 78 | runner.worktree = runner._content("sha256:postland") |
| 79 | runner.muse_rev_parse_main_values = ["sha256:anchor2", "sha256:moved2"] |
| 80 | |
| 81 | code = run_cli(["governance-sync", "--write"], cwd=tmp_path, runner=runner, kit=kit_root()) |
| 82 | assert code == 0, "same-day second --write must not exit 2" |
| 83 | assert runner.muse_branch == branch |
| 84 | second_tip = runner.muse_tips[branch] |
| 85 | assert second_tip != first_tip |
| 86 | assert "sha256:postland" in runner.muse_ancestors[second_tip] |
| 87 | assert runner.muse_tips["main"] == "sha256:postland" |
| 88 | # §GSB.8 least privilege across BOTH runs: zero git/gh argv. |
| 89 | assert not any(c.startswith(("git ", "gh ")) for c, _ in runner.calls) |
| 90 | assert not any("--force" in c for c, _ in runner.calls) |
| 91 | |
| 92 | |
| 93 | def test_muse_git_mirror_same_day_second_write_never_refuses_checkout( |
| 94 | tmp_path: Path, |
| 95 | ) -> None: |
| 96 | """The live defect class: after the first sync, the dated branch tips are |
| 97 | stale day-1 content on both histories. The second same-day ``--write`` |
| 98 | must reconcile them so the Muse ensure never rewrites the shared worktree |
| 99 | to a stale tip that makes the Git checkout refuse.""" |
| 100 | branch = _feature_branch() |
| 101 | seed_gsw_repo(tmp_path, "muse+git-mirror") |
| 102 | runner = gsw_runner(tmp_path, "muse+git-mirror") |
| 103 | |
| 104 | code = run_cli(["governance-sync", "--write"], cwd=tmp_path, runner=runner, kit=kit_root()) |
| 105 | assert code == 0 |
| 106 | assert runner.git_branch == branch and runner.muse_branch == branch |
| 107 | muse_tip_one = runner.muse_tips[branch] |
| 108 | git_tip_one = runner.git_tips[branch] |
| 109 | |
| 110 | # Post-land: both mains advanced past the day-1 sync; operator back on |
| 111 | # main on both histories; bridge anchor follows muse main (D2 aligned); |
| 112 | # GitHub main moved (D1 drift drives the apply). The day-1 tips hold |
| 113 | # distinct content from the post-land tree — the exact live shape whose |
| 114 | # stale checkout dirtied the Git tree. |
| 115 | runner.git_branch = "main" |
| 116 | runner.muse_branch = "main" |
| 117 | runner.git_tips["main"] = "postland" |
| 118 | runner.muse_tips["main"] = "sha256:postland" |
| 119 | runner.git_ancestors["postland"] = {git_tip_one} | runner.git_ancestors.get( |
| 120 | git_tip_one, set() |
| 121 | ) |
| 122 | runner.muse_ancestors["sha256:postland"] = {muse_tip_one} | runner.muse_ancestors.get( |
| 123 | muse_tip_one, set() |
| 124 | ) |
| 125 | runner.origin_main_tip = "postland" |
| 126 | runner.content_map[muse_tip_one] = "content:day1-muse" |
| 127 | runner.content_map[git_tip_one] = "content:day1-git" |
| 128 | runner.worktree = runner._content("postland") |
| 129 | (tmp_path / ".muse" / "git-bridge.toml").write_text( |
| 130 | f'[last_export]\nmuse_commit_id = "sha256:postland"\ngit_sha = "{"1" * 40}"\n', |
| 131 | encoding="utf-8", |
| 132 | ) |
| 133 | |
| 134 | code = run_cli(["governance-sync", "--write"], cwd=tmp_path, runner=runner, kit=kit_root()) |
| 135 | assert code == 0, "same-day second --write must not exit 2 (live defect class)" |
| 136 | # Dual-HEAD success posture on the reconciled dated branch. |
| 137 | assert runner.git_branch == branch |
| 138 | assert runner.muse_branch == branch |
| 139 | # The Git checkout was never refused: reconcile put the tips at the |
| 140 | # post-land targets before any checkout of the dated name. |
| 141 | refused = [ |
| 142 | c for c, _ in runner.calls if "would be overwritten by checkout" in c |
| 143 | ] |
| 144 | assert refused == [] |
| 145 | second_muse_tip = runner.muse_tips[branch] |
| 146 | assert second_muse_tip != muse_tip_one |
| 147 | assert "sha256:postland" in runner.muse_ancestors[second_muse_tip] |
| 148 | # main untouched on both histories. |
| 149 | assert runner.git_tips["main"] == "postland" |
| 150 | assert runner.muse_tips["main"] == "sha256:postland" |
| 151 | push_calls = [c for c, _ in runner.calls if c.startswith("git push")] |
| 152 | assert push_calls and all(branch in c for c in push_calls) |
| 153 | assert not any(c.rstrip().endswith(" main") for c in push_calls) |
| 154 | assert not any("--force" in c for c, _ in runner.calls) |