Skip to repository content

tenant.openagents/omega

No repository description is available.

OpenAgents Git authority 2026-07-28T03:34:58.951Z Public web read
NIP-34 coordinate30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omega
MaintainersHidden in public view
References2 branches · 1 tag
Read-only clonegit clone https://openagents.com/git/tenant.openagents/omega.git
Browse files

remote_identity.rs

200 lines · 6.9 KB · rust
1use crate::RemoteConnectionOptions;
2
3/// A normalized remote identity for matching live remote hosts against
4/// persisted remote metadata.
5///
6/// This mirrors workspace persistence identity semantics rather than full
7/// `RemoteConnectionOptions` equality, so runtime-only fields like SSH
8/// nicknames or Docker environment overrides do not affect matching.
9#[derive(Debug, Clone, PartialEq, Eq, Hash)]
10pub enum RemoteConnectionIdentity {
11    Ssh {
12        host: String,
13        username: Option<String>,
14        port: Option<u16>,
15    },
16    Wsl {
17        distro_name: String,
18        user: Option<String>,
19    },
20    Docker {
21        container_id: String,
22        name: String,
23        remote_user: String,
24    },
25    #[cfg(any(test, feature = "test-support"))]
26    Mock { id: u64 },
27}
28
29impl RemoteConnectionIdentity {
30    /// A stable string form of this identity, suitable for use in
31    /// persistence keys (e.g. database keys scoped to a remote host).
32    pub fn persistence_key(&self) -> String {
33        match self {
34            Self::Ssh {
35                host,
36                username,
37                port,
38            } => format!(
39                "ssh:{}@{}:{}",
40                username.as_deref().unwrap_or_default(),
41                host,
42                port.map(|port| port.to_string()).unwrap_or_default()
43            ),
44            Self::Wsl { distro_name, user } => format!(
45                "wsl:{}@{}",
46                user.as_deref().unwrap_or_default(),
47                distro_name
48            ),
49            Self::Docker {
50                container_id,
51                name,
52                remote_user,
53            } => format!("docker:{remote_user}@{name}:{container_id}"),
54            #[cfg(any(test, feature = "test-support"))]
55            Self::Mock { id } => format!("mock:{id}"),
56        }
57    }
58}
59
60impl From<&RemoteConnectionOptions> for RemoteConnectionIdentity {
61    fn from(options: &RemoteConnectionOptions) -> Self {
62        match options {
63            RemoteConnectionOptions::Ssh(options) => Self::Ssh {
64                host: options.host.to_string(),
65                username: options.username.clone(),
66                port: options.port,
67            },
68            RemoteConnectionOptions::Wsl(options) => Self::Wsl {
69                distro_name: options.distro_name.clone(),
70                user: options.user.clone(),
71            },
72            RemoteConnectionOptions::Docker(options) => Self::Docker {
73                container_id: options.container_id.clone(),
74                name: options.name.clone(),
75                remote_user: options.remote_user.clone(),
76            },
77            #[cfg(any(test, feature = "test-support"))]
78            RemoteConnectionOptions::Mock(options) => Self::Mock { id: options.id },
79        }
80    }
81}
82
83pub fn remote_connection_identity(options: &RemoteConnectionOptions) -> RemoteConnectionIdentity {
84    options.into()
85}
86
87pub fn same_remote_connection_identity(
88    left: Option<&RemoteConnectionOptions>,
89    right: Option<&RemoteConnectionOptions>,
90) -> bool {
91    match (left, right) {
92        (Some(left), Some(right)) => {
93            remote_connection_identity(left) == remote_connection_identity(right)
94        }
95        (None, None) => true,
96        _ => false,
97    }
98}
99
100#[cfg(test)]
101mod tests {
102    use std::collections::BTreeMap;
103
104    use super::*;
105    use crate::{DockerConnectionOptions, SshConnectionOptions, WslConnectionOptions};
106
107    #[test]
108    fn ssh_identity_ignores_non_persisted_runtime_fields() {
109        let left = RemoteConnectionOptions::Ssh(SshConnectionOptions {
110            host: "example.com".into(),
111            username: Some("anth".to_string()),
112            port: Some(2222),
113            password: Some("secret".to_string()),
114            args: Some(vec!["-v".to_string()]),
115            connection_timeout: Some(30),
116            nickname: Some("work".to_string()),
117            upload_binary_over_ssh: true,
118            ..Default::default()
119        });
120        let right = RemoteConnectionOptions::Ssh(SshConnectionOptions {
121            host: "example.com".into(),
122            username: Some("anth".to_string()),
123            port: Some(2222),
124            password: None,
125            args: None,
126            connection_timeout: None,
127            nickname: None,
128            upload_binary_over_ssh: false,
129            ..Default::default()
130        });
131
132        assert!(same_remote_connection_identity(Some(&left), Some(&right),));
133    }
134
135    #[test]
136    fn ssh_identity_distinguishes_persistence_key_fields() {
137        let left = RemoteConnectionOptions::Ssh(SshConnectionOptions {
138            host: "example.com".into(),
139            username: Some("anth".to_string()),
140            port: Some(2222),
141            ..Default::default()
142        });
143        let right = RemoteConnectionOptions::Ssh(SshConnectionOptions {
144            host: "example.com".into(),
145            username: Some("anth".to_string()),
146            port: Some(2223),
147            ..Default::default()
148        });
149
150        assert!(!same_remote_connection_identity(Some(&left), Some(&right),));
151    }
152
153    #[test]
154    fn wsl_identity_includes_user() {
155        let left = RemoteConnectionOptions::Wsl(WslConnectionOptions {
156            distro_name: "Ubuntu".to_string(),
157            user: Some("anth".to_string()),
158        });
159        let right = RemoteConnectionOptions::Wsl(WslConnectionOptions {
160            distro_name: "Ubuntu".to_string(),
161            user: Some("root".to_string()),
162        });
163
164        assert!(!same_remote_connection_identity(Some(&left), Some(&right),));
165    }
166
167    #[test]
168    fn docker_identity_ignores_non_persisted_runtime_fields() {
169        let left = RemoteConnectionOptions::Docker(DockerConnectionOptions {
170            name: "zed-dev".to_string(),
171            container_id: "container-123".to_string(),
172            remote_user: "anth".to_string(),
173            upload_binary_over_docker_exec: true,
174            use_podman: true,
175            remote_env: BTreeMap::from([("FOO".to_string(), "BAR".to_string())]),
176        });
177        let right = RemoteConnectionOptions::Docker(DockerConnectionOptions {
178            name: "zed-dev".to_string(),
179            container_id: "container-123".to_string(),
180            remote_user: "anth".to_string(),
181            upload_binary_over_docker_exec: false,
182            use_podman: false,
183            remote_env: BTreeMap::new(),
184        });
185
186        assert!(same_remote_connection_identity(Some(&left), Some(&right),));
187    }
188
189    #[test]
190    fn local_identity_matches_only_local_identity() {
191        let remote = RemoteConnectionOptions::Wsl(WslConnectionOptions {
192            distro_name: "Ubuntu".to_string(),
193            user: Some("anth".to_string()),
194        });
195
196        assert!(same_remote_connection_identity(None, None));
197        assert!(!same_remote_connection_identity(None, Some(&remote)));
198    }
199}
200
Served at tenant.openagents/omega Member data and write actions are omitted.