Skip to content

Commit 44dd983

Browse files
authored
feat(mac): add canonical elevation host workflow (#123569)
* feat(mac): add canonical elevation host workflow * fix(mac): clarify elevation host deployment * test(ui): stabilize cloud startup effort flow * fix(mac): fail closed on unknown TCC status * test: synchronize async CI boundaries * test: remove remaining CI timing races
1 parent 7292fbe commit 44dd983

25 files changed

Lines changed: 1553 additions & 406 deletions

‎apps/macos/README.md‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,43 @@ still runs in process as usual. App relocation, Sparkle updates, and post-update
4949
service repair are disabled in profile mode; update the installed app through
5050
the normal default-profile workflow.
5151

52-
## Packaging flow
52+
## Packaging flows
53+
54+
Development bundle (signed but not notarized):
5355

5456
```bash
5557
scripts/package-mac-app.sh
5658
```
5759

58-
Creates `dist/OpenClaw.app` and signs it via `scripts/codesign-mac-app.sh`.
60+
This creates `dist/OpenClaw.app` and signs it via `scripts/codesign-mac-app.sh`.
61+
It is not a distribution artifact. For a notarized app ZIP and DMG, use:
62+
63+
```bash
64+
scripts/package-mac-dist.sh
65+
```
66+
67+
For an unattended Peekaboo elevation host, use the closed Foundation signing
68+
profile and source-addressed ZIP workflow. `package` is an internal release
69+
operator command: it requires the OpenClaw Foundation signing identity and
70+
notarization credentials, and its archive is not a general-download artifact.
71+
72+
```bash
73+
scripts/mac-elevation-host.sh package
74+
scripts/mac-elevation-host.sh install \
75+
--archive "dist/elevation-host/OpenClaw-<full-source-sha>-stable.zip"
76+
scripts/mac-elevation-host.sh status
77+
```
78+
79+
The elevation package is ZIP-only, notarized and stapled, contains exactly
80+
`OpenClaw.app`, omits Apple Events entitlements, records an immutable receipt,
81+
and verifies a freshly extracted copy. Installation owns the separate
82+
`ai.openclaw.mac.elevation-host` launchd job with `RunAtLoad` and `KeepAlive`.
83+
It refuses to replace or race the ordinary `ai.openclaw.mac` Launch at login
84+
job. `recover` restores the recorded prior bundle after a failed cutover;
85+
`uninstall` removes only the elevation job and preserves the app, state,
86+
Keychain, TCC, and recovery receipt. Installation exits successfully once the
87+
launchd-owned process is Bridge-ready; missing TCC remains a degraded `status`
88+
result until the required grants are present.
5989

6090
## Signing behavior
6191

‎apps/macos/Sources/OpenClaw/AppLaunchPresentationPolicy.swift‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,53 @@
11
import Foundation
22

3-
struct AppLaunchPresentationPolicy: Equatable {
4-
let backgroundOnly: Bool
3+
struct AppLaunchRuntimePlan: Equatable {
4+
enum Mode: Equatable {
5+
case interactive
6+
case background
7+
case elevationHost
8+
}
9+
10+
let mode: Mode
11+
let attachOnly: Bool
512

613
init(arguments: [String]) {
7-
self.backgroundOnly = arguments.contains("--background-only")
14+
if arguments.contains("--elevation-host") {
15+
self.mode = .elevationHost
16+
self.attachOnly = true
17+
} else {
18+
self.mode = arguments.contains("--background-only") ? .background : .interactive
19+
self.attachOnly = arguments.contains("--attach-only") || arguments.contains("--no-launchd")
20+
}
821
}
922

1023
static var current: Self {
1124
Self(arguments: CommandLine.arguments)
1225
}
1326

27+
var isElevationHost: Bool {
28+
self.mode == .elevationHost
29+
}
30+
1431
var allowsAutomaticPresentation: Bool {
15-
!self.backgroundOnly
32+
self.mode == .interactive
1633
}
1734

1835
/// GUI-owned Keychain items may present SecurityAgent when a newly signed build is not in an item's ACL.
1936
/// Background hosts keep that state cold; config and environment still own their primary Gateway route.
2037
var allowsGatewayUIKeychainAccess: Bool {
21-
!self.backgroundOnly
38+
self.mode == .interactive
39+
}
40+
41+
var allowsUpdater: Bool {
42+
!self.isElevationHost
43+
}
44+
45+
var allowsDockIcon: Bool {
46+
!self.isElevationHost
47+
}
48+
49+
var allowsInteractiveServices: Bool {
50+
!self.isElevationHost
2251
}
2352

2453
func shouldAutoOpenChat(arguments: [String]) -> Bool {

‎apps/macos/Sources/OpenClaw/DashboardManager.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ private let dashboardManagerLogger = Logger(subsystem: "ai.openclaw", category:
1111
final class DashboardManager {
1212
static let shared = DashboardManager(
1313
automaticGatewayProfileRefreshEnabled:
14-
AppLaunchPresentationPolicy.current.allowsGatewayUIKeychainAccess)
14+
AppLaunchRuntimePlan.current.allowsGatewayUIKeychainAccess)
1515

1616
private struct AuxiliaryWindowInstance {
1717
var target: DashboardGatewayTarget

‎apps/macos/Sources/OpenClaw/DockIconManager.swift‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,18 @@ final class DockIconManager: NSObject, @unchecked Sendable {
3939
} ?? []
4040

4141
let hasVisibleWindows = !visibleWindows.isEmpty
42-
let policy: NSApplication.ActivationPolicy = !userWantsDockHidden || hasVisibleWindows
43-
? .regular
44-
: .accessory
42+
let policy = Self.activationPolicy(
43+
launchPlan: .current,
44+
userWantsDockHidden: userWantsDockHidden,
45+
hasVisibleWindows: hasVisibleWindows)
4546
guard NSApp.activationPolicy() != policy else { return }
4647
NSApp.setActivationPolicy(policy)
4748
}
4849
}
4950

5051
func temporarilyShowDock() {
5152
Task { @MainActor in
53+
guard AppLaunchRuntimePlan.current.allowsDockIcon else { return }
5254
guard NSApp != nil else {
5355
self.logger.warning("NSApp not ready, cannot show Dock icon")
5456
return
@@ -58,6 +60,15 @@ final class DockIconManager: NSObject, @unchecked Sendable {
5860
}
5961
}
6062

63+
static func activationPolicy(
64+
launchPlan: AppLaunchRuntimePlan,
65+
userWantsDockHidden: Bool,
66+
hasVisibleWindows: Bool) -> NSApplication.ActivationPolicy
67+
{
68+
guard launchPlan.allowsDockIcon else { return .accessory }
69+
return !userWantsDockHidden || hasVisibleWindows ? .regular : .accessory
70+
}
71+
6172
private func setupObservers() {
6273
Task { @MainActor [self] in
6374
guard let app = NSApp else {

‎apps/macos/Sources/OpenClaw/GatewayConnection.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ extension GatewayConnection {
10671067
}
10681068

10691069
static func activationBindingKey(
1070-
launchPolicy: AppLaunchPresentationPolicy,
1070+
launchPolicy: AppLaunchRuntimePlan,
10711071
loadOrCreate: () -> SymmetricKey?) -> SymmetricKey?
10721072
{
10731073
guard launchPolicy.allowsGatewayUIKeychainAccess else { return nil }

‎apps/macos/Sources/OpenClaw/GatewayLaunchAgentManager.swift‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ enum GatewayLaunchAgentManager {
1818
return testingDisableLaunchAgentMarkerURL
1919
}
2020
#endif
21-
let root = AppProfile.current.isActive
22-
? OpenClawPaths.stateDirURL
23-
: FileManager().homeDirectoryForCurrentUser.appendingPathComponent(".openclaw", isDirectory: true)
24-
return root.appendingPathComponent(self.disableLaunchAgentMarker)
21+
return self.disableLaunchAgentMarkerURL(in: OpenClawPaths.stateDirURL)
22+
}
23+
24+
static func disableLaunchAgentMarkerURL(in stateDirectoryURL: URL) -> URL {
25+
stateDirectoryURL.appendingPathComponent(self.disableLaunchAgentMarker)
2526
}
2627

2728
private static var plistURL: URL {

‎apps/macos/Sources/OpenClaw/MenuBar.swift‎

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ struct OpenClawApp: App {
4141
}
4242

4343
init() {
44+
let launchPlan = AppLaunchRuntimePlan.current
4445
if let error = AppProfile.current.validationError {
46+
if launchPlan.isElevationHost {
47+
fputs("OpenClaw elevation host profile is invalid: \(error.localizedDescription)\n", stderr)
48+
Darwin.exit(2)
49+
}
4550
let alert = NSAlert()
4651
alert.alertStyle = .critical
4752
alert.messageText = "OpenClaw profile is invalid"
@@ -59,7 +64,7 @@ struct OpenClawApp: App {
5964
}
6065
OpenClawLogging.bootstrapIfNeeded()
6166

62-
Self.applyAttachOnlyOverrideIfNeeded()
67+
Self.applyAttachOnlyOverrideIfNeeded(plan: launchPlan)
6368
_state = State(initialValue: AppStateStore.shared)
6469
}
6570

@@ -113,7 +118,7 @@ struct OpenClawApp: App {
113118
}
114119
.onChange(of: self.state.connectionMode) { _, mode in
115120
Task { await ConnectionModeCoordinator.shared.apply(mode: mode, paused: self.state.isPaused) }
116-
if AppLaunchPresentationPolicy.current.allowsAutomaticPresentation {
121+
if AppLaunchRuntimePlan.current.allowsAutomaticPresentation {
117122
CLIInstallPrompter.shared.checkAndPromptIfNeeded(reason: "connection-mode")
118123
}
119124
BrowserProfileImportModel.shared.handleConnectionModeChange()
@@ -179,14 +184,13 @@ struct OpenClawApp: App {
179184
: "OpenClaw"
180185
}
181186

182-
private static func applyAttachOnlyOverrideIfNeeded() {
183-
let args = CommandLine.arguments
184-
guard args.contains("--attach-only") || args.contains("--no-launchd") else { return }
187+
private static func applyAttachOnlyOverrideIfNeeded(plan: AppLaunchRuntimePlan) {
188+
guard plan.attachOnly else { return }
185189
if let error = GatewayLaunchAgentManager.applyAttachOnlyRuntimeOverride() {
186-
Self.logger.error("attach-only flag failed: \(error, privacy: .public)")
190+
self.logger.error("attach-only flag failed: \(error, privacy: .public)")
187191
return
188192
}
189-
Self.logger.info("attach-only flag enabled")
193+
self.logger.info("attach-only flag enabled")
190194
}
191195

192196
private var isGatewaySleeping: Bool {
@@ -454,6 +458,12 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
454458
: DisabledUpdaterController()
455459
super.init()
456460
if let instanceOwnershipFailure {
461+
if AppLaunchRuntimePlan.current.isElevationHost {
462+
fputs(
463+
"OpenClaw elevation host could not claim its instance lock: \(instanceOwnershipFailure)\n",
464+
stderr)
465+
Darwin.exit(2)
466+
}
457467
let alert = NSAlert()
458468
alert.alertStyle = .critical
459469
alert.messageText = "OpenClaw could not claim its instance lock"
@@ -527,6 +537,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
527537
}
528538

529539
func application(_: NSApplication, open urls: [URL]) {
540+
guard !AppLaunchRuntimePlan.current.isElevationHost else { return }
530541
Task { @MainActor in
531542
for url in urls {
532543
await DeepLinkHandler.shared.handle(url: url)
@@ -535,6 +546,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
535546
}
536547

537548
func applicationShouldHandleReopen(_: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
549+
guard AppLaunchRuntimePlan.current.allowsAutomaticPresentation else { return false }
538550
if flag {
539551
return true
540552
}
@@ -551,13 +563,13 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
551563
return
552564
}
553565
#endif
554-
let launchPolicy = AppLaunchPresentationPolicy.current
555-
if !AppProfile.current.isActive {
566+
let launchPlan = AppLaunchRuntimePlan.current
567+
if !AppProfile.current.isActive, !launchPlan.isElevationHost {
556568
switch ApplicationRelocator.handleLaunch() {
557569
case .terminating:
558570
return
559571
case let .continueLaunch(startUpdater):
560-
if startUpdater {
572+
if startUpdater, launchPlan.allowsUpdater {
561573
if OpenClawConfigFile.gatewayUpdateChannel() == nil {
562574
self.updaterController.startAfterResolvingGatewayUpdateChannel()
563575
} else {
@@ -575,10 +587,10 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
575587
MacNodeModeCoordinator.prepareNodeIdentityProfile(
576588
isExistingInstallation: state.onboardingSeen || state.connectionMode != .unconfigured)
577589
}
578-
AppActivationPolicy.apply(showDockIcon: state?.showDockIcon ?? false)
590+
AppActivationPolicy.apply(showDockIcon: launchPlan.allowsDockIcon && (state?.showDockIcon ?? false))
579591
if let state {
580592
let shouldWaitForConnection = state.connectionMode != .unconfigured
581-
if !shouldWaitForConnection, launchPolicy.allowsAutomaticPresentation {
593+
if !shouldWaitForConnection, launchPlan.allowsAutomaticPresentation {
582594
Task { @MainActor in
583595
await self.scheduleFirstRunOnboardingIfNeeded(gatewayConnected: false)
584596
}
@@ -592,34 +604,36 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
592604
await ConnectionModeCoordinator.shared.apply(
593605
mode: state.connectionMode,
594606
paused: state.isPaused)
595-
guard shouldWaitForConnection, launchPolicy.allowsAutomaticPresentation else { return }
607+
guard shouldWaitForConnection, launchPlan.allowsAutomaticPresentation else { return }
596608
await self.scheduleFirstRunOnboardingIfNeeded(
597609
gatewayConnected: ControlChannel.shared.state == .connected)
598610
}
599611
}
600612
TerminationSignalWatcher.shared.start()
601-
NodePairingApprovalPrompter.shared.start()
602-
DevicePairingApprovalPrompter.shared.start()
603-
ExecApprovalsPromptServer.shared.start()
604-
ExecApprovalsGatewayPrompter.shared.start()
605613
MacNodeModeCoordinator.shared.start()
606-
if let state {
607-
CookieSyncManager.shared.start(state: state)
614+
if launchPlan.allowsInteractiveServices {
615+
NodePairingApprovalPrompter.shared.start()
616+
DevicePairingApprovalPrompter.shared.start()
617+
ExecApprovalsPromptServer.shared.start()
618+
ExecApprovalsGatewayPrompter.shared.start()
619+
if let state {
620+
CookieSyncManager.shared.start(state: state)
621+
}
622+
VoiceWakeGlobalSettingsSync.shared.start()
623+
QuickChatController.shared.start()
608624
}
609-
VoiceWakeGlobalSettingsSync.shared.start()
610-
QuickChatController.shared.start()
611625
Task { PresenceReporter.shared.start() }
612626
Task { await HealthStore.shared.refresh(onDemand: true) }
613627
Task { await PortGuardian.shared.sweep(mode: AppStateStore.shared.connectionMode) }
614628
AppStateStore.shared.applyPeekabooBridgeHostState()
615-
if launchPolicy.allowsAutomaticPresentation {
629+
if launchPlan.allowsAutomaticPresentation {
616630
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
617631
if !PostUpdateController.shared.startIfNeeded() {
618632
CLIInstallPrompter.shared.checkAndPromptIfNeeded(reason: "launch")
619633
}
620634
}
621635
}
622-
if launchPolicy.allowsAutomaticPresentation {
636+
if launchPlan.allowsAutomaticPresentation {
623637
Task {
624638
try? await Task.sleep(for: .seconds(2))
625639
DashboardManager.shared.preloadIfConfigured()
@@ -628,7 +642,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
628642

629643
#if DEBUG
630644
// Screenshot/demo helper: show the pairing panel with sample requests.
631-
if launchPolicy.allowsAutomaticPresentation,
645+
if launchPlan.allowsAutomaticPresentation,
632646
ProcessInfo.processInfo.environment["OPENCLAW_DEBUG_PAIRING_DEMO"] == "1"
633647
{
634648
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
@@ -637,14 +651,14 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
637651
}
638652
#endif
639653
// Developer/testing helper: auto-open chat when launched with --chat (or legacy --webchat).
640-
if launchPolicy.shouldAutoOpenChat(arguments: CommandLine.arguments) {
654+
if launchPlan.shouldAutoOpenChat(arguments: CommandLine.arguments) {
641655
self.webChatAutoLogger.debug("Auto-opening chat via CLI flag")
642656
Task { @MainActor in
643657
let sessionKey = await WebChatManager.shared.preferredSessionKey()
644658
WebChatManager.shared.show(sessionKey: sessionKey)
645659
}
646660
}
647-
if launchPolicy.shouldAutoOpenDashboard(arguments: CommandLine.arguments) {
661+
if launchPlan.shouldAutoOpenDashboard(arguments: CommandLine.arguments) {
648662
self.webChatAutoLogger.info("Auto-opening dashboard via CLI flag")
649663
self.openDashboardAction()
650664
}

0 commit comments

Comments
 (0)