Skip to content

refactor(agentflow): Simplify ReactFlow and Context state sync#5827

Merged
j-sanaa merged 5 commits intomainfrom
refactor/agentflow-sdk-clean-bidirectional-sync
Feb 24, 2026
Merged

refactor(agentflow): Simplify ReactFlow and Context state sync#5827
j-sanaa merged 5 commits intomainfrom
refactor/agentflow-sdk-clean-bidirectional-sync

Conversation

@j-sanaa
Copy link
Copy Markdown
Contributor

@j-sanaa j-sanaa commented Feb 24, 2026

  • Replaced brittle bidirectional sync with synchronous state updates - context operations now directly update both context state and ReactFlow local state via registered callbacks
  • Removed setTimeout-based sync logic that were prone to race conditions

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @j-sanaa, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors the state management within the Agentflow component, moving from an error-prone, time-delayed synchronization method to a direct and synchronous update strategy. The change aims to enhance the reliability and predictability of the application's state, particularly when user interactions or context-driven operations modify the ReactFlow canvas. By centralizing the control of local ReactFlow state updates within the Agentflow context, the system becomes more robust and easier to maintain, preventing inconsistencies that could arise from asynchronous updates.

Highlights

  • Simplified State Synchronization: Replaced a brittle bidirectional state synchronization mechanism between ReactFlow's local state and the Agentflow context with a more robust, synchronous approach.
  • Eliminated Race Conditions: Removed setTimeout-based synchronization logic that was prone to race conditions, leading to more predictable state updates.
  • Direct Local State Updates: Implemented a new registerLocalStateSetters function in the context, allowing context operations (like deleting or duplicating nodes) to directly update ReactFlow's local state, ensuring immediate consistency.
Changelog
  • packages/agentflow/src/Agentflow.tsx
    • Removed useRef flags (syncingToContext, syncingFromContext) and setTimeout calls previously used for bidirectional state synchronization.
    • Modified useEffect hooks for state synchronization to be one-way, pushing local ReactFlow changes to the context.
    • Introduced a call to registerLocalStateSetters to provide ReactFlow's local state update functions to the Agentflow context.
  • packages/agentflow/src/infrastructure/store/AgentflowContext.tsx
    • Added registerLocalStateSetters to the AgentflowContextValue interface and implemented it to store ReactFlow's local setNodes and setEdges functions using useRef.
    • Updated deleteNode, duplicateNode, updateNodeData, and deleteEdge functions to directly invoke the stored ReactFlow local state setters after modifying the context state, ensuring immediate UI reflection.
    • Imported useRef from 'react'.
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively refactors the state synchronization between ReactFlow and the Agentflow context. By replacing the previous bidirectional, setTimeout-based sync with a synchronous, callback-based approach, you've eliminated a source of brittleness and potential race conditions. The new logic is much clearer and more robust, with context operations now directly updating both the context state and ReactFlow's local state. This is a great improvement for the stability and maintainability of the component.

@j-sanaa
Copy link
Copy Markdown
Contributor Author

j-sanaa commented Feb 24, 2026

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request successfully refactors the state synchronization between ReactFlow and the AgentflowContext. By replacing the previous brittle bidirectional sync (which relied on setTimeout and refs to prevent loops) with a more direct, synchronous approach using registered callbacks, the code is now much simpler and less prone to race conditions. The use of refs within the context provider to store local state setters is a clever way to maintain the dual-state architecture while ensuring consistency. I have suggested one minor improvement to ensure node normalization is applied consistently across both state sources during context-initiated updates.

@j-sanaa j-sanaa marked this pull request as ready for review February 24, 2026 00:39
@j-sanaa j-sanaa merged commit 5cc657b into main Feb 24, 2026
7 checks passed
@j-sanaa j-sanaa deleted the refactor/agentflow-sdk-clean-bidirectional-sync branch February 24, 2026 00:56
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