Skip to content

Conversation

@Sg312
Copy link
Contributor

@Sg312 Sg312 commented Jan 28, 2026

Summary

  • Loop with empty collection would execute the body once
  • Subflow validations restored with correct order of ops

Type of Change

  • Bug fix

Testing

Tested with @icecrasher321

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 28, 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 28, 2026 8:08pm

Request Review

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Jan 28, 2026

Greptile Overview

Greptile Summary

Fixed a critical bug where loops with empty collections (forEach with empty arrays or for loops with 0 iterations) would incorrectly execute the loop body once instead of being skipped entirely.

Key Changes:

  • Reordered loop exit edge creation to occur after boundary checks and added condition to only create edges when target is outside the loop
  • Added guard in edge manager to prevent incorrect node reactivation when taking LOOP_EXIT or PARALLEL_EXIT routes
  • Enhanced debug logging for better observability during edge processing
  • Updated log message to be generic across all loop types

The fix ensures proper control flow by preventing the loop body from executing when the initial condition evaluation returns false (empty collection case).

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The changes are well-focused on fixing the specific bug, involve minimal modifications to critical execution paths, and have been tested. The logic is sound and addresses the root cause by preventing incorrect edge activation when loops should be skipped.
  • No files require special attention

Important Files Changed

Filename Overview
apps/sim/executor/dag/construction/edges.ts Moved loop exit edge creation after boundary check and added condition to only create when target is outside loop
apps/sim/executor/execution/edge-manager.ts Added guard to prevent node reactivation when exiting loop or parallel blocks

Sequence Diagram

sequenceDiagram
    participant Engine as Execution Engine
    participant NodeOrch as Node Orchestrator
    participant LoopOrch as Loop Orchestrator
    participant EdgeMgr as Edge Manager
    participant DAG as DAG

    Note over Engine,DAG: Loop with Empty Collection Flow

    Engine->>NodeOrch: Execute loop sentinel start
    NodeOrch->>LoopOrch: evaluateInitialCondition(loopId)
    
    alt forEach with empty items
        LoopOrch-->>NodeOrch: return false (skip loop)
    else for with 0 iterations
        LoopOrch-->>NodeOrch: return false (skip loop)
    else while/doWhile
        LoopOrch-->>NodeOrch: evaluate condition
    end

    NodeOrch-->>Engine: {sentinelStart: true, shouldExit: true, selectedRoute: LOOP_EXIT}
    
    Engine->>EdgeMgr: getNextNodes(output)
    EdgeMgr->>EdgeMgr: Activate LOOP_EXIT edge
    
    Note over EdgeMgr: NEW: Skip node reactivation check<br/>when selectedRoute is LOOP_EXIT
    
    EdgeMgr->>DAG: Find target via LOOP_EXIT edge
    Note over DAG: Edge created only if target<br/>is outside loop (originalTarget check)
    
    EdgeMgr-->>Engine: [nextNode outside loop]
    Engine->>Engine: Continue execution after loop
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.

2 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@icecrasher321
Copy link
Collaborator

@cursor review

1 similar comment
@icecrasher321
Copy link
Collaborator

@cursor review

@icecrasher321 icecrasher321 merged commit 01e0723 into staging Jan 28, 2026
11 checks passed
@waleedlatif1 waleedlatif1 deleted the fix/loop-empty-collection branch January 29, 2026 00:27
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.

3 participants