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(persons): describe random split targets in the split api docs (#117300)

Eli Reisman9763ec47ca95
diff --git a/posthog/api/person.py b/posthog/api/person.py
index 70696df727a6..31742cf51fc6 100644
--- a/posthog/api/person.py
+++ b/posthog/api/person.py
@@ -1313,11 +1313,11 @@ class PersonViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
"The original person always retains its properties. To clear individual "
"properties afterward, use the `delete_property` endpoint.\n\n"
"The split runs asynchronously: a 201 response means the task was enqueued. "
- "Newly-created split-off persons get a deterministic UUID derived from "
- "`(team_id, distinct_id)`, so they can be located client-side without polling. "
- "If you need to delete a split-off person after this call, prefer looking it up by "
- "that deterministic UUID rather than by distinct_id, since the latter still "
- "resolves to the original merged person until the async task completes."
+ "A split-off person usually gets a UUID derived from `(team_id, distinct_id)`, "
+ "but gets a random UUID when another person already holds that UUID, so do not "
+ "compute it client-side. To find or delete a split-off person, look it up by its "
+ "distinct_id once the task completes. Until then the distinct_id still resolves "
+ "to the original person."
),
request=PersonSplitRequestSerializer,
responses={201: PersonSplitResponseSerializer},
diff --git a/posthog/models/person/person.py b/posthog/models/person/person.py
index d35529623825..74688d91aed2 100644
--- a/posthog/models/person/person.py
+++ b/posthog/models/person/person.py
@@ -387,7 +387,8 @@ class Person(models.Model):
SplitPerson RPC. Personhog owns this write — there is no ORM path.
The server creates each new person with a deterministic UUIDv5
- (matching ``uuidFromDistinctId``) and bumps versions by 101, higher
+ (matching ``uuidFromDistinctId``), or a random UUID when a live person
+ already holds that one, and bumps versions by 101, higher
than delete events (which use version + 100) so the split overrides
any deleted rows. Keep in sync with:
posthog/models/person/util.py (_delete_person) and
diff --git a/posthog/models/person/test/test_split_person.py b/posthog/models/person/test/test_split_person.py
index 6b94e3321c79..e48cb7c7e8a2 100644
--- a/posthog/models/person/test/test_split_person.py
+++ b/posthog/models/person/test/test_split_person.py
@@ -175,30 +175,38 @@ class TestSplitPerson(BaseTest):
for call in mock_create_person.call_args_list:
assert call.kwargs["team_id"] == self.team.id
- def test_split_pre_existing_person_keeps_created_at_and_bumps_version(self, mock_create_pdi, mock_create_person):
- # A pre-existing split target (e.g. from a previous partial run) keeps
- # its original created_at but gets the new version; the Kafka message
- # must carry both so ClickHouse converges on the Postgres state.
+ @parameterized.expand([("live_person", False), ("tombstone", True)])
+ def test_split_target_at_the_derived_uuid(self, mock_create_pdi, mock_create_person, _name, is_deleted):
original_created_at = datetime(2020, 1, 2, 3, 4, 5, tzinfo=UTC)
with fake_personhog_client() as fake:
person = self._setup_person(fake, ["id1", "id2"], mock_create_pdi, mock_create_person, version=5)
- pre_existing_uuid = str(uuidFromDistinctId(self.team.id, "id2"))
+ derived_uuid = str(uuidFromDistinctId(self.team.id, "id2"))
fake.add_person(
team_id=self.team.id,
person_id=999_999,
- uuid=pre_existing_uuid,
+ uuid=derived_uuid,
+ version=500,
+ is_deleted=is_deleted,
created_at=int(original_created_at.timestamp() * 1000),
)
person.split_person(main_distinct_id="id1")
- assert fake._persons_by_uuid[(self.team.id, pre_existing_uuid)].version == 5 + 101
+ at_derived = fake._persons_by_uuid[(self.team.id, derived_uuid)]
+ assert at_derived.is_deleted is False
assert mock_create_person.call_count == 1
- assert mock_create_person.call_args.kwargs["uuid"] == pre_existing_uuid
- assert mock_create_person.call_args.kwargs["version"] == 5 + 101
- assert mock_create_person.call_args.kwargs["created_at"] == original_created_at
+ published = mock_create_person.call_args.kwargs
+ if is_deleted:
+ assert published["uuid"] == derived_uuid
+ assert published["version"] == 501
+ assert published["created_at"] == original_created_at
+ assert at_derived.version == 501
+ else:
+ assert published["uuid"] not in (derived_uuid, str(person.uuid))
+ assert published["version"] == 5 + 101
+ assert at_derived.version == 500
def test_partial_split_moves_only_specified_distinct_ids(self, mock_create_pdi, mock_create_person):
with fake_personhog_client() as fake:
diff --git a/posthog/personhog_client/fake_client.py b/posthog/personhog_client/fake_client.py
index 2a0bb7f97320..0c3e30f6b242 100644
--- a/posthog/personhog_client/fake_client.py
+++ b/posthog/personhog_client/fake_client.py
@@ -22,6 +22,7 @@ import time
from contextlib import contextmanager
from dataclasses import dataclass
from typing import Any
+from uuid import uuid4
from unittest.mock import patch
@@ -822,9 +823,10 @@ class FakePersonHogClient:
self, request: person_pb2.SplitPersonRequest, timeout: float | None = None
) -> person_pb2.SplitPersonResponse:
"""Stateful mirror of the real RPC: validates ownership, creates new
- persons with deterministic UUIDv5s (preserving created_at for
- pre-existing ones), moves the distinct_ids, and bumps versions by 101.
- Results are returned in request order, like the real service.
+ persons with deterministic UUIDv5s (a random UUID when a live person
+ holds it; a tombstone there is revived with its created_at), moves the
+ distinct_ids, and bumps versions by 101. Results are returned in
+ request order, like the real service.
"""
self.calls.append(_Call("split_person", request))
@@ -847,8 +849,12 @@ class FakePersonHogClient:
new_uuid = str(uuidFromDistinctId(request.team_id, did))
existing = self._persons_by_uuid.get((request.team_id, new_uuid))
+ if existing is not None and not existing.is_deleted:
+ new_uuid = str(uuid4())
+ existing = None
if existing is not None:
- existing.version = new_person_version
+ existing.is_deleted = False
+ existing.version = max(existing.version + 1, new_person_version)
new_person = existing
else:
self._next_split_person_id += 1
@@ -871,7 +877,7 @@ class FakePersonHogClient:
person_pb2.SplitResult(
distinct_id=did,
new_person_uuid=new_uuid,
- new_person_version=new_person_version,
+ new_person_version=new_person.version,
pdi_version=pdi_version,
new_person_created_at_ms=new_person.created_at,
)
diff --git a/products/persons/frontend/generated/api.ts b/products/persons/frontend/generated/api.ts
index 0089ce836b83..f33c43fb5f5a 100644
--- a/products/persons/frontend/generated/api.ts
+++ b/products/persons/frontend/generated/api.ts
@@ -390,7 +390,7 @@ export const getPersonsSplitCreateUrl = (projectId: string, id: string, params?:
*
* The original person always retains its properties. To clear individual properties afterward, use the `delete_property` endpoint.
*
- * The split runs asynchronously: a 201 response means the task was enqueued. Newly-created split-off persons get a deterministic UUID derived from `(team_id, distinct_id)`, so they can be located client-side without polling. If you need to delete a split-off person after this call, prefer looking it up by that deterministic UUID rather than by distinct_id, since the latter still resolves to the original merged person until the async task completes.
+ * The split runs asynchronously: a 201 response means the task was enqueued. A split-off person usually gets a UUID derived from `(team_id, distinct_id)`, but gets a random UUID when another person already holds that UUID, so do not compute it client-side. To find or delete a split-off person, look it up by its distinct_id once the task completes. Until then the distinct_id still resolves to the original person.
*/
export const personsSplitCreate = async (
projectId: string,
diff --git a/products/persons/frontend/generated/api.zod.ts b/products/persons/frontend/generated/api.zod.ts
index cca5d8fde472..d913e880f7d9 100644
--- a/products/persons/frontend/generated/api.zod.ts
+++ b/products/persons/frontend/generated/api.zod.ts
@@ -54,7 +54,7 @@ export const PersonsDeletePropertyCreateBody = /* @__PURE__ */ zod.object({
*
* The original person always retains its properties. To clear individual properties afterward, use the `delete_property` endpoint.
*
- * The split runs asynchronously: a 201 response means the task was enqueued. Newly-created split-off persons get a deterministic UUID derived from `(team_id, distinct_id)`, so they can be located client-side without polling. If you need to delete a split-off person after this call, prefer looking it up by that deterministic UUID rather than by distinct_id, since the latter still resolves to the original merged person until the async task completes.
+ * The split runs asynchronously: a 201 response means the task was enqueued. A split-off person usually gets a UUID derived from `(team_id, distinct_id)`, but gets a random UUID when another person already holds that UUID, so do not compute it client-side. To find or delete a split-off person, look it up by its distinct_id once the task completes. Until then the distinct_id still resolves to the original person.
*/
export const PersonsSplitCreateBody = /* @__PURE__ */ zod.object({
main_distinct_id: zod