Skip to content

Commit 46d73be

Browse files
GWealecopybara-github
authored andcommitted
chore: Add more info to "Session not found" error message in ADK runners for differently named app and folder
PiperOrigin-RevId: 815795412
1 parent e0dd06f commit 46d73be

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,19 @@ def _perform_load(self, agent_name: str) -> Union[BaseAgent, App]:
205205
envs.load_dotenv_for_agent(actual_agent_name, str(agents_dir))
206206

207207
if root_agent := self._load_from_module_or_package(actual_agent_name):
208+
self._ensure_app_name_matches(
209+
maybe_app=root_agent,
210+
expected_app_name=actual_agent_name,
211+
agents_dir=agents_dir,
212+
)
208213
return root_agent
209214

210215
if root_agent := self._load_from_submodule(actual_agent_name):
216+
self._ensure_app_name_matches(
217+
maybe_app=root_agent,
218+
expected_app_name=actual_agent_name,
219+
agents_dir=agents_dir,
220+
)
211221
return root_agent
212222

213223
if root_agent := self._load_from_yaml_config(actual_agent_name, agents_dir):
@@ -223,6 +233,33 @@ def _perform_load(self, agent_name: str) -> Union[BaseAgent, App]:
223233
" file can be loaded if present, and a root_agent is exposed."
224234
)
225235

236+
def _ensure_app_name_matches(
237+
self,
238+
*,
239+
maybe_app: Union[BaseAgent, App],
240+
expected_app_name: str,
241+
agents_dir: str,
242+
) -> None:
243+
"""Raises a detailed error when App.name does not match its directory."""
244+
245+
if not isinstance(maybe_app, App):
246+
return
247+
248+
# Built-in apps live under double-underscore directories.
249+
if expected_app_name.startswith("__"):
250+
return
251+
252+
if maybe_app.name == expected_app_name:
253+
return
254+
255+
raise ValueError(
256+
"App name mismatch detected. The App defined at "
257+
f"'{agents_dir}/{expected_app_name}' declares name "
258+
f"'{maybe_app.name}', but ADK expects it to match the directory "
259+
f"name '{expected_app_name}'. Rename the App or the folder so they "
260+
"match, then reload."
261+
)
262+
226263
@override
227264
def load_agent(self, agent_name: str) -> Union[BaseAgent, App]:
228265
"""Load an agent module (with caching & .env) and return its root_agent."""

src/google/adk/runners.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
from __future__ import annotations
1616

1717
import asyncio
18+
import inspect
1819
import logging
20+
from pathlib import Path
1921
import queue
2022
from typing import Any
2123
from typing import AsyncGenerator
@@ -149,6 +151,11 @@ def __init__(
149151
self.memory_service = memory_service
150152
self.credential_service = credential_service
151153
self.plugin_manager = PluginManager(plugins=plugins)
154+
(
155+
self._agent_origin_app_name,
156+
self._agent_origin_dir,
157+
) = self._infer_agent_origin(self.agent)
158+
self._enforce_app_name_alignment()
152159

153160
def _validate_runner_params(
154161
self,
@@ -211,6 +218,48 @@ def _validate_runner_params(
211218
)
212219
return app_name, agent, context_cache_config, resumability_config, plugins
213220

221+
def _infer_agent_origin(
222+
self, agent: BaseAgent
223+
) -> tuple[Optional[str], Optional[Path]]:
224+
module = inspect.getmodule(agent.__class__)
225+
if not module:
226+
return None, None
227+
module_file = getattr(module, '__file__', None)
228+
if not module_file:
229+
return None, None
230+
module_path = Path(module_file).resolve()
231+
project_root = Path.cwd()
232+
try:
233+
module_path.relative_to(project_root)
234+
except ValueError:
235+
return None, module_path.parent
236+
237+
current = module_path.parent
238+
while current != project_root and current.parent != current:
239+
parent = current.parent
240+
if parent.name == 'agents':
241+
return current.name, current
242+
current = parent
243+
244+
return None, module_path.parent
245+
246+
def _enforce_app_name_alignment(self) -> None:
247+
origin_name = self._agent_origin_app_name
248+
origin_dir = self._agent_origin_dir
249+
if not origin_name or origin_name.startswith('__'):
250+
return
251+
if origin_name == self.app_name:
252+
return
253+
origin_location = str(origin_dir) if origin_dir else origin_name
254+
message = (
255+
'App name mismatch detected. The runner is configured with '
256+
f'app name "{self.app_name}", but the root agent was loaded from '
257+
f'"{origin_location}", which implies app name "{origin_name}". '
258+
'Rename the App or its directory so the names match before running '
259+
'the agent.'
260+
)
261+
raise ValueError(message)
262+
214263
def run(
215264
self,
216265
*,

0 commit comments

Comments
 (0)