Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
fix(spaces-macos): restart a daemon that dies before it is watched; stable create-watchdog tests (#5825)
ddupontf06f203291c6
diff --git a/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/DaemonSupervisor.swift b/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/DaemonSupervisor.swiftindex 19247aaaa0..f61ea1cbde 100644--- a/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/DaemonSupervisor.swift+++ b/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/DaemonSupervisor.swift@@ -226,7 +226,8 @@ public final class DaemonSupervisor: @unchecked Sendable {restarted: (@MainActor @Sendable () async -> Void)? = nil,start: (@Sendable () -> String?)? = nil,daemonPid: (@Sendable () -> Int32?)? = nil,- accepted: (@Sendable () async -> UInt32?)? = nil) -> Task<Void, Never> {+ accepted: (@Sendable () async -> UInt32?)? = nil,+ watching: (@Sendable (Int32) -> Void)? = nil) -> Task<Void, Never> {let wake = SupervisorWake()let watcher = ProcessExitWatcher { wake.fire() }let pidNow: @Sendable () -> Int32? = daemonPid ?? { [self] in@@ -236,9 +237,25 @@ public final class DaemonSupervisor: @unchecked Sendable {defer { watcher.stop() }var failures = 0var reported = false+ // A daemon of this app's was watched or just started, so its pid+ // should be there to watch.+ var expectPid = falsewhile !Task.isCancelled {- // A daemon that already exited fires at once.- if failures == 0, let pid = pidNow() { watcher.watch(pid) }+ if failures == 0 {+ if let pid = pidNow() {+ // A daemon that already exited fires at once.+ watcher.watch(pid)+ expectPid = true+ watching?(pid)+ } else if expectPid {+ // That daemon is gone before its pid was read+ // again: there is nothing to watch, so probe now+ // instead of after a whole interval (once: a daemon+ // this app cannot watch is probed every interval).+ expectPid = false+ wake.fire()+ }+ }await wake.wait(for: failures == 0 ? interval : Self.backoff(failures))if Task.isCancelled { return }if await isUp() {@@ -253,6 +270,7 @@ public final class DaemonSupervisor: @unchecked Sendable {error = "the running cua daemon (pid \(running)) is another app's, which this app uses, and it does not answer"}if error == nil {+ expectPid = true// Running now (started again, or it ran and only this// app's connection to it stopped answering): connect// again, which supervises the new connection.diff --git a/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/DaemonRespawnTests.swift b/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/DaemonRespawnTests.swiftindex 33698cd1a1..90616f565d 100644--- a/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/DaemonRespawnTests.swift+++ b/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/DaemonRespawnTests.swift@@ -15,6 +15,10 @@ import Testingvar starts = 0var probes = 0var restarted = 0+ /// The pids the supervisor said it watches, in order.+ var watched: [Int32] = []+ /// Whether the supervisor watches the daemon running now.+ var watchingIt: Bool { lock.withLock { process.map { watched.last == $0.processIdentifier } ?? false } }func spawn() {let p = Process()@@ -56,9 +60,12 @@ import Testingdaemon.spawn()return nil},- daemonPid: { daemon.pid })+ daemonPid: { daemon.pid },+ watching: { pid in daemon.lock.withLock { daemon.watched.append(pid) } })defer { task.cancel() }- try await Task.sleep(for: .milliseconds(300))+ // Killed only once the supervisor watches it (else only the hourly+ // probe could see the kill).+ #expect(await eventually(60) { daemon.watchingIt })#expect(daemon.lock.withLock { daemon.starts } == 0)// Waiting is not polling.#expect(daemon.lock.withLock { daemon.probes } == 0)@@ -69,16 +76,56 @@ import Testing#expect(await eventually(60) { daemon.lock.withLock { daemon.starts } == 1 })#expect(await eventually(60) { daemon.lock.withLock { daemon.restarted } == 1 })- // The new daemon is watched too. The supervisor watches the new pid- // only after its confirming probe (the second one: the miss, then- // this one), so wait for that probe instead of a fixed sleep; a kill- // before the watch is armed is never seen until the next hourly probe.- #expect(await eventually(60) { daemon.lock.withLock { daemon.probes } >= 2 })- try await Task.sleep(for: .milliseconds(200))+ // The new daemon is watched too (its own pid, after its confirming+ // probe).+ #expect(await eventually(60) { daemon.watchingIt })daemon.kill()#expect(await eventually(60) { daemon.lock.withLock { daemon.starts } == 2 })}+ /// A daemon started again that dies before the supervisor reads its+ /// pid leaves nothing to watch: it is probed (and started) again at+ /// once, not after the next probe an hour later.+ @Test func aDaemonThatDiesBeforeItIsWatchedIsStartedAgainAtOnce() async throws {+ let supervisor = try #require(DaemonSupervisor(bundledCua: "/bin/sh"))+ let daemon = Daemon()+ daemon.spawn()+ defer { daemon.stop() }+ final class Once: @unchecked Sendable { var done = false }+ let once = Once()+ let task = supervisor.supervise(+ interval: .seconds(3600),+ isUp: {+ let up = daemon.pid != nil+ // The first restart answers its confirming probe, then+ // dies before the supervisor reads its pid.+ let die = daemon.lock.withLock { () -> Bool in+ guard up, daemon.starts == 1, !once.done else { return false }+ once.done = true+ return true+ }+ if die {+ let p = daemon.lock.withLock { daemon.process }+ p?.terminate()+ p?.waitUntilExit()+ }+ return up+ },+ report: { _ in },+ start: {+ daemon.lock.withLock { daemon.starts += 1 }+ daemon.spawn()+ return nil+ },+ daemonPid: { daemon.pid },+ watching: { pid in daemon.lock.withLock { daemon.watched.append(pid) } })+ defer { task.cancel() }+ #expect(await eventually(60) { daemon.watchingIt })+ daemon.kill()+ #expect(await eventually(60) { daemon.lock.withLock { daemon.starts } == 2 })+ #expect(await eventually(60) { daemon.watchingIt })+ }+@Test func anExitWatcherFiresForAProcessThatIsAlreadyGone() async {let p = Process()p.executableURL = URL(fileURLWithPath: "/usr/bin/true")diff --git a/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/StalePollTests.swift b/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/StalePollTests.swiftindex dc70ce488e..37d47c312e 100644--- a/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/StalePollTests.swift+++ b/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/StalePollTests.swift@@ -133,7 +133,8 @@ import Testingmodel.listTimeout = 1model.hostTimeout = 1model.listStaleAfter = 3- model.createAcceptTimeout = 2+ // Creates keep the app's acceptance timeout: on a loaded run a+ // create the daemon takes may report later than any short one.model.reconnect = {guard let connection = next() else { return false }pending.replace(connection, live: nil)@@ -286,6 +287,10 @@ import Testinglet model = makeModel(gate: gate) { second }model.listStaleAfter = 600first.wedge()+ // Short for the attempt that hangs; the app's own for the retry,+ // which the daemon takes.+ let acceptTimeout = model.createAcceptTimeout+ model.createAcceptTimeout = 2let started = Date()let retries = Retries()@@ -293,8 +298,13 @@ import Testing// is made again, and the model runs the create once more by itself// (the row says "Retrying…"), over the new connection.let id = try await model.runCreate(args("e2e-1005-linux"), os: .linux, pendingId: "pending:one",- retrying: { error, _ in retries.errors.append(error) })- #expect(Date().timeIntervalSince(started) < 60)+ retrying: { error, _ in+ retries.errors.append(error)+ model.createAcceptTimeout = acceptTimeout+ })+ // Well before the 4 min stall this replaced (the retry may wait+ // the app's whole acceptance timeout on a loaded run).+ #expect(Date().timeIntervalSince(started) < 240)#expect(retries.errors.count == 1)#expect(retries.errors.first?.contains("didn't start this create") == true)#expect(model.reconnects == 1)@@ -341,6 +351,8 @@ import Testing// The new connection is wedged too.let model = makeModel(gate: gate) { connection }model.listStaleAfter = 600+ // Both attempts hang: a short wait is enough to see each one fail.+ model.createAcceptTimeout = 2connection.wedge()do {_ = try await model.runCreate(args("e2e-1005-linux"), os: .linux, pendingId: "pending:one")@@ -359,8 +371,12 @@ import Testinglet gate = LiveGate<LiveServices>()gate.resolve(LiveServices(backend: connection))let model = makeModel(gate: gate) { nil }+ let started = Date()let create = Task { try await model.runCreate(args("slow"), os: .linux, pendingId: "pending:slow") }- try await Task.sleep(for: .seconds(3))+ // Taken (the daemon said "preparing"), then held past the watchdog.+ #expect(await eventually { connection.createCalls == 1 })+ let past = model.createAcceptTimeout + 1 - Date().timeIntervalSince(started)+ if past > 0 { try await Task.sleep(for: .seconds(past)) }connection.inner.releaseCreate()let id = try await create.value#expect(id == "local:slow")