Skip to content

perf(tui): cap subagent card wrapping to the tail - #4507

Merged
Grapedge merged 1 commit into
MoonshotAI:mainfrom
JochenYang:pr/tui-subagent-tail-wrap
Oct 29, 2026
Merged

Grapedge merged 1 commit into
MoonshotAI:mainfrom
JochenYang:pr/tui-subagent-tail-wrap

Conversation

@JochenYang

@JochenYang JochenYang commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Requirement or Bug

Resolve #4506

Bug Reproduction Steps

Long foreground subagent in fullscreen mode makes input latency grow over time;
backgrounding it (Ctrl+B) restores responsiveness.

Root Cause

The subagent card shows a fixed two-row window, but every streamed delta was
kept in an unbounded string and the whole accumulation was wrapped on each
frame before all but the last two rows were discarded. Frame cost therefore grew
with the run's total output. This is a fundamental fix, not a workaround: the
retained text is bounded, so wrapping a frame no longer depends on run length.

Code Changes

  • keep only the trailing 8000 characters of streamed subagent text for display
  • when at least two non-empty lines remain in that slice, drop the partial first
    line: the retained lines then wrap exactly as they would unbounded
  • otherwise keep the raw tail, so the newest line still fills the window
  • drop control sequences from the retained slice instead of replaying them
    without their opener
  • leave the result summary and the failure text uncapped: they are written once
    at a terminal state, and their first line is what the grouped card shows
  • add regression tests for the multi-line, long-line-then-short, single-line,
    trailing-blank and ANSI-tail cases

Behavior Changes and Affected Users

Multi-line output renders exactly as before, including output past the cap. When
the retained slice ends in a single over-long line (or one long line followed by
a short one), that partial line is kept so the newest text is visible; the row
it occupies can then carry a different number of characters than the unbounded
render, because the line's start is no longer retained. Control sequences inside
the retained slice are dropped; the card applies its own styling and the
streamed subagent text is plain model output. No config, wire format or
persisted data changes; the full text remains in the session events.

Behavior Before After Who relies on the old behavior Escape hatch
Subagent card frame cost grows with the run's output bounded by the retained window none n/a
Retained preview text unbounded last 8000 characters none (display only) n/a

Verification

  • pnpm --filter @moonshot-ai/kimi-code run typecheck -> exit 0
  • vitest run test/tui/components/messages/tool-call.test.ts test/tui/components/messages/agent-group.test.ts -> 154 passed
  • vitest run test/tui/components/messages test/tui/tasks-browser.test.ts -> 555 passed
  • per-delta append + render stays ~0.1 ms at 1M accumulated chars (independent review measured 0.113 ms at 442k chars)
  • multi-line windows are byte-identical to the unbounded render past the cap
  • manual: 2-hour foreground subagent run in fullscreen stayed responsive

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve). Linked Fullscreen TUI gets progressively slower while a foreground subagent runs #4506; the issue awaits a maintainer's /approve.
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete.
  • Ran gen-changesets skill, or this PR needs no changeset. (Added a patch changeset for @moonshot-ai/kimi-code.)
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5ab295c

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 Oct 29, 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-10-29T13:02:53.300791Z 5ab295c Manual request
ℹ️ 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.

@Grapedge

Copy link
Copy Markdown
Collaborator

@codex review

@pkg-pr-new

pkg-pr-new Bot commented Oct 29, 2026 •

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

commit: 5ab295c

@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: e5be8f686d

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

let end = text.length;
for (let seen = 0; seen < maxLines; seen++) {
const start = text.lastIndexOf('\n', end - 1);
if (start === -1) return text;

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 Bound long logical lines before wrapping

When a long-running foreground subagent streams a thinking or answer paragraph with fewer than eight \n characters, this branch returns the entire accumulated buffer. PrefixedWrappedLine.render() then still calls Text.render() on all of it for every new frame, so wrapping cost and input latency continue growing with total output—the exact #4506 failure this commit is meant to bound. Retain incremental wrap state or otherwise bound the final logical line as well.

Useful? React with 👍 / 👎.

Comment on lines +569 to +571
const start = text.lastIndexOf('\n', end - 1);
if (start === -1) return text;
end = start;

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 Preserve leading newlines in short tails

When content begins with \n and contains fewer than maxLines separators (for example, \nanswer), reaching end === 0 makes the next lastIndexOf(..., -1) search index 0 again, so the same newline is counted repeatedly and the returned suffix drops it. In the two-row active window, the previous blank-first/answer-second layout becomes answer-first/padding-second even though the PR declares rows unchanged; stop before another lookup at end === 0.

AGENTS.md reference: AGENTS.md:L113-L113

Useful? React with 👍 / 👎.

@JochenYang

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”.

An unknown error occurred
ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Grapedge

Copy link
Copy Markdown
Collaborator

@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: ee75b3aa67

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

@JochenYang
JochenYang force-pushed the pr/tui-subagent-tail-wrap branch from ee75b3a to 51dae62 Compare October 29, 2026 06:29
@JochenYang

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”.

An unknown error occurred
ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Grapedge

Copy link
Copy Markdown
Collaborator

@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: 51dae62ada

ℹ️ 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 thread .changeset/subagent-card-tail-wrap.md Resolved
@JochenYang
JochenYang force-pushed the pr/tui-subagent-tail-wrap branch 3 times, most recently from fd470c3 to 731453b Compare October 29, 2026 07:38
@Grapedge

Copy link
Copy Markdown
Collaborator

@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: 731453b93a

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

- keep only the trailing slice of streamed subagent text for display
- trim on append so wrapping cost no longer grows with the run
- trim at a line boundary so the remaining rows keep their wrapping
- cap the result summary and the failure text the same way

Verified: pnpm --filter @moonshot-ai/kimi-code run typecheck (exit 0);
  25 TUI test files / 551 tests passed; per-delta append + render stays
  ~0.1 ms at 1M chars; multi-line output renders byte-identically
  manual: 2h fullscreen subagent run stayed responsive
@JochenYang
JochenYang force-pushed the pr/tui-subagent-tail-wrap branch from 731453b to 5ab295c Compare October 29, 2026 11:20
@JochenYang

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”.

An unknown error occurred
ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Grapedge

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 5ab295c1ed

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

Copy link
Copy Markdown
Collaborator

Thanks for the fix!

@Grapedge
Grapedge merged commit 5fc3de8 into MoonshotAI:main Oct 29, 2026
17 checks passed
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.

Fullscreen TUI gets progressively slower while a foreground subagent runs

2 participants