Skip to content

fix(analytics): Resolve feature flags before first render#2656

Merged
charlesvien merged 1 commit into
mainfrom
fix/cloud-mode-flag-race
Jun 13, 2026
Merged

fix(analytics): Resolve feature flags before first render#2656
charlesvien merged 1 commit into
mainfrom
fix/cloud-mode-flag-race

Conversation

@charlesvien

@charlesvien charlesvien commented Jun 13, 2026

Copy link
Copy Markdown
Member

Problem

The "Cloud" option in the workspace-mode picker intermittently vanished on launch and only returned after a hard refresh or restart. It is gated by the twig-cloud-mode-toggle feature flag, but the renderer deferred PostHog initialization behind an async getSessionId IPC round-trip. React mounted first, so useFeatureFlag read the flag as false (flags not loaded yet) and hid the option until init finished and flags arrived. Every useFeatureFlag consumer was exposed to the same race; the || import.meta.env.DEV fallback masked it during local development.

Closes #2598

Changes

  1. Pass the main-owned session id to the renderer synchronously via additionalArguments
  2. Expose it on window.__posthogBootstrap from the preload
  3. Initialize PostHog before React mounts so flags resolve on the first render
  4. Keep AnalyticsBootContribution as an idempotent async fallback when the bridge value is absent
  5. Session stitching is preserved — the renderer still uses the main process session id

How did you test this?

  • pnpm --filter code typecheck (main + renderer tsconfigs) — pass
  • biome check on the changed files — clean (also enforced by the pre-commit hook)
  • pnpm --filter code exec vitest run src/main/platform-adapters/posthog-analytics.test.ts — 7/7 pass
  • pnpm --filter @posthog/ui exec vitest run src/shell/posthogAnalyticsImpl.test.ts — 11/11 pass

Not yet run: an end-to-end check on a production build with twig-cloud-mode-toggle enabled — the flag path is dev-forced on, so the race only manifests in production builds. A genuinely cold flag cache still has a one-time load window until the network flags response arrives, which is expected first-load behavior.

Automatic notifications

  • Publish to changelog?
  • Alert Sales and Marketing teams?

charlesvien commented Jun 13, 2026

Copy link
Copy Markdown
Member Author

@github-actions

github-actions Bot commented Jun 13, 2026

Copy link
Copy Markdown

React Doctor found no issues in the changed files. 🎉

Reviewed by React Doctor for commit b704380.

@charlesvien charlesvien changed the title initialize posthog before first render fix(analytics): Resolve feature flags before first render Jun 13, 2026
@charlesvien charlesvien marked this pull request as ready for review June 13, 2026 05:56
@charlesvien charlesvien added the Stamphog This will request an autostamp by stamphog on small changes label Jun 13, 2026
github-actions[bot]
github-actions Bot previously approved these changes Jun 13, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Low-risk analytics timing fix fully contained within the Electron host layer. Uses correct Electron patterns (contextBridge, additionalArguments), the session ID is not a secret, and the fallback async path remains intact if the sync bridge is absent.

@greptile-apps

greptile-apps Bot commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "initialize posthog before first render" | Re-trigger Greptile

@charlesvien charlesvien force-pushed the fix/cloud-mode-flag-race branch from e29fc66 to a32e3db Compare June 13, 2026 06:01
@github-actions github-actions Bot dismissed their stale review June 13, 2026 06:02

New commits pushed (delta classified non_linear_history) — stamphog approval dismissed; re-review running automatically.

github-actions[bot]
github-actions Bot previously approved these changes Jun 13, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Low-risk analytics timing fix fully contained within the Electron host layer. The synchronous bootstrap path via contextBridge and additionalArguments is correct, the async fallback remains intact when the bootstrap value is absent, and the session ID is not sensitive data.

@charlesvien charlesvien force-pushed the fix/auth-recover-projectless-session branch from 6e2fdfb to 975837d Compare June 13, 2026 06:26
@charlesvien charlesvien force-pushed the fix/cloud-mode-flag-race branch from a32e3db to 110c8db Compare June 13, 2026 06:26
@github-actions github-actions Bot dismissed their stale review June 13, 2026 06:26

New commits pushed (delta classified non_linear_history) — stamphog approval dismissed; re-review running automatically.

github-actions[bot]
github-actions Bot previously approved these changes Jun 13, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Low-risk analytics timing fix fully contained within the Electron host layer. Uses correct Electron patterns (contextBridge, additionalArguments), the async fallback remains intact, and no sensitive data is exposed.

@charlesvien charlesvien force-pushed the fix/cloud-mode-flag-race branch 2 times, most recently from 127a211 to f04da06 Compare June 13, 2026 06:52
@github-actions github-actions Bot dismissed their stale review June 13, 2026 06:52

New commits pushed (delta classified non_linear_history) — stamphog approval dismissed; re-review running automatically.

github-actions[bot]
github-actions Bot previously approved these changes Jun 13, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Low-risk analytics timing fix fully contained in the Electron host layer. The idempotency guard in initializePostHog prevents double initialization, the async fallback is preserved when the sync bridge is absent, and the session ID is a non-sensitive analytics UUID.

Base automatically changed from fix/auth-recover-projectless-session to main June 13, 2026 07:16
@charlesvien charlesvien force-pushed the fix/cloud-mode-flag-race branch from f04da06 to b704380 Compare June 13, 2026 07:17
@github-actions github-actions Bot dismissed their stale review June 13, 2026 07:17

New commits pushed (delta classified non_linear_history) — stamphog approval dismissed; re-review running automatically.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No showstoppers. Low-risk analytics timing fix fully contained within the Electron host layer — correct use of contextBridge and additionalArguments, the async fallback is preserved when the sync bridge is absent, and the session ID is a non-sensitive analytics UUID. Logic paths in main.tsx and app-boot.contributions.ts are consistent (sync path initializes PostHog, async path skips it, no double-init risk).

@charlesvien charlesvien enabled auto-merge (squash) June 13, 2026 07:20
@charlesvien charlesvien merged commit ad099ba into main Jun 13, 2026
24 checks passed
@charlesvien charlesvien deleted the fix/cloud-mode-flag-race branch June 13, 2026 07:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Stamphog This will request an autostamp by stamphog on small changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cloud option intermittently missing from execution environment dropdown

1 participant