: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
feat(slack): show one session link in the reply footer (#108159)
Vojta Bartoše63ef0224a85
diff --git a/products/slack_app/backend/services/slack_messages.py b/products/slack_app/backend/services/slack_messages.pyindex 261a04188db8..810c5814a5a9 100644--- a/products/slack_app/backend/services/slack_messages.py+++ b/products/slack_app/backend/services/slack_messages.py@@ -676,7 +676,6 @@ class RunFooter:"""task_url: str | None = None- desktop_url: str | None = Nonemodel: str | None = Nonereasoning_effort: str | None = Nonerun_id: str | None = None@@ -693,7 +692,7 @@ class RunFooter:keeps meaning "None-coalesce" and cannot silently discard a partial instance.The ids are not part of the answer — they say nothing on their own."""- return any((self.task_url, self.desktop_url, self.model, self.project))+ return any((self.task_url, self.model, self.project))def _project_name(team_id: int, *, integration_id: int | None, created_by_id: int | None) -> str | None:@@ -746,9 +745,8 @@ def load_run_footer(run_id: str | UUID | None, *, integration_id: int | None) ->Never raises: the footer is the last thing added to an answer that is alreadywritten, so failing to describe the run must not cost the reader the answer.- Describes the run in full, links included. Whether the reader gets the desktop link- is ``viewer_has_code_access``'s question, asked where the reader is known; the web- link is for everyone, since the task page enforces access itself.+ The task link is for everyone, since the task page enforces access itself. It is the+ only link: the task page carries its own link into PostHog Desktop.``integration_id`` is the install the reply is posted through, and the workspacebehind it is what the project segment is judged against. Required rather than@@ -770,11 +768,6 @@ def load_run_footer(run_id: str | UUID | None, *, integration_id: int | None) ->run_id=str(run.id),task_id=str(run.task_id),task_url=_task_url(run.team_id, run.task_id, run.id),- # The web bridge page, not the raw `posthog-code://` scheme: it redirects into the- # desktop app when installed and offers a download when not, so a reader without- # the app lands somewhere useful instead of a dead link. It also picks the right- # scheme (prod vs dev) client-side, which a server-minted scheme link can't.- desktop_url=_desktop_bridge_url(run.task_id),model=state.model,reasoning_effort=state.reasoning_effort,project=_project_name(@@ -789,19 +782,19 @@ def load_run_footer(run_id: str | UUID | None, *, integration_id: int | None) ->def _run_context_segments(footer: RunFooter) -> list[str]:- """What the run is, as footer segments: the project it answered from, then the model.+ """What the run is, as footer segments: the model, then the project it answered from.Shared by the reply footer and the progress message so the two cannot describe onerun differently, and so the escaping below has a single home."""segments: list[str] = []+ if footer.model:+ segments.append(describe_run_model(footer.model, footer.reasoning_effort))if footer.project:# A project name is tenant text in a `mrkdwn` block, so `<!channel>` broadcasts# and `<url|label>` renders a link the reader reads as the bot's. Escaped here# rather than on the way in, so `RunFooter.project` stays the plain name.segments.append(f"Project: *{escape_slack_mrkdwn(footer.project)}*")- if footer.model:- segments.append(describe_run_model(footer.model, footer.reasoning_effort))return segments@@ -825,9 +818,7 @@ def reply_footer_block(footer: RunFooter, configure_url: str | None = None) -> d"""segments: list[str] = []if footer.task_url:- segments.append(f"<{footer.task_url}|View on web>")- if footer.desktop_url:- segments.append(f"<{footer.desktop_url}|View on desktop>")+ segments.append(f"<{footer.task_url}|View session>")segments.extend(_run_context_segments(footer))if configure_url:segments.append(f"<{configure_url}|Configure>")@@ -992,13 +983,6 @@ def _task_url(team_id: int, task_id: UUID, run_id: UUID) -> str:return _public_url(f"/project/{team_id}/tasks/{task_id}?runId={run_id}&{UNFURL_OPT_OUT_PARAM}=false")-def _desktop_bridge_url(task_id: UUID) -> str:- # `/code/task/<id>` is the public bridge scene (see `CodeTaskLink`), not the desktop- # app's own route. `unfurl=false` keeps our unfurler off it — the footer already names- # the run right beside the link.- return _public_url(f"/code/task/{task_id}?{UNFURL_OPT_OUT_PARAM}=false")--def _public_url(path: str) -> str:# Mirrors the Slack onboarding links: in local dev the tunnel is what makes a link# posted into Slack actually reachable.diff --git a/products/slack_app/backend/slack_thread.py b/products/slack_app/backend/slack_thread.pyindex 8ebdbd052043..25935c903df4 100644--- a/products/slack_app/backend/slack_thread.py+++ b/products/slack_app/backend/slack_thread.py@@ -27,7 +27,6 @@ from products.slack_app.backend.services.slack_messages import (run_context_block,slack_message_exists,turn_feedback_block,- viewer_has_code_access,)if TYPE_CHECKING:@@ -197,14 +196,13 @@ class SlackThreadHandler:# Beside the footer rather than in it: a trace id belongs to one turn, and the# next turn in the same thread has its own.self.turn_trace_id = turn_trace_id- # Who this reply is for. Links are gated on their access, not the task creator's:- # a thread outlives its opener, and a link only helps the person looking at it.+ # Who this reply is for, which can differ from the task creator because a thread+ # outlives its opener.self.actor_slack_user_id = actor_slack_user_id or context.mentioning_slack_user_idself._integration: Integration | None = Noneself._client: WebClient | None = Noneself._bot_user_id: str | None = Noneself._fork_flag: bool | None = None- self._code_access: bool | None = None@classmethoddef for_run(@@ -248,29 +246,6 @@ class SlackThreadHandler:self._client = SlackIntegration(integration).clientreturn self._client- def viewer_can_open_code_links(self) -> bool:- """Whether this reply's reader passes the PostHog Desktop access check. Memoized:- the cards ask for their buttons and the footer asks again for its desktop link."""- if self._code_access is None:- self._code_access = viewer_has_code_access(self._get_integration(), self.actor_slack_user_id)- return bool(self._code_access)-- def reader_footer(self) -> RunFooter:- """`run_footer` with the desktop link withheld where this reply's reader can't- open it.-- The web task link is never withheld: the task page enforces access itself, so at- worst it asks the reader to sign in. The one place that answers this, so a card's- buttons and the footer's links can't disagree about the same reader. A footer- carrying no desktop link asks nothing, which keeps a plain answer off the- identity lookup behind the access check.- """- if not self.run_footer.desktop_url:- return self.run_footer- if self.viewer_can_open_code_links():- return self.run_footer- return replace(self.run_footer, desktop_url=None)-def reader_task_url(self) -> str | None:"""The task page behind this reply, or `None` when the run has no task. Shown toevery reader; the page enforces access itself."""@@ -280,7 +255,7 @@ class SlackThreadHandler:"""This handler's footer, or `None` when there is nothing to describe."""if not self.run_footer.has_content():return None- footer = self.reader_footer()+ footer = self.run_footerif not include_task_url:footer = replace(footer, task_url=None)configure_url = app_home_url(self._get_integration())@@ -523,9 +498,7 @@ class SlackThreadHandler:The project and model ride along as a context line rather than their ownmessage: what a task is running on and against is a property of the task, and- the thread already has one place that describes it while it works. Unlike the- reply footer's links this is not gated on the reader — a running task says what- it is running on either way.+ the thread already has one place that describes it while it works."""text = f"*{PROGRESS_MESSAGE_MARKER}* :hourglass_flowing_sand:\nStage: {stage}"blocks: list[dict[str, Any]] = [diff --git a/products/slack_app/backend/tests/services/slack_messages/test_run_footer.py b/products/slack_app/backend/tests/services/slack_messages/test_run_footer.pyindex 678175b36f68..f79a8852c3ac 100644--- a/products/slack_app/backend/tests/services/slack_messages/test_run_footer.py+++ b/products/slack_app/backend/tests/services/slack_messages/test_run_footer.py@@ -19,7 +19,6 @@ from products.slack_app.backend.services.slack_messages import ()TASK_URL = "https://us.posthog.com/project/1/tasks/2?runId=3&unfurl=false"-DESKTOP_URL = "https://us.posthog.com/code/task/2?unfurl=false"class TestRunFooter(SimpleTestCase):@@ -27,25 +26,25 @@ class TestRunFooter(SimpleTestCase):[("everything",- RunFooter(TASK_URL, DESKTOP_URL, "claude-opus-5", "high"),+ RunFooter(TASK_URL, "claude-opus-5", "high", project="Fernwood staging"),"slack://app?team=T1&id=A1&tab=home",- f"<{TASK_URL}|View on web> · <{DESKTOP_URL}|View on desktop>"- " · *Claude Opus 5* [High] · <slack://app?team=T1&id=A1&tab=home|Configure>",+ f"<{TASK_URL}|View session> · *Claude Opus 5* [High]"+ " · Project: *Fernwood staging* · <slack://app?team=T1&id=A1&tab=home|Configure>",),(- "links_only",- RunFooter(TASK_URL, DESKTOP_URL),+ "link_only",+ RunFooter(TASK_URL),None,- f"<{TASK_URL}|View on web> · <{DESKTOP_URL}|View on desktop>",+ f"<{TASK_URL}|View session>",),("model_without_effort", RunFooter(model="claude-opus-5"), None, "*Claude Opus 5*"),# A workspace connected to several projects answers from one of them, and the# answer looks the same whichever it was, so the footer has to name it.(- "project_precedes_the_model",+ "model_precedes_the_project",RunFooter(model="claude-opus-5", project="Fernwood staging"),None,- "Project: *Fernwood staging* · *Claude Opus 5*",+ "*Claude Opus 5* · Project: *Fernwood staging*",),# A project name is tenant text with no character validation, and the footer# rides under every reply. Unescaped, `<!channel>` broadcasts to everyone in@@ -87,8 +86,7 @@ class TestRunFooter(SimpleTestCase):class TestLoadRunFooter(SimpleTestCase):@patch("products.tasks.backend.facade.run_config.parse_run_state")@patch("products.tasks.backend.facade.api.get_task_run")- def test_describes_the_run_links_included(self, mock_get_run, mock_parse) -> None:- # Whether the reader may open the links is asked later, where the reader is known.+ def test_describes_the_run(self, mock_get_run, mock_parse) -> None:task_id = uuid4()mock_get_run.return_value = SimpleNamespace(id=uuid4(), task_id=task_id, team_id=1, state={}, created_by_id=None@@ -97,7 +95,6 @@ class TestLoadRunFooter(SimpleTestCase):footer = load_run_footer("run-1", integration_id=None)- assert f"/code/task/{task_id}" in (footer.desktop_url or "")assert f"/tasks/{task_id}" in (footer.task_url or "")assert footer.model == "claude-opus-5"diff --git a/products/slack_app/backend/tests/test_slack_thread.py b/products/slack_app/backend/tests/test_slack_thread.pyindex ce8a317dfa64..3407f974a5d6 100644--- a/products/slack_app/backend/tests/test_slack_thread.py+++ b/products/slack_app/backend/tests/test_slack_thread.py@@ -299,7 +299,7 @@ class TestSlackThreadHandlerWithoutTaskUrl(SimpleTestCase):blocks = mock_client.chat_postMessage.call_args.kwargs["blocks"]context_text = next(b["elements"][0]["text"] for b in blocks if b["type"] == "context")- assert context_text == "Project: *Staging* · *Claude Opus 5* [High]"+ assert context_text == "*Claude Opus 5* [High] · Project: *Staging*"@patch.object(SlackThreadHandler, "delete_progress")@patch.object(SlackThreadHandler, "_get_client")@@ -427,36 +427,6 @@ class TestReplyFooterGate(SimpleTestCase):)return SlackThreadHandler(context, footer or RunFooter(model="claude-opus-5"))- @parameterized.expand([("withheld", False), ("granted", True)])- @patch.object(SlackThreadHandler, "_get_integration")- @patch.object(SlackThreadHandler, "_get_client")- def test_withholding_the_links_still_leaves_the_model_and_configure(- self,- _name: str,- code_access: bool,- mock_get_client,- mock_get_integration,- ) -> None:- # Desktop access changes only the desktop segment: the web link works for anyone- # with a PostHog login, and the model and the way to change it are theirs either way.- mock_client = MagicMock()- mock_get_client.return_value = mock_client- mock_get_integration.return_value = Integration(config={"app_id": "A1"}, integration_id="T1")- footer = RunFooter(- task_url="https://app/project/1/tasks/t",- desktop_url="https://us.posthog.com/code/task/t",- model="claude-opus-5",- )-- with patch.object(SlackThreadHandler, "viewer_can_open_code_links", return_value=code_access):- self._handler(footer).post_thread_message("the answer", with_footer=True)-- line = mock_client.chat_postMessage.call_args.kwargs["blocks"][-1]["elements"][0]["text"]- assert "*Claude Opus 5*" in line- assert "|Configure>" in line- assert "View on web" in line- assert ("View on desktop" in line) is code_access-@patch.object(SlackThreadHandler, "_get_integration")@patch.object(SlackThreadHandler, "_get_client")def test_streamed_reply_carries_the_footer(self, mock_get_client, mock_get_integration) -> None:diff --git a/products/tasks/backend/temporal/process_task/activities/post_slack_update.py b/products/tasks/backend/temporal/process_task/activities/post_slack_update.pyindex d7eb28fa7779..fe3e17dfe6c8 100644--- a/products/tasks/backend/temporal/process_task/activities/post_slack_update.py+++ b/products/tasks/backend/temporal/process_task/activities/post_slack_update.py@@ -103,7 +103,7 @@ def post_slack_update(input: PostSlackUpdateInput) -> None:try:context = SlackThreadContext.from_dict(input.slack_thread_context)handler = SlackThreadHandler.for_run(context, task_run.id)- # The buttons lead where the footer's links do, so they answer to the same reader.+ # The buttons lead where the footer's link does.task_url = handler.reader_task_url()pr_url = (task_run.output or {}).get("pr_url")diff --git a/products/tasks/backend/temporal/slack_relay/activities.py b/products/tasks/backend/temporal/slack_relay/activities.pyindex a357eebaab49..f35eac9315f0 100644--- a/products/tasks/backend/temporal/slack_relay/activities.py+++ b/products/tasks/backend/temporal/slack_relay/activities.py@@ -248,7 +248,7 @@ def relay_slack_message(input: RelaySlackMessageInput) -> None:context = SlackThreadContext.from_mapping(mapping, user_message_ts=input.user_message_ts)# Mention resolution, most precise first: the echoed message's recorded# sender, then the live/mapping actors for pre-rollout runs. Resolved before the- # handler so the footer's links are gated on whoever this reply is actually for.+ # handler so the handler knows whoever this reply is actually for.mention_from_message = get_message_actor(input.run_id, input.message_id) if input.message_id else Nonetarget = (mention_from_messagediff --git a/products/tasks/backend/tests/test_post_slack_update.py b/products/tasks/backend/tests/test_post_slack_update.pyindex 46f20a9ab4f1..606fe2ac2bd7 100644--- a/products/tasks/backend/tests/test_post_slack_update.py+++ b/products/tasks/backend/tests/test_post_slack_update.py@@ -25,20 +25,14 @@ class TestPostSlackUpdate(TestCase):"thread_ts": "1111.0000","user_message_ts": "2222.0000",}- # The footer, and the access gate behind its links, is built in slack_app and- # tested there. Here it only decides whether the cards carry a url, so default to- # "linkable" and let the deny-path test re-patch it.+ # The footer is built in slack_app and tested there. Here it only decides whether+ # the cards carry a url.self._footer_patcher = patch("products.slack_app.backend.slack_thread.load_run_footer",return_value=RunFooter(task_url="http://localhost:8000/project/1/tasks/10?runId=run-1"),)self._footer_patcher.start()self.addCleanup(self._footer_patcher.stop)- # The gate is patched on the class so it answers without a Slack identity lookup;- # the deny-path test flips it to prove the web url does not hang off this answer.- self._access_patcher = patch.object(SlackThreadHandler, "viewer_can_open_code_links", return_value=True)- self._access_patcher.start()- self.addCleanup(self._access_patcher.stop)# The PR-opened notification path resolves the reply target from a live# SlackThreadTaskMapping. Default that lookup to "no mapping" so tests# that don't exercise multiplayer tagging aren't forced to seed the@@ -589,86 +583,6 @@ class TestPostSlackUpdate(TestCase):bot_authored=False,)- @patch.object(SlackThreadHandler, "post_completion")- @patch.object(SlackThreadHandler, "post_or_update_progress")- @patch.object(SlackThreadHandler, "post_error")- @patch.object(SlackThreadHandler, "post_pr_opened")- @patch.object(SlackThreadHandler, "update_reaction")- @patch("products.tasks.backend.models.TaskRun")- def test_user_without_posthog_code_access_still_gets_task_url(- self,- mock_task_run_class,- _mock_update_reaction,- mock_post_pr_opened,- mock_post_error,- mock_post_progress,- mock_post_completion,- ):- # The web task link is never withheld — the task page enforces access- # itself — so every handler call (including the progress handler)- # receives the task url even when the reader fails the access check.- self._access_patcher.stop()- deny_patcher = patch.object(SlackThreadHandler, "viewer_can_open_code_links", return_value=False)- deny_patcher.start()- self.addCleanup(deny_patcher.stop)-- scenarios: list[tuple[MagicMock, MagicMock]] = []-- completed_no_pr = self._make_mock_run(mock_task_run_class.Status.COMPLETED, output={})- scenarios.append((completed_no_pr, mock_post_completion))-- completed_with_pr = self._make_mock_run(- mock_task_run_class.Status.COMPLETED, output={"pr_url": "https://github.com/org/repo/pull/1"}, state={}- )- scenarios.append((completed_with_pr, mock_post_pr_opened))-- failed = self._make_mock_run(mock_task_run_class.Status.FAILED, error_message="boom")- scenarios.append((failed, mock_post_error))-- in_progress = self._make_mock_run(mock_task_run_class.Status.IN_PROGRESS, stage="Building")- scenarios.append((in_progress, mock_post_progress))-- in_progress_with_pr = self._make_mock_run(- mock_task_run_class.Status.IN_PROGRESS,- stage="Opening PR",- output={"pr_url": "https://github.com/org/repo/pull/2"},- state={},- )- scenarios.append((in_progress_with_pr, mock_post_pr_opened))-- for run, handler_mock in scenarios:- handler_mock.reset_mock()- mock_task_run_class.objects.select_related.return_value.get.return_value = run- post_slack_update(PostSlackUpdateInput(run_id="run-1", slack_thread_context=self.slack_thread_context))- handler_mock.assert_called_once()- # ``task_url`` is the second positional argument on ``post_pr_opened``- # and the trailing positional argument on every other handler — the- # contract is "no access ⇒ the web link still flows through".- task_url_arg = (- handler_mock.call_args.args[1]- if handler_mock is mock_post_pr_opened- else handler_mock.call_args.args[-1]- )- assert task_url_arg == "http://localhost:8000/project/1/tasks/10?runId=run-1"-- mock_post_pr_opened.reset_mock()- cleaned_with_pr = self._make_mock_run(- mock_task_run_class.Status.COMPLETED,- output={"pr_url": "https://github.com/org/repo/pull/3"},- state={},- )- mock_task_run_class.objects.select_related.return_value.get.return_value = cleaned_with_pr- post_slack_update(- PostSlackUpdateInput(- run_id="run-1",- slack_thread_context=self.slack_thread_context,- sandbox_cleaned=True,- )- )- mock_post_pr_opened.assert_called_once()- # task_url is the second positional argument on post_pr_opened.- assert mock_post_pr_opened.call_args.args[1] == "http://localhost:8000/project/1/tasks/10?runId=run-1"-@patch.object(SlackThreadHandler, "post_pr_opened")@patch.object(SlackThreadHandler, "update_reaction")@patch("products.tasks.backend.models.TaskRun")