From 01afd3549150fe28a5b97233fcd465ae682fe1ff Mon Sep 17 00:00:00 2001 From: Andrew Park Date: Tue, 24 Nov 2026 15:24:44 -0800 Subject: [PATCH 1/3] feat(sandbox-utils): fetch_container_logs takes max_chars per stream The 500-character default keeps the result small enough to inline in an error message. A caller that stores the logs somewhere else can now ask for more of each stream. Existing callers keep the default. --- .../task_steps/sandbox_utils/sandbox_utils.py | 11 ++++++++--- tst/unit/task_step/test_sandbox_utils_logs.py | 9 +++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/agent_env/task_step/task_steps/sandbox_utils/sandbox_utils.py b/src/agent_env/task_step/task_steps/sandbox_utils/sandbox_utils.py index c8c34f4a6..adda840a5 100644 --- a/src/agent_env/task_step/task_steps/sandbox_utils/sandbox_utils.py +++ b/src/agent_env/task_step/task_steps/sandbox_utils/sandbox_utils.py @@ -34,7 +34,9 @@ def ended_turn_error(state: Any, reply: TerminalResponse) -> str: return ": ".join(part for part in (f"state {state}", detail, text) if part) -async def fetch_container_logs(agent, tail: int = 500) -> Optional[str]: +async def fetch_container_logs( + agent, tail: int = 500, *, max_chars: int = _STREAM_TAIL_CHARS +) -> Optional[str]: """Tail the docker logs of the agent's container, so a failed A2A task stays diagnosable after its sandbox is reaped. @@ -42,6 +44,9 @@ async def fetch_container_logs(agent, tail: int = 500) -> Optional[str]: containers are tailed up to ``_FALLBACK_CONTAINER_LIMIT``, agent-named ones first. Best effort: None on any failure, including a sandbox with no docker, so it never masks the task's own failure. + + ``max_chars`` bounds each stream of each container. The default keeps the result small + enough to inline in an error message; a caller that stores the logs elsewhere can ask for more. """ sandbox_id = getattr(agent, "sandbox_id", None) if not sandbox_id: @@ -70,8 +75,8 @@ async def fetch_container_logs(agent, tail: int = 500) -> Optional[str]: "sudo", "docker", "logs", "--tail", str(tail), cid, ) parts = [] - out_tail = (out or "").strip()[-_STREAM_TAIL_CHARS:] - err_tail = (err or "").strip()[-_STREAM_TAIL_CHARS:] + out_tail = (out or "").strip()[-max_chars:] + err_tail = (err or "").strip()[-max_chars:] if out_tail: parts.append(f"[stdout]\n{out_tail}") if err_tail: diff --git a/tst/unit/task_step/test_sandbox_utils_logs.py b/tst/unit/task_step/test_sandbox_utils_logs.py index 2c0a5177e..7d5daa269 100644 --- a/tst/unit/task_step/test_sandbox_utils_logs.py +++ b/tst/unit/task_step/test_sandbox_utils_logs.py @@ -94,6 +94,15 @@ async def test_long_stdout_does_not_push_out_stderr(install): assert out.count("x") == 500 +@pytest.mark.asyncio +async def test_max_chars_widens_each_stream(install): + sandbox = FakeSandbox(rows=[("c1", "agent-api")], logs={"c1": ("x" * 5000, "y" * 5000)}) + + out = await fetch_container_logs(install(sandbox), max_chars=3000) + + assert (out.count("x"), out.count("y")) == (3000, 3000) + + @pytest.mark.asyncio async def test_no_docker_is_none_after_one_exec(install): sandbox = FakeSandbox(docker_ok=False) From f2c1ca62a294142ccde1fb000197d49263774cf9 Mon Sep 17 00:00:00 2001 From: Andrew Park Date: Tue, 24 Nov 2026 15:38:27 -0800 Subject: [PATCH 2/3] fix(sandbox-utils): max_chars of zero keeps no characters A slice of [-0:] keeps the whole string, so max_chars=0 returned every stream in full. --- .../task_step/task_steps/sandbox_utils/sandbox_utils.py | 9 +++++++-- tst/unit/task_step/test_sandbox_utils_logs.py | 7 +++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/agent_env/task_step/task_steps/sandbox_utils/sandbox_utils.py b/src/agent_env/task_step/task_steps/sandbox_utils/sandbox_utils.py index adda840a5..328cf0bdc 100644 --- a/src/agent_env/task_step/task_steps/sandbox_utils/sandbox_utils.py +++ b/src/agent_env/task_step/task_steps/sandbox_utils/sandbox_utils.py @@ -34,6 +34,11 @@ def ended_turn_error(state: Any, reply: TerminalResponse) -> str: return ": ".join(part for part in (f"state {state}", detail, text) if part) +def _last_chars(text: str, limit: int) -> str: + """The last ``limit`` characters of ``text``; none for a limit of zero or less, where ``text[-0:]`` would keep all.""" + return text[len(text) - max(limit, 0):] + + async def fetch_container_logs( agent, tail: int = 500, *, max_chars: int = _STREAM_TAIL_CHARS ) -> Optional[str]: @@ -75,8 +80,8 @@ async def fetch_container_logs( "sudo", "docker", "logs", "--tail", str(tail), cid, ) parts = [] - out_tail = (out or "").strip()[-max_chars:] - err_tail = (err or "").strip()[-max_chars:] + out_tail = _last_chars((out or "").strip(), max_chars) + err_tail = _last_chars((err or "").strip(), max_chars) if out_tail: parts.append(f"[stdout]\n{out_tail}") if err_tail: diff --git a/tst/unit/task_step/test_sandbox_utils_logs.py b/tst/unit/task_step/test_sandbox_utils_logs.py index 7d5daa269..a31c1434e 100644 --- a/tst/unit/task_step/test_sandbox_utils_logs.py +++ b/tst/unit/task_step/test_sandbox_utils_logs.py @@ -103,6 +103,13 @@ async def test_max_chars_widens_each_stream(install): assert (out.count("x"), out.count("y")) == (3000, 3000) +@pytest.mark.asyncio +async def test_max_chars_zero_keeps_nothing(install): + sandbox = FakeSandbox(rows=[("c1", "agent-api")], logs={"c1": ("x" * 50, "y" * 50)}) + + assert await fetch_container_logs(install(sandbox), max_chars=0) is None + + @pytest.mark.asyncio async def test_no_docker_is_none_after_one_exec(install): sandbox = FakeSandbox(docker_ok=False) From 761018b6231b79e97e00efc11caebba0105fd1cc Mon Sep 17 00:00:00 2001 From: Andrew Park Date: Tue, 24 Nov 2026 15:43:34 -0800 Subject: [PATCH 3/3] fix(sandbox-utils): a stream shorter than max_chars is kept whole The start index went negative when a stream was shorter than the limit, and Python counts a negative index from the end, so a 499-character stream kept only its last character. --- .../task_step/task_steps/sandbox_utils/sandbox_utils.py | 2 +- tst/unit/task_step/test_sandbox_utils_logs.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/agent_env/task_step/task_steps/sandbox_utils/sandbox_utils.py b/src/agent_env/task_step/task_steps/sandbox_utils/sandbox_utils.py index 328cf0bdc..70869b0bb 100644 --- a/src/agent_env/task_step/task_steps/sandbox_utils/sandbox_utils.py +++ b/src/agent_env/task_step/task_steps/sandbox_utils/sandbox_utils.py @@ -36,7 +36,7 @@ def ended_turn_error(state: Any, reply: TerminalResponse) -> str: def _last_chars(text: str, limit: int) -> str: """The last ``limit`` characters of ``text``; none for a limit of zero or less, where ``text[-0:]`` would keep all.""" - return text[len(text) - max(limit, 0):] + return text[max(len(text) - max(limit, 0), 0):] async def fetch_container_logs( diff --git a/tst/unit/task_step/test_sandbox_utils_logs.py b/tst/unit/task_step/test_sandbox_utils_logs.py index a31c1434e..4bf43bba5 100644 --- a/tst/unit/task_step/test_sandbox_utils_logs.py +++ b/tst/unit/task_step/test_sandbox_utils_logs.py @@ -103,6 +103,15 @@ async def test_max_chars_widens_each_stream(install): assert (out.count("x"), out.count("y")) == (3000, 3000) +@pytest.mark.asyncio +async def test_a_stream_shorter_than_max_chars_is_kept_whole(install): + sandbox = FakeSandbox(rows=[("c1", "agent-api")], logs={"c1": ("", "z" * 499)}) + + out = await fetch_container_logs(install(sandbox)) + + assert out.count("z") == 499 + + @pytest.mark.asyncio async def test_max_chars_zero_keeps_nothing(install): sandbox = FakeSandbox(rows=[("c1", "agent-api")], logs={"c1": ("x" * 50, "y" * 50)})