Skip to content

Commit f1944ac

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
fix: Preserve caller session_id as agent_engine:upstream_session_id on fallback session creation in AdkApp.
When `AdkApp.streaming_agent_run_with_events` receives a caller-supplied `request.session_id` and `session_service.get_session` does not find an existing session, `AdkApp` falls back to `self._init_session(...)` and creates a new session with a server-generated `session.id`. Pass `state={_UPSTREAM_SESSION_ID_KEY: request.session_id}` (`"agent_engine:upstream_session_id"`) to `session_service.create_session(...)` inside `_init_session` so custom ADK agents and plugins (including `on_user_message_callback` on turn 1) can correlate logs and telemetry with the caller's originating session ID via `session.state["agent_engine:upstream_session_id"]`. PiperOrigin-RevId: 1007686681
1 parent 1ba7a41 commit f1944ac

6 files changed

Lines changed: 683 additions & 12 deletions

File tree

‎agentplatform/frameworks/adk.py‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114

115115
_DEFAULT_APP_NAME = "default_app_name"
116116
_DEFAULT_USER_ID = "default-user-id"
117+
_UPSTREAM_SESSION_ID_KEY = "agent_engine:upstream_session_id"
117118
_TELEMETRY_API_DISABLED_WARNING = """\
118119
Tracing integration for Agent Engine has migrated to a new API.
119120
The 'telemetry.googleapis.com' has not been enabled in project %s.
@@ -984,14 +985,18 @@ async def _init_session(
984985
session_service: "BaseSessionService",
985986
artifact_service: "BaseArtifactService",
986987
request: _StreamRunRequest,
988+
state: Optional[Dict[str, Any]] = None,
987989
):
988990
"""Initializes the session, and returns the session id."""
989991
from google.adk.events.event import Event
990992

991-
session = await session_service.create_session(
992-
app_name=self._app_name(),
993-
user_id=request.user_id,
994-
)
993+
create_session_kwargs: Dict[str, Any] = {
994+
"app_name": self._app_name(),
995+
"user_id": request.user_id,
996+
}
997+
if state is not None:
998+
create_session_kwargs["state"] = state
999+
session = await session_service.create_session(**create_session_kwargs)
9951000
if not session:
9961001
raise RuntimeError("Create session failed.")
9971002
if request.events:
@@ -1530,6 +1535,11 @@ async def streaming_agent_run_with_events(self, request_json: str):
15301535
you have defined for the AdkApp. This method is primarily meant for
15311536
invocation from AgentSpace.
15321537
1538+
When `request_json` includes a `session_id` that is not found in
1539+
`session_service`, a new fallback session is created and the caller's
1540+
`session_id` is stored in `session.state` under
1541+
`agent_engine:upstream_session_id` (`_UPSTREAM_SESSION_ID_KEY`).
1542+
15331543
Args:
15341544
request_json (str):
15351545
Required. The request to stream responses for.
@@ -1594,6 +1604,7 @@ async def streaming_agent_run_with_events(self, request_json: str):
15941604
session_service=session_service,
15951605
artifact_service=artifact_service,
15961606
request=request,
1607+
state={_UPSTREAM_SESSION_ID_KEY: request.session_id},
15971608
)
15981609
else:
15991610
# Not providing a session ID will create a new in-memory session.

‎tests/unit/agentplatform/frameworks/test_frameworks_adk.py‎

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from google.genai import types
4444
import pytest
4545
import requests
46+
from google.adk.plugins.base_plugin import BasePlugin
4647
from google.adk.sessions.base_session_service import BaseSessionService
4748

4849

@@ -392,6 +393,30 @@ async def run_async(self, *args, **kwargs):
392393
)
393394

394395

396+
class _SessionStateCapturingPlugin(BasePlugin):
397+
"""Captures session state on user message and short-circuits the run."""
398+
399+
def __init__(self):
400+
super().__init__(name="session_state_capturing_plugin")
401+
self.user_message_session_states = []
402+
403+
async def on_user_message_callback(
404+
self, *, invocation_context, user_message
405+
):
406+
del user_message
407+
self.user_message_session_states.append(
408+
dict(invocation_context.session.state)
409+
)
410+
return None
411+
412+
async def before_run_callback(self, *, invocation_context):
413+
del invocation_context
414+
return types.Content(
415+
role="model",
416+
parts=[types.Part(text="Sweden")],
417+
)
418+
419+
395420
@pytest.mark.usefixtures("google_auth_mock")
396421
class TestAdkApp:
397422
def test_adk_version(self):
@@ -834,6 +859,187 @@ async def mock_run_async(*args, **kwargs):
834859
assert run_config is not None
835860
assert run_config.labels == labels
836861

862+
@pytest.mark.asyncio
863+
@pytest.mark.usefixtures(
864+
"default_instrumentor_builder_mock",
865+
"get_project_id_mock",
866+
)
867+
async def test_streaming_agent_run_with_events_fallback_session_preserves_upstream_session_id(
868+
self,
869+
):
870+
"""Seeds agent_engine:upstream_session_id in session.state on fallback."""
871+
plugin = _SessionStateCapturingPlugin()
872+
app = adk_template.AdkApp(agent=_TEST_AGENT, plugins=[plugin])
873+
app.set_up()
874+
875+
real_runner = app._tmpl_attrs[ # pylint: disable=protected-access
876+
"runner"
877+
]
878+
run_async_spy = mock.MagicMock(wraps=real_runner.run_async)
879+
real_runner.run_async = run_async_spy
880+
881+
request_json = json.dumps(
882+
{
883+
"user_id": _TEST_USER_ID,
884+
"session_id": "ge_originating_session_123",
885+
"message": {
886+
"parts": [
887+
{"text": "What is the exchange rate from USD to SEK?"}
888+
],
889+
"role": "user",
890+
},
891+
}
892+
)
893+
894+
events = []
895+
async for event in app.streaming_agent_run_with_events(
896+
request_json=request_json,
897+
):
898+
events.append(event)
899+
900+
assert len(events) == 1
901+
run_async_spy.assert_called_once()
902+
created_session_id = run_async_spy.call_args.kwargs["session_id"]
903+
assert created_session_id != "ge_originating_session_123"
904+
assert run_async_spy.call_args.kwargs["state_delta"] is None
905+
assert plugin.user_message_session_states == [
906+
{"agent_engine:upstream_session_id": "ge_originating_session_123"}
907+
]
908+
session = await app.async_get_session(
909+
user_id=_TEST_USER_ID,
910+
session_id=created_session_id,
911+
)
912+
assert session.state == {
913+
"agent_engine:upstream_session_id": "ge_originating_session_123"
914+
}
915+
916+
@pytest.mark.asyncio
917+
@pytest.mark.usefixtures(
918+
"default_instrumentor_builder_mock",
919+
"get_project_id_mock",
920+
)
921+
async def test_streaming_agent_run_with_events_fallback_session_with_authorizations(
922+
self,
923+
):
924+
"""Persists upstream_session_id in session.state while keeping tokens temp."""
925+
plugin = _SessionStateCapturingPlugin()
926+
app = adk_template.AdkApp(agent=_TEST_AGENT, plugins=[plugin])
927+
app.set_up()
928+
929+
session_service = app._tmpl_attrs[ # pylint: disable=protected-access
930+
"session_service"
931+
]
932+
create_session_spy = mock.AsyncMock(
933+
wraps=session_service.create_session
934+
)
935+
session_service.create_session = create_session_spy
936+
937+
real_runner = app._tmpl_attrs[ # pylint: disable=protected-access
938+
"runner"
939+
]
940+
run_async_spy = mock.MagicMock(wraps=real_runner.run_async)
941+
real_runner.run_async = run_async_spy
942+
943+
request_json = json.dumps(
944+
{
945+
"authorizations": {
946+
"test_user_id1": {"access_token": "test_access_token"},
947+
},
948+
"user_id": _TEST_USER_ID,
949+
"session_id": "ge_originating_session_123",
950+
"message": {
951+
"parts": [
952+
{"text": "What is the exchange rate from USD to SEK?"}
953+
],
954+
"role": "user",
955+
},
956+
}
957+
)
958+
959+
events = []
960+
async for event in app.streaming_agent_run_with_events(
961+
request_json=request_json,
962+
):
963+
events.append(event)
964+
965+
assert len(events) == 1
966+
create_session_spy.assert_called_once()
967+
assert create_session_spy.call_args.kwargs.get("state") == {
968+
"agent_engine:upstream_session_id": "ge_originating_session_123"
969+
}
970+
run_async_spy.assert_called_once()
971+
created_session_id = run_async_spy.call_args.kwargs["session_id"]
972+
assert created_session_id != "ge_originating_session_123"
973+
assert run_async_spy.call_args.kwargs["state_delta"] == {
974+
"temp:test_user_id1": "test_access_token"
975+
}
976+
assert plugin.user_message_session_states == [
977+
{"agent_engine:upstream_session_id": "ge_originating_session_123"}
978+
]
979+
session = await app.async_get_session(
980+
user_id=_TEST_USER_ID,
981+
session_id=created_session_id,
982+
)
983+
assert session.state == {
984+
"agent_engine:upstream_session_id": "ge_originating_session_123"
985+
}
986+
assert "test_access_token" not in json.dumps(session.state)
987+
988+
@pytest.mark.asyncio
989+
@pytest.mark.usefixtures(
990+
"default_instrumentor_builder_mock",
991+
"get_project_id_mock",
992+
)
993+
async def test_streaming_agent_run_with_events_existing_session_omits_upstream_session_id(
994+
self,
995+
):
996+
"""Does not add agent_engine:upstream_session_id when get_session succeeds."""
997+
plugin = _SessionStateCapturingPlugin()
998+
app = adk_template.AdkApp(agent=_TEST_AGENT, plugins=[plugin])
999+
app.set_up()
1000+
1001+
await app.async_create_session(
1002+
user_id=_TEST_USER_ID,
1003+
session_id="test_session_id",
1004+
)
1005+
1006+
real_runner = app._tmpl_attrs[ # pylint: disable=protected-access
1007+
"runner"
1008+
]
1009+
run_async_spy = mock.MagicMock(wraps=real_runner.run_async)
1010+
real_runner.run_async = run_async_spy
1011+
1012+
request_json = json.dumps(
1013+
{
1014+
"user_id": _TEST_USER_ID,
1015+
"session_id": "test_session_id",
1016+
"message": {
1017+
"parts": [
1018+
{"text": "What is the exchange rate from USD to SEK?"}
1019+
],
1020+
"role": "user",
1021+
},
1022+
}
1023+
)
1024+
1025+
events = []
1026+
async for event in app.streaming_agent_run_with_events(
1027+
request_json=request_json,
1028+
):
1029+
events.append(event)
1030+
1031+
assert len(events) == 1
1032+
run_async_spy.assert_called_once()
1033+
assert run_async_spy.call_args.kwargs["session_id"] == "test_session_id"
1034+
assert run_async_spy.call_args.kwargs["state_delta"] is None
1035+
assert plugin.user_message_session_states == [{}]
1036+
session = await app.async_get_session(
1037+
user_id=_TEST_USER_ID,
1038+
session_id="test_session_id",
1039+
)
1040+
assert "agent_engine:upstream_session_id" not in session.state
1041+
assert not session.state
1042+
8371043
@pytest.mark.asyncio
8381044
@mock.patch.dict(
8391045
os.environ,

0 commit comments

Comments
 (0)