Skip to main content

Using State Diff Management

This is very early beta for a very complex feature. We recommend joining the Discord or emailing jesse@cedarcopilot.com for support and feedback.
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
Animation showing the Cedar OS state diff system tracking changes, displaying visual diff markers, and allowing users to accept or reject modifications

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 the computeState function to add diff markers:

Diff Modes

Cedar OS supports two diff modes that control how changes are handled:
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

Use useDiffStateOperations 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

Ensure you’re using a computeState function that adds diff markers:
Don’t call setValue directly in the component body. Use it in event handlers or effects:
Make sure you’re using the state value returned by the hook, not a separate state variable:

Next Steps