Files
Luís Miguel 7a703369f4 Add optional Copilot budget extras (#1273)
* Add Copilot budget extras

- Import optional GitHub web budget windows for Copilot
- Add settings for budget extras, cookies, and icon selection
- Cover the new resolver, fetcher, and settings persistence

* Fix Copilot budget cookie imports

- Default Copilot budget cookie import to Chrome only

- Surface budget fetch and decode failures consistently

- Cover cookie defaults and invalid budget JSON

* Fix Copilot budget cookie routing

- Ignore stale manual cookies when budget cookies are auto
- Cover Copilot budget cookie source routing

* Fix Copilot budget review issues

- Default budget extras off unless explicitly enabled
- Harden budget web fetch and manual cookie refresh
- Document budget cookie and reset heuristics

* Fix Copilot budget settings preview

- Forward the budget extras setting into provider previews

- Cover Copilot preview budget visibility

* Add helper for Copilot budget snapshot copies

- Centralize UsageSnapshot extra-window copying
- Clear Copilot reset baselines consistently
- Remove dead Copilot budget overloads
- Log when budget pagination hits the page cap

* Prevent Copilot budget cookies from crossing accounts

- Thread selected Copilot account identity into settings

- Reject budget cookie sessions for a different GitHub account

- Cover the matching and mismatched account flows in tests

* Bind Copilot budgets to token identity

- Resolve GitHub identity from the active token before cookie checks
- Cover stale selected account identifiers in budget tests

* Harden Copilot budget identity parsing

- Treat missing budget identity metadata as invalid response

- Reuse compiled meta parsing regexes and tighten test stubs

* Fix Copilot budget cookie fallback

- Clear mismatched cached budget cookies before browser fallback
- Treat missing GitHub identity as an unknown account mismatch
- Cover cached mismatch and missing-identity fallback behavior

* Move Copilot menu card test

* Fix settings defaults lint after rebase

* fix: preserve subscription metadata in snapshot copies

* fix: keep manual Copilot budget cookies fail closed

* refactor: keep menu card model within lint limit

* fix: preserve snapshot data with budget windows

---------

Co-authored-by: Peter Steinberger <steipete@gmail.com>
2026-06-12 08:10:52 -07:00

3.3 KiB

summary, read_when
summary read_when
Copilot provider data sources: GitHub device flow, Copilot internal usage API, and optional GitHub web budgets.
Debugging Copilot login or usage parsing
Updating GitHub OAuth device flow behavior

Copilot provider

Copilot uses GitHub OAuth device flow and the Copilot internal usage API for primary usage. Optional budget extras use GitHub web cookies only when enabled.

Data sources + fallback order

  1. GitHub OAuth device flow (user initiated)

    • Device code request:
      • POST https://github.com/login/device/code
    • Token polling:
      • POST https://github.com/login/oauth/access_token
    • Optional enterprise host:
      • set Copilot enterpriseHost in ~/.codexbar/config.json or the provider settings UI
      • CodexBar normalizes values such as https://octocorp.ghe.com/login to octocorp.ghe.com
      • device flow uses https://<enterpriseHost>/login/...
    • Scope: read:user.
    • Token stored in config:
      • ~/.codexbar/config.jsonproviders[].apiKey for copilot
      • token accounts use providers[].tokenAccounts
  2. Usage fetch

    • GET https://api.github.com/copilot_internal/user
    • With an enterprise host, the API host is api.<enterpriseHost>.
    • Headers:
      • Authorization: token <github_oauth_token>
      • Accept: application/json
      • Editor-Version: vscode/1.96.2
      • Editor-Plugin-Version: copilot-chat/0.26.7
      • User-Agent: GitHubCopilotChat/0.26.7
      • X-Github-Api-Version: 2025-04-01
  3. Budget fetch (optional GitHub web endpoint, best-effort)

    • Disabled by default. The Copilot provider's "Budget extras" setting must be enabled before CodexBar imports github.com cookies or renders budget bars.
    • CodexBar asks the logged-in GitHub web endpoint for customer-scope budgets:
      • GET https://github.com/settings/billing/budgets?page=<page>&page_size=10&scope=customer
    • Headers:
      • Cookie: <github.com browser cookies>
      • Accept: application/json
      • X-Requested-With: XMLHttpRequest
      • GitHub-Verified-Fetch: true
      • X-Fetch-Nonce: <fresh nonce when available>
    • CodexBar first tries to read a fresh nonce from https://github.com/settings/billing/budgets, then calls the JSON endpoint. If GitHub rejects the web request, CodexBar keeps the normal Copilot quota bars and omits budget bars.
    • This is intentionally not the public GitHub REST billing API. The REST API did not expose the personal budget list for the tested individual account.

Snapshot mapping

  • Primary: quotaSnapshots.premiumInteractions percent remaining → used percent.
  • Secondary: quotaSnapshots.chat percent remaining → used percent.
  • Extra: positive Copilot billing budgets from the GitHub web endpoint → extraRateWindows, only when "Budget extras" is enabled.
    • Product budget: copilot
    • SKU budgets: copilot_premium_request, copilot_agent_premium_request, spark_premium_request
  • Reset dates are not provided by the API.
  • Plan label from copilotPlan.

Key files

  • Sources/CodexBarCore/Providers/Copilot/CopilotUsageFetcher.swift
  • Sources/CodexBarCore/Providers/Copilot/CopilotDeviceFlow.swift
  • Sources/CodexBar/Providers/Copilot/CopilotLoginFlow.swift
  • Sources/CodexBar/CopilotTokenStore.swift (legacy migration helper)