R6 class for secure code execution with tool-call IPC.
Wraps a callr::r_session with a bidirectional Unix domain socket protocol
that allows code running in the child process to pause, call tools on the
parent side, and resume with the result.
Methods
Method new()
Create 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 (legacy format for backward compatibility)sandboxLogical, whether to enable the OS-level sandbox. On macOS this uses
sandbox-execwith a Seatbelt profile that denies network access and restricts file writes to temp directories. On Linux this uses bubblewrap (bwrap) with full namespace isolation. On Windows this provides environment isolation (clean HOME/TMPDIR, empty R_LIBS_USER) and resource limits (memory, CPU time, process count) via Job Objects. On other platforms the session runs without sandboxing.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). Whensandbox = TRUEandlimitsisNULL(the default), sensible defaults are applied automatically (seedefault_limits()). Passlimits = list()to explicitly disable resource limits. Whensandbox = FALSE,NULLmeans no limits.verboseLogical, whether to emit diagnostic messages via
message(). Useful for debugging. Users can suppress withsuppressMessages().sandbox_strictLogical, whether to error if sandbox tools are not available on the current platform (default
FALSE). WhenTRUEandsandbox = TRUE, the session will stop with an informative error if the OS-level sandbox cannot be set up. WhenFALSE(default), the existing behavior is preserved: a warning is emitted and the session continues without sandboxing.audit_logOptional path to a JSONL file for persistent audit logging. If
NULL(the default), no file logging is performed. When a path is provided, structured JSON entries are appended for session lifecycle events, executions, and tool calls.max_executionsOptional integer, the maximum number of
$execute()calls allowed on this session (defaultNULL= unlimited). Once the limit is reached, subsequent$execute()calls stop with an error. Useful for disposable sessions in agent workflows.pre_execute_hookOptional function taking a single
codeargument. Called at the start of every$execute()invocation. If it returnsFALSE, execution is blocked with an error. Any other return value (includingNULLorTRUE) allows execution to proceed. DefaultNULL(no hook).sanitize_errorsLogical, whether to strip sensitive details (file paths, PIDs, hostnames) from error messages returned by
$execute()(defaultFALSE). WhenTRUE,sanitize_error_message()is applied before the error is raised.
Method execute()
Execute R code in the secure 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). Pass
NULLto disable the timeout entirely. Both this method and theexecute_r()convenience wrapper default to 30 seconds. For long-running workloads, pass an explicit higher value orNULL.validateLogical, whether to pre-validate the code for syntax errors before sending it to the child process (default
TRUE).output_handlerAn optional callback function that receives output lines (character) as they arrive from the child process. If
NULL(default), output is only collected and returned as the"output"attribute on the result.max_tool_callsMaximum number of tool calls allowed in this execution, or
NULLfor unlimited (defaultNULL).max_code_lengthMaximum allowed
nchar(code)(default 100000). Code exceeding this limit is rejected before parsing. Prevents resource exhaustion from extremely large code strings.max_output_linesMaximum number of output lines to accumulate (default
NULL= unlimited). Once the limit is reached, further output from the child is still drained but not stored.
Method close()
Close the session and clean up resources
Method format()
Format method for display
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()
} # }