Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

fix(host): keep the phones a computer had when it joins its first account

leepokai14183246e20d
diff --git a/host/src/remote/owner.rs b/host/src/remote/owner.rs
index 5ee2c45a..64651386 100644
--- a/host/src/remote/owner.rs
+++ b/host/src/remote/owner.rs
@@ -15,7 +15,8 @@ pub fn owner(store: &Store) -> Option<Owner> {
}
/// Records the account the cloud reports. When it is another one than before (joining one, moving, or leaving),
-/// every device let in for another account goes. The first record after an upgrade only records.
+/// every device let in for another account goes; joining a first account keeps the phones paired before devices
+/// recorded their account. The first record after an upgrade only records.
pub fn remember(hub: &Hub, now: Option<&Owner>) -> Result<()> {
let saved = serde_json::to_string(&now)?;
let before = hub.store.kv_get(OWNER);
@@ -27,7 +28,7 @@ pub fn remember(hub: &Hub, now: Option<&Owner>) -> Result<()> {
let before = serde_json::from_str::<Option<Owner>>(&before).ok().flatten().map(|o| o.user_id);
let now = now.map(|o| o.user_id.as_str());
if before.as_deref() != now {
- let gone = hub.store.keep_devices_of(now)?;
+ let gone = hub.store.keep_devices_of(now, before.is_none())?;
tracing::info!(devices = gone.len(), "the computer changed its account; the old account's devices are gone");
if !gone.is_empty() {
hub.auth_changed();
@@ -102,4 +103,15 @@ mod tests {
assert!(keys(&hub).is_empty(), "leaving drops every device");
assert!(owner(&hub.store).is_none());
}
+
+ #[test]
+ fn joining_a_first_account_keeps_the_phones_it_had() {
+ let hub = hub();
+ remember(&hub, None).unwrap();
+ device(&hub, "legacy", None);
+ device(&hub, "mine", Some("user_a"));
+ device(&hub, "theirs", Some("user_b"));
+ remember(&hub, Some(&account("user_a"))).unwrap();
+ assert_eq!(keys(&hub), ["legacy", "mine"]);
+ }
}
diff --git a/host/src/store/devices.rs b/host/src/store/devices.rs
index 235a79d6..971637a7 100644
--- a/host/src/store/devices.rs
+++ b/host/src/store/devices.rs
@@ -120,15 +120,18 @@ impl Store {
Ok(n > 0)
}
- /// The computer changed the account it belongs to: every device let in for another account (or for none,
- /// before devices recorded theirs) goes, with its tickets. `None`: it left its account, and all go.
+ /// The computer changed the account it belongs to: every device let in for another account goes, with its
+ /// tickets. `None`: it left its account, and all go. Devices paired before devices recorded their account
+ /// stay only with `keep_unrecorded` (a computer joining its first account keeps the phones it had).
/// Returns the removed keys.
- pub fn keep_devices_of(&self, account: Option<&str>) -> Result<Vec<String>> {
+ pub fn keep_devices_of(&self, account: Option<&str>, keep_unrecorded: bool) -> Result<Vec<String>> {
let mut c = self.db.locked();
let tx = c.transaction()?;
let gone: Vec<String> = {
- let mut st = tx.prepare("SELECT key FROM devices WHERE account IS NULL OR account IS NOT ?1")?;
- st.query_map([account], |r| r.get(0))?.collect::<rusqlite::Result<_>>()?
+ let mut st = tx.prepare(
+ "SELECT key FROM devices WHERE (account IS NULL AND NOT ?2) OR (account IS NOT NULL AND account IS NOT ?1)",
+ )?;
+ st.query_map(params![account, keep_unrecorded], |r| r.get(0))?.collect::<rusqlite::Result<_>>()?
};
for key in &gone {
tx.execute("DELETE FROM devices WHERE key = ?1", [key])?;