Skip to content

Conversation

@waleedlatif1
Copy link
Collaborator

Summary

  • add preprocessing util shared by all execution paths that encompasses all logging sessions, billing checks, permission checks, rate limit checks, etc

Type of Change

  • Bug fix
  • New feature

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 Nov 20, 2025

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

1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
docs Skipped Skipped Nov 20, 2025 9:32pm

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Nov 20, 2025

Greptile Overview

Greptile Summary

This PR introduces a centralized preprocessExecution utility that consolidates workflow validation, billing resolution, rate limiting, and usage checks across all execution paths (chat, webhooks, API, schedules, and resume operations).

Major improvements:

  • Eliminates code duplication by replacing scattered validation logic with a single preprocessing function
  • Provides consistent error handling and logging across all execution triggers
  • Introduces isResumeContext flag for graceful billing fallback when resuming paused workflows
  • Simplifies test mocking with a single preprocessing mock instead of multiple service mocks
  • Refactors search modal component to extract fuzzy search logic into a separate utility file

Key behavioral changes:

  • Chat route now returns 403 instead of 503 when workflow is not deployed (more accurate HTTP status)
  • Resume operations now allow billing fallback to workflow owner if workspace billing fails mid-execution
  • All preprocessing errors are now consistently logged to execution_logs table for user visibility

Confidence Score: 4/5

  • This PR is safe to merge with minimal risk - it's primarily a refactoring that consolidates existing logic
  • Score reflects solid refactoring with comprehensive test coverage, but execution path complexity warrants thorough integration testing. The centralized preprocessing logic is well-structured with proper error handling, and the fallback billing mechanism for resume contexts is a thoughtful addition. However, the wide-reaching changes across multiple execution paths require careful validation in staging.
  • No files require special attention - the refactoring is clean and well-tested

Important Files Changed

File Analysis

Filename Score Overview
apps/sim/lib/execution/preprocessing.ts 4/5 New centralized preprocessing utility that consolidates billing, rate limiting, and permission checks across all execution paths
apps/sim/app/api/chat/[identifier]/route.ts 4/5 Refactored to use preprocessExecution utility, replacing duplicated workflow validation and billing logic
apps/sim/app/api/workflows/[id]/execute/route.ts 4/5 Updated to use preprocessExecution for centralized validation, simplifying workflow execution flow
apps/sim/lib/webhooks/processor.ts 4/5 Replaced checkRateLimits and checkUsageLimits with checkWebhookPreprocessing wrapper around new preprocessing utility
apps/sim/app/api/resume/[workflowId]/[executionId]/[contextId]/route.ts 4/5 Added preprocessing checks for resume operations with isResumeContext flag and skipUsageLimits for continuation of authorized execution
apps/sim/background/schedule-execution.ts 4/5 Refactored to use preprocessExecution instead of manual billing/rate limit checks, removed duplicate helper functions

Sequence Diagram

sequenceDiagram
    participant Client
    participant API as API Route (Chat/Webhook/Workflow)
    participant Preprocess as preprocessExecution()
    participant DB as Database
    participant Billing as Billing Service
    participant RateLimit as Rate Limiter
    participant Logging as LoggingSession
    participant Executor as Workflow Executor

    Client->>API: POST request (trigger workflow)
    API->>Logging: new LoggingSession()
    API->>Preprocess: preprocessExecution({workflowId, userId, triggerType, ...})
    
    Note over Preprocess: Step 1: Validate Workflow
    Preprocess->>DB: SELECT workflow WHERE id = workflowId
    alt Workflow not found
        DB-->>Preprocess: No records
        Preprocess->>Logging: safeStart() + safeCompleteWithError()
        Preprocess-->>API: {success: false, error: {message, statusCode: 404}}
        API-->>Client: 404 Workflow not found
    end
    DB-->>Preprocess: workflow record
    
    Note over Preprocess: Step 2: Check Deployment
    alt checkDeployment && !isDeployed
        Preprocess->>Logging: safeStart() + safeCompleteWithError()
        Preprocess-->>API: {success: false, error: {message, statusCode: 403}}
        API-->>Client: 403 Workflow not deployed
    end
    
    Note over Preprocess: Step 3: Resolve Billing Actor
    Preprocess->>Billing: getWorkspaceBilledAccountUserId(workspaceId)
    Billing-->>Preprocess: actorUserId
    alt actorUserId is null
        alt isResumeContext
            Note over Preprocess: Fallback to workflow owner for resume
            Preprocess->>Preprocess: Use workflowRecord.userId
        else
            Preprocess->>Logging: safeStart() + safeCompleteWithError()
            Preprocess-->>API: {success: false, error: {statusCode: 500}}
            API-->>Client: 500 Unable to resolve billing
        end
    end
    
    Note over Preprocess: Step 4: Get Subscription
    Preprocess->>Billing: getHighestPrioritySubscription(actorUserId)
    Billing-->>Preprocess: userSubscription
    
    Note over Preprocess: Step 5: Check Rate Limits
    alt checkRateLimit
        Preprocess->>RateLimit: checkRateLimitWithSubscription()
        RateLimit-->>Preprocess: {allowed, remaining, resetAt}
        alt !allowed
            Preprocess->>Logging: safeStart() + safeCompleteWithError()
            Preprocess-->>API: {success: false, error: {statusCode: 429}}
            API-->>Client: 429 Rate limit exceeded
        end
    end
    
    Note over Preprocess: Step 6: Check Usage Limits
    alt !skipUsageLimits
        Preprocess->>Billing: checkServerSideUsageLimits(actorUserId)
        Billing-->>Preprocess: {isExceeded, currentUsage, limit}
        alt isExceeded
            Preprocess->>Logging: safeStart() + safeCompleteWithError()
            Preprocess-->>API: {success: false, error: {statusCode: 402}}
            API-->>Client: 402 Usage limit exceeded
        end
    end
    
    Note over Preprocess: All checks passed
    Preprocess-->>API: {success: true, actorUserId, workflowRecord, userSubscription}
    
    API->>Executor: executeWorkflow(workflow, actorUserId, input)
    Executor->>Logging: safeStart()
    Executor->>Executor: Execute workflow blocks
    Executor->>Logging: safeComplete() / safeCompleteWithError()
    Executor-->>API: ExecutionResult
    API-->>Client: Response (200/streaming)
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.

17 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@waleedlatif1 waleedlatif1 merged commit 75f55c8 into staging Nov 20, 2025
9 checks passed
@waleedlatif1 waleedlatif1 deleted the fix/logging branch November 20, 2025 22:05
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