//! WHERE THIS PHONE'S COPY STANDS, for a screen that has nothing to show //! (incident, 21 Nov 2026: the Worker answered 500 to every sync of a big //! object for hours, and a phone whose copy was being made again showed //! Notes' "No notes yet" and "That note is not here any more", and Chat's //! "No chats yet", while nothing of the person's had come down). //! //! Three states, told apart here once for every app: //! //! - [`Standing::Bringing`]: a signed-in copy whose first pull from the //! object has not ended — the person's things are on their way; //! - [`Standing::Unreached`]: the same, and the last pass failed (the object //! answered a 5xx, nothing reached it): Cotton could not be reached just //! now, and the loop tries again on its ladder; //! - [`Standing::Held`]: the copy holds what the object held at its last //! pass (or it is the phone's own database): an empty list is truly empty, //! and a record it lacks is gone. //! //! A list with no rows says the standing's words in place of its own empty //! line while the copy has not come whole, and a record not found says them //! in place of "no longer here" (`not_here_yet`), so "gone" is said only //! once the copy holds what the object holds. use serde_json::{json, Value}; use crate::methods::MethodError; use crate::shell::client::ClientController; /// The code a record not found is refused with while the copy has not come /// whole: not "gone", which only a copy that holds the object's rows can say. pub const NOT_HERE_YET: &str = "not_here_yet"; /// Whether a read's refusal says the row is not here: `record_not_found`, or /// `not_here_yet` while the copy has not come whole. A caller reading a row /// that may well be absent (a settings row, a document not yet arrived) takes /// both alike; only a screen says which. pub fn absent(code: &str) -> bool { matches!(code, "record_not_found" | NOT_HERE_YET) } /// Where the copy stands. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Standing { /// The copy holds what the object held at its last pass. Held, /// The first pull runs: the person's things are on their way. Bringing, /// The first pull has not ended and the last pass failed. Unreached, /// Bringing, but nothing comes until another device of the person's lets this one in (GETTING-IN.md): a screen /// reads `bringing`, and the words are the slim line's, never "Loading your data…", which a waiting phone's /// empty list said for as long as it waited (walk on dev, 22 Nov 2026). Waiting, } impl Standing { /// From what the copy and the loop say: its first pull still under way, /// and whether the last pass failed. pub fn judged(first_pull_underway: bool, last_pass_failed: bool) -> Standing { match (first_pull_underway, last_pass_failed) { (false, _) => Standing::Held, (true, false) => Standing::Bringing, (true, true) => Standing::Unreached, } } /// The word a screen reads (`held`, `bringing`, `unreached`). pub fn word(self) -> &'static str { match self { Standing::Held => "held", Standing::Bringing | Standing::Waiting => "bringing", Standing::Unreached => "unreached", } } /// The kit's words for it, none for a copy that holds its rows. pub fn words(self) -> Option { let line = match self { Standing::Held => return None, Standing::Bringing => "home.bringing", Standing::Unreached => "home.unreached", Standing::Waiting => return cotton_core::rules::shell_lines::resolve("signin", "getting_in.slim", &[], &[]), }; cotton_core::rules::shell_lines::LineSet::shipped("boot").and_then(|lines| lines.say(line, &[])) } } /// Where `client`'s copy stands now: the first pull is read off the copy's /// cursor, the last pass off the loop. pub fn of(client: &ClientController) -> Standing { // Only a copy of the person's object (its door held since `personal.connect`) pulls by the object's cursor: a // phone paired with a Mac holds no such cursor, and reading its absence as a first pull would say "bringing" for ever. if !client.signed_in() || client.object_door().is_none() { return Standing::Held; } let cursor = cotton_client::transport::object::CURSOR_KEY; let underway = client .with_file(|file| { file.driver .get("SELECT hlc_seen FROM _cursors WHERE author_device_id = ?", &[rusqlite::types::Value::Text(cursor.to_string())]) .map_err(|error| error.to_string()) .map(|row| row.and_then(|row| row.text("hlc_seen").map(str::to_string))) }) .is_ok_and(|held| cotton_client::transport::object::first_pull_underway(&held.into_iter().map(|held| (cursor.to_string(), held)).collect())); match Standing::judged(underway || waits_for_key(client), client.last_pass_failed()) { Standing::Bringing if crate::phone::getting_in::waiting_line(client).is_some() => Standing::Waiting, standing => standing, } } /// Whether the person's things have come down but wait for the account's key to open (big invented account on the /// emulator, 21 Nov 2026: a new phone's first pull of 2,800 sealed notes ended in five seconds with every row parked /// for want of the key, and Notes said "No notes yet" and the loading line was gone while the phone asked for the /// recovery code). Such a copy does not hold the person's things yet: it is still bringing them. Only while no data /// key is held, so a row that will not open even with the key never keeps a copy bringing for ever. pub fn waits_for_key(client: &ClientController) -> bool { use cotton_client::seal::DataKeyStore as _; let Ok(database) = client.current_database() else { return false }; if client.data_keys().data_key(&database).ok().flatten().is_some() { return false; } client .with_file(|file| { // Off the parks' keys (FIRST SYNC 8): never a scan of the parks themselves. cotton_client::seal::any_parked_with(file.driver.connection(), cotton_client::seal::SEAL_KEY_MISSING) }) // A copy that has never parked a row has no such table. .unwrap_or(false) } /// A list's answer with no rows says where the copy stands while it has /// not come whole: its `empty` line is the standing's words, and `copy` /// says which (`held`, `bringing`, `unreached`). `empty` is the key path of /// the list's empty line in `answer`. pub fn said_on_list(standing: Standing, answer: &mut Value, empty: &[&str], has_rows: bool) { answer["copy"] = json!(standing.word()); let Some(words) = standing.words().filter(|_| !has_rows) else { return }; let mut slot = &mut *answer; for key in empty { slot = &mut slot[*key]; } *slot = json!(words); } /// A refusal that a record is not here (`not_found`, `record_not_found`), /// said again while the copy has not come whole: `not_here_yet` with the /// standing's words, never "gone". Any other refusal is as it was. pub fn not_here(standing: Standing, refused: MethodError) -> MethodError { match standing.words() { Some(words) if matches!(refused.code.as_str(), "not_found" | "record_not_found") => MethodError::new(NOT_HERE_YET, words), _ => refused, } } #[cfg(test)] mod tests { use super::*; /// RULE (incident, 21 Nov 2026): a copy is "bringing" while its first pull runs, "unreached" when its last pass also failed, and "held" once a pull has ended — whatever the last pass did then: only a held copy says a list is empty or a record gone. #[test] fn a_copy_is_bringing_unreached_or_held() { assert_eq!(Standing::judged(true, false), Standing::Bringing); assert_eq!(Standing::judged(true, true), Standing::Unreached); assert_eq!(Standing::judged(false, true), Standing::Held); assert_eq!(Standing::judged(false, false), Standing::Held); assert_eq!(Standing::Bringing.words().as_deref(), Some("Loading your data…")); assert!(Standing::Unreached.words().is_some_and(|words| words.starts_with("Cotton could not be reached just now"))); assert_eq!(Standing::Held.words(), None); let gone = MethodError::new("not_found", "That note is not here any more."); assert_eq!(not_here(Standing::Held, gone.clone()).code, "not_found"); assert_eq!(not_here(Standing::Unreached, gone).code, NOT_HERE_YET); assert_eq!(not_here(Standing::Bringing, MethodError::new("bad_request", "no id")).code, "bad_request"); let mut list = json!({"rows": [], "empty": "No notes yet."}); said_on_list(Standing::Unreached, &mut list, &["empty"], false); assert_eq!(list["copy"], "unreached"); assert!(list["empty"].as_str().is_some_and(|words| words.starts_with("Cotton could not be reached"))); let mut chats = json!({"rows": [{"id": "a"}], "words": {"empty": "No chats yet."}}); said_on_list(Standing::Bringing, &mut chats, &["words", "empty"], true); assert_eq!(chats["words"]["empty"], "No chats yet.", "a list with rows keeps its own line"); } /// RULE (walk on dev, 22 Nov 2026): a copy waiting for another device to let it in reads `bringing` to a screen, /// and its empty list says the slim line, not "Loading your data…" for as long as it waits. #[test] fn a_waiting_copy_says_its_things_are_not_here_yet() { assert_eq!(Standing::Waiting.word(), "bringing"); assert_eq!(Standing::Waiting.words().as_deref(), Some("Your things from other devices aren't here yet.")); let mut list = json!({"rows": [], "empty": "No notes yet."}); said_on_list(Standing::Waiting, &mut list, &["empty"], false); assert_eq!((list["copy"].clone(), list["empty"].clone()), (json!("bringing"), json!("Your things from other devices aren't here yet."))); } }