fix(global): isolate a malformed repo entry instead of discarding all (#5020)

Follow-up to #5025. The landed guard raised on any non-dict repo value, so a
single malformed entry backed up the whole manifest and dropped every tracked
repo. Keep the whole-file backup only for an unrecoverable shape (non-dict root
or repos map); when repos is a dict, drop just the malformed entries, keep the
valid repos, and report which went. Matches the per-entry isolation of #5024.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
safishamsiandClaude Opus 4.8 committed 2026-11-07 22:33:44 +00:00
1 parent 1192efc639
commit 2ce70ce17d
2 files changed
+50 -3

No files matched your search

+16 -2
View File
@@ -17,8 +17,22 @@ def _load_manifest() -> dict:
try:
manifest = json.loads(_GLOBAL_MANIFEST.read_text(encoding="utf-8"))
repos = manifest.get("repos") if isinstance(manifest, dict) else None
if not isinstance(repos, dict) or any(not isinstance(info, dict) for info in repos.values()):
raise ValueError("expected a manifest object with a repos map of repository objects")
if not isinstance(repos, dict):
raise ValueError("expected a manifest object with a repos map")
# Isolate a malformed repo entry instead of discarding every tracked
# repo: a single bad value should not lose the user's other repos
# (#5020). Drop the bad ones, keep the rest, and say which went. Only
# a non-dict root or repos map is unrecoverable enough to back up.
bad = sorted(name for name, info in repos.items() if not isinstance(info, dict))
if bad:
for name in bad:
repos.pop(name)
print(
f"[graphify global] dropped {len(bad)} malformed repo "
f"entr{'y' if len(bad) == 1 else 'ies'} ({', '.join(bad)}) "
f"from {_GLOBAL_MANIFEST}; kept {len(repos)} valid repo(s).",
file=sys.stderr,
)
return manifest
except Exception as exc:
# Don't silently wipe the user's manifest on a parse error: that
+34 -1
View File
@@ -7,8 +7,10 @@ import pytest
from graphify import global_graph
@pytest.mark.parametrize("payload", [None, [], {}, {"repos": []}, {"repos": {"legacy": "invalid"}}])
@pytest.mark.parametrize("payload", [None, [], {}, {"repos": []}])
def test_global_list_backs_up_wrong_manifest_shape(tmp_path, monkeypatch, capsys, payload):
# A non-dict root, or a repos value that is not a map at all, is too broken
# to recover per-entry: back it up and start fresh.
manifest = tmp_path / "global-manifest.json"
original = json.dumps(payload)
manifest.write_text(original, encoding="utf-8")
@@ -22,6 +24,37 @@ def test_global_list_backs_up_wrong_manifest_shape(tmp_path, monkeypatch, capsys
assert "moved to" in capsys.readouterr().err
def test_global_list_isolates_malformed_repo_entries(tmp_path, monkeypatch, capsys):
# A single malformed entry must not discard the user's other repos (#5020):
# drop only the bad one, keep the good ones, and do NOT back up the file.
manifest = tmp_path / "global-manifest.json"
good = {"source_path": "graph.json", "node_count": 2}
manifest.write_text(
json.dumps({"version": 1, "repos": {"goodA": good, "bad": "oops", "goodB": good}}),
encoding="utf-8",
)
monkeypatch.setattr(global_graph, "_GLOBAL_MANIFEST", manifest)
assert global_graph.global_list() == {"goodA": good, "goodB": good}
assert manifest.is_file() # not backed up
assert not list(tmp_path.glob("global-manifest.json.corrupt.*"))
err = capsys.readouterr().err
assert "dropped 1 malformed repo" in err and "bad" in err
def test_global_list_drops_all_malformed_entries_without_backup(tmp_path, monkeypatch):
# An all-malformed repos map is still a valid shape: it collapses to empty
# rather than being backed up.
manifest = tmp_path / "global-manifest.json"
manifest.write_text(json.dumps({"repos": {"legacy": "invalid"}}), encoding="utf-8")
monkeypatch.setattr(global_graph, "_GLOBAL_MANIFEST", manifest)
assert global_graph.global_list() == {}
assert manifest.is_file()
assert not list(tmp_path.glob("global-manifest.json.corrupt.*"))
def test_global_list_preserves_valid_manifest(tmp_path, monkeypatch):
manifest = tmp_path / "global-manifest.json"
repos = {"project": {"source_path": "graph.json", "node_count": 2}}