Skip to content

The agent that grows with you

fix(cli): widen prompt_toolkit fallback to catch any runtime failure

kshitijk4poor45aa0dc33e38
diff --git a/hermes_cli/cli_output.py b/hermes_cli/cli_output.py
index 3aab6ff2e36..7de9198755a 100644
--- a/hermes_cli/cli_output.py
+++ b/hermes_cli/cli_output.py
@@ -61,12 +61,16 @@ def line_input(prompt_text: str) -> str:
try:
return prompt_toolkit_prompt(ANSI(prompt_text))
- except OSError:
+ except (KeyboardInterrupt, EOFError):
+ raise
+ except Exception:
# Some terminals report isatty() == True yet reject registering stdin
# with the asyncio event-loop selector (observed on macOS, where kqueue
# raises EINVAL / "Invalid argument" for fd 0). prompt_toolkit cannot
# attach its input there, so fall back to the built-in line reader,
- # which needs no selector and works in cooked mode.
+ # which needs no selector and works in cooked mode. Any prompt_toolkit
+ # runtime failure (OSError, ValueError, RuntimeError) degrades the same
+ # way — the wizard proceeds instead of crashing.
return input(prompt_text)
diff --git a/tests/hermes_cli/test_cli_output.py b/tests/hermes_cli/test_cli_output.py
index ccd8f69b01a..5a75aecbaaa 100644
--- a/tests/hermes_cli/test_cli_output.py
+++ b/tests/hermes_cli/test_cli_output.py
@@ -70,6 +70,30 @@ def test_line_input_falls_back_to_input_when_prompt_toolkit_raises_oserror(monke
assert seen["prompt"] == "Choice [1/2]: "
+def test_line_input_falls_back_to_input_on_any_prompt_toolkit_failure(monkeypatch):
+ """Any prompt_toolkit runtime failure — not just OSError — degrades to
+ input() so the wizard keeps running. ValueError and RuntimeError can
+ arise from exotic stream wrappers or event-loop issues that share the
+ same root cause: prompt_toolkit cannot attach stdin on this terminal.
+ """
+ seen = {}
+
+ def raising_prompt(*_args, **_kwargs):
+ raise ValueError("Invalid file descriptor")
+
+ def fake_input(prompt_text):
+ seen["prompt"] = prompt_text
+ return "fallback-value"
+
+ monkeypatch.setattr(cli_output.sys, "stdin", _TTY())
+ monkeypatch.setattr(cli_output.sys, "stdout", _TTY())
+ monkeypatch.setattr("prompt_toolkit.prompt", raising_prompt)
+ monkeypatch.setattr("builtins.input", fake_input)
+
+ assert cli_output.line_input("Enter token: ") == "fallback-value"
+ assert seen["prompt"] == "Enter token: "
+
+
def test_password_prompt_uses_masked_secret_prompt(monkeypatch):
seen = {}