-
Notifications
You must be signed in to change notification settings - Fork 333
Fix enterprise/team Cursor usage via usage-summary fallback #832
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -183,6 +183,152 @@ enum CursorUsageMapper { | |||||||||||||||||||||||||||||||||||||||||||||||
| return CursorMappedUsage(plan: planLabel(planName), lines: lines) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| static func mapUsageSummary( | ||||||||||||||||||||||||||||||||||||||||||||||||
| _ summary: [String: Any], | ||||||||||||||||||||||||||||||||||||||||||||||||
| planName: String?, | ||||||||||||||||||||||||||||||||||||||||||||||||
| creditGrants: [String: Any]?, | ||||||||||||||||||||||||||||||||||||||||||||||||
| stripeBalanceCents: Double | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) throws -> CursorMappedUsage { | ||||||||||||||||||||||||||||||||||||||||||||||||
| var lines: [MetricLine] = [] | ||||||||||||||||||||||||||||||||||||||||||||||||
| appendCredits(creditGrants: creditGrants, stripeBalanceCents: stripeBalanceCents, to: &lines) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| let cycle = billingCycleFromSummary(summary) | ||||||||||||||||||||||||||||||||||||||||||||||||
| let teamUsage = summary["teamUsage"] as? [String: Any] | ||||||||||||||||||||||||||||||||||||||||||||||||
| let individualUsage = summary["individualUsage"] as? [String: Any] | ||||||||||||||||||||||||||||||||||||||||||||||||
| let pooled = teamUsage?["pooled"] as? [String: Any] | ||||||||||||||||||||||||||||||||||||||||||||||||
| let onDemand = teamUsage?["onDemand"] as? [String: Any] | ||||||||||||||||||||||||||||||||||||||||||||||||
| let overall = individualUsage?["overall"] as? [String: Any] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| let hasIndividualOverall = appendDollarUsageBucket( | ||||||||||||||||||||||||||||||||||||||||||||||||
| overall, | ||||||||||||||||||||||||||||||||||||||||||||||||
| label: "Total usage", | ||||||||||||||||||||||||||||||||||||||||||||||||
| resetsAt: cycle.resetsAt, | ||||||||||||||||||||||||||||||||||||||||||||||||
| periodDurationMs: cycle.periodDurationMs, | ||||||||||||||||||||||||||||||||||||||||||||||||
| to: &lines | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||
| var hasUsageMetric = hasIndividualOverall | ||||||||||||||||||||||||||||||||||||||||||||||||
| if !hasUsageMetric { | ||||||||||||||||||||||||||||||||||||||||||||||||
| hasUsageMetric = appendDollarUsageBucket( | ||||||||||||||||||||||||||||||||||||||||||||||||
| pooled, | ||||||||||||||||||||||||||||||||||||||||||||||||
| label: "Total usage", | ||||||||||||||||||||||||||||||||||||||||||||||||
| resetsAt: cycle.resetsAt, | ||||||||||||||||||||||||||||||||||||||||||||||||
| periodDurationMs: cycle.periodDurationMs, | ||||||||||||||||||||||||||||||||||||||||||||||||
| to: &lines | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } else if appendDollarUsageBucket( | ||||||||||||||||||||||||||||||||||||||||||||||||
| pooled, | ||||||||||||||||||||||||||||||||||||||||||||||||
| label: "Team pool", | ||||||||||||||||||||||||||||||||||||||||||||||||
| resetsAt: cycle.resetsAt, | ||||||||||||||||||||||||||||||||||||||||||||||||
| periodDurationMs: cycle.periodDurationMs, | ||||||||||||||||||||||||||||||||||||||||||||||||
| to: &lines | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| hasUsageMetric = true | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if let autoPercent = percentFromDisplayMessage(summary["autoModelSelectedDisplayMessage"] as? String) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| hasUsageMetric = true | ||||||||||||||||||||||||||||||||||||||||||||||||
| lines.append(.progress( | ||||||||||||||||||||||||||||||||||||||||||||||||
| label: "Auto usage", | ||||||||||||||||||||||||||||||||||||||||||||||||
| used: autoPercent, | ||||||||||||||||||||||||||||||||||||||||||||||||
| limit: 100, | ||||||||||||||||||||||||||||||||||||||||||||||||
| format: .percent, | ||||||||||||||||||||||||||||||||||||||||||||||||
| resetsAt: cycle.resetsAt, | ||||||||||||||||||||||||||||||||||||||||||||||||
| periodDurationMs: cycle.periodDurationMs | ||||||||||||||||||||||||||||||||||||||||||||||||
| )) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if let apiPercent = percentFromDisplayMessage(summary["namedModelSelectedDisplayMessage"] as? String) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| hasUsageMetric = true | ||||||||||||||||||||||||||||||||||||||||||||||||
| lines.append(.progress( | ||||||||||||||||||||||||||||||||||||||||||||||||
| label: "API usage", | ||||||||||||||||||||||||||||||||||||||||||||||||
| used: apiPercent, | ||||||||||||||||||||||||||||||||||||||||||||||||
| limit: 100, | ||||||||||||||||||||||||||||||||||||||||||||||||
| format: .percent, | ||||||||||||||||||||||||||||||||||||||||||||||||
| resetsAt: cycle.resetsAt, | ||||||||||||||||||||||||||||||||||||||||||||||||
| periodDurationMs: cycle.periodDurationMs | ||||||||||||||||||||||||||||||||||||||||||||||||
| )) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if appendDollarUsageBucket(onDemand, label: "On-demand", to: &lines) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| hasUsageMetric = true | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| guard hasUsageMetric else { | ||||||||||||||||||||||||||||||||||||||||||||||||
| throw CursorUsageError.requestBasedUnavailable("Enterprise usage data unavailable. Try again later.") | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| let resolvedPlan = planName ?? (summary["membershipType"] as? String) | ||||||||||||||||||||||||||||||||||||||||||||||||
| return CursorMappedUsage(plan: planLabel(resolvedPlan), lines: lines) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| static func shouldUseUsageSummaryFallback(usage: [String: Any], planName: String?) -> Bool { | ||||||||||||||||||||||||||||||||||||||||||||||||
| let (shouldFallback, _) = shouldUseRequestBasedFallback( | ||||||||||||||||||||||||||||||||||||||||||||||||
| usage: usage, | ||||||||||||||||||||||||||||||||||||||||||||||||
| planName: planName, | ||||||||||||||||||||||||||||||||||||||||||||||||
| planInfoUnavailable: false | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||
| guard shouldFallback else { return false } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| let normalizedPlan = planName?.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() ?? "" | ||||||||||||||||||||||||||||||||||||||||||||||||
| if normalizedPlan == "enterprise" || normalizedPlan == "team" { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return true | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| let spendLimitUsage = usage["spendLimitUsage"] as? [String: Any] | ||||||||||||||||||||||||||||||||||||||||||||||||
| let pooledLimit = ProviderParse.number(spendLimitUsage?["pooledLimit"]) ?? 0 | ||||||||||||||||||||||||||||||||||||||||||||||||
| return (spendLimitUsage?["limitType"] as? String)?.lowercased() == "team" || pooledLimit > 0 | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| private static func appendDollarUsageBucket( | ||||||||||||||||||||||||||||||||||||||||||||||||
| _ bucket: [String: Any]?, | ||||||||||||||||||||||||||||||||||||||||||||||||
| label: String, | ||||||||||||||||||||||||||||||||||||||||||||||||
| resetsAt: Date? = nil, | ||||||||||||||||||||||||||||||||||||||||||||||||
| periodDurationMs: Int? = nil, | ||||||||||||||||||||||||||||||||||||||||||||||||
| to lines: inout [MetricLine] | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> Bool { | ||||||||||||||||||||||||||||||||||||||||||||||||
| guard bucket?["enabled"] as? Bool != false, | ||||||||||||||||||||||||||||||||||||||||||||||||
| let limitCents = ProviderParse.number(bucket?["limit"]), | ||||||||||||||||||||||||||||||||||||||||||||||||
| limitCents > 0 | ||||||||||||||||||||||||||||||||||||||||||||||||
| else { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return false | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| let usedCents = ProviderParse.number(bucket?["used"]) | ||||||||||||||||||||||||||||||||||||||||||||||||
| ?? (limitCents - (ProviderParse.number(bucket?["remaining"]) ?? 0)) | ||||||||||||||||||||||||||||||||||||||||||||||||
| lines.append(.progress( | ||||||||||||||||||||||||||||||||||||||||||||||||
| label: label, | ||||||||||||||||||||||||||||||||||||||||||||||||
| used: ProviderParse.centsToDollars(usedCents), | ||||||||||||||||||||||||||||||||||||||||||||||||
| limit: ProviderParse.centsToDollars(limitCents), | ||||||||||||||||||||||||||||||||||||||||||||||||
| format: .dollars, | ||||||||||||||||||||||||||||||||||||||||||||||||
| resetsAt: resetsAt, | ||||||||||||||||||||||||||||||||||||||||||||||||
| periodDurationMs: periodDurationMs | ||||||||||||||||||||||||||||||||||||||||||||||||
| )) | ||||||||||||||||||||||||||||||||||||||||||||||||
| return true | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| private static func billingCycleFromSummary(_ summary: [String: Any]) -> (resetsAt: Date?, periodDurationMs: Int) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| let cycleStart = (summary["billingCycleStart"] as? String).flatMap(OpenUsageISO8601.date(from:)) | ||||||||||||||||||||||||||||||||||||||||||||||||
| let cycleEnd = (summary["billingCycleEnd"] as? String).flatMap(OpenUsageISO8601.date(from:)) | ||||||||||||||||||||||||||||||||||||||||||||||||
| guard let cycleStart, let cycleEnd, cycleEnd > cycleStart else { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return (cycleEnd, billingPeriodMs) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||
| cycleEnd, | ||||||||||||||||||||||||||||||||||||||||||||||||
| Int(cycleEnd.timeIntervalSince(cycleStart) * 1000) | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| private static func percentFromDisplayMessage(_ message: String?) -> Double? { | ||||||||||||||||||||||||||||||||||||||||||||||||
| guard let message else { return nil } | ||||||||||||||||||||||||||||||||||||||||||||||||
| let pattern = #"(\d+(?:\.\d+)?)\s*%"# | ||||||||||||||||||||||||||||||||||||||||||||||||
| guard let regex = try? NSRegularExpression(pattern: pattern), | ||||||||||||||||||||||||||||||||||||||||||||||||
| let match = regex.firstMatch(in: message, range: NSRange(message.startIndex..., in: message)), | ||||||||||||||||||||||||||||||||||||||||||||||||
| let range = Range(match.range(at: 1), in: message) | ||||||||||||||||||||||||||||||||||||||||||||||||
| else { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| return Double(message[range]) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+320
to
+330
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: Sources/OpenUsage/Providers/Cursor/CursorUsageMapper.swift
Line: 311-321
Comment:
The `NSRegularExpression` is compiled from scratch on every call to `percentFromDisplayMessage`. The pattern is static, so a one-time `static let` avoids repeated compilation.
```suggestion
private static let percentRegex = try? NSRegularExpression(pattern: #"(\d+(?:\.\d+)?)\s*%"#)
private static func percentFromDisplayMessage(_ message: String?) -> Double? {
guard let message,
let regex = percentRegex,
let match = regex.firstMatch(in: message, range: NSRange(message.startIndex..., in: message)),
let range = Range(match.range(at: 1), in: message)
else {
return nil
}
return Double(message[range])
}
```
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| static func shouldUseRequestBasedFallback( | ||||||||||||||||||||||||||||||||||||||||||||||||
| usage: [String: Any], | ||||||||||||||||||||||||||||||||||||||||||||||||
| planName: String?, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
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.
usageSummaryResulttry?discards any error thrown byusageSummaryResult— includingCursorUsageError.connectionFailedifcursor.comis reachable from the gRPC endpoint (api2.cursor.sh) but not from the REST endpoint. When that happens the provider silently falls through torequestBasedResult, which will also fail and ultimately surface "Enterprise usage data unavailable" rather than "Connection failed". This is consistent with theshouldTryGenericRequestFallbackblock above, but there the suppressed path is a speculative optimistic attempt; here it is the primary enterprise code path. Consider propagating at least connection-class errors instead of uniformly swallowing them.Prompt To Fix With AI