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..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 @@ -34,7 +34,14 @@ 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]: +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[max(len(text) - max(limit, 0), 0):] + + +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 +49,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 +80,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 = _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 2c0a5177e..4bf43bba5 100644 --- a/tst/unit/task_step/test_sandbox_utils_logs.py +++ b/tst/unit/task_step/test_sandbox_utils_logs.py @@ -94,6 +94,31 @@ 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_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)}) + + 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)