Skip to content

perf(tui): reuse unchanged tool output lines across renders - #4665

Merged
Grapedge merged 6 commits into
mainfrom
fix-transcript-render-cache
Nov 5, 2026
Merged

Grapedge merged 6 commits into
mainfrom
fix-transcript-render-cache

Conversation

@Grapedge

@Grapedge Grapedge commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

展开大量工具输出以后(按 ctrl+o,或在分组里点开很多步),界面每次刷新都会把一部分没有变化的工具输出重新算一遍。展开得越多,打字、滚动和动画就越慢。这个问题 main 上就有。fullscreen 默认的 30 步合并让展开的内容少一些,所以平时不明显;#4618 开启分组后不再合并,问题就明显了。这个 PR 让这些部分在内容和宽度不变时直接复用上一次的结果,画面不变。

本 PR 堆叠在 #4618 上,只需要看最后 6 个提交。#4618 合入后会 rebase 到 main。

Bug Reproduction Steps

  1. 在 fullscreen 模式下恢复一个工具调用很多的长会话(实测用的会话约 480 步)。
  2. 按 ctrl+o 展开最近三轮。
  3. 在输入框里打字,看每个字从按下到出现在屏幕上要多久。

在 #4618 的分支上,这一步约 18 毫秒;main 默认(有 30 步合并)约 5 毫秒。数据见下面的“性能”。

Root Cause

父组件按“子组件这次返回的行数组是不是上次那一个”判断能不能复用自己的缓存,例如工具卡片和工具输出里的 ToolOutputMarkdown。下面四处每次刷新都返回新数组,或者重新计算,让上层的缓存永远失效:

  1. 工具卡片里的空行组件。pi-tui 的 Spacer.render 每次返回一个新数组。
  2. Shell 输出组件没有渲染缓存,每次把子组件的行重新拼一遍。
  3. 折叠状态下“输出有没有被截断”的判断(collapsedOutcomeClips)每次刷新都重算一遍,TruncatedOutputComponent 也没有缓存。
  4. Markdown 容器每次把子组件的行拼成新数组。

这是根本修复:每一处在输入不变时返回同一个数组,上层原有的缓存就能命中。

Code Changes

四处用同一个办法:记下上次的宽度和每个子组件返回的行数组,这次都没变就返回上次拼好的结果。

 render(width)
-  return the children's lines joined into a new array
+  render every child (each child keeps its own cache)
+  if the width is the same and every child returned the same array as last time
+    return the joined lines from last time
+  join the lines and remember them with the width and the child arrays
位置 缓存按什么判断 什么时候失效
工具卡片的空行(StableSpacer) 宽度 宽度变化
Shell 输出组件 宽度、子组件、子组件返回的行数组 invalidate(),或任一子组件的输出变了
折叠状态的截断判断 宽度 工具卡片重建内容或收到结果(rebuildContent、rebuildBody)
TruncatedOutputComponent 宽度、源文本 源文本变化
Markdown 的子组件拼接 宽度、子组件、子组件返回的行数组 invalidate(),或任一子组件的输出变了

主题变化时,上层会调用 invalidate(),缓存跟着清掉,所以颜色变化不会被旧结果挡住。

五处缓存都遵守现有的渲染缓存开关(KIMI_TUI_NO_RENDER_CACHE=1 或 setRenderCacheEnabled(false)):开关关掉时既不读也不写缓存,和仓库里其他缓存一致,排查问题和对比有无缓存的性能时仍能整体关掉。

一个取舍。 Shell 输出组件和 Markdown 不再走 pi-tui Container.render,所以不再记录鼠标命中用的布局。点击落在它们上面时,pi-tui 会退回到逐个渲染子组件来算高度。结果正确,而且子组件都有缓存,这一步很便宜。

阅读指南。 前四个提交各改一处缓存,第五个是 changeset,第六个让这些缓存遵守渲染缓存开关。按提交顺序看即可。测试都加在对应组件现有的测试文件里。

Behavior Changes and Affected Users

None:渲染出的文字和颜色不变,只省掉重复计算。

证据:

  • 每处都有测试断言三件事:内容不变时重复渲染返回同一个数组;内容、宽度或主题变化后输出跟着变;输出和改动前逐字节相同。
  • 在真实程序里恢复一个约 30 步的会话,开启分组,逐页截取整个对话(收起状态和按 ctrl+o 展开后各一遍),和不带本 PR 的 feat(tui): fold tool calls and thinking into transcript groups in fullscreen #4618 分支对比,0 行差异。

性能。 低速模式(macOS 后台优先级 taskpolicy -c background,模拟较慢的机器),每组 2 次,数值是每次打字回显的中位数:

场景 main #4618 #4618 + 本 PR
约 480 步的会话,按 ctrl+o 展开后打字 5.1 ms 18.2 ms 5.1 ms
同一会话,收起时打字 3.4 ms 3.5 ms 3.6 ms
约 30 步的会话,工具运行时的处理器占用(各 4、4、2 次的中位数) 3.65% 4.0% 3.6%

受影响模块。 只改 apps/kimi-code/src/tui/components/ 下的四个文件:messages/tool-call.ts、messages/shell-execution.ts、messages/tool-renderers/truncated.ts、markdown/markdown.ts。对应测试在 apps/kimi-code/test/tui/components/ 下同名的测试文件里。

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.

@changeset-bot

changeset-bot Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ad67a9a

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 4, 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-05T04:13:11.521190Z ad67a9a New commits
ℹ️ 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 4, 2026 •

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

commit: ad67a9a

@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: 4a7cbd6f50

ℹ️ 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 force-pushed the fix-transcript-render-cache branch from 4a7cbd6 to 98c9a34 Compare November 4, 2026 05:38
@Grapedge
Grapedge added this pull request to stack #4619 November 4, 2026 05:42
@Grapedge
Grapedge force-pushed the fix-transcript-render-cache branch from 98c9a34 to 42e89cc Compare November 4, 2026 06:03
@Grapedge
Grapedge force-pushed the fix-transcript-render-cache branch from 42e89cc to 74d4962 Compare November 4, 2026 14:50
Copilot AI balanced review requested due to automatic review settings November 4, 2026 14:50

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.

🟢 Approval recommended

There are no active review issues for the tool-output rendering cache changes.

No open issues 🎉

What changed in this PR

本 PR 为 TUI 的工具输出渲染增加缓存,减少大量内容展开后的重复计算,缓解 #4618 引入分组后更明显的输入和滚动延迟,不改变渲染内容。

Changes:

  • 复用工具卡片空行、Shell 输出、截断输出和 Markdown 的未变化行数组,并缓存折叠输出的截断判断。
  • 缓存遵守现有渲染缓存开关,按宽度、内容或组件失效状态更新。
  • 补充缓存复用、失效和禁用场景的测试,并添加 patch changeset。
File Description
apps/​kimi-code/​test/​tui/​components/​messages/​tool-renderers/​truncated.test.ts 验证截断输出缓存及失效行为。
apps/​kimi-code/​test/​tui/​components/​messages/​tool-call.test.ts 验证卡片缓存、结果更新和截断判断。
apps/​kimi-code/​test/​tui/​components/​messages/​shell-execution.test.ts 验证 Shell 缓存与原渲染结果一致。
apps/​kimi-code/​test/​tui/​components/​markdown/​markdown.test.ts 验证 Markdown 缓存及文本、宽度和主题更新。
apps/​kimi-code/​src/​tui/​components/​messages/​tool-renderers/​truncated.ts 缓存折叠后的截断输出。
apps/​kimi-code/​src/​tui/​components/​messages/​tool-call.ts 增加稳定空行和截断判断缓存。
apps/​kimi-code/​src/​tui/​components/​messages/​shell-execution.ts 复用未变化的子组件合并结果。
apps/​kimi-code/​src/​tui/​components/​markdown/​markdown.ts 缓存 Markdown 子组件合并结果。
.changeset/​expanded-tool-output-render.md 记录展开大量工具输出时的响应性能改进。

Review effort: Balanced

liruifengv
liruifengv previously approved these changes Nov 5, 2026
Base automatically changed from verb-group to main November 5, 2026 04:10
@Grapedge
Grapedge dismissed liruifengv’s stale review November 5, 2026 04:10

The merge-base changed after approval.

@Grapedge
Grapedge force-pushed the fix-transcript-render-cache branch from 74d4962 to ad67a9a Compare November 5, 2026 04:10
@Grapedge
Grapedge merged commit 8d1af37 into main Nov 5, 2026
27 of 28 checks passed
@Grapedge
Grapedge deleted the fix-transcript-render-cache branch November 5, 2026 04:26
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 11, 2026
Port upstream MoonshotAI#4665. Cache rendered lines in Markdown, ShellExecution,
TruncatedOutput, and a StableSpacer for the tool card so unchanged
output is not re-wrapped on every frame. Gate the existing
collapsedOutcomeClips memo behind isRenderCacheEnabled to match upstream
so disabling the cache re-measures each render. Output is identical when
the cache is enabled.
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