Repository navigation
Fix Codex cost attribution for long turn contexts #1014
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 3 commits into
steipete:main
from
hhh2210:codex/fix-codex-long-turn-model
May 18, 2026
Merged
Changes from all commits
Commits
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
121 changes: 121 additions & 0 deletions
121
Sources/CodexBarCore/Vendored/CostUsage/CostUsageScanner+CodexTruncatedPrefix.swift
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,121 @@ | ||
| import Foundation | ||
|
|
||
| extension CostUsageScanner { | ||
| static func extractCodexTurnContextModel(from bytes: Data) -> String? { | ||
| guard let text = truncatedUTF8String(from: bytes) else { return nil } | ||
| let object = text[...] | ||
| guard Self.extractJSONStringField("type", from: object, atDepth: 1) == "turn_context", | ||
| let payloadText = Self.extractJSONObjectField("payload", from: object, atDepth: 1) | ||
| else { return nil } | ||
|
|
||
| let payloadModel = Self.extractJSONStringField("model", from: payloadText, atDepth: 1) | ||
| ?? Self.extractJSONStringField("model_name", from: payloadText, atDepth: 1) | ||
| if let payloadModel { return payloadModel } | ||
|
|
||
| guard let infoText = Self.extractJSONObjectField("info", from: payloadText, atDepth: 1) else { return nil } | ||
| return Self.extractJSONStringField("model", from: infoText, atDepth: 1) | ||
| ?? Self.extractJSONStringField("model_name", from: infoText, atDepth: 1) | ||
| } | ||
|
|
||
| private static func truncatedUTF8String(from bytes: Data) -> String? { | ||
| for dropCount in 0...min(4, bytes.count) { | ||
| let end = bytes.count - dropCount | ||
| if let text = String(bytes: bytes.prefix(end), encoding: .utf8) { | ||
| return text | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| private static func extractJSONStringField( | ||
| _ field: String, | ||
| from text: Substring, | ||
| atDepth targetDepth: Int) -> String? | ||
| { | ||
| self.extractJSONField(field, from: text, atDepth: targetDepth) { text, index in | ||
| guard index < text.endIndex, text[index] == "\"" else { return nil } | ||
| let value = Self.parseJSONString(in: text, index: &index) | ||
| return value?.isEmpty == true ? nil : value | ||
| } | ||
| } | ||
|
|
||
| private static func extractJSONObjectField( | ||
| _ field: String, | ||
| from text: Substring, | ||
| atDepth targetDepth: Int) -> Substring? | ||
| { | ||
| self.extractJSONField(field, from: text, atDepth: targetDepth) { text, index in | ||
| guard index < text.endIndex, text[index] == "{" else { return nil } | ||
| return text[index...] | ||
| } | ||
| } | ||
|
|
||
| private static func extractJSONField<T>( | ||
| _ field: String, | ||
| from text: Substring, | ||
| atDepth targetDepth: Int, | ||
| parseValue: (Substring, inout String.Index) -> T?) -> T? | ||
| { | ||
| var index = text.startIndex | ||
| var depth = 0 | ||
|
|
||
| while index < text.endIndex { | ||
| let character = text[index] | ||
| if character == "{" { | ||
| depth += 1 | ||
| text.formIndex(after: &index) | ||
| } else if character == "}" { | ||
| depth -= 1 | ||
| text.formIndex(after: &index) | ||
| } else if character == "\"" { | ||
| var valueIndex = index | ||
| guard let key = Self.parseJSONString(in: text, index: &valueIndex) else { return nil } | ||
| defer { index = valueIndex } | ||
| guard depth == targetDepth, key == field else { continue } | ||
|
|
||
| Self.skipJSONWhitespace(in: text, index: &valueIndex) | ||
| guard valueIndex < text.endIndex, text[valueIndex] == ":" else { continue } | ||
|
|
||
| text.formIndex(after: &valueIndex) | ||
| Self.skipJSONWhitespace(in: text, index: &valueIndex) | ||
| if let value = parseValue(text, &valueIndex) { | ||
| return value | ||
| } | ||
| } else { | ||
| text.formIndex(after: &index) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| private static func parseJSONString(in text: Substring, index: inout String.Index) -> String? { | ||
| guard index < text.endIndex, text[index] == "\"" else { return nil } | ||
| text.formIndex(after: &index) | ||
| var value = "" | ||
| var isEscaped = false | ||
| while index < text.endIndex { | ||
| let character = text[index] | ||
| if isEscaped { | ||
| value.append(character) | ||
| isEscaped = false | ||
| } else if character == "\\" { | ||
| isEscaped = true | ||
| } else if character == "\"" { | ||
| text.formIndex(after: &index) | ||
| return value | ||
| } else { | ||
| value.append(character) | ||
| } | ||
| text.formIndex(after: &index) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| private static func skipJSONWhitespace(in text: Substring, index: inout String.Index) { | ||
| while index < text.endIndex, text[index].isWhitespace { | ||
| text.formIndex(after: &index) | ||
| } | ||
| } | ||
| } |
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.
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.
When a truncated
turn_contextline setscurrentModel, latertoken_countevents that include their own model can still be attributed to the stale context model becauseparseCodexFileresolves model ascurrentModel ?? modelFromInfo. This became a practical regression with this change because oversized contexts now updatecurrentModelmore often; if Codex emits a differing explicit model ontoken_count(for example after a model switch), usage will be booked to the wrong model. Keep the recovered context as fallback only, and let explicit event-level model win.Useful? React with 👍 / 👎.