Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

refactor(install,subscription): share manifest parsing and warn on corrupt manifests (#4125)

Tejas Chopraf30d33068215
diff --git a/headroom/install/state.py b/headroom/install/state.py
index 5a091a87a..9102dc74c 100644
--- a/headroom/install/state.py
+++ b/headroom/install/state.py
@@ -108,6 +108,15 @@ def _migrate_deprecated_image(image: Any) -> Any:
return image
+def _manifest_from_payload(payload: dict[str, Any]) -> DeploymentManifest:
+ """Build a manifest from its decoded JSON, migrating the retired image repo."""
+ payload["mutations"] = [ManagedMutation(**item) for item in payload.get("mutations", [])]
+ payload["artifacts"] = [ArtifactRecord(**item) for item in payload.get("artifacts", [])]
+ if "image" in payload:
+ payload["image"] = _migrate_deprecated_image(payload["image"])
+ return DeploymentManifest(**payload)
+
+
def load_manifest(profile: str = "default") -> DeploymentManifest | None:
"""Load a deployment manifest when present."""
@@ -119,13 +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:
- payload = json.loads(path.read_text(encoding="utf-8"))
- payload["mutations"] = [ManagedMutation(**item) for item in payload.get("mutations", [])]
- payload["artifacts"] = [ArtifactRecord(**item) for item in payload.get("artifacts", [])]
- if "image" in payload:
- payload["image"] = _migrate_deprecated_image(payload["image"])
- return DeploymentManifest(**payload)
- 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
@@ -139,16 +150,24 @@ def list_manifests() -> list[DeploymentManifest]:
manifests: list[DeploymentManifest] = []
for candidate in sorted(root.glob("*/manifest.json")):
try:
- payload = json.loads(candidate.read_text(encoding="utf-8"))
- payload["mutations"] = [
- ManagedMutation(**item) for item in payload.get("mutations", [])
- ]
- payload["artifacts"] = [ArtifactRecord(**item) for item in payload.get("artifacts", [])]
- if "image" in payload:
- payload["image"] = _migrate_deprecated_image(payload["image"])
- manifests.append(DeploymentManifest(**payload))
- except (OSError, ValueError, TypeError):
+ 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,
+ )
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.",
+ candidate,
+ e,
+ )
return manifests
diff --git a/headroom/subscription/client.py b/headroom/subscription/client.py
index 1cf9ed7fb..4fc13d4e7 100644
--- a/headroom/subscription/client.py
+++ b/headroom/subscription/client.py
@@ -23,6 +23,7 @@ import httpx
from headroom.offline import OfflineEgressBlocked, guard_egress, note_refusal
from headroom.subscription.models import SubscriptionSnapshot
+from headroom.subscription.session_tracking import _claude_config_dir
logger = logging.getLogger(__name__)
@@ -32,8 +33,7 @@ _TOKEN_EXPIRY_BUFFER_S = 60
def _credentials_path() -> Path:
- base = os.environ.get("CLAUDE_CONFIG_DIR") or os.path.expanduser("~/.claude")
- return Path(base) / ".credentials.json"
+ return _claude_config_dir() / ".credentials.json"
def _load_credentials_file() -> dict[str, Any] | None:
diff --git a/tests/test_install/test_state.py b/tests/test_install/test_state.py
index 647cfd37f..931572f85 100644
--- a/tests/test_install/test_state.py
+++ b/tests/test_install/test_state.py
@@ -1,6 +1,9 @@
from __future__ import annotations
import json
+import logging
+import os
+import stat
from pathlib import Path
import pytest
@@ -90,7 +93,7 @@ def test_recovery_manifest_preserves_mutations_and_artifacts(monkeypatch, tmp_pa
assert not recovery.exists()
-def test_list_manifests_ignores_invalid_payloads(monkeypatch, tmp_path: Path) -> None:
+def test_list_manifests_ignores_invalid_payloads(monkeypatch, tmp_path: Path, caplog) -> None:
monkeypatch.setattr(Path, "home", lambda: tmp_path)
valid = _manifest()
save_manifest(valid)
@@ -99,9 +102,67 @@ def test_list_manifests_ignores_invalid_payloads(monkeypatch, tmp_path: Path) ->
broken_dir.mkdir(parents=True)
(broken_dir / "manifest.json").write_text("{not json", encoding="utf-8")
- manifests = list_manifests()
+ with caplog.at_level(logging.WARNING, logger="headroom.install.state"):
+ manifests = list_manifests()
assert [manifest.profile for manifest in manifests] == ["default"]
+ warnings = [r.getMessage() for r in caplog.records if r.levelno == logging.WARNING]
+ assert any(str(broken_dir / "manifest.json") in message for message in warnings)
+ 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")
+
+
[email protected](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)
+ save_manifest(_manifest())
+ path = tmp_path / ".headroom" / "deploy" / "default" / "manifest.json"
+ assert stat.S_IMODE(path.stat().st_mode) == 0o600
+
+ # A manifest left group/world-readable is tightened on the next save.
+ path.chmod(0o644)
+ save_manifest(_manifest())
+ assert stat.S_IMODE(path.stat().st_mode) == 0o600
def _write_manifest_with_image(profile_dir: Path, image: str) -> None:
diff --git a/tests/test_subscription_tracker.py b/tests/test_subscription_tracker.py
index 481102574..cc2efed0a 100644
--- a/tests/test_subscription_tracker.py
+++ b/tests/test_subscription_tracker.py
@@ -1,6 +1,8 @@
from __future__ import annotations
import json
+import os
+import stat
import sys
import threading
from datetime import datetime, timedelta
@@ -362,6 +364,19 @@ async def test_maybe_poll_runs_transcript_scan_off_event_loop(
assert seen["thread_id"] != loop_thread_id
[email protected](os.name != "posix", reason="mode bits decide access only on POSIX")
+def test_persist_state_keeps_file_owner_only(tmp_path: Path) -> None:
+ persist_path = tmp_path / "tracker-state.json"
+ tracker = SubscriptionTracker(persist_path=persist_path)
+ tracker._persist_state()
+ assert stat.S_IMODE(persist_path.stat().st_mode) == 0o600
+
+ # A state file left group/world-readable is tightened on the next write.
+ persist_path.chmod(0o644)
+ tracker._persist_state()
+ assert stat.S_IMODE(persist_path.stat().st_mode) == 0o600
+
+
@pytest.mark.asyncio
async def test_maybe_poll_logs_a_failed_otel_update(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture