App icon indicating copy to clipboard operation
App copied to clipboard

fix: keep Concierge side panel question + thinking indicator visible across clock-skew race

Open David-2002065 opened this issue 5 months ago • 22 comments

Explanation of Change

When a user with prior Concierge history asks Concierge from search, clock skew could make the exchange disappear from the side panel in two ways:

  1. The just-sent question and "Concierge is thinking…" briefly show, then vanish and the panel collapses to "Hi there, how can I help?" — the question only reappears after clicking Show history.
  2. Concierge's reply sometimes never shows at all, even after it has arrived ("sometimes no response from Concierge").

Root cause: the Concierge "hidden history" view decides whether an action belongs to the current session by comparing created timestamps against sessionStartTime. But those timestamps come from different clocks:

  • sessionStartTime is stamped with the raw client clock (DateUtils.getDBTime()).
  • The outgoing optimistic question is stamped with the skew-adjusted client clock (getDBTimeWithSkew), and its created is never rewritten when AddComment succeeds.
  • Concierge's reply carries an authoritative server timestamp.

So when the client clock runs ahead of the server (non-zero networkTimeSkew), the user's own question can get created < sessionStartTime, and the reply's server created can land below both sessionStartTime and firstUserMessageCreated. Either way the action falls outside the session and is filtered out — the question flickers away and the reply may never appear. This poisons the four session checks: hasUserSentMessage, hasSessionActivity, firstUserMessageCreated, and isCurrentSessionAction.

Fix: stop relying on cross-clock timestamp comparisons to decide session membership. Membership is now captured by arrival in a new useCurrentSessionActionIDs hook, from clock-independent facts:

  • The current user's own messages are captured while optimistic (pendingAction === ADD) and kept for the rest of the session, so a skewed question keeps counting even after its pendingAction clears.
  • Once the user has sent a message this session, any newly-arriving action is captured as the reply to it, gated by a server-time floor over the history already loaded at session start so late-loading older history can't leak in.

A shared isCurrentUserPendingAddAction predicate is added in @libs/ReportActionsUtils, and both it and the captured session IDs are ORed into the four session checks, so the fix is applied consistently. The existing created >= sessionStartTime branch is kept, so when the clocks are in sync the behavior is unchanged.

Fixed Issues

$ https://github.com/Expensify/App/issues/90974 PROPOSAL: https://github.com/Expensify/App/issues/90974#issuecomment-4478600885

Tests

Precondition: the test account has a previous chat history with Concierge.

  1. Open the app and navigate to any conversation or workspace chat
  2. Click the search icon in the top right corner
  3. Type a query intended for Concierge (e.g. "How to connect QBD")
  4. Click "Ask Concierge" shown below the search input results
  5. Verify the side panel shows the just-sent question with "Concierge is thinking…" underneath it
  6. Wait until the reply arrives and verify the question + reply stay visible (the panel does NOT collapse to "Hi there, how can I help?")
  7. Verify prior Concierge history remains hidden until "Show history" is clicked
  8. Click "Show history" and verify the prior history appears together with the current question and reply
  • [x] Verify that no errors appear in the JS console

Offline tests

  1. Go offline
  2. Ask Concierge from search as in the Tests section
  3. Verify the question and "Concierge is thinking…" stay visible and the panel does not collapse to the welcome greeting
  4. Go back online and verify the message syncs and the reply arrives, with the view unchanged (no flicker, prior history still hidden until "Show history")

QA Steps

Same as Tests.

  • [x] Verify that no errors appear in the JS console

PR Author Checklist

  • [x] I linked the correct issue in the ### Fixed Issues section above
  • [x] I wrote clear testing steps that cover the changes made in this PR
    • [x] I added steps for local testing in the Tests section
    • [x] I added steps for the expected offline behavior in the Offline steps section
    • [x] I added steps for Staging and/or Production testing in the QA steps section
    • [x] I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • [x] 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)
    • [x] 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).
  • [x] I included screenshots or videos for tests on all platforms
  • [x] I ran the tests on all platforms & verified they passed on:
    • [x] Android: Native
    • [x] Android: mWeb Chrome
    • [x] iOS: Native
    • [x] iOS: mWeb Safari
    • [x] MacOS: Chrome / Safari
  • [x] 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)
  • [x] I followed proper code patterns (see Reviewing the code)
    • [x] I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • [x] I verified that comments were added to code that is not self explanatory
    • [x] 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.
    • [x] 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)
  • [x] If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • [x] I followed the guidelines as stated in the Review Guidelines
  • [x] 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)
  • [x] If any new file was added I verified that:
    • [x] The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • [x] If a new CSS style is added I verified that:
    • [x] A similar style doesn't already exist
    • [x] The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • [x] If new assets were added or existing ones were modified, I verified that:
    • [x] The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • [x] The assets load correctly across all supported platforms.
  • [x] 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.
  • [x] 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)
  • [x] 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.
  • [x] 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.
  • [x] 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:
    • [x] I verified that all the inputs inside a form are aligned with each other.
    • [x] I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • [x] I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • [x] 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

https://github.com/user-attachments/assets/40f67605-a04a-45f3-85c0-124dacbdda4e

Android: mWeb Chrome

https://github.com/user-attachments/assets/7c20ea57-ad6e-432a-b421-01228c6f92ed

iOS: Native

https://github.com/user-attachments/assets/34942091-ed37-4e2f-b157-800b625260c7

iOS: mWeb Safari

https://github.com/user-attachments/assets/21d94bfa-f300-419d-9fb0-e8c7c314af1e

MacOS: Chrome / Safari

https://github.com/user-attachments/assets/37f5a8cd-b206-4466-b195-547310a81229

https://github.com/user-attachments/assets/3ece14bd-1ffc-4bac-82c4-ffccc6846722

David-2002065 avatar Jun 29 '26 09:06 David-2002065

@thelullabyy 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[bot] avatar Jun 29 '26 09:06 melvin-bot[bot]

@Gonals 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[bot] avatar Jun 29 '26 09:06 melvin-bot[bot]

Codecov Report

❌ Looks like you've decreased code coverage for some files. Please write tests to increase, or at least maintain, the existing level of code coverage. See our documentation here for how to interpret this table.

Files with missing lines Coverage Δ
.../components/SidePanel/SidePanelContextProvider.tsx 87.75% <ø> (ø)
src/hooks/useConciergeSidePanelReportActions.ts 95.12% <100.00%> (+0.31%) :arrow_up:
src/hooks/useReportActionsVisibility.ts 94.44% <100.00%> (+0.15%) :arrow_up:
src/hooks/useShouldSuppressConciergeIndicators.tsx 100.00% <100.00%> (+6.66%) :arrow_up:
src/libs/NetworkState.ts 94.17% <100.00%> (+0.80%) :arrow_up:
src/libs/ReportActionsUtils.ts 78.45% <100.00%> (+0.02%) :arrow_up:
src/libs/ReportUtils.ts 86.29% <100.00%> (-0.74%) :arrow_down:
src/pages/inbox/ConciergeSessionContext.tsx 55.55% <0.00%> (ø)
src/libs/actions/Report/index.ts 71.58% <60.00%> (+0.86%) :arrow_up:
... and 436 files with indirect coverage changes

codecov[bot] avatar Jun 29 '26 10:06 codecov[bot]

@thelullabyy this is ready for your review whenever you get a chance — all checks are green, and the testing steps + videos are in the PR description. Let me know if you need anything else.

David-2002065 avatar Jun 29 '26 13:06 David-2002065

@dilshodmackbook-sketch Please add recording to all envs

thelullabyy avatar Jun 30 '26 06:06 thelullabyy

@codex review

thelullabyy avatar Jun 30 '26 06:06 thelullabyy

Heads up on the failing Jest checks: they are not real test failures. The jobs are getting OOM-killed on the runner (exit code 143), and a different shard goes down on each run. Other unrelated PRs are hitting the same exit-143 in this same window, so it looks like a repo-wide runner issue rather than anything in this PR. Locally the related suites pass. I'll re-trigger once the runners settle.

David-2002065 avatar Jun 30 '26 07:06 David-2002065

@dilshodmackbook-sketch Please add recording to all envs

all videos uploaded

David-2002065 avatar Jun 30 '26 07:06 David-2002065

The two failing Jest shards (job 6 and job 8) are not real test failures — they're runner OOM. Both shards run every test to PASS and then the process is killed with exit code 143 (SIGTERM) during coverage collection, e.g. job 6 ends with:

PASS tests/unit/components/HTMLEngineProvider/getCodeBlockText.test.ts
##[error]Process completed with exit code 143.

There isn't a single ✕/FAIL marker in either shard's log. This is the known memory pressure on the heavier shards (--coverage --maxWorkers=6) and is unrelated to the changes in this PR. Re-running the two failed jobs should clear it. Could someone with write access kick off a re-run of jobs 6 and 8? Thanks.

David-2002065 avatar Jul 01 '26 07:07 David-2002065

@dilshodmackbook-sketch You can push an empty commit to re-run the tests

thelullabyy avatar Jul 01 '26 08:07 thelullabyy

@thelullabyy ready to review

David-2002065 avatar Jul 03 '26 10:07 David-2002065

Hi @thelullabyy — friendly bump on this one. It's complete and passing on my end, and it's been a few days since the last activity. Whenever you get a chance, could you run through the review? Happy to address any feedback. Thanks!

cc @Gonals

David-2002065 avatar Jul 07 '26 06:07 David-2002065

@dilshodmackbook-sketch Sorry for the delay. I will recheck it today. I was struggled to reproduce the issue...

thelullabyy avatar Jul 07 '26 20:07 thelullabyy

Reviewer Checklist

  • [ ] I have verified the author checklist is complete (all boxes are checked off).
  • [ ] I verified the correct issue is linked in the ### Fixed Issues section above
  • [ ] I verified testing steps are clear and they cover the changes made in this PR
    • [ ] I verified the steps for local testing are in the Tests section
    • [ ] I verified the steps for Staging and/or Production testing are in the QA steps section
    • [ ] I verified the steps cover any possible 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 checked that screenshots or videos are included for tests on all platforms
  • [ ] I included screenshots or videos for tests on all platforms
  • [ ] I verified that the composer does not automatically focus or open the keyboard on mobile unless explicitly intended. This includes checking that returning the app from the background does not unexpectedly open the keyboard.
  • [ ] I verified tests pass on all platforms & I tested again on:
    • [ ] Android: HybridApp
    • [ ] Android: mWeb Chrome
    • [ ] iOS: HybridApp
    • [ ] iOS: mWeb Safari
    • [ ] MacOS: Chrome / Safari
  • [ ] If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • [ ] I verified proper code patterns were followed (see Reviewing the code)
    • [ ] I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick).
    • [ ] 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 verified that this PR follows the guidelines as stated in the Review Guidelines
  • [ ] I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • [ ] If a new component is created I verified that:
    • [ ] A similar component doesn't exist in the codebase
    • [ ] All props are defined accurately and each prop has a /** comment above it */
    • [ ] The file is named correctly
    • [ ] The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • [ ] The only data being stored in the state is data necessary for rendering and nothing else
    • [ ] For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • [ ] Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • [ ] All JSX used for rendering exists in the render method
    • [ ] The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • [ ] If any new file was added I verified that:
    • [ ] The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • [ ] 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 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.
  • [ ] For any bug fix or new feature in this PR, I verified that sufficient unit tests are included to prevent regressions in this 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.
  • [ ] I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: HybridApp
Android: mWeb Chrome
iOS: HybridApp
iOS: mWeb Safari
MacOS: Chrome / Safari

thelullabyy avatar Jul 07 '26 20:07 thelullabyy

@dilshodmackbook-sketch Please merge main. Thanks

thelullabyy avatar Jul 07 '26 23:07 thelullabyy

@thelullabyy done, merged latest main. No conflicts and CI is green. Ready for another look whenever you get a chance, thanks!

David-2002065 avatar Jul 08 '26 05:07 David-2002065

BUG: Sometime I don't see any response from Concierge @dilshodmackbook-sketch

https://github.com/user-attachments/assets/ef1aaf5a-a6c5-4959-8b70-94acde23b169

https://github.com/user-attachments/assets/24b5de37-48b4-4463-afb1-e1efa69c9ff1

https://github.com/user-attachments/assets/180387bc-0510-4885-a2dc-ec69195de47d

thelullabyy avatar Jul 09 '26 04:07 thelullabyy

@thelullabyy thanks for catching this, fixed and verified locally.

Root cause: the reply from Concierge carries an authoritative server timestamp, but the session boundary (sessionStartTime) and firstUserMessageCreated are client-clock values, and the AddComment success never rewrites the optimistic created. When the client clock runs ahead of the server, the reply's created lands below both floors and stays filtered out for good, so the panel never shows it.

Rather than trusting those cross-clock comparisons, the reply is now captured by arrival: once the user has sent a message this session, the message that arrives after it is kept as a current-session action, gated by a server-time floor over the history already loaded at session start so older messages can't leak in. Added a unit test covering the skewed reply.

Recording of the fixed flow:

https://github.com/user-attachments/assets/df05b696-c86c-4003-afab-7d2d591eab58

David-2002065 avatar Jul 09 '26 06:07 David-2002065

@dilshodmackbook-sketch Please shorten all the code comments. They are wall of text now

Besides, let's update the explanation of changes part to correct with current approach

thelullabyy avatar Jul 09 '26 08:07 thelullabyy

@thelullabyy done. Shortened all the code comments to concise single-line explanations, and updated the Explanation of Change to match the current arrival-based approach (session membership captured by arrival via useCurrentSessionActionIDs, covering both the vanishing question and the missing reply). Ready for another look, thanks!

David-2002065 avatar Jul 09 '26 09:07 David-2002065