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(experiments): make the precomputation flag read-only in the settings api (#119616)

Juraj Majerikb30f06dab4f5
diff --git a/posthog/api/team/integration_config.py b/posthog/api/team/integration_config.py
index d96b21db9765..3a468a366660 100644
--- a/posthog/api/team/integration_config.py
+++ b/posthog/api/team/integration_config.py
@@ -296,6 +296,10 @@ def handle_experiments_config(request: request.Request, team: Team) -> response.
"default_sequential_tuning_parameter",
"flag_cleanup_repository",
]
+ # Precomputation is rolled out by PostHog, through the auto-enrollment job and the
+ # staff tools. A customer write here would stamp the row as manual and stop the job
+ # from managing that team.
+ read_only_fields = ["experiment_precomputation_enabled"]
def validate_experiment_recalculation_times(self, value: list[str] | None) -> list[str] | None:
try:
@@ -304,13 +308,6 @@ def handle_experiments_config(request: request.Request, team: Team) -> response.
raise serializers.ValidationError(e.messages)
return value
- def update(self, instance: "TeamExperimentsConfig", validated_data: dict[str, Any]) -> "TeamExperimentsConfig":
- # A human toggling precomputation must stick: the auto-enrollment job only
- # writes when precomputation_enabled_set_by is null or "auto".
- if "experiment_precomputation_enabled" in validated_data:
- instance.precomputation_enabled_set_by = TeamExperimentsConfig.PrecomputationEnabledSetBy.MANUAL
- return super().update(instance, validated_data)
-
def validate_flag_cleanup_repository(self, value: str | None) -> str | None:
# Keeps the sandbox/LLM runtime the repo-selection module pulls in off the
# request import path.
diff --git a/posthog/api/test/test_project.py b/posthog/api/test/test_project.py
index 4ccecb36927d..e59b3094daca 100644
--- a/posthog/api/test/test_project.py
+++ b/posthog/api/test/test_project.py
@@ -1446,26 +1446,20 @@ class TestProjectAPI(team_api_test_factory()): # type: ignore
self.assertEqual(response.status_code, status.HTTP_200_OK, response.json())
self.assertIn("default_experiment_stats_method", response.json())
- def test_experiments_config_precomputation_toggle_stamps_manual_provenance(self):
- # A missing stamp would let the auto-enrollment job override a human's disable
- # on its next run. Other settings must not stamp it.
+ def test_experiments_config_ignores_precomputation_toggle(self):
+ # A write here would stamp the row as manual and stop the auto-enrollment job from
+ # managing the team, so the field is read-only for project admins.
response = self.client.patch(
f"/api/projects/{self.project.id}/experiments_config/",
- {"default_cuped_enabled": True},
+ {"experiment_precomputation_enabled": True, "default_cuped_enabled": True},
format="json",
)
self.assertEqual(response.status_code, status.HTTP_200_OK, response.json())
+ self.assertFalse(response.json()["experiment_precomputation_enabled"])
config = TeamExperimentsConfig.objects.get(team_id=self.project.id)
+ self.assertFalse(config.experiment_precomputation_enabled)
self.assertIsNone(config.precomputation_enabled_set_by)
-
- response = self.client.patch(
- f"/api/projects/{self.project.id}/experiments_config/",
- {"experiment_precomputation_enabled": True},
- format="json",
- )
- self.assertEqual(response.status_code, status.HTTP_200_OK, response.json())
- config.refresh_from_db()
- self.assertEqual(config.precomputation_enabled_set_by, TeamExperimentsConfig.PrecomputationEnabledSetBy.MANUAL)
+ self.assertTrue(config.default_cuped_enabled)
def test_experiments_config_changes_are_logged_as_team_activity(self):
# The first read creates the config row with defaults, which is not a change anyone made.