//! Account › Assistants: the person's own Claude, its //! connections, what it may read (each sealed table On, Ask each time or Off) //! and Remove; and what the phone hands it on its own after a sync pass. //! Decided here from the object's `unlock_grant` rows whose `account` is //! true; the face draws what this answers. //! //! A table is Off when the person switched it off or left it off on the card //! (the signed policy withholds it: turning it on is signed again, behind the //! phone's lock), Ask each time when a read should file a timed card, else //! On. Saved secrets ([`NEVER`]) are never listed. use chrono::{DateTime, Utc}; use serde::Serialize; use serde_json::Value; use super::shell_lines::LineSet; use super::unlock::{ago_said, flag, grant_ended}; pub use crate::seal::agent::{closed, CLOSED, NEVER}; /// One connection of your Claude: a live account grant. #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct Connection { #[serde(rename = "grantId")] pub grant_id: String, #[serde(rename = "memberId")] pub member_id: String, pub client: String, /// `Claude · connected 3 days ago`. pub said: String, } /// One of a table's three settings. #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct AccessChoice { pub access: &'static str, pub label: String, pub on: bool, } /// One sealed table as the screen lists it. #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct TableAccess { pub table: String, pub label: String, pub access: &'static str, /// Turning it on asks the phone's lock (the device key comes through it; signed again where the card left it /// off): true whenever it is not on now. #[serde(rename = "needsLock")] pub needs_lock: bool, pub choices: Vec, } #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct Screen { pub heading: String, pub promise: String, pub connections: Vec, /// Said when no connection stands. #[serde(skip_serializing_if = "Option::is_none")] pub none: Option, #[serde(rename = "mayRead")] pub may_read: String, pub tables: Vec, /// Connect Claude: offered only while no Claude is connected (none yet, or /// only a pair waiting for Claude to connect). Once one is, it is left out /// (owner's iPhone, Comms build 19: offered beside a connected Claude). #[serde(skip_serializing_if = "Option::is_none")] pub connect: Option, #[serde(skip_serializing_if = "Option::is_none")] pub remove: Option, } fn text(row: &Value, key: &str) -> Option { row.get(key).and_then(Value::as_str).map(str::trim).filter(|s| !s.is_empty()).map(str::to_string) } /// A list column as the object serves it (an array) or a copy keeps it (its JSON text). pub fn list(row: &Value, key: &str) -> Vec { let value = match row.get(key) { Some(Value::String(text)) => serde_json::from_str(text).unwrap_or(Value::Null), Some(other) => other.clone(), None => Value::Null, }; value.as_array().into_iter().flatten().filter_map(Value::as_str).map(str::to_string).collect() } /// The live grants to your Claude, newest first. pub fn live(grants: &[Value]) -> Vec<&Value> { let mut out: Vec<&Value> = grants.iter().filter(|g| flag(g, "account") && !grant_ended(g)).collect(); out.sort_by(|a, b| text(b, "granted_at").cmp(&text(a, "granted_at"))); out } /// How a table stands for your Claude: `on`, `ask` or `off`, by the newest live grant. pub fn access(grant: Option<&Value>, table: &str) -> &'static str { let Some(g) = grant else { return "on" }; if list(g, "off").contains(&table.to_string()) || list(g, "withheld").contains(&table.to_string()) { "off" } else if list(g, "ask").contains(&table.to_string()) { "ask" } else { "on" } } /// The screen. `sealed` is every sealed table this database defines; `read` the grants an assistant has read through /// (`assistant_read.grant_id`). A grant Connect Claude made on the phone (`request_id` `connect:`) through which /// nothing has read yet is the pair waiting for Claude to connect (the object keeps who claimed it to itself), said /// `account.waiting`, never "connected": on the emulator (21 Nov 2026) Connect Claude said "Claude · connected just /// now" before any Claude had connected. pub fn screen(grants: &[Value], read: &[String], sealed: &[String], now: DateTime, label: impl Fn(&str) -> Option, lines: &LineSet) -> Screen { let said = |line: &str, fills: &[(&str, &str)]| lines.say(line, fills).unwrap_or_default(); let live = live(grants); let mut connected = false; let connections: Vec = live .iter() .filter_map(|g| { // Its name as a person reads it: a build session token's own name ("Lane …") reads as Claude Code. let client = super::unlock::plain_client(&text(g, "client").unwrap_or_else(|| "Claude".into()), lines); let when = ago_said(&text(g, "granted_at")?, now, lines); let grant_id = text(g, "id")?; let waiting = text(g, "request_id").is_some_and(|r| r.starts_with("connect:")) && !read.contains(&grant_id); let words = if waiting { said("account.waiting", &[("client", &client)]) } else { said("account.connected", &[("client", &client), ("when", &when)]) }; let connection = Connection { grant_id, member_id: text(g, "member_id")?, said: words, client }; connected |= !waiting; Some(connection) }) .collect(); let newest = live.first().copied(); let mut tables: Vec = sealed .iter() .filter(|t| !NEVER.contains(&t.as_str()) && !closed(t)) .map(|t| { let now_at = access(newest, t); // Turning it on widens what Claude reads: it always asks the phone's lock (and is signed again where the // card left it off); stepping down asks nothing (coordinator, 22 Nov 2026). let needs_lock = newest.is_some() && now_at != "on"; let choices = ["on", "ask", "off"].into_iter().map(|a| AccessChoice { access: a, label: said(&format!("account.{a}"), &[]), on: a == now_at }).collect(); TableAccess { table: t.clone(), label: label(t).unwrap_or_else(|| t.clone()), access: now_at, needs_lock, choices } }) .collect(); tables.sort_by(|a, b| a.label.to_lowercase().cmp(&b.label.to_lowercase()).then(a.table.cmp(&b.table))); tables.dedup_by(|a, b| a.table == b.table); Screen { heading: said("account.heading", &[]), promise: said("account.promise", &[]), none: connections.is_empty().then(|| said("account.none", &[])), remove: (!connections.is_empty()).then(|| said("account.remove", &[])), connections, may_read: said("account.may_read", &[]), tables: if newest.is_some() { tables } else { Vec::new() }, connect: (!connected).then(|| said("account.connect", &[])), } } /// One group a person knows (Chats, Mail, Photos, Calls, Contacts, Files: the kit's `reads`; else the core's, as the /// card offers them: Notes, Calendar and to-dos, Messages and calls…), with how its tables /// stand for your Claude: `on`, `ask` or `off` when they all stand alike, else `mixed` (no choice is on). #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct GroupAccess { pub id: String, pub label: String, pub line: String, pub access: &'static str, /// The group's tables this database seals, which its choice sets. pub tables: Vec, /// Turning it on asks the phone's lock: true whenever it is not on now (mixed included). #[serde(rename = "needsLock")] pub needs_lock: bool, pub choices: Vec, } /// An act Your Claude's state offers: `ask_all` (Ask each time instead) or `stop` (Stop now). #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct ReadingAct { pub id: &'static str, pub label: String, /// Stop now's windows, ended each by `unlock.end`. #[serde(rename = "grantIds", skip_serializing_if = "Vec::is_empty")] pub grant_ids: Vec, } /// How your Claude may read now, said once for the page and once for the row that opens it /// (you-grouped.html, the owner's review of 22 Nov 2026): `standing` ("Claude can read any time", with Ask each /// time instead), `timed` ("Claude can read until 10:30", with Stop now), `asks` (every group asks each time), /// `nothing` (every group off), `waiting` (Connect Claude made a pair no Claude has used yet), or `none`. #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct Reading { pub state: &'static str, /// The row on You: "Can read any time", "Can read until 10:30", "Asks each time". pub row: String, /// "Connected" while a Claude is. #[serde(skip_serializing_if = "Option::is_none")] pub connected: Option, #[serde(skip_serializing_if = "Option::is_none")] pub title: Option, #[serde(skip_serializing_if = "Option::is_none")] pub line: Option, #[serde(skip_serializing_if = "Option::is_none")] pub act: Option, /// Under the groups: what Ask each time means. #[serde(skip_serializing_if = "Option::is_none")] pub note: Option, /// Disconnect Claude, at the foot, while one is connected or waits. #[serde(skip_serializing_if = "Option::is_none")] pub disconnect: Option, } /// How a set of tables stands: alike, or `mixed`. fn together(each: &[&'static str]) -> &'static str { match each.first() { Some(first) if each.iter().all(|a| a == first) => first, Some(_) => "mixed", None => "on", } } /// The groups the kit names (`groups`: each id and its tables), each kept to the tables this database seals; a group /// with none of them is left out. Read by the newest live grant, as the tables are. `core`: the groups are the core's /// (`kits/keys/reads.json`, for a kit that names none), worded as the card words them; else the kit's own. pub fn groups(grants: &[Value], groups: &[(String, Vec)], sealed: &[String], lines: &LineSet, core: bool) -> Vec { let said = |line: &str| lines.say(line, &[]).unwrap_or_default(); // Nothing connected: nothing to choose, as the tables list none (the emulator, 22 Nov 2026: six groups all "On" // stood under Connect Claude). let Some(newest) = live(grants).first().copied() else { return Vec::new() }; let newest = Some(newest); groups .iter() .filter_map(|(id, tables)| { let tables: Vec = tables.iter().filter(|t| sealed.contains(t) && !NEVER.contains(&t.as_str()) && !closed(t)).cloned().collect(); if tables.is_empty() { return None; } let each: Vec<&'static str> = tables.iter().map(|t| access(newest, t)).collect(); let now_at = together(&each); let needs_lock = newest.is_some() && now_at != "on"; let choices = ["on", "ask", "off"].into_iter().map(|a| AccessChoice { access: a, label: said(&format!("account.{a}")), on: a == now_at }).collect(); // A kit's own group is worded under `account.groups`; the core's groups (`kits/keys/reads.json`) under // `card.groups`, as the card said them: Comms' Photos is "Sent and received", Story's photos are not // (the emulator, 23 Nov 2026). let (first, then) = if core { ("card", "account") } else { ("account", "card") }; let word = |part: &str| lines.say(&format!("{first}.groups.{id}.{part}"), &[]).or_else(|| lines.say(&format!("{then}.groups.{id}.{part}"), &[])).unwrap_or_default(); Some(GroupAccess { label: word("label"), line: word("line"), id: id.clone(), access: now_at, tables, needs_lock, choices }) }) .collect() } /// How your Claude may read now. `shown` is what the person chooses between (the groups' tables where the kit names /// groups, else every sealed table the screen lists); `clock` says an instant as the reader's clock reads it ("10:30"). /// A timed window open now (an assistant's window that is not the standing grant) comes first: the person sees until /// when, and Stop now ends every such window. Else, connected: any of `shown` on reads any time (Ask each time /// instead moves those to Ask each time), else any on Ask asks each time, else nothing. pub fn reading(grants: &[Value], read: &[String], shown: &[(String, &'static str)], now: DateTime, clock: impl Fn(&str) -> String, lines: &LineSet) -> Reading { let said = |line: &str, fills: &[(&str, &str)]| lines.say(line, fills).unwrap_or_default(); let live = live(grants); let waiting = |g: &&Value| text(g, "request_id").is_some_and(|r| r.starts_with("connect:")) && text(g, "id").is_none_or(|id| !read.contains(&id)); let connected = live.iter().any(|g| !waiting(g)); let mut timed: Vec<&Value> = grants .iter() .filter(|g| !flag(g, "account") && !super::unlock::grant_standing(g) && super::unlock::grant_open(g, now)) .collect(); timed.sort_by(|a, b| text(b, "ends_at").cmp(&text(a, "ends_at"))); let disconnect = (!live.is_empty()).then(|| said("account.reading.disconnect", &[])); let blank = |state: &'static str, row: &str| Reading { state, row: said(row, &[]), connected: None, title: None, line: None, act: None, note: None, disconnect: disconnect.clone() }; if let Some(last) = timed.first() { let time = clock(&text(last, "ends_at").unwrap_or_default()); let client = text(last, "client").unwrap_or_else(|| "Claude".into()); return Reading { state: "timed", row: said("account.reading.row.timed", &[("time", &time)]), connected: connected.then(|| said("account.reading.connected", &[])), title: Some(said("account.reading.timed", &[("client", &client), ("time", &time)])), line: Some(said("account.reading.timed_line", &[])), act: Some(ReadingAct { id: "stop", label: said("account.reading.stop", &[]), grant_ids: timed.iter().filter_map(|g| text(g, "id")).collect() }), note: connected.then(|| said("account.reading.note", &[])), disconnect, }; } if !connected { return blank(if live.is_empty() { "none" } else { "waiting" }, if live.is_empty() { "account.reading.row.none" } else { "account.reading.row.waiting" }); } // Nothing listed to choose between (a copy that holds no table definitions yet): the grant's own tables say it. let granted: Vec<(String, &'static str)> = live.first().map(|g| list(g, "tables").into_iter().map(|t| { let at = access(Some(g), &t); (t, at) }).collect()).unwrap_or_default(); let shown = if shown.is_empty() { granted.as_slice() } else { shown }; let any = |a: &str| shown.iter().any(|(_, at)| *at == a); let (state, act) = if any("on") { ("standing", Some(ReadingAct { id: "ask_all", label: said("account.reading.ask_all", &[]), grant_ids: Vec::new() })) } else if any("ask") { ("asks", None) } else { ("nothing", None) }; Reading { state, row: said(&format!("account.reading.row.{state}"), &[]), connected: Some(said("account.reading.connected", &[])), title: Some(said(&format!("account.reading.{state}"), &[])), line: Some(said(&format!("account.reading.{state}_line"), &[])), act, note: Some(said("account.reading.note", &[])), disconnect, } } /// What Ask each time instead moves: of `shown`, every table on now (a narrowing, never signed). pub fn to_ask(shown: &[(String, &'static str)]) -> Vec { shown.iter().filter(|(_, at)| *at == "on").map(|(t, _)| t.clone()).collect() } /// What the phone hands a live grant on its own after a pass, inside its /// signed policy: each table in scope whose keys it has not handed, or whose /// current version passed what it handed, at its current version. `handed` /// is what this phone noted for the grant; `current` the table's seal /// version (0 when it has none yet: nothing to hand). pub fn to_hand(grant: &Value, sealed: &[String], handed: &[(String, u32)], current: impl Fn(&str) -> u32) -> Vec<(String, u32)> { let narrowed: Vec = [list(grant, "withheld"), list(grant, "off"), list(grant, "ask")].concat(); let mut out: Vec<(String, u32)> = sealed .iter() .filter(|t| !NEVER.contains(&t.as_str()) && !closed(t) && !narrowed.contains(t)) .filter_map(|t| { let v = current(t); let had = handed.iter().filter(|(h, _)| h == t).map(|(_, v)| *v).max(); (v > 0 && had.map_or(true, |had| had < v)).then(|| (t.clone(), v)) }) .collect(); out.sort(); out.dedup(); out } #[cfg(test)] #[path = "assistants_tests.rs"] mod tests;