Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/features/remote-workspaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,22 @@ Existing remote-workspace records keep their paths and connection metadata.
Legacy connection IDs that included the SSH port are migrated together with
their password-vault and workspace references.

The SSH port is part of a connection's identity. Servers behind one address that
differ only by port (for example cluster nodes mapped to ports 2201 and 2202)
are separate profiles, workspaces, and session stores. Default-port (22)
profiles keep the `ssh-user@host` id, workspace id, and on-disk runtime and
session paths unchanged. New profiles on other ports use `ssh-user@host#port`;
`#` was never produced by an earlier build, so older peers treat these ids as
opaque instead of folding them into the legacy `ssh-user@host:port` alias.
Profiles saved by earlier builds keep their id when the port is edited, and
their workspaces and sessions stay where they are; saving a profile for another
port never overwrites them. Re-adding a deleted legacy profile reuses its old id
while workspace records still reference it. Workspace records that an older
host stored under the host-only identity keep that identity after an upgrade.
Peer hosts advertise `ssh_port_scoped_identity_v1`; against an older peer the
controller keeps legacy ids and refuses to add a second port for the same host
and user instead of replacing the existing profile.

Legacy local-Docker profiles do not need an SSH password-vault entry, even if
their old serialized auth placeholder is an empty password.

Expand Down
8 changes: 6 additions & 2 deletions src/apps/desktop/src/api/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1396,7 +1396,7 @@ pub async fn open_remote_workspace(
request: OpenRemoteWorkspaceRequest,
) -> Result<WorkspaceInfoDto, String> {
use openbitfun_core::service::remote_ssh::normalize_remote_workspace_path;
use openbitfun_core::service::remote_ssh::workspace_state::remote_workspace_stable_id;
use openbitfun_core::service::remote_ssh::workspace_state::remote_workspace_stable_id_for_connection;
use openbitfun_core::service::workspace::{
remote_workspace_connection_conflict_message, RemoteConnectionRebind,
WorkspaceCreateOptions,
Expand Down Expand Up @@ -1465,7 +1465,11 @@ pub async fn open_remote_workspace(
request.connection_name.clone()
});

let stable_workspace_id = remote_workspace_stable_id(&ssh_host, &remote_path);
let stable_workspace_id = remote_workspace_stable_id_for_connection(
&ssh_host,
Some(&request.connection_id),
&remote_path,
);

let display_name = remote_path
.split('/')
Expand Down
8 changes: 7 additions & 1 deletion src/apps/desktop/src/api/path_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ fn runtime_root_for_workspace_info(workspace: &WorkspaceInfo) -> Result<PathBuf,
})?;

let remote_root = normalize_remote_workspace_path(&workspace.root_path.to_string_lossy());
return Ok(remote_workspace_runtime_root(ssh_host, &remote_root));
let identity_host =
openbitfun_core::service::remote_ssh::workspace_state::ssh_workspace_identity_host(
ssh_host,
workspace.remote_ssh_connection_id(),
&remote_root,
);
return Ok(remote_workspace_runtime_root(&identity_host, &remote_root));
}

Ok(get_path_manager_arc().project_runtime_root(&workspace.root_path))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public object LegacyWorkspaceCompatibility {
?.takeIf { host -> host != LOCALHOST || connectionId != null }
val candidates = catalog.filter { candidate ->
normalizedRoot(candidate.path) == root &&
// A reference without a connection id predates port-scoped SSH
// profiles (`ssh-user@host#port`) and can never name one.
(connectionId != null || !isPortScopedSshConnectionId(candidate.remoteConnectionId)) &&
(connectionId == null || candidate.remoteConnectionId?.trim() == connectionId) &&
(sshHost == null || candidate.remoteSshHost?.trim() == sshHost)
}
Expand Down Expand Up @@ -87,6 +90,46 @@ public object LegacyWorkspaceCompatibility {
resolveReference(reference, catalog).identityOrNull

private const val LOCALHOST: String = "localhost"
private const val WORKSPACE_HOST_PREFIX: String = "workspace-host-"
private val PORT_SCOPED_SSH_ID = Regex("""^ssh-[^@]*@[^#]*#(\d+)(?:~\d+)?(?:-.*)?$""")

/**
* Mirrors `ssh_connection_id_port_scope` in the Rust workspace identity
* owner: `ssh-user@host#port` ids (also wrapped as
* `workspace-host-<encoded id>...`) name a distinct connection per port.
*/
public fun isPortScopedSshConnectionId(connectionId: String?): Boolean {
var id = connectionId?.trim().orEmpty()
if (id.startsWith(WORKSPACE_HOST_PREFIX)) {
id = percentDecode(id.removePrefix(WORKSPACE_HOST_PREFIX)) ?: return false
}
val port = PORT_SCOPED_SSH_ID.matchEntire(id)?.groupValues?.get(1) ?: return false
return (port.toIntOrNull() ?: Int.MAX_VALUE) <= 65535
}

private fun percentDecode(value: String): String? {
val bytes = ArrayList<Byte>(value.length)
var index = 0
while (index < value.length) {
if (value[index] == '%') {
if (index + 2 >= value.length) return null
// Like `decodeURIComponent`: exactly two hex digits (no sign).
val digits = value.substring(index + 1, index + 3)
if (!digits.all { it in '0'..'9' || it in 'a'..'f' || it in 'A'..'F' }) return null
val hex = digits.toInt(16)
bytes.add(hex.toByte())
index += 3
} else {
value[index].toString().encodeToByteArray().forEach(bytes::add)
index += 1
}
}
return try {
bytes.toByteArray().decodeToString(throwOnInvalidSequence = true)
} catch (_: CharacterCodingException) {
null
}
}

private fun normalizedRoot(path: String): String = path.trim().let { it.trimEnd('/').ifEmpty { it } }
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ class LegacyWorkspaceCompatibilityTest {
LegacyWorkspaceCompatibility.resolveReference(RemoteWorkspaceIdentity("/repo", " ", ""), catalog))
}

@Test
fun connectionlessLegacyReferenceNeverSelectsAPortScopedSshWorkspace() {
val legacy = RemoteWorkspaceIdentity("/srv/app", "ssh-root@cluster", "cluster", "legacy-id")
val node = RemoteWorkspaceIdentity("/srv/app", "ssh-root@cluster#2201", "cluster", "node-id")
val nodes = listOf(legacy, node)
assertEquals(legacy, LegacyWorkspaceCompatibility.resolve(RemoteWorkspaceIdentity("/srv/app", null, "cluster"), nodes))
assertEquals(node, LegacyWorkspaceCompatibility.resolve(RemoteWorkspaceIdentity("/srv/app", "ssh-root@cluster#2201", "cluster"), nodes))
assertTrue(LegacyWorkspaceCompatibility.isPortScopedSshConnectionId("workspace-host-ssh-root%40cluster%232201-container-web"))
assertFalse(LegacyWorkspaceCompatibility.isPortScopedSshConnectionId("ssh-root@cluster:2201"))
assertFalse(LegacyWorkspaceCompatibility.isPortScopedSshConnectionId("workspace-host-%ZZ"))
assertFalse(LegacyWorkspaceCompatibility.isPortScopedSshConnectionId("workspace-host-ssh-root%40cluster%+12201"))
assertFalse(LegacyWorkspaceCompatibility.isPortScopedSshConnectionId("workspace-host-ssh-root%40cluster%232201%C3"))
}

@Test
fun bareLocalhostHostIsIgnoredOnlyWithoutAConnectionId() {
// 1.0.0 stamped `localhost` on local workspaces: without a connection it says nothing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,14 @@ impl SessionStorePort for CoreSessionStorePort {
})?;
(
runtime
.context_for_remote_workspace(host, &workspace.root_path.to_string_lossy())
.context_for_remote_workspace(
&openbitfun_services_core::workspace_identity::ssh_workspace_identity_host(
host,
Some(connection_id),
&workspace.root_path.to_string_lossy(),
),
&workspace.root_path.to_string_lossy(),
)
.sessions_dir,
SessionStorageKind::Remote,
Some(connection_id.to_owned()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ pub fn permission_project_id_for_workspace_identity(

Ok(
openbitfun_services_core::workspace_identity::remote_workspace_stable_id(
&identity.hostname,
&identity.identity_host(),
identity.logical_workspace_path(),
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ impl ToolUseContext {
if workspace.is_remote() {
let identity = &workspace.session_identity;
Ok(remote_workspace_runtime_root(
&identity.hostname,
&identity.identity_host(),
identity.logical_workspace_path(),
))
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/crates/assembly/core/src/agentic/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl WorkspaceBinding {
}
return runtime_service
.context_for_remote_workspace(
&self.session_identity.hostname,
&self.session_identity.identity_host(),
self.session_identity.logical_workspace_path(),
)
.sessions_dir;
Expand Down
71 changes: 66 additions & 5 deletions src/crates/assembly/core/src/service/remote_ssh/workspace_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use crate::service::workspace_runtime::WorkspaceRuntimeService;
pub use openbitfun_services_integrations::remote_ssh::{
local_workspace_stable_storage_id, normalize_remote_workspace_path,
remote_root_to_mirror_subpath, remote_workspace_stable_id,
sanitize_remote_mirror_path_component, sanitize_ssh_connection_id_for_local_dir,
sanitize_ssh_hostname_for_mirror, unresolved_remote_session_storage_key, workspace_logical_key,
remote_workspace_stable_id_for_connection, sanitize_remote_mirror_path_component,
sanitize_ssh_connection_id_for_local_dir, sanitize_ssh_hostname_for_mirror,
ssh_workspace_identity_host, unresolved_remote_session_storage_key, workspace_logical_key,
workspace_session_identity, RemoteWorkspaceEntry, RemoteWorkspaceRegistry,
RemoteWorkspaceState, WorkspaceSessionIdentity, LOCAL_WORKSPACE_SSH_HOST,
};
Expand Down Expand Up @@ -319,13 +320,23 @@ impl RemoteWorkspaceStateManager {
let path_norm = normalize_remote_workspace_path(workspace_path);
if let Some(host) = remote_ssh_host.map(str::trim).filter(|s| !s.is_empty()) {
return runtime_service
.context_for_remote_workspace(host, &path_norm)
.context_for_remote_workspace(
&ssh_workspace_identity_host(host, remote_id, &path_norm),
&path_norm,
)
.sessions_dir;
}
if let Some(entry) = self.lookup_connection(workspace_path, remote_id).await {
if !entry.ssh_host.trim().is_empty() {
return runtime_service
.context_for_remote_workspace(&entry.ssh_host, &entry.remote_root)
.context_for_remote_workspace(
&ssh_workspace_identity_host(
&entry.ssh_host,
Some(&entry.connection_id),
&entry.remote_root,
),
&entry.remote_root,
)
.sessions_dir;
}
return unresolved_remote_session_storage_dir(remote_id.unwrap(), &path_norm);
Expand Down Expand Up @@ -381,7 +392,10 @@ pub async fn get_effective_session_path(
.sessions_dir;
}
return runtime_service
.context_for_remote_workspace(&identity.hostname, identity.logical_workspace_path())
.context_for_remote_workspace(
&identity.identity_host(),
identity.logical_workspace_path(),
)
.sessions_dir;
}

Expand Down Expand Up @@ -612,6 +626,53 @@ mod tests {
);
}

#[tokio::test]
async fn effective_session_path_is_port_scoped_only_for_port_scoped_connections() {
let root = "/home/wsp/projects/port-scope";
let legacy = remote_workspace_session_mirror_dir("cluster", root);
// Ids written by released builds keep the legacy session directory.
for connection_id in ["ssh-root@cluster", "ssh-root@cluster:2201"] {
let actual =
super::get_effective_session_path(root, Some(connection_id), Some("cluster")).await;
assert_eq!(actual, legacy, "{connection_id}");
}
let port_a =
super::get_effective_session_path(root, Some("ssh-root@cluster#2201"), Some("cluster"))
.await;
let port_b =
super::get_effective_session_path(root, Some("ssh-root@cluster#2202"), Some("cluster"))
.await;
assert_ne!(port_a, legacy);
assert_ne!(port_b, legacy);
assert_ne!(port_a, port_b);
assert_eq!(
port_a,
remote_workspace_session_mirror_dir("cluster#2201", root)
);

let manager = super::RemoteWorkspaceStateManager::new();
manager
.register_remote_workspace(
root.to_string(),
"ssh-root@cluster#2201".to_string(),
"Node 1".to_string(),
"cluster".to_string(),
)
.await;
assert_eq!(
manager
.get_effective_session_path(root, Some("ssh-root@cluster#2201"), None)
.await,
port_a
);
assert_eq!(
manager
.get_effective_session_path(root, Some("ssh-root@cluster#2201"), Some("cluster"))
.await,
port_a
);
}

#[tokio::test]
async fn manager_effective_session_path_returns_local_sessions_dir() {
let workspace_root = std::env::temp_dir().join(format!(
Expand Down
5 changes: 3 additions & 2 deletions src/crates/assembly/core/src/service/remote_ssh_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ pub mod workspace_state {
local_workspace_stable_storage_id, normalize_local_workspace_root_for_stable_id,
normalize_remote_workspace_path, remote_root_to_mirror_subpath,
remote_workspace_session_mirror_dir as remote_workspace_session_mirror_dir_at,
remote_workspace_stable_id, sanitize_remote_mirror_path_component,
sanitize_ssh_connection_id_for_local_dir, sanitize_ssh_hostname_for_mirror,
remote_workspace_stable_id, remote_workspace_stable_id_for_connection,
sanitize_remote_mirror_path_component, sanitize_ssh_connection_id_for_local_dir,
sanitize_ssh_hostname_for_mirror, ssh_workspace_identity_host,
unresolved_remote_session_storage_key, workspace_logical_key, workspace_session_identity,
WorkspaceSessionIdentity, LOCAL_WORKSPACE_SSH_HOST,
};
Expand Down
8 changes: 5 additions & 3 deletions src/crates/assembly/core/src/service/snapshot/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ fn bound_snapshot_context(
ssh_host,
remote_root,
} => {
if identity.hostname != *ssh_host
if identity.identity_host() != *ssh_host
|| identity.logical_workspace_path != *remote_root
|| identity.hostname.trim().is_empty()
|| identity
Expand Down Expand Up @@ -639,8 +639,10 @@ pub async fn open_snapshot_history_for_workspace(
let identity = &binding.session_identity;
let runtime_service = get_workspace_runtime_service_arc();
let context = if identity.is_remote() {
runtime_service
.context_for_remote_workspace(&identity.hostname, &identity.logical_workspace_path)
runtime_service.context_for_remote_workspace(
&identity.identity_host(),
&identity.logical_workspace_path,
)
} else {
runtime_service.context_for_local_workspace(Path::new(&identity.logical_workspace_path))
};
Expand Down
Loading
Loading