Repository navigation
Conversation
Reapplies Expensify#101360, which Expensify#105844 reverted because of deploy blocker Expensify#105813.
On web, Onyx saves the whole value of a key from the writing tab's cache. A tab that created an agent but is not the leader could still lack the real agent that CreateAgent's response added, so its cleanup erased the real agent's personal details and the agent disappeared from every tab (Expensify#105813). Every tab still redirects its own screens; only the leader writes the shared cleanup.
…ateAgent Leadership moves to the most recently opened or focused tab, so the leader at cleanup time can be a tab whose cache never received the real agent. The middleware now marks each mapping in the tab that applies the response, and only that tab, or a tab that loaded the mapping from storage on start, clears the optimistic agent. Other tabs only redirect their own screens.
A tab can apply the response or receive the mapping before the real agent's personal details, whichever tab sent CreateAgent. Gate the cleanup on what the writing tab's own cache holds instead of on tab ownership: a tab clears the optimistic agent only when its cache already has the real agent (or the agent was deleted while CreateAgent was in flight), so its full-value write cannot erase the real agent. Other tabs only redirect their own screens.
|
This PR adds a new |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0da9683104
ℹ️ About Codex in GitHub
Your team has set up Codex to 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 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
deleteAgent resolves an optimistic accountID to the real one as soon as the middleware registers it, so it only marked and removed the real agent. The optimistic agent kept its ADD marker, and the successful deletion removed the real personal details its cleanup waits for, so it stayed listed. Mark the optimistic agent as deleted too, and drop that marker if the deletion fails.
garrettmknight
left a comment
There was a problem hiding this comment.
Looks good from a product perspective.
Reviewer Checklist
Screenshots/VideosAndroid: HybridAppAndroid: mWeb ChromeiOS: HybridAppiOS: mWeb SafariMacOS: Chrome / Safari |
|
@KJ21-ENG the agent disappears when you create it in one tab and open it in another, even after refresh the agent is not editable or accessible: Screen.Recording.2026-11-13.at.10.12.22.AM.mov
|
|
@getusha thanks for testing. I tried to reproduce this on the latest head (90f5729) with main merged, with two windows side by side: created the agent in one window and watched the other, closed the chat, switched focus between windows, and repeated it with the agent created in each window. The agent showed up in both windows and stayed, also after a refresh (6 tries). The only odd thing I could trigger is when the tab that's running the request queue gets reloaded, or a new tab is opened, while CreateAgent is still in flight. The queue sends CreateAgent again, the backend returns 409, and the row briefly shows "There was a problem adding this agent" before it settles on the real agent. That looks like the queue re-sending the in-flight request when another tab takes over, not the agent cleanup in this PR, and it recovers on its own. Could you share:
The agent without an email in your list looks like a leftover from an earlier attempt, so clearing the cache before retesting would help too. Recording below. highlights.mp4 |
… tab holds the real one The cleanup gate now needs the real agent's login, so neither the DELETE marker nor deleteAgent's placeholder personal details let a lagging tab save a personal details list without the real agent. A successful DeleteAgent clears the optimistic agent itself, also when it was queued while CreateAgent was in flight. A tab that skipped the cleanup retries when personal details change, until nothing optimistic is left.
…and-leader-cleanup
|
This PR adds a new |
| } | ||
| } | ||
| if (Object.keys(detailsToRemove).length > 0) { | ||
| updates.push(buildPersonalDetailsUpdate(detailsToRemove)); |
There was a problem hiding this comment.
If another tab just saved a second agent, this merge can overwrite the list with a copy that doesn’t have it yet. Could we remove just the temporary entry without writing back the whole list?
There was a problem hiding this comment.
on web Onyx can't save one entry of personalDetailsList, a merge always saves the whole list. so i removed the separate cleanup write instead: the temporary entry now goes out in CreateAgent's successData, which is applied together with the response, so it normally goes out in the same write that brings in the real agent's details (if that data already arrived through pusher, successData is applied on its own). the cleanup no longer writes personal details from any tab, it only removes agent_<temp>, which is its own key, and fixes the DM's participants if this tab's copy still has the temporary ID. fixed in b2e0924. in two tabs each create now makes 2 list writes (optimistic + response) and none from the cleanup. the response write itself still saves the whole list, same as on main.
| /** Follow the server-assigned ID even when this screen opens after the navigation redirect. */ | ||
| function useAgentAccountID(accountID: number | string, navigation: AgentNavigation) { | ||
| const [mappedAccountID, metadata] = useOnyx(ONYXKEYS.OPTIMISTIC_AGENT_ACCOUNT_ID_MAPPING, {selector: (mapping) => mapping?.[accountID]}); | ||
| const resolvedAccountID = isValidAgentAccountID(mappedAccountID) ? mappedAccountID : Number(accountID); |
There was a problem hiding this comment.
Edit Agent shows Not Found if the mapping arrives before the real agent’s data. Let’s keep showing the temporary agent until the real data arrives and add a test for that order.
There was a problem hiding this comment.
reproduced it: with two tabs the mapping reaches the other tab before agent_<real>, so Edit Agent showed Not Found for 0.1 to 2.5s. fixed in b2e0924: the hook and the background redirect keep the temporary agent until this tab has the real agent and its details, then switch. added the test for that order in EditAgentPageTest.
Every write of the personal details list saves the whole list from the writing tab's cache, so the separate cleanup write could drop entries another tab had just saved. CreateAgent's success data now removes the temporary personal detail. It is applied together with the response, so it normally goes out in the same write that adds the real agent's details. The cleanup only removes the temporary agent record, which is its own key, from any tab. The mapping can reach a tab before the real agent's record and details, which showed Edit Agent as Not Found. The hook and the background redirect now keep the temporary agent until this tab holds the real agent and its server details, then switch.
| const [hasRealDetails] = usePersonalDetail(realAccountID, (personalDetail) => !!personalDetail?.login); | ||
| const [hasTemporaryAgent] = useOnyx(`${ONYXKEYS.COLLECTION.AGENT}${accountID}`, {selector: (agent) => !!agent}); | ||
| const [hasTemporaryDetails] = usePersonalDetail(Number(accountID), (personalDetail) => !!personalDetail); | ||
| const shouldUseRealAccountID = !!realAccountID && ((!!hasRealAgent && !!hasRealDetails) || (!hasTemporaryAgent && !hasTemporaryDetails)); |
There was a problem hiding this comment.
If I save a name in the second tab after it gets the ID mapping but before it gets the agent’s details, the save can wipe the agent’s login in both tabs. Can we queue the save without writing to the details list in that case?
There was a problem hiding this comment.
reproduced it when the second tab gets the details list late (forced, not seen at normal speed). the same window also wiped agent_<real> (prompt and owner) through the record merge, and a failed delete lost the login. fixed in 3eb5c06: when this tab already maps to the real ID but doesn't hold the real details or record yet, the name, avatar, prompt and delete actions still queue the request but skip the optimistic writes to keys this tab doesn't hold. their success and failure data are still applied by the tab running the queue, which holds the real data. a skipped prompt change is saved through the success data instead, and nothing skipped is reverted on failure. UpdateAgentName's response already brings the new name.
| // first tab that holds the real agent's record and server details removes it (replaceOptimisticAgentWithActualAgent), | ||
| // so open agent screens always have an agent to show. | ||
| const successData: AnyOnyxUpdate[] = [ | ||
| buildPersonalDetailsUpdate({[optimisticAccountID]: null}), |
There was a problem hiding this comment.
If the details arrive before the agent record, the name and avatar go blank. Let’s show the saved name and avatar while keeping the temporary instructions.
There was a problem hiding this comment.
reproduced it with agent_<real> held back in the second tab: name and avatar were blank for the whole hold. fixed in 3eb5c06: useAgentAccountID now also returns the ID to read personal details with, which switches to the real ID as soon as this tab has the real details, so the Edit, Name and Avatar pages show the saved name and avatar while the instructions still come from the temporary agent.
|
@KJ21-ENG I left a couple of comments below |
|
@luacmartins Ah seem like like missed those, on it now. |
A tab can resolve an agent's real accountID before it holds the real agent's personal details or record. Saving the name, avatar or prompt, or deleting the agent, then merged into keys this tab did not hold: that saved the whole personal details list without the real agent's login, or the agent record without its prompt and owner, in every tab. These actions still send their request and keep their success and failure data, but skip the optimistic writes to keys this tab does not hold yet. The name and avatar come back in the server's response; a prompt saved this way is written by the success data instead. The Edit, Name and Avatar pages read personal details with the real accountID as soon as this tab holds the real details, so the name and avatar no longer go blank while the temporary agent's record is still shown.
|
This PR adds a new |
| @@ -410,6 +461,17 @@ function deleteAgent(accountID: number, agentLogin?: string, allPolicies?: OnyxC | |||
| buildPersonalDetailsUpdate({[agentAccountID]: {accountID: agentAccountID}}), | |||
There was a problem hiding this comment.
A failed delete can wipe the agent’s login if the tab handling it hasn’t received the agent’s details yet. Let’s skip this rollback if we didn’t change those details.
There was a problem hiding this comment.
good catch, fixed. the delete's failure data now only rolls back the agent's details when this tab changed them, so a tab that didn't hold the details can't write the list back without the login. delete is also off while a new agent is still settling in a tab (the mapping is there but not the real details and record yet), on the edit screen and in bulk delete. added a test for the failure data and checked it on staging with a forced DeleteAgent failure: the agent kept its login in both tabs and after a reload.
| key: onyxKey, | ||
| value: {pendingAction: null, promptErrors: null}, | ||
| // Without the optimistic write, the new prompt is saved by the tab that runs the request, which holds the record. | ||
| value: {...(canWriteAgentRecord ? {} : {prompt}), pendingAction: null, promptErrors: null}, |
There was a problem hiding this comment.
Save instructions before the real record arrives, then again after it arrives. If both saves are still queued, the older instructions can win.
There was a problem hiding this comment.
fixed. a prompt save for a new agent now always carries its prompt in the success data and doesn't revert it on failure, and a save made while one of those is still in the queue does the same, so the responses apply in order and the newer prompt is the one left. tests cover the earlier save made before the record arrived with the later one on the temporary and on the real accountID. on staging, with both saves queued behind CreateAgent (response held, and offline), the agent ended on the second prompt on screen and after a reload. the trade-off: if one of those saves fails, its prompt isn't rolled back, because the prompt the screen started from can be older than a queued save that already succeeded. the field shows the error instead.
| // first tab that holds the real agent's record and server details removes it (replaceOptimisticAgentWithActualAgent), | ||
| // so open agent screens always have an agent to show. | ||
| const successData: AnyOnyxUpdate[] = [ | ||
| buildPersonalDetailsUpdate({[optimisticAccountID]: null}), |
There was a problem hiding this comment.
If the new details arrive before the real record, the agent briefly disappears from the list. Can we keep it visible and test that order?
There was a problem hiding this comment.
fixed. the list now builds a row per agent record and keeps the temporary record's row until the tab holds the real record and its details, using the real details as soon as they arrive, or the name saved on the temporary record if they arrive before the mapping. the tests cover each order in which the mapping, the details and the record can arrive, and on staging the forced order (details first, record held for 3 seconds) no longer hides the row. one limit: if the real agent's details and record both reach a tab before the mapping, nothing links them to the temporary record yet, so the agent shows twice for that moment, the temporary row with its actions off.
A new agent's data reaches a second tab key by key, in any order: the mapping, the personal details write that swaps the temporary details for the real ones, and the real agent's record. In between, that tab could save from an incomplete copy or show the agent blank or not at all. One rule now decides, for screens and lists, which ID to show and act on and whether the agent is still settling; while it is, Save and Delete are off. The temporary record keeps a fallback name and avatar, and each list shows the agent once in every order. Actions also check what the tab holds when the caller passes a new agent's real ID, only roll back details they changed, and every instructions save carries its text so the newest one wins. Only the leader tab repairs the DM's participants, since its copy of the report is current.
|
This PR adds a new |
|
@luacmartins thanks. since this is the same window again, we didn't stop at these three comments: we went through every place that reads or writes a new agent's data (the lists, the edit screens, the actions and the cleanup) in every order the mapping, the details and the real record can reach another tab, and fixed what we found. besides the three above:
limits we can't remove on the client:
the diff grew to 12 source files because this window runs through the hook, the four edit screens, both lists, the actions and the cleanup. each fix has a test, and we ran the two-tab cases again on staging. |
|
@getusha bump for review |

Explanation of Change
This re-lands #101360, which was reverted in #105844 because of deploy blocker #105813 (with two tabs open, a new agent appeared in the other tab and then disappeared).
The first commit re-applies #101360 unchanged. The commits after it are the fix for #105813:
Fixed Issues
$ #100865
PROPOSAL: #100865 (comment)
Tests
Offline tests
QA Steps
PR Author Checklist
### Fixed Issuessection aboveTestssectionOffline stepssectionQA stepssectionAvatar, I verified the components usingAvatarare working as expected)npm run compress-svg)Avataris modified, I verified thatAvataris working as expected in all cases)Designlabel and/or tagged@Expensify/designso the design team can review the changes.mainbranch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTeststeps.Screenshots/Videos
Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari
Two tabs on the same account. The agent is created in the other tab; the recording shows the tab that is watching the list. The agent appears and stays.
after-leader-tab.mp4