Repository navigation
fix(tui): resume sessions with models removed from config - #5378
Conversation
Tolerate a removed main-agent model alias in the v2 SDK status and status-event bridges, surface a resume warning in the TUI, and block prompt submission until an available model is selected. Avoid closing the incoming session when setSession receives the already-active instance.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
🦋 Changeset detectedLatest commit: 4d43cbc The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a77a08516f
ℹ️ 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".
…ervice Place missingConfiguredModelAlias in agent/profile/missingModel.ts and import IAgentLoopService from its contract module so loopService.ts stays unchanged.
…esume warning Block /btw, /init, and input routed to an open Btw panel when the session model alias is no longer configured, and clear the cached resume warning once Session.setModel succeeds so startup -m overrides do not print a stale warning.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4d43cbc276
ℹ️ 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".
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
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". |
* fix(tui): resume sessions with models removed from config Tolerate a removed main-agent model alias in the v2 SDK status and status-event bridges, surface a resume warning in the TUI, and block prompt submission until an available model is selected. Avoid closing the incoming session when setSession receives the already-active instance. * refactor(agent-core-v2): move missing-model alias lookup out of loopService Place missingConfiguredModelAlias in agent/profile/missingModel.ts and import IAgentLoopService from its contract module so loopService.ts stays unchanged. * fix(tui): gate /btw and /init on unavailable models and clear stale resume warning Block /btw, /init, and input routed to an open Btw panel when the session model alias is no longer configured, and clear the cached resume warning once Session.setModel succeeds so startup -m overrides do not print a stale warning. * refactor(tui): report unavailable session model errors in one helper
* fix(tui): resume sessions with models removed from config Tolerate a removed main-agent model alias in the v2 SDK status and status-event bridges, surface a resume warning in the TUI, and block prompt submission until an available model is selected. Avoid closing the incoming session when setSession receives the already-active instance. * refactor(agent-core-v2): move missing-model alias lookup out of loopService Place missingConfiguredModelAlias in agent/profile/missingModel.ts and import IAgentLoopService from its contract module so loopService.ts stays unchanged. * fix(tui): gate /btw and /init on unavailable models and clear stale resume warning Block /btw, /init, and input routed to an open Btw panel when the session model alias is no longer configured, and clear the cached resume warning once Session.setModel succeeds so startup -m overrides do not print a stale warning. * refactor(tui): report unavailable session model errors in one helper
Screenshots / Recordings
以下快照来自真实运行的源码 CLI(
apps/kimi-code/src/main.ts),在 100x30 虚拟终端中用 testkit 假模型驱动。先用old-model创建会话并完成一轮对话,再从config.toml中删除old-model,随后运行kimi -r。修复前(
82ace45bd):在kimi -r中选中会话失败,再次按 Enter 会把刚恢复的会话关掉。修复后(本 PR):会话正常打开并展示历史与切模型警告;未换模型时发送被拦截,通过
/model选择可用模型后正常完成对话。完整虚拟终端画面(`kimi -c` 启动恢复、`kimi -r` 选择器恢复、发送拦截、换模型后恢复)
修复前 —
kimi -r会话选择器修复后 —
kimi -c启动恢复修复后 —
kimi -r选择器恢复、发送拦截,以及/model换模型后恢复Requirement or Bug
当会话保存的模型别名已从
config.toml中删除后,TUI 用户无法恢复该会话(kimi -r、kimi -r <id>、kimi -c或/sessions均受影响)。选中会话时报Model "<alias>" is not configured in config.toml.(且被误标为Failed to apply startup flags),再次选中同一条会话则报[session.closed] Session is closed。本 PR 让 TUI 与 #5212 中 Desktop/Web 的恢复行为对齐:进入会话时保留历史并给出切模型警告;在用户通过
/model选择可用模型之前拦截发送;切换模型后可正常继续对话,resume 本身不改写会话保存的别名。问题来自内部反馈,未重复创建 GitHub issue。Bug Reproduction Steps
config.toml中配置两个模型(testkit和old-model),运行kimi -m old-model,发送first question,等回复完成后/exit。config.toml中删除[models."old-model"]。kimi -r,在该会话上连续按两次 Enter;再尝试运行kimi -c。Error: Failed to apply startup flags: Model "old-model" is not configured in config.toml.,第二次 Enter 报Error: Failed to apply startup flags: [session.closed] Session is closed,kimi -c也会在启动阶段失败。kimi -r和kimi -c都能打开会话并显示历史与警告;此时直接发送消息会在提交前被拦截;执行/model选择testkit后再发送,可正常完成回合。Root Cause
本 PR 是根本修复,不是绕过。
TUI 通过
SDKRpcClientV2(@moonshot-ai/kimi-code-sdk)直接驱动进程内的 v2 引擎,不经过 kap-server。#5212 修复了 kap-server 的兼容状态桥接(readLegacyStatus),但packages/node-sdk/src/v2/session-wiring.ts中镜像的 SDK 状态桥接和SDKRpcClientV2.getStatus()仍在无保护地调用resolveModelContext():harness.resumeSession()本身是成功的,因为resumedAgentState()已经容忍了模型别名缺失。KimiTUI.resumeSession()把switchToSession()放在了try/catch之外。switchToSession()->syncRuntimeState()->getStatus()抛出config.invalid,一路抛到会话选择器的统一 catch,被误标为Failed to apply startup flags。任何触发agent.status.updated的操作(setPermission、setPlanMode、回合生命周期)也会在SessionEventWiring中以同样方式抛错。setSession()已经执行了this.session = session,而syncRuntimeState()在更新appState.sessionId之前就抛错了。第二次按 Enter 时,harness.resumeSession()从缓存返回同一个Session实例,setSession()把previous === session关闭——把正要切入的会话自己关掉了。sequenceDiagram participant User as 用户 participant Picker as TUI 会话选择器 participant TUI as KimiTUI participant SDK as SDKRpcClientV2 User->>Picker: 第 1 次按 Enter 选中会话 Picker->>TUI: resumeSession(id) TUI->>SDK: resumeSession(id) SDK-->>TUI: 返回 Session(恢复成功) TUI->>TUI: setSession(session):this.session = session TUI->>SDK: switchToSession -> getStatus()(在 try/catch 之外) SDK--xTUI: 抛出 Model "..." is not configured TUI--xPicker: 向上抛错,显示 Failed to apply startup flags User->>Picker: 第 2 次按 Enter 选中同一会话 Picker->>TUI: resumeSession(id) TUI->>TUI: setSession(同一 Session):previous === session 被 close TUI->>SDK: 对已关闭的 Session 调 getStatus() SDK--xPicker: [session.closed] Session is closedCode Changes
核心思路: 让 SDK 的 v1 兼容状态读取只容忍「主 agent 绑定别名已从
config.toml删除」这一种精确错误(上下文窗口回落到默认模型),在 resume 结果中填充 TUI 现有启动恢复和选择器恢复都会展示的warning,并在 TUI 发送前拦截不可用模型的提交,引导用户用/model换模型。用户主动切换之前,不改写会话保存的模型别名。/model切换default_model/modelwire.jsonl,与 Desktop 的发送门禁一致agent-core-v2叶子模块共享判定,kap-server 与 node-sdk 共同复用阅读顺序建议:
packages/agent-core-v2/src/agent/profile/errors.ts与packages/agent-core-v2/src/agent/profile/missingModel.ts:共享判定与别名查询。missingModel.ts从纯契约模块agent/loop/loop.ts导入IAgentLoopService,不改动loopService.ts实现,也不会引入初始化环。packages/node-sdk/src/v2/session-wiring.ts:readStatusModelContext()及其在withStatusSnapshot()中的使用。packages/node-sdk/src/sdk-rpc-client-v2.ts:getStatus()与resumedSessionSummary()的警告生成。apps/kimi-code/src/tui/utils/model-switch.ts、apps/kimi-code/src/tui/kimi-tui.ts、apps/kimi-code/src/tui/commands/{dispatch,btw,session}.ts:各入口的发送拦截、setSession防自我关闭、resumeSession错误边界;packages/node-sdk/src/session.ts:setModel()成功后清除过时的 resume 警告。kap-server 的改动仅为收敛重复实现、替换 import;
.changeset/tui-resume-removed-model.md只包含@moonshot-ai/kimi-code的patch条目。Behavior Changes and Affected Users
config.toml删除的会话resumeSession虽返回Session,但 TUI 在getStatus()/switchToSession中失败;启动参数-r <id>/-c同样失败ResumedSessionSummary.warning;TUI 展示历史并提示Warning: Session model "<alias>" is not configured in config.toml. Use /model to select an available model./model(或session.setModel())选择可用模型SDKRpcClientV2.getStatus()与agent.status.updated事件config.invalid(导致getStatus、setPermission、setPlanMode和事件转发失败)maxContextTokens回落到默认模型上下文窗口(无可用默认模型时为0/undefined)KimiHarness的进程内 SDK 调用方setModel()//model绑定已配置模型default_model时的withStatusSnapshot()maxContextTokens: undefinedreadLegacyStatus对齐availableModels中时,在 TUI 提交普通消息、inline skill、插件命令、/btw、/init或在已打开的 Btw 面板中发送wire.jsonl再失败;/btw会 fork 出继承失效别名的 side agent 再失败Error: Model "<alias>" is not configured in config.toml. Use /model to select an available model.;/model、/reload和 shell 命令仍可用/reload)的 TUI 用户/model切换模型;或在config.toml中补回该模型后运行/reload-m指定可用模型(或 resume 后调用Session.setModel())getResumeState().warning仍保留旧警告,finishStartup仍提示运行/modelSession.setModel()成功后清除resumeState.warning,不再打印误导性警告;setModel失败时保留警告kimi -r <id> -m <model>/kimi -c -m <model>启动的 TUI 用户与 SDK 调用方Session实例调用KimiTUI.setSession()Session,导致this.session变成已关闭状态KimiTUI.resumeSession()中switchToSession()抛出的错误Failed to apply startup flags: ...resumeSession()内捕获,显示Failed to resume session <id>: ...max_context_size、不支持的 thinking effort)以及子 agent 模型别名缺失resumedSessionSummary保留resumedAgentState在 resume 期间原有的容错行为config.toml受影响模块与测试覆盖:
packages/node-sdk/test/session-event-wiring.test.ts:默认模型上下文窗口回落、无默认模型时为0、其他配置错误与子 agent 缺失别名仍严格抛错。packages/node-sdk/test/sdk-rpc-client-v2.test.ts:真实引擎端到端恢复失效别名会话,覆盖warning、getStatus()、setPermission()、setPlanMode()、agent.status.updated载荷,以及setModel()换绑恢复。apps/kimi-code/test/tui/kimi-tui-message-flow.test.ts:普通消息、inline skill、插件命令、/btw、/init及已打开 Btw 面板输入的发送前拦截(及模型恢复可用后正常发送)、setSession同实例不关闭、switchToSession警告展示。apps/kimi-code/test/tui/kimi-tui-startup.test.ts:启动恢复(-c/-r)时展示警告,以及-r <id> -m <可用模型>覆盖后不再展示旧警告。packages/node-sdk/test/sdk-rpc-client-v2.test.ts:真实Session.setModel()成功后清除getResumeState().warning,失败时保留。apps/kimi-code/src/main.ts+VirtualTerminal+ testkit 假模型):在 base82ace45bd上复现了选择器连续两次失败;在本分支验证kimi -c和kimi -r均正常恢复并展示历史与警告,被拦截的消息未进入模型请求或历史,/model切换后模型收到["first question", "continue after switching"]并正常渲染回复。实际运行的命令与结果
pnpm lint— 通过(0 warnings,0 errors)。pnpm exec tsc -p packages/agent-core-v2/tsconfig.json --noEmit— 通过。pnpm exec tsc -p packages/node-sdk/tsconfig.json --noEmit— 通过。pnpm exec tsc -p packages/kap-server/tsconfig.json --noEmit— 通过。pnpm exec tsc -p apps/kimi-code/tsconfig.json --noEmit— 通过。pnpm exec vitest run --reporter=agent apps/kimi-code/test/tui/kimi-tui-startup.test.ts apps/kimi-code/test/tui/kimi-tui-message-flow.test.ts— 687 条通过。pnpm exec vitest run --reporter=agent packages/node-sdk/test/session-event-wiring.test.ts packages/node-sdk/test/sdk-rpc-client-v2.test.ts packages/kap-server/test/sessionEventBroadcaster.test.ts— 192 条通过,1 条本机 macOS 既有失败:sdk-rpc-client-v2.test.ts > serves suggestFiles through the workspace handler fs service。原因是本机安装了/opt/homebrew/bin/fd,而fdSuggest.ts未传--no-require-git,导致fd在非 git 的/tmp目录不读取.gitignore(Linux CI 走rg --no-require-git路径无此问题)。与本次改动无关,未夹带修改。pnpm exec vitest run --reporter=agent packages/kap-server/test/sessions.test.ts -t 'removed|configuration errors'— 4 条通过。pnpm run check:duplication 82ace45bdda95f7378e85701692a1e2a0b339101— 通过(无新增重复)。Checklist
gen-changesets技能(仅@moonshot-ai/kimi-codepatch)。gen-docs技能评估——无需更新文档:本次修复恢复的是文档已承诺的会话恢复能力,未新增或修改 CLI 参数、斜杠命令或配置项。