// Table → screen family: how a change in the file reaches the screens.//// The replica IS the cache (design P5), so there is nothing to expire and no// cache library — what a screen needs is the list of TABLES that moved. A row// can change three ways: this device's write (the `write` answer carries// `tables`), a fold of another device's ops (`pull_and_fold` carries the tables// of the APPLIED ops, never re-derived), and a server worker filing a capture.// Only the first starts on a screen, so invalidation hangs off the tables.//// The entries, and two rules — the count is deliberately not written down// here, because a number in prose beside a list that grows is a number that is// wrong (it said twenty-eight while the list held thirty-three) —//// * **a table appears under exactly ONE entry**, so a fold that touched four// tables of one entry invalidates that entry's families once, not four// times; and// * **a family is invalidated ONCE however many of its tables changed**,// which is what de-duplicating by VALUE buys.//// What it is NOT is a query-key map. There is no query cache to invalidate: a// family is a set of `@Observable` models, and invalidation is "re-read".//// **This file is hand-held, and it is guarded rather than generated.** Design// P5 asked for a generated `InvalidationMap.swift`; the generator it named was// replaced by `cotton-gen` in step 5's 5b, and `cotton-gen` renders// `cotton_core::design` — a Rust record that does not hold the registry and// that step 5 does not add. So: declared here, and `InvalidationMapTests` holds// the map to its own invariants. The parity half went with the registry it// guarded — the same arrangement `MetricsTests` already made, recorded in that// test's own header and in Cotton note "The phone and Mac app — how a screen is built".importFoundation/// A set of screens that read the same tables.////// One per tool, plus the two cross-cutting roots (`core` and `summary` — a/// summary is read by its OWN root on every record page, never by the tool's,/// so four surfaces cannot disagree about who wrote a paragraph).publicenumScreenFamily: String, CaseIterable, Sendable, Comparable {/// The transparency log: what an assistant did in this database, and what/// a run of one cost. Server-written both, so a FOLD is the only way/// either moves here.case activity/// The owner's alarms (`/alarms`). Its own family: nothing else reads the/// table, and an alarm set on another device must ring in this list.case alarmscase bincase calendarcase callLogcase core/// The devices admitted to this database — `read.devices`, drawn by the/// Devices tool, the settings sheet's row and Home's one line about a/// Mac. Its own family beside `sessions`: a device row also moves the/// sessions strip, and a screen that draws the LIST must not be asked to/// re-read for a session's prompt landing.case devices/// The dictation ledger the keyboard settings page counts.case dictationcase documentscase email/// Feedback about a project — `cotton_feedback`, read by a project's own/// feedback section. (A shake report goes to Cotton's database instead.)case feedbackcase goalscase locationscase memorycase messagescase moneycase moodcase peoplecase photoscase projectscase sessions/// The owner's own API keys and secure notes. Its own family rather than a/// third table on `activity`, because it moves for a different reason and/// is read on a different screen.case settingscase storagecase summarycase todoscase videoscase decisioncase post// gen-kind: screen-familiespublicstaticfunc < (lhs: ScreenFamily, rhs: ScreenFamily) -> Bool { lhs.rawValue < rhs.rawValue }}publicenumInvalidationMap {publicstructEntry: Sendable, Equatable {/// Physical table names in the file.publiclet tables: [String]/// The screen families that re-read when any of those tables change.publiclet families: [ScreenFamily] }publicstaticlet entries: [Entry] = [Entry( tables: ["cotton_todos", "cotton_todo_recurrences"], families: [.projects, .todos] ),Entry( tables: ["cotton_msg_threads", "cotton_msg_messages", "cotton_msg_reactions", "cotton_msg_receipts"], families: [.messages, .projects, .summary] ),Entry( tables: ["cotton_call_log"], families: [.callLog] ),Entry( tables: ["cotton_memories", "cotton_memory_projects", "cotton_project_links"], families: [.core, .memory, .projects, .summary] ),Entry(// `cotton_session_tasks` is the agent's own task list, mirrored// whole on every `log_session` (schema v31): without it here a// session's page never sees its tasks move. tables: ["cotton_sessions", "cotton_prompts", "cotton_session_messages", "cotton_session_tasks"], families: [.sessions] ),Entry(// The Macs and the agent logins (schema v14, which retired// `cotton_machines` and `cotton_account_usage`). A device row moves// every fifteen minutes with the sample it writes about itself, and// the sessions screens draw that strip.// The two SAMPLE tables ride here as well: a reading landing is// what moves the strip's "last reading N min ago" and the thirty-day// history under a Mac, and a table nobody claims reaches no screen// at all. tables: ["cotton_devices", "cotton_agent_accounts","cotton_device_samples", "cotton_account_usage_samples", ], families: [.sessions, .devices] ),Entry(// The repository tables. They move by FOLD and never by a write// this phone made — the `repos` worker on the serving Mac is their// only writer, and `repo.hold` / `repo.merge` / `repo.refresh` are// routes it performs rather than rows the phone files. So without// an entry here a release landing, a bundle being written or a// pull request's checks turning green would reach every device's// database and no device's screen: the `data` event names tables,// and a table nobody claims invalidates nothing.//// `.core` because the repository screens read on `[.core]`, and// `.projects` because a project's page states its repositories'// counts and the repository page reads on both.// CI's two SYNCED tables ride here too (schema v20): a job// turning red reaches every device's database, and without a claim// it would reach no device's screen. The other two of that rung —// `cotton_repo_runners` and `cotton_repo_vars` — are NOT here.// `cotton_repo_vars` is server-only (`SERVER_ONLY_TABLES`) and a// table nothing hands over cannot arrive in a `data` event at all.// `cotton_repo_runners` syncs since v29, and is still absent because// no read draws it: `code.runners` asks the serving Mac, whose live// answer no arriving row could stand in for. tables: ["cotton_repos", "cotton_repo_pulls", "cotton_repo_issues","cotton_repo_comments", "cotton_repo_commits","cotton_repo_bundles", "cotton_repo_clones", "cotton_repo_releases","cotton_repo_runs", "cotton_repo_jobs", ], families: [.core, .projects] ),Entry( tables: ["cotton_workspaces"], families: [.core] ),Entry( tables: ["cotton_msg_owner_settings"], families: [.core] ),Entry( tables: ["_media"], families: [.core, .storage] ),Entry(// The person's own order of Home — the caps, the objects, and which// tools they put away. `.core` and nothing else: Home is the one// screen that draws it, and the Mac's sidebar draws Home's model.// An arrangement made on the phone has to reach the Mac's column// without a cold start, which is what this entry buys. tables: ["cotton_home_arrangement"], families: [.core] ),Entry(// A screen's board and the database's theme (design note// 26203e21): a layout or a skin changed on another device is a// row that syncs, and Home, the Tools screen and the database// page redraw from it without a cold start. tables: ["cotton_board_definitions", "cotton_theme_definitions"], families: [.core] ),// ── 4p's two, arriving with schema v46 ────────────────────────────Entry(// ONE entry for the two because they answer one question on one// surface — "what has been done in my database, and by whom".// Both are written by the SERVER and by nothing on this device, so// a fold is the only way either of them ever moves here: without// this entry the transparency log shows what it showed when the// screen was opened, and an assistant that has been working since// is invisible until a cold start. tables: ["cotton_mcp_audit", "cotton_assistant_runs"], families: [.activity] ),Entry(// An audit row lands when an assistant CALLS something; a secret// row lands when somebody creates or deletes one. Different reason,// different screen, so its own entry.//// `bin` since schema v6: a secret is BINNABLE now, so one moved to// the bin on another device has to reach `/bin` without a cold// start. `settings` is the AI screen's keys card, which is the same// table read a different way. tables: ["cotton_secrets"], families: [.bin, .settings] ),Entry(// The vault's own tables. They move when a SERVER wraps the master// key for this device or rotates it, and a fold is the only way// either ever moves here — so without this entry a phone that was// waiting for its Mac goes on saying "waiting" until it is// relaunched, with the wrap already sitting in its replica. tables: ["cotton_vault_keys", "cotton_vault_state", "cotton_secret_placements"], families: [.settings] ),Entry(// The words dictation listens for (the Voice words tool): a word// added on another device reaches an open list at its fold. tables: ["cotton_voice_vocabulary"], families: [.settings] ),Entry( tables: ["cotton_email_threads", "cotton_emails", "cotton_email_participants", "cotton_blocked_senders", "cotton_email_senders"], families: [.bin, .email, .summary] ),Entry( tables: ["cotton_people"], families: [.email, .people, .summary] ),// `bin` here as well as on the email tables: the bin lists what was// binned whatever its kind, and a binned document has to reach it.//// `cotton_thought_attachments` (store schema 36) rides with the// captures it belongs to: `read.thoughts` answers each thought's// pictures, so a picture filed onto a thought from another device —// or taken off one — has to wake the same screens a capture does, the// thought's own page first.Entry( tables: ["cotton_inbox_items", "cotton_thought_attachments", "cotton_documents", "cotton_comments"], families: [.bin, .core, .documents, .storage] ),Entry( tables: ["cotton_events", "cotton_collections"], families: [.calendar, .core] ),Entry( tables: ["cotton_money_accounts", "cotton_money_transactions", "cotton_money_transaction_lines", "cotton_money_statements", "cotton_money_statement_lines", "cotton_money_evidence"], families: [.money] ),Entry( tables: ["cotton_google_accounts"], families: [.calendar] ),Entry( tables: ["cotton_feedback"], families: [.projects, .feedback] ),Entry( tables: ["cotton_moods"], families: [.mood] ),Entry( tables: ["cotton_photos"], families: [.photos, .storage] ),Entry( tables: ["cotton_videos"], families: [.storage, .videos] ),Entry( tables: ["cotton_media_sightings"], families: [.photos, .storage, .videos] ),Entry( tables: ["cotton_media_faces"], families: [.photos, .videos] ),Entry( tables: ["cotton_media_albums", "cotton_media_album_items"], families: [.photos] ),Entry( tables: ["cotton_media_duplicates"], families: [.photos, .storage, .videos] ),Entry( tables: ["cotton_person_suggestions"], families: [.people] ),// The four the Expo registry never named (lane I1): each was a screen// that never re-read on a fold, and nothing about it looked wrong.Entry( tables: ["cotton_goals"], families: [.goals] ),Entry( tables: ["cotton_locations"], families: [.locations] ),Entry( tables: ["cotton_dictations"], families: [.dictation] ),Entry( tables: ["cotton_alarms"], families: [.alarms] ),Entry( tables: ["cotton_decisions"], families: [.decision] ),Entry(// Storage's keep rules and each device's last reading: a rule set// from another device, or that device's new reading, redraws an// open Storage list — and the phone's device row, whose file// choices are those same rules (`KeepOnThisPhone`). tables: ["cotton_storage_rules"], families: [.storage, .devices] ),Entry(// What a phone keeps: a rule written on this phone, from a// group's page, a record's menu or another device, redraws the// phone's device row the moment it lands. tables: ["cotton_device_keep"], families: [.devices] ),Entry( tables: ["cotton_posts"], families: [.post] ),// gen-kind: invalidation-entries ]/// Every table the map knows.publicstaticlet tables: Set<String> = Set(entries.flatMap(\.tables))/// The families to re-read for a set of changed tables.////// De-duplicated BY VALUE, not by identity: a family legitimately rides/// several entries (`projects` sits under both the todos and the messages/// entries, because a project's page reads both), and "once each" is/// the property a fold's invalidation must keep — a duplicate is the same/// screen re-reading twice for one fold.////// A table nobody claims contributes nothing rather than invalidating/// everything. The map is keyed on TABLE NAMES exactly as the `data` event/// lists them, and a fold moves bookkeeping alongside the rows a screen/// draws: `_cursors` and `_shadow_writes` (how far this file has seen each/// author, and the authors a row's own stamp does not name), `_outbox` for/// this device's own writes, and the FTS shadow tables behind every search/// index. None of them is a screen, and a catch-all here would redraw the/// whole app on every single fold.publicstaticfunc families(for changed: someSequence<String>, origin: String?) -> [ScreenFamily] {// The one place the origin is read: see the overload below.let all = families(for: changed)guard origin == ClientEvent.localMediaOrigin else { return all }return all.filter { $0 == .storage } }/// The families for `changed`, read with the event's ORIGIN: an/// origin-`media` event (`ClientEvent.localMediaOrigin`) is bytes moving on/// this device and no row, and reaches `.storage` alone. The `_media`/// entry keeps `.core` because a CAPTURE writes `_media` too, and that/// one IS rows.publicstaticfunc families(for changed: someSequence<String>) -> [ScreenFamily] {let moved = Set(changed)var seen: Set<ScreenFamily> = []var out: [ScreenFamily] = []for entry in entries where entry.tables.contains(where: moved.contains) {for family in entry.families where !seen.contains(family) { seen.insert(family) out.append(family) } }return out }}/// The window a burst of `data` events is coalesced over.////// Leading AND trailing: the first event redraws at once (a tap must feel/// immediate) and the last one redraws again (a fold that arrived 40 ms later/// must not be lost).publiclet COALESCE_WINDOW_MS = 250