Skip to content

fix: identify FullStory with freshest user metadata to prevent stale overwrite (new-account session unsearchable) - #96849

Open
dilshodmackbook-sketch wants to merge 7 commits into
Expensify:mainfrom
dilshodmackbook-sketch:dilshod/fix-93730-fullstory-identify-race
Open

fix: identify FullStory with freshest user metadata to prevent stale overwrite (new-account session unsearchable)#96849
dilshodmackbook-sketch wants to merge 7 commits into
Expensify:mainfrom
dilshodmackbook-sketch:dilshod/fix-93730-fullstory-identify-race

Conversation

@dilshodmackbook-sketch

@dilshodmackbook-sketch dilshodmackbook-sketch commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Explanation of Change

For a newly-created production account, the FullStory session is tagged with a uid but no email, so searching by email in the FullStory dashboard returns "No matches found".

The root cause is an out-of-order async identification race in consentAndIdentify. USER_METADATA is backend-populated in two stages for a new account — first {accountID}, then {accountID, email} — and each update fires consentAndIdentify, spawning two racing async chains:

  • Chain A ({accountID}, no email): FullStory is not yet initialized, so onReady runs init() and resolves on the ~1s setTimeout fallback.
  • Chain B ({accountID, email}): FullStory is already initialized, so onReady resolves immediately via the OBSERVE callback.

Because each chain captured its own metadata at start time and Chain A resolves last, its FS.identify re-identifies the user with the email-less metadata, clobbering the good identity set by Chain B. This is why the bug is specific to new accounts and reproduces on both web and native.

This PR keeps a module-level latestUserMetadata reference and reads it inside the deferred onReady/getEnvironment callback, so whichever chain resolves last identifies with the freshest, most complete metadata received so far. The same change is applied to both src/libs/Fullstory/index.ts (web) and src/libs/Fullstory/index.native.ts (native). As a bonus, the identify path now builds a fresh object instead of mutating the Onyx-held metadata object.

Fixed Issues

$ #93730
PROPOSAL: #93730 (comment)

Tests

Because the actual bug (session searchable by email in the FullStory dashboard) can only be observed on production/staging with FullStory + Applause dashboard access, the fix itself is verified locally by the unit test, and the app is separately checked for no regression.

1. Unit test (verifies the fix mechanism — runnable by anyone):

npx jest tests/unit/FullstoryConsentAndIdentifyTest.ts
PASS tests/unit/FullstoryConsentAndIdentifyTest.ts
  Fullstory consentAndIdentify (web)
    ✓ identifies with the freshest metadata when a stale email-less chain resolves last
    ✓ identifies with the email when a single update already contains it
    ✓ does not mutate the metadata object held by Onyx
  Fullstory consentAndIdentify (native)
    ✓ identifies with the freshest metadata when a stale email-less chain resolves last
    ✓ identifies with the email when a single update already contains it
Tests:       5 passed, 5 total

The web/native tests reproduce the two-stage ordering race (the email-less chain resolves last) and assert the final identify call still carries properties.email; the remaining tests cover the single-update case and that the Onyx-held metadata object is not mutated.

Unit test passing

2. No regression (manual — see Screenshots/Videos):

  1. Sign in and create a brand-new account.
  2. Complete onboarding and land in the app (Concierge inbox).
  3. Verify the app loads and works normally with no errors in the JS console.
  • Verify that no errors appear in the JS console

Offline tests

N/A — FullStory identification only runs while online, so there is no offline-specific behavior for this change.

QA Steps

  1. Log in with a brand-new account on staging.
  2. Do some actions in the app, then close the tab/app.
  3. Navigate to app.fullstory.com and log in with the QA credentials.
  4. Search → "Email is..", enter the new account's email, Apply filters.
  5. Verify the session is found and is available to watch.
  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

Android: Native
Screen.Recording.2026-07-23.at.15.36.13.mov
Android: mWeb Chrome
93730-mweb-chrome.mp4
iOS: Native
Screen.Recording.2026-07-23.at.15.18.26.mov
iOS: mWeb Safari
93730-mweb-safari.mp4
MacOS: Chrome / Safari
93730-web-chrome.mp4

dilshodmackbook-sketch and others added 2 commits July 23, 2026 11:58
…overwrite

For a newly created account USER_METADATA arrives in two stages (accountID
first, then accountID + email), so consentAndIdentify spawns two racing async
chains. The email-less chain can resolve last (web: onReady init path resolves
on a 1s setTimeout fallback) and re-identify the user without an email, making
the session unsearchable in FullStory.

Keep a module-level latestUserMetadata reference and read it at resolve time,
so whichever chain resolves last identifies with the most complete metadata.
Also stop mutating the Onyx-held metadata object by building a fresh one.

Fixes: Expensify#93730

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Simulate the new-account flow where an email-less identification chain
resolves after the chain that carries the email, and assert the final
identify call still includes the email on both web and native. Also
assert the Onyx-held metadata object is no longer mutated.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…ullstory-identify-race

# Conflicts:
#	src/libs/Fullstory/index.native.ts
dilshodmackbook-sketch added a commit to dilshodmackbook-sketch/App that referenced this pull request Jul 23, 2026
@dilshodmackbook-sketch
dilshodmackbook-sketch marked this pull request as ready for review July 23, 2026 10:48
@dilshodmackbook-sketch
dilshodmackbook-sketch requested review from a team as code owners July 23, 2026 10:48
@melvin-bot
melvin-bot Bot requested review from JmillsExpensify and daledah and removed request for a team July 23, 2026 10:48
@melvin-bot

melvin-bot Bot commented Jul 23, 2026

Copy link
Copy Markdown

@daledah Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@melvin-bot
melvin-bot Bot removed the request for review from a team July 23, 2026 10:48

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 226c20ac34

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/libs/Fullstory/index.ts Outdated
FS.identify(localMetadata);
// Identify with the freshest metadata rather than the value captured when this chain
// started, so an email-less chain that resolves late does not clobber the identity.
FS.identify({...latestUserMetadata, environment: envName});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Re-check eligibility before identifying latest metadata

Because the FullStory eligibility check above still uses the metadata captured when this async chain started, this line can identify a different latestUserMetadata object that would fail the gate itself. If an eligible account starts initialization and the user then switches to an ineligible account (for example an expensify.com/support or non-production non-test user) while the old getEnvironment/onReady promise is pending, the stale callback can consent/restart FullStory and identify the ineligible latest account; use the same fresh metadata for shouldInitialize or drop callbacks whose accountID is no longer current.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch. Fixed in the latest commit. The eligibility gate now reads the same freshest latestUserMetadata snapshot that identify uses, so the gate and the identity can no longer diverge. On web, since identify runs behind a second async hop (onReady), I re-read and re-check eligibility inside onReady — if the current user switched to an ineligible account (support / non-production) while onReady was pending, FS is shut down instead of identifying it. Native gates and identifies from a single synchronous read, so it stays consistent by construction. Added a unit test covering the eligible→ineligible account-switch case.

Comment thread src/libs/Fullstory/index.native.ts Outdated
FS.identify(userMetadata, envName);
// Identify with the freshest metadata rather than the value captured when this chain
// started, so an email-less chain that resolves late does not clobber the identity.
FS.identify(latestUserMetadata, envName);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Re-check eligibility before identifying latest metadata

This identifies the module-level latest metadata even though the shouldInitialize decision was made with the older metadata captured by this callback. During an account switch from an eligible account to an ineligible one (such as an expensify.com account or a non-production non-test account) while the older environment lookup is still pending, this stale callback can call restart/consent, identify the ineligible latest account, and set native tracking enabled; use the same current metadata for the gate or ignore outdated callbacks.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch. Fixed in the latest commit. The eligibility gate now reads the same freshest latestUserMetadata snapshot that identify uses, so the gate and the identity can no longer diverge. On web, since identify runs behind a second async hop (onReady), I re-read and re-check eligibility inside onReady — if the current user switched to an ineligible account (support / non-production) while onReady was pending, FS is shut down instead of identifying it. Native gates and identifies from a single synchronous read, so it stays consistent by construction. Added a unit test covering the eligible→ineligible account-switch case.

Codex review flagged that shouldInitialize was checked against the
metadata captured when the async chain started, while identify used the
module-level latestUserMetadata. On an account switch from an eligible
to an ineligible account mid-flight, a stale chain could pass the gate
yet identify the ineligible account. Gate and identify now read the same
freshest snapshot; web re-checks eligibility inside onReady and shuts FS
down if the current user became ineligible while it was pending.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@codecov

codecov Bot commented Jul 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ Changes either increased or maintained existing code coverage, great job!

Files with missing lines Coverage Δ
src/libs/Fullstory/index.native.ts 38.46% <100.00%> (+38.46%) ⬆️
src/libs/Fullstory/index.ts 45.90% <100.00%> (+45.90%) ⬆️
... and 9 files with indirect coverage changes

Exercise the isInitialized()==true path so the SHUTDOWN call inside the
onReady eligibility recheck is covered (fixes Codecov patch miss).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

@JmillsExpensify JmillsExpensify left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@dilshodmackbook-sketch

Copy link
Copy Markdown
Contributor Author

@daledah friendly bump on this one — it's been open since the 23rd and just waiting on your review. Let me know if anything needs clarifying on my end, happy to walk through the change. Thanks!

@github-actions

Copy link
Copy Markdown
Contributor

🚧 MonilBhavsar has triggered a test Expensify/App build. You can view the workflow run here.

@github-actions

Copy link
Copy Markdown
Contributor

🧪🧪 Use the links below to test this adhoc build on Android, iOS, and Web. Happy testing! 🧪🧪
Built from App PR #96849.

Android 🤖 iOS 🍎
https://ad-hoc-expensify-cash.s3.us-east-1.amazonaws.com/rock-artifacts/ad-hoc/rock-android-Adhoc-3a97b6e9359-fefd261-3911c0dd1090af143f6b2fc18112a87353e13670/index.html ❌ FAILED ❌
Android The QR code can't be generated, because the iOS build failed
Web 🕸️
https://96849.pr-testing.expensify.com
Web

👀 View the workflow run that generated this build 👀

@daledah

daledah commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Requested QA testing here: #93730 (comment)

@daledah

daledah commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

@dilshodmackbook-sketch please merge main

@dilshodmackbook-sketch

Copy link
Copy Markdown
Contributor Author

Done, merged main.

@daledah

daledah commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

@dilshodmackbook-sketch please resolve conflict

Resolve Fullstory conflicts: combine the freshest-metadata race fix
(latestUserMetadata re-read at resolve/onReady time) with main's new
required session param on shouldInitialize/consentAndIdentify. Update
FullstoryConsentAndIdentifyTest to pass the session argument.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@dilshodmackbook-sketch

Copy link
Copy Markdown
Contributor Author

@daledah Done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants