capsule_core/notify/mod.rs
1//! Alert classes and their trigger predicates — the **one shared decision function** every
2//! platform evaluates instead of reimplementing the taxonomy (core half; SSoT:
3//! [Notifications]).
4//!
5//! # The surface
6//!
7//! [`evaluate()`] turns a snapshot of device-held state ([`NotifyInput`]) into the [`Alert`]s
8//! that are true at an instant. [`pre_arm_deadlines()`] returns the instant an OS timer must be
9//! armed for, **per class** — a class absent from the map has no timer to hold.
10//! [`next_deadline()`] is its minimum, for a caller that holds only one timer.
11//!
12//! Both are **pure**: no clock read, no socket, no SQLite, no `unsafe`, and no allocation beyond
13//! the returned vector. `now` is always an argument, so the whole surface is driven by a mocked
14//! clock in tests with no sleeps and no I/O — the same discipline as the recovery cadence whose
15//! projection it consumes ([`RecoveryFacts`]).
16//!
17//! # Why every input is caller-supplied
18//!
19//! Nothing in this crate holds the trigger state. There is no `last_completed_sync` column, no
20//! client-side quota type (server-held, and only as current as the last `GET /v1/quota`), and no
21//! persisted quarantine table — a refused sync entry is a per-entry verdict
22//! (`capsule_library::lifecycle::SyncApplyOutcome`), not a row. Pending drops live in the provisioning
23//! user's server-side inbox. So the predicate cannot read its own inputs; it takes them, which
24//! is also what keeps it pure.
25//!
26//! [`NotifyInput`] therefore carries **counts and instants only** — no album ids, no titles, no
27//! asset ids, nothing a server could author. That is forced rather than chosen: alert text is
28//! composed on-device from decrypted state, and a key-free server has no plaintext to compose
29//! from.
30//!
31//! # Delivery is not here
32//!
33//! This module decides *which classes are true* and *when to arm*. Presentation — a scheduled
34//! `UNCalendarNotificationTrigger`, a `NotificationManagerCompat` post, an in-app badge — is
35//! native per client, as is the permission prompt (asked the first time a class has something to
36//! say, never at launch). The `notification.*` catalog keys land with the implementing client
37//! slice, because the i18n guard requires a live consumer; this module emits a class and its
38//! parameters, never a string a user reads.
39//!
40//! # Pre-arming, and what it costs
41//!
42//! An alert whose trigger is a deadline the device can compute MUST be pre-armed at the moment
43//! that deadline becomes known, not evaluated when it expires — otherwise the staleness alert is
44//! starved by the very absence of background windows it exists to report.
45//!
46//! The consequence for this module is the reason [`pre_arm_deadlines()`] is narrower than
47//! [`evaluate()`]: an armed OS notification fires **without the app running**, so it cannot be
48//! re-checked at fire time. An instant is therefore only returned when the alert is certain to be
49//! true when it arrives — see [`AlertClass::pre_armable`] for which classes can be armed at all,
50//! and [`pre_arm_deadlines()`] for the three conditions that withhold one from a pre-armable
51//! class.
52//!
53//! Because the answer is a pure function of state, "re-arm on every state change that moves the
54//! deadline" reduces on the client to: recompute after any state change, then reconcile the
55//! timers against the map. One entry per class from one function is also why two live timers for
56//! one class is structurally impossible.
57//!
58//! # Determinism
59//!
60//! [`Alert::params`] is a [`BTreeMap`](std::collections::BTreeMap), and [`evaluate()`] emits in
61//! [`AlertClass::ALL`] order. Two calls on equal input are equal, byte-for-byte, through
62//! `serde`.
63//!
64//! [Notifications]: https://docs/design/notifications/
65
66pub(crate) mod class;
67pub(crate) mod evaluate;
68pub(crate) mod input;
69
70pub use class::{Alert, AlertClass, AlertSeverity};
71pub use evaluate::{DAY_SECS, SYNC_STALE_SECS, evaluate, next_deadline, pre_arm_deadlines};
72pub use input::{NotifyInput, QuotaAdvisory, QuotaFacts, RecoveryFacts, SyncFacts};