Skip to content

Commit f401b01

Browse files
committed
fix(macos): tolerate first-run state migrations during gateway start
1 parent 7cf4f9a commit f401b01

6 files changed

Lines changed: 29 additions & 17 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Docs: https://docs.openclaw.ai
6565

6666
### Fixes
6767

68+
- **macOS first-run Gateway startup:** keep onboarding and daemon lifecycle waits alive through bounded state integrity checks and startup migrations instead of reporting a premature background-service failure.
6869
- **Control UI appearance accessibility:** keep the unavailable custom-theme card announced as an Import command while preserving selected-state semantics for selectable themes and text sizes. Thanks @shakkernerd.
6970
- **Control UI dashboard index refresh:** keep an open Dashboards page current after session changes, agent-scope updates, and Gateway reconnects while preserving the last safe list until replacement hydration completes. Fixes #120602. Thanks @shakkernerd.
7071
- **Browser extension relay security:** require canonical 64-character relay secrets and safe WebSocket pairing URLs, and recheck OpenClaw tab-group consent at the extension edge before every authority-bearing existing-tab command.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,8 @@ enum CLIInstaller {
573573
paused: Bool = AppStateStore.shared.isPaused,
574574
start: @MainActor () -> Void = { GatewayProcessManager.shared.setActive(true) },
575575
waitUntilReady: @MainActor () async -> Bool = {
576-
await GatewayProcessManager.shared.waitForGatewayReady(timeout: 12)
576+
await GatewayProcessManager.shared.waitForGatewayReady(
577+
timeout: GatewayLaunchAgentManager.startupMigrationTolerance)
577578
}) async -> LocalGatewayActivation
578579
{
579580
guard mode == .local, !paused else { return .deferred }

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ enum GatewayLaunchAgentManager {
88

99
private static let logger = Logger(subsystem: "ai.openclaw", category: "gateway.launchd")
1010
private static let disableLaunchAgentMarker = ".openclaw/disable-launchagent"
11+
/// A first-run daemon command may wait behind state integrity checks and the shared startup-
12+
/// migration lease. Keep the app from killing healthy migration work before it can finish.
13+
static let startupMigrationTolerance: TimeInterval = 120
1114

1215
private static var disableLaunchAgentMarkerURL: URL {
1316
#if DEBUG
@@ -130,7 +133,7 @@ enum GatewayLaunchAgentManager {
130133
self.logger.info("launchd restart skipped (disable marker set)")
131134
return nil
132135
}
133-
return await self.runDaemonCommand(["restart"], timeout: 20)
136+
return await self.runDaemonCommand(["restart"])
134137
}
135138

136139
static func launchdConfigSnapshot() -> LaunchAgentPlistSnapshot? {
@@ -234,7 +237,7 @@ extension GatewayLaunchAgentManager {
234237

235238
private static func runDaemonCommand(
236239
_ args: [String],
237-
timeout: Double = 15,
240+
timeout: Double = Self.startupMigrationTolerance,
238241
quiet: Bool = false) async -> String?
239242
{
240243
let result = await self.runDaemonCommandResult(args, timeout: timeout, quiet: quiet)

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -698,14 +698,13 @@ extension GatewayProcessManager {
698698
context: LaunchAgentStartupContext,
699699
startGeneration: UInt64,
700700
readinessWindow: TimeInterval = 6,
701-
// A fresh install gets ten six-second probe windows for Local Network authorization.
702-
firstInstallReadinessGraceWindows: Int = 9) async
701+
// Fresh installs keep probing through the same first-run migration budget as the CLI.
702+
firstInstallReadinessBudget: TimeInterval = GatewayLaunchAgentManager.startupMigrationTolerance) async
703703
{
704704
let startedAt = Date()
705705
var deadline = startedAt.addingTimeInterval(readinessWindow)
706-
let graceWindowCount = max(0, firstInstallReadinessGraceWindows)
707706
let finalProbeDeadline = startedAt.addingTimeInterval(
708-
readinessWindow * (Double(graceWindowCount) + 1))
707+
max(readinessWindow, firstInstallReadinessBudget))
709708
var latestRetryDisposition: GatewayProbeFailureDisposition?
710709
var readinessPID = context.readinessPID
711710
var freshInstallGraceAuthorized = false
@@ -983,7 +982,8 @@ extension GatewayProcessManager {
983982
launchAgentInstalled: Bool = false) async -> Bool
984983
{
985984
let startGeneration = self.gatewayStartGeneration
986-
if let result = await self.observeCurrentGatewayStart(generation: startGeneration) { return result }
985+
if await self.observeCurrentGatewayStart(generation: startGeneration) == true { return true }
986+
guard !Task.isCancelled, self.isCurrentGatewayStart(startGeneration) else { return false }
987987
let readinessCandidate = self.launchAgentReadinessCandidate
988988
let readinessFailure = self.launchAgentReadinessFailure
989989
let readinessRevision = self.launchAgentReadinessRevision
@@ -1337,7 +1337,7 @@ extension GatewayProcessManager {
13371337
port: Int,
13381338
pid: Int32,
13391339
readinessWindow: TimeInterval,
1340-
firstInstallReadinessGraceWindows: Int)
1340+
firstInstallReadinessBudget: TimeInterval)
13411341
{
13421342
self.desiredActive = true
13431343
self.status = .starting
@@ -1354,7 +1354,7 @@ extension GatewayProcessManager {
13541354
readinessRevision: readinessRevision),
13551355
startGeneration: generation,
13561356
readinessWindow: readinessWindow,
1357-
firstInstallReadinessGraceWindows: firstInstallReadinessGraceWindows)
1357+
firstInstallReadinessBudget: firstInstallReadinessBudget)
13581358
}
13591359
}
13601360
}

‎apps/macos/Tests/OpenClawIPCTests/GatewayLaunchAgentManagerTests.swift‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import Testing
44

55
@Suite(.serialized)
66
struct GatewayLaunchAgentManagerTests {
7+
@Test func `daemon commands tolerate first run state migrations`() {
8+
#expect(GatewayLaunchAgentManager.startupMigrationTolerance >= 120)
9+
}
10+
711
@Test func `reads Gateway service ownership command directly from launchd`() throws {
812
let url = FileManager.default.temporaryDirectory
913
.appendingPathComponent("openclaw-gateway-\(UUID().uuidString).plist")

‎apps/macos/Tests/OpenClawIPCTests/GatewayProcessManagerTests.swift‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -980,11 +980,13 @@ struct GatewayProcessManagerTests {
980980
}
981981
}
982982

983-
@Test func `readiness waiter observes the current owner past its timeout`() async throws {
983+
@Test func `readiness waiter rechecks after current owner fails past its timeout`() async throws {
984984
let port = 19114
985985
let url = try #require(URL(string: "ws://example.invalid"))
986986
let (session, connection, manager) = self.makeGatewayReadinessFixture(url: url) {
987-
self.gatewayTask(healthSucceedsAfter: 1)
987+
self.gatewayTask(
988+
healthSucceedsAfter: 0,
989+
stallsFirstHealthResponse: true)
988990
}
989991
defer { manager.setTestingConnection(nil) }
990992

@@ -1007,7 +1009,7 @@ struct GatewayProcessManagerTests {
10071009
port: port,
10081010
pid: 4242,
10091011
readinessWindow: 0.5,
1010-
firstInstallReadinessGraceWindows: 1)
1012+
firstInstallReadinessBudget: 0.5)
10111013
let readiness = Task { @MainActor in
10121014
await manager.waitForGatewayReady(timeout: 0.01)
10131015
}
@@ -1019,6 +1021,7 @@ struct GatewayProcessManagerTests {
10191021
#expect(!manager._testHasLaunchAgentReadinessFailure())
10201022
#expect(await readiness.value)
10211023
#expect(manager.status == .running(details: "pid 4242"))
1024+
#expect((session.latestTask()?.snapshotSendCount() ?? 0) > 1)
10221025

10231026
await connection.shutdown()
10241027
await PortGuardian.shared.setTestingDescriptor(nil, forPort: port)
@@ -1046,7 +1049,7 @@ struct GatewayProcessManagerTests {
10461049
port: port,
10471050
pid: 4242,
10481051
readinessWindow: 0.5,
1049-
firstInstallReadinessGraceWindows: 1)
1052+
firstInstallReadinessBudget: 1)
10501053
let readiness = Task { @MainActor in
10511054
await manager.waitForGatewayReady(timeout: 0.01)
10521055
}
@@ -1095,7 +1098,7 @@ struct GatewayProcessManagerTests {
10951098
port: port,
10961099
pid: 4242,
10971100
readinessWindow: 0.05,
1098-
firstInstallReadinessGraceWindows: 2)
1101+
firstInstallReadinessBudget: 0.15)
10991102
await manager.waitForStartupAttempt()
11001103

11011104
#expect(GatewayLaunchAgentManager.testingDaemonCommandCallsSnapshot()
@@ -1138,7 +1141,7 @@ struct GatewayProcessManagerTests {
11381141
port: port,
11391142
pid: 4242,
11401143
readinessWindow: 0.01,
1141-
firstInstallReadinessGraceWindows: 1)
1144+
firstInstallReadinessBudget: 0.02)
11421145
await manager.waitForStartupAttempt()
11431146

11441147
#expect(manager.status == .failed("Gateway did not start in time"))
@@ -1180,7 +1183,7 @@ struct GatewayProcessManagerTests {
11801183
port: port,
11811184
pid: 4242,
11821185
readinessWindow: 0.05,
1183-
firstInstallReadinessGraceWindows: 1)
1186+
firstInstallReadinessBudget: 0.1)
11841187
await manager.waitForStartupAttempt()
11851188
guard case .failed("Gateway did not start in time") = manager.status else {
11861189
Issue.record("fresh launchd readiness did not fail within its bounded grace")

0 commit comments

Comments
 (0)