Skip to content

Commit ac990df

Browse files
yupengtangcopybara-github
authored andcommitted
fix(sessions): check user_id is not None instead of truthiness in SqliteSessionService.list_sessions
Previously, `SqliteSessionService.list_sessions` used `if user_id:` instead of `if user_id is not None:`. Because `bool("")` is `False`, passing an empty string `user_id=""` fell through to the unfiltered `WHERE app_name=?` query and returned sessions and user state across all users in the database. Check `if user_id is not None:` so `user_id=""` only queries sessions belonging to `""`, matching `InMemorySessionService`, `DatabaseSessionService`, and `RedisSessionService`, and add a shared contract test covering `user_id=""`. Merge #7171 PiperOrigin-RevId: 995906908
1 parent cb9b8f7 commit ac990df

3 files changed

Lines changed: 28 additions & 5 deletions

File tree

‎src/google/adk/sessions/sqlite_session_service.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ async def list_sessions(
358358
sessions_list = []
359359
async with self._get_db_connection() as db:
360360
# Fetch sessions
361-
if user_id:
361+
if user_id is not None:
362362
session_rows = await db.execute_fetchall(
363363
"SELECT id, user_id, state, update_time FROM sessions WHERE"
364364
" app_name=? AND user_id=? ORDER BY update_time, user_id, id",
@@ -376,7 +376,7 @@ async def list_sessions(
376376

377377
# Fetch user states
378378
user_states_map: dict[str, dict[str, Any]] = {}
379-
if user_id:
379+
if user_id is not None:
380380
user_state = await self._get_user_state(db, app_name, user_id)
381381
if user_state:
382382
user_states_map[user_id] = user_state

‎tests/unittests/sessions/_conformance.py‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,6 @@ async def _make_per_agent_database(
137137
_Backend('in_memory_light_copy', _make_in_memory_light_copy),
138138
_Backend('database', _make_database),
139139
_Backend('sqlite', _make_sqlite),
140-
# One more Redis divergence has no contract test to hang an xfail on yet:
141-
# it builds its key scan pattern from a truthiness check on the user id, so
142-
# an empty one lists every user's sessions.
143140
_Backend(
144141
'redis',
145142
_make_redis,

‎tests/unittests/sessions/test_session_service.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,32 @@ async def test_list_sessions_all_users(session_service):
711711
assert sessions_all_map['session2a'].state == {'key': 'value2a'}
712712

713713

714+
@pytest.mark.asyncio
715+
async def test_list_sessions_with_empty_user_id_lists_only_that_user(
716+
session_service,
717+
):
718+
"""An empty user id is a user id, not a request for every user."""
719+
app_name = 'my_app'
720+
await session_service.create_session(
721+
app_name=app_name, user_id='', session_id='empty_user_session'
722+
)
723+
await session_service.create_session(
724+
app_name=app_name,
725+
user_id='other_user',
726+
session_id='other_user_session',
727+
state={'user:name': 'other'},
728+
)
729+
730+
list_sessions_response = await session_service.list_sessions(
731+
app_name=app_name, user_id=''
732+
)
733+
734+
assert [s.id for s in list_sessions_response.sessions] == [
735+
'empty_user_session'
736+
]
737+
assert list_sessions_response.sessions[0].state == {}
738+
739+
714740
@pytest.mark.asyncio
715741
async def test_app_state_is_shared_by_all_users_of_app(session_service):
716742
app_name = 'my_app'

0 commit comments

Comments
 (0)