Skip to content

Commit 2472782

Browse files
authored
fix(macos): gate remote onboarding on gateway auth (#117904)
* fix(macos): gate remote onboarding on gateway auth * fix(macos): address onboarding CI failures * test(macos): stabilize gateway auth routing coverage * fix(macos): bound remote gateway onboarding probe * fix(macos): satisfy remote probe CI guards * fix(macos): scope remote probe cleanup ownership * refactor(macos): remove unused health snapshot wrapper
1 parent 85e4094 commit 2472782

19 files changed

Lines changed: 866 additions & 235 deletions

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

Lines changed: 136 additions & 128 deletions
Large diffs are not rendered by default.

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

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -605,32 +605,32 @@ extension GatewayConnection {
605605
}
606606

607607
func captureRoute() async -> Route? {
608+
try? await self.captureRequiredRoute()
609+
}
610+
611+
func captureRequiredRoute() async throws -> Route {
608612
let shutdownGeneration = shutdownGeneration
609-
do {
610-
let endpoint = try await currentEndpoint()
611-
let cfg = endpoint.config
612-
_ = try await self.configure(
613-
url: cfg.url,
614-
token: cfg.token,
615-
password: cfg.password,
616-
tls: endpoint.tls,
617-
deviceAuthGatewayID: endpoint.deviceAuthGatewayID,
618-
routeAuthority: endpoint.routeAuthority,
619-
shutdownGeneration: shutdownGeneration)
620-
return Route(
621-
generation: self.routeGeneration,
622-
authority: endpoint.routeAuthority,
623-
url: cfg.url,
624-
token: cfg.token,
625-
password: cfg.password,
626-
tls: endpoint.tls,
627-
deviceAuthGatewayID: endpoint.deviceAuthGatewayID,
628-
activationOwnershipFingerprint: Self.activationOwnershipFingerprint(
629-
config: cfg,
630-
key: self.configuredActivationBindingKey))
631-
} catch {
632-
return nil
633-
}
613+
let endpoint = try await currentEndpoint()
614+
let cfg = endpoint.config
615+
_ = try await self.configure(
616+
url: cfg.url,
617+
token: cfg.token,
618+
password: cfg.password,
619+
tls: endpoint.tls,
620+
deviceAuthGatewayID: endpoint.deviceAuthGatewayID,
621+
routeAuthority: endpoint.routeAuthority,
622+
shutdownGeneration: shutdownGeneration)
623+
return Route(
624+
generation: self.routeGeneration,
625+
authority: endpoint.routeAuthority,
626+
url: cfg.url,
627+
token: cfg.token,
628+
password: cfg.password,
629+
tls: endpoint.tls,
630+
deviceAuthGatewayID: endpoint.deviceAuthGatewayID,
631+
activationOwnershipFingerprint: Self.activationOwnershipFingerprint(
632+
config: cfg,
633+
key: self.configuredActivationBindingKey))
634634
}
635635

636636
/// Connect and bind subsequent work to the hello snapshot's physical
@@ -1418,14 +1418,6 @@ extension GatewayConnection {
14181418

14191419
// MARK: - Health
14201420

1421-
func healthSnapshot(timeoutMs: Double? = nil) async throws -> HealthSnapshot {
1422-
let data = try await requestRaw(method: .health, timeoutMs: timeoutMs)
1423-
if let snap = decodeHealthSnapshot(from: data) {
1424-
return snap
1425-
}
1426-
throw GatewayDecodingError(method: Method.health.rawValue, message: "failed to decode health snapshot")
1427-
}
1428-
14291421
func healthOK(timeoutMs: Int = 8000) async throws -> Bool {
14301422
let data = try await requestRaw(method: .health, timeoutMs: Double(timeoutMs))
14311423
return (try? self.decoder.decode(OpenClawGatewayHealthOK.self, from: data))?.ok ?? true

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

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,22 @@ enum UIStrings {
88
static let welcomeTitle = "Welcome to OpenClaw"
99
}
1010

11+
struct RemoteGatewayProbeInput: Equatable {
12+
let transport: AppState.RemoteTransport
13+
let target: String
14+
let token: String
15+
}
16+
1117
enum RemoteOnboardingProbeState: Equatable {
1218
case idle
13-
case checking
14-
case ok(RemoteGatewayProbeSuccess)
15-
case failed(String)
19+
case checking(RemoteGatewayProbeInput)
20+
case ok(RemoteGatewayProbeInput, RemoteGatewayProbeSuccess)
21+
case failed(RemoteGatewayProbeInput, String)
22+
}
23+
24+
struct RemoteGatewayAdvanceDecision: Equatable {
25+
let canAdvance: Bool
26+
let shouldProbe: Bool
1627
}
1728

1829
enum OnboardingSystemAgentResumeStore {
@@ -619,6 +630,8 @@ struct OnboardingView: View {
619630
@State var showRemoteChoices = false
620631
@State var preferredGatewayID: String?
621632
@State var remoteProbeState: RemoteOnboardingProbeState = .idle
633+
@State var remoteProbeAttemptID: UUID?
634+
@State var remoteProbeTemporaryRestoreMode: AppState.ConnectionMode?
622635
@State var remoteAuthIssue: RemoteGatewayAuthIssue?
623636
@State var suppressRemoteProbeReset = false
624637
@State var gatewayDiscovery: GatewayDiscoveryModel
@@ -755,6 +768,30 @@ struct OnboardingView: View {
755768
!self.isCLIBlocking && !self.isAISetupBlocking
756769
}
757770

771+
static func remoteGatewayAdvanceDecision(
772+
connectionMode: AppState.ConnectionMode,
773+
activePageIndex: Int,
774+
connectionPageIndex: Int,
775+
authIssue: RemoteGatewayAuthIssue?,
776+
probeState: RemoteOnboardingProbeState,
777+
input: RemoteGatewayProbeInput) -> RemoteGatewayAdvanceDecision
778+
{
779+
guard connectionMode == .remote, activePageIndex == connectionPageIndex else {
780+
return RemoteGatewayAdvanceDecision(canAdvance: true, shouldProbe: false)
781+
}
782+
guard authIssue == nil else {
783+
return RemoteGatewayAdvanceDecision(canAdvance: false, shouldProbe: true)
784+
}
785+
switch probeState {
786+
case let .ok(verifiedInput, _) where verifiedInput == input:
787+
return RemoteGatewayAdvanceDecision(canAdvance: true, shouldProbe: false)
788+
case let .checking(checkingInput) where checkingInput == input:
789+
return RemoteGatewayAdvanceDecision(canAdvance: false, shouldProbe: false)
790+
case .idle, .checking, .ok, .failed:
791+
return RemoteGatewayAdvanceDecision(canAdvance: false, shouldProbe: true)
792+
}
793+
}
794+
758795
struct LocalGatewayProbe: Equatable {
759796
let port: Int
760797
let pid: Int32
@@ -790,7 +827,8 @@ struct OnboardingView: View {
790827
_aiSetup = State(initialValue: OnboardingAISetupModel(
791828
gateway: aiSetupGateway,
792829
defaults: systemAgentDefaults,
793-
routeIdentityProvider: routeIdentityProvider))
830+
routeIdentityProvider: routeIdentityProvider,
831+
connectionModeProvider: { state.connectionMode }))
794832
_configuredGatewayProbe = State(
795833
initialValue: OnboardingConfiguredGatewayProbe(
796834
gateway: aiSetupGateway,

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

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ final class OnboardingAISetupModel {
7171
private(set) var detectError: Failure?
7272
private(set) var pendingActivationVerification = false
7373
private(set) var waitingForPendingActivationDeadline = false
74-
private(set) var configuredGatewayProbeUnavailable = false
74+
private(set) var configuredGatewayBlocker: ConfiguredGatewayBlocker?
7575
/// Set once every detected candidate failed; opens the manual key form.
7676
private(set) var exhaustedAutoCandidates = false
7777

@@ -108,7 +108,7 @@ final class OnboardingAISetupModel {
108108
/// Once setup starts changing inference, its successful result belongs to
109109
/// OpenClaw rather than the existing-Gateway onboarding bypass.
110110
var ownsInferenceTransition: Bool {
111-
(self.phase == .detecting && !self.configuredGatewayProbeUnavailable) ||
111+
(self.phase == .detecting && self.configuredGatewayBlocker == nil) ||
112112
self.phase == .testing || self.manualTesting || self.authBusy || self.connected ||
113113
self.pendingActivationVerification
114114
}
@@ -122,6 +122,7 @@ final class OnboardingAISetupModel {
122122
private let gateway: GatewayConnection
123123
private let defaults: UserDefaults
124124
private let routeIdentityProvider: @MainActor () -> String?
125+
private let connectionModeProvider: @MainActor () -> AppState.ConnectionMode
125126
private var started = false
126127
private var attemptToken = UUID()
127128
/// One-shot: the next detection pass lists choices without auto-activating,
@@ -166,11 +167,15 @@ final class OnboardingAISetupModel {
166167
defaults: UserDefaults = .standard,
167168
routeIdentityProvider: @escaping @MainActor () -> String? = {
168169
OnboardingSystemAgentResumeStore.selectedRouteIdentity()
170+
},
171+
connectionModeProvider: @escaping @MainActor () -> AppState.ConnectionMode = {
172+
AppStateStore.shared.connectionMode
169173
})
170174
{
171175
self.gateway = gateway
172176
self.defaults = defaults
173177
self.routeIdentityProvider = routeIdentityProvider
178+
self.connectionModeProvider = connectionModeProvider
174179
}
175180

176181
private struct DetectResult: Decodable {
@@ -216,7 +221,7 @@ final class OnboardingAISetupModel {
216221
self.resetForGatewayChange(clearPendingHandoff: false)
217222
}
218223
guard !self.started else { return }
219-
self.configuredGatewayProbeUnavailable = false
224+
self.configuredGatewayBlocker = nil
220225
self.started = true
221226
self.phase = .detecting
222227
scheduleDetection()
@@ -225,7 +230,7 @@ final class OnboardingAISetupModel {
225230
func retryFromScratch() {
226231
// The configured-Gateway preflight has its own read-only retry. Never
227232
// turn an unavailable agents.list response into setup mutation.
228-
guard !self.configuredGatewayProbeUnavailable else { return }
233+
guard self.configuredGatewayBlocker == nil else { return }
229234
guard !self.waitingForPendingActivationDeadline else { return }
230235
if self.pendingActivationVerification {
231236
Task { await self.verifyPendingConfiguredInference() }
@@ -256,27 +261,6 @@ final class OnboardingAISetupModel {
256261
return true
257262
}
258263

259-
func showConfiguredGatewayProbeUnavailable() {
260-
guard !self.ownsInferenceTransition ||
261-
self.configuredGatewayProbeUnavailable ||
262-
self.waitingForPendingActivationDeadline
263-
else { return }
264-
// Retire stale candidates and `started` state. A later successful
265-
// missing-model probe must be able to run a fresh detect/activate flow.
266-
self.resetForGatewayChange(clearPendingHandoff: false)
267-
self.configuredGatewayProbeUnavailable = true
268-
self.phase = .ready
269-
self.detectError = Failure(
270-
summary: "The Gateway did not answer the inference check. Nothing was changed.",
271-
detail: nil)
272-
}
273-
274-
func beginConfiguredGatewayProbeRetry() {
275-
guard self.configuredGatewayProbeUnavailable else { return }
276-
self.phase = .detecting
277-
self.detectError = nil
278-
}
279-
280264
func waitForPendingActivationDeadline() {
281265
guard !self.connected,
282266
self.phase != .testing,
@@ -293,6 +277,16 @@ final class OnboardingAISetupModel {
293277
routeIdentity: routeIdentity)
294278
}
295279

280+
func updateConfiguredGatewayBlockerState(
281+
_ blocker: ConfiguredGatewayBlocker?,
282+
phase: Phase,
283+
detectError: Failure?)
284+
{
285+
self.configuredGatewayBlocker = blocker
286+
self.phase = phase
287+
self.detectError = detectError
288+
}
289+
296290
/// Restore only the pending handoff state. A configured model label is not
297291
/// proof that the ambiguous activation completed or that inference works.
298292
func resumeConfiguredInference(modelRef: String) {
@@ -675,7 +669,7 @@ final class OnboardingAISetupModel {
675669
self.detectError = nil
676670
self.pendingActivationVerification = false
677671
self.waitingForPendingActivationDeadline = false
678-
self.configuredGatewayProbeUnavailable = false
672+
self.configuredGatewayBlocker = nil
679673
self.exhaustedAutoCandidates = false
680674
self.serverLease = nil
681675
self.manualProviderID = ""
@@ -836,6 +830,10 @@ extension OnboardingAISetupModel {
836830
} catch {
837831
guard self.isCurrentAttempt(context) else { return }
838832
self.suppressNextAutoActivation = false
833+
if self.connectionModeProvider() == .remote, let authIssue = RemoteGatewayAuthIssue(error: error) {
834+
self.enterGatewayAuthBlocker(authIssue)
835+
return
836+
}
839837
self.phase = .ready
840838
self.detectError = Self.transportFailure(error.localizedDescription)
841839
self.showManualEntry = self.candidates.isEmpty

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,29 @@ private struct OnboardingRecommendedInstallCard: View {
148148
}
149149
}
150150

151+
struct GatewayAuthCard: Equatable {
152+
let title: String
153+
let message: String
154+
let primaryTitle: String
155+
let secondaryTitle: String
156+
}
157+
151158
struct OnboardingAISetupView: View {
152159
@Bindable var model: OnboardingAISetupModel
153160
var systemAgentChat: SystemAgentOnboardingChatModel
154161
@Binding var showSystemAgentChat: Bool
162+
var returnToGatewayAuthentication: () -> Void
155163
var retryConfiguredGatewayProbe: () -> Void
156164
@State private var openedProviderAuthURL: URL?
157165

166+
static func gatewayAuthCard(for issue: RemoteGatewayAuthIssue) -> GatewayAuthCard {
167+
GatewayAuthCard(
168+
title: "Gateway authentication required",
169+
message: issue.statusMessage,
170+
primaryTitle: "Back to Gateway",
171+
secondaryTitle: "Try again")
172+
}
173+
158174
var body: some View {
159175
VStack(alignment: .leading, spacing: 12) {
160176
switch self.model.phase {
@@ -212,7 +228,10 @@ struct OnboardingAISetupView: View {
212228
self.candidateRow(candidate)
213229
}
214230
}
215-
} else if self.model.phase != .connected, self.model.detectError == nil {
231+
} else if self.model.phase != .connected,
232+
self.model.detectError == nil,
233+
self.model.configuredGatewayAuthIssue == nil
234+
{
216235
// A failed detect must not claim "nothing found" — the error card
217236
// below owns that state and the claim would be unproven.
218237
self.noCandidatesIntro
@@ -222,7 +241,17 @@ struct OnboardingAISetupView: View {
222241
self.unavailableCandidatesSection
223242
}
224243

225-
if let detectError = model.detectError {
244+
if let authIssue = model.configuredGatewayAuthIssue {
245+
let card = Self.gatewayAuthCard(for: authIssue)
246+
OnboardingErrorCard(
247+
title: card.title,
248+
message: card.message,
249+
docsSlug: "start/onboarding",
250+
retryTitle: card.primaryTitle,
251+
secondaryTitle: card.secondaryTitle,
252+
secondary: self.retryConfiguredGatewayProbe,
253+
retry: self.returnToGatewayAuthentication)
254+
} else if let detectError = model.detectError {
226255
OnboardingErrorCard(
227256
title: self.model.configuredGatewayProbeUnavailable
228257
? "Couldn’t check this Gateway for AI accounts"
@@ -928,13 +957,17 @@ struct OnboardingErrorCard: View {
928957
let docsSlug: String
929958
var retryTitle: String?
930959
var retry: (() -> Void)?
960+
var secondaryTitle: String?
961+
var secondary: (() -> Void)?
931962

932963
init(
933964
title: String,
934965
message: String,
935966
details: String? = nil,
936967
docsSlug: String,
937968
retryTitle: String? = nil,
969+
secondaryTitle: String? = nil,
970+
secondary: (() -> Void)? = nil,
938971
retry: (() -> Void)? = nil)
939972
{
940973
self.title = title
@@ -943,6 +976,8 @@ struct OnboardingErrorCard: View {
943976
self.docsSlug = docsSlug
944977
self.retryTitle = retryTitle
945978
self.retry = retry
979+
self.secondaryTitle = secondaryTitle
980+
self.secondary = secondary
946981
}
947982

948983
var body: some View {
@@ -967,6 +1002,11 @@ struct OnboardingErrorCard: View {
9671002
.buttonStyle(.borderedProminent)
9681003
.controlSize(.small)
9691004
}
1005+
if let secondaryTitle, let secondary {
1006+
Button(secondaryTitle, action: secondary)
1007+
.buttonStyle(.bordered)
1008+
.controlSize(.small)
1009+
}
9701010
Button("Open help…") {
9711011
if let url = URL(string: "https://docs.openclaw.ai/\(docsSlug)") {
9721012
NSWorkspace.shared.open(url)

0 commit comments

Comments
 (0)