Skip to content

fix(agent): follow server model switches via session.model.selected - #508

Merged
jensenojs merged 1 commit into
sudo-tee:mainfrom
jensenojs:feat/sync-model-from-server
Oct 14, 2026
Merged

jensenojs merged 1 commit into
sudo-tee:mainfrom
jensenojs:feat/sync-model-from-server

Conversation

@jensenojs

@jensenojs jensenojs commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

An attached nvim client can go stale: switching the model or variant in the TUI (or any other client sharing the server) while the panel is closed or unfocused leaves nvim showing the old selection forever.

Upstream now broadcasts switches as session.model.selected on /api/event (added in #52499, first released with 2.0.22; verified live on 2.0.28). On older servers (<=2.0.21) the event stream is completely silent at switch time — verified by watching it while switching — so polling GET /api/session/{id} was the only channel then.

Approach

  • Subscribe to OpencodeEvent:session.model.selected (the v2 observation layer already re-emits every server event as an autocmd). Switches for the active session apply live via a shared apply_server_selection — same entry point used by the GET path.
  • On panel open, run sync_from_server() once to converge state that drifted while the client was not attached (events are incremental; the opening GET closes the gap).
  • v1 protocol has no such event, so v1 only converges on panel open — no polling branch kept.

Verification

  • Live bidirectional test against 2.0.28: TUI model switch -> nvim follows; nvim switch -> TUI follows; variant-only switches follow too.
  • Full ./run_tests.sh green; stylua --check clean. New unit tests cover apply_server_selection (V1/V2 model shapes, variant presence, incomplete refs).

@disrupted

Copy link
Copy Markdown
Contributor

The server does not broadcast model/variant switches over SSE

that sounds like an upstream bug to me. did you check if any issues exist?

@kkannan8728-blip

Copy link
Copy Markdown

U have available to sarvice in

@jensenojs
jensenojs force-pushed the feat/sync-model-from-server branch from 183bcc3 to 3c8322b Compare October 14, 2026 06:21
@jensenojs
jensenojs force-pushed the feat/sync-model-from-server branch from 3c8322b to ea23d9e Compare October 14, 2026 07:03
@jensenojs jensenojs changed the title feat(agent): sync session model from server on panel open feat(agent): follow server model switches via session.model.selected Oct 14, 2026
@jensenojs
jensenojs marked this pull request as ready for review October 14, 2026 07:06
@jensenojs

Copy link
Copy Markdown
Collaborator Author

The server does not broadcast model/variant switches over SSE

that sounds like an upstream bug to me. did you check if any issues exist?

Yes — upstream fixed this in 2.0.22. we just subscribes to that event now, no workaround needed. Thanks!

@jensenojs jensenojs changed the title feat(agent): follow server model switches via session.model.selected fix(agent): follow server model switches via session.model.selected Oct 14, 2026
@jensenojs
jensenojs merged commit e06cbb1 into sudo-tee:main Oct 14, 2026
6 checks passed
@jensenojs
jensenojs deleted the feat/sync-model-from-server branch October 14, 2026 07:09
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.

3 participants