StateSense
All posts
Technical Deep DiveJanuary 5, 20257 min read

How StateSense Handles Large AI Refactors

When Copilot rewrites 50 files, VS Code's undo history is useless. It tracks edits per-file and per-editor — not workspace-wide. Close a file and its undo history is gone.

StateSense solves this with incremental, workspace-scoped snapshots. Here's how the system works technically.

Snapshot triggers

StateSense's EventEngine monitors the workspace file system using VS Code's `FileSystemWatcher`. Raw events are filtered and debounced to avoid creating thousands of snapshots from a single refactor.

The debounce system works like this: 1. File change detected → reset a 10-second timer 2. Timer fires → check if the accumulated change set meets any trigger threshold 3. If yes → create a snapshot of the current workspace state

This means a 30-second AI refactor that touches 40 files creates one snapshot (just before the timer fires) rather than hundreds.

Incremental storage

Each snapshot stores only files that changed since the previous snapshot. A typical snapshot from an AI refactor might store 20–50 files. With gzip compression, this is usually 5–50 KB per snapshot.

The SnapshotEngine maintains a list of "tracked files" — files that have been modified since the initial workspace state. To create a snapshot, it reads the current state of all tracked files and stores them as a compressed diff object.

Full restoration

Restoration is where incremental snapshots get interesting.

To restore snapshot N, StateSense needs to reconstruct the complete workspace state at that point in time. It does this by:

1. Starting from the earliest snapshot that contains the base state of each file 2. Replaying all snapshots up to N, applying each file's state in order 3. For files that were deleted between the target snapshot and now, restoring their content 4. For files created after the target snapshot, reverting them to their state in git HEAD

This ensures a complete and accurate workspace reconstruction, not just a partial restore of files that happened to change in that specific snapshot.

The fast-change window

One of the more subtle features is the fast-change window. When StateSense detects that multiple files are changing in rapid succession (more than a configurable threshold), it opens a 5-second window during which it monitors for the "quiet period" that signals the AI has finished generating.

This is what captures the exact moment before and after an AI tool runs — giving you a snapshot of the pre-AI state that you can always roll back to.