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
10 changes: 9 additions & 1 deletion src/google/adk/tools/environment/_environment_toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,16 @@ async def process_llm_request(
# a session-scoped environment can answer from `ctx.state`.
working_dir = working_dir_or_none(self._environment)
if working_dir is not None:
# Render with forward slashes rather than with `str()`, whose output
# follows the host's path flavour. The directory belongs to the
# environment, and a remote environment is a Linux sandbox whose shell
# runs the commands the instruction describes (`&&`, `cat`, ...), so on a
# Windows host `str(WindowsPath('/home/user'))` would tell the model the
# directory is `\home\user` while the sandbox runs at `/home/user`.
instruction = (
ENVIRONMENT_WORKING_DIR_INSTRUCTION.format(working_dir=working_dir)
ENVIRONMENT_WORKING_DIR_INSTRUCTION.format(
working_dir=working_dir.as_posix()
)
+ '\n'
+ instruction
)
Expand Down
30 changes: 30 additions & 0 deletions tests/unittests/tools/test_environment_toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

import asyncio
from pathlib import Path
from pathlib import PurePath
from pathlib import PurePosixPath
from pathlib import PureWindowsPath
import pickle
from typing import Any
from typing import Optional
Expand Down Expand Up @@ -229,6 +232,33 @@ async def test_instruction_includes_working_dir_before_initialization():
assert not env.contexts


@pytest.mark.asyncio
@pytest.mark.parametrize(
"working_dir",
[PurePosixPath("/home/user"), PureWindowsPath("/home/user")],
ids=["posix", "windows"],
)
async def test_instruction_renders_working_dir_with_forward_slashes(
working_dir: PurePath,
):
"""The directory is the environment's own, not a re-spelling of it.

A remote environment is a Linux sandbox that reports a POSIX directory such
as `/home/user`, built from a POSIX string. On a Windows host the `Path` is
a `WindowsPath`, whose `str()` would tell the model the directory is
`\\home\\user`, which is not a directory the sandbox has.
"""
env = _FakeEnvironment(stdout="", file_content=b"", working_dir=working_dir)
toolset = EnvironmentToolset(environment=env)

instruction = await _instruction(toolset)

# The directory is the first line of the injected instruction, and the
# template appends a forward slash, so an unrendered `str()` shows a
# backslash there on a Windows host.
assert instruction.splitlines()[0] == "Your environment is at /home/user/"


class _RaisingBeforeInitEnvironment(_FakeEnvironment):
"""Raises from `working_dir` until initialized, as older environments do."""

Expand Down
Loading