checkpoint

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package checkpoint is fairpeer's snapshot-based edit safety net. Before a writer tool changes a file, the agent records the file's pre-edit content here, keyed to the current user turn; a frontend can then rewind the workspace (and, via the controller, the conversation) to an earlier turn.

It is deliberately git-free (like Claude Code's rewind): snapshots live beside the session, never touch the user's git, and work in a non-git directory. Only edit-tool changes are tracked — bash side effects are not (a shell command's targets can't be known in advance), which is why the capture hook only fires for tools that can Preview their change.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Checkpoint

type Checkpoint struct {
	Turn     int        `json:"turn"`
	Time     time.Time  `json:"time"`
	Prompt   string     `json:"prompt"`
	MsgIndex int        `json:"msgIndex"`
	Files    []FileSnap `json:"files"`
}

Checkpoint anchors the pre-edit state of every distinct file touched during one user turn. MsgIndex is len(Session.Messages) at the turn's start — the conversation-rewind boundary — persisted so a resumed session can rewind the conversation and fork, not just the code.

type FileSnap

type FileSnap struct {
	Path     string        `json:"path"`
	Content  *string       `json:"content"`
	Encoding *fileenc.Kind `json:"encoding,omitempty"`
}

FileSnap is one file's state at the moment it was first touched in a turn. Content == nil means the file did not exist then, so a restore deletes it.

type Meta

type Meta struct {
	Turn   int
	Time   time.Time
	Prompt string
	Paths  []string
}

Meta is the picker-facing summary of a checkpoint (no file contents).

type Store

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

Store holds a session's checkpoints in memory and, when dir is set, persists one JSON file per turn under it (cheap delete, corruption-isolated). All methods are safe for concurrent use — the agent snapshots from tool goroutines.

func New

func New(dir, root string) *Store

New returns a store for the given checkpoint dir and workspace root, loading any checkpoints already persisted under dir. A "" dir disables persistence (the store still works in memory for the session).

func (*Store) Begin

func (s *Store) Begin(turn int, prompt string, msgIndex int)

Begin opens a checkpoint for a new user turn, finalizing the previous one. The prompt labels it in the picker; msgIndex is the conversation-rewind boundary.

func (*Store) Bounds

func (s *Store) Bounds() map[int]int

Bounds returns turn → MsgIndex over all checkpoints (persisted + current), so the controller can rebuild its conversation-rewind boundaries after loading a resumed session's checkpoints from disk.

func (*Store) List

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

List returns every checkpoint's metadata, oldest turn first.

func (*Store) NextTurn

func (s *Store) NextTurn() int

NextTurn returns the turn number a new checkpoint should take: one past the highest existing turn (0 when empty), so a resumed session keeps numbering without colliding with checkpoints loaded from disk.

func (*Store) RestoreCode

func (s *Store) RestoreCode(fromTurn int) (written, deleted []string, err error)

RestoreCode reverts the workspace to its state at the start of turn `fromTurn`: for every file touched in turn fromTurn or later, it writes back that file's earliest recorded content (or deletes it when the earliest snapshot was nil). Returns the paths written and deleted.

func (*Store) Snapshot

func (s *Store) Snapshot(ch diff.Change)

Snapshot records the pre-edit state of the file a writer is about to change. Only the first touch of a path in the current turn is kept (that is its turn-start content). A no-op before the first Begin.

Jump to

Keyboard shortcuts

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