The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(auth): a transient read failure is not corruption
MaxFreedomPollard31032b4f51a0
diff --git a/hermes_cli/auth.py b/hermes_cli/auth.pyindex 5db9dd30610..ebc0af0e45b 100644--- a/hermes_cli/auth.py+++ b/hermes_cli/auth.py@@ -1124,18 +1124,45 @@ def _load_auth_store(auth_file: Optional[Path] = None) -> Dict[str, Any]:try:raw = json.loads(auth_file.read_text(encoding="utf-8"))+ except OSError:+ # The file exists (checked above) but could not be READ: EMFILE under+ # fd exhaustion, EACCES, EIO, a stalled network mount. None of those+ # mean the contents are bad, and this module does read-modify-write in+ # ~15 places, so degrading to an empty store here is one+ # _save_auth_store() away from erasing every stored credential.+ # Fail loudly instead and leave the file on disk untouched.+ logger.warning(+ "auth: could not read %s, leaving the store on disk untouched "+ "rather than degrading to an empty one",+ auth_file, exc_info=True,+ )+ raiseexcept Exception as exc:+ # Genuine corruption: unparseable JSON, or bytes that are not UTF-8.corrupt_path = auth_file.with_suffix(".json.corrupt")+ preserved = Falsetry:import shutilshutil.copy2(auth_file, corrupt_path)+ preserved = Trueexcept Exception:- pass- logger.warning(- "auth: failed to parse %s (%s) — starting with empty store. "- "Corrupt file preserved at %s",- auth_file, exc, corrupt_path,- )+ logger.debug(+ "auth: could not preserve a copy of the corrupt store at %s",+ corrupt_path, exc_info=True,+ )+ if preserved:+ logger.warning(+ "auth: failed to parse %s (%s), starting with empty store. "+ "Corrupt file preserved at %s",+ auth_file, exc, corrupt_path,+ )+ else:+ # Do not advertise a backup that was never written.+ logger.warning(+ "auth: failed to parse %s (%s), starting with empty store. "+ "A copy could NOT be preserved at %s",+ auth_file, exc, corrupt_path,+ )return {"version": AUTH_STORE_VERSION, "providers": {}}if isinstance(raw, dict) and (diff --git a/tests/hermes_cli/test_auth_store_read_failure.py b/tests/hermes_cli/test_auth_store_read_failure.pynew file mode 100644index 00000000000..e48979970b1--- /dev/null+++ b/tests/hermes_cli/test_auth_store_read_failure.py@@ -0,0 +1,98 @@+"""A transient read failure on auth.json must not degrade to an empty store.++``_load_auth_store`` treated every exception as corruption and returned+``{"version": ..., "providers": {}}``. This module does read-modify-write in+roughly fifteen places, so an ``OSError`` (EMFILE under fd exhaustion, EACCES,+EIO, a stalled mount) followed by any ``_save_auth_store`` rewrote auth.json+with an empty provider set and destroyed every stored credential.++Genuine corruption still degrades, still preserves a copy, and now only claims+to have preserved one when the copy actually landed.+"""++import errno+import json+import logging++import pytest++import hermes_cli.auth as auth+++def store_file(tmp_path):+ f = tmp_path / "auth.json"+ f.write_text(+ json.dumps({"version": 1, "providers": {"nous": {"api_key": "secret"}}}),+ encoding="utf-8",+ )+ return f+++def _fail_read(exc):+ def _read(self, *args, **kwargs):+ raise exc+ return _read+++ "exc",+ [+ OSError(errno.EMFILE, "Too many open files"),+ PermissionError(errno.EACCES, "Permission denied"),+ OSError(errno.EIO, "Input/output error"),+ ],+ ids=["emfile", "eacces", "eio"],+)+def test_read_failure_raises_and_leaves_the_store_alone(store_file, monkeypatch, exc):+ from pathlib import Path++ before = store_file.read_bytes()+ monkeypatch.setattr(Path, "read_text", _fail_read(exc))++ with pytest.raises(OSError):+ auth._load_auth_store(store_file)++ assert store_file.read_bytes() == before, "the store on disk was modified"+ assert not store_file.with_suffix(".json.corrupt").exists(), (+ "a read failure is not corruption and must not write a .corrupt sidecar"+ )+++def test_unparseable_json_still_degrades_and_preserves_a_copy(store_file):+ store_file.write_text("{ not json", encoding="utf-8")++ result = auth._load_auth_store(store_file)++ assert result == {"version": auth.AUTH_STORE_VERSION, "providers": {}}+ corrupt = store_file.with_suffix(".json.corrupt")+ assert corrupt.exists(), "genuine corruption must still be preserved"+ assert corrupt.read_text(encoding="utf-8") == "{ not json"+++def test_healthy_store_is_returned_unchanged(store_file):+ result = auth._load_auth_store(store_file)+ assert result["providers"]["nous"]["api_key"] == "secret"+++def test_log_does_not_claim_a_backup_that_was_not_written(+ store_file, monkeypatch, caplog+):+ """The old message advertised the .corrupt path even when copy2 failed."""+ import shutil++ store_file.write_text("{ not json", encoding="utf-8")++ def _no_copy(*args, **kwargs):+ raise OSError(errno.EMFILE, "Too many open files")++ monkeypatch.setattr(shutil, "copy2", _no_copy)++ with caplog.at_level(logging.WARNING, logger="hermes_cli.auth"):+ result = auth._load_auth_store(store_file)++ assert result == {"version": auth.AUTH_STORE_VERSION, "providers": {}}+ assert not store_file.with_suffix(".json.corrupt").exists()+ text = caplog.text+ assert "could NOT be preserved" in text+ assert "Corrupt file preserved at" not in text