@@ -31,8 +31,10 @@ final class AppState {
3131 @ ObservationIgnored private let execApprovalsDefaultsAsyncResolver :
3232 @MainActor ( ) async -> Result < ExecApprovalsResolvedDefaults , ExecApprovalsReadError >
3333 @ ObservationIgnored private let execApprovalsReadRetryDelay : Duration
34+ @ ObservationIgnored let bundleLocationAllowsPersistentIntegration : Bool
3435 @ ObservationIgnored private var execApprovalsReadRetryTask : Task < Void , Never > ?
3536 @ ObservationIgnored private var execApprovalsReadGeneration = 0
37+ @ ObservationIgnored private var isHydratingLaunchAtLogin = false
3638 private var isInitializing = true
3739 private var isApplyingRemoteTokenConfig = false
3840 private enum GatewayConfigSyncState : Equatable {
@@ -101,11 +103,25 @@ final class AppState {
101103
102104 var launchAtLogin : Bool {
103105 didSet {
104- guard ! self . isInitializing else { return }
106+ guard Self . shouldPersistLaunchAtLoginChange (
107+ isInitializing : self . isInitializing ,
108+ isHydrating : self . isHydratingLaunchAtLogin ,
109+ isEnabling : self . launchAtLogin ,
110+ bundleLocationAllowsPersistentIntegration : self . bundleLocationAllowsPersistentIntegration )
111+ else { return }
105112 self . ifNotPreview { Task { AppStateStore . updateLaunchAtLogin ( enabled : self . launchAtLogin ) } }
106113 }
107114 }
108115
116+ static func shouldPersistLaunchAtLoginChange (
117+ isInitializing : Bool ,
118+ isHydrating : Bool ,
119+ isEnabling : Bool ,
120+ bundleLocationAllowsPersistentIntegration : Bool ) -> Bool
121+ {
122+ ! isInitializing && ! isHydrating && ( ! isEnabling || bundleLocationAllowsPersistentIntegration )
123+ }
124+
109125 var onboardingSeen : Bool {
110126 didSet { self . ifNotPreview { UserDefaults . standard . set ( self . onboardingSeen , forKey : onboardingSeenKey ) }
111127 }
@@ -343,6 +359,8 @@ final class AppState {
343359 {
344360 let isPreview = preview || ProcessInfo . processInfo . isRunningTests
345361 self . isPreview = isPreview
362+ self . bundleLocationAllowsPersistentIntegration =
363+ isPreview || ApplicationRelocator . currentBundleAllowsPersistentIntegration ( )
346364 self . execApprovalsDefaultsAsyncResolver = execApprovalsDefaultsAsyncResolver
347365 self . execApprovalsReadRetryDelay = execApprovalsReadRetryDelay
348366 if ! isPreview {
@@ -463,7 +481,7 @@ final class AppState {
463481 if ! self . isPreview {
464482 Task . detached ( priority : . utility ) { [ weak self ] in
465483 let current = await LaunchAgentManager . status ( )
466- await MainActor . run { [ weak self ] in self ? . launchAtLogin = current }
484+ await MainActor . run { [ weak self ] in self ? . hydrateLaunchAtLogin ( current ) }
467485 }
468486 }
469487
@@ -491,6 +509,13 @@ final class AppState {
491509 }
492510 }
493511
512+ private func hydrateLaunchAtLogin ( _ enabled : Bool ) {
513+ // Reading launchd state must not rewrite a valid plist with this process's transient bundle path.
514+ self . isHydratingLaunchAtLogin = true
515+ self . launchAtLogin = enabled
516+ self . isHydratingLaunchAtLogin = false
517+ }
518+
494519 @ MainActor
495520 deinit {
496521 self . execApprovalsReadRetryTask ? . cancel ( )
0 commit comments