//! `developer.stuckChange` — COTTON DEV ONLY: two changes this copy made that the server refused for good, as UNSENT //! HEAL shows them (`sync.stuck`, a parked push, said at once): two invented people, written as any write is and //! queued, then parked from their receipts as the server's refusal parks a push (one refused now, one two days ago). They are real writes, //! so Send again puts them back in the queue and a real pass sends them; Let go ends their park and they stay on this //! phone. For a walk of the Sync page on a simulator. The real Cotton refuses it by name. //! //! Real writes, never invented bytes: a queued op whose bytes do not decode stops every pass on the copy (the walk, //! 26 Nov 2026: the Sync page said "Last synced at 9:46 am" for hours while two such rows were queued). //! //! ```text //! developer.stuckChange {} → { parked: 2 } //! ``` use serde_json::{json, Map, Value}; use cotton_store::rejected::{Class, Direction}; use cotton_tools::mcp::handlers::common::insert_row; use crate::handle::CottonHandle; use crate::methods::MethodError; pub fn stuck_change(handle: &CottonHandle, _request: &Value) -> Result { if handle.flavor() != cotton_core::flavor::Flavor::Dev { return Err(MethodError::new("not_dev", "developer.stuckChange answers only in Cotton Dev")); } let client = handle.client(); // A copy of this phone's own sends nothing: its writes are never queued, so nothing could be parked. if !client.signed_in() { return Err(MethodError::new("not_signed_in", "developer.stuckChange needs a signed-in copy: a local one queues nothing to send")); } let now = client.now(); let two_days_ago = (now - chrono::Duration::days(2)).to_rfc3339_opts(chrono::SecondsFormat::Millis, true); let just_now = now.to_rfc3339_opts(chrono::SecondsFormat::Millis, true); // Fresh ids each time, so every walk has its own two. let round = now.timestamp_millis(); let people = [(format!("invented-stuck-{round}-a"), "Invented Stuck A", just_now), (format!("invented-stuck-{round}-b"), "Invented Stuck B", two_days_ago)]; // Parked once the server's copy of each has come back (a few seconds; at most ten): the pass a write starts sends // it, and a row that moves after its push was parked offers the park again, which then goes and ends it (the // walk, 26 Nov 2026). Parked after, the row does not move again and the park stands until the person acts. let made = make(&client, &people); let parked = made?; if parked == 0 { return Err(MethodError::new("not_written", "developer.stuckChange wrote two people, but no receipt of them was kept")); } Ok(json!({ "parked": parked })) } /// The two people written, and each write parked as the server's refusal parks a push. Answers how many parked. fn make(client: &crate::shell::client::ClientController, people: &[(String, &str, String)]) -> Result { super::cotton_chat::write(client, "developer.stuckChange", |ctx| { for (id, name, _) in people { let mut row = Map::new(); row.insert("full_name".into(), json!(name)); insert_row(ctx, "cotton_people", id, row).map_err(|error| error.to_string())?; } Ok(json!({})) })?; let ids: Vec = people.iter().map(|(id, _, _)| rusqlite::types::Value::Text(id.clone())).collect(); for _ in 0..40 { let back = client .with_file(|file| { file.driver.get("SELECT COUNT(DISTINCT row_id) AS n FROM _writes WHERE tbl = 'cotton_people' AND row_id IN (?, ?) AND hlc LIKE '%:object'", &ids).map_err(|error| error.to_string()) .map(|row| row.and_then(|row| row.int("n")).unwrap_or(0)) }) .unwrap_or(0); if back >= 2 { break; } std::thread::sleep(std::time::Duration::from_millis(250)); } client .with_writer(|file| { let mut parked = 0; for (id, _, at) in people { // The op this phone wrote, from its receipt (never the server's copy); a queued copy leaves the queue. let written = file.driver.all("SELECT * FROM _writes WHERE tbl = 'cotton_people' AND row_id = ? AND bytes IS NOT NULL AND hlc NOT LIKE '%:object'", &[rusqlite::types::Value::Text(id.clone())]).map_err(|error| error.to_string())?; for row in &written { let op = cotton_store::stored::receipt_row(row)?; cotton_store::rejected::park(file.driver, &op, Direction::Push, Class::Conflict, "bad_column: an invented refusal", at).map_err(|error| error.to_string())?; // not a copy: a dev fixture's invented refusal: the op is parked in the same file first. file.driver.run("DELETE FROM _outbox WHERE op_id = ?", &[rusqlite::types::Value::Text(op.op_id.clone())]).map_err(|error| error.to_string())?; parked += 1; } } Ok(parked) }) .map_err(|error| MethodError::new("unwritable", error)) }