From 140e5fae4fe07e8da7485d55eb85633e916e5010 Mon Sep 17 00:00:00 2001 From: Aarav Mittal <137450929+a2105z@users.noreply.github.com> Date: Mon, 10 Aug 2026 17:32:32 -0700 Subject: [PATCH] feat(tools): Persist skill-script outputs as artifacts Skill scripts wrote files into a TemporaryDirectory that was deleted after execution, so generated PDFs/exports were lost. Capture new files from the script workspace, save them via tool_context.save_artifact, and return saved_artifacts in the tool result. Fixes #5579 --- src/google/adk/tools/skill_toolset.py | 156 +++++++++++++- tests/unittests/tools/test_skill_toolset.py | 219 ++++++++++++++++++++ 2 files changed, 373 insertions(+), 2 deletions(-) diff --git a/src/google/adk/tools/skill_toolset.py b/src/google/adk/tools/skill_toolset.py index 4c0f45701a9..ac0971d0edf 100644 --- a/src/google/adk/tools/skill_toolset.py +++ b/src/google/adk/tools/skill_toolset.py @@ -19,6 +19,7 @@ from __future__ import annotations import asyncio +import base64 import collections import json import logging @@ -57,6 +58,11 @@ _DEFAULT_SCRIPT_TIMEOUT = 300 _MAX_SKILL_PAYLOAD_BYTES = 16 * 1024 * 1024 # 16 MB +# Caps for files generated by skill scripts that are auto-saved as artifacts. +_MAX_GENERATED_ARTIFACT_BYTES = 5 * 1024 * 1024 # 5 MiB per file +_MAX_GENERATED_ARTIFACTS_TOTAL_BYTES = 16 * 1024 * 1024 # 16 MiB total +# Printed by the skill wrapper after execution; stripped from tool stdout. +_GENERATED_FILES_MARKER = "__ADK_SKILL_GENERATED_FILES__:" # Message used for the "Content Injection" pattern. _BINARY_FILE_DETECTED_MSG = ( @@ -65,6 +71,94 @@ ) +def _extract_generated_files_from_stdout( + stdout: str | None, +) -> tuple[str, list[dict[str, Any]]]: + """Strips the generated-files marker from stdout and returns file payloads.""" + if not stdout or _GENERATED_FILES_MARKER not in stdout: + return stdout or "", [] + + cleaned_parts: list[str] = [] + generated: list[dict[str, Any]] = [] + for line in stdout.splitlines(keepends=True): + marker_at = line.find(_GENERATED_FILES_MARKER) + if marker_at == -1: + cleaned_parts.append(line) + continue + prefix = line[:marker_at] + if prefix: + cleaned_parts.append(prefix) + if not prefix.endswith("\n"): + cleaned_parts.append("\n") + payload = line[marker_at + len(_GENERATED_FILES_MARKER) :].strip() + if not payload: + continue + try: + parsed = json.loads(payload) + except json.JSONDecodeError: + logger.warning("Failed to parse skill generated-files marker payload") + continue + if isinstance(parsed, list): + generated = [item for item in parsed if isinstance(item, dict)] + return "".join(cleaned_parts), generated + + +async def _save_generated_skill_artifacts( + tool_context: ToolContext | None, + generated_files: list[dict[str, Any]], +) -> list[str]: + """Saves skill-generated files via tool_context and returns saved names.""" + if not generated_files: + return [] + if tool_context is None: + logger.warning( + "Skill script produced %d generated file(s) but no tool_context was" + " provided; skipping artifact save.", + len(generated_files), + ) + return [] + if tool_context._invocation_context.artifact_service is None: + logger.warning( + "Skill script produced %d generated file(s) but artifact service is" + " not initialized; skipping artifact save.", + len(generated_files), + ) + return [] + + saved: list[str] = [] + for item in generated_files: + path = item.get("path") + content_b64 = item.get("content_b64") + if ( + not isinstance(path, str) + or not path + or not isinstance(content_b64, str) + ): + continue + norm = PurePosixPath(path.replace("\\", "/")) + if norm.is_absolute() or ".." in norm.parts: + logger.warning("Skipping generated skill file with unsafe path: %s", path) + continue + mime_type = item.get("mime_type") + if not isinstance(mime_type, str) or not mime_type: + mime_type = "application/octet-stream" + try: + data = base64.b64decode(content_b64) + except Exception: # pylint: disable=broad-exception-caught + logger.warning("Failed to decode generated skill file: %s", path) + continue + filename = str(norm) + try: + await tool_context.save_artifact( + filename=filename, + artifact=types.Part.from_bytes(data=data, mime_type=mime_type), + ) + saved.append(filename) + except Exception: # pylint: disable=broad-exception-caught + logger.exception("Failed to save skill-generated artifact '%s'", filename) + return saved + + def _build_skill_system_instruction( prefix: str | None = None, skills_folder: Path | None = None ) -> str: @@ -542,6 +636,7 @@ async def execute_script_async( script_args: dict[str, Any] | list[str] | None, short_options: dict[str, Any] | None = None, positional_args: list[str] | None = None, + tool_context: ToolContext | None = None, ) -> dict[str, Any]: """Prepares and executes the script using the base executor. @@ -554,9 +649,12 @@ async def execute_script_async( long options or a list of strings. short_options: Optional short options (single hyphen) as key-value pairs. positional_args: Optional positional arguments. + tool_context: Optional tool context used to persist generated files as + artifacts. Returns: A dictionary containing execution results (stdout, stderr, status). + When generated files are saved, also includes ``saved_artifacts``. """ code = self._build_wrapper_code( skill, file_path, script_args, short_options, positional_args @@ -584,6 +682,7 @@ async def execute_script_async( stdout = result.stdout stderr = result.stderr + stdout, generated_files = _extract_generated_files_from_stdout(stdout) # Shell scripts serialize both streams as JSON # through stdout; parse the envelope if present. @@ -617,13 +716,19 @@ async def execute_script_async( elif stderr: status = "warning" - return { + saved_artifacts = await _save_generated_skill_artifacts( + tool_context, generated_files + ) + response: dict[str, Any] = { "skill_name": skill.name, "file_path": file_path, "stdout": stdout, "stderr": stderr, "status": status, } + if saved_artifacts: + response["saved_artifacts"] = saved_artifacts + return response except SystemExit as e: if e.code in (None, 0): return { @@ -807,8 +912,54 @@ def _build_wrapper_code( else: return None + # After the script runs, collect files created under the tempdir that were + # not part of the original skill payload and emit them on stdout for the + # host to persist as artifacts (tempdir is deleted when this block exits). code_lines.extend([ " finally:", + " try:", + " import base64 as _b64", + " import mimetypes as _mt", + " _initial = {", + " os.path.normpath(_p).replace('\\\\', '/')", + " for _p in _files", + " }", + " _generated = []", + " _total = 0", + f" _max_file = {_MAX_GENERATED_ARTIFACT_BYTES!r}", + f" _max_total = {_MAX_GENERATED_ARTIFACTS_TOTAL_BYTES!r}", + " for _root, _dirs, _names in os.walk(td):", + " _dirs[:] = [d for d in _dirs if d != '__pycache__']", + " for _name in _names:", + " if _name.endswith('.pyc'):", + " continue", + " _full = os.path.join(_root, _name)", + " _rel = os.path.relpath(_full, td).replace('\\\\', '/')", + " _norm = os.path.normpath(_rel).replace('\\\\', '/')", + " if _norm in _initial or _norm.startswith('..'):", + " continue", + " try:", + " _sz = os.path.getsize(_full)", + " except OSError:", + " continue", + " if _sz > _max_file or _total + _sz > _max_total:", + " continue", + " with open(_full, 'rb') as _gf:", + " _data = _gf.read()", + " _total += len(_data)", + " _mime, _ = _mt.guess_type(_norm)", + " _generated.append({", + " 'path': _norm,", + " 'content_b64': _b64.b64encode(_data).decode('ascii'),", + " 'mime_type': _mime or 'application/octet-stream',", + " })", + " if _generated:", + ( + f" print({_GENERATED_FILES_MARKER!r} +" + " _json.dumps(_generated))" + ), + " except Exception:", + " pass", " os.chdir(_orig_cwd)", ]) @@ -1068,7 +1219,8 @@ async def run_async( file_path, script_args, short_options, - positional_args, # pylint: disable=protected-access + positional_args, + tool_context=tool_context, ) async def _ensure_skill_materialized_in_env( diff --git a/tests/unittests/tools/test_skill_toolset.py b/tests/unittests/tools/test_skill_toolset.py index d923360c815..f7f3f423808 100644 --- a/tests/unittests/tools/test_skill_toolset.py +++ b/tests/unittests/tools/test_skill_toolset.py @@ -14,6 +14,7 @@ from __future__ import annotations import asyncio +import base64 import collections import json import logging @@ -22,15 +23,20 @@ import sys from unittest import mock +from google.adk.agents.invocation_context import InvocationContext from google.adk.agents.readonly_context import ReadonlyContext +from google.adk.agents.sequential_agent import SequentialAgent +from google.adk.artifacts.in_memory_artifact_service import InMemoryArtifactService from google.adk.code_executors.base_code_executor import BaseCodeExecutor from google.adk.code_executors.code_execution_utils import CodeExecutionResult from google.adk.code_executors.unsafe_local_code_executor import UnsafeLocalCodeExecutor from google.adk.environment import BaseEnvironment from google.adk.models import llm_request as llm_request_model +from google.adk.sessions.in_memory_session_service import InMemorySessionService from google.adk.skills import models from google.adk.tools import skill_toolset from google.adk.tools import tool_context +from google.adk.tools.tool_context import ToolContext from google.genai import types import pytest @@ -2019,6 +2025,219 @@ async def test_execute_script_input_files_packaged(mock_skill1): assert "'assets/asset1.txt': 'asset content 1'" in code_input.code +# ── generated file → artifact persistence (#5579) ── + + +def test_extract_generated_files_from_stdout_strips_marker(): + """Generated-files marker is removed from stdout and parsed.""" + payload = [{ + "path": "report.pdf", + "content_b64": base64.b64encode(b"%PDF").decode("ascii"), + "mime_type": "application/pdf", + }] + stdout = ( + f"hello\n{skill_toolset._GENERATED_FILES_MARKER}{json.dumps(payload)}\n" + ) + cleaned, files = skill_toolset._extract_generated_files_from_stdout(stdout) + assert cleaned == "hello\n" + assert len(files) == 1 + assert files[0]["path"] == "report.pdf" + + +@pytest.mark.asyncio +async def test_execute_script_saves_generated_files_as_artifacts(mock_skill1): + """Files emitted by the skill wrapper are saved and listed in the result.""" + payload = [{ + "path": "output_report.pdf", + "content_b64": base64.b64encode(b"%PDF-1.4").decode("ascii"), + "mime_type": "application/pdf", + }] + stdout = ( + f"done\n{skill_toolset._GENERATED_FILES_MARKER}{json.dumps(payload)}\n" + ) + executor = _make_mock_executor(stdout=stdout) + toolset = skill_toolset.SkillToolset([mock_skill1], code_executor=executor) + tool = skill_toolset.RunSkillScriptTool(toolset) + ctx = _make_tool_context_with_agent() + ctx._invocation_context.artifact_service = mock.MagicMock() + ctx.save_artifact = mock.AsyncMock(return_value=0) + + result = await tool.run_async( + args={"skill_name": "skill1", "file_path": "run.py"}, + tool_context=ctx, + ) + + assert result["status"] == "success" + assert result["stdout"] == "done\n" + assert result["saved_artifacts"] == ["output_report.pdf"] + ctx.save_artifact.assert_awaited_once() + kwargs = ctx.save_artifact.await_args.kwargs + assert kwargs["filename"] == "output_report.pdf" + assert kwargs["artifact"].inline_data.data == b"%PDF-1.4" + + +@pytest.mark.asyncio +async def test_execute_script_shell_envelope_with_generated_files(mock_skill1): + """Shell JSON envelope still parses when a generated-files marker follows.""" + envelope = json.dumps({ + "__shell_result__": True, + "stdout": "shell out\n", + "stderr": "", + "returncode": 0, + }) + payload = [{ + "path": "exports/data.csv", + "content_b64": base64.b64encode(b"a,b\n1,2\n").decode("ascii"), + "mime_type": "text/csv", + }] + stdout = ( + f"{envelope}\n" + f"{skill_toolset._GENERATED_FILES_MARKER}{json.dumps(payload)}\n" + ) + executor = _make_mock_executor(stdout=stdout) + toolset = skill_toolset.SkillToolset([mock_skill1], code_executor=executor) + tool = skill_toolset.RunSkillScriptTool(toolset) + ctx = _make_tool_context_with_agent() + ctx._invocation_context.artifact_service = mock.MagicMock() + ctx.save_artifact = mock.AsyncMock(return_value=1) + + result = await tool.run_async( + args={"skill_name": "skill1", "file_path": "setup.sh"}, + tool_context=ctx, + ) + + assert result["status"] == "success" + assert result["stdout"] == "shell out\n" + assert result["saved_artifacts"] == ["exports/data.csv"] + + +@pytest.mark.asyncio +async def test_execute_script_skips_artifact_save_without_service(mock_skill1): + """Missing artifact service does not fail the tool call.""" + payload = [{ + "path": "out.txt", + "content_b64": base64.b64encode(b"x").decode("ascii"), + "mime_type": "text/plain", + }] + stdout = f"{skill_toolset._GENERATED_FILES_MARKER}{json.dumps(payload)}\n" + executor = _make_mock_executor(stdout=stdout) + toolset = skill_toolset.SkillToolset([mock_skill1], code_executor=executor) + tool = skill_toolset.RunSkillScriptTool(toolset) + ctx = _make_tool_context_with_agent() + ctx._invocation_context.artifact_service = None + ctx.save_artifact = mock.AsyncMock() + + result = await tool.run_async( + args={"skill_name": "skill1", "file_path": "run.py"}, + tool_context=ctx, + ) + + assert result["status"] == "success" + assert "saved_artifacts" not in result + ctx.save_artifact.assert_not_awaited() + + +@pytest.mark.asyncio +async def test_execute_script_wrapper_collects_generated_files(mock_skill1): + """Wrapper code walks the tempdir and emits the generated-files marker.""" + executor = _make_mock_executor(stdout="ok\n") + toolset = skill_toolset.SkillToolset([mock_skill1], code_executor=executor) + tool = skill_toolset.RunSkillScriptTool(toolset) + ctx = _make_tool_context_with_agent() + await tool.run_async( + args={"skill_name": "skill1", "file_path": "run.py"}, + tool_context=ctx, + ) + code = executor.execute_code.call_args[0][1].code + assert skill_toolset._GENERATED_FILES_MARKER in code + assert "os.walk(td)" in code + assert "__pycache__" in code + + +@pytest.mark.asyncio +async def test_integration_python_generated_file_saved_as_artifact(): + """Real executor: files written by a skill script are saved as artifacts.""" + script = models.Script( + src=( + "from pathlib import" + " Path\nPath('output_report.txt').write_text('artifact body'," + " encoding='utf-8')\nprint('wrote report')\n" + ) + ) + skill = _make_skill_with_script("test_skill", "write_report.py", script) + toolset = _make_real_executor_toolset([skill]) + tool = skill_toolset.RunSkillScriptTool(toolset) + + session_service = InMemorySessionService() + session = await session_service.create_session( + app_name="test_app", user_id="test_user" + ) + artifact_service = InMemoryArtifactService() + invocation_context = InvocationContext( + invocation_id="inv_artifacts", + agent=SequentialAgent(name="test_agent"), + session=session, + session_service=session_service, + artifact_service=artifact_service, + ) + ctx = ToolContext(invocation_context) + + result = await tool.run_async( + args={ + "skill_name": "test_skill", + "file_path": "write_report.py", + }, + tool_context=ctx, + ) + + assert result["status"] == "success", result + assert result["stdout"] == "wrote report\n" + assert result.get("saved_artifacts") == ["output_report.txt"] + saved = await artifact_service.load_artifact( + app_name="test_app", + user_id="test_user", + session_id=session.id, + filename="output_report.txt", + ) + assert saved is not None + assert saved.inline_data.data == b"artifact body" + + +@pytest.mark.asyncio +async def test_integration_python_does_not_resave_skill_resources(): + """Real executor: packaged skill resources are not re-saved as artifacts.""" + script = models.Script(src="print('ok')") + skill = _make_skill_with_script("test_skill", "noop.py", script) + skill.resources.get_reference.side_effect = lambda n: ( + "ref body" if n == "notes.txt" else None + ) + skill.resources.list_references.return_value = ["notes.txt"] + toolset = _make_real_executor_toolset([skill]) + tool = skill_toolset.RunSkillScriptTool(toolset) + + session_service = InMemorySessionService() + session = await session_service.create_session( + app_name="test_app", user_id="test_user" + ) + artifact_service = InMemoryArtifactService() + invocation_context = InvocationContext( + invocation_id="inv_no_resave", + agent=SequentialAgent(name="test_agent"), + session=session, + session_service=session_service, + artifact_service=artifact_service, + ) + ctx = ToolContext(invocation_context) + + result = await tool.run_async( + args={"skill_name": "test_skill", "file_path": "noop.py"}, + tool_context=ctx, + ) + + assert result["status"] == "success", result + assert "saved_artifacts" not in result + + # ── Integration: shell non-zero exit ──