Skip to content

The agent that grows with you

fix(file-ops): keep fenced byte-exact reads usable under xtrace

kshitijk4poora342e6563d2a
diff --git a/tests/tools/test_file_write_safety.py b/tests/tools/test_file_write_safety.py
index 33ca769d947..1fa9dd9fb29 100644
--- a/tests/tools/test_file_write_safety.py
+++ b/tests/tools/test_file_write_safety.py
@@ -588,6 +588,28 @@ class TestBomHandling:
signal.signal(signal.SIGALRM, previous)
assert data is None and failed is not None and "handed to the shell" in failed.stdout
+ @pytest.mark.skipif(os.name == "nt", reason="POSIX bash xtrace via BASH_ENV")
+ def test_byte_exact_read_survives_a_real_xtrace_shell(self, tmp_path: Path, monkeypatch):
+ # A user rc / BASH_ENV with `set -x` must not break byte-exact reads, or every edit fails
+ # its read. (The post-write sha256 check under noisy stdout is a separate issue.)
+ import shutil
+ from tools.file_operations import ShellFileOperations
+ from tools.environments.local import LocalEnvironment
+ if not shutil.which("bash"):
+ pytest.skip("bash not installed")
+ monkeypatch.setenv("HERMES_NATIVE_FILE_READ", "0")
+ hook = tmp_path / "xtrace.sh"
+ hook.write_text("set -x\n")
+ target = tmp_path / "conf.txt"
+ original = b"HEADER\n\x1b]0;osc\x07\n" + b"line\n" * 250 + b"VERSION=1 \xff\n"
+ target.write_bytes(original)
+ env = LocalEnvironment(cwd=str(tmp_path), env={"BASH_ENV": str(hook)})
+ ops = ShellFileOperations(env, cwd=str(tmp_path))
+
+ assert ops._sample_file_bytes(str(target)) == original[:1000]
+ raw = ops.read_file_raw(str(target))
+ assert raw.error is None and raw.content.encode("utf-8", "surrogateescape") == original
+
class TestProtectedInstructionFiles:
"""Writes to agent-instruction files ALWAYS require approval.
diff --git a/tools/file_operations.py b/tools/file_operations.py
index 98ea6b9c266..2a9d3aab635 100644
--- a/tools/file_operations.py
+++ b/tools/file_operations.py
@@ -266,7 +266,9 @@ class ShellFileOperations(LintMixin, SearchMixin, FileOperations):
sentinel = _new_sentinel(_BYTES_SENTINEL_PREFIX)
mark = f"echo {sentinel}"
rest = "".join(f"{mark}; {cmd}; " for cmd in more)
- result = self._exec(f"{mark}; {body}; __hb=$?; {rest}{mark}; echo $__hb")
+ # xtrace off first: a traced ``+ echo <sentinel>`` line is an extra separator, and the
+ # traces of the transport commands would land inside the payload segments.
+ result = self._exec(f"{{ set +x; }} 2>/dev/null; {mark}; {body}; __hb=$?; {rest}{mark}; echo $__hb")
segments = _split_segments(result.stdout or "", sentinel)
if len(segments) != len(more) + 3:
return None, None, result