Skip to content

The agent that grows with you

fix(desktop): keep code geometry stable while highlighting loads

brooklyn!673d33095b95
diff --git a/apps/desktop/src/components/chat/shiki-block.test.tsx b/apps/desktop/src/components/chat/shiki-block.test.tsx
index 26273a73e7f..6bbe21e5d80 100644
--- a/apps/desktop/src/components/chat/shiki-block.test.tsx
+++ b/apps/desktop/src/components/chat/shiki-block.test.tsx
@@ -50,6 +50,14 @@ afterEach(() => {
})
describe('CachedShikiBlock (warm-switch perf guard)', () => {
+ it('keeps escaped code visible while a cache miss waits for highlighting', () => {
+ const code = '<script>not executable</script>\nconst two = 2\n'
+ const { container } = render(<CachedShikiBlock code={code} language="typescript" />)
+ expect(container.querySelector('pre code')?.textContent).toBe(code)
+ expect(container.querySelector('script')).toBeNull()
+ expect(codeToHtml).not.toHaveBeenCalled()
+ })
+
it('highlights on first mount and reuses the cached HTML on remount', async () => {
const { unmount } = render(<CachedShikiBlock {...TS_BLOCK} />)
await waitForHighlighted()
diff --git a/apps/desktop/src/components/chat/shiki-block.tsx b/apps/desktop/src/components/chat/shiki-block.tsx
index 3704185caf5..9eb866d87d5 100644
--- a/apps/desktop/src/components/chat/shiki-block.tsx
+++ b/apps/desktop/src/components/chat/shiki-block.tsx
@@ -26,6 +26,8 @@ import { createOnigurumaEngine } from 'shiki/engine/oniguruma'
import { SHIKI_HIGHLIGHT_SCOPE, SHIKI_THEME } from '@/components/chat/shiki-config'
import { highlightCache, highlightCacheKey } from '@/components/chat/shiki-highlight-cache'
+import { PlainShiki } from './shiki-plain'
+
/** Same debounce react-shiki's `delay` used to throttle highlight work with. */
const HIGHLIGHT_DELAY_MS = 120
@@ -164,9 +166,10 @@ export default function CachedShikiBlock({ language, code, theme, colorReplaceme
}, [cacheKey, code, language, replacements, themeConfig])
if (html === null) {
- // Nothing to paint yet (miss, debounce pending). Matches react-shiki's
- // own empty render while the highlight is in flight.
- return null
+ // Keep the same pre/code geometry during the debounce. An empty render
+ // collapses every cold code card, then shifts the transcript when color
+ // arrives. React escapes the payload; only highlighting uses cached HTML.
+ return <PlainShiki code={code} />
}
return <div className="rs-root not-prose" dangerouslySetInnerHTML={{ __html: html }} data-testid="shiki-container" />
diff --git a/apps/desktop/src/components/chat/shiki-highlighter.tsx b/apps/desktop/src/components/chat/shiki-highlighter.tsx
index 2fa6e606eab..c9782c22d3e 100644
--- a/apps/desktop/src/components/chat/shiki-highlighter.tsx
+++ b/apps/desktop/src/components/chat/shiki-highlighter.tsx
@@ -13,6 +13,7 @@ import { useI18n } from '@/i18n'
import { isLikelyProseCodeBlock } from '@/lib/markdown-code'
import type { CachedShikiBlockProps } from './shiki-block'
+import { PlainShiki } from './shiki-plain'
export { SHIKI_COLOR_REPLACEMENTS, SHIKI_THEME } from '@/components/chat/shiki-config'
/**
@@ -56,7 +57,7 @@ const ShikiBlock = lazy(() => import('./shiki-block'))
* until the shiki chunk arrives. Highlighted output is cached by
* (theme, language, code), so revisits never re-tokenize (#95595). */
export const LazyShiki: FC<CachedShikiBlockProps> = ({ language, code, theme, colorReplacements }) => (
- <Suspense fallback={<PlainCode code={code} />}>
+ <Suspense fallback={<PlainShiki code={code} />}>
<ShikiBlock code={code} colorReplacements={colorReplacements} language={language} theme={theme} />
</Suspense>
)
diff --git a/apps/desktop/src/components/chat/shiki-plain.tsx b/apps/desktop/src/components/chat/shiki-plain.tsx
new file mode 100644
index 00000000000..3875f79d4c9
--- /dev/null
+++ b/apps/desktop/src/components/chat/shiki-plain.tsx
@@ -0,0 +1,10 @@
+/** Matches highlighted markup so loading a grammar changes color, not layout. */
+export function PlainShiki({ code }: { code: string }) {
+ return (
+ <div className="rs-root not-prose">
+ <pre className="shiki" style={{ backgroundColor: 'transparent', margin: 0 }}>
+ <code>{code}</code>
+ </pre>
+ </div>
+ )
+}