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.

chore(product-analytics): hold the trends watch scope with an invariant (#96199)

Thomas Obermüller7819c0e0d003
diff --git a/posthog/test/repo_invariants/test_model_crossing_uses.py b/posthog/test/repo_invariants/test_model_crossing_uses.py
index 62ce22d38a71..3fd0c5b2c2b4 100644
--- a/posthog/test/repo_invariants/test_model_crossing_uses.py
+++ b/posthog/test/repo_invariants/test_model_crossing_uses.py
@@ -19,6 +19,10 @@ and runs; `drives(<Name>)` is a name it imports from there. `hogli product:lint`
in the product's contract-check inputs while a line stands, so the isolated tests stay sound. A new
line is a new outside test that drives product code, and that test belongs in the product.
+A product may watch one subtree of that location instead of the whole of it, once every other
+subtree has no line left. The second test below holds that scope: it fails when a line names
+something outside the watched subtree, because the watch would then miss the code the line drives.
+
The check is strict equality, not "no worse than": a line that disappears must be deleted from the
file in the same change, so the file can never go stale behind the code.
@@ -27,7 +31,14 @@ Regenerate after removing uses:
bin/hogli product:crossings --all --write-baseline
"""
-from hogli_commands.product.crossings import BASELINE_PATH, all_crossing_uses, disallowed_uses, read_baseline
+from hogli_commands.product.crossings import (
+ BASELINE_PATH,
+ all_crossing_uses,
+ disallowed_uses,
+ names_defined_in,
+ read_baseline,
+ wiring_location_label,
+)
REGENERATE = "bin/hogli product:crossings --all --write-baseline"
@@ -57,3 +68,27 @@ def test_disallowed_crossing_uses_match_the_baseline() -> None:
f"A '-' line means a use went away — good, but the file must record that too. Run: {REGENERATE}\n"
f"{report}"
)
+
+
+# product_analytics watches backend/hogql_queries/trends/ alone, because trends is the only subtree
+# tests outside the product still drive. These are the query kinds whose dispatch reaches it. Do not
+# extend this set to release a line for another subtree: widen the inputs in
+# products/product_analytics/turbo.json instead, or the suite stops re-running on a change the line
+# says it must cover.
+TRENDS_KINDS = frozenset({"TrendsQuery", "CalendarHeatmapQuery"})
+WATCHED_SUBTREE = "backend/hogql_queries/trends/"
+
+
+def test_product_analytics_drives_only_the_watched_subtree() -> None:
+ label = wiring_location_label("product_analytics", "backend/hogql_queries/")
+ watched = {f"drives({name})" for name in TRENDS_KINDS | names_defined_in("product_analytics", WATCHED_SUBTREE)}
+ outside = sorted(
+ line for line in read_baseline() if line.startswith(f"{label} ") and line.split()[2] not in watched
+ )
+ assert not outside, (
+ f"{BASELINE_PATH.name} has drives lines for product_analytics that name code outside "
+ f"{WATCHED_SUBTREE}, which is the only subtree products/product_analytics/turbo.json watches. "
+ "A change to the subtree the line names would skip the Django suite. Either move the driving "
+ "test into the product, or widen the contract-check inputs to backend/hogql_queries/**.\n"
+ + "\n".join(f" {line}" for line in outside)
+ )
diff --git a/products/architecture.md b/products/architecture.md
index 884bf0b625ca..ddc823058f7b 100644
--- a/products/architecture.md
+++ b/products/architecture.md
@@ -108,7 +108,7 @@ These cross the boundary as classes — allowed only under all three rules:
Nothing is declared. Move the driving tests into the product, regenerate the baseline, and the input may leave.
A location with several subtrees may be watched one subtree at a time.
`product_analytics` watches `backend/hogql_queries/trends/` alone, because trends is the only subtree an outside test still drives, so its funnels, retention, lifecycle, paths and stickiness runners change without re-running the suite.
- The lint reads coverage per location rather than per subtree, so nothing mechanical holds that scope yet.
+ The lint reads coverage per location rather than per subtree, so it cannot hold that scope; the repo invariant `test_product_analytics_drives_only_the_watched_subtree` does, and it fails when a line names code outside the watched subtree.
The other locations stay in the inputs by presence until their channel (Celery task names, Temporal workflow names, Max tool names) is read the same way.
3. **Validated registration.**
Registration points check `issubclass(cls, Base)` and reject anything else.
diff --git a/tools/hogli-commands/hogli_commands/product/crossings.py b/tools/hogli-commands/hogli_commands/product/crossings.py
index 9c5dfa6c89ab..95c19fc19dd7 100644
--- a/tools/hogli-commands/hogli_commands/product/crossings.py
+++ b/tools/hogli-commands/hogli_commands/product/crossings.py
@@ -803,6 +803,15 @@ def _wiring_location_exports(product: str, location: str) -> dict[_Export, str]:
return exports
+def names_defined_in(product: str, location: str) -> frozenset[str]:
+ """The names a wiring location, or a subtree of one, defines and hands out.
+
+ A product may watch one subtree of a computed wiring location rather than the whole of it, and
+ that is only sound while every `drives(...)` line names something inside the watched subtree.
+ This is how such a check reads the subtree."""
+ return frozenset(export.name for export in _wiring_location_exports(product, location))
+
+
def _top_level_names(tree: ast.Module) -> list[str]:
"""Every public name a module defines at top level: classes, functions, and assigned constants."""
names: list[str] = []