Repository navigation
Conversation
PostHog Review · Deep · reviewed
|
| Step | Status | Result |
|---|---|---|
| Prepare the diff | done | 3 files, 1 chunk |
| Pick perspectives | done | 3 perspectives |
| Run review passes | done | 2 issues (+1 blind-spot) |
| Merge overlapping findings | done | 3 → 3 |
| Validate findings | done | 2 kept, 1 dismissed |
| Publish | done | 2 posted (view review) |
posthog-android Compliance ReportDate: 2026-12-02T15:20:48.244666+00:00 ✅ All Tests Passed!243/243 tests passed Capture_V1 Tests✅ 111/111 tests passed View Details
Capture_Ai_V1 Tests✅ 115/115 tests passed View Details
Feature_Flags Tests✅ 17/17 tests passed View Details
|
PR overviewNo security issues were found in this pull request. Reviews
|
dustinbyrne
left a comment
There was a problem hiding this comment.
The v2 flag-leg report works as described for a single setup. The paths, the dedupe across groups, the React Native exclusion, the reset() stop, and the opt-out fix all check out. Locally, the focused PostHogRemoteConfigTest/PostHogTest/replay suites pass at b75c2e6, and the new tests fail on the base as expected.
There's one concern, inline: after close() and then setup() on the same instance, the start-up report marks the flag as called while the SDK is disabled. There are also two notes on the changeset.
Outside this PR:
- sdk-specs#286 needs a rebase onto main before it merges. Main now has the rule that a boolean
truesatisfies a flag leg that configures a variant (session-replay-trigger-groups/spec.md"Flag leg" paragraph, and the scenario "A boolean true flag value satisfies a flag leg with a variant"). #286's rewrite of that paragraph comes from an older base and drops both. The Android evaluator implements that rule. - posthog-ios still marks a flag as called while opted out (
reportFeatureFlagCalled, also in #1141). Worth mirroring this PR's opt-out guard there. - The posthog.com replay docs will be stale.
session-replay/_snippets/flutter-installation.mdxsays replay flag evaluation doesn't capture$feature_flag_called. The "Feature flag triggers" section ofhow-to-control-which-sessions-you-record.mdxcould also say that a linked flag reports as called and counts toward experiment exposure. - The in-flight
/flagsatreset()issue from the resolved thread reproduces on main too. I'm leaving it to your planned follow-up.
AI-assisted review.
| return | ||
| } | ||
| // capture() drops the event while opted out; marking it called would suppress the exposure after optIn | ||
| if (isOptedOut()) { |
There was a problem hiding this comment.
Concern: After close() and then setup() on the same instance, the start-up flag-leg report marks the flag as called while the SDK is disabled. The app's next $feature_flag_called for that flag and value is then suppressed.
The PR body says the preload report never reaches sendFeatureFlagCalled, because remoteConfigHolder isn't set yet. That holds for the first setup but not for a re-setup:
remoteConfigisconfig?.remoteConfigHolder(PostHog.kt:211-212).close()(:763-807) setsenabled = falseand clearsfeatureFlagsCalled, but keepsthis.configand its holder.- On the next
setup(),config.remoteConfigProvider(...)(:437) constructsPostHogRemoteConfig. ItsinitrunspreloadSessionRecordingConfig()(PostHogRemoteConfig.kt:217; report at:1330) beforethis.config = config(:461) andenabled = true(:502). - So the report reaches
sendFeatureFlagCalledthrough the closed setup's holder, passes theremoteConfig == nullandisOptedOut()checks, and adds the key (:2837-2845).capturethen drops the event inprepareEventbecause!isEnabled()(:1367). - Nothing clears the key afterwards. The app's next
getFeatureFlagwith the same value sends nothing, and neither do the/configor/flagsre-arm reports, untilreset(),close(), or a value change.
PostHog.close()
PostHogAndroid.setup(context, config) // same shared instance; the cache holds a v2 flag-leg replay config and the flag value
PostHog.getFeatureFlag("session-replay-flag") // returns true, but no $feature_flag_called is capturedOn the base, v2 flag legs aren't reported at start-up, so that read sends its event. The v1 linkedFlag already has this problem on the base; this PR extends it to every v2 flag-leg flag. Re-setup on the shared instance is a supported path: the static PostHog.setup/close delegate to it, and PostHogTest covers re-setup with both a fresh and a reused config. posthog-ios#1141 doesn't have this problem, because close() sets remoteConfig = nil.
Impact: apps that re-initialize the SDK lose $feature_flag_called for replay flag-leg flags, including the app's own reads, for the rest of that setup. Experiment exposure for those flags is undercounted.
Consider returning early while disabled as well as while opted out:
// capture() drops the event while disabled or opted out; marking it called would suppress the next exposure
if (!enabled || isOptedOut()) {
return
}getFeatureFlag and getFeatureFlagResult already return early while disabled, so this only changes remote-config reports made while disabled. It would also be worth updating the PR body's preload paragraph.
Regression test, added to PostHogTest. I ran it: at head b75c2e6 the v2 test fails with expected:<1> but was:<0>, and at base 9655a57 it passes. The v1 linkedFlag variant fails on both, and a single-setup control passes on both. I haven't run it with the fix applied.
private fun reSetupFlagCalledEvents(cachedRecording: Map<String, Any?>): List<PostHogEvent> {
val http = mockHttp()
val cachePreferences = PostHogMemoryPreferences()
cachePreferences.setValue(SESSION_REPLAY, cachedRecording)
cachePreferences.setValue(FEATURE_FLAGS, mapOf("session-replay-flag" to true))
// first setup: preload runs before remoteConfigHolder exists, so it can't reach sendFeatureFlagCalled
val sut = getSut(http.url("/").toString(), preloadFeatureFlags = false, cachePreferences = cachePreferences)
sut.close()
val flagCalled = CopyOnWriteArrayList<PostHogEvent>()
val newConfig =
PostHogConfig(PROJECT_TOKEN, http.url("/").toString()).apply {
storagePrefix = tmpDir.newFolder().absolutePath
preloadFeatureFlags = false
this.cachePreferences = cachePreferences
addBeforeSend { event ->
if (event.event == "\$feature_flag_called") flagCalled.add(event)
event
}
}
sut.setup(newConfig)
assertEquals(true, sut.getFeatureFlag("session-replay-flag"))
sut.close()
return flagCalled.filter { it.properties?.get("\$feature_flag") == "session-replay-flag" }
}
@Test
fun `during re-setup a v2 flag leg preload does not suppress the first app read exposure`() {
val events =
reSetupFlagCalledEvents(
mapOf(
"endpoint" to "/b/",
"version" to 2,
"triggerGroups" to
listOf(mapOf("id" to "g0", "sampleRate" to 1.0, "conditions" to mapOf("flag" to "session-replay-flag"))),
),
)
assertEquals(1, events.size)
}
@Test
fun `during re-setup a v1 linked flag preload does not suppress the first app read exposure`() {
val events = reSetupFlagCalledEvents(mapOf("endpoint" to "/b/", "linkedFlag" to "session-replay-flag"))
assertEquals(1, events.size)
}| "posthog-android": patch | ||
| --- | ||
|
|
||
| Session replay v2 trigger groups now send `$feature_flag_called` for each flag a group uses, once per flag and not once per group, like the v1 linked flag. This reverses the behavior released in android-v4.1.0, which sent no event for v2 flag legs. A user whose SDK evaluates the group flag now counts as exposed to an experiment on that flag. Until `/flags` responds, the first report of a launch can carry a cached or bootstrapped flag value. The event follows `sendFeatureFlagEvent` and the existing per-value dedupe, so a flag key and value sends no event when it was already reported since the last reset, for example by an app flag read. React Native is unchanged. |
There was a problem hiding this comment.
Suggestion: It would help readers to say who gets the new events. The report runs even when the app has session replay off (sessionReplay defaults to false, PostHogConfig.kt:157-162), as long as the project's v2 trigger groups use a flag that has a value, because the /config, /flags re-arm and quota paths don't check the local setting. Also, "android-v4.1.0" is the posthog-android version, but this entry also lands in posthog/CHANGELOG.md, where that change shipped as 7.1.0.
Consider adding something like: "This applies even when session replay is off in the app, and adds up to one event per flag key and value per launch. Reverses the behavior released in posthog 7.1.0 / posthog-android 4.1.0." posthog-ios#1141's changeset states the per-launch volume in the same way.
| @@ -0,0 +1,6 @@ | |||
| --- | |||
| "posthog": patch | |||
There was a problem hiding this comment.
Question: Should this be a minor for both packages? The SDK guidelines ("Keep changelogs and version bumps accurate") reserve patch releases for changes that don't add customer-facing behavior. This adds $feature_flag_called events that change experiment exposure counts, and the v1 linked-flag equivalent shipped as a minor in 3.36.0. If patch is meant to match posthog-ios#1141, that's fine too. This doesn't block the code change.
turnipdabeets
left a comment
There was a problem hiding this comment.
Checked against sdk-specs#286: one report per distinct key through the normal flag-called path, no V1 linkedFlag under groups, a variant leg with true reports true, no report without a value, React Native excluded (JS owns it), and the reset() generation stop all hold. The focused PostHogRemoteConfigTest and PostHogTest pass locally at b75c2e6. Approving on the understanding that this waits for #286 to merge (after its rebase onto main's variant/boolean rule) and that Dustin's re-setup thread gets an answer; the !enabled guard he suggests looks like the right small fix.
| } | ||
|
|
||
| @Test | ||
| fun `a reset during a v2 flag leg report stops the other legs of that pass`() { |
There was a problem hiding this comment.
nit: this covers a reset() during the /flags report. The /config report goes through the same reportLinkedFlagExposures but runs inside remoteConfigLock. Worth a sibling case driven by loadRemoteConfig, so moving that report out of the lock later (your follow-up) keeps the stop?
💡 Motivation and Context
Session replay v2 trigger groups gate recording on feature flags, but unlike the v1 linked flag they never sent
$feature_flag_calledfor those flags. So a user whose SDK evaluated the group flag didn't count as exposed to an experiment on that flag. android-v4.1.0 (#1001) shipped that on purpose because the spec said so; the spec now says to report them, in PostHog/sdk-specs#286.sdk-specs main still has the old SHALL NOT for flag legs until PostHog/sdk-specs#286 merges, so this PR should merge after it.
Related: PostHog/posthog-ios#1141 makes the same change on iOS.
Changes:
featureFlagsLockwith the recording decision, and reported after the lock.isRecordingActive, so /config, the /flags re-arm, the quota path and the cache preload all report them. React Native stays excluded.sendFeatureFlagEventand the existing per-value dedupe, so a key and value already reported since the last reset sends nothing./flagsresponds, the first report of a launch can carry a cached or bootstrapped value.clear()bumps a generation counter and the report loop stops when it changes, so areset()frombeforeSendduring the report doesn't send the other legs for the previous identity.sendFeatureFlagCallednow returns early while opted out. Before, it marked the flag value as reported even though capture dropped the event, so the first read afteroptIn()sent nothing. This applied to app reads and the v1 linked flag too, and the v2 legs would have made it more common.The preload report doesn't reach
sendFeatureFlagCalledin a real setup, same as v1 today: preload runs in thePostHogRemoteConfigconstructor, beforePostHogsetsremoteConfigHolder, so it returns early at theremoteConfig == nullcheck.Out of scope, existing behaviour:
beforeSendinsideremoteConfigLock. Worth a follow-up to move it out of the lock like the /flags path./flagsresponse that was in flight atreset()is still installed and reported for the new identity, on main too. That needs a request-time generation inexecuteFeatureFlags, so it gets its own PR.💚 How did you test it?
PostHogRemoteConfigTestandPostHogTest: one event per flag leg, dedupe across groups, flags surviving the report, preload, quota, React Native exclusion, and the flag-called switch.a reset during a v2 flag leg report stops the other legs of that passsent 2 events before the generation check and 1 after.optIn()before the fix and 1 after../gradlew :posthog:build spotlessCheck :posthog-android:testWithoutCompose --continuepasses.📝 Checklist
If releasing new changes
pnpm changesetto generate a changeset file🤖 Agent context
DRI: @ioannisj
Autonomy: Human-driven (agent-assisted)