Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions daemon/codex_ratelimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,10 @@ func loadCodexAuth() (*codexAuthData, error) {

// whamUsageResponse maps the response from chatgpt.com/backend-api/wham/usage
type whamUsageResponse struct {
PlanType string `json:"plan_type"`
RateLimit *whamRateLimitCategory `json:"rate_limit"`
CodeReviewRateLimit *whamRateLimitCategory `json:"code_review_rate_limit"`
PlanType string `json:"plan_type"`
RateLimit *whamRateLimitCategory `json:"rate_limit"`
CodeReviewRateLimit *whamRateLimitCategory `json:"code_review_rate_limit"`
AdditionalRateLimits map[string]*whamRateLimitCategory `json:"additional_rate_limits"`
}

type whamRateLimitCategory struct {
Expand Down Expand Up @@ -230,6 +231,18 @@ func fetchCodexUsage(ctx context.Context, auth *codexAuthData) (*CodexRateLimitD
}
}

for name, cat := range usage.AdditionalRateLimits {
if cat == nil {
continue
}
if w := cat.PrimaryWindow; w != nil {
windows = append(windows, mapWhamWindow(name, "primary", w))
}
if w := cat.SecondaryWindow; w != nil {
windows = append(windows, mapWhamWindow(name, "secondary", w))
}
}
Comment on lines +234 to +244
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The iteration over AdditionalRateLimits does not check for potential key collisions with the explicitly handled fields (rate_limit and code_review_rate_limit). If the backend returns these keys within the additional_rate_limits map, it will result in duplicate entries in the windows slice with identical LimitIDs. Consider skipping these keys to ensure uniqueness. Additionally, note that map iteration in Go is non-deterministic, so the order of these additional windows in the slice will vary between calls.

	for name, cat := range usage.AdditionalRateLimits {
		if name == "rate_limit" || name == "code_review_rate_limit" {
			continue
		}
		if cat == nil {
			continue
		}
		if w := cat.PrimaryWindow; w != nil {
			windows = append(windows, mapWhamWindow(name, "primary", w))
		}
		if w := cat.SecondaryWindow; w != nil {
			windows = append(windows, mapWhamWindow(name, "secondary", w))
		}
	}


return &CodexRateLimitData{
Plan: usage.PlanType,
Windows: windows,
Expand Down
Loading