Repository navigation
Apply turn options to the live Pi session - #2221
Merged
Merged
Conversation
SawyerHood
force-pushed
the
fix/2160-pi-turn-model-options
branch
from
August 24, 2026 23:44
35cc55a to
e7eadc4
Compare
The Pi bridge read options.model and options.reasoningLevel only when it constructed a session (thread/start, resume, fork). handleTurnStart ignored params.options, so a model or reasoning level picked after the session existed never reached Pi: every later turn, and any manual compaction, ran on the construction model until something rebuilt the session. The runtime stopped diffing execution options in #1640 and expects each bridge to reconcile them itself; Codex and Claude do, Pi did not. Reconcile on every turn/start, ahead of both the prompt and the /compact branch. Pi takes the model and the thinking level as spawn flags (--model, --thinking), and its set_model / set_thinking_level commands persist the selection as the user's default in ~/.pi/agent/settings.json, which construction never does. So a changed selection rebuilds the pi child from the thread's session file, the way the Codex bridge rebuilds from its rollout: the history is on disk, so the conversation survives, and the replacement re-reports its identity, resets the native id space, and is announced with session/replaced. A model that does not resolve fails the turn with that error instead of silently keeping the old one. turn/steer is left alone: a steer joins the turn already running on the model it started with, and rebuilding there would kill that run. Fixes #2160 Co-Authored-By: Claude <[email protected]>
A turn that changed the model or reasoning level rebuilt the thread's pi child by closing the live child first and constructing the replacement after. When the replacement never started -- a `provider/id` pi accepts at spawn but cannot run, a readiness timeout, a crash -- the thread was left with no session at all. The turn answered a bare -32000, and every later turn/start hit the "No active pi session" guard, while the runtime still held the thread, so nothing resumed it. Only thread/stop plus thread/resume recovered. Before this PR the option change was ignored and the thread kept working, so the rebuild introduced the failure mode. Construct, start and verify the replacement before closing the child it replaces. `start()` is the verification: it answers get_state, waits for the extension's `ready`, and refuses a child that came up on another model. Two pi children may hold one session file at once -- the file is append-only, the outgoing child is idle, and pi takes no lock (checked against pi 0.84.3) -- so the overlap is safe. A replacement that dies at spawn now costs the turn alone: the previous child never stopped serving and the next turn runs on it. The retired child's close is not awaited, so the old child's abort/leaf exchange does not spend the turn's request budget. Refs #2160
SawyerHood
force-pushed
the
fix/2160-pi-turn-model-options
branch
from
August 25, 2026 01:12
e7eadc4 to
32c9ac8
Compare
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.
What was wrong
The Pi bridge read
options.modelandoptions.reasoningLevelonly when it constructed a session (thread/start,thread/resume,thread/fork).handleTurnStartignoredparams.options. So a model or reasoning level picked after the session existed never reached Pi. Every later turn, and any manual compaction, ran on the construction model until something rebuilt the session. The runtime stopped diffing execution options in #1640 and expects each bridge to reconcile them itself. Codex and Claude do. Pi did not.What changed
The bridge now reconciles the options each
turn/startcarries, ahead of both the prompt branch and the/compactbranch. Pi takes the model and the thinking level as spawn flags (--model,--thinking). On pi below 0.84.3 itsset_modelandset_thinking_levelcommands persist the selection as the user's default in~/.pi/agent/settings.json, which construction never does. So a changed selection rebuilds the pi child from the thread's session file, the way the Codex bridge rebuilds from its rollout. The history is on disk, so the conversation survives. The replacement re-reports its identity, resets the native id space, and is announced withsession/replaced.The rebuild never gives up a working session for one that may not start. The replacement is constructed, started and verified —
start()answersget_state, waits for the extension'sready, and refuses a child that came up on another model — before the child it replaces is closed. Two pi children may hold one session file at once: the file is append-only, the outgoing child is idle, and pi takes no lock (checked against pi 0.84.3). So a selection the rebuild cannot serve costs the turn alone, and the next turn runs on the child that never stopped serving. The retired child's close is not awaited, so its abort/leaf exchange does not spend the turn's request budget.turn/steeris left alone: a steer joins the turn already running on the model it started with, and a rebuild there would kill that run.Note on scope:
resolvePiModelrejects an unresolvable bare id up front. Aprovider/idstring under a provider pi does not know is passed through to the spawn, where pi refuses it; the turn then fails on the spawn error instead. Either way the live session is kept.How you verified
plugins/provider-pi/src/bridge/bridge.turn-options.test.ts: 6 tests over a scriptedpi --mode rpcchild. A changed model rebuilds and reportssession/replaced; a changed reasoning level rebuilds; a repeated selection does not; a manual compaction runs on the selected model; an unresolvable model fails the turn and keeps the live session; a replacement that dies at spawn fails the turn and the next turn still runs on the previous child.bridge.settings.test.tsupdated: a turn with different options now rebuilds, and the bridge still sends noset_modelorset_thinking_level.pnpm exec turbo run test --filter=bb-plugin-provider-pi --force: 112 tests passed.pnpm exec turbo run test --filter=@bb/agent-runtime --force: 318 tests passed.pnpm exec turbo run build typecheck lint: 92/92 tasks passed.node scripts/check-provider-literal-ratchet.mjs --base origin/main: OK.github-copilot/gpt-5-mini→github-copilot/grok-4.6: the turn requested as grok-4.6 is answered by grok-4.6 (pi session file), the recorded wire showsget_stateon the replacement, thenabortof the old child,thread/identity,session/replaced,prompt. A reasoning-level-only change takes the same path; a repeated selection goes straight toprompt.ghostprovider/some-model(pi refuses it at spawn) fails with the provider error and sends noabortand nosession/replaced; the next turn on the previous model is a plainturn/startanswered by the still-live child — nothread/resumeanywhere in the run. Whole-thread census:set_model/set_thinking_level= 0,abort= 1,session/replaced= 1.Fixes #2160