Skip to content

Publish live Grok tokens and xAI spend into Usage & Spend - #3085

Open
Chipagosfinest wants to merge 8 commits into
steipete:mainfrom
Chipagosfinest:agent/grok-xai-live-spend
Open

Publish live Grok tokens and xAI spend into Usage & Spend#3085
Chipagosfinest wants to merge 8 commits into
steipete:mainfrom
Chipagosfinest:agent/grok-xai-live-spend

Conversation

@Chipagosfinest

@Chipagosfinest Chipagosfinest commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Put enabled Grok and xAI into the shared Usage & Spend catalog that Overview already counts.
  • Map xAI Management API daily spend (the existing billing chart) as vendor-metered USD. Prepaid ledger balance stays remaining credit, not spend.
  • Map Grok ~/.grok/sessions/**/signals.json into per-day token buckets. SuperGrok/X Premium+ credits stay a quota window and are never converted into dollars.
  • UTC day labels for xAI follow the same dashboard calendar mapping as Mistral and OpenRouter.

This continues the Overview spend work in #3023, #3054, #3063, and #3067: every enabled subscription should contribute the strongest truthful signal instead of silently dropping out.

Why this slice

#2759 / #2855 were closed so review could happen concern-by-concern. After #3067, Grok and xAI were still supportsTokenCost: false, so they inflated Overview coverage (x of y subscriptions have spend) without publishing live data.

Test plan

  • GrokLocalSessionScannerTests — daily local buckets, no invented dollars, empty home publishes nothing, and local scan time owns Today
  • XAICostUsageMappingTests — chart → vendor-metered catalog; prepaid balance alone is not spend; partial history stays estimated
  • GrokXAISpendCatalogTests — both providers are snapshot-backed spend sources plus opt-in redacted live catalog proof
  • XAIProviderTests — existing golden balance/chart behavior unchanged; malformed successful history stays unavailable
  • GrokMenuCardModelTests — credit-bar presentation unchanged
  • PopupLocalizationTests — OpenRouter generic details localize titles/row labels via L() after 84a4ca7, while values and chart points stay canonical

Exact-head verification

Verified on a4f80675a4d1a2d1d76beafd335be6e2a5267002 after rebase onto main and Peter's merge of 84a4ca725:

make check
# passed: SwiftFormat, SwiftLint, JS/TS lint, generated-resource and repository gates

swift test --filter PopupLocalizationTests
# 6 tests in 1 suite passed

swift test --filter 'PopupLocalizationTests|GrokLocalSessionScannerTests|XAICostUsageMappingTests|XAIProviderTests|GrokXAISpendCatalogTests'
# 29 tests in 5 suites passed on the prior head; PopupLocalizationTests re-verified on this head

Redacted after-fix output from the real local Grok scanner through SpendDashboardModel.build:

catalog_source=grok
today_tokens=4841945
last_30_days_tokens=4841945
daily_buckets=1
available_sources=grok

No paths, account identifiers, session identifiers, prompts, or model names are included in this proof.

Residual risk

  • xAI still needs a Management API key + team ID for dollars to appear. Without them the provider stays enabled and reports unavailable, not $0.
  • Grok token totals are local session signals, not the SuperGrok bill. That is intentional.

@clawsweeper

clawsweeper Bot commented Aug 20, 2026

Copy link
Copy Markdown

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

Chipagosfinest added a commit to Chipagosfinest/CodexBar that referenced this pull request Aug 20, 2026

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

Copy link
Copy Markdown

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: 26fd232eef

ℹ️ 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/CodexBarCore/Providers/XAI/XAICostUsageMapping.swift Outdated
Comment thread Sources/CodexBar/UsageStore+TokenCost.swift
Comment thread Sources/CodexBarCore/Providers/XAI/XAICostUsageMapping.swift Outdated
Comment thread Sources/CodexBarCore/Providers/Grok/GrokLocalSessionScanner.swift Outdated
@clawsweeper clawsweeper Bot added merge-risk: 🚨 compatibility 🚨 Merging this PR could break existing users, config, migrations, defaults, or upgrades. P2 Normal priority bug or improvement with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Aug 20, 2026
@clawsweeper

clawsweeper Bot commented Aug 20, 2026

Copy link
Copy Markdown

Codex review: needs changes before merge. Reviewed August 20, 2026, 4:07 AM ET / 08:07 UTC.

ClawSweeper review

What this changes

The PR adds Grok local-session token activity and xAI Management API daily spend as sources for CodexBar’s shared Usage & Spend catalog.

Merge readiness

⚠️ Needs maintainer review before merge - 4 items remain

The prior cache-path blocker remains on the current head: Grok session files are still rescanned after refresh while constructing the shared catalog. The PR otherwise has adequate redacted real-behavior proof and no security concern.

Priority: P2
Reviewed head: a4f80675a4d1a2d1d76beafd335be6e2a5267002

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The feature has strong redacted local proof, but the unresolved cache-only P2 prevents merge readiness.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (terminal): Redacted terminal output from a real local Grok scanner through the shared catalog shows after-fix token and daily-bucket publication; it contains no account or session identifiers.
Patch quality 🦐 gold shrimp (3/6) 2 actionable review findings remain.

Verification

Check Result Evidence
Real behavior Verified Sufficient (terminal): Redacted terminal output from a real local Grok scanner through the shared catalog shows after-fix token and daily-bucket publication; it contains no account or session identifiers.
Evidence reviewed 6 items Current Grok mapper rescans local files: The Grok branch calls the recursive local-session scanner instead of reusing the snapshot already built during the provider refresh.
Catalog request invokes the mapper after refresh: The request builder refreshes snapshot-backed providers, then its Grok path calls the same mapper to populate catalog input; this retains the prior scan-on-catalog-path defect.
Refresh already has a Grok-derived snapshot: The Grok provider converts its local summary to cost usage during the refresh flow, so a cached publication can supply the catalog without another scan.
Findings 2 actionable findings [P2] Read Grok catalog data from the published snapshot
[P3] Remove the release-owned changelog entry
Security None None.

Live Verification

Command: swift test --filter XAICostUsageMappingTests

Result: FAIL (failed) — execution before step 1 run: sh -lc pnpm install --ignore-scripts --frozen-lockfile failed: ! Corepack is about to download https://registry.npmjs.org/pnpm/-/pnpm-11.22.0.tgz

sh -lc pnpm install --ignore-scripts --frozen-lockfile failed: ! Corepack is about to download https://registry.npmjs.org/pnpm/-/pnpm-11.22.0.tgz

Assertions:

  • FAIL expect_output: Test run

How this fits together

Provider refreshes produce token/spend snapshots that the shared catalog publishes to both the Overview menu and the Usage & Spend dashboard. Grok data comes from local session logs, while xAI data comes from the Management API’s daily billing history.

flowchart LR
A[Grok local session logs] --> B[Provider refresh]
C[xAI daily billing history] --> B
B --> D[Published provider snapshots]
D --> E[Shared Usage and Spend catalog]
E --> F[Overview menu]
E --> G[Settings dashboard]
Loading

Before merge

  • Read Grok catalog data from the published snapshot (P2) - The prior P2 remains on this unchanged path: makeRequest refreshes Grok and then invokes this mapper again while capturing catalog input, which recursively walks ~/.grok/sessions. The refresh snapshot already carries costUsage; reuse its cached publication so menu/dashboard updates cannot rescan local logs on the main-actor catalog path.
  • Remove the release-owned changelog entry (P3) - CHANGELOG.md is release-owned in this repository workflow, so this normal provider feature PR should leave release notes to the release process.
  • Resolve merge risk (P1) - Opening or refreshing the catalog can synchronously walk a large ~/.grok/sessions tree on the main-actor request path, delaying menu/dashboard responsiveness and producing duplicate local file reads.
  • Complete next step (P2) - A concrete cache-only repair and release-file removal can be made mechanically on this PR branch.

Findings

  • [P2] Read Grok catalog data from the published snapshot — Sources/CodexBar/UsageStore+TokenCost.swift:477-479
  • [P3] Remove the release-owned changelog entry — CHANGELOG.md:5
Agent review details

Security

None.

Review metrics

Metric Value Why it matters
Patch surface 19 files affected; 544 additions, 33 deletions The change spans provider parsing, snapshot publication, dashboard aggregation, tests, and documentation.

Merge-risk options

Maintainer options:

  1. Cache Grok session publication before merge (recommended)
    Reuse the Grok cost-usage value captured during refresh and add a regression proving catalog capture does not enumerate local session files.
  2. Pause the provider extension
    Do not merge this slice if the cache-only publication boundary cannot be preserved without a larger lifecycle redesign.
Copy recommended automerge instruction
@clawsweeper automerge

Special instructions:
Reuse the Grok cost snapshot published during provider refresh for the shared catalog; do not call the local-session scanner from catalog capture or menu-facing mapping, and add focused regression coverage.

Technical review

Best possible solution:

Publish Grok’s scan once during provider refresh, retain it as the current provider snapshot/publication, and have catalog construction read only that cached value.

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

Yes, from source: enable Grok with a large local session tree, trigger shared catalog capture, and the current mapper synchronously calls the local scanner after refresh.

Is this the best way to solve the issue?

No: the existing refresh snapshot already contains the Grok-derived cost usage, so rescanning during catalog capture is avoidable and violates the cache-only boundary.

Full review comments:

  • [P2] Read Grok catalog data from the published snapshot — Sources/CodexBar/UsageStore+TokenCost.swift:477-479
    The prior P2 remains on this unchanged path: makeRequest refreshes Grok and then invokes this mapper again while capturing catalog input, which recursively walks ~/.grok/sessions. The refresh snapshot already carries costUsage; reuse its cached publication so menu/dashboard updates cannot rescan local logs on the main-actor catalog path.
    Confidence: 0.98
  • [P3] Remove the release-owned changelog entry — CHANGELOG.md:5
    CHANGELOG.md is release-owned in this repository workflow, so this normal provider feature PR should leave release notes to the release process.
    Confidence: 0.99

Overall correctness: patch is incorrect
Overall confidence: 0.98

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 84a4ca725d5d.

Labels

Label justifications:

  • P2: The feature is useful but a main-actor local-file scan can cause a normal-priority responsiveness regression.
  • merge-risk: 🚨 availability: Merging retains a recursive local-session scan on the catalog request path used by the shared menu/dashboard publisher.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦞 diamond lobster and patch quality is 🦐 gold shrimp.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Sufficient (terminal): Redacted terminal output from a real local Grok scanner through the shared catalog shows after-fix token and daily-bucket publication; it contains no account or session identifiers.
  • proof: sufficient: Contributor real behavior proof is sufficient. Redacted terminal output from a real local Grok scanner through the shared catalog shows after-fix token and daily-bucket publication; it contains no account or session identifiers.

Evidence

Acceptance criteria:

  • [P1] swift test --filter GrokLocalSessionScannerTests.
  • [P1] swift test --filter GrokXAISpendCatalogTests.
  • [P1] swift test --filter XAICostUsageMappingTests.
  • [P1] make check.

What I checked:

Likely related people:

  • Chipagosfinest: The supplied history identifies this contributor as the author of the merged shared spend-source catalog work and of this provider-specific extension. (role: shared-catalog feature contributor; confidence: high; commits: 40297051639b, a4f80675a4d1; files: Sources/CodexBar/SpendDashboardController.swift, Sources/CodexBar/UsageStore+TokenCost.swift)
  • Alec Gutman: Blame assigns the unchanged Grok catalog and mapper lines to the current PR head commit. (role: current-head source author; confidence: high; commits: a4f80675a4d1; files: Sources/CodexBar/SpendDashboardController.swift, Sources/CodexBar/UsageStore+TokenCost.swift)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Reuse the refresh-published Grok snapshot in catalog capture.
  • Add focused coverage that catalog construction cannot trigger a Grok local-session scan.
  • Remove the release-owned CHANGELOG.md edit.

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
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.

Workflow

  • 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.

History

Review history (5 earlier review cycles)
  • reviewed 2026-08-20T01:03:50.059Z sha 55e602c :: needs real behavior proof before merge. :: [P1] Preserve xAI analytics failures as unavailable | [P2] Publish local Grok tokens without a remote snapshot | [P2] Select xAI’s current UTC billing-day entry | [P2] Select Grok’s current local-day token bucket | [P2] Keep xAI’s 30-day history from posing as 365 days
  • reviewed 2026-08-20T01:15:17.742Z sha bef86db :: needs real behavior proof before merge. :: [P1] Preserve xAI analytics failures as unavailable | [P2] Publish Grok tokens without a remote snapshot | [P2] Use xAI’s current UTC day for Today | [P2] Use Grok’s current local-day bucket for Today | [P2] Limit xAI coverage to its 30-day response
  • reviewed 2026-08-20T01:36:58.552Z sha 4a93fbe :: needs real behavior proof before merge. :: [P1] Reject malformed xAI usage responses before publishing zero spend | [P2] Anchor Grok’s local Today value to the local scan time
  • reviewed 2026-08-20T06:55:01.271Z sha c44ec7a :: needs changes before merge. :: [P2] Cache Grok scans outside menu rendering
  • reviewed 2026-08-20T07:32:30.082Z sha c48d4a2 :: needs changes before merge. :: [P2] Read Grok cost history from the cached publication

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels Aug 20, 2026
@Chipagosfinest

Copy link
Copy Markdown
Contributor Author

Addressed the ClawSweeper/Codex source-state findings on the current head:

  1. xAI /usage failures stay unavailable (missing chart). Successful empty history is confirmed empty.
  2. Grok local ~/.grok/sessions tokens publish even when remote billing fails.
  3. xAI Today is the UTC day of updatedAt, not the newest point.
  4. Grok Today is the current local-day bucket; idle days are not reused.
  5. xAI coverage stays a 30-day vendor source even when the dashboard asks for 365.

Focused tests: GrokLocalSessionScannerTests, XAICostUsageMappingTests, XAIProviderTests, GrokXAISpendCatalogTests — 20 passed.

Redacted local Grok proof from this machine after the fix (no identity):

signals_files=6
aggregate_tokens=1242673
models=['grok-4.6']

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Aug 20, 2026

Copy link
Copy Markdown

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event exact_review_queue).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Re-review progress:

@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Aug 20, 2026
@Chipagosfinest

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

Exact head c44ec7abc: both durable findings are fixed, make check passes, 23 focused tests pass, and the PR body now includes redacted real Grok scanner → shared catalog output. All four Codex review threads are resolved.

@clawsweeper

clawsweeper Bot commented Aug 20, 2026

Copy link
Copy Markdown

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event exact_review_queue).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

@clawsweeper clawsweeper Bot added merge-risk: 🚨 availability 🚨 Merging this PR could cause crashes, hangs, restart loops, stalls, or process outages. proof: sufficient Contributor real behavior proof is sufficient. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. merge-risk: 🚨 compatibility 🚨 Merging this PR could break existing users, config, migrations, defaults, or upgrades. labels Aug 20, 2026
Enabled Grok and xAI now join the shared spend catalog instead of only
inflating the unavailable denominator. xAI contributes vendor-metered
daily USD from the Management API chart; Grok contributes local session
tokens. SuperGrok credits and xAI prepaid balance stay quotas, not spend.
Wrap no-data copy under 120 characters and drop a redundant throws
on the prepaid-balance mapping test.
Preserve xAI analytics failures as unavailable instead of known-zero
spend, publish local Grok tokens when remote billing fails, pin Today
to the current UTC/local day, and keep xAI history as a 30-day source.
steipete#3086 scoped localizedProviderDetails to DeepSeek and z.ai, so generic
OpenRouter details keep canonical English. The merge test still expected
zh-Hant "API 金鑰" and failed macOS shard 0.
@Chipagosfinest
Chipagosfinest force-pushed the agent/grok-xai-live-spend branch from c44ec7a to c48d4a2 Compare August 20, 2026 07:28
@Chipagosfinest

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

Exact head c48d4a2bb: rebased onto current main and fixed the macOS shard 0 failure from #3086. That PR scoped localizedProviderDetails to DeepSeek/z.ai, so generic OpenRouter details stay canonical English; the merge test still expected API 金鑰. make check passes; 29 focused tests in 5 suites pass.

@clawsweeper

clawsweeper Bot commented Aug 20, 2026

Copy link
Copy Markdown

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event exact_review_queue).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Re-review progress:

steipete and others added 2 commits August 20, 2026 00:58
Peter restored generic title/row L() localization in 84a4ca7 after
steipete#3086 scoped it away. The merge kept the canonical-English assertion
from the earlier CI fix; match the restored shared catalog instead.
@Chipagosfinest

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

Exact head a4f80675a: Peter merged main (84a4ca725 restored generic title/row L() localization). The OpenRouter popup test now matches that restored catalog. make check and PopupLocalizationTests pass on this head.

@clawsweeper

clawsweeper Bot commented Aug 20, 2026

Copy link
Copy Markdown

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event exact_review_queue).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-risk: 🚨 availability 🚨 Merging this PR could cause crashes, hangs, restart loops, stalls, or process outages. P2 Normal priority bug or improvement with limited blast radius. proof: sufficient Contributor real behavior proof is sufficient. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants