Skip to content

fix(tui): wait for the session create before switching a session's model - #57361

Open
Bearmancer wants to merge 2 commits into
anomalyco:devfrom
Bearmancer:fix/tui-model-switch-session-creation
Open

Bearmancer wants to merge 2 commits into
anomalyco:devfrom
Bearmancer:fix/tui-model-switch-session-creation

Conversation

@Bearmancer

Copy link
Copy Markdown

Issue for this PR

Fixes #57354

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

The client creates a session optimistically, so the session exists in the TUI before the server has stored it. A model switch taken from the composer selection (blank Enter in the session tab, or a slash command) is sent right away, the server answers 404 SessionNotFoundError, and the TUI shows a red Failed to prepare session toast.

This gates the agent and model switch on the in-flight create, the way session.prompt is already gated on it in sendAdmission.

The switch that raced the create is commitModel; it is reached from the composer selection without a create gate, while the prompt path is gated.

From the server log of the reported case, the switch left 152 ms before the session was created (time.created = 07:40:36.919Z):

timestamp=2026-10-20T07:40:36.767Z message="Sent HTTP response" http.method=POST http.url=/api/session/ses_ea7beb81effeGcyHs8y0qQt2Fj/model http.status=404

How did you verify your code works?

packages/tui/test/prompt-model-switch.test.tsx holds the create response back and asserts that the model switch does not leave before the create was answered, and that it carries the session id unchanged.

The test fails on the base revision (the switch leaves with the create still pending) and passes with this change. @opencode/tui and @opencode/client typechecks pass.

Screenshots / recordings

No visual change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix works. With the #57354 drive script and network jitter, the base showed the Session not found toast in 3 of 4 runs and this branch in 0 of 4. The new test fails on the base and passes here, and the tui and client typechecks pass. Waiting on the create the same way sendAdmission does is the right approach, and it does nothing in the prompt path, where prepare already runs after the create.

Public API: unchanged. data.session.creation() is a new internal method on the Solid data layer, and nothing is removed or changed.

The only thing worth changing is in the test (see inline).

Before

After

// the switch may not leave before the create was answered.
const early = await Promise.race([
switched.promise,
new Promise<undefined>((resolve) => setTimeout(() => resolve(undefined), 2000)),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds a fixed 2 s wait to every run, and it only checks that nothing happened during that window. That makes the test slower, and it could pass on the base if the switch were just slow. The order check at the end (sent.createAnswered === true, with the create held until create.resolve()) already covers the fix by itself: you could release the create once the empty Enter has been handled and drop the Promise.race. The toUpperCase check on line 93 has nothing to do with this bug and can be dropped.

@Bearmancer

Copy link
Copy Markdown
Author

@opencode-agent — Addressed the review in commit 211ae90: removed the 2s Promise.race wait from the test and dropped the redundant toUpperCase check. Both TUI and client tests and typechecks pass.

Bearmancer and others added 2 commits November 11, 2026 14:45
The client creates a session optimistically, so a model switch taken from the composer selection can reach the server before the session is stored. The server answers 404 SessionNotFoundError and the TUI shows a Failed to prepare session toast.

Gate the agent and model switch on the in-flight create, the way the prompt path already gates on it.
@Bearmancer
Bearmancer force-pushed the fix/tui-model-switch-session-creation branch from 211ae90 to 06d99ef Compare November 11, 2026 09:15
@Bearmancer

Copy link
Copy Markdown
Author

Rebased onto origin/dev:e56eff2bc01649b3aa21224876b8874e77dea4b0 -> new head 06d99efd43 (2 commits replayed, clean rebase, no conflicts). Scope preserved: packages/client/src/solid/data.ts (+6), packages/tui/src/component/prompt/index.tsx (+8/-1), test/prompt-model-switch.test.tsx (+90). Bot inline test nit already addressed in-head: fixed 2s wait removed, Promise.race dropped, toUpperCase check dropped — current test uses create.promise gating + waitForFrame order checks only (verified, no sleep/2000 remains). Verification: no test rerun in this throwaway clone (deps not installed); relying on CI. No generators touched.

This branch has not been deployed

No deployments
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.

tui: model switch races session creation and gets a Session not found toast

1 participant