Skip to content

The agent that grows with you

fix(agent): keep shell exit codes when recording pipelines

BrokeSkill2b7ab9371136
diff --git a/agent/verification_evidence.py b/agent/verification_evidence.py
index 5f2b8b5d995..32c12dca6ce 100644
--- a/agent/verification_evidence.py
+++ b/agent/verification_evidence.py
@@ -244,7 +244,7 @@ def _segment_status(
),
default=sequence_start,
)
- if pipeline_status and match_index == pipeline_start:
+ if pipeline_status and len(pipeline_status) >= 2 and match_index == pipeline_start:
return pipeline_status[0]
return None
if not operators:
diff --git a/agent/verification_stop.py b/agent/verification_stop.py
index 6a3ecf7eaca..f5f835fc6a4 100644
--- a/agent/verification_stop.py
+++ b/agent/verification_stop.py
@@ -16,9 +16,9 @@ _MAX_CHANGED_PATHS_IN_NUDGE = 8
_MAX_STATUS_SUMMARY_CHARS = 1200
_UNPIPED_RUN_WARNING = (
- " Run the check bare: no `| tail`, `| head`, or `| grep` filter. A piped run's exit "
- "status belongs to the filter, not the check, so it cannot count as evidence; the "
- "terminal already truncates long output and spills the rest to a file."
+ " Run the check directly. If you pipe it, keep the check as the first command in "
+ "the pipeline so its exit status can be attributed; the terminal already truncates "
+ "long output and spills the rest to a file."
)
# Prose/data extensions and extension-less prose filenames (case-insensitive) with
diff --git a/tests/agent/test_verification_evidence.py b/tests/agent/test_verification_evidence.py
index 1fa91d2e035..1fb86b6740f 100644
--- a/tests/agent/test_verification_evidence.py
+++ b/tests/agent/test_verification_evidence.py
@@ -117,6 +117,20 @@ def test_piped_verifier_without_pipeline_status_is_not_evidence(tmp_path, monkey
) is None
[email protected]("pipeline_status", [[0], [1]])
+def test_piped_verifier_with_single_pipeline_status_is_not_evidence(
+ tmp_path, monkeypatch, pipeline_status,
+):
+ """A one-element PIPESTATUS belongs to a compound command (``!``, ``if``), not a pipe."""
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
+ _python_project(tmp_path)
+
+ assert classify_verification_command(
+ "pytest -q 2>&1 | tail -20", cwd=tmp_path, session_id="s1",
+ exit_code=0, pipeline_status=pipeline_status,
+ ) is None
+
+
@pytest.mark.parametrize(
"command",
[
diff --git a/tests/agent/test_verification_stop.py b/tests/agent/test_verification_stop.py
index 10a4c3c1dd3..865390e2723 100644
--- a/tests/agent/test_verification_stop.py
+++ b/tests/agent/test_verification_stop.py
@@ -182,8 +182,8 @@ def test_path_under_a_deleted_directory_does_not_nudge(tmp_path, monkeypatch):
assert build_verify_on_stop_nudge(session_id="s1", changed_paths=[gone]) is None
-def test_nudge_demands_an_unpiped_verification_run(tmp_path, monkeypatch):
- """A piped run reports the filter's exit status, so the ledger can never record it."""
+def test_nudge_asks_for_an_attributable_pipeline(tmp_path, monkeypatch):
+ """A piped run counts only when the check leads the pipeline."""
monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
_node_project(tmp_path)
changed = _edited(tmp_path, "src/app.ts")
@@ -192,7 +192,7 @@ def test_nudge_demands_an_unpiped_verification_run(tmp_path, monkeypatch):
nudge = build_verify_on_stop_nudge(session_id="s1", changed_paths=[changed])
assert nudge is not None
- assert "no `| tail`" in nudge
+ assert "keep the check as the first command in the pipeline" in nudge
# ---------------------------------------------------------------------------
# Fix C: documentation/prose edits carry no verifiable behavior and must never
diff --git a/tests/hermes_cli/test_agent_env_advertisement.py b/tests/hermes_cli/test_agent_env_advertisement.py
index aaf0bd18856..7251be96f8c 100644
--- a/tests/hermes_cli/test_agent_env_advertisement.py
+++ b/tests/hermes_cli/test_agent_env_advertisement.py
@@ -53,6 +53,7 @@ class TestWrapCommandAdvertisesHarness:
env._snapshot_ready = False
env._session_id = "testsession0"
env._cwd_marker = "__HERMES_CWD_testsession0__"
+ env._pipe_marker = "__HERMES_PIPE_testsession0__"
env._snapshot_path = "/tmp/hermes-snap-testsession0.sh"
env._snapshot_passthrough_names = set()
return env._wrap_command(command, "/tmp")
diff --git a/tests/tools/test_docker_environment.py b/tests/tools/test_docker_environment.py
index 1cc01ca8ac4..0d1418f951b 100644
--- a/tests/tools/test_docker_environment.py
+++ b/tests/tools/test_docker_environment.py
@@ -115,6 +115,7 @@ def _make_execute_only_env(forward_env=None):
env._snapshot_path = "/tmp/hermes-snap-test123.sh"
env._cwd_file = "/tmp/hermes-cwd-test123.txt"
env._cwd_marker = "__HERMES_CWD_test123__"
+ env._pipe_marker = "__HERMES_PIPE_test123__"
env._snapshot_ready = True
env._last_sync_time = None
env._init_env_args = []
diff --git a/tests/tools/test_terminal_pipeline_status.py b/tests/tools/test_terminal_pipeline_status.py
index e0b5d43b37d..c236869cb6e 100644
--- a/tests/tools/test_terminal_pipeline_status.py
+++ b/tests/tools/test_terminal_pipeline_status.py
@@ -2,6 +2,8 @@
import subprocess
+import pytest
+
from tools.environments.base_session_env import _wrap_command_script
CWD_MARKER = "__HERMES_CWD_test__"
@@ -16,8 +18,12 @@ def _script(command: str) -> str:
)
+def _run_result(command: str) -> subprocess.CompletedProcess:
+ return subprocess.run(["bash", "-c", _script(command)], capture_output=True, text=True, check=False)
+
+
def _run(command: str) -> str:
- return subprocess.run(["bash", "-c", _script(command)], capture_output=True, text=True, check=False).stdout
+ return _run_result(command).stdout
def test_pipeline_status_captures_the_leading_command():
@@ -32,8 +38,33 @@ def test_plain_command_reports_one_status():
assert f"{PIPE_MARKER}1{PIPE_MARKER}" in output
-def test_wrapper_captures_pipestatus_before_anything_else():
- script = _script("echo hi")
+ ("command", "expected"),
+ [
+ ("if false; then echo yes; fi", 0),
+ ("while read line; do echo \"$line\"; done < /dev/null", 0),
+ ("set -o pipefail; false | true", 1),
+ ("! false", 0),
+ ("! true", 1),
+ ("echo 'unbalanced", 2),
+ ("echo hi \\", 0),
+ ("exit 3", 3),
+ ],
+)
+def test_wrapper_preserves_shell_exit_code(command, expected):
+ """``PIPESTATUS`` is side data: the wrapper must return the shell's own ``$?``."""
+ assert _run_result(command).returncode == expected
+
+
+def test_wrapper_does_not_leak_internals_on_syntax_error():
+ result = _run_result("echo 'unbalanced")
+
+ assert "__hermes_ps" not in result.stderr
+
+
+def test_wrapper_does_not_leak_internals_after_a_trailing_backslash():
+ result = _run_result("echo hi \\")
- assert script.index('__hermes_ps=("${PIPESTATUS[@]}")') < script.index("umask 077")
- assert script.index("__hermes_ec=${__hermes_ps[${#__hermes_ps[@]}-1]}") < script.index(PIPE_MARKER)
+ assert result.returncode == 0
+ assert "__hermes_ps" not in result.stdout
+ assert "__hermes_ps" not in result.stderr
diff --git a/tools/environments/base_session_env.py b/tools/environments/base_session_env.py
index ac94acc3ad3..57d2785931d 100644
--- a/tools/environments/base_session_env.py
+++ b/tools/environments/base_session_env.py
@@ -169,8 +169,8 @@ def _wrap_command_script(
'export GIT_PAGER="${GIT_PAGER:-cat}" PAGER="${PAGER:-cat}"',
# ``--`` keeps hyphen-prefixed directory names from being parsed as options.
f"builtin cd -- {quoted_cwd} || exit 126",
- f"eval '{escaped}\n__hermes_ps=(\"${{PIPESTATUS[@]}}\")'",
- "__hermes_ec=${__hermes_ps[${#__hermes_ps[@]}-1]}",
+ f"eval '{escaped}\n\n__hermes_rc=$? __hermes_ps=(\"${{PIPESTATUS[@]}}\"); (exit $__hermes_rc)'",
+ "__hermes_ec=$?",
"umask 077"]
if snapshot_ready and persist_env:
parts.append(