Repository navigation
keep the chat guardrails on after a binding fails to restore - #19807
Conversation
restoreBindings() no longer loads a namespace that was unloaded to restore its bindings, and clears chat.bindingsInjected even when a binding can't be restored, so the next injectBindings() hooks the bindings that were. A binding left hooked keeps its saved original. Addresses #19806.
A binding whose restore failed partway may be hooked on the search path but not in its namespace, so hook it again from the original saved for it rather than skip it. Restore failures are now reported once the restore is done and chat.bindingsInjected is cleared, since options(warn = 2) turns each warning into an error. Addresses #19806.
An original saved from a namespace that was unloaded, or loaded again, is neither written back by restoreBindings() nor hooked from by the next injection: that namespace's bindings were never hooked. This replaces the check for an unloaded namespace. Restore failures are reported in a single warning. Addresses #19806.
An original's environment isn't always its namespace (e.g. a function mocked in the namespace), so identifying a replaced namespace from it could drop a valid original and leave its hook in place. chat.hookedNamespaces records the namespace each package's originals were saved from instead. Addresses #19806.
Also hook splines through addPreflightHook() in the namespace lifecycle test, as an activation would. Addresses #19806.
Also give the replaced splines namespace a distinct bs, and check that an allowed writeChar() call after a failed restore is checked once. Addresses #19806.
RStudio E2E - all platformsAll tests passed!
All platforms - commit |
gtritchie
left a comment
There was a problem hiding this comment.
Looks good. I left one non-blocking finding inline, in code this PR didn't introduce; it could go in a follow-up issue.
| if (!.rs.chat.isHookedNamespaceCurrent(package)) | ||
| rm(list = bindings, envir = originals) | ||
|
|
||
| for (binding in ls(envir = originals, all.names = TRUE)) |
There was a problem hiding this comment.
Non-blocking, and already in main, not introduced here: a package loaded during an agent run keeps the hooked functions it imports after this restore. Loading a namespace copies the current values of its imports into its imports env, so while the hooks are active it gets the hooked closures. restoreBindings() only puts back the source namespace and package:<pkg>. The hooks call validateFileEdit()/validateFileRead() whether or not an agent run is active. So afterwards, the user's own Console calls through that package are refused for any path outside the allowed locations.
I reproduced this outside RStudio with a throwaway package that does importFrom(utils, write.table). I hooked utils::write.table in the namespace and on the search path, ran loadNamespace() on the package, then put both back. The package's imports env still held the hooked closure, and calling the package's function ran the hook.
One option: on restore, also repair the imports envs of namespaces loaded since the hooks were installed.
There was a problem hiding this comment.
Confirmed on this branch: with a package that does importFrom(utils, write.table, download.file) and is first loaded under .rs.chat.safeEval(), the imports env still holds the hooked closures after the restore, and a later Console call through the package to a path outside the allowed locations fails with "One or more agent file operations were blocked." Same on main, so I've filed #19847 rather than widen this PR.
# Conflicts: # src/cpp/session/modules/SessionChat.R # src/cpp/tests/testthat/test-chat-guardrails.R
Intent
If restoring one of the functions that Posit Assistant's checks hook failed, the checks stopped applying to R code Posit Assistant ran for the rest of the session. This happened when code it ran unloaded fs and fs couldn't be loaded again: a later
writeLines()outside the allowed locations went through.Fixes #19806.
Approach
chat.restoreBindings()leftchat.bindingsInjectedset toTRUEwhen any binding failed to restore.chat.injectBindings()returns early whenever that flag isTRUE, which is meant as its guard against reentrant calls. So later activations installed no hooks, and the bindings that had been restored stayed unhooked. The fs failure came from.rs.replaceBinding()callingrequireNamespace()for a namespace the evaluation had unloaded.restoreBindings()clears the flag even when a binding fails to restore. It reports the failures in one warning once the restore is done, with interrupts still suspended. Underoptions(warn = 2)that warning is an error, and raising it earlier would cut the restore short.chat.hookedNamespacesrecords the namespace each package's originals were saved from. If that namespace has since been unloaded or loaded again, its originals are dropped. Nothing is written back, the namespace isn't loaded again to do it, and the next activation hooks the new namespace's own bindings.#19795 also changed
restoreBindings()and merged first. main is merged into this branch: the restore records the working directory, as #19795 added, before it restores the bindings.Not addressed here, and unchanged from main: fs is hooked only if it is loaded when an activation starts. Code that loads fs during an evaluation calls unhooked fs functions.
Automated Tests
New tests in
src/cpp/tests/testthat/test-chat-guardrails.R:restoreBindings()leaves alone a namespace that was unloaded, or unloaded and loaded again. The test uses splines, hooked throughchat.addPreflightHook(). The restore raises no warning and doesn't load splines again, and the reloaded namespace keeps its own binding.utils::write.table()and a full failure forwriteChar(), at the defaultwarnlevel and withwarn = 2. It checks that:writeChar()call is checked once;Each test was run against the code before the fix to confirm it fails there.
QA Notes
Run the steps from #19806 in the Console, with fs installed in a user library. Step 1 no longer warns and fs stays unloaded. Step 2 is refused, and
file.exists()returnsFALSE.Documentation
None needed.
Checklist
NEWS.md