Skip to content

test(e2e): keep the TUI busy until queued messages reach the transcript - #5431

Merged
RealKai42 merged 1 commit into
mainfrom
test/e2e-tui-queued-message-gap
Nov 25, 2026
Merged

RealKai42 merged 1 commit into
mainfrom
test/e2e-tui-queued-message-gap

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

Requirement or Bug

新 E2E 的 TUI 驱动在回合结束、还有排队消息时,偶尔会提前判定产品已空闲:waitIdle() 在排队消息还没发出去时就返回了。用到排队的场景和自测因此偶发变红。main 上已出现过一次:#5206 合入后那次运行里,TUI 的 queue positive 自测红了,模型只收到 1 个请求。这个 PR 之后,TUI 上 waitIdle() 会等到每条排队消息都真正发出、跑完才返回。

Bug Reproduction Steps

在 main 上加压跑四条用到排队的用例:

pnpm --filter @moonshot-ai/e2e exec playwright test --project=tui --repeat-each=30 --workers=10 --grep "holds the message while the turn runs|returns only after the messages queued|runs messages queued during a turn|does not take the reply to an earlier queued message"

本机 macOS 上 120 次里有 5 次失败:

  • queue positive 2 次:waitIdle 返回时,model.requests('main') 只有 1 个请求。
  • expectReply negative: does not take the reply to an earlier queued message 3 次:waitIdle 停在两条排队消息之间,把第一条排队消息的回复当成了最后一条回复。

Root Cause

产品没有 bug,问题出在驱动把一帧过渡画面当成了空闲。

回合结束且队列里还有消息时,TUI 会做三件事(apps/kimi-code/src/tui/controllers/streaming-ui.ts 的 finalizeTurn 和 dispatchQueuedAfterContinuation):

  1. 取出队首消息,把界面切回空闲;
  2. 用 setTimeout(0) 推迟发送;
  3. 发送时再同步追加用户消息、显示活动指示。

如果第 1 步之前已经有一个排好的渲染定时器,它会在第 2 步之前触发,画出这样一帧:没有活动指示,排队区是空的,状态栏是上一回合的 Worked for …,排队的那条消息也还没出现在转录里。下一次渲染要等 pi-tui 的 16ms 节流,所以这帧至少停留约 15ms,机器负载高时更久。

sequenceDiagram
    participant CLI as TUI (product)
    participant Screen as Terminal screen
    participant Driver as TUI driver
    CLI->>CLI: finalizeTurn: shift queued message, phase = idle
    CLI->>Screen: pending render draws a frame with no activity indicator and an empty queue
    Driver->>Screen: read twice, 20ms apart: identical idle frame
    Driver-->>Driver: waitIdle() returns
    CLI->>CLI: setTimeout(0): send the queued message
    CLI->>Screen: user message + activity indicator
Loading

驱动的 turnOver() 认为回合结束的条件是:编辑器可见、没有活动指示、排队区为空、有完成标记,并且 settle() 连续两次(间隔 20ms)读到同样的画面。上面那帧停留超过一个轮询间隔,就会被当成空闲。

这一帧和真正空闲的画面在屏幕上没有区别,所以只能从「用户自己排过哪些消息」这条线索区分。这个 PR 是根本修复,没有加等待或重试。

Code Changes

只改 TUI 驱动(tests/e2e/surfaces/tui.ts)和 E2E 指南,不碰产品代码。

 queue(text)
+  sentBefore = count of user messages with this text in the transcript
   paste text, press Ctrl+Q, wait until the queue shows it
+  remember { text, sentBefore }        # also when the queue cuts the message short: it was queued
 
 turnOver(screen)
   if not editor, or activity indicator, or queue not empty: busy
+  if any remembered message has not appeared as a new user message: busy
   ...
 
 send() / compact() / newSession()
   require idle
+  forget the remembered messages      # all of them were sent once the product is idle
 
 describe()                            # failure descriptions
+  list remembered messages that never reached the transcript

驱动依据的是用户自己能看到的证据:自己排进去的消息离开了排队区,但还没出现在对话里,说明还在发送中。产品以后如果悄悄丢了排队消息,waitIdle() 会超时,报错里会写明是哪条消息没出现,不会把这种情况当成空闲。

tests/e2e/AGENTS.md 里 waitIdle() 那一条补充了 TUI 上的这个空档,以及对应的破坏性检查。

Behavior Changes and Affected Users

产品行为不变。下表是驱动行为的变化,受影响的只有写 TUI 场景的人和 CI。

行为 改动前 改动后 谁依赖旧行为 退路
TUI 上 waitIdle() 和所有以「产品空闲」为前提的判断(send、compact、newSession、expectAbsent、场景收尾),出队后、新回合开始前的那一帧 可能被当成空闲 算作忙,等到排队消息出现在转录里 没有:旧行为就是这个 bug 不需要
同一空档里调用 queue、steer、stop、expectTool({ status: 'running' }) 可能报「产品空闲」 当成回合还在进行 没有场景依赖这一点 不需要
队列把消息截短时 queue() 照旧报错 驱动不知道这条消息已入队 驱动记下它,waitIdle() 等它发出 「queue cuts it short」反例:它在报错后照样 waitIdle(),预期这条消息会被发出 不需要
排队消息一直没出现在转录里 可能被当成空闲 waitIdle() 超时,报错列出这些消息 没有 不需要

Desktop、Web、ACP 的驱动没有改动。Desktop 和 Web 出队、标记正在发送、加乐观的用户消息是在同一个同步块里完成的(packages/app-client/src/client/usePromptPipeline.ts 的 flushQueueHead),按代码看没有这个空档。ACP 上没有排队动作。

验证覆盖:所有用到 queue() 的 TUI 自测和场景(queue、steer、waitIdle、expectReply 的相关用例,以及 turn/queued-messages-run-in-order),分别在 tui 和 tui-kitty 两个跑法下验证。没有新增用例:这些现有用例在修复前就会失败,平时是偶发,把空档放大后每次都会失败(见下面的破坏性检查),它们就是这次修复的回归测试。未验证:Windows(驱动在 Windows 上走同一条代码路径,可以手动触发 Windows 作业来看)。

本机验证记录(macOS)
检查 结果
破坏性检查:临时把产品出队后发送的 setTimeout(0) 改成 300ms(两处),跑 7 条用到排队的用例,每条 2 遍 旧驱动 14/14 红,都是 waitIdle 提前返回导致的断言失败;新驱动 14/14 绿。产品改动已还原,不在本 PR 里
加压:未改动的产品,上面四条用例各 30 遍,10 个 worker 修复前 5/120 失败;修复后 120/120 通过(本机负载 40 左右,比修复前那次更高)
tui + tui-kitty 全套 374 通过,0 失败,16 不适用
pnpm exec tsc -p tests/e2e/tsconfig.json --noEmit 通过
pnpm lint 0 退出
node --test 'scripts/checks/*.test.mjs' 98/98
pnpm run check:duplication <base> 无新增重复

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.

When a turn ends with messages queued, the TUI takes the next message off
the queue, switches to idle and sends it on the next timer tick. A render
can land in between and draw a frame with no activity indicator and an
empty queue. The TUI driver read that frame as the end of the turn, so
waitIdle() sometimes returned before the queued message was sent.

The driver now remembers the messages queue() puts in the queue and
counts the product as busy until each one shows up as a user message in
the transcript. Failure descriptions list the queued messages that never
showed up.
@changeset-bot

changeset-bot Bot commented Nov 25, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 5586a52

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@chatgpt-codex-connector

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

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T13:33:55.580586Z 5586a52 PR opened
🔒 Security Review ❌ Failed 2026-11-25T13:32:23.556492Z
We couldn’t complete this review. Comment @codex security review to try again.
5586a52 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 25, 2026

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

commit: 5586a52

@RealKai42
RealKai42 merged commit 909637e into main Nov 25, 2026
23 checks passed
@RealKai42
RealKai42 deleted the test/e2e-tui-queued-message-gap branch November 25, 2026 13:42
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 25, 2026
Port the testkit half of MoonshotAI#5380: UNIT_TEST_SWITCHES names the defaults that
exist only to speed unit tests, TEST_ENV_DEFAULTS spreads them, and the
fixtures test pins that setting each name to undefined restores product
defaults while telemetry isolation stays on.

Ledger: MoonshotAI#5373, MoonshotAI#5389, MoonshotAI#5431 and MoonshotAI#5423 are not-applicable because the
tests/e2e Playwright suite and its workflow do not exist here. MoonshotAI#5380 is
partial (testkit half landed), and MoonshotAI#5385 is partial: its ACP fake-model
migration remains for the testkit wave while the legacy-e2e freeze does
not apply.
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