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-vision): add the observation media table (#102836)

Tue Haulunda742b8b6788a
diff --git a/.semgrep/rules/security/idor-team-scoped-models.yaml b/.semgrep/rules/security/idor-team-scoped-models.yaml
index b8342cfd508e..f3728834a0c2 100644
--- a/.semgrep/rules/security/idor-team-scoped-models.yaml
+++ b/.semgrep/rules/security/idor-team-scoped-models.yaml
@@ -314,6 +314,7 @@ rules:
|QuickFilterContext
|ReplayObservation
|ReplayObservationLabel
+ |ReplayObservationMedia
|ReplayObservationUsage
|ReplayObservationView
|ReplayScanner
@@ -723,6 +724,7 @@ rules:
|QuickFilterContext
|ReplayObservation
|ReplayObservationLabel
+ |ReplayObservationMedia
|ReplayObservationUsage
|ReplayObservationView
|ReplayScanner
diff --git a/products/replay_vision/backend/admin.py b/products/replay_vision/backend/admin.py
index 7bb63de80321..5b70a6a5a752 100644
--- a/products/replay_vision/backend/admin.py
+++ b/products/replay_vision/backend/admin.py
@@ -1,7 +1,7 @@
from django.contrib import admin
from django.http import HttpRequest
-from products.replay_vision.backend.models import ReplayObservation, ReplayScanner
+from products.replay_vision.backend.models import ReplayObservation, ReplayObservationMedia, ReplayScanner
@admin.register(ReplayScanner)
@@ -40,3 +40,17 @@ class ReplayObservationAdmin(admin.ModelAdmin):
def has_add_permission(self, request: HttpRequest) -> bool:
# Created by workflow/consumer, never via admin.
return False
+
+
[email protected](ReplayObservationMedia)
+class ReplayObservationMediaAdmin(admin.ModelAdmin):
+ list_display = ("observation", "kind", "position", "created_at")
+ list_select_related = ("observation", "team")
+ list_filter = ("kind",)
+ # raw_id_fields, not the default select: each of these targets a table too large to enumerate per row.
+ raw_id_fields = ("observation", "asset", "team")
+ readonly_fields = ("id", "created_at")
+
+ def has_add_permission(self, request: HttpRequest) -> bool:
+ # Written by the media workflow, never via admin.
+ return False
diff --git a/products/replay_vision/backend/migrations/0096_replayobservationmedia.py b/products/replay_vision/backend/migrations/0096_replayobservationmedia.py
new file mode 100644
index 000000000000..dc4296f3b782
--- /dev/null
+++ b/products/replay_vision/backend/migrations/0096_replayobservationmedia.py
@@ -0,0 +1,78 @@
+# Generated by Django 5.2.17 on 2026-09-17 18:10
+
+import django.db.models.deletion
+from django.db import migrations, models
+
+import posthog.uuidt
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("exports", "0015_exportedasset_session_index"),
+ ("posthog", "1367_widen_activitylog_client"),
+ ("replay_vision", "0095_replayobservation_rlo_team_session_idx"),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name="ReplayObservationMedia",
+ fields=[
+ (
+ "id",
+ models.UUIDField(
+ default=posthog.uuidt.uuid7,
+ editable=False,
+ primary_key=True,
+ serialize=False,
+ ),
+ ),
+ (
+ "kind",
+ models.CharField(
+ choices=[("thumbnail", "Thumbnail"), ("clip", "Clip")],
+ max_length=16,
+ ),
+ ),
+ ("position", models.PositiveSmallIntegerField()),
+ ("description", models.TextField(blank=True, null=True)),
+ ("video_start_ms", models.PositiveIntegerField()),
+ ("video_end_ms", models.PositiveIntegerField(blank=True, null=True)),
+ ("rec_start_ms", models.PositiveIntegerField(blank=True, null=True)),
+ ("rec_end_ms", models.PositiveIntegerField(blank=True, null=True)),
+ ("created_at", models.DateTimeField(auto_now_add=True)),
+ (
+ "asset",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="+",
+ to="exports.exportedasset",
+ ),
+ ),
+ (
+ "observation",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="media",
+ to="replay_vision.replayobservation",
+ ),
+ ),
+ (
+ "team",
+ models.ForeignKey(
+ db_constraint=False,
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="+",
+ to="posthog.team",
+ ),
+ ),
+ ],
+ options={
+ "constraints": [
+ models.UniqueConstraint(
+ fields=("observation", "kind", "position"),
+ name="unique_replay_observation_media_slot",
+ )
+ ],
+ },
+ ),
+ ]
diff --git a/products/replay_vision/backend/migrations/max_migration.txt b/products/replay_vision/backend/migrations/max_migration.txt
index 278010bce5f8..62d8050d1874 100644
--- a/products/replay_vision/backend/migrations/max_migration.txt
+++ b/products/replay_vision/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0095_replayobservation_rlo_team_session_idx
+0096_replayobservationmedia
diff --git a/products/replay_vision/backend/models/__init__.py b/products/replay_vision/backend/models/__init__.py
index 9eba46b6c535..1ca9c27a62ba 100644
--- a/products/replay_vision/backend/models/__init__.py
+++ b/products/replay_vision/backend/models/__init__.py
@@ -1,5 +1,6 @@
from products.replay_vision.backend.models.replay_observation import ReplayObservation
from products.replay_vision.backend.models.replay_observation_label import ReplayObservationLabel
+from products.replay_vision.backend.models.replay_observation_media import ReplayObservationMedia
from products.replay_vision.backend.models.replay_observation_usage import ReplayObservationUsage
from products.replay_vision.backend.models.replay_observation_view import ReplayObservationView
from products.replay_vision.backend.models.replay_scanner import ReplayScanner
@@ -14,6 +15,7 @@ from products.replay_vision.backend.models.vision_alert import (
__all__ = [
"ReplayObservation",
"ReplayObservationLabel",
+ "ReplayObservationMedia",
"ReplayObservationUsage",
"ReplayObservationView",
"ReplayScanner",
diff --git a/products/replay_vision/backend/models/replay_observation_media.py b/products/replay_vision/backend/models/replay_observation_media.py
new file mode 100644
index 000000000000..16238265c2de
--- /dev/null
+++ b/products/replay_vision/backend/models/replay_observation_media.py
@@ -0,0 +1,63 @@
+from django.db import models
+
+from posthog.models.scoping.root_mixin import TeamScopedRootMixin
+from posthog.models.utils import UUIDModel
+
+
+class ReplayObservationMedia(TeamScopedRootMixin, UUIDModel):
+ """A thumbnail or clip for one observation. The bytes live on the `ExportedAsset`, which also carries
+ the render state: no `content_location` means the render is still running, and `exception` means it
+ failed. This row carries what an export asset has no place for: which observation owns the media,
+ its order, the model's sentence, and the two time bases."""
+
+ class Kind(models.TextChoices):
+ THUMBNAIL = "thumbnail", "Thumbnail"
+ CLIP = "clip", "Clip"
+
+ observation = models.ForeignKey("replay_vision.ReplayObservation", on_delete=models.CASCADE, related_name="media")
+ # CASCADE so the expiry sweep's asset delete takes this row with it, leaving no link to a gone object.
+ asset = models.ForeignKey("exports.ExportedAsset", on_delete=models.CASCADE, related_name="+")
+ # db_constraint=False: the migration policy blocks real FK constraints to the hot posthog_team table.
+ team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, related_name="+", db_constraint=False)
+
+ kind = models.CharField(max_length=16, choices=Kind.choices)
+ # No default: it is half of a unique constraint, so a silent 0 collides on the second row.
+ position = models.PositiveSmallIntegerField()
+ description = models.TextField(null=True, blank=True)
+
+ # Analysis-video time, for seeking the player the person is looking at.
+ video_start_ms = models.PositiveIntegerField()
+ video_end_ms = models.PositiveIntegerField(null=True, blank=True)
+ # Recording time, which is what the rasterizer was asked to render.
+ rec_start_ms = models.PositiveIntegerField(null=True, blank=True)
+ rec_end_ms = models.PositiveIntegerField(null=True, blank=True)
+
+ created_at = models.DateTimeField(auto_now_add=True)
+
+ def save(self, *args, **kwargs) -> None:
+ # Tenant invariant, as on ReplayObservationLabel: the media, its observation and its asset are one
+ # team's. Checked on every write, not only the insert, because the render's update_or_create can
+ # point an existing row at a different asset. Nothing here comes from a request, so a mismatch is
+ # our own bug filing a frame under the wrong tenant.
+ observation_team_id = self.observation.team_id
+ if self._state.adding and not self.team_id:
+ self.team_id = observation_team_id
+ if self.team_id != observation_team_id:
+ raise ValueError(
+ f"ReplayObservationMedia.team_id ({self.team_id}) must match observation.team_id "
+ f"({observation_team_id})"
+ )
+ if self.asset.team_id != observation_team_id:
+ raise ValueError(
+ f"ReplayObservationMedia.asset.team_id ({self.asset.team_id}) must match "
+ f"observation.team_id ({observation_team_id})"
+ )
+ super().save(*args, **kwargs)
+
+ class Meta:
+ constraints = [
+ # Its unique index also serves the ordered read of one observation's media.
+ models.UniqueConstraint(
+ fields=["observation", "kind", "position"], name="unique_replay_observation_media_slot"
+ ),
+ ]
diff --git a/products/replay_vision/backend/tests/test_replay_observation_media_model.py b/products/replay_vision/backend/tests/test_replay_observation_media_model.py
new file mode 100644
index 000000000000..48ae56d971a0
--- /dev/null
+++ b/products/replay_vision/backend/tests/test_replay_observation_media_model.py
@@ -0,0 +1,81 @@
+import pytest
+from posthog.test.base import BaseTest
+
+from django.utils import timezone
+
+from posthog.models.organization import Organization
+from posthog.models.team import Team
+from posthog.models.utils import uuid7
+
+from products.exports.backend.models.exported_asset import ExportedAsset
+from products.replay_vision.backend.models.replay_observation import (
+ ObservationStatus,
+ ObservationTrigger,
+ ReplayObservation,
+)
+from products.replay_vision.backend.models.replay_observation_media import ReplayObservationMedia
+from products.replay_vision.backend.models.replay_scanner import ReplayScanner, ScannerModel, ScannerType
+
+
+class TestReplayObservationMediaTenancy(BaseTest):
+ def setUp(self) -> None:
+ super().setUp()
+ scanner = ReplayScanner.objects.create(
+ team=self.team,
+ name="Checkout monitor",
+ scanner_type=ScannerType.MONITOR,
+ scanner_config={"prompt": "did the user check out?"},
+ model=ScannerModel.GEMINI_3_8_FLASH,
+ )
+ self.observation = ReplayObservation.objects.create(
+ scanner=scanner,
+ team=self.team,
+ session_id=f"s-{uuid7()}",
+ status=ObservationStatus.SUCCEEDED,
+ completed_at=timezone.now(),
+ triggered_by=ObservationTrigger.SCHEDULE,
+ )
+ self.other_team = Team.objects.create(organization=Organization.objects.create(name="other"), name="other")
+
+ def _asset(self, team: Team) -> ExportedAsset:
+ return ExportedAsset.objects.create(
+ team=team, export_format=ExportedAsset.ExportFormat.PNG, export_context={}, is_system=True
+ )
+
+ def test_the_team_comes_from_the_observation(self) -> None:
+ media = ReplayObservationMedia.objects.for_team(self.team.id).create(
+ observation=self.observation,
+ asset=self._asset(self.team),
+ kind=ReplayObservationMedia.Kind.THUMBNAIL,
+ position=0,
+ video_start_ms=1000,
+ )
+
+ assert media.team_id == self.observation.team_id
+
+ def test_moving_an_existing_row_onto_another_teams_asset_is_refused(self) -> None:
+ # The render's update_or_create repoints an existing row at a freshly created asset, so the
+ # invariant has to hold on the update too, not only the insert.
+ media = ReplayObservationMedia.objects.for_team(self.team.id).create(
+ observation=self.observation,
+ asset=self._asset(self.team),
+ kind=ReplayObservationMedia.Kind.THUMBNAIL,
+ position=0,
+ video_start_ms=1000,
+ )
+ media.asset = self._asset(self.other_team)
+
+ with pytest.raises(ValueError):
+ media.save(update_fields=["asset"])
+
+ def test_an_asset_from_another_team_is_refused(self) -> None:
+ # Nothing here comes from a request, so this is a guard against our own bug filing a frame under
+ # the wrong tenant.
+ with pytest.raises(ValueError):
+ ReplayObservationMedia.objects.for_team(self.team.id).create(
+ observation=self.observation,
+ asset=self._asset(self.other_team),
+ kind=ReplayObservationMedia.Kind.THUMBNAIL,
+ position=0,
+ video_start_ms=1000,
+ )