The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cli): don't serve stale update-check results after fetch failure (#82166)
Shakti Prasad Mohapatra55d50d5c9249
diff --git a/hermes_cli/banner.py b/hermes_cli/banner.pyindex 259c454bab8..5c2cd805f68 100644--- a/hermes_cli/banner.py+++ b/hermes_cli/banner.py@@ -340,13 +340,22 @@ def _check_via_local_git(repo_dir: Path) -> Optional[int]:if is_shallow:fetch_args += ["--depth", "1"]fetch_args.append("--quiet")- subprocess.run(+ fetch_proc = subprocess.run(fetch_args,capture_output=True, timeout=10,cwd=str(repo_dir),)+ fetch_ok = fetch_proc.returncode == 0except Exception:- pass # Offline or timeout — use stale refs, that's fine+ fetch_ok = False # Offline or timeout — don't use stale refs++ # When the fetch fails, the local origin/main tracking ref is stale.+ # Comparing HEAD against it can report "0 behind" (up to date) even when+ # upstream has moved forward — the exact stale-cache symptom in #82166.+ # Return None so the caller knows the check was inconclusive and doesn't+ # cache a false "up to date".+ if not fetch_ok:+ return Noneif is_shallow:# No history to count across the shallow boundary. `origin/main` may not@@ -444,10 +453,16 @@ def check_for_updates() -> Optional[int]:behind = _check_via_local_git(repo_dir)try:- cache_file.write_text(- json.dumps({"ts": now, "behind": behind, "rev": embedded_rev, "ver": VERSION}),- encoding="utf-8",- )+ # Don't cache inconclusive results (None). A None means the check+ # could not run — typically a failed git fetch. Caching None would+ # suppress retries for the full 6-hour cache window, leaving the+ # user with a stale "up to date" or no information for hours after+ # connectivity is restored (#82166).+ if behind is not None:+ cache_file.write_text(+ json.dumps({"ts": now, "behind": behind, "rev": embedded_rev, "ver": VERSION}),+ encoding="utf-8",+ )except Exception:passdiff --git a/tests/hermes_cli/test_update_check.py b/tests/hermes_cli/test_update_check.pyindex d6af37b6b06..05143aa7a35 100644--- a/tests/hermes_cli/test_update_check.py+++ b/tests/hermes_cli/test_update_check.py@@ -58,5 +58,100 @@ def test_prefetch_non_blocking():assert banner._update_result == 5+def test_check_via_local_git_fetch_failure_returns_none(tmp_path, monkeypatch):+ """When git fetch fails, _check_via_local_git must return None instead of+ comparing against stale origin/main refs (#82166).++ Without this fix, a fetch failure silently falls through to+ ``git rev-list --count HEAD..origin/main`` using the stale tracking ref,+ which can report 0 (up to date) even when upstream has moved forward.+ """+ from hermes_cli import banner++ repo_dir = tmp_path / "hermes-agent"+ repo_dir.mkdir()+ (repo_dir / ".git").mkdir()++ # Simulate a non-shallow, non-SSH-remote checkout+ def mock_git_stdout(args, *, cwd, timeout=5):+ if args[:2] == ["remote", "get-url"]:+ return "https://github.com/NousResearch/hermes-agent.git"+ if args[:2] == ["rev-parse", "--is-shallow-repository"]:+ return "false"+ return None++ # Simulate fetch failure (returncode != 0)+ failed_proc = MagicMock()+ failed_proc.returncode = 1+ failed_proc.stdout = ""+ failed_proc.stderr = "fatal: could not reach remote"++ monkeypatch.setattr(banner, "_git_stdout", mock_git_stdout)+ monkeypatch.setattr(banner.subprocess, "run", MagicMock(return_value=failed_proc))++ result = banner._check_via_local_git(repo_dir)+ assert result is None, "Fetch failure must return None, not a stale behind-count"+++def test_check_for_updates_does_not_cache_none(tmp_path, monkeypatch):+ """check_for_updates must not cache None results so a transient fetch+ failure doesn't suppress retries for the full 6-hour cache window (#82166).++ Instead of mocking the full Path resolution chain, we verify the cache-write+ guard directly: call check_for_updates with a mocked _check_via_local_git+ that returns None, and confirm no cache file is created.+ """+ import hermes_cli.banner as banner++ cache_file = tmp_path / ".update_check"+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))+ monkeypatch.delenv("HERMES_REVISION", raising=False)++ # Create a fake repo dir so the .git check passes+ repo_dir = tmp_path / "hermes-agent"+ repo_dir.mkdir()+ (repo_dir / ".git").mkdir()++ # Mock the internal functions to force the local-git path returning None+ monkeypatch.setattr(banner, "_check_via_local_git", lambda rd: None)+ monkeypatch.setattr(+ "hermes_cli.config.detect_install_method", lambda root: "git"+ )+ monkeypatch.setattr(+ "hermes_cli.config.get_project_root", lambda: repo_dir+ )++ # Patch __file__ resolution by monkeypatching the module's Path calls.+ # check_for_updates does: Path(__file__).parent.parent.resolve()+ # We intercept by making the resolve() return our fake repo_dir.+ original_init = Path.__init__++ def patched_path_init(self, *args, **kwargs):+ original_init(self, *args, **kwargs)++ # Simpler: just patch the get_hermes_home and the repo_dir resolution+ # by making check_for_updates find our fake repo via hermes_home fallback.+ # The code checks Path(__file__).parent.parent/.git first, then falls+ # back to hermes_home / "hermes-agent". We ensure the fallback hits.+ # To do this, we make Path(__file__).parent.parent.resolve() return+ # a path without .git, so it falls through to hermes_home / "hermes-agent".+ real_resolve = Path.resolve++ def fake_resolve(self, *args, **kwargs):+ s = str(self)+ if "banner.py" in s or s.endswith("hermes_cli"):+ # Return a path that has no .git, forcing the fallback+ return tmp_path / "no-git-here"+ return real_resolve(self, *args, **kwargs)++ monkeypatch.setattr(Path, "resolve", fake_resolve)++ result = banner.check_for_updates()+ assert result is None++ # The cache file must NOT have been written with a None result+ assert not cache_file.exists(), "None result must not be cached"++