Files
kilocode/packages/kilo-ui
Marius 00ec003c11 perf(streaming): four fixes that unfreeze long-session streaming (#9341)
* perf(vscode): stop O(N) reactive cascade on every streaming token

The webview DataBridge wrapped the whole session Data shape in a
`createMemo`, whose body walked `store.parts[msg.id]` for every message
in the session family. Any single part mutation — i.e. every token
delta — invalidated the memo, produced a fresh POJO, and invalidated
every downstream consumer (including O(N) scans inside each mounted
SessionTurn). On a 200-message session a Chrome CPU profile showed
three back-to-back 440ms main-thread blocks per SSE batch, ~46% of
the time in Solid reactive runtime alone.

Expose `data` as a plain object with reactive getters over
`session.allMessages`/`allParts`/`allStatusMap` so consumers reading
`data.store.part[Y]` subscribe to only that key. Removes the now-unused
`familyData` helper and its interface/mock entries.

* perf(ui): drop TextShimmer JS timer — CSS-only animation

A createEffect inside TextShimmer ran clearTimeout + setTimeout on every
`active` prop change to gate the sweep animation via a `data-run`
attribute. During LLM token streaming in long sessions, `active` props
(bound to `pending()` / `running()` accessors) thrashed as tools
started/finished across many shimmer instances. CPU profile of a 7s
streaming window showed ~2,500 timer operations — 16% of the blocked
main-thread time.

Remove the effect and drive the animation purely from the `data-active`
attribute. The opacity transition on the shimmer char (220ms) already
handles the fade, so visual behavior is unchanged. Adds one static
regression guard and one runtime perf assertion (with happy-dom) that
toggling the prop 1000 times results in zero timer calls.

* perf(kilo-ui): skip layout reads in GrowBox watch-mode ResizeObserver

The GrowBox component wraps each assistant part and, when watch=true
(which is set on the currently-streaming text part), runs a
ResizeObserver that called body.getBoundingClientRect() via
targetHeight() on every body-size change. During streaming this fires
at ~60Hz and each call forces a synchronous layout. CPU profile of a
7s streaming window showed 1,362 gBCR samples (~9% of blocked
main-thread time) all attributable to this path.

Reuse the browser's pre-measured contentBoxSize / contentRect from the
observer entries — no extra layout read. Also skip sub-pixel updates
(<2px) that the spring absorbs imperceptibly anyway, cutting per-token
spring work when tokens add tiny height deltas.

* perf(ui): coalesce markdown parse to one per animation frame

During LLM token streaming, the Markdown render effect ran
temp.innerHTML = content + morphdom on every content update. SSE
tokens arrive at 60–200Hz and each delta reparsed the entire
accumulated HTML. CPU profile of a 7s streaming window showed 2,940
ParseHTML events totaling ~619ms (~46% of blocked main-thread time).

Queue the latest content in a component-scoped pending variable and
run the morphdom pass inside requestAnimationFrame. K rapid updates
before the frame fires now collapse to one parse. The onCleanup
handler cancels any queued frame so it doesn't touch an unmounted
DOM. Fast-path is preserved untouched so non-streaming first paint
stays synchronous.

* chore(changeset): consolidate streaming-perf changesets into one

Per-commit changesets produced four nearly-identical release-note
entries. The user-visible change is a single perceptual improvement —
streaming is smooth in long sessions — so roll them up into one
feature-oriented entry.

* test(vscode): consolidate streaming perf tests + wire into CI

Replace three synthetic reactivity tests with a single end-to-end
streaming perf benchmark that:
- Renders the real TextShimmer component and asserts zero setTimeout/
  clearTimeout calls during a 100-toggle burst (TextShimmer fix).
- Asserts per-key Solid reactivity: 100 text deltas on one message
  must re-run only that message's consumer, not O(N) consumers
  (DataBridge cascade fix).
- Uses only count-based assertions against deterministic APIs
  (setTimeout/clearTimeout/innerHTML setter) — no wall-clock
  thresholds, so it doesn't flake under CI load.

Also wire `bun run test:webview-reactivity` into the test-vscode
workflow so the benchmark runs on every PR that touches
packages/kilo-vscode, packages/ui, or packages/kilo-ui. Without this
wiring the perf regression guard would have shipped dormant.

* test(vscode): declare @happy-dom/global-registrator as devDep

The streaming-perf benchmark imports @happy-dom/global-registrator to
get a DOM for mounting the real TextShimmer component. It resolved
locally through workspace hoisting but CI's clean install didn't have
it. Make the dependency explicit.

* test(vscode): import TextShimmer via package export so JSX resolves

Using the deep relative path (../../../ui/src/components/text-shimmer)
made Bun's test transpiler apply kilo-vscode's tsconfig — which has no
`jsxImportSource` — so the .tsx file was compiled with the default
React runtime, producing "React is not defined" in CI.

Resolving through the package export (@opencode-ai/ui/text-shimmer)
picks up packages/ui/tsconfig.json which sets
`jsxImportSource: solid-js`. Works consistently across Linux/macOS/
Windows CI without needing bunfig-level JSX overrides.

* test(vscode): address bot review — add runtime coverage for Markdown + GrowBox, drop empty smoke test

Two kilo-code-bot findings on the streaming perf bench:

1. The 'benchmark completes quickly' smoke test timed an empty block,
   so `elapsed` was always near zero and the assertion never fired.
   Drop it — the real benchmarks below already complete in ~90ms.

2. The original file installed spy counters for innerHTML writes and
   getBoundingClientRect but never asserted against them, leaving
   Markdown rAF coalescing and GrowBox layout-read regressions silently
   uncaught.

Add two runtime mirrors:
- Markdown rAF pattern: 100 async content updates coalesce to <20 parses
  (would be exactly 100 pre-fix).
- GrowBox ResizeObserver pattern: 100 synthetic resize callbacks using
  contentBoxSize/contentRect trigger zero getBoundingClientRect calls
  (would be exactly 100 pre-fix).

Source-level regression guards in tests/unit/markdown-raf-coalesce.test.ts
and tests/unit/growbox-no-layout-thrash.test.ts cover the actual
component code. The runtime tests here prove the patterns the guards
require actually deliver the perf property at runtime.

Benchmark runs in ~90ms, 5 consecutive local runs all green.

* test(vscode): drop unstable TextShimmer runtime mount from streaming perf bench

The benchmark tried to mount the real @opencode-ai/ui TextShimmer to
assert zero setTimeout/clearTimeout calls. That required Bun's test
runner to transpile text-shimmer.tsx with Solid's JSX runtime, which
depends on tsconfig resolution walking up to packages/ui/tsconfig.json.
In CI (fresh workspace, different node_modules layout) this resolution
was unstable and kept falling back to React JSX ("React is not
defined").

Keep the three runtime patterns that don't need JSX transpilation
(DataBridge cascade, Markdown rAF coalescing, GrowBox contentRect),
plus the source-level regression guard at
tests/unit/textshimmer-no-timer.test.ts which asserts text-shimmer.tsx
contains no setTimeout/clearTimeout/createEffect/data-run. Together
these cover all four fixes without CI flakiness.

5 consecutive local runs pass in ~80ms.

* test(vscode): remove streaming perf tests

Static source-parsing guards and pattern-mirror runtime tests didn't
actually exercise the fixed component code — a regression in the real
code could have left them green. Remove them along with the
test:webview-reactivity script, the workflow step, the
@happy-dom/global-registrator devDep, and the tests/webview-reactivity
directory. The four perf fixes stand on their own; adding dubious
guards was worse than adding none.

* test(vscode): restore static perf-regression guards wired to real source

Restore four guards that each parse the actual fixed component source
and fail loudly if the fix pattern is removed:

- databridge-shape.test.ts     reads webview-ui/src/App.tsx, asserts
                               `data` is not wrapped in createMemo
- textshimmer-no-timer.test.ts reads ui/src/components/text-shimmer.tsx
                               + .css, asserts no setTimeout/
                               clearTimeout/createEffect, animation
                               gated on data-active
- markdown-raf-coalesce.test.ts reads ui/src/components/markdown.tsx,
                                asserts the render createEffect uses
                                requestAnimationFrame + cancelAnimationFrame
- growbox-no-layout-thrash.test.ts reads kilo-ui/src/components/grow-box.tsx,
                                   asserts the ResizeObserver callback
                                   does not call gBCR, uses contentRect/
                                   contentBoxSize, and has the sub-pixel
                                   delta guard

Verified by mutation: each guard fails when its fix pattern is removed
from the real source file and passes again once restored. Runs as part
of the existing test:unit script (no extra CI wiring).
2026-04-23 10:40:15 +02:00
..
2026-02-18 17:02:44 -03:00