Using State Diff Management
Cedar OS provides a powerful state diff management system that enables you to track changes, visualize differences, and give users control over accepting or rejecting modifications. This is particularly useful for AI-generated changes where users need to review and approve modifications before they’re applied.Overview
The State Diff system provides:- Change tracking - Automatically track differences between old and new states
- Visual diff markers - Add visual indicators to show what’s been added, changed, or removed
- User control - Accept or reject changes individually or in bulk
- History management - Undo/redo support with full history tracking
- Custom computations - Transform states to add diff markers or other metadata

State diff system in action - tracking and visualizing changes with accept/reject controls
Quick Start
Basic Usage with useDiffState
The simplest way to use diff tracking is with the useDiffState hook, which works like React’s useState but with automatic diff tracking:
Adding Visual Diff Markers
To visualize changes in your UI, use thecomputeState function to add diff markers:
Diff Modes
Cedar OS supports two diff modes that control how changes are handled:- defaultAccept
- holdAccept
Changes are immediately visible to users but can be reverted:Best for:
- Real-time collaboration
- Non-critical changes
- Quick iterations
Managing Diffs
Accepting and Rejecting Changes
UseuseDiffStateOperations to access diff management functions:
Individual Diff Management
For granular control over specific changes:Advanced Usage
Custom State Setters
Define custom operations that work with diff tracking:Subscribing to Diff Values
Monitor specific fields within your diff state:Using with AI Agents
Integrate diff tracking with AI-generated changes:Best Practices
Use Descriptive Keys
Choose meaningful, unique keys for your diff states to avoid conflicts and
make debugging easier.
Add Descriptions
Provide clear descriptions for states and setters to help AI agents understand
your data structure.
Choose the Right Diff Mode
Use
holdAccept for critical changes and defaultAccept for non-critical
updates.Clean Up Diff Markers
Remember to handle diff markers in your UI components to provide visual
feedback.
Common Patterns
Pattern 1: Form with Diff Tracking
Pattern 2: Collaborative Editing
Troubleshooting
Changes not appearing with diff markers
Changes not appearing with diff markers
Ensure you’re using a
computeState function that adds diff markers:Infinite loops when using setValue
Infinite loops when using setValue
Don’t call
setValue directly in the component body. Use it in event handlers or effects:State not syncing with UI
State not syncing with UI
Make sure you’re using the state value returned by the hook, not a separate state variable:
Next Steps
- Learn about State Diff Internals to understand how the system works
- Explore Agentic State Access for AI integration
- Check out Human-in-the-Loop Workflows for approval processes

