Improve Claude setup-token OAuth usage and API fallback#133
Improve Claude setup-token OAuth usage and API fallback#133hanrw merged 1 commit intotddworks:mainfrom
Conversation
Use intelligent probe fallback so Claude can recover from /usage parse failures by using OAuth API quotas, and harden long-lived token normalization to avoid malformed auth headers.
📝 WalkthroughWalkthroughThe PR implements a multi-probe fallback mechanism for the Claude provider, allowing graceful switching between CLI and API usage probes when either fails. Additionally, whitespace normalization is applied to credential tokens during loading and in API request headers. Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant Provider as ClaudeProvider
participant Primary as primaryProbe()
participant Fallback as fallbackProbe()
participant Snapshot as snapshot
Caller->>Provider: refresh()
Provider->>Primary: probe() for usage snapshot
alt Primary Succeeds
Primary-->>Provider: snapshot returned
Provider->>Snapshot: update with success
Provider-->>Caller: success
else Primary Fails
Primary-->>Provider: error thrown
Provider->>Fallback: attempt fallbackProbe()
alt Fallback Available
Fallback->>Fallback: probe() for usage snapshot
alt Fallback Succeeds
Fallback-->>Provider: snapshot returned
Provider->>Snapshot: update with fallback snapshot
Provider->>Provider: clear lastError
Provider-->>Caller: success via fallback
else Fallback Fails
Fallback-->>Provider: error thrown
Provider-->>Caller: propagate error
end
else No Fallback Available
Provider-->>Caller: propagate primary error
end
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Tip Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs). Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
Sources/Domain/Provider/Claude/ClaudeProvider.swift (1)
164-173: Preserve primary failure context when fallback probe also fails.On Line 171/Line 172,
lastErrorand thrown error become fallback-only, which hides the original failure cause from diagnostics.♻️ Proposed fix
- } catch { + } catch let primaryError { if let fallback = await fallbackProbe() { do { let newSnapshot = try await fallback.probe() snapshot = newSnapshot lastError = nil return newSnapshot - } catch { - lastError = error - throw error + } catch let fallbackError { + lastError = primaryError + throw fallbackError } } - lastError = error - throw error + lastError = primaryError + throw primaryError }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@Sources/Domain/Provider/Claude/ClaudeProvider.swift` around lines 164 - 173, The current logic overwrites the primary probe failure with the fallback error, losing original context; modify the block in ClaudeProvider (around fallbackProbe(), snapshot, lastError) to capture and keep the original error before invoking fallback.probe(), and if the fallback also throws, set lastError to a composite error that preserves both the primary and fallback errors (or attach the primary as an underlying/inner error) and rethrow the fallback error (or the composite) so diagnostic code can access both failures; ensure when fallback succeeds you still clear lastError and update snapshot as before.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@Sources/Domain/Provider/Claude/ClaudeProvider.swift`:
- Around line 164-173: The current logic overwrites the primary probe failure
with the fallback error, losing original context; modify the block in
ClaudeProvider (around fallbackProbe(), snapshot, lastError) to capture and keep
the original error before invoking fallback.probe(), and if the fallback also
throws, set lastError to a composite error that preserves both the primary and
fallback errors (or attach the primary as an underlying/inner error) and rethrow
the fallback error (or the composite) so diagnostic code can access both
failures; ensure when fallback succeeds you still clear lastError and update
snapshot as before.
ℹ️ Review info
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
Sources/Domain/Provider/Claude/ClaudeProvider.swiftSources/Infrastructure/Claude/ClaudeAPIUsageProbe.swiftSources/Infrastructure/Claude/ClaudeCredentialLoader.swiftTests/AcceptanceTests/ClaudeConfigSpec.swiftTests/InfrastructureTests/Claude/ClaudeAPIUsageProbeTests.swiftTests/InfrastructureTests/Claude/ClaudeCredentialLoaderTests.swift
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #133 +/- ##
==========================================
+ Coverage 77.97% 78.04% +0.06%
==========================================
Files 78 79 +1
Lines 6048 6121 +73
==========================================
+ Hits 4716 4777 +61
- Misses 1332 1344 +12
🚀 New features to boost your workflow:
|
Summary
CLAUDE_CODE_OAUTH_TOKENsetup tokens (including whitespace/newline normalization) so long-lived local OAuth tokens are accepted reliably/usageparsing failsVerification
Summary by CodeRabbit
Bug Fixes
New Features