-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add a read-only OpenCodex usage.jsonl parser and independent cache #3018
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
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
39b17af
Add spend provenance, pinned day timezone, and Codex fork fail-closed.
Yuxin-Qiao 0d7af47
Fix spend cache timezone, provenance, and windowed metered totals.
Yuxin-Qiao 2988d0b
Add custom-pricing overlay above models.dev and builtin rates.
Yuxin-Qiao 4f8fba1
Apply custom-pricing overlay to aggregate fallbacks once per scan.
Yuxin-Qiao 03417c6
Refresh the Codex parser hash after overlay pricing changes.
Yuxin-Qiao 6c79095
Split overlay cost helpers so CostUsagePricing stays under the lint l…
Yuxin-Qiao 725419a
Keep overlay cost helper within the parameter-count lint limit.
Yuxin-Qiao 197bd61
Add a read-only OpenCodex usage.jsonl parser and independent cache.
Yuxin-Qiao a532d41
Keep OpenCodex session totals and coverage on the current day.
Yuxin-Qiao 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
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
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
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,185 @@ | ||
| import Foundation | ||
|
|
||
| /// How a cost figure was produced. This is display-time accounting, not a billing receipt. | ||
| public enum CostProvenance: String, Sendable, Equatable, Codable { | ||
| /// Token counts × public API list prices. | ||
| case listPriceEstimate | ||
| /// Vendor-reported metered spend (for example Cursor plan deductions). | ||
| case vendorMetered | ||
| /// Window mixes list-price rows with vendor-metered rows. | ||
| case mixed | ||
| case unknown | ||
|
|
||
| public var isBillingReceipt: Bool { | ||
| false | ||
| } | ||
|
|
||
| /// Narrow a snapshot-level provenance to the costs actually present in a window. | ||
| /// Daily vendor-reported rows stay vendor-metered even when `meteredCostUSD` is absent. | ||
| public static func forWindow( | ||
| snapshot: CostProvenance, | ||
| hasWindowCosts: Bool, | ||
| includesMetered: Bool) -> CostProvenance | ||
| { | ||
| switch snapshot { | ||
| case .vendorMetered: | ||
| includesMetered || hasWindowCosts ? .vendorMetered : .unknown | ||
| case .mixed: | ||
| switch (includesMetered, hasWindowCosts) { | ||
| case (true, true): | ||
| .mixed | ||
| case (true, false): | ||
| .vendorMetered | ||
| case (false, true): | ||
| .listPriceEstimate | ||
| case (false, false): | ||
| .unknown | ||
| } | ||
| case .listPriceEstimate: | ||
| hasWindowCosts ? .listPriceEstimate : .unknown | ||
| case .unknown: | ||
| .unknown | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Request/row coverage for a cost window. Counts stay independent so a missing | ||
| /// category is `0` rather than collapsing into another bucket. | ||
| public struct CostUsageCoverageCounts: Sendable, Equatable, Codable { | ||
| public var priced: Int | ||
| public var unpriced: Int | ||
| public var unmetered: Int | ||
| public var estimated: Int | ||
|
|
||
| public init(priced: Int = 0, unpriced: Int = 0, unmetered: Int = 0, estimated: Int = 0) { | ||
| self.priced = max(0, priced) | ||
| self.unpriced = max(0, unpriced) | ||
| self.unmetered = max(0, unmetered) | ||
| self.estimated = max(0, estimated) | ||
| } | ||
|
|
||
| public var total: Int { | ||
| self.priced + self.unpriced + self.unmetered + self.estimated | ||
| } | ||
|
|
||
| public var coverageRatio: Double? { | ||
| let measured = self.priced + self.estimated | ||
| let denominator = self.total | ||
| guard denominator > 0 else { return nil } | ||
| return Double(measured) / Double(denominator) | ||
| } | ||
|
|
||
| public mutating func merge(_ other: CostUsageCoverageCounts) { | ||
| self.priced += other.priced | ||
| self.unpriced += other.unpriced | ||
| self.unmetered += other.unmetered | ||
| self.estimated += other.estimated | ||
| } | ||
|
|
||
| public static func + (lhs: Self, rhs: Self) -> Self { | ||
| var merged = lhs | ||
| merged.merge(rhs) | ||
| return merged | ||
| } | ||
| } | ||
|
|
||
| /// Token-class mix. `nil` means the source did not establish that class — never treat as zero. | ||
| public struct CostUsageTokenMix: Sendable, Equatable, Codable { | ||
| public var inputTokens: Int? | ||
| public var outputTokens: Int? | ||
| public var cacheReadTokens: Int? | ||
| public var cacheCreationTokens: Int? | ||
| public var reasoningTokens: Int? | ||
|
|
||
| public init( | ||
| inputTokens: Int? = nil, | ||
| outputTokens: Int? = nil, | ||
| cacheReadTokens: Int? = nil, | ||
| cacheCreationTokens: Int? = nil, | ||
| reasoningTokens: Int? = nil) | ||
| { | ||
| self.inputTokens = Self.nonnegative(inputTokens) | ||
| self.outputTokens = Self.nonnegative(outputTokens) | ||
| self.cacheReadTokens = Self.nonnegative(cacheReadTokens) | ||
| self.cacheCreationTokens = Self.nonnegative(cacheCreationTokens) | ||
| self.reasoningTokens = Self.nonnegative(reasoningTokens) | ||
| } | ||
|
|
||
| public var hasAnyClass: Bool { | ||
| self.inputTokens != nil | ||
| || self.outputTokens != nil | ||
| || self.cacheReadTokens != nil | ||
| || self.cacheCreationTokens != nil | ||
| || self.reasoningTokens != nil | ||
| } | ||
|
|
||
| public mutating func merge(_ other: CostUsageTokenMix) { | ||
| self.inputTokens = Self.add(self.inputTokens, other.inputTokens) | ||
| self.outputTokens = Self.add(self.outputTokens, other.outputTokens) | ||
| self.cacheReadTokens = Self.add(self.cacheReadTokens, other.cacheReadTokens) | ||
| self.cacheCreationTokens = Self.add(self.cacheCreationTokens, other.cacheCreationTokens) | ||
| self.reasoningTokens = Self.add(self.reasoningTokens, other.reasoningTokens) | ||
| } | ||
|
|
||
| public static func + (lhs: Self, rhs: Self) -> Self { | ||
| var merged = lhs | ||
| merged.merge(rhs) | ||
| return merged | ||
| } | ||
|
|
||
| public static func from(entry: CostUsageDailyReport.Entry) -> Self { | ||
| Self( | ||
| inputTokens: entry.inputTokens, | ||
| outputTokens: entry.outputTokens, | ||
| cacheReadTokens: entry.cacheReadTokens, | ||
| cacheCreationTokens: entry.cacheCreationTokens, | ||
| reasoningTokens: entry.reasoningTokens) | ||
| } | ||
|
|
||
| private static func nonnegative(_ value: Int?) -> Int? { | ||
| guard let value, value >= 0 else { return nil } | ||
| return value | ||
| } | ||
|
|
||
| private static func add(_ lhs: Int?, _ rhs: Int?) -> Int? { | ||
| switch (lhs, rhs) { | ||
| case let (left?, right?): | ||
| let (result, overflow) = left.addingReportingOverflow(right) | ||
| return overflow ? nil : result | ||
| case let (left?, nil): | ||
| return left | ||
| case let (nil, right?): | ||
| return right | ||
| case (nil, nil): | ||
| return nil | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Pinned IANA timezone used to bucket cost-usage days. Re-bucketing the same history | ||
| /// under a different zone would move midnight-adjacent events and inflate totals. | ||
| public enum CostUsageBucketTimeZone: Sendable { | ||
| public static func calendar(identifier: String?) -> Calendar { | ||
| var calendar = Calendar(identifier: .gregorian) | ||
| calendar.timeZone = self.timeZone(identifier: identifier) | ||
| return calendar | ||
| } | ||
|
|
||
| public static func timeZone(identifier: String?) -> TimeZone { | ||
| if let identifier { | ||
| let trimmed = identifier.trimmingCharacters(in: .whitespacesAndNewlines) | ||
| if !trimmed.isEmpty, let zone = TimeZone(identifier: trimmed) { | ||
| return zone | ||
| } | ||
| } | ||
| return .current | ||
| } | ||
|
|
||
| public static func pinIdentifier(from timeZone: TimeZone = .current) -> String { | ||
| timeZone.identifier | ||
| } | ||
|
|
||
| public static func isValidIdentifier(_ identifier: String) -> Bool { | ||
| TimeZone(identifier: identifier) != nil | ||
| } | ||
| } |
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.
The pinned calendar is passed only to this foreground refresh. The startup hydration path still calls
loadCachedCodexTokenSnapshotResulton the default fetcher, and the Spend Dashboard constructsCostUsageFetcher(cacheRoot:)without a calendar, so after the system timezone changes those paths bucket the same cache using.currentwhile this path uses the pinned zone. This can initially publish different day totals and lets dashboard scans re-bucket history; propagate the pinned calendar through the shared fetcher and cached/dashboard loaders as well.Useful? React with 👍 / 👎.