Skip to content

Commit d9a672e

Browse files
White-Mousecopybara-github
authored andcommitted
fix(sessions): honor zero recent events in database service
Merge google#5965 ## Summary - honor `GetSessionConfig(num_recent_events=0)` in `DatabaseSessionService` - add cross-backend regression coverage for zero recent events in `test_get_session_with_config` - keep existing positive-limit and no-config behavior unchanged ## Motivation `GetSessionConfig` documents `num_recent_events=0` as returning no events. InMemory and Sqlite already preserve that behavior, and ADK call sites use `num_recent_events=0` when they only need to check whether a session exists. `DatabaseSessionService` used a truthy check, so `0` skipped the SQL `LIMIT` and returned the full event history. ## Testing - `uv run --extra test pytest tests/unittests/sessions/test_session_service.py::test_get_session_with_config -q` - `uv run --extra test pytest tests/unittests/sessions -q` - `uvx pyink --check src/google/adk/sessions/database_session_service.py tests/unittests/sessions/test_session_service.py` - `uv run python -m py_compile src/google/adk/sessions/database_session_service.py tests/unittests/sessions/test_session_service.py` - `git diff --check` COPYBARA_INTEGRATE_REVIEW=google#5965 from White-Mouse:codex/adk-session-zero-events d64df8f PiperOrigin-RevId: 929438116
1 parent c11ac7d commit d9a672e

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

src/google/adk/sessions/database_session_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ async def get_session(
531531

532532
stmt = stmt.order_by(schema.StorageEvent.timestamp.desc())
533533

534-
if config and config.num_recent_events:
534+
if config and config.num_recent_events is not None:
535535
stmt = stmt.limit(config.num_recent_events)
536536

537537
result = await sql_session.execute(stmt)

tests/unittests/sessions/test_session_service.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,13 @@ async def test_get_session_with_config(session_service):
10581058
events = session.events
10591059
assert len(events) == num_test_events
10601060

1061+
# Explicitly requesting zero recent events should return no event history.
1062+
config = GetSessionConfig(num_recent_events=0)
1063+
session = await session_service.get_session(
1064+
app_name=app_name, user_id=user_id, session_id=session.id, config=config
1065+
)
1066+
assert not session.events
1067+
10611068
# Only expect the most recent 3 events.
10621069
num_recent_events = 3
10631070
config = GetSessionConfig(num_recent_events=num_recent_events)

0 commit comments

Comments
 (0)