//! Presence: the last-value-wins facts that were filling the op log. //! //! Three tables carry readings rather than history. A polling session stamps //! `cotton_sessions.listened_at` every thirty seconds, rewrote the whole //! `cotton_machines` reading beside it, and every tenth poll rewrote //! `cotton_account_usage` — and each stamp minted an op that stayed in the //! server's `_ops` log for ever. On the owner's file that was 58% of a 254 MB //! op log: 5 machine rows became 118,029 ops, 295 session rows became 126,954, //! and every one of those ops said the same thing as the next one about the //! same row. That log is gone (version 8 stopped writing it, version 11 dropped //! the table) and the rule below is what is left of the lesson: a device's //! OUTBOX fills the same way, one stamp at a time, and a queue nobody collapses //! is a queue that only grows while the device is offline. //! //! **A presence op SUPERSEDES its predecessor; it is not dropped.** Dropping //! these writes off the wire was the other candidate and it is wrong: the phone //! draws the machines screen and the sessions list out of its own replica //! (`cotton-ffi/src/phone/reads_work.rs`), so a device that never receives the //! stamps shows a machine last seen twenty days ago while it is live. What the //! log does not need is the INTERMEDIATE readings, because the row is //! last-value-wins: a puller that receives only the newest one lands on exactly //! the same row state. //! //! So the rule, applied at the ONE place an op is now written down — a device's //! `_outbox` ([`crate::outbox::queue_ops`]) — is: //! //! > When a new op is a presence op for a row, delete the row's existing //! > presence op whose patch columns are a SUBSET of the new one's and whose //! > hlc is older. //! //! Both conditions are load-bearing. **Subset**, because `stamp_heartbeat` //! sometimes carries a real column beside the reading (`account` the first //! time, `title` when the session has one) and superseding an op that names a //! column the newer one does not would lose that column from a replay. **Older //! hlc**, because the fold is per-column last-write-wins by hlc: superseding an //! op that is newer in hlc but earlier in the queue would make a replay from //! zero land on a different row than the file already holds. //! //! The subset rule is also what keeps a row's BIRTH in the log: an insert's //! patch carries `created_at`, which no later stamp does, so no stamp can ever //! stand in for it. //! //! The tables are a NAMED LIST, never a rule read off the shape of a patch. The //! collapse is safe only for a row whose intermediate states nobody reads, and //! that is a property of what the table MEANS — it cannot be inferred from the //! columns, so it is declared here and a new table is a line in this file. use serde_json::{Map, Value}; /// The reading columns, per table. /// /// A table absent from this list has no presence op and is never superseded. /// A column absent from a listed table's row is an ordinary column: an op that /// names one is still a presence op (the stamp carries them together), and the /// subset rule is what keeps it from being lost. pub static PRESENCE_COLUMNS: &[(&str, &[&str])] = &[ // The machine reading, on the device row it belongs to since version 14 — // `cotton_machines` was retired at that rung and its entry came here. // // NOT the whole table. A device row carries who admitted it and who cut it // off, and those are the file's most consequential facts; only the columns // the helper rewrites every fifteen minutes are a reading. The subset rule // is what keeps the two apart when a write names both. ( "cotton_devices", &[ "cpu_count", "mem_total_gb", "mem_free_pct", "load1", "disk_free_gb", "sessions_running", "build_lock_held", // The budget (version 24). Every one of these is measured on the // same fifteen-minute tick as the readings above it, so it belongs // on the same side of the subset rule: a helper rewriting them // must not read as a write about who admitted this Mac. "mem_committed_gb", "mem_budget_gb", "session_peak_gb", "session_peak_samples", "agents", "account_ids", // Which repositories this Mac can start a session in (version // 36): read off its own disk on the same tick. "repos", "host_version", "sampled_at", // Where this Mac's copy stands (version 132): the schema it is // stamped with and the moment it was last level with the Mac that // serves, written with every sample by the Mac itself. "schema_version", "copy_as_of", // DELIBERATELY ABSENT: `chip`, `os_version`, `disk_total_gb` // (version 39). Those are what the machine IS, recorded once and // rewritten only when the fact changes; the mirror writes them in // their own op, never in the sample's, so the op that carries one // is not a reading and nothing may supersede it. Listing them here // would let a later sample stand in for the op that first said // which chip this is — and a replay would lose it. // // DELIBERATELY ABSENT: `update_hold` (version 40). Why this Mac // is holding a downloaded update is written by the app in its // own op when the reason changes, and cleared when the install // goes; a reading superseding it would blank the one sentence // that says why a Mac is behind. ], ), // The agent login's usage windows (version 14), rewritten every time a // session's status line reports them — which is far more often than the // fifteen-minute sample beside it. `id` is the account row. ( "cotton_agent_accounts", &[ "five_hour_pct", "five_hour_resets_at", "weekly_pct", "weekly_resets_at", "seen_at", "seen_on_device_id", ], ), // NOT the whole table — a session row carries a summary, a title and a // shipped list that are history. These four are the liveness half. ( "cotton_sessions", &[ "last_active_at", "listened_at", "turn_count", "prompt_count", ], ), ]; /// The reading columns declared for a table, or `&[]`. #[must_use] pub fn presence_columns(table: &str) -> &'static [&'static str] { PRESENCE_COLUMNS .iter() .find(|(name, _)| *name == table) .map_or(&[], |(_, columns)| *columns) } /// Is this op a reading — a patch on a declared table that names at least one /// of its reading columns? #[must_use] pub fn is_presence_patch(table: &str, patch: Option<&Map>) -> bool { let Some(patch) = patch else { // A delete is never a reading: retracting a device's admission must // reach every peer, never be superseded by a later machine reading. return false; }; let columns = presence_columns(table); !columns.is_empty() && columns.iter().any(|column| patch.contains_key(*column)) } /// May `new_patch` (at `new_hlc`) stand in for `old_patch` (at `old_hlc`)? /// /// Both must be readings for the same table and row; the older op's columns /// must all appear in the newer one's; and the newer op must really be newer by /// hlc. See the module doc for why each half is there. #[must_use] pub fn supersedes( table: &str, new_patch: Option<&Map>, new_hlc: &str, old_patch: Option<&Map>, old_hlc: &str, ) -> bool { if !is_presence_patch(table, new_patch) || !is_presence_patch(table, old_patch) { return false; } if new_hlc <= old_hlc { return false; } let (Some(new_patch), Some(old_patch)) = (new_patch, old_patch) else { return false; }; old_patch .keys() .all(|column| new_patch.contains_key(column.as_str())) } #[cfg(test)] #[path = "presence_tests.rs"] mod tests;