Skip email notifications for inactive rules

Closed
JA
2mo ago
+1389-372

Closes #445

Upstream issue: we-promise/sure#4196
Upstream draft PR: we-promise/sure#4211 (pull/4211), opened 2026-10-14 from this same branch and commit (cffa5576b).

Base: mirror/upstream-main at a3bb5d991 (upstream main). Branch up/rule-inactive-no-email, head cffa5576b.

Problem

"Apply all" (ApplyAllRulesJob, and rake rules:apply_all) passes family.rules to Rule::Runner, which executes every requested rule whatever its active?, so that its transaction changes apply. Rule::ActionExecutor::SendEmailNotification#execute never checked the rule's own active flag, so an email rule the user had switched off recorded its new matches in NotificationDelivery and enqueued RuleEmailNotificationJob. With we-promise/sure#4089, a rule switched off after a timed-out baseline would email its whole history the same way.

Change

SendEmailNotification#execute returns 0 before NotificationDelivery.record_for when rule.active? is false. Nothing else changes: not ApplyAllRulesJob, Rule::Runner or the other executors. An inactive rule's transaction-changing actions still apply under Apply all.

The inactive run records nothing, so matches made while the rule is off stay unrecorded. Without #4089, the rule's next active run emails them, the same as switching it on and running it today. With #4089, switching it on takes a baseline first.

Blast radius

Who else hits this line, and what do they pass? execute is reached only through Rule::Action#apply (app/models/rule/action.rb:55) ← Rule#apply (rule.rb:153) ← Rule::Runner#execute (runner.rb:115). The runner is built by:

CallerRules passedEffect of the guard
ApplyAllRulesJob (apply_all_rules_job.rb:7), from RulesController#apply_allfamily.rules, inactive includedinactive email rules stop emailing (the fix)
lib/tasks/rules.rake:24 (rules:apply_all)family.rules, inactive includedsame as above
ApplyRulesJob (apply_rules_job.rb:10), sync and nightlyfamily.rules.where(active: true)none
RuleJob (rule_job.rb:9), from Rule#apply_later ← RulesController#apply (:55-56) and the assistant's apply_rule (apply_rule.rb:84,95)[rule], after both callers switch it onnone, unless the rule is switched off again before the job runs; then it sends nothing, which is intended
Rules above the requested ones (runner.rb:59)active onlynone

Sibling grep:

  • SendEmailNotification is constructed only in app/models/rule/registry/transaction_resource.rb:35.
  • NotificationDelivery.record_for is called at send_email_notification.rb:20 (guarded now) and rule/action.rb:92 (seed_notification_baseline). The baseline is left alone deliberately: it records without enqueueing, and it is what makes a re-enabled rule skip its history.
  • RuleEmailNotificationJob is enqueued only by this executor.
  • executor.execute has one caller, Rule::Action#apply.

Tests

test/jobs/apply_all_rules_job_test.rb. Each email rule has its existing match recorded, as its baseline would leave it, and then one new match:

  • does not email for an inactive rule: no RuleEmailNotificationJob, and the NotificationDelivery count for the rule is unchanged. Fails on unfixed code: 0 jobs expected, 1 enqueued.
  • applies an inactive rule's category action but does not email: the new match's category goes from nil to Groceries, and no email is sent or recorded. Fails on unfixed code: 1 job enqueued.
  • emails new matches for an active rule: one job with [rule.id, [new_match.id]], and the delivery count goes up by 1. Passes before and after.
  • does not re-send an active rule's already recorded matches: no job, and the count is unchanged. Passes before and after.

test/models/rule/action_executor/send_email_notification_test.rb:

  • an inactive rule records and enqueues nothing: no job, delivery count unchanged, returns 0. Fails on unfixed code: 1 job enqueued.
  • The existing tests used rules(:one), which is inactive by the column default (active default: false), so setup now switches it on. Without that, three existing tests fail on the guard. The watermark test's rule is created active: true for the same reason. Without that, its "sends nothing" assertion would pass because of the guard rather than the baseline.

Mutations, each run against both files:

  • Drop the guard: the three inactive tests fail (1 job enqueued).
  • Invert it (return 0 if rule.active?): 7 fail, including the active-rule job test and the three existing executor send tests.
  • Remove the setup switch-on: the three existing executor send tests fail.
  • Disable seed_notification_baseline: the watermark test fails.

Merge with the open rule PRs (git merge-tree, then trial merge and tests)

  • we-promise/sure#4089 (up/rule-text-operators, c9f239baf): clean. This should land before or with #4089. With the merge, its tests plus the rule, job and assistant tests pass (546 runs, 0 failures), including notification_baseline_on_enable_test.rb.
  • we-promise/sure#4150 (up/rule-email-backfill, e17833f5c): clean. With the merge, 523 runs, 0 failures.
  • we-promise/sure#4176 (up/rule-first-run-baseline, 3b4a64d4c) has one textual conflict, in send_email_notification_test.rb's watermark test. To resolve it, take #4176's version and add active: true to its rules.create!. There is also one semantic conflict, in #4176's update_rule_test.rb test "resending an unchanged email action keeps the baseline, so a pending match is still emailed". update_rule switches the rule off when its actions are replaced, and the test then calls Rule#apply on the inactive rule and expects an email. With the guard, no email is sent. Production goes through apply_rule, which switches the rule on first. Adding rule.update_columns(active: true) before the apply makes it pass (10 runs, 0 failures). Whichever of the two PRs lands second carries that line.

Validation

  • Full suite on cffa5576b: 11826 runs, 54575 assertions, 0 failures, 0 errors, 34 skips.
  • bin/rubocop -f github on the changed files: no offences.
  • bin/brakeman --no-pager -q: 0 warnings.
  • No ERB, JS, migration or locale changes.