Cotton code
97 lines · 5.0 KB · RustRaw
//! Blocking a sender touches that sender's mail, not the file's.//!//! The architect's first sweep blocks senders one MCP call at a time. On 11//! Oct 2026 each call held the serving Mac's personal file for 17.5 s//! (06:14:29Z: 194,183,597 steps, 5,862 facts folded) — far more than the//! sender's own mail asks of it.//!//! Every value is invented ([nothing-real-ever-goes-in-cotton-s-public-tree]).mod support;use rusqlite::types::Value as SqlValue;use serde_json::json;use support::harness::Harness;use support::mcp::{call_tool, mint_token};const NOW: &str = "2026-10-05T09:00:00.000Z";const SENDER: &str = "a-newsletter@example.invalid";/// A file holding `others` emails from forty other senders in threads of five,/// and `own` emails from [`SENDER`], each its own thread.fn file_with(own: i64, others: i64) -> (Harness, String) {    let mut h = Harness::new();    let secret = mint_token(&mut h, &["read", "write"], "Claude");    let workspace = || SqlValue::Text(h.workspace_id.clone());    let threads = others / 5 + own;    h.run(        "WITH RECURSIVE n(i) AS (SELECT 0 UNION ALL SELECT i + 1 FROM n WHERE i + 1 < ?1) \         INSERT INTO cotton_email_threads (id, workspace_id, external_key, subject, first_at, last_at, email_count, created_at, updated_at) \         SELECT printf('thread-%08d', i), ?2, printf('key-%08d', i), 'An invented subject', ?3, ?3, 1, ?3, ?3 FROM n",        &[SqlValue::Integer(threads), workspace(), SqlValue::Text(NOW.into())],    );    h.run(        "WITH RECURSIVE n(i) AS (SELECT 0 UNION ALL SELECT i + 1 FROM n WHERE i + 1 < ?1) \         INSERT INTO cotton_emails (id, workspace_id, thread_id, external_id, subject, from_name, from_email, sent_at, body, created_at) \         SELECT printf('other-%08d', i), ?2, printf('thread-%08d', i / 5), printf('<other-%d@example.invalid>', i), \                'An invented subject', 'An Invented Sender', printf('sender%d@example.invalid', i % 40), \                strftime('%Y-%m-%dT%H:%M:%fZ', '2026-01-01', printf('+%d minutes', i)), 'An invented body.', ?3 FROM n",        &[SqlValue::Integer(others), workspace(), SqlValue::Text(NOW.into())],    );    h.run(        "WITH RECURSIVE n(i) AS (SELECT 0 UNION ALL SELECT i + 1 FROM n WHERE i + 1 < ?1) \         INSERT INTO cotton_emails (id, workspace_id, thread_id, external_id, subject, from_name, from_email, sent_at, body, gmail_id, gmail_account, created_at) \         SELECT printf('own-%08d', i), ?2, printf('thread-%08d', ?4 + i), printf('<own-%d@example.invalid>', i), \                'An invented offer', 'A Newsletter', ?5, \                strftime('%Y-%m-%dT%H:%M:%fZ', '2026-01-01', printf('+%d minutes', i)), 'An invented offer.', \                printf('%x', 1000000 + i), 'owner@example.invalid', ?3 FROM n",        &[            SqlValue::Integer(own),            workspace(),            SqlValue::Text(NOW.into()),            SqlValue::Integer(others / 5),            SqlValue::Text(SENDER.into()),        ],    );    // Settled by the reader, as every thread is after its first pass (v107).    h.run(        "UPDATE cotton_email_threads SET sender_kind = 'service', sender_address = \           (SELECT e.from_email FROM cotton_emails e WHERE e.thread_id = cotton_email_threads.id ORDER BY e.sent_at LIMIT 1)",        &[],    );    (h, secret)}/// Steps the block itself takes — the call's own statements, not the/// harness's bookkeeping after it — with the heaviest named on failure.fn steps_to_block(h: &Harness, secret: &str, own: i64) -> (u64, Vec<(u64, String)>) {    h.db.watch_statements();    let answer = call_tool(h, secret, "block_sender", json!({ "address": SENDER }));    assert!(answer.get("address").is_some(), "{answer}");    let by: Vec<(u64, String)> = h        .db        .steps_by_statement()        .into_iter()        .filter(|(_, sql)| !sql.contains("NOT EXISTS (SELECT 1 FROM _writes"))        .collect();    let filed = h        .db        .get("SELECT count(*) AS n FROM cotton_email_threads WHERE sender_kind = 'junk'", &[])        .expect("reads")        .and_then(|row| row.int("n"));    assert_eq!(filed, Some(own), "every one of the sender's {own} threads is filed into Junk");    (by.iter().map(|(steps, _)| steps).sum(), by.into_iter().take(6).collect())}/// RULE: Blocking a sender reads and writes that sender's threads by index (the sender index, v107), so blocking one with 100 emails costs the same among 1,000 other emails as among 100,000.#[test]fn blocking_a_sender_costs_the_same_among_a_thousand_emails_and_a_hundred_thousand() {    let (small_file, small_secret) = file_with(100, 1_000);    let (small, small_top) = steps_to_block(&small_file, &small_secret, 100);    let (large_file, large_secret) = file_with(100, 100_000);    let (large, large_top) = steps_to_block(&large_file, &large_secret, 100);    assert!(        large <= small + small / 4,        "blocking read {large} steps among 100,000 emails and {small} among 1,000\nheaviest, large: {large_top:#?}\nheaviest, small: {small_top:#?}"    );}