fix(tui): repaint the header context window on a /model switch

After `/model`, the header kept the previous model's window ("15K /
1.0M" on a 262K model) until the next turn or a restart. The pager
caches the window inside its context snapshot (streaming `totalTokens`
updates refresh it from the catalog) and nothing refreshed the cache
when the session's model changed. Comparing cached numbers against
catalog windows cannot tell a catalog-derived total from a shell
effective limit (KIGI_DEBUG_CONTEXT_WINDOW, header upgrades), so the
shell, which installs the effective window, now reports it:

- kigi-shell: `handle_set_session_model` returns the installed window;
  `model_switch::apply` stamps it as `totalContextTokens` in the
  `session/set_model` response meta and carries it on the
  `ModelChanged` broadcast (`context_window`, serde-default: old pagers
  ignore it, old leaders omit it). The response meta previously
  serialized the actor's inner `Result` and reported success on an
  actor failure; that error now propagates.
- kigi-tui: `Effect::SwitchModel` reads the meta; `SwitchModelComplete`
  carries `Ok(Option<u64>)`; the completion handler and the follower
  `ModelChanged` arm call `AgentView::apply_context_window`, which
  re-derives the snapshot (pct, free tokens) and reports whether it
  changed so a follower redraws on a window-only change.
- Tests: the bare `/model <name>` dispatch path asserts the snapshot and
  the drawn header; follower with/without the field; same-model new
  window redraw; actor returns the window; serde round trip.

Claude-Session: https://claude.ai/code/session_01RUqV8HVGXcAETyYbZ2tPQ4
This commit is contained in:
雷电芽衣 committed 2026-09-13 00:15:14 -04:00
1 parent ff92a00fea
commit b957e12370
16 files changed
+288 -27

No files matched your search

@@ -812,6 +812,7 @@ pub(super) fn handle_session_notification(notif: &acp::ExtNotification, app: &mu
XaiSessionUpdate::ModelChanged {
model_id,
reasoning_effort,
context_window,
} => {
if agent.session.model_switch_pending {
tracing::debug!(
@@ -847,6 +848,8 @@ pub(super) fn handle_session_notification(notif: &acp::ExtNotification, app: &mu
.session
.models
.set_current(new_model_id.clone(), effort);
let window_changed =
context_window.is_some_and(|total| agent.apply_context_window(total));
agent.session.user_model_preference = Some(new_model_id.clone());
let resolved_effort = agent.session.models.reasoning_effort;
let actually_changed =
@@ -858,7 +861,7 @@ pub(super) fn handle_session_notification(notif: &acp::ExtNotification, app: &mu
"ModelChanged broadcast applied (remote switch)"
);
}
actually_changed
actually_changed || window_changed
}
XaiSessionUpdate::MemoryFiles { files } => {
let entries = crate::views::memory_modal::build_entries(files);