: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(conversations): triage composed tickets once the customer replies (#125645)
Alex V1fd44fce70c2
diff --git a/products/conversations/backend/temporal/coordinator.py b/products/conversations/backend/temporal/coordinator.pyindex bbb8442b3285..1f9ba29db9ce 100644--- a/products/conversations/backend/temporal/coordinator.py+++ b/products/conversations/backend/temporal/coordinator.py@@ -194,13 +194,18 @@ def _collect_eligible(lookback_minutes: int = TICKET_LOOKBACK_MINUTES) -> list[E# note ("AI") or a human/team reply ("support"/"team"/"human"). Those are excluded below# for first-draft tickets. Awaiting tickets already have an AI question, so they skip# that gate and re-engage only on a newer settled customer reply with no human after it.+ # A team-opened ticket (compose, or a team member starting the thread) has a team comment+ # before the first customer comment. That opener is not engagement, so such a ticket is+ # engaged only by a non-customer comment newer than the first customer comment.# 2. Settle/debounce: the latest customer comment timestamp per ticket.by_team: dict[int, list[str]] = {}for team_id, ticket_id_str, _created_at, _awaiting, _round in candidates:by_team.setdefault(team_id, []).append(ticket_id_str)engaged: set[str] = set()+ first_customer_msg: dict[str, datetime] = {}latest_customer_msg: dict[str, datetime] = {}+ earliest_non_customer_msg: dict[str, datetime] = {}latest_ai_msg: dict[str, datetime] = {}latest_human_msg: dict[str, datetime] = {}for team_id, ticket_ids in by_team.items():@@ -214,8 +219,14 @@ def _collect_eligible(lookback_minutes: int = TICKET_LOOKBACK_MINUTES) -> list[Eprev = latest_customer_msg.get(item_id)if prev is None or comment_created_at > prev:latest_customer_msg[item_id] = comment_created_at+ first = first_customer_msg.get(item_id)+ if first is None or comment_created_at < first:+ first_customer_msg[item_id] = comment_created_atcontinueengaged.add(item_id)+ earliest = earliest_non_customer_msg.get(item_id)+ if earliest is None or comment_created_at < earliest:+ earliest_non_customer_msg[item_id] = comment_created_atif author_type == "AI":prev_ai = latest_ai_msg.get(item_id)if prev_ai is None or comment_created_at > prev_ai:@@ -243,7 +254,22 @@ def _collect_eligible(lookback_minutes: int = TICKET_LOOKBACK_MINUTES) -> list[Eif last_customer > settle_cutoff:continueelse:- if ticket_id_str in engaged:+ first_customer = first_customer_msg.get(ticket_id_str)+ earliest_team = earliest_non_customer_msg.get(ticket_id_str)+ team_opened = earliest_team is not None and (first_customer is None or earliest_team < first_customer)+ if team_opened:+ # Wait for the customer without recording a skip, because no team reply exists yet.+ if first_customer is None:+ continue+ last_human = latest_human_msg.get(ticket_id_str)+ if last_human is not None and last_human > first_customer:+ if ticket_id_str in untriaged_ticket_ids:+ skipped_ticket_ids_by_result[SKIPPED_TEAM_REPLIED].append(ticket_id_str)+ continue+ last_ai = latest_ai_msg.get(ticket_id_str)+ if last_ai is not None and last_ai > first_customer:+ continue+ elif ticket_id_str in engaged:if ticket_id_str in untriaged_ticket_ids and ticket_id_str in latest_human_msg:skipped_ticket_ids_by_result[SKIPPED_TEAM_REPLIED].append(ticket_id_str)continuediff --git a/products/conversations/backend/temporal/tests/test_coordinator.py b/products/conversations/backend/temporal/tests/test_coordinator.pyindex 256e8cde9038..5c2d6657a405 100644--- a/products/conversations/backend/temporal/tests/test_coordinator.py+++ b/products/conversations/backend/temporal/tests/test_coordinator.py@@ -145,7 +145,7 @@ class TestCollectEligible:# One query per team: .filter().values_list("item_id", author_type, created_at) rows.now = timezone.now()- rows: list[tuple[str, str, object]] = []+ rows: list[tuple[str, str, object]] = [(ticket_id, "customer", now - timedelta(minutes=10))]if has_ai:rows.append((ticket_id, "AI", now))if has_team_reply:@@ -222,7 +222,11 @@ class TestCollectEligible:mock_ticket_model.objects.filter.return_value.select_related.return_value = [ticket]# AI note exists → engaged → skipped.- mock_comment_model.objects.filter.return_value.values_list.return_value = [(ticket_id, "AI", timezone.now())]+ now = timezone.now()+ mock_comment_model.objects.filter.return_value.values_list.return_value = [+ (ticket_id, "customer", now - timedelta(minutes=10)),+ (ticket_id, "AI", now),+ ]result = _collect_eligible()assert result == []@@ -495,6 +499,40 @@ class TestCollectEligibleAwaitingClarification:else:assert result == []+ @parameterized.expand(+ [+ ("opener_without_customer_reply", [("human", 4)], False, None),+ ("settled_customer_reply_after_opener", [("human", 4), ("customer", 3)], True, None),+ ("customer_reply_still_settling", [("human", 4), ("customer", 0)], False, None),+ (+ "team_replied_after_customer",+ [("human", 4), ("customer", 3), ("human", 2)],+ False,+ "skipped_team_replied",+ ),+ ("ai_note_after_customer", [("human", 4), ("customer", 3), ("AI", 2)], False, None),+ ("inbound_team_reply", [("customer", 4), ("human", 3)], False, "skipped_team_replied"),+ ]+ )+ @pytest.mark.django_db+ @patch(f"{COORD_MODULE}._is_master_flag_enabled", return_value=True)+ def test_first_draft_eligibility_by_comment_order(+ self,+ _name,+ comments,+ expected_eligible,+ expected_result,+ mock_master_flag,+ ):+ team, ticket = self._team_and_ticket(status="open", awaiting=False)+ for author_type, minutes_ago in comments:+ self._comment(team, ticket, author_type=author_type, minutes_ago=minutes_ago)++ result = _collect_eligible()+ assert [t.ticket_id for t in result] == ([str(ticket.id)] if expected_eligible else [])+ ticket.refresh_from_db()+ assert ticket.ai_triage.get("result") == expected_result+@pytest.mark.django_db@patch(f"{COORD_MODULE}._is_master_flag_enabled", return_value=True)def test_second_clarification_round_is_not_eligible(self, mock_master_flag):@@ -514,7 +552,6 @@ class TestCollectEligibleAwaitingClarification:@parameterized.expand([- ("team_started_ticket", {}, True, None, "human", "skipped_team_replied"),("ai_note_only", {}, True, None, "AI", None),("channel_excluded",