Repository navigation
Add one-shot dashboard snapshot command #2499
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
Merged
steipete
merged 6 commits into
steipete:main
from
umutkeltek:agent/dashboard-snapshot-cli
Aug 3, 2026
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
55eb99c
Add one-shot dashboard snapshot command
umutkeltek 0e00869
Fix dashboard pricing refresh policy
umutkeltek ad37578
Honor Cursor source policy in dashboard
umutkeltek 430bb4d
Merge branch 'main' into agent/dashboard-snapshot-cli
steipete 5133b25
test: dedupe cliExecutableURL helper after branch update
978ce6e
chore: merge main into dashboard snapshot CLI
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| import CodexBarCore | ||
| import Commander | ||
| import Foundation | ||
|
|
||
| struct DashboardOptions: CommanderParsable { | ||
| @Flag(names: [.short("v"), .long("verbose")], help: "Enable verbose logging") | ||
| var verbose: Bool = false | ||
|
|
||
| @Flag(name: .long("json-output"), help: "Emit machine-readable logs") | ||
| var jsonOutput: Bool = false | ||
|
|
||
| @Option(name: .long("log-level"), help: "Set log level (trace|verbose|debug|info|warning|error|critical)") | ||
| var logLevel: String? | ||
|
|
||
| @Flag(name: .long("pretty"), help: "Pretty-print JSON output") | ||
| var pretty: Bool = false | ||
|
|
||
| @Option( | ||
| name: .long("timeout"), | ||
| help: "Overall fetch timeout in seconds, 0...86400 (default 30; 0 disables)") | ||
| var timeout: Double? | ||
| } | ||
|
|
||
| struct DashboardSnapshotResult { | ||
| let payload: DashboardSnapshotPayload | ||
| let usageCacheKeys: [String?] | ||
| } | ||
|
|
||
| /// Collects the stable dashboard-v1 payload independently of its transport. | ||
| /// The CLI command encodes it directly while `codexbar serve` wraps it in the | ||
| /// existing authenticated HTTP cache. | ||
| struct DashboardSnapshotProducer: Sendable { | ||
| let collectUsage: @Sendable ([UsageProvider]) async throws -> UsageCommandOutput | ||
| let collectCost: @Sendable ([UsageProvider]) async -> [CostPayload] | ||
| let now: @Sendable () -> Date | ||
|
|
||
| func collect( | ||
| config: CodexBarConfig, | ||
| refreshInterval: TimeInterval, | ||
| codexBarVersion: String?) async throws -> DashboardSnapshotResult | ||
| { | ||
| let selection = CodexBarCLI.providerSelection( | ||
| rawOverride: nil, | ||
| enabled: config.enabledProviders()) | ||
| let usageOutput = try await self.collectUsage(selection.asList) | ||
| let costPayloads = await self.collectCost(CodexBarCLI.costProviders(from: selection)) | ||
|
|
||
| let payload = DashboardSnapshotBuilder.makeSnapshot( | ||
| usagePayloads: usageOutput.payload, | ||
| costPayloads: costPayloads, | ||
| config: config, | ||
| identityMode: .redacted, | ||
| generatedAt: self.now(), | ||
| refreshInterval: refreshInterval, | ||
| codexBarVersion: codexBarVersion) | ||
| return DashboardSnapshotResult( | ||
| payload: payload, | ||
| usageCacheKeys: usageOutput.payload.map(\.cacheAccountKey)) | ||
| } | ||
|
|
||
| static func live(context: DashboardSnapshotContext) -> Self { | ||
| Self( | ||
| collectUsage: { providers in | ||
| try await CodexBarCLI.serveUsageOutput( | ||
| selection: .custom(providers), | ||
| context: context.usage) | ||
| }, | ||
| collectCost: { providers in | ||
| let costFetcher = CostUsageFetcher() | ||
| return await CodexBarCLI.serveCollectCostPayloads( | ||
| providers: providers, | ||
| context: context.costCollection) | ||
| { provider in | ||
| do { | ||
| let snapshot = try await costFetcher.loadTokenSnapshot( | ||
| provider: provider, | ||
| forceRefresh: false, | ||
| refreshPricingInBackground: CodexBarCLI.serveCostRefreshesPricingInBackground) | ||
| return CodexBarCLI.makeCostPayload(provider: provider, snapshot: snapshot, error: nil) | ||
| } catch { | ||
| return CodexBarCLI.makeCostPayload(provider: provider, snapshot: nil, error: error) | ||
| } | ||
| } | ||
| }, | ||
| now: { Date() }) | ||
| } | ||
| } | ||
|
|
||
| extension CodexBarCLI { | ||
| static func runDashboard(_ values: ParsedValues) async { | ||
| guard let timeout = decodeDashboardTimeout(from: values) else { | ||
| exit( | ||
| code: .failure, | ||
| message: "--timeout must be a finite number of seconds from 0 through 86400.", | ||
| kind: .args) | ||
| } | ||
|
|
||
| let configSnapshot: CLIServeConfigSnapshot | ||
| do { | ||
| configSnapshot = try Self.loadServeConfigSnapshot() | ||
| } catch { | ||
| Self.exit(code: .failure, message: error.localizedDescription, kind: .config) | ||
| } | ||
|
|
||
| let providerOperations = CLIServeOperationCoordinator<UsageCommandOutput>() | ||
| let costOperations = CLIServeOperationCoordinator<CostPayload>() | ||
| let signalMonitor = CLITerminationSignalMonitor { signalNumber in | ||
| CLITerminationSignalMonitor.terminateActiveHelpersAndReraise(signalNumber) | ||
| } | ||
| defer { signalMonitor.cancel() } | ||
|
|
||
| let startedAt = ContinuousClock().now | ||
| let providerTimeout = Self.serveProviderTimeout(requestTimeout: timeout) | ||
| let context = DashboardSnapshotContext( | ||
| config: configSnapshot.config, | ||
| usage: ServeUsageContext( | ||
| config: configSnapshot.config, | ||
| configFingerprint: configSnapshot.cacheToken, | ||
| refreshInterval: 0, | ||
| providerTimeout: providerTimeout, | ||
| providerDeadline: Self.serveProviderDeadline( | ||
| startedAt: startedAt, | ||
| requestTimeout: timeout), | ||
| providerOperations: providerOperations, | ||
| includeAllCodexAccounts: false, | ||
| persistCLISessions: false), | ||
| costCollection: ServeCostCollectionContext( | ||
| configFingerprint: configSnapshot.cacheToken, | ||
| providerTimeout: providerTimeout, | ||
| requestDeadline: Self.serveRequestDeadline( | ||
| startedAt: startedAt, | ||
| requestTimeout: timeout), | ||
| now: { ContinuousClock().now }, | ||
| providerOperations: costOperations), | ||
| codexBarVersion: Self.currentVersion()) | ||
|
|
||
| let result: DashboardSnapshotResult | ||
| do { | ||
| result = try await DashboardSnapshotProducer.live(context: context).collect( | ||
| config: context.config, | ||
| refreshInterval: context.usage.refreshInterval, | ||
| codexBarVersion: context.codexBarVersion) | ||
| } catch { | ||
| await Self.shutdownDashboardRuntime( | ||
| providerOperations: providerOperations, | ||
| costOperations: costOperations) | ||
| Self.exit(code: .failure, message: error.localizedDescription) | ||
| } | ||
|
|
||
| await Self.shutdownDashboardRuntime( | ||
| providerOperations: providerOperations, | ||
| costOperations: costOperations) | ||
|
|
||
| guard let json = Self.encodeJSON(result.payload, pretty: values.flags.contains("pretty")) else { | ||
| Self.exit(code: .failure, message: "Could not encode dashboard snapshot.") | ||
| } | ||
| print(json) | ||
| } | ||
|
|
||
| static func decodeDashboardTimeout(from values: ParsedValues) -> TimeInterval? { | ||
| let raw = values.options["timeout"]?.last ?? String(Int(Self.defaultServeRequestTimeout)) | ||
| guard let timeout = TimeInterval(raw), | ||
| timeout.isFinite, | ||
| timeout >= 0, | ||
| timeout <= 86400 | ||
| else { | ||
| return nil | ||
| } | ||
| return timeout | ||
| } | ||
|
|
||
| private static func shutdownDashboardRuntime( | ||
| providerOperations: CLIServeOperationCoordinator<UsageCommandOutput>, | ||
| costOperations: CLIServeOperationCoordinator<CostPayload>) async | ||
| { | ||
| await providerOperations.shutdown() | ||
| await costOperations.shutdown() | ||
| await ProviderCLISessionLifecycle.shutdownPersistentSessions() | ||
| TTYCommandRunner.terminateActiveProcessesForAppShutdown() | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.