//! A runner holds no copy of any database: the one door every copy is opened//! through refuses it, before a directory is made or a byte fetched.use cotton_client::connection::{ConnectionFile, ConnectionMode, StoredConnection};use cotton_client::layout::Layout;use cotton_client::replica::{open_replica, ReplicaError};use cotton_client::seams::{SystemClock, SystemRandom};fn paired(layout: &Layout, scope: Option<&str>, takes_placements: bool) { std::fs::create_dir_all(layout.connection_json().parent().unwrap()).unwrap();ConnectionFile::at(layout.connection_json()) .write(StoredConnection { device_scope: scope.map(str::to_string), takes_placements, mode: ConnectionMode::Remote, base_url: "https://garden-dev.relay.invented.test".into(), workspace_id: Some("synthetic".into()), owner_id: Some("owner:invented".into()), connected_at: Some("2026-10-14T00:00:00.000Z".into()), }) .unwrap();}/// RULE: A runner (scope runner, with or without the placement grant) never opens or fetches a database copy — the replica door refuses it before it creates a directory or calls the bootstrap — because a runner holds no rows and no files of any database, and a copy it opened would be one the owner believes is gone.#[test]fn a_runner_is_refused_at_the_replica_door_and_fetches_nothing() {for takes in [true, false] {let dir = tempfile::tempdir().unwrap();let layout = Layout::new(dir.path()); paired(&layout, Some("runner"), takes); assert!(cotton_client::connection::holds_no_copy(&layout));letmut fetched = false;letmut bootstrap = |_: &std::path::Path| -> Result<(), String> { fetched = true;Ok(()) };let refused = open_replica(&layout, "synthetic", &SystemClock, &SystemRandom, &mut bootstrap); assert!(matches!(refused, Err(ReplicaError::Runner))); assert!(!fetched, "a runner fetched a copy"); assert!(!layout.replica("synthetic").exists()); assert!(!layout.replica("synthetic").parent().unwrap().exists()); }}/// RULE: A full device (no scope written, or scope full) is not a runner, so the runner refusal never takes a person's own copy away from them.#[test]fn a_full_device_is_not_a_runner() {let dir = tempfile::tempdir().unwrap();let layout = Layout::new(dir.path()); assert!(!cotton_client::connection::holds_no_copy(&layout)); paired(&layout, None, false); assert!(!cotton_client::connection::holds_no_copy(&layout)); paired(&layout, Some("full"), false); assert!(!cotton_client::connection::holds_no_copy(&layout));}fn rust_sources(dir: &std::path::Path, out: &mutVec<std::path::PathBuf>) {for entry in std::fs::read_dir(dir).unwrap() {let path = entry.unwrap().path();if path.is_dir() { rust_sources(&path, out); } elseif path.extension().is_some_and(|ext| ext == "rs") && !path.file_name().unwrap().to_string_lossy().ends_with("_tests.rs") { out.push(path); } }}/// The writable opens of a replica path in `text` that no `exists()` //// `is_file()` check in the same function stands before, as line numbers.fn unguarded_replica_opens(text: &str) -> Vec<usize> {// A file's own test module opens its synthetic copies freely.let lines: Vec<&str> = text.lines().take_while(|line| line.trim() != "#[cfg(test)]").collect();letmut found = Vec::new();for (at, line) in lines.iter().enumerate() {let code = line.split("//").next().unwrap_or("");letSome(open) = code.find("Driver::open(") else { continue };if !code[open..].contains("replica") {continue; }let checked = lines[..at] .iter() .rev() .take_while(|before| !before.trim_start().starts_with("pub fn ") && !before.trim_start().starts_with("fn ") && !before.trim_start().starts_with("pub(crate) fn ")) .any(|before| { before.contains(".exists()") || before.contains(".is_file()") || before.contains("holds_no_copy(") });if !checked { found.push(at + 1); } } found}/// RULE: Outside the replica door (`open_replica_with`, which refuses a runner), no client or Mac code opens a replica path writably without first checking the file is there or refusing a runner — a writable SQLite open creates the file, and on a runner that would be a database copy nobody asked for.#[test]fn no_code_creates_a_copy_outside_the_door() {let crates = std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").expect("cargo sets the manifest directory")).join("..");letmut files = Vec::new();for krate in ["cotton-client/src", "cotton-ffi/src"] { rust_sources(&crates.join(krate), &mut files); } assert!(files.len() > 50, "the scan read the tree");letmut offenders = Vec::new();for file in files {let text = std::fs::read_to_string(&file).unwrap();for line in unguarded_replica_opens(&text) { offenders.push(format!("{}:{line}", file.display())); } } assert!(offenders.is_empty(), "unguarded writable replica opens: {offenders:#?}");// The reader catches what it is for. assert_eq!(unguarded_replica_opens("fn f(replica: &Path) {\n let db = Driver::open(replica);\n}"), vec![2]); assert!(unguarded_replica_opens("fn f(replica: &Path) {\n if !replica.exists() { return; }\n let db = Driver::open(replica);\n}").is_empty());}