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.

fix(mcp): accept an empty install ledger instead of failing mutations as malformed (#3235)

Abhay Singh19ddfe1a56ec
diff --git a/headroom/mcp_registry/ledger.py b/headroom/mcp_registry/ledger.py
index e802227e7..b7fcdba91 100644
--- a/headroom/mcp_registry/ledger.py
+++ b/headroom/mcp_registry/ledger.py
@@ -127,6 +127,16 @@ def _read_ledger(path: Path, *, for_mutation: bool = False) -> dict[str, Any]:
return {}
if for_mutation:
for section in ("agents",):
+ # An *absent* section is a valid empty ledger, not a malformed one.
+ # ``clear_install`` pops ``agents`` once its last entry is removed, so
+ # a fully-unwrapped ledger is exactly ``{}`` — and ``record_install``
+ # likewise treats a missing ``agents`` as empty. Rejecting it here made
+ # the very next mutation (e.g. ``headroom mcp adopt``) fail with a
+ # bogus "malformed" error after an ordinary unwrap. A section that is
+ # *present* but not a dict (e.g. ``{"agents": null}``) is still
+ # malformed — so key-absence, not a ``None`` value, is what is skipped.
+ if section not in data:
+ continue
section_data = data.get(section)
if not isinstance(section_data, dict) or any(
not isinstance(agent_entry, dict)
diff --git a/tests/test_mcp_registry/test_ledger.py b/tests/test_mcp_registry/test_ledger.py
index aa7ff08dc..406fefca5 100644
--- a/tests/test_mcp_registry/test_ledger.py
+++ b/tests/test_mcp_registry/test_ledger.py
@@ -55,6 +55,28 @@ def test_mutation_preflight_rejects_unsafe_shapes(tmp_path, value):
validate_ledger_for_mutation(ledger)
[email protected]("contents", ["{}", '{"agents": {}}'])
+def test_mutation_preflight_accepts_empty_ledger(tmp_path, contents):
+ """An empty ledger must not be rejected as malformed.
+
+ ``clear_install`` pops the ``agents`` section once its last entry is removed,
+ so a fully-unwrapped ledger is exactly ``{}``. Rejecting it made the next
+ mutation (``headroom mcp adopt``) fail with a bogus "malformed" error.
+ """
+ ledger = tmp_path / "mcp_installs.json"
+ ledger.write_text(contents)
+ validate_ledger_for_mutation(ledger) # must not raise
+
+
+def test_mutation_preflight_accepts_ledger_after_clearing_last_install(tmp_path):
+ ledger = tmp_path / "mcp_installs.json"
+ spec = _spec()
+ record_install("claude", spec, path=ledger)
+ clear_install("claude", "serena", path=ledger)
+ assert ledger.read_text().strip() == "{}"
+ validate_ledger_for_mutation(ledger) # must not raise
+
+
def test_mutation_preflight_rejects_unreadable_ledger(monkeypatch, tmp_path):
ledger = tmp_path / "mcp_installs.json"
ledger.write_text('{"agents": {}}')