Skip to content

The agent that grows with you

fix(terminal): restore the exit status without re-firing the user's ERR trap

Tekniumc98e8dc63a4b
diff --git a/tests/tools/test_terminal_pipeline_status.py b/tests/tools/test_terminal_pipeline_status.py
index 980fd6d948d..af05aa29e0c 100644
--- a/tests/tools/test_terminal_pipeline_status.py
+++ b/tests/tools/test_terminal_pipeline_status.py
@@ -63,6 +63,7 @@ def _seen(result: subprocess.CompletedProcess) -> tuple[int, str, str]:
("echo hi \\", 0, "hi \\\n"),
("cat <<'EOF'\nunterminated heredoc", 0, "unterminated heredoc\n"),
("exit 3", 3, ""),
+ ("trap 'echo T' ERR; false", 1, "T\n"),
],
)
def test_capturing_pipestatus_changes_nothing_the_model_sees(command, exit_code, stdout):
diff --git a/tools/environments/base_session_env.py b/tools/environments/base_session_env.py
index 1eed1d58bc2..f399d4ed645 100644
--- a/tools/environments/base_session_env.py
+++ b/tools/environments/base_session_env.py
@@ -61,9 +61,10 @@ def _eval_capturing_pipestatus(escaped: str) -> str:
The array must be read inside the same ``eval`` (afterwards it holds only ``eval``'s
status), while ``$?`` stays the exit code: the two differ for ``!``, ``if``/``while``,
``pipefail`` and parse errors. The blank line stops the capture joining a command
- continued by a trailing backslash.
+ continued by a trailing backslash. The restore sits on the left of ``&&`` so its
+ non-zero status fires neither a user's ``ERR`` trap a second time nor ``set -e``.
"""
- return f"eval '{escaped}\n\n__hermes_rc=$? __hermes_ps=(\"${{PIPESTATUS[@]}}\"); (exit $__hermes_rc)'"
+ return f"eval '{escaped}\n\n__hermes_rc=$? __hermes_ps=(\"${{PIPESTATUS[@]}}\"); (exit $__hermes_rc) && :'"
def _can_capture_pipestatus(command: str) -> bool: