An R6 class for a child R process that runs code, usually in a sandbox, and can call tools in your session.
It wraps a callr::r_session and connects to it over a Unix domain
socket. When code in the child calls a tool, the child pauses, your
session runs the tool, and the child carries on with the result.
Methods
Method new()
Start a new SecureSession
Usage
SecureSession$new(
tools = list(),
sandbox = FALSE,
limits = NULL,
verbose = FALSE,
sandbox_strict = FALSE,
audit_log = NULL,
max_executions = NULL,
pre_execute_hook = NULL,
sanitize_errors = FALSE
)Arguments
toolsA list of
securer_tool()objects, or a named list of functions (the older format, still accepted)sandboxLogical, whether to use the OS sandbox. On macOS this is
sandbox-execwith a Seatbelt profile that blocks the network and only allows writes to temp directories. On Linux it's bubblewrap (bwrap), with the child in its own namespaces. On Windows there's no real sandbox: securer cleans the environment (privateHOMEandTMPDIR, emptyR_LIBS_USER) and can limit memory, CPU time, and process count with a Job Object. On other platforms the session runs without a sandbox.limitsAn optional named list of resource limits to apply to the child process via
ulimit. Supported names:cpu(seconds),memory(bytes, virtual address space),fsize(bytes, max file size),nproc(max processes),nofile(max open files),stack(bytes, stack size). Withsandbox = TRUE,NULL(the default) meansdefault_limits(), andlimits = list()means no limits. Withsandbox = FALSE,NULLmeans no limits.verboseLogical, whether to print what the session is doing with
message(). Handy for debugging. UsesuppressMessages()to hide them.sandbox_strictLogical, whether to stop if the sandbox tools aren't available on this platform (default
FALSE). WithTRUEandsandbox = TRUE, the session fails to start if it can't set up the sandbox. WithFALSE, you get a warning and the session runs without one.audit_logOptional path to a JSONL file. If you give one, the session appends a JSON line for each start, close, restart, run, and tool call. If
NULL(the default), nothing is written.max_executionsOptional integer, the maximum number of
$execute()calls this session allows. The default,NULL, means no limit. After that,$execute()stops with an error. Useful for throwaway sessions in agent code.pre_execute_hookOptional function taking a single
codeargument, called at the start of every$execute(). If it returnsFALSE, the code doesn't run and$execute()stops with an error. Anything else,NULLincluded, lets the code run. The default,NULL, means no hook.sanitize_errorsLogical, whether to remove file paths, process IDs, and host names from the errors
$execute()raises (defaultFALSE). WithTRUE, each message goes throughsanitize_error_message()first.
Method execute()
Run R code in the session
Usage
SecureSession$execute(
code,
timeout = 30,
validate = TRUE,
output_handler = NULL,
max_tool_calls = NULL,
max_code_length = 100000L,
max_output_lines = NULL
)Arguments
codeCharacter string of R code to execute
timeoutTimeout in seconds (default 30, the same as
execute_r()). PassNULLfor no timeout. For long jobs, pass a bigger number orNULL.validateLogical, whether to check the code for syntax errors before sending it to the child process (default
TRUE).output_handlerOptional function that gets each line of output from the child as it's printed. Either way, the output is also attached to the result as its
"output"attribute.max_tool_callsMaximum number of tool calls allowed in this execution, or
NULLfor unlimited (defaultNULL).max_code_lengthLargest
nchar(code)allowed (default 100000). Longer code is rejected before it's parsed, so a huge string can't tie up the session.max_output_linesMost lines of output to keep. The default,
NULL, means no limit. Past the limit, securer still reads the child's output but throws it away.
Method close()
Close the session and remove its temp files
Method format()
Format the session for printing
Method print()
Print method
Examples
# \donttest{
# Basic usage
session <- SecureSession$new()
session$execute("1 + 1")
#> [1] 2
session$close()
# With tools
tools <- list(
securer_tool("add", "Add numbers",
fn = function(a, b) a + b,
args = list(a = "numeric", b = "numeric"))
)
session <- SecureSession$new(tools = tools)
session$execute("add(2, 3)")
#> [1] 5
session$close()
# }
if (FALSE) { # \dontrun{
# With sandbox (requires platform-specific tools)
session <- SecureSession$new(sandbox = TRUE)
session$execute("1 + 1")
session$close()
} # }