Skip to content

Chat guardrails: allow agent R code to write to Posit Assistant's scratch directory #19792

Description

@ferenci-tamas

Problem Statement

When Posit Assistant runs R code in RStudio, the chat guardrails in SessionChat.R only allow file writes inside .rs.chat.allowedRoots(). That list covers R's tempdir(), the working directory, the project, RStudio's user data directory, .libPaths() / R_LIBS_USER, and the tools::R_user_dir() directories.

Posit Assistant gives each conversation its own scratch directory for temporary files such as downloads and intermediate outputs (here: C:/Users/<user>/AppData/Local/Temp/p-uyx1sd/<id>). Its shell tool receives that directory as PA_SCRATCH_DIR / TMPDIR, and its file tools may edit it by default. However, the scratch directory isn't one of the allowed roots, so R code that the assistant runs can't write there. For example, downloading a PDF in R to read it with pdftools fails:

Error: One or more agent file operations were blocked.

- Action: download.file()
- Path:   C:/Users/<user>/AppData/Local/Temp/p-uyx1sd/<id>/<filename>.pdf
- Reason: Path is not within the project, working directory, or other allowed locations.

The same check, run directly:

.rs.chat.isFileEditAllowed("C:/Users/<user>/AppData/Local/Temp/p-uyx1sd/<id>/<filename>.pdf")
#> [1] "Path is not within the project, working directory, or other allowed locations."
.rs.chat.isFileEditAllowed(file.path(tempdir(), "<filename>.pdf"))
#> [1] ""

So for temporary files created from R, the assistant has to fall back to one of these:

  • the user's project directory, which then fills up with clutter
  • the user's R session tempdir(), which Posit Assistant tries to keep free of its own files
  • the shell instead of R

Users can't change this either. It seems that .rs.chat.allowedRoots() consults no option, preference or environment variable other than R_LIBS_USER. That variable doesn't fit this purpose, because it also adds the directory to .libPaths().

Proposed Behaviour

  1. While running code for Posit Assistant, add the current conversation's scratch directory to the allowed write roots, for example by having Posit Assistant pass the path with the execute request. The directory is created for the conversation and is already writable by the assistant's own tools, so this wouldn't widen what the agent can modify. The existing deny patterns for credentials and system files would still apply inside it.
  2. Optionally, add a user-level (not project-level) setting for extra allowed write locations, which administrators could also enforce. Keeping it out of project settings would stop a cloned project from widening the guardrails.

QA Notes

  • In code run by the assistant, download.file(), writeLines(), saveRDS() etc. to the conversation's scratch directory succeed.
  • Writes to other paths outside the allowed roots (e.g. %TEMP% itself) are still blocked.
  • The deny patterns for credentials and system files still take precedence inside the scratch directory.

Activity

  1. gtritchie commented on Nov 23, 2026

    @gtritchie
    Member

    Thanks for the suggestion. Overall it seems like a good idea. Given our schedule, I'm going to assign this to our January 2027 milestone and we'll look closer then.

  2. added this to the 2027.01 milestone on Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions