//! The `device` primitive's whole MCP surface (v13) — `list_devices`, and the //! attribution `log_session` makes when a paired device's key signed the call. //! //! Two properties, and the second is the load-bearing one: //! //! 1. **The list reads whole and says who is cut off.** A revoked device stays //! in the answer with its revocation, because "this key was cut off" and //! "this key was never here" have opposite answers to the only question //! anybody asks of the list. //! 2. **An attribution is PROVED or absent.** `cotton_sessions.device_id` is //! written from the caller's own credential, never from `machine` — which is //! a hostname the caller typed — and never from a key that has been revoked. //! //! Every row is synthetic and every value invented //! ([nothing-real-ever-goes-in-cotton-s-public-tree]). mod support; use rusqlite::types::Value as SqlValue; use serde_json::{json, Value}; use support::harness::Harness; use support::mcp::{call_tool, call_tool_signed_by, mint_token}; const NOW: &str = "2026-09-08T10:00:00.000Z"; /// A device row's own key — a CREDENTIAL id, invented. const STUDY_MAC: &str = "a-synthetic-study-mac-key"; const OLD_PHONE: &str = "a-synthetic-old-phone-key"; fn text(value: &str) -> SqlValue { SqlValue::Text(value.to_string()) } fn harness() -> (Harness, String) { let mut h = Harness::new(); let secret = mint_token(&mut h, &["read", "write"], "Claude"); (h, secret) } /// A device the owner admitted. Written straight into the file, because /// approving one is a person comparing six digits and no tool may do it. fn admit(h: &Harness, device_id: &str, name: &str, platform: &str, relay_key: Option<&str>) { h.db.run( "INSERT INTO cotton_devices (id, workspace_id, device_id, name, platform, app_version, \ relay_identity_key, approved_at, approved_by_device, created_at, updated_at) \ VALUES (?,?,?,?,?,?,?,?,?,?,?)", &[ text(&format!("device-row-{device_id}")), text(&h.workspace_id), text(device_id), text(name), text(platform), text("0.4.11"), relay_key.map_or(SqlValue::Null, text), text(NOW), text("self"), text(NOW), text(NOW), ], ) .expect("the device row"); } fn revoke(h: &Harness, device_id: &str, reason: &str) { h.db.run( "UPDATE cotton_devices SET revoked_at = ?, revoked_by_device = ?, revoked_reason = ? \ WHERE device_id = ?", &[ text("2026-09-09T08:00:00.000Z"), text(STUDY_MAC), text(reason), text(device_id), ], ) .expect("the revocation"); } fn devices(answer: &Value) -> Vec { answer["devices"] .as_array() .expect("an array of devices") .clone() } // ── list_devices ──────────────────────────────────────────────────────────── #[test] fn lists_every_admitted_device_with_what_it_is_and_who_let_it_in() { let (h, secret) = harness(); admit(&h, STUDY_MAC, "The study Mac", "macos", Some("a-synthetic-relay-key")); let listed = devices(&call_tool(&h, &secret, "list_devices", json!({}))); assert_eq!(listed.len(), 1); let one = &listed[0]; assert_eq!(one["deviceId"], Value::from(STUDY_MAC)); assert_eq!(one["name"], Value::from("The study Mac")); assert_eq!(one["platform"], Value::from("macos")); assert_eq!(one["appVersion"], Value::from("0.4.11")); assert_eq!(one["approvedBy"], Value::from("self")); assert_eq!(one["revokedAt"], Value::Null); // The plain fact, never an election: this Mac published a key it can be // recognised by on the tunnel. WHICH Mac is serving right now is the // server's own runtime and no column in this file. assert_eq!(one["publishesRelayIdentity"], Value::Bool(true)); assert!( one.get("serving").is_none() && one.get("lastSeenAt").is_none(), "neither is in the file, and a tool that answered them would be inventing them" ); } #[test] fn why_a_mac_is_holding_an_update_is_answered_and_null_while_it_holds_none() { // Version 40: the sentence `UpdateGate` writes about this Mac while a // downloaded build waits, so a device page can say what a Mac that is // behind is waiting for. Null is "holding nothing", never "". let (h, secret) = harness(); admit(&h, STUDY_MAC, "The study Mac", "macos", Some("a-synthetic-relay-key")); let listed = devices(&call_tool(&h, &secret, "list_devices", json!({}))); assert_eq!(listed[0]["updateHold"], Value::Null); h.db.run( "UPDATE cotton_devices SET update_hold = ? WHERE device_id = ?", &[ text("holding 0.21.0 \u{2014} 'ci-swift-mac' holds the build lock"), text(STUDY_MAC), ], ) .expect("the sentence"); let listed = devices(&call_tool(&h, &secret, "list_devices", json!({}))); assert_eq!( listed[0]["updateHold"], Value::from("holding 0.21.0 \u{2014} 'ci-swift-mac' holds the build lock") ); } #[test] fn a_phone_publishes_no_relay_identity_and_says_so_rather_than_being_left_out() { let (h, secret) = harness(); admit(&h, OLD_PHONE, "A synthetic phone", "ios", None); let listed = devices(&call_tool(&h, &secret, "list_devices", json!({}))); assert_eq!(listed[0]["publishesRelayIdentity"], Value::Bool(false)); } #[test] fn a_revoked_device_stays_in_the_list_and_carries_its_revocation() { let (h, secret) = harness(); admit(&h, STUDY_MAC, "The study Mac", "macos", None); admit(&h, OLD_PHONE, "A phone that was lost", "ios", None); revoke(&h, OLD_PHONE, "lost"); let listed = devices(&call_tool(&h, &secret, "list_devices", json!({}))); assert_eq!(listed.len(), 2, "a revoked device is still in the answer"); let gone = listed .iter() .find(|row| row["deviceId"] == OLD_PHONE) .expect("the revoked row"); assert_eq!(gone["revokedAt"], Value::from("2026-09-09T08:00:00.000Z")); assert_eq!(gone["revokedBy"], Value::from(STUDY_MAC)); assert_eq!(gone["revokedReason"], Value::from("lost")); // And the caller that wants only the keys that open the database today // says so. let live = devices(&call_tool( &h, &secret, "list_devices", json!({ "includeRevoked": false }), )); assert_eq!(live.len(), 1); assert_eq!(live[0]["deviceId"], Value::from(STUDY_MAC)); } /// The serving ORDER (store v27): the person's own column, answered as it was /// written. Lower is preferred and `null` is a Mac nobody placed — and `null` /// rather than a 0, because a 0 would read as "first" and put an unplaced /// laptop in front of the Studio. #[test] fn a_device_answers_where_the_person_put_it_in_the_serving_order_or_null() { let (h, secret) = harness(); admit(&h, STUDY_MAC, "The study Mac", "macos", Some("a-synthetic-relay-key")); admit(&h, OLD_PHONE, "A synthetic phone", "ios", None); let listed = devices(&call_tool(&h, &secret, "list_devices", json!({}))); for one in &listed { assert_eq!( one["servingRank"], Value::Null, "a household nobody has ordered has no rank anywhere in it" ); } h.db.run( "UPDATE cotton_devices SET serving_rank = 1 WHERE device_id = ?", &[text(STUDY_MAC)], ) .expect("the place"); let listed = devices(&call_tool(&h, &secret, "list_devices", json!({}))); let mac = listed .iter() .find(|row| row["deviceId"] == STUDY_MAC) .expect("the Mac"); assert_eq!(mac["servingRank"], Value::from(1)); let phone = listed .iter() .find(|row| row["deviceId"] == OLD_PHONE) .expect("the phone"); assert_eq!(phone["servingRank"], Value::Null); // STILL NOT AN ELECTION. The rank is where the person would like the name // answered from; which line the relay actually holds is the server's own // runtime and no column in this file. assert!( mac.get("serving").is_none(), "a rank is a preference, and answering `serving` from it would be a guess" ); } /// Three answers, and the list keeps all three apart: a Mac that has never /// said (null), a Mac that looked and holds none (`{}`), and a Mac that holds /// some. #[test] fn a_mac_says_which_repositories_it_can_start_a_session_in_or_null() { let (h, secret) = harness(); admit(&h, STUDY_MAC, "The study Mac", "macos", Some("a-synthetic-relay-key")); let mac = |h: &Harness| { devices(&call_tool(h, &secret, "list_devices", json!({}))) .into_iter() .find(|row| row["deviceId"] == STUDY_MAC) .expect("the Mac") }; assert_eq!(mac(&h)["repos"], Value::Null, "it has never said"); h.db.run( "UPDATE cotton_devices SET repos = '{}' WHERE device_id = ?", &[text(STUDY_MAC)], ) .expect("an empty answer"); assert_eq!(mac(&h)["repos"], json!({}), "it looked and holds none"); h.db.run( "UPDATE cotton_devices SET repos = ? WHERE device_id = ?", &[ text(r#"{"https://git.example.test/owner/one":"present","https://git.example.test/owner/two":"clonable"}"#), text(STUDY_MAC), ], ) .expect("two repositories"); assert_eq!( mac(&h)["repos"], json!({ "https://git.example.test/owner/one": "present", "https://git.example.test/owner/two": "clonable", }) ); } #[test] fn a_database_nobody_has_joined_answers_an_empty_list_rather_than_refusing() { let (h, secret) = harness(); assert!(devices(&call_tool(&h, &secret, "list_devices", json!({}))).is_empty()); } // ── the attribution ───────────────────────────────────────────────────────── fn log_a_session(h: &Harness, secret: &str, signed_by: Option<&str>, external_id: &str) -> Value { let args = json!({ "tool": "claude-code", "externalId": external_id, "machine": "a-synthetic-hostname", "lastActiveAt": NOW, }); let answer = match signed_by { Some(device_id) => call_tool_signed_by(h, secret, device_id, "log_session", args), None => { return call_tool(h, secret, "log_session", args); } }; answer.json() } fn device_of(h: &Harness, external_id: &str) -> Option { h.db.get( "SELECT device_id FROM cotton_sessions WHERE external_id = ?", &[text(external_id)], ) .expect("reads") .expect("the session row") .text("device_id") .map(str::to_string) } #[test] fn a_session_logged_by_a_paired_device_says_which_device_it_ran_on() { let (h, secret) = harness(); admit(&h, STUDY_MAC, "The study Mac", "macos", None); log_a_session(&h, &secret, Some(STUDY_MAC), "ext-1"); assert_eq!(device_of(&h, "ext-1").as_deref(), Some(STUDY_MAC)); // And the reader answers it beside the machine, which stays what it was. let listed = call_tool(&h, &secret, "list_sessions", json!({})); let row = &listed["sessions"][0]; assert_eq!(row["deviceId"], Value::from(STUDY_MAC)); assert_eq!(row["machine"], Value::from("a-synthetic-hostname")); } /// The one that matters. An MCP connection authenticates a TOKEN, so a session /// logged over it genuinely does not know which device it is — and `machine` is /// a hostname somebody typed. NULL is the honest answer, and a handler matching /// the hostname against a device NAME would be writing a guess into the file. #[test] fn a_caller_that_cannot_prove_a_device_attributes_the_session_to_none() { let (h, secret) = harness(); admit(&h, STUDY_MAC, "a-synthetic-hostname", "macos", None); log_a_session(&h, &secret, None, "ext-1"); assert_eq!( device_of(&h, "ext-1"), None, "the device row's NAME is the hostname exactly, and the handler still refused to match \ them — a name is not a key" ); } #[test] fn a_key_no_longer_admitted_attributes_nothing() { let (h, secret) = harness(); admit(&h, OLD_PHONE, "A phone that was lost", "ios", None); revoke(&h, OLD_PHONE, "lost"); log_a_session(&h, &secret, Some(OLD_PHONE), "ext-1"); assert_eq!( device_of(&h, "ext-1"), None, "attributing new work to a revoked key would say a key that may no longer open this \ database is opening it" ); } #[test] fn a_key_that_names_no_row_at_all_attributes_nothing() { let (h, secret) = harness(); log_a_session(&h, &secret, Some("a-key-nobody-admitted"), "ext-1"); assert_eq!(device_of(&h, "ext-1"), None); } /// A later turn that knows no more than the first did must not erase what the /// first proved: `log_session` is called once per turn, and only a call that /// carries a device writes the column. #[test] fn a_later_turn_with_no_device_leaves_the_attribution_alone() { let (h, secret) = harness(); admit(&h, STUDY_MAC, "The study Mac", "macos", None); log_a_session(&h, &secret, Some(STUDY_MAC), "ext-1"); log_a_session(&h, &secret, None, "ext-1"); assert_eq!(device_of(&h, "ext-1").as_deref(), Some(STUDY_MAC)); }