π OpenHands: AI-Driven Development
fix(app-server): preserve conversation created_at across lifecycle webhooks (#15243)
Sujit Patil3949e1cc17d9
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.pyindex da1539ea19..b535562bd2 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@@ -397,6 +397,28 @@ class SQLAppConversationInfoService(AppConversationInfoService):metrics = info.metrics or MetricsSnapshot()usage = metrics.accumulated_token_usage or TokenUsage()+ # Preserve the original creation time on update.+ #+ # ``save`` is an upsert (``merge`` on the primary key), and several+ # callers rebuild an ``AppConversationInfo`` from scratch rather than+ # mutating the loaded row (e.g. the ``on_conversation_update`` webhook,+ # which fires on every start/pause/resume/interrupt). Those callers do+ # not carry ``created_at`` forward, so ``AppConversationInfo`` fills it+ # from ``default_factory=utc_now``. Without this guard the merge would+ # overwrite the persisted ``created_at`` with "now" on every lifecycle+ # webhook, corrupting created-at ordering and the usage/billing+ # dashboards that bucket conversations by creation time. Look up the+ # stored value directly by primary key (not via ``_secure_select``) so+ # this works under the ADMIN webhook context as well.+ created_at = info.created_at+ existing_created_at = await self.db_session.scalar(+ select(StoredConversationMetadata.created_at).where(+ StoredConversationMetadata.conversation_id == str(info.id)+ )+ )+ if existing_created_at is not None:+ created_at = existing_created_at+stored = StoredConversationMetadata(conversation_id=str(info.id),selected_repository=info.selected_repository,@@ -404,7 +426,7 @@ class SQLAppConversationInfoService(AppConversationInfoService):git_provider=info.git_provider.value if info.git_provider else None,title=info.title,last_updated_at=info.updated_at,- created_at=info.created_at,+ created_at=created_at,trigger=info.trigger.value if info.trigger else None,pr_number=info.pr_number or [],# Cost and token metricsdiff --git a/tests/unit/app_server/test_sql_app_conversation_info_service.py b/tests/unit/app_server/test_sql_app_conversation_info_service.pyindex 4f68c9bc24..7f1b5aa2c1 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@@ -1270,3 +1270,110 @@ class TestFixTimezone:aware_dt = datetime(2024, 1, 1, 12, 0, 0, tzinfo=timezone.utc)result = service._fix_timezone(aware_dt)assert result == aware_dt+++class TestCreatedAtPreservation:+ """Regression tests: created_at must be immutable across re-saves.++ The ``on_conversation_update`` webhook rebuilds an ``AppConversationInfo``+ from scratch on every lifecycle transition and does not carry ``created_at``+ forward, so the model defaults it to ``utc_now()``. ``save_app_conversation_info``+ is an upsert, so without a guard the merge would clobber the persisted+ ``created_at`` with "now" on every webhook.+ """++ @pytest.mark.asyncio+ async def test_created_at_preserved_when_omitted_on_resave(+ self,+ service: SQLAppConversationInfoService,+ ):+ """A re-save that defaults created_at must not overwrite the stored value."""+ conversation_id = uuid4()+ original_created_at = datetime(2024, 1, 1, 12, 0, 0, tzinfo=timezone.utc)++ await service.save_app_conversation_info(+ AppConversationInfo(+ id=conversation_id,+ created_by_user_id=None,+ sandbox_id='sandbox_123',+ title='Original',+ created_at=original_created_at,+ updated_at=original_created_at,+ )+ )++ # Simulate the webhook rebuild: a fresh model whose created_at/updated_at+ # come from the default_factory (i.e. "now"), not the stored value.+ rebuilt = AppConversationInfo(+ id=conversation_id,+ created_by_user_id=None,+ sandbox_id='sandbox_123',+ title='Updated via webhook',+ )+ assert rebuilt.created_at > original_created_at # default_factory ran++ await service.save_app_conversation_info(rebuilt)++ stored = await service.get_app_conversation_info(conversation_id)+ assert stored is not None+ # created_at is preserved from the original insert...+ assert stored.created_at == original_created_at+ # ...while other fields (title) still update.+ assert stored.title == 'Updated via webhook'++ @pytest.mark.asyncio+ async def test_created_at_stable_across_repeated_webhooks(+ self,+ service: SQLAppConversationInfoService,+ ):+ """Repeated lifecycle webhooks must not drift created_at forward."""+ conversation_id = uuid4()+ original_created_at = datetime(2024, 1, 1, 12, 0, 0, tzinfo=timezone.utc)++ await service.save_app_conversation_info(+ AppConversationInfo(+ id=conversation_id,+ created_by_user_id=None,+ sandbox_id='sandbox_123',+ title='Original',+ created_at=original_created_at,+ updated_at=original_created_at,+ )+ )++ for i in range(3):+ await service.save_app_conversation_info(+ AppConversationInfo(+ id=conversation_id,+ created_by_user_id=None,+ sandbox_id='sandbox_123',+ title=f'Webhook update {i}',+ )+ )+ stored = await service.get_app_conversation_info(conversation_id)+ assert stored is not None+ assert stored.created_at == original_created_at++ @pytest.mark.asyncio+ async def test_created_at_used_on_first_insert(+ self,+ service: SQLAppConversationInfoService,+ ):+ """On the initial insert, the provided created_at is honored."""+ conversation_id = uuid4()+ created_at = datetime(2023, 6, 15, 8, 30, 0, tzinfo=timezone.utc)++ await service.save_app_conversation_info(+ AppConversationInfo(+ id=conversation_id,+ created_by_user_id=None,+ sandbox_id='sandbox_123',+ title='Fresh',+ created_at=created_at,+ updated_at=created_at,+ )+ )++ stored = await service.get_app_conversation_info(conversation_id)+ assert stored is not None+ assert stored.created_at == created_at