Skip to content

[Due for payment 2026-09-25] [Sentry: APP-9JM] Removing the last approver throws "Approval workflow must have at least one approver" #100266

Closed

Description

@mountiny

If you haven’t already, check out our contributing guidelines for onboarding. To join our Slack channel, fill out this form.


Version Number: 9.4.67 / 9.4.68 / 9.4.69
Reproducible in staging?: Needs to be confirmed
Reproducible in production?: Yes — crash is live in production
If this was caught during regression testing, add the test name, ID and link from BrowserStack: N/A — found via Sentry production crash scan
Email or phone of affected tester (no customers): N/A
Logs: https://stackoverflow.com/c/expensify/questions/4856
Expensify/Expensify Issue URL:
Issue reported by: Applause / Sentry production crash scan
Slack conversation (hyperlinked to channel name): N/A

Action Performed:

  1. Open a workspace where the approval workflow has exactly one approver.
  2. Go to Workspace > Members and open the member details page for that approver.
  3. Tap Remove from workspace.
  4. Confirm the removal.

Expected Result:

The member is removed and the workspace falls back to a valid approval workflow (for example, the workspace owner becomes the approver). No crash.

Actual Result:

The app throws an unhandled error and the page crashes.

removeUser() in WorkspaceMemberDetailsPage calls updateApprovalWorkflow(workflow, [], [], policy). convertApprovalWorkflowToPolicyEmployees then reads approvalWorkflow.approvers.at(0), finds it empty, and throws.

Workaround:

Unknown. Reloading the page recovers the session, but the removal does not complete.

Platforms:

Select the officially supported platforms where the issue was reproduced:

  • Android: App
  • Android: mWeb Chrome
  • iOS: App
  • iOS: mWeb Safari
  • iOS: mWeb Chrome
  • Windows: Chrome
  • MacOS: Chrome / Safari

Screenshots/Videos

Sentry details

Sentry issue: https://expensify.sentry.io/issues/APP-9JM

Impact (snapshot at filing, 2026-09-03):

  • Users (total since first seen): 3
  • Events (total): 16
  • Users in scanned releases (9.4.67–9.4.69): 1
  • First seen: 2026-04-08
  • Last seen: ongoing
  • Platform: web
  • Affected route (parameterized): /workspaces/:policyID/members/:accountID
  • Mechanism: auto.browser.global_handlers.onunhandledrejection

Stack trace (first-party frames):

src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx:271  removeUser()
src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx:251  updateApprovalWorkflow(workflow, [], [], policy)
src/libs/actions/Workflow.ts:128                                convertApprovalWorkflowToPolicyEmployees({...})
src/libs/WorkflowUtils.ts:376                                   throw new Error('Approval workflow must have at least one approver')

Suspected cause: removing the last remaining approver leaves approvalWorkflow.approvers empty, and convertApprovalWorkflowToPolicyEmployees throws instead of falling back to the default approver.

Related: #57298 covered the same throw and was closed in April 2025. Sentry first saw the current group on 2026-04-08, so this is a regression or a second path into the same throw.

View all open jobs on GitHub

Issue OwnerCurrent Issue Owner: @mallenexpensify

Activity

  1. self-assigned this
    on Sep 3, 2026
  2. added
    ExternalAdded to denote the issue can be worked on by a contributor
    BugSomething is broken. Auto assigns a BugZero manager.
    on Sep 3, 2026
  3. melvin-bot commented on Sep 3, 2026

    @melvin-bot

    Triggered auto assignment to Contributor-plus team member for initial proposal review - @cretadn22 (External)

  4. melvin-bot commented on Sep 3, 2026

    @melvin-bot

    Unable to auto-create job on Upwork. The BZ team member should create it manually for this issue.

  5. MelvinBot commented on Sep 3, 2026

    @MelvinBot
    Contributor

    ⏱️ Ran out of time before I could finish. This usually means the task was too complex or I got stuck exploring. Please try again with a more specific request or post in #melvinbot-ai.

    Run details

  6. cretadn22 commented on Sep 3, 2026

    @cretadn22
    Contributor

    @MelvinBot Investigate this issue

  7. MelvinBot commented on Sep 3, 2026

    @MelvinBot
    Contributor

    Proposal

    Note: the "Suspected cause" in the issue body is not correct. Removing the last approver does not empty the array — updateWorkflowDataOnApproverRemoval cannot shrink a non-empty approvers list to zero on any branch. The empty array is already present in the workflow list before removal, and removeUser() then submits it. Details below.

    What is the root cause of that problem?

    The synthesized default approval workflow is allowed to be created with zero approvers.

    WorkflowUtils.ts:283-292 prepends a default workflow whenever no employee submits to the default approver. It calls calculateApprovers, which returns [] immediately if employees[defaultApprover] doesn't exist (:78-81). Unlike the in-loop path — which has an explicit empty fallback at :213-224 — this unshift has no guard.

    That empty workflow then flows straight through:

    1. updateWorkflowDataOnApproverRemoval sees approvers.length === 0, so isSingleApprover and isMultipleApprovers are both false and every .some(...) on [] is false. It falls to the pass-through return workflow at :557 — unchanged, and not flagged removeApprovalWorkflow.
    2. removeUser() loops over every returned workflow and calls updateApprovalWorkflow on it — including this untouched, unrelated, empty one.
    3. It throws at WorkflowUtils.ts:376.

    Repro conditions (all four required — which is why this is only 3 users / 16 events, and why the steps in the issue body won't reproduce it):

    1. At least one employee submits to a valid employee, so a workflow exists.
    2. No employee submits to the default approver, so firstWorkflow.isDefault === false and the unshift runs.
    3. defaultApprover — getHRFinalApprover(policy) ?? getDefaultApprover(policy) (:175) — is not a key in employeeList. Real sources: a stale policy.approver pointing at an already-removed member, an HR/Gusto finalApprover who isn't a workspace member, or the secondary/primary login mismatch noted at Member.ts:388-408.
    4. The member being removed passes isPolicyApprover, so removeUser takes the workflow branch at all.

    This is not a regression from one PR. The unguarded unshift has been there since Aug 2024; the fix for #57298 (PR #59468, Apr 2025) only added !employees[submitsTo] to the in-loop skip — which is exactly what makes condition 2 true more often. So this is a second path into the same throw, not the old bug returning.

    What changes do you think we should make in order to solve the problem?

    Fix the producer — make the synthesized default workflow unable to have zero approvers. In WorkflowUtils.ts:283-292, mirror the existing in-loop fallback at :213-224: if calculateApprovers comes back empty, build a single approver from defaultApprover (falling back to policy.owner), and if there's no usable email at all, skip the unshift entirely. Extract this into one shared helper, because the identical unguarded block is copy-pasted into the beta converter at :1705-1712 and needs the same treatment.

    This also fixes two side effects of the same bad data: the defaultHasOwner check at :470 silently evaluating false, and an empty default-workflow card rendering on the workflows page.

    Add defense in depth — at the top of the flatMap callback in updateWorkflowDataOnApproverRemoval (:465), drop any workflow with no approvers by returning []. Because it's a flatMap, that removes the workflow and both call sites are fixed with no change at either (WorkspaceMemberDetailsPage.tsx:245 and WorkspaceMembersPage.tsx:249). Note: flagging it removeApprovalWorkflow: true instead would not work — removeApprovalWorkflow calls the same converter and would throw again.

    Tests to add (all three cases are currently uncovered — see details):

    1. convertPolicyEmployeesToApprovalWorkflows with policy.approver absent from employeeList plus one real workflow → the default workflow must have at least one approver.
    2. Same, with an HR finalApprover not in employeeList.
    3. updateWorkflowDataOnApproverRemoval given a workflow with approvers: [] → must not emit it.

    What alternative solutions did you explore? (Optional)

    • Guard in removeUser() only — rejected. It would have to be duplicated in WorkspaceMembersPage.tsx, and the corrupt workflow would still render in the approvals UI.
    • Soften the throw at :376 to a graceful return — rejected. It's a real invariant guarding three writers (createApprovalWorkflow, updateApprovalWorkflow, removeApprovalWorkflow). Returning {} would silently no-op user-initiated create/edit saves from WorkspaceWorkflowsApprovalsEditPage.tsx and hide future data bugs.
    Supporting evidence

    Why updateWorkflowDataOnApproverRemoval is not the culprit. Every branch that returns a modified approvers array is provably non-empty: owner substitution yields 1; the hasOverLimitToRemovedApprover .map() preserves length; removedIndex === 0 on a multi-approver chain does .slice(1) on a length ≥2 array; the slice(0, idx) branches are guaranteed idx > 0 because idx === 0 is handled earlier; and [...slice(0, idx), owner] yields ≥2. The only exits carrying an empty array are the two unmodified pass-throughs at :557 and :659.

    A second, narrower producer of approvers: []. The Expensify-team filter at :225-227 runs after the in-loop empty fallback, so it can re-empty the array when the entire chain is @expensify.com — plausible on customer workspaces where a member submits to a Guide. This landed after Sentry first saw the group (2026-04-08), so it isn't the original trigger, but the recommended producer-side fix should account for it.

    Existing test coverage gaps. In tests/unit/WorkflowUtilsTest.ts, updateWorkflowDataOnApproverRemoval has 12 cases and every input workflow has at least one approver. convertApprovalWorkflowToPolicyEmployees has 8 cases and none asserts the throw. The convertPolicyEmployeesToApprovalWorkflows suite includes the #59468 regression test, but its defaultApprover is an employee, so the result is [] and the unshift never runs; its createMockPolicy helper always sets approver to a valid employee. tests/ui/WorkspaceMemberDetailsPageTest.tsx has 11 tests, none of which exercises the remove-member path.

    Separate bug found while tracing (not this crash). When the removed approver is the workspace owner in a single-approver default workflow, the condition currentApprover?.email !== ownerEmail at :474 is false, so the workflow returns unchanged with the removed owner still listed as approver. Worth a separate issue.

    Runtime verification not obtained. I tried to drive the web build to confirm that the issue's stated four steps do not crash, but the test session came up with a blank page and never recovered, so that check is unverified. It doesn't change the code analysis: the throw needs defaultApprover to be missing from employeeList, which those steps don't create.

    Useful next step to narrow it further. For the policyIDs in the Sentry breadcrumbs, check whether policy.approver (or an HR finalApprover) was absent from that policy's employeeList at crash time. That distinguishes which of the three data conditions in point 3 the affected accounts hit.

    Possibly related open issues (neither is a duplicate): #75020 (admin becomes approver when dismissing a delete-approver error) and #94891 (approval workflow mismatch between New and Old Expensify) both touch approver-list correctness.


    Next Steps for Contributor+ team:
    To accept: @MelvinBot implement [this](https://github.com/Expensify/App/issues/100266#issuecomment-5527827020) to create a draft PR.
    To refine: @MelvinBot <your feedback>
    To reject: Explain why you are rejecting Melvin's proposal.


    view run

  8. cretadn22 commented on Sep 4, 2026

    @cretadn22
    Contributor

    @MelvinBot implement this

  9. MelvinBot commented on Sep 4, 2026

    @MelvinBot
    Contributor

    🤖 Draft PR created: #100370

    Please mark it as "Ready for review" when it is ready.

    Implemented the proposal as written. Two changes plus three tests:

    1. Fix the producer. New shared helper calculateDefaultWorkflowApprovers falls back to a single approver built from defaultApprover, then policy.owner, when calculateApprovers comes back empty — and returns [] only when there's no usable email at all, in which case the caller skips the unshift entirely. Used at both sites: the legacy converter and the beta rules converter, which had the same unguarded block copy-pasted.

    2. Defense in depth. The flatMap callback in updateWorkflowDataOnApproverRemoval now drops any workflow with no approvers by returning []. Both call sites are fixed with no change at either. This also covers the second, narrower producer you flagged — the Expensify-team filter re-emptying an array after the in-loop fallback.

    Left the throw alone, per your rejected-alternatives section.

    Tests: all three cases from the proposal are in tests/unit/WorkflowUtilsTest.ts. I confirmed all three fail on main and pass on the branch.

    Local checks
    Check Result
    npm test -- tests/unit/WorkflowUtilsTest.ts ✅ 108 passed (3 new)
    Same suite with the WorkflowUtils.ts change stashed ❌ 3 failed — confirms the new tests bite
    npm test -- tests/actions/WorkflowTest.ts tests/ui/WorkspaceMemberDetailsPageTest.tsx ✅ 40 passed
    npm test -- tests/ui/ApproverSelectionListTest.tsx tests/ui/WorkspaceWorkflowsApprovalsEditPageTest.tsx tests/ui/WorkspaceWorkflowsLoadMoreTest.tsx tests/unit/ApprovalWorkflowSectionTest.tsx ✅ 14 passed
    npm run typecheck ✅ passed
    npm run lint-changed ✅ passed
    npm run spell-changed ✅ 0 issues

    Not run: npm run prettier (no such script in package.json; formatting is covered by lint-changed) and npm run react-compiler-compliance-check (no .tsx source changed).

    Runtime verification not obtained. I tried to drive the web build to smoke-test workspace member removal and the workflows page on this branch, but the test session came up as a blank page with zero DOM nodes and never recovered — the same failure the analysis run hit. The unit tests cover the logic; a manual pass on the Tests steps is still worth doing before this goes up for review.

    One thing for you to decide. cretadn22 — I couldn't push to your fork, so the branch lives on upstream (claude-defaultApprovalWorkflowMissingApprover). To take ownership and push updates:

    git remote add upstream https://github.com/Expensify/App.git
    git fetch upstream claude-defaultApprovalWorkflowMissingApprover
    git checkout -b claude-defaultApprovalWorkflowMissingApprover upstream/claude-defaultApprovalWorkflowMissingApprover
    git push -u origin claude-defaultApprovalWorkflowMissingApprover
    

    Or just push to the upstream branch directly if you have access — either works.

    The Tests and QA Steps sections of the PR are left as TODO for you. Reaching the crash needs a workspace where no member submits to the default approver and policy.approver isn't a key in employeeList, so the repro steps in the issue body won't produce it as written.


    view run

  10. 80 remaining items

  11. melvin-bot commented on Sep 20, 2026

    @melvin-bot

    ✅ Contributor details stored successfully. Thank you for contributing to Expensify!

  12. cretadn22 commented on Sep 22, 2026

    @cretadn22
    Contributor

    A regression test is not required for this PR. We just need to monitor Sentry to verify whether the bug has been resolved

  13. added
    Awaiting PaymentAuto-added when associated PR is deployed to production
    and removed on Sep 22, 2026
  14. melvin-bot commented on Sep 24, 2026

    @melvin-bot

    Triggered auto assignment to @mallenexpensify (Awaiting Payment)

  15. melvin-bot commented on Sep 24, 2026

    @melvin-bot

    Payment Summary

    Resolving PRs:

    BugZero Checklist (@mallenexpensify)

    • I have confirmed assignees, roles, and Upwork contracts look correct
    • I have paid out Upwork contracts / manual NewDot requests
    • [BugZero Assignee] I have created a GH issue for creating/updating the regression test once above steps have been agreed upon
  16. cretadn22 commented on Sep 25, 2026

    @cretadn22
    Contributor

    A regression test is not required for this PR. We just need to monitor Sentry to verify whether the bug has been resolved

  17. cretadn22 commented on Sep 25, 2026

    @cretadn22
    Contributor

    The amount should be $250 since this work was completed prior to the downprice announcement

  18. mallenexpensify commented on Sep 26, 2026

    @mallenexpensify
    Contributor

    Payment Summary

    Contributor+: @cretadn22 due $250 via NewDot

  19. JmillsExpensify commented on Nov 4, 2026

    @JmillsExpensify
    Contributor

    $250 approved for @cretadn22

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

Metadata

Metadata

Labels

Awaiting PaymentAuto-added when associated PR is deployed to productionBugSomething is broken. Auto assigns a BugZero manager.DailyKSv2ExternalAdded to denote the issue can be worked on by a contributor

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions