-
Notifications
You must be signed in to change notification settings - Fork 1
fix: cached-render exposure read before identify() (SRM canary) #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| { | ||
| "generated_at": "2026-06-13", | ||
| "generated_at": "2026-06-15", | ||
| "reviews": [ | ||
| { | ||
| "author": "adeline", | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // tests/exposure-ordering.test.mjs | ||
| // Guards the flag-before-identify invariant on the instant-cache render path | ||
| // (spec §3.1.4 / §5.1). The cache path MUST read landing-variant synchronously | ||
| // (so $feature_flag_called lands in the anon_id bucket before the bootstrap's | ||
| // identify(site_uuid)) and MUST NOT use an async onFeatureFlags subscription | ||
| // there — an async read could fire post-identify and record the wrong bucket, | ||
| // breaking the SRM/stickiness canary. | ||
| import test from 'node:test'; | ||
| import assert from 'node:assert/strict'; | ||
| import { readFileSync } from 'node:fs'; | ||
|
|
||
| const loader = readFileSync(new URL('../src/bootstrap/variant-loader.ts', import.meta.url), 'utf8'); | ||
| const bootstrap = readFileSync(new URL('../src/bootstrap/index.tsx', import.meta.url), 'utf8'); | ||
|
|
||
| /** Extract the body of a named function/arrow up to a heuristic close. */ | ||
| function slice(source, from, to) { | ||
| const a = source.indexOf(from); | ||
| const b = source.indexOf(to, a + from.length); | ||
| return a >= 0 && b > a ? source.slice(a, b) : ''; | ||
| } | ||
|
|
||
| test('cache path records exposure synchronously, not via onFeatureFlags', () => { | ||
| // The synchronous exposure helper exists and reads the flag. | ||
| assert.match(loader, /function recordCachedExposure/); | ||
| const helper = slice(loader, 'function recordCachedExposure', '\n}'); | ||
| assert.match(helper, /getFeatureFlag\(FLAG_KEY\)/, 'must read landing-variant (fires $feature_flag_called)'); | ||
| assert.doesNotMatch(helper, /onFeatureFlags/, 'exposure read must be synchronous — no subscription that could fire post-identify'); | ||
| }); | ||
|
|
||
| test('cache branch calls the synchronous helper before returning', () => { | ||
| const cacheBranch = slice(loader, "fast.renderSource === 'cache'", 'return {'); | ||
| assert.match(cacheBranch, /recordCachedExposure\(ph\)/); | ||
| assert.doesNotMatch(cacheBranch, /onFeatureFlags|watchExposure/, 'cache path must not subscribe for exposure'); | ||
| }); | ||
|
|
||
| test('kill-switch read does not pollute exposure and clears cache for next load', () => { | ||
| const helper = slice(loader, 'function recordCachedExposure', '\n}'); | ||
| assert.match(helper, /KILL_SWITCH_KEY,\s*\{\s*send_event:\s*false\s*\}/, 'kill-switch must not fire $feature_flag_called'); | ||
| assert.match(helper, /clearCache\(\)/, 'kill-switch on a cached render clears the cache (takes effect next load)'); | ||
| }); | ||
|
|
||
| test('bootstrap reads the flag (prepareVariant) before identifyConsented', () => { | ||
| // Ordering: prepareVariant (which does the synchronous exposure read on the | ||
| // cache path, and awaits resolveFlag on the cold path) resolves before identify. | ||
| const prepareIdx = bootstrap.indexOf('await prepareVariant'); | ||
| const identifyIdx = bootstrap.indexOf('identifyConsented()'); | ||
| assert.ok(prepareIdx >= 0 && identifyIdx >= 0, 'both calls present'); | ||
| assert.ok(prepareIdx < identifyIdx, 'prepareVariant (flag read) must precede identifyConsented()'); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the ops kill-switch is flipped after a visitor's last load, this cache path only reads the synchronously persisted value, so a stale
falseleaveswcpos_landing_assignmentintact and the user gets at least one extra cached variant render after PostHog refreshes flags in the background. The previous cache-path subscription cleared the assignment when the fresh flag response arrived; keeping exposure synchronous is fine, but the kill-switch still needs a fresh, non-exposure (send_event: false) path so the kill takes effect on the next load as documented.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right — fixed in #30. Added
watchKillSwitch(ph): a backgroundonFeatureFlagswatch that re-reads the kill-switch withsend_event: false(non-exposure, so it's safe after identify — the ops flag is bucket-agnostic) and clears the assignment cache so a freshly-flipped kill takes effect next load. The experiment-flag exposure read stays synchronous + pre-identify, so #29's invariant is intact.