-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Claude: report the terminal verdict when no refresh can restore the profile #2747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
luisgonzaleznf
wants to merge
1
commit into
steipete:main
from
luisgonzaleznf:fix/claude-unrecoverable-oauth-guidance
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
Tests/CodexBarTests/ClaudeUnrecoverableOAuthGuidanceTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import Foundation | ||
| import Testing | ||
| @testable import CodexBarCore | ||
|
|
||
| /// Regression coverage for #2733: a profile no refresh can restore must not be told to click Refresh. | ||
| @Suite(.serialized) | ||
| struct ClaudeUnrecoverableOAuthGuidanceTests { | ||
| private func makeTemporaryDirectory() throws -> URL { | ||
| let root = URL(fileURLWithPath: NSTemporaryDirectory()) | ||
| .appendingPathComponent("codexbar-guidance-\(UUID().uuidString)") | ||
| try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true) | ||
| return root | ||
| } | ||
|
|
||
| private func makeCredentialsData(expiresAt: Date) -> Data { | ||
| let millis = Int(expiresAt.timeIntervalSince1970 * 1000) | ||
| return Data(""" | ||
| { | ||
| "claudeAiOauth": { | ||
| "accessToken": "from-file", | ||
| "expiresAt": \(millis), | ||
| "scopes": ["user:profile"] | ||
| } | ||
| } | ||
| """.utf8) | ||
| } | ||
|
|
||
| private func backgroundPolicy() -> ClaudeUsageFetcher.ClaudeOAuthKeychainPromptPolicy { | ||
| ClaudeUsageFetcher.ClaudeOAuthKeychainPromptPolicy( | ||
| mode: .onlyOnUserAction, | ||
| isApplicable: true, | ||
| interaction: .background) | ||
| } | ||
|
|
||
| private func message(from error: Error) -> String? { | ||
| guard case let ClaudeUsageError.oauthFailed(message) = error else { return nil } | ||
| return message | ||
| } | ||
|
|
||
| @Test | ||
| func `a profile no refresh can restore is told to switch source, not to click Refresh`() async throws { | ||
| let root = try self.makeTemporaryDirectory() | ||
| defer { try? FileManager.default.removeItem(at: root) } | ||
|
|
||
| // Keychain reads disabled (production always is) and no credentials file: nothing a delegated | ||
| // refresh produces can ever be read back, so "Click Refresh" would loop forever. | ||
| try await ClaudeOAuthCredentialsStore.withKeychainAccessOverrideForTesting(true) { | ||
| try await ClaudeOAuthCredentialsStore.withCredentialsURLOverrideForTesting( | ||
| root.appendingPathComponent(".credentials.json")) | ||
| { | ||
| #expect(ClaudeUsageFetcher.isDelegatedRefreshProvablyUnreadable(environment: [:])) | ||
|
|
||
| let thrown = #expect(throws: ClaudeUsageError.self) { | ||
| try ClaudeUsageFetcher.assertDelegatedRefreshAllowedInCurrentInteraction( | ||
| policy: self.backgroundPolicy(), | ||
| allowBackgroundDelegatedRefresh: false, | ||
| isProvablyUnreadable: true) | ||
| } | ||
| #expect(self.message(from: thrown!) == ClaudeUsageFetcher.unreadableCredentialsMessage) | ||
| #expect(self.message(from: thrown!)?.contains("Click Refresh") != true) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| func `a recoverable profile keeps the click Refresh guidance`() async throws { | ||
| let root = try self.makeTemporaryDirectory() | ||
| defer { try? FileManager.default.removeItem(at: root) } | ||
| let credentialsURL = root.appendingPathComponent(".credentials.json") | ||
| try self.makeCredentialsData(expiresAt: Date(timeIntervalSinceNow: 3600)).write(to: credentialsURL) | ||
|
|
||
| try await ClaudeOAuthCredentialsStore.withKeychainAccessOverrideForTesting(true) { | ||
| try await ClaudeOAuthCredentialsStore.withCredentialsURLOverrideForTesting(credentialsURL) { | ||
| // A credentials file is present, so a delegated refresh can still hand something back. | ||
| #expect(!ClaudeUsageFetcher.isDelegatedRefreshProvablyUnreadable(environment: [:])) | ||
|
|
||
| let thrown = #expect(throws: ClaudeUsageError.self) { | ||
| try ClaudeUsageFetcher.assertDelegatedRefreshAllowedInCurrentInteraction( | ||
| policy: self.backgroundPolicy(), | ||
| allowBackgroundDelegatedRefresh: false, | ||
| isProvablyUnreadable: false) | ||
| } | ||
| #expect(self.message(from: thrown!)?.contains("Click Refresh") == true) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| func `a user initiated refresh is never blocked by the prompt policy`() throws { | ||
| let policy = ClaudeUsageFetcher.ClaudeOAuthKeychainPromptPolicy( | ||
| mode: .onlyOnUserAction, | ||
| isApplicable: true, | ||
| interaction: .userInitiated) | ||
| // Delegation must still run for user actions: on older Claude Code the touch itself can create the | ||
| // credentials file, which is exactly the case the terminal verdict must not pre-empt. | ||
| try ClaudeUsageFetcher.assertDelegatedRefreshAllowedInCurrentInteraction( | ||
| policy: policy, | ||
| allowBackgroundDelegatedRefresh: false, | ||
| isProvablyUnreadable: true) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a background poll hits
onlyOnUserActionbefore the user has tried a manual delegated refresh, the absence of a credentials file is not enough to prove the profile is unrecoverable: the delegated-refresh coordinator intentionally treats older Claude Code as potentially able to create that file during a retried touch (ClaudeOAuthDelegatedRefreshCoordinator.swift:307-311). Returning true here makes those recoverable cached CLI credentials display the terminal “Switch source” message instead of the only action that can create the file, so this should depend on an actual post-touchisUnreadableAfterRefreshverdict or equivalent persisted evidence.Useful? React with 👍 / 👎.