Skip to content

fix(tui): stop /init from leaving the terminal UI busy - #5628

Merged
RealKai42 merged 1 commit into
mainfrom
fix/tui-init-settles-the-request
Nov 27, 2026
Merged

RealKai42 merged 1 commit into
mainfrom
fix/tui-init-settles-the-request

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

Requirement or Bug

在 TUI 里运行 /init,生成 AGENTS.md 的子 agent 跑完之后,输入框上方的转圈一直不停,Worked for … 不出现;用户接着发的消息只会进队列,一直发不出去。默认的 v2 引擎下每次都这样。这个 PR 让 /init 跑完后 TUI 回到空闲,并按顺序发出排队的消息。

新 E2E 的 init 自测正例(tests/e2e/self-test/init.spec.ts)和缓存场景 tests/e2e/scenarios/cache/init-keeps-the-prefix.spec.ts 随 #5578 合入,它们在 main 上的 tui、tui-kitty 和 Windows tui 上一直红在「等回合结束」,就是这个问题。

Bug Reproduction Steps

  1. 用默认配置启动 kimi,发一条消息,等回复结束。
  2. 运行 /init,批准子 agent 写 AGENTS.md。
  3. 子 agent 卡片显示 Completed 之后,转圈仍在,Worked for … 不出现。
  4. 再发一条消息:它出现在输入框上方的排队区,一直不发出。

Root Cause

v2 引擎的 SDK 客户端报告 loopLifecycleEvents: true。这时 StreamingUIController.finalizeTurn 不自己收尾:它把阶段设回 waiting,等主 agent 的 turn.ended 带着 continuation 到达,再由 finalizeContinuation 回到空闲、取出排队的消息。

/init 不开主回合:引擎起一个 coder 子 agent 写 AGENTS.md,跑完只给主 agent 加一条提醒。所以主 agent 的 turn.ended 永远不来,TUI 就一直停在 waiting。

sequenceDiagram
    participant User
    participant TUI
    participant Engine
    User->>TUI: /init
    TUI->>TUI: beginSessionRequest (busy)
    TUI->>Engine: session.init()
    Engine->>Engine: coder subagent writes AGENTS.md, then a reminder for the main agent
    Engine-->>TUI: init() resolves, no main turn ran
    TUI->>TUI: finalizeTurn: lifecycle events on, so phase = waiting until turn.ended
    Note over TUI,Engine: turn.ended never comes, so the spinner never stops and the queue never drains
Loading

现有单测「dispatches /init to the active session and clears busy state after completion」没设 loopLifecycleEvents,走的是旧引擎的路径,所以一直是绿的。其他调用 beginSessionRequest() 的地方都会开一个真正的主回合,提示词被 hook 拦下时也是在已开始的回合里结束、照样有 turn.ended,只有 /init 是这样。这是根本修复:/init 按「没有主回合」的请求收尾,不再等一个不会来的事件。

Code Changes

StreamingUIController 多一个收尾入口 finalizeTurnlessRequest,给不开主回合的请求用;/init 成功后改用它。finalizeTurn 照旧按 loopLifecycleEvents 决定要不要等 turn.ended,两者共用同一段收尾逻辑。

 finalizeTurn(sendQueued)
-  ...shared body, awaits turn.ended when loopLifecycleEvents is on
+  finalizeRequest(sendQueued, awaitLoopEnd = loopLifecycleEvents)
+
+finalizeTurnlessRequest(sendQueued)
+  finalizeRequest(sendQueued, awaitLoopEnd = false)

 handleInitCommand
   await session.init()
-  finalizeTurn(...)
+  finalizeTurnlessRequest(...)

awaitLoopEnd 为 false 时走的就是旧引擎原来的收尾:有排队的消息就发出下一条,否则回到空闲、重置活动区,并发出终端通知。

阅读指南:先看 apps/kimi-code/src/tui/controllers/streaming-ui.ts 的 finalizeRequest(原 finalizeTurn 的函数体,只把条件换成参数),再看 apps/kimi-code/src/tui/commands/session.ts 的一行改动;测试在 apps/kimi-code/test/tui/kimi-tui-message-flow.test.ts。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
/init 成功之后 TUI 的忙碌状态(v2 引擎) 一直停在 waiting,转圈不停,没有 Worked for … 回到空闲,显示 Worked for … 无,是 bug 不需要
/init 运行期间或之后发的消息(v2 引擎) 进排队区后一直不发出 /init 跑完后按顺序发出 无 不需要
/init 跑完时的终端通知(开了通知时,v2 引擎) 不发 发「Kimi Code task complete」,和旧引擎一样 无 通知设置照旧可以关
旧引擎(loopLifecycleEvents 关)下的 /init 跑完回到空闲 不变:两条入口在这时走同一段逻辑 — —
其他调用 finalizeTurn 的路径(turn.ended、提示词被 hook 拦下) 开着生命周期事件时等 turn.ended 的 continuation 不变:finalizeTurn 仍按 loopLifecycleEvents 判断 — —
/init 失败或被取消 直接回到空闲 不变(这两条路径不经过 finalizeTurn) — —

受影响的只有 TUI 用户(/init 只在 TUI 里有);print 模式、SDK、Desktop、Web、ACP、VS Code 不经过这段代码。

测试覆盖:

  • 新增两条 v2 引擎下的单测:「clears busy state after /init completes, though no main turn reports how it ended」和「sends a message typed while /init runs once /init completes」,修复前都失败(前者停在 waiting,后者 session.prompt 一次都没被调用)。
  • 旧引擎下的原有单测不变、仍然通过。
  • 从真实入口验证的是新 E2E 的 init 自测正例和 E6 场景:修复前在 TUI 上超时,修复后通过。
验证

基于 main 5f7f90a98(#5578 已合入)。

  • 单测:pnpm exec vitest run --reporter=agent apps/kimi-code/test/tui/kimi-tui-message-flow.test.ts 567 条通过;两条新用例在修复前都失败。apps/kimi-code/test/tui/controllers 和 apps/kimi-code/test/tui/commands 共 46 个文件、828 条通过。
  • E2E(构建产物,TESTKIT_CLI_BUILD=dist,macOS):
    • --project=tui 整套:268 条通过,20 条按设计不在 TUI 上跑,1 条失败,是 scenarios/cache/mcp-tool-change-keeps-the-tool-list。它在 main 上本来就红(feat(agent-core-v2): defer MCP tools by default and cap startup wait at 2s #5433 之后),和这个改动无关。
    • self-test/init.spec.ts 和 scenarios/cache/init-keeps-the-prefix.spec.ts 在 tui、tui-kitty 上 6 条通过;修复前它们在 main 的 CI 上超时。
    • Windows 没在本地跑,交给 CI 的 Windows tui 作业。
  • pnpm lint 通过;pnpm exec tsc -p apps/kimi-code/tsconfig.json --noEmit 通过;node --test 'scripts/checks/*.test.mjs' 124 条通过;pnpm run check:duplication 5f7f90a98 没有新增重复。

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.

Under the v2 engine the SDK reports loop lifecycle events, so
finalizeTurn sets the phase back to waiting and leaves the rest to the
main agent's turn.ended. /init runs no main turn: a coder subagent
writes AGENTS.md and the main agent only gets a reminder. turn.ended
never arrives, the spinner never stops and messages typed meanwhile
stay queued.

/init now settles through finalizeTurnlessRequest, which shares the
finalize logic but does not wait for a loop end. finalizeTurn keeps
deciding by loopLifecycleEvents as before.

Two unit tests under the v2 engine cover the busy state and a message
typed while /init runs; both fail without the fix.
@RealKai42 RealKai42 added the e2e Run the cross-surface E2E suite on this pull request label Nov 27, 2026
@changeset-bot

changeset-bot Bot commented Nov 27, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4fc847e

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 Patch

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 27, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-27T06:23:38.393530Z 4fc847e PR opened
ℹ️ 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 27, 2026

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

commit: 4fc847e

@RealKai42
RealKai42 merged commit 74d8315 into main Nov 27, 2026
24 of 41 checks passed
@RealKai42
RealKai42 deleted the fix/tui-init-settles-the-request branch November 27, 2026 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

e2e Run the cross-surface E2E suite on this pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants