Skip to content

fix(interactive): stop and reach adopted arf sessions only through the verified session - #1987

Merged
eitsupi merged 9 commits into
mainfrom
fix/interactive-adopted-stop
Nov 22, 2026
Merged

eitsupi merged 9 commits into
mainfrom
fix/interactive-adopted-stop

Conversation

@eitsupi

@eitsupi eitsupi commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Stop of an attached arf session fell back to SIGTERM and then SIGKILL when arf rejected shutdown, as an interactive arf does, and when arf accepted it but had not exited after a few seconds. An interactive arf then could not restore its terminal or finish R's exit, and the PID could already belong to another process.

Requests to an attached arf also went to its saved socket path, which another arf session can bind while the attached R is still running. Discovery read arf's session files from the Linux and macOS cache directories, so it found no sessions on Windows, and a session whose IPC restarted at a new socket path could not be reached again.

An attached arf process is now never sent a signal or killed, and every request to it goes over a connection that reached the verified session.

Stop

  • Stop asks arf to shut down. When arf rejects the request, when its endpoint is unknown, or when Stop is forced, Stop is refused and the session, its queue and its status stay as they were. An arf that runs in a terminal refuses, and the error asks to end R in that terminal.
  • When arf accepts but has not exited within the deadline, Stop reports that, and the session still records the exit when it happens. A later Stop reports that R has not exited yet, and succeeds once the exit is recorded.
  • Queued cells are cancelled only once Stop is not refused.
  • Stop requests, including stopping several sessions, wait long enough for the attach, shutdown and exit deadlines.
  • Clients ask an agent to stop an attached session only when the agent advertises that it does not terminate the process; an agent from an earlier extension version would.
  • shutdown goes over a new attach connection that reached the same arf session. When that connection cannot be opened, nothing was sent, so Stop is refused and the queue is kept.
  • Overlapping Stop requests share one outcome. After a refused Stop, the workspace is read before the next queued cell runs. An agent whose stopped runtime exits after every client has left shuts down.

Requests to an attached arf

  • Cells, Interrupt, the R environment read and the bootstrap go over the attach connection that reached the verified session, not over a new connection to its socket path.
  • A running cell is interrupted on the connection that sent it, whether the request is sent at once or once arf reports the execution ID. If that connection has closed, Interrupt fails instead of sending the ID elsewhere.

Sending cells

  • Whether a cell is sent is decided just before its code is written. Cancel Queued Cells and R's exit cancel a cell not yet written; Stop holds it, and it runs again if Stop is refused. Cancellation applies only to a cell whose send is in progress, so a cell already sent but not yet reported as started, and cells submitted later, are not affected. A cell that may have been written stays uncertain and is not sent again.
  • A cell that could not be sent, because arf could not be reached or its endpoint now reaches another session, stays queued instead of becoming uncertain, and is retried with a growing delay. Code is then sent only once a new attach reaches the same session again.
  • A confirmed exit stays final for the session's status; later events of a cell only settle its record. After R exits, the agent records the outcome of a cell being sent before it closes, and does not close on its own if a client has connected meanwhile. The cell is then left to its send to settle instead of being recorded as uncertain.

Discovery

  • Running sessions are listed with arf ipc list, run without a shell, with a deadline and an output limit. The deadline holds even when the CLI handles SIGTERM and keeps running, and the output pipes are closed when it passes. A listing that cannot be obtained is reported instead of showing no sessions, and the session picker then offers to configure arf; the R Interactive output shows the details. An untrusted workspace does not run the CLI.
  • Each listed session is asked for its metadata within one deadline. A session whose endpoint answers for another PID, or for another start time when one is listed, is not offered, since a restarted arf can keep its PID and socket path.
  • The session ID and PID of the chosen session go to its agent, and every attach must reach the same arf before the extension evaluates anything in it.
  • Listing needs the arf CLI on the R host and uses the extension host's environment. A custom ARF_IPC_SESSIONS_DIR must be set to the same absolute path for arf and for the extension host. The documentation asks for a custom sessions directory to be private to the user, as arf requires.
  • Listing runs in the directory of the workspace folder that a new session is created for, or of the first folder.
  • arf registers a new socket path when its IPC restarts, as after :ipc stop and :ipc start, keeping its PID and session ID. When the saved path cannot be attached, an attached session is listed again with the arf CLI, directory and registry selection (ARF_IPC_SESSIONS_DIR, XDG_CACHE_HOME and HOME, including unset ones) that listed it in the session picker, even if the agent runs with another environment, as in an existing tmux server, or another folder became active before the session was created. Queued code then resumes at a listed path whose attach reports the same PID and session ID. Stop does the same on a connection of its own, within 5 seconds.
  • A test with the real arf lists, probes and attaches to a running session on every platform, including Windows named pipes, and checks that arf keeps running and evaluating after its agent closes.

This changes behavior on Linux and macOS too: an attached arf that runs in a terminal can no longer be stopped from VS Code.

This is also a step toward connecting to existing arf sessions on Windows.
Where Node can only terminate a process forcibly, so the previous fallback would have killed arf outright on Windows.

@eitsupi
eitsupi requested a review from renkun-ken November 22, 2026 01:49
@eitsupi
eitsupi marked this pull request as ready for review November 22, 2026 01:49
@eitsupi
eitsupi added this pull request to stack #1989 November 22, 2026 02:20

@renkun-ken renkun-ken 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.

Two cases still need protection before merge. A reconnected older agent can terminate an adopted terminal process, and a reused socket path can direct shutdown to a different R session. The inline comments also cover queue recovery, concurrent Stop requests, and agent cleanup after a delayed exit.

TypeScript compiled and 93 focused Node tests passed at f0c985d. I verified the upgrade case separately with both Stop entry points against the previous agent, intercepting its process signals.

Comment thread src/interactive/sessionLifecycle.ts Resolved
Comment thread src/interactive/backends/arf.ts Resolved
Comment thread src/interactive/backends/arf.ts Resolved
Comment thread src/interactive/backends/arf.ts Resolved
Comment thread src/interactive/agent.ts Resolved
Comment thread src/interactive/agent.ts Resolved
Stop of an attached arf fell back to SIGTERM and SIGKILL when arf
rejected shutdown, as an interactive arf does, and after shutdown was
accepted but arf had not exited. An interactive arf then did not
restore its terminal or finish R's exit, and the PID could have been
reused by another process.

An adopted arf is now only asked to shut down. Stop is refused when arf
rejects the request, when the endpoint is unknown, and when it is
forced; the session, its queue and its status stay as they were, and a
workspace refresh deferred while stopping resumes. When arf accepts but
does not exit in time, Stop reports that, and the later exit is still
recorded. Queued work is cancelled once Stop is not refused, instead of
before it is attempted. Stop of several sessions waits long enough for
an adopted arf to answer.
…sed Stop

An agent from an earlier extension version still terminates an adopted
arf on Stop, so clients now ask an agent to stop an adopted session
only when it advertises that it does not.

Shutdown went to the saved socket path, which another arf session can
bind while the adopted R is still alive. It is now sent over a new
attach connection that reached the same session. When that connection
cannot be opened, nothing was sent, so Stop is refused and the queue is
kept, as after `:ipc stop` in the terminal.

Overlapping Stop requests now share the outcome of the one in flight.
After a refused Stop, the workspace is read before the next queued cell
runs. An agent whose explicitly stopped runtime exits after every
client has left shuts down, as the last client would have made it.
@eitsupi
eitsupi force-pushed the fix/interactive-adopted-stop branch from f0c985d to cb70fdb Compare November 22, 2026 02:57
@eitsupi
eitsupi requested a review from renkun-ken November 22, 2026 03:00

@renkun-ken renkun-ken 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.

I rechecked the six earlier findings on cb70fdb. Both Stop commands now protect sessions served by older agents. The shutdown request uses an identity-verified connection. With real arf 0.7.2 on macOS, terminal arf refused Stop and stayed alive, and headless arf exited with code 0 and no signal. The new tests also cover overlapping requests, workspace refresh ordering and cleanup after a late exit.

I’m requesting changes to keep cells queued after a verification failure and to make both initial and repeated Stop requests report the actual shutdown outcome. TypeScript compiled, and all 99 focused Node tests passed.

Comment thread src/interactive/backends/arf.ts Resolved
Comment thread src/interactive/agent.ts Resolved
Comment thread src/interactive/sessionLifecycle.ts Resolved
…report pending Stop

A cell whose dispatch failed before its code was sent, because the
attach connection could not be opened again or the endpoint no longer
reached the adopted session, was recorded as unknown. It now stays
first in the queue and is retried with a bounded backoff. After a Stop
found that the endpoint did not reach the adopted session, or after a
send could not open its connection, code is sent only once a new
connection reaches the same session again. A cell found unsent after
Stop cancelled the queue is cancelled.

Once an accepted shutdown missed its exit deadline, a later Stop
reported success while R was still running. It now reports that R has
not exited yet, and succeeds once the exit is recorded.

Clients waited 15 seconds for Stop, which an adopted arf can need for
verifying a new attach connection, answering shutdown and exiting. All
Stop requests now share a 20-second timeout.

@renkun-ken renkun-ken 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.

The pending-Stop and client-timeout findings are fixed at 1869d2b. Single and bulk Stop both returned the backend’s pending-exit error after 15.1 seconds, a later Stop continued to report it while R was alive, and both commands succeeded after exit. TypeScript compiled and all 104 focused Node tests passed.

I’m requesting changes on the automatic retry path. The connection that receives a queued cell must be the one whose identity was verified, retries need to find the original process after its IPC path changes, and cancelling queued work must prevent a later retry from executing it.

Comment thread src/interactive/backends/arf.ts Resolved
Comment thread src/interactive/backends/arf.ts Resolved
Comment thread src/interactive/agent.ts Resolved
…and honor cancellation while sending

A cell for an adopted arf was evaluated over a new connection to the
saved socket path, which another arf can serve once the attach
connection has verified the session. It is now evaluated on that
attach connection. A session that must be verified again is reached by
a new attach, which checks the session before anything is sent.

A cell is taken off the queue while it is being sent, so Cancel Queued
Cells, Stop and R's exit missed it, and a retry could then run it. Its
cancellation is now kept for the attempt in flight, and a cell found
unsent is cancelled. A cell whose send R's exit leaves uncertain is
still recorded as unknown. After R exits, the agent closes once that
outcome is recorded, for up to 5 seconds, whether the last client
leaves, shutdown is requested or no client remains.
@eitsupi
eitsupi requested a review from renkun-ken November 22, 2026 08:38

@renkun-ken renkun-ken 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.

Adopted cells now evaluate on the connection that verified the arf session. With real arf 0.7.2 on macOS, an ordinary cell ran, a running cell accepted an interrupt, and headless Stop exited cleanly. TypeScript compiled and all 108 focused Node tests passed.

The earlier socket rediscovery finding is still open. I repeated the test on this commit with one real terminal arf. After :ipc stop and :ipc start, the PID and session ID matched at the new socket, but two dispatch attempts both returned DispatchNotSentError for the deleted original path. Please rediscover the current endpoint and verify the original session ID before resuming. Existing review thread

The inline findings cover cancellation when reconnection succeeds, interrupt routing, and two event orderings during agent cleanup.

Comment thread src/interactive/agent.ts Resolved
Comment thread src/interactive/backends/arf.ts Resolved
Comment thread src/interactive/agent.ts Resolved
Comment thread src/interactive/agent.ts Resolved
@eitsupi
eitsupi marked this pull request as draft November 22, 2026 08:58
…on, and decide each send at its write

Requests to an adopted arf went partly over the attach connection that
verified the session and partly over new connections to its socket
path, which another arf can serve. Interrupt, the R environment read
and the bootstrap now go over the attach connection too. An adopted
execution keeps the connection that sent it, and its interrupt, sent at
once or once arf reports its ID, uses that connection, or fails if it
has closed.

Whether a cell is sent was decided in several places after the fact, so
each combination of Cancel Queued Cells, Stop, exit and retry could
send or lose it. A dispatch now carries a cancellation signal that the
backend checks just before writing the code. Cancelling, or R's exit,
cancels a cell not yet written; Stop holds it, and it runs again if Stop
is refused.

A confirmed exit is now final for the session's status: later events of
a cell only settle its record. After R exits, the agent closes on its
own only if no client has connected while it waited for a dispatch.
…opted session again after its IPC restarts

Discovery read arf's session files from the Linux and macOS cache
directories, so it found nothing on Windows, and an adopted session
whose IPC restarted could not be reached again: arf then registers a
new socket path, keeping its PID and session ID, and the saved path is
gone.

Running sessions are now listed with `arf ipc list`, without a shell,
with a deadline that closes the output pipes and with an output limit.
A listing that cannot be obtained is reported instead of showing no
sessions. A listed session is offered only when its endpoint answers
for the listed PID and start time, and its session ID and PID go to the
agent, whose attach must reach the same arf. The agent keeps the arf
CLI and the directory that listed the session.

When the saved path cannot be attached, the session is listed again and
queued code resumes at a listed path whose attach reports the same PID
and session ID. Stop does the same on a connection of its own, within 5
seconds, so that its shutdown and exit fit within its client timeout.
@eitsupi
eitsupi requested a review from renkun-ken November 22, 2026 09:29
@eitsupi
eitsupi marked this pull request as ready for review November 22, 2026 09:30
@eitsupi eitsupi changed the title fix(interactive): never terminate an adopted arf process on Stop fix(interactive): stop and reach adopted arf sessions only through the verified session Nov 22, 2026
… removing its directory

The listing deadline kills the CLI and returns without waiting for it to
exit. On Windows, the killed CLI can still hold its working directory,
the test's temporary directory, so removing it after the test failed
with EPERM. CLI fixtures now record their PIDs, and on Windows the
teardown waits for those processes to exit before removing the
directory.

@renkun-ken renkun-ken 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.

I rechecked the previous findings at c025b9e. With real arf 0.7.2 on macOS, a queued cell resumed on the same terminal R process after :ipc stop and :ipc start changed its socket. Headless evaluation, interrupt, and graceful shutdown passed. The regression tests for pre-send cancellation, terminal exit status, and keeping a reconnected client attached also passed.

I’m requesting changes for cancellation leaking to a later submission and for supervised agents losing the registry used by the picker. The inline comments also cover the remaining journal race and the scope of the arf path setting. TypeScript compiled, 142 focused Node tests passed, and the new packaged-extension adoption test passed with real R.

Comment thread src/interactive/agent.ts Resolved
Comment thread src/interactive/backends/arf.ts Resolved
Comment thread src/interactive/agent.ts Resolved
Comment thread package.json Resolved
…session again as it was listed, and leave a reconnected client's cell to its dispatch

Cancel Queued Cells marked the dispatch as cancelled whenever the cell
being sent was still queued, including after its dispatch had sent it
and before R reported that it started. A later cell that then could not
be sent was cancelled although it was submitted after the request. The
mark is now set only while a dispatch is in flight.

An adopted session was listed again with the agent's environment and
with the arf CLI and directory resolved again when it was created. An
agent started in an existing tmux server can have another registry
selection than the extension host, and another folder can become active
while the picker is open. The picker now keeps the resolved CLI, the
directory and the variables that select the session registry
(ARF_IPC_SESSIONS_DIR, XDG_CACHE_HOME and HOME, an unset one included)
with each session it offers, and the agent lists the session again with
them.

An agent waiting to close after R exited recorded the cell still being
sent as unknown before it checked for a reconnected client. The cell
then stayed unknown even when its dispatch found it unsent. The agent
now checks first and, while a client keeps it open, leaves the cell to
its dispatch.
The extension test lists one arf session from a custom registry with a
fake arf CLI, and, while the picker is open, makes the second folder
active and changes the arf path setting and ARF_IPC_SESSIONS_DIR. The
saved agent configuration must still hold the CLI, the directory and the
registry selection that listed the session. The fake endpoint answers
the probe and refuses the attach, and the test checks that the agent
reports its startup error and exits.
@eitsupi
eitsupi requested a review from renkun-ken November 22, 2026 14:26

@renkun-ken renkun-ken 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.

Approved at 983d5fc. Cancelling an acknowledged cell now leaves a later submission free to retry. A client that reconnects before the five-second cleanup deadline receives the definitive cell outcome when dispatch settles. Both picker entry points retain the CLI, directory, and registry that listed the session. I verified rediscovery through an existing tmux server with conflicting registry settings, and with real arf 0.7.2 on macOS after the same R process moved its IPC socket.

The extension built, TypeScript compiled, and 147 focused Node tests passed. The packaged-extension adoption test with real R and the new picker regression in VS Code also passed. I verified that the approved, still-open follow-up sets r.interactive.arfPath to machine-overridable and adds folder-selection coverage. PR #1988

One non-blocking suggestion is to fold environment key names to uppercase only on Windows in withRegistryEnvironment(). On macOS I confirmed that the helper removes lowercase home as well as HOME. Unix treats these as distinct, so a CLI wrapper could lose an unrelated variable during rediscovery.

@eitsupi
eitsupi merged commit a818e94 into main Nov 22, 2026
16 checks passed
@eitsupi
eitsupi deleted the fix/interactive-adopted-stop branch November 22, 2026 15:08
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.

2 participants