"""TDD — ``_coerce_commit_dict`` must preserve ``structured_delta`` from the wire. Root cause (found while debugging a real staging clone / `muse verify` failure report): every field in ``_coerce_commit_dict`` reads from the incoming wire dict via ``raw.get(...)`` *except* ``structured_delta``, which was hardcoded to ``None`` -- discarding whatever the server actually sent. The function's own docstring claims "structured_delta is preserved as-is", which was false. Effect: every ``muse fetch`` / ``muse clone`` / ``muse pull`` permanently loses structured_delta for every commit, even though the server sends it correctly (confirmed separately via a live push+fetch round trip against staging.musehub.ai). This is unrelated to a separate, historical-only signature-corruption issue found in the same investigation (~1179 pre-existing commits with invalid signatures) -- that one does not reproduce on a fresh push+fetch cycle and needs its own data-repair investigation, not a code fix. """ from __future__ import annotations from muse.core.transport import _coerce_commit_dict def _raw_wire_commit(**overrides: object) -> dict[str, object]: base: dict[str, object] = { "commit_id": "sha256:" + "a" * 64, "repo_id": "sha256:" + "b" * 64, "branch": "main", "snapshot_id": "sha256:" + "c" * 64, "message": "a commit", "committed_at": "2026-05-20T14:43:25.290457+00:00", "parent_commit_id": None, "parent2_commit_id": None, "author": "gabriel", "metadata": {}, "structured_delta": { "domain": "code", "ops": [{"op": "patch", "address": "src/foo.py"}], "summary": "1 modified file", }, "sem_ver_bump": "patch", "breaking_changes": [], "agent_id": "claude-code", "model_id": "claude-sonnet-5", "toolchain_id": "", "prompt_hash": "", "signature": "ed25519:" + "d" * 86, "signer_public_key": "ed25519:" + "e" * 43, "signer_key_id": "sha256:" + "f" * 64, "reviewed_by": [], "test_runs": 0, } base.update(overrides) return base class TestCoerceCommitDictStructuredDelta: def test_real_structured_delta_is_preserved(self) -> None: raw = _raw_wire_commit() result = _coerce_commit_dict(raw) assert result["structured_delta"] == raw["structured_delta"] def test_missing_structured_delta_stays_none(self) -> None: raw = _raw_wire_commit() del raw["structured_delta"] result = _coerce_commit_dict(raw) assert result["structured_delta"] is None def test_non_dict_structured_delta_is_rejected_not_passed_through(self) -> None: """A malformed wire value (e.g. a string or list) must not be trusted as structured_delta -- coerce to None rather than propagate garbage.""" raw = _raw_wire_commit(structured_delta="not-a-dict") result = _coerce_commit_dict(raw) assert result["structured_delta"] is None def test_other_fields_unaffected(self) -> None: """Regression guard: every other field this function handles must keep working exactly as before -- this bug only ever affected structured_delta.""" raw = _raw_wire_commit() result = _coerce_commit_dict(raw) assert result["commit_id"] == raw["commit_id"] assert result["signature"] == raw["signature"] assert result["signer_public_key"] == raw["signer_public_key"] assert result["agent_id"] == raw["agent_id"] assert result["sem_ver_bump"] == "patch"