//! A KEY SUCCESSION OF THE SERVING MAC (architect, 14 Oct 2026): the serving //! Mac re-keyed by its own act keeps serving. Its approval — given on another //! device — writes serving fact E+1 naming the new key, in the same commit as //! the succession of the old row. Not a take-over: the serving line says which //! machine serves, and the fact named a key only because the key was the //! machine's identity. So a succession fact changes nothing but the key: the //! same relay key, no backup access key of its own (the household's key as it //! stands), the next epoch, written by a serving author of the epoch it //! follows, and marked in its start positions with the key it succeeds. //! //! [`fact`] composes the row; [`refusal`] is the fold's guard on it, a pure //! function of the receipt and the facts before it, so every device answers //! alike. use rusqlite::types::Value as SqlValue; use serde_json::{Map, Value}; use crate::driver::Driver; use crate::error::Result; /// The key in a succession fact's `start_positions` naming the key it succeeds. pub const SUCCEEDS: &str = "succeeds"; /// Fact E+1 of `workspace_id` naming `successor`, when the latest fact names /// `predecessor` — `None` when the key re-keyed is not the serving one. `since` /// is at or after every stamp of the act that writes it: the predecessor's /// authors are cut off there (`fact_hooks::past_the_cut_off`), and the act is /// theirs. Its id is derived from the database and epoch, as every writer's is. pub fn fact( db: &Driver, workspace_id: &str, predecessor: &str, successor: &str, author_id: &str, since: &str, ) -> Result)>> { let Some(latest) = crate::serving::serving_fact(db, workspace_id)? else { return Ok(None); }; if latest.device_credential_id != predecessor { return Ok(None); } let epoch = latest.epoch + 1; let positions = crate::cursors::cursor_ceiling(db)?; let mut row = Map::new(); row.insert("epoch".into(), Value::from(epoch)); row.insert("device_credential_id".into(), Value::from(successor)); row.insert("author_id".into(), Value::from(author_id)); row.insert("relay_key".into(), latest.relay_key.map_or(Value::Null, Value::from)); row.insert("backup_access_key_id".into(), Value::Null); row.insert("since".into(), Value::from(since)); row.insert("confirmed_by".into(), Value::Null); row.insert( "start_positions".into(), serde_json::json!({ "positions": positions, "skipped": [], SUCCEEDS: predecessor }), ); let id = crate::protocol::derived_id::derived_id( "cotton_serving_machine", &[workspace_id, &epoch.to_string()], ); Ok(Some((id, row))) } /// Why a serving fact that says it succeeds a key is not taken, or `None` when /// it is no succession or a sound one. A succession names the key the fact /// before it names, a different key of its own, the same relay key and no /// backup access key, and comes from a serving author of the epoch before it; /// anything else is a take-over dressed as a re-key, and parks `conflict`. /// Before this file holds the fact before it, the receipt WAITS. pub fn refusal(db: &Driver, fields: &std::collections::BTreeMap, author: &str) -> Result> { let positions = match fields.get("start_positions") { Some(Value::String(text)) => serde_json::from_str::(text).ok(), Some(value) => Some(value.clone()), None => None, }; let Some(predecessor) = positions .as_ref() .and_then(|positions| positions.get(SUCCEEDS)) .map(|value| value.as_str().unwrap_or_default().to_string()) else { return Ok(None); }; let conflict = |why: &str| Ok(Some(format!("conflict: a serving fact that re-keys the serving Mac {why}"))); let workspace_id = fields.get("workspace_id").and_then(Value::as_str); let epoch = fields.get("epoch").and_then(Value::as_i64); let successor = fields.get("device_credential_id").and_then(Value::as_str).unwrap_or_default(); let (Some(workspace_id), Some(epoch)) = (workspace_id, epoch) else { return conflict("names no database or epoch"); }; let Some(before) = db.get( "SELECT device_credential_id, relay_key FROM cotton_serving_machine \ WHERE workspace_id = ? AND epoch = ? ORDER BY id DESC LIMIT 1", &[SqlValue::Text(workspace_id.into()), SqlValue::Integer(epoch - 1)], )? else { return Ok(Some(crate::rejected::waiting_for_serving_reason(author))); }; if before.text("device_credential_id") != Some(predecessor.as_str()) { return conflict("succeeds a key that did not serve the epoch before it"); } if successor.is_empty() || successor == predecessor { return conflict("names no new key"); } let relay_key = fields.get("relay_key").and_then(Value::as_str); if relay_key != before.text("relay_key") { return conflict("changes the relay key — a re-key changes nothing but the device key"); } if fields.get("backup_access_key_id").is_some_and(|value| !value.is_null()) { return conflict("takes a backup access key — a re-key changes nothing but the device key"); } let authors = crate::serving::authors_of_epoch(db, workspace_id, epoch - 1)?.unwrap_or_default(); if !authors.contains(author) { return conflict("is written by an author that did not serve the epoch before it"); } Ok(None) } #[cfg(test)] #[path = "serving_succession_tests.rs"] mod serving_succession_tests;