checkpoint

package
v0.31.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package checkpoint provides file-level undo for agent turns.

Before each write/edit tool modifies a file, Capture snapshots its current content. When the user runs /undo, the most recent checkpoint is popped and all files are restored to their pre-modification state.

Checkpoints are in-memory only (lost on process exit) and scoped to a session. A circular buffer keeps the most recent N checkpoints.

v1 limitation: only covers write/edit builtins. Changes via bash, MCP, extensions, or subagents are not tracked.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Checkpoint

type Checkpoint struct {
	ID        int
	CreatedAt time.Time
	Label     string     // e.g. first 60 chars of user message
	Files     []Snapshot // files modified in this turn
}

Checkpoint groups all snapshots from one agent turn.

type Snapshot

type Snapshot struct {
	Path    string      // absolute resolved path
	Content []byte      // original content; nil means file didn't exist (created by agent)
	Perm    fs.FileMode // original permissions
	// contains filtered or unexported fields
}

Snapshot holds the pre-modification state of one file.

func (Snapshot) ModifiedSinceCapture

func (s Snapshot) ModifiedSinceCapture() bool

ModifiedSinceCapture reports whether the file diverged from the state the agent left it in (recorded at Commit). A true result means an external edit happened after the agent's turn, so Undo must not overwrite it.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is an in-memory, thread-safe checkpoint store with a circular buffer.

func New

func New(capacity int) *Store

New creates a Store with the given capacity (number of checkpoints retained).

func (*Store) Begin

func (s *Store) Begin(label string)

Begin opens a checkpoint for the current turn. Thread-safe. If a previous active checkpoint exists (stale from aborted run), it's discarded.

func (*Store) Capture

func (s *Store) Capture(path string) error

Capture snapshots a file before modification. Thread-safe. No-op if already captured in this turn or if no active checkpoint. Returns error on I/O failure reading the file to snapshot.

func (*Store) Commit

func (s *Store) Commit()

Commit closes the active checkpoint and adds it to the ring. No-op if no active checkpoint or if no files were captured.

func (*Store) Discard

func (s *Store) Discard()

Discard closes the active checkpoint without saving.

func (*Store) List

func (s *Store) List() []Summary

List returns summaries of all checkpoints, newest first.

func (*Store) Repush

func (s *Store) Repush(cp *Checkpoint)

Repush puts a checkpoint back on the ring buffer after a failed undo. This allows the caller to retry /undo after fixing the restore failure.

func (*Store) Restore

func (s *Store) Restore(cp *Checkpoint) error

Restore applies a checkpoint as one transaction. It first verifies that no file has changed since the agent's turn, then saves every current state. If any restore operation fails, every file already touched is rolled back. The checkpoint remains owned by the caller, which can Repush it for retry.

func (*Store) Undo

func (s *Store) Undo() (*Checkpoint, error)

Undo pops the most recent checkpoint and returns it for the caller to restore. If restoration fails, call Repush to put it back for retry. Returns error if no checkpoints exist or if a turn is in progress.

func (*Store) UndoAndRestore

func (s *Store) UndoAndRestore() error

UndoAndRestore performs the entire undo transaction under one operation lock. A new run cannot open/capture/commit a checkpoint between popping this checkpoint and either committing its restoration or putting it back.

type Summary

type Summary struct {
	ID        int       `json:"id"`
	Label     string    `json:"label"`
	FileCount int       `json:"file_count"`
	CreatedAt time.Time `json:"created_at"`
}

Summary is the public info for listing checkpoints.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL