Skip to contents

gptr_rewind() is a branch-in-place on the one session object: it undoes the checkpoint records of the turns being left (newest first), redoes the records of a target on another branch (oldest first) and appends one gptr.rewind entry parented at the target (the session file stays append-only and the rewind survives a reload). It hands back the undone prompt in s$editor_text and returns s invisibly, so it pipes: session |> gptr_rewind() |> peter("Try another way"). Use gptr_fork() for a second session.

gptr_checkpoints() lists one row per turn of the active path (all = TRUE adds the turns of abandoned branches): turn; id, the last entry of the turn (pass it as to to gptr_rewind()); time, when the prompt was sent (local time); prompt (its first 60 characters); objects and files, "changed/restorable" counts (a changed object is restorable while its image is kept); held_mb and disk_mb, megabytes (2^20 bytes) of object images held in memory and on disk (in memory, for an object with more than 12.5 million strings an estimate, at or above the truth on average; on disk, the files' sizes); and branch ("active" or "abandoned").

Usage

gptr_rewind(
  s,
  turn = -1L,
  to = NULL,
  restore = c("all", "conversation", "workspace"),
  force = FALSE,
  preview = FALSE
)

gptr_checkpoints(s, all = FALSE)

Arguments

s

A gptr_session.

turn

Integer. Negative counts back from the last turn (-1 undoes the last turn); 0 goes back to before the first turn; k > 0 keeps turns 1..k of the active path.

to

An entry id from gptr_checkpoints(s, all = TRUE); overrides turn. Any entry works, including the end of an abandoned branch, which is how redo works.

restore

"all" (objects, files, session state and the conversation), "conversation" (only the conversation) or "workspace" (only objects, files and session state; the conversation stays where it is).

force

Also restore items that changed after the checkpoint (3-way conflicts).

preview

Return the plan instead: a data frame with one row per item (record, turn, action, checkpointer, item, restore, reason); nothing changes.

all

Also list the turns of abandoned branches.

Value

gptr_rewind() returns s, invisibly (the plan when preview = TRUE); s$last_rewind holds the report and s$editor_text the undone prompt, whose preloaded skills are s$last_rewind$skills. gptr_checkpoints() returns a gptr_checkpoints data frame (it prints at most 20 rows).

Details

Restores are 3-way: an object or file that changed after the turn (by you or another session) is kept and reported, unless force = TRUE. Reference objects (environments, R6, external pointers), objects too large for the undo limits (see Objects below), environment variables, loaded namespaces, the random-number state and effects outside R (network, databases, processes) are not restored. s$last_rewind$report lists each object, file and state item once, by its outcome after the whole rewind; items that were not restored raise the warning gptr_warning_rewind_partial. Protected files and files that configure R or gptr (.env, .Renviron, .Rprofile, renv.lock, keys under .ssh/; see gptr_security) are never copied to the checkpoint store, so a turn's edit or deletion of one is not undone. Directories are not tracked: an empty directory a turn created stays. Files outside the project and tempdir() are restored only after you confirm each path. Automatic file scans are bounded by time, entry and capture limits (see gptr_options); a scan that stops early is reported as incomplete coverage of the calls and turns that could write files (R code that only reads and assigns objects cannot). The files gptr's tools name (write and edit targets, literal paths in R code) are backed up before the call; other changes to unvisited files (computed paths in R code, shell commands, a CLI's own edits) have no automatic backup. After a full restore the model is told nothing (the next request's prefix is byte-identical to the earlier request at the target); after a partial one the next prompt carries a short <rewind> block listing what was not restored. Model code may rewind another session only when you approve that call.

Objects

Before a call that runs R code, gptr keeps a reference to every object up to undo.capture_max bytes and to every object the code is predicted to change up to undo.memory bytes. Nothing is copied or written to disk then: a kept object costs memory only when it is edited in place, and the edit then copies what it touches: all of a vector or matrix, one column of a data frame, one element of a list. A data.table the code changes by reference (:=, set()) is copied with data.table::copy() first. After every call and at the end of each request the images in memory are held to undo.memory bytes: the largest is spilled to the checkpoint store while it fits undo.disk, else the oldest is dropped (a non-interactive run without a .gptr/ workspace writes no image). Spilled images are held to undo.disk bytes per session, the oldest dropped first, and images older than undo.turns turns are dropped. An image is counted for what it does not share with the object when it is kept, and again when another image of the object leaves memory; an edit you make at the console between requests can make it hold more meanwhile. An object that cannot be undone is named with the reason (for its size, the limit to raise) when you are asked to approve the call, and in what the model is told after it.

The limits are the fields of the setting undo:

  • capture_max (1e8): bytes; every object up to this size is kept.

  • memory (1e9): bytes; the largest object kept that the code is predicted to change, and the images held in memory per session.

  • disk (2e9): bytes of images spilled to disk per session.

  • turns (20): images older than this many turns are dropped.

Read them with gptr_config()$undo. Change them with gptr_config(undo = list(memory = 4e9), .scope = "user") or, for this R process, options(gptr.undo.memory = 4e9). A field you leave out comes from the layers below; a second call at the same scope replaces the first, so name there every field you set.

A change made after the turn is kept and reported as a conflict when it rebinds or removes the object, replaces a list element, a column or an attribute, or changes one of the values gptr samples (at most 64 a vector). An in-place edit of other values (x[37] = 0 of a million) is not seen, and an undo or redo overwrites it. Environments, R6 objects and external pointers inside a restored list, data frame or S4 object are shared, not copied, so they keep their current state. Images in memory do not outlive the R process; an image spilled to disk is restored in a later R process only with force = TRUE.

Options

Options of checkpoints and rewind (?gptr_options collects every option):

  • gptr.checkpoint ("on"): "files" checkpoints files only, "off" nothing (also the settings key checkpoint).

  • gptr.undo.capture_max, gptr.undo.memory, gptr.undo.disk and gptr.undo.turns: the option layer of the setting undo (see Objects).

  • gptr.checkpoint_disk_bytes (2e9): bytes of the checkpoint store (file blobs and the spilled images of every session) before a notice; it deletes nothing, while undo.disk holds one session's spilled images.

  • gptr.checkpoint_days (30): unreferenced blobs older than this many days are deleted.

  • gptr.checkpoint_turns (100): file checkpoints older than this many turns are not restored.

  • gptr.checkpoint_track_file_max (1e6): bytes; the largest file the first scan stores.

  • gptr.checkpoint_track_total (1e8): bytes one automatic scan stores in all.

  • gptr.checkpoint_capture_max (5e7): bytes; a larger changed file gets no stored copy.

  • gptr.checkpoint_scan_budget (0.25): seconds of project scans per request; past them, its calls back up the files they name and one scan at its end records the rest, edits you made to the project meanwhile included.

  • gptr.checkpoint_scan_timeout (2): seconds; the longest an automatic scan runs. An incomplete scan also spends its request's scan budget.

  • gptr.checkpoint_rng (TRUE): report changes of the random-number state.

  • gptr.checkpoint_close_devices (FALSE): close graphics devices an undone turn opened.

Examples

d = tempfile("project")
dir.create(d)
op = options(gptr.project_root = d)
fake = gptr_fake_provider(list(
  list(tool = "r", input = list(code = "x = 1")), "Made x.",
  list(tool = "r", input = list(code = "x = x + 1")), "Added one."))
work = new.env()
session = peter("Make x", model = fake, mode = auto, envir = work)
#> gptr: thinking
#> gptr: r  x = 1
#> gptr: thinking
#> gptr: done · fake-1 · 2 requests · 3.2k tokens · 99% context left
session |> peter("Add one to x")
#> gptr: thinking
#> gptr: r  x = x + 1
#> gptr: thinking
#> gptr: done · fake-1 · 2 requests · 3.2k tokens · 99% context left
#> Added one.
#> idle · fake/fake-1 · 2 turns · 6.4k tokens · $0.0000 · sa1b0845a68
work$x
#> [1] 2
session |> gptr_rewind()
work$x
#> [1] 1
session$editor_text
#> [1] "Add one to x"
gptr_checkpoints(session, all = TRUE)
#> # Columns 1/2
#>   id        branch     turn  time          prompt        objects  files  held_mb
#> 1 bd623b58  active     1     2026-11-2...  Make x        1/1      0/0    0
#> 2 4cfb9150  abandoned  2     2026-11-2...  Add one to x  1/1      0/0    0
#> # Columns 2/2
#>   disk_mb
#> 1 0
#> 2 0
#> 
#> # 2 rows
#> Rewind with gptr_rewind(s, <turn>) or gptr_rewind(s, to = "<id>").
options(op)