Skip to content

perf: background credits and dashboard fetch for regular refreshes#1078

Merged
steipete merged 7 commits into
steipete:mainfrom
ptstory:perf/background-codex-refresh
May 22, 2026
Merged

perf: background credits and dashboard fetch for regular refreshes#1078
steipete merged 7 commits into
steipete:mainfrom
ptstory:perf/background-codex-refresh

Conversation

@ptstory
Copy link
Copy Markdown
Contributor

@ptstory ptstory commented May 21, 2026

Summary

Background regular Codex web refreshes so the UI never blocks on slow network operations during the polling cycle. Add credits-refresh coalescing so repeated poll cycles cannot stack concurrent credits fetches.

Split from #1073 per review feedback. Related: #678.

What changed

For non-forced refreshes, refreshCreditsIfNeeded() and refreshOpenAIDashboardIfNeeded() now run in background tasks instead of blocking the regular refresh path. Credits refresh now uses a Task<Void, Never>? guard so a second poll skips starting another credits fetch while one is already in flight.

Why

The regular refresh cycle was awaiting Codex credits and OpenAI dashboard web work inline, which could block the main thread for seconds during slow network scrapes. It also allowed repeated poll cycles to stack concurrent credits fetches without any coalescing.

How to verify

swift test --filter CodexManagedOpenAIWebRefreshTests
./Scripts/lint.sh format
./Scripts/lint.sh lint
make check

Notable decisions

  • Forced refreshes still await inline so manual refresh behavior stays synchronous.
  • Credits coalescing uses a Task? reference instead of a boolean so the in-flight state is derived from the task itself.
  • Tradeoff: these non-forced refresh background tasks are unstructured and are not cancelled if the parent refresh is cancelled. Acceptable for a menu bar app on a 2-minute poll cycle.

Not included

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 55945600c8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread Sources/CodexBar/UsageStore.swift
Comment thread Sources/CodexBar/UsageStore.swift Outdated
@clawsweeper
Copy link
Copy Markdown

clawsweeper Bot commented May 22, 2026

Codex review: needs real behavior proof before merge.

Latest ClawSweeper review: 2026-05-22 19:42 UTC / May 22, 2026, 3:42 PM ET.

Workflow note: Future ClawSweeper reviews update this same comment in place.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

Summary
The branch backgrounds non-forced Codex credits and OpenAI dashboard refreshes, coalesces account-scoped refresh tasks, persists widget snapshots after background completion, and adds focused tests plus a changelog entry.

Reproducibility: yes. from source inspection: current main awaits credits in the refresh task group and awaits OpenAI dashboard refresh inline. I did not run tests because this review is read-only, but the PR adds blocking-loader tests that exercise regular refresh returning before those loaders complete.

PR rating
Overall: 🧂 unranked krab
Proof: 🧂 unranked krab
Patch quality: 🐚 platinum hermit
Summary: The implementation signal is solid and test-focused, but overall readiness is capped because required real behavior proof is missing.

Rank-up moves:

  • Add redacted runtime proof to the PR body showing a slow regular refresh returns promptly and later updates credits/dashboard/widget state.
  • Include diagnostic output or logs that make the background completion and active-account guard visible, with private account/session details redacted.
  • After updating the PR body, let ClawSweeper re-review automatically or ask a maintainer to comment @clawsweeper re-review if it does not.
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Real behavior proof
Needs real behavior proof before merge: The PR body and comments provide test/lint commands but no after-fix real behavior proof; add redacted logs, terminal output, screenshots, a recording, or another linked artifact before merge. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Risk before merge

  • Regular refresh can now return before credits, dashboard, and widget fields update, so reviewers still need runtime proof that later background completion updates only the active account state.
  • The new background credits/dashboard tasks intentionally outlive the parent refresh; token/key guards and tests reduce stale-state risk, but the external PR has not shown this path in a running app or redacted logs.

Maintainer options:

  1. Require runtime account-scoped refresh proof (recommended)
    Ask for redacted terminal output, logs, or a short recording showing a slow regular refresh returning promptly and the later background completion updating the active account and widget state.
  2. Accept test-only validation
    Maintainers can merge based on the focused coalescing, cancellation, and widget-persistence tests while explicitly owning the remaining runtime timing risk.
  3. Pause for a structured scheduler
    If unstructured background refresh lifetime is not acceptable for this app, pause this PR and ask for a scheduler/lifetime design before continuing.

Next step before merge
Human review should wait for contributor-supplied runtime proof; I did not find a narrow code defect for repair automation.

Security
Cleared: Cleared: the diff changes existing cookie-backed refresh scheduling/cancellation and tests but adds no dependencies, CI changes, downloads, permissions, or broader cookie access.

Review details

Best possible solution:

Merge the scoped background-refresh implementation after the PR body includes redacted real behavior proof that a slow regular refresh returns promptly and later updates the active account’s credits, dashboard, and widget state.

Do we have a high-confidence way to reproduce the issue?

Yes, from source inspection: current main awaits credits in the refresh task group and awaits OpenAI dashboard refresh inline. I did not run tests because this review is read-only, but the PR adds blocking-loader tests that exercise regular refresh returning before those loaders complete.

Is this the best way to solve the issue?

Likely yes: the branch keeps forced refreshes synchronous, adds account-key coalescing/cancellation guards, and persists widget snapshots after background completion. The remaining blocker is runtime proof, not a clear code defect.

Label justifications:

  • P2: This is a normal-priority performance and responsiveness fix with limited blast radius but real account-state timing risk.
  • merge-risk: 🚨 session-state: The PR changes when account-scoped credits, dashboard, and widget state are applied relative to the parent refresh cycle.
  • rating: 🧂 unranked krab: Current PR rating is 🧂 unranked krab because proof is 🧂 unranked krab, patch quality is 🐚 platinum hermit, and The implementation signal is solid and test-focused, but overall readiness is capped because required real behavior proof is missing.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body and comments provide test/lint commands but no after-fix real behavior proof; add redacted logs, terminal output, screenshots, a recording, or another linked artifact before merge. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Acceptance criteria:

  • swift test --filter CodexManagedOpenAIWebRefreshTests
  • swift test --filter CodexBackgroundRefreshCoalescingTests
  • ./Scripts/lint.sh format
  • ./Scripts/lint.sh lint
  • make check

What I checked:

  • Current main still blocks regular refresh on credits and dashboard work: On current main, refresh() adds refreshCreditsIfNeeded to the awaited task group and then awaits refreshOpenAIDashboardIfNeeded inline when OpenAI web refresh is allowed. (Sources/CodexBar/UsageStore.swift:557, de7c25ba2ef0)
  • PR moves regular credits refresh to a scheduled background task: The PR head adds scheduleCreditsRefreshIfNeeded, account-key coalescing, cancellation for forced refreshes, and widget snapshot persistence after background credits completion. (Sources/CodexBar/Providers/Codex/UsageStore+CodexRefresh.swift:16, 68895e19006a)
  • PR moves regular dashboard refresh to a scheduled background task: The PR head adds scheduleOpenAIDashboardRefreshIfNeeded, background dashboard task tracking, token/cancellation checks, and widget snapshot persistence after dashboard completion. (Sources/CodexBar/UsageStore+OpenAIWeb.swift:421, 68895e19006a)
  • Focused test coverage exists on the PR branch: The PR adds tests for nonblocking regular refreshes, credits/dashboard coalescing, account-switch rescheduling, forced credits cancellation, cancelled import side effects, and widget snapshot persistence. (Tests/CodexBarTests/CodexBackgroundRefreshCoalescingTests.swift:10, 68895e19006a)
  • No real behavior proof in PR body or comments: The PR body lists swift test, lint, and make check commands, and the existing ClawSweeper comment still asks for after-fix runtime proof rather than tests only.
  • History routes this area to recent refresh/OpenAI web owners: History for the central files shows Peter Steinberger as the most recent high-volume current-main contributor and Ratul Sarna as the main OpenAI web/account-guard feature-history contributor.

Likely related people:

  • steipete: Current-main blame/log point to Peter Steinberger for the recent refresh/OpenAI web surface import and release baseline, and the PR branch includes follow-up fixes for scoped coalescing and cancellation. (role: recent area contributor and branch repair author; confidence: high; commits: f61469c3cde6, 9857e1de0bae, a9a8d1ddbc7d; files: Sources/CodexBar/UsageStore.swift, Sources/CodexBar/UsageStore+OpenAIWeb.swift, Tests/CodexBarTests/CodexManagedOpenAIWebRefreshTests.swift)
  • ratulsarna: Ratul Sarna authored multiple prior OpenAI web, battery, Codex account guard, and dashboard authority commits in the same refresh/account-scoping path. (role: feature-history owner; confidence: high; commits: 0199e1ca9a00, 3ab4dd8fb843, 0f86a0135084; files: Sources/CodexBar/UsageStore+OpenAIWeb.swift, Sources/CodexBar/Providers/Codex/UsageStore+CodexRefresh.swift, Sources/CodexBar/Providers/Codex/UsageStore+CodexAccountState.swift)
  • xiaoqianWX: Yubo Wang/xiaoqianWX has recent current-main history in OpenAI dashboard behavior, which is adjacent to this refresh scheduling change. (role: adjacent dashboard contributor; confidence: medium; commits: b183b275ee3c, cbd59562addc; files: Sources/CodexBar/UsageStore+OpenAIWeb.swift)

Codex review notes: model gpt-5.5, reasoning high; reviewed against de7c25ba2ef0.

ptstory and others added 5 commits May 22, 2026 19:40
Move refreshCreditsIfNeeded() and refreshOpenAIDashboardIfNeeded() into background tasks for non-forced refreshes. Add Task?-based coalescing guard for credits refresh to prevent unbounded stacking.

Forced refreshes (manual) still await inline.

Split from steipete#1073.
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 27edc3b56e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

return
}

self.openAIDashboardBackgroundRefreshTask?.cancel()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Cancel active dashboard fetch when replacing background refresh

scheduleOpenAIDashboardRefreshIfNeeded only cancels openAIDashboardBackgroundRefreshTask, but the actual scrape is executed by refreshOpenAIDashboardIfNeeded, which spawns/stores a separate openAIDashboardRefreshTask (Task { ... } + await task.value). When a new key is scheduled (for example during account switches), the old scrape can keep running concurrently even after this cancellation, defeating the coalescing intent and allowing stale-account web work to continue in parallel.

Useful? React with 👍 / 👎.

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels May 22, 2026
@clawsweeper
Copy link
Copy Markdown

clawsweeper Bot commented May 22, 2026

ClawSweeper PR egg

🎁 Pass real behavior proof to wake the egg and unlock a hatchable treat.

Where did the egg go?
  • The egg game starts only after the PR passes the real-behavior proof check.
  • Before that, no creature or rarity is rolled. The treat waits for real proof.
  • This is still just collectible flavor: proof affects review readiness, not creature quality.

@steipete steipete force-pushed the perf/background-codex-refresh branch from 27edc3b to a9a8d1d Compare May 22, 2026 19:08
@clawsweeper clawsweeper Bot added P2 Normal priority bug or improvement with limited blast radius. merge-risk: 🚨 session-state 🚨 Merging this PR could lose, corrupt, stale, or mis-associate session or agent state. labels May 22, 2026
@steipete steipete merged commit c2b63a0 into steipete:main May 22, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-risk: 🚨 session-state 🚨 Merging this PR could lose, corrupt, stale, or mis-associate session or agent state. P2 Normal priority bug or improvement with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants