Skip to content

PromptL nested loops bug - #49

Merged
neoxelox merged 1 commit into
mainfrom
cursor/promptl-nested-loops-bug-e439
Feb 25, 2026
Merged

PromptL nested loops bug#49
neoxelox merged 1 commit into
mainfrom
cursor/promptl-nested-loops-bug-e439

Conversation

@neoxelox

Copy link
Copy Markdown
Contributor

Fixes a bug in nested loops where inner loop elements were skipped due to stale AST node status.

The root cause was that child AST nodes were shared across for-loop iterations, and their completedAs status was not reset. When an outer loop re-invoked an inner loop, the completedAs status from a previous invocation would cause the first inner elements to be erroneously skipped. The fix introduces a loopInvocationCount to make completedValue unique across invocations and a clearNodeStatus function to recursively reset node status after loop completion.


Slack Thread

Open in Web Open in Cursor 

Root cause: In for-loop compilation, child AST nodes are shared across all
iterations. Each child gets marked with completedAs='step_${i}' after
processing. When a for-loop completes and is re-invoked (e.g., from an outer
loop), the iteration counter restarts at 0, causing completedValue 'step_0'
to match the stale completedAs 'step_0' from a prior invocation. This caused
the first elements of inner loops to be silently skipped.

Fix:
1. Use invocation-scoped completedValue ('step_${invocationCount}_${i}')
   so values are unique across for-loop invocations.
2. Clear descendant node status (completedAs, scopePointers) when the
   for-loop completes all iterations, preventing stale state from
   affecting future invocations.

Added 12 comprehensive test cases covering:
- Nested loops with varying inner array sizes
- Three levels of nesting
- Nested loops with conditionals and index variables
- Nested loops with else blocks
- Chain steps inside nested loops with varying sizes

Co-authored-by: Alex Rodríguez <me@arn.sh>
@cursor

cursor Bot commented Feb 25, 2026

Copy link
Copy Markdown

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@neoxelox
neoxelox marked this pull request as ready for review February 25, 2026 13:39
@neoxelox
neoxelox merged commit 91bfc91 into main Feb 25, 2026
2 checks passed
@neoxelox
neoxelox deleted the cursor/promptl-nested-loops-bug-e439 branch February 25, 2026 13:40
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