Skip to content

Conversation

@Sg312
Copy link
Contributor

@Sg312 Sg312 commented Dec 18, 2025

Summary

Adds edge handle validation to copilot edit workflow

Type of Change

  • Bug fix

Testing

Manual

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 6:57pm

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Dec 18, 2025

Greptile Summary

Added comprehensive edge handle validation to the copilot edit workflow tool to prevent invalid workflow connections from being created.

Key Changes

  • Added validateSourceHandleForBlock() to validate source handles based on block type (loop, parallel, condition, router, default)
  • Added validateConditionHandle() to validate condition handles reference actual conditions in the block, supporting both internal IDs and semantic keys (if/else-if/else)
  • Added validateTargetHandle() to ensure target handles are 'target' or empty/undefined (which defaults to 'target')
  • Added createValidatedEdge() helper that performs all validations before creating edges and logs skipped items with reasons
  • Refactored addConnectionsAsEdges() to use the new validated edge creation
  • Added two new SkippedItemType entries: invalid_source_handle and invalid_target_handle

Impact

  • Prevents copilot from creating workflow edges with invalid handles that would fail at runtime
  • Provides detailed error messages explaining why edges were skipped, helping copilot learn correct handle patterns
  • Maintains backward compatibility - all validation is additive and skips invalid edges rather than failing the entire operation

Confidence Score: 5/5

  • Safe to merge - comprehensive validation with no blocking issues
  • The PR adds important validation logic that prevents invalid edges from being created. The implementation correctly handles multiple block types (loop, parallel, condition, router) with appropriate handle validation. The condition handle validation supports both internal IDs and semantic keys. The code follows existing patterns, has good error messages, and properly logs skipped items. While there's one minor comment about empty string handling in validateTargetHandle, all actual callers default values appropriately before validation, making this a non-issue in practice.
  • No files require special attention

Important Files Changed

Filename Overview
apps/sim/lib/copilot/tools/server/workflow/edit-workflow.ts Added comprehensive edge handle validation with three new functions (validateSourceHandleForBlock, validateConditionHandle, validateTargetHandle) and refactored edge creation to use new createValidatedEdge helper, preventing invalid connections from being created

Sequence Diagram

sequenceDiagram
    participant Copilot
    participant editWorkflow
    participant createValidatedEdge
    participant validateSourceHandleForBlock
    participant validateConditionHandle
    participant validateTargetHandle
    participant WorkflowState

    Copilot->>editWorkflow: Execute operations (add/edit blocks with connections)
    editWorkflow->>editWorkflow: applyOperationsToWorkflowState()
    
    Note over editWorkflow: Process operations in order: delete, extract, add, insert, edit
    
    alt Add/Edit operation with connections
        editWorkflow->>createValidatedEdge: Create edge with source/target handles
        
        createValidatedEdge->>WorkflowState: Check if target block exists
        alt Target block missing
            WorkflowState-->>createValidatedEdge: Block not found
            createValidatedEdge->>editWorkflow: Skip edge (log invalid_edge_target)
        end
        
        createValidatedEdge->>WorkflowState: Check if source block exists
        alt Source block missing
            WorkflowState-->>createValidatedEdge: Block not found
            createValidatedEdge->>editWorkflow: Skip edge (log invalid_edge_source)
        end
        
        createValidatedEdge->>validateSourceHandleForBlock: Validate source handle for block type
        
        alt Block type specific validation
            validateSourceHandleForBlock->>validateSourceHandleForBlock: Check block type (loop/parallel/condition/router/default)
            
            alt Condition block
                validateSourceHandleForBlock->>validateConditionHandle: Validate condition handle against conditions
                validateConditionHandle->>validateConditionHandle: Parse conditions array
                validateConditionHandle->>validateConditionHandle: Build valid handles set (condition-{id}, condition-{blockId}-if/else-if/else)
                validateConditionHandle-->>validateSourceHandleForBlock: Return validation result
            end
            
            validateSourceHandleForBlock-->>createValidatedEdge: Return validation result
        end
        
        alt Invalid source handle
            createValidatedEdge->>editWorkflow: Skip edge (log invalid_source_handle)
        end
        
        createValidatedEdge->>validateTargetHandle: Validate target handle
        validateTargetHandle-->>createValidatedEdge: Return validation result
        
        alt Invalid target handle
            createValidatedEdge->>editWorkflow: Skip edge (log invalid_target_handle)
        end
        
        createValidatedEdge->>WorkflowState: Add validated edge
        createValidatedEdge-->>editWorkflow: Return success
    end
    
    editWorkflow->>WorkflowState: Return modified workflow state with skipped items
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.

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@Sg312 Sg312 merged commit 83d813a into staging Dec 18, 2025
10 checks passed
@waleedlatif1 waleedlatif1 deleted the fix/copilot-edge-handles branch December 19, 2025 09:28
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