diff --git a/hermes_cli/oneshot.py b/hermes_cli/oneshot.py index 1dd24951c..f66d71c62 100644 --- a/hermes_cli/oneshot.py +++ b/hermes_cli/oneshot.py @@ -174,28 +174,51 @@ def run_oneshot( # Redirect stderr AND stdout to devnull for the entire call tree. # We'll print the final response to the real stdout at the end. real_stdout = sys.stdout + real_stderr = sys.stderr devnull = open(os.devnull, "w", encoding="utf-8") + response: Optional[str] = None + failure: BaseException | None = None try: with redirect_stdout(devnull), redirect_stderr(devnull): - response = _run_agent( - prompt, - model=model, - provider=provider, - toolsets=explicit_toolsets, - use_config_toolsets=use_config_toolsets, - ) + try: + response = _run_agent( + prompt, + model=model, + provider=provider, + toolsets=explicit_toolsets, + use_config_toolsets=use_config_toolsets, + ) + except BaseException as exc: # noqa: BLE001 + # Capture anything that escapes the agent (including OSError + # from prompt_toolkit/Vt100 when stdout is a non-TTY pipe, + # KeyboardInterrupt, SystemExit, etc.) so we can surface it on + # the real stderr instead of crashing past the redirect with a + # traceback that the caller never sees. A silent exit in a + # cron / SSH / subprocess context is the worst failure mode. + # See #30623. + failure = exc finally: try: devnull.close() except Exception: pass - if not (response or "").strip(): - sys.stderr.write("hermes -z: no final response was produced; treating the run as failed.\n") - sys.stderr.flush() + if failure is not None: + # Re-raise control-flow exceptions so the parent handles them as usual + # (Ctrl-C / explicit sys.exit() inside the agent). + if isinstance(failure, (KeyboardInterrupt, SystemExit)): + raise failure + real_stderr.write(f"hermes -z: agent failed: {failure}\n") + real_stderr.flush() return 1 + if not (response or "").strip(): + real_stderr.write("hermes -z: no final response was produced; treating the run as failed.\n") + real_stderr.flush() + return 1 + + assert response is not None # narrowed by the empty-response guard above real_stdout.write(response) if not response.endswith("\n"): real_stdout.write("\n") diff --git a/scripts/release.py b/scripts/release.py index a5f8fcb10..30d0d84d6 100755 --- a/scripts/release.py +++ b/scripts/release.py @@ -658,6 +658,7 @@ AUTHOR_MAP = { "incharge.automation@gmail.com": "inchargeautomation-lab", "danielrpike9@gmail.com": "Bartok9", "96944678+ymylive@users.noreply.github.com": "sweetcornna", + "laflamme@illinoisalumni.org": "briancl2", "skozyuk@cruxexperts.com": "CruxExperts", "154585401+LeonSGP43@users.noreply.github.com": "LeonSGP43", "12250313+Kailigithub@users.noreply.github.com": "Kailigithub", diff --git a/tests/hermes_cli/test_tui_resume_flow.py b/tests/hermes_cli/test_tui_resume_flow.py index ef002c9af..d15d67c00 100644 --- a/tests/hermes_cli/test_tui_resume_flow.py +++ b/tests/hermes_cli/test_tui_resume_flow.py @@ -662,6 +662,36 @@ def test_oneshot_prints_nonempty_final_response(monkeypatch, capsys): assert captured.err == "" +def test_oneshot_fails_closed_on_agent_exception(monkeypatch, capsys): + _stub_plugin_discovery(monkeypatch) + import hermes_cli.oneshot as oneshot_mod + + def _boom(*_args, **_kwargs): + raise OSError("not a TTY") + + monkeypatch.setattr(oneshot_mod, "_run_agent", _boom) + + assert oneshot_mod.run_oneshot("hello") == 1 + captured = capsys.readouterr() + assert captured.out == "" + assert "agent failed" in captured.err + assert "not a TTY" in captured.err + + +def test_oneshot_reraises_keyboard_interrupt(monkeypatch): + _stub_plugin_discovery(monkeypatch) + import hermes_cli.oneshot as oneshot_mod + import pytest as _pytest + + def _interrupt(*_args, **_kwargs): + raise KeyboardInterrupt + + monkeypatch.setattr(oneshot_mod, "_run_agent", _interrupt) + + with _pytest.raises(KeyboardInterrupt): + oneshot_mod.run_oneshot("hello") + + def test_oneshot_filters_invalid_toolsets_before_redirect(monkeypatch, capsys): _stub_plugin_discovery(monkeypatch) from hermes_cli.oneshot import _validate_explicit_toolsets