Automatic System Rules Applied to Mail on Completion
Executive summary
When a mail item is moved to the Completed status, the system now automatically applies the organization's active system-defined rules to that mail in the background. This brings mail in line with how documents are already processed, so finishing a mail can trigger the same automated downstream actions without any manual step.
Why this was needed
Documents already had system rules applied automatically once they reached a completed state, but mail did not. Completing a mail left configured automation (the active system rules) un-triggered, requiring manual follow-up and creating an inconsistency between how mail and documents are handled.
Client / user impact
Clients with active system rules will see those rules automatically run against a mail the moment it is marked Completed (when coming from Processing or Proofreading). The work runs in the background, so the status change itself stays fast and is not blocked or delayed by rule processing. If no system rules are configured, behavior is unchanged.
Technical scope
status_transition_service.transition_mail_statusnow accepts an optionalbackground_tasksargument.- On a transition into the Completed status from Processing or Proofreading, it queries for active system rules (
is_active=True,is_system=True) scoped to the mail's tenant/client. - Matching rules are dispatched to
BackgroundRuleService.execute_rules_backgroundforentity_type="mail", preferring FastAPIBackgroundTasksand falling back to anasynciotask when none is provided. mail_service.update_mailwas updated to passbackground_tasksthrough to the transition call.- All rule-queuing logic is wrapped in try/except with logging so failures never break the status transition.
Risk & mitigation
Low-to-moderate. Rule execution is fire-and-forget in the background and fully wrapped in error handling, so it cannot fail the status update. Main risks are unexpected side effects from system rules now firing on mail (where they previously did not) and the asyncio fallback path running outside FastAPI's managed background lifecycle. Mitigation: rules are limited to those flagged is_system and is_active, scoped per tenant, and only triggered on the specific Completed transition.
QA validation focus
- Move a mail from Processing to Completed and confirm active system rules execute against it in the background.
- Repeat for Proofreading to Completed; confirm other transitions (e.g., into Processing) do NOT trigger system rules.
- Verify the status change returns promptly and is unaffected by rule processing time.
- Confirm a tenant with no active system rules sees no change and no errors.
- Confirm that a failing or erroring rule does not block or roll back the completion, and that the failure is logged.
- Compare resulting mail behavior against the existing document completion flow for consistency.