Skip to content

Commit 3740070

Browse files
authored
fix(mac): adopt CLI identity during state migration (#121313)
* fix(identity): one canonical device-identity contract across app and CLI * fix(ci): satisfy native identity checks * chore: drop changelog edit (release-generation owns CHANGELOG.md)
1 parent 1367e9a commit 3740070

11 files changed

Lines changed: 284 additions & 57 deletions

‎apps/.i18n/native-source.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35523,7 +35523,7 @@
3552335523
},
3552435524
{
3552535525
"kind": "ui-call",
35526-
"line": 380,
35526+
"line": 383,
3552735527
"path": "apps/macos/Sources/OpenClaw/OnboardingView+Layout.swift",
3552835528
"source": "Back",
3552935529
"surface": "apple",

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

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -767,25 +767,20 @@ extension GatewayProcessManager {
767767
var latestRetryDisposition: GatewayProbeFailureDisposition?
768768
var readinessPID = context.readinessPID
769769
var freshInstallGraceAuthorized = false
770+
var responsiveStartupProgressObserved = false
770771
readinessLoop: while true {
771772
guard !Task.isCancelled, self.isCurrentGatewayStart(startGeneration) else { return }
772773
while Date() >= deadline {
773774
guard deadline < finalProbeDeadline else { break readinessLoop }
774-
if freshInstallGraceAuthorized {
775-
// Repeating launchd status at every boundary would expand the wall-clock budget.
776-
// Generation/revision guard intermediate windows; success and final repair re-check ownership.
777-
guard self.isCurrentFreshInstallReadiness(
778-
context: context,
779-
startGeneration: startGeneration)
780-
else { return }
781-
} else {
782-
guard let reusablePID = await self.currentInstallReusableLaunchdPID(
783-
context: context,
784-
startGeneration: startGeneration)
785-
else { break readinessLoop }
786-
readinessPID = reusablePID
787-
freshInstallGraceAuthorized = true
788-
}
775+
let extensionAuthorization = await self.authorizeReadinessExtension(
776+
context: context,
777+
startGeneration: startGeneration,
778+
responsiveStartupProgressObserved: responsiveStartupProgressObserved,
779+
freshInstallGraceAuthorized: freshInstallGraceAuthorized,
780+
readinessPID: readinessPID)
781+
guard extensionAuthorization.allowed else { break readinessLoop }
782+
readinessPID = extensionAuthorization.readinessPID
783+
freshInstallGraceAuthorized = true
789784
deadline = min(
790785
deadline.addingTimeInterval(readinessWindow),
791786
finalProbeDeadline)
@@ -821,6 +816,9 @@ extension GatewayProcessManager {
821816
case .retryWithoutRepair:
822817
// A responsive transient invalidates older connection-failure evidence.
823818
latestRetryDisposition = .retryWithoutRepair
819+
if self.probeFailureShowsStartupProgress(error) {
820+
responsiveStartupProgressObserved = true
821+
}
824822
}
825823
let retryDelay = min(0.4, max(0, deadline.timeIntervalSinceNow))
826824
if retryDelay > 0 {
@@ -843,6 +841,27 @@ extension GatewayProcessManager {
843841
}
844842
}
845843

844+
private func authorizeReadinessExtension(
845+
context: LaunchAgentStartupContext,
846+
startGeneration: UInt64,
847+
responsiveStartupProgressObserved: Bool,
848+
freshInstallGraceAuthorized: Bool,
849+
readinessPID: Int32?) async -> (allowed: Bool, readinessPID: Int32?)
850+
{
851+
if responsiveStartupProgressObserved || freshInstallGraceAuthorized {
852+
// One live response or verified launchd owner authorizes the bounded migration window;
853+
// repeating launchd status at every boundary would expand the wall-clock budget.
854+
let isCurrent = !Task.isCancelled &&
855+
self.isCurrentGatewayStart(startGeneration) &&
856+
self.launchAgentReadinessRevision == context.readinessRevision
857+
return (isCurrent, readinessPID)
858+
}
859+
let reusablePID = await self.currentInstallReusableLaunchdPID(
860+
context: context,
861+
startGeneration: startGeneration)
862+
return (reusablePID != nil, reusablePID)
863+
}
864+
846865
private func currentInstallReusableLaunchdPID(
847866
context: LaunchAgentStartupContext,
848867
startGeneration: UInt64) async -> Int32?
@@ -998,6 +1017,11 @@ extension GatewayProcessManager {
9981017
}
9991018
}
10001019

1020+
private func probeFailureShowsStartupProgress(_ error: Error) -> Bool {
1021+
guard let response = error as? GatewayResponseError else { return false }
1022+
return response.code.uppercased() == "UNAVAILABLE"
1023+
}
1024+
10011025
private func probeFailureIsCancellation(_ error: Error) -> Bool {
10021026
if error is CancellationError { return true }
10031027
let nsError = error as NSError

‎apps/macos/Sources/OpenClaw/OnboardingView+Layout.swift‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ extension OnboardingView {
6161
guard installed else { return }
6262
self.updateMonitoring(for: self.activePageIndex)
6363
}
64+
.onChange(of: GatewayProcessManager.shared.status) { _, status in
65+
self.reviseCLIActivationFailureIfGatewayReady(status)
66+
}
6467
.onDisappear {
6568
self.onboardingDidDisappear()
6669
}

‎apps/macos/Sources/OpenClaw/OnboardingView+Monitoring.swift‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,31 @@ extension OnboardingView {
9999
isLocal && executableReady && !installing
100100
}
101101

102+
static func shouldReviseCLIActivationFailure(
103+
gatewayStatus: GatewayProcessManager.Status,
104+
isLocal: Bool,
105+
executableReady: Bool,
106+
installed: Bool) -> Bool
107+
{
108+
guard isLocal, executableReady, !installed else { return false }
109+
return switch gatewayStatus {
110+
case .running, .attachedExisting: true
111+
case .stopped, .starting, .failed: false
112+
}
113+
}
114+
115+
func reviseCLIActivationFailureIfGatewayReady(_ status: GatewayProcessManager.Status) {
116+
guard Self.shouldReviseCLIActivationFailure(
117+
gatewayStatus: status,
118+
isLocal: state.connectionMode == .local,
119+
executableReady: cliExecutableReady,
120+
installed: cliInstalled)
121+
else { return }
122+
cliInstalled = true
123+
cliStatusKnown = true
124+
cliStatus = nil
125+
}
126+
102127
func finishExistingCLIActivation() async {
103128
defer {
104129
installingCLI = false

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,43 @@ struct GatewayProcessManagerTests {
11531153
}
11541154
}
11551155

1156+
@Test func `responsive startup progress extends readiness without launchd status proof`() async throws {
1157+
let port = 19119
1158+
let url = try #require(URL(string: "ws://example.invalid"))
1159+
let (_, connection, manager) = self.makeGatewayReadinessFixture(url: url) {
1160+
self.gatewayTask(healthSucceedsAfter: 1)
1161+
}
1162+
defer { manager.setTestingConnection(nil) }
1163+
1164+
try await self.withLaunchAgentEnvironment(
1165+
port: port,
1166+
statusPayload: #"{"ok":true,"service":{"loaded":false}}"#)
1167+
{
1168+
manager.setTestingSkipControlChannelRefresh(true)
1169+
manager._testClearLaunchAgentReadinessFailure()
1170+
let descriptor = self.gatewayDescriptor(pid: 4242)
1171+
await PortGuardian.shared.setTestingDescriptor(descriptor, forPort: port)
1172+
defer {
1173+
manager.setTestingSkipControlChannelRefresh(false)
1174+
manager._testClearLaunchAgentReadinessFailure()
1175+
}
1176+
1177+
manager._testStartLaunchdGatewayReadiness(
1178+
port: port,
1179+
pid: 4242,
1180+
readinessWindow: 0.05,
1181+
firstInstallReadinessBudget: 0.5)
1182+
await manager.waitForStartupAttempt()
1183+
1184+
#expect(manager.status == .running(details: "pid 4242"))
1185+
#expect(manager.lastFailureReason == nil)
1186+
#expect(!manager._testHasLaunchAgentReadinessFailure())
1187+
1188+
await connection.shutdown()
1189+
await PortGuardian.shared.setTestingDescriptor(nil, forPort: port)
1190+
}
1191+
}
1192+
11561193
@Test func `delayed fresh install authorization cannot restart readiness budget`() async throws {
11571194
let port = 19118
11581195
let url = try #require(URL(string: "ws://example.invalid"))

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,29 @@ struct OnboardingViewSmokeTests {
234234
installing: true))
235235
}
236236

237+
@Test func `later gateway readiness revises a pinned CLI activation failure`() {
238+
#expect(OnboardingView.shouldReviseCLIActivationFailure(
239+
gatewayStatus: .running(details: "pid 4242"),
240+
isLocal: true,
241+
executableReady: true,
242+
installed: false))
243+
#expect(OnboardingView.shouldReviseCLIActivationFailure(
244+
gatewayStatus: .attachedExisting(details: "pid 4242"),
245+
isLocal: true,
246+
executableReady: true,
247+
installed: false))
248+
#expect(!OnboardingView.shouldReviseCLIActivationFailure(
249+
gatewayStatus: .failed("still unavailable"),
250+
isLocal: true,
251+
executableReady: true,
252+
installed: false))
253+
#expect(!OnboardingView.shouldReviseCLIActivationFailure(
254+
gatewayStatus: .running(details: nil),
255+
isLocal: false,
256+
executableReady: true,
257+
installed: false))
258+
}
259+
237260
@Test func `connection mode change restarts full page monitoring`() {
238261
let state = AppState(preview: true)
239262
let view = OnboardingView(state: state)

‎apps/shared/OpenClawKit/Sources/OpenClawKit/DeviceAuthStore.swift‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public enum DeviceAuthStore {
139139
.appendingPathComponent("openclaw.sqlite", isDirectory: false)
140140
guard let identity = try? DeviceIdentitySQLiteStore.loadExisting(
141141
databaseURL: databaseURL,
142+
destinationStateDirURL: stateDirectoryURL,
142143
profile: profile)
143144
else { return }
144145
try? self.withStore(profile: profile) { database in

‎apps/shared/OpenClawKit/Sources/OpenClawKit/DeviceIdentity.swift‎

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,22 +259,41 @@ public enum DeviceIdentityStore {
259259
#endif
260260

261261
public static func loadOrCreate(profile: GatewayDeviceIdentityProfile) -> DeviceIdentity {
262-
guard let identity = loadOrCreatePersisted(profile: profile) else {
263-
preconditionFailure("Could not persist the OpenClaw device identity")
262+
do {
263+
return try self.loadOrCreatePersistedOrThrow(profile: profile)
264+
} catch {
265+
preconditionFailure("Could not persist the OpenClaw device identity: \(error.localizedDescription)")
264266
}
265-
return identity
266267
}
267268

268269
/// Loads or creates an identity, returning nil unless its key material was durably persisted.
269270
public static func loadOrCreatePersisted(
270271
profile: GatewayDeviceIdentityProfile = .primary) -> DeviceIdentity?
272+
{
273+
try? self.loadOrCreatePersistedOrThrow(profile: profile)
274+
}
275+
276+
/// Loads or creates an identity and preserves the storage failure for callers that can report it.
277+
static func loadOrCreatePersistedOrThrow(
278+
profile: GatewayDeviceIdentityProfile = .primary) throws -> DeviceIdentity
271279
{
272280
let stateDirURL = DeviceIdentityPaths.stateDirURL()
273-
return try? DeviceIdentitySQLiteStore.loadOrCreate(
274-
databaseURL: self.databaseURL(stateDirURL: stateDirURL),
275-
destinationStateDirURL: stateDirURL,
276-
profile: profile,
277-
legacySources: DeviceIdentityPaths.legacyIdentitySources(profile: profile))
281+
do {
282+
return try DeviceIdentitySQLiteStore.loadOrCreate(
283+
databaseURL: self.databaseURL(stateDirURL: stateDirURL),
284+
destinationStateDirURL: stateDirURL,
285+
profile: profile,
286+
legacySources: DeviceIdentityPaths.legacyIdentitySources(profile: profile))
287+
} catch {
288+
throw NSError(
289+
domain: "ai.openclaw.device-identity-store",
290+
code: 2,
291+
userInfo: [
292+
NSLocalizedDescriptionKey:
293+
"Could not access the persisted device identity: \(error.localizedDescription)",
294+
NSUnderlyingErrorKey: error,
295+
])
296+
}
278297
}
279298

280299
public static func signPayload(_ payload: String, identity: DeviceIdentity) -> String? {

0 commit comments

Comments
 (0)