//! `developer.stampStaleCopy` — COTTON DEV ONLY: this phone's copy stamped as //! the build BEFORE this one wrote it, so a walk can stage "the phone holds //! unsent changes, the Mac moves the schema, the phone updates" with one build //! (the release walk's case, architect ruling 10 Oct 2026, rule 3). The copy //! is let go of first and only its `user_version` moves: its receipts are left //! exactly as they were queued, and the next open finds what an updated app //! finds — another build's copy, which `personal.connect` makes again from //! the object, its unsent writes posted first. The real Cotton refuses it by //! name. use serde_json::{json, Value}; use cotton_client::connection::read_connection; use cotton_store::migrate::SCHEMA_VERSION; use crate::handle::CottonHandle; use crate::methods::MethodError; pub fn stamp(handle: &CottonHandle) -> Result { if handle.flavor() != cotton_core::flavor::Flavor::Dev { return Err(MethodError::new("not_dev", "developer.stampStaleCopy answers only in Cotton Dev")); } let Some(workspace_id) = read_connection(&handle.layout().connection_json()) .ok() .flatten() .and_then(|stored| stored.workspace_id) else { return Err(MethodError::new("no_database", "this phone holds no database to stamp")); }; let path = handle.layout().replica(&workspace_id); if !path.exists() { return Err(MethodError::new("no_database", "this phone holds no copy yet")); } // Let go of the file first: a controller still holding it would answer // from the open handle as if nothing changed. handle.client().detach(); let stamped = SCHEMA_VERSION - 1; let driver = cotton_store::Driver::open(&path).map_err(|error| MethodError::new("stamp_failed", error.to_string()))?; driver .exec(&format!("PRAGMA user_version = {stamped}")) .map_err(|error| MethodError::new("stamp_failed", error.to_string()))?; drop(driver); let unsent = cotton_client::unsent::unsent_changes(&path) .map(|changes| changes.iter().filter(|change| !change.is_bookkeeping()).count()) .unwrap_or(0); Ok(json!({ "workspaceId": workspace_id, "stamped": stamped, "unsent": unsent })) } #[cfg(test)] mod tests { use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine}; use super::*; fn open_as(path: &std::path::Path, flavor: cotton_core::flavor::Flavor) -> CottonHandle { CottonHandle::open(crate::handle::OpenConfig { mode: crate::handle::OpenMode::Phone { container_path: path.to_string_lossy().into(), previous_container_path: None, }, app_version: None, time_zone: Some("Etc/UTC".into()), device_secret: Some(URL_SAFE_NO_PAD.encode([29u8; 32])), vault_secret: None, flavor, manifest: None, }) .expect("a handle") } /// RULE: developer.stampStaleCopy — the release walk's way to stage a schema bump under unsent changes with one build — answers only in Cotton Dev, moves only the copy's stamp to the build before this one, and leaves its receipts as they were; the real Cotton refuses it by name and touches nothing. #[test] fn the_stale_stamp_is_cotton_devs_alone() { let dir = tempfile::tempdir().unwrap(); let handle = open_as(dir.path(), cotton_core::flavor::Flavor::Real); crate::methods::dispatch(&handle, "workspace.startLocal", &json!({})).unwrap(); let refused = crate::methods::dispatch(&handle, "developer.stampStaleCopy", &json!({})).expect_err("the real Cotton"); assert_eq!(refused.code, "not_dev"); let dir = tempfile::tempdir().unwrap(); let handle = open_as(dir.path(), cotton_core::flavor::Flavor::Dev); crate::methods::dispatch(&handle, "workspace.startLocal", &json!({})).unwrap(); let answer = crate::methods::dispatch(&handle, "developer.stampStaleCopy", &json!({})).expect("stamped"); assert_eq!(answer["stamped"], json!(SCHEMA_VERSION - 1)); let workspace = answer["workspaceId"].as_str().expect("the database").to_string(); let driver = cotton_store::Driver::open(handle.layout().replica(&workspace)).unwrap(); assert_eq!(cotton_store::migrate::current_version(&driver).unwrap(), SCHEMA_VERSION - 1); } }