From 4b9b51e8ab2388db37f78b884346f22ebe8c199d Mon Sep 17 00:00:00 2001 From: Kazuki Nakashima <65545348+lynnswap@users.noreply.github.com> Date: Sat, 21 Nov 2026 15:42:41 +0900 Subject: [PATCH] Show CLI setup help only when executable lookup fails --- .../Model/CodexReviewServerState.swift | 10 +++ .../CodexReview/Store/CodexReviewStore.swift | 18 +++-- .../CodexExecutableResolver.swift | 39 +++++----- Sources/ReviewUI/SignIn/SignInView.swift | 73 +++++++++++++++---- .../CodexExecutableResolverTests.swift | 4 +- .../CodexReviewHostTests.swift | 2 + 6 files changed, 108 insertions(+), 38 deletions(-) diff --git a/Sources/CodexReview/Model/CodexReviewServerState.swift b/Sources/CodexReview/Model/CodexReviewServerState.swift index 50125210..a3e89bee 100644 --- a/Sources/CodexReview/Model/CodexReviewServerState.swift +++ b/Sources/CodexReview/Model/CodexReviewServerState.swift @@ -1,3 +1,13 @@ +package enum CodexExecutableFailureKind: Equatable, Sendable { + case notFound + case invalidConfiguredPath + case invalidEnvironment(String) +} + +package protocol CodexExecutableFailure: Error { + var kind: CodexExecutableFailureKind { get } +} + public enum CodexReviewServerState: Sendable, Equatable { case stopped case starting diff --git a/Sources/CodexReview/Store/CodexReviewStore.swift b/Sources/CodexReview/Store/CodexReviewStore.swift index 9d867824..d691d97a 100644 --- a/Sources/CodexReview/Store/CodexReviewStore.swift +++ b/Sources/CodexReview/Store/CodexReviewStore.swift @@ -97,7 +97,13 @@ public final class CodexReviewStore { @ObservationIgnored package var runtimeAccountOperations: [UUID: Task] = [:] @ObservationIgnored package var unclosedCodexUpdateRuntime: PreparedRuntime? - public package(set) var serverState: CodexReviewServerState = .stopped + package private(set) var serverStatus: ( + state: CodexReviewServerState, executableFailure: CodexExecutableFailureKind? + ) = (.stopped, nil) + public package(set) var serverState: CodexReviewServerState { + get { serverStatus.state } + set { serverStatus = (newValue, nil) } + } public let auth: CodexReviewAuthModel package let settings: SettingsStore public package(set) var serverURL: URL? @@ -1153,7 +1159,7 @@ public final class CodexReviewStore { retainedMCP: nil, failureIncident: context.failureIncident ) - transitionToFailed(error.localizedDescription) + transitionToFailed(error.localizedDescription, executableFailure: (error as? any CodexExecutableFailure)?.kind) } } @@ -1311,7 +1317,7 @@ public final class CodexReviewStore { failureIncident: nil ) serverURL = replacement.retainedMCP.serverURL - serverState = .failed(failureMessage) + serverStatus = (.failed(failureMessage), (error as? any CodexExecutableFailure)?.kind) writeDiagnosticsIfNeeded() replacement.finish(.failed(failureMessage)) } @@ -1819,12 +1825,14 @@ public final class CodexReviewStore { writeDiagnosticsIfNeeded() } - package func transitionToFailed(_ message: String, resetJobs: Bool = false) { + package func transitionToFailed( + _ message: String, executableFailure: CodexExecutableFailureKind? = nil, resetJobs: Bool = false + ) { serverURL = nil if resetJobs { resetReviews() } - serverState = .failed(message) + serverStatus = (.failed(message), executableFailure) writeDiagnosticsIfNeeded() } diff --git a/Sources/CodexReviewHost/CodexExecutableResolver.swift b/Sources/CodexReviewHost/CodexExecutableResolver.swift index 794a672c..b257b826 100644 --- a/Sources/CodexReviewHost/CodexExecutableResolver.swift +++ b/Sources/CodexReviewHost/CodexExecutableResolver.swift @@ -1,7 +1,7 @@ import Foundation +import CodexReview -package struct CodexExecutableResolutionError: LocalizedError, Equatable, Sendable { - package enum Kind: Equatable, Sendable { case invalidExplicit, notFound } +package struct CodexExecutableResolutionError: LocalizedError, CodexExecutableFailure, Equatable { package enum Source: Equatable, Sendable { case configuredPath, environment(String), path(String), homeLocalBin case fallbackBin(String) @@ -10,12 +10,15 @@ package struct CodexExecutableResolutionError: LocalizedError, Equatable, Sendab package var source: Source package var candidate, reason: String } - package var kind: Kind + package var kind: CodexExecutableFailureKind package var trace: [Trace] package var errorDescription: String? { - let headline = kind == .invalidExplicit - ? "The explicitly selected Codex executable is invalid." - : "No usable Codex executable was found. Install the Codex CLI or select its executable in Settings." + let headline = switch kind { + case .notFound: + "No usable Codex executable was found. Install the Codex CLI or select its executable in Settings." + case .invalidConfiguredPath, .invalidEnvironment: + "The explicitly selected Codex executable is invalid." + } return ([headline] + trace.map { "\($0.source): \($0.candidate) — \($0.reason)" }) .joined(separator: "\n") } @@ -121,9 +124,12 @@ package struct CodexExecutableResolver: Sendable { var trace: [CodexExecutableResolutionError.Trace] = [] mutating func explicitPath(_ value: String) throws(CodexExecutableResolutionError) -> (launcherURL: URL, executableURL: URL) { let path = value.trimmingCharacters(in: .whitespacesAndNewlines) - guard path.hasPrefix("/") else { try failExplicit(.configuredPath, value, "not absolute") } - if let url = candidate(URL(fileURLWithPath: path), .configuredPath) { return url } - throw .init(kind: .invalidExplicit, trace: trace) + if path.hasPrefix("/") { + if let url = candidate(URL(fileURLWithPath: path), .configuredPath) { return url } + } else { + reject(.configuredPath, value, "not absolute") + } + throw .init(kind: .invalidConfiguredPath, trace: trace) } mutating func environmentCommand( _ value: String, key: String, path: [URL] @@ -131,8 +137,11 @@ package struct CodexExecutableResolver: Sendable { let command = value.trimmingCharacters(in: .whitespacesAndNewlines) let source = CodexExecutableResolutionError.Source.environment(key) if command.contains("/") { - guard command.hasPrefix("/") else { try failExplicit(source, value, "not absolute") } - if let url = candidate(URL(fileURLWithPath: command), source) { return url } + if command.hasPrefix("/") { + if let url = candidate(URL(fileURLWithPath: command), source) { return url } + } else { + reject(source, value, "not absolute") + } } else if command.isEmpty == false { for directory in path { if let url = candidate(directory.appendingPathComponent(command), source) { return url } @@ -141,7 +150,7 @@ package struct CodexExecutableResolver: Sendable { } else { trace.append(.init(source: source, candidate: value, reason: "command is empty")) } - throw .init(kind: .invalidExplicit, trace: trace) + throw .init(kind: .invalidEnvironment(key), trace: trace) } mutating func candidate(_ rawURL: URL, _ source: CodexExecutableResolutionError.Source) -> (launcherURL: URL, executableURL: URL)? { let fs = configuration.fileSystem @@ -161,12 +170,6 @@ package struct CodexExecutableResolver: Sendable { ) { trace.append(.init(source: source, candidate: candidate, reason: reason)) } - mutating func failExplicit( - _ source: CodexExecutableResolutionError.Source, _ candidate: String, _ reason: String - ) throws(CodexExecutableResolutionError) -> Never { - trace.append(.init(source: source, candidate: candidate, reason: reason)) - throw .init(kind: .invalidExplicit, trace: trace) - } } } diff --git a/Sources/ReviewUI/SignIn/SignInView.swift b/Sources/ReviewUI/SignIn/SignInView.swift index e344582c..173e14c6 100644 --- a/Sources/ReviewUI/SignIn/SignInView.swift +++ b/Sources/ReviewUI/SignIn/SignInView.swift @@ -14,6 +14,19 @@ struct SignInView: View { } var body: some View { + if store.serverStatus.executableFailure != nil { + ScrollView { + signInContent + .fixedSize(horizontal: false, vertical: true) + .frame(maxWidth: .infinity) + } + .defaultScrollAnchor(.center, for: .alignment) + } else { + signInContent + } + } + + private var signInContent: some View { ContentUnavailableView { Text("Welcome to CodexReviewMonitor") .font(.largeTitle) @@ -61,11 +74,13 @@ struct SignInView: View { } description: { VStack(spacing: 12) { - Text("Reviews run using the Codex CLI installed on your Mac.") - Link("Install Codex CLI", destination: URL(string: "https://github.com/openai/codex")!) - .accessibilityIdentifier("review-monitor.install-codex-link") - if let descriptionText { - Text(descriptionText) + if let kind = store.serverStatus.executableFailure, let message = serverFailureMessage { + CodexCLISetupHelp(kind: kind, diagnostic: message) + } + if let message = store.auth.progress?.detail ?? store.auth.errorMessage { + Text(message) + } else if store.serverStatus.executableFailure == nil, let message = serverFailureMessage { + Text(message) } if case .failed = store.serverState { Button("Retry Setup", systemImage: "arrow.clockwise") { @@ -79,10 +94,6 @@ struct SignInView: View { .scenePadding() } - private var descriptionText: String? { - store.auth.progress?.detail ?? store.auth.errorMessage ?? serverFailureMessage - } - private var serverFailureMessage: String? { guard case .failed(let message) = store.serverState else { return nil @@ -92,6 +103,42 @@ struct SignInView: View { } } +private struct CodexCLISetupHelp: View { + let kind: CodexExecutableFailureKind + let diagnostic: String + @State private var showsDetails = false + + var body: some View { + switch kind { + case .notFound: + Text("Install the Codex CLI, then retry. If it’s already installed, set its path in Settings, then quit and reopen ReviewMonitor.") + case .invalidConfiguredPath: + Text("Check the Codex executable path in Settings, then quit and reopen ReviewMonitor.") + case .invalidEnvironment(let variable): + Text("Update or remove the \(variable) environment variable, then quit and reopen ReviewMonitor.") + } + HStack(spacing: 16) { + if kind == .notFound { + Link("Install Codex CLI", destination: URL(string: "https://github.com/openai/codex#quickstart")!) + .accessibilityIdentifier("review-monitor.install-codex-link") + } + Button("Diagnostic Details") { showsDetails = true } + .buttonStyle(.link) + .popover(isPresented: $showsDetails) { + ScrollView { + Text(verbatim: diagnostic) + .font(.callout.monospaced()) + .textSelection(.enabled) + .multilineTextAlignment(.leading) + .frame(maxWidth: .infinity, alignment: .leading) + .padding() + } + .frame(width: 360, height: 180) + } + } + } +} + #if DEBUG #Preview("Signed Out") { SignInView(store: makeSignInPreviewStore()) @@ -102,17 +149,17 @@ struct SignInView: View { } #Preview("Codex CLI Not Installed") { - SignInView(store: makeSetupFailurePreviewStore("No usable Codex executable was found. Install the Codex CLI, then retry setup.")) + SignInView(store: makeSetupFailurePreviewStore(.notFound, message: "No usable Codex executable was found.\n/usr/local/bin/codex — not an executable regular file")) } #Preview("Codex Startup Failed") { - SignInView(store: makeSetupFailurePreviewStore("The explicitly selected Codex executable is invalid. Check its path in Settings.")) + SignInView(store: makeSetupFailurePreviewStore(.invalidConfiguredPath, message: "The explicitly selected Codex executable is invalid.")) } @MainActor -private func makeSetupFailurePreviewStore(_ message: String) -> CodexReviewStore { +private func makeSetupFailurePreviewStore(_ kind: CodexExecutableFailureKind, message: String) -> CodexReviewStore { let store = makeSignInPreviewStore() - store.transitionToFailed(message) + store.transitionToFailed(message, executableFailure: kind) return store } diff --git a/Tests/CodexReviewHostTests/CodexExecutableResolverTests.swift b/Tests/CodexReviewHostTests/CodexExecutableResolverTests.swift index 6b9d5be6..8530e650 100644 --- a/Tests/CodexReviewHostTests/CodexExecutableResolverTests.swift +++ b/Tests/CodexReviewHostTests/CodexExecutableResolverTests.swift @@ -30,7 +30,7 @@ struct CodexExecutableResolverTests { _ = try resolver.resolve(configuredPath: "/missing", environment: ["PATH": "/auto"]) Issue.record("Invalid configured path unexpectedly fell back.") } catch { - #expect(error.kind == .invalidExplicit) + #expect(error.kind == .invalidConfiguredPath) #expect(error.trace.first?.source == .configuredPath) } do { @@ -41,7 +41,7 @@ struct CodexExecutableResolverTests { ]) Issue.record("Invalid environment command unexpectedly fell back.") } catch { - #expect(error.kind == .invalidExplicit) + #expect(error.kind == .invalidEnvironment("CODEX_APP_SERVER_CODEX_EXECUTABLE")) #expect(error.trace.allSatisfy { $0.source == .environment("CODEX_APP_SERVER_CODEX_EXECUTABLE") }) } } diff --git a/Tests/CodexReviewHostTests/CodexReviewHostTests.swift b/Tests/CodexReviewHostTests/CodexReviewHostTests.swift index c38c92c7..21488839 100644 --- a/Tests/CodexReviewHostTests/CodexReviewHostTests.swift +++ b/Tests/CodexReviewHostTests/CodexReviewHostTests.swift @@ -5747,12 +5747,14 @@ struct CodexReviewHostTests { Issue.record("Missing Codex should fail setup.") return } + #expect(store.serverStatus.executableFailure == .notFound) try Data("#!/bin/sh\nexit 0\n".utf8).write(to: executableURL) try FileManager.default.setAttributes([.posixPermissions: 0o700], ofItemAtPath: executableURL.path) await store.restart() #expect(store.serverState == .running) + #expect(store.serverStatus.executableFailure == nil) #expect(store.canPerformPrimaryAuthenticationAction) #expect(store.auth.accounts.isEmpty) await store.stop()