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
5 changes: 4 additions & 1 deletion src/google/adk/tools/skill_toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1404,9 +1404,12 @@ def _materialize_and_run():
'returncode': _r.returncode,
}}))
except subprocess.TimeoutExpired as _e:
_stdout = _e.stdout
if isinstance(_stdout, bytes):
_stdout = _stdout.decode('utf-8', errors='replace')
print('\\n' + _json.dumps({{
'__shell_result__': True,
'stdout': _e.stdout or '',
'stdout': _stdout or '',
'stderr': 'Timed out after {timeout}s',
'returncode': -1,
'timeout': True,
Expand Down
48 changes: 48 additions & 0 deletions tests/unittests/tools/test_skill_toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
import asyncio
import base64
import collections
import contextlib
import io
import json
import logging
from pathlib import Path
from pathlib import PurePosixPath
import re
import shlex
import subprocess
import sys
from unittest import mock

Expand Down Expand Up @@ -2568,6 +2571,51 @@ async def test_shell_json_envelope_timeout(mock_skill1):
assert "Exit code" not in result["stderr"]


@pytest.mark.asyncio
@pytest.mark.parametrize(
"timeout_stdout, expected_stdout",
[
(b"partial \xff output\n", "partial \ufffd output\n"),
(b"", ""),
(None, ""),
],
)
async def test_shell_wrapper_timeout_decodes_bytes_stdout(
mock_skill1, timeout_stdout, expected_stdout
):
"""Shell wrapper decodes bytes TimeoutExpired.stdout instead of failing."""
executor = mock.create_autospec(BaseCodeExecutor, instance=True)

def _execute_wrapper(_ctx, code_input):
buf = io.StringIO()
timeout_exc = subprocess.TimeoutExpired(
cmd=["bash", "scripts/setup.sh"],
timeout=5,
output=timeout_stdout,
stderr=b"",
)
with (
mock.patch("subprocess.run", side_effect=timeout_exc),
contextlib.redirect_stdout(buf),
):
exec(code_input.code, {})
return CodeExecutionResult(stdout=buf.getvalue(), stderr="")

executor.execute_code.side_effect = _execute_wrapper
toolset = skill_toolset.SkillToolset(
[mock_skill1], code_executor=executor, script_timeout=5
)
tool = skill_toolset.RunSkillScriptTool(toolset)
ctx = _make_tool_context_with_agent()
result = await tool.run_async(
args={"skill_name": "skill1", "file_path": "setup.sh"},
tool_context=ctx,
)
assert result["status"] == "error"
assert result["stdout"] == expected_stdout
assert result["stderr"] == "Timed out after 5s"


@pytest.mark.asyncio
async def test_shell_non_json_stdout_passthrough(mock_skill1):
"""Non-JSON shell stdout is passed through without parsing."""
Expand Down