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.

feat(replay): copy recordings between teams with per-session encryption (#120179)

Tue Haulund71f46c6e084d
diff --git a/posthog/management/commands/start_temporal_worker.py b/posthog/management/commands/start_temporal_worker.py
index 2d0b4a285e63..2bd31af97f65 100644
--- a/posthog/management/commands/start_temporal_worker.py
+++ b/posthog/management/commands/start_temporal_worker.py
@@ -107,6 +107,7 @@ from posthog.temporal.salesforce_enrichment import (
ACTIVITIES as SALESFORCE_ENRICHMENT_ACTIVITIES,
WORKFLOWS as SALESFORCE_ENRICHMENT_WORKFLOWS,
)
+from posthog.temporal.session_replay.copy_recording import COPY_RECORDING_ACTIVITIES, COPY_RECORDING_WORKFLOWS
from posthog.temporal.session_replay.count_playlist_items import (
COUNT_PLAYLIST_ITEMS_ACTIVITIES,
COUNT_PLAYLIST_ITEMS_WORKFLOWS,
@@ -534,6 +535,7 @@ _task_queue_specs = [
(
settings.SESSION_REPLAY_TASK_QUEUE,
COUNT_PLAYLIST_ITEMS_WORKFLOWS
+ + COPY_RECORDING_WORKFLOWS
+ DELETE_RECORDINGS_WORKFLOWS
+ ENFORCE_MAX_REPLAY_RETENTION_WORKFLOWS
+ RASTERIZE_RECORDING_WORKFLOWS
@@ -541,6 +543,7 @@ _task_queue_specs = [
+ SURFACING_SCORE_EXPORT_SWEEP_WORKFLOWS
+ SURFACING_SCORING_SWEEP_WORKFLOWS,
COUNT_PLAYLIST_ITEMS_ACTIVITIES
+ + COPY_RECORDING_ACTIVITIES
+ DELETE_RECORDINGS_ACTIVITIES
+ ENFORCE_MAX_REPLAY_RETENTION_ACTIVITIES
+ RASTERIZE_RECORDING_ACTIVITIES
diff --git a/posthog/management/commands/start_temporal_workflow.py b/posthog/management/commands/start_temporal_workflow.py
index f8a41ad92e2a..f1b6d7552456 100644
--- a/posthog/management/commands/start_temporal_workflow.py
+++ b/posthog/management/commands/start_temporal_workflow.py
@@ -16,6 +16,7 @@ from posthog.temporal.health_checks import WORKFLOWS as HEALTH_CHECK_WORKFLOWS
from posthog.temporal.proxy_service import WORKFLOWS as PROXY_SERVICE_WORKFLOWS
from posthog.temporal.quota_limiting import WORKFLOWS as QUOTA_LIMITING_WORKFLOWS
from posthog.temporal.salesforce_enrichment import WORKFLOWS as SALESFORCE_ENRICHMENT_WORKFLOWS
+from posthog.temporal.session_replay.copy_recording import COPY_RECORDING_WORKFLOWS
from posthog.temporal.session_replay.count_playlist_items import COUNT_PLAYLIST_ITEMS_WORKFLOWS
from posthog.temporal.session_replay.delete_recordings import DELETE_RECORDINGS_WORKFLOWS
from posthog.temporal.session_replay.enforce_max_replay_retention import ENFORCE_MAX_REPLAY_RETENTION_WORKFLOWS
@@ -149,6 +150,7 @@ class Command(BaseCommand):
+ SALESFORCE_ENRICHMENT_WORKFLOWS
+ TEST_WORKFLOWS
+ COUNT_PLAYLIST_ITEMS_WORKFLOWS
+ + COPY_RECORDING_WORKFLOWS
+ DELETE_RECORDINGS_WORKFLOWS
+ ENFORCE_MAX_REPLAY_RETENTION_WORKFLOWS
+ SYNC_EVENTS_RETENTION_WORKFLOWS
diff --git a/posthog/session_recordings/recordings/recording_api_client.py b/posthog/session_recordings/recordings/recording_api_client.py
index 5f22f46294de..74be81087aff 100644
--- a/posthog/session_recordings/recordings/recording_api_client.py
+++ b/posthog/session_recordings/recordings/recording_api_client.py
@@ -1,3 +1,4 @@
+import base64
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
@@ -95,6 +96,28 @@ class RecordingApiClient:
)
raise
+ async def encrypt_blocks(
+ self, session_id: str, team_id: int, blocks: list[bytes], retention_days: int
+ ) -> list[bytes]:
+ """Encrypt compressed blocks with the session's key, minting it on first call.
+
+ Raises RecordingDeletedError if the session's key was shredded.
+ """
+ url = f"{self.base_url}/api/projects/{team_id}/recordings/{session_id}/encrypt"
+ body = {
+ "blocks": [base64.b64encode(block).decode("ascii") for block in blocks],
+ "retention_days": retention_days,
+ }
+ async with self.session.post(url, json=body, headers=recording_api_auth_headers(team_id, "write")) as response:
+ if response.status == 410:
+ data = await response.json()
+ raise RecordingDeletedError(
+ "Recording has been deleted", deleted_at=data.get("deleted_at"), deleted_by=data.get("deleted_by")
+ )
+ response.raise_for_status()
+ data = await response.json()
+ return [base64.b64decode(block) for block in data["blocks"]]
+
async def delete_recordings(self, session_ids: list[str], team_id: int, deleted_by: str) -> list[str]:
"""
Delete recordings via the Recording API.
diff --git a/posthog/session_recordings/recordings/recording_api_jwt.py b/posthog/session_recordings/recordings/recording_api_jwt.py
index 48176c8e7e7d..5e6dcd437d97 100644
--- a/posthog/session_recordings/recordings/recording_api_jwt.py
+++ b/posthog/session_recordings/recordings/recording_api_jwt.py
@@ -10,7 +10,7 @@ from posthog.scoped_service_jwt import ScopedServiceJwtPurpose
logger = structlog.get_logger(__name__)
-RecordingApiOp = Literal["read", "delete"]
+RecordingApiOp = Literal["read", "delete", "write"]
# Direct callers mint a fresh token per request, so a short lifetime suffices. The rasterizer
# can't re-mint mid-render and reuses one relayed token across a whole render, so it passes a
diff --git a/posthog/temporal/session_replay/copy_recording/__init__.py b/posthog/temporal/session_replay/copy_recording/__init__.py
new file mode 100644
index 000000000000..4542a632db6d
--- /dev/null
+++ b/posthog/temporal/session_replay/copy_recording/__init__.py
@@ -0,0 +1,6 @@
+from posthog.temporal.session_replay.copy_recording.activities import copy_session_recording
+from posthog.temporal.session_replay.copy_recording.workflow import CopyRecordingsWorkflow
+
+COPY_RECORDING_WORKFLOWS = [CopyRecordingsWorkflow]
+
+COPY_RECORDING_ACTIVITIES = [copy_session_recording]
diff --git a/posthog/temporal/session_replay/copy_recording/activities.py b/posthog/temporal/session_replay/copy_recording/activities.py
new file mode 100644
index 000000000000..c79e7cc7e7b3
--- /dev/null
+++ b/posthog/temporal/session_replay/copy_recording/activities.py
@@ -0,0 +1,283 @@
+import time
+import asyncio
+import secrets
+from collections.abc import Iterator
+from datetime import UTC, datetime, timedelta
+
+from django.conf import settings
+
+import aiohttp
+import aioboto3
+import structlog
+from asgiref.sync import sync_to_async
+from botocore.client import Config
+from temporalio import activity
+from temporalio.exceptions import ApplicationError
+
+from posthog.hogql import ast
+from posthog.hogql.query import execute_hogql_query
+
+from posthog.clickhouse.client import sync_execute
+from posthog.clickhouse.query_tagging import Feature, Product, tag_queries
+from posthog.models import Team
+from posthog.session_recordings.recordings.errors import RecordingDeletedError
+from posthog.session_recordings.recordings.recording_api_client import recording_api_client
+from posthog.session_recordings.session_recording_v2_service import RecordingBlock, fetch_blocks_from_recording_api
+from posthog.temporal.common.heartbeat import Heartbeater
+from posthog.temporal.session_replay.copy_recording.types import (
+ RETENTION_PERIOD_BY_DAYS,
+ CopySessionInput,
+ CopySessionResult,
+)
+
+logger = structlog.get_logger(__name__)
+
+# Keeps each encrypt request under recording-api's 20mb JSON body limit after base64.
+MAX_ENCRYPT_BATCH_BYTES = 10 * 1024 * 1024
+MAX_ENCRYPT_BATCH_BLOCKS = 100
+MAX_CONCURRENT_BLOCK_FETCHES = 8
+
+# SessionReplayEvents.exists() skips expired rows, and a copy keeps the source's old start time.
+REPLAY_ROW_EXISTS_QUERY = """
+SELECT count() FROM session_replay_events
+WHERE team_id = %(target_team_id)s AND session_id = %(session_id)s
+"""
+
+SESSION_WINDOW = """
+ AND timestamp >= parseDateTime64BestEffort(%(start)s, 6, 'UTC') - INTERVAL 1 DAY
+ AND timestamp <= parseDateTime64BestEffort(%(end)s, 6, 'UTC') + INTERVAL 1 DAY
+"""
+
+SESSION_EVENT_COUNT_QUERY = (
+ """
+SELECT count() FROM events
+WHERE team_id = %(target_team_id)s AND `$session_id` = %(session_id)s
+"""
+ + SESSION_WINDOW
+)
+
+# Waits for every shard, so the count after the insert sees all copied rows.
+SYNC_INSERT_SETTINGS = {"insert_distributed_sync": 1}
+
+# dmat_* columns are per-team slot allocations, so the target team reads these properties from `properties` instead.
+COPY_EVENTS_QUERY = (
+ """
+INSERT INTO writable_events (
+ uuid, event, properties, timestamp, team_id, distinct_id, elements_chain, created_at,
+ person_id, person_created_at, person_properties,
+ group0_properties, group1_properties, group2_properties, group3_properties, group4_properties,
+ group0_created_at, group1_created_at, group2_created_at, group3_created_at, group4_created_at,
+ person_mode
+)
+SELECT
+ uuid, event, properties, timestamp, %(target_team_id)s, distinct_id, elements_chain, created_at,
+ person_id, person_created_at, person_properties,
+ group0_properties, group1_properties, group2_properties, group3_properties, group4_properties,
+ group0_created_at, group1_created_at, group2_created_at, group3_created_at, group4_created_at,
+ person_mode
+FROM events
+WHERE team_id = %(source_team_id)s AND `$session_id` = %(session_id)s
+"""
+ + SESSION_WINDOW
+ + """
+ AND uuid NOT IN (
+ SELECT uuid FROM events
+ WHERE team_id = %(target_team_id)s AND `$session_id` = %(session_id)s
+"""
+ + SESSION_WINDOW
+ + ")"
+)
+
+# AI tags, highlights and surfacing scores are left out so the copy starts unscanned.
+COPY_REPLAY_ROW_QUERY = """
+INSERT INTO writable_session_replay_events (
+ session_id, team_id, distinct_id, min_first_timestamp, max_last_timestamp,
+ block_first_timestamps, block_last_timestamps, block_urls,
+ first_url, all_urls, click_count, keypress_count, mouse_activity_count, active_milliseconds,
+ console_log_count, console_warn_count, console_error_count, size, message_count, event_count,
+ snapshot_source, snapshot_library, snapshot_mode_v2, _timestamp, retention_period_days
+)
+SELECT
+ session_id, %(target_team_id)s, any(distinct_id), min(min_first_timestamp), max(max_last_timestamp),
+ arrayMap(x -> parseDateTime64BestEffort(x, 6, 'UTC'), %(block_first_timestamps)s),
+ arrayMap(x -> parseDateTime64BestEffort(x, 6, 'UTC'), %(block_last_timestamps)s),
+ %(block_urls)s,
+ argMinMergeState(first_url), groupUniqArrayArray(all_urls), sum(click_count), sum(keypress_count),
+ sum(mouse_activity_count), sum(active_milliseconds), sum(console_log_count), sum(console_warn_count),
+ sum(console_error_count), sum(size), sum(message_count), sum(event_count),
+ argMinMergeState(snapshot_source), argMinMergeState(snapshot_library), argMinMergeState(snapshot_mode_v2),
+ now(), %(retention_days)s
+FROM session_replay_events
+WHERE team_id = %(source_team_id)s AND session_id = %(session_id)s
+GROUP BY session_id
+HAVING max(is_deleted) = 0
+"""
+
+
+def _batch_blocks(blocks: list[bytes]) -> Iterator[list[bytes]]:
+ batch: list[bytes] = []
+ batch_bytes = 0
+ for block in blocks:
+ if batch and (batch_bytes + len(block) > MAX_ENCRYPT_BATCH_BYTES or len(batch) >= MAX_ENCRYPT_BATCH_BLOCKS):
+ yield batch
+ batch, batch_bytes = [], 0
+ batch.append(block)
+ batch_bytes += len(block)
+ if batch:
+ yield batch
+
+
+def _new_block_key(retention_days: int) -> str:
+ # Same shape as the ingestion consumer's batch keys, which recording-api validates before serving a block.
+ return f"{settings.SESSION_RECORDING_V2_S3_PREFIX}/{RETENTION_PERIOD_BY_DAYS[retention_days]}/{int(time.time() * 1000)}-{secrets.token_hex(8)}"
+
+
+async def _upload(key: str, body: bytes) -> None:
+ session = aioboto3.Session()
+ async with session.client( # type: ignore[call-overload]
+ "s3",
+ endpoint_url=settings.SESSION_RECORDING_V2_S3_ENDPOINT or None,
+ aws_access_key_id=settings.SESSION_RECORDING_V2_S3_ACCESS_KEY_ID,
+ aws_secret_access_key=settings.SESSION_RECORDING_V2_S3_SECRET_ACCESS_KEY,
+ config=Config(signature_version="s3v4"),
+ region_name=settings.SESSION_RECORDING_V2_S3_REGION,
+ ) as client:
+ await client.put_object(Bucket=settings.SESSION_RECORDING_V2_S3_BUCKET, Key=key, Body=body)
+
+
+def _count(query: str, params: dict) -> int:
+ return int(sync_execute(query, params)[0][0])
+
+
+def _visible_event_count(team_id: int, session_id: str, start: str, end: str) -> int:
+ team = Team.objects.get(id=team_id)
+ response = execute_hogql_query(
+ """
+ SELECT count() FROM events
+ WHERE $session_id = {session_id}
+ AND timestamp >= toDateTime64({start}, 6, 'UTC') - INTERVAL 1 DAY
+ AND timestamp <= toDateTime64({end}, 6, 'UTC') + INTERVAL 1 DAY
+ """,
+ placeholders={
+ "session_id": ast.Constant(value=session_id),
+ "start": ast.Constant(value=start),
+ "end": ast.Constant(value=end),
+ },
+ team=team,
+ )
+ return int(response.results[0][0])
+
+
+async def _copy_blocks(input: CopySessionInput, source_blocks: list[RecordingBlock]) -> list[str]:
+ fetch_slots = asyncio.Semaphore(MAX_CONCURRENT_BLOCK_FETCHES)
+
+ async with recording_api_client() as client:
+
+ async def fetch(block: RecordingBlock) -> bytes:
+ async with fetch_slots:
+ return await client.fetch_block(
+ block.key, block.start_byte, block.end_byte, input.session_id, input.source_team_id
+ )
+
+ plaintext = await asyncio.gather(*(fetch(b) for b in source_blocks))
+ encrypted: list[bytes] = []
+ for batch in _batch_blocks(plaintext):
+ encrypted += await client.encrypt_blocks(
+ input.session_id, input.target_team_id, batch, input.retention_days
+ )
+
+ key = _new_block_key(input.retention_days)
+ await _upload(key, b"".join(encrypted))
+
+ urls: list[str] = []
+ offset = 0
+ for block in encrypted:
+ urls.append(
+ f"s3://{settings.SESSION_RECORDING_V2_S3_BUCKET}/{key}?range=bytes={offset}-{offset + len(block) - 1}"
+ )
+ offset += len(block)
+ return urls
+
+
+def _check_same_organization(source_team_id: int, target_team_id: int) -> None:
+ teams = dict(Team.objects.filter(id__in=[source_team_id, target_team_id]).values_list("id", "organization_id"))
+ if source_team_id == target_team_id or len(teams) != 2:
+ raise ApplicationError("Source and target must be two existing teams", non_retryable=True)
+ if teams[source_team_id] != teams[target_team_id]:
+ raise ApplicationError("Source and target teams must be in the same organization", non_retryable=True)
+
+
+async def copy_session_recording(input: CopySessionInput) -> CopySessionResult:
+ async with Heartbeater():
+ return await _copy_session_recording(input)
+
+
+async def _copy_session_recording(input: CopySessionInput) -> CopySessionResult:
+ # The replay row goes in last because a retry treats an existing row as a finished copy.
+ tag_queries(product=Product.REPLAY, feature=Feature.QUERY, team_id=input.target_team_id)
+ log = logger.bind(session_id=input.session_id, target_team_id=input.target_team_id)
+ params = {
+ "source_team_id": input.source_team_id,
+ "target_team_id": input.target_team_id,
+ "session_id": input.session_id,
+ }
+
+ await sync_to_async(_check_same_organization)(input.source_team_id, input.target_team_id)
+
+ if await sync_to_async(_count)(REPLAY_ROW_EXISTS_QUERY, params):
+ log.info("copy_recording.already_copied")
+ return CopySessionResult(session_id=input.session_id, status="already_copied")
+
+ source_blocks = await fetch_blocks_from_recording_api(input.session_id, input.source_team_id)
+ if not source_blocks:
+ log.warning("copy_recording.source_not_found")
+ return CopySessionResult(session_id=input.session_id, status="not_found")
+
+ start, end = source_blocks[0].start_timestamp, source_blocks[-1].end_timestamp
+ # Expiry counts from the copied start time, so a too-short retention would hide the copy at once.
+ if datetime.fromisoformat(start).astimezone(UTC) + timedelta(days=input.retention_days) <= datetime.now(UTC):
+ raise ApplicationError("retention_days ends before today for this recording", non_retryable=True)
+
+ try:
+ block_urls = await _copy_blocks(input, source_blocks)
+ except RecordingDeletedError as e:
+ raise ApplicationError("Recording was deleted", non_retryable=True) from e
+ except aiohttp.ClientResponseError as e:
+ # 409 means the target session already holds a recording, 413 a block too large to encrypt in one request.
+ if e.status in (409, 413):
+ raise ApplicationError(f"recording-api refused to encrypt: {e.status}", non_retryable=True) from e
+ raise
+
+ window_params = {**params, "start": start, "end": end}
+ await sync_to_async(sync_execute)(COPY_EVENTS_QUERY, window_params, settings=SYNC_INSERT_SETTINGS)
+ event_count = await sync_to_async(_count)(SESSION_EVENT_COUNT_QUERY, window_params)
+
+ await sync_to_async(sync_execute)(
+ COPY_REPLAY_ROW_QUERY,
+ {
+ **params,
+ "block_first_timestamps": [b.start_timestamp for b in source_blocks],
+ "block_last_timestamps": [b.end_timestamp for b in source_blocks],
+ "block_urls": block_urls,
+ "retention_days": input.retention_days,
+ },
+ settings=SYNC_INSERT_SETTINGS,
+ )
+ if not await sync_to_async(_count)(REPLAY_ROW_EXISTS_QUERY, params):
+ raise ApplicationError("Source recording was deleted during the copy", non_retryable=True)
+
+ warnings: list[str] = []
+ visible = await sync_to_async(_visible_event_count)(input.target_team_id, input.session_id, start, end)
+ if visible != event_count:
+ # HogQL may read the native-JSON events table, which this copy does not write.
+ warnings.append(f"HogQL sees {visible} of {event_count} copied events")
+
+ log.info("copy_recording.copied", block_count=len(block_urls), event_count=event_count, warnings=warnings)
+ return CopySessionResult(
+ session_id=input.session_id,
+ status="copied",
+ block_count=len(block_urls),
+ event_count=event_count,
+ warnings=warnings,
+ )
diff --git a/posthog/temporal/session_replay/copy_recording/types.py b/posthog/temporal/session_replay/copy_recording/types.py
new file mode 100644
index 000000000000..ec5d2e1c1b8c
--- /dev/null
+++ b/posthog/temporal/session_replay/copy_recording/types.py
@@ -0,0 +1,37 @@
+from typing import Literal
+
+from pydantic import BaseModel, Field
+
+from posthog.session_recordings.data_retention import RETENTION_PERIOD_DAYS
+
+MAX_SESSIONS_PER_COPY = 50
+
+RETENTION_PERIOD_BY_DAYS: dict[int, str] = {days: period for period, days in RETENTION_PERIOD_DAYS.items()}
+
+RetentionDays = Literal[30, 90, 365, 1825]
+
+
+class CopyRecordingsInput(BaseModel):
+ source_team_id: int
+ target_team_id: int
+ session_ids: list[str] = Field(min_length=1, max_length=MAX_SESSIONS_PER_COPY)
+ retention_days: RetentionDays = 365
+
+
+class CopySessionInput(BaseModel):
+ source_team_id: int
+ target_team_id: int
+ session_id: str
+ retention_days: RetentionDays
+
+
+class CopySessionResult(BaseModel):
+ session_id: str
+ status: Literal["copied", "already_copied", "not_found", "failed"]
+ block_count: int = 0
+ event_count: int = 0
+ warnings: list[str] = Field(default_factory=list)
+
+
+class CopyRecordingsResult(BaseModel):
+ sessions: list[CopySessionResult]
diff --git a/posthog/temporal/session_replay/copy_recording/workflow.py b/posthog/temporal/session_replay/copy_recording/workflow.py
new file mode 100644
index 000000000000..e9db0948bbe6
--- /dev/null
+++ b/posthog/temporal/session_replay/copy_recording/workflow.py
@@ -0,0 +1,42 @@
+from datetime import timedelta
+
+from temporalio import common, workflow
+from temporalio.exceptions import ActivityError
+
+from posthog.temporal.common.base import PostHogWorkflow
+from posthog.temporal.session_replay.copy_recording.activities import copy_session_recording
+from posthog.temporal.session_replay.copy_recording.types import (
+ CopyRecordingsInput,
+ CopyRecordingsResult,
+ CopySessionInput,
+ CopySessionResult,
+)
+
+
[email protected](name="copy-recordings")
+class CopyRecordingsWorkflow(PostHogWorkflow):
+ """Copies recordings from one team to another, for example to seed a demo project."""
+
+ inputs_cls = CopyRecordingsInput
+
+ @workflow.run
+ async def run(self, input: CopyRecordingsInput) -> CopyRecordingsResult:
+ sessions: list[CopySessionResult] = []
+ for session_id in input.session_ids:
+ try:
+ result: CopySessionResult = await workflow.execute_activity(
+ copy_session_recording,
+ CopySessionInput(
+ source_team_id=input.source_team_id,
+ target_team_id=input.target_team_id,
+ session_id=session_id,
+ retention_days=input.retention_days,
+ ),
+ start_to_close_timeout=timedelta(minutes=15),
+ heartbeat_timeout=timedelta(minutes=2),
+ retry_policy=common.RetryPolicy(maximum_attempts=3, initial_interval=timedelta(seconds=30)),
+ )
+ except ActivityError as e:
+ result = CopySessionResult(session_id=session_id, status="failed", warnings=[str(e.cause or e)])
+ sessions.append(result)
+ return CopyRecordingsResult(sessions=sessions)
diff --git a/posthog/temporal/tests/session_replay/copy_recording/__init__.py b/posthog/temporal/tests/session_replay/copy_recording/__init__.py
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/posthog/temporal/tests/session_replay/copy_recording/test_activities.py b/posthog/temporal/tests/session_replay/copy_recording/test_activities.py
new file mode 100644
index 000000000000..f5b566a83a7d
--- /dev/null
+++ b/posthog/temporal/tests/session_replay/copy_recording/test_activities.py
@@ -0,0 +1,145 @@
+import re
+from datetime import UTC, datetime, timedelta
+
+from posthog.test.base import APIBaseTest, ClickhouseTestMixin, _create_event, flush_persons_and_events
+from unittest.mock import AsyncMock, MagicMock, patch
+
+from django.test import override_settings
+
+from asgiref.sync import async_to_sync
+from parameterized import parameterized
+from temporalio.exceptions import ApplicationError
+from temporalio.testing import ActivityEnvironment
+
+from posthog.models import Organization, Team
+from posthog.session_recordings.queries.session_replay_events import SessionReplayEvents
+from posthog.session_recordings.queries.test.session_replay_sql import produce_replay_summary
+from posthog.session_recordings.session_recording_v2_service import RecordingBlock
+from posthog.temporal.session_replay.copy_recording.activities import copy_session_recording
+from posthog.temporal.session_replay.copy_recording.types import CopySessionInput, CopySessionResult
+
+ACTIVITIES = "posthog.temporal.session_replay.copy_recording.activities"
+
+
+@override_settings(SESSION_RECORDING_V2_S3_BUCKET="replay-bucket", SESSION_RECORDING_V2_S3_PREFIX="session_recordings")
+class TestCopySessionRecording(ClickhouseTestMixin, APIBaseTest):
+ def setUp(self) -> None:
+ super().setUp()
+ self.target_team = Team.objects.create(organization=self.organization, name="Demo")
+ self.start = datetime.now(UTC).replace(microsecond=0) - timedelta(days=2)
+ self.blocks = [
+ RecordingBlock(
+ key=f"session_recordings/30d/{i}-0000000000000000",
+ start_byte=0,
+ end_byte=9,
+ start_timestamp=(self.start + timedelta(minutes=i)).isoformat(),
+ end_timestamp=(self.start + timedelta(minutes=i, seconds=50)).isoformat(),
+ )
+ for i in range(2)
+ ]
+ produce_replay_summary(
+ team_id=self.team.pk,
+ session_id="s1",
+ distinct_id="d1",
+ first_timestamp=self.start,
+ last_timestamp=self.start + timedelta(minutes=1, seconds=50),
+ first_url="https://example.com/pricing",
+ click_count=7,
+ active_milliseconds=60_000,
+ block_urls=[f"s3://replay-bucket/{b.key}?range=bytes=0-9" for b in self.blocks],
+ block_first_timestamps=[datetime.fromisoformat(b.start_timestamp) for b in self.blocks],
+ block_last_timestamps=[datetime.fromisoformat(b.end_timestamp) for b in self.blocks],
+ ensure_analytics_event_in_session=False,
+ )
+ self.event_uuids = [
+ _create_event(
+ team=self.team,
+ event="$pageview",
+ distinct_id="d1",
+ timestamp=self.start + timedelta(seconds=10 * i),
+ properties={"$session_id": "s1", "$current_url": "https://example.com/pricing"},
+ )
+ for i in range(3)
+ ]
+ flush_persons_and_events()
+
+ def _run(self, target_team_id: int | None = None) -> tuple[CopySessionResult, MagicMock, AsyncMock]:
+ client = MagicMock()
+ client.fetch_block = AsyncMock(side_effect=lambda key, *_: f"plain-{key[-20:]}".encode())
+ client.encrypt_blocks = AsyncMock(side_effect=lambda _s, _t, blocks, _r: [b"sealed:" + b for b in blocks])
+ client_cm = MagicMock()
+ client_cm.__aenter__ = AsyncMock(return_value=client)
+ client_cm.__aexit__ = AsyncMock(return_value=None)
+ upload = AsyncMock()
+ with (
+ patch(f"{ACTIVITIES}.fetch_blocks_from_recording_api", AsyncMock(return_value=self.blocks)),
+ patch(f"{ACTIVITIES}.recording_api_client", return_value=client_cm),
+ patch(f"{ACTIVITIES}._upload", upload),
+ ):
+ input = CopySessionInput(
+ source_team_id=self.team.pk,
+ target_team_id=target_team_id or self.target_team.pk,
+ session_id="s1",
+ retention_days=365,
+ )
+ result: CopySessionResult = async_to_sync(self._run_activity)(input)
+ return result, client, upload
+
+ async def _run_activity(self, input: CopySessionInput) -> CopySessionResult:
+ return await ActivityEnvironment().run(copy_session_recording, input)
+
+ @parameterized.expand([("fresh_target", 0), ("retry_after_partial_event_copy", 1)])
+ def test_copies_recording_events_and_metadata_into_target_team(self, _: str, pre_copied_events: int) -> None:
+ for uuid in self.event_uuids[:pre_copied_events]:
+ _create_event(
+ team=self.target_team,
+ event_uuid=uuid,
+ event="$pageview",
+ distinct_id="d1",
+ timestamp=self.start,
+ properties={"$session_id": "s1"},
+ )
+ flush_persons_and_events()
+
+ result, client, upload = self._run()
+
+ assert (result.status, result.block_count, result.event_count, result.warnings) == ("copied", 2, 3, [])
+ client.encrypt_blocks.assert_awaited_once()
+ assert client.encrypt_blocks.await_args.args[1] == self.target_team.pk
+
+ assert upload.await_args is not None
+ key, body = upload.await_args.args
+ assert re.fullmatch(r"session_recordings/1y/\d+-[0-9a-f]{16}", key)
+ metadata = SessionReplayEvents().get_metadata("s1", self.target_team)
+ assert metadata is not None
+ assert (metadata["first_url"], metadata["click_count"], metadata["active_seconds"]) == (
+ "https://example.com/pricing",
+ 7,
+ 60,
+ )
+ assert metadata["retention_period_days"] == 365
+ stored = [
+ body[int(m.group(1)) : int(m.group(2)) + 1]
+ for url in metadata["block_urls"]
+ if (m := re.search(r"range=bytes=(\d+)-(\d+)", url))
+ ]
+ assert stored == [b"sealed:" + f"plain-{b.key[-20:]}".encode() for b in self.blocks]
+
+ def test_rerun_after_copy_does_not_duplicate(self) -> None:
+ self._run()
+ result, client, upload = self._run()
+
+ assert result.status == "already_copied"
+ client.encrypt_blocks.assert_not_awaited()
+ upload.assert_not_awaited()
+
+ @parameterized.expand([("another_organization", True), ("missing_team", False)])
+ def test_refuses_target_outside_the_source_organization(self, _: str, target_exists: bool) -> None:
+ other_team = Team.objects.create(organization=Organization.objects.create(name="Other"), name="Other")
+ target_team_id = other_team.pk if target_exists else other_team.pk + 1000
+
+ with self.assertRaises(ApplicationError) as error:
+ self._run(target_team_id=target_team_id)
+
+ assert error.exception.non_retryable
+ assert SessionReplayEvents().get_metadata("s1", other_team) is None