Skip to content

feat: wait for background jobs before an rpc turn settles when asked - #77

Merged
Pringled merged 2 commits into
Pringled:mainfrom
diniket:feat/rpc-settle-wait
Nov 28, 2026
Merged

Pringled merged 2 commits into
Pringled:mainfrom
diniket:feat/rpc-settle-wait

Conversation

@diniket

@diniket diniket commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

pi-background: wait for the job before an rpc turn settles when the host asks

Single commit on top of e708fe8, no local customizations. The full commit (code, README, tests) is
also stored as 0001-feat-rpc-settle-wait.patch (git am format) so the change stays reproducible.

Problem

bg_run survives a turn that ends, but the result only reaches the model because the job's wake is a
sendMessage({ triggerTurn: true }) into the session that is still running. In --mode rpc that
holds only while the caller keeps the process and keeps reading its JSONL stream.

Pi Web does (mode: "rpc", session process persists), so jobs report normally. Open WebUI, which
drives Pi through Open Terminal, does not: its runner treats the first agent_settled as the end of
the reply (while not settled: await event(await anext(messages))), then closes the transport, and
Open Terminal implements that close as SIGTERM to the session's process group
(start_new_session=True + killpg). The job dies with Pi, session_shutdown aborts it, and the
report is never sent. The user sees the reply stop right after "Started bg1" and nothing ever
follows.

Measured with a 15 s job (sleep 15; echo DONE >> marker), three hosts, same extension:

host settle job
pi --mode rpc, transport kept open after the job completed, wake read
Pi Web session after the job completed, wake read
Open WebUI via Open Terminal immediately, DELETE +0.1 s killed, log ends [bg] aborted

pi -p had the same shape and #59 fixed it for print/json only, deliberately: "The TUI and rpc
are unchanged". The rpc case needs the same wait, but only when the host wants it.

Change

PI_BACKGROUND_SETTLE_WAIT=1 in the environment of the process that starts pi --mode rpc makes
agent_before_settle wait like print/json already do. Unset (the default) rpc behaves exactly as
before, so a live host such as Pi Web keeps getting the turn settled and the report as a later turn.
It reuses background.exitWait, ignores wake_on jobs (they may never end) and ends at the first
wake.

11 lines in extensions/background.ts, 2 sentences in the README.

Why it does not deadlock or recurse

  • agent_before_settle runs inside _startRun, before _emitAgentSettled, so awaiting there only
    defers the settle: exactly what a host that ends the turn on it needs.
  • During the hook isStreaming is still true (_isAgentRunActive until settle), so the wake's
    sendMessage(triggerTurn) takes the streaming branch and is queued on the agent instead of
    starting a nested run. Pi's boundary then returns shouldContinue = continue || agent.hasQueuedMessages()
    and the same run does agent.continue(): one agent_settled, at the end. Covered by a test that
    counts them.
  • The loop exits at the first wake, when no job that can end is left, or at exitWait.

Tests

node --test packages/pi-background/test/*.test.ts → 13/13 (7 new):

  • rpc + PI_BACKGROUND_SETTLE_WAIT=1: waits, reads the result, exactly one settle;
  • rpc without it: settles with the job running (Pi Web / editor behaviour unchanged);
  • failing job: exit code reaches the model before the settle;
  • two jobs started together: reported once, one settle;
  • background.exitWait cap: gives up and settles with the job running;
  • wake_on job: does not hold the turn;
  • a stop during the wait: agent_settled.aborted is true and the pending report is discarded.

Also checked on the real binary with only this extension loaded (pi -ne -e …/background.ts):
rpc + variable, 6 s job → single settle at 15.2 s with the result in the answer; pi -p unchanged
(waits, reads the result); a second, isolated Open Terminal instance driven with Open WebUI's exact
sequence (settle → DELETE) at 8 s and 150 s → job completed, result inside the same reply, DELETE
after it.

Known limit

Pi does not expose an abort signal inside agent_before_settle (ctx.signal is undefined there)
and emits no event when an abort arrives in that window, so a stop is honoured at the next settle,
not immediately. For the host this targets, that costs nothing: it stops the process, Pi aborts the
waiting job and the wait ends. For an interactive rpc host, a stop waits for the running job, which
is why the behavior is opt-in. A ctx.signal in the boundary would let the hook return early; that
is a Pi-side change, not a pi-background one.

Pi agent@pi-hub and others added 2 commits November 28, 2026 11:00
A host that reads the JSONL stream itself decides what `agent_settled` means. Pi
Web keeps the process and shows the later turn, so waiting there would only hold
the chat open for no reason. Other hosts (Open WebUI through Open Terminal, for
example) treat the first settle as the end of the reply, stop reading and stop the
process, which kills the job before it finishes: its result is never delivered.

`PI_BACKGROUND_SETTLE_WAIT=1` in the environment of the process that starts
`pi --mode rpc` now makes rpc wait like `pi -p` and `--mode json` already do. The
wait reuses `background.exitWait`, ignores jobs started with `wake_on`, and ends at
the first wake, so the result is read and the settle arrives once, after the
answer. Unset, rpc behaves exactly as before.

The wait relies on the session still being active during `agent_before_settle`:
`isStreaming` is true there, so the wake is queued on the agent instead of starting
a nested run, and Pi's boundary hands the queue to `agent.continue()` inside the
same run.

Tests cover both rpc cases, a failing job, jobs started together, the exitWait cap,
a wake_on job and a stop during the wait.
@Pringled

Copy link
Copy Markdown
Owner

@diniket thanks for the PR! I've trimmed the tests since they were a bit too much, will release this soon.

@Pringled
Pringled merged commit 95f4c8d into Pringled:main Nov 28, 2026
2 of 3 checks passed
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