"""muse#93 — harmony auto-apply reports "auto-resolved" but silently discards the recorded resolution's content. Root cause: `harmony_resolved` (returned by `auto_apply`) is keyed by the *original conflict path*, which for the code domain is a symbol address ("calc.py::add"), not a plain file path. `merged_files` -- the manifest that actually gets committed -- is keyed by plain file paths, same as every other manifest in `muse/cli/commands/merge.py`. The old `merged_files.update(harmony_resolved)` inserted a bogus "calc.py::add"-keyed entry while leaving the real "calc.py" entry untouched at its pre-harmony value. The CLI printed "✔ [harmony] auto-resolved: calc.py::add" and incremented the resolution's `applied_count`, but the committed file content stayed "ours" -- the recorded, human-verified resolution was never actually applied to the thing that got committed. This test reproduces the exact scenario from the ticket end-to-end through the real CLI (not synthetic objects): two branches conflict on the same symbol, resolve manually to a THIRD value (differing from both "ours" and "theirs"), confirm harmony recorded it, then reproduce the identical conflict pattern on a fresh pair of branches and assert the merged file's *committed* content equals the recorded resolution -- not either original side. """ from __future__ import annotations import json import os import pathlib import pytest from tests.cli_test_helper import CliRunner, InvokeResult runner = CliRunner() def _run(repo: pathlib.Path, *args: str) -> InvokeResult: saved = os.getcwd() try: os.chdir(repo) return runner.invoke(None, list(args)) finally: os.chdir(saved) def _commit_calc(repo: pathlib.Path, body: str, message: str) -> None: (repo / "calc.py").write_text(f"def add(a, b):\n return {body}\n") r = _run(repo, "code", "add", "calc.py") assert r.exit_code == 0, r.stderr r = _run(repo, "commit", "-m", message) assert r.exit_code == 0, r.stderr def _branch_from(repo: pathlib.Path, base: str, name: str, body: str, message: str) -> None: r = _run(repo, "checkout", base) assert r.exit_code == 0, r.stderr r = _run(repo, "branch", name) assert r.exit_code == 0, r.stderr r = _run(repo, "checkout", name) assert r.exit_code == 0, r.stderr _commit_calc(repo, body, message) def test_harmony_auto_apply_commits_the_recorded_resolution_not_ours(tmp_path: pathlib.Path) -> None: repo = tmp_path r = _run(repo, "init") assert r.exit_code == 0, r.stderr _commit_calc(repo, "a + b", "base") # First occurrence: a real symbol-level conflict, resolved manually to a # THIRD value that is neither "ours" (+1) nor "theirs" (+100). _branch_from(repo, "main", "feat/a", "a + b + 1", "a changes add") _branch_from(repo, "main", "feat/b", "a + b + 100", "b changes add") r = _run(repo, "checkout", "main") assert r.exit_code == 0, r.stderr r = _run(repo, "merge", "feat/a") assert r.exit_code == 0, r.stderr r = _run(repo, "merge", "feat/b", "--json") payload = json.loads(r.output) assert payload["status"] == "conflict" assert payload["conflicts"] == ["calc.py::add"] (repo / "calc.py").write_text("def add(a, b):\n return a + b + 101\n") r = _run(repo, "resolve", "calc.py::add") assert r.exit_code == 0, r.stderr r = _run(repo, "commit", "-m", "resolve add conflict") assert r.exit_code == 0, r.stderr # Confirm harmony actually recorded the resolution before testing replay. r = _run(repo, "harmony", "list", "--json") patterns = json.loads(r.output)["patterns"] assert len(patterns) == 1 assert patterns[0]["resolution_count"] == 1 # Second occurrence: the IDENTICAL conflict pattern on a fresh pair of # branches off the same original base. Harmony should auto-apply the # recorded resolution, not silently fall back to "ours". _branch_from(repo, "main", "feat/c", "a + b + 1", "c changes add") _branch_from(repo, "main", "feat/d", "a + b + 100", "d changes add") r = _run(repo, "checkout", "main") assert r.exit_code == 0, r.stderr r = _run(repo, "merge", "feat/c") assert r.exit_code == 0, r.stderr r = _run(repo, "merge", "feat/d", "--json") assert r.exit_code == 0, r.stderr payload = json.loads(r.output) assert payload["status"] == "merged", ( "harmony should have auto-resolved this -- merge must report 'merged', " "not 'conflict'" ) # The acceptance bar from the ticket: check BOTH the working tree and the # actual committed snapshot object -- not just a working-tree artifact. working_tree_content = (repo / "calc.py").read_text() assert "+ 101" in working_tree_content, ( f"working tree has the wrong content after harmony auto-apply: {working_tree_content!r}" ) r = _run(repo, "read", "--json", "--manifest") assert r.exit_code == 0, r.stderr manifest = json.loads(r.output)["manifest"] committed_object_id = manifest["calc.py"] r = _run(repo, "cat-object", committed_object_id, "--json") assert r.exit_code == 0, r.stderr committed_bytes_info = json.loads(r.output) r = _run(repo, "cat", "calc.py") assert r.exit_code == 0, r.stderr committed_content = r.output assert "+ 101" in committed_content, ( f"COMMITTED snapshot has the wrong content -- harmony reported " f"auto-resolved but committed 'ours' or 'theirs' instead of the " f"recorded resolution: {committed_content!r}" ) assert "+ 1\n" not in committed_content.replace("+ 101", "") assert "+ 100" not in committed_content