Skip to content

feat(tui): allow /model and /effort while the agent is busy - #4621

Merged
Grapedge merged 8 commits into
mainfrom
busy-model-switch
Nov 3, 2026
Merged

Grapedge merged 8 commits into
mainfrom
busy-model-switch

Conversation

@Grapedge

@Grapedge Grapedge commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

agent 忙时(模型回复中、压缩中、! 命令运行中)输入 /model 或 /effort,会先弹出选择器,选完才报 Cannot switch models while streaming — press Esc or Ctrl-C first.,用户白选一次。这个 PR 之后,忙时可以直接切换,从下一轮开始生效。Kimi 官方模型的切换确认框忙时也照常弹出;选 Compact and continue 会排进消息队列,等当前这一轮结束后先用旧模型压缩,再切到新模型。

Bug Reproduction Steps

N/A

Root Cause

N/A

Code Changes

核心思路

引擎在每一轮第一次请求时固定模型和思考强度,所以 TUI 不再拦截忙时切换:设置立即写入,当前这一轮剩下的请求仍用旧设置,下一轮用新设置。Compact and continue 不一样:压缩请求不属于任何一轮,读的是当时的模型,所以必须先压缩、后切换。忙时选它,就往消息队列里放一个“先压缩再切换”的条目,和普通消息、! 命令、排队的 /compact 一起先进先出。

关键决策

决策 选择 没选 理由
忙时切换的生效时机 立即写入,靠引擎的每轮快照从下一轮生效 排队到这一轮结束再写 引擎已有每轮快照,不需要额外机制
忙时的 Compact and continue 入队,轮到时先压缩再切换 忙时去掉这个选项 忙时和空闲时看到的选项保持一致
队列条目类型 按 mode 区分的联合类型 在原接口上继续加可选字段 模型切换的字段不能和消息、/compact 的字段混用
排队期间已经压缩过 跳过这次压缩,提示后只切换 每次都压缩 避免连续压缩两次

走一遍

sequenceDiagram
    participant User
    participant TUI
    participant Engine
    User->>TUI: 忙时选 Compact and continue
    TUI->>TUI: 队列加入“先压缩再切换”
    Engine-->>TUI: 当前这一轮结束
    TUI->>Engine: 用旧模型压缩
    Engine-->>TUI: 主 agent 压缩完成
    TUI->>Engine: 切换模型和思考强度
    TUI->>Engine: 发出队列里的下一条
Loading

切换完成之前,队列保持占用,压缩完成事件不会提前发出下一条消息,所以下一条一定用新模型。

阅读指南

  1. apps/kimi-code/src/tui/commands/config.ts:确认框忙时不再被跳过;Compact and continue 忙时入队、空闲时直接执行;出队执行和队列占用都在这里。
  2. apps/kimi-code/src/tui/utils/model-switch.ts:删掉忙时拦截,忙时切换成功的提示追加 Takes effect from the next turn.。
  3. apps/kimi-code/src/tui/types.ts:队列条目改成联合类型,新增模型切换条目。
  4. apps/kimi-code/src/tui/kimi-tui.ts、controllers/session-event-handler.ts、components/panes/queue-pane.ts:入队、出队分派、Ctrl-S 屏障、压缩计数和面板显示。

可以略读:staging-leases.ts 和 kimi-tui.ts 里按联合类型收窄的接线。

Screenshots / Recordings

忙时切换,改动前:

   Error: Cannot switch models while streaming — press Esc or Ctrl-C first.

改动后,普通模型直接切换:

 ● Running a command · $ sleep 8
   Switched to model-a with thinking low. Takes effect from the next turn.

改动后,命中云端规则时,忙时照常弹出确认框(规则说明由云端下发,这里是本地测试配置):

 ● Running a command · $ sleep 8
 ─────────────────────────────────────────────────────────────
  Switch model?  model-b → model-a
  ↑↓ navigate · Enter select · Esc cancel
  Local model rule
   ❯ Compact and continue    Lower cost and better output quality when continuing this session
     Continue as-is          Output quality may suffer · cache loss may increase token usage
     Cancel                  Stay on the current model
 ─────────────────────────────────────────────────────────────

选 Compact and continue 后,队列面板显示模型切换项:

 ● Running a command · $ sleep 8
 ─────────────────────────────────────────────────────────────
   ❯ Compact, then switch to model-a with thinking low
   ↑ to edit · will send after current task

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
忙时 /model、/effort、/settings 的 Model 项 弹出选择器,选完报错 立即切换,下一轮生效,提示追加 Takes effect from the next turn. 无已知依赖 无
忙时 Kimi 官方模型的切换确认框 忙时跳过 忙时照常弹出 使用 Kimi Code 登录模型的 TUI 用户 无
忙时选 Compact and continue 不可达 入队;出队时先用旧模型压缩再切换;排队期间已压缩过则提示后只切换;引擎还在忙、压缩启动不了时提示原因,不切换,继续处理队列 无 选 Continue as-is
空闲时选 Compact and continue,压缩期间新发的消息 压缩完成后立即发出,可能早于切换、用旧模型 切换完成后才发出 使用 Kimi Code 登录模型的 TUI 用户 无
等待“先压缩再切换”的压缩结果 任何 agent 的压缩事件都会结束等待 只认主 agent 的事件 子 agent 同时在压缩的会话 无
Ctrl-S 转向 只有排队的 /compact 是屏障 排队的模型切换也是屏障 无 无
其他斜杠命令、/compact 的排队与跳过 — 不变 — —

已知、这次不处理的行为:

  • 恰好在一轮结束的瞬间确认切换,紧接着的下一轮可能仍用旧模型。
  • goal 自动续跑期间,排队的 Compact and continue 等不到压缩时机,会提示后保持原模型;可以暂停 goal 或改选 Continue as-is。
  • 先排队“压缩后切到 A”,同一轮里又直接切到 B:B 立即生效,这一轮结束后 A 生效。

受影响模块与测试:model-switch-confirm.test.ts 覆盖忙时确认框、直接切换的提示、入队和只认主 agent 的压缩事件;kimi-tui-message-flow.test.ts 覆盖出队顺序、切换完成前新输入继续排队、压缩失败或取消后继续处理队列、跳过压缩的提示、Ctrl-S 屏障、↑ 撤回和 /new 清空;queue-pane.test.ts 覆盖面板显示和 Ctrl-S 提示。

验证
  • 三个主要测试文件 467 个测试通过,相关测试共 609 个;pnpm --filter @moonshot-ai/kimi-code typecheck 通过。
  • 真实 TUI:本地假模型制造多步工具调用,忙时切换 /model 和 /effort,请求日志显示当前这一轮仍用旧模型和旧强度,下一轮用新的。
  • 真实 TUI:本地提供云端配置,触发忙时确认框并选 Compact and continue,请求顺序为旧模型的当前这一轮、旧模型的压缩、新模型的下一轮。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

Copilot AI balanced review requested due to automatic review settings November 2, 2026 16:11
@changeset-bot

changeset-bot Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0dc8a26

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-02T17:29:41.147800Z 0dc8a26 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@0dc8a26
npx https://pkg.pr.new/@moonshot-ai/kimi-code@0dc8a26

commit: 0dc8a26

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 12e1c850b2

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

A dispatch race lets Ctrl-S or Tower auto-steering send subsequent messages before a pending model or thinking-setting change finishes, so they may run with stale settings.

1 open issue
What changed in this PR

本 PR 允许 TUI 在 Agent 忙碌时切换模型和思考强度,并通过队列协调“先压缩、后切换”的执行顺序。

Changes:

  • /model、/effort 忙时可用,提示新设置从下一轮生效。
  • 新增模型切换队列项,支持压缩去重、主 Agent 事件过滤及转向屏障。
  • 补充命令、消息流和面板测试,更新中英文文档与 changeset。
File Description
docs/​zh/​reference/​slash-commands.md 说明忙时切换的生效时机。
docs/​en/​reference/​slash-commands.md 同步英文命令说明。
apps/​kimi-code/​test/​tui/​kimi-tui-message-flow.test.ts 覆盖切换队列顺序、失败处理和撤回。
apps/​kimi-code/​test/​tui/​components/​panes/​queue-pane.test.ts 验证切换项展示与转向提示。
apps/​kimi-code/​test/​tui/​commands/​model-switch-confirm.test.ts 覆盖忙时确认、状态提示和压缩事件过滤。
apps/​kimi-code/​src/​tui/​utils/​model-switch.ts 移除忙时拦截,增加下一轮生效提示。
apps/​kimi-code/​src/​tui/​types.ts 将队列项改为联合类型,新增切换项。
apps/​kimi-code/​src/​tui/​kimi-tui.ts 接入切换项入队、分派和转向屏障。
apps/​kimi-code/​src/​tui/​controllers/​staging-leases.ts 限定仅从提示消息释放媒体资源。
apps/​kimi-code/​src/​tui/​controllers/​session-event-handler.ts 记录压缩次数,并在队列占用时暂停出队。
apps/​kimi-code/​src/​tui/​components/​panes/​queue-pane.ts 展示目标模型、思考强度及排队提示。
apps/​kimi-code/​src/​tui/​commands/​dispatch.ts 扩展命令宿主的队列控制接口。
apps/​kimi-code/​src/​tui/​commands/​config.ts 实现忙时确认及持有队列的压缩后切换流程。
.changeset/​busy-model-switches.md 记录用户可见功能变化。

Review effort: Balanced

Comment thread apps/kimi-code/src/tui/kimi-tui.ts Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 25a9bde5a2

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread docs/en/reference/slash-commands.md Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 12502230d8

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@Grapedge

Grapedge commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0dc8a26693

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread apps/kimi-code/src/tui/kimi-tui.ts Resolved
Comment thread apps/kimi-code/src/tui/kimi-tui.ts Resolved
@Grapedge
Grapedge merged commit 1b13934 into main Nov 3, 2026
16 checks passed
@Grapedge
Grapedge deleted the busy-model-switch branch November 3, 2026 03:59
@github-actions github-actions Bot mentioned this pull request Nov 3, 2026
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
Port of upstream MoonshotAI/kimi-code 1b13934 (MoonshotAI#4621).

A model or effort switch no longer errors out while a turn runs. A turn that has
already sent its first request keeps the model it was launched with, and the
switch applies from the next turn; before that first request the switch lands
immediately. Choosing Compact and continue while busy queues the whole action:
the queued item is a FIFO barrier like a queued /compact, so prompts behind it
wait, the compaction runs when the queue drains, and it is skipped when another
compaction completed in the meantime. A child agent's compaction no longer
completes the wait.

Local-form adaptations, because this fork's queue diverged from upstream's:
- The queued item type is a five-variant union here (prompt / bash / skill /
  compact / model-switch); upstream has four. Steering narrows to
  QueuedPromptMessage | QueuedSkillActivation so the fork's slash-skill queueing
  keeps working, and the two dispatcher copies (KimiTUI and
  MessageDispatchController) both handle the new mode instead of sending the
  item's /model text as a prompt.
- New user-facing copy goes through t() with en/zh keys (4 keys) instead of
  upstream's literal strings, and the queue row names the model and effort.
- compactForModelSwitch detects compaction.unable by error code rather than
  isKimiError: the SDK normalizes RPC failures across the boundary, so the
  rejection is not an Error2 instance and the type guard never matched.
- The fork's runModelSwitchAction keeps its extra 'new' action; only the
  'compact' arm gained the busy branch.
- QueuePaneComponent is a Container with its own row budget here rather than
  upstream's DockSection, so the model-switch row renders inline.

Also adds the missing wire.version_too_low error copy (en/zh) that MoonshotAI#4638 left
behind; check:t-coverage failed on it.

Verified: app typecheck clean; kimi-tui-message-flow 408 tests (407 pass, 1
skipped), model-switch-confirm 21, queue-pane 12, session-event-handler
compaction/goal-queue + program-status + create-tui-state 40, editor-keyboard +
staging-leases 73; check:locale-keys, check:locale-placeholders,
check:t-coverage, check:no-hardcoded-prose and check:no-comments green; oxlint
--type-aware 0 errors on the touched files. Reverse checks: dropping
model-switch from the barrier predicate reddens the Ctrl-S and queue-pane cases,
forcing busy=false reddens the queued-compact case, and removing the next-turn
suffix reddens two notice cases.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 5, 2026
Ports the docs half of upstream MoonshotAI/kimi-code 8b6a003 (MoonshotAI#4673): the
2.5.0 section from the upstream release notes lands in both changelogs, en
and zh, word for word.

Every feature the section names is in this tree - busy /model and /effort
(MoonshotAI#4621), merged project-level skills (MoonshotAI#4520), remote sessions in SSH, Docker
and custom-command environments (MoonshotAI#4062), [workspace] require_trust (MoonshotAI#4517),
web search off by default (MoonshotAI#4516), and the subagent model pool (MoonshotAI#4418) - so
the notes describe what the fork actually does.

Deliberately not touched: apps/kimi-code/CHANGELOG.md still tops out at 2.4.1
and package.json still says 2.4.1. That file is generated by `changeset
version`, this tree has 377 pending changesets, and AGENTS.md forbids running
version or publish from this repository, so the entry is written on the docs
side only. Whoever next cuts a release here should reconcile this section
with what changeset version generates rather than re-deriving it by hand.
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