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): let PostHog AI find the legacy migration tool (#104743)

Anders5cf1c77721a3
diff --git a/products/experiments/backend/experiment_service.py b/products/experiments/backend/experiment_service.py
index 2fca39742dee..8957e18e5ca3 100644
--- a/products/experiments/backend/experiment_service.py
+++ b/products/experiments/backend/experiment_service.py
@@ -4094,7 +4094,10 @@ class ExperimentService:
if disallowed_fields:
raise ValidationError(
f"This experiment uses legacy metric formats and can only have its name, description, or end_date updated. "
- f"Cannot update: {', '.join(sorted(disallowed_fields))}"
+ f"Cannot update: {', '.join(sorted(disallowed_fields))}. "
+ f"To change these, migrate the experiment to the new experiments engine first: "
+ f"POST /api/projects/{experiment.team_id}/experiments/{experiment.id}/migrate "
+ f"(the experiment-migrate tool). It keeps this experiment and its results, and returns a new one."
)
# Validate end_date if present
diff --git a/products/experiments/backend/test/test_experiment_service.py b/products/experiments/backend/test/test_experiment_service.py
index e627f68bbf29..b68c601bdf3f 100644
--- a/products/experiments/backend/test/test_experiment_service.py
+++ b/products/experiments/backend/test/test_experiment_service.py
@@ -5615,6 +5615,7 @@ class TestExperimentService(APIBaseTest):
service.update_experiment(experiment, update_data)
self.assertIn("legacy metric formats", str(cm.exception))
self.assertIn(f"Cannot update: {expected_field_in_error}", str(cm.exception))
+ self.assertIn(f"/experiments/{experiment.id}/migrate", str(cm.exception))
@parameterized.expand(
[
diff --git a/products/experiments/mcp/tools.yaml b/products/experiments/mcp/tools.yaml
index 0da5aca659fd..50e8cf828c1b 100644
--- a/products/experiments/mcp/tools.yaml
+++ b/products/experiments/mcp/tools.yaml
@@ -966,8 +966,6 @@ tools:
- experimentId
- experiment_id
ui_app: experiment
- feature_flag: experiment-migrate-mcp-tool
- feature_flag_behavior: enable
experiment-pause:
operation: experiments_pause_create
enabled: true
diff --git a/products/experiments/skills/managing-experiment-lifecycle/SKILL.md b/products/experiments/skills/managing-experiment-lifecycle/SKILL.md
index d349e1dcb15e..6c7f7ebf9674 100644
--- a/products/experiments/skills/managing-experiment-lifecycle/SKILL.md
+++ b/products/experiments/skills/managing-experiment-lifecycle/SKILL.md
@@ -1,6 +1,6 @@
---
name: managing-experiment-lifecycle
-description: "Guides experiment state transitions: launching, pausing, resuming, freezing/unfreezing exposure, ending, shipping variants, archiving, resetting, duplicating, and copying to another project. Covers preconditions, implications for variant assignment and analysis, and the decision framework for when to use each action.\nTRIGGER when: user asks to launch, pause, resume, end, ship, archive, reset, duplicate, or copy an experiment to another project, or to freeze/unfreeze exposure (stop enrolling new users while metrics keep flowing, or reopen enrollment).\nDO NOT TRIGGER when: user is creating an experiment (use creating-experiments), configuring rollout (use configuring-experiment-rollout), or setting up metrics (use configuring-experiment-analytics)."
+description: "Guides experiment state transitions: launching, pausing, resuming, freezing/unfreezing exposure, ending, shipping variants, archiving, resetting, duplicating, copying to another project, and migrating a legacy experiment to the new experiments engine. Covers preconditions, implications for variant assignment and analysis, and the decision framework for when to use each action.\nTRIGGER when: user asks to launch, pause, resume, end, ship, archive, reset, duplicate, or copy an experiment to another project, to freeze/unfreeze exposure (stop enrolling new users while metrics keep flowing, or reopen enrollment), or to migrate, convert, move or upgrade a legacy experiment to the new engine or the new metric format.\nDO NOT TRIGGER when: user is creating an experiment (use creating-experiments), configuring rollout (use configuring-experiment-rollout), or setting up metrics (use configuring-experiment-analytics)."
---
# Managing experiment lifecycle
@@ -205,6 +205,29 @@ Copies an experiment into a **different project in the same organization** as a
**Confirm the source experiment and target project by name before calling** — this writes into a project the user
isn't looking at. The returned experiment (and its id) belongs to the target project.
+### Migrate to the new engine (`experiment-migrate`)
+
+Moves a legacy experiment (`is_legacy: true`, metrics of kind `ExperimentTrendsQuery` or `ExperimentFunnelsQuery`) onto the new experiments engine.
+
+Never hand-roll this.
+Creating a new experiment and copying the metrics over produces a second feature flag, so the new experiment starts with no exposures and no data.
+`experiment-migrate` reuses the original flag, so the migrated experiment keeps its audience from the first minute.
+
+- **Preconditions**: the experiment must be legacy. A 400 says it is already on the new engine.
+- **What happens**: a new experiment is created with the same configuration and the metrics converted to the new format. The legacy one is left untouched and keeps its results, so the project ends up with two experiments on one feature flag.
+- **Legacy shared metrics**: converted in the same call. Each gets a new shared metric that the migrated experiment links to.
+- **Variants**: unchanged — the flag is shared, so users keep the variant they have.
+- **Analysis**: the migrated experiment reads the same exposures. Results are recomputed by the new engine, so numbers can differ from the legacy view.
+
+**Tell the user there will be two experiments before you call it**, and link both afterwards.
+Calling it again returns the experiment the first call created, so a retry is safe.
+
+No request body.
+
+A legacy experiment also refuses most edits: `experiment-update` returns 400 for anything but name, description and end_date.
+Those three still work on the legacy experiment, so edit it directly and do not migrate for them.
+Migrate only when the user asks for it, or when a requested change touches a field the guard blocks, and then apply the change to the migrated experiment.
+
## Decision framework
| Situation | Action | Tool |
@@ -220,6 +243,7 @@ isn't looking at. The returned experiment (and its id) belongs to the target pro
| Need to start over with same config | Reset to draft | `experiment-reset` |
| Want a similar experiment with a fresh start | Duplicate | `experiment-duplicate` |
| Want the same experiment in a different project | Copy to another project | `experiment-copy-to-project` |
+| Experiment is legacy and needs the new engine | Migrate | `experiment-migrate` |
## Resolving experiments
@@ -229,19 +253,20 @@ All lifecycle actions require an experiment ID. If you don't have one, load the
## Error handling
-| Error message | Meaning |
-| ----------------------------------------------------------------- | ------------------------------------ |
-| "Experiment has already been launched." | Can't launch a non-draft experiment |
-| "Experiment has not been launched yet." | Can't end/pause/ship a draft |
-| "Experiment has already ended." | Can't end/pause a stopped experiment |
-| "Experiment is already paused." | Use resume instead |
-| "Experiment is not paused." | It's already active |
-| "Experiment is already in draft state." | Nothing to reset |
-| "Experiment is already archived." | Already done |
-| "Experiment exposure is already frozen." | Nothing to freeze |
-| "Experiment exposure is not frozen." | Nothing to unfreeze |
-| "Cannot freeze a paused experiment. Resume it first." | Resume, then freeze |
-| "Group-aggregated experiments cannot have their exposure frozen." | Structural limitation — don't retry |
+| Error message | Meaning |
+| ----------------------------------------------------------------- | --------------------------------------- |
+| "Experiment has already been launched." | Can't launch a non-draft experiment |
+| "Experiment has not been launched yet." | Can't end/pause/ship a draft |
+| "Experiment has already ended." | Can't end/pause a stopped experiment |
+| "Experiment is already paused." | Use resume instead |
+| "Experiment is not paused." | It's already active |
+| "Experiment is already in draft state." | Nothing to reset |
+| "Experiment is already archived." | Already done |
+| "Experiment exposure is already frozen." | Nothing to freeze |
+| "Experiment exposure is not frozen." | Nothing to unfreeze |
+| "Cannot freeze a paused experiment. Resume it first." | Resume, then freeze |
+| "Group-aggregated experiments cannot have their exposure frozen." | Structural limitation — don't retry |
+| "This experiment uses legacy metric formats..." | Migrate it first — `experiment-migrate` |
When you get a 400, explain the situation to the user rather than retrying.
diff --git a/services/mcp/schema/generated-tool-definitions.json b/services/mcp/schema/generated-tool-definitions.json
index 9559b1726353..3ba0e2987ef1 100644
--- a/services/mcp/schema/generated-tool-definitions.json
+++ b/services/mcp/schema/generated-tool-definitions.json
@@ -4742,9 +4742,7 @@
"idempotentHint": true,
"openWorldHint": true,
"readOnlyHint": false
- },
- "feature_flag": "experiment-migrate-mcp-tool",
- "feature_flag_behavior": "enable"
+ }
},
"experiment-pause": {
"description": "Requires an experiment ID. If you don't have the ID, load the finding-experiments skill to resolve the user's reference first. Load the managing-experiment-lifecycle skill for preconditions and side effects.\n\nPause a running experiment by deactivating its feature flag (sets flag active=false). The flag is no longer returned by the /decide endpoint, so users fall back to the application default (typically control). No new exposure events are recorded while paused. The experiment stays in running status — it is not ended.\n\nReturns 400 if: experiment is in draft (\"Experiment has not been launched yet.\"), already stopped (\"Experiment has already ended.\"), no linked flag (\"Experiment does not have a feature flag linked.\"), or already paused (\"Experiment is already paused.\").\n\nNo request body needed. Use resume to reactivate.",
diff --git a/services/mcp/schema/tool-definitions-all.json b/services/mcp/schema/tool-definitions-all.json
index 5e62cd73f384..6fa6b7f78cfc 100644
--- a/services/mcp/schema/tool-definitions-all.json
+++ b/services/mcp/schema/tool-definitions-all.json
@@ -4827,9 +4827,7 @@
"idempotentHint": true,
"openWorldHint": true,
"readOnlyHint": false
- },
- "feature_flag": "experiment-migrate-mcp-tool",
- "feature_flag_behavior": "enable"
+ }
},
"experiment-pause": {
"description": "Requires an experiment ID. If you don't have the ID, load the finding-experiments skill to resolve the user's reference first. Load the managing-experiment-lifecycle skill for preconditions and side effects.\n\nPause a running experiment by deactivating its feature flag (sets flag active=false). The flag is no longer returned by the /decide endpoint, so users fall back to the application default (typically control). No new exposure events are recorded while paused. The experiment stays in running status — it is not ended.\n\nReturns 400 if: experiment is in draft (\"Experiment has not been launched yet.\"), already stopped (\"Experiment has already ended.\"), no linked flag (\"Experiment does not have a feature flag linked.\"), or already paused (\"Experiment is already paused.\").\n\nNo request body needed. Use resume to reactivate.",
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/experiment-migrate.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/experiment-migrate.json
new file mode 100644
index 000000000000..1f1461179dd3
--- /dev/null
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/experiment-migrate.json
@@ -0,0 +1,11 @@
+{
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
+ "properties": {
+ "id": {
+ "description": "A unique integer value identifying this experiment.",
+ "type": "number"
+ }
+ },
+ "required": ["id"],
+ "type": "object"
+}
diff --git a/services/mcp/tests/unit/tool-filtering.test.ts b/services/mcp/tests/unit/tool-filtering.test.ts
index 9ff0f50ee997..8fb875e71003 100644
--- a/services/mcp/tests/unit/tool-filtering.test.ts
+++ b/services/mcp/tests/unit/tool-filtering.test.ts
@@ -1028,7 +1028,6 @@ describe('Tool Filtering - Feature Flags', () => {
'streamlit-apps',
'posthog-connect',
'experiment-behavior-comparison',
- 'experiment-migrate-mcp-tool',
'experiment-setup-context',
'data-warehouse-scene',
'data-quality-checks',
@@ -1036,7 +1035,7 @@ describe('Tool Filtering - Feature Flags', () => {
'warehouse-multi-destination',
])
)
- expect(flags).toHaveLength(38)
+ expect(flags).toHaveLength(37)
})
it('every loops tool is gated on the loops flag', () => {