: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): stop private notes blocking ai suggested replies (#128368)
Alex Vb7f58200e771
diff --git a/products/conversations/backend/temporal/coordinator.py b/products/conversations/backend/temporal/coordinator.pyindex 1f9ba29db9ce..4a21f5b4df93 100644--- a/products/conversations/backend/temporal/coordinator.py+++ b/products/conversations/backend/temporal/coordinator.py@@ -190,13 +190,15 @@ def _collect_eligible(lookback_minutes: int = TICKET_LOOKBACK_MINUTES) -> list[Ereturn []# One comment query per team instead of round-trips per ticket. Two things come out of it:- # 1. Dedupe: a ticket is "already engaged" iff it has any non-customer comment — our own AI- # 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.+ # 1. Dedupe: a ticket is "already engaged" iff it has our own AI note ("AI", private or not)+ # or a public human/team/workflow reply. A private note from a human or workflow is not a+ # reply to the customer, so it is ignored here. This lets automations that log new tickets+ # as private notes keep the AI draft. Engaged tickets 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.+ # engaged only by an engaging 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:@@ -209,12 +211,12 @@ def _collect_eligible(lookback_minutes: int = TICKET_LOOKBACK_MINUTES) -> list[Elatest_ai_msg: dict[str, datetime] = {}latest_human_msg: dict[str, datetime] = {}for team_id, ticket_ids in by_team.items():- for item_id, author_type, comment_created_at in Comment.objects.filter(+ for item_id, author_type, is_private, comment_created_at in Comment.objects.filter(team_id=team_id,scope="conversations_ticket",item_id__in=ticket_ids,item_context__automated_update_key__isnull=True,- ).values_list("item_id", "item_context__author_type", "created_at"):+ ).values_list("item_id", "item_context__author_type", "item_context__is_private", "created_at"):if author_type == "customer":prev = latest_customer_msg.get(item_id)if prev is None or comment_created_at > prev:@@ -223,6 +225,8 @@ def _collect_eligible(lookback_minutes: int = TICKET_LOOKBACK_MINUTES) -> list[Eif first is None or comment_created_at < first:first_customer_msg[item_id] = comment_created_atcontinue+ if author_type != "AI" and is_private is True:+ continueengaged.add(item_id)earliest = earliest_non_customer_msg.get(item_id)if earliest is None or comment_created_at < earliest:diff --git a/products/conversations/backend/temporal/tests/test_coordinator.py b/products/conversations/backend/temporal/tests/test_coordinator.pyindex 5c2d6657a405..95b559dd0964 100644--- a/products/conversations/backend/temporal/tests/test_coordinator.py+++ b/products/conversations/backend/temporal/tests/test_coordinator.py@@ -143,13 +143,13 @@ class TestCollectEligible:mock_ticket_model.objects.filter.return_value.select_related.return_value = [ticket]mock_has_ready.return_value = has_ready- # One query per team: .filter().values_list("item_id", author_type, created_at) rows.+ # One query per team: .filter().values_list("item_id", author_type, is_private, created_at) rows.now = timezone.now()- rows: list[tuple[str, str, object]] = [(ticket_id, "customer", now - timedelta(minutes=10))]+ rows: list[tuple[str, str, bool, object]] = [(ticket_id, "customer", False, now - timedelta(minutes=10))]if has_ai:- rows.append((ticket_id, "AI", now))+ rows.append((ticket_id, "AI", True, now))if has_team_reply:- rows.append((ticket_id, "support", now))+ rows.append((ticket_id, "support", False, now))mock_comment_model.objects.filter.return_value.values_list.return_value = rowsresult = _collect_eligible()@@ -224,8 +224,8 @@ class TestCollectEligible:# AI note exists → engaged → skipped.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),+ (ticket_id, "customer", False, now - timedelta(minutes=10)),+ (ticket_id, "AI", True, now),]result = _collect_eligible()@@ -262,9 +262,9 @@ class TestCollectEligible:ticket, ticket_id, team = _make_ticket(created_minutes_ago=created_min_ago)mock_ticket_model.objects.filter.return_value.select_related.return_value = [ticket]- rows: list[tuple[str, str, object]] = []+ rows: list[tuple[str, str, bool, object]] = []if last_msg_min_ago is not None:- rows.append((ticket_id, "customer", timezone.now() - timedelta(minutes=last_msg_min_ago)))+ rows.append((ticket_id, "customer", False, timezone.now() - timedelta(minutes=last_msg_min_ago)))mock_comment_model.objects.filter.return_value.values_list.return_value = rowsresult = _collect_eligible()@@ -410,7 +410,16 @@ class TestCollectEligibleAwaitingClarification:ticket.refresh_from_db()return team, ticket- def _comment(self, team, ticket, *, author_type: str, minutes_ago: int, automated_update_key: str | None = None):+ def _comment(+ self,+ team,+ ticket,+ *,+ author_type: str,+ minutes_ago: int,+ automated_update_key: str | None = None,+ is_private: bool = False,+ ):from posthog.models.comment import Commentcreated = timezone.now() - timedelta(minutes=minutes_ago)@@ -421,7 +430,7 @@ class TestCollectEligibleAwaitingClarification:content=author_type,item_context={"author_type": author_type,- "is_private": False,+ "is_private": is_private,"persist_as": "clarification",**({"automated_update_key": automated_update_key} if automated_update_key else {}),},@@ -433,6 +442,10 @@ class TestCollectEligibleAwaitingClarification:ticket.refresh_from_db()return comment+ def _comments(self, team, ticket, comments: list[tuple[str, int] | tuple[str, int, bool]]) -> None:+ for author_type, minutes_ago, *private in comments:+ self._comment(team, ticket, author_type=author_type, minutes_ago=minutes_ago, is_private=private == [True])+@pytest.mark.django_db@patch(f"{COORD_MODULE}._is_master_flag_enabled", return_value=True)def test_automated_pr_update_does_not_answer_a_customer_followup(self, mock_master_flag):@@ -475,6 +488,13 @@ class TestCollectEligibleAwaitingClarification:False,None,),+ (+ "private_workflow_note_after_question",+ True,+ [("customer", 10), ("AI", 5), ("workflow", 4, True), ("customer", 3)],+ True,+ 1,+ ),])@pytest.mark.django_db@@ -489,8 +509,7 @@ class TestCollectEligibleAwaitingClarification:mock_master_flag,):team, ticket = self._team_and_ticket(awaiting=awaiting)- for author_type, minutes_ago in comments:- self._comment(team, ticket, author_type=author_type, minutes_ago=minutes_ago)+ self._comments(team, ticket, comments)result = _collect_eligible()if expected_eligible:@@ -512,6 +531,9 @@ class TestCollectEligibleAwaitingClarification:),("ai_note_after_customer", [("human", 4), ("customer", 3), ("AI", 2)], False, None),("inbound_team_reply", [("customer", 4), ("human", 3)], False, "skipped_team_replied"),+ ("private_workflow_note", [("customer", 4), ("workflow", 3, True)], True, None),+ ("private_support_note", [("customer", 4), ("support", 3, True)], True, None),+ ("private_ai_note", [("customer", 4), ("AI", 3, True)], False, None),])@pytest.mark.django_db@@ -525,8 +547,7 @@ class TestCollectEligibleAwaitingClarification: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)+ self._comments(team, ticket, comments)result = _collect_eligible()assert [t.ticket_id for t in result] == ([str(ticket.id)] if expected_eligible else [])