Skip to content

Conversation

@waleedlatif1
Copy link
Collaborator

Summary

  • add condition to prevent duplicate identical edges between the same source port and target ports

Type of Change

  • Bug fix

Testing

Tested manaully

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 Jan 14, 2026

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

1 Skipped Deployment
Project Deployment Review Updated (UTC)
docs Skipped Skipped Jan 14, 2026 0:35am

@waleedlatif1 waleedlatif1 changed the title fix)comparison): add condition to prevent duplicate identical edges fix(comparison): add condition to prevent duplicate identical edges Jan 14, 2026
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Jan 14, 2026

Greptile Summary

This PR restores duplicate edge prevention at the store level, implementing a more precise check that validates all four connection fields (source, sourceHandle, target, targetHandle) instead of just source and target nodes. This addresses the senior developer's previous feedback about programmatic edge additions potentially creating duplicates.

Key Changes:

  • Enhanced batchAddEdges in store.ts to check for identical connections by comparing source port, source handle, target port, and target handle
  • Maintained the connectionCompletedRef pattern in workflow.tsx to distinguish between handle-to-handle and handle-to-body drag operations
  • The two-layer approach provides defense in depth: UI-level prevention via ref tracking and store-level validation for programmatic calls

Why This Approach:
The developer noted "so many codepaths converge at this point" - batchAddEdges is called from multiple sources (user interactions, collaborative updates, imports, etc.), so having duplicate prevention at the store level ensures consistency regardless of the calling context.

Confidence Score: 5/5

  • This PR is safe to merge with no identified risks
  • The implementation correctly addresses the previous reviewer's concern by adding comprehensive duplicate detection that checks all four connection fields. The logic properly handles both existing edges and edges being added in the current batch. The connectionCompletedRef pattern in the UI layer works correctly with the store-level validation to prevent duplicates from all code paths.
  • No files require special attention

Important Files Changed

Filename Overview
apps/sim/stores/workflows/workflow/store.ts Added precise duplicate edge detection checking all four connection fields (source, sourceHandle, target, targetHandle) instead of just source and target
apps/sim/app/workspace/[workspaceId]/w/[workflowId]/workflow.tsx Uses connectionCompletedRef pattern to prevent duplicate connections when dropping edges, avoiding the need for store-level duplicate checks in onConnectEnd

Sequence Diagram

sequenceDiagram
    participant User
    participant onConnectStart
    participant onConnect
    participant onConnectEnd
    participant batchAddEdges
    
    User->>onConnectStart: Start dragging connection
    activate onConnectStart
    onConnectStart->>onConnectStart: Set connectionCompletedRef = false
    onConnectStart->>onConnectStart: Store source node/handle
    deactivate onConnectStart
    
    alt Handle-to-handle connection
        User->>onConnect: Drop on target handle
        activate onConnect
        onConnect->>onConnect: Validate connection
        onConnect->>batchAddEdges: Add edge
        activate batchAddEdges
        batchAddEdges->>batchAddEdges: Check if edge ID exists
        batchAddEdges->>batchAddEdges: Check for self-reference
        batchAddEdges->>batchAddEdges: Check if identical connection exists<br/>(source, sourceHandle, target, targetHandle)
        batchAddEdges->>batchAddEdges: Check for cycles
        batchAddEdges->>batchAddEdges: Add edge to store
        deactivate batchAddEdges
        onConnect->>onConnect: Set connectionCompletedRef = true
        deactivate onConnect
        User->>onConnectEnd: Connection drag ends
        activate onConnectEnd
        onConnectEnd->>onConnectEnd: Check connectionCompletedRef = true
        onConnectEnd->>onConnectEnd: Skip (already handled)
        deactivate onConnectEnd
    else Handle-to-body connection
        User->>onConnectEnd: Drop on block body
        activate onConnectEnd
        onConnectEnd->>onConnectEnd: Check connectionCompletedRef = false
        onConnectEnd->>onConnectEnd: Find target node at cursor
        onConnectEnd->>onConnect: Create connection to 'target' handle
        activate onConnect
        onConnect->>batchAddEdges: Add edge
        activate batchAddEdges
        batchAddEdges->>batchAddEdges: Check duplicate (all 4 fields)
        batchAddEdges->>batchAddEdges: Add edge to store
        deactivate batchAddEdges
        onConnect->>onConnect: Set connectionCompletedRef = true
        deactivate onConnect
        deactivate onConnectEnd
    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.

Additional Comments (1)

  1. apps/sim/stores/workflows/workflow/store.ts, line 300-311 (link)

    logic: Check that test still passes - it expects calling batchAddEdges twice with different IDs but same source/target creates only one edge. With duplicate check removed, both edges will be added.

2 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

@waleedlatif1
Copy link
Collaborator Author

@greptile

@waleedlatif1 waleedlatif1 merged commit 2b49d15 into staging Jan 14, 2026
11 checks passed
@waleedlatif1 waleedlatif1 deleted the fix/comp branch January 14, 2026 01:17
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