: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(tasks): mark a live gated run's stream as watched when its log is read (#120767)
Shy Alterdf8cc6321e12
diff --git a/products/logs/frontend/generated/api.ts b/products/logs/frontend/generated/api.tsindex 1201fe3fa9a6..a8e5141bf27a 100644--- a/products/logs/frontend/generated/api.ts+++ b/products/logs/frontend/generated/api.ts@@ -1315,7 +1315,7 @@ export const getTasksRunsLogsRetrieveUrl = (projectId: string, taskId: string, i}/**- * Fetch the logs for a task run as JSONL. If the run resumes from another (state.resume_from_run_id), each ancestor's log is concatenated first (oldest ancestor → ... → this run) so resume consumers see a single continuous history. A resumed Pi run includes the earlier runs of its task session the same way, each preceded by a pi_run_started entry that names its run.+ * Fetch the logs for a task run as JSONL. If the run resumes from another (state.resume_from_run_id), each ancestor's log is concatenated first (oldest ancestor → ... → this run) so resume consumers see a single continuous history. A resumed Pi run includes the earlier runs of its task session the same way, each preceded by a pi_run_started entry that names its run. For a live presence-gated run, the request marks the stream as watched before it reads the log, so events after the snapshot reach the live stream.* @summary Get task run logs*/export const tasksRunsLogsRetrieve = async (diff --git a/products/tasks/backend/facade/api.py b/products/tasks/backend/facade/api.pyindex 2d4c078c41cd..0c809bec6787 100644--- a/products/tasks/backend/facade/api.py+++ b/products/tasks/backend/facade/api.py@@ -102,7 +102,11 @@ from products.tasks.backend.constants import ()from products.tasks.backend.error_telemetry import truncate_error_messagefrom products.tasks.backend.facade.gateway import mint_private_gateway_token, revoke_private_gateway_token-from products.tasks.backend.feature_flags import get_model_access_error, pi_cloud_runtime_enabled+from products.tasks.backend.feature_flags import (+ get_model_access_error,+ pi_cloud_runtime_enabled,+ run_stream_presence_gated,+)from products.tasks.backend.github_repository_access import (inaccessible_repositories_via_integration as _inaccessible_repositories_via_integration,)@@ -352,6 +356,7 @@ __all__ = ["get_task_run_log_size","read_task_run_log_content","read_task_run_conversation_logs",+ "mark_task_run_stream_watched","read_task_run_logs","record_comment_activity","record_run_failure_cause",@@ -5193,6 +5198,37 @@ def read_task_run_logs(run_id: str | UUID, task_id: str | UUID, team_id: int) ->return read_task_run_log_content(log_urls)+_MARK_WATCHED_REDIS_TIMEOUT_SECONDS = 0.5+++def mark_task_run_stream_watched(run_id: str | UUID, task_id: str | UUID, team_id: int) -> None:+ from products.tasks.backend.logic.stream.redis_stream import ( # noqa: PLC0415 — keep redis off the api import path+ TASK_RUN_STREAM_WATCHED_TIMEOUT,+ get_task_run_stream_key,+ get_task_run_stream_watched_key,+ )+ from products.tasks.backend.redis import ( # noqa: PLC0415 — keep redis off the api import path+ get_tasks_stream_redis_sync,+ run_uses_dedicated_stream,+ )++ info = get_task_run_stream_info(run_id, task_id, team_id)+ if info is None or info.is_terminal or not run_stream_presence_gated(info.state):+ return+ try:+ get_tasks_stream_redis_sync(+ run_uses_dedicated_stream(info.state),+ socket_timeout=_MARK_WATCHED_REDIS_TIMEOUT_SECONDS,+ socket_connect_timeout=_MARK_WATCHED_REDIS_TIMEOUT_SECONDS,+ ).set(+ get_task_run_stream_watched_key(get_task_run_stream_key(str(info.id))),+ "1",+ ex=TASK_RUN_STREAM_WATCHED_TIMEOUT,+ )+ except Exception:+ logger.warning("task_run_stream_mark_watched_failed run_id=%s", info.id, exc_info=True)++def read_task_run_conversation_logs(run_id: str | UUID, task_id: str | UUID, team_id: int) -> str | None:run = _get_visible_run(run_id, task_id, team_id)if run is None:diff --git a/products/tasks/backend/presentation/views/api.py b/products/tasks/backend/presentation/views/api.pyindex d163d59818b4..5092497b5a44 100644--- a/products/tasks/backend/presentation/views/api.py+++ b/products/tasks/backend/presentation/views/api.py@@ -3557,13 +3557,17 @@ class TaskRunViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):"concatenated first (oldest ancestor → ... → this run) so resume ""consumers see a single continuous history. A resumed Pi run ""includes the earlier runs of its task session the same way, each "- "preceded by a pi_run_started entry that names its run."+ "preceded by a pi_run_started entry that names its run. For a live "+ "presence-gated run, the request marks the stream as watched before "+ "it reads the log, so events after the snapshot reach the live stream."),)@action(detail=True, methods=["get"], url_path="logs", required_scopes=["task:read"])def logs(self, request, pk=None, **kwargs):task_id = self._ensure_task_accessible()timer = ServerTimingsGathered()+ with timer("mark_watched"):+ tasks_facade.mark_task_run_stream_watched(pk, task_id, self.team_id)with timer("s3_read"):log_content = tasks_facade.read_task_run_conversation_logs(pk, task_id, self.team_id)if log_content is None:diff --git a/products/tasks/backend/redis.py b/products/tasks/backend/redis.pyindex 6c63931b7fd1..779e098f66df 100644--- a/products/tasks/backend/redis.py+++ b/products/tasks/backend/redis.py@@ -27,8 +27,17 @@ def get_tasks_stream_redis_async(use_dedicated: bool = False):return get_async_client(_tasks_stream_redis_url(use_dedicated))-def get_tasks_stream_redis_sync(use_dedicated: bool = False):- return get_client(_tasks_stream_redis_url(use_dedicated))+def get_tasks_stream_redis_sync(+ use_dedicated: bool = False,+ *,+ socket_timeout: float | None = None,+ socket_connect_timeout: float | None = None,+):+ return get_client(+ _tasks_stream_redis_url(use_dedicated),+ socket_timeout=socket_timeout,+ socket_connect_timeout=socket_connect_timeout,+ )def get_tasks_cache() -> BaseCache:diff --git a/products/tasks/backend/tests/test_api.py b/products/tasks/backend/tests/test_api.pyindex b93e93cd15fc..afd2a7366dc5 100644--- a/products/tasks/backend/tests/test_api.py+++ b/products/tasks/backend/tests/test_api.py@@ -11199,6 +11199,29 @@ class TestTaskRunAPI(BaseTaskAPITest):self.assertEqual(response.status_code, status.HTTP_200_OK)self.assertEqual(response.content.decode("utf-8").splitlines(), expected_lines)+ @parameterized.expand(+ [+ ("live_gated_run_is_marked", TaskRun.Status.IN_PROGRESS, True, True),+ ("finished_gated_run_is_not_marked", TaskRun.Status.COMPLETED, True, False),+ ("live_ungated_run_is_not_marked", TaskRun.Status.IN_PROGRESS, False, False),+ ]+ )+ @patch("posthog.storage.object_storage.read")+ def test_logs_endpoint_marks_live_gated_stream_watched(+ self, _name: str, run_status: str, gated: bool, expected_watched: bool, mock_read+ ):+ task = self.create_task()+ run = TaskRun.objects.create(+ task=task, team=self.team, status=run_status, state={"stream_presence_gated": gated}+ )+ mock_read.side_effect = lambda path, missing_ok=False: '{"line":"x"}\n' if path == run.log_url else None++ response = self.client.get(f"/api/projects/@current/tasks/{task.id}/runs/{run.id}/logs/")++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ watched_key = get_task_run_stream_watched_key(get_task_run_stream_key(str(run.id)))+ self.assertEqual(bool(get_tasks_stream_redis_sync(False).exists(watched_key)), expected_watched)+@parameterized.expand([("earlier_run_in_session", "same", -1, ["earlier", "target"]),