Skip to content

Commit d33ca5f

Browse files
wyf7107copybara-github
authored andcommitted
fix: support nested agent paths in dot_adk_folder resolution
Updates dot_adk_folder_for_agent to resolve app names containing dots (e.g., "parent.child") to nested subdirectories (e.g., "parent/child") instead of a single flat directory. This ensures that local storage and session databases are correctly located for nested agents Co-authored-by: Yifan Wang <wanyif@google.com> PiperOrigin-RevId: 952905400
1 parent 26f3d45 commit d33ca5f

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

src/google/adk/cli/utils/dot_adk_folder.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def _resolve_agent_dir(*, agents_root: Path | str, app_name: str) -> Path:
2323
"""Resolves the agent directory with safety checks."""
2424
agents_root_path = Path(agents_root).resolve()
2525
agent_dir = (agents_root_path / app_name).resolve()
26+
if "." in app_name:
27+
agent_dir = (agents_root_path / app_name.replace(".", "/")).resolve()
2628
if not agent_dir.is_relative_to(agents_root_path):
2729
raise ValueError(
2830
f"Invalid app_name '{app_name}': resolves outside base directory"

tests/unittests/cli/utils/test_dot_adk_folder.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,15 @@ def test_for_agent_rejects_prefix_sibling_escape(tmp_path: Path):
5555
# "agents_evil"); a startswith() check would wrongly accept this.
5656
with pytest.raises(ValueError):
5757
dot_adk_folder_for_agent(agents_root=agents_root, app_name="../agents_evil")
58+
59+
60+
def test_for_agent_resolves_nested_agent(tmp_path: Path):
61+
agents_root = tmp_path / "agents"
62+
nested_agent = agents_root / "multi_agent" / "hello_world_ma"
63+
nested_agent.mkdir(parents=True)
64+
65+
folder = dot_adk_folder_for_agent(
66+
agents_root=agents_root, app_name="multi_agent.hello_world_ma"
67+
)
68+
69+
assert folder.agent_dir == nested_agent.resolve()

tests/unittests/cli/utils/test_local_storage.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ async def test_per_agent_session_service_creates_scoped_dot_adk(
5454
assert agent_b_sessions.sessions[0].app_name == "agent_b"
5555

5656

57+
@pytest.mark.asyncio
58+
async def test_per_agent_session_service_supports_nested_agent_path(
59+
tmp_path: Path,
60+
) -> None:
61+
nested_agent = tmp_path / "multi_agent" / "hello_world_ma"
62+
nested_agent.mkdir(parents=True)
63+
64+
async with PerAgentDatabaseSessionService(agents_root=tmp_path) as service:
65+
await service.create_session(
66+
app_name="multi_agent.hello_world_ma", user_id="user_a"
67+
)
68+
69+
assert (nested_agent / ".adk" / "session.db").exists()
70+
assert not (tmp_path / "multi_agent.hello_world_ma").exists()
71+
72+
5773
@pytest.mark.asyncio
5874
async def test_per_agent_session_service_respects_app_name_alias(
5975
tmp_path: Path,

0 commit comments

Comments
 (0)