pi-mono/packages/coding-agent/docs
robopipi[bot] 1708b47889
fix(coding-agent): process idle ctx.shutdown() requests in RPC mode (#11746)
In RPC mode, `shutdownHandler` only set a `shutdownRequested` flag. That
flag was checked in two places: after a stdin command was handled and on
`agent_settled`. If an extension called `ctx.shutdown()` while the
session was idle (for example from a timer), neither check ever ran. The
process stayed alive until the client sent another command. Interactive
mode already shuts down right away in this case.

Changes in `packages/coding-agent/src/modes/rpc/rpc-mode.ts`:
- **One check for all paths:** `checkShutdownRequested()` shuts down
only when a request is pending, input is attached, no command is being
handled, and the session is idle. Each of those ending calls it again:
command completion, prompt settle, `agent_settled`, and input attach.
- **Idle requests:** the shutdown handler schedules the check on the
next tick, so the requesting handler finishes first. The conditions are
checked when the tick runs, not when it is scheduled: a stdin command
that arrives in between is handled first, and shutdown follows when it
finishes.
- **Requests during startup:** a request made before input is attached
(for example a synchronous `ctx.shutdown()` in `session_start`) is
checked once the input reader is attached.
- **Requests during a command:** shutdown waits until the command
finishes, including extension commands. A `prompt` command returns
before `session.prompt()` settles, so it stays counted as in flight
until the prompt settles, and the shutdown check runs then. This also
fixes an existing ordering problem: an extension command that called
`ctx.shutdown()` could exit the process before its `prompt` response was
written.
- **Repeated requests:** `checkShutdownRequested()` does nothing once
shutdown has started, so a second `ctx.shutdown()` does not call
`process.exit` in the middle of async cleanup. A second stdin EOF or
signal still forces exit, as before.

Shutdown requested during a running turn is still handled on
`agent_settled`, as before. A request made while an extension-started
compaction runs (not idle, no run to settle) still waits for the next
command or run; `docs/rpc.md` and `docs/extensions.md` note this.

Docs (`docs/rpc.md`, `docs/extensions.md`) and the CHANGELOG are
updated.

## Testing
- Added three tests to `test/rpc-prompt-response-semantics.test.ts`:
  - an async `ctx.shutdown()` on an idle session exits;
- an extension command that calls `ctx.shutdown()` gets its `prompt`
response written before exit;
- a command that arrives after an idle request but before the scheduled
check runs gets its response written before exit.

  All three fail without the change and pass with it.
- `npm run check` passes. All `test/rpc*` tests pass.
- In `./test.sh`, all coding-agent tests pass. The only failures were
timeouts in durable/harness tests (`store-storage`, `tools-images`,
connection close), which this change does not touch.
- Reproduction from the issue: run `pi --mode rpc --no-session -ne -e
./shutdown-probe.mjs` with stdin held open and nothing sent.
- Before the change, the process stayed alive until a timeout killed it
(exit 124).
- After the change, `session_shutdown` fires and the process exits with
code 0.
- A synchronous `ctx.shutdown()` in `session_start` also exits with code
0.

Fixes #11599

Requested by @petrroll

---------

Co-authored-by: triagent <triagent@localhost>
Co-authored-by: Petr Houška <houskape@gmail.com>
2026-11-03 21:27:14 +01:00
..
images docs(coding-agent): refresh documentation (#9898) 2026-09-22 16:48:19 +02:00
cli-integration.md fix(coding-agent): warn and exit non-zero in print mode on truncated response 2026-10-12 16:06:42 +02:00
cli.md feat(coding-agent): add PI_SELF_UPDATE_COMMAND and selfUpdateCommand for pi update (#11629) 2026-10-30 22:30:39 +01:00
codemode.md fix(coding-agent): queue codemode scripts at the worker limit 2026-10-20 20:50:27 +02:00
compaction.md fix(coding-agent): remove fixed output limits from summary requests (#11036) 2026-10-16 13:53:16 +02:00
configuration.md feat(coding-agent): codemode and MCP 2026-09-29 09:12:50 +02:00
containerization.md docs(coding-agent): refresh documentation (#9898) 2026-09-22 16:48:19 +02:00
custom-provider.md docs(coding-agent): shorten device-code login guidance 2026-10-22 21:38:58 +02:00
docs.json docs(ai,coding-agent): generate models.json reference from schemas (#11038) 2026-10-16 21:53:00 +01:00
environment-variables.md feat(coding-agent): add PI_SELF_UPDATE_COMMAND and selfUpdateCommand for pi update (#11629) 2026-10-30 22:30:39 +01:00
extensions.md fix(coding-agent): process idle ctx.shutdown() requests in RPC mode (#11746) 2026-11-03 21:27:14 +01:00
how-pi-works.md docs(coding-agent): refresh documentation (#9898) 2026-09-22 16:48:19 +02:00
index.md docs(coding-agent): add Radius to providers page, rename provider docs sections 2026-10-01 17:06:00 +02:00
json.md feat(tui,coding-agent): report program status via OSC 7501 2026-10-07 19:05:41 +00:00
keybindings.md fix(tui,coding-agent): handle selection page keys in all scrolling lists 2026-10-28 19:05:21 +01:00
llama-cpp.md docs(coding-agent): update llama.cpp setup for llama.app (#10179) 2026-10-27 16:21:37 +01:00
mcp.md feat(coding-agent): prefer the client ID metadata document for MCP OAuth 2026-10-30 10:36:22 +01:00
message-types.md feat(ai): carry responseModel on pi-messages terminal events (#11265) 2026-10-23 10:54:18 +02:00
models-configuration.md Anthropic message threads (#11545) 2026-11-03 14:07:57 +01:00
models.md feat(ai): add Microsoft-Decision-1 classifier to GitHub Copilot (#11679) 2026-11-02 16:12:43 +01:00
nvidia-rtx-llama-cpp.md docs(coding-agent): restructure NVIDIA RTX llama.cpp prerequisites 2026-10-13 15:04:38 +01:00
packages.md docs(coding-agent): document how -e packages are cached and refreshed 2026-10-29 21:43:11 +01:00
prompt-templates.md docs(coding-agent): refresh documentation (#9898) 2026-09-22 16:48:19 +02:00
providers.md feat(ai,coding-agent): list only the OpenRouter models a key may use (#10672) 2026-10-12 10:17:40 +02:00
quickstart.md docs(quickstart): add Windows installer tab (#11231) 2026-10-28 10:10:44 +01:00
rpc-commands.md feat(coding-agent): report prompt disposition in RPC responses 2026-09-25 14:53:11 +02:00
rpc-extension-ui.md docs(coding-agent): refresh documentation (#9898) 2026-09-22 16:48:19 +02:00
rpc.md fix(coding-agent): process idle ctx.shutdown() requests in RPC mode (#11746) 2026-11-03 21:27:14 +01:00
sdk.md docs(coding-agent): add Radius to providers page, rename provider docs sections 2026-10-01 17:06:00 +02:00
security.md feat(coding-agent): codemode and MCP 2026-09-29 09:12:50 +02:00
session-format.md feat(coding-agent): Virtual models (#10035) 2026-09-28 23:30:45 +02:00
sessions.md docs(coding-agent): document which working directory resumed sessions use (#11617) 2026-10-30 17:22:05 +00:00
settings.md feat(coding-agent): add PI_SELF_UPDATE_COMMAND and selfUpdateCommand for pi update (#11629) 2026-10-30 22:30:39 +01:00
shell-aliases.md docs(coding-agent): refresh documentation (#9898) 2026-09-22 16:48:19 +02:00
skills.md docs(coding-agent): refresh documentation (#9898) 2026-09-22 16:48:19 +02:00
slash-commands.md docs(coding-agent): document which working directory resumed sessions use (#11617) 2026-10-30 17:22:05 +00:00
terminal-setup.md fix(tui): treat ESC+Return with native Shift held as Shift+Enter 2026-10-29 20:17:54 +01:00
termux.md docs(coding-agent): refresh documentation (#9898) 2026-09-22 16:48:19 +02:00
themes.md feat(coding-agent): use pi.dev configuration schemas (#10751) 2026-10-11 14:48:42 +01:00
tmux.md docs(coding-agent): list external limitations as known issues with remedies (#11647) 2026-10-31 17:55:58 +01:00
tui.md feat(coding-agent): let inline ctx.ui.custom() components report blocked program status (#11471) 2026-10-27 09:27:28 +01:00
usage.md feat(coding-agent): configure session share destination 2026-10-13 15:41:13 +02:00
virtual-models.md feat(coding-agent): Virtual models (#10035) 2026-09-28 23:30:45 +02:00
windows.md docs(coding-agent): list external limitations as known issues with remedies (#11647) 2026-10-31 17:55:58 +01:00