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(canvas): component + grid canvas models and home pointer (#83542)

Peter Kirkham58c54be48c59
diff --git a/.semgrep/rules/security/idor-team-scoped-models.yaml b/.semgrep/rules/security/idor-team-scoped-models.yaml
index 31ad78d4bae9..6870633ddea6 100644
--- a/.semgrep/rules/security/idor-team-scoped-models.yaml
+++ b/.semgrep/rules/security/idor-team-scoped-models.yaml
@@ -87,6 +87,7 @@ rules:
|ButtonTile
|Canvas
|CanvasBuild
+ |CanvasHomePreference
|CanvasSourceVersion
|CanvasState
|ChangeRequest
@@ -453,6 +454,7 @@ rules:
|ButtonTile
|Canvas
|CanvasBuild
+ |CanvasHomePreference
|CanvasSourceVersion
|CanvasState
|ChangeRequest
diff --git a/products/canvas/backend/migrations/0012_grid_canvas_foundation.py b/products/canvas/backend/migrations/0012_grid_canvas_foundation.py
new file mode 100644
index 000000000000..f699e8eec2d0
--- /dev/null
+++ b/products/canvas/backend/migrations/0012_grid_canvas_foundation.py
@@ -0,0 +1,82 @@
+# Generated by Django 5.2.17 on 2026-08-16 21:58
+
+import django.utils.timezone
+import django.db.models.deletion
+from django.conf import settings
+from django.db import migrations, models
+
+import posthog.uuidt
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("canvas", "0011_canvasstate"),
+ ("posthog", "1304_organization_has_active_subscription"),
+ ("tasks", "0090_alter_origin_product_signals_chat"),
+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name="CanvasHomePreference",
+ fields=[
+ (
+ "id",
+ models.UUIDField(
+ default=posthog.uuidt.uuid7,
+ editable=False,
+ primary_key=True,
+ serialize=False,
+ ),
+ ),
+ ("created_at", models.DateTimeField(default=django.utils.timezone.now)),
+ ("updated_at", models.DateTimeField(auto_now=True)),
+ (
+ "canvas",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="+",
+ to="canvas.canvas",
+ ),
+ ),
+ (
+ "team",
+ models.ForeignKey(
+ db_constraint=False,
+ on_delete=django.db.models.deletion.CASCADE,
+ to="posthog.team",
+ ),
+ ),
+ (
+ "user",
+ models.ForeignKey(
+ db_constraint=False,
+ on_delete=django.db.models.deletion.CASCADE,
+ to=settings.AUTH_USER_MODEL,
+ ),
+ ),
+ ],
+ options={
+ "db_table": "posthog_canvas_home_preference",
+ },
+ ),
+ migrations.AddField(
+ model_name="canvas",
+ name="description",
+ field=models.TextField(blank=True, default=""),
+ ),
+ migrations.AddField(
+ model_name="canvas",
+ name="kind",
+ field=models.CharField(default="freeform", max_length=16),
+ ),
+ migrations.AddField(
+ model_name="canvassourceversion",
+ name="component_meta",
+ field=models.JSONField(blank=True, null=True),
+ ),
+ migrations.AddConstraint(
+ model_name="canvashomepreference",
+ constraint=models.UniqueConstraint(fields=("team", "user"), name="canvas_home_one_per_user_team"),
+ ),
+ ]
diff --git a/products/canvas/backend/migrations/0013_canvas_kind_store_index.py b/products/canvas/backend/migrations/0013_canvas_kind_store_index.py
new file mode 100644
index 000000000000..941d039e1e9b
--- /dev/null
+++ b/products/canvas/backend/migrations/0013_canvas_kind_store_index.py
@@ -0,0 +1,25 @@
+from django.db import migrations, models
+
+from posthog.migration_helpers import SafeAddIndexConcurrently
+
+
+class Migration(migrations.Migration):
+ # CREATE INDEX CONCURRENTLY cannot run inside a transaction, hence
+ # atomic = False. SafeAddIndexConcurrently disables statement timeouts
+ # and is idempotent under bin/migrate retries.
+ atomic = False
+
+ dependencies = [
+ ("canvas", "0012_grid_canvas_foundation"),
+ ]
+
+ operations = [
+ SafeAddIndexConcurrently(
+ model_name="canvas",
+ index=models.Index(
+ condition=models.Q(("kind", "freeform"), _negated=True),
+ fields=["team", "kind"],
+ name="canvas_kind_store",
+ ),
+ ),
+ ]
diff --git a/products/canvas/backend/migrations/max_migration.txt b/products/canvas/backend/migrations/max_migration.txt
index 0894d328c1f6..f2c3459b1043 100644
--- a/products/canvas/backend/migrations/max_migration.txt
+++ b/products/canvas/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0011_canvasstate
+0013_canvas_kind_store_index
diff --git a/products/canvas/backend/models.py b/products/canvas/backend/models.py
index 49ec2d464021..f4744680f3bb 100644
--- a/products/canvas/backend/models.py
+++ b/products/canvas/backend/models.py
@@ -14,8 +14,25 @@ class Canvas(TeamScopedRootMixin, UUIDModel):
``current_source_version`` is the editable head (publish advances it,
revert moves it back) and ``published_build`` is the live artifact pointer —
it only advances when a build completes for a version that is still the head.
+
+ ``kind`` splits the model into three shapes sharing this lifecycle:
+
+ - ``freeform`` — a standalone app; its source project compiles to one artifact.
+ - ``component`` — a reusable widget other canvases place on a grid. Same
+ source/build pipeline as freeform, plus a config schema and grid size
+ snapshotted onto each version. Visibility rides the channel like any
+ canvas: a component in a personal channel is private, in a team channel
+ it is the team's.
+ - ``grid`` — a composition of components. Its "source" is a layout document
+ (placements referencing component canvases), so publishing a grid
+ validates and versions the layout without queuing a build.
"""
+ KIND_FREEFORM = "freeform"
+ KIND_GRID = "grid"
+ KIND_COMPONENT = "component"
+ KINDS = [KIND_FREEFORM, KIND_GRID, KIND_COMPONENT]
+
# db_constraint=False: a real FK constraint to the hot posthog_team table
# takes a parent lock during migration; scoping is enforced app-side.
team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, db_constraint=False)
@@ -23,6 +40,11 @@ class Canvas(TeamScopedRootMixin, UUIDModel):
channel = models.ForeignKey("tasks.Channel", on_delete=models.CASCADE, db_constraint=False, related_name="canvases")
name = models.CharField(max_length=400)
+ kind = models.CharField(max_length=16, default=KIND_FREEFORM)
+ # Short prose describing what the canvas is/does. For components this is
+ # the store-search text agents match against, so it should say what the
+ # widget shows and what its config controls.
+ description = models.TextField(blank=True, default="")
template_id = models.CharField(max_length=64, default="freeform")
# Author-written markdown handed to generation tasks as background context.
context = models.TextField(blank=True, default="")
@@ -53,7 +75,16 @@ class Canvas(TeamScopedRootMixin, UUIDModel):
class Meta:
db_table = "posthog_canvas"
- indexes = [models.Index(fields=["channel", "-created_at"], name="canvas_channel_recency")]
+ indexes = [
+ models.Index(fields=["channel", "-created_at"], name="canvas_channel_recency"),
+ # The component store lists/searches by team + kind; freeform rows
+ # (the overwhelming majority) stay out of the index.
+ models.Index(
+ fields=["team", "kind"],
+ condition=~Q(kind="freeform"),
+ name="canvas_kind_store",
+ ),
+ ]
class CanvasSourceVersion(TeamScopedRootMixin, UUIDModel):
@@ -86,6 +117,12 @@ class CanvasSourceVersion(TeamScopedRootMixin, UUIDModel):
# without reading object storage. Null for versions that predate it.
capabilities = models.JSONField(null=True, blank=True)
+ # For component-kind canvases: the version's placement contract (config
+ # schema, grid size), denormalized from the stored source so the store and
+ # layout validation can read it without hitting object storage. Null for
+ # freeform/grid versions and component versions that predate it.
+ component_meta = models.JSONField(null=True, blank=True)
+
# True while the version is a staged draft: stored and built like any other
# version, but never the canvas head, so its build can't go live. Promoting
# clears the flag; after that the version is indistinguishable from a publish.
@@ -162,6 +199,33 @@ class CanvasBuild(TeamScopedRootMixin, UUIDModel):
]
+class CanvasHomePreference(TeamScopedRootMixin, UUIDModel):
+ """One user's home-canvas selection within a team.
+
+ Home is a pointer to an ordinary canvas (normally a grid canvas in the
+ user's personal channel), not a flag on the canvas itself — a canvas-side
+ marker was tried before (``is_home``) and retired because it entangled
+ canvas lifecycle with surface lifecycle. The FK cascade only clears the
+ pointer on a hard delete of the team or channel; the canvas delete endpoint
+ is a soft delete (``deleted=True``), so a pointer at a soft-deleted canvas
+ survives. The home reader must treat a pointer whose canvas is deleted as no
+ home set — filter ``canvas__deleted=False`` — and re-provision on next open.
+ """
+
+ team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, db_constraint=False)
+ user = models.ForeignKey("posthog.User", on_delete=models.CASCADE, db_constraint=False)
+ canvas = models.ForeignKey(Canvas, on_delete=models.CASCADE, related_name="+")
+
+ created_at = models.DateTimeField(default=timezone.now)
+ updated_at = models.DateTimeField(auto_now=True)
+
+ class Meta:
+ db_table = "posthog_canvas_home_preference"
+ constraints = [
+ models.UniqueConstraint(fields=["team", "user"], name="canvas_home_one_per_user_team"),
+ ]
+
+
class CanvasState(TeamScopedRootMixin, UUIDModel):
"""One key of a canvas's runtime key-value store (the ``ph.state`` verb).