🔇 fix: Stop Every Row's Read-Aloud Element From Loading the Page (#18168)

Each assistant row mounts a hidden `<audio autoplay>` for its read-aloud
button and passed it `src={audioRef.current?.src}`. An element with no
source reports `''`, so the row's second render wrote an empty `src`, and
its third wrote that back as the document's base URL. Both start a media
load: the first fails at once, the second fetches the app's HTML and fails
to decode it. A thread of 250 rows did that twice per row each time the
thread was opened and a run began.

The player assigns the source itself when speech starts, so the prop
carried nothing. Without it React never touches the attribute.

The live-run benchmarks ran with read-aloud off and so never mounted these
elements. They now leave it on, as the app does, and `speech.perf.spec.ts`
counts the media errors and requests the rows make.
This commit is contained in:
Danny Avila 2026-11-14 15:02:16 -05:00 • committed by GitHub
parent 929f05daec
commit 461a4c2c52
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 180 additions and 8 deletions

View file

@ -83,7 +83,6 @@ export function BrowserTTS({
preload="none"
controlsList="nodownload nofullscreen noremoteplayback"
className="hidden"
src={audioRef.current?.src}
onError={(error) => {
logger.error('Error fetching audio:', error);
}}
@ -175,7 +174,6 @@ export function ExternalTTS({
preload="none"
controlsList="nodownload nofullscreen noremoteplayback"
className="hidden"
src={audioRef.current?.src}
onError={(error) => {
logger.error('Error fetching audio:', error);
}}

View file

@ -0,0 +1,67 @@
import { RecoilRoot } from 'recoil';
import { render } from '@testing-library/react';
import type { ReactElement } from 'react';
import { BrowserTTS, ExternalTTS } from '../TTS';
/**
* Every assistant row mounts one of these, hidden, for its read-aloud button. Until the button
* is pressed the element must stay without a source: an `<audio autoplay>` given one starts
* loading it, so a thread of 250 rows that each point at the page made 250 failed media loads
* each time the rows re-rendered.
*/
/** The speech engines need auth and a speech synthesizer; the element and its ref are real. */
function mockSpeechHook() {
const useAudioRef = jest.requireActual('~/hooks/Audio/useAudioRef').default;
const useSpeech = () => ({
...useAudioRef({ setIsPlaying: () => undefined }),
toggleSpeech: jest.fn(),
isSpeaking: false,
isLoading: false,
});
return { __esModule: true, default: useSpeech };
}
jest.mock('~/hooks/Audio/useTTSBrowser', () => mockSpeechHook());
jest.mock('~/hooks/Audio/useTTSExternal', () => mockSpeechHook());
const wrap = (ui: ReactElement) => <RecoilRoot>{ui}</RecoilRoot>;
/** jsdom has no object URLs; the element's cleanup revokes its source on unmount. */
const revokeObjectURL = URL.revokeObjectURL;
beforeAll(() => {
URL.revokeObjectURL = jest.fn();
});
afterAll(() => {
URL.revokeObjectURL = revokeObjectURL;
});
describe.each([
['BrowserTTS', BrowserTTS],
['ExternalTTS', ExternalTTS],
])('%s audio element', (_name, TTS) => {
it('stays without a source across re-renders', () => {
const props = { index: 0, messageId: 'message-1', content: 'Read this' };
const { container, rerender } = render(wrap(<TTS {...props} isLast={false} />));
const audio = container.querySelector('audio');
expect(audio).not.toBeNull();
expect(audio?.hasAttribute('src')).toBe(false);
rerender(wrap(<TTS {...props} isLast={true} />));
rerender(wrap(<TTS {...props} isLast={false} />));
expect(container.querySelector('audio')).toBe(audio);
expect(audio?.hasAttribute('src')).toBe(false);
});
it('keeps a source the player assigned', () => {
const props = { index: 0, messageId: 'message-2', content: 'Read this' };
const { container, rerender } = render(wrap(<TTS {...props} isLast={false} />));
const audio = container.querySelector('audio');
audio?.setAttribute('src', 'blob:speech');
rerender(wrap(<TTS {...props} isLast={true} />));
expect(audio?.getAttribute('src')).toBe('blob:speech');
});
});

View file

@ -18,6 +18,12 @@ transcript, from a Chromium trace of the built client:
no event arriving, whatever the main thread does there it does for the run's
animations.
- **`speech.perf.spec.ts`**: the cost of the read-aloud control. Every assistant row carries a
hidden `<audio>` element for it, which should load nothing until its button is pressed. The
spec counts the media errors and media requests those elements make while the thread mounts
and across two short runs, and traces the main thread for each. Anything above zero errors
and one request (the composer's silent clip) is a row loading something it was not asked to.
- **`restyle.perf.spec.ts`**: the cost of one small change in every row at once, which is
what a run starting or ending is. It flips a class, flips `disabled` and mounts a button on
each row by hand and times the style and layout pass that follows. With a stylesheet whose
@ -46,6 +52,7 @@ by side.
| ------------------------ | -------------------------------------------------------------------- |
| `FADE_PERF_CPU` | Slows the renderer by this factor, as DevTools CPU throttling does. |
| `FADE_PERF_THEME` | The color theme to open the chat in, such as `dark`. |
| `FADE_PERF_TTS` | `0` turns read-aloud off, removing the per-row `<audio>` elements. |
| `FADE_PERF_PROMPT` | The fake model's scripted reply for the live run. |
| `FADE_PERF_CSS` | A stylesheet laid over the shipped one, to try a remedy. |
| `FADE_PERF_SKIP_LIVE` | `1` stops the frame spec after the idle and label traces. |

View file

@ -98,12 +98,18 @@ export async function openChat(
page: Page,
chat: BenchChat,
): Promise<{ nodes: number; rows: number }> {
await page.addInitScript((theme) => {
localStorage.setItem('textToSpeech', 'false');
if (theme) {
localStorage.setItem('color-theme', theme);
}
}, process.env.FADE_PERF_THEME);
await page.addInitScript(
({ theme, speech }) => {
if (!speech) {
localStorage.setItem('textToSpeech', 'false');
}
if (theme) {
localStorage.setItem('color-theme', theme);
}
},
/** Read-aloud stays on, as it is by default: it puts an `<audio>` element in every row. */
{ theme: process.env.FADE_PERF_THEME, speech: process.env.FADE_PERF_TTS !== '0' },
);
await page.goto(`/c/${chat.id}`, { timeout: 180_000 });
await expect(messagesView(page).locator('.message-render').first()).toBeAttached({
timeout: 180_000,

View file

@ -0,0 +1,94 @@
import { test } from '@playwright/test';
import { formatFrameCost, traceFrames } from './trace';
import { CONVOS, awaitRunEnd, chatName, draft, openChat, removeChats, seedChat } from './chats';
/**
* What the read-aloud control costs a long thread.
*
* Every assistant row carries a hidden `<audio>` element for its read-aloud button. Nothing is
* meant to load into it until the button is pressed, so it should cost the page nothing: no
* media error, no request, and no main-thread time after the thread has mounted or a run has
* started. `FADE_PERF_TTS=0` turns read-aloud off, for a thread without the elements.
*/
test.use({ trace: 'off', video: 'off' });
const PROMPT = process.env.FADE_PERF_PROMPT ?? 'say hello';
const SETTLE_MS = 12_000;
declare global {
interface Window {
__AUDIO_ERRORS__?: number[];
}
}
test.describe('read-aloud elements in a long thread', () => {
test.skip(CONVOS.length === 0, 'FADE_PERF_CHATS names no exported conversation');
test.afterAll(removeChats);
for (const convo of CONVOS) {
test(`audio elements of ${chatName(convo)}`, async ({ page, browser }) => {
await seedChat(convo);
await page.addInitScript(() => {
window.__AUDIO_ERRORS__ = [];
document.addEventListener(
'error',
(event) => {
if (event.target instanceof HTMLAudioElement) {
window.__AUDIO_ERRORS__?.push(Math.round(performance.now()));
}
},
true,
);
});
const media: string[] = [];
page.on('request', (request) => {
if (request.resourceType() === 'media') {
media.push(new URL(request.url()).pathname);
}
});
const read = () =>
page.evaluate(() => ({
audio: document.querySelectorAll('audio').length,
withSrc: document.querySelectorAll('audio[src]').length,
errors: window.__AUDIO_ERRORS__?.length ?? 0,
last: window.__AUDIO_ERRORS__?.at(-1) ?? 0,
first: window.__AUDIO_ERRORS__?.[0] ?? 0,
}));
const report = async (name: string) => {
const now = await read();
console.log(
`${name.padEnd(28)} audio=${now.audio} withSrc=${now.withSrc} errors=${now.errors} ` +
`(first ${now.first}ms, last ${now.last}ms) mediaRequests=${media.length}`,
);
};
const size = await openChat(page, convo);
console.log(`\n${chatName(convo)}: ${size.rows} rows, ${size.nodes} elements`);
await report('after the thread mounted');
const settled = await traceFrames(browser, page, () => page.waitForTimeout(SETTLE_MS));
console.log(formatFrameCost('open thread, held still', settled));
await report('after holding still');
await draft(page, PROMPT);
const run = await traceFrames(browser, page, async () => {
await page.keyboard.press('Enter');
await awaitRunEnd(page);
await page.waitForTimeout(SETTLE_MS);
});
console.log(formatFrameCost('one short run, then held still', run));
await report('after one run');
await draft(page, PROMPT);
const second = await traceFrames(browser, page, async () => {
await page.keyboard.press('Enter');
await awaitRunEnd(page);
await page.waitForTimeout(SETTLE_MS);
});
console.log(formatFrameCost('a second run, then held still', second));
await report('after a second run');
console.log(`media request paths: ${JSON.stringify([...new Set(media)])}`);
});
}
});