Skip to content

Re-land: resolve new agent IDs in every tab without erasing the agent in other tabs - #106109

Open
KJ21-ENG wants to merge 11 commits into
Expensify:mainfrom
KJ21-ENG:KJ21-ENG/100865-reland-leader-cleanup
Open

KJ21-ENG wants to merge 11 commits into
Expensify:mainfrom
KJ21-ENG:KJ21-ENG/100865-reland-leader-cleanup

Conversation

@KJ21-ENG

@KJ21-ENG KJ21-ENG commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • Cause: on web, Onyx saves the whole value of a key from the writing tab's own cache. A tab can get the temporary-to-real agent mapping before it gets the real agent's personal details from the same CreateAgent response. When that tab cleared the temporary agent, it saved a personal details list without the real agent, which removed the real agent in every tab.
  • Fix: the temporary agent's personal details are now removed in CreateAgent's success data. It is applied together with the response, so they normally leave in the same write that adds the real agent's details, and the cleanup no longer writes the personal details list from any tab. Each tab keeps showing the temporary agent until its own cache holds the real agent and its details from the server. Then it moves its agent screens to the real ID and removes the temporary agent record, which is a separate key. An agent that is deleted before that is cleared by the successful DeleteAgent instead. If that deletion fails, the agent stays and the normal cleanup applies.

Fixed Issues

$ #100865
PROPOSAL: #100865 (comment)

Tests

  • Verify that no errors appear in the JS console
  1. Sign in and open Account > Agents in two browser tabs on the same account, opening the second tab last. In the second tab, type the new agent's name into Find agent.
  2. In the first tab, select New agent > Build custom agent, enter that name and select Create agent.
  3. Verify the agent appears in the second tab and stays there. Reload the second tab and verify it is still listed.
  4. Repeat with the tabs swapped: create the agent in the tab opened last.
  5. In the first tab, create another agent, close its automatic chat, and immediately open the new agent from the list. Verify Edit Agent stays visible while creation finishes, then shows the saved name, instructions and avatar.
  6. During creation, open Name, Instructions or Avatar. Save an edit or use Back. Verify the values update and navigation returns to the same Edit Agent screen.
  7. With one tab open, create an agent and verify it appears once in the list and its chat header shows the agent's name.
  8. In the first tab, create an agent and delete it from its Edit page right away, before its email shows. Verify it disappears from both tabs and stays gone after reloading both.
  9. With both tabs open, create an agent and wait until it shows its email in both tabs. Verify each tab lists it once.
  10. Create an agent in the second tab. Before its email shows, open that agent from the list in the first tab. Verify Edit Agent stays visible and never shows the "Hmm... it's not here" page.
  11. Create an agent in the first tab and open it right away in the second tab. Verify Edit Agent keeps showing the agent's name the whole time, and that Save and Delete work once its email shows.

Offline tests

  1. In the first tab, disconnect the network, create an agent, and open its Edit page. Edit its name or instructions while creation is pending.
  2. Reconnect with the second tab open. Verify the agent and queued edits save, the agent stays listed in both tabs, and its Edit page remains usable.
  3. Disconnect the network, create an agent, delete it from its Edit page, then reconnect with the second tab open. Verify it is gone from both tabs and stays gone after reloading.

QA Steps

  • Verify that no errors appear in the JS console
  1. On staging, open Account > Agents in two browser tabs on the same account, opening the second tab last.
  2. Create a custom agent in the first tab. Verify the agent appears in the second tab and stays there, and that the first tab's agent chat header shows the agent's name instead of a loading skeleton.
  3. Repeat, creating the agent in the second tab.
  4. Create an agent, close its automatic chat, and immediately open the new agent. Verify Edit Agent stays visible as creation finishes. Repeat with one tab.
  5. Open Name, Instructions or Avatar before creation finishes. Verify Save and Back return to the same agent and saved changes appear.
  6. Create an agent offline in the first tab, edit its name or instructions, and reconnect while the second tab is open. Verify the agent and edits save and the agent stays listed in both tabs.
  7. With two tabs open, create an agent and delete it right away from its Edit page. Verify it disappears from both tabs and does not come back after reloading.
  8. With two tabs open, create an agent in one tab and immediately open it from the list in the other tab. Verify Edit Agent stays visible and never shows the "Hmm... it's not here" page.
  9. Create an agent in the first tab and open it right away in the second tab. Verify Edit Agent keeps showing the agent's name the whole time, and that Save and Delete work once its email shows.

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 verified any copy / text that was added to the app 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 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 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.
  • If the PR adds or modifies the UI:
    • I asked an AI agent to review the changes for accessibility issues and addressed its findings.
    • I tested with a screen reader (VoiceOver on macOS) and verified all new/changed elements are reachable with a logical focus order.
    • I verified all new/changed elements have meaningful accessible names and roles.
    • I verified state changes are announced (e.g. checked/unchecked, expanded/collapsed, selected).
  • 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
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

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.
@KJ21-ENG
KJ21-ENG marked this pull request as ready for review November 6, 2026 06:27
@KJ21-ENG
KJ21-ENG requested review from a team as code owners November 6, 2026 06:27
@melvin-bot
melvin-bot Bot requested review from getusha and removed request for a team November 6, 2026 06:27
@melvin-bot

melvin-bot Bot commented Nov 6, 2026

Copy link
Copy Markdown

@getusha 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]

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

This PR adds a new Onyx.connectWithoutView call, so I've requested a review from the Onyx performance reviewers (@tgolen, @mountiny, @luacmartins, @chuckdries) — a review from any one of them is enough. Please add a link in your PR description to the Slack discussion where the @frontend-performance team approved using connectWithoutView here.

@melvin-bot
melvin-bot Bot removed the request for review from a team November 6, 2026 06:27
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-06T06:54:45.990786Z 0da9683 Draft marked ready
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@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: 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.
tgolen
tgolen previously approved these changes Nov 6, 2026
garrettmknight
garrettmknight previously approved these changes Nov 11, 2026

@garrettmknight garrettmknight 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.

Looks good from a product perspective.

@getusha

getusha commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

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 any copy / text that was added to the app 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 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
    • 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
    • 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 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.
  • If the PR adds or modifies the UI:
    • I asked an AI agent to review the changes for accessibility issues and addressed its findings.
    • I tested with a screen reader (VoiceOver on macOS) and verified all new/changed elements are reachable with a logical focus order.
    • I verified all new/changed elements have meaningful accessible names and roles.
    • I verified state changes are announced (e.g. checked/unchecked, expanded/collapsed, selected).
  • 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

@getusha

getusha commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

@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
Screenshot 2026-11-13 at 10 13 01 AM

@KJ21-ENG

KJ21-ENG commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author

@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:

  1. which commit you tested and whether main was merged into it
  2. how many New Expensify tabs/windows were open
  3. whether Troubleshoot > Clear cache and restart brings the missing agent back

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

@chuckdries
chuckdries removed their request for review November 19, 2026 20:39
… 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.
@github-actions

Copy link
Copy Markdown
Contributor

This PR adds a new Onyx.connectWithoutView call, so I've requested a review from the Onyx performance reviewers (@tgolen, @mountiny, @luacmartins, @chuckdries) — a review from any one of them is enough. Please add a link in your PR description to the Slack discussion where the @frontend-performance team approved using connectWithoutView here.

@KJ21-ENG
KJ21-ENG requested a review from luacmartins November 20, 2026 08:41
}
}
if (Object.keys(detailsToRemove).length > 0) {
updates.push(buildPersonalDetailsUpdate(detailsToRemove));

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.

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?

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.

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.

Comment thread src/hooks/useAgentAccountID.ts Outdated
/** 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);

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.

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.

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.

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.
@KJ21-ENG

Copy link
Copy Markdown
Contributor Author

@getusha pushed b2e0924. the cleanup no longer writes the personal details list from any tab. a stale write of that list from the second tab was the only way i could make a real agent disappear in both tabs (only when forced, never at normal speed). can you retest your flow on this commit?

@mountiny
mountiny removed their request for review November 21, 2026 03:08
Comment thread src/hooks/useAgentAccountID.ts Outdated
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));

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.

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?

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.

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.

Comment thread src/libs/actions/Agent.ts
// 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}),

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.

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.

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.

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
KJ21-ENG requested a review from luacmartins November 24, 2026 09:55
@luacmartins

Copy link
Copy Markdown
Contributor

@KJ21-ENG I left a couple of comments below

@KJ21-ENG

Copy link
Copy Markdown
Contributor Author

@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.
@github-actions
github-actions Bot requested a review from mountiny November 24, 2026 20:38
@github-actions

Copy link
Copy Markdown
Contributor

This PR adds a new Onyx.connectWithoutView call, so I've requested a review from the Onyx performance reviewers (@tgolen, @mountiny, @luacmartins, @chuckdries) — a review from any one of them is enough. Please add a link in your PR description to the Slack discussion where the @frontend-performance team approved using connectWithoutView here.

@mountiny
mountiny removed their request for review November 25, 2026 01:46
Comment thread src/libs/actions/Agent.ts Outdated
@@ -410,6 +461,17 @@ function deleteAgent(accountID: number, agentLogin?: string, allPolicies?: OnyxC
buildPersonalDetailsUpdate({[agentAccountID]: {accountID: agentAccountID}}),

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.

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.

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. 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.

Comment thread src/libs/actions/Agent.ts Outdated
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},

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.

Save instructions before the real record arrives, then again after it arrives. If both saves are still queued, the older instructions can win.

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.

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.

Comment thread src/libs/actions/Agent.ts
// 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}),

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.

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?

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.

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.

@chuckdries
chuckdries removed their request for review November 25, 2026 21:01
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.
@github-actions

Copy link
Copy Markdown
Contributor

This PR adds a new Onyx.connectWithoutView call, so I've requested a review from the Onyx performance reviewers (@tgolen, @mountiny, @luacmartins, @chuckdries) — a review from any one of them is enough. Please add a link in your PR description to the Slack discussion where the @frontend-performance team approved using connectWithoutView here.

@KJ21-ENG

Copy link
Copy Markdown
Contributor Author

@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:

  • while a tab knows the new agent's real ID but doesn't hold its record and details yet, or the temporary details are gone before the mapping arrives, save and delete are off on the edit, name, prompt and avatar screens and in bulk delete, so nothing is saved from a partial copy. it's one rule in AgentUtils that all of them use, and existing agents aren't affected.
  • the temporary agent record keeps the name and avatar it was created with, so the edit screens show them while the details are still moving between tabs.
  • the name, prompt and avatar saves and delete no longer skip the check of what the tab holds when they're given a new agent's real ID.
  • the avatar's success data only writes the details list when the tab changed it (the same issue as the delete one).
  • the cleanup no longer writes the agent's DM back from a copy older than CreateAgent's response.
  • the workspace agents table follows the list's rows, so once a tab has the mapping it shows the agent once, with its role.

limits we can't remove on the client:

  • the agent can show twice for a moment if its real data reaches a tab before the mapping;
  • the DM can briefly show "Hidden" as its name;
  • a server response applied by a tab that's behind can still write from its older copy;
  • queued work is lost if the leader tab closes before it flushes;
  • in one forced order (the temporary record's removal and the details reaching a tab before the mapping) the edit screen can still show not found;
  • in the rare case where CreateAgent's data reaches a tab through pusher first, the real record can stay missing until the agents page is opened again or the app reloads, and save and delete stay off on that agent until then.

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.

@chuckdries
chuckdries removed their request for review November 25, 2026 23:05
@mountiny
mountiny removed their request for review November 26, 2026 04:14
@luacmartins

Copy link
Copy Markdown
Contributor

@getusha bump for review

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants