Skip to content

fix(DatabaseSessionService): Align event filtering and ordering logic #915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/google/adk/sessions/database_session_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.
import copy
from datetime import datetime
from datetime import timezone
import json
import logging
from typing import Any
Expand Down Expand Up @@ -371,18 +370,16 @@ async def get_session(
return None

if config and config.after_timestamp:
after_dt = datetime.fromtimestamp(
config.after_timestamp, tz=timezone.utc
)
timestamp_filter = StorageEvent.timestamp > after_dt
after_dt = datetime.fromtimestamp(config.after_timestamp)
timestamp_filter = StorageEvent.timestamp >= after_dt
else:
timestamp_filter = True

storage_events = (
session_factory.query(StorageEvent)
.filter(StorageEvent.session_id == storage_session.id)
.filter(timestamp_filter)
.order_by(StorageEvent.timestamp.asc())
.order_by(StorageEvent.timestamp.desc())
.limit(
config.num_recent_events
if config and config.num_recent_events
Expand Down Expand Up @@ -429,7 +426,7 @@ async def get_session(
error_message=e.error_message,
interrupted=e.interrupted,
)
for e in storage_events
for e in reversed(storage_events)
]
return session

Expand Down
2 changes: 1 addition & 1 deletion tests/unittests/sessions/test_session_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ async def test_append_event_complete(service_type):


@pytest.mark.asyncio
@pytest.mark.parametrize('service_type', [SessionServiceType.IN_MEMORY])
@pytest.mark.parametrize('service_type', [SessionServiceType.IN_MEMORY, SessionServiceType.DATABASE])
async def test_get_session_with_config(service_type):
session_service = get_session_service(service_type)
app_name = 'my_app'
Expand Down