fix(install): tell unreadable manifests apart from malformed ones

A manifest that could not be read (permissions, non-UTF-8 bytes) was reported
as "corrupt" with advice to fix or delete it. load_manifest and list_manifests
now read first and report "could not be read ... Check the file's permissions
and that it is UTF-8"; only a JSON or schema failure is called corrupt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Assisted-by: claude-code:claude-opus-5
Headline-Session: 88f3a7f6-986c-4fa8-ab24-0c064dbb438a
Headline-AI-Share: 2% strict / 2% weighted
This commit is contained in:
Tejas Chopra
2026-10-09 18:29:39 -07:00
co-authored by Claude Opus 5.5
parent 828b107a4a
commit 71e669eb47
2 changed files with 61 additions and 5 deletions
+20 -5
View File
@@ -128,8 +128,15 @@ def load_manifest(profile: str = "default") -> DeploymentManifest | None:
# command and the auto-run `init hook ensure` route through here. Raise a
# typed error so callers can report cleanly or degrade gracefully.
try:
return _manifest_from_payload(json.loads(path.read_text(encoding="utf-8")))
except (json.JSONDecodeError, ValueError, TypeError, OSError) as e:
text = path.read_text(encoding="utf-8")
except (OSError, UnicodeDecodeError) as e:
raise ManifestError(
f"deployment profile '{profile}' could not be read ({path}): {e}. "
"Check the file's permissions and that it is UTF-8."
) from e
try:
return _manifest_from_payload(json.loads(text))
except (ValueError, TypeError) as e:
raise ManifestError(f"deployment profile '{profile}' is corrupt ({path}): {e}") from e
@@ -143,10 +150,18 @@ def list_manifests() -> list[DeploymentManifest]:
manifests: list[DeploymentManifest] = []
for candidate in sorted(root.glob("*/manifest.json")):
try:
manifests.append(
_manifest_from_payload(json.loads(candidate.read_text(encoding="utf-8")))
text = candidate.read_text(encoding="utf-8")
except (OSError, UnicodeDecodeError) as e:
logger.warning(
"Skipping unreadable deployment manifest %s: %s. Check the file's "
"permissions and that it is UTF-8.",
candidate,
e,
)
except (OSError, ValueError, TypeError) as e:
continue
try:
manifests.append(_manifest_from_payload(json.loads(text)))
except (ValueError, TypeError) as e:
logger.warning(
"Skipping corrupt deployment manifest %s: %s. Fix or delete the file to "
"restore that profile.",
+41
View File
@@ -111,6 +111,47 @@ def test_list_manifests_ignores_invalid_payloads(monkeypatch, tmp_path: Path, ca
assert not any(str(tmp_path / ".headroom" / "deploy" / "default") in m for m in warnings)
def _broken_profile(tmp_path: Path, name: str) -> Path:
path = tmp_path / ".headroom" / "deploy" / name / "manifest.json"
path.parent.mkdir(parents=True)
return path
def test_unreadable_and_malformed_manifests_get_distinct_messages(
monkeypatch, tmp_path: Path, caplog
) -> None:
"""A read failure must not be reported as corruption: the fix differs."""
monkeypatch.setattr(Path, "home", lambda: tmp_path)
malformed = _broken_profile(tmp_path, "malformed")
malformed.write_text("{not json", encoding="utf-8")
unreadable = _broken_profile(tmp_path, "unreadable")
unreadable.write_text(json.dumps({"profile": "unreadable"}), encoding="utf-8")
real_read_text = Path.read_text
def read_text(self: Path, *args, **kwargs) -> str:
if self == unreadable:
raise PermissionError(13, "Permission denied", str(self))
return real_read_text(self, *args, **kwargs)
monkeypatch.setattr(Path, "read_text", read_text)
with caplog.at_level(logging.WARNING, logger="headroom.install.state"):
assert list_manifests() == []
messages = [r.getMessage() for r in caplog.records if r.name == "headroom.install.state"]
assert len(messages) == 2
corrupt = next(m for m in messages if str(malformed) in m)
unread = next(m for m in messages if str(unreadable) in m)
assert corrupt.startswith("Skipping corrupt deployment manifest")
assert unread.startswith("Skipping unreadable deployment manifest")
assert "Permission denied" in unread and "permissions" in unread
with pytest.raises(ManifestError, match="is corrupt"):
load_manifest("malformed")
with pytest.raises(ManifestError, match="could not be read .*Permission denied"):
load_manifest("unreadable")
@pytest.mark.skipif(os.name != "posix", reason="mode bits decide access only on POSIX")
def test_save_manifest_keeps_manifest_owner_only(monkeypatch, tmp_path: Path) -> None:
monkeypatch.setattr(Path, "home", lambda: tmp_path)