: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(notebooks): pass user to notebook HogQL execution (#66658)
Alex Lider52ebfba366c9
diff --git a/products/notebooks/backend/kernel_runtime.py b/products/notebooks/backend/kernel_runtime.pyindex 1009f27f0658..a970ae36029c 100644--- a/products/notebooks/backend/kernel_runtime.py+++ b/products/notebooks/backend/kernel_runtime.py@@ -552,7 +552,9 @@ class KernelRuntimeService:else:try:placeholders = self._parse_hogql_placeholders_payload(placeholders_payload)- response = execute_hogql_query(query=query, team=team, placeholders=placeholders)+ response = execute_hogql_query(+ query=query, team=team, placeholders=placeholders, user=handle.runtime.user+ )if hasattr(response, "model_dump"):response_payload = response.model_dump(exclude_none=True)else:diff --git a/products/notebooks/backend/presentation/views/notebook.py b/products/notebooks/backend/presentation/views/notebook.pyindex 9358cee57143..97c4666f8aa5 100644--- a/products/notebooks/backend/presentation/views/notebook.py+++ b/products/notebooks/backend/presentation/views/notebook.py@@ -781,7 +781,9 @@ class NotebookViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, ForbidDnotebook = self._get_notebook_for_kernel()try:- response = execute_hogql_query(query=serializer.validated_data["query"], team=self.team)+ response = execute_hogql_query(+ query=serializer.validated_data["query"], team=self.team, user=self._current_user()+ )except Exception as err:logger.exception("notebook_hogql_execute_failed", notebook_short_id=notebook.short_id)return Response({"error": str(err)}, status=400)diff --git a/products/notebooks/backend/test/test_kernel_runtime.py b/products/notebooks/backend/test/test_kernel_runtime.pyindex eaa6ee6034bd..18190b332504 100644--- a/products/notebooks/backend/test/test_kernel_runtime.py+++ b/products/notebooks/backend/test/test_kernel_runtime.py@@ -47,6 +47,7 @@ class _FakeSandbox:self.stream = streamself.command: str | None = Noneself.timeout_seconds: int | None = None+ self.written_files: dict[str, bytes] = {}def execute(self, command: str, timeout_seconds: int | None = None) -> _FakeExecutionResult:self.command = command@@ -60,6 +61,10 @@ class _FakeSandbox:raise RuntimeError("Fake sandbox stream not configured")return self.stream+ def write_file(self, path: str, content: bytes) -> _FakeExecutionResult:+ self.written_files[path] = content+ return self.result+class _FakeSandboxStream:def __init__(self, stdout_lines: list[str], result: _FakeExecutionResult) -> None:@@ -342,6 +347,43 @@ class TestKernelRuntimeService(BaseTest):assert execution_result.stdout == "before\nafter"mock_payload.assert_called_once_with(bridge_payload_json, handle)+ def test_notebook_bridge_hogql_executes_as_runtime_user(self) -> None:+ service = KernelRuntimeService(execution_timeout=5)+ notebook = Notebook.objects.create(team=self.team)+ runtime = KernelRuntime.objects.create(+ team=self.team,+ notebook=notebook,+ notebook_short_id=notebook.short_id,+ user=self.user,+ status=KernelRuntime.Status.RUNNING,+ backend=KernelRuntime.Backend.DOCKER,+ connection_file="/tmp/connection.json",+ sandbox_id="sandbox-1",+ )+ handle = _KernelHandle(+ runtime=runtime,+ lock_name="lock",+ started_at=timezone.now(),+ last_activity_at=timezone.now(),+ backend=KernelRuntime.Backend.DOCKER,+ sandbox_id=runtime.sandbox_id,+ )+ sandbox = _FakeSandbox(_FakeExecutionResult(stdout=""))+ _FakeSandboxClass.sandbox = sandbox+ payload = {"call": "hogql_execute", "query": "select * from warehouse_table", "response_path": "/tmp/resp.json"}++ with (+ patch.object(service, "_get_sandbox_class", return_value=_FakeSandboxClass),+ patch("products.notebooks.backend.kernel_runtime.execute_hogql_query") as mock_execute,+ ):+ mock_execute.return_value.model_dump.return_value = {"results": [[1]], "columns": ["count"]}+ service._handle_notebook_bridge_payload(json.dumps(payload), handle)++ mock_execute.assert_called_once()+ assert mock_execute.call_args.kwargs["user"] == self.user+ header, body = sandbox.written_files["/tmp/resp.json"].split(b"\n", 1)+ assert int(header) == len(body)+def test_execute_in_sandbox_stream_yields_output_and_result(self) -> None:service = KernelRuntimeService(execution_timeout=5)notebook = Notebook.objects.create(team=self.team)