Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix: handle nullable datetime in _fix_timezone methods (#14073)

Rohit Malhotra033c6202b7be
diff --git a/enterprise/server/sharing/sql_shared_conversation_info_service.py b/enterprise/server/sharing/sql_shared_conversation_info_service.py
index 4474dca471..737d80aa02 100644
--- a/enterprise/server/sharing/sql_shared_conversation_info_service.py
+++ b/enterprise/server/sharing/sql_shared_conversation_info_service.py
@@ -26,6 +26,7 @@ from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from storage.stored_conversation_metadata_saas import StoredConversationMetadataSaas
+from openhands.agent_server.utils import utc_now
from openhands.app_server.app_conversation.sql_app_conversation_info_service import (
StoredConversationMetadata,
)
@@ -146,9 +147,15 @@ class SQLSharedConversationInfoService(SharedConversationInfoService):
updated_at=updated_at,
)
- def _fix_timezone(self, value: datetime) -> datetime:
+ def _fix_timezone(self, value: datetime | None) -> datetime:
"""Sqlite does not store timezones - and since we can't update the existing models
- we assume UTC if the timezone is missing."""
+ we assume UTC if the timezone is missing. Returns current UTC time if value is None.
+ """
+ if value is None:
+ # Fallback for legacy data: use current time to match model defaults.
+ # The DB columns have default=utc_now, so None only occurs in legacy records.
+ # Using utc_now() keeps the API model non-nullable and matches new record behavior.
+ return utc_now()
if not value.tzinfo:
value = value.replace(tzinfo=UTC)
return value
diff --git a/openhands/app_server/app_conversation/sql_app_conversation_info_service.py b/openhands/app_server/app_conversation/sql_app_conversation_info_service.py
index 92884644bf..9854af072f 100644
--- a/openhands/app_server/app_conversation/sql_app_conversation_info_service.py
+++ b/openhands/app_server/app_conversation/sql_app_conversation_info_service.py
@@ -570,10 +570,15 @@ class SQLAppConversationInfoService(AppConversationInfoService):
updated_at=updated_at,
)
- def _fix_timezone(self, value: datetime) -> datetime:
- """Sqlite does not stpre timezones - and since we can't update the existing models
- we assume UTC if the timezone is missing.
+ def _fix_timezone(self, value: datetime | None) -> datetime:
+ """Sqlite does not store timezones - and since we can't update the existing models
+ we assume UTC if the timezone is missing. Returns current UTC time if value is None.
"""
+ if value is None:
+ # Fallback for legacy data: use current time to match model defaults.
+ # The DB columns have default=utc_now, so None only occurs in legacy records.
+ # Using utc_now() keeps the API model non-nullable and matches new record behavior.
+ return utc_now()
if not value.tzinfo:
value = value.replace(tzinfo=UTC)
return value
diff --git a/tests/unit/app_server/test_sql_app_conversation_info_service.py b/tests/unit/app_server/test_sql_app_conversation_info_service.py
index 6322dc27be..aa228df881 100644
--- a/tests/unit/app_server/test_sql_app_conversation_info_service.py
+++ b/tests/unit/app_server/test_sql_app_conversation_info_service.py
@@ -1241,3 +1241,32 @@ class TestSandboxIdFilter:
sandbox_id__eq='sandbox_time_test', created_at__gte=cutoff
)
assert count == 2
+
+
+class TestFixTimezone:
+ """Test suite for the _fix_timezone helper method."""
+
+ def test_fix_timezone_with_none(self, service: SQLAppConversationInfoService):
+ """Test that None values return current UTC time."""
+ from openhands.agent_server.utils import utc_now
+
+ result = service._fix_timezone(None)
+ assert result.tzinfo == timezone.utc
+ # Verify it's close to current time (within 1 second)
+ assert abs((utc_now() - result).total_seconds()) < 1
+
+ def test_fix_timezone_with_naive_datetime(
+ self, service: SQLAppConversationInfoService
+ ):
+ """Test that naive datetime gets UTC timezone added."""
+ naive_dt = datetime(2024, 1, 1, 12, 0, 0)
+ result = service._fix_timezone(naive_dt)
+ assert result == datetime(2024, 1, 1, 12, 0, 0, tzinfo=timezone.utc)
+
+ def test_fix_timezone_with_aware_datetime(
+ self, service: SQLAppConversationInfoService
+ ):
+ """Test that aware datetime is returned unchanged."""
+ aware_dt = datetime(2024, 1, 1, 12, 0, 0, tzinfo=timezone.utc)
+ result = service._fix_timezone(aware_dt)
+ assert result == aware_dt