Skip to content

Apply turn options to the live Pi session - #2221

Merged
SawyerHood merged 2 commits into
mainfrom
fix/2160-pi-turn-model-options
Aug 25, 2026
Merged

SawyerHood merged 2 commits into
mainfrom
fix/2160-pi-turn-model-options

Conversation

@SawyerHood

@SawyerHood SawyerHood commented Aug 21, 2026 •

Copy link
Copy Markdown
Collaborator

What was wrong

The Pi bridge read options.model and options.reasoningLevel only when it constructed a session (thread/start, thread/resume, thread/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.

What changed

The bridge now reconciles the options each turn/start carries, ahead of both the prompt branch and the /compact branch. Pi takes the model and the thinking level as spawn flags (--model, --thinking). On pi below 0.84.3 its set_model and 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. The replacement re-reports its identity, resets the native id space, and is announced with session/replaced.

The rebuild never gives up a working session for one that may not start. The replacement is constructed, started and verified — start() answers get_state, waits for the extension's ready, 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/steer is 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: resolvePiModel rejects an unresolvable bare id up front. A provider/id string 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

  • New suite plugins/provider-pi/src/bridge/bridge.turn-options.test.ts: 6 tests over a scripted pi --mode rpc child. A changed model rebuilds and reports session/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.ts updated: a turn with different options now rebuilds, and the bridge still sends no set_model or set_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.
  • Live, real pi 0.84.3 through the pi provider plugin, 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 shows get_state on the replacement, then abort of the old child, thread/identity, session/replaced, prompt. A reasoning-level-only change takes the same path; a repeated selection goes straight to prompt.
  • Live failure path: a turn on ghostprovider/some-model (pi refuses it at spawn) fails with the provider error and sends no abort and no session/replaced; the next turn on the previous model is a plain turn/start answered by the still-live child — no thread/resume anywhere in the run. Whole-thread census: set_model/set_thinking_level = 0, abort = 1, session/replaced = 1.

Fixes #2160

AGENT GENERATED

@SawyerHood
SawyerHood force-pushed the fix/2160-pi-turn-model-options branch from 35cc55a to e7eadc4 Compare August 24, 2026 23:44
SawyerHood and others added 2 commits August 24, 2026 18:11
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
SawyerHood force-pushed the fix/2160-pi-turn-model-options branch from e7eadc4 to 32c9ac8 Compare August 25, 2026 01:12
@SawyerHood
SawyerHood merged commit 5f070f1 into main Aug 25, 2026
15 checks passed
@SawyerHood
SawyerHood deleted the fix/2160-pi-turn-model-options branch August 25, 2026 02:06
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.

Pi keeps using previous model after model picker change until /compact

1 participant