Skip to content

feat(chat): change the reasoning level while a run streams - #1022

Merged
agegr merged 1 commit into
mainfrom
claude/issue-851-feasibility-4080a5
Sep 30, 2026
Merged

agegr merged 1 commit into
mainfrom
claude/issue-851-feasibility-4080a5

Conversation

@agegr

@agegr agegr commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Fixes #851

Problem

During a long agent run the reasoning level cannot be changed: the control next to Stop is greyed out until the run ends. #777 disabled it on the assumption that the level cannot change mid-turn.

That assumption does not hold. pi-agent-core snapshots reasoning into the loop config when a run starts, but AgentSession installs prepareRequest and prepareNextTurnWithContext hooks that re-read agent.state.thinkingLevel before every model request. pi's own TUI lets Shift+Tab change the level during a run. set_thinking_level in lib/rpc-manager.ts never refused the command either — only the composer did.

Changes

  • Usable control while streaming. ChatInput no longer disables the reasoning button (or dims it, or drops its hover state) while a run streams, and no longer force-closes its menu when a run starts. The tool preset dropdown is still hidden and closed while streaming, as before. The tooltip text is unchanged.
  • Behavior. A new level applies from the next model request of the run; the response already streaming keeps the level it was sent with. In an agentic run that is the next step after the current tool call.
  • SDK regression guard. lib/thinking-level-mid-run.integration.test.mjs drives a real AgentSession with the SDK's faux provider and changes the level while the first request streams; it asserts the requests go out at low, then high. If an SDK upgrade goes back to freezing the level for the whole run, this fails instead of the control silently doing nothing.
  • AGENTS.md records why the control is live mid-run and points at that test.

Tests

  • npm test: all pass except lib/worktree.test.mjs "forced worktree removal passes Git's force flag", which fails identically on an untouched checkout of main (unrelated).
  • tsc --noEmit and npm run lint pass.
  • End to end in the browser against a dev server with a temporary PI_CODING_AGENT_DIR and a local OpenAI-compatible endpoint that streams for ~20s and then calls ls: sent at low, opened the menu mid-stream and picked high. The endpoint logged reasoning_effort: "low" for the first request and "high" for the request after the tool result; the session file got a mid-run thinking_level_change entry, the run finished normally, and the console had no errors.

Known limitation

While a run is active the client reconciles with GET /api/agent/[id] every 15s. A response that was read on the server just before the change can briefly show the old level in the button; the next poll corrects it. The level the server uses is not affected.

🤖 Generated with Claude Code

The reasoning control was disabled for the whole run on the assumption
that the level cannot change mid-turn (#777). pi-agent-core does snapshot
`reasoning` into the loop config when a run starts, but AgentSession's
prepareRequest / prepareNextTurnWithContext hooks re-read
agent.state.thinkingLevel before every model request, and pi's TUI lets
Shift+Tab change it during a run. So set_thinking_level mid-run applies
from the next request; only the response already streaming keeps its
level. The server never refused the command, only the composer did.

Keep the control usable while streaming and stop closing its menu when a
run starts. The tooltip is unchanged.

An integration test drives a real AgentSession with the faux provider
and changes the level while the first request streams: the requests go
out at low, then high. That pins the SDK behavior the control now relies
on across SDK upgrades.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@agegr
agegr merged commit d0bf6be into main Sep 30, 2026
2 checks passed
@agegr
agegr deleted the claude/issue-851-feasibility-4080a5 branch October 1, 2026 13:28
Kano-u pushed a commit to Kano-u/pi-web that referenced this pull request Oct 2, 2026
…式中改思考等级)

解决 agegr#1018 之后这两个提交 0 冲突。

- agegr#1021 技能/插件分组标题本身就是开关
- agegr#1022 一次运行流式输出过程中即可修改思考等级(agegr#851)
agegr added a commit that referenced this pull request Nov 3, 2026
The composer's model selector was disabled while a run streamed, a lock
from the first version. pi's TUI keeps Ctrl+P / Ctrl+L working mid-run,
and AgentSession hands the agent loop `agent.state.model` before every
turn, so `set_model` answers from the run's next request, exactly as the
reasoning level already does here (#1022).

The selector stays usable now. A switch during a run (or a `!` command)
takes the model and level from the live state instead of reading the
session file, which would replace the run's messages under the stream;
the run's end reads the file as before. An idle switch is unchanged.

lib/model-mid-run.integration.test.mjs pins the SDK side with the faux
provider, as the thinking-level test does.

Co-authored-by: Claude Opus 5.5 <[email protected]>
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 6, 2026
agegr#1022)

The reasoning control was disabled for the whole run on the assumption
that the level cannot change mid-turn (agegr#777). pi-agent-core does snapshot
`reasoning` into the loop config when a run starts, but AgentSession's
prepareRequest / prepareNextTurnWithContext hooks re-read
agent.state.thinkingLevel before every model request, and pi's TUI lets
Shift+Tab change it during a run. So set_thinking_level mid-run applies
from the next request; only the response already streaming keeps its
level. The server never refused the command, only the composer did.

Keep the control usable while streaming and stop closing its menu when a
run starts. The tooltip is unchanged.

An integration test drives a real AgentSession with the faux provider
and changes the level while the first request streams: the requests go
out at low, then high. That pins the SDK behavior the control now relies
on across SDK upgrades.

Co-authored-by: Claude Opus 5.5 <[email protected]>
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 8, 2026
agegr#1022)

The reasoning control was disabled for the whole run on the assumption
that the level cannot change mid-turn (agegr#777). pi-agent-core does snapshot
`reasoning` into the loop config when a run starts, but AgentSession's
prepareRequest / prepareNextTurnWithContext hooks re-read
agent.state.thinkingLevel before every model request, and pi's TUI lets
Shift+Tab change it during a run. So set_thinking_level mid-run applies
from the next request; only the response already streaming keeps its
level. The server never refused the command, only the composer did.

Keep the control usable while streaming and stop closing its menu when a
run starts. The tooltip is unchanged.

An integration test drives a real AgentSession with the faux provider
and changes the level while the first request streams: the requests go
out at low, then high. That pins the SDK behavior the control now relies
on across SDK upgrades.

Co-authored-by: Claude Opus 5.5 <[email protected]>
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 8, 2026
The composer's model selector was disabled while a run streamed, a lock
from the first version. pi's TUI keeps Ctrl+P / Ctrl+L working mid-run,
and AgentSession hands the agent loop `agent.state.model` before every
turn, so `set_model` answers from the run's next request, exactly as the
reasoning level already does here (agegr#1022).

The selector stays usable now. A switch during a run (or a `!` command)
takes the model and level from the live state instead of reading the
session file, which would replace the run's messages under the stream;
the run's end reads the file as before. An idle switch is unchanged.

lib/model-mid-run.integration.test.mjs pins the SDK side with the faux
provider, as the thinking-level test does.

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

是否能支持会话中途改变模型思考强度?

1 participant