Skip to content

Commit f8dff86

Browse files
committed
fix(sessions): respect num_recent_events=0 in DatabaseSessionService
`DatabaseSessionService.get_session` used a truthy check (`if config and config.num_recent_events:`) which treated `0` as "no limit" and returned all events, contradicting the documented behavior in `GetSessionConfig` ("if 0, no events are returned"). Switch to `is not None` so that `LIMIT 0` is appended for the zero case. Add a boundary assertion to `test_get_session_with_config`, which is parametrized over all four session backends (InMemory, InMemory-light-copy, Database, Sqlite). Closes #5730
1 parent bd062ec commit f8dff86

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
@@ -520,7 +520,7 @@ async def get_session(
520520

521521
stmt = stmt.order_by(schema.StorageEvent.timestamp.desc())
522522

523-
if config and config.num_recent_events:
523+
if config and config.num_recent_events is not None:
524524
stmt = stmt.limit(config.num_recent_events)
525525

526526
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
@@ -1067,6 +1067,13 @@ async def test_get_session_with_config(session_service):
10671067
assert len(events) == num_recent_events
10681068
assert events[0].timestamp == num_test_events - num_recent_events + 1
10691069

1070+
# num_recent_events=0 should return no events (boundary case).
1071+
config = GetSessionConfig(num_recent_events=0)
1072+
session = await session_service.get_session(
1073+
app_name=app_name, user_id=user_id, session_id=session.id, config=config
1074+
)
1075+
assert session.events == []
1076+
10701077
# Only expect events after timestamp 4.0 (inclusive), i.e., 2 events.
10711078
after_timestamp = 4.0
10721079
config = GetSessionConfig(after_timestamp=after_timestamp)

0 commit comments

Comments
 (0)