Skip to content

Show when Posit Assistant's code waits on a busy Console, and let the user stop it - #19836

Merged
gtritchie merged 12 commits into
mainfrom
feature/chat-busy-console-notice
Nov 30, 2026
Merged

gtritchie merged 12 commits into
mainfrom
feature/chat-busy-console-notice

Conversation

@gtritchie

@gtritchie gtritchie commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Fixes #19834.

While R runs the user's code (a Shiny app, or anything else), a runtime/executeCode request from Posit Assistant waits in the queue, which drains only when R is idle. RStudio never answered it, so the Assistant's own timer gave up with a generic timeout and the chat pane showed nothing.

What changes

Background processing, which runs while R is busy, now checks the queue whenever R is running the user's code (console input, or an idle-loop handler such as a later::later app) and no agent code:

  • Notice. After a request has waited 3 s, a bar in the Posit Assistant pane says the Assistant is waiting for the R console, with a button that interrupts R as the Console's Stop button does. When a Shiny app has opened a viewer since the console last prompted or took input, the bar says a Shiny app is running and the button is Stop App; otherwise it says the console is busy running your code and the button is Interrupt R. The bar is announced to screen readers, also appears in the popped-out chat window, and is restored when the pane loads or the window opens mid-wait (chat_get_console_wait, or a request from the popped-out window).
  • Busy answer. Shortly before the timeout the request sent (timeout − min(5 s, timeout / 5)), RStudio answers it unrun with an error the agent can act on, e.g. "Not run: the R console is busy running a Shiny app the user started. Ask the user to stop the app, then try again." Later requests during the same user code are answered at once, so the Assistant's setup-failure fallback doesn't wait out more timeouts.
  • Agent code doesn't run later. An answered request is removed from the queue, except the silent setup and teardown around the agent's code: those are kept and run without a response once the console frees, so a teardown never misses the setup it undoes. Kept requests don't count toward the queue limit.
  • Expiry. At drain time, a request that waited on the user's code expires by the timeout it sent (silent requests excepted), not only by RStudio's run limit, which is 0 for silent and Run in Console requests. Such waits are exempt from the 60 s queue cap, so a request without a timeout waits for the console rather than being rejected after the notice offered to let it run.
  • Canceled requests are answered while R is busy rather than at the next idle.

Requests queued behind the agent's own code wait as before. A Run in Console request queued behind the user's code is now answered and dropped like the agent's code, where it previously ran if the console freed within 60 s.

Testing

  • Unit tests for the timing and queue decisions (ChatExecutionDeadlineTests.cpp: busyAnswerDue, waitNoticeDue, blockedRequestAction, queueTimeout, drainAction).
  • Manually, on RStudio Server with Posit Assistant 1.14.0:
    • Shiny app running, agent asked to run code: bar after ~4 s; Stop App stops the app and the agent's code runs without an error.
    • App left running: the agent gets the busy error ~26 s after the request (before its 30 s timer), the bar clears, and the code doesn't run when the app is later stopped.
    • Sys.sleep(60): generic wording, Interrupt R, and the generic error.
    • Sys.sleep(2): no bar; the code runs.
    • Browser reload during a wait restores the bar; the popped-out window shows it, including when popped out mid-wait, and its Interrupt R lets the agent's code run.
    • After a busy answer and the user's code finishing, console output is intact (sink.number() is 0) and the agent's next run uses its normal setup.

Review findings not addressed

Automated reviews raised these; each was judged unlikely or low-impact enough to leave as is. Reviewers may disagree.

  • Stale state from the pane's initial fetch. chat_get_console_wait could sample "not waiting" just before the 3 s notice becomes due, and if its response arrived after the newer chat_console_wait event, the bar would stay hidden for that wait. This needs the pane to load (only on page load or first opening) within about one 50 ms background tick of the 3 s mark, and the immediately sent RPC response to be overtaken by an event enqueued a tick later. The agent is still answered and the Console's Stop button still works; a revision counter shared by the fetch and the events wasn't worth it.
  • Stale BusyHandoff state. Requests removed by the busy-console scan don't go through BusyHandoff::finished, so a handoff could stay marked held. Reaching that needs the user's code paused in readline() while a queued agent request runs inside it and another waits, the user's code resuming before the second runs, and later a cancellation during file.choose().
  • Immediate busy answers across buffered input. s_busyAnswerSent is cleared only at idle or while agent code runs, so with Run All or pasted multi-line input, a busy answer during one command makes later requests during the following commands answer at once, without waiting or a notice. The answer is still accurate: the Console is busy with the user's code.
  • Notice not reshown after an update notification. If an update notification is up when a wait starts, the notice is skipped and not shown when that notification is dismissed. The existing readline notice behaves the same way, and the agent is still answered.
  • Kept requests aren't capped. Setup and teardown requests kept to run later don't count toward the queue limit, so an agent that keeps retrying during a long run of user code adds a pair each time. Each needs an agent turn, the busy error tells the model to ask the user rather than retry, and a pair runs in milliseconds once the console frees; capping them would mean dropping some, which brings back a setup without its teardown.
  • Popped-out bar flickers while resizing. The popped-out window re-measures the bar on each resize event by briefly collapsing it, so it can flicker while the window edge is dragged.

Screenshots

screenshot of Posit Assistant showing the new message bar indicating session is busy running Shiny app screenshot of Posit Assistant showing the message given after request times out screenshot of Posit Assistant showing new message bar indicating session is busy running R code other than a Shiny app

While R runs the user's code (a Shiny app, or anything else), a
runtime/executeCode request waits in the queue, which drained only when
R was idle. RStudio never answered it, so the Assistant's own timer gave
up with a generic timeout and the chat pane showed nothing.

Background processing, which runs while R is busy, now checks the queue
when no agent code is running:

- After a request has waited 3 s, a chat_console_wait event shows a bar
  in the chat pane with a button that interrupts R ("Stop App" when a
  Shiny app has launched since the last console prompt, "Interrupt R"
  otherwise). It is announced to screen readers.
- Shortly before the timeout the request sent (timeout - min(5 s,
  timeout / 5)), the request is answered unrun with an error saying the
  Console is busy and what to ask the user, and removed from the queue.
  This uses the sent timeout for silent requests too, since the
  Assistant's setup request is silent and its timer runs regardless.
  Later requests during the same user code are answered at once, so the
  Assistant's setup-failure fallback doesn't wait out two more timeouts.
- Canceled requests are answered then rather than at the next idle.

Addresses #19834.
A client that connects while a request waits on the user's code (e.g.
after a browser reload) never got the notice: the session sent the
chat_console_wait event only when its state changed. Client init now
resets the last-sent state, so the next background check sends it.

A queued request is now also skipped at drain time once the timeout it
sent has passed, not only RStudio's run limit, which is 0 for silent and
Run in Console requests. Before, such a request read while R polled and
then held past its timeout by code that stopped polling (e.g. native
code) still ran at the next idle, after the backend had given up on it.

Addresses #19834.
The chat pane loads on demand and drops events that arrive before it
does, so the client-init resend from fd844ff could still be lost
after a reload. The presenter now fetches the current state with
chat_get_console_wait once its event handler is registered, which
replaces the client-init reset.

fd844ff also expired every queued request by the timeout it sent,
which discarded the Assistant's silent teardown when it queued behind
agent code that outran its limit, leaving the setup's sink installed.
Only requests that waited on the user's code now expire by the sent
timeout; others keep the run limit as before.

Addresses #19834.
With Posit Assistant in its own window, the notice that its code waits
on the user's code appeared only in the main window's pane, which may be
hidden. ChatConsoleWaitEvent is now a cross-window event: the presenter
forwards each state to the popped-out window, which shows the same bar
below its toolbar (pushing the chat down rather than covering it) and
announces it through a status region. The window asks for the current
state when it opens, so popping out during a wait shows it too. Its
button sends an "interrupt-r" action to the main window, which runs
interruptR there.

Addresses #19834.
A suspended session runs no user code, and a forced suspend can drop the
event that would clear the notice, leaving it (and the state sent to the
popped-out window) stale after resume. Clear it on suspend, as the
readline notice already is.

Addresses #19834.
A silent request queued behind the user's code was answered "Not run"
and removed, so a teardown that arrived just after the user's code
started never ran and left the setup's sink installed, diverting the
user's console output. Silent requests are now answered the same way but
kept, and run without a response once the console frees, so a teardown
never misses the setup it undoes. Drain-time expiry by the sent timeout
no longer applies to them either.

The queue decision moves into blockedRequestAction(), with tests. Also:
count agent executions with a scope guard, wrap two long lines, and
shorten the comments the review flagged; the s_agentExecutionsRunning
comment no longer claims every busy console is the user's code.

Addresses #19834.
The flag that picks the Shiny wording was cleared only by a reported
console prompt. A tutorial opened from the Tutorial pane sets it with no
prompt after, and prompts restored after idle-loop code aren't reported,
so the next long command was described as a Shiny app. Input starts new
code, so clear the flag then too.

Addresses #19834.
- Don't replace a connection-lost error (and its Restart button) with
  the notice; hiding the notice later removed the error for good.
- In the popped-out window, give the notice its place below the toolbar
  when it is added, so it never paints over the whole window.
- Move the new presenter methods off the readline comment they had
  split from onConsoleReadCompleted, and wrap a long dispatcher line.

Addresses #19834.
A null request id meant "already answered", but the backend's messages
may carry a null id on a real request, which then got no response. Pass
an explicit respond flag to executeCodeImpl instead.

Kept requests also counted toward the 10-request queue limit, so enough
retries during one stretch of user code could keep a setup and reject
the teardown after it, leaving the setup's sink installed once it ran.
The limit now counts only requests not yet answered.

Addresses #19834.
@gtritchie gtritchie added this to the 2026.12 (Parrot Balsam) milestone Nov 30, 2026
Comment thread src/cpp/session/modules/SessionChat.cpp Resolved
Comment thread src/cpp/session/modules/SessionChat.cpp Resolved
Comment thread src/cpp/session/modules/SessionChat.cpp Resolved
Comment thread src/cpp/session/modules/SessionChat.cpp Resolved
Comment thread src/cpp/session/modules/SessionChat.cpp Resolved
Comment thread src/cpp/session/modules/SessionChat.cpp Resolved
From PR review:

- The 60 s queue cap rejected a request that had waited on the user's
  code, after the notice offered to let it run (e.g. no timeout, or a
  model-chosen 120 s one). Waits on user code are now exempt: a request
  with a timeout is answered before it runs out, and one without waits.
  The drain decision moves into drainAction() and queueTimeout(), with
  tests.
- The busy-console check treated any non-idle R as the user's code,
  including RStudio's own R work for an RPC, where the client isn't
  busy and the notice's interrupt button does nothing. It now requires
  console_input::isRunningCode(): console input or an idle-loop handler.
- A kept request, already answered, no longer streams its output to the
  backend, which has closed it.
- Correct the comment on where queued cancellations are answered.

Addresses #19834.
The notice was appended after the chat frame, so keyboard users had to
tab through the whole chat to reach its button. Insert it before the
frame, as the pane does.

Addresses #19834.
LayoutPanel.insert orders its widgets but appends the new widget's DOM
layer, so 1c8be1b left the notice's button after the chat iframe in
tab order. Move the notice's layer before the frame's, as
ApplicationWindow.showWorkbenchView does; the frame stays put, since
moving an iframe reloads it.

Addresses #19834.
@gtritchie

Copy link
Copy Markdown
Member Author

Second look at the three new commits (5120863, 1c8be1b, 3025736). Read only; not built or run.

Fixed

  • Queue cap vs. notice: drainAction() spares requests that waited on the user's code from the 60 s cap, and the ChatDrainedRequest tests cover both sides.
  • Interrupt R no-op: the console_input::isRunningCode() condition keeps RStudio's own RPC work from showing the bar or getting the busy answer.
  • Output for an answered request: a kept request no longer registers as active, so background processing drops its notifications, and the final drain is replaced by a clear.
  • Popped-out focus order: the layer element is moved before the frame's (3025736 is the part that matters), and panel.remove() still cleans it up.
  • Stale comment and test coverage for queueTimeout() / drainAction().

No change needed

  • Setup answered "Not run": covered by the check of Assistants 1.10.0 through 1.14.0.

Left as is

  • Unbounded kept requests: the reasoning holds for an agent that follows the busy error; the exposure is only an agent that ignores it and keeps retrying.

One new nit, not blocking

waitedOnUserCode is set by a single scan tick in answerRequestsBlockedByUserCode() and never cleared. A request that waited on the user's code briefly and then sits behind the agent's own long-running code for over 60 s is now spared the queue cap too, since drainAction() only applies RejectQueueCap when !request.waitedOnUserCode.

@gtritchie

Copy link
Copy Markdown
Member Author

On the waitedOnUserCode nit: leaving it as is.

Reaching it needs two agent requests queued at once behind the user's code (a background task or a second conversation, since the Assistant sends one at a time), the first then running past 60 s once the user's code ends (agent code with the default 30 s limit is interrupted before that), and the second having no timeout or being a silent setup/teardown.

The outcome is benign: a request with a timeout still expires by it, because it waited on the user's code; a silent teardown running late keeps it paired with its setup, which is the point of keeping those; and a request without a timeout set no limit, so running once the console frees matches what its sender asked. Clearing the flag would also drop the sent-timeout expiry, so a proper fix would mean tracking time waited behind user code separately from time behind agent code, which isn't worth it for this case.

@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

RStudio E2E - all platforms

All tests passed!

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

Passed Failed Skipped Flaky
5885 0 255 1
Platform ✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
✅ auth-teardown 4 0 16 0
✅ desktop-linux 1471 0 49 0
✅ desktop-macos 1477 0 44 0
✅ desktop-windows 1466 0 60 0
✅ server-linux 1419 0 54 1
✅ setup 48 0 32 0

View report online

⚠️ 48 shard report(s) are from a previous attempt of this run: shards that did not re-run, or that re-ran without producing one (the merge job warns about each).


All platforms - commit 3025736c7e261da41beaeadaa0ed277d10581fa8 - Run #2551

@gtritchie
gtritchie merged commit 1bb7fba into main Nov 30, 2026
80 of 81 checks passed
@gtritchie
gtritchie deleted the feature/chat-busy-console-notice branch November 30, 2026 20:10
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: show when agent code is waiting on a busy Console, and let the user stop it

1 participant