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.

perf(frontend): load app context fields the first screen does not read after boot (#128226)

Paul D'Ambra48a1204abf12
diff --git a/frontend/src/lib/components/TaxonomicFilter/taxonomicFilterPinnedPropertiesLogic.test.ts b/frontend/src/lib/components/TaxonomicFilter/taxonomicFilterPinnedPropertiesLogic.test.ts
index dcf3518985c8..f9669aff0b87 100644
--- a/frontend/src/lib/components/TaxonomicFilter/taxonomicFilterPinnedPropertiesLogic.test.ts
+++ b/frontend/src/lib/components/TaxonomicFilter/taxonomicFilterPinnedPropertiesLogic.test.ts
@@ -1,5 +1,7 @@
import posthog from 'posthog-js'
+import { applyFullStageAppContext } from 'scenes/fullStageAppContext'
+
import { initKeaTests } from '~/test/init'
import { AppContext } from '~/types'
@@ -361,6 +363,16 @@ describe('taxonomicFilterPinnedPropertiesLogic', () => {
expect(captureSpy).toHaveBeenCalledWith('taxonomic filter default pins seeded', { values: ['email'] })
})
+ it('tops up email when the full stage app context arrives after the mount', () => {
+ mountWith(true, false)
+ delete window.POSTHOG_APP_CONTEXT?.has_person_email
+ window.POSTHOG_APP_CONTEXT!.current_team = { id: 1 } as AppContext['current_team']
+
+ applyFullStageAppContext({ current_user: {}, current_team: { id: 1 }, has_person_email: true })
+
+ expect(logic.values.pinnedFilters.map((f) => f.value)).toEqual(['$current_url', 'email'])
+ })
+
it('any pin interaction opts the user out of all future seeding', () => {
mountWith(true, false)
logic.actions.togglePin(TaxonomicFilterGroupType.EventProperties, 'Event properties', '$current_url', {
diff --git a/frontend/src/lib/components/TaxonomicFilter/taxonomicFilterPinnedPropertiesLogic.ts b/frontend/src/lib/components/TaxonomicFilter/taxonomicFilterPinnedPropertiesLogic.ts
index ee8b14c968cf..92d79e12bb88 100644
--- a/frontend/src/lib/components/TaxonomicFilter/taxonomicFilterPinnedPropertiesLogic.ts
+++ b/frontend/src/lib/components/TaxonomicFilter/taxonomicFilterPinnedPropertiesLogic.ts
@@ -3,6 +3,7 @@ import posthog from 'posthog-js'
import { getAppContext } from 'lib/utils/getAppContext'
import { permanentlyMount } from 'lib/utils/kea-logic-builders'
+import { onFullStageTeamFieldsApplied } from 'scenes/fullStageAppContext'
import { META_GROUP_TYPES, TaxonomicDefinitionTypes, TaxonomicFilterGroupType, TaxonomicFilterValue } from './types'
@@ -266,7 +267,7 @@ export const taxonomicFilterPinnedPropertiesLogic = kea<taxonomicFilterPinnedPro
})
},
})),
- events(({ actions, values }) => ({
+ events(({ actions, values, cache }) => ({
afterMount: () => {
if (typeof window === 'undefined') {
return
@@ -334,6 +335,9 @@ export const taxonomicFilterPinnedPropertiesLogic = kea<taxonomicFilterPinnedPro
migrateOldQuickFilters()
seedDefaultPinnedFilters()
+ cache.disposables.add(() => onFullStageTeamFieldsApplied(seedDefaultPinnedFilters), 'fullStageSeed', {
+ pauseOnPageHidden: false,
+ })
},
})),
permanentlyMount(),
diff --git a/frontend/src/lib/utils/deferredAppContext.ts b/frontend/src/lib/utils/deferredAppContext.ts
index 862d7d5f3341..04cdadbac2d7 100644
--- a/frontend/src/lib/utils/deferredAppContext.ts
+++ b/frontend/src/lib/utils/deferredAppContext.ts
@@ -24,7 +24,6 @@ const USER_APP_CONTEXT_KEYS = new Set([
'current_project',
'effective_resource_access_control',
'resource_access_control',
- 'frontend_apps',
'default_event_name',
'has_pageview',
'has_screen',
diff --git a/frontend/src/models/membersLogic.tsx b/frontend/src/models/membersLogic.tsx
index 23c3f7a9003b..70569583c990 100644
--- a/frontend/src/models/membersLogic.tsx
+++ b/frontend/src/models/membersLogic.tsx
@@ -30,7 +30,7 @@ function meAsMember(user: UserType): OrganizationMemberType {
last_login: null,
joined_at: '',
updated_at: '',
- is_2fa_enabled: user.is_2fa_enabled,
+ is_2fa_enabled: user.is_2fa_enabled ?? false,
has_social_auth: user.has_social_auth,
}
}
diff --git a/frontend/src/scenes/authentication/two-factor-setup/twoFactorLogic.test.ts b/frontend/src/scenes/authentication/two-factor-setup/twoFactorLogic.test.ts
index 9b916eb0cb84..55f22be171e8 100644
--- a/frontend/src/scenes/authentication/two-factor-setup/twoFactorLogic.test.ts
+++ b/frontend/src/scenes/authentication/two-factor-setup/twoFactorLogic.test.ts
@@ -1,7 +1,12 @@
+import { MOCK_DEFAULT_USER } from 'lib/api.mock'
+
import { expectLogic } from 'kea-test-utils'
+import { userLogic } from 'scenes/userLogic'
+
import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'
+import type { UserType } from '~/types'
import { twoFactorLogic } from './twoFactorLogic'
@@ -98,4 +103,30 @@ describe('twoFactorLogic', () => {
logic.actions.toggleBackupCodesModal(false)
expect(logic.values.generatingCodes).toBeNull()
})
+
+ it.each([
+ ['a user who has 2FA', true, false],
+ ['a user who has no 2FA', false, true],
+ ])(
+ 'waits for the full stage 2FA fields before it forces setup on %s',
+ async (_, is2faEnabled, expectedModalOpen) => {
+ initKeaTests()
+ const bootUser: UserType = {
+ ...MOCK_DEFAULT_USER,
+ organization: { ...MOCK_DEFAULT_USER.organization!, enforce_2fa: true },
+ }
+ delete bootUser.is_2fa_enabled
+ delete bootUser.has_sso_enforcement
+ userLogic.mount()
+ userLogic.actions.loadUserSuccess(bootUser)
+ const logic = twoFactorLogic()
+ logic.mount()
+ expect(logic.values.isTwoFactorSetupModalOpen).toBe(false)
+
+ userLogic.actions.addFullStageUserFields({ is_2fa_enabled: is2faEnabled, has_sso_enforcement: false })
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(logic.values.isTwoFactorSetupModalOpen).toBe(expectedModalOpen)
+ }
+ )
})
diff --git a/frontend/src/scenes/authentication/two-factor-setup/twoFactorLogic.ts b/frontend/src/scenes/authentication/two-factor-setup/twoFactorLogic.ts
index c53627efea8b..195af79cc16b 100644
--- a/frontend/src/scenes/authentication/two-factor-setup/twoFactorLogic.ts
+++ b/frontend/src/scenes/authentication/two-factor-setup/twoFactorLogic.ts
@@ -25,6 +25,17 @@ export interface TwoFactorLogicProps {
onSuccess?: () => void
}
+// The 2FA and SSO fields are absent until the app context's full stage arrives, and absent is not false.
+function mustSetUpTwoFactor(user: UserType | null): boolean {
+ return (
+ !!user?.organization?.enforce_2fa &&
+ user.is_2fa_enabled === false &&
+ user.has_sso_enforcement === false &&
+ !user.is_impersonated &&
+ !user.has_social_auth
+ )
+}
+
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface twoFactorLogicValues {
featureFlags: FeatureFlagsSet // featureFlagLogic
@@ -86,6 +97,19 @@ export interface twoFactorLogicActions {
loadAllMembers: () => {
value: true
} // membersLogic
+ addFullStageUserFieldsSuccess: (
+ user: UserType | null,
+ payload?:
+ | {
+ fields: Partial<UserType>
+ }
+ | undefined
+ ) => {
+ payload?: {
+ fields: Partial<UserType>
+ }
+ user: UserType | null
+ } // userLogic
loadUser: (resetOnFailure?: boolean | undefined) => {
resetOnFailure: boolean | undefined
} // userLogic
@@ -256,7 +280,7 @@ export const twoFactorLogic = kea<twoFactorLogicType>([
props({} as TwoFactorLogicProps),
connect(() => ({
values: [preflightLogic, ['preflight'], featureFlagLogic, ['featureFlags'], userLogic, ['user']],
- actions: [userLogic, ['loadUser'], membersLogic, ['loadAllMembers']],
+ actions: [userLogic, ['loadUser', 'addFullStageUserFieldsSuccess'], membersLogic, ['loadAllMembers']],
})),
actions({
setGeneralError: (code: string, detail: string) => ({ code, detail }),
@@ -401,6 +425,11 @@ export const twoFactorLogic = kea<twoFactorLogicType>([
},
})),
listeners(({ props, actions, values }) => ({
+ addFullStageUserFieldsSuccess: ({ user }) => {
+ if (mustSetUpTwoFactor(user)) {
+ actions.openTwoFactorSetupModal(true)
+ }
+ },
submitTokenSuccess: () => {
lemonToast.success('2FA method added successfully')
actions.loadStatus()
@@ -439,14 +468,7 @@ export const twoFactorLogic = kea<twoFactorLogicType>([
})),
afterMount(({ actions, values }) => {
- if (
- values.user &&
- values.user.organization?.enforce_2fa &&
- !values.user.is_2fa_enabled &&
- !values.user.is_impersonated &&
- !values.user.has_social_auth &&
- !values.user.has_sso_enforcement
- ) {
+ if (mustSetUpTwoFactor(values.user)) {
actions.openTwoFactorSetupModal(true)
}
}),
diff --git a/frontend/src/scenes/bootApp.ts b/frontend/src/scenes/bootApp.ts
index 3786a6e7a68e..e60df3183194 100644
--- a/frontend/src/scenes/bootApp.ts
+++ b/frontend/src/scenes/bootApp.ts
@@ -1,4 +1,5 @@
import { registerToastGetHelp } from 'lib/components/Support/registerToastGetHelp'
+import { loadFullStageAppContext } from 'scenes/fullStageAppContext'
import { registerNotebookLinkDrag } from 'scenes/notebooks/AddToNotebook/registerNotebookLinkDrag'
import { prefetchScene } from 'scenes/prefetchScene'
@@ -35,6 +36,7 @@ export function bootApp(): void {
// toolbar and exporter keep linking out to the support options docs
registerToastGetHelp()
prefetchScene()
+ loadFullStageAppContext()
const idle =
typeof window.requestIdleCallback === 'function'
diff --git a/frontend/src/scenes/fullStageAppContext.test.ts b/frontend/src/scenes/fullStageAppContext.test.ts
new file mode 100644
index 000000000000..89c2f6e691de
--- /dev/null
+++ b/frontend/src/scenes/fullStageAppContext.test.ts
@@ -0,0 +1,40 @@
+import { MOCK_TEAM_ID } from 'lib/api.mock'
+
+import { expectLogic } from 'kea-test-utils'
+
+import { ProductKey } from '~/queries/schema/schema-general'
+import { initKeaTests } from '~/test/init'
+import { ProductIntentType } from '~/types'
+
+import { applyFullStageAppContext } from './fullStageAppContext'
+import { teamLogic } from './teamLogic'
+import { userLogic } from './userLogic'
+
+const FULL_STAGE_INTENTS = [{ product_type: ProductKey.SESSION_REPLAY }] as ProductIntentType[]
+
+describe('applyFullStageAppContext', () => {
+ beforeEach(() => {
+ initKeaTests()
+ })
+
+ it.each([
+ ['the page team', MOCK_TEAM_ID, FULL_STAGE_INTENTS],
+ ['a team the user switched to in another tab', MOCK_TEAM_ID + 1, undefined],
+ ])('merges the full stage fields into the mounted logics for %s', async (_, serverTeamId, expectedIntents) => {
+ delete window.POSTHOG_APP_CONTEXT?.current_team?.product_intents
+ delete window.POSTHOG_APP_CONTEXT?.has_person_email
+ userLogic.mount()
+ teamLogic.mount()
+ await expectLogic(teamLogic).toDispatchActions(['loadCurrentTeamSuccess'])
+
+ applyFullStageAppContext({
+ current_user: { is_2fa_enabled: true, has_sso_enforcement: true },
+ current_team: { id: serverTeamId, product_intents: FULL_STAGE_INTENTS },
+ has_person_email: true,
+ })
+
+ expect(userLogic.values.user).toMatchObject({ is_2fa_enabled: true, has_sso_enforcement: true })
+ expect(teamLogic.values.currentTeam?.product_intents).toEqual(expectedIntents)
+ expect(window.POSTHOG_APP_CONTEXT?.has_person_email).toBe(expectedIntents ? true : undefined)
+ })
+})
diff --git a/frontend/src/scenes/fullStageAppContext.ts b/frontend/src/scenes/fullStageAppContext.ts
new file mode 100644
index 000000000000..c1f940c18678
--- /dev/null
+++ b/frontend/src/scenes/fullStageAppContext.ts
@@ -0,0 +1,72 @@
+import posthog from 'posthog-js'
+
+import { retryWithBackoff } from 'lib/utils/async'
+import { getAppContext } from 'lib/utils/getAppContext'
+import { teamLogic } from 'scenes/teamLogic'
+import { userLogic } from 'scenes/userLogic'
+
+import type { ProjectType, TeamType, UserType } from '~/types'
+
+// pinned: the fields `full_stage_user_app_context` in posthog/utils.py returns
+export interface FullStageAppContext {
+ current_user: Partial<UserType>
+ current_team?: Partial<TeamType>
+ current_project?: Partial<ProjectType>
+ has_person_email?: boolean
+}
+
+const teamFieldsListeners = new Set<() => void>()
+
+/** For a logic that reads the full stage team fields from the app context once, on mount. Returns the unsubscribe. */
+export function onFullStageTeamFieldsApplied(listener: () => void): () => void {
+ teamFieldsListeners.add(listener)
+ return () => teamFieldsListeners.delete(listener)
+}
+
+export function applyFullStageAppContext(fullStage: FullStageAppContext): void {
+ const context = getAppContext()
+ if (!context) {
+ return
+ }
+ // Merge in place too, so a logic that mounts after this reads the fields from the app context.
+ if (context.current_user) {
+ Object.assign(context.current_user, fullStage.current_user)
+ }
+ userLogic.findMounted()?.actions.addFullStageUserFields(fullStage.current_user)
+
+ // The server sends the user's current team, which can differ from the page's team after a switch in another tab.
+ const fullStageTeam = fullStage.current_team
+ const teamId = fullStageTeam?.id
+ if (fullStageTeam && teamId !== undefined && context.current_team?.id === teamId) {
+ Object.assign(context.current_team, fullStageTeam)
+ if (context.current_project) {
+ Object.assign(context.current_project, fullStage.current_project)
+ }
+ context.has_person_email = fullStage.has_person_email ?? false
+ const mountedTeamLogic = teamLogic.findMounted()
+ if (mountedTeamLogic?.values.currentTeam?.id === teamId) {
+ mountedTeamLogic.actions.addFullStageTeamFields(fullStageTeam)
+ }
+ teamFieldsListeners.forEach((listener) => listener())
+ }
+}
+
+/**
+ * A deferred page boots without the fields the first screen does not read. This fetches them after the boot.
+ */
+export function loadFullStageAppContext(): void {
+ if (!window.POSTHOG_APP_CONTEXT_REQUEST) {
+ return
+ }
+ const teamId = getAppContext()?.current_team?.id
+ const projectPrefix = teamId ? `/project/${teamId}` : ''
+ void retryWithBackoff(async () => {
+ const response = await fetch(`${projectPrefix}/_app_context?stage=full`, { credentials: 'same-origin' })
+ if (!response.ok) {
+ throw new Error(`The full stage app context request failed with status ${response.status}`)
+ }
+ return (await response.json()) as { app_context: FullStageAppContext }
+ })
+ .then((body) => applyFullStageAppContext(body.app_context))
+ .catch((error) => posthog.captureException(error))
+}
diff --git a/frontend/src/scenes/teamLogic.tsx b/frontend/src/scenes/teamLogic.tsx
index 33899514937a..f1bdf9d5a599 100644
--- a/frontend/src/scenes/teamLogic.tsx
+++ b/frontend/src/scenes/teamLogic.tsx
@@ -165,6 +165,21 @@ export interface teamLogicActions {
destination: string | undefined
teamId: number | string
} // userLogic
+ addFullStageTeamFields: (fields: Partial<TeamType>) => Partial<TeamType>
+ addFullStageTeamFieldsFailure: (
+ error: string,
+ errorObject?: any
+ ) => {
+ error: string
+ errorObject?: any
+ }
+ addFullStageTeamFieldsSuccess: (
+ currentTeam: TeamPublicType | TeamType | null,
+ payload?: Partial<TeamType>
+ ) => {
+ currentTeam: TeamPublicType | TeamType | null
+ payload?: Partial<TeamType>
+ }
addProductIntent: (properties: ProductIntentProperties) => ProductIntentProperties
addProductIntentFailure: (
error: string,
@@ -412,6 +427,9 @@ export const teamLogic = kea<teamLogicType>([
return values.currentTeam
}
},
+ // No listeners, so the late fields do not rerun what a team load triggers.
+ addFullStageTeamFields: (fields: Partial<TeamType>): TeamType | TeamPublicType | null =>
+ values.currentTeam ? ({ ...values.currentTeam, ...fields } as TeamType | TeamPublicType) : null,
refreshCurrentTeam: async () => {
const projectId = values.currentTeam?.project_id
if (!projectId) {
diff --git a/frontend/src/scenes/userLogic.ts b/frontend/src/scenes/userLogic.ts
index ee4c9fc2e709..977fec0ba444 100644
--- a/frontend/src/scenes/userLogic.ts
+++ b/frontend/src/scenes/userLogic.ts
@@ -102,6 +102,27 @@ export interface userLogicValues {
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface userLogicActions {
+ addFullStageUserFields: (fields: Partial<UserType>) => {
+ fields: Partial<UserType>
+ }
+ addFullStageUserFieldsFailure: (
+ error: string,
+ errorObject?: any
+ ) => {
+ error: string
+ errorObject?: any
+ }
+ addFullStageUserFieldsSuccess: (
+ user: UserType | null,
+ payload?: {
+ fields: Partial<UserType>
+ }
+ ) => {
+ user: UserType | null
+ payload?: {
+ fields: Partial<UserType>
+ }
+ }
cancelEmailChangeRequest: () => {
value: true
}
@@ -439,6 +460,7 @@ export const userLogic = kea<userLogicType>([
path(['scenes', 'userLogic']),
actions(() => ({
loadUser: (resetOnFailure?: boolean) => ({ resetOnFailure }),
+ addFullStageUserFields: (fields: Partial<UserType>) => ({ fields }),
updateCurrentOrganization: (organizationId: string, destination?: string) => ({ organizationId, destination }),
updateCurrentOrganizationFailure: true,
logout: (preserveLocation = false, nextUrl?: string, reason?: string) => ({
@@ -530,6 +552,9 @@ export const userLogic = kea<userLogicType>([
}
return null
},
+ // No listeners, so the late fields do not rerun what a user load triggers.
+ addFullStageUserFields: ({ fields }): UserType | null =>
+ values.user ? { ...values.user, ...fields } : null,
updateUser: async ({ user, successCallback, failureCallback }): Promise<UserType> => {
const previousUpdate = cache.pendingUserUpdate as Promise<void> | undefined
let completeUpdate: () => void = () => {}
diff --git a/frontend/src/types.ts b/frontend/src/types.ts
index 66e22352da14..af0036bc38ed 100644
--- a/frontend/src/types.ts
+++ b/frontend/src/types.ts
@@ -1,4 +1,3 @@
-import { LogicWrapper } from 'kea'
import type { PostHog, PropertyMatchType, SupportedWebVitalsMetrics } from 'posthog-js'
import { LogLevel } from 'posthog-js/rrweb-plugin-console-record'
import { eventWithTime } from 'posthog-js/rrweb-types'
@@ -424,9 +423,11 @@ export interface UserType extends UserBaseType {
realm?: Realm
is_email_verified?: boolean | null
pending_email?: string | null
- is_2fa_enabled: boolean
+ /** Absent until the app context's full stage arrives. */
+ is_2fa_enabled?: boolean
has_social_auth: boolean
- has_sso_enforcement: boolean
+ /** Absent until the app context's full stage arrives. */
+ has_sso_enforcement?: boolean
shortcut_position: UserShortcutPosition
has_seen_product_intro_for?: Record<string, boolean>
hide_mcp_hints?: boolean
@@ -2966,27 +2967,6 @@ export interface PluginType {
export type AppType = PluginType
-/** Config passed to app component and logic as props. Sent in Django's app context */
-export interface FrontendAppConfig {
- pluginId: number
- pluginConfigId: number
- pluginType: PluginInstallationType | null
- name: string
- url: string
- config: Record<string, any>
-}
-
-/** Frontend app created after receiving a bundle via import('').getFrontendApp() */
-export interface FrontendApp {
- id: number
- pluginId: number
- error?: any
- title?: string
- logic?: LogicWrapper
- component?: (props: FrontendAppConfig) => JSX.Element
- onInit?: (props: FrontendAppConfig) => void
-}
-
export interface JobPayloadFieldOptions {
type: 'string' | 'boolean' | 'json' | 'number' | 'date' | 'daterange'
title?: string
@@ -5290,7 +5270,8 @@ export interface AppContext {
default_event_name: string | null
has_pageview: boolean
has_screen: boolean
- has_person_email: boolean
+ /** Absent until the app context's full stage arrives. */
+ has_person_email?: boolean
/**
* Flags the server bootstraps as enabled (a list of keys). Storybook may instead pass a
* record to pin specific multivariate variants (e.g. an experiment arm) for a story.
@@ -5299,7 +5280,6 @@ export interface AppContext {
/** Whether the server's access gate gives this user PostHog AI 2.0. Absent when no user is signed in. */
posthog_ai_access?: boolean
anonymous: boolean
- frontend_apps?: Record<number, FrontendAppConfig>
effective_resource_access_control: Record<AccessControlResourceType, AccessControlLevel>
resource_access_control: Record<AccessControlResourceType, AccessControlLevel>
commit_sha?: string
diff --git a/posthog/frontend_views.py b/posthog/frontend_views.py
index 78d637dfaf9d..a691dd18e041 100644
--- a/posthog/frontend_views.py
+++ b/posthog/frontend_views.py
@@ -13,7 +13,7 @@ from posthog.dataclasses import frozen
from posthog.models.instance_setting import get_instance_setting
from posthog.models.user import User
from posthog.templatetags.posthog_filters import TolerantJSONEncoder
-from posthog.utils import add_user_app_context, feature_flag_bootstrap, render_template
+from posthog.utils import add_user_app_context, feature_flag_bootstrap, full_stage_user_app_context, render_template
from posthog.views import login_required
APP_POSTHOG_HOST = "app.posthog.com"
@@ -177,17 +177,24 @@ def home_with_region_redirect(request: HttpRequest, *args: Any, **kwargs: Any) -
def app_context(request: HttpRequest) -> JsonResponse:
- """The signed-in user's part of the app context, for an app page rendered without it."""
+ """The signed-in user's part of the app context, for an app page rendered without it.
+
+ The `boot` stage, the default, has what the first screen reads. The page boots on it, then fetches the `full`
+ stage for the rest."""
if not request.user.is_authenticated:
return JsonResponse({"detail": "Authentication credentials were not provided."}, status=401)
- posthog_app_context: dict[str, Any] = {}
- add_user_app_context(request, posthog_app_context)
- response = JsonResponse(
- {
+ stage = request.GET.get("stage", "boot")
+ if stage == "full":
+ body: dict[str, Any] = {"app_context": full_stage_user_app_context(request)}
+ elif stage == "boot":
+ posthog_app_context: dict[str, Any] = {}
+ add_user_app_context(request, posthog_app_context, stage="boot")
+ body = {
"app_context": posthog_app_context,
"feature_flags": feature_flag_bootstrap(cast(User, request.user), posthog_app_context["current_user"]),
- },
- encoder=TolerantJSONEncoder,
- )
+ }
+ else:
+ return JsonResponse({"detail": "stage must be boot or full."}, status=400)
+ response = JsonResponse(body, encoder=TolerantJSONEncoder)
patch_cache_control(response, no_store=True)
return response
diff --git a/posthog/test/test_get_context_for_template.py b/posthog/test/test_get_context_for_template.py
index b3397e45a69e..fa76880bbc81 100644
--- a/posthog/test/test_get_context_for_template.py
+++ b/posthog/test/test_get_context_for_template.py
@@ -429,16 +429,23 @@ class TestGetContextForTemplate(APIBaseTest):
request.user = self.user
return request
+ self.user.shortcut_position = "below"
+ self.user.save()
inline = get_context_for_template("index.html", user_request("/?deferred_app_context=0"))
deferred = get_context_for_template("index.html", user_request("/?deferred_app_context=1"))
fetched = json.loads(app_context(user_request("/_app_context")).content)
+ fetched_full = json.loads(app_context(user_request("/_app_context?stage=full")).content)
assert deferred["posthog_app_context"]["deferred"] is True
assert deferred["posthog_app_context"]["current_user"] is None
assert "featureFlags" not in deferred["posthog_bootstrap"]
assert deferred["posthog_app_context"].pop("deferred_app_context_variant") == "test"
assert inline["posthog_app_context"].pop("deferred_app_context_variant") == "control"
+ assert "product_intents" not in fetched["app_context"]["current_team"]
+ assert "product_intents" not in fetched["app_context"]["current_project"]
page_with_fetched = {**deferred["posthog_app_context"], **fetched["app_context"]}
+ for key, value in fetched_full["app_context"].items():
+ page_with_fetched[key] = {**page_with_fetched[key], **value} if isinstance(value, dict) else value
page_with_fetched.pop("deferred")
assert page_with_fetched == json.loads(json.dumps(inline["posthog_app_context"], cls=TolerantJSONEncoder))
assert fetched["feature_flags"] == inline["posthog_bootstrap"]["featureFlags"]
diff --git a/posthog/utils.py b/posthog/utils.py
index dbf551fd070e..92c96daf3e09 100644
--- a/posthog/utils.py
+++ b/posthog/utils.py
@@ -19,7 +19,7 @@ from contextlib import contextmanager, suppress
from enum import Enum
from functools import lru_cache, wraps
from operator import itemgetter
-from typing import TYPE_CHECKING, Any, Optional, Union, cast
+from typing import TYPE_CHECKING, Any, Literal, Optional, Union, cast
from urllib.parse import unquote, urljoin, urlparse
from zoneinfo import ZoneInfo
@@ -80,6 +80,8 @@ TEMPLATE_CONTEXT_DURATION_HISTOGRAM = Histogram(
if TYPE_CHECKING:
from django.contrib.auth.models import AbstractBaseUser, AnonymousUser
+ from rest_framework.serializers import Serializer
+
from posthog.models import Organization, Team, User
from products.dashboards.backend.models.dashboard import Dashboard
@@ -890,7 +892,33 @@ def feature_flag_bootstrap(user: "User", current_user: dict[str, Any]) -> Option
)
-def add_user_app_context(request: HttpRequest, posthog_app_context: dict[str, Any]) -> None:
+# Fields the first screen does not read. A deferred page fetches them with `stage=full` after the app boots.
+FULL_STAGE_USER_FIELDS = frozenset({"is_2fa_enabled", "has_sso_enforcement"})
+FULL_STAGE_TEAM_FIELDS = frozenset({"product_intents"})
+FULL_STAGE_PROJECT_FIELDS = FULL_STAGE_TEAM_FIELDS
+
+AppContextStage = Literal["inline", "boot", "full"]
+
+
+def _serialize_app_context_stage(
+ serializer: "Serializer", full_stage_fields: frozenset[str], stage: AppContextStage
+) -> dict[str, Any]:
+ if stage == "inline":
+ return serializer.data
+
+ def in_stage(name: str) -> bool:
+ return (name in full_stage_fields) == (stage == "full")
+
+ for name in list(serializer.fields):
+ if not in_stage(name):
+ serializer.fields.pop(name)
+ # A `to_representation` hook can add a field back, such as the `shortcut_position` backfill, so filter the output too.
+ return {name: value for name, value in serializer.data.items() if in_stage(name)}
+
+
+def add_user_app_context(
+ request: HttpRequest, posthog_app_context: dict[str, Any], stage: Literal["inline", "boot"] = "inline"
+) -> None:
"""Adds the signed-in user's user, team, project and access data to the app context."""
from posthog.api.project import ProjectSerializer
from posthog.api.team.team_serializer import TeamSerializer
@@ -936,7 +964,9 @@ def add_user_app_context(request: HttpRequest, posthog_app_context: dict[str, An
},
many=False,
)
- posthog_app_context["current_user"] = user_serialized.data
+ posthog_app_context["current_user"] = _serialize_app_context_stage(
+ user_serialized, FULL_STAGE_USER_FIELDS, stage
+ )
if user.team:
with tracer.start_as_current_span("template.team_serializer"):
@@ -949,7 +979,9 @@ def add_user_app_context(request: HttpRequest, posthog_app_context: dict[str, An
},
many=False,
)
- posthog_app_context["current_team"] = team_serialized.data
+ posthog_app_context["current_team"] = _serialize_app_context_stage(
+ team_serialized, FULL_STAGE_TEAM_FIELDS, stage
+ )
with tracer.start_as_current_span("template.project_serializer"):
project_serialized = ProjectSerializer(
@@ -957,13 +989,15 @@ def add_user_app_context(request: HttpRequest, posthog_app_context: dict[str, An
context={"request": request, "user_permissions": user_permissions},
many=False,
)
- posthog_app_context["current_project"] = project_serialized.data
- posthog_app_context["frontend_apps"] = get_frontend_apps(user.team.pk)
+ posthog_app_context["current_project"] = _serialize_app_context_stage(
+ project_serialized, FULL_STAGE_PROJECT_FIELDS, stage
+ )
event_info = get_default_event_info(user.team)
posthog_app_context["default_event_name"] = event_info["default_event_name"]
posthog_app_context["has_pageview"] = event_info["has_pageview"]
posthog_app_context["has_screen"] = event_info["has_screen"]
- posthog_app_context["has_person_email"] = get_has_person_email(user.team)
+ if stage == "inline":
+ posthog_app_context["has_person_email"] = get_has_person_email(user.team)
with tracer.start_as_current_span("template.posthog_ai_access"):
try:
@@ -978,6 +1012,35 @@ def add_user_app_context(request: HttpRequest, posthog_app_context: dict[str, An
posthog_app_context["homepage"] = (home_settings.homepage or None) if home_settings else None
+def full_stage_user_app_context(request: HttpRequest) -> dict[str, Any]:
+ """The app context fields that `add_user_app_context` leaves out of the boot stage."""
+ from posthog.api.project import ProjectSerializer
+ from posthog.api.team.team_serializer import TeamSerializer
+ from posthog.api.user import UserSerializer
+
+ from products.access_control.backend.facade.user_access_control import UserAccessControl
+
+ user = cast("User", request.user)
+ full_stage: dict[str, Any] = {
+ "current_user": _serialize_app_context_stage(
+ UserSerializer(user, context={"request": request}), FULL_STAGE_USER_FIELDS, "full"
+ )
+ }
+ if user.team and UserAccessControl(user=user, team=user.team).has_project_access:
+ # The id lets the page skip fields for a team it no longer shows.
+ full_stage["current_team"] = {
+ "id": user.team.id,
+ **_serialize_app_context_stage(
+ TeamSerializer(user.team, context={"request": request}), FULL_STAGE_TEAM_FIELDS, "full"
+ ),
+ }
+ full_stage["current_project"] = _serialize_app_context_stage(
+ ProjectSerializer(user.team.project, context={"request": request}), FULL_STAGE_PROJECT_FIELDS, "full"
+ )
+ full_stage["has_person_email"] = get_has_person_email(user.team)
+ return full_stage
+
+
def render_template(
template_name: str,
request: HttpRequest,
@@ -1270,45 +1333,6 @@ def invalidate_has_person_email_cache(project_id: int) -> None:
safe_cache_delete(_has_person_email_cache_key(project_id))
[email protected]_as_current_span("template.frontend_apps")
-def get_frontend_apps(team_id: int) -> dict[int, dict[str, Any]]:
- from products.cdp.backend.models.plugin import Plugin, PluginSourceFile, get_secret_fields
-
- plugin_configs = (
- Plugin.objects.filter(pluginconfig__team_id=team_id, pluginconfig__enabled=True)
- .filter(
- pluginsourcefile__status=PluginSourceFile.Status.TRANSPILED,
- pluginsourcefile__filename="frontend.tsx",
- )
- .values(
- "pluginconfig__id",
- "pluginconfig__config",
- "config_schema",
- "id",
- "plugin_type",
- "name",
- )
- .all()
- )
-
- frontend_apps = {}
- for p in plugin_configs:
- config = p["pluginconfig__config"] or {}
- for key in get_secret_fields(p["config_schema"] or {}):
- if key in config:
- config[key] = "** SECRET FIELD **"
- frontend_apps[p["pluginconfig__id"]] = {
- "pluginConfigId": p["pluginconfig__id"],
- "pluginId": p["id"],
- "pluginType": p["plugin_type"],
- "name": p["name"],
- "url": f"/app/{p['pluginconfig__id']}/",
- "config": config,
- }
-
- return frontend_apps
-
-
def friendly_time(seconds: float):
minutes, seconds = divmod(seconds, 60.0)
hours, minutes = divmod(minutes, 60.0)
diff --git a/products/tasks/backend/access.py b/products/tasks/backend/access.py
index 0bbf05600f41..1242ddb834b0 100644
--- a/products/tasks/backend/access.py
+++ b/products/tasks/backend/access.py
@@ -1,4 +1,5 @@
import json
+import hashlib
from datetime import UTC, datetime
from enum import StrEnum
from typing import TYPE_CHECKING
@@ -215,7 +216,13 @@ _POSTHOG_AI_DECISION_VALUES = frozenset(decision.value for decision in PostHogAI
POSTHOG_AI_GRANDFATHERED_FLAGS = ("phai-sandbox-mode", "tasks")
POSTHOG_AI_BETA_FLAG = "phai-2-beta"
_POSTHOG_AI_FUNDING_CACHE_SECONDS = 300
-_POSTHOG_AI_DECISION_CACHE_KEY = "posthog_ai_access_decision:{organization_id}:{user_id}"
+_POSTHOG_AI_DECISION_CACHE_KEY = "posthog_ai_access_decision:{organization_id}:{user_id}:{flags_fingerprint}"
+# Most page loads come more than a minute after the user's previous one, so a short cache misses on nearly every
+# load, and each miss waits on a remote flag lookup. An edit to a deciding flag changes the cache key, so only other
+# inputs, such as billing or the trust score, can make the UI lag an access change, by up to this long.
+_POSTHOG_AI_PAGE_RENDER_CACHE_SECONDS = 24 * 60 * 60
+# Before the flag definitions load, a flag edit cannot change the cache key, so the cache stays short.
+_POSTHOG_AI_UNLOADED_DEFINITIONS_CACHE_SECONDS = 60
@frozen
@@ -306,12 +313,27 @@ def get_posthog_ai_access_decision(user: User, organization: "Organization") ->
return PostHogAIAccessDecision.ALLOWED
+def _deciding_flags_fingerprint() -> str | None:
+ all_definitions = posthoganalytics.feature_flag_definitions()
+ if all_definitions is None:
+ return None
+ deciding_keys = {*POSTHOG_AI_GRANDFATHERED_FLAGS, POSTHOG_AI_BETA_FLAG}
+ definitions = [definition for definition in all_definitions if definition.get("key") in deciding_keys]
+ serialized = json.dumps(sorted(definitions, key=lambda definition: definition["key"]), sort_keys=True, default=str)
+ return hashlib.sha256(serialized.encode()).hexdigest()[:16]
+
+
def get_cached_posthog_ai_access_decision(
- user: User, organization: "Organization", *, cache_seconds: int = 60
+ user: User, organization: "Organization", *, cache_seconds: int = _POSTHOG_AI_PAGE_RENDER_CACHE_SECONDS
) -> PostHogAIAccessDecision:
"""For page renders, so a load does not wait on remote flag lookups. Enforcement points call the uncached
decision. An unresolved decision is never cached."""
- cache_key = _POSTHOG_AI_DECISION_CACHE_KEY.format(organization_id=organization.id, user_id=user.pk)
+ flags_fingerprint = _deciding_flags_fingerprint()
+ if flags_fingerprint is None:
+ cache_seconds = min(cache_seconds, _POSTHOG_AI_UNLOADED_DEFINITIONS_CACHE_SECONDS)
+ cache_key = _POSTHOG_AI_DECISION_CACHE_KEY.format(
+ organization_id=organization.id, user_id=user.pk, flags_fingerprint=flags_fingerprint
+ )
try:
cached = cache.get(cache_key)
except Exception:
diff --git a/products/tasks/backend/tests/test_posthog_ai_access.py b/products/tasks/backend/tests/test_posthog_ai_access.py
index e065f5ea00a0..bab8a650ceb2 100644
--- a/products/tasks/backend/tests/test_posthog_ai_access.py
+++ b/products/tasks/backend/tests/test_posthog_ai_access.py
@@ -1,7 +1,8 @@
import json
-from datetime import UTC, datetime
+from datetime import UTC, datetime, timedelta
from typing import Any
+import time_machine
from unittest.mock import patch
from django.core.cache import cache
@@ -173,10 +174,95 @@ class TestPostHogAIAccessDecision(SimpleTestCase):
@parameterized.expand(
[
- ("resolved_decision_is_reused", False, RULES, True, PostHogAIAccessDecision.ALLOWED, 1),
- ("beta_without_rules_is_not_cached", False, None, True, PostHogAIAccessDecision.UNRESOLVED, 2),
- ("failed_override_lookup_is_not_cached", None, RULES, True, PostHogAIAccessDecision.UNRESOLVED, 2),
- ("failed_beta_lookup_is_not_cached", False, RULES, False, PostHogAIAccessDecision.UNRESOLVED, 2),
+ ("resolved_decision_is_reused", False, RULES, True, 30, None, True, PostHogAIAccessDecision.ALLOWED, 1),
+ (
+ "resolved_decision_expires",
+ False,
+ RULES,
+ True,
+ 24 * 60 + 1,
+ None,
+ True,
+ PostHogAIAccessDecision.ALLOWED,
+ 2,
+ ),
+ (
+ "deciding_flag_edit_busts_it",
+ False,
+ RULES,
+ True,
+ 30,
+ "phai-2-beta",
+ True,
+ PostHogAIAccessDecision.ALLOWED,
+ 2,
+ ),
+ (
+ "other_flag_edit_keeps_it",
+ False,
+ RULES,
+ True,
+ 30,
+ "other-flag",
+ True,
+ PostHogAIAccessDecision.ALLOWED,
+ 1,
+ ),
+ (
+ "unloaded_definitions_reused_briefly",
+ False,
+ RULES,
+ True,
+ 0,
+ None,
+ False,
+ PostHogAIAccessDecision.ALLOWED,
+ 1,
+ ),
+ (
+ "unloaded_definitions_expire_fast",
+ False,
+ RULES,
+ True,
+ 2,
+ None,
+ False,
+ PostHogAIAccessDecision.ALLOWED,
+ 2,
+ ),
+ (
+ "beta_without_rules_is_not_cached",
+ False,
+ None,
+ True,
+ 30,
+ None,
+ True,
+ PostHogAIAccessDecision.UNRESOLVED,
+ 2,
+ ),
+ (
+ "failed_override_lookup_not_cached",
+ None,
+ RULES,
+ True,
+ 30,
+ None,
+ True,
+ PostHogAIAccessDecision.UNRESOLVED,
+ 2,
+ ),
+ (
+ "failed_beta_lookup_not_cached",
+ False,
+ RULES,
+ False,
+ 30,
+ None,
+ True,
+ PostHogAIAccessDecision.UNRESOLVED,
+ 2,
+ ),
]
)
def test_cached_decision(
@@ -185,6 +271,9 @@ class TestPostHogAIAccessDecision(SimpleTestCase):
override_value: bool | None,
rules: dict[str, Any] | None,
beta_resolves: bool,
+ minutes_between_loads: int,
+ edited_flag: str | None,
+ definitions_loaded: bool,
expected: PostHogAIAccessDecision,
expected_evaluations: int,
) -> None:
@@ -198,10 +287,18 @@ class TestPostHogAIAccessDecision(SimpleTestCase):
if beta_resolves
else None
)
+ definitions = [
+ {"key": key, "active": True, "filters": {"groups": [{"properties": [], "rollout_percentage": 0}]}}
+ for key in ("phai-2-beta", "other-flag")
+ ]
with (
patch("products.tasks.backend.access.get_feature_flag_or_none", return_value=override_value),
patch("products.tasks.backend.access.posthoganalytics.get_feature_flag_result", return_value=beta),
+ patch(
+ "products.tasks.backend.access.posthoganalytics.feature_flag_definitions",
+ return_value=definitions if definitions_loaded else None,
+ ),
patch(
"products.tasks.backend.access._get_funding_status",
return_value=OrganizationFundingStatus(
@@ -211,8 +308,13 @@ class TestPostHogAIAccessDecision(SimpleTestCase):
patch(
"products.tasks.backend.access.get_posthog_ai_access_decision", wraps=get_posthog_ai_access_decision
) as mock_decision,
+ time_machine.travel(datetime(2026, 11, 3, 12, 0, tzinfo=UTC), tick=False) as traveller,
):
first = get_cached_posthog_ai_access_decision(user, organization)
+ traveller.shift(timedelta(minutes=minutes_between_loads))
+ for definition in definitions:
+ if definition["key"] == edited_flag:
+ definition["filters"] = {"groups": [{"properties": [], "rollout_percentage": 100}]}
second = get_cached_posthog_ai_access_decision(user, organization)
self.assertEqual((first, second), (expected, expected))