//! No MCP read lists a sign-in code (`cotton_core::messages::sign_in`). //! //! The serving Mac reads every SMS into Cotton, and the texts a service sends //! to sign the owner in carry codes that are credentials for the minutes they //! live. Only the sign-in engine reads one, for the service it names and the //! window it gives. An assistant reads the text with its code withheld. //! //! WHY AT THE ANSWER. Message words leave by many doors: a thread read, a //! search hit, a list's computed line, a thread's last-line preview, a filing //! proposal, a summary. They meet at [`dispatch::call_tool`] //! (super::dispatch::call_tool), so the withholding is done once, there, on //! every answer that read chats — the same rule that holds the B3 push //! (`chat_reads`) — and a door added tomorrow is covered without anyone //! remembering to. //! //! Three things happen to such an answer: //! - an element found by the code (a search for its digits) leaves it, so a //! search cannot confirm a code the assistant guessed; //! - every code found anywhere in it is withheld in every string, so a cut //! preview beside the whole line keeps nothing of it; //! - a `code` field on a message row leaves, whatever it holds. use serde_json::{Map, Value}; use cotton_core::messages::sign_in; /// The answer with every sign-in code withheld; `args` are the call's, whose /// digits name the codes a search may have been looking for. pub fn withhold(answer: Value, args: &Map) -> Value { let mut codes = Vec::new(); collect(&answer, &mut codes); if codes.is_empty() { return answer; } let asked: Vec = codes .iter() .filter(|code| args.values().any(|value| names(value, code))) .cloned() .collect(); let answer = if asked.is_empty() { answer } else { drop_found(answer, &asked) }; mask(answer, &codes) } fn collect(value: &Value, codes: &mut Vec) { match value { Value::String(text) => { if let Some(found) = sign_in::read(text, None) { if !codes.contains(&found.code) { codes.push(found.code); } } } Value::Array(items) => items.iter().for_each(|item| collect(item, codes)), Value::Object(fields) => fields.values().for_each(|item| collect(item, codes)), _ => {} } } /// Does `value` hold `code`'s digits (an argument naming it)? fn names(value: &Value, code: &str) -> bool { match value { // A run of digits that IS the code (`482913`, `482 913`, `G-482913`); // an id that happens to hold its digits inside a longer run is not // a search for it. Value::String(text) => { let squeezed = text.replace([' ', '-'], ""); squeezed .split(|char: char| !char.is_ascii_digit()) .any(|run| run == code) } Value::Array(items) => items.iter().any(|item| names(item, code)), Value::Object(fields) => fields.values().any(|item| names(item, code)), _ => false, } } /// Does anything in `value` carry one of `codes`? fn carries(value: &Value, codes: &[String]) -> bool { match value { Value::String(text) => { sign_in::read(text, None).is_some_and(|found| codes.contains(&found.code)) || sign_in::withhold_codes(text, codes) != *text } Value::Array(items) => items.iter().any(|item| carries(item, codes)), Value::Object(fields) => fields.values().any(|item| carries(item, codes)), _ => false, } } /// Every element of every list that carries an asked-for code leaves it. fn drop_found(value: Value, asked: &[String]) -> Value { match value { Value::Array(items) => Value::Array( items .into_iter() .filter(|item| !carries(item, asked)) .map(|item| drop_found(item, asked)) .collect(), ), Value::Object(fields) => Value::Object( fields .into_iter() .map(|(key, item)| (key, drop_found(item, asked))) .collect(), ), other => other, } } fn mask(value: Value, codes: &[String]) -> Value { match value { Value::String(text) => { let text = sign_in::withhold(&text).into_owned(); Value::String(sign_in::withhold_codes(&text, codes)) } Value::Array(items) => { Value::Array(items.into_iter().map(|item| mask(item, codes)).collect()) } Value::Object(fields) => { let row = fields.contains_key("thread_id") || fields.contains_key("channel"); Value::Object( fields .into_iter() .filter(|(key, _)| !(row && key == "code")) .map(|(key, item)| (key, mask(item, codes))) .collect(), ) } other => other, } } #[cfg(test)] #[path = "sign_in_codes_tests.rs"] mod tests;