Principle:ARCANGEL0 EVA Workspace Snapshot Restore
| Knowledge Sources | |
|---|---|
| Domains | File_System, Undo_System, Security |
| Last Updated | 2026-10-29 14:00 GMT |
Overview
Technique for capturing bounded workspace snapshots, computing forward diffs as base64 before-images, and restoring files to a prior conversation point — with path traversal protection, symlink rejection, and sensitive-file exclusion.
Description
When an autonomous agent modifies files in a workspace, the operator may need to undo those changes — rolling back to a prior conversation turn. The workspace snapshot restore pattern:
- Captures snapshots: Walks the workspace directory (skipping .git, .ssh, .venv, etc.), reads regular files (max 2MB each, 64MB total), and stores them as a path→bytes dict.
- Diffs snapshots: Compares before/after snapshots to produce base64-encoded before-images for changed files (or `None` for newly created files). Delta capped at 8MB.
- Restores changes: Applies ordered change sets in reverse — writing before-images back via atomic temp-file replacement, and deleting files that were created (encoded as `None`).
Security is critical:
- Path traversal protection: Rejects absolute paths and `..` components.
- Symlink rejection: Never follows symlinks during capture or restore; checks that parent directories resolve within the workspace root.
- Sensitive file exclusion: Skips `.env`, `id_rsa`, `*.pem`, `credentials.json`, `secrets.json`, etc.
- Atomic restoration: Uses `NamedTemporaryFile` + `os.replace` for crash-safe file writes.
Usage
Apply this principle when:
- Building an undo/rewind system for an agent that modifies files.
- Needing to roll back file changes to a specific conversation turn.
- Protecting against path traversal and symlink escape in file restoration.
- Excluding sensitive files from snapshots.
Theoretical Basis
The snapshot is a bounded copy-on-read:
# Abstract algorithm (NOT real implementation)
def capture(root):
snapshot = {}
for dirpath, dirs, files in os.walk(root, followlinks=False):
dirs[:] = [d for d in dirs if d not in SKIP_DIRS and not is_symlink(d)]
for f in files:
path = os.path.join(dirpath, f)
if is_symlink(path) or is_sensitive(path):
continue
if file_size(path) > MAX_FILE or total > MAX_TOTAL:
return None
snapshot[relative_path(path)] = read_bytes(path)
return snapshot
Diffing produces before-images only (not after-images, since the after state is the current file):
# Abstract algorithm (NOT real implementation)
def diff(before, after):
changed = {}
for path in before.keys() | after.keys():
if before.get(path) != after.get(path):
if path not in before:
changed[path] = None # newly created
else:
changed[path] = base64(before[path])
return changed
Practical Guide
- Never follow symlinks during capture or restore — use `followlinks=False` and check `is_symlink()`.
- Enforce total size limits — abort if the workspace exceeds a reasonable cap (64MB).
- Store before-images, not after-images — the after state is the current file on disk.
- Verify parent paths resolve within root before writing — prevent symlink escape.
- Use atomic writes (temp file + os.replace) so a crash mid-restore doesn't corrupt files.
- Skip sensitive files by name pattern — credentials, keys, environment files.
Related Pages
Implemented By
- Implementation:ARCANGEL0_EVA_capture_workspace
- Implementation:ARCANGEL0_EVA_diff_workspace
- Implementation:ARCANGEL0_EVA_apply_undo_changes