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.

feat(conversations): expose resolved-ticket evidence to business know… (#99425)

Alex V8abd81de0cbf
diff --git a/products/conversations/backend/apps.py b/products/conversations/backend/apps.py
index d3807256b97f..92d4052830cc 100644
--- a/products/conversations/backend/apps.py
+++ b/products/conversations/backend/apps.py
@@ -7,5 +7,8 @@ class ConversationsConfig(AppConfig):
label = "conversations"
verbose_name = "Support"
- def ready(self):
+ def ready(self) -> None:
from . import signals # noqa: F401
+ from .learning_provider import register_conversations_learning_provider
+
+ register_conversations_learning_provider()
diff --git a/products/conversations/backend/facade/api.py b/products/conversations/backend/facade/api.py
index fe6c9d04e39b..9c302caf33de 100644
--- a/products/conversations/backend/facade/api.py
+++ b/products/conversations/backend/facade/api.py
@@ -9,10 +9,12 @@ team's Slack credentials directly.
import asyncio
from datetime import datetime
from typing import Any, Protocol, cast
+from uuid import UUID
from django.conf import settings
from django.db import transaction
-from django.db.models import F, OuterRef, Prefetch, Q, QuerySet, Subquery
+from django.db.models import CharField, Exists, F, OuterRef, Prefetch, Q, QuerySet, Subquery
+from django.db.models.functions import Cast
import structlog
from slack_sdk.errors import SlackApiError
@@ -36,6 +38,8 @@ from products.conversations.backend.facade.types import (
EmailThreadAddress as EmailThreadAddress,
EmailThreadForAccountMatching as EmailThreadForAccountMatching,
EmailThreadParticipantSummary as EmailThreadParticipantSummary,
+ PublicHumanReplies as PublicHumanReplies,
+ ResolvedTicketRevision as ResolvedTicketRevision,
SupportChannel as SupportChannel,
SupportTicketMessage as SupportTicketMessage,
TicketSummary as TicketSummary,
@@ -47,8 +51,10 @@ from products.conversations.backend.models import (
EmailThreadMessage,
EmailThreadParticipant,
EmailThreadParticipantKind,
+ Status,
Ticket,
)
+from products.conversations.backend.services.messages import public_human_ticket_replies
from products.conversations.backend.slack import get_slack_client
from products.conversations.backend.support_slack import get_support_slack_bot_token
from products.conversations.backend.support_slack_channels import (
@@ -473,6 +479,107 @@ def list_account_ticket_messages(
return messages, count
+def _comment_team_ids(team: Team) -> set[int]:
+ # Comments are RootTeamMixin, so save() stores them on the parent. Tickets stay on the environment.
+ team_ids = {team.id}
+ if team.parent_team_id:
+ team_ids.add(team.parent_team_id)
+ return team_ids
+
+
+def _support_learning_team(team_id: int) -> Team | None:
+ team = Team.objects.filter(id=team_id).only("id", "conversations_enabled", "parent_team_id").first()
+ if team is None or team.conversations_enabled is not True:
+ return None
+ return team
+
+
+def list_resolved_ticket_revisions(
+ team_id: int,
+ *,
+ since: datetime,
+ limit: int,
+) -> list[ResolvedTicketRevision]:
+ if limit <= 0:
+ return []
+ team = _support_learning_team(team_id)
+ if team is None:
+ return []
+
+ comment_team_ids = _comment_team_ids(team)
+ has_public_human_reply = public_human_ticket_replies(comment_team_ids).filter(
+ item_id=Cast(OuterRef("id"), output_field=CharField()),
+ )
+ tickets = list(
+ Ticket.objects.filter(
+ team_id=team.id,
+ status=Status.RESOLVED,
+ )
+ .filter(Q(updated_at__gte=since) | Q(last_message_at__gte=since))
+ .filter(Exists(has_public_human_reply))
+ .order_by("-updated_at", "-id")[:limit]
+ )
+ if not tickets:
+ return []
+
+ latest_by_item = {
+ comment.item_id: comment
+ for comment in public_human_ticket_replies(comment_team_ids, [str(ticket.id) for ticket in tickets])
+ .order_by("item_id", "-created_at", "-id")
+ .distinct("item_id")
+ .only("id", "item_id")
+ }
+ revisions: list[ResolvedTicketRevision] = []
+ for ticket in tickets:
+ resolution_comment = latest_by_item.get(str(ticket.id))
+ if resolution_comment is None:
+ continue
+ revisions.append(
+ ResolvedTicketRevision(
+ ticket_id=ticket.id,
+ ticket_number=ticket.ticket_number,
+ resolution_comment_id=resolution_comment.id,
+ source_team_id=ticket.team_id,
+ display_label=f"ticket #{ticket.ticket_number}",
+ deep_link=f"{settings.SITE_URL}/project/{ticket.team_id}/support/tickets/{ticket.ticket_number}",
+ )
+ )
+ return revisions
+
+
+def get_public_human_replies(
+ team_id: int,
+ ticket_id: UUID,
+ *,
+ resolution_comment_id: UUID | None = None,
+) -> PublicHumanReplies | None:
+ team = _support_learning_team(team_id)
+ if team is None:
+ return None
+ if not Ticket.objects.filter(team_id=team.id, id=ticket_id).exists():
+ return None
+
+ replies: list[str] = []
+ found_resolution = resolution_comment_id is None
+ comments = (
+ public_human_ticket_replies(_comment_team_ids(team), [str(ticket_id)])
+ .order_by("created_at", "id")
+ .only("id", "content")
+ )
+ for comment in comments:
+ content = comment.content or ""
+ if not content.strip():
+ continue
+ replies.append(content)
+ if resolution_comment_id is not None and comment.id == resolution_comment_id:
+ # A later public human reply is a new revision under a new evidence key.
+ found_resolution = True
+ break
+ if not found_resolution or not replies:
+ return None
+ return PublicHumanReplies(replies=tuple(replies))
+
+
def resolve_group_keys_by_email(
team_id: int,
emails: list[str],
diff --git a/products/conversations/backend/facade/types.py b/products/conversations/backend/facade/types.py
index 6c8a2844a3f6..34a4833c4015 100644
--- a/products/conversations/backend/facade/types.py
+++ b/products/conversations/backend/facade/types.py
@@ -8,6 +8,7 @@ yet. Rename it once the product meets the strict structure rules.
"""
from datetime import datetime
+from uuid import UUID
from pydantic.dataclasses import dataclass
@@ -46,6 +47,21 @@ class SupportTicketMessage:
created_at: datetime
+@dataclass(frozen=True, kw_only=True)
+class ResolvedTicketRevision:
+ ticket_id: UUID
+ ticket_number: int
+ resolution_comment_id: UUID
+ source_team_id: int
+ display_label: str
+ deep_link: str
+
+
+@dataclass(frozen=True, kw_only=True)
+class PublicHumanReplies:
+ replies: tuple[str, ...]
+
+
@dataclass(frozen=True)
class TicketSummary:
"""A support ticket, reduced to what an account's tickets list renders."""
diff --git a/products/conversations/backend/learning_provider.py b/products/conversations/backend/learning_provider.py
new file mode 100644
index 000000000000..4a591e1abf35
--- /dev/null
+++ b/products/conversations/backend/learning_provider.py
@@ -0,0 +1,50 @@
+from __future__ import annotations
+
+from datetime import datetime
+
+from products.business_knowledge.backend.learning.contracts import EvidenceBundle, EvidenceRef, evidence_key_for
+from products.business_knowledge.backend.learning.providers import get_learning_provider, register_learning_provider
+from products.business_knowledge.backend.models.constants import LearningProvider
+
+
+class ConversationsLearningProvider:
+ name: str = LearningProvider.CONVERSATIONS
+
+ def collect(self, team_id: int, *, since: datetime, limit: int) -> list[EvidenceRef]:
+ from products.conversations.backend.facade.api import ( # noqa: PLC0415 — keeps temporalio off the startup path
+ list_resolved_ticket_revisions,
+ )
+
+ return [
+ EvidenceRef(
+ evidence_key=evidence_key_for(revision.ticket_id, revision.resolution_comment_id),
+ source_team_id=revision.source_team_id,
+ display_label=revision.display_label,
+ deep_link=revision.deep_link,
+ provider=self.name,
+ ticket_id=revision.ticket_id,
+ ticket_number=revision.ticket_number,
+ resolution_comment_id=revision.resolution_comment_id,
+ )
+ for revision in list_resolved_ticket_revisions(team_id, since=since, limit=limit)
+ ]
+
+ def load(self, ref: EvidenceRef) -> EvidenceBundle | None:
+ from products.conversations.backend.facade.api import ( # noqa: PLC0415 — keeps temporalio off the startup path
+ get_public_human_replies,
+ )
+
+ replies = get_public_human_replies(
+ ref.source_team_id,
+ ref.ticket_id,
+ resolution_comment_id=ref.resolution_comment_id,
+ )
+ if replies is None:
+ return None
+ return EvidenceBundle(replies=replies.replies)
+
+
+def register_conversations_learning_provider() -> None:
+ if get_learning_provider(LearningProvider.CONVERSATIONS) is not None:
+ return
+ register_learning_provider(ConversationsLearningProvider())
diff --git a/products/conversations/backend/services/messages.py b/products/conversations/backend/services/messages.py
index ad9f4c5b082b..03fdf6192bd1 100644
--- a/products/conversations/backend/services/messages.py
+++ b/products/conversations/backend/services/messages.py
@@ -5,6 +5,15 @@ from posthog.models.comment import Comment
# Ticket messages are stored as comments under this scope.
TICKET_MESSAGE_SCOPE = "conversations_ticket"
+# "team" is analytics-only. "AI" and "customer" are not learning evidence.
+PUBLIC_HUMAN_AUTHOR_TYPES = ("support", "human")
+
+
+def _public_ticket_message_context() -> Q:
+ # isnull keeps comments with no is_private key, because ~Q alone drops SQL NULL.
+ # signals._is_private_message treats anything but boolean True as public.
+ return ~Q(item_context__is_private=True) | Q(item_context__is_private__isnull=True)
+
def visible_ticket_messages(team_id: int, ticket_id: str) -> QuerySet[Comment]:
"""
@@ -12,11 +21,6 @@ def visible_ticket_messages(team_id: int, ticket_id: str) -> QuerySet[Comment]:
private team note. Getting this predicate wrong shows a private team note to a customer,
so callers should reuse it rather than write their own.
- The isnull arm keeps comments whose item_context has no is_private key, because `~Q` alone
- drops them when the JSONB lookup returns SQL NULL rather than false. That matches the
- identity check in signals._is_private_message, which treats anything but boolean True as
- public.
-
widget.py keeps its own copy of the predicate. It filters on a Team object, which
RootTeamQuerySet does not rewrite, where this helper filters on an id, which it does.
The two agree while no team sets parent_team. If environments ship they diverge, and this
@@ -27,4 +31,23 @@ def visible_ticket_messages(team_id: int, ticket_id: str) -> QuerySet[Comment]:
scope=TICKET_MESSAGE_SCOPE,
item_id=ticket_id,
deleted=False,
- ).filter(~Q(item_context__is_private=True) | Q(item_context__is_private__isnull=True))
+ ).filter(_public_ticket_message_context())
+
+
+def public_human_ticket_replies(
+ comment_team_ids: set[int],
+ ticket_ids: list[str] | None = None,
+) -> QuerySet[Comment]:
+ qs = (
+ Comment.objects.filter(
+ team_id__in=comment_team_ids,
+ scope=TICKET_MESSAGE_SCOPE,
+ deleted=False,
+ item_context__author_type__in=PUBLIC_HUMAN_AUTHOR_TYPES,
+ )
+ .filter(_public_ticket_message_context())
+ .filter(content__regex=r"\S")
+ )
+ if ticket_ids is not None:
+ qs = qs.filter(item_id__in=ticket_ids)
+ return qs
diff --git a/products/conversations/backend/tests/test_facade.py b/products/conversations/backend/tests/test_facade.py
index bd7e6959b6b1..38a5ceccf5c4 100644
--- a/products/conversations/backend/tests/test_facade.py
+++ b/products/conversations/backend/tests/test_facade.py
@@ -1,8 +1,10 @@
from datetime import UTC, datetime, timedelta
+from uuid import uuid4
from posthog.test.base import BaseTest
from unittest.mock import MagicMock, patch
+from django.conf import settings
from django.utils import timezone
from parameterized import parameterized
@@ -14,11 +16,14 @@ from posthog.models.comment import Comment
from products.conversations.backend.channel_summary_ids import build_channel_summary_workflow_id
from products.conversations.backend.facade.api import (
SupportMessageSendError,
+ get_public_human_replies,
list_account_ticket_messages,
list_account_tickets,
+ list_resolved_ticket_revisions,
post_support_message,
trigger_immediate_channel_summary,
)
+from products.conversations.backend.models.constants import Status
from products.conversations.backend.models.ticket import Ticket
CLIENT = "products.conversations.backend.facade.api.get_slack_client"
@@ -328,3 +333,223 @@ class TestTriggerImmediateChannelSummary(BaseTest):
account_id=ACCOUNT_ID, cadence="daily", period_start=PERIOD_START.date()
)
assert client.start_workflow.call_args.args[1].slack_channel_id == "C123"
+
+
+LOOKBACK = timedelta(days=7)
+
+
+class TestResolvedTicketEvidence(BaseTest):
+ def setUp(self) -> None:
+ super().setUp()
+ self.team.conversations_enabled = True
+ self.team.save(update_fields=["conversations_enabled"])
+ self.since = timezone.now() - LOOKBACK
+
+ def _ticket(self, *, team: Team | None = None, status: str = Status.RESOLVED, number: int = 1) -> Ticket:
+ team = team or self.team
+ return Ticket.objects.create(
+ team=team,
+ ticket_number=number,
+ widget_session_id=f"s{number}-{team.id}",
+ distinct_id=f"d{number}-{team.id}",
+ status=status,
+ )
+
+ def _comment(
+ self,
+ ticket: Ticket,
+ *,
+ author_type: str,
+ content: str,
+ is_private: bool | None = False,
+ deleted: bool = False,
+ extra_context: dict[str, str] | None = None,
+ ) -> Comment:
+ item_context: dict = {"author_type": author_type}
+ if is_private is not None:
+ item_context["is_private"] = is_private
+ if extra_context:
+ item_context.update(extra_context)
+ return Comment.objects.create(
+ team=ticket.team,
+ scope="conversations_ticket",
+ item_id=str(ticket.id),
+ content=content,
+ deleted=deleted,
+ item_context=item_context,
+ )
+
+ def _revisions(self, *, team: Team | None = None, limit: int = 10):
+ return list_resolved_ticket_revisions((team or self.team).id, since=self.since, limit=limit)
+
+ def test_returns_only_public_human_reply_text(self) -> None:
+ ticket = self._ticket()
+ self._comment(
+ ticket,
+ author_type="customer",
+ content="Customer asked about the rate limit",
+ extra_context={"author_name": "Ada", "author_email": "[email protected]"},
+ )
+ human = self._comment(
+ ticket,
+ author_type="support",
+ content="The rate limit is 1000 events per hour",
+ extra_context={"author_name": "Support agent", "author_email": "[email protected]"},
+ )
+ self._comment(ticket, author_type="support", content="Private diagnosis", is_private=True)
+ self._comment(ticket, author_type="AI", content="AI suggested a reply")
+ self._comment(ticket, author_type="team", content="Team analytics author type")
+
+ revisions = self._revisions()
+ replies = get_public_human_replies(self.team.id, ticket.id)
+
+ assert [revision.ticket_id for revision in revisions] == [ticket.id]
+ assert revisions[0].resolution_comment_id == human.id
+ assert replies is not None
+ assert replies.replies == ("The rate limit is 1000 events per hour",)
+
+ @parameterized.expand(
+ [
+ ("customer", "customer", False, False, "Customer said this"),
+ ("ai", "AI", False, False, "AI drafted this"),
+ ("team", "team", False, False, "Team analytics type"),
+ ("private_support", "support", True, False, "Internal note"),
+ ("deleted_support", "support", False, True, "Retracted reply"),
+ ("empty_content", "support", False, False, ""),
+ ("whitespace_only", "support", False, False, " "),
+ ]
+ )
+ def test_collect_skips_tickets_without_a_public_human_reply(
+ self,
+ _name: str,
+ author_type: str,
+ is_private: bool,
+ deleted: bool,
+ content: str,
+ ) -> None:
+ ticket = self._ticket()
+ self._comment(
+ ticket,
+ author_type=author_type,
+ content=content,
+ is_private=is_private,
+ deleted=deleted,
+ )
+
+ assert self._revisions() == []
+ assert get_public_human_replies(self.team.id, ticket.id) is None
+
+ @parameterized.expand(
+ [
+ ("support", "support", False),
+ ("human", "human", False),
+ ("missing_is_private_key", "support", None),
+ ]
+ )
+ def test_collect_includes_public_human_author_types(
+ self,
+ _name: str,
+ author_type: str,
+ is_private: bool | None,
+ ) -> None:
+ ticket = self._ticket()
+ comment = self._comment(ticket, author_type=author_type, content="Reusable answer", is_private=is_private)
+
+ revisions = self._revisions()
+ replies = get_public_human_replies(self.team.id, ticket.id)
+
+ assert [revision.resolution_comment_id for revision in revisions] == [comment.id]
+ assert replies is not None
+ assert replies.replies == ("Reusable answer",)
+
+ def test_revision_follows_the_latest_public_human_comment(self) -> None:
+ ticket = self._ticket()
+ first = self._comment(ticket, author_type="support", content="First answer")
+ later = self._comment(ticket, author_type="human", content="Follow-up answer")
+ self._comment(ticket, author_type="AI", content="Later AI note")
+ self._comment(ticket, author_type="support", content="Deleted later", deleted=True)
+ now = timezone.now()
+ Comment.objects.filter(pk=first.id).update(created_at=now - timedelta(minutes=2))
+ Comment.objects.filter(pk=later.id).update(created_at=now - timedelta(minutes=1))
+
+ revisions = self._revisions()
+ replies = get_public_human_replies(self.team.id, ticket.id)
+ pinned = get_public_human_replies(self.team.id, ticket.id, resolution_comment_id=first.id)
+
+ assert [revision.resolution_comment_id for revision in revisions] == [later.id]
+ assert replies is not None
+ assert replies.replies == ("First answer", "Follow-up answer")
+ assert pinned is not None
+ assert pinned.replies == ("First answer",)
+ assert get_public_human_replies(self.team.id, ticket.id, resolution_comment_id=uuid4()) is None
+
+ def test_open_ticket_is_not_collected(self) -> None:
+ ticket = self._ticket(status=Status.OPEN)
+ self._comment(ticket, author_type="support", content="Still working on it")
+
+ assert self._revisions() == []
+ replies = get_public_human_replies(self.team.id, ticket.id)
+ assert replies is not None
+ assert replies.replies == ("Still working on it",)
+
+ def test_conversations_disabled_returns_nothing(self) -> None:
+ ticket = self._ticket()
+ self._comment(ticket, author_type="support", content="Reusable answer")
+ self.team.conversations_enabled = False
+ self.team.save(update_fields=["conversations_enabled"])
+
+ assert self._revisions() == []
+ assert get_public_human_replies(self.team.id, ticket.id) is None
+
+ def test_does_not_return_other_teams_tickets(self) -> None:
+ other = Team.objects.create_with_data(organization=self.organization, initiating_user=self.user)
+ other.conversations_enabled = True
+ other.save(update_fields=["conversations_enabled"])
+ other_ticket = self._ticket(team=other, number=1)
+ self._comment(other_ticket, author_type="support", content="Other team answer")
+
+ assert self._revisions() == []
+ assert get_public_human_replies(self.team.id, other_ticket.id) is None
+
+ def test_child_environment_reads_comments_stored_on_the_parent(self) -> None:
+ child = Team.objects.create(
+ organization=self.organization,
+ project=self.project,
+ parent_team=self.team,
+ name="Child environment",
+ conversations_enabled=True,
+ )
+ ticket = self._ticket(team=child)
+ comment = self._comment(ticket, author_type="support", content="Answer from the child environment")
+
+ revisions = self._revisions(team=child)
+ replies = get_public_human_replies(child.id, ticket.id)
+
+ assert comment.team_id == self.team.id
+ assert [revision.source_team_id for revision in revisions] == [child.id]
+ assert revisions[0].resolution_comment_id == comment.id
+ assert self._revisions() == []
+ assert replies is not None
+ assert replies.replies == ("Answer from the child environment",)
+
+ def test_lookback_excludes_stale_resolved_tickets(self) -> None:
+ ticket = self._ticket()
+ self._comment(ticket, author_type="support", content="Old answer")
+ stale = timezone.now() - timedelta(days=8)
+ Ticket.objects.filter(pk=ticket.id).update(updated_at=stale, last_message_at=stale)
+
+ assert self._revisions() == []
+
+ def test_limit_returns_the_most_recently_updated_ticket(self) -> None:
+ older = self._ticket(number=1)
+ newer = self._ticket(number=2)
+ self._comment(older, author_type="support", content="Older answer")
+ self._comment(newer, author_type="support", content="Newer answer")
+ Ticket.objects.filter(pk=older.id).update(updated_at=timezone.now() - timedelta(hours=2))
+ Ticket.objects.filter(pk=newer.id).update(updated_at=timezone.now() - timedelta(hours=1))
+
+ revisions = self._revisions(limit=1)
+
+ assert [revision.ticket_id for revision in revisions] == [newer.id]
+ assert revisions[0].display_label == "ticket #2"
+ assert revisions[0].deep_link == f"{settings.SITE_URL}/project/{self.team.id}/support/tickets/2"
diff --git a/products/conversations/backend/tests/test_learning_provider.py b/products/conversations/backend/tests/test_learning_provider.py
new file mode 100644
index 000000000000..af311a11df5a
--- /dev/null
+++ b/products/conversations/backend/tests/test_learning_provider.py
@@ -0,0 +1,130 @@
+from datetime import timedelta
+
+from posthog.test.base import BaseTest
+
+from django.test import SimpleTestCase
+from django.utils import timezone
+
+from posthog.models import Team
+from posthog.models.comment import Comment
+
+from products.business_knowledge.backend.learning import providers as providers_mod
+from products.business_knowledge.backend.learning.contracts import evidence_key_for
+from products.business_knowledge.backend.learning.providers import get_learning_provider, get_learning_providers
+from products.conversations.backend.learning_provider import (
+ ConversationsLearningProvider,
+ register_conversations_learning_provider,
+)
+from products.conversations.backend.models.constants import Status
+from products.conversations.backend.models.ticket import Ticket
+
+
+class TestConversationsLearningProviderRegistration(SimpleTestCase):
+ def setUp(self) -> None:
+ super().setUp()
+ self._saved = providers_mod._providers.copy()
+ providers_mod._providers.clear()
+
+ def tearDown(self) -> None:
+ providers_mod._providers.clear()
+ providers_mod._providers.update(self._saved)
+ super().tearDown()
+
+ def test_ready_can_register_twice_without_raising(self) -> None:
+ register_conversations_learning_provider()
+ register_conversations_learning_provider()
+
+ provider = get_learning_provider("conversations")
+ assert provider is not None
+ assert provider.name == "conversations"
+ assert len(get_learning_providers()) == 1
+
+
+class TestConversationsLearningProvider(BaseTest):
+ def setUp(self) -> None:
+ super().setUp()
+ self.team.conversations_enabled = True
+ self.team.save(update_fields=["conversations_enabled"])
+ self.provider = ConversationsLearningProvider()
+ self.since = timezone.now() - timedelta(days=7)
+
+ def _ticket(self, *, team: Team | None = None, number: int = 1) -> Ticket:
+ team = team or self.team
+ return Ticket.objects.create(
+ team=team,
+ ticket_number=number,
+ widget_session_id=f"learn-{number}-{team.id}",
+ distinct_id=f"learn-d{number}-{team.id}",
+ status=Status.RESOLVED,
+ )
+
+ def _support_reply(self, ticket: Ticket, content: str) -> Comment:
+ return Comment.objects.create(
+ team=ticket.team,
+ scope="conversations_ticket",
+ item_id=str(ticket.id),
+ content=content,
+ item_context={"author_type": "support", "is_private": False, "author_email": "[email protected]"},
+ )
+
+ def test_collect_maps_the_latest_public_human_comment_to_an_evidence_ref(self) -> None:
+ ticket = self._ticket()
+ comment = self._support_reply(ticket, "The limit is 1000")
+
+ refs = self.provider.collect(self.team.id, since=self.since, limit=10)
+ bundle = self.provider.load(refs[0])
+
+ assert len(refs) == 1
+ assert refs[0].provider == "conversations"
+ assert refs[0].ticket_id == ticket.id
+ assert refs[0].ticket_number == 1
+ assert refs[0].source_team_id == self.team.id
+ assert refs[0].resolution_comment_id == comment.id
+ assert refs[0].evidence_key == evidence_key_for(ticket.id, comment.id)
+ assert bundle is not None
+ assert bundle.replies == ("The limit is 1000",)
+
+ def test_load_pins_replies_to_the_collected_revision(self) -> None:
+ ticket = self._ticket()
+ first = self._support_reply(ticket, "The limit is 1000")
+
+ refs = self.provider.collect(self.team.id, since=self.since, limit=10)
+ self._support_reply(ticket, "Also the burst is 5000")
+ bundle = self.provider.load(refs[0])
+ later_refs = self.provider.collect(self.team.id, since=self.since, limit=10)
+
+ assert bundle is not None
+ assert bundle.replies == ("The limit is 1000",)
+ assert later_refs[0].resolution_comment_id != first.id
+ later_bundle = self.provider.load(later_refs[0])
+ assert later_bundle is not None
+ assert later_bundle.replies == ("The limit is 1000", "Also the burst is 5000")
+
+ def test_load_rejects_a_resolution_comment_that_is_no_longer_public(self) -> None:
+ ticket = self._ticket()
+ comment = self._support_reply(ticket, "The limit is 1000")
+
+ refs = self.provider.collect(self.team.id, since=self.since, limit=10)
+ Comment.objects.filter(pk=comment.id).update(deleted=True)
+
+ assert self.provider.load(refs[0]) is None
+
+ def test_child_environment_keeps_source_team_id_on_the_child(self) -> None:
+ child = Team.objects.create(
+ organization=self.organization,
+ project=self.project,
+ parent_team=self.team,
+ name="Child environment",
+ conversations_enabled=True,
+ )
+ ticket = self._ticket(team=child)
+ comment = self._support_reply(ticket, "Child environment answer")
+
+ refs = self.provider.collect(child.id, since=self.since, limit=10)
+ bundle = self.provider.load(refs[0])
+
+ assert [ref.source_team_id for ref in refs] == [child.id]
+ assert refs[0].evidence_key == evidence_key_for(ticket.id, comment.id)
+ assert self.provider.collect(self.team.id, since=self.since, limit=10) == []
+ assert bundle is not None
+ assert bundle.replies == ("Child environment answer",)
diff --git a/tach.toml b/tach.toml
index 40b6c75cfb75..4d208694920b 100644
--- a/tach.toml
+++ b/tach.toml
@@ -1314,6 +1314,9 @@ expose = [
"backend\\.constants.*",
"backend\\.models.*",
"backend\\.temporal.*",
+ # Other products register LearningEvidenceProvider here. BK never imports them.
+ "backend\\.learning\\.contracts.*",
+ "backend\\.learning\\.providers.*",
]
from = [
"products.business_knowledge",