Skip to content

Port upstream 0.66.0: OpenAI Admin pagination, token totals, balance-fallback gate, retry - #645

Open
Finesssee wants to merge 1 commit into
port/upstream-0.66.0from
port/micro-0.66.0-openai-admin-parity
Open

Finesssee wants to merge 1 commit into
port/upstream-0.66.0from
port/micro-0.66.0-openai-admin-parity

Conversation

@Finesssee

Copy link
Copy Markdown
Collaborator

Summary

OpenAI Admin usage (rust/src/providers/openaiapi/mod.rs) now matches upstream v0.66.0 openai.js / native OpenAIAPIUsageFetcher behavior:

  • /v1/organization/costs (group_by=line_item) and /v1/organization/usage/completions (group_by=model) use bucket_width=1d, limit <= 31 and UTC-day-aligned 31-day ranges, following has_more / next_page (100-page cap per range; a missing, blank or repeated cursor is a parse error). A busy org is no longer silently truncated.
  • Token totals are input + input_audio + output + output_audio. Cached input is a subset of input and is no longer added on top (it was double counted in the Tokens row and model ranking).
  • Legacy credit-balance fallback is skipped for a project-scoped Admin key (the endpoint is not project-filtered); unscoped keys and non-Admin keys keep the billing-api label and API balance login method.
  • Each Admin GET gets one transient retry (408/429/500/502/503/504, timeout, refused connection); numeric Retry-After is honored up to 10 s, default 1 s. Auth failures and other transport errors are never retried. Request timeout is 20 s as upstream.
  • Existing tests moved to a sibling tests.rs (mod.rs stays under 1000 lines).

Upstream reference

  • Release 0.66.0 bullet: "OpenAI and Fireworks: use bundled provider plugins while preserving OpenAI usage charts and project labels" (commit a634f55c; native behavior from v0.65.0 OpenAIAPIUsageFetcher.swift).
  • Tag-pinned v0.66.0: Sources/CodexBarCore/Resources/Plugins/openai.js (ranges, pages, queryURL, tokens), Sources/CodexBarCore/Providers/OpenAI/OpenAIAPIProviderDescriptor.swift (OpenAIAPIUsageCredential.allowsLegacyBalanceFallback), tests OpenAIAPIUsageFetcherTests, OpenAIAPIProjectScopeTests.

Ported / Deferred

Ported: pagination, UTC day ranges, token totals, balance-fallback gate, transient retry.

Deferred:

  • OPENAI_HISTORY_DAYS (1-365): there is no Windows setting for it, so the window stays 30 days and the label stays Last 30 days. usage_ranges already takes the day count, so wiring a setting later is a one-line change. Documented in the module header.
  • Per-day usage chart card (card.openAIAPIUsage): separate UI decision, no Windows chart counterpart.
  • Deliberate deviation: the final range end is clamped to now (upstream ends at tomorrow's midnight) because the API rejects a future end_time.

Validation

All on the pinned toolchain cargo +1.98.0, E-core wrappers, slot-3.

  • cargo +1.98.0 fmt --all: clean.
  • cargo +1.98.0 clippy -p codexbar --all-targets -- -D warnings: pass.
  • cargo +1.98.0 test -p codexbar openaiapi -- --test-threads=4: 29 passed, 0 failed (6 pre-existing tests kept, 23 new: pagination cursors, missing/repeated cursor, 100-page cap, long-history ranges, token totals, fallback gate matrix, retry policy, retry once then report status, no retry for auth).
  • Workspace clippy was not run: the codexbar-desktop-tauri build script fails in slot-3 on a stale tauri permissions path from another slot's cache (environmental). This PR does not touch that crate.

Affected areas

  • Provider (rust/src/providers/openaiapi)
  • Tauri shell / frontend / settings / tray / float bar
  • Docs

UI proof

Not applicable (Tokens/Requests row values change; no UI code touched).

@Finesssee

Copy link
Copy Markdown
Collaborator Author

Thermo-nuclear review

Reviewed by Codex gpt-6-luna (xhigh); verified and validated by Claude

Findings (1):

  • Medium, rust/src/providers/openaiapi/mod.rs:287: costs and completions pages are fetched sequentially although independent. Codex suggested tokio::try_join!.

Disposition: not applied. Running both paginated fetches concurrently doubles the request burst against the rate-limited Admin API, which this PR already guards with transient retry; the latency gain is marginal for a background refresh. No other defects found against the 0.66.0 openai-admin-parity spec. No code changes needed; no commit pushed.

@Finesssee

Copy link
Copy Markdown
Collaborator Author

Validated 2026-10-02 at 44dada5 (PR head):

  • Reviewed against the 0.66.0.md PR 13 spec (OpenAI Admin pagination, token totals, balance-fallback gate, transient retry): the diff matches — /v1/organization/costs (group_by=line_item) and /v1/organization/usage/completions (group_by=model) paginate with bucket_width=1d, limit <= 31, UTC-day-aligned 31-day ranges, has_more/next_page cursor following with a 100-page cap and missing/repeated-cursor errors; token totals are input + input_audio + output + output_audio with cached input tracked separately (never added); balance fallback only when the key is not project-scoped Admin (project_id.is_none() || !is_admin), keeping the billing-api label; one transient retry for Admin GETs (408/429/500/502/503/504 + timeout) with Retry-After capped at 10 s, base 1 s, never on 401/403. The prior thermo review's Medium finding (sequential costs/completions fetch) was dispositioned as not applied with rationale — consistent with upstream behavior. No defects found; no changes needed.
  • cargo fmt --all --check — clean.
  • cargo test -p codexbar — 2183 passed / 0 failed / 1 ignored (openai focused 62/62, incl. pagination-cursor, project-scope, retry, and fallback gates).
  • cargo test -p codexbar-desktop-tauri — 461 passed / 1 failed = the documented Isolate bootstrap payload test from real settings #684 bootstrap env baseline (79 vs 78; hermetic fix Make the bootstrap catalog test hermetic (#684) #711 not on this base; also fails on unmodified base).
  • cargo clippy --all-targets -- -D warnings — only the three documented pre-existing main-history lint sites (alibabatokenplan/kiro/openai-subscription, rustc 1.96 drift, all outside this diff; QUEUE INTEGRATION FINDINGS).
  • No push needed (head 44dada5 already on origin); no fixes required.

Chain note: base for #699 (per-day usage core) and #701 (chart UI). Proceeding to #699's CI diagnosis next.

Finesssee added a commit that referenced this pull request Oct 2, 2026
…re and bridge (stacked on #645; includes the #721 midnight-flake fixture cherry-pick)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant