Skip to content

Conversation

@waleedlatif1
Copy link
Collaborator

@waleedlatif1 waleedlatif1 commented Dec 18, 2025

Summary

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 18, 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 18, 2025 3:01am

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Dec 18, 2025

Greptile Summary

This PR implements cycle detection for the workflow graph by adding a wouldCreateCycle function that uses depth-first search to check if adding an edge would create a cycle. The implementation follows ReactFlow's recommended approach.

Key changes:

  • Added wouldCreateCycle utility function using DFS algorithm to detect cycles
  • Integrated cycle check in addEdge method at the store level for centralized validation
  • Added cycle checks to all 5 handle isValidConnection callbacks for immediate UI feedback
  • Removed redundant self-connection check from onConnect handler

Implementation is sound:

  • Algorithm correctly checks if target can reach source (which would create a cycle when adding source→target edge)
  • Self-connections are properly detected
  • All edge addition paths go through the store's addEdge method where validation occurs
  • UI feedback is provided during drag operations via isValidConnection

Minor observation:

  • The isValidConnection callbacks call useWorkflowStore.getState().edges during drag operations, which could have performance implications for very large graphs (hundreds of nodes). However, this is following ReactFlow's example pattern and is unlikely to be an issue for typical workflow sizes.

Confidence Score: 5/5

  • This PR is safe to merge with no blocking issues
  • The cycle detection algorithm is correct and well-implemented. All edge addition paths are properly validated. The code follows ReactFlow's recommended patterns. No logical errors, security issues, or breaking changes detected.
  • No files require special attention

Important Files Changed

Filename Overview
apps/sim/stores/workflows/workflow/utils.ts Added wouldCreateCycle function implementing cycle detection using DFS - algorithm is correct and well-documented
apps/sim/stores/workflows/workflow/store.ts Integrated cycle check in addEdge method to prevent cycles at the store level - proper centralized validation
apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/workflow-block/workflow-block.tsx Added cycle checks to all 5 handle isValidConnection callbacks for UI feedback - potential performance concern with repeated store access during drag
apps/sim/app/workspace/[workspaceId]/w/[workflowId]/workflow.tsx Removed redundant self-connection check from onConnect as it's now handled by cycle detection - cleaner code

Sequence Diagram

sequenceDiagram
    participant User
    participant Handle as WorkflowBlock Handle
    participant ReactFlow
    participant OnConnect as workflow.tsx onConnect
    participant Store as useWorkflowStore
    participant Utils as wouldCreateCycle

    User->>Handle: Drag connection from source to target
    Handle->>Utils: isValidConnection(connection)
    Utils->>Store: getState().edges
    Store-->>Utils: current edges[]
    Utils->>Utils: Check if source === target
    alt Self-connection
        Utils-->>Handle: false (reject)
    else Valid so far
        Utils->>Utils: Build adjacency list from edges
        Utils->>Utils: DFS from target to find source
        alt Cycle would be created
            Utils-->>Handle: false (reject)
        else No cycle
            Utils-->>Handle: true (allow UI)
        end
    end
    
    alt Connection allowed by UI
        User->>ReactFlow: Drop connection
        ReactFlow->>OnConnect: onConnect(connection)
        OnConnect->>OnConnect: Validate container boundaries
        OnConnect->>Store: addEdge(edge)
        Store->>Utils: wouldCreateCycle(edges, source, target)
        Utils->>Utils: Run cycle detection again
        alt Cycle detected
            Utils-->>Store: true
            Store->>Store: Log warning and return early
        else No cycle
            Utils-->>Store: false
            Store->>Store: Check for duplicates
            Store->>Store: Add edge to store
        end
    end
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.

3 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@waleedlatif1
Copy link
Collaborator Author

@greptile

@waleedlatif1 waleedlatif1 merged commit 5516fa3 into staging Dec 18, 2025
11 checks passed
@waleedlatif1 waleedlatif1 deleted the fix/cyclic branch December 18, 2025 03:13
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