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(doctor): don't crash on a valid-but-non-object settings.json (#2482)

Abhay Singh7524854da78b
diff --git a/headroom/cli/doctor.py b/headroom/cli/doctor.py
index 9a970b5e1..5a0a032b4 100644
--- a/headroom/cli/doctor.py
+++ b/headroom/cli/doctor.py
@@ -157,6 +157,17 @@ def check_claude_routing(settings_path: Path, port: int) -> CheckResult:
status=WARN,
summary=f"could not parse {settings_path}: {exc}",
)
+ # `json.loads` succeeds on valid non-object JSON (e.g. `[]`, `null`, `42`),
+ # which a hand-edited or reset settings file can contain. `.get` on a
+ # non-dict raises AttributeError, and it is not one of the caught parse
+ # errors above, so it would crash the very command run to diagnose the
+ # broken config. Treat a non-object like an unparseable file.
+ if not isinstance(payload, dict):
+ return CheckResult(
+ name=name,
+ status=WARN,
+ summary=f"could not parse {settings_path}: not a JSON object",
+ )
base_url = ""
env_block = payload.get("env")
if isinstance(env_block, dict):
@@ -199,7 +210,10 @@ def check_claude_remote_control_gate(
if settings_path.exists():
try:
payload = json.loads(settings_path.read_text(encoding="utf-8"))
- env_block = payload.get("env")
+ # Valid non-object JSON (`[]`, `null`, ...) parses fine but has no
+ # `.get`, and AttributeError is not caught below; guard for dict-ness
+ # so a malformed settings file can't crash the gate check.
+ env_block = payload.get("env") if isinstance(payload, dict) else None
if isinstance(env_block, dict):
settings_env = env_block
settings_base_url = str(env_block.get("ANTHROPIC_BASE_URL", "") or "")
diff --git a/tests/test_cli_doctor.py b/tests/test_cli_doctor.py
index ab8ff91fe..a8cebba3f 100644
--- a/tests/test_cli_doctor.py
+++ b/tests/test_cli_doctor.py
@@ -108,6 +108,16 @@ class TestClaudeRouting:
path.write_text("{not json", encoding="utf-8")
assert check_claude_routing(path, 8787).status == WARN
+ @pytest.mark.parametrize("body", ["[]", "null", "42", '"a string"'])
+ def test_non_object_json_warns(self, tmp_path, body):
+ # Valid JSON that isn't an object parses cleanly, so it slips past the
+ # JSONDecodeError guard; the later payload.get("env") must not crash the
+ # diagnostic command that is being run precisely because the config is
+ # suspect.
+ path = tmp_path / "settings.json"
+ path.write_text(body, encoding="utf-8")
+ assert check_claude_routing(path, 8787).status == WARN
+
def test_no_env_key_warns(self, tmp_path):
path = tmp_path / "settings.json"
path.write_text(json.dumps({"env": {}}), encoding="utf-8")
@@ -172,6 +182,19 @@ class TestClaudeRemoteControlGate:
)
assert check_claude_remote_control_gate(path, {}) is None
+ @pytest.mark.parametrize("body", ["[]", "null", "42"])
+ def test_non_object_settings_does_not_crash(self, tmp_path, body):
+ # A valid-but-non-object settings file parses past the JSONDecodeError
+ # guard; payload.get("env") must not raise AttributeError. The shell env
+ # still drives the gate, so a custom base there still warns.
+ path = tmp_path / "settings.json"
+ path.write_text(body, encoding="utf-8")
+ result = check_claude_remote_control_gate(
+ path, {"ANTHROPIC_BASE_URL": "http://127.0.0.1:8787"}
+ )
+ assert result is not None
+ assert result.status == WARN
+
def test_api_key_auth_suppresses_warning(self, tmp_path):
# Issue #1779: a PAYG / API-key session never had Remote Control, so the
# gate warning must not fire even behind a custom base URL.