Skip to content

Conversation

@waleedlatif1
Copy link
Collaborator

Summary

  • update query invalidation for usage to update in realtime
  • incorrect query key was being invalidated

Type of Change

  • Bug fix

Testing

Tested manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link

vercel bot commented Dec 27, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Review Updated (UTC)
docs Skipped Skipped Dec 27, 2025 11:11pm

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Dec 27, 2025

Greptile Summary

Fixed query invalidation to update usage indicators in real-time by using subscriptionKeys.all instead of subscriptionKeys.user().

The root cause was that subscriptionKeys.user() is a function requiring an includeOrg parameter, creating distinct cache keys for different parameter values. Components use both useSubscriptionData() (defaults to includeOrg: false) and useSubscriptionData({ includeOrg: true }), resulting in two separate cache entries. The previous invalidation only targeted one variant, leaving the usage indicator stale.

Key changes:

  • Updated 6 files to use subscriptionKeys.all for query invalidation after operations that affect usage (workflow execution, wand generation, copilot, subscription changes)
  • Enhanced useUpdateUsageLimit to handle optimistic updates for both query variants and properly restore both on errors

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The fix correctly addresses the root cause by invalidating all subscription-related queries. The change is consistent across all affected files, maintains proper optimistic update patterns, and doesn't introduce breaking changes or side effects.
  • No files require special attention

Important Files Changed

Filename Overview
apps/sim/hooks/queries/subscription.ts Fixed optimistic updates to handle both query variants (with/without org data), ensuring correct rollback on errors
apps/sim/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-workflow-execution.ts Updated query invalidation to use subscriptionKeys.all in two locations after workflow execution completes
apps/sim/app/workspace/[workspaceId]/w/components/sidebar/components/usage-indicator/usage-indicator.tsx Updated query invalidation to use subscriptionKeys.all on operation confirmed events

Sequence Diagram

sequenceDiagram
    participant User
    participant Component
    participant QueryClient
    participant API
    participant Cache

    Note over Component,Cache: Before: subscriptionKeys.user() without params
    Component->>QueryClient: invalidateQueries(subscriptionKeys.user())
    Note over QueryClient: Only invalidates ['subscription', 'user', {includeOrg: undefined}]
    QueryClient->>Cache: Check cache keys
    Note over Cache: Miss: ['subscription', 'user', {includeOrg: false}]<br/>Miss: ['subscription', 'user', {includeOrg: true}]
    Note over Cache: Usage indicator not updated!

    Note over Component,Cache: After: subscriptionKeys.all
    User->>Component: Execute workflow/wand/copilot
    Component->>API: POST /api/execute
    API-->>Component: Success (usage updated server-side)
    Component->>QueryClient: invalidateQueries(subscriptionKeys.all)
    Note over QueryClient: Invalidates all queries starting with ['subscription']
    QueryClient->>Cache: Invalidate ['subscription', 'user', {includeOrg: false}]
    QueryClient->>Cache: Invalidate ['subscription', 'user', {includeOrg: true}]
    QueryClient->>Cache: Invalidate ['subscription', 'usage']
    Cache-->>Component: All subscription queries marked stale
    Component->>API: Refetch /api/billing?context=user&includeOrg=true
    API-->>Component: Fresh usage data
    Component->>User: Updated usage indicator displayed
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Additional Comments (5)

  1. apps/sim/hooks/queries/subscription.ts, line 101-102 (link)

    logic: onMutate still cancels specific query keys, inconsistent with the onSettled invalidation change. This could cause issues if includeOrg parameter variants exist.

  2. apps/sim/hooks/queries/subscription.ts, line 104-105 (link)

    logic: getQueryData calls still use specific query keys without parameters. Since useSubscriptionData can be called with includeOrg: true, this won't retrieve the correct cached data.

  3. apps/sim/hooks/queries/subscription.ts, line 107-123 (link)

    logic: setQueryData calls still use specific query keys without parameters. This won't update the correct cache entry when useSubscriptionData({ includeOrg: true }) is used (as in usage-indicator.tsx:194).

  4. apps/sim/hooks/queries/subscription.ts, line 125-134 (link)

    logic: setQueryData still uses specific query key without parameters, won't update the correct cache entry.

  5. apps/sim/hooks/queries/subscription.ts, line 140 (link)

    logic: onError rollback still uses specific query keys, won't restore the correct cache entries.

6 files reviewed, 5 comments

Edit Code Review Agent Settings | Greptile

@waleedlatif1
Copy link
Collaborator Author

@greptile

@waleedlatif1 waleedlatif1 merged commit aa9cc56 into staging Dec 27, 2025
11 checks passed
@waleedlatif1 waleedlatif1 deleted the improvement/usage-indicator branch December 27, 2025 23:23
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