Skip to content

Commit 4cdb349

Browse files
steipetePeter Steinberger
andauthored
test: harden the provider special-case gatekeeper and derive its findings (#2693)
* test: harden provider architecture gatekeeper * refactor: derive provider presentation policies * test: pin provider dispatch constructs * test: document gatekeeper catalog lint scope --------- Co-authored-by: Peter Steinberger <steipete@clawstudio.local>
1 parent 61fbe9f commit 4cdb349

38 files changed

Lines changed: 2422 additions & 235 deletions

Sources/CodexBar/CostHistoryChartMenuView.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,14 @@ struct CostHistoryChartMenuView: View {
300300
}
301301

302302
static func estimateDisclaimer(provider: UsageProvider) -> String? {
303-
provider == .codex ? L("codex_api_estimate_hint") : nil
303+
guard let hint = ProviderDescriptorRegistry.descriptor(for: provider).tokenCost.chartEstimateDisclaimer else {
304+
return nil
305+
}
306+
return switch hint {
307+
case let .localized(key): L(key)
308+
case .estimate: UsageFormatter.costEstimateHint(provider: provider)
309+
case let .literal(text): L(text)
310+
}
304311
}
305312

306313
private struct Model {

Sources/CodexBar/InlineUsageDashboardContent.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ extension UsageMenuCardView.Model {
164164
: historyDays == 30
165165
? "30d"
166166
: String(format: L("Last %d days"), historyDays))
167-
let historyTitle = provider == .codex ? codexHistoryPeriod : defaultHistoryTitle
167+
let tokenCost = ProviderDescriptorRegistry.descriptor(for: provider).tokenCost
168+
let historyTitle = tokenCost.historyTitleStyle == .compact ? codexHistoryPeriod : defaultHistoryTitle
168169
let tokenHistoryTitle = snapshot.historyLabel.map { "\($0) \(L("tokens"))" }
169170
?? (historyDays == 1
170171
? L("Today tokens")
@@ -193,7 +194,7 @@ extension UsageMenuCardView.Model {
193194
accessibilityValue: "\(entry.date): \(convertedString(cost))")
194195
}
195196
let latest = CostUsageTokenSnapshot.latestEntry(in: snapshot.daily)
196-
let usesLatestPrimary = provider == .bedrock || provider == .mistral
197+
let usesLatestPrimary = tokenCost.primaryValue == .latestDaily
197198
let primaryCostUSD = usesLatestPrimary ? latest?.costUSD : snapshot.sessionCostUSD
198199
var details: [String] = []
199200
if comparisonPeriodsEnabled {
@@ -211,16 +212,16 @@ extension UsageMenuCardView.Model {
211212
if let topModel = Self.topCostModel(from: snapshot.daily) {
212213
details.append("\(L("Top model")): \(Self.shortModelName(topModel))")
213214
}
214-
if provider == .codex {
215-
details.append(L("codex_api_estimate_hint"))
215+
let hintLines = Self.tokenUsageHintLines(provider: provider)
216+
if tokenCost.hintPlacement == .beforeRequestHistory {
217+
details.append(contentsOf: hintLines)
216218
}
217-
if provider != .groq {
219+
if tokenCost.showsRequestHistory {
218220
if let requestCount = snapshot.last30DaysRequests {
219221
details
220222
.append("\(requestHistoryTitle): \(UsageFormatter.tokenCountString(requestCount)) \(L("requests"))")
221223
}
222-
if provider != .codex {
223-
let hintLines = Self.tokenUsageHintLines(provider: provider)
224+
if tokenCost.hintPlacement == .afterRequestHistory {
224225
if hintLines.isEmpty == false {
225226
details.append(contentsOf: hintLines)
226227
} else {

Sources/CodexBar/MenuCardView+Costs.swift

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -376,16 +376,13 @@ extension UsageMenuCardView.Model {
376376
}
377377

378378
static func providerCostSection(
379-
provider: UsageProvider,
380379
cost: ProviderCostSnapshot?,
380+
style: ProviderCostMenuCardStyle,
381381
isClaudeAdminAPI: Bool = false,
382382
preferredCurrencyCode: String = "auto") -> ProviderCostSection?
383383
{
384-
if provider == .manus {
385-
return nil
386-
}
384+
guard style != .hidden else { return nil }
387385
guard let cost else { return nil }
388-
guard provider != .synthetic else { return nil }
389386

390387
/// Formats a cost value using the user's currency preference.
391388
func formatCost(_ value: Double, providerCurrency: String? = nil) -> String {
@@ -395,7 +392,7 @@ extension UsageMenuCardView.Model {
395392
providerCurrency: providerCurrency ?? cost.currencyCode)
396393
}
397394

398-
if provider == .factory || provider == .devin, cost.period == "Extra usage balance" {
395+
if style == .extraUsageBalance {
399396
let balance = formatCost(cost.used)
400397
return ProviderCostSection(
401398
title: L("Extra usage"),
@@ -404,7 +401,7 @@ extension UsageMenuCardView.Model {
404401
percentLine: nil)
405402
}
406403

407-
if provider == .opencodego, cost.period == "Zen balance" {
404+
if style == .zenBalance {
408405
let balance = formatCost(cost.used)
409406
return ProviderCostSection(
410407
title: L("Zen balance"),
@@ -413,7 +410,7 @@ extension UsageMenuCardView.Model {
413410
percentLine: nil)
414411
}
415412

416-
if provider == .minimax, cost.period == "MiniMax points balance" {
413+
if style == .pointsBalance {
417414
let balance = String(format: "%.0f", cost.used)
418415
return ProviderCostSection(
419416
title: L("Credits"),
@@ -422,7 +419,7 @@ extension UsageMenuCardView.Model {
422419
percentLine: nil)
423420
}
424421

425-
if provider == .xai, cost.period == "Prepaid credits" {
422+
if style == .prepaidCredits {
426423
let balance = UsageFormatter.currencyString(cost.used, currencyCode: cost.currencyCode)
427424
return ProviderCostSection(
428425
title: L("Credits"),
@@ -431,7 +428,7 @@ extension UsageMenuCardView.Model {
431428
percentLine: nil)
432429
}
433430

434-
if provider == .zenmux || provider == .neuralwatt {
431+
if style == .payAsYouGoBalance {
435432
let balance = formatCost(cost.used)
436433
return ProviderCostSection(
437434
title: L("metric_mistral_payg"),
@@ -440,7 +437,7 @@ extension UsageMenuCardView.Model {
440437
percentLine: nil)
441438
}
442439

443-
if provider == .claude {
440+
if style == .claude {
444441
if isClaudeAdminAPI {
445442
let spend = formatCost(cost.used)
446443
let periodLabel = Self.localizedPeriodLabel(cost.period ?? "Last 30 days")
@@ -479,9 +476,7 @@ extension UsageMenuCardView.Model {
479476
showsInProviderDetails: false)
480477
}
481478

482-
if provider == .openai || provider == .litellm || provider == .aiand,
483-
cost.limit <= 0
484-
{
479+
if style == .apiSpend {
485480
let spend = formatCost(cost.used)
486481
let periodLabel = Self.localizedPeriodLabel(cost.period ?? "Last 30 days")
487482
return ProviderCostSection(
@@ -491,11 +486,7 @@ extension UsageMenuCardView.Model {
491486
percentLine: nil)
492487
}
493488

494-
if provider == .litellm {
495-
return nil
496-
}
497-
498-
if provider == .clawrouter, cost.limit <= 0 {
489+
if style == .clawRouter, cost.limit <= 0 {
499490
let spend = formatCost(cost.used)
500491
return ProviderCostSection(
501492
title: "ClawRouter spend",
@@ -510,7 +501,7 @@ extension UsageMenuCardView.Model {
510501
let limit: String
511502
let title: String
512503

513-
if provider == .clawrouter {
504+
if style == .clawRouter {
514505
title = "Monthly budget"
515506
used = formatCost(cost.used)
516507
limit = formatCost(cost.limit)

Sources/CodexBar/MenuCardView+ModelHelpers.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,12 @@ extension UsageMenuCardView.Model {
428428
}
429429

430430
static func progressColor(for provider: UsageProvider) -> Color {
431-
if provider == .elevenlabs {
431+
let branding = ProviderDescriptorRegistry.descriptor(for: provider).branding
432+
if branding.progressColorStyle == .label {
432433
return Color(nsColor: .labelColor)
433434
}
434435

435-
let color = ProviderDescriptorRegistry.descriptor(for: provider).branding.color
436+
let color = branding.color
436437
return Color(red: color.red, green: color.green, blue: color.blue)
437438
}
438439

Sources/CodexBar/MenuCardView.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,8 @@ extension UsageMenuCardView.Model {
911911
let openAIAPIUsage = input.snapshot?.openAIAPIUsage
912912
let inlineUsageDashboard = Self.inlineUsageDashboard(input: input)
913913
let usageNotes = Self.usageNotes(input: input)
914-
let menuCard = ProviderDescriptorRegistry.descriptor(for: input.provider).presentation.menuCard
914+
let presentation = ProviderDescriptorRegistry.descriptor(for: input.provider).presentation
915+
let menuCard = presentation.menuCard
915916
let rawCreditsText: String? = if !menuCard.showsCreditsSection ||
916917
!input.showOptionalCreditsAndExtraUsage
917918
{
@@ -932,12 +933,15 @@ extension UsageMenuCardView.Model {
932933
let showsProviderCost = menuCard.showsProviderCost(context: ProviderCostVisibilityContext(
933934
snapshot: input.snapshot,
934935
showOptionalUsage: input.showOptionalCreditsAndExtraUsage))
936+
let providerCostStyle = input.snapshot.map {
937+
presentation.cost(snapshot: $0).menuCardStyle
938+
} ?? .generic
935939
let providerCost: ProviderCostSection? = if !showsProviderCost {
936940
nil
937941
} else {
938942
Self.providerCostSection(
939-
provider: input.provider,
940943
cost: input.snapshot?.providerCost,
944+
style: providerCostStyle,
941945
isClaudeAdminAPI: isClaudeAdminAPI,
942946
preferredCurrencyCode: input.preferredCurrencyCode)
943947
}
@@ -989,10 +993,13 @@ extension UsageMenuCardView.Model {
989993
private static func visibleProviderDetails(input: Input) -> [ProviderDetailSection] {
990994
var details = input.snapshot?.details ?? []
991995
if !input.showOptionalCreditsAndExtraUsage {
992-
if input.provider == .sakana {
996+
let policy = ProviderDescriptorRegistry.descriptor(for: input.provider).presentation.optionalDetails
997+
if policy.hidesAllWithoutOptionalUsage {
993998
details = []
994-
} else if input.provider == .minimax {
995-
details.removeAll { $0.title == "Billing history" }
999+
} else if !policy.hiddenTitlesWithoutOptionalUsage.isEmpty {
1000+
details.removeAll { section in
1001+
section.title.map(policy.hiddenTitlesWithoutOptionalUsage.contains) == true
1002+
}
9961003
}
9971004
}
9981005
guard input.hidePersonalInfo else { return details }

Sources/CodexBar/MenuDescriptor.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,13 @@ struct MenuDescriptor {
390390
usage: mistralUsage,
391391
preferredCurrencyCode: preferredCurrencyCode)
392392
}
393-
if provider != .sakana || showOptionalUsage {
394-
let details = provider == .minimax && !showOptionalUsage
395-
? snapshot.details.filter { $0.title != "Billing history" }
396-
: snapshot.details
393+
let policy = ProviderDescriptorRegistry.descriptor(for: provider).presentation.optionalDetails
394+
if !policy.hidesAllWithoutOptionalUsage || showOptionalUsage {
395+
let details = showOptionalUsage || policy.hiddenTitlesWithoutOptionalUsage.isEmpty
396+
? snapshot.details
397+
: snapshot.details.filter { section in
398+
section.title.map(policy.hiddenTitlesWithoutOptionalUsage.contains) != true
399+
}
397400
for section in details {
398401
for row in section.rows {
399402
let value = [row.value, row.secondaryValue].compactMap(\.self).joined(separator: " · ")

Sources/CodexBar/PreferencesDebugPane.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ struct DebugPane: View {
77
@Bindable var settings: SettingsStore
88
@Bindable var store: UsageStore
99
@AppStorage("debugFileLoggingEnabled") private var debugFileLoggingEnabled = false
10+
// Provider-specific by design: debug probe, fetch, and error pickers historically start on Codex.
1011
@State private var currentLogProvider: UsageProvider = .codex
1112
@State private var currentFetchProvider: UsageProvider = .codex
1213
@State private var isLoadingLog = false
@@ -126,6 +127,7 @@ struct DebugPane: View {
126127
}
127128
.disabled(self.isLoadingLog && self.logText.isEmpty)
128129

130+
// Provider-specific by design: only Claude exposes the raw parser dump diagnostic.
129131
if self.currentLogProvider == .claude {
130132
Button { self.loadClaudeDump() } label: {
131133
Label(L("load_parse_dump"), systemImage: "doc.text.magnifyingglass")

Sources/CodexBar/ProviderRegistry.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ struct ProviderRegistry {
7979
},
8080
providerManualTokenUpdater: { provider, token in
8181
await MainActor.run {
82+
// Provider-specific by design: StepFun rotates its legacy app-owned session token.
8283
if provider == .stepfun {
8384
settings.stepfunToken = token
8485
}
@@ -155,6 +156,7 @@ struct ProviderRegistry {
155156
// quotas, and dashboard data. Token-cost/session history is intentionally handled
156157
// separately because it is provider-level local telemetry from this Mac's Codex sessions,
157158
// not account-owned remote state.
159+
// Provider-specific by design: managed Codex account selection scopes the fetcher's CODEX_HOME.
158160
if provider == .codex {
159161
let codexActiveSource = codexActiveSourceOverride ?? settings.codexResolvedActiveSource
160162
if let managedHomePath = settings.managedCodexRemoteHomePath(forActiveSource: codexActiveSource) {
@@ -169,6 +171,7 @@ struct ProviderRegistry {
169171
}
170172

171173
static func makeFetcher(base: UsageFetcher, provider: UsageProvider, env: [String: String]) -> UsageFetcher {
174+
// Provider-specific by design: a Codex account scope needs a fetcher rebuilt with its selected CODEX_HOME.
172175
guard provider == .codex else { return base }
173176
return UsageFetcher(environment: env)
174177
}

Sources/CodexBar/SettingsStore.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ extension SettingsStore {
410410
config: CodexBarConfig,
411411
hadExistingConfig: Bool) -> Bool
412412
{
413+
// Provider-specific by design: the legacy OpenAI web-access flag was inferred from Codex's cookie config.
413414
guard let codex = config.providerConfig(for: .codex) else { return false }
414415
if let cookieSource = codex.cookieSource {
415416
return cookieSource.isEnabled
@@ -732,6 +733,7 @@ extension SettingsStore {
732733

733734
// Tagged builds through v0.35 used primary=Claude, secondary=Gemini Pro,
734735
// and tertiary=Gemini Flash. Remap those meanings once to the two-pool schema.
736+
// Provider-specific by design: this one-time migration rewrites Antigravity's historical persisted lanes.
735737
var migrated = preferences
736738
switch MenuBarMetricPreference(rawValue: migrated[UsageProvider.antigravity.rawValue] ?? "") {
737739
case .primary:

Sources/CodexBar/StatusItemController+Actions.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ extension StatusItemController: StatusItemMenuPersistentActionDelegate {
468468
}
469469

470470
@objc func openTerminalCommand(_ sender: NSMenuItem) {
471+
// Provider-specific by design: legacy terminal menu items without a command payload open Claude.
471472
let command = sender.representedObject as? String ?? "claude"
472473
self.openTerminal(command: command)
473474
}

0 commit comments

Comments
 (0)