-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fix CLIProxyApi model normalization and cross-provider pricing in Claude Code (#2393) #2415
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
Yuxin-Qiao
wants to merge
3
commits into
steipete:main
from
Yuxin-Qiao:fix/issue-2393-cli-proxy-pricing
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| // Generated by Scripts/regenerate-codex-parser-hash.sh. Do not edit by hand. | ||
|
|
||
| enum CodexParserHash { | ||
| static let value = "48ac20dad61e9a7f" | ||
| static let value = "588b3c4be6e47928" | ||
| } |
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
153 changes: 153 additions & 0 deletions
153
Tests/CodexBarTests/ClaudeIssue2403ReproductionTests.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,153 @@ | ||
| import CodexBarCore | ||
| import Foundation | ||
| import Testing | ||
| @testable import CodexBar | ||
|
|
||
| @MainActor | ||
| @Suite(.serialized) | ||
| struct ClaudeIssue2403ReproductionTests { | ||
| private func makeSettings(suiteSuffix: String) -> SettingsStore { | ||
| let suite = "ClaudeIssue2403ReproductionTests-\(suiteSuffix)" | ||
| let defaults = UserDefaults(suiteName: suite)! | ||
| defaults.removePersistentDomain(forName: suite) | ||
| return SettingsStore( | ||
| userDefaults: defaults, | ||
| configStore: testConfigStore(suiteName: suite), | ||
| zaiTokenStore: NoopZaiTokenStore(), | ||
| syntheticTokenStore: NoopSyntheticTokenStore()) | ||
| } | ||
|
|
||
| @Test | ||
| func `web refresh failure disconnects after login`() async throws { | ||
| let registry = ProviderRegistry.shared | ||
| let claudeMetadata = try #require(registry.metadata[.claude]) | ||
|
|
||
| let settings = self.makeSettings(suiteSuffix: "web-unauthorized") | ||
| settings.statusChecksEnabled = false | ||
| settings.refreshFrequency = .manual | ||
| settings.providerDetectionCompleted = true | ||
| settings.claudeUsageDataSource = .web | ||
| settings.setProviderEnabled(provider: .claude, metadata: claudeMetadata, enabled: false) | ||
|
|
||
| let fetcher = UsageFetcher() | ||
| let store = UsageStore( | ||
| fetcher: fetcher, | ||
| browserDetection: BrowserDetection(cacheTTL: 0), | ||
| settings: settings) | ||
|
|
||
| // 1. Simulate user clicking "Sign in with Claude Code..." which runs login runner and succeeds | ||
| await withStatusItemControllerForTesting(store: store, settings: settings, fetcher: fetcher) { controller in | ||
| let didLogin = await controller.runClaudeLoginFlow { _, onPhaseChange in | ||
| onPhaseChange(.requesting) | ||
| await Task.yield() | ||
| onPhaseChange(.waitingBrowser) | ||
| await Task.yield() | ||
| return ClaudeLoginRunner.Result( | ||
| outcome: .success, | ||
| output: "Successfully logged in", | ||
| authLink: nil) | ||
| } | ||
|
|
||
| #expect(didLogin) | ||
| #expect(settings.isProviderEnabledCached(provider: .claude, metadataByProvider: registry.metadata)) | ||
| } | ||
|
|
||
| // 2. Immediately after login, store refreshes provider. | ||
| // If web API is unauthorized (e.g. browser not logged in), web fetch fails with unauthorized | ||
| store.errors[.claude] = ClaudeWebAPIFetcher.FetchError.unauthorized.localizedDescription | ||
|
|
||
| // 3. Verify menu actions immediately revert to re-login / disconnect state | ||
| let actions = MenuDescriptor.build( | ||
| provider: .claude, | ||
| store: store, | ||
| settings: settings, | ||
| account: fetcher.loadAccountInfo(), | ||
| updateReady: false) | ||
| .sections | ||
| .flatMap(\.entries) | ||
| .compactMap { entry -> (String, MenuDescriptor.MenuAction)? in | ||
| guard case let .action(label, action) = entry else { return nil } | ||
| return (label, action) | ||
| } | ||
|
|
||
| #expect(actions.contains { | ||
| $0.0 == "Re-login at claude.ai" && $0.1 == .loginToProvider(url: "https://claude.ai/") | ||
| }) | ||
| } | ||
|
|
||
| @Test | ||
| func `login flow succeeds but stale token account causes immediate refresh failure and disconnect`() async throws { | ||
| let registry = ProviderRegistry.shared | ||
| let claudeMetadata = try #require(registry.metadata[.claude]) | ||
|
|
||
| let settings = self.makeSettings(suiteSuffix: "token-account-stale") | ||
| settings.statusChecksEnabled = false | ||
| settings.refreshFrequency = .manual | ||
| settings.providerDetectionCompleted = true | ||
| settings.claudeUsageDataSource = .auto | ||
|
|
||
| // User previously configured a stale/invalid token account | ||
| settings.addTokenAccount(provider: .claude, label: "Old Account", token: "invalid-cookie-session-token") | ||
| settings.setProviderEnabled(provider: .claude, metadata: claudeMetadata, enabled: false) | ||
|
|
||
| let fetcher = UsageFetcher() | ||
| let store = UsageStore( | ||
| fetcher: fetcher, | ||
| browserDetection: BrowserDetection(cacheTTL: 0), | ||
| settings: settings) | ||
|
|
||
| // 1. User signs in with Claude Code CLI | ||
| await withStatusItemControllerForTesting(store: store, settings: settings, fetcher: fetcher) { controller in | ||
| let didLogin = await controller.runClaudeLoginFlow { _, onPhaseChange in | ||
| onPhaseChange(.requesting) | ||
| await Task.yield() | ||
| return ClaudeLoginRunner.Result( | ||
| outcome: .success, | ||
| output: "Logged in successfully", | ||
| authLink: nil) | ||
| } | ||
|
|
||
| #expect(didLogin) | ||
| } | ||
|
|
||
| // 2. Token account override causes fetch to fail on refresh | ||
| store.errors[.claude] = "Claude session key invalid" | ||
|
|
||
| withStatusItemControllerForTesting(store: store, settings: settings, fetcher: fetcher) { controller in | ||
| let model = controller.menuCardModel(for: .claude) | ||
| #expect(model?.subtitleStyle == .error) | ||
| #expect(model?.subtitleText == "Claude session key invalid") | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| func `oauth unauthorized error after login forces terminal re-auth prompt`() { | ||
| let settings = self.makeSettings(suiteSuffix: "oauth-unauthorized") | ||
| settings.claudeUsageDataSource = .oauth | ||
|
|
||
| let fetcher = UsageFetcher() | ||
| let store = UsageStore( | ||
| fetcher: fetcher, | ||
| browserDetection: BrowserDetection(cacheTTL: 0), | ||
| settings: settings) | ||
|
|
||
| store.errors[.claude] = ClaudeOAuthFetchError.unauthorized.localizedDescription | ||
|
|
||
| let actions = MenuDescriptor.build( | ||
| provider: .claude, | ||
| store: store, | ||
| settings: settings, | ||
| account: fetcher.loadAccountInfo(), | ||
| updateReady: false) | ||
| .sections | ||
| .flatMap(\.entries) | ||
| .compactMap { entry -> (String, MenuDescriptor.MenuAction)? in | ||
| guard case let .action(label, action) = entry else { return nil } | ||
| return (label, action) | ||
| } | ||
|
|
||
| #expect(actions.contains { | ||
| $0.0 == "Open Terminal" && $0.1 == .openTerminal(command: "claude") | ||
| }) | ||
| } | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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 Claude Code logs a non-Claude model with
cache_creation_input_tokens,totalPromptTokensalready includes those cache-write tokens, but this call passescacheWriteInputTokens: 0.codexCostUSDthen treats the writes as ordinary uncached input, so proxy OpenAI models with cache-write tariffs (for example GPT-5.6 built-ins or models.devcache_writerates) get incorrect costs; passcacheCreationInputTokensas the cache-write subset instead.Useful? React with 👍 / 👎.