gptr_doc(path) binds a history document to this R process and is your explicit consent that
gptr writes agent blocks into it. Calls written in that document (run with source(),
gptr_source(), Rscript, knitr or Jupyter) get their blocks below them. Calls in no
document (the console, Rscript -e, standard input) are appended to it, except to a notebook,
as s_<id> = peter("prompt", <context names>) (later turns of that session as
s_<id> |> peter(...)). The statement ends with model = "<provider/model>" or
mode = "<mode>" when its turn ran with a model or mode the statement would not get without
them, as after /model, /mode, the plan menu or in a console started as peter(mode = plan):
for =, one other than the model and mode settings (the model is left out when the call
started the session without naming one); for |>, one other than the session's previous turn
in the transcript. A model is written as its full reference (model = opus is recorded as
"anthropic/..."). A model given as a provider object is not recorded on a session's first
statement. Your variable name and the other arguments are not recorded. A call in
another document is recorded only into that document, with its own consent; a run that cannot
ask for it records nothing and says so once per document.
Nothing is written until a block is recorded. gptr_doc(FALSE) removes the binding and
gptr_doc() shows it. sync = TRUE writes the blocks that were recorded while the document
could not be written, and says how many: a Jupyter notebook that was open (run it from another
R session after closing the notebook), or the deferred writes of an Rscript run that was
killed. Such blocks wait in the .gptr/ of the document's project, else in
tools::R_user_dir("gptr", "cache"), so any R process of the same user can apply them.
Arguments
- path
NULLto return the current binding,FALSEto remove it, or the path of an.R,.Rmd,.qmdor.ipynbdocument.- format
NULL(from the file extension) or the format of that extension:"r","rmd","qmd"or"ipynb";"transcript"records console turns into an.Rfile as one steered session.- sync
TRUEapplies the pending blocks of the document now and reports how many it wrote.
Value
With path = NULL, the binding list(path, format) or NULL, visibly. Otherwise the
previous binding, invisibly.
Details
Eligible top-level calls and pipe chains own document blocks; calls nested inside loops or
functions do not. In R Markdown, Quarto and notebooks a call's blocks are a chunk or cell after
its calling one, so when the document runs again, code after the call in that chunk or cell runs
before them: use what the agent made from a later chunk or cell (gptr says so once when a calling
chunk or cell does). Only blank lines may separate a call's statement from its blocks: after a
comment or code line between them the blocks belong to no call, so the call is asked again and
gets a new block while the old block's code still runs. A block records the R code the agent ran:
of a call that stopped part-way,
the expressions that ran, and code that changed your objects even where the model passed
record = false, which leaves out only inspection. Binding supplies recording consent even
when automatic recording is off, but .opts = list(record = FALSE) suppresses recording for
that call. Binding does not reconstruct earlier unrecorded turns. A narrative summary or
model-generated script draft needs review and execution checks before it can serve as a
reproducible workflow.
In a console transcript (an .R file) a turn's block opens with #< comment lines holding what
the console sent with the prompt: the head of each @file (at most 40 lines, redacted) and the
notes of earlier ! lines. Only that run, under the turn's s_<hex> statement, is sent again,
and none from a transcript whose gptr markers are malformed; a line of recorded code that would
continue the run gets one more # (##< ). In every format a whole comment line of recorded
code that reads as a ## Steer: or ## Follow-up: line or as a block marker gets one more #
too, so code the model wrote is never sent as your context or your steer and never ends its
block. The block's header names the objects the turn created (created=), as the header of
each direct R line does (# direct R (no model) created=p), and a comment line of ! code that
reads as a console line, such as # /tmp is scratch, or as a block marker gets one more #. A
block whose code holds a marker line inside a string is not written, and gptr says so. After
each /undo, /redo, /retry and ! line the transcript is read again from the top: a direct
R line that uses an object last created above it by an undone turn or by a line that is off is
turned off (#~ before each line, # direct R (no model; uses undone fit)) and back on when
that turn returns, so source() does not stop there; a whole fit = <value> makes fit and
uses only what <value> names. The console names the lines it turned off. Your own objects
stay in your session: an undo removes what the turn created and restores what it changed. So a
later turn that uses an object of a line that is off still stops source(), as turn blocks are
never turned off, and a direct R line that read an object the turn changed, such as
n = nrow(dat) after the turn cut dat, stays on and replays with the restored value. Names
are found with all.names(): a column or formula term with the same name can turn a line off,
and a name reached through a string, as in get("fit"), is not seen.
Options
Options of history documents (?gptr_options collects every option):
gptr.record (settings, "ask"): "auto" records into documents without asking, "off"
prevents automatic recording; an explicit binding supplies consent. Also the settings key
record, which a project can only tighten. A call's .opts = list(record = FALSE) opts out
even when bound; .opts = list(record = TRUE) retains the usual consent checks.
gptr.replay (settings, "auto"): the replay mode "auto", "replay", "live" or
"record" (also the GPTR_REPLAY environment variable); a call's replay = argument wins.
gptr.doc_output_lines (12): #> output lines recorded per execution.
gptr.doc_source_frames (TRUE): FALSE stops gptr from reading source() frames to find
calls in scripts sourced without srcrefs.
gptr.spill_days (7): gptr_cache("prune") removes temporary files older than this.
Examples
f = tempfile(fileext = ".R")
writeLines("library(gptr)", f)
gptr_doc(f)
gptr_doc()
#> $path
#> [1] "/tmp/Rtmp753Ve2/rel-site-1ab55e258a76/tmp/RtmpJaBmFu/file1ac1693b7f7e.R"
#>
#> $format
#> [1] "r"
#>
gptr_doc(FALSE)