The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cli): cancel /prompt compose on editor failure instead of shell fallback
Brooklyn Nicholson4b707a0f2b47
diff --git a/hermes_cli/cli_commands_mixin.py b/hermes_cli/cli_commands_mixin.pyindex 2a1d7dcc422..a1ed54c9b42 100644--- a/hermes_cli/cli_commands_mixin.py+++ b/hermes_cli/cli_commands_mixin.py@@ -2392,10 +2392,16 @@ class CLICommandsMixin:if initial_text:fh.write(initial_text)try:- subprocess.call([*shlex.split(editor), path])- except Exception:- # Fall back to a bare invocation (editor value may not be argv-splittable everywhere).- subprocess.call(f"{editor} {shlex.quote(path)}", shell=True)+ editor_argv = [*shlex.split(editor), path]+ except ValueError:+ return "" # unbalanced quotes in $EDITOR: cancel, never retry through a shell+ try:+ status = subprocess.call(editor_argv)+ except OSError:+ return "" # editor not runnable: cancel the compose (#81364)+ # A failed editor may leave seeded or abandoned text in the buffer.+ if status != 0:+ return ""with open(path, "r", encoding="utf-8-sig") as fh:raw = fh.read()finally:diff --git a/tests/hermes_cli/test_prompt_compose_command.py b/tests/hermes_cli/test_prompt_compose_command.pyindex 96cc492eb64..c93eb3e7990 100644--- a/tests/hermes_cli/test_prompt_compose_command.py+++ b/tests/hermes_cli/test_prompt_compose_command.py@@ -7,9 +7,8 @@ uses). These drive a fake editor subprocess to verify read-back, headerstripping, seeding, and the empty-buffer cancel path."""-import os-import stat-import tempfile+import sys+from pathlib import Pathimport pytest@@ -21,29 +20,27 @@ class _Stub(CLICommandsMixin):self._pending_agent_seed = None-def _fake_editor(body: str, mode: str = "append") -> str:- """Write a tiny shell 'editor' that mutates the file it is handed."""- f = tempfile.NamedTemporaryFile("w", suffix=".sh", delete=False)- if mode == "append":- f.write("#!/usr/bin/env bash\n")- f.write(f"cat >> \"$1\" <<'EOF'\n{body}\nEOF\n")- else: # clear- f.write("#!/usr/bin/env bash\n: > \"$1\"\n")- f.close()- os.chmod(f.name, os.stat(f.name).st_mode | stat.S_IEXEC)- return f.name+def _fake_editor(tmp_path, body: str, mode: str = "append", exit_code: int = 0) -> str:+ """Write a tiny python 'editor' that mutates the file it is handed."""+ script = tmp_path / "editor.py"+ script.write_text(+ "import sys\nfrom pathlib import Path\n"+ "path = Path(sys.argv[1])\n"+ + (f"path.write_text(path.read_text(encoding='utf-8') + {body!r}, encoding='utf-8')\n"+ if mode == "append" else "path.write_text('', encoding='utf-8')\n")+ + f"sys.exit({exit_code})\n", encoding="utf-8")+ return f'"{Path(sys.executable).as_posix()}" "{script.as_posix()}"'@pytest.fixture(autouse=True)def _no_visual(monkeypatch):monkeypatch.delenv("VISUAL", raising=False)--+ monkeypatch.delenv("EDITOR", raising=False)@pytest.mark.platforms("linux")-def test_compose_reads_and_strips_header(monkeypatch):- monkeypatch.setenv("EDITOR", _fake_editor("Refactor the auth module.\nUse pytest."))+def test_compose_reads_and_strips_header(tmp_path, monkeypatch):+ monkeypatch.setenv("EDITOR", _fake_editor(tmp_path, "Refactor the auth module.\nUse pytest."))out = _Stub()._compose_in_editor("")assert "Refactor the auth module." in outassert "Use pytest." in out@@ -51,8 +48,42 @@ def test_compose_reads_and_strips_header(monkeypatch):@pytest.mark.platforms("linux")-def test_empty_buffer_does_not_seed(monkeypatch):- monkeypatch.setenv("EDITOR", _fake_editor("", mode="clear"))+def test_empty_buffer_does_not_seed(tmp_path, monkeypatch):+ monkeypatch.setenv("EDITOR", _fake_editor(tmp_path, "", mode="clear"))s = _Stub()s._handle_prompt_compose_command("/prompt")assert s._pending_agent_seed is None+++def test_editor_failure_never_falls_back_to_shell(monkeypatch):+ """An editor the argv path cannot run must not retry through the shell.++ The old fallback re-invoked ``$EDITOR`` via ``shell=True`` with the editor+ string unquoted, so a crafted EDITOR value executed as shell code. Now a+ failed editor simply cancels the compose (#81364).+ """+ import subprocess as _sp++ calls = []++ def _record(args, **kwargs):+ calls.append((args, kwargs))+ raise OSError("editor not runnable")++ monkeypatch.setattr(_sp, "call", _record)+ monkeypatch.setenv("EDITOR", "nonexistent-editor; touch /tmp/pwned")+ assert _Stub()._compose_in_editor("") == ""+ assert calls, "argv invocation should have been attempted"+ for argv, kwargs in calls:+ assert kwargs.get("shell") is not True+ monkeypatch.setenv("EDITOR", '"unterminated')+ assert _Stub()._compose_in_editor("seed") == ""+++def test_nonzero_editor_exit_cancels_even_with_buffer_content(tmp_path, monkeypatch):+ """A failed editor may leave seeded or abandoned text in the buffer — cancel."""+ monkeypatch.setenv("EDITOR", _fake_editor(tmp_path, "typed but abandoned\n", exit_code=3))+ assert _Stub()._compose_in_editor("seed text from the command line") == ""+ stub = _Stub()+ stub._handle_prompt_compose_command("/prompt draft this")+ assert stub._pending_agent_seed is None