: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(tasks): stop showing "Keeping CI green" when the loop is off (#89490)
Alessandro Pogliaghi1b1f18cc79ef
diff --git a/products/tasks/backend/constants.py b/products/tasks/backend/constants.pyindex ea9f6e8cdfa8..da2081c94ffc 100644--- a/products/tasks/backend/constants.py+++ b/products/tasks/backend/constants.py@@ -38,6 +38,7 @@ TASK_ANALYSIS_INSIGHTS_STATE_KEY = "task_analysis_insights"# Run-state key the telemetry flag decision is stamped under at dispatch (temporal/client.py).# Consumers read the stamp, so the decision stays stable for the run's whole lifetime.AGENT_OTEL_TELEMETRY_STATE_KEY = "agent_otel_telemetry_enabled"+PR_LOOP_ENABLED_STATE_KEY = "pr_loop_enabled"# Models a caller may only select while the paired flag is enabled for them. The Desktop# pickers already hide these client-side (`products/desktop/packages/shared/src/flags.ts`),diff --git a/products/tasks/backend/facade/api.py b/products/tasks/backend/facade/api.pyindex 2a97e94f236f..2c02dea1ca3a 100644--- a/products/tasks/backend/facade/api.py+++ b/products/tasks/backend/facade/api.py@@ -74,6 +74,7 @@ from products.tasks.backend.constants import (MAX_CUSTOM_IMAGES_PER_TEAM,MAX_CUSTOM_IMAGES_PER_USER,PI_CLOUD_RUNTIME_FEATURE_FLAG,+ PR_LOOP_ENABLED_STATE_KEY,PR_STATES as PR_STATES, # re-exported for presentationRESERVED_SANDBOX_ENVIRONMENT_VARIABLE_KEYS,TASK_ANALYSIS_FEATURE_FLAG,@@ -2138,6 +2139,7 @@ _PROTECTED_RUN_STATE_KEYS = frozenset(# the run-log mirror with the rollout off).AGENT_OTEL_TELEMETRY_STATE_KEY,"sandbox_event_ingest_enabled",+ PR_LOOP_ENABLED_STATE_KEY,"snapshot_external_id","snapshot_kind","snapshot_mount_path",@@ -2742,14 +2744,17 @@ def update_task_run(post_pr_created_thread_update(run, new_pr_url)# Surface the PR in the run's progress timeline the moment the agent reports it, so the install# UI advances past "Started agent" instead of waiting on the 15-min CI follow-up loop to emit- # these. Steps coalesce by id with the workflow's own pr/ci emissions (frontend mergeProgressStep),- # so the double-emit is harmless. Tolerant: a logging/stream hiccup must not fail the PATCH.+ # these. Tolerant: a logging/stream hiccup must not fail the PATCH.try:run.emit_progress_event("pr", "completed", "Opened pull request", "setup", detail=new_pr_url)- run.emit_progress_event("ci", "in_progress", "Keeping CI green", "setup")+ if (run.state or {}).get(PR_LOOP_ENABLED_STATE_KEY):+ run.emit_progress_event("ci", "in_progress", "Keeping CI green", "setup")except Exception:logger.warning("task_run.pr_progress_emit_failed", extra={"run_id": str(run.id)}, exc_info=True)+ if new_status in _TERMINAL_TASK_RUN_STATUSES and old_status != new_status:+ run.close_ci_progress_step()+new_commit_head = _commit_push_head_sha(run.output)if caller_is_agent and isinstance(run.output, dict) and new_commit_head and new_commit_head != old_commit_head:post_commits_pushed_thread_update(run, run.output["commit_push"])diff --git a/products/tasks/backend/models.py b/products/tasks/backend/models.pyindex 545a346c3958..9e6ae04c56a6 100644--- a/products/tasks/backend/models.py+++ b/products/tasks/backend/models.py@@ -42,10 +42,11 @@ from posthog.storage import object_storagefrom posthog.temporal.oauth import PosthogMcpScopesfrom posthog.uuidt import uuid7-from products.tasks.backend.constants import DEFAULT_TRUSTED_DOMAINS+from products.tasks.backend.constants import DEFAULT_TRUSTED_DOMAINS, PR_LOOP_ENABLED_STATE_KEYfrom products.tasks.backend.error_telemetry import truncate_error_messagefrom products.tasks.backend.logic.stream.redis_stream import publish_task_run_stream_eventfrom products.tasks.backend.metrics import observe_task_run_created, observe_task_run_dispatch_callback+from products.tasks.backend.pr_urls import read_pr_urlsfrom products.tasks.backend.redis import evaluate_dedicated_stream_flag, run_uses_dedicated_streamfrom products.tasks.backend.storage import append_jsonl_object@@ -2712,6 +2713,20 @@ class TaskRun(models.Model):self.append_log([event])self.publish_stream_event(event)+ def ci_progress_step_announced(self) -> bool:+ """Whether this run has a "Keeping CI green" step waiting to be closed."""+ state = self.state if isinstance(self.state, dict) else {}+ return bool(state.get(PR_LOOP_ENABLED_STATE_KEY)) and bool(read_pr_urls(self.output))++ def close_ci_progress_step(self) -> None:+ """Complete the "Keeping CI green" step so a finished run shows no spinning CI row."""+ if not self.ci_progress_step_announced():+ return+ try:+ self.emit_progress_event("ci", "completed", "Keeping CI green", "setup")+ except Exception:+ logger.warning("task_run.ci_progress_close_failed", run_id=str(self.id), exc_info=True)+def build_progress_event(self,step: str,diff --git a/products/tasks/backend/temporal/process_task/activities/get_task_processing_context.py b/products/tasks/backend/temporal/process_task/activities/get_task_processing_context.pyindex 67f418d575c2..7b99cd4cebc4 100644--- a/products/tasks/backend/temporal/process_task/activities/get_task_processing_context.py+++ b/products/tasks/backend/temporal/process_task/activities/get_task_processing_context.py@@ -21,6 +21,7 @@ from products.tasks.backend.constants import (MODAL_NETWORK_ALLOWLIST_FEATURE_FLAG,OVERLAP_CLONE_BOOT_FEATURE_FLAG,PR_BABYSIT_SNAPSHOT_FEATURE_FLAG,+ PR_LOOP_ENABLED_STATE_KEY,RTK_DISABLED_FEATURE_FLAG,SANDBOX_EVENT_INGEST_FEATURE_FLAG,SANDBOX_ROTATION_FEATURE_FLAG,@@ -1068,6 +1069,10 @@ def get_task_processing_context(input: GetTaskProcessingContextInput) -> TaskProor False) # Ensure we get a boolean value even if the flag is missingemit_agent_log(run_id, "debug", f"pr_loop_enabled: {pr_loop_enabled} for this task run")+ try:+ TaskRun.update_state_atomic(task_run.id, updates={PR_LOOP_ENABLED_STATE_KEY: pr_loop_enabled})+ except Exception as e:+ log_with_activity_context("pr_loop_enabled_stamp_failed", run_id=run_id, error=str(e))pi_persistent_streaming = task.runtime == Task.Runtime.PI and not is_slack_interaction_state(state)sandbox_event_ingest_override = state.get("sandbox_event_ingest_enabled")if pi_persistent_streaming and not isinstance(sandbox_event_ingest_override, bool):diff --git a/products/tasks/backend/temporal/process_task/activities/update_task_run_status.py b/products/tasks/backend/temporal/process_task/activities/update_task_run_status.pyindex 1283d841b649..68e60d9f9216 100644--- a/products/tasks/backend/temporal/process_task/activities/update_task_run_status.py+++ b/products/tasks/backend/temporal/process_task/activities/update_task_run_status.py@@ -119,6 +119,9 @@ def update_task_run_status(input: UpdateTaskRunStatusInput) -> None:task_run.publish_stream_state_event()observe_wizard_run_unbound(task_run)+ if input.status in _TERMINAL_STATUSES and old_status != input.status:+ task_run.close_ci_progress_step()+if input.status in [TaskRun.Status.COMPLETED, TaskRun.Status.FAILED] and old_status != input.status:_capture_terminal_analytics(task_run, input)diff --git a/products/tasks/backend/webhooks.py b/products/tasks/backend/webhooks.pyindex b02b22353f90..a4fbb3bbc3f3 100644--- a/products/tasks/backend/webhooks.py+++ b/products/tasks/backend/webhooks.py@@ -24,6 +24,7 @@ from posthog.models.user_integration import UserIntegrationfrom products.signals.backend.models import InvalidStatusTransition, SignalReportfrom products.signals.backend.report_generation.resolve_reviewers import resolve_org_github_login_to_users+from products.tasks.backend.constants import PR_LOOP_ENABLED_STATE_KEYfrom products.tasks.backend.facade.api import post_pr_created_thread_update, signal_workflow_completionfrom products.tasks.backend.facade.cancellation import cancel_task_runfrom products.tasks.backend.metrics import (@@ -380,10 +381,10 @@ def _record_run_pr_url(task_run: TaskRun, pr_url: str) -> None:# log batches at exactly this moment — and append_log's read-modify-write would race it.# Tolerant: a stream hiccup must not fail the webhook; clients recover on refetch.try:- for event in (- task_run.build_progress_event("pr", "completed", "Opened pull request", "setup", detail=pr_url),- task_run.build_progress_event("ci", "in_progress", "Keeping CI green", "setup"),- ):+ events = [task_run.build_progress_event("pr", "completed", "Opened pull request", "setup", detail=pr_url)]+ if (task_run.state or {}).get(PR_LOOP_ENABLED_STATE_KEY):+ events.append(task_run.build_progress_event("ci", "in_progress", "Keeping CI green", "setup"))+ for event in events:task_run.publish_stream_event(event)task_run.publish_stream_state_event()except Exception: