Skip to content

fix: Treat database update time as UTC for session's last update time #1474

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
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
18 changes: 12 additions & 6 deletions src/google/adk/sessions/database_session_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from __future__ import annotations

import copy
from datetime import datetime
from datetime import datetime, timezone
import json
import logging
from typing import Any
Expand Down Expand Up @@ -144,6 +144,12 @@ class StorageSession(Base):
def __repr__(self):
return f"<StorageSession(id={self.id}, update_time={self.update_time})>"

def create_timestamp_utc(self) -> datetime:
return self.create_time.replace(tzinfo=timezone.utc).timestamp()

def update_timestamp_utc(self) -> datetime:
return self.update_time.replace(tzinfo=timezone.utc).timestamp()


class StorageEvent(Base):
"""Represents an event stored in the database."""
Expand Down Expand Up @@ -412,7 +418,7 @@ async def create_session(
user_id=str(storage_session.user_id),
id=str(storage_session.id),
state=merged_state,
last_update_time=storage_session.update_time.timestamp(),
last_update_time=storage_session.update_timestamp_utc(),
)
return session

Expand Down Expand Up @@ -473,7 +479,7 @@ async def get_session(
user_id=user_id,
id=session_id,
state=merged_state,
last_update_time=storage_session.update_time.timestamp(),
last_update_time=storage_session.update_timestamp_utc(),
)
session.events = [e.to_event() for e in reversed(storage_events)]
return session
Expand All @@ -496,7 +502,7 @@ async def list_sessions(
user_id=user_id,
id=storage_session.id,
state={},
last_update_time=storage_session.update_time.timestamp(),
last_update_time=storage_session.update_timestamp_utc(),
)
sessions.append(session)
return ListSessionsResponse(sessions=sessions)
Expand Down Expand Up @@ -529,7 +535,7 @@ async def append_event(self, session: Session, event: Event) -> Event:
StorageSession, (session.app_name, session.user_id, session.id)
)

if storage_session.update_time.timestamp() > session.last_update_time:
if storage_session.update_timestamp_utc() > session.last_update_time:
raise ValueError(
"The last_update_time provided in the session object"
f" {datetime.fromtimestamp(session.last_update_time):'%Y-%m-%d %H:%M:%S'} is"
Expand Down Expand Up @@ -577,7 +583,7 @@ async def append_event(self, session: Session, event: Event) -> Event:
session_factory.refresh(storage_session)

# Update timestamp with commit time
session.last_update_time = storage_session.update_time.timestamp()
session.last_update_time = storage_session.update_timestamp_utc()

# Also update the in-memory session
await super().append_event(session=session, event=event)
Expand Down
Loading