Skip to content

Add rules engine types and merchant rule AST helpers - #100389

Merged
JS00001 merged 46 commits into
Expensify:mainfrom
Krishna2323:krishna2323/issue-100300
Oct 1, 2026
Merged

JS00001 merged 46 commits into
Expensify:mainfrom
Krishna2323:krishna2323/issue-100300

Conversation

@Krishna2323

@Krishna2323 Krishna2323 commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Explanation of Change

Fixed Issues

$ #100300
$ #102273
PROPOSAL:

Tests

Setup

Control workspace with Categories, Tags, Taxes, and Rules enabled. Use a second Control workspace for §5, §7, and §8. If merchant rules already exist, verify they still appear in Expense defaults.

1. Create, edit, delete

  1. Add a Starbucks merchant rule with Contains + category. Verify summary, refresh, and persistence.
  2. Edit it to Exactly matches + different category. Verify it updates without creating a duplicate.
  3. Delete it and verify it stays gone after refresh.

2. All fields round-trip

  1. Create a rule with merchant rename, category, tag, tax, description, vendor, reimbursable, and billable.
  2. Refresh/reopen and verify all values persist, including markdown description and tax rate/name. Don't change should remain unset.
  3. Rename/change a tax rate and verify the rule shows the live name/rate.
  4. Clear the description with spaces only and verify it saves empty.

3. Rules load on every entry point ⚠️

Clear cache before each:

  1. Cold-load workspace overview and verify rules load.
  2. Cold-load Home and verify Getting started → Set up rules is complete when rules exist.
  3. From the workspaces list, open Duplicate workspace and Copy settings without opening a workspace first. Verify Merchant rules and the correct count appear.
  4. Go offline/online and verify rules still show.

4. Offline and errors

  1. Offline create/edit/delete → verify pending state and correct reconnect behavior.
  2. Force a save failure → verify red error/dot and clean removal after dismissing.

5. Multi-workspace scoping ⚠️

  1. Create rules on A and B. Verify each workspace only shows its own rules.
  2. Verify the same separation in Rules row, Copy settings, Duplicate workspace, and Getting started counts.

6. Unsupported rules stay read-only

  1. Verify a nested and / multi-comparison rule shows a summary but can't be opened or selected.
  2. Paste its edit URL → Not found, not an empty editor.
  3. Paste an approval-workflow rule ID and another workspace's rule ID → Not found.
  4. Save another rule and verify the unsupported rule is unchanged.

7. CSV import

  1. Import merchant rules and verify they appear/persist.
  2. Re-import the same file → duplicates skipped and count reported.
  3. Import unknown category/vendor → invalid and not saved.
  4. Open/edit an imported rule.

8. Duplicate workspace / Copy settings

  1. Duplicate with Merchant rules selected → verify count and copied rules.
  2. Duplicate with Merchant rules unselected → verify none copied.
  3. Copy rules to two targets → verify independent copies and unchanged source.
  4. Verify a zero-rule workspace doesn't error.

9. Approvals coexist with merchant rules ⚠️

  1. With both configured, disable Add approvals → workflows removed, merchant rules survive refresh. Re-enable and verify rules remain.
  2. Edit an approval workflow and remove an approver → verify merchant rules are untouched.
  3. Run the existing five approval-workflow checks.

10. Category tax defaults

  1. Add a default category tax → verify save, persist, edit, and delete.
  2. Verify it does not appear in Expense defaults.

11. Indicators and side effects

  1. Getting started → Set up rules: incomplete with no rules, complete after adding one.
  2. Rules row red dot only appears for failures in the current workspace.
  3. Preview matches still lists matching unsubmitted expenses.
  4. Search/filter matches merchant, category, tag, and description.
  5. Bulk-delete several rules → verify all stay deleted after refresh.
  • Verify that no errors appear in the JS console

Offline tests

  1. Offline: create a rule → verify pending/greyed out.
  2. Online: verify pending clears and rule persists.
  3. Offline: delete a rule → verify pending delete, then gone after reconnect.
  4. Force SetRule failure → verify red error, Rules row red dot, and clean removal after dismissing.
  5. Offline: edit a rule → verify pending state and correct reconnect behavior.

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
    • MacOS: Desktop
  • 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 there are no new alerts related to the canBeMissing param for useOnyx
  • 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 shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I used JaimeGPT to get English > Spanish translation. I then posted it in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • 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)
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • 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)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.ts or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • 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.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • 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_hybrid.mp4
Android: mWeb Chrome
android_mWeb.mp4
iOS: Native
ios_hybrid.mp4
iOS: mWeb Safari
ios_mWeb.mp4
MacOS: Chrome / Safari
web_chrome.mp4

Signed-off-by: krishna2323 <belivethatkg@gmail.com>
@codecov

codecov Bot commented Sep 4, 2026 •

Copy link
Copy Markdown

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.

Files with missing lines Coverage Δ
src/CONST/index.ts 95.83% <100.00%> (+0.03%) ⬆️
src/ONYXKEYS.ts 100.00% <ø> (ø)
src/hooks/useRulesPrefetch.ts 100.00% <100.00%> (ø)
src/libs/API/types.ts 100.00% <ø> (ø)
src/libs/ExportOnyxState/masking.ts 80.95% <ø> (ø)
src/libs/PolicyUtils.ts 89.04% <100.00%> (-0.03%) ⬇️
src/libs/WorkflowUtils.ts 93.53% <100.00%> (-0.09%) ⬇️
src/libs/actions/Policy/Policy.ts 71.44% <100.00%> (+0.46%) ⬆️
...tingStartedSection/hooks/useGettingStartedItems.ts 98.70% <100.00%> (+0.05%) ⬆️
src/pages/workspace/WorkspaceInitialPage.tsx 85.56% <100.00%> (+0.15%) ⬆️
... and 17 more
... and 27 files with indirect coverage changes

Signed-off-by: krishna2323 <belivethatkg@gmail.com>
Signed-off-by: krishna2323 <belivethatkg@gmail.com>
Signed-off-by: krishna2323 <belivethatkg@gmail.com>
Signed-off-by: krishna2323 <belivethatkg@gmail.com>
Signed-off-by: krishna2323 <belivethatkg@gmail.com>
…ategory tax rules

Signed-off-by: krishna2323 <belivethatkg@gmail.com>
Signed-off-by: krishna2323 <belivethatkg@gmail.com>
Signed-off-by: krishna2323 <belivethatkg@gmail.com>
…gers

Signed-off-by: krishna2323 <belivethatkg@gmail.com>
Signed-off-by: krishna2323 <belivethatkg@gmail.com>
@Krishna2323

Krishna2323 commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Review finding

  • [P1] Preserve meaningful condition text for unsupported merchant rules. getMerchantRuleFormValues() intentionally returns undefined for nested or multi-merchant filters so those rules remain read-only. src/libs/ExpenseDefaultRuleUtils.tsL260-L275 However, MerchantRulesSection derives both the displayed merchant name and search tokens exclusively from that helper. src/pages/workspace/rules/MerchantRulesSection.tsxL114-L115 src/pages/workspace/rules/MerchantRulesSection.tsxL157-L164 Consequently, every unsupported rule is rendered as a disabled row with an empty condition (for example, “If merchant contains “””), and cannot be found by its merchant. This contradicts the intended behavior in the PR’s test plan that unsupported rules still appear with a useful summary. Use getRuleFilterLeaves() (as the revamp table already does) to derive display/search merchant values independently of editability, while continuing to disable navigation.

No code changes were made, so no commit or follow-up PR was created.

Checks

  • ✅ git diff --check 0283d2bebad28796ca74b9506d358232988fe376...HEAD
  • ⚠️ npm test -- --runInBand tests/unit/ExpenseDefaultRuleUtilsTest.ts tests/actions/PolicyRulesTest.ts tests/actions/CopyPolicySettingsTest.ts tests/actions/PolicyTest.ts tests/unit/WorkflowUtilsTest.ts tests/unit/PolicyUtilsTest.ts (could not run because dependencies are unavailable: jest: not found).

View task →

Signed-off-by: krishna2323 <belivethatkg@gmail.com>
Signed-off-by: krishna2323 <belivethatkg@gmail.com>
@JS00001
JS00001 requested a review from situchan September 8, 2026 11:05
@Krishna2323
Krishna2323 marked this pull request as ready for review September 8, 2026 11:06
@Krishna2323
Krishna2323 requested review from a team as code owners September 8, 2026 11:06
@melvin-bot
melvin-bot Bot requested review from JS00001, flaviadefaria and luacmartins and removed request for a team September 8, 2026 11:06
@melvin-bot

melvin-bot Bot commented Sep 8, 2026

Copy link
Copy Markdown

@luacmartins @JS00001 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]

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

ℹ️ 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".

Comment on lines 226 to 230
const rule: ImportedMerchantRule = {
filters: {
left: 'merchant',
operator: merchantIsValue ? CONST.SEARCH.SYNTAX_OPERATORS.EQUAL_TO : CONST.SEARCH.SYNTAX_OPERATORS.CONTAINS,
operator: formValues.matchType ?? CONST.SEARCH.SYNTAX_OPERATORS.CONTAINS,
right: merchantToMatch,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Convert imported rows to rules-engine payloads

For every valid CSV row this still builds the legacy flat CodingRule shape, but the migrated list/editor only recognize rules-engine records with CreateTransaction triggers and Set actions in the rules_ collection. Thus imports either fail at the legacy endpoint or create rules that this UI cannot display or edit; construct rules-engine bodies (or switch the import endpoint) before reporting the import as successful.

Useful? React with 👍 / 👎.

@flaviadefaria
flaviadefaria removed their request for review September 8, 2026 13:36
@Krishna2323

Copy link
Copy Markdown
Contributor Author

@JS00001 While testing, I found a blocker I thought was already fixed on the BE. On staging, my workspace shows 6 merchant rules, while this branch shows 3 and those 3 can't be deleted.

The cause looks like policy.rules.codingRules and the new rules_ collection are out of sync on the backend. codingRules has 6 IDs, rules_ has 3, and none of them overlap:

codingRuleIDs:     1577212093784538139, 1735736249944622059, 2753024661937163348,
                   3942641543825743207, 5181083725344271538, 5277224537906375191
ruleCollectionIDs: 5938659776896190551, 6964069421310331044, 8160082849313325051

Staging reads from codingRules, so it shows 6. This PR reads from rules_, so it shows 3.

Delete then fails because we send the rules_ ID to SetPolicyCodingRule, which doesn't know about it. It returns 200 with an empty onyxData and deletes nothing. We then drop the row locally, and the next GetRules brings it back:

delete sent for: 5938659776896190551
response:        {"jsonCode":200,"requestID":"a43b836bd88454b3-SJC","onyxData":[]}

Newly created rules are fine since we generate one ID and send it as the codingRuleID, so both stores agree. It's the older rules that are affected.

This looks like a blocker for the read-side switch. Until the two stores contain the same rules, this PR will show users a different set of rules than they have today.

Monosnap.screencast.2026-10-01.18-45-31.mp4

@JS00001

JS00001 commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Discussing ^ w/ krishna in slack

@JS00001

JS00001 commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

I took a look at the logs. These bugs came from using the new SetRule/DeleteRule PR , alongside some legacy API calls, which got the account into a wonky state. This would not happen for real customers

@JS00001

JS00001 commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

The details I sent to krishna in slack:

Here is the flow that you did on this account:

Import merchant rules (which replicated the legacy coding rules to the new rules format
Selected the rules and called DeleteRule on them
[10:02 AM] Both DeleteRule and SetRule are new commands that are not backwards compatible (meaning we deleted the rules from the rules table, but not from the legacy codingRules location
[10:03 AM] So I think you got the account into this state while testing the PR for SetRule and DeleteRule

JS00001
JS00001 previously approved these changes Oct 1, 2026
@melvin-bot
melvin-bot Bot requested a review from JS00001 October 1, 2026 14:31
@JS00001
JS00001 merged commit 5ee15cf into Expensify:main Oct 1, 2026
37 of 38 checks passed
@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

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

@OSBotify

OSBotify commented Oct 1, 2026

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.

@luacmartins luacmartins 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 for working on this

@OSBotify OSBotify mentioned this pull request Oct 1, 2026
73 tasks done
@OSBotify

OSBotify commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/JS00001 in version: 9.5.1-0 🚀

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

@MelvinBot

Copy link
Copy Markdown
Contributor

No help site update needed. This PR moves merchant rules to the new rules engine data format and APIs behind the scenes. It doesn't change any labels, steps, or user-facing behavior that an existing article describes.


view run

@jponikarchuk

Copy link
Copy Markdown

This PR failing because of the issues: #102869, #102878
This issue is reproducible in: Web, Android app

@situchan

situchan commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Those already happen on production so not blockers

@OSBotify

OSBotify commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by https://github.com/Julesssss in version: 9.5.1-9 🚀

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants