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(cua-spaces-macos): make Restart to update install the downloaded update (#6759)

cua-agents[bot]e1c784079a92
diff --git a/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/UpdateDriver.swift b/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/UpdateDriver.swift
index 6fc611f1e6..181d66b1b6 100644
--- a/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/UpdateDriver.swift
+++ b/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/UpdateDriver.swift
@@ -159,8 +159,16 @@ public final class UpdateUserDriver: NSObject, SPUUserDriver {
public var onEvent: ((String) -> Void)?
/// Sparkle's held "ready to install" reply.
private var heldReply: ((SPUUserUpdateChoice) -> Void)?
- /// The silent path's immediate install (`willInstallUpdateOnQuit`).
- private var immediateInstall: (() -> Void)?
+ /// An update downloaded on the silent path (`willInstallUpdateOnQuit`).
+ /// Sparkle's session for it has ended and its installer tool waits for
+ /// the app to quit, so the install block it handed out is dead.
+ private var silentPending = false
+ /// "Restart to update" was pressed on the silent path; the resumed
+ /// session's ready-to-install reply is answered with `.install`.
+ private var installWhenReady = false
+ /// Starts a check, which resumes the silently downloaded update and
+ /// reaches `showReady` (set by `UpdatesModel`).
+ public var resumeSilentUpdate: (() -> Void)?
private var told = false
public var state: UpdateState { flow.state }
@@ -189,9 +197,9 @@ public final class UpdateUserDriver: NSObject, SPUUserDriver {
}
/// The silent path (`automaticallyDownloadsUpdates`): Sparkle installs on
- /// quit, or at once through `install`.
- public func silentlyDownloaded(version: String, install: @escaping () -> Void) {
- immediateInstall = install
+ /// quit, or at once when `installNow` resumes the update.
+ public func silentlyDownloaded(version: String) {
+ silentPending = true
if flow.state.version == nil { changed { $0.found(version: version) } }
becameReady()
}
@@ -206,8 +214,12 @@ public final class UpdateUserDriver: NSObject, SPUUserDriver {
reply(.install)
return true
}
- if let install = immediateInstall {
- install()
+ if silentPending, let resume = resumeSilentUpdate {
+ // Sparkle ignores the install block once it is not given the
+ // install (`willInstallUpdateOnQuit` returns false), so resume
+ // the update through a check and answer its ready prompt.
+ installWhenReady = true
+ resume()
return true
}
return false
@@ -268,6 +280,7 @@ public final class UpdateUserDriver: NSObject, SPUUserDriver {
/// a plain line for it (no dialog, so nothing loops).
func handleError(_ message: String) {
NSLog("Cua Spaces: the updater failed: %@", message)
+ installWhenReady = false
changed { $0.failed(message) }
}
@@ -281,8 +294,13 @@ public final class UpdateUserDriver: NSObject, SPUUserDriver {
public func showReady(toInstallAndRelaunch reply: @escaping (SPUUserUpdateChoice) -> Void) {
// Held until installNow: replying here would restart the app.
- heldReply = reply
becameReady()
+ if installWhenReady {
+ installWhenReady = false
+ reply(.install)
+ return
+ }
+ heldReply = reply
}
public func showInstallingUpdate(withApplicationTerminated applicationTerminated: Bool,
@@ -299,7 +317,8 @@ public final class UpdateUserDriver: NSObject, SPUUserDriver {
public func dismissUpdateInstallation() {
heldReply = nil
- immediateInstall = nil
+ silentPending = false
+ installWhenReady = false
told = false
changed { $0.dismissed() }
}
diff --git a/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/UpdatesModel.swift b/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/UpdatesModel.swift
index 2a932ad934..72802e2298 100644
--- a/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/UpdatesModel.swift
+++ b/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/UpdatesModel.swift
@@ -92,6 +92,7 @@ public final class SparkleUpdater: NSObject, UpdaterDriving, SPUUpdaterDelegate
super.init()
updater = SPUUpdater(hostBundle: bundle, applicationBundle: bundle, userDriver: driver, delegate: self)
driver.onChange = { [weak self] in self?.onChange?() }
+ driver.resumeSilentUpdate = { [weak self] in self?.updater.checkForUpdates() }
driver.onEvent = { [weak self] event in self?.onUpdateEvent?(event) }
let changed: @Sendable () -> Void = { [weak self] in
DispatchQueue.main.async { self?.onChange?() }
@@ -181,18 +182,19 @@ public final class SparkleUpdater: NSObject, UpdaterDriving, SPUUpdaterDelegate
}
public nonisolated func updaterDidNotFindUpdate(_ updater: SPUUpdater) { tell("not_found") }
+ /// What `willInstallUpdateOnQuit` returns: false leaves Sparkle in charge,
+ /// so the update installs when the app quits (CUA-3065). Sparkle then ends
+ /// its session and the install block it passed is dead, so "Restart to
+ /// update" resumes the update through a check instead (`installNow`).
+ static let handlesQuitInstallItself = false
+
/// The silent path (`automaticallyDownloadsUpdates`): the update is
- /// downloaded and the page offers "Restart to update" through
- /// `immediateInstallHandler`. Returning false leaves Sparkle in charge of
- /// the install, so the update is also applied when the app quits. Returning
- /// true would promise that the app installs it, and only the click does:
- /// users who never click (the app lives in the menu bar and notch) stayed
- /// on the old version for ever (CUA-3065).
+ /// downloaded and the page offers "Restart to update".
public nonisolated func updater(_ updater: SPUUpdater, willInstallUpdateOnQuit item: SUAppcastItem,
immediateInstallationBlock immediateInstallHandler: @escaping () -> Void) -> Bool {
let version = item.displayVersionString
- MainActor.assumeIsolated { driver.silentlyDownloaded(version: version, install: immediateInstallHandler) }
- return false
+ MainActor.assumeIsolated { driver.silentlyDownloaded(version: version) }
+ return Self.handlesQuitInstallItself
}
/// A cycle that ended with nothing to show clears a pending "checking".
diff --git a/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/UpdatesTests.swift b/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/UpdatesTests.swift
index c5b66ae63a..98c8b69b9c 100644
--- a/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/UpdatesTests.swift
+++ b/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/UpdatesTests.swift
@@ -226,17 +226,52 @@ struct UpdatesTests {
#expect(driver.state.status == .idle)
}
- @Test func theSilentPathReachesDownloadedWithItsImmediateInstall() {
+ @Test func theSilentPathReachesDownloaded() {
let driver = UpdateUserDriver(currentVersion: "0.9.2")
var events: [String] = []
- var installs = 0
driver.onEvent = { events.append($0) }
- driver.silentlyDownloaded(version: "0.9.3") { installs += 1 }
+ driver.silentlyDownloaded(version: "0.9.3")
#expect(driver.state.status == .downloaded && driver.state.version == "0.9.3")
#expect(events == ["downloaded"])
- #expect(installs == 0)
+ }
+
+ /// Sparkle ignores the install block unless the delegate takes the
+ /// install (returns true), and taking it loses the install on quit
+ /// (CUA-3065). So the delegate leaves it to Sparkle.
+ @Test func quitInstallStaysWithSparkle() {
+ #expect(SparkleUpdater.handlesQuitInstallItself == false)
+ }
+
+ /// 0.21.1: "Restart to update" did nothing on the silent path, since the
+ /// install block was dead. It must resume the update and install it.
+ @Test func restartToUpdateInstallsASilentlyDownloadedUpdate() {
+ let driver = UpdateUserDriver(currentVersion: "0.9.2")
+ var resumes = 0
+ var choices: [SPUUserUpdateChoice] = []
+ driver.resumeSilentUpdate = { resumes += 1 }
+ // Nothing downloaded: nothing to do.
+ #expect(!driver.installNow() && resumes == 0)
+ driver.silentlyDownloaded(version: "0.9.3")
+ #expect(driver.installNow())
+ #expect(resumes == 1)
+ // The resumed session asks to install: answered at once.
+ driver.showReady(toInstallAndRelaunch: { choices.append($0) })
+ #expect(choices == [.install])
+ // A later session's prompt is held again, not auto-answered.
+ driver.dismissUpdateInstallation()
+ driver.showReady(toInstallAndRelaunch: { choices.append($0) })
+ #expect(choices == [.install])
+ }
+
+ @Test func aFailedResumeDoesNotInstallALaterPrompt() {
+ let driver = UpdateUserDriver(currentVersion: "0.9.2")
+ var choices: [SPUUserUpdateChoice] = []
+ driver.resumeSilentUpdate = {}
+ driver.silentlyDownloaded(version: "0.9.3")
#expect(driver.installNow())
- #expect(installs == 1)
+ driver.handleError("offline")
+ driver.showReady(toInstallAndRelaunch: { choices.append($0) })
+ #expect(choices.isEmpty)
}
@Test func nothingFoundAndBackgroundErrorsStayQuiet() {