Skip to content

refactor(backend): converge notification sending onto one dispatch owner (typed intents, single policy point) #9518

Open

Description

@Git-on-my-level

Summary

Converge backend notification sending onto a single dispatch owner. Today 15+ modules call notification-send functions directly, so cross-cutting policy — rate limiting, dedup, quiet hours, user preference checks, batching — must be re-implemented (or is silently missing) at each call site. Callers should submit typed notification intents; one dispatcher owns policy and delivery.

Same governing rule as #9515 / #9516: one exclusive mutation API per canonical concern, migrate every caller, delete the alternatives. This is deliberately the smaller second convergence on the backend — sequence it after #9516 proves the pattern.

Evidence (verified in tree)

Direct senders (via utils/notifications.py send functions, webhooks, or proactive_notification):

  • routers/action_items.py, routers/oauth.py, routers/payment.py, routers/users.py, routers/apps.py, routers/integration.py, routers/notifications.py
  • utils/app_integrations.py, utils/webhooks.py, utils/fair_use.py, utils/wrapped/generate_2025.py
  • utils/conversations/process_conversation.py, utils/llm/proactive_notification.py (~22K of its own policy), utils/llm/model_config.py
  • modal/job.py (notifications cron)

There is no single point where "should this user receive this, now, on this channel?" is decided.

Target architecture

Producers (routers, processing pipeline, integrations, cron)
    |
    | typed intents: {kind, uid, payload, urgency, dedup_key, source}
    v
Notification dispatch service (one module)
    - owns preference/consent checks
    - owns rate limits, dedup (by dedup_key), quiet hours, batching
    - owns channel selection (FCM push, in-app, integration webhook)
    - emits one delivery outcome per intent (delivered / suppressed(reason) / failed)
    |
    v
delivery drivers (FCM sender, webhook poster) — transport only, no policy

Suppressions are observable outcomes, not silent drops (AGENTS.md: silent UX healing allowed, silent ops is not — use existing domain metrics; only provider/mode fallbacks use record_fallback).

Scope

  • Introduce the intent type + dispatcher; migrate every producer listed above.
  • Delete before merge: direct calls to low-level send functions outside the dispatcher and delivery drivers; per-caller dedup/preference snippets superseded by the dispatcher.
  • CI guard: fail on new direct send-function calls outside the dispatcher module. Ratchet to zero.
  • Contract tests: preference-off suppression, dedup collision, rate-limit ceiling, quiet-hours deferral, delivery failure surfaced — through controllable seams per backend/docs/test_isolation.md.

Definition of done

  • All producers submit intents; zero direct send calls outside dispatcher/drivers (guard at zero).
  • Policy checks exist exactly once.
  • Suppression reasons observable in metrics/logs without raw content (log-sanitizer rules apply).
  • modal/job.py cron migrated (its deploy env constraints in AGENTS.md still hold).
  • Contract tests hermetic and in CI; backend/test.sh green.
  • Docs updated where service boundaries are described.

Non-goals

  • Changing what notifications exist or their copy/timing product behavior.
  • The proactive-notification content generation in utils/llm/proactive_notification.py — only its send path moves.
  • Client-side notification handling (Flutter/macOS).

Related

Activity

  1. added
    backend
    maintainer
    on Jul 12, 2026
  2. aryanorastar commented on Aug 30, 2026

    @aryanorastar
    Collaborator

    Opened #12409 as the first production convergence slice for this issue.

    What moved behind the new owner:

    • typed NotificationIntent values with explicit source/kind/policy
    • one dispatcher with observable dispatched, suppressed, and failed outcomes
    • both external integration notification routes (v1 and v2)
    • internal and realtime app-integration push helpers
    • the duplicated external-app hourly policy, now backed by the existing atomic Redis reservation primitive

    The external contract remains 10 notifications per app/user per UTC hour, including the existing rate-limit headers and 429 response. A deterministic test pins expiry to the next UTC-hour boundary. Plugin attribution and app/main-chat navigation payloads are also covered.

    Proof:

    • 9 focused dispatch/payload tests passed
    • 1 v1 validation regression test passed
    • 67 app/realtime coordinator tests passed
    • 50 mentor/proactive coordinator tests passed
    • 5 guard mutation tests passed
    • full local PR preflight passed: 117 checks in 135.8s
    • independent bounded push gate passed, including Pyright (0 errors), OpenAPI compatibility, import purity, async-blocker scan, backend tests, and Black

    The new AST ratchet records the remaining 20 legacy transport dependencies exactly and rejects new direct imports/calls, alias escapes, stale baselines, and baseline-assisted growth. Follow-up slices can only reduce that number toward the issue's zero-direct-sender definition of done.

    This PR is intentionally marked Part of #9518; it does not close the parent until the other producer families and shared preference/dedup/quiet-hour policies are migrated.

  3. added
    ux-polish
    p3
    docs-tooling
    retrieval-action
    and removed
    ux-polish
    p3
    docs-tooling
    retrieval-action
    on Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendmaintainerp3pythonrefactorux-polish

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions