test_phase3_merge_state_ordering.py
python
sha256:c06a9b9b9fee26c68ea725b44d54b2c0a171301ce9de746d5b656617b4463a9a
fix: repair four test failures from post-migration audit
Sonnet 4.6
patch
28 days ago
| 1 | """Phase 3 — MERGE_STATE ordering: apply_manifest before write_merge_state. |
| 2 | |
| 3 | Invariant: in the merge conflict path, the working tree must be updated |
| 4 | (apply_manifest) BEFORE the MERGE_STATE.json is written. |
| 5 | |
| 6 | Rationale: |
| 7 | If a crash occurs between write_merge_state and apply_manifest: |
| 8 | - MERGE_STATE.json exists (claims a conflict is in progress) |
| 9 | - Working tree is still at 'ours' (clean, not showing any conflict) |
| 10 | - 'muse conflicts' lists conflict paths but the files look clean |
| 11 | - The user cannot resolve anything — they are stuck |
| 12 | |
| 13 | If a crash occurs between apply_manifest and write_merge_state: |
| 14 | - Working tree has partially merged state (some files updated) |
| 15 | - No MERGE_STATE.json (no in-progress merge recorded) |
| 16 | - The user sees a dirty tree, can inspect diffs, and can run 'muse merge' again |
| 17 | - Recoverable |
| 18 | |
| 19 | The safe order is apply_manifest first, write_merge_state second. |
| 20 | |
| 21 | Testing tiers |
| 22 | ------------- |
| 23 | Unit write_merge_state is called AFTER apply_manifest (call order) |
| 24 | Integration merge command produces a dirty working tree AND a MERGE_STATE.json |
| 25 | Data after a simulated crash between apply and write_merge_state: |
| 26 | no MERGE_STATE.json exists, working tree is at partially merged state |
| 27 | """ |
| 28 | |
| 29 | from __future__ import annotations |
| 30 | |
| 31 | import pathlib |
| 32 | from unittest.mock import call, patch, MagicMock |
| 33 | |
| 34 | import pytest |
| 35 | |
| 36 | from muse.core.types import Manifest |
| 37 | from tests.cli_test_helper import CliRunner |
| 38 | |
| 39 | runner = CliRunner() |
| 40 | |
| 41 | |
| 42 | def _run(repo: pathlib.Path, *args: str, expect_failure: bool = False) -> "CliRunner.Result": |
| 43 | r = runner.invoke(None, list(args), cwd=repo) |
| 44 | if not expect_failure: |
| 45 | assert r.exit_code == 0, f"muse {' '.join(args)} failed:\n{r.output}" |
| 46 | return r |
| 47 | |
| 48 | |
| 49 | def _setup_conflict_scenario(repo: pathlib.Path) -> None: |
| 50 | """Create two diverged branches with a conflict in file.py.""" |
| 51 | f = repo / "file.py" |
| 52 | |
| 53 | # initial commit on main |
| 54 | f.write_text("base content\n") |
| 55 | _run(repo, "code", "add", "file.py") |
| 56 | _run(repo, "commit", "-m", "base") |
| 57 | |
| 58 | # branch A modifies file.py |
| 59 | _run(repo, "checkout", "-b", "branch-a") |
| 60 | f.write_text("branch-a content\n") |
| 61 | _run(repo, "code", "add", "file.py") |
| 62 | _run(repo, "commit", "-m", "branch-a change") |
| 63 | |
| 64 | # go back to main and make a conflicting change |
| 65 | _run(repo, "checkout", "main") |
| 66 | f.write_text("main content\n") |
| 67 | _run(repo, "code", "add", "file.py") |
| 68 | _run(repo, "commit", "-m", "main change") |
| 69 | |
| 70 | |
| 71 | # --------------------------------------------------------------------------- |
| 72 | # Unit — call order: apply_manifest before write_merge_state |
| 73 | # --------------------------------------------------------------------------- |
| 74 | |
| 75 | class TestCallOrder: |
| 76 | def test_apply_manifest_called_before_write_merge_state( |
| 77 | self, muse_repo: pathlib.Path |
| 78 | ) -> None: |
| 79 | """apply_manifest must be called before write_merge_state in the conflict path.""" |
| 80 | _setup_conflict_scenario(muse_repo) |
| 81 | |
| 82 | call_order: list[str] = [] |
| 83 | |
| 84 | orig_apply = __import__( |
| 85 | "muse.core.workdir", fromlist=["apply_manifest"] |
| 86 | ).apply_manifest |
| 87 | orig_write_merge = __import__( |
| 88 | "muse.core.merge_engine", fromlist=["write_merge_state"] |
| 89 | ).write_merge_state |
| 90 | |
| 91 | def track_apply(root: pathlib.Path, prev_manifest: Manifest, target_manifest: Manifest) -> None: |
| 92 | call_order.append("apply_manifest") |
| 93 | return orig_apply(root, prev_manifest, target_manifest) |
| 94 | |
| 95 | def track_write_merge(root: pathlib.Path, **kwargs: str | list[str] | None) -> None: |
| 96 | call_order.append("write_merge_state") |
| 97 | return orig_write_merge(root, **kwargs) |
| 98 | |
| 99 | with ( |
| 100 | patch("muse.cli.commands.merge.apply_manifest", side_effect=track_apply), |
| 101 | patch("muse.cli.commands.merge.write_merge_state", side_effect=track_write_merge), |
| 102 | ): |
| 103 | _run(muse_repo, "merge", "branch-a", expect_failure=True) |
| 104 | |
| 105 | apply_idx = next( |
| 106 | (i for i, n in enumerate(call_order) if n == "apply_manifest"), None |
| 107 | ) |
| 108 | write_merge_idx = next( |
| 109 | (i for i, n in enumerate(call_order) if n == "write_merge_state"), None |
| 110 | ) |
| 111 | |
| 112 | assert apply_idx is not None, "apply_manifest was never called during conflict merge" |
| 113 | assert write_merge_idx is not None, "write_merge_state was never called during conflict merge" |
| 114 | assert apply_idx < write_merge_idx, ( |
| 115 | f"write_merge_state (pos {write_merge_idx}) was called before " |
| 116 | f"apply_manifest (pos {apply_idx}) — wrong order" |
| 117 | ) |
| 118 | |
| 119 | |
| 120 | # --------------------------------------------------------------------------- |
| 121 | # Integration — merge conflict leaves both dirty workdir and MERGE_STATE |
| 122 | # --------------------------------------------------------------------------- |
| 123 | |
| 124 | class TestMergeConflictIntegration: |
| 125 | def test_conflict_produces_merge_state_file(self, muse_repo: pathlib.Path) -> None: |
| 126 | """A merge conflict must create .muse/MERGE_STATE.json.""" |
| 127 | _setup_conflict_scenario(muse_repo) |
| 128 | _run(muse_repo, "merge", "branch-a", expect_failure=True) |
| 129 | assert (muse_repo / ".muse" / "MERGE_STATE.json").exists(), ( |
| 130 | "MERGE_STATE.json not created after conflicting merge" |
| 131 | ) |
| 132 | |
| 133 | def test_conflict_leaves_working_tree_dirty(self, muse_repo: pathlib.Path) -> None: |
| 134 | """After a conflicting merge, the working tree must reflect the partial merge.""" |
| 135 | _setup_conflict_scenario(muse_repo) |
| 136 | _run(muse_repo, "merge", "branch-a", expect_failure=True) |
| 137 | |
| 138 | # The status command should report the working tree as dirty (merge in progress) |
| 139 | import json |
| 140 | r = runner.invoke(None, ["status", "--json"], cwd=muse_repo) |
| 141 | status = json.loads(r.output) |
| 142 | assert status["merge_in_progress"] or status["dirty"] or status["conflict_count"] > 0, ( |
| 143 | "Working tree appears clean after a conflicting merge — apply_manifest may not have run" |
| 144 | ) |
| 145 | |
| 146 | |
| 147 | # --------------------------------------------------------------------------- |
| 148 | # Data — crash between apply_manifest and write_merge_state is recoverable |
| 149 | # --------------------------------------------------------------------------- |
| 150 | |
| 151 | class TestCrashRecovery: |
| 152 | def test_crash_after_apply_before_write_merge_leaves_no_merge_state( |
| 153 | self, muse_repo: pathlib.Path |
| 154 | ) -> None: |
| 155 | """Simulated crash after apply_manifest but before write_merge_state: |
| 156 | MERGE_STATE.json must NOT exist (no ghost merge state). |
| 157 | The working tree has partial merge content (dirty but recoverable).""" |
| 158 | _setup_conflict_scenario(muse_repo) |
| 159 | |
| 160 | def crash_after_apply(root: pathlib.Path, **kwargs: str | list[str] | None) -> None: |
| 161 | raise RuntimeError("simulated crash after apply_manifest") |
| 162 | |
| 163 | with ( |
| 164 | patch("muse.cli.commands.merge.write_merge_state", side_effect=crash_after_apply), |
| 165 | ): |
| 166 | try: |
| 167 | _run(muse_repo, "merge", "branch-a", expect_failure=True) |
| 168 | except (RuntimeError, SystemExit): |
| 169 | pass |
| 170 | |
| 171 | assert not (muse_repo / ".muse" / "MERGE_STATE.json").exists(), ( |
| 172 | "MERGE_STATE.json exists after crash before write_merge_state — " |
| 173 | "wrong ordering: write_merge_state was called before apply_manifest" |
| 174 | ) |
File History
2 commits
sha256:c06a9b9b9fee26c68ea725b44d54b2c0a171301ce9de746d5b656617b4463a9a
fix: repair four test failures from post-migration audit
Sonnet 4.6
patch
28 days ago
sha256:1900655993c83c4107067375548a7be823e471d2515830842f1a12cba4bd3cdf
fix: unified object store migration — idempotent writes, JS…
Sonnet 4.6
minor
⚠
28 days ago