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(spaces-macos): the host service never checks in as the app (Cua Spaces could not be reopened) (#6057)

dduponte3379370551a
diff --git a/.github/workflows/ci-cua-spaces-macos.yml b/.github/workflows/ci-cua-spaces-macos.yml
index b06e8fac8b..a74037e981 100644
--- a/.github/workflows/ci-cua-spaces-macos.yml
+++ b/.github/workflows/ci-cua-spaces-macos.yml
@@ -310,6 +310,12 @@ jobs:
test -s "$app/Contents/Resources/Assets.car"
test -s "$app/Contents/Resources/AppIcon.icns"
test "$(/usr/libexec/PlistBuddy -c 'Print :CFBundleIconName' "$app/Contents/Info.plist")" = AppIcon
+ # The host service never checks in with LaunchServices as the app:
+ # while a windowless helper held the app's entry, opening Cua Spaces
+ # failed with error -600 (0.17.0-beta.3/beta.4, through the Sentry SDK).
+ - name: The host service is not an app
+ working-directory: apps/cua-spaces-macos
+ run: scripts/tests/helper-not-an-app.sh ".build/app/Cua Spaces.app"
# Cua Spaces Local (--channel local) runs beside an installed Cua
# Spaces and must not be able to touch it (CUA-1776): the Local bundle
# id, helper, URL scheme and home, staging, and no Sparkle feed. The
diff --git a/apps/cua-spaces-macos/Sources/CuaSpacesMac/main.swift b/apps/cua-spaces-macos/Sources/CuaSpacesMac/main.swift
index ef2875305f..a519161031 100644
--- a/apps/cua-spaces-macos/Sources/CuaSpacesMac/main.swift
+++ b/apps/cua-spaces-macos/Sources/CuaSpacesMac/main.swift
@@ -24,10 +24,19 @@ if CommandLine.arguments.dropFirst().first == "--check-launch-only" {
// build changes nothing here.
TargetEnvironment.pinChannel()
+let launchArgs = Array(CommandLine.arguments.dropFirst()).filter { !$0.hasPrefix("-psn_") }
+
// Crash reporting, as early as the switch can be read (it lives in the
// channel's home, settled above) and before the Rust core or the daemon start:
// it sets the CUA_CRASH_* variables they inherit. Off without a DSN or consent.
-CrashReporting.start()
+// The host service sets only those variables for its cua-spacesd: the Swift
+// SDK would check the windowless helper in with LaunchServices as the app,
+// and opening Cua Spaces would then fail while the service runs.
+if HelperService.isServiceCommand(launchArgs) {
+ CrashReporting.prepareService()
+} else {
+ CrashReporting.start()
+}
// The host service, run by this same executable so every permission is the
// app's ("Cua Spaces"; ServiceLauncher.swift), before anything else:
@@ -41,7 +50,6 @@ CrashReporting.start()
// SMAppService, which only this bundle's own code may register. cua-host's
// app-helper runner runs it (from the app's SDK and from the `cua` CLI);
// prints `{"status": ...}` (and `"error"`).
-let launchArgs = Array(CommandLine.arguments.dropFirst()).filter { !$0.hasPrefix("-psn_") }
if launchArgs.first == "--service-helper" {
if launchArgs.dropFirst().first == "run" { ServiceLauncher.serve() }
exit(HelperService.run(Array(launchArgs.dropFirst())))
diff --git a/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/CrashReporting.swift b/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/CrashReporting.swift
index 902326e1a8..ef1c76cbc5 100644
--- a/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/CrashReporting.swift
+++ b/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/CrashReporting.swift
@@ -218,6 +218,25 @@ public enum CrashReporting {
}
}
+ /// The host service (`CuaSpacesMac --service-helper run`): only the
+ /// variables its cua-spacesd child reports with. The Swift SDK is not
+ /// started there: starting it checks the windowless helper in with
+ /// LaunchServices as "Cua Spaces" itself, and opening the app then fails
+ /// (error -600) while the helper runs. Returns whether the variables were
+ /// set (a DSN and consent).
+ @discardableResult
+ public static func prepareService(bundle: Bundle = .main,
+ environment: [String: String] = ProcessInfo.processInfo.environment) -> Bool {
+ guard environment["CUA_SPACES_FIXTURES"] != "1",
+ let settings = settings(info: bundle.infoDictionary ?? [:]),
+ currentlyAllowed() else { return false }
+ for (key, value) in childEnvironment(settings) { setenv(key, value, 1) }
+ return true
+ }
+
+ /// Whether the Swift SDK was started in this process.
+ public static var isStarted: Bool { started }
+
/// The Settings switch changed: closes the SDK when reports are no
/// longer allowed (it is not restarted until the next launch).
public static func switchChanged(enabled: Bool, noticeShown: Bool) {
diff --git a/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/LoginItem.swift b/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/LoginItem.swift
index 67cf7de6f9..bdb31458f8 100644
--- a/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/LoginItem.swift
+++ b/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/LoginItem.swift
@@ -218,6 +218,23 @@ public enum HelperService {
/// (EX_TEMPFAIL): non-zero, so launchd starts it again.
public static let restartExitCode: Int32 = 75
+ /// A launch of this executable that is not the app's window: the host
+ /// service and its commands (`--service-helper`, `check-permissions`,
+ /// `preflight-permissions`). These must never touch AppKit or start
+ /// anything that does (the Sentry SDK): a windowless process checked in
+ /// with LaunchServices as "Cua Spaces" makes opening the app fail.
+ public static func isServiceCommand(_ args: [String]) -> Bool {
+ ["--service-helper", "check-permissions", "preflight-permissions"].contains(args.first ?? "")
+ }
+
+ /// The service holds the app's LaunchServices entry: it shows as a
+ /// running "Cua Spaces" (0.17.0-beta.3 and beta.4 checked in through the
+ /// Sentry SDK). Opening the app then fails until the service restarts.
+ static func serviceHoldsAppEntry(servicePid: pid_t, bundleId: String) -> Bool {
+ NSRunningApplication.runningApplications(withBundleIdentifier: bundleId)
+ .contains { $0.processIdentifier == servicePid }
+ }
+
/// The service started from an older build than the one installed now:
/// it started before this app's cua-spacesd was put in place. That is
/// the file's status-change time (an update writes new files, and no
@@ -277,10 +294,14 @@ public enum HelperService {
guard let executable else { return }
let target = "gui/\(getuid())/\(label)"
DispatchQueue.global(qos: .utility).async {
- guard let pid = runningPid(target: target), let started = processStart(pid),
- let installed = placedTime(daemonPath(executable: executable).path),
- isStale(serviceStarted: started, daemonInstalled: installed) else { return }
- NSLog("cua-spaces: the host service (pid %d) is from before this build; restarting it", pid)
+ guard let pid = runningPid(target: target) else { return }
+ let stale = processStart(pid).flatMap { started in
+ placedTime(daemonPath(executable: executable).path).map { isStale(serviceStarted: started, daemonInstalled: $0) }
+ } ?? false
+ let holdsEntry = serviceHoldsAppEntry(servicePid: pid, bundleId: Bundle.main.bundleIdentifier ?? "com.trycua.spaces.macos")
+ guard stale || holdsEntry else { return }
+ NSLog("cua-spaces: restarting the host service (pid %d): %@", pid,
+ stale ? "it is from before this build" : "it holds the app's LaunchServices entry")
let p = Process()
p.executableURL = URL(fileURLWithPath: "/bin/launchctl")
p.arguments = ["kickstart", "-k", target]
diff --git a/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/HelperServiceTests.swift b/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/HelperServiceTests.swift
index 32a0416752..759dab2b67 100644
--- a/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/HelperServiceTests.swift
+++ b/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/HelperServiceTests.swift
@@ -122,4 +122,18 @@ struct HelperServiceTests {
if let born, let started { #expect(!HelperService.isStale(serviceStarted: started, daemonInstalled: born)) }
#expect(HelperService.processStart(-1) == nil)
}
+
+ /// The host service and its commands never start the Sentry SDK (or
+ /// anything else that checks a process in with LaunchServices); the app
+ /// does. scripts/tests/helper-not-an-app.sh checks the built bundle.
+ @Test func theServiceCommandsAreNotTheApp() {
+ for args in [["--service-helper", "run"], ["--service-helper", "status"], ["check-permissions"], ["preflight-permissions"]] {
+ #expect(HelperService.isServiceCommand(args), "\(args)")
+ }
+ for args in [[], ["-NSDocumentRevisionsDebugMode", "YES"], ["--check-launch-only"], ["cua://add/ABCD"]] {
+ #expect(!HelperService.isServiceCommand(args), "\(args)")
+ }
+ // This test process is not an app another service could be mistaken for.
+ #expect(!HelperService.serviceHoldsAppEntry(servicePid: getpid(), bundleId: "com.trycua.spaces.macos"))
+ }
}
diff --git a/apps/cua-spaces-macos/scripts/tests/helper-not-an-app.sh b/apps/cua-spaces-macos/scripts/tests/helper-not-an-app.sh
new file mode 100755
index 0000000000..811845223f
--- /dev/null
+++ b/apps/cua-spaces-macos/scripts/tests/helper-not-an-app.sh
@@ -0,0 +1,57 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: FSL-1.1-MIT
+# Copyright (c) 2026 Cua AI, Inc.
+#
+# The host service (`CuaSpacesMac --service-helper run`) must never check in
+# with LaunchServices as the app: while a windowless helper holds the app's
+# entry, opening Cua Spaces fails (error -600). Runs the bundle's helper in a
+# temporary home with crash reporting allowed (the Sentry SDK did the check-in
+# in 0.17.0-beta.3 and beta.4) and a local-only cua-spacesd, then asks
+# LaunchServices whether the helper's pid is an app.
+#
+# scripts/tests/helper-not-an-app.sh "path/Cua Spaces.app"
+set -euo pipefail
+app="${1:?usage: helper-not-an-app.sh <Cua Spaces.app>}"
+exe="$app/Contents/MacOS/CuaSpacesMac"
+[ -x "$exe" ] || { echo "no $exe" >&2; exit 2; }
+
+home="$(mktemp -d)"
+pid=""
+cleanup() {
+ if [ -n "$pid" ]; then
+ pkill -TERM -P "$pid" 2>/dev/null || true
+ kill -TERM "$pid" 2>/dev/null || true
+ wait "$pid" 2>/dev/null || true
+ fi
+ rm -rf "$home"
+}
+trap cleanup EXIT
+
+mkdir -p "$home/.cua/helper" "$home/.cua/telemetry" "$home/data"
+# Crash reporting allowed: the notice was shown and telemetry is on.
+echo 1 >"$home/.cua/telemetry/notice_shown"
+cat >"$home/.cua/helper/service.json" <<JSON
+{"args": ["serve", "--listen", "127.0.0.1:0", "--no-desktop", "--no-driver", "--no-mcp", "--data-dir", "$home/data"],
+ "env": [], "log": "$home/service.log"}
+JSON
+
+# No CI, DO_NOT_TRACK or CUA_TELEMETRY: they would turn crash reporting off
+# and hide the check-in this test is for.
+env -u CI -u GITHUB_ACTIONS -u DO_NOT_TRACK -u CUA_TELEMETRY HOME="$home" CUA_HOME="$home/.cua" \
+ "$exe" --service-helper run >"$home/helper.out" 2>&1 &
+pid=$!
+
+registered=""
+for _ in $(seq 1 16); do
+ sleep 0.5
+ kill -0 "$pid" 2>/dev/null || { echo "the helper exited early:" >&2; cat "$home/helper.out" "$home/service.log" >&2 2>/dev/null; exit 1; }
+ registered="$(lsappinfo find "pid=$pid" 2>/dev/null || true)"
+ [ -n "$registered" ] && break
+done
+
+if [ -n "$registered" ]; then
+ echo "the host service (pid $pid) checked in with LaunchServices as an app:" >&2
+ lsappinfo info "$registered" >&2 || true
+ exit 1
+fi
+echo "ok: the host service ran for 8 s and never checked in as an app"