merge_engine.py
python
| 1 | """Muse VCS merge engine — fast-forward, 3-way, op-level, and CRDT merge. |
| 2 | |
| 3 | Public API |
| 4 | ---------- |
| 5 | Pure functions (no I/O): |
| 6 | |
| 7 | - :func:`diff_snapshots` — paths that changed between two snapshot manifests. |
| 8 | - :func:`detect_conflicts` — paths changed on *both* branches since the base. |
| 9 | - :func:`apply_merge` — build merged manifest for a conflict-free 3-way merge. |
| 10 | - :func:`crdt_join_snapshots` — convergent CRDT join; always succeeds. |
| 11 | |
| 12 | Operational Transformation (operation-level) merge: |
| 13 | |
| 14 | - :mod:`muse.core.op_transform` — ``ops_commute``, ``transform``, ``merge_op_lists``, |
| 15 | ``merge_structured``, and :class:`~muse.core.op_transform.MergeOpsResult`. |
| 16 | Plugins that implement :class:`~muse.domain.StructuredMergePlugin` use these |
| 17 | functions to auto-merge non-conflicting ``DomainOp`` lists. |
| 18 | |
| 19 | CRDT convergent merge: |
| 20 | |
| 21 | - :func:`crdt_join_snapshots` — detects :class:`~muse.domain.CRDTPlugin` at |
| 22 | runtime and delegates to ``plugin.join(a, b)``. Returns a |
| 23 | :class:`~muse.domain.MergeResult` with an empty ``conflicts`` list; CRDT |
| 24 | joins never fail. |
| 25 | |
| 26 | File-based helpers: |
| 27 | |
| 28 | - :func:`find_merge_base` — lowest common ancestor (LCA) of two commits. |
| 29 | - :func:`read_merge_state` — detect and load an in-progress merge. |
| 30 | - :func:`write_merge_state` — persist conflict state before exiting. |
| 31 | - :func:`clear_merge_state` — remove MERGE_STATE.json after resolution. |
| 32 | - :func:`apply_resolution` — restore a specific object version to muse-work/. |
| 33 | |
| 34 | ``MERGE_STATE.json`` schema |
| 35 | --------------------------- |
| 36 | |
| 37 | .. code-block:: json |
| 38 | |
| 39 | { |
| 40 | "base_commit": "abc123...", |
| 41 | "ours_commit": "def456...", |
| 42 | "theirs_commit": "789abc...", |
| 43 | "conflict_paths": ["beat.mid", "lead.mp3"], |
| 44 | "other_branch": "feature/experiment" |
| 45 | } |
| 46 | |
| 47 | ``other_branch`` is optional; all other fields are required when conflicts exist. |
| 48 | """ |
| 49 | from __future__ import annotations |
| 50 | |
| 51 | import json |
| 52 | import logging |
| 53 | import pathlib |
| 54 | from collections import deque |
| 55 | from dataclasses import dataclass, field |
| 56 | from typing import TYPE_CHECKING, TypedDict |
| 57 | |
| 58 | if TYPE_CHECKING: |
| 59 | from muse.domain import MergeResult, MuseDomainPlugin |
| 60 | |
| 61 | logger = logging.getLogger(__name__) |
| 62 | |
| 63 | _MERGE_STATE_FILENAME = "MERGE_STATE.json" |
| 64 | |
| 65 | |
| 66 | # --------------------------------------------------------------------------- |
| 67 | # Wire-format TypedDict |
| 68 | # --------------------------------------------------------------------------- |
| 69 | |
| 70 | |
| 71 | class MergeStatePayload(TypedDict, total=False): |
| 72 | """JSON-serialisable form of an in-progress merge state.""" |
| 73 | |
| 74 | base_commit: str |
| 75 | ours_commit: str |
| 76 | theirs_commit: str |
| 77 | conflict_paths: list[str] |
| 78 | other_branch: str |
| 79 | |
| 80 | |
| 81 | # --------------------------------------------------------------------------- |
| 82 | # MergeState dataclass |
| 83 | # --------------------------------------------------------------------------- |
| 84 | |
| 85 | |
| 86 | @dataclass(frozen=True) |
| 87 | class MergeState: |
| 88 | """Describes an in-progress merge with unresolved conflicts.""" |
| 89 | |
| 90 | conflict_paths: list[str] = field(default_factory=list) |
| 91 | base_commit: str | None = None |
| 92 | ours_commit: str | None = None |
| 93 | theirs_commit: str | None = None |
| 94 | other_branch: str | None = None |
| 95 | |
| 96 | |
| 97 | # --------------------------------------------------------------------------- |
| 98 | # Filesystem helpers |
| 99 | # --------------------------------------------------------------------------- |
| 100 | |
| 101 | |
| 102 | def read_merge_state(root: pathlib.Path) -> MergeState | None: |
| 103 | """Return :class:`MergeState` if a merge is in progress, otherwise ``None``.""" |
| 104 | merge_state_path = root / ".muse" / _MERGE_STATE_FILENAME |
| 105 | if not merge_state_path.exists(): |
| 106 | return None |
| 107 | try: |
| 108 | data = json.loads(merge_state_path.read_text()) |
| 109 | except (json.JSONDecodeError, OSError) as exc: |
| 110 | logger.warning("⚠️ Failed to read %s: %s", _MERGE_STATE_FILENAME, exc) |
| 111 | return None |
| 112 | |
| 113 | raw_conflicts = data.get("conflict_paths", []) |
| 114 | conflict_paths: list[str] = ( |
| 115 | [str(c) for c in raw_conflicts] if isinstance(raw_conflicts, list) else [] |
| 116 | ) |
| 117 | |
| 118 | def _str_or_none(key: str) -> str | None: |
| 119 | val = data.get(key) |
| 120 | return str(val) if val is not None else None |
| 121 | |
| 122 | return MergeState( |
| 123 | conflict_paths=conflict_paths, |
| 124 | base_commit=_str_or_none("base_commit"), |
| 125 | ours_commit=_str_or_none("ours_commit"), |
| 126 | theirs_commit=_str_or_none("theirs_commit"), |
| 127 | other_branch=_str_or_none("other_branch"), |
| 128 | ) |
| 129 | |
| 130 | |
| 131 | def write_merge_state( |
| 132 | root: pathlib.Path, |
| 133 | *, |
| 134 | base_commit: str, |
| 135 | ours_commit: str, |
| 136 | theirs_commit: str, |
| 137 | conflict_paths: list[str], |
| 138 | other_branch: str | None = None, |
| 139 | ) -> None: |
| 140 | """Write ``.muse/MERGE_STATE.json`` to signal an in-progress conflicted merge. |
| 141 | |
| 142 | Called by the ``muse merge`` command when the merge produces at least one |
| 143 | conflict that cannot be auto-resolved. The file is read back by |
| 144 | :func:`read_merge_state` on subsequent ``muse status`` and ``muse commit`` |
| 145 | invocations to surface conflict state to the user. |
| 146 | |
| 147 | Args: |
| 148 | root: Repository root (parent of ``.muse/``). |
| 149 | base_commit: Commit ID of the merge base (common ancestor). |
| 150 | ours_commit: Commit ID of the current branch (HEAD) at merge time. |
| 151 | theirs_commit: Commit ID of the branch being merged in. |
| 152 | conflict_paths: Sorted list of workspace-relative POSIX paths with |
| 153 | unresolvable conflicts. |
| 154 | other_branch: Name of the branch being merged in; stored for |
| 155 | informational display but not required for resolution. |
| 156 | """ |
| 157 | merge_state_path = root / ".muse" / _MERGE_STATE_FILENAME |
| 158 | payload: MergeStatePayload = { |
| 159 | "base_commit": base_commit, |
| 160 | "ours_commit": ours_commit, |
| 161 | "theirs_commit": theirs_commit, |
| 162 | "conflict_paths": sorted(conflict_paths), |
| 163 | } |
| 164 | if other_branch is not None: |
| 165 | payload["other_branch"] = other_branch |
| 166 | merge_state_path.write_text(json.dumps(payload, indent=2)) |
| 167 | logger.info("✅ Wrote MERGE_STATE.json with %d conflict(s)", len(conflict_paths)) |
| 168 | |
| 169 | |
| 170 | def clear_merge_state(root: pathlib.Path) -> None: |
| 171 | """Remove ``.muse/MERGE_STATE.json`` after a successful merge or resolution.""" |
| 172 | merge_state_path = root / ".muse" / _MERGE_STATE_FILENAME |
| 173 | if merge_state_path.exists(): |
| 174 | merge_state_path.unlink() |
| 175 | logger.debug("✅ Cleared MERGE_STATE.json") |
| 176 | |
| 177 | |
| 178 | def apply_resolution( |
| 179 | root: pathlib.Path, |
| 180 | rel_path: str, |
| 181 | object_id: str, |
| 182 | ) -> None: |
| 183 | """Restore a specific object version to ``muse-work/<rel_path>``. |
| 184 | |
| 185 | Used by the ``muse merge --resolve`` workflow: after a user has chosen |
| 186 | which version of a conflicting file to keep, this function writes that |
| 187 | version into the working directory so ``muse commit`` can snapshot it. |
| 188 | |
| 189 | Args: |
| 190 | root: Repository root (parent of ``.muse/``). |
| 191 | rel_path: Workspace-relative POSIX path of the conflicting file. |
| 192 | object_id: SHA-256 of the chosen resolution content in the object store. |
| 193 | |
| 194 | Raises: |
| 195 | FileNotFoundError: When *object_id* is not present in the local store. |
| 196 | """ |
| 197 | from muse.core.object_store import read_object |
| 198 | |
| 199 | content = read_object(root, object_id) |
| 200 | if content is None: |
| 201 | raise FileNotFoundError( |
| 202 | f"Object {object_id[:8]} for '{rel_path}' not found in local store." |
| 203 | ) |
| 204 | dest = root / "muse-work" / rel_path |
| 205 | dest.parent.mkdir(parents=True, exist_ok=True) |
| 206 | dest.write_bytes(content) |
| 207 | logger.debug("✅ Restored '%s' from object %s", rel_path, object_id[:8]) |
| 208 | |
| 209 | |
| 210 | def is_conflict_resolved(merge_state: MergeState, rel_path: str) -> bool: |
| 211 | """Return ``True`` if *rel_path* is NOT listed as a conflict in *merge_state*.""" |
| 212 | return rel_path not in merge_state.conflict_paths |
| 213 | |
| 214 | |
| 215 | # --------------------------------------------------------------------------- |
| 216 | # Pure merge functions (no I/O) |
| 217 | # --------------------------------------------------------------------------- |
| 218 | |
| 219 | |
| 220 | def diff_snapshots( |
| 221 | base_manifest: dict[str, str], |
| 222 | other_manifest: dict[str, str], |
| 223 | ) -> set[str]: |
| 224 | """Return the set of paths that differ between *base_manifest* and *other_manifest*. |
| 225 | |
| 226 | A path is "different" if it was added (in *other* but not *base*), deleted |
| 227 | (in *base* but not *other*), or modified (present in both with different |
| 228 | content hashes). |
| 229 | |
| 230 | Args: |
| 231 | base_manifest: Path → content-hash map for the ancestor snapshot. |
| 232 | other_manifest: Path → content-hash map for the other snapshot. |
| 233 | |
| 234 | Returns: |
| 235 | Set of workspace-relative POSIX paths that differ. |
| 236 | """ |
| 237 | base_paths = set(base_manifest.keys()) |
| 238 | other_paths = set(other_manifest.keys()) |
| 239 | added = other_paths - base_paths |
| 240 | deleted = base_paths - other_paths |
| 241 | common = base_paths & other_paths |
| 242 | modified = {p for p in common if base_manifest[p] != other_manifest[p]} |
| 243 | return added | deleted | modified |
| 244 | |
| 245 | |
| 246 | def detect_conflicts( |
| 247 | ours_changed: set[str], |
| 248 | theirs_changed: set[str], |
| 249 | ) -> set[str]: |
| 250 | """Return paths changed on *both* branches since the merge base.""" |
| 251 | return ours_changed & theirs_changed |
| 252 | |
| 253 | |
| 254 | def apply_merge( |
| 255 | base_manifest: dict[str, str], |
| 256 | ours_manifest: dict[str, str], |
| 257 | theirs_manifest: dict[str, str], |
| 258 | ours_changed: set[str], |
| 259 | theirs_changed: set[str], |
| 260 | conflict_paths: set[str], |
| 261 | ) -> dict[str, str]: |
| 262 | """Build the merged snapshot manifest for a conflict-free 3-way merge. |
| 263 | |
| 264 | Starts from *base_manifest* and applies non-conflicting changes from both |
| 265 | branches: |
| 266 | |
| 267 | - Ours-only changes (in *ours_changed* but not *conflict_paths*) are taken |
| 268 | from *ours_manifest*. Deletions are handled by the absence of the path |
| 269 | in *ours_manifest*. |
| 270 | - Theirs-only changes (in *theirs_changed* but not *conflict_paths*) are |
| 271 | taken from *theirs_manifest* by the same logic. |
| 272 | - Paths in *conflict_paths* are excluded — callers must resolve them |
| 273 | separately before producing a final merged snapshot. |
| 274 | |
| 275 | Args: |
| 276 | base_manifest: Path → content-hash for the common ancestor. |
| 277 | ours_manifest: Path → content-hash for our branch. |
| 278 | theirs_manifest: Path → content-hash for their branch. |
| 279 | ours_changed: Paths changed by our branch (from :func:`diff_snapshots`). |
| 280 | theirs_changed: Paths changed by their branch. |
| 281 | conflict_paths: Paths with concurrent changes — excluded from output. |
| 282 | |
| 283 | Returns: |
| 284 | Merged path → content-hash mapping; conflict paths are absent. |
| 285 | """ |
| 286 | merged: dict[str, str] = dict(base_manifest) |
| 287 | for path in ours_changed - conflict_paths: |
| 288 | if path in ours_manifest: |
| 289 | merged[path] = ours_manifest[path] |
| 290 | else: |
| 291 | merged.pop(path, None) |
| 292 | for path in theirs_changed - conflict_paths: |
| 293 | if path in theirs_manifest: |
| 294 | merged[path] = theirs_manifest[path] |
| 295 | else: |
| 296 | merged.pop(path, None) |
| 297 | return merged |
| 298 | |
| 299 | |
| 300 | # --------------------------------------------------------------------------- |
| 301 | # CRDT convergent join |
| 302 | # --------------------------------------------------------------------------- |
| 303 | |
| 304 | |
| 305 | def crdt_join_snapshots( |
| 306 | plugin: MuseDomainPlugin, |
| 307 | a_snapshot: dict[str, str], |
| 308 | b_snapshot: dict[str, str], |
| 309 | a_vclock: dict[str, int], |
| 310 | b_vclock: dict[str, int], |
| 311 | a_crdt_state: dict[str, str], |
| 312 | b_crdt_state: dict[str, str], |
| 313 | domain: str, |
| 314 | ) -> MergeResult: |
| 315 | """Convergent CRDT merge — always succeeds, no conflicts possible. |
| 316 | |
| 317 | Detects :class:`~muse.domain.CRDTPlugin` support via ``isinstance`` and |
| 318 | delegates to ``plugin.join(a, b)``. The returned :class:`~muse.domain.MergeResult` |
| 319 | always has an empty ``conflicts`` list — the defining property of CRDT joins. |
| 320 | |
| 321 | This function is the CRDT entry point for the ``muse merge`` command. |
| 322 | It is only called when ``DomainSchema.merge_mode == "crdt"`` AND the plugin |
| 323 | passes the ``isinstance(plugin, CRDTPlugin)`` check. |
| 324 | |
| 325 | Args: |
| 326 | plugin: The loaded domain plugin instance. |
| 327 | a_snapshot: ``files`` mapping (path → content hash) for replica A. |
| 328 | b_snapshot: ``files`` mapping (path → content hash) for replica B. |
| 329 | a_vclock: Vector clock ``{agent_id: count}`` for replica A. |
| 330 | b_vclock: Vector clock ``{agent_id: count}`` for replica B. |
| 331 | a_crdt_state: CRDT metadata hashes (path → blob hash) for replica A. |
| 332 | b_crdt_state: CRDT metadata hashes (path → blob hash) for replica B. |
| 333 | domain: Domain name string (e.g. ``"music"``). |
| 334 | |
| 335 | Returns: |
| 336 | A :class:`~muse.domain.MergeResult` with the joined snapshot and an |
| 337 | empty ``conflicts`` list. |
| 338 | |
| 339 | Raises: |
| 340 | TypeError: When *plugin* does not implement the |
| 341 | :class:`~muse.domain.CRDTPlugin` protocol. |
| 342 | """ |
| 343 | from muse.domain import CRDTPlugin, CRDTSnapshotManifest, MergeResult, StateSnapshot |
| 344 | |
| 345 | if not isinstance(plugin, CRDTPlugin): |
| 346 | raise TypeError( |
| 347 | f"crdt_join_snapshots: plugin {type(plugin).__name__!r} does not " |
| 348 | "implement CRDTPlugin — cannot use CRDT join path." |
| 349 | ) |
| 350 | |
| 351 | a_crdt: CRDTSnapshotManifest = { |
| 352 | "files": a_snapshot, |
| 353 | "domain": domain, |
| 354 | "vclock": a_vclock, |
| 355 | "crdt_state": a_crdt_state, |
| 356 | "schema_version": 1, |
| 357 | } |
| 358 | b_crdt: CRDTSnapshotManifest = { |
| 359 | "files": b_snapshot, |
| 360 | "domain": domain, |
| 361 | "vclock": b_vclock, |
| 362 | "crdt_state": b_crdt_state, |
| 363 | "schema_version": 1, |
| 364 | } |
| 365 | |
| 366 | result_crdt = plugin.join(a_crdt, b_crdt) |
| 367 | plain_snapshot: StateSnapshot = plugin.from_crdt_state(result_crdt) |
| 368 | |
| 369 | return MergeResult( |
| 370 | merged=plain_snapshot, |
| 371 | conflicts=[], |
| 372 | applied_strategies={}, |
| 373 | ) |
| 374 | |
| 375 | |
| 376 | # --------------------------------------------------------------------------- |
| 377 | # File-based merge base finder |
| 378 | # --------------------------------------------------------------------------- |
| 379 | |
| 380 | |
| 381 | def find_merge_base( |
| 382 | repo_root: pathlib.Path, |
| 383 | commit_id_a: str, |
| 384 | commit_id_b: str, |
| 385 | ) -> str | None: |
| 386 | """Find the Lowest Common Ancestor (LCA) of two commits. |
| 387 | |
| 388 | Uses BFS to collect all ancestors of *commit_id_a* (inclusive), then |
| 389 | walks *commit_id_b*'s ancestor graph (BFS) until the first node found |
| 390 | in *a*'s ancestor set is reached. |
| 391 | |
| 392 | Args: |
| 393 | repo_root: The repository root directory. |
| 394 | commit_id_a: First commit ID (e.g., current branch HEAD). |
| 395 | commit_id_b: Second commit ID (e.g., target branch HEAD). |
| 396 | |
| 397 | Returns: |
| 398 | The LCA commit ID, or ``None`` if the commits share no common ancestor. |
| 399 | """ |
| 400 | from muse.core.store import read_commit |
| 401 | |
| 402 | def _all_ancestors(start: str) -> set[str]: |
| 403 | visited: set[str] = set() |
| 404 | queue: deque[str] = deque([start]) |
| 405 | while queue: |
| 406 | cid = queue.popleft() |
| 407 | if cid in visited: |
| 408 | continue |
| 409 | visited.add(cid) |
| 410 | commit = read_commit(repo_root, cid) |
| 411 | if commit is None: |
| 412 | continue |
| 413 | if commit.parent_commit_id: |
| 414 | queue.append(commit.parent_commit_id) |
| 415 | if commit.parent2_commit_id: |
| 416 | queue.append(commit.parent2_commit_id) |
| 417 | return visited |
| 418 | |
| 419 | a_ancestors = _all_ancestors(commit_id_a) |
| 420 | |
| 421 | visited_b: set[str] = set() |
| 422 | queue_b: deque[str] = deque([commit_id_b]) |
| 423 | while queue_b: |
| 424 | cid = queue_b.popleft() |
| 425 | if cid in visited_b: |
| 426 | continue |
| 427 | visited_b.add(cid) |
| 428 | if cid in a_ancestors: |
| 429 | return cid |
| 430 | commit = read_commit(repo_root, cid) |
| 431 | if commit is None: |
| 432 | continue |
| 433 | if commit.parent_commit_id: |
| 434 | queue_b.append(commit.parent_commit_id) |
| 435 | if commit.parent2_commit_id: |
| 436 | queue_b.append(commit.parent2_commit_id) |
| 437 | |
| 438 | return None |