The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): handle UnicodeDecodeError in interactive update prompts
ygd5870957591ffb5
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.pyindex 6d6c139633e..45099198bb2 100644--- a/hermes_cli/update_cmd.py+++ b/hermes_cli/update_cmd.py@@ -1258,7 +1258,15 @@ def _restore_stashed_changes(if input_fn is not None:response = input_fn("Restore local changes now? [Y/n]", "y")else:- response = input().strip().lower()+ try:+ response = input().strip().lower()+ except (EOFError, UnicodeDecodeError):+ # Mirror the config-migration prompt's fix: don't let a+ # terminal-encoding issue or a closed stdin crash the+ # update mid-restore. Falls through to the existing+ # skip-restore path below, which already explains how to+ # restore manually from git stash.+ response = "n"if response not in {"", "y", "yes"}:print("Skipped restoring local changes.")print("Your changes are still preserved in git stash.")@@ -1545,7 +1553,7 @@ def _sync_with_upstream_if_needed(git_cmd: list[str], cwd: Path) -> None:response = (input("Add official repo as 'upstream' remote? [Y/n]: ").strip().lower())- except (EOFError, KeyboardInterrupt):+ except (EOFError, KeyboardInterrupt, UnicodeDecodeError):print()response = "n"@@ -4545,6 +4553,16 @@ def _cmd_update_impl(args, gateway_mode: bool):)except EOFError:response = "n"+ except UnicodeDecodeError:+ # input() can raise this when the terminal encoding can't+ # decode the byte sequence (e.g. a non-UTF-8 locale, or an+ # embedded terminal). Without this, the exception escapes+ # here and crashes the update at this prompt.+ print(+ " ⚠ Could not read input (encoding issue). Skipping. "+ "Run 'hermes config migrate' manually to configure."+ )+ response = "n"if response in {"", "y", "yes", "auto"}:print()diff --git a/tests/hermes_cli/test_update_yes_flag.py b/tests/hermes_cli/test_update_yes_flag.pyindex 699d57a9716..1e10ddb5d34 100644--- a/tests/hermes_cli/test_update_yes_flag.py+++ b/tests/hermes_cli/test_update_yes_flag.py@@ -135,3 +135,99 @@ class TestUpdateYesConfigMigration:class TestUpdateYesStashRestore:"""--yes auto-restores the pre-update autostash without prompting."""+++class TestUnicodeDecodeErrorInUpdatePrompts:+ """Regression tests (review of #68497): input() can raise+ UnicodeDecodeError when the terminal encoding can't decode the byte+ sequence (e.g. a non-UTF-8 locale, or an embedded terminal). Three+ interactive update prompts call input() directly -- the config-+ migration prompt, the stash-restore prompt, and the upstream-remote+ prompt -- and each must fail safe (skip, don't crash) rather than let+ the exception escape and crash `hermes update` mid-flight.+ """++ @patch("hermes_cli.config.migrate_config")+ @patch("hermes_cli.config.check_config_version", return_value=(1, 2))+ @patch("hermes_cli.config.get_missing_config_fields", return_value=[])+ @patch("hermes_cli.config.get_missing_env_vars", return_value=["NEW_KEY"])+ @patch("shutil.which", return_value=None)+ @patch("subprocess.run")+ def test_unicode_decode_error_in_tty_skips_and_prints_hint(+ self,+ mock_run,+ _mock_which,+ _mock_missing_env,+ _mock_missing_cfg,+ _mock_version,+ mock_migrate,+ capsys,+ ):+ mock_run.side_effect = _make_run_side_effect(+ branch="main", verify_ok=True, commit_count="1"+ )+ mock_migrate.return_value = {"env_added": [], "config_added": []}+ args = SimpleNamespace(yes=False)++ import sys as _sys++ with patch(+ "builtins.input",+ side_effect=UnicodeDecodeError("utf-8", b"\xff", 0, 1, "invalid byte"),+ ), patch.object(_sys.stdin, "isatty", return_value=True), patch.object(+ _sys.stdout, "isatty", return_value=True+ ):+ cmd_update(args) # must not raise++ out = capsys.readouterr().out+ assert "hermes config migrate" in out+ assert mock_migrate.call_count == 0 or (+ mock_migrate.call_args and mock_migrate.call_args.kwargs.get("interactive") is not True+ )++ def test_stash_restore_unicode_decode_error_falls_through_to_skip(self, tmp_path, capsys):+ from hermes_cli.update_cmd import _restore_stashed_changes++ with patch(+ "builtins.input",+ side_effect=UnicodeDecodeError("utf-8", b"\xff", 0, 1, "invalid byte"),+ ):+ result = _restore_stashed_changes(+ ["git"], tmp_path, "stash@{0}", prompt_user=True, input_fn=None,+ ) # must not raise++ assert result is False+ out = capsys.readouterr().out+ assert "Skipped restoring local changes" in out+ assert "git stash apply stash@{0}" in out++ def test_stash_restore_eof_error_still_falls_through_to_skip(self, tmp_path):+ """Sanity: this fix must not regress the pre-existing EOFError case,+ which the raw input() path had no guard for at all before this fix."""+ from hermes_cli.update_cmd import _restore_stashed_changes++ with patch("builtins.input", side_effect=EOFError()):+ result = _restore_stashed_changes(+ ["git"], tmp_path, "stash@{0}", prompt_user=True, input_fn=None,+ ) # must not raise++ assert result is False++ def test_upstream_remote_prompt_unicode_decode_error_falls_through_to_skip(+ self, tmp_path+ ):+ from hermes_cli.update_cmd import _sync_with_upstream_if_needed++ with patch(+ "hermes_cli.update_cmd._has_upstream_remote", return_value=False+ ), patch(+ "hermes_cli.update_cmd._should_skip_upstream_prompt", return_value=False+ ), patch(+ "hermes_cli.update_cmd._add_upstream_remote"+ ) as mock_add, patch(+ "builtins.input",+ side_effect=UnicodeDecodeError("utf-8", b"\xff", 0, 1, "invalid byte"),+ ):+ _sync_with_upstream_if_needed(["git"], tmp_path) # must not raise++ mock_add.assert_not_called()