Skip to content

Conversation

@waleedlatif1
Copy link
Collaborator

Summary

  • fixed issue where code in function blocks would get nuked after undo/redo cycles
  • when undoing/redoing block operations, now captures latest subblock values to preserve edits

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 Jan 19, 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 19, 2026 8:14pm

Request Review

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Jan 19, 2026

Greptile Summary

Fixed critical undo/redo bug where subblock values (like code in function blocks) were lost during undo/redo cycles.

Key Changes:

  • Extracted inline logic into two utility functions: captureLatestEdges and captureLatestSubBlockValues in apps/sim/stores/undo-redo/utils.ts
  • Updated both undo and redo operations to capture and preserve latest subblock values before removing blocks
  • Applies fix to both BATCH_REMOVE_BLOCKS operations in undo (line 449-461) and redo (line 1142-1154) code paths
  • Added comprehensive test suite with 24 test cases covering edge cases like null/undefined values, empty strings, and zero numeric values

Technical Implementation:
The fix captures the current state of subblock values using mergeSubblockState before block removal operations, then updates both the operation and its inverse with these values. This ensures that when blocks are re-added during undo/redo, their subblock values are restored from the captured state rather than stale snapshots.

Confidence Score: 5/5

  • This PR is safe to merge with no blocking issues
  • Well-designed fix with proper extraction to utility functions, comprehensive test coverage (24 test cases), consistent application in both undo and redo paths, and follows existing patterns in the codebase
  • No files require special attention

Important Files Changed

Filename Overview
apps/sim/stores/undo-redo/utils.ts extracted inline logic into reusable utility functions captureLatestEdges and captureLatestSubBlockValues
apps/sim/hooks/use-undo-redo.ts refactored to use new utility functions and preserve subblock values in both undo and redo operations
apps/sim/stores/undo-redo/utils.test.ts comprehensive test coverage for new utility functions with edge cases

Sequence Diagram

sequenceDiagram
    participant User
    participant Hook as use-undo-redo
    participant Utils as undo-redo/utils
    participant Store as WorkflowStore
    participant SubBlockStore
    
    User->>Hook: Undo batch remove operation
    Hook->>Store: Get current edges
    Hook->>Utils: captureLatestEdges(edges, blockIds)
    Utils-->>Hook: Latest edges for blocks
    
    Hook->>Store: Get current blocks
    Hook->>Utils: captureLatestSubBlockValues(blocks, workflowId, blockIds)
    Utils->>Utils: mergeSubblockState(blocks, workflowId, blockId)
    Utils->>Utils: Extract non-null/undefined subblock values
    Utils-->>Hook: Latest subblock values
    
    Hook->>Hook: Update operation.data.subBlockValues
    Hook->>Hook: Update inverse.data.subBlockValues
    Hook->>Store: batchRemoveBlocks(blockIds)
    
    Note over Hook,SubBlockStore: Values preserved for redo operation
    
    User->>Hook: Redo operation
    Hook->>Store: batchAddBlocks with preserved subBlockValues
    Hook->>SubBlockStore: setState with subBlockValues
    
    Note over User,SubBlockStore: Subblock values restored successfully
Loading

@waleedlatif1
Copy link
Collaborator Author

@greptile

@waleedlatif1 waleedlatif1 merged commit 037dad6 into staging Jan 19, 2026
11 checks passed
@waleedlatif1 waleedlatif1 deleted the fix/code-subblock branch January 19, 2026 20:19
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