//! **Why a copy an update could not move in place was not moved, written //! down first** — before anything else is tried — so the stop page and the //! bootstrap's status can name it (architect, 12 Oct 2026: on 255 the reason //! was lost because it was written only after a set-aside that failed). //! //! `crate::dev_heal` leaves such a copy exactly where it is. The mark sits //! beside the replica, outlives a relaunch, goes with a leave //! (`join::leave::delete_replica_files`), and is cleared when the copy opens. use std::path::{Path, PathBuf}; use crate::layout::Layout; /// Beside the replica: `replica.db.not-moved`. pub const NOT_MOVED_SUFFIX: &str = ".not-moved"; fn mark_path(replica: &Path) -> PathBuf { let mut name = replica.as_os_str().to_os_string(); name.push(NOT_MOVED_SUFFIX); PathBuf::from(name) } /// Remember that `workspace_id`'s copy, stamped `stamped`, could not be moved /// in place, and `why`. pub fn mark(layout: &Layout, workspace_id: &str, stamped: u32, why: &str) { let path = mark_path(&layout.replica(workspace_id)); let body = serde_json::json!({ "stamped": stamped, "why": why }).to_string(); // not a copy: the not-moved mark beside the copy. if let Err(error) = std::fs::write(&path, body) { eprintln!("cotton-client: could not remember that {workspace_id}'s copy was not moved: {error}"); } } /// Why `workspace_id`'s copy could not be moved in place, while that stands. pub fn why(layout: &Layout, workspace_id: &str) -> Option { let body = std::fs::read_to_string(mark_path(&layout.replica(workspace_id))).ok()?; let parsed: serde_json::Value = serde_json::from_str(&body).ok()?; Some(parsed.get("why")?.as_str()?.to_string()) } /// The copy is open: there is nothing more to explain — and, since what the /// heal calls once a copy moved is this, the writes it parked `newer-shape` /// (minted by a build ahead of the one this copy was at) are offered to the /// fold now: the ones its schema holds fold, the rest keep waiting /// (`cotton_store::rejected::refold_newer_shape`). A copy that is not there is /// left not there — opening it would mint an empty file at its address. pub fn clear(layout: &Layout, workspace_id: &str) { let replica = layout.replica(workspace_id); // not a copy: the note beside it. let _ = std::fs::remove_file(mark_path(&replica)); if !replica.is_file() { return; } // Every fold keeps `_computed` in step, this one too. cotton_tools::derive::install(); let now = chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true); let refolded = cotton_store::Driver::open(&replica) .and_then(|db| cotton_store::rejected::refold_newer_shape(&db, &now)); match refolded { Ok(report) if !report.is_empty() => eprintln!( "cotton-client: {workspace_id}: {} newer-shape write(s) folded, {} still wait", report.folded.len(), report.still_rejected.len() ), Ok(_) => {} Err(error) => eprintln!("cotton-client: {workspace_id}: its newer-shape writes were not offered again ({error})"), } } #[cfg(test)] #[path = "not_moved_tests.rs"] mod tests;