Skip to content

fix(undo): Undo all removes folders a chat made for a file it then deleted - #5443

Merged
kovtcharov-amd merged 2 commits into
amd:mainfrom
kovtcharov:claude/undo-all-leftover-folders
Oct 23, 2026
Merged

kovtcharov-amd merged 2 commits into
amd:mainfrom
kovtcharov:claude/undo-all-leftover-folders

Conversation

@kovtcharov

Copy link
Copy Markdown
Contributor

Undo all left empty folders behind when a chat created a file inside new folders and later deleted it. Across the whole chat that file counts as unchanged, so Undo all skipped it, along with the folders made for it. Undo all now removes those folders as well. It keeps any folder that holds other files and names it, the same way undoing one turn does.

This was the remaining low-severity finding from the bot review of #5283. Its other findings landed with #5326.

Test plan

  • pytest tests/unit/agents/test_session_snapshots.py tests/unit/agents/test_file_snapshots.py tests/unit/chat/ui/test_changes_router.py: 79 passed. Both new tests fail without the fix.
  • python util/lint.py --black --isort --pylint --flake8 is clean.

…leted

A file a chat created and later deleted resolves to "unchanged" across the
chat, so Undo all skipped it and left the empty folders made for it behind.
Undo all now removes those folders too, and reports any it keeps because
they hold other files.
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Request changes

This fixes the main case: Undo all now removes empty folders left by a file the chat made and then deleted. But the cleanup handles folders one file at a time, so it can leave an empty folder behind and wrongly tell the user it was kept because it "holds other files".

  • A parent folder can be left behind, empty and wrongly reported. Say a chat writes tmp/notes.md, then writes and deletes tmp/sub/x.txt. The first cleanup pass tries tmp while tmp/sub still exists, so it fails and names tmp as kept. Then the new pass removes tmp/sub. The user ends up with an empty tmp and a message saying it holds their files. The same happens with two such files in sibling folders (tmp/a/x, tmp/b/y). Fix: clean up all the folders in a single pass, deepest first, the way the existing per-turn cleanup already does.

Real-world evidence

N/A: this is a backend change to snapshot restore with no evidence bundle. The verdict rests on static review plus a local repro script that calls restore_session directly (output below).

🔍 Technical details

🟡 Folder cleanup runs per span, after _apply's pass (src/gaia/agents/base/file_snapshots.py:872-883)

_apply already sorts all created_dirs by depth and removes the deepest first (file_snapshots.py:644-654). The new loop runs after that pass and goes span by span, so a folder _apply failed to remove (because a made-and-deleted child dir still existed), or one shared by two made-and-deleted spans, gets tried before its children are gone. It is never retried, and it ends up in kept_folders. I reproduced both cases against the PR head:

parent:   tmp exists True contents [] kept ['…/work/tmp']
siblings: tmp exists True kept ['…/work/tmp']

Fix: give the extra dirs to _apply so everything goes through one depth-sorted pass, and delete the new loop.

    leftover = [
        d
        for span in _spans(turns, include_unchanged=True)
        if span.change == "unchanged" and not span.first["existed"]
        for d in span.first.get("created_dirs") or []
    ]
    # A file made and then deleted within the chat leaves only its folders.
    kept_dirs = _apply(steps, session_dir / "rollback", extra_dirs=leftover)

In _apply, add extra_dirs: Iterable[str] = () and use created = {d for step in steps for d in step.created_dirs} | set(extra_dirs). This also removes the duplicated rmdir/kept_dirs block. Add a test for the parent case (tmp/notes.md created, tmp/sub/x.txt created then deleted → tmp gone, kept_folders == []).

Strengths

  • Uses rmdir only, so a folder holding anything else is never deleted. No data-loss risk.
  • Both new tests target the exact before/after behaviour, including the "kept and named" path.

A parent folder could be left behind empty and reported as holding other
files: the extra pass for made-and-deleted files ran per file after the
main pass, so a parent was tried before its made-and-deleted child folder
was gone. Those folders now join the same depth-sorted pass.
@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

Fixed: Undo all now clears every folder in one deepest-first pass, so a parent emptied by a made-and-deleted subfolder is removed and no longer reported as holding other files.

Technical details
  • The made-and-deleted folders now go to _apply as extra_dirs and join its depth-sorted rmdir pass; the separate per-span loop is gone.
  • New tests: tmp/notes.md made plus tmp/sub/x.txt made then deleted leaves no tmp; two siblings (tmp/a/x.txt, tmp/b/y.txt) made then deleted leave no tmp. Both fail on the previous commit.
  • Local: test_session_snapshots.py 19 passed; tests/unit/agents -k "snapshot or undo or restore" 96 passed. Black and isort are clean.
  • Commit: e5485ba

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Oct 23, 2026
Merged via the queue into amd:main with commit 7fff9fc Oct 23, 2026
34 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the claude/undo-all-leftover-folders branch October 23, 2026 07:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants