Cotton code
124 lines · 5.9 KB · RustRaw
//! A group is a list a person picked by hand. On 11 Oct 2026 the owner's//! personal database held 273 todo groups nobody made — one per todo, named//! after it — written at every server start by a data step that turned the//! night pass's title-derived labels into groups (`cotton_tools::todo_labels`,//! d546bdb00, through `groups::gather`). This scan holds that only a person's//! verb makes a group: no production code outside `groups.rs` calls a//! function that writes one, except the callers on [`ALLOWED`].use std::path::{Path, PathBuf};/// The functions in `groups.rs` that write a new group row.const MAKERS: &[&str] = &["create_group", "gather"];/// The only production files that may name a maker, each with why.const ALLOWED: &[(&str, &str)] = &[    (        "cotton-tools/src/mcp/handlers/tables/groups.rs",        "where the makers live",    ),    (        "cotton-tools/src/mcp/handlers/tables/mod.rs",        "the verb table: `create_group` answers a person's call (the phone's group sheet, or a conversation acting for them)",    ),    (        "cotton-ffi/src/phone/book_acts.rs",        "Books' Goodreads import, which the person runs on their own export: each list it makes is a shelf they made by hand on Goodreads, named as they named it",    ),    (        "cotton-ffi/src/phone/songs.rs",        "Music's playlist files, which the person hands in from a folder they picked (music.lists): each .m3u or .m3u8 is a list they made by hand in their own player, named after its file, and a list already held by that name is appended to, not made again",    ),    (        "cotton-tools/src/mcp/handlers/records_1/note_tags.rs",        "Notes' hashtags, filed at every write that typed them (a phone's edit and an assistant's note write alike): a `#tag` written into a note files it under a group named `#tag` (made when none holds that name), and a `#` group left empty goes; each group is a word written into the note, never one derived from a title",    ),    (        "cotton-ffi/src/phone/notes.rs",        "Notes' hashtags: a `#tag` the person typed into their own note files it under a group named `#tag` (made when none holds that name), and a `#` group the person's edits leave empty goes; each group is a word they wrote by hand, never one derived from a title",    ),    (        "cotton-tools/src/synthetic/write.rs",        "the synthetic household of a Cotton Dev database, through the person's own verb; never a real database",    ),];fn crates_root() -> PathBuf {    cotton_store::manifest_dir()        .parent()        .expect("cotton-store sits in the crates directory")        .to_path_buf()}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<PathBuf> = 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;        }        let name = path.file_name().and_then(|n| n.to_str()).unwrap_or_default();        if !name.ends_with(".rs") || name.ends_with("_tests.rs") || name == "tests.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('\\', "/");        // Comments may name a maker; only code calls one.        let code: String = text            .lines()            .map(|line| line.split("//").next().unwrap_or_default())            .collect::<Vec<_>>()            .join("\n");        found.push((relative, code));    }}/// Whether `code` calls `word` or names it as a path (`groups::gather`) — the/// verb's name inside a string (the catalogue, the phone's method table) is/// no call.fn names(code: &str, word: &str) -> bool {    code.match_indices(word).any(|(at, _)| {        let before = &code[..at];        let after = code[at + word.len()..].trim_start();        let ident = |c: Option<char>| c.is_some_and(|c| c.is_alphanumeric() || c == '_');        if ident(before.chars().next_back()) || ident(code[at + word.len()..].chars().next()) {            return false;        }        before.ends_with("::") || after.starts_with('(')    })}/// RULE: no code path makes a group the person did not ask for — only `create_group`, a person's verb, writes a new group, and no production code outside groups.rs names a group maker except the verb table, Books' Goodreads import of the person's own shelves, Music's import of the person's own playlist files and the synthetic Cotton Dev household; a server-start data step once made 273 todo groups from the night pass's title-derived labels in the owner's personal database (11 Oct 2026).#[test]fn no_code_outside_the_verbs_makes_a_group() {    let root = crates_root();    let mut found = Vec::new();    let mut srcs: Vec<PathBuf> = 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();    srcs.sort();    for src in srcs {        walk(&src, &root, &mut found);    }    assert!(found.len() > 200, "the scan found only {} files", found.len());    let allowed: Vec<&str> = ALLOWED.iter().map(|(path, _)| *path).collect();    for path in &allowed {        assert!(found.iter().any(|(p, _)| p == path), "{path} is on ALLOWED but no longer exists");    }    let offenders: Vec<String> = found        .iter()        .filter(|(path, _)| !allowed.contains(&path.as_str()))        .flat_map(|(path, code)| {            MAKERS.iter().filter(|maker| names(code, maker)).map(move |maker| format!("{path} names {maker}"))        })        .collect();    assert!(        offenders.is_empty(),        "a group is made only by a person's create_group; these make one unasked: {offenders:#?}"    );}