Adding "undo" to an AI action in a Chrome extension: snapshot and restore tab groups
I build TabZipsa, a Chrome extension that sorts every open tab into native Chrome tab groups with one click. This week a developer gave me feedback that I can't stop thinking about: If one click rearranges 40 tabs and the result is off, you lose trust right away. Show the proposed groups, and give

I build TabZipsa, a Chrome extension that sorts every open tab into native Chrome tab groups with one click. This week a developer gave me feedback that I can't stop thinking about:
If one click rearranges 40 tabs and the result is off, you lose trust right away. Show the proposed groups, and give me an undo right after.
Right now my extension applies the AI result immediately and has no undo. If you build anything where AI changes the user's stuff (tabs, bookmarks, files), you probably have the same gap. Here is the approach I'm prototyping, using only chrome.tabs and chrome.tabGroups.
Tab groups in Chrome are always contiguous, so a snapshot needs only three things per tab: window, index and group. Store group titles and colors separately.
async function snapshotTabs ( ) { const tabs = await chrome. tabs. query ( { } ); const groups = await chrome. tabGroups. query ( { } ); return { takenAt: Date. now ( ), tabs: tabs. filter ( t =>! t. incognito &&! t. pinned ). map ( t => ( { id: t. id, windowId: t. windowId, index: t. index, groupId: t. groupId } ) ), groups: groups. map ( g => ( { id: g. id, title: g. title, color: g. color, collapsed: g. collapsed } ) ) }; } Enter fullscreen mode Exit fullscreen mode I skip pinned tabs (they can't move into the unpinned area anyway) and incognito tabs (they shouldn't be sent to an AI in the first place).
The order matters. If you regroup first and then move tabs, a move can pull a tab out of its group. So:
async function restoreSnapshot ( snap ) { const alive = new Set ( ( await chrome. tabs. query ( { } ) ). map ( t => t. id ) ); const tabs = snap. tabs. filter ( t => alive. has ( t. id ) ); if (! tabs. length ) return; await chrome. tabs. ungroup ( tabs. map ( t => t. id ) ); for ( const t of [... tabs ]. sort ( ( a, b ) => a. index - b. index ) ) { await chrome. tabs. move ( t. id, { windowId: t. windowId, index: t. index } ); } const byGroup = new Map ( ); for ( const t of tabs ) { if ( t. groupId === chrome. tabGroups. TAB_GROUP_ID_NONE ) continue; if (! byGroup. has ( t. groupId ) ) byGroup. set ( t. groupId, [ ] ); byGroup. get ( t. groupId ). push ( t. id ); } for ( const [ oldId, tabIds ] of byGroup ) { const meta = snap. groups. find ( g => g. id === oldId ); const newId = await chrome. tabs. group ( { tabIds } ); if ( meta ) { await chrome. tabGroups. update ( newId, { title: meta. title, color: meta. color, collapsed: meta. collapsed } ); } } } Enter fullscreen mode Exit fullscreen mode Tabs the user closed in the meantime are simply skipped; new tabs opened after the snapshot are left alone.
The same developer also asked for a preview. I think undo comes first: it costs one snapshot, it doesn't slow down the "one click" flow that other users told me they like, and it removes most of the risk. A preview can be an option for people who want it.
How do you handle "AI just changed my stuff" moments in your own tools? Preview, undo, or both?
Disclosure: I'm the developer of TabZipsa ( https://tabzipsa.com ). This post was written with AI assistance; the code is a prototype, so test it with your own edge cases.