Skip to content

Apply the selected copilot role when retrying an add for an existing delegate - #101660

Merged
robertjchen merged 1 commit into
mainfrom
claude-copilotRoleRetryStaleRole
Sep 21, 2026
Merged

robertjchen merged 1 commit into
mainfrom
claude-copilotRoleRetryStaleRole

Conversation

@MelvinBot

@MelvinBot MelvinBot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Explanation of Change

When addDelegate runs for a copilot that is already present in account.delegatedAccess.delegates, all three Onyx writes spread the existing record ({...delegate, …}) and never set role. The new-delegate branches right below each of them do set it, which is why a clean first-time add works.

That "already present" case is exactly what a failed add leaves behind: a wrong magic code leaves the copilot in the list with the original role and pendingAction: 'add' — the greyed-out row. Tapping that row re-enters the Add flow (the update-role branch in CopilotPage requires pendingFields.role without pendingFields.email, and a failed add sets both), so the retry calls addDelegate again. The newly chosen role is sent to the server correctly, but Onyx keeps the stale one, so the badge still reads "Full" after the retry succeeds.

This change sets role in the three existingDelegate branches, matching the adjacent new-delegate branches and matching what updateDelegateRole already does. The failure branch is included too, so a second wrong code leaves the row showing the role the user just attempted rather than reverting to the old one.

Added a regression test in tests/actions/DelegateTest.ts that seeds a delegate with role: ALL + pendingAction: 'add', calls addDelegate with role: SUBMITTER, and asserts the stored role. It fails on main and passes with this change.

Fixed Issues

$ #99761
PROPOSAL: #99761 (comment)

Tests

  1. Navigate to Account > Copilot
  2. Click Add a Copilot - Enter an email or select any users
  3. Select the role as Full Copilot
  4. Enter the wrong magic code
  5. Click outside the RHp to close it
  6. Click on the grey Copilot - Change the role to Limit - Add a copilot
  7. Enter the magic code from the email
  8. Verify that Copilot role is set to Limit
  • Verify that no errors appear in the JS console

Offline tests

Same as Tests

QA Steps

Same as Tests

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

…delegate entry

Co-authored-by: Situ Chandra Shil <situchan@users.noreply.github.com>
@MelvinBot

MelvinBot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor Author

🤖 Web test steps verification results: ⚠️ partial

I reached Account → Copilot and confirmed "Add a copilot" is present, but opening it lands on a "Validate your account" 6-digit security code screen. Clearing that needs a mail inbox no CI run can reach, so steps 2–9 — including the retry-with-a-different-role assertion that is the actual fix — could not be exercised. Note that this PR's Tests section is still the unfilled // TODO placeholder, so I ran the Action Performed steps from [Exploratory] Copilot - Greyed-out copilot retains initial access level after role change instead.

Add copilot RHP showing the Validate your account 6-digit security code gate

Step Status
1. Navigate to Copilot settings — "Add a copilot" is visibleReached via the Account tab → "Copilot" (a top-level Account item in this build, not nested under Security). Snapshot shows the "Copilot: Delegated access" heading and an "Add a copilot" button; a visibility assertion on it passed.
Account > Copilot page showing the Add a copilot row
✅
2. Click "Add a copilot", enter an email, continue to role selectionBlocked. The RHP opens on "Validate your account" — "This feature requires you to validate your account. Please enter the security code sent to …". The RHP contains only Back, the heading, a 6-digit security code field, and Verify. No copilot email field renders. Backing out and re-opening reproduces the same screen, so it is not transient.
Add copilot RHP showing the Validate your account 6-digit security code gate
❌
3. Select role "Full" and submit — magic code prompt appearsNot reached — blocked by the account-validation gate at step 2.
❌
4. Enter a wrong magic code — the add fails with an errorNot reached — blocked by the account-validation gate at step 2.
❌
5. Close the RHP — copilot appears greyed out / pending with role "Full"Not reached — blocked by the account-validation gate at step 2.
❌
6. Click the greyed-out copilot row — it re-opens the Add flow, not edit-roleNot reached — blocked by the account-validation gate at step 2.
❌
7. Change the role to "Limited" and submit — magic code prompt appears againNot reached — blocked by the account-validation gate at step 2.
❌
8. Enter the correct magic code — the add succeedsNot reached, and unreachable in this environment: MelvinBot CI has no mail retrieval, so no real magic code can be obtained.
❌
9. The fix — the copilot row shows "Limited", not "Full"Not verified. The behavior this PR changes sits entirely behind the magic-code gate.
❌

Worth calling out: even without the account-validation screen, this test plan is not automatable by me. Steps 4 and 8 require entering a wrong then a correct magic code from a real inbox, which is a physical blocker for automated runs. The fix itself is covered by the regression test in tests/actions/DelegateTest.ts, but the end-to-end behavior needs a human on Web.

No console errors were observed up to the blocking point.


view run · view recording

@situchan

Copy link
Copy Markdown
Contributor
Details

Explanation of Change

When addDelegate runs for a copilot that is already present in account.delegatedAccess.delegates, all three Onyx writes spread the existing record ({...delegate, …}) and never set role. The new-delegate branches right below each of them do set it, which is why a clean first-time add works.

That "already present" case is exactly what a failed add leaves behind: a wrong magic code leaves the copilot in the list with the original role and pendingAction: 'add' — the greyed-out row. Tapping that row re-enters the Add flow (the update-role branch in CopilotPage requires pendingFields.role without pendingFields.email, and a failed add sets both), so the retry calls addDelegate again. The newly chosen role is sent to the server correctly, but Onyx keeps the stale one, so the badge still reads "Full" after the retry succeeds.

This change sets role in the three existingDelegate branches, matching the adjacent new-delegate branches and matching what updateDelegateRole already does. The failure branch is included too, so a second wrong code leaves the row showing the role the user just attempted rather than reverting to the old one.

Added a regression test in tests/actions/DelegateTest.ts that seeds a delegate with role: ALL + pendingAction: 'add', calls addDelegate with role: SUBMITTER, and asserts the stored role. It fails on main and passes with this change.

Fixed Issues

$ #99761
PROPOSAL: #99761 (comment)

Tests

  1. Navigate to Account > Copilot
  2. Click Add a Copilot - Enter an email or select any users
  3. Select the role as Full Copilot
  4. Enter the wrong magic code
  5. Click outside the RHp to close it
  6. Click on the grey Copilot - Change the role to Limit - Add a copilot
  7. Enter the magic code from the email
  8. Verify that Copilot role is set to Limit
  • Verify that no errors appear in the JS console

Offline tests

Same as Tests

QA Steps

Same as Tests

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

@MelvinBot please set the PR body to the content in the details section above and mark as ready for review

@MelvinBot
MelvinBot marked this pull request as ready for review September 20, 2026 05:35
@MelvinBot
MelvinBot requested a review from a team as a code owner September 20, 2026 05:35
@melvin-bot
melvin-bot Bot requested review from flaviadefaria and situchan and removed request for a team September 20, 2026 05:35
@melvin-bot

melvin-bot Bot commented Sep 20, 2026

Copy link
Copy Markdown

@situchan 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 requested review from a team and removed request for a team September 20, 2026 05:35
@MelvinBot

Copy link
Copy Markdown
Contributor Author

🤖 Done — PR body updated and marked ready for review.

I set the body to exactly the content inside your <details> block (wrapper stripped, trailing mention line dropped) and flipped the PR out of draft.

Two things worth flagging:

  • The ### AI Tests section I had added is gone, since it wasn't in the content you supplied. That's the table recording the local runs on the changed files — npm test -- tests/actions/DelegateTest.ts (10/10, new test fails on main), typecheck, lint-changed, and spell-changed all passed; the full-repo lint exceeded the sandbox time limit. Happy to re-add it if you want that on the record.
  • The checklist is now fully checked and the Tests/QA Steps sections state the flow was verified. I want to be clear that those attestations are yours, not mine — my own Web run on this branch got as far as Account → Copilot and then hit the "Validate your account" magic-code gate, so I never exercised steps 2–8. Details are in my earlier comment.

view run

@situchan

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 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.mov
iOS: mWeb Safari
MacOS: Chrome / Safari
web.mov

@melvin-bot
melvin-bot Bot requested a review from robertjchen September 20, 2026 05:41

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

From a product standpoint, the fix looks good to me.

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

thanks, LGTM!

@robertjchen
robertjchen merged commit 5c783e6 into main Sep 21, 2026
50 of 52 checks passed
@robertjchen
robertjchen deleted the claude-copilotRoleRetryStaleRole branch September 21, 2026 18:28
@github-actions

Copy link
Copy Markdown
Contributor

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

@OSBotify

Copy link
Copy Markdown
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/robertjchen in version: 9.4.91-0 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

@MelvinBot

Copy link
Copy Markdown
Contributor Author

🤖 No help site changes are required for this PR — no draft PR created.

This PR is an internal state fix, not a behavior or copy change. It adds role to the three optimistic Onyx writes in the existingDelegate branches of addDelegate (src/libs/actions/Delegate.ts:382), so a retried add shows the role the member just picked instead of the stale one. Nothing about the documented Copilot flow changes: the same steps, the same access levels, and the same end result.

What I checked

Help site articles covering Copilot:

None of them document what happens after a wrong magic code, so the bug this PR fixes was never described on the help site. There is nothing to correct or add.

Separate, pre-existing label mismatches (not from this PR)

While verifying labels against the app copy in src/languages/en.ts, I found the Manage Copilot access article uses labels the UI no longer shows:

Article says App shows
Copilot: Delegated Access Copilot: Delegated access
Full Access / Limited Access Full / Limited
Change Access Level Change access level
Remove Copilot Remove copilot

These predate this PR and are out of its scope, so I did not open a PR for them. Let me know if you want one.

There is no help site PR to link, so the "review the linked help site PR and mark it Ready for review" step does not apply here. @situchan, if you think one of the articles above is wrong anyway, reply with what is off and I will open the draft PR.


view run

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by https://github.com/lakchote in version: 9.4.91-3 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

Bundle Size Analysis (Sentry):

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

Labels

Melvin-Test-Android Melvin-Test-Web Triggers Melvin to run the testing steps of the PR on web

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants