Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(lume): prevent ls hangs, add SSH fallback, fix bridged IP resolution

f-trycuac0cbadfa125b
diff --git a/libs/lume/src/Commands/SSH.swift b/libs/lume/src/Commands/SSH.swift
index 229ff689eb..a3ec38137b 100644
--- a/libs/lume/src/Commands/SSH.swift
+++ b/libs/lume/src/Commands/SSH.swift
@@ -66,31 +66,73 @@ struct SSH: AsyncParsableCommand {
throw SSHError.sshNotAvailable(name)
}
- // Create SSH client
+ // Try NIO SSH first, fall back to system SSH on connection failure
+ do {
+ try await executeWithNIOSSH(host: ipAddress)
+ } catch let error as SSHError {
+ // Only fall back on connection errors (not auth failures, timeouts, etc.)
+ if case .connectionFailed = error {
+ Logger.debug(
+ "NIO SSH connection failed, falling back to system SSH",
+ metadata: ["host": "\(ipAddress)"])
+ try executeWithSystemSSH(host: ipAddress)
+ } else {
+ throw error
+ }
+ }
+ }
+
+ /// Execute using the built-in NIO SSH client
+ @MainActor
+ private func executeWithNIOSSH(host: String) async throws {
let sshClient = SSHClient(
- host: ipAddress,
+ host: host,
port: 22,
user: user,
password: password
)
if command.isEmpty {
- // Interactive mode
try await sshClient.interactive()
} else {
- // Execute command
let fullCommand = command.joined(separator: " ")
let result = try await sshClient.execute(
command: fullCommand,
timeout: TimeInterval(timeout)
)
- // Print output
if !result.output.isEmpty {
print(result.output)
}
- // Exit with same code as remote command
+ if result.exitCode != 0 {
+ throw ExitCode(result.exitCode)
+ }
+ }
+ }
+
+ /// Execute using the system /usr/bin/ssh binary (fallback for restricted environments)
+ private func executeWithSystemSSH(host: String) throws {
+ let systemClient = SystemSSHClient(
+ host: host,
+ port: 22,
+ user: user,
+ password: password
+ )
+
+ if command.isEmpty {
+ try systemClient.interactive()
+ } else {
+ let fullCommand = command.joined(separator: " ")
+ let result = try systemClient.execute(
+ command: fullCommand,
+ timeout: TimeInterval(timeout)
+ )
+
+ if !result.output.isEmpty {
+ print(result.output)
+ }
+
if result.exitCode != 0 {
throw ExitCode(result.exitCode)
}
diff --git a/libs/lume/src/SSH/SystemSSHClient.swift b/libs/lume/src/SSH/SystemSSHClient.swift
new file mode 100644
index 0000000000..b033c64d61
--- /dev/null
+++ b/libs/lume/src/SSH/SystemSSHClient.swift
@@ -0,0 +1,152 @@
+import Foundation
+
+/// SSH client that delegates to the system's /usr/bin/ssh binary.
+/// Used as a fallback when NIO SSH cannot establish a direct TCP connection
+/// (e.g., in sandboxed environments where only system-signed binaries can
+/// access certain network interfaces like vmnet).
+///
+/// Uses SSH_ASKPASS to provide password authentication non-interactively.
+public final class SystemSSHClient: Sendable {
+ private let host: String
+ private let port: Int
+ private let user: String
+ private let password: String
+
+ public init(
+ host: String,
+ port: UInt16 = 22,
+ user: String = "lume",
+ password: String = "lume"
+ ) {
+ self.host = host
+ self.port = Int(port)
+ self.user = user
+ self.password = password
+ }
+
+ /// Execute a command on the remote host using system ssh
+ public func execute(command: String, timeout: TimeInterval = 60) throws -> SSHResult {
+ let askpassPath = try createAskpassScript()
+ defer { try? FileManager.default.removeItem(atPath: askpassPath) }
+
+ let process = Process()
+ process.executableURL = URL(fileURLWithPath: "/usr/bin/ssh")
+ process.arguments = sshArguments(extraArgs: ["\(user)@\(host)", command])
+
+ var environment = ProcessInfo.processInfo.environment
+ environment["SSH_ASKPASS"] = askpassPath
+ environment["SSH_ASKPASS_REQUIRE"] = "force"
+ environment["DISPLAY"] = ":0"
+ process.environment = environment
+
+ let stdoutPipe = Pipe()
+ let stderrPipe = Pipe()
+ process.standardOutput = stdoutPipe
+ process.standardError = stderrPipe
+ // Detach from controlling terminal so SSH_ASKPASS is used
+ process.standardInput = FileHandle.nullDevice
+
+ try process.run()
+
+ // Set up timeout
+ if timeout > 0 {
+ DispatchQueue.global().asyncAfter(deadline: .now() + timeout) {
+ if process.isRunning {
+ process.terminate()
+ }
+ }
+ }
+
+ process.waitUntilExit()
+
+ let stdoutData = stdoutPipe.fileHandleForReading.readDataToEndOfFile()
+ let stderrData = stderrPipe.fileHandleForReading.readDataToEndOfFile()
+ let output = String(data: stdoutData, encoding: .utf8) ?? ""
+ let errorOutput = String(data: stderrData, encoding: .utf8) ?? ""
+
+ // Filter out SSH warnings from stderr (known_hosts, etc.)
+ let filteredError = errorOutput.components(separatedBy: "\n")
+ .filter { line in
+ !line.contains("Warning: Permanently added") &&
+ !line.contains("known_hosts") &&
+ !line.trimmingCharacters(in: .whitespaces).isEmpty
+ }
+ .joined(separator: "\n")
+
+ let combinedOutput = filteredError.isEmpty ? output : output + filteredError
+
+ return SSHResult(
+ exitCode: process.terminationStatus,
+ output: combinedOutput
+ )
+ }
+
+ /// Start an interactive SSH session using system ssh
+ public func interactive() throws {
+ let askpassPath = try createAskpassScript()
+ defer { try? FileManager.default.removeItem(atPath: askpassPath) }
+
+ let process = Process()
+ process.executableURL = URL(fileURLWithPath: "/usr/bin/ssh")
+ process.arguments = sshArguments(extraArgs: ["-t", "\(user)@\(host)"])
+
+ var environment = ProcessInfo.processInfo.environment
+ environment["SSH_ASKPASS"] = askpassPath
+ environment["SSH_ASKPASS_REQUIRE"] = "force"
+ environment["DISPLAY"] = ":0"
+ process.environment = environment
+
+ // For interactive mode, pass through stdin/stdout/stderr
+ process.standardInput = FileHandle.standardInput
+ process.standardOutput = FileHandle.standardOutput
+ process.standardError = FileHandle.standardError
+
+ try process.run()
+ process.waitUntilExit()
+
+ if process.terminationStatus != 0 {
+ throw SSHError.connectionFailed(
+ "System SSH exited with code \(process.terminationStatus)"
+ )
+ }
+ }
+
+ // MARK: - Private
+
+ private func sshArguments(extraArgs: [String]) -> [String] {
+ var args = [
+ "-o", "StrictHostKeyChecking=no",
+ "-o", "UserKnownHostsFile=/dev/null",
+ "-o", "LogLevel=ERROR",
+ "-o", "ConnectTimeout=10",
+ ]
+
+ if port != 22 {
+ args += ["-p", "\(port)"]
+ }
+
+ args += extraArgs
+ return args
+ }
+
+ /// Creates a temporary script that outputs the password for SSH_ASKPASS
+ private func createAskpassScript() throws -> String {
+ let tempDir = FileManager.default.temporaryDirectory
+ let scriptPath = tempDir.appendingPathComponent("lume-askpass-\(UUID().uuidString).sh").path
+
+ let scriptContent = """
+ #!/bin/sh
+ echo '\(password.replacingOccurrences(of: "'", with: "'\\''"))'
+ """
+
+ guard FileManager.default.createFile(
+ atPath: scriptPath,
+ contents: scriptContent.data(using: .utf8),
+ attributes: [.posixPermissions: 0o700]
+ ) else {
+ throw SSHError.connectionFailed("Failed to create askpass script")
+ }
+
+ return scriptPath
+ }
+}
diff --git a/libs/lume/src/Utils/NetworkUtils.swift b/libs/lume/src/Utils/NetworkUtils.swift
index c515acd1bd..eefebf1555 100644
--- a/libs/lume/src/Utils/NetworkUtils.swift
+++ b/libs/lume/src/Utils/NetworkUtils.swift
@@ -2,6 +2,38 @@ import Foundation
import Network
enum NetworkUtils {
+ /// Runs a process with a timeout, killing it if it exceeds the deadline.
+ /// Prevents `lume ls` and other commands from hanging when subprocesses
+ /// (lsof, nc, ping) get stuck during concurrent VM state transitions.
+ /// - Parameters:
+ /// - process: The configured Process to run
+ /// - timeout: Maximum time to wait in seconds
+ /// - Returns: true if the process completed successfully (exit code 0), false otherwise
+ private static func runWithTimeout(_ process: Process, timeout: TimeInterval) -> Bool {
+ do {
+ try process.run()
+ } catch {
+ return false
+ }
+
+ let deadline = Date().addingTimeInterval(timeout)
+ // Poll for process completion with 100ms intervals
+ while process.isRunning {
+ if Date() > deadline {
+ process.terminate()
+ // Give it a moment to terminate gracefully
+ Thread.sleep(forTimeInterval: 0.1)
+ if process.isRunning {
+ // Force kill via SIGKILL if terminate (SIGTERM) didn't work
+ kill(process.processIdentifier, SIGKILL)
+ }
+ return false
+ }
+ Thread.sleep(forTimeInterval: 0.1)
+ }
+ return process.terminationStatus == 0
+ }
+
/// Checks if an IP address is reachable by sending a ping
/// - Parameter ipAddress: The IP address to check
/// - Returns: true if the IP is reachable, false otherwise
@@ -14,13 +46,7 @@ enum NetworkUtils {
process.standardOutput = pipe
process.standardError = pipe
- do {
- try process.run()
- process.waitUntilExit()
- return process.terminationStatus == 0
- } catch {
- return false
- }
+ return runWithTimeout(process, timeout: 3)
}
/// Checks if a TCP port is open on an IP address
@@ -40,13 +66,8 @@ enum NetworkUtils {
process.standardOutput = pipe
process.standardError = pipe
- do {
- try process.run()
- process.waitUntilExit()
- return process.terminationStatus == 0
- } catch {
- return false
- }
+ // Process timeout = nc timeout + 2s buffer for process overhead
+ return runWithTimeout(process, timeout: timeout + 2)
}
/// Checks if SSH (port 22) is available on an IP address
@@ -69,13 +90,8 @@ enum NetworkUtils {
process.standardOutput = pipe
process.standardError = pipe
- do {
- try process.run()
- process.waitUntilExit()
- // lsof returns 0 if it finds a matching process
- return process.terminationStatus == 0
- } catch {
- return false
- }
+ // lsof should complete quickly for local port checks;
+ // 5s timeout guards against hangs during VM state transitions
+ return runWithTimeout(process, timeout: 5)
}
}
diff --git a/libs/lume/src/Virtualization/DHCPLeaseParser.swift b/libs/lume/src/Virtualization/DHCPLeaseParser.swift
index e1025bf9cf..e7db35ca63 100644
--- a/libs/lume/src/Virtualization/DHCPLeaseParser.swift
+++ b/libs/lume/src/Virtualization/DHCPLeaseParser.swift
@@ -53,24 +53,77 @@ private struct DHCPLease {
enum DHCPLeaseParser {
private static let leasePath = "/var/db/dhcpd_leases"
- /// Retrieves the IP address for a given MAC address from the DHCP lease file
+ /// Retrieves the IP address for a given MAC address.
+ /// First checks vmnet's DHCP leases (for NAT mode), then falls back to the
+ /// system ARP table (for bridged mode where the VM gets its IP from the
+ /// physical network's DHCP server).
/// - Parameter macAddress: The MAC address to look up
/// - Returns: The IP address if found, nil otherwise
static func getIPAddress(forMAC macAddress: String) -> String? {
- guard let leaseContents = try? String(contentsOfFile: leasePath, encoding: .utf8) else {
- return nil
- }
-
// Normalize the input MAC address to ensure consistent format
let normalizedMacAddress = macAddress.split(separator: ":").map { component in
let hex = String(component)
return hex.count == 1 ? "0\(hex)" : hex
}.joined(separator: ":")
-
- let leases = try? parseDHCPLeases(leaseContents)
- return leases?.first { lease in
- lease.macAddress == normalizedMacAddress
- }?.ipAddress
+
+ // Check both sources: vmnet DHCP leases (NAT mode) and ARP table (bridged mode)
+ let dhcpIP: String? = {
+ guard let leaseContents = try? String(contentsOfFile: leasePath, encoding: .utf8),
+ let leases = try? parseDHCPLeases(leaseContents) else {
+ return nil
+ }
+ return leases.first(where: { $0.macAddress == normalizedMacAddress })?.ipAddress
+ }()
+ let arpIP = getIPFromARP(forMAC: normalizedMacAddress)
+
+ // If ARP has an entry, the VM is actively communicating on the physical
+ // network (bridged mode). Prefer ARP since DHCP may contain a stale NAT
+ // lease from a previous session.
+ if let arpIP = arpIP {
+ return arpIP
+ }
+
+ // No ARP entry means NAT mode (vmnet traffic doesn't appear in host ARP table)
+ return dhcpIP
+ }
+
+ /// Looks up an IP address in the system ARP table by MAC address.
+ /// Used for bridged networking where the VM gets its IP from the physical
+ /// network's DHCP server instead of vmnet.
+ private static func getIPFromARP(forMAC macAddress: String) -> String? {
+ let process = Process()
+ process.executableURL = URL(fileURLWithPath: "/usr/sbin/arp")
+ process.arguments = ["-an"]
+
+ let pipe = Pipe()
+ process.standardOutput = pipe
+ process.standardError = Pipe()
+
+ do {
+ try process.run()
+ process.waitUntilExit()
+ } catch {
+ return nil
+ }
+
+ guard let output = String(data: pipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) else {
+ return nil
+ }
+
+ // ARP output format: ? (192.168.14.18) at be:bd:fd:9d:8b:38 on en0 ifscope [ethernet]
+ let lowercaseMAC = macAddress.lowercased()
+ for line in output.components(separatedBy: "\n") {
+ if line.lowercased().contains(lowercaseMAC) {
+ // Extract IP from parentheses: ? (192.168.14.18) at ...
+ if let openParen = line.firstIndex(of: "("),
+ let closeParen = line.firstIndex(of: ")") {
+ let ip = String(line[line.index(after: openParen)..<closeParen])
+ return ip
+ }
+ }
+ }
+
+ return nil
}
/// Parses the contents of a DHCP lease file into lease entries