Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(llma): make /ticket org check async-safe (#61557)

Georgiy Tarasov5ef57b00a62e
diff --git a/ee/hogai/chat_agent/slash_commands/commands/ticket/command.py b/ee/hogai/chat_agent/slash_commands/commands/ticket/command.py
index 30bc434d8fd0..88a20dcc02fe 100644
--- a/ee/hogai/chat_agent/slash_commands/commands/ticket/command.py
+++ b/ee/hogai/chat_agent/slash_commands/commands/ticket/command.py
@@ -4,6 +4,7 @@ from uuid import uuid4
from django.conf import settings
from django.utils import timezone
+from asgiref.sync import sync_to_async
from langchain_core.messages import (
AIMessage,
HumanMessage as LangchainHumanMessage,
@@ -29,21 +30,23 @@ class TicketCommand(SlashCommand):
_window_manager = AnthropicConversationCompactionManager()
- def _is_organization_new(self) -> bool:
+ async def _is_organization_new(self) -> bool:
"""Check if the organization was created less than 3 months ago."""
- org_created_at = self._team.organization.created_at
+ # `self._team.organization` is a FK access that hits the DB when not prefetched,
+ # so it must be wrapped to be safe inside this async context.
+ org_created_at = await sync_to_async(lambda: self._team.organization.created_at)()
if not org_created_at:
return False
months_since_creation = (timezone.now() - org_created_at).days / 30
return months_since_creation < 3
- def _can_create_ticket(self, config: RunnableConfig) -> bool:
+ async def _can_create_ticket(self, config: RunnableConfig) -> bool:
"""Check if user's subscription allows ticket creation."""
# Enable ticket creation in local dev
if settings.DEBUG:
return True
- if self._is_organization_new():
+ if await self._is_organization_new():
return True
billing_context_data = config.get("configurable", {}).get("billing_context")
@@ -61,7 +64,7 @@ class TicketCommand(SlashCommand):
return has_paid_subscription or has_active_trial
async def execute(self, config: RunnableConfig, state: AssistantState) -> PartialAssistantState:
- if not self._can_create_ticket(config):
+ if not await self._can_create_ticket(config):
return PartialAssistantState(
messages=[
AssistantMessage(
diff --git a/ee/hogai/chat_agent/slash_commands/commands/ticket/test/test_ticket.py b/ee/hogai/chat_agent/slash_commands/commands/ticket/test/test_ticket.py
index 760c4a8c354a..71677b263531 100644
--- a/ee/hogai/chat_agent/slash_commands/commands/ticket/test/test_ticket.py
+++ b/ee/hogai/chat_agent/slash_commands/commands/ticket/test/test_ticket.py
@@ -2,10 +2,11 @@ from datetime import timedelta
import pytest
from posthog.test.base import BaseTest
-from unittest.mock import patch
+from unittest.mock import AsyncMock, patch
from django.utils import timezone
+from asgiref.sync import sync_to_async
from langchain_core.runnables import RunnableConfig
from posthog.schema import AssistantMessage, HumanMessage
@@ -168,20 +169,22 @@ class TestTicketCommand(BaseTest):
)
self.assertFalse(self.command._is_first_message(state))
- def test_is_organization_new_returns_true_for_recent_org(self):
+ @pytest.mark.asyncio
+ async def test_is_organization_new_returns_true_for_recent_org(self):
"""Test that _is_organization_new returns True for org created less than 3 months ago."""
self.team.organization.created_at = timezone.now() - timedelta(days=30)
- self.team.organization.save()
- self.assertTrue(self.command._is_organization_new())
+ await sync_to_async(self.team.organization.save)()
+ self.assertTrue(await self.command._is_organization_new())
- def test_is_organization_new_returns_false_for_old_org(self):
+ @pytest.mark.asyncio
+ async def test_is_organization_new_returns_false_for_old_org(self):
"""Test that _is_organization_new returns False for org created more than 3 months ago."""
self.team.organization.created_at = timezone.now() - timedelta(days=100)
- self.team.organization.save()
- self.assertFalse(self.command._is_organization_new())
+ await sync_to_async(self.team.organization.save)()
+ self.assertFalse(await self.command._is_organization_new())
@pytest.mark.asyncio
- @patch.object(TicketCommand, "_is_organization_new", return_value=True)
+ @patch.object(TicketCommand, "_is_organization_new", new_callable=AsyncMock, return_value=True)
async def test_execute_new_org_free_user_allowed(self, mock_is_new):
"""Test that /ticket works for free users in new organizations."""
state = AssistantState(messages=[HumanMessage(content="/ticket")])
@@ -197,7 +200,7 @@ class TestTicketCommand(BaseTest):
self.assertIn("describe your issue", message.content.lower())
@pytest.mark.asyncio
- @patch.object(TicketCommand, "_is_organization_new", return_value=False)
+ @patch.object(TicketCommand, "_is_organization_new", new_callable=AsyncMock, return_value=False)
async def test_execute_old_org_free_user_blocked(self, mock_is_new):
"""Test that /ticket returns upgrade message for free users in old organizations."""
state = AssistantState(messages=[HumanMessage(content="/ticket")])