Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Sources/CodexReview/Model/CodexReviewServerState.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 13 additions & 5 deletions Sources/CodexReview/Store/CodexReviewStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,13 @@ public final class CodexReviewStore {
@ObservationIgnored package var runtimeAccountOperations: [UUID: Task<Void, any Error>] = [:]
@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?
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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()
}

Expand Down
39 changes: 21 additions & 18 deletions Sources/CodexReviewHost/CodexExecutableResolver.swift
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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")
}
Expand Down Expand Up @@ -121,18 +124,24 @@ 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]
) throws(CodexExecutableResolutionError) -> (launcherURL: URL, executableURL: URL) {
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 }
Expand All @@ -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
Expand All @@ -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)
}
}
}

Expand Down
73 changes: 60 additions & 13 deletions Sources/ReviewUI/SignIn/SignInView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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") {
Expand All @@ -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
Expand All @@ -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())
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/CodexReviewHostTests/CodexExecutableResolverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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") })
}
}
Expand Down
2 changes: 2 additions & 0 deletions Tests/CodexReviewHostTests/CodexReviewHostTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading