//! Intended losses when carrying a file into the current shape, one //! declaration per rise above the oldest stamp this build moves //! (`shape_move::LEDGER_BASE`, 141: the oldest shipped file). An empty list is //! an explicit promise that this schema discards no values. //! //! Each rise also says whether it is ADDITIVE, and [`floor`] is read off //! those flags: the lowest stamp whose exchange shape this build still serves. //! A stamp below the first declaration answers itself — an undeclared rise is //! taken as not additive. use crate::schema::ddl::SCHEMA_VERSION; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Loss { /// The table goes with every row, its values kept nowhere. DropTable { table: &'static str, }, /// A column's values carried into another table. No converter for one is /// written in this build, so a move that crosses one refuses /// (`shape_move`) and an unsent change it touches is held. FoldColumn { table: &'static str, column: &'static str, into: &'static str, }, } #[derive(Debug, Clone, Copy)] pub struct MigrationIntent { pub version: u32, /// The rise to this version only ADDS to the exchange shape — what one /// build sends another, the rows of the synced tables: on those it creates /// tables and appends columns by `ADD COLUMN`, and none is rebuilt or gone /// and no rule over a peer's rows changes. What a rise does to a local /// table (`_parked`, `_outbox` and kin: never synced), a search index or a /// plain index does not count. The rule is /// `shape_move::Crossing::not_additive`, and a guard holds every flag to /// it over the frozen shapes; `false` wherever no pair of them proves /// otherwise. pub additive: bool, pub reason: &'static str, pub losses: &'static [Loss], } pub const INTENTS: &[MigrationIntent] = &[ MigrationIntent { version: 142, additive: false, reason: "The object decides: four rules the copy kept and the object does not know go, each by rebuilding its table with every row and rowid — cotton_documents.status_changed_at and cotton_events.collection_id take null, cotton_notes' (managed_by IS NULL) OR (external_id IS NOT NULL) and cotton_money_accounts' kind IN ('asset', …) CHECKs go. No stored value is discarded.", losses: &[] }, MigrationIntent { version: 143, additive: false, reason: "The object decides: an event is the database's, never its calendar's — cotton_events' FOREIGN KEY (collection_id) REFERENCES cotton_collections(id) goes with the owning reference it made, by rebuilding cotton_events with every row and rowid, and its range and invitation indexes are keyed on starts_at and my_response, never collection_id, since no read reaches an event through a calendar; the object keeps no calendars, so an event naming one this copy never holds was kept out of its table. The move's finish settles every event the reference held out. No stored value is discarded.", losses: &[] }, MigrationIntent { version: 144, additive: false, reason: "The home-screen widgets left the phone (27 Oct 2026): cotton_widgets, one row per widget a phone had placed, goes with every row. Nothing draws a widget any more and nothing reads what one showed, so no value is carried anywhere.", losses: &[ Loss::DropTable { table: "cotton_widgets" }, ] }, MigrationIntent { version: 145, additive: false, reason: "A media-only message keeps its kind in its mark (7 Nov 2026), so a message whose bytes were left behind holds an empty body: cotton_msg_messages' (deleted_at IS NOT NULL) OR (trim(COALESCE(body, '')) <> '') OR (json_array_length(media) > 0) CHECK goes, by rebuilding the table with every row and rowid. No stored value is discarded.", losses: &[] }, MigrationIntent { version: 146, additive: true, reason: "Owed's cut and heads read one index (8 Nov 2026): idx_cotton_money_transactions_owed covers every money row naming a person by workspace, status, trash, person and day, with the columns the cut and the heads read. An index alone: no column, no row, no rule moves.", losses: &[] }, MigrationIntent { version: 147, additive: false, reason: "Cotton Keys becomes a whole password manager (12 Nov 2026): cotton_secrets' kind CHECK goes (the object's definition decides which kinds a secret may be: codes, wifi and addresses join), by rebuilding the table with every row and rowid, and it gains favourite_at, uses (NOT NULL DEFAULT 0), passkey_id, changed_at and history_enc, each empty on a row that was there. No stored value is discarded.", losses: &[] }, MigrationIntent { version: 148, additive: false, reason: "Cotton Calling (12 Nov 2026): a call made in Cotton lands in cotton_call_log beside the imported history, under source cotton, so the copy keeps no CHECK of its own on source (the object decides where a call came from), by rebuilding the table with every row and rowid. No column, no row and no stored value is discarded.", losses: &[] }, MigrationIntent { version: 149, additive: true, reason: "Cotton Chat's search (21 Nov 2026): fts_chat indexes each message's text by any three characters in a row, so a match needs no word boundary in any script. A search index alone, created empty and filled in the background newest first (fts_fill); no column, no row, no rule moves.", losses: &[] }, MigrationIntent { version: 150, additive: true, reason: "Cotton Chat's disappearing messages (21 Nov 2026): two partial indexes on cotton_msg_messages — the messages that name when they disappear (meta.cotton.expiresAt), and a chat's timer lines (meta.cotton.timer) by thread and moment. Indexes alone: no column, no row, no rule moves.", losses: &[] }, MigrationIntent { version: 151, additive: true, reason: "TRIM (21 Nov 2026): a receipt the object served is kept as its identity while its row says what it says. _writes gains implied (the set of its field names), _field_sets holds those sets, and _trim how far the in-place trim has walked. Two tables and a column added; nothing is moved or dropped here, and a copy's receipts are trimmed afterwards, in bounded steps, by crate::receipts::implied.", losses: &[] }, MigrationIntent { version: 152, additive: true, reason: "All his mail on the phone (MAIL ALL ON PHONE, 24 Nov 2026): cotton_emails gains to_line, cc_line, direction, message_id, message_refs, list_unsubscribe, list_unsubscribe_post, list_id, precedence, auto_submitted, gmail_category and archived_at by ADD COLUMN, each empty on a mail that was there until the object carries it again. Columns added; nothing is moved or dropped.", losses: &[] }, MigrationIntent { version: 153, additive: true, reason: "Comms' Senders at 100,000 mails (SENDERS INDEX, 24 Nov 2026): one index on cotton_emails, idx_cotton_emails_sender (workspace_id, lower(trim(from_email)), sent_at, from_name, trashed_at, from_email), so the Senders tab, a sender's page and Delete everything read the index, never every mail's body. No column changes.", losses: &[] }, MigrationIntent { version: 154, additive: true, reason: "Delete everything at once (DELETE FAST, 24 Nov 2026): the device-local _fts_purge, the search indexes an erase left words in, merged in the background; an erase deletes from the index plainly instead of securely. No synced table changes.", losses: &[] }, MigrationIntent { version: 155, additive: true, reason: "Post at his size, cold (POST AT SIZE, 26 Nov 2026): three partial indexes on cotton_emails, idx_cotton_emails_inbox (the Inbox by sender, with sent_at, created_at, id and thread_id), idx_cotton_emails_archived (mail archived by its own flag, by thread) and idx_cotton_emails_files (mail with attachments by sent_at), so Home, Archived and Files never read every mail's body. Indexes alone: no column, no row, no rule moves.", losses: &[] }, MigrationIntent { version: 156, additive: true, reason: "Hidden categories in Cinemas (HIDDEN, owner 26 Nov 2026; assets/design/cinemas/CONTRACT-2.md): cotton_groups gains hidden (0 or 1, default 0). On the phone it is a plain column; on the wire it is sealed inside the row (record_group keeps it out of seal.clear, HIDDEN 2), so the server never learns which are hidden. A title in a hidden category leaves every Cinemas answer outside Hidden and the flag syncs between his phones. One column by ADD COLUMN: no row, no rule moves.", losses: &[] }, MigrationIntent { version: 157, additive: true, reason: "Mail cuts (MAIL PREDICATE phase 2 and PHASE 2 PHONE 2, 27 Nov 2026): cotton_emails gains landed_at, when the mail landed on the server by its own clock, written as a pulled mail row brings it; _mail_landed keeps it beside the row (id, landed_at, the sender key and its domain), from triggers on cotton_emails and from the phone's landing fill for mail held before 157, so the fill never rewrites a mail's body; _mail_cuts, every mail cut this copy holds (with when he made it and its state), which the mail predicate reads; _mail_own, his own addresses, never covered. The sender index gains id, made in the background (index_later) with the 153 index dropped once it is held. Added only: no row of cotton_emails is rewritten.", losses: &[] }, MigrationIntent { version: 158, additive: true, reason: "The upload queue says where each file is (MEDIA DRAIN 1, 27 Nov 2026): the device-local _media gains last_code (the word a refusal or a wait is filed under: too_big, cotton_full, file_gone, refused, space_full, dead_key), network (any, the default and what every app does today, or wifi), asked_by (the app or act that queued the file) and source (where else the bytes live, photos for a copy of an iPhone Photos original). Columns appended to a local table by ADD COLUMN: no row, rule or synced table moves.", losses: &[] }, ]; /// Only declarations crossed by this move can authorize a loss. pub fn losses_between(from: u32, to: u32) -> impl Iterator { INTENTS .iter() .filter(move |intent| intent.version > from && intent.version <= to) .flat_map(|intent| intent.losses) } /// The lowest stamp from which every rise up to `stamp` is additive: a build /// at `stamp` serves a peer or a file stamped anywhere from it up to `stamp`, /// because every synced table that one holds is a table `stamp` holds with at /// most columns appended. A stamp this build has no declaration for — above /// its own schema, or below the first declared rise — answers itself: an /// undeclared rise is taken as not additive. pub const fn floor_of(stamp: u32) -> u32 { if stamp > SCHEMA_VERSION || stamp < INTENTS[0].version { return stamp; } let mut floor = INTENTS[0].version - 1; let mut at = 0; while at < INTENTS.len() { if INTENTS[at].version <= stamp && !INTENTS[at].additive { floor = INTENTS[at].version; } at += 1; } floor } /// The synced tables a rise that is not additive rebuilds in place, by its /// stamp: what a move from below it rebuilds beside whatever the additive /// rises around it append. Held to the frozen shapes by the rise's own proof. pub const REBUILDS: &[(u32, &[&str])] = &[ // 142: the copy keeps no rule the object does not know (27 Oct 2026). (142, &["cotton_documents", "cotton_events", "cotton_money_accounts", "cotton_notes"]), // 143: an event is the database's, never its calendar's (27 Oct 2026). (143, &["cotton_events"]), // 145: a media-only message with no bytes keeps an empty body (7 Nov 2026). (145, &["cotton_msg_messages"]), // 147: Cotton Keys' kinds are the object's to decide (12 Nov 2026). (147, &["cotton_secrets"]), // 148: a call made in Cotton lands beside the imported ones (12 Nov 2026). (148, &["cotton_call_log"]), ]; /// Every synced table a rise above `from` rebuilds ([`REBUILDS`]): the most a /// move from `from` to this build may rebuild. pub fn rebuilt_above(from: u32) -> std::collections::BTreeSet<&'static str> { REBUILDS.iter().filter(|(version, _)| *version > from).flat_map(|(_, tables)| tables.iter().copied()).collect() } /// The owning references a rise ends, by its stamp: `(child table, column)`. /// A row such a reference held out of its table — live by its receipts, its /// parent never here — is dormant in a file below the rise and belongs in its /// table above it, so the move's finish settles each one /// (`fold::settle_released`). Held to [`crate::schema::table_specs::OWNING_REFS`] /// by the rise's own proof. pub const RELEASES: &[(u32, &str, &str)] = &[ // 143: the object keeps no calendars; 151 of a phone's 154 events named // one, and none of them ever reached its table (27 Oct 2026). (143, "cotton_events", "collection_id"), ]; /// Every owning reference a rise above `from` ends ([`RELEASES`]). pub fn released_above(from: u32) -> impl Iterator { RELEASES.iter().filter(move |(version, _, _)| *version > from).map(|(_, table, column)| (*table, *column)) } /// The lowest stamp this build serves: [`floor_of`] its own schema, so /// `floor() <= SCHEMA_VERSION` always. It is about the EXCHANGE shape, so a /// rise that rebuilds only local tables leaves it where it was. Two builds /// serve each other when the NEWER one's floor is at or below the OLDER one's /// stamp. pub const fn floor() -> u32 { floor_of(SCHEMA_VERSION) } // A rise that discards or folds a stored value is never additive, and the // declarations climb one stamp at a time, which `floor_of` reads them by. const _: () = { let mut at = 0; while at < INTENTS.len() { assert!(!(INTENTS[at].additive && !INTENTS[at].losses.is_empty()), "a rise with a declared loss is not additive"); assert!(at == 0 || (INTENTS[at].version == INTENTS[at - 1].version || INTENTS[at].version == INTENTS[at - 1].version + 1), "batched intents share a stamp and the declared rises climb one stamp at a time"); at += 1; } };