154 lines · 7.8 KB · RustRaw
//! HANDING STUCK (25 Nov 2026): the Mac hands every live grant to your Claude the keys of the tables the object//! serves sealed that the grant covers and lacks, the bundle a phone's top-up makes, through `assistant_add`. On the//! fake object, which refuses what the Worker's agent.ts `add` refuses. Synthetic keys and rows only.mod support;use std::sync::Arc;use base64::engine::general_purpose::URL_SAFE_NO_PAD;use base64::Engine as _;use cotton_client::seal::{DataKeyStore, HeldDataKeys};use cotton_core::seal::agent::open_agent_wrap;use cotton_core::seal::DataKey;use cotton_mac::hand::{hand, Handed, GRANTS};use cotton_mac::store::Store;use cotton_mac::object::ObjectClient;use serde_json::{json, Value};use support::object::{kit_table, FakeObject};const DB: &str = "00000000-0000-4000-8000-00000000a11d";const OWNER: &str = "+15550000011";const MEMBER_SECRET: [u8; 32] = [42; 32];fn sealed(table: &str) -> Value { json!({ "id": table, "columns": [], "seal": { "clear": [], "blind": [], "blind_exact": [] } })}fn open(table: &str) -> Value { json!({ "id": table, "columns": [] })}/// An object whose definitions serve the session tables sealed (the open tables lock's `handing`), memory and notes/// sealed, saved secrets sealed, and projects open; one live grant to your Claude that holds memory's key only,/// withholds notes and has turned session_task off; one ended grant; one window grant that is not your Claude's.fn object() -> FakeObject { let object = FakeObject::start( DB, OWNER, vec![kit_table("seal_key"), open(GRANTS), sealed("session"), sealed("prompt"), sealed("session_task"), sealed("memory"), sealed("note"), sealed("secret"), open("project")], ); let member_pub = URL_SAFE_NO_PAD.encode(cotton_core::vault::wrap::public_of(&MEMBER_SECRET)); object.put_row("seal_key", "v:prompt", json!({ "v": 2 })); object.put_row( GRANTS, "g-live", json!({ "account": true, "member_id": "m-live", "member_pub": member_pub, "request_id": "connect:m-live", "granted_at": "2026-11-20T10:00:00Z", "tables": ["memory"], "withheld": ["note"], "off": ["session_task"] }), ); object.put_row( GRANTS, "g-ended", json!({ "account": true, "member_id": "m-old", "member_pub": member_pub, "granted_at": "2026-11-01T10:00:00Z", "tables": [], "ended_at": "2026-11-10T10:00:00Z" }), ); object.put_row(GRANTS, "g-window", json!({ "member_id": "m-window", "member_pub": member_pub, "granted_at": "2026-11-21T10:00:00Z", "tables": [] })); object}fn keys() -> Arc<dyn DataKeyStore> { let keys = HeldDataKeys::new(); keys.hold(DB, &DataKey::from_bytes([7; 32])); keys}fn client(object: &FakeObject) -> ObjectClient { ObjectClient::new(object.server.url.clone(), Some("device".into())).unwrap()}fn grant_tables(object: &FakeObject, id: &str) -> Vec<String> { let state = object.state.lock().unwrap(); let held = state.live(GRANTS).into_iter().find(|(g, _)| g == id).unwrap().1; serde_json::from_value(held.data.get("tables").cloned().unwrap_or(json!([]))).unwrap()}/// The tables a wrap carries, each with its versions, opened with the member's own key.fn carried(wrapped: &str) -> Vec<(String, Vec<u32>)> { let bundle = open_agent_wrap(&MEMBER_SECRET, "m-live", &["note".to_string()], wrapped).unwrap(); bundle.keys.iter().map(|(t, v)| (t.clone(), v.keys().copied().collect())).collect()}fn v(t: &str, versions: &[u32]) -> (String, Vec<u32>) { (t.to_string(), versions.to_vec())}#[test]fn the_mac_hands_a_live_grant_every_sealed_table_it_covers_once_and_a_new_version_when_one_comes() { let object = object(); let dir = tempfile::tempdir().unwrap(); let record = Store::open(&dir.path().join("importer.db")).unwrap(); // At launch: every table the whole definitions serve sealed that the grant covers, at its current version, even // memory, which a phone handed already (harmless, OPEN-TABLES.md). Never notes (withheld), session_task (off), // saved secrets or projects (open); an ended grant and a window grant nothing. let first = hand(&client(&object), DB, &keys(), &record, None).unwrap(); assert_eq!(first.handed, Handed { live: 1, grants: 1, tables: 3, refused: 0 }); assert_eq!(grant_tables(&object, "g-live"), vec!["memory", "prompt", "session"], "gate 2's missing is empty for it"); assert!(grant_tables(&object, "g-ended").is_empty() && grant_tables(&object, "g-window").is_empty()); let wraps = object.state.lock().unwrap().wraps.clone(); assert_eq!(wraps.len(), 1); assert_eq!(wraps[0].0, "g-live"); assert_eq!(carried(&wraps[0].1), vec![v("memory", &[1]), v("prompt", &[1, 2]), v("session", &[1])]); assert!(open_agent_wrap(&[9; 32], "m-live", &[], &wraps[0].1).is_err(), "another key opens nothing"); // The next pass sees the grant its own addition changed, looks, and by its record hands nothing; after that, // with nothing changed, one small read and no act. let acts = |o: &FakeObject| o.server.requests().iter().filter(|r| r.path == "/v1/act").count(); let before = acts(&object); let echo = hand(&client(&object), DB, &keys(), &record, Some(first.next_since)).unwrap(); assert!(echo.looked); assert_eq!(echo.handed, Handed { live: 1, grants: 0, tables: 0, refused: 0 }); let quiet = hand(&client(&object), DB, &keys(), &record, Some(echo.next_since)).unwrap(); assert!(!quiet.looked); assert_eq!(acts(&object), before); // A table's next key version: exactly that table goes again, every version up to the new one (the bundle // carries 1..=current, grant.rs `bundle`, so older rows still open). object.put_row("seal_key", "v:prompt", json!({ "v": 3 })); let moved = hand(&client(&object), DB, &keys(), &record, Some(quiet.next_since)).unwrap(); assert_eq!(moved.handed, Handed { live: 1, grants: 1, tables: 1, refused: 0 }); let wraps = object.state.lock().unwrap().wraps.clone(); assert_eq!(carried(&wraps[1].1), vec![v("prompt", &[1, 2, 3])]); // A relaunch looks again and, by its record, hands nothing more. let relaunch = hand(&client(&object), DB, &keys(), &record, None).unwrap(); assert!(relaunch.looked); assert_eq!(relaunch.handed, Handed { live: 1, grants: 0, tables: 0, refused: 0 }); assert_eq!(object.state.lock().unwrap().wraps.len(), 2);}#[test]fn a_mac_without_the_data_key_or_without_a_live_grant_hands_nothing() { let dir = tempfile::tempdir().unwrap(); let record = Store::open(&dir.path().join("importer.db")).unwrap(); let object = object(); let none: Arc<dyn DataKeyStore> = HeldDataKeys::new(); let pass = hand(&client(&object), DB, &none, &record, None).unwrap(); assert!(!pass.looked); assert!(object.server.requests().is_empty(), "nothing asked of the object"); let bare = FakeObject::start(DB, OWNER, vec![kit_table("seal_key"), open(GRANTS), sealed("session")]); let pass = hand(&client(&bare), DB, &keys(), &record, None).unwrap(); assert_eq!(pass.handed, Handed::default()); assert!(bare.server.requests().iter().all(|r| r.path != "/v1/act"));}#[test]fn the_fake_refuses_a_withheld_table_or_an_ended_grant_as_the_worker_does() { let object = object(); let body = json!({ "act": "assistant_add", "args": { "grant_id": "g-live", "tables": ["note"], "wrapped": "w1:x" } }); let answer = reqwest::blocking::Client::new().post(format!("{}/v1/act", object.server.url)).json(&body).send().unwrap(); assert_eq!(answer.status().as_u16(), 403); let ended = json!({ "act": "assistant_add", "args": { "grant_id": "g-ended", "tables": ["session"], "wrapped": "w1:x" } }); let answer = reqwest::blocking::Client::new().post(format!("{}/v1/act", object.server.url)).json(&ended).send().unwrap(); assert_eq!(answer.status().as_u16(), 409); assert!(object.state.lock().unwrap().wraps.is_empty());}