Skip to content

The agent that grows with you

fix: capped @-reference child output always reports a nonzero returncode

teknium13e579ee7af48
diff --git a/agent/context_references.py b/agent/context_references.py
index 5561b0876cc..8e1f566ea45 100644
--- a/agent/context_references.py
+++ b/agent/context_references.py
@@ -104,6 +104,7 @@ _LINE_COUNT_MAX_BYTES = 4 * 1024 * 1024
# Per-command stdout/stderr ceiling for the ``git``/``rg`` helpers; past it the child is
# killed and the nonzero returncode routes the caller to its fallback path.
_MAX_QUIET_OUTPUT_BYTES = 4 * 1024 * 1024
+_OUTPUT_CAP_EXCEEDED_RETURNCODE = 137 # 128 + SIGKILL, the code a shell reports for a killed child
_FENCE_LANGUAGES = {
".py": "python", ".js": "javascript", ".ts": "typescript", ".tsx": "tsx", ".jsx": "jsx",
".json": "json", ".md": "markdown", ".sh": "bash", ".yml": "yaml", ".yaml": "yaml", ".toml": "toml",
@@ -365,14 +366,18 @@ def _run_quiet(cmd: list[str], cwd: Path, timeout: int, env: dict | None = None)
``capture_output=True`` buffers the child's entire stdout — an unbounded ``git diff``
or ``rg --files`` would materialize a hostile-size stream in memory. Each pipe is
- drained up to ``_MAX_QUIET_OUTPUT_BYTES``; past it the child is killed and its
- nonzero returncode routes callers to their existing fallback paths.
+ drained up to ``_MAX_QUIET_OUTPUT_BYTES``; past it the child is killed and the result
+ carries a nonzero returncode that routes callers to their existing fallback paths. Set
+ explicitly: a child that flushed everything and exited 0 before the drain thread crossed
+ the cap is not killable and would otherwise report success with truncated output.
"""
popen_kwargs: dict = {"creationflags": windows_hide_flags()} if IS_WINDOWS else {}
proc = subprocess.Popen(
cmd, cwd=cwd, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
**popen_kwargs, **({} if env is None else {"env": env}))
+ truncated = threading.Event()
+
def _drain(stream, sink: list[bytes]) -> None:
total = 0
while True:
@@ -383,6 +388,7 @@ def _run_quiet(cmd: list[str], cwd: Path, timeout: int, env: dict | None = None)
if total <= _MAX_QUIET_OUTPUT_BYTES:
sink.append(chunk)
else:
+ truncated.set()
proc.kill()
return
@@ -402,7 +408,10 @@ def _run_quiet(cmd: list[str], cwd: Path, timeout: int, env: dict | None = None)
for thread in threads:
thread.join()
stdout, stderr = (b"".join(sink).decode("utf-8", "replace") for sink in sinks)
- return subprocess.CompletedProcess(cmd, proc.returncode, stdout, stderr)
+ returncode = proc.returncode
+ if truncated.is_set() and returncode == 0:
+ returncode = _OUTPUT_CAP_EXCEEDED_RETURNCODE
+ return subprocess.CompletedProcess(cmd, returncode, stdout, stderr)
def _expand_git_reference(ref: ContextReference, cwd: Path, args: list[str], label: str) -> Expansion:
diff --git a/tests/agent/test_context_references.py b/tests/agent/test_context_references.py
index 30408af8a3f..c940f923273 100644
--- a/tests/agent/test_context_references.py
+++ b/tests/agent/test_context_references.py
@@ -287,6 +287,20 @@ def test_run_quiet_caps_child_output(tmp_path: Path):
assert huge.returncode != 0
assert len(huge.stdout) <= _MAX_QUIET_OUTPUT_BYTES
+ # A child that flushes past the cap and exits 0 before the drain thread reads it (the common
+ # case for a fast writer on a loaded runner) must still report failure: the caller's fallback
+ # path keys on the returncode, and a 0 with truncated stdout was a silent success.
+ with patch("agent.context_references.subprocess.Popen") as popen:
+ import io
+ fake = popen.return_value
+ fake.stdout = io.BytesIO(b"x" * (_MAX_QUIET_OUTPUT_BYTES + 1000))
+ fake.stderr = io.BytesIO(b"")
+ fake.returncode = 0
+ fake.wait.return_value = 0
+ exited = _run_quiet([sys.executable, "-c", "pass"], tmp_path, 30)
+ assert exited.returncode != 0
+ assert len(exited.stdout) <= _MAX_QUIET_OUTPUT_BYTES
+
ok = _run_quiet([sys.executable, "-c", "print('hello')"], tmp_path, 30)
assert ok.returncode == 0 and "hello" in ok.stdout