Repository navigation
feat: wait for background jobs before an rpc turn settles when asked - #77
Merged
Merged
Conversation
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.
Owner
|
@diniket thanks for the PR! I've trimmed the tests since they were a bit too much, will release this soon. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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) isalso stored as
0001-feat-rpc-settle-wait.patch(git amformat) so the change stays reproducible.Problem
bg_runsurvives a turn that ends, but the result only reaches the model because the job's wake is asendMessage({ triggerTurn: true })into the session that is still running. In--mode rpcthatholds 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, whichdrives Pi through Open Terminal, does not: its runner treats the first
agent_settledas the end ofthe reply (
while not settled: await event(await anext(messages))), then closes the transport, andOpen Terminal implements that close as
SIGTERMto the session's process group(
start_new_session=True+killpg). The job dies with Pi,session_shutdownaborts it, and thereport 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:pi --mode rpc, transport kept openDELETE+0.1 s[bg] abortedpi -phad the same shape and #59 fixed it forprint/jsononly, deliberately: "The TUI and rpcare unchanged". The rpc case needs the same wait, but only when the host wants it.
Change
PI_BACKGROUND_SETTLE_WAIT=1in the environment of the process that startspi --mode rpcmakesagent_before_settlewait likeprint/jsonalready do. Unset (the default) rpc behaves exactly asbefore, so a live host such as Pi Web keeps getting the turn settled and the report as a later turn.
It reuses
background.exitWait, ignoreswake_onjobs (they may never end) and ends at the firstwake.
11 lines in
extensions/background.ts, 2 sentences in the README.Why it does not deadlock or recurse
agent_before_settleruns inside_startRun, before_emitAgentSettled, so awaiting there onlydefers the settle: exactly what a host that ends the turn on it needs.
isStreamingis still true (_isAgentRunActiveuntil settle), so the wake'ssendMessage(triggerTurn)takes the streaming branch and is queued on the agent instead ofstarting a nested run. Pi's boundary then returns
shouldContinue = continue || agent.hasQueuedMessages()and the same run does
agent.continue(): oneagent_settled, at the end. Covered by a test thatcounts them.
exitWait.Tests
node --test packages/pi-background/test/*.test.ts→ 13/13 (7 new):PI_BACKGROUND_SETTLE_WAIT=1: waits, reads the result, exactly one settle;background.exitWaitcap: gives up and settles with the job running;wake_onjob: does not hold the turn;agent_settled.abortedistrueand 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 -punchanged(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,
DELETEafter it.
Known limit
Pi does not expose an abort signal inside
agent_before_settle(ctx.signalisundefinedthere)and emits no event when an
abortarrives 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.signalin the boundary would let the hook return early; thatis a Pi-side change, not a pi-background one.