Skip to content

fix(web): keep long streams responsive#1643

Merged
wbxl2000 merged 2 commits into
mainfrom
codex/fix-kimi-web-stream-backlog
Jul 15, 2026
Merged

fix(web): keep long streams responsive#1643
wbxl2000 merged 2 commits into
mainfrom
codex/fix-kimi-web-stream-backlog

Conversation

@wbxl2000

@wbxl2000 wbxl2000 commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

No linked issue. This is a focused fix for a reproduced long-stream rendering stall on the current kap-server /api/v1 path.

Problem

When animation frames are suspended, high-frequency WebSocket render events accumulated without coalescing or a processing budget. A later lifecycle event synchronously drained the entire backlog, repeatedly cloning message state and producing a main-thread long task. The WebSocket remained healthy, but the transcript could stop visibly advancing until a refresh rebuilt it from the authoritative snapshot.

In an isolated kap-server session, 20,000 raw deltas became 1,544 Web render events; the old control-event drain processed all 1,544 at once, took 48.6 ms, and produced a 52 ms long task while still preserving the final 80,000-character text.

What changed

  • Carry raw turn, offset, and text/thinking identity alongside projected assistant deltas.
  • Coalesce only adjacent deltas with the same session, turn, message, content index, kind, and contiguous offset. Cap each group and split oversized frames so concatenation cost stays bounded.
  • Process ordered groups in bounded slices with both animation-frame scheduling and a hidden-tab task fallback. Lifecycle, approval, snapshot, and other control events remain ordering barriers and are never dropped.
  • Flush around authoritative resync snapshots and dispose both scheduled callbacks during HMR so stale modules cannot mutate old state.
  • Discard only the removed session's pending render and control events during teardown, while preserving other sessions' queued order.
  • Add public-path coverage for 10,000 deltas, hidden-tab fallback, control ordering, snapshot/resync replacement, text/thinking metadata, sequence watermarks, oversized frames, per-session teardown, and idempotent disposal.

A stronger 400,000-character isolated burst reduced 782 Web enqueue calls to 13 capped reducer groups, with 1.2 ms total enqueue time, 0.8 ms coalescing time, and a 0.4 ms maximum reducer drain. The final text matched the server SHA-256 exactly. The terminal Markdown commit for that intentionally extreme unbroken block still produced one 61 ms task; the streaming queue itself did not, so this change does not introduce a broader UI state rewrite.

Validation:

  • pnpm --filter @moonshot-ai/kimi-web test (28 files, 499 tests)
  • pnpm --filter @moonshot-ai/kimi-web typecheck
  • pnpm --filter @moonshot-ai/kimi-web check:style (baseline findings only)
  • pnpm --filter @moonshot-ai/kimi-web build
  • kap-server WebSocket/snapshot targeted tests (5 files, 55 tests)
  • git diff --check

No user guide update is needed because commands, configuration, and interaction semantics are unchanged; the changeset records the user-visible reliability fix.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • 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 Jul 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e774b6f

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

@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review

@pkg-pr-new

pkg-pr-new Bot commented Jul 13, 2026

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

commit: e774b6f

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: eeded68f70

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

@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: ab9ad560a2

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

@wbxl2000 wbxl2000 force-pushed the codex/fix-kimi-web-stream-backlog branch from ab9ad56 to e774b6f Compare July 15, 2026 05:42
@wbxl2000 wbxl2000 merged commit d8d4e8c into main Jul 15, 2026
14 checks passed
@wbxl2000 wbxl2000 deleted the codex/fix-kimi-web-stream-backlog branch July 15, 2026 05:47
@github-actions github-actions Bot mentioned this pull request Jul 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.

1 participant