Skip to content

keep the chat guardrails on after a binding fails to restore - #19807

Merged
kevinushey merged 11 commits into
mainfrom
bugfix/19806-chat-guardrails-restore
Nov 30, 2026
Merged

kevinushey merged 11 commits into
mainfrom
bugfix/19806-chat-guardrails-restore

Conversation

@kevinushey

@kevinushey kevinushey commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

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() left chat.bindingsInjected set to TRUE when any binding failed to restore. chat.injectBindings() returns early whenever that flag is TRUE, 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() calling requireNamespace() 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. Under options(warn = 2) that warning is an error, and raising it earlier would cut the restore short.
  • A binding that failed to restore keeps its saved original. The next activation hooks it again from that original rather than from the current binding, which may be the hook, on the search path, in the namespace, or both. A hooked version is never saved as an original.
  • A new chat.hookedNamespaces records 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 through chat.addPreflightHook(). The restore raises no warning and doesn't load splines again, and the reloaded namespace keeps its own binding.
  • Originals saved from a namespace that was replaced aren't hooked from.
  • An original defined outside its namespace, such as a mock, is still restored.
  • A binding that fails to restore doesn't turn off later checks. This simulates a partial restore of utils::write.table() and a full failure for writeChar(), at the default warn level and with warn = 2. It checks that:
    • the next activation refuses writes;
    • it hooks from the saved originals, so an allowed writeChar() call is checked once;
    • the warning is raised with interrupts suspended;
    • everything is restored afterwards.

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() returns FALSE.

Documentation

None needed.

Checklist

  • If this PR adds a new feature, or fixes a bug in a previously released version, it includes an entry in NEWS.md
  • If this PR adds or changes UI, the updated UI meets accessibility standards
  • A reviewer is assigned to this PR (if unsure who to assign, check Area Owners list)
  • This PR passes all local unit tests (ran the chat, chat-guardrails and linter R test files locally; relying on CI for the rest)

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.
@kevinushey kevinushey added this to the 2026.12 (Parrot Balsam) milestone Nov 25, 2026
@kevinushey
kevinushey requested a review from gtritchie November 25, 2026 03:41
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

RStudio E2E - all platforms

All tests passed!

#################### 100% pass rate

Passed Failed Skipped Flaky
5977 0 274 2
Platform ✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
✅ auth-teardown 4 0 16 0
✅ desktop-linux 1499 0 49 0
✅ desktop-macos 1486 0 63 0
✅ desktop-windows 1493 0 60 1
✅ server-linux 1447 0 54 1
✅ setup 48 0 32 0

View report online


All platforms - commit 45bb34e27d6590854ef692a989ce7b45d99d08d9 - Run #2561

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@kevinushey
kevinushey merged commit 68562a4 into main Nov 30, 2026
15 of 20 checks passed
@kevinushey
kevinushey deleted the bugfix/19806-chat-guardrails-restore branch November 30, 2026 21:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Posit Assistant's R code checks stay off for the session after a hooked function fails to restore

2 participants