Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,24 @@ 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.

The agent's own container is the one named ``sandbox.container_name``. When none is, the
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:
Expand Down Expand Up @@ -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:
Expand Down
25 changes: 25 additions & 0 deletions tst/unit/task_step/test_sandbox_utils_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading