Skip to content
1 change: 1 addition & 0 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Each subject has one doc in `docs/architecture/`. `pnpm run check:docs` enforces
| --- | --- |
| [Account model](docs/architecture/account-model.md) | Entity relationships, Device/relay data ownership and cross-Runner job routing |
| [Identity](docs/architecture/identity.md) | Key pairs and the identity device, pairing and unpairing, Devices and Runners, what the relay sees, the account's provider credentials |
| [Device names](docs/architecture/device-names.md) | Account aliases, machine names and durable roster intent |
| [Device revocation](docs/architecture/device-revocation.md) | Lost, un-wiped Device boundaries and the proposed, unimplemented containment and rotation contract |
| [Relay](docs/architecture/relay.md) | `crates/relay`: storage on SQLite or Postgres, files, housekeeping, quotas, metrics, rate limits, auth, tables and migrations, the blob, sync socket, and push APIs, deploys |
| [Computer display](docs/architecture/computer-display.md) | Proposed authenticated display/input transport, Device-bound sessions, lifecycle fences, client feasibility and measurement gaps |
Expand Down
6 changes: 6 additions & 0 deletions crates/cli/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ pub async fn dispatch(app: &Arc<App>, method: &str, params: Value) -> Result<Val
Ok(Value::Null)
}

"device.set_name" => {
let id = string(&params, "id")?;
let name = params["name"].as_str().ok_or("missing name")?;
app.set_device_name(&id, name).map_err(|e| e.to_string())?;
Ok(Value::Null)
}
"device.rename" => {
let name = string(&params, "name")?;
app.rename_device(&name).map_err(|e| e.to_string())?;
Expand Down
182 changes: 169 additions & 13 deletions crates/cli/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ impl Slot {
#[derive(Debug, Clone, Default)]
pub struct State {
pub devices: Vec<Device>,
pub device_names: std::collections::BTreeMap<String, String>,
pub device_name_edits: std::collections::BTreeMap<String, String>,
pub bots: Vec<Bot>,
pub chats: Vec<Chat>,
pub routines: Vec<Routine>,
Expand Down Expand Up @@ -798,6 +800,40 @@ impl App {
Ok(())
}

/// Gives a known Device an account alias; blank restores its machine name.
pub fn set_device_name(&self, id: &str, name: &str) -> anyhow::Result<()> {
let name = name.trim();
anyhow::ensure!(name.chars().count() <= 64, "Device names must be at most 64 characters");
let _edit = self.roster_edit.lock().unwrap();
let lifecycle = self.plugin_admission(None).map_err(anyhow::Error::msg)?;
let dek = self.machine_file().ok_or_else(|| anyhow::anyhow!("No identity on this Device"))?.dek()?;
{
let mut state = self.state.lock().unwrap();
let device = state.devices.iter().find(|device| device.id == id)
.ok_or_else(|| anyhow::anyhow!("Unknown Device"))?;
let alias = (!name.is_empty() && name != device.name).then(|| name.to_owned());
let mut snapshot = state.clone();
match alias {
Some(name) => { snapshot.device_names.insert(id.to_owned(), name); }
None => { snapshot.device_names.remove(id); }
}
snapshot.device_name_edits.insert(id.to_owned(), uuid::Uuid::new_v4().to_string());
let item = OutboxItem {
id: uuid::Uuid::new_v4().to_string(), kind: "roster".into(), recipient: None,
ciphertext: crate::crypto::encrypt_json(&dek, "roster", &Self::roster_from_state(&snapshot))?,
slot: Some(Slot::latest("roster")), group: None,
};
remember_applied(&mut snapshot, &item.id);
self.store.queue_outbox_with_state(&item, &snapshot)?;
*state = snapshot;
}
drop(_edit);
drop(lifecycle);
self.outbox_notify.notify_waiters();
self.emit(self.roster_summary());
Ok(())
}

/// Forgets the identity on this Device: keys, credentials, and everything synced. The
/// relay keeps the account; another Device or the backup phrase brings it back.
pub fn forget_identity(&self) -> anyhow::Result<()> {
Expand Down Expand Up @@ -941,11 +977,21 @@ impl App {

pub fn push_roster(&self) {
let Some(dek) = self.dek() else { return };
let roster = {
let state = self.state.lock().unwrap();
RosterBlob {
let roster = Self::roster_from_state(&self.state.lock().unwrap());
match crate::crypto::encrypt_json(&dek, "roster", &roster) {
Ok(ciphertext) => {
self.push_slot_blob("roster", Slot::latest("roster"), None, ciphertext);
}
Err(error) => tracing::error!(%error, "encrypting roster"),
}
}

pub(crate) fn roster_from_state(state: &State) -> RosterBlob {
RosterBlob {
bots: state.bots.clone(),
chats: state.chats.iter().map(|c| c.meta.clone()).collect(),
device_names: state.device_names.clone(),
device_name_edits: state.device_name_edits.clone(),
routines: state.routines.clone(),
workflows: state.workflows.clone(),
auto_review: state.auto_review.clone(),
Expand All @@ -956,13 +1002,6 @@ impl App {
policy_capabilities: state.policy_capabilities.clone(),
deleted_bot_versions: state.deleted_bot_versions.clone(),
updated_at: config::now_secs(),
}
};
match crate::crypto::encrypt_json(&dek, "roster", &roster) {
Ok(ciphertext) => {
self.push_slot_blob("roster", Slot::latest("roster"), None, ciphertext);
}
Err(error) => tracing::error!(%error, "encrypting roster"),
}
}

Expand Down Expand Up @@ -1084,7 +1123,10 @@ impl App {
}

pub fn device(&self, id: &str) -> Option<Device> {
self.state.lock().unwrap().devices.iter().find(|d| d.id == id).cloned()
let state = self.state.lock().unwrap();
let mut device = state.devices.iter().find(|d| d.id == id).cloned()?;
if let Some(name) = state.device_names.get(id) { device.name = name.clone(); }
Some(device)
}

/// A chat's name as the apps show it: a group's title, else its members' names; a direct
Expand Down Expand Up @@ -2270,7 +2312,8 @@ impl App {
fn devices_out(&self, state: &State) -> Vec<Value> {
let this_id = self.this_device_id();
let mut devices: Vec<&Device> = state.devices.iter().collect();
devices.sort_by_key(|d| (Some(d.id.clone()) != this_id, d.name.to_lowercase()));
let name = |device: &Device| state.device_names.get(&device.id).unwrap_or(&device.name).clone();
devices.sort_by_key(|d| (Some(d.id.clone()) != this_id, name(d).to_lowercase()));
let mut out: Vec<Value> = devices
.into_iter()
.map(|device| {
Expand All @@ -2279,7 +2322,8 @@ impl App {
let status = if is_this || state.device_online.contains(&device.id) { "online" } else { "offline" };
json!({
"id": device.id,
"name": device.name,
"name": name(device),
"machine_name": device.name,
"model": device.model,
"os": device.os,
"os_version": device.os_version,
Expand Down Expand Up @@ -2540,6 +2584,118 @@ mod tests {
assert_eq!(recovered.error_code.as_deref(),Some("response_lost"));
}

#[test]
fn device_alias_preserves_machine_name_and_survives_restart() {
let scratch = scratch_app();
let app = &scratch.0;
crate::identity::create(app, Some("Workbench".into())).unwrap();
app.state.lock().unwrap().devices.push(Device { id: "peer".into(), name: "Machine".into(), ..Default::default() });
app.set_device_name("peer", " Office ").unwrap();
assert_eq!(app.device("peer").unwrap().name, "Office");
let restarted = App::load(Config { home: scratch.1.clone(), port: 0 }).unwrap();
let snapshot = restarted.snapshot();
let row = snapshot["devices"].as_array().unwrap().iter().find(|d| d["id"] == "peer").unwrap();
assert_eq!(row["name"], "Office");
assert_eq!(row["machine_name"], "Machine");
assert!(restarted.set_device_name("missing", "Name").is_err());
assert!(restarted.set_device_name("peer", &"界".repeat(65)).is_err());
restarted.set_device_name("peer", &"界".repeat(64)).unwrap();
restarted.state.lock().unwrap().devices.iter_mut().find(|d| d.id == "peer").unwrap().name = "New machine name".into();
restarted.set_device_name("peer", " \n ").unwrap();
let reset = App::load(Config { home: scratch.1.clone(), port: 0 }).unwrap();
assert!(!reset.state.lock().unwrap().device_names.contains_key("peer"));
assert_eq!(reset.device("peer").unwrap().name, "New machine name");
}

#[tokio::test]
async fn device_alias_api_keeps_machine_rename_and_forget_boundaries() {
let scratch = scratch_app();
let app = &scratch.0;
crate::identity::create(app, Some("Machine".into())).unwrap();
let id = app.this_device_id().unwrap();
crate::api::dispatch(app, "device.set_name", json!({"id":id,"name":"Office"})).await.unwrap();
crate::api::dispatch(app, "device.rename", json!({"name":"New machine"})).await.unwrap();
assert_eq!(app.device(&id).unwrap().name, "Office");
assert_eq!(app.machine_file().unwrap().name, "New machine");
for value in [Value::Null, json!(17), json!(false)] {
assert!(crate::api::dispatch(app, "device.set_name", json!({"id":id,"name":value})).await.is_err());
}
crate::api::dispatch(app, "device.set_name", json!({"id":id,"name":""})).await.unwrap();
assert_eq!(app.device(&id).unwrap().name, "New machine");
app.set_device_name(&id, "Old account").unwrap();
app.forget_identity().unwrap();
assert!(app.set_device_name(&id, "Late alias").is_err());
crate::identity::create(app, Some("Replacement".into())).unwrap();
assert!(app.state.lock().unwrap().device_names.is_empty());
assert!(app.store.load_state().unwrap().device_names.is_empty());
assert!(app.set_device_name(&id, "Late alias").is_err());
}

fn assert_device_alias_transaction_failure(table: &str, operation: &str) {
let scratch = scratch_app();
let app = &scratch.0;
crate::identity::create(app, Some("Workbench".into())).unwrap();
app.state.lock().unwrap().devices.push(Device { id: "peer".into(), name: "Machine".into(), ..Default::default() });
app.set_device_name("peer", "Old alias").unwrap();
let before = app.store.load_state().unwrap();
let queued = app.store.queued_roster().unwrap().unwrap();
let ancestor = serde_json::to_value(app.store.roster_baseline("queued").unwrap()).unwrap();
let db = rusqlite::Connection::open(app.config.database_path()).unwrap();
db.execute_batch(&format!("CREATE TRIGGER fail_alias BEFORE {operation} ON {table} BEGIN SELECT RAISE(ABORT, 'synthetic alias failure'); END;")).unwrap();
let mut events = app.events.subscribe();
let error = app.set_device_name("peer", "New alias").unwrap_err();
assert!(error.to_string().contains("synthetic alias failure"));
let persisted = app.store.load_state().unwrap();
assert_eq!(app.state.lock().unwrap().device_names, before.device_names);
assert_eq!(persisted.device_names, before.device_names);
assert_eq!(persisted.device_name_edits, before.device_name_edits);
assert_eq!(app.state.lock().unwrap().device_name_edits, before.device_name_edits);
assert_eq!(app.state.lock().unwrap().applied_blob_ids, before.applied_blob_ids);
assert_eq!(persisted.applied_blob_ids, before.applied_blob_ids);
let retained = app.store.queued_roster().unwrap().unwrap();
assert_eq!(retained.id, queued.id);
assert_eq!(retained.ciphertext, queued.ciphertext);
assert_eq!(serde_json::to_value(app.store.roster_baseline("queued").unwrap()).unwrap(), ancestor);
assert!(matches!(events.try_recv(), Err(broadcast::error::TryRecvError::Empty)));
}

#[test]
fn device_alias_state_failure_rejects_without_publication() {
assert_device_alias_transaction_failure("device_names", "UPDATE");
}

#[test]
fn device_alias_outbox_failure_rolls_back_state_and_receipt() {
assert_device_alias_transaction_failure("outbox", "UPDATE");
}

#[test]
fn device_alias_committed_intent_survives_dropped_app() {
let home = tempfile::tempdir().unwrap();
let config = Config { home: home.path().into(), port: 0 };
let (queued_id, dek, ancestor) = {
let app = App::load(config.clone()).unwrap();
crate::identity::create(&app, Some("Workbench".into())).unwrap();
app.state.lock().unwrap().devices.push(Device { id: "peer".into(), name: "Machine".into(), ..Default::default() });
app.set_device_name("peer", "Office").unwrap();
let ancestor = serde_json::to_value(app.store.roster_baseline("queued").unwrap().unwrap()).unwrap();
let queued = app.store.queued_roster().unwrap().unwrap();
(queued.id, app.dek().unwrap(), ancestor)
};
// The original App and its SQLite connection are dropped before admission.
let reopened = App::load(config).unwrap();
assert_eq!(reopened.device("peer").unwrap().name, "Office");
let persisted = reopened.store.load_state().unwrap();
assert_eq!(persisted.device_names.get("peer").unwrap(), "Office");
let queued = reopened.store.queued_roster().unwrap().unwrap();
assert_eq!(queued.id, queued_id);
assert!(persisted.applied_blob_ids.contains(&queued_id));
assert_eq!(queued.slot, Some(Slot::latest("roster")));
let roster: RosterBlob = crate::crypto::decrypt_json(&dek, "roster", &queued.ciphertext).unwrap();
assert_eq!(roster.device_names, persisted.device_names);
assert_eq!(serde_json::to_value(reopened.store.roster_baseline("queued").unwrap().unwrap()).unwrap(), ancestor);
}

#[test]
fn load_propagates_final_normalization_save_failure() {
let home = tempfile::tempdir().unwrap();
Expand Down
24 changes: 22 additions & 2 deletions crates/cli/src/local_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,11 @@ impl LocalStore {
seq INTEGER NOT NULL,
json TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS device_names (
id TEXT PRIMARY KEY NOT NULL,
position INTEGER NOT NULL,
json TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS blob_deletes (
id TEXT PRIMARY KEY NOT NULL,
position INTEGER NOT NULL
Expand Down Expand Up @@ -756,6 +761,8 @@ impl LocalStore {
let policy: crate::model::RosterBlob = serde_json::from_str(&policy_json).context("decoding policy state")?;
Ok(State {
devices: load_json_table(&connection, "devices")?,
device_name_edits: policy.device_name_edits,
device_names: load_json_table::<(String, String)>(&connection, "device_names")?.into_iter().collect(),
bots: load_json_table(&connection, "bots")?,
chats: load_json_table(&connection, "chats")?,
routines: load_json_table(&connection, "routines")?,
Expand Down Expand Up @@ -785,14 +792,19 @@ impl LocalStore {
}

pub fn save_state(&self, state: &State) -> anyhow::Result<()> {
self.save_state_removing_device_turns(state, None)
self.save_state_removing_device_turns(state, None, None)
}

pub(crate) fn save_state_removing_device_turns(&self, state: &State, device_id: Option<&str>) -> anyhow::Result<()> {
pub(crate) fn save_state_removing_device_turns(&self, state: &State, device_id: Option<&str>, roster: Option<&OutboxItem>) -> anyhow::Result<()> {
let mut connection = self.connection.lock().unwrap();
let tx = connection.transaction()?;
save_state_tx(&tx, state)?;
if let Some(id) = device_id { tx.execute("DELETE FROM device_turns WHERE id = ?1", [id])?; }
if let Some(item) = roster {
tx.execute("INSERT OR IGNORE INTO roster_baseline (name, seq, json) SELECT 'queued', seq, json FROM roster_baseline WHERE name = 'observed'", [])?;
tx.execute("INSERT OR IGNORE INTO roster_baseline (name, seq, json) VALUES ('queued', 0, ?1)", [serde_json::to_string(&crate::model::RosterBlob::default())?])?;
queue_outbox_tx(&tx, item)?;
}
tx.commit()?;
Ok(())
}
Expand Down Expand Up @@ -1535,6 +1547,9 @@ impl LocalStore {
)?;
}
save_metadata_tx(&tx, state)?;
sync_json_table(&tx, "device_names", state.device_names.iter()
.map(|(id, name)| (id.clone(), serde_json::to_string(&(id, name))))
.collect())?;
sync_json_table(
&tx,
"devices",
Expand Down Expand Up @@ -1745,6 +1760,7 @@ impl LocalStore {
"outbox",
"sent_jobs",
"device_turns",
"device_names",
] {
tx.execute(&format!("DELETE FROM {table}"), [])?;
}
Expand Down Expand Up @@ -1810,6 +1826,9 @@ fn reconcile_schedules_tx(tx: &Transaction<'_>, state: &State) -> anyhow::Result
fn save_state_tx(tx: &Transaction<'_>, state: &State) -> anyhow::Result<()> {
reconcile_schedules_tx(tx,state)?;
save_metadata_tx(tx, state)?;
sync_json_table(tx, "device_names", state.device_names.iter()
.map(|(id, name)| (id.clone(), serde_json::to_string(&(id, name))))
.collect())?;
sync_json_table(
tx,
"devices",
Expand Down Expand Up @@ -1874,6 +1893,7 @@ fn save_metadata_tx(tx: &Transaction<'_>, state: &State) -> anyhow::Result<()> {
state.credentials_uploaded,
state.paused,
serde_json::to_string(&crate::model::RosterBlob {
device_name_edits: state.device_name_edits.clone(),
policy_clock: state.policy_clock,
pause_version: state.pause_version.clone(),
capability_versions: state.capability_versions.clone(),
Expand Down
6 changes: 6 additions & 0 deletions crates/cli/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,12 @@ pub struct WorkflowRecord {
pub struct RosterBlob {
pub bots: Vec<Bot>,
pub chats: Vec<ChatMeta>,
/// Account aliases keyed by Device id; machine metadata keeps the original name.
#[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
pub device_names: std::collections::BTreeMap<String, String>,
/// Opaque edit ids include resets, whose alias value is absent.
#[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
pub device_name_edits: std::collections::BTreeMap<String, String>,
#[serde(default)]
pub routines: Vec<Routine>,
#[serde(default)]
Expand Down
Loading