//! Slice 2b's guard: a served file has ONE author, and a write says who asked //! for it by its ACTOR — a device, a code credential, an MCP token, or a job //! named in code (`job:`). The `server:` author strings that each //! job used to stamp are gone, and this scan keeps them gone: one left behind //! is a second author stream on a file that may have one, or a label no reader //! of the new `job:` form will ever match. //! //! Tests are scanned too: a fixture that still writes `server:x` is asserting //! the old world, and passes for the wrong reason. use std::path::{Path, PathBuf}; fn crates_root() -> PathBuf { cotton_store::manifest_dir() .parent() .expect("cotton-store sits in the crates directory") .to_path_buf() } /// Every source file under `crates/*/src`, tests included, as (path from /// `crates/`, text with every `//` comment blanked). fn sources() -> Vec<(String, String)> { let root = crates_root(); let mut found = Vec::new(); let mut crates: Vec = std::fs::read_dir(&root) .expect("the crates directory") .filter_map(Result::ok) .map(|entry| entry.path().join("src")) .filter(|src| src.is_dir()) .collect(); crates.sort(); for src in crates { walk(&src, &root, &mut found); } assert!( found.len() > 200, "the scan found only {} files", found.len() ); found } fn walk(dir: &Path, root: &Path, found: &mut Vec<(String, String)>) { let Ok(entries) = std::fs::read_dir(dir) else { return; }; let mut paths: Vec = entries.filter_map(Result::ok).map(|e| e.path()).collect(); paths.sort(); for path in paths { if path.is_dir() { walk(&path, root, found); continue; } if path.extension().and_then(|e| e.to_str()) != Some("rs") { continue; } let Ok(text) = std::fs::read_to_string(&path) else { continue; }; let relative = path .strip_prefix(root) .unwrap_or(&path) .to_string_lossy() .replace('\\', "/"); found.push((relative, uncommented(&text))); } } /// `text` with every `//` comment blanked, so a sentence about the old world /// is not a literal. Line numbers are kept. fn uncommented(text: &str) -> String { text.lines() .map(|line| match line.find("//") { Some(at) => &line[..at], None => line, }) .collect::>() .join("\n") } /// The line of each string literal in `text` that starts `"server:` and a /// letter — an author constant's shape. `"server:{}"` (a format string) and /// `"server:"` (a bare prefix) are not: those are the `cotton_vault_keys` row /// keys of a server's wrap (`vault/boot.rs`, `vault/rotate.rs`, the FFI's /// `methods/vault.rs`), which are not authors. fn server_authors(text: &str) -> Vec { const OPEN: &str = "\"server:"; let mut found = Vec::new(); let mut from = 0; while let Some(at) = text[from..].find(OPEN) { let offset = from + at; let next = text[offset + OPEN.len()..].chars().next(); if next.is_some_and(|c| c.is_ascii_alphabetic()) { found.push(text[..offset].matches('\n').count() + 1); } from = offset + OPEN.len(); } found } /// RULE: No source under crates/*/src, tests included, holds a "server:" string literal — a write names its asker as an actor and a job as job:. #[test] fn no_server_author_constant_remains() { let mut offenders = Vec::new(); for (path, text) in sources() { for line in server_authors(&text) { offenders.push(format!("{path}:{line}")); } } assert!( offenders.is_empty(), "a `server:` author string remains — a job writes as `job:` \ (`cotton_tools::mcp::writer::job`), a caller as its device, credential or token:\n {}", offenders.join("\n ") ); } #[test] fn the_scan_refuses_what_it_says_it_refuses() { assert_eq!(server_authors("const A: &str = \"server:gmail\";"), vec![1]); assert_eq!(server_authors("fn a() {}\nlet x = r#\"server:mcp\"#;"), vec![2]); assert!(server_authors("format!(\"server:{}\", id)").is_empty()); assert!(server_authors("id.starts_with(\"server:\")").is_empty()); assert!(server_authors("\"cotton-server: refused\"").is_empty()); assert!(server_authors(&uncommented("// the old \"server:x\" author\nfn a() {}")).is_empty()); }