Skip to content
Esc
↑↓navigate↵open⌘Jpreview
Start
On this page

Rewind file changes with checkpointing

Every file the agent writes through a file tool is reported. The SDKs surface those writes as file_modified events, and git, CLI isolation flags, and hooks give you ways to review or revert them. This page covers each layer.

How tracking works

Whenever the agent calls a file tool (write_file, append_file, apply_patch, search_replace, multi_file_edit, notebook_edit, format_file, delete_path, rename_path, copy_path, add_dependency, remove_dependency, or git_checkout), the CLI emits a file_modified event over JSON-RPC. The event carries the file path, a changeType of create, modify, or delete, and the ID of the tool call that made the change.

Field names follow each SDK’s conventions: filePath, changeType, and toolId in TypeScript; file_path, change_type, and tool_id in Python; FilePath, ChangeType, and ToolID in Go; filePath(), changeType(), and toolCallId() in Java.

Shell commands (run_command) do not emit file_modified. If the agent edits files with sed, a code generator, or a build script, those changes appear only in git status, so always check the working tree as well as the event stream.

The CLI does not keep its own per-run copy of changed files. Use git, or the CLI options below, as the record you revert from.

Observe file modifications in real time

Match file_modified events while you stream a prompt. Use them to invalidate caches, refresh editor buffers, or track what the agent changed for an audit log.

const modified = new Set();

for await (const event of sdk.streamPrompt({ message: prompt })) {
  if (event.type === 'file_modified') {
    // event.changeType is 'create' | 'modify' | 'delete'; event.toolId links it to the tool call
    modified.add(event.filePath);
    editor.refresh(event.filePath);
  }
  if (event.type === 'agent_end') {
    console.log('Files touched:', [...modified]);
  }
}
modified = set()

async for event in sdk.stream_prompt(prompt):
    if event["type"] == "file_modified":
        # event["change_type"] is create, modify, or delete; event["tool_id"] links it to the tool call
        modified.add(event["file_path"])
        editor.refresh(event["file_path"])
    if event["type"] == "agent_end":
        print("Files touched:", sorted(modified))
modified := map[string]struct{}{}

for event := range events {
    switch e := event.(type) {
    case autohand.FileModifiedEvent:
        // e.ChangeType is create, modify, or delete; e.ToolID links it to the tool call
        modified[e.FilePath] = struct{}{}
        editor.Refresh(e.FilePath)
    case autohand.AgentEndEvent:
        fmt.Println("Files touched:", modified)
    }
}
Set<String> modified = new HashSet<>();

sdk.streamPrompt(new PromptParams(prompt), event -> {
    if (event instanceof Events.FileModifiedEvent fme) {
        // fme.changeType() is create, modify, or delete; fme.toolCallId() links it to the tool call
        modified.add(fme.filePath());
        editor.refresh(fme.filePath());
    } else if (event instanceof Events.AgentEndEvent) {
        System.out.println("Files touched: " + modified);
    }
});

Revert with git

The simplest, safest revert path is git. Start from a clean working tree (or commit a snapshot first) and record the commit you started from, so you can return to it whether or not the agent committed.

# Snapshot before the agent runs (only needed if git status shows changes)
git add -A && git commit -m "pre-agent snapshot"

# Record the snapshot commit
SNAP=$(git rev-parse HEAD)

# Run the agent
bun run my-agent.ts

# If unhappy and the agent did not commit: drop its edits and new files
git reset --hard HEAD && git clean -fd

# If unhappy and the agent made commits: return to the snapshot
git reset --hard "$SNAP"

Do not reset to the reflog entry HEAD@{1} here. Right after the snapshot commit, HEAD@{1} points to the commit before the snapshot, so it discards the work you just saved. Resetting to the recorded $SNAP hash is always correct.

If you let the agent commit on your behalf, restrict it to its own branch:

git switch -c agent/$(date +%s)
bun run my-agent.ts
# Review the diff before merging back to main
git diff main...HEAD

Limit and undo changes with the CLI

The CLI has no run-history command. Instead, it gives you ways to keep an agent’s changes away from your checkout, or to undo them:

  • --worktree [name] runs the session in an isolated git worktree on a new branch. See Worktrees.
  • --dry-run previews actions without applying mutations.
  • --patch (with --prompt) generates a git patch without applying changes; add --output <file> to write it to a file.
  • /undo in an interactive session reverts the last file change the agent recorded and removes the last conversation turn. It does not touch changes made by shell commands.
# Run in an isolated git worktree on its own branch; your main checkout is untouched
autohand --worktree agent-fix -p "Fix the failing auth tests"

# Preview actions without applying mutations
autohand --dry-run -p "Rename the config loader"

# Generate a git patch instead of applying changes, then review and apply it yourself
autohand -p "Upgrade the logger" --patch --output agent.patch
git apply --check agent.patch && git apply agent.patch

For unattended runs, add a file-modified hook that writes each change to an audit log. The hook receives HOOK_SESSION_ID, HOOK_PATH, and HOOK_CHANGE_TYPE as environment variables. Hooks do not run with --bare. See Hooks Reference.

{
  "hooks": {
    "hooks": [
      {
        "event": "file-modified",
        "command": "echo \"$HOOK_SESSION_ID $HOOK_CHANGE_TYPE $HOOK_PATH\" >> .autohand-audit.log",
        "description": "Audit every agent file write"
      }
    ]
  }
}

Best practices

  • Run agents inside a clean git working tree so revert is one command away.
  • Stream file_modified into your editor / IDE integration so users see changes the moment they happen.
  • For unattended runs, write file_modified paths to an audit log alongside the session ID. That pair lets you tie back any change to the agent and prompt that produced it.
  • Check git status after every run. Shell commands can change files without emitting file_modified.
  • Treat git (or your VCS of choice) as the source of truth for what changed and what to revert.

Last updated on

Was this helpful?

Send Feedback

Help us improve this documentation page. Your feedback is valuable.

We'll only contact you if we need clarification