checkpoints

package
v0.16.397 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: AGPL-3.0 Imports: 24 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package checkpoints keeps per-conversation snapshots of a working folder in a shadow git repository (one per tab and folder, under the runtime's data folder) and returns the folder to one of them. The repositories, their refs, commit messages and the rewind log are laid out exactly as the previous runtime laid them out, so either runtime reads what the other wrote.

Index

Constants

View Source
const (
	KindTurn         = "turn"
	KindBeforeRewind = "before-rewind"
)
View Source
const (
	Throttled    = "throttled"
	TooManyFiles = "too-many-files"
	GitFailed    = "git-failed"
)

Snapshot outcomes besides success.

View Source
const (
	NotFound       = "not-found"
	SnapshotFailed = "snapshot-failed"
	Busy           = "busy"
)

Rewind outcomes besides success.

Variables

View Source
var DefaultLimits = Limits{
	MaxFileBytes:   5 * 1024 * 1024,
	MaxFiles:       20_000,
	MaxCheckpoints: 30,
	MinIntervalMs:  3000,
	MaxDiffBytes:   600_000,
	GitTimeout:     30 * time.Second,
}

Functions

func IsSafeRelPath

func IsSafeRelPath(rel string) bool

IsSafeRelPath: a relative path with no empty, ".", ".." or ".git" part.

func IsTurnEnd

func IsTurnEnd(prev, next string) bool

IsTurnEnd is whether a change of the agent's state ends a turn.

func ValidID

func ValidID(id string) bool

ValidID is whether id names a checkpoint.

func ValidTabID

func ValidTabID(id string) bool

ValidTabID is the previous runtime's check of a tab id.

Types

type API

type API struct {
	Store *Store
	// Tab is an agent tab: its tmux session and the agent's state; ok false when there is no such agent tab.
	Tab func(tabID string) (session, cliState string, ok bool)
	// Cwd is a tmux session's working folder, "" when unknown.
	Cwd func(session string) string
}

API is the checkpoints route of the platform's pages, past the API's own checks (a caller with a page session): GET ?tabId lists a conversation's snapshots and rewinds, GET ?tabId&id is the diff from the working folder to one, POST {tabId, id} returns the working folder to it. The folder is the tab's own, never a parameter.

func (*API) Serve

func (a *API) Serve(req Request) (int, any, http.Header)

Serve answers a request: status, body (a JSON value, a TextAnswer, or nil for none) and headers.

type Checkpoint

type Checkpoint struct {
	ID    string
	Kind  string
	At    float64
	Tree  string
	Files float64
}

Checkpoint is one snapshot: its id ("<ms>-<kind>[-<n>]"), when it was taken, its tree and how many files it holds.

func (Checkpoint) View

func (c Checkpoint) View() *jsvalue.Object

View is a checkpoint as the API shows it.

type Diff

type Diff struct {
	Diff      string
	Truncated bool
	Files     []*jsvalue.Object
}

Diff is what a rewind would change, as a patch from the working folder to the snapshot.

func (*Diff) JSON

func (d *Diff) JSON() *jsvalue.Object

JSON is the diff as the API answers it.

type Hooks

type Hooks struct {
	// IsBusy is asked once the snapshot before the rewind exists and before anything is written: true stops it.
	IsBusy func() bool
	// BeforeApply runs once the rewind is decided; BeforeReplace right before a file is replaced or deleted.
	BeforeApply   func()
	BeforeReplace func(rel string)
}

Hooks are the moments a rewind asks about or lets a test act in.

type Limits

type Limits struct {
	MaxFileBytes   int64
	MaxFiles       int
	MaxCheckpoints int
	MinIntervalMs  int64
	MaxDiffBytes   int
	GitTimeout     time.Duration
}

type Param

type Param struct {
	Value   any
	Present bool
}

Param is a request value as the previous runtime's handler saw it: absent, or any JSON value (a query parameter is a string, a repeated one an array).

type Request

type Request struct {
	Method      string
	ContentType string
	TabID, ID   Param
}

Request is what the handler reads of a request.

type RewindResult

type RewindResult struct {
	Reason  string
	Before  Checkpoint
	Changed []string
	Skipped []Skipped
}

RewindResult is a rewind done (Reason "") or refused.

func (*RewindResult) JSON

func (r *RewindResult) JSON() *jsvalue.Object

JSON is the answer to a done rewind.

type Scope

type Scope struct {
	Dir, Root string
	Limits    Limits
	// contains filtered or unexported fields
}

Scope is one tab's folder and its shadow repository.

func (*Scope) DiffTo

func (sc *Scope) DiffTo(id string) (*Diff, error)

DiffTo compares the working folder with a snapshot; nil when there is no such snapshot. It writes nothing to the working folder (the shadow index is refreshed).

func (*Scope) List

func (sc *Scope) List() ([]Checkpoint, error)

List is the checkpoints, the newest first.

func (*Scope) Rewind

func (sc *Scope) Rewind(id string, hooks Hooks) (RewindResult, error)

Rewind returns the working folder to a snapshot. The current state is snapshotted first (no rewind without it), only files both states know are written, and a file that changed in the meantime is skipped.

func (*Scope) Rewinds

func (sc *Scope) Rewinds() []any

Rewinds is the log of the rewinds, as written.

func (*Scope) Take

func (sc *Scope) Take(now int64) (Checkpoint, string)

Take snapshots the working folder as a turn's checkpoint; the person's own repository is never touched. The reason is "" on success.

type Skipped

type Skipped struct {
	Path, Reason string
	// Blob holds the content found in a file that turned out to have changed, kept in the shadow repository.
	Blob string
}

Skipped is a file the rewind left alone, and why.

type Store

type Store struct {
	// Dir holds the repositories (<data>/checkpoints).
	Dir  string
	Home string
	// Namespace names the refs (refs/<ns>/checkpoints/, refs/<ns>/saved/) and the commits' author (<ns>@localhost).
	Namespace string
	// TmpTag marks the files a rewind stages beside the ones it replaces (<file>.<tag>-<hex>.tmp).
	TmpTag string
	// Pool shares one object store between the repositories of a working folder (pool.go).
	Pool   bool
	Limits *Limits
	// Now is the clock in milliseconds; nil for the real one.
	Now func() int64
}

Store is where the shadow repositories live and how they are named.

func (*Store) Scope

func (s *Store) Scope(tabID, cwd string) (*Scope, error)

Scope is the shadow repository of a tab's folder: the folder resolved, never the root or the home folder.

func (*Store) SnapshotTurnEnd

func (s *Store) SnapshotTurnEnd(tabID, cwd string)

SnapshotTurnEnd snapshots a tab's folder once its agent's turn ended; a failure is logged and never reaches the agent.

func (*Store) Sweep

func (s *Store) Sweep(pause time.Duration)

Sweep hands over the objects of every repository that never did, the ones of closed tabs too, and collects the pools. One repository at a time, with a pause, so a data folder of hundreds of them does not take the disk.

type TextAnswer

type TextAnswer string

TextAnswer is a plain-text answer (the dispatcher's own failure).

Directories

Path Synopsis
Package scripttest replays the scripted cases the previous runtime's modules ran (the golden files of the checkpoint store and of the routes over git): the same file and git actions in a temporary folder, and the same dumps of a working folder and a shadow repository, so a Go test compares like with like.
Package scripttest replays the scripted cases the previous runtime's modules ran (the golden files of the checkpoint store and of the routes over git): the same file and git actions in a temporary folder, and the same dumps of a working folder and a shadow repository, so a Go test compares like with like.

Jump to

Keyboard shortcuts

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