Repository navigation
Hide device management & merge accounts and repurpose close account when copiloting into an agent - #95812
Conversation
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.
|
|
🚧 NicolasBonet has triggered a test Expensify/App build. You can view the workflow run here. |
|
🤖 I looked into the failing checks — there's no code fix to push. The only failing check is human-only, and every code/correctness check is green. Failing check: ➡️ To clear it: the human co-author fills in the Everything code-related passed: Still running (not failures): |
This comment has been minimized.
This comment has been minimized.
|
🤖 Fixed the failing ESLint check and pushed to the branch. Failure: Fix: dropped the Note: I couldn't run ESLint locally in this environment, so I'm relying on the re-triggered CI |
|
@ikevin127 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] |
…hen copiloting into an agent Co-authored-by: Nicolás Bonet <NicolasBonet@users.noreply.github.com>
Co-authored-by: Nicolás Bonet <NicolasBonet@users.noreply.github.com>
Co-authored-by: Nicolás Bonet <NicolasBonet@users.noreply.github.com>
1fd870b to
e62cea5
Compare
|
@truph01 @NicolasBonet One of you needs to 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] |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1fd870b673
ℹ️ 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".
… flushes Co-authored-by: Nicolás Bonet <NicolasBonet@users.noreply.github.com>
|
🤖 Re-checked the PR — nothing is currently failing, so there's no fix to push.
There's nothing actionable to fix at the moment. If a check does fail once this run finishes, re-run |
Reviewer Checklist
Screenshots/VideosMacOS: Chrome
|
There was a problem hiding this comment.
🐞 Two bugs in the "copiloting into an agent → Close account → Delete" flow
Tested this flow and hit two separate issues. Both are fixed by the DIFF below.
Bug 1 — Delete fails with a 666 permission error; the agent is never deleted
The copilot session ends (you return to your own account) but the agent still appears under Settings > Agents and can still be copiloted into. The API responds with:
{
"jsonCode": 666,
"title": "Cannot complete action",
"message": "You do not have the permission to do the requested action."
}Root cause: DELETE_AGENT must be issued by the agent's owner, but the current ordering fires it while the session is still authenticated as the agent. Requests get their auth token at send time via enhanceParameters() → getAuthToken() (mirrors ONYXKEYS.SESSION), and enhanceParameters also appends the delegate param while a copilot session is active. So the delete flushes under the agent's identity → backend sees the agent trying to delete itself → 666. This is the same command EditAgentPage runs successfully; the only difference is the identity it's issued under.
The current commit (Delete agent within copilot session, then disconnect after the delete flushes) regressed this — the earlier Delete agent as owner after ending copilot session, not as the agent ordering was correct. The Delegate.ts comment ("the promise is returned so callers can run follow-up work … once the original session is restored") already describes the intended disconnect-first design; the code just stopped following it.
Bug Video
current.mov
Fix: end the copilot session first (restoring the owner's session + auth token), then delete the agent as the owner. Capture the agent's accountID before disconnecting, and await disconnect() so the request goes out under the owner's token. Also remove the now-unused waitForIdle import from @libs/Network/SequentialQueue. The Delegate.ts change (returning the disconnect promise) is now correct as-used since we await it.
Bug 2 — Confirming the delete while offline hangs silently
Repro: copilot into an agent → turn off the network → tap Close account → Delete. Nothing happens in the UI; the network tab shows a pending DisconnectAsDelegate request that never resolves. Going back online (ReconnectApp, AuthenticatePusher, SearchForTodos all fire) does not un-stick it — DisconnectAsDelegate stays pending and the agent is never deleted.
Root cause: disconnect() uses API.makeRequestWithSideEffects (a side-effect request), which is not persisted to the SequentialQueue, so offline it's never queued/retried and await disconnect() never resolves — the whole flow stalls before deleteAgent() is ever reached.
Fix: this flow fundamentally requires a network round-trip, so guard on isOffline and show the standard offline modal (same pattern as DomainMembersPage) instead of starting the flow.
Combined diff (src/pages/settings/Security/SecuritySettingsPage.tsx)
// imports
import useNetwork from '@hooks/useNetwork';
// remove: import {waitForIdle} from '@libs/Network/SequentialQueue';
// inside the component
const {isOffline} = useNetwork();
// close-account action, copilot branch:
if (isCopilotingIntoAgent) {
// Deleting the agent ends the copilot session via DisconnectAsDelegate, which is a network
// side-effect request that can't be queued offline (it never resolves, leaving the flow stuck).
// So require a connection before starting the flow.
if (isOffline) {
showConfirmModal({
title: translate('common.youAppearToBeOffline'),
prompt: translate('common.thisFeatureRequiresInternet'),
confirmText: translate('common.buttonConfirm'),
shouldShowCancelButton: false,
});
return;
}
const result = await showConfirmModal({
title: translate('editAgentPage.deleteAgentTitle'),
prompt: translate('editAgentPage.deleteAgentMessage'),
confirmText: translate('common.delete'),
cancelText: translate('common.cancel'),
danger: true,
});
if (result.action !== ModalActions.CONFIRM) {
return;
}
// The DeleteAgent command must be issued by the agent's owner, but while copiloting the session is
// authenticated as the agent itself (deleting as the agent fails with a 666 permission error). So end
// the copilot session first (which restores the owner's session and auth token) and only then delete
// the agent, now that we're acting as the owner.
const agentAccountID = session?.accountID ?? CONST.DEFAULT_NUMBER_ID;
await disconnect({stashedCredentials, stashedSession});
deleteAgent(agentAccountID, undefined, undefined, false);
return;
}Remember to add isOffline to the useMemo dependency array for the menu items.
I verified both fixes locally (typecheck passes): disconnect-first deletes the agent successfully as the owner, and the offline guard surfaces a clear "You appear to be offline" modal instead of hanging.
Suggested tests to add
No tests ship with this PR, and the two bugs above are exactly the kind of thing a review can't catch by eye — the failure lives in ordering and offline transport, not in the visible UI. Some coverage worth adding:
- Ordering / identity (guards Bug 1): unit test the copilot close-account branch asserting
disconnect()is called beforedeleteAgent(), and thatdeleteAgentreceives the agent'saccountIDcaptured pre-disconnect. Why: this is the whole fix — a future refactor could silently flip the order back (as happened here) and reintroduce the666. A test pins the invariant that the delete runs as the owner. - Offline guard (guards Bug 2): render the page with
isOffline = true, trigger the action, and assert the "You appear to be offline" modal shows and neitherdisconnect()nordeleteAgent()is called. Why: prevents anyone from later "simplifying" the guard away and re-stranding the flow on aDisconnectAsDelegatethat can't resolve. - Menu-item gating (guards the core PR change): with
isCopilotingIntoAgent = true, assert Device management and Merge accounts are absent and Close account is present; with itfalse, assert all three render and Close account navigates toSETTINGS_CLOSE. Why: this is the primary behavior of the PR and the most likely thing to regress when the security menu is edited later. - Cancel path: confirm modal → Cancel resolves to a no-op (no
disconnect/deleteAgent). Why: a destructive action must be safely abortable; cheap to assert and easy to break.
Note on the offline modal + PR description
The blocking modal is needed specifically because of DisconnectAsDelegate: unlike DELETE_AGENT (a normal API.write() that the SequentialQueue persists and flushes on reconnect), disconnect() is a makeRequestWithSideEffects call that bypasses the queue — it's fire-once, never persisted, never retried. So the usual "queue it and let it run when back online" behavior doesn't apply, and the flow genuinely can't complete offline. That's why we block up front instead of optimistically queuing.
Because of this, the offline behavior here differs from online and the PR's Offline tests section shouldn't just say "Same as tests." It should call out that, when copiloting into an agent, tapping Close account while offline surfaces the offline modal and does not delete the agent or end the copilot session (that requires a connection) — so QA verifies the guard rather than the delete.
cc @NicolasBonet MelvinBot will need your nudge to apply the fixes I mentioned in this comment, due to permission issues.
…state DeleteAgent must be issued by the agent's owner (the backend derives the caller from the auth token and rejects the agent deleting itself), so end the copilot session first and only delete once disconnect() confirms the owner session was restored. Since DisconnectAsDelegate is a side-effect request that can't be queued offline, block the flow with the standard offline modal when there's no connection. Also pass the agent login and policies to deleteAgent so policy employeeList rows are cleaned up like they are when deleting from the Edit Agent page. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Thanks @ikevin127 — confirmed both bugs and pushed fixes in a729088. I verified the root causes independently rather than applying the diff as-is:
I also addressed the two Codex findings on top of your fixes:
Also updated the PR's Offline tests section to describe the offline-modal behavior instead of "Same as tests." |
ikevin127
left a comment
There was a problem hiding this comment.
🟢 LGTM - All issues were addressed and tests well
…tionsWhenCopilotingIntoAgent Co-authored-by: Nicolás Bonet <NicolasBonet@users.noreply.github.com>
|
🤖 Fixed by merging The failing ESLint check was not caused by this PR — it flagged 4 files this PR never touches (
I merged the latest Evidence
|
|
|
|
@MelvinBot fix eslint |
|
|
|
@MelvinBot fix eslint |
|
|
|
@NicolasBonet Could you unassign me from this PR? |
|
@MelvinBot please address the conflicts with main |
|
|
…Agent # Conflicts: # src/libs/actions/Delegate.ts
|
🚧 puneetlath has triggered a test Expensify/App build. You can view the workflow run here. |
|
🧪🧪 Use the links below to test this adhoc build on Android, iOS, and Web. Happy testing! 🧪🧪
|
|
🚀 Deployed to staging by https://github.com/puneetlath in version: 9.4.34-0 🚀
|
|
🤖 Help site review complete — docs update required. This PR changes user-facing behavior on the Security page when Copiloting into an agent (hides Device management and Merge accounts, and repurposes Close account to delete the agent and end the Copilot session). That behavior is documented in the Create and Use Agents article, so I opened a draft docs PR to reflect it. Draft docs PR: #96050 What it updates:
All UI labels were verified against @NicolasBonet, please review the linked help site PR and confirm it reflects the current behavior. Then mark the linked help site PR |
|
🚀 Deployed to production by https://github.com/roryabraham in version: 9.4.34-14 🚀
|
|
🚀 Deployed to production by https://github.com/roryabraham in version: 9.4.34-14 🚀
Bundle Size Analysis (Sentry): |
Explanation of Change
When a user copilots into an agent account (acting as a delegate for an
agent_*@expensify.aiaccount), the Security settings page previously showed options that don't apply to an agent and let the user run the normal "close account" flow against the agent.This PR adjusts the Security page for the "copiloting into an agent" state (
isAgentAccount && isActingAsDelegate):DELETE_AGENTcommand for the agent you're copiloting into, and then ends the copilot session (disconnect), returning you to your own account.Details:
src/pages/settings/Security/SecuritySettingsPage.tsx— adds theisCopilotingIntoAgentguard, conditionally hides the two menu items, and swaps the close-account action for the delete-agent + disconnect flow. The confirmation modal reuses the existingeditAgentPage.deleteAgentTitle/deleteAgentMessagecopy (same UX as deleting an agent from the Edit agent page).src/libs/actions/Agent.ts—deleteAgentgains an optionalshouldNavigateBackparam (defaulttrue, so existing callers are unaffected). The copilot flow passesfalsebecause the delegate transition resets navigation on its own.Fixed Issues
$ #95726
Tests
Offline tests
Same as tests, except for the copilot "Close account" flow: ending a copilot session requires a live network call (
DisconnectAsDelegateis a side-effect request that can't be queued offline), so when copiloting into an agent and offline, tapping Close account shows a "You appear to be offline" modal and does not delete the agent or end the copilot session. Verify the modal appears and nothing else happens; the delete flow itself can only be tested online.QA Steps
Same as tests.
PR Author Checklist
### Fixed Issuessection aboveTestssectionOffline stepssectionQA stepssectionAvatar, I verified the components usingAvatarare working as expected)StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))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
MacOS: Chrome / Safari
CleanShot.2026-07-10.at.11.20.49.mp4