Skip to content

Commit 0247444

Browse files
ystorycopybara-github
authored andcommitted
Align event filtering and ordering logic
Copybara import of the project: -- d01a8fd by Leo Yongsul Kim <ystory84@gmail.com>: fix(DatabaseSessionService): Align event filtering and ordering logic This commit addresses inconsistencies in how DatabaseSessionService handles config.after_timestamp and config.num_recent_events parameters, aligning its behavior with InMemorySessionService and VertexAiSessionService. Key changes: - Made after_timestamp filtering inclusive - Corrected num_recent_events behavior to fetch the N most recent events - Refined timezone handling for after_timestamp - Updated the unit test test_get_session_with_config to includeSessionServiceType.DATABASE, allowing verification of these fixes. Fixes google#911 COPYBARA_INTEGRATE_REVIEW=google#915 from ystory:fix/database-session-timestamp-recency 5cc8cf5 PiperOrigin-RevId: 763874840
1 parent a6b5244 commit 0247444

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

src/google/adk/sessions/database_session_service.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414
import copy
1515
from datetime import datetime
16-
from datetime import timezone
1716
import json
1817
import logging
1918
from typing import Any
@@ -371,18 +370,16 @@ async def get_session(
371370
return None
372371

373372
if config and config.after_timestamp:
374-
after_dt = datetime.fromtimestamp(
375-
config.after_timestamp, tz=timezone.utc
376-
)
377-
timestamp_filter = StorageEvent.timestamp > after_dt
373+
after_dt = datetime.fromtimestamp(config.after_timestamp)
374+
timestamp_filter = StorageEvent.timestamp >= after_dt
378375
else:
379376
timestamp_filter = True
380377

381378
storage_events = (
382379
session_factory.query(StorageEvent)
383380
.filter(StorageEvent.session_id == storage_session.id)
384381
.filter(timestamp_filter)
385-
.order_by(StorageEvent.timestamp.asc())
382+
.order_by(StorageEvent.timestamp.desc())
386383
.limit(
387384
config.num_recent_events
388385
if config and config.num_recent_events
@@ -429,7 +426,7 @@ async def get_session(
429426
error_message=e.error_message,
430427
interrupted=e.interrupted,
431428
)
432-
for e in storage_events
429+
for e in reversed(storage_events)
433430
]
434431
return session
435432

tests/unittests/sessions/test_session_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ async def test_append_event_complete(service_type):
315315

316316

317317
@pytest.mark.asyncio
318-
@pytest.mark.parametrize('service_type', [SessionServiceType.IN_MEMORY])
318+
@pytest.mark.parametrize('service_type', [SessionServiceType.IN_MEMORY, SessionServiceType.DATABASE])
319319
async def test_get_session_with_config(service_type):
320320
session_service = get_session_service(service_type)
321321
app_name = 'my_app'

0 commit comments

Comments
 (0)