Skip to content

fix(tui): stop a session switch from leaving the old session on screen - #797

Merged
huichen merged 2 commits into
mainfrom
fix/tui-switch-residue
Sep 23, 2026
Merged

huichen merged 2 commits into
mainfrom
fix/tui-switch-residue

Conversation

@huichen

@huichen huichen commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

What

ctrl-r → pick a session could leave the previous session on screen. Auditing the switch path (spawn_switch_flow → apply_refresh_state → restore_session_input → apply_messages → hide_overlay) turned up five ways it happens:

# Symptom Cause
1 The old transcript stays on screen under the new session's name, and a prompt typed there goes to the new session get_messages failed → apply_messages early-returned without clearing
2 The old session's saved draft is restored into the new session's input box; later refreshes//status address the wrong session get_state failed (a separate call) → state.session_id stayed on the session just left
3 The new conversation opens mid-transcript and never follows its own output clear_messages kept the previous viewport_top/auto_scroll
4 A superseded pick replaces the transcript with a session the app is not showing (and can leave the client addressing it, so the next prompt lands in the wrong session) two picks overlap (the menu stays open until the first result lands) and the agent has no notion of a "current" session — whichever RPC lands last wins
5 The previous session's text appears in the new transcript, or replaces its last reply a frame still in flight from the session just left is applied (text_chunk appends, agent_end overwrites)

Plus: the window title kept the previous session's name (apply_refresh_state never read session_name).

Fix

  • UiCmd::SessionSwitched now carries the request's target and whether the switch really happened (RPC ok, not cancelled, client bound to the target). The app keeps the newest request (latest_session_switch); a result that is not the newest — or that arrives after another lifecycle flow (/new, a fork) moved the app — is dropped, and the client is re-pointed at the session the app actually shows. A declined switch keeps the transcript, because nothing changed on the wire.
  • A successful switch always adopts the target as the app's session identity, so a failed get_state no longer leaves drafts/refreshes keyed to the session just left.
  • A failed transcript load clears the transcript and names the failure in the banner instead of showing the old conversation as this session's history.
  • ChatArea::clear_messages re-anchors to the tail (the old scroll position referred to lines that no longer exist).
  • handle_agent_event ignores session-stamped events from another session (generalizing the guard that only covered compaction_*).
  • apply_refresh_state adopts the snapshot's session_name and refreshes the terminal title.

Tests

Nine new tests in tui/src/app.rs cover each path (failed get_messages, failed get_state, view re-anchoring, both superseded orderings, takeover by /new, declined switch, foreign events, title name). cargo test -p future-tui 2030 passed; cargo fmt --check and cargo clippy --all-targets -D warnings clean. (crash::tests::dropping_the_guard_restores_the_hook_that_was_installed_before flaked once under parallel load, passes in isolation and on re-run — pre-existing panic-hook global state, untouched here.)

A successful `switch_session` could leave the previous session's display
in place, and a superseded one could replace the transcript with a
conversation the app is not showing:

- A failed `get_messages` left the old transcript on screen under the new
  session's name while a prompt typed there went to the new session. The
  switch has already happened on the wire, so the transcript is now
  cleared and the failure is named instead of being shown as this
  session's history.
- A failed `get_state` (a separate call) left `state.session_id` on the
  session just left, so that session's saved draft was restored into the
  new session's input box and later refreshes addressed the wrong
  session. The identity now follows the switch on its own.
- The cleared chat kept the previous session's scroll offset (and its
  `auto_scroll` off), so the new conversation opened mid-transcript and
  never followed its own output.
- Two picks can overlap (the sessions menu stays open until the first
  result lands) and the agent has no notion of a "current" session:
  whichever `switch_session` RPC lands last wins on the wire. Results are
  now matched against the newest request, so a stale one is dropped and
  the client is pointed back at the session the app actually shows.
- A frame still in flight from the session just left could append to the
  new transcript (`text_chunk`) or overwrite its last reply (`agent_end`).
  Broadcast events are session-stamped, so foreign ones are ignored —
  generalizing the guard that only covered `compaction_*`.
- The window title kept the previous session's name; the name now follows
  the `get_state` snapshot.
@huichen
huichen enabled auto-merge (squash) September 23, 2026 08:24
@huichen
huichen merged commit 5ecba10 into main Sep 23, 2026
12 checks passed
@huichen
huichen deleted the fix/tui-switch-residue branch September 23, 2026 08:29
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