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(notebooks): add durable widget snapshot storage (#104851)

Marius Andra350f8a207248
diff --git a/.semgrep/rules/security/idor-team-scoped-models.yaml b/.semgrep/rules/security/idor-team-scoped-models.yaml
index c077908c005b..85c151a84932 100644
--- a/.semgrep/rules/security/idor-team-scoped-models.yaml
+++ b/.semgrep/rules/security/idor-team-scoped-models.yaml
@@ -262,6 +262,7 @@ rules:
|Loop
|LoopFire
|NotebookWidgetInstance
+ |NotebookWidgetSnapshot
|LoopTrigger
|LLMModelConfiguration
|LLMPrompt
@@ -684,6 +685,7 @@ rules:
|LoopFire
|LoopTrigger
|NotebookWidgetInstance
+ |NotebookWidgetSnapshot
|LLMModelConfiguration
|LLMPrompt
|LLMPromptDependency
diff --git a/docs/internal/activity-logging.md b/docs/internal/activity-logging.md
index eccc191ad111..1f266b10bd48 100644
--- a/docs/internal/activity-logging.md
+++ b/docs/internal/activity-logging.md
@@ -123,6 +123,10 @@ Four registries in `activity_log.py` tune it per scope:
- `field_with_masked_contents` - record that the field changed, never its values. Use it for secrets and encrypted inputs.
- `field_name_overrides` - rename a field in the stored change so the describer shows the user-facing label.
+`changes_between` also reads named reverse relations.
+Exclude relations that hold execution results or storage bookkeeping, such as a notebook's widget snapshots.
+Reading their fail-closed managers can require team context that background writes do not have.
+
## Writes the signal cannot see
The mixin hooks `save()` and `delete()`.
diff --git a/posthog/models/activity_logging/activity_log.py b/posthog/models/activity_logging/activity_log.py
index b738b69c508a..833c29262002 100644
--- a/posthog/models/activity_logging/activity_log.py
+++ b/posthog/models/activity_logging/activity_log.py
@@ -678,6 +678,7 @@ field_exclusions: dict[AuditableScope, list[str]] = {
"Notebook": [
"text_content",
"widget_instances",
+ "widget_snapshots",
],
"FeatureFlag": [
"experiment",
diff --git a/products/notebooks/backend/migrations/0026_widget_snapshot.py b/products/notebooks/backend/migrations/0026_widget_snapshot.py
new file mode 100644
index 000000000000..533c850bec1a
--- /dev/null
+++ b/products/notebooks/backend/migrations/0026_widget_snapshot.py
@@ -0,0 +1,63 @@
+# Generated by Django 5.2.17 on 2026-09-22 22:56
+
+import django.db.models.deletion
+from django.db import migrations, models
+
+import posthog.uuidt
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("notebooks", "0025_notebook_node_run_by_run_idx"),
+ ("posthog", "1368_sessionrecording_untrack_lts_fields"),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name="NotebookWidgetSnapshot",
+ fields=[
+ (
+ "id",
+ models.UUIDField(
+ default=posthog.uuidt.uuid7,
+ editable=False,
+ primary_key=True,
+ serialize=False,
+ ),
+ ),
+ ("node_id", models.CharField(max_length=128)),
+ ("input_bindings", models.JSONField(default=dict)),
+ ("source_runs", models.JSONField(default=dict)),
+ ("frames", models.JSONField(default=dict)),
+ ("created_at", models.DateTimeField(auto_now_add=True)),
+ (
+ "notebook",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="widget_snapshots",
+ to="notebooks.notebook",
+ ),
+ ),
+ (
+ "team",
+ models.ForeignKey(
+ db_constraint=False,
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="+",
+ to="posthog.team",
+ ),
+ ),
+ (
+ "version",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="snapshots",
+ to="notebooks.generatedwidgetversion",
+ ),
+ ),
+ ],
+ options={
+ "db_table": "posthog_notebook_widget_snapshot",
+ },
+ ),
+ ]
diff --git a/products/notebooks/backend/migrations/max_migration.txt b/products/notebooks/backend/migrations/max_migration.txt
index d0370fb04771..0b7b137820b3 100644
--- a/products/notebooks/backend/migrations/max_migration.txt
+++ b/products/notebooks/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0025_notebook_node_run_by_run_idx
+0026_widget_snapshot
diff --git a/products/notebooks/backend/models.py b/products/notebooks/backend/models.py
index 31fc10c75f49..2007f7179655 100644
--- a/products/notebooks/backend/models.py
+++ b/products/notebooks/backend/models.py
@@ -490,6 +490,20 @@ class NotebookWidgetInstance(TeamScopedRootMixin, UUIDModel):
]
+class NotebookWidgetSnapshot(TeamScopedRootMixin, UUIDModel):
+ team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, db_constraint=False, related_name="+")
+ notebook = models.ForeignKey("notebooks.Notebook", on_delete=models.CASCADE, related_name="widget_snapshots")
+ node_id = models.CharField(max_length=MAX_WIDGET_NODE_ID_LENGTH)
+ version = models.ForeignKey("notebooks.GeneratedWidgetVersion", on_delete=models.CASCADE, related_name="snapshots")
+ input_bindings: JSONField = JSONField(default=dict)
+ source_runs: JSONField = JSONField(default=dict)
+ frames: JSONField = JSONField(default=dict)
+ created_at = models.DateTimeField(auto_now_add=True)
+
+ class Meta:
+ db_table = "posthog_notebook_widget_snapshot"
+
+
class GeneratedWidgetGenerationJob(TeamScopedRootMixin, UUIDModel):
class Status(models.TextChoices):
QUEUED = "queued", "queued"