Record an email action's baseline when it is created
Closes #411. Upstream issue: we-promise/sure#4152.
Upstream draft PR: we-promise/sure#4176 (pull/4176), from this same branch: same branch, same commit.
Base: mirror/upstream-main (an exact copy of we-promise/sure main). Branch up/rule-first-run-baseline, head 95230b936. The merge-base with the mirror is its current tip bf8d6cc39, so the real change is 13 files, +391 −33. GitHub still records an older base SHA (0c4ff952), so its Files tab and Codacy's range show 64 files, 51 of them upstream's own.
Problem
A "Send email notification" action is meant to record the transactions its rule already matches as delivered when it is created, so the rule only ever emails what appears afterwards. That create-time baseline never ran. Rule::Action registered the same method twice:
after_create_commit :seed_notification_baseline
after_update_commit :seed_notification_baseline, if: :saved_change_to_action_type?
Both expand to after_commit :seed_notification_baseline, on: ..., and Rails keeps only the last commit callback naming a method. So a new email rule, or an email action added to an existing rule, emailed every transaction it already matched on its first run (Apply, or the next sync). Only changing an existing action's type to email took a baseline.
Change
Rule::Action: oneafter_commit :seed_notification_baseline, on: %i[create update], if: :saved_change_to_action_type?. On create the type changes fromnil, so the guard admits every new action; on update it still seeds only when an action becomes an email action.- One definition of the email action.
Rule::Action::EMAIL_NOTIFICATION, anemail_notificationsscope andRule::Action#email_notification?. A test pins the constant toRule::ActionExecutor::SendEmailNotification#key, which is derived from the class name.Rule#seed_notification_baseline(the criteria-change re-seed from we-promise/sure#4150, now onmain) uses the scope too, so the literal appears only in the constant. - A replacement email action keeps the rule's baseline. Three paths destroy and rebuild a rule's actions:
RuleImport,Family::DataImporter#import_rulesand the assistant'supdate_rule(also served over MCP). With the create-time seed working, an identical re-import or an unchanged resend would re-seed and record a match still waiting for the rule's next run as delivered, so it would never be emailed. Each path now setskeep_notification_baselineon the rebuilt actions when the rule already had a stored email action, and the seed skips them. The baseline stays under the rule's id. A path that creates a rule, or gives a rule its first email action, still seeds. - How "already had" is decided differs by path, on purpose:
- The importers write every row in one transaction. A rule named twice in one file would see, on its second row, the email action its first row had just written. That action is replaced before it commits, so its seed never runs, and keeping "its" baseline would keep nothing. So both importers take
Rule.ids_with_stored_email_notification_action(family.rules)before writing anything, and askrule.persisted? && set.include?(rule.id). update_rulecommits on each call, so it readsRule#stored_email_notification_action?(persisted? && actions.email_notifications.exists?). It reads the database, so actions marked for destruction in memory do not change it.
- The importers write every row in one transaction. A rule named twice in one file would see, on its second row, the email action its first row had just written. That action is replaced before it commits, so its seed never runs, and keeping "its" baseline would keep nothing. So both importers take
- Tests that seeded by hand because the create-time seed never ran now rely on the callback: the executor test (
send_email_notification_test.rb), the edit-test helper (notification_baseline_on_edit_test.rb), and the switch-on test (notification_baseline_on_enable_test.rb, from we-promise/sure#4089), which now asserts the delta on the create.
Left out: any backfill for rules created before this fix. Those rules have already emailed their history, or will on their next run, and a migration cannot tell which.
How this sits with we-promise/sure#4150 and we-promise/sure#4089
Both are now on upstream main, and this branch has merged them in (565c2401c, 73688e915):
- #4150 (re-seed when a rule's criteria change, as
Rule'safter_update).keep_notification_baselineis read beforerule.notification_baseline_taken_in_this_save?, so an import that replaces an email action on a rule whose criteria also changed still gets #4150's rule-level re-seed, and an identical re-import gets neither. The importers' marked-for-destruction conditions are #4150's; this adds the stored-action snapshot above them. - #4089 (re-seed when a rule is switched on). Its test relied on the create seed not running and seeded by hand;
33329eafdmakes it expect the create seed and assert the delta instead.
Tests
test/models/rule/notification_baseline_on_create_test.rb (new), one form test in test/controllers/rules_controller_test.rb, two tests each in test/models/rule/action_test.rb and the assistant create_rule/update_rule tests, and the corrected hand-seeding tests above. Measured as NotificationDelivery rows and enqueued RuleEmailNotificationJob arguments, before and after.
| Test | Observed before the fix |
|---|---|
| creating a rule with an email action records its current matches as the baseline | on 3e35aaa07: 0 rows, 2 expected |
| a new email rule's first run emails nothing that matched before it | on 3e35aaa07: emailed both old matches |
| a match that appears after the email rule is created is still emailed | on 3e35aaa07: emailed the new match and both old ones |
| adding an email action to an existing rule records the baseline | on 3e35aaa07: [] |
| controller: creating an email rule through the form records its current matches | on 3e35aaa07: [] |
| RuleImport / Sure import record the baseline for a new email rule | on 3e35aaa07: [] |
| a RuleImport re-import that adds an email action to an existing rule records the baseline | on 3e35aaa07: [] |
| an identical RuleImport / Sure re-import keeps the baseline, so a pending match is still emailed | on 3e35aaa07: emailed the pending match and both old ones |
| a RuleImport naming a new email rule twice still records its baseline | on 33329eafd: baseline [], 2 expected |
| a Sure import naming a new email rule twice still records its baseline | on 33329eafd: baseline [], 2 expected |
| executor: pre-seed watermark records existing matches without sending | on 3e35aaa07: 1 job enqueued |
assistant create_rule: a new email rule records its current matches as the baseline | regression guard for the seed on this path |
assistant update_rule: replacing a rule's actions with an email action records the baseline | regression guard for the seed on this path |
assistant update_rule: resending an unchanged email action keeps the baseline, so a pending match is still emailed | on 5071b52d5: the pending match was recorded as delivered |
| the email action type is the email executor's key | on 3b4a64d4c: NameError (drift guard) |
email_notifications selects only the email actions | on 3b4a64d4c: no such scope |
| a rule reports a stored email action, even once it is marked for replacement | on 3b4a64d4c: no such predicate |
| an email action that is only built, not stored, does not count as the rule's | on 3b4a64d4c: no such predicate |
The two "named twice" rows were re-run for this description: 01a8e7470's test file on 33329eafd's code, 16 runs, 2 failures, both --- expected [two ids] +++ actual [].
Negatives, passing before and after: changing an existing action's type to email still seeds; creating a rule without an email action records nothing and never runs the match query; editing an email action without changing its type does not re-seed.
Mutations, each caught:
- the two separate callbacks restored: 11 tests;
on: %i[update]only: 11 tests; - the
saved_change_to_action_type?guard dropped: "editing an email action without changing its type"; keep_notification_baselineignored: both identical re-import tests;- the flag forced off in either importer: that importer's identical re-import test; forced on in RuleImport: 3 tests (new rule, adding an email action, identical re-import); in the Sure importer: 2 tests;
- in
update_rule: the flag never set (the resend test); the flag forced on (create_rule's new rule and "replacing ... with an email action"); - both importers reading
rule.stored_email_notification_action?inside the import transaction instead of the snapshot: both "named twice" tests (re-run for this description); - the shared definition: changing the constant's value, 17 tests;
stored_email_notification_action?always false, 4 tests;email_notification?always false, 15 tests; the scope inverted, 8 tests.
Blast radius. Who else creates a Rule::Action, and does it need the flag?
- The rule form's create: a new rule, so it seeds (controller test). Its update keeps the ids of unchanged actions, so nothing is recreated.
RulesController#new: an unsaved rule to prefill the form, never saved.Rule.create_from_grouping: a set-category action only, so it never reaches the seed.RuleImport,Family::DataImporter#import_rulesand the assistant'supdate_rule: flagged, as above.create_rulebuilds a new rule, so it seeds.preview_rulenever saves.- The v1 rules API is read-only.
Rule::Action#seed_notification_baseline has no other caller.
Validation
- Full suite on
95230b936: 12960 runs, 59739 assertions, 1 failure, 0 errors, 36 skips (Ruby 3.3.6). The failure isProvider::OpenaiTest#test_extra_headers_stringifies_nested_values_Ruby-style, which depends on Ruby 3.4'sHash#inspectformat and is untouched by this PR; CI ran this head green on 3.4. bin/rubocopon the 13 changed files: no offences.bin/brakeman --no-pager -q: 0 warnings.- No UI, API, migration or locale changes. Migration notes: none.