Skip to content

feat: sync imagine quota from upstream after generation - #886

Merged
chenyme merged 5 commits into
chenyme:mainfrom
scvxzf1:feat/imagine-quota-sync-after-generation
Aug 13, 2026
Merged

feat: sync imagine quota from upstream after generation#886
chenyme merged 5 commits into
chenyme:mainfrom
scvxzf1:feat/imagine-quota-sync-after-generation

Conversation

@scvxzf1

@scvxzf1 scvxzf1 commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Summary

Imagine image/video quotas (image_pro / image_edit / video / video_720p) were read-only snapshots that never refreshed after generation, so Accounts → Grok Web showed stale quotas even though real consumption was happening. The per-generation refresh path was blocked end-to-end:

  • imagine models had an empty catalog Mode, so QuotaMode() returned "";
  • QueueQuotaRefresh rejected imagine modes (whitelist of console/weekly/chat only);
  • runQuotaRefresh force-switched every non-console mode back to weekly;
  • SyncQuotaMode only supported weekly and /rest/rate-limits, not /rest/media/imagine/quota_info;
  • the image/video gateways fed effectiveQuotaMode (always weekly for web) into QueueQuotaRefresh, so imagine modes never reached the refresh queue.

This PR wires imagine models into the per-generation refresh pipeline so a successful imagine generation triggers an upstream /rest/media/imagine/quota_info sync for that account.

Changes

  • catalog.go — assign image_pro / image_edit / video Mode to imagine models so QuotaMode() returns the imagine mode. (grok-imagine-image-lite keeps fast — it uses the chat endpoint.)
  • quota.goSyncQuotaMode gains an imagine branch that calls syncImagineQuota and returns the requested mode's window; full SyncQuota now retains imagine windows alongside the weekly snapshot instead of dropping them.
  • service.go — new isWebImagineQuotaMode (reuses imagineQuotaBlockModels keys); QueueQuotaRefresh accepts imagine modes; runQuotaRefresh skips the force-weekly override for imagine modes; refreshQuotaMode applies applyImagineQuotaCooldown after persisting imagine windows.
  • image.go / video.go — pass the catalog imagine mode (image_pro / video) to QueueQuotaRefresh instead of the always-weekly effectiveQuotaMode / lease.QuotaMode.
  • pricing.go — extend the official image-cost estimator to imagine-x-1.
  • model_repository.go — add imagine-x-1 route defaults.
  • account_repository.go / repository/account.goOverwriteModelQuotaBlock to align cooldown to the upstream authoritative nextAvailableAt.
  • account-quota.tsx / i18n — surface imagine quota windows in the Accounts UI as a dedicated row (remaining/total, exhaustion + cooldown state, reset tooltip) with localized labels.

Routing is unchanged: getRoutingQuotaWindows keeps weekly first in its ORDER BY and the per-account map keeps the first row, so candidate.QuotaWindow and effectiveQuotaMode stay weekly and selection is unaffected. The catalog Mode only feeds QuotaMode() for the finalize refresh call.

Verification

Deployed and tested end-to-end against a live Grok Web pool (imagine-x-1):

  • Generation 1 (account 11249): image_pro synced_at advanced 18:21:49 → 18:55:12 (≈1–2s after generation).
  • Generation 2 (account 11251): image_pro 8/8 → 6/6, synced_at 18:21:49 → 19:00:09, reflecting the two upstream consumption events.
  • Container logs confirmed POST /rest/media/imagine/quota_info fired after each generation and quota_refresh_events reported outcome=success.
  • go build ./... and go vet clean; affected package tests pass (internal/infra/provider/web, internal/application/account, internal/application/gateway); frontend pnpm typecheck clean.

Test plan

  • Run go test ./internal/infra/provider/web/... ./internal/application/account/... ./internal/application/gateway/...
  • Generate an image via imagine-x-1 and confirm the used account's image_pro window synced_at advances and remaining reflects the upstream value in Accounts → Grok Web.
  • Generate via grok-imagine-image-lite (fast mode) and confirm the existing fast-quota decrement path is unaffected.
  • Exhaust an account's image_pro (remaining 0) and confirm a ModelQuotaBlock cooldown is written for imagine-x-1 / grok-imagine-image-quality.
  • Confirm weekly quota and account selection behave unchanged before/after.

scvxzf1 and others added 5 commits August 9, 2026 03:34
Imagine image/video quotas (image_pro/image_edit/video/video_720p) were
read-only snapshots that never refreshed after generation, so the
"Accounts > Grok Web" view showed stale quotas despite real consumption.
The post-generation refresh path was blocked end-to-end: imagine models
had empty catalog Mode, QueueQuotaRefresh rejected imagine modes,
runQuotaRefresh force-switched non-console modes back to weekly, and
SyncQuotaMode only supported weekly and /rest/rate-limits.

Wire imagine models into the per-generation refresh pipeline:
- catalog: assign image_pro/image_edit/video Mode to imagine models so
  QuotaMode() returns the imagine mode for routing/finalize
- SyncQuotaMode: add imagine branch calling syncImagineQuota and picking
  the requested mode's window
- QueueQuotaRefresh: accept imagine modes via new isWebImagineQuotaMode
  (reuses imagineQuotaBlockModels keys)
- runQuotaRefresh: skip the force-weekly override for imagine modes
- refreshQuotaMode: apply imagine cooldown after persisting the window
- image/video gateways: pass the catalog imagine mode (image_pro/video)
  to QueueQuotaRefresh instead of the always-weekly effectiveQuotaMode

Also surface imagine quota windows in the accounts UI with a dedicated
row (remaining/total, exhaustion, reset tooltip) and i18n labels, and
extend full SyncQuota to retain imagine windows alongside the weekly
snapshot. Routing is unchanged: getRoutingQuotaWindows keeps weekly
first, so candidate.QuotaWindow and effectiveQuotaMode stay weekly.
# Conflicts:
#	backend/internal/application/gateway/video.go
#	backend/internal/domain/audit/pricing.go
#	backend/internal/infra/provider/web/catalog.go
@chenyme chenyme self-assigned this Aug 13, 2026
@chenyme
chenyme merged commit 736c093 into chenyme:main Aug 13, 2026
9 checks passed
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.

2 participants