: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
fix(experiments): report truncation of target surface libs (#105283)
Marcel Poelkeraf1544dd8423
diff --git a/products/experiments/backend/presentation/serializers.py b/products/experiments/backend/presentation/serializers.pyindex 5d70a23e4aeb..74828c8dedd4 100644--- a/products/experiments/backend/presentation/serializers.py+++ b/products/experiments/backend/presentation/serializers.py@@ -2546,7 +2546,8 @@ class ExperimentSetupTargetSurfaceSerializer(serializers.Serializer):"experiment-calculate-running-time, scaled by the share of traffic the experiment will include."))- libs = ExperimentSetupLibReachSerializer(many=True, help_text="Up to 5 SDKs by persons reached.")+ libs = ExperimentSetupLibReachSerializer(many=True, help_text="Up to 5 SDKs, most persons reached first.")+ libs_truncated = serializers.BooleanField(help_text="True when more SDKs sent target events than libs lists.")anonymous_share = serializers.FloatField(allow_null=True,help_text=(diff --git a/products/experiments/backend/setup_context.py b/products/experiments/backend/setup_context.pyindex 343865d7126f..21087a99e8c3 100644--- a/products/experiments/backend/setup_context.py+++ b/products/experiments/backend/setup_context.py@@ -274,6 +274,7 @@ class TargetSurface:unique_persons: intexposures_per_day_estimate: floatlibs: list[LibReach]+ libs_truncated: boolanonymous_share: float | Nonedevice_id_share: float | None@@ -520,7 +521,7 @@ def _cache_key(team: Team, section: str, inputs: dict[str, Any]) -> str:# Bump the version whenever a cached dataclass changes shape: entries are pickled, so a deploy# would otherwise restore instances that miss the new fields.digest = hashlib.sha256(json.dumps(inputs, sort_keys=True).encode()).hexdigest()- return f"experiment_setup_context_v2_{team.pk}_{section}_{digest}"+ return f"experiment_setup_context_v3_{team.pk}_{section}_{digest}"def _cached(key: str, ttl: int, compute: Callable[[], T]) -> T:@@ -875,7 +876,7 @@ def _compute_target_surface(team: Team, inputs: SetupContextInputs) -> TargetSur""",{"where": _and(conditions),- "limit": ast.Constant(value=TARGET_SURFACE_MAX_LIBS),+ "limit": ast.Constant(value=TARGET_SURFACE_MAX_LIBS + 1),},)unique_persons, events, device_id_events, anonymous_ids, identity_known_ids, top_libs = (@@ -906,8 +907,9 @@ def _compute_target_surface(team: Team, inputs: SetupContextInputs) -> TargetSurlib_identity_known_ids,lib_device_id_events,lib_events,- ) in top_libs or []+ ) in (top_libs or [])[:TARGET_SURFACE_MAX_LIBS]],+ libs_truncated=len(top_libs or []) > TARGET_SURFACE_MAX_LIBS,anonymous_share=_share(anonymous_ids, identity_known_ids),device_id_share=_share(device_id_events, events),)diff --git a/products/experiments/backend/test/test_setup_context.py b/products/experiments/backend/test/test_setup_context.pyindex 6b69ba8878fe..929456a0424c 100644--- a/products/experiments/backend/test/test_setup_context.py+++ b/products/experiments/backend/test/test_setup_context.py@@ -37,6 +37,7 @@ from products.experiments.backend.running_time_calculator import BaselineStats,from products.experiments.backend.setup_context import (SDK_PROFILE_MAX_LIBS,SERVER_LIBS,+ TARGET_SURFACE_MAX_LIBS,SetupContextInputs,build_setup_context,classify_lib,@@ -492,9 +493,26 @@ class TestTargetSurfaceAndCandidateMetric(ClickhouseTestMixin, APIBaseTest):assert surface.unique_persons == 3assert surface.exposures_per_day_estimate == 3 / 14assert [(lib.lib, lib.category, lib.unique_persons) for lib in surface.libs] == [("web", "web", 3)]+ assert surface.libs_truncated is Falseassert surface.anonymous_share == 2 / 3assert surface.device_id_share == 2 / 3+ @parameterized.expand(+ [+ ("at_the_cap", TARGET_SURFACE_MAX_LIBS, False),+ ("above_the_cap", TARGET_SURFACE_MAX_LIBS + 2, True),+ ]+ )+ def test_libs_are_truncated_only_above_the_cap(self, _name: str, lib_count: int, truncated: bool) -> None:+ for index in range(lib_count):+ _create_person(team=self.team, distinct_ids=[f"user-{index}"])+ self._event(f"user-{index}", "$pageview", 1, **{"$lib": f"lib-{index:02d}"})++ surface = get_target_surface(self.team, SetupContextInputs(target_event="$pageview"))++ assert len(surface.libs) == TARGET_SURFACE_MAX_LIBS+ assert surface.libs_truncated is truncated+def test_shares_come_from_the_rows_whichever_sdk_sent_them(self) -> None:# A mobile surface carries $is_identified and $device_id as well, so the shares have to# come from every SDK rather than from the web rows alone.diff --git a/products/experiments/frontend/generated/api.schemas.ts b/products/experiments/frontend/generated/api.schemas.tsindex 74371b6d57fc..80f2abe0ea14 100644--- a/products/experiments/frontend/generated/api.schemas.ts+++ b/products/experiments/frontend/generated/api.schemas.ts@@ -3217,8 +3217,10 @@ export interface ExperimentSetupTargetSurfaceApi {unique_persons: number/** unique_persons divided by window_days. Pass it as exposure_rate_per_day to experiment-calculate-running-time, scaled by the share of traffic the experiment will include. */exposures_per_day_estimate: number- /** Up to 5 SDKs by persons reached. */+ /** Up to 5 SDKs, most persons reached first. */libs: ExperimentSetupLibReachApi[]+ /** True when more SDKs sent target events than libs lists. */+ libs_truncated: boolean/*** Among all distinct ids that report whether they are identified, whichever SDK they came from, the share that was anonymous. Null when no target event reported it.* @nullablediff --git a/products/experiments/frontend/setupInspector/SetupInspector.stories.tsx b/products/experiments/frontend/setupInspector/SetupInspector.stories.tsxindex 6d89a9446bec..66701bbc19d9 100644--- a/products/experiments/frontend/setupInspector/SetupInspector.stories.tsx+++ b/products/experiments/frontend/setupInspector/SetupInspector.stories.tsx@@ -81,6 +81,7 @@ const FULL_CONTEXT: ExperimentSetupContextResponseApi = {unique_persons: 8_470,exposures_per_day_estimate: 605,libs: [{ lib: 'web', category: 'web', unique_persons: 8_470, anonymous_share: 0.38, device_id_share: 1 }],+ libs_truncated: false,anonymous_share: 0.38,device_id_share: 1,},diff --git a/products/experiments/skills/creating-experiments/references/setup-decisions.md b/products/experiments/skills/creating-experiments/references/setup-decisions.mdindex 3353e5f45d4b..5f853511f20b 100644--- a/products/experiments/skills/creating-experiments/references/setup-decisions.md+++ b/products/experiments/skills/creating-experiments/references/setup-decisions.md@@ -39,7 +39,7 @@ Precedent can confirm a choice these facts already allow. It never overrides thi### The steps, in orderDevice-id bucketing is a web option. A mobile SDK puts no `$device_id` on its events, flag calls included, so both device-id shares step 1 reads sit near 0 on a mobile row and step 1 can never pass.-When every `target_surface.libs[]` row that reaches the surface has `category: "mobile"`, go straight to step 2: the choice is persistence or user-id bucketing, and reporting "not decided" over a share that can never arrive is wrong.+When every `target_surface.libs[]` row that reaches the surface has `category: "mobile"` and `target_surface.libs_truncated` is false, go straight to step 2: the choice is persistence or user-id bucketing, and reporting "not decided" over a share that can never arrive is wrong.1. **Device-id bucketing**, if `target_surface.device_id_share` is near 1, at least one `sdk_profile.libs` row has a `lib` that appears in `target_surface.libs`, and every matching row has `sdk_profile.libs[].device_id_share` near 1. A flag call without a device ID gets no variant, so a server SDK must forward the browser's device ID; local evaluation works when it does. `target_surface.device_id_share` is read from the target events; a `sdk_profile.libs` row's `device_id_share` is read from flag-call events. Neither is read from the flag requests, so neither proves that the request carried a device ID. The web SDK sends the device ID on flag requests from posthog-js 1.307.1. An older version puts a device ID on events and still gets no variant, so tell the user to check the SDK version. Set it up with the device-id recipe in `configuring-experiment-rollout`. `experiment-create` cannot set it.2. **Persistence** (`ensure_experience_continuity: true`), if no matching `sdk_profile.libs` row evaluates flags locally: every row that matches a `lib` in `target_surface.libs` has `sdk_profile.libs[].locally_evaluated_share` near 0, or null on a web or mobile row. Null on a server row is unknown: go to step 3. Persistence also needs person profiles for anonymous users, no bootstrapping, and `$anon_distinct_id` on server flag calls. The tool cannot see those three, so list them for the user to check.@@ -51,10 +51,10 @@ Name every unmatched SDK in the report.When an unmatched row has `category: "server"`, take step 3: an unread server SDK is the case that breaks both steps, because it may send its flag calls without a device ID and may evaluate them locally.Otherwise keep the step's choice and drop the tier to "best guess", naming the SDK that was not checked.-Both lists are capped, so read how many rows came back before you trust a match.-`target_surface.libs` holds at most the 5 SDKs with the most people on the surface, and the response carries no flag for what the cap dropped.-Fewer than 5 rows is therefore the whole list and the check above is conclusive; exactly 5 rows can hide a further SDK, a server one included.-On exactly 5 rows, say the list is at its cap and drop the tier to "best guess" even when every row matched, because "no unmatched SDK" is then unproven rather than true.+Both lists are capped, so read the truncation flag before you trust a match.+`target_surface.libs` holds at most the 5 SDKs with the most people on the surface, and `target_surface.libs_truncated` is true when the cap dropped a further SDK.+When it is false, the list is complete and the check above is conclusive; when it is true, an unseen SDK can reach the surface, a server one included.+When `target_surface.libs_truncated` is true, say the list is at its cap and drop the tier to "best guess" even when every row matched, because "no unmatched SDK" is then unproven rather than true.`sdk_profile.libs` is capped at 10 and does report it, in `sdk_profile.libs_truncated`.When that is true, an SDK can read as unmatched only because its own row was dropped, so name the cap alongside it. The cap cannot add a bad matching row, so steps 1 and 2 stay safe on the rows that did come back.@@ -62,9 +62,10 @@ If `sdk_profile.libs` is empty, read `sdk_profile.libs_on_any_event`.The endpoint fills that field only on an empty profile, so a profile that holds rows and matches none of them to `target_surface.libs` is not this branch: the unmatched-SDK rules above decide that case, and the field reads null there.It is null on an empty profile too when its own query timed out. Nothing then names the platforms, so none of the reads below apply: say so and stay on user-id bucketing, marked "not decided".It names the platforms the project sends from, which settles one case and no other: a project that sends only from mobile SDKs can never reach the share step 1 needs, so the choice there is persistence or user-id bucketing.+Trust that mobile-only read only when `sdk_profile.libs_on_any_event_truncated` is false. When it is true, the cap dropped the SDKs that sent the fewest events, so the project can also send from a web or server SDK that the list does not show.Every other platform mix stays open, a server-only project included. A server SDK that forwards the browser's device ID puts one on every flag call, and this fallback cannot see whether it does.It says nothing about device IDs on flag calls or about local evaluation, so steps 1 and 2 still do not pass. Keep user-id bucketing and mark it "not decided".-Say which platforms the project sends from. When `target_surface.device_id_share` is near 1 and `target_surface.libs` lists no server SDK, say device-id bucketing is the likely fit: with no server SDK in the mix, nothing has to forward the device ID for the flag call to carry one. A mobile-only project never reaches that share, so it is settled above rather than here.+Say which platforms the project sends from. When `target_surface.device_id_share` is near 1, `target_surface.libs` lists no server SDK, and `target_surface.libs_truncated` is false, say device-id bucketing is the likely fit: with no server SDK in the mix, nothing has to forward the device ID for the flag call to carry one. A mobile-only project never reaches that share, so it is settled above rather than here.That share counts target events, and this branch read no flag call at all, so step 1's version caveat applies here with nothing to offset it: posthog-js sends the device ID on flag requests only from 1.307.1, and an older version puts one on the events while the request still gets no variant.Give device-id bucketing as a lead to check, with the version named as the thing to confirm, and leave the choice at user-id bucketing.@@ -99,6 +100,7 @@ Keep the test-account filter on and say so. `team_defaults.new_experiments_filte- An empty `shared_metrics.metrics[].metric_event_roles` with a true match is not a rejection. The metric does count the event, but it is stored in a shape the role reader does not parse, which its `shared_metrics.metrics[].metric_type` shows as null or as a type not listed above. Retrieve it and read the query.- When the role fits, load the metric with `experiment-saved-metrics-retrieve` and check its `metric_type` and `math` against what the user asked for. The role alone does not prove those, so the tier stays best guess until you have read the query.- If it matches, link it after creation through `experiment-update` with `saved_metrics_ids`. Tier: confident. When the user asked for no questions, link it and report it rather than asking first.+ - When no listed metric fits and `shared_metrics.metric_event_match_truncated` is true, the match read only the newest shared metrics, so an older metric that counts the event shows `matches_metric_event` false. Call `experiment-saved-metrics-list` with `event` set to the metric event before you build an inline metric. That filter reads every shared metric.- If it doesn't, build an inline metric.3. Otherwise pick a shape from the metric templates in `configuring-experiment-analytics` (`references/metric-templates.md`). Tier: best guess.diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex be6a8a15de6c..ad1be17e698f 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -40803,8 +40803,10 @@ export namespace Schemas {unique_persons: number;/** unique_persons divided by window_days. Pass it as exposure_rate_per_day to experiment-calculate-running-time, scaled by the share of traffic the experiment will include. */exposures_per_day_estimate: number;- /** Up to 5 SDKs by persons reached. */+ /** Up to 5 SDKs, most persons reached first. */libs: ExperimentSetupLibReach[];+ /** True when more SDKs sent target events than libs lists. */+ libs_truncated: boolean;/*** Among all distinct ids that report whether they are identified, whichever SDK they came from, the share that was anonymous. Null when no target event reported it.* @nullable