Cotton code
198 lines · 6.8 KB · RustRaw
//! A sign-in code a text carries is in no MCP read (`cotton_core::messages:://! sign_in`, `cotton_tools::mcp::sign_in_codes`): every door that answers//! message words answers them with the code withheld, a search for the//! code's digits finds nothing of it, and only what it is for is said.//!//! Synthetic throughout: an invented service's text on an invented number.mod support;use rusqlite::types::Value as SqlValue;use serde_json::{json, Value};use support::harness::{synthetic_uuid, Harness};use support::mcp::{call_tool_raw, mint_token};const AT: &str = "2026-10-15T09:00:00.000Z";const CODE: &str = "731905";const TEXT: &str = "G-731905 is your Examplegoogle verification code. Don't share it.";fn t(value: &str) -> SqlValue {    SqlValue::Text(value.to_string())}struct Seeded {    h: Harness,    secret: String,    thread: String,    message: String,}/// An SMS conversation with an invented sender: the code, then an ordinary/// line, so a door is seen answering words at all.fn seeded() -> Seeded {    let mut h = Harness::new();    let ws = h.workspace_id.clone();    let thread = synthetic_uuid("sms-thread");    h.run(        "INSERT INTO cotton_msg_threads (id, workspace_id, kind, title, last_message_at, created_at, updated_at)         VALUES (?, ?, 'dm', 'Examplegoogle', ?, ?, ?)",        &[t(&thread), t(&ws), t(AT), t(AT), t(AT)],    );    let line = |body: &str, sent_at: &str| {        let id = synthetic_uuid("sms-line");        h.run(            "INSERT INTO cotton_msg_messages               (id, thread_id, workspace_id, direction, channel, origin, body, media, external_id,                sent_at, created_at, reactions, meta)             VALUES (?, ?, ?, 'in', 'sms', 'imported', ?, '[]', ?, ?, ?, '[]', '{}')",            &[                t(&id),                t(&thread),                t(&ws),                t(body),                t(&format!("sms:{id}")),                t(sent_at),                t(sent_at),            ],        );        id    };    let message = line(TEXT, AT);    line(        "Your Examplegoogle account was opened on a new device",        "2026-10-15T09:01:00.000Z",    );    let secret = mint_token(&mut h, &["read", "write"], "Claude");    Seeded {        h,        secret,        thread,        message,    }}/// RULE: no MCP read door answers a sign-in code a text carries — not a thread read, a list, a record, a search hit or a summary — and a search for its digits finds nothing; what it is for may be said.#[test]fn no_read_door_answers_a_sign_in_code() {    let s = seeded();    let doors: Vec<(&str, Value)> = vec![        ("list_message_threads", json!({})),        ("read_message_thread", json!({ "threadId": s.thread })),        ("search_messages", json!({ "query": "Examplegoogle" })),        ("search_messages", json!({ "query": CODE })),        ("search_records", json!({ "query": "Examplegoogle" })),        ("search_records", json!({ "query": CODE })),        ("search_records", json!({ "query": "G-731905" })),        ("list_records", json!({ "table": "message" })),        ("list_records", json!({ "table": "thread" })),        (            "get_record_by_table",            json!({ "table": "message", "id": s.message }),        ),        (            "get_record_by_table",            json!({ "table": "thread", "id": s.thread }),        ),        ("summarize_records", json!({ "table": "message" })),        ("summarize_records", json!({ "table": "thread" })),        ("today_agenda", json!({ "today": "2026-10-15" })),    ];    let mut answered_words = false;    for (tool, args) in doors {        let said = call_tool_raw(&s.h, &s.secret, tool, args.clone());        assert!(            !said.is_error,            "{tool} {args} refused, so it proves nothing: {}",            said.text        );        assert!(            !said.text.contains(CODE),            "{tool} {args} answered the code: {}",            said.text        );        answered_words |= said            .text            .contains("is your Examplegoogle verification code");    }    assert!(        answered_words,        "a door answers the text's words, with the code withheld"    );}#[test]fn a_message_says_what_its_code_is_for_and_never_the_code() {    let s = seeded();    let said = call_tool_raw(        &s.h,        &s.secret,        "get_record_by_table",        json!({ "table": "message", "id": s.message }),    );    assert!(!said.is_error, "{}", said.text);    let row: Value = serde_json::from_str(&said.text).unwrap();    let text = row.to_string();    assert!(text.contains("\"code_for\":\"Examplegoogle\""), "{text}");    assert!(!text.contains("\"code\":"), "{text}");    assert!(!text.contains(CODE), "{text}");}#[test]fn a_search_for_the_codes_digits_finds_no_message() {    let s = seeded();    for tool in ["search_messages", "search_records"] {        let said = call_tool_raw(&s.h, &s.secret, tool, json!({ "query": CODE }));        assert!(            !said.text.contains(&s.message),            "{tool} found the text by its code: {}",            said.text        );    }}/// RULE: only the sign-in engine reads a code (`sign_in::newest_for`); no MCP source calls it or fills a message's `code` column, so the column is empty at every assistant door by construction.#[test]fn no_mcp_source_reads_a_code_or_fills_its_column() {    fn walk(dir: &std::path::Path, out: &mut Vec<(String, String)>) {        for entry in std::fs::read_dir(dir).unwrap() {            let path = entry.unwrap().path();            if path.is_dir() {                walk(&path, out);            } else if path.extension().is_some_and(|ext| ext == "rs") {                out.push((                    path.display().to_string(),                    std::fs::read_to_string(&path).unwrap(),                ));            }        }    }    let mut sources = Vec::new();    walk(        &std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").expect("cargo sets CARGO_MANIFEST_DIR")).join("src"),        &mut sources,    );    for (path, text) in sources {        assert!(            !text.contains("newest_for"),            "{path} reads a sign-in code for an assistant"        );        // A source that reads message rows never writes a `code` key.        let reads_messages = ["cotton_msg_messages", "message_extras", "sign_in::"]            .iter()            .any(|seam| text.contains(seam));        if !reads_messages {            continue;        }        for filled in [            "put(\"code\"",            "insert(\"code\"",            "\"code\".to_string()",            "\"code\".into()",        ] {            assert!(                !text.contains(filled),                "{path} fills a message's code column"            );        }    }}