: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(devex): refresh and simplify product isolation skill (#59353)
Julian Bez04645e51c085
diff --git a/.agents/skills/isolating-product-facade-contracts/SKILL.md b/.agents/skills/isolating-product-facade-contracts/SKILL.mdindex b33cfd16cffa..2667f9dd6c01 100644--- a/.agents/skills/isolating-product-facade-contracts/SKILL.md+++ b/.agents/skills/isolating-product-facade-contracts/SKILL.md@@ -20,6 +20,7 @@ Read these before changing code:4. [posthog/models/team/README.md](posthog/models/team/README.md) (team extension model rule)5. [docs/published/handbook/engineering/type-system.md](docs/published/handbook/engineering/type-system.md) (serializer/OpenAPI type flow)6. [docs/published/handbook/engineering/ai/implementing-mcp-tools.md](docs/published/handbook/engineering/ai/implementing-mcp-tools.md) (schema quality and team isolation expectations)+7. [.agents/security.md](.agents/security.md) (SQL/HogQL security guidelines)Use Visual review as the concrete reference implementation:@@ -31,17 +32,31 @@ Use Visual review as the concrete reference implementation:- [products/visual_review/backend/tests/test_api.py](products/visual_review/backend/tests/test_api.py)- [products/visual_review/backend/tests/test_presentation.py](products/visual_review/backend/tests/test_presentation.py)-For detailed sequencing, load [references/phased-migration-plan.md](references/phased-migration-plan.md).+Before changing code, get the baseline:++```bash+hogli product:maturity <name> # scores models, facade, presentation, boundaries, codegen+hogli product:lint <name> # structural lint + isolation chain (strict if facade/contracts.py exists)+rg -n "from products\.<name>\.backend\.(models|logic|presentation|tasks|storage)" .+```++The `rg` output is your import map: every line is a caller that needs to migrate to the facade.## Guardrails- Keep facades thin; put business rules in `logic.py`.+- Transaction boundaries belong in the facade (or logic), not in views.- Never return ORM models across product boundaries.- Keep contracts pure (no Django/DRF imports).- Filter by `team_id` in querysets.- Do not add product-specific fields to `Team`; use a Team Extension model.- Add request/response schema annotations on viewset endpoints (`@validated_request` or `@extend_schema`).- Regenerate OpenAPI/types (`hogli build:openapi`) when serializer/view changes affect API schema.+- Presentation may only reach internals via the facade — enforced by the+ `presentation must use facade` import-linter contract in `pyproject.toml`+ (`tool.importlinter`). Any new internal module (`cache.py`, `helpers.py`, …) is+ auto-covered; there is no blocklist to maintain. New cross-cutting imports+ must either go through the facade or be temporarily allowlisted there.## Required migration workflow@@ -60,20 +75,46 @@ For detailed sequencing, load [references/phased-migration-plan.md](references/p5. Move presentation to consume the facade.- Serializers convert JSON <-> contracts.- Views call facade methods only.-6. Enforce boundaries and verify.- - Add a global `[[interfaces]]` block in `tach.toml` with `expose` patterns for the facade and presentation views.- - Add `backend:contract-check` to `package.json` so turbo-discover treats the product as isolated.- - Run `tach check --interfaces` to verify no external imports bypass the facade.- - Run `hogli product:lint <name>` to verify the product passes all checks.+6. Enforce boundaries and verify. This is a four-step chain — each step depends+ on the previous one, and `hogli product:lint` (via `IsolationChainCheck`)+ fails if any step is skipped:+ 1. **Real facade** — `backend/facade/api.py` must have actual function defs,+ not just re-exports from `logic`.+ 2. **Tach interfaces** — preferred path is to add the product name to the+ regex in the existing shared `[[interfaces]]` block in `tach.toml` that+ exposes `backend\.facade.*` and `backend\.presentation\.views.*`. Only+ add a new dedicated block if the product needs a non-standard expose+ pattern.+ 3. **`backend:contract-check` script** — add to `package.json` so+ turbo-discover treats the product as isolated.+ 4. **Narrowed `turbo.json` inputs** — restrict `backend:contract-check`+ inputs to `backend/facade/**` and `backend/presentation/**` so the+ Django suite is only re-run on facade/presentation changes (see+ `products/visual_review/turbo.json`).+ - Verify with `tach check --dependencies --interfaces`, `lint-imports`+ (import-linter contract for presentation → facade), and `hogli product:lint <name>`.+ - Use `hogli product:maturity <name>` for a detailed breakdown of remaining+ isolation work scored across models, facade, presentation, boundaries, codegen.- Run focused tests for changed files, then product-level backend tests.+### Legacy leaks during migration++If `posthog/` or `ee/` still imports product internals (`backend.models`,+`backend.oauth`, …) when you cut the first isolation PR, add a second+`[[interfaces]]` block under the "Legacy leaks" section of `tach.toml`+allow-listing exactly the modules core still touches. This keeps the build+green while you migrate callers in subsequent PRs. Shrink and delete that+block as imports move behind the facade — the final PR removes it entirely.+`hogli product:lint` flags any product that still has legacy leak interfaces+with a `⚠ has legacy interface leaks` warning.+## PR slicing strategyDefault to several PRs instead of one big migration:-- PR 1: Add contracts + facade methods without changing external callers.+- PR 1: Add contracts + facade methods, with no caller behavior changes. For a small product this PR can also flip `api.py`/`webhooks.py` into `presentation/` and enable the 4-step chain (see `user_interviews` PR #59132 for that combined shape).- PR 2-N: Migrate caller clusters one-by-one to the facade.-- Final PR: Remove deprecated internal import paths, tighten tach boundaries, and clean dead adapters.+- Final PR: Remove deprecated internal import paths, drop "Legacy leaks" `[[interfaces]]` blocks and `ignore_imports` TODOs, and clean dead adapters.If a product has many endpoints, migrate in this order:@@ -91,8 +132,11 @@ Treat migration as complete only when:- Facade returns/accepts contracts, not ORM.- Presentation layer no longer encodes business logic.- Tests cover facade and presentation boundaries.-- A global `[[interfaces]]` block in `tach.toml` restricts imports to facade + presentation views.-- `tach check --interfaces` passes with no violations for this product.+- The product is listed in the shared `[[interfaces]]` block in `tach.toml`+ exposing `backend.facade.*` and `backend.presentation.views.*` — no legacy+ leak block remains.+- `tach check --dependencies --interfaces` passes with no violations for this product.- `lint-imports` passes (import-linter verifies presentation doesn't bypass the facade internally).-- `hogli product:lint <name>` shows no legacy leak warning.-- `backend:contract-check` is present in `package.json` (enables isolated testing in CI).+- `hogli product:lint <name>` shows no legacy leak warning and the isolation chain is intact.+- `backend:contract-check` is present in `package.json` with `turbo.json` inputs+ narrowed to `backend/facade/**` and `backend/presentation/**` (enables isolated testing in CI).diff --git a/.agents/skills/isolating-product-facade-contracts/references/phased-migration-plan.md b/.agents/skills/isolating-product-facade-contracts/references/phased-migration-plan.mddeleted file mode 100644index 7bccf4b680f6..000000000000--- a/.agents/skills/isolating-product-facade-contracts/references/phased-migration-plan.md+++ /dev/null@@ -1,117 +0,0 @@-# Phased migration plan for product isolation--This reference provides a practical execution sequence for converting an existing product-into the facade+contracts pattern used by Visual review.--## Phase 0 — Baseline and scoping--**Prerequisite:** the product must already live under `products/<name>/`. This workflow does not cover moving code out of `posthog/`, `ee/`, or other shared directories — that is a separate step. If the product still lives in legacy/common code, stop here and do the move first.--- Read architecture docs:- - [products/architecture.md](products/architecture.md)- - [products/README.md](products/README.md)- - [docs/internal/monorepo-layout.md](docs/internal/monorepo-layout.md)-- Run `hogli product:lint <name>` — the isolation progress section shows exactly which layers exist,- what's structurally wrong with each, and what to do next. Use this as the starting point before- reading code.-- Locate the product's current code under `products/<name>/backend/` and identify what's already there vs. what's still missing (models, logic, presentation, facade).-- Inventory existing tests and gaps in `backend/tests/`.--Suggested commands:--```bash-rg -n "from products\.<target>\.backend\.(models|logic|presentation|tasks|storage)" .-rg -n "products\.<target>\.backend\.facade" .-```--## Phase 1 — Create contract skeleton--Start with read paths before write paths.--1. Add `backend/facade/contracts.py` with `pydantic.dataclasses.dataclass` frozen dataclasses for existing read responses. (Stdlib `dataclasses.dataclass` works too, but the Pydantic variant adds runtime type validation on construction for the same syntax.)-2. Add `backend/facade/__init__.py` exports to make imports straightforward.-3. Add minimal domain enums either in `contracts.py` or `facade/enums.py` if they grow.--Reference patterns:--- [products/visual_review/backend/facade/contracts.py](products/visual_review/backend/facade/contracts.py)-- [products/architecture.md](products/architecture.md)--## Phase 2 — Add thin facade API--1. Add `backend/facade/api.py` with thin methods wrapping logic.-2. Implement `_to_contract` mapper functions close to facade methods.-3. Keep method signatures capability-based (`list_*`, `get_*`, `create_*`, etc.).--Reference patterns:--- [products/visual_review/backend/facade/api.py](products/visual_review/backend/facade/api.py)-- [products/architecture.md](products/architecture.md)--## Phase 3 — Migrate low-risk consumers first--Start with call sites that are easiest to verify:--1. Read-only DRF endpoints.-2. Internal helper modules that only read.-3. Any explicit cross-product imports into `models` or `logic`.--For DRF endpoints:--- Ensure schema annotations are present (`@validated_request` or `@extend_schema`).-- Keep serializers as JSON adapters between API payloads and contract dataclasses.--Docs:--- [docs/published/handbook/engineering/type-system.md](docs/published/handbook/engineering/type-system.md)-- [products/visual_review/backend/presentation/views.py](products/visual_review/backend/presentation/views.py)-- [products/visual_review/backend/presentation/serializers.py](products/visual_review/backend/presentation/serializers.py)--## Phase 4 — Migrate writes and background flows--1. Migrate create/update/delete endpoints to call facade methods.-2. Migrate Celery or async entrypoints to call facade methods, not internals.-3. Keep transaction boundaries in facade where needed.--Reference patterns:--- [products/visual_review/backend/tasks/tasks.py](products/visual_review/backend/tasks/tasks.py)-- [products/architecture.md](products/architecture.md)--## Phase 5 — Tighten boundaries and clean up--1. Remove direct callers of internal modules where facade replacements exist.-2. Add a global `[[interfaces]]` block in `tach.toml` with `expose` patterns for facade and presentation views. Add `backend:contract-check` to `package.json`. Run `tach check --interfaces` and `hogli product:lint` to verify.-3. Remove obsolete adapters and dead helper functions.--Then verify:--- Product tests pass.-- API schema generation remains healthy after serializer/view changes.-- No cross-product internal imports remain.--## Recommended PR plan--Use this template to avoid long, risky PRs:--- PR A: Add contracts + read-only facade + unit tests for facade mapping.-- PR B: Move read-only presentation endpoints to facade.-- PR C: Move write endpoints to facade.-- PR D: Move tasks/background integrations.-- PR E: Boundary enforcement and cleanup (`tach.toml`, dead code removal).--When possible, keep each PR deployable and backward compatible.--## Security and isolation checklist--- All querysets scoped by team.-- No product-specific fields added to `Team`.-- Contracts avoid Django/DRF imports.-- No cross-product ORM imports.-- View endpoints declare schemas for request/response.--Additional references:--- [posthog/models/team/README.md](posthog/models/team/README.md)-- [.agents/security.md](.agents/security.md)-- [docs/published/handbook/engineering/ai/implementing-mcp-tools.md](docs/published/handbook/engineering/ai/implementing-mcp-tools.md)