Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions hermes_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,18 @@ def cmd_chat(args):
print("You can run 'hermes setup' at any time to configure.")
sys.exit(1)

# Interactive prompt_toolkit chat requires a real terminal for stdin.
# In headless/non-PTY invocations (regular file/pipe stdin), kqueue on macOS
# can raise OSError(EINVAL) from loop.add_reader(fd=0). Fail early with a
# clear message, while still allowing `hermes chat -q ...` / `--image ...`
# automation paths to run without a TTY.
if (
not use_tui
and not getattr(args, "query", None)
and not getattr(args, "image", None)
):
_require_tty("chat")

# Start update check in background (runs while other init happens)
try:
from hermes_cli.banner import prefetch_update_check
Expand Down
44 changes: 44 additions & 0 deletions tests/hermes_cli/test_setup_noninteractive.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def _make_chat_args(**overrides):
pass_session_id=overrides.get("pass_session_id", False),
quiet=overrides.get("quiet", False),
checkpoints=overrides.get("checkpoints", False),
image=overrides.get("image", None),
tui=overrides.get("tui", False),
)


Expand Down Expand Up @@ -144,6 +146,48 @@ def test_chat_first_run_headless_skips_setup_prompt(self, capsys):
out = capsys.readouterr().out
assert "hermes config set model.provider custom" in out

def test_chat_headless_interactive_exits_before_prompt_toolkit(self, capsys):
"""Bare `hermes` with configured provider needs a TTY instead of crashing in prompt_toolkit."""
from hermes_cli.main import cmd_chat

args = _make_chat_args()

with (
patch("hermes_cli.main._has_any_provider_configured", return_value=True),
patch("hermes_cli.main._resolve_session_by_name_or_id", return_value=None),
patch("cli.main", side_effect=AssertionError("prompt_toolkit should not start")),
patch("sys.stdin") as mock_stdin,
):
mock_stdin.isatty.return_value = False
with pytest.raises(SystemExit) as exc:
cmd_chat(args)

assert exc.value.code == 1
err = capsys.readouterr().err
assert "hermes chat" in err
assert "requires an interactive terminal" in err

def test_chat_headless_single_query_still_allowed(self):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the equivalent no-TTY image=... case here. cli.py:16043 treats an image without -q as one-shot mode too, and the production guard explicitly exempts it.

"""`hermes chat -q` is the supported non-interactive automation path."""
from hermes_cli.main import cmd_chat

args = _make_chat_args(query="hello")
captured = {}

def fake_cli_main(**kwargs):
captured.update(kwargs)

with (
patch("hermes_cli.main._has_any_provider_configured", return_value=True),
patch("tools.skills_sync.sync_skills"),
patch("cli.main", side_effect=fake_cli_main),
patch("sys.stdin") as mock_stdin,
):
mock_stdin.isatty.return_value = False
cmd_chat(args)

assert captured["query"] == "hello"

def test_main_accepts_tts_setup_section(self, monkeypatch):
"""`hermes setup tts` should parse and dispatch like other setup sections."""
from hermes_cli import main as main_mod
Expand Down