Skip to content

perf(tui): batch and cache agent swarm progress rendering - #3777

Merged
RealKai42 merged 8 commits into
MoonshotAI:mainfrom
huangzheng2016:perf/tui-swarm-rendering
Sep 15, 2026
Merged

RealKai42 merged 8 commits into
MoonshotAI:mainfrom
huangzheng2016:perf/tui-swarm-rendering

Conversation

@huangzheng2016

Copy link
Copy Markdown
Contributor

Related Issue

No tracking issue — internal performance work.

Problem

On large AgentSwarm runs the TUI saturates a core (~100% CPU) and each round renders slower than the last: 130s → 513s per round for identical work. CPU profile of the main thread shows ~94% of time in rendering:

  • every subagent delta triggers requestRender(), so the whole tree re-renders at ~60fps;
  • every frame recomputes all 128 cells (truncateToWidth 33.7%, extractAnsiCode 18%, collapseWhitespace 11.2%) for every swarm component in the transcript — including long-finished ones, which have no cache;
  • the progress estimator rescans all members per cell per frame, and terminal members keep their full output text (tens of MB across rounds).

What changed

  • Whole-component render cache keyed by (width × renderVersion × palette × grid height); all mutators bump the version; terminal-state swarms render at O(1) and return the identical line array — this removes the per-round slowdown at the root.
  • Removed the per-delta requestRender() in the swarm branch; the component's 80ms frame timer (kept alive while members run) batches updates at ~12.5fps. Lifecycle events still render immediately.
  • Cell-level memo for labels/bars keyed by member state; progress-estimator prior cached by samples version; findMemberByAgentId is a Map lookup; dock grid-height measurement is cached per frame.
  • Terminal members keep at most a 400-column summary (completedText/failureText) and release latestModelText/cell cache — previously each member's full output stayed resident (≈8MB/round in the load test).
  • Tests: cache hit returns the identical array, delta batching no longer renders per event (lifecycle still does), estimator invalidation on new samples, terminal text caps and cache release.

Load test after the fix: round times flat at ~127s (was 130→513s), process ~68% idle (was ~100% CPU), render share ~14% (was ~94%).

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (N/A — internal perf work).
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill (.changeset/fix-swarm-rendering-slowdown.md, patch).
  • Ran gen-docs skill, or this PR needs no doc update (no user-facing doc change).

@pkg-pr-new

pkg-pr-new Bot commented Sep 14, 2026 •

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

commit: 81cc139

@huangzheng2016
huangzheng2016 force-pushed the perf/tui-swarm-rendering branch from ed08fd5 to c384ef0 Compare September 14, 2026 16:01
@huangzheng2016

Copy link
Copy Markdown
Contributor 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: c384ef0204

ℹ️ 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 on lines +748 to +749
const cached = member.cellCache;
if (cached !== undefined && cellCacheKeyEquals(cached.key, key)) return cached.value;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Honor the render-cache escape hatch for cell memoization

When KIMI_TUI_NO_RENDER_CACHE=1 disables caching, this path still returns member.cellCache hits and repopulates that cache unconditionally below. Consequently, the documented escape hatch in tui/utils/render-cache.ts cannot produce an uncached Agent Swarm render, which also invalidates cached-versus-uncached benchmarks and prevents users from bypassing this cache while diagnosing stale rendering. Gate both the cell-cache lookup and write on isRenderCacheEnabled().

Useful? React with 👍 / 👎.

@changeset-bot

changeset-bot Bot commented Sep 14, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 81cc139

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

@huangzheng2016

Copy link
Copy Markdown
Contributor 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: c0234ac8b1

ℹ️ 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".

return (
this.progressEstimator.hasPendingCatchup() ||
this.members.some((member) =>
member.phase === 'running' ||

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Stop frame ticks after the tool call has ended

When a successful AgentSwarm result cannot be parsed, handleAgentSwarmToolResult() calls markToolCallEnded() but ignores the false return from applyResult(), leaving previously running members in that phase. Because this new condition treats every running member as permanently animated, the interval continues requesting whole-tree renders at 12.5 fps until the component is explicitly cleared, defeating the performance fix; restrict this condition to an active tool call or transition unparsed results out of running.

Useful? React with 👍 / 👎.

}

function capFinalOutputLabel(text: string): string {
return truncateToWidth(text, MAX_FINAL_OUTPUT_LABEL_CHARS, '');

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Enforce the retained label's character bound

When a completion or failure contains ANSI escape sequences or long zero-width graphemes, truncateToWidth() limits visible terminal columns rather than the stored string length, so a value with at most 400 display columns can still retain an arbitrarily large number of code units. This undermines the terminal-state memory bound introduced by this change; apply a storage-length cap in addition to the display-width truncation.

Useful? React with 👍 / 👎.

@huangzheng2016

Copy link
Copy Markdown
Contributor 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: dc3a7fe757

ℹ️ 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 on lines +26 to +27
const MAX_FINAL_OUTPUT_LABEL_CHARS = 400;
const MAX_FINAL_OUTPUT_LABEL_CODE_UNITS = 2_000;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Move the new limits into the TUI constants directory

These newly added limits are declared directly in a presentation component, contrary to the package rule that constants live in the corresponding constant directory. Move them under src/tui/constant/ and import them here so these rendering limits remain discoverable and centrally maintained.

AGENTS.md reference: apps/kimi-code/AGENTS.md:L51-L51

Useful? React with 👍 / 👎.

Comment on lines +1004 to +1006
const code = text.codePointAt(end - 1);
if (code >= 0xd800 && code <= 0xdbff) end -= 1;
return text.slice(0, end);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid splitting surrogate pairs at the storage limit

When ANSI or zero-width content makes this cap land between an emoji's two UTF-16 code units, codePointAt(end - 1) returns the combined scalar value rather than the high-surrogate value, so this condition does not decrement end. The resulting slice() retains a lone high surrogate and can render a replacement character in the final status label; inspect the individual code unit with charCodeAt(end - 1) instead.

Useful? React with 👍 / 👎.

@huangzheng2016

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

Unknown error
ℹ️ 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".

@huangzheng2016

Copy link
Copy Markdown
Contributor 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: e62c436ceb

ℹ️ 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".

// off so the retained label never ends in an unpaired surrogate.
const codePoint = text.codePointAt(end - 1);
if (codePoint !== undefined && codePoint > 0xffff) end -= 1;
return text.slice(0, end);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Close OSC hyperlinks after slicing retained labels

When a valid OSC 8 hyperlink opens before the 2,000-code-unit boundary and closes after it—for example, when its linked text contains enough zero-width combining characters to remain within the 400-column limit—capCodeUnits() retains the opener but removes the closer with this slice. The subsequent SGR styling does not close OSC hyperlinks, so later padding and agent cells on the same grid row inherit the link until the renderer's end-of-line reset; preserve an active OSC close sequence or strip OSC hyperlinks when applying the storage cap.

Useful? React with 👍 / 👎.

@huangzheng2016

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

Unknown error
ℹ️ 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".

@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: 6c9229a5c7

ℹ️ 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".

// off so the retained label never ends in an unpaired surrogate.
const codePoint = text.codePointAt(end - 1);
if (codePoint !== undefined && codePoint > 0xffff) end -= 1;
return text.slice(0, end) + osc8CloseSuffix;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Reset SGR styles after slicing retained labels

When a final label contains an SGR opener such as bold before the 2,000-code-unit boundary and its reset after that boundary, this slice drops the reset while synthesizing a close only for OSC 8 hyperlinks. If the content remains within 400 display columns because it contains ANSI sequences or zero-width marks, downstream width truncation need not append its own reset, so the active SGR style leaks into padding and subsequent swarm cells. Close active SGR state or append a full reset whenever this storage cap slices the label.

Useful? React with 👍 / 👎.

@huangzheng2016

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 4b12ebf562

ℹ️ 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".

@huangzheng2016

Copy link
Copy Markdown
Contributor 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: e25df3b3de

ℹ️ 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 on lines +1024 to +1025
function membersOf(component: AgentSwarmProgressComponent): SwarmMemberInternals[] {
return (component as unknown as { members: SwarmMemberInternals[] }).members;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Test retention through a public seam

This helper bypasses TypeScript privacy to inspect members, and the entire terminal-memory suite consequently depends on the component's internal array and cache representation. Renaming these fields or replacing the storage strategy would break the tests even when observable rendering and retention behavior remain correct, contrary to the required TDD guidance that tests target public seams rather than internals; verify the bound through a supported diagnostic/behavioral seam instead.

AGENTS.md reference: AGENTS.md:L61-L61

Useful? React with 👍 / 👎.

@huangzheng2016

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 81cc139480

ℹ️ 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".

@RealKai42
RealKai42 merged commit dc76b0c into MoonshotAI:main Sep 15, 2026
15 checks passed
@github-actions github-actions Bot mentioned this pull request Sep 15, 2026
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.

2 participants