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(notebooks): don't load notebook content on the collab hot path (#81141)

Fernando Gomes8122e0228225
diff --git a/products/notebooks/backend/presentation/views/notebook.py b/products/notebooks/backend/presentation/views/notebook.py
index ccc0a0fe7dd0..25d13f4bd268 100644
--- a/products/notebooks/backend/presentation/views/notebook.py
+++ b/products/notebooks/backend/presentation/views/notebook.py
@@ -558,6 +558,9 @@ def _format_hogql_response_payload(response: Any) -> dict[str, Any]:
return response_payload
+IDENTITY_ONLY_DETAIL_ACTIONS = frozenset({"collab_presence", "collab_stream", "activity"})
+
+
@extend_schema(
description="The API for interacting with Notebooks. This feature is in early access and the API can have "
"breaking changes without announcement.",
@@ -691,6 +694,8 @@ class NotebookViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, ForbidD
# (ProseMirror JSON + full plaintext) that we'd otherwise load and JSON-decode per row.
# search/contains filters run as WHERE-clause predicates, so they don't need the columns in Python.
queryset = queryset.defer("content", "text_content")
+ elif self.action in IDENTITY_ONLY_DETAIL_ACTIONS:
+ queryset = queryset.defer("content", "text_content")
order = self.request.GET.get("order", None)
if order:
diff --git a/products/notebooks/backend/test/test_collab_api.py b/products/notebooks/backend/test/test_collab_api.py
index 76e5099f22bf..adadbe23a7db 100644
--- a/products/notebooks/backend/test/test_collab_api.py
+++ b/products/notebooks/backend/test/test_collab_api.py
@@ -5,8 +5,11 @@ from posthog.test.base import APIBaseTest
from unittest.mock import patch
from django.conf import settings
+from django.db import connection
+from django.test.utils import CaptureQueriesContext
import fakeredis
+from parameterized import parameterized
from rest_framework import status
from posthog import redis as redis_module
@@ -565,6 +568,34 @@ class TestNotebookCollabStreamAPI(APIBaseTest):
)
assert response.status_code == status.HTTP_403_FORBIDDEN
+ @parameterized.expand([("presence", "post"), ("stream", "get"), ("activity", "get")])
+ def test_identity_only_endpoints_do_not_select_notebook_content(self, action: str, method: str):
+ notebook = self._create_notebook()
+ urls = {
+ "presence": self._presence_url(notebook["short_id"]),
+ "stream": self._stream_url(notebook["short_id"]),
+ "activity": f"/api/projects/{self.team.id}/notebooks/{notebook['short_id']}/activity/",
+ }
+
+ with CaptureQueriesContext(connection) as captured:
+ if method == "post":
+ response = self.client.post(
+ urls[action],
+ data={"client_id": "caret-client", "version": notebook["version"], "cursor": {"head": 1}},
+ format="json",
+ )
+ else:
+ response = self.client.get(urls[action])
+ assert response.status_code in (status.HTTP_200_OK, status.HTTP_204_NO_CONTENT)
+ if response.streaming:
+ self._consume_stream(response)
+
+ notebook_queries = [q["sql"] for q in captured.captured_queries if "posthog_notebook" in q["sql"]]
+ assert notebook_queries
+ for sql in notebook_queries:
+ assert '"posthog_notebook"."content"' not in sql
+ assert '"posthog_notebook"."text_content"' not in sql
+
def _markdown_doc(markdown: str) -> dict:
return {