Skip to main content

agentmux_srv_core/backend/storage/
muxbus_presence.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Whether a channel has a MuxBus sign-in of its own stored, asked of the store alone.
5//!
6//! A channel's sign-in is a row in `db_muxbus_credentials` (`channel:<ch>`, or `global` in a
7//! store that is already the channel's own) plus its tokens in the OS keychain under
8//! `muxbus:channel:<ch>`. The row is written when the sign-in completes and deleted when it
9//! ends. Both are keyed by the channel, not the version, so an in-app upgrade (the same channel,
10//! a new version) finds its predecessor's sign-in. Reading the row touches no keychain, so
11//! boot can ask this before deciding whether a reconnect would read one.
12
13use super::muxbus::current_row_id;
14use super::store::Store;
15
16impl Store {
17    /// Whether this process's channel has a MuxBus sign-in of its own stored. A SQL read only.
18    pub fn muxbus_has_own_sign_in(&self) -> bool {
19        self.muxbus_has_row(&current_row_id())
20    }
21
22    fn muxbus_has_row(&self, row_id: &str) -> bool {
23        let conn = self.conn.lock().unwrap_or_else(|e| e.into_inner());
24        conn.query_row("SELECT 1 FROM db_muxbus_credentials WHERE id = ?1", [row_id], |_| Ok(()))
25            .is_ok()
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::super::muxbus::row_id_for;
32    use super::*;
33
34    const CHANNEL: &str = "local-HEAD-7138a5-8dbe7897";
35
36    fn sign_in(store: &Store, row_id: &str) {
37        let conn = store.conn.lock().unwrap();
38        conn.execute(
39            "INSERT INTO db_muxbus_credentials (id, user_email, user_sub) VALUES (?1, 'a@b.c', 'sub-1')",
40            [row_id],
41        )
42        .unwrap();
43    }
44
45    #[test]
46    fn a_channel_with_no_sign_in_of_its_own_has_none() {
47        let store = Store::open_in_memory().unwrap();
48        assert!(!store.muxbus_has_row(&row_id_for(Some(CHANNEL), false)));
49    }
50
51    /// The host-wide `stable` sign-in is not this channel's: a local channel on a machine where
52    /// the installed release is signed in still has none of its own.
53    #[test]
54    fn the_stable_sign_in_is_not_the_channels() {
55        let store = Store::open_in_memory().unwrap();
56        sign_in(&store, &row_id_for(Some("stable"), false));
57        assert!(!store.muxbus_has_row(&row_id_for(Some(CHANNEL), false)));
58    }
59
60    #[test]
61    fn another_channels_sign_in_is_not_this_channels() {
62        let store = Store::open_in_memory().unwrap();
63        sign_in(&store, &row_id_for(Some("local-main-b28b7a-651ced27"), false));
64        assert!(!store.muxbus_has_row(&row_id_for(Some(CHANNEL), false)));
65    }
66
67    /// Sign in on version N; the in-app upgrade restarts srv as N+1 on the same store file, in
68    /// the same channel: the row is found, so boot reconnects (before: it was skipped and the
69    /// window showed signed out until someone signed in again).
70    #[test]
71    fn an_upgrade_in_the_same_channel_finds_the_previous_versions_sign_in() {
72        let dir = tempfile::tempdir().unwrap();
73        let path = dir.path().join("store.db");
74        let row = row_id_for(Some(CHANNEL), false);
75        {
76            let version_n = Store::open(&path).unwrap();
77            assert!(!version_n.muxbus_has_row(&row));
78            sign_in(&version_n, &row);
79        }
80        let version_n_plus_1 = Store::open(&path).unwrap();
81        assert!(version_n_plus_1.muxbus_has_row(&row));
82    }
83}