Files
pipelines/app/tool/chat_reopen_probe.mjs
T
alan 0a19ef229f Show a reopened chat's last transcript while it refreshes
ChatScreen keeps a bounded in-memory cache of recently left transcripts
(16 chats / 4000 messages, LRU). A reopened chat is seeded from it with no
loading state, still re-subscribes and re-reads, and the newest page replaces
the cached copy while live frames received during the read are kept. /close
removes the entry; only transcripts backed by a successful read on the
current connection are written.

Adds tool/chat_reopen_probe.mjs and an optional history delay in the loopback
fixture. Measurement shows a second cost the cache does not remove: opening at
the end of the top-anchored transcript lays out every row (notes).
2026-10-24 18:14:27 +01:00

124 lines
7.6 KiB
JavaScript

// Measures list -> chat -> Back -> same chat against a populated loopback
// fixture (never operator data). It separates the history read from rendering:
// HISTORY_DELAY_MS holds every `chat_messages` reply, and each visit records
// when the reply was sent and when the transcript's text first reached the
// semantics tree.
// HISTORY_DELAY_MS=1500 CHAT_PAIRS=100 node tool/chat_reopen_probe.mjs <web-build> <out>
import assert from 'node:assert/strict';
import { mkdir, mkdtemp, writeFile } from 'node:fs/promises';
import { once } from 'node:events';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import { performance } from 'node:perf_hooks';
import { startFixture } from './runs_probe_server.mjs';
import { launchChrome, waitForPort, waitForPage, connect, sleep } from './pipelines_probe_cdp_lib.mjs';
import { axNodes, waitFor, capture } from './pipelines_probe_cdp_input.mjs';
const pairs = Number(process.env.CHAT_PAIRS ?? 100);
const width = Number(process.env.PROBE_WIDTH ?? 390);
const visits = Number(process.env.VISITS ?? 3);
const historyDelayMs = Number(process.env.HISTORY_DELAY_MS ?? 0);
const text = '# Recorded answer\n\n' +
Array.from({ length: 12 }, (_, i) => `Paragraph ${i}: This is recorded fixture content with **formatting**, an [inert link](https://example.test), and enough text to wrap naturally on a phone.\n`).join('\n') +
'\n```dart\n' + Array.from({ length: 25 }, (_, i) => `final value${i} = ${i};`).join('\n') + '\n```\n';
const messages = Array.from({ length: pairs }, (_, i) => [
{ id: `u${i}`, role: 'user', parts: [{ type: 'text', text: `Recorded question ${i}` }] },
{ id: `a${i}`, role: 'assistant', parts: [{ id: `p${i}`, type: 'text', text }] },
]).flat();
const output = resolve(process.argv[3] ?? 'build/chat-reopen');
await mkdir(output, { recursive: true });
const historyEvents = [];
const fixture = await startFixture({
root: resolve(process.argv[2]), chatMessages: messages, chatHistoryDelayMs: historyDelayMs,
onChatHistory: (phase) => historyEvents.push({ phase, at: performance.now() }),
});
const profile = await mkdtemp(join(tmpdir(), 'chat-reopen-'));
const chrome = launchChrome('/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', profile, 'about:blank');
const log = { pairs, width, historyDelayMs, cpuRate: Number(process.env.CPU_RATE ?? 1), host: process.env.PROBE_PLATFORM ?? 'cupertino', visits: [], checks: [], captures: [] };
let c;
// Transcript text is in the semantics DOM once the view has painted it; the
// marker never appears in the list, the header or a loading indicator.
const TRANSCRIPT_VISIBLE = `(() => {
const host = document.querySelector('flt-semantics-host');
if (!host) return false;
if (host.textContent.includes('Paragraph')) return true;
return [...host.querySelectorAll('[aria-label]')].some(e => e.getAttribute('aria-label').includes('Paragraph'));
})()`;
async function tapNode(node) {
const { model } = await c.send('DOM.getBoxModel', { backendNodeId: node.backendDOMNodeId });
const [x1, y1, , , x3, y3] = model.content;
await c.send('Input.dispatchMouseEvent', { type: 'mousePressed', x: (x1 + x3) / 2, y: (y1 + y3) / 2, button: 'left', clickCount: 1 });
await c.send('Input.dispatchMouseEvent', { type: 'mouseReleased', x: (x1 + x3) / 2, y: (y1 + y3) / 2, button: 'left', clickCount: 1 });
}
try {
c = await connect((await waitForPage(await waitForPort(join(profile, 'DevToolsActivePort')))).webSocketDebuggerUrl);
await c.send('Page.enable');
if (log.host === 'android') await c.send('Emulation.setUserAgentOverride', {
userAgent: 'Mozilla/5.0 (Linux; Android 16; BrowserFixture) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/155.0.0.0 Mobile Safari/537.36', platform: 'Linux armv8l',
});
await c.send('Emulation.setDeviceMetricsOverride', { width, height: 844, deviceScaleFactor: 1, mobile: true });
await c.send('Emulation.setCPUThrottlingRate', { rate: log.cpuRate });
await c.send('Page.navigate', { url: fixture.url + '/chat' });
assert.ok(await waitFor(() => c.evaluate("Boolean(document.querySelector('flt-semantics-placeholder'))"), 30000));
const rect = await c.evaluate("(() => { const r = document.querySelector('flt-semantics-placeholder').getBoundingClientRect(); return {x:(r.left+r.right)/2,y:(r.top+r.bottom)/2}; })()");
await c.send('Input.dispatchMouseEvent', { type: 'mousePressed', x: rect.x, y: rect.y, button: 'left', clickCount: 1 });
await c.send('Input.dispatchMouseEvent', { type: 'mouseReleased', x: rect.x, y: rect.y, button: 'left', clickCount: 1 });
for (let visit = 0; visit < visits; visit++) {
const row = await waitFor(async () => (await axNodes(c)).find(n => n.name?.value?.startsWith('Probe conversation') && n.backendDOMNodeId && ['group', 'button'].includes(n.role?.value)), 30000);
assert.ok(row, 'chat list row');
const firstEvent = historyEvents.length;
// The page's own clock times the visit; the in-page frame loop sees the
// first frame whose semantics carry transcript text.
// Long tasks and frame gaps show whether the main thread is busy
// rendering, independently of when the semantics tree catches up.
await c.evaluate(`(() => {
const open = window.__open = {started: performance.now(), frames: [], longTasks: []};
new PerformanceObserver(list => { for (const e of list.getEntries()) if (e.startTime >= open.started) open.longTasks.push({start: e.startTime - open.started, duration: e.duration}); }).observe({entryTypes: ['longtask']});
const frame = t => {
open.frames.push(t);
if (!open.visible && ${TRANSCRIPT_VISIBLE}) open.visible = performance.now();
if (t - open.started < 6000) requestAnimationFrame(frame);
};
requestAnimationFrame(frame);
})()`);
const tapped = performance.now();
await tapNode(row);
await sleep(Number(process.env.EARLY_CAPTURE_MS ?? 250));
await capture(c, output, log, `visit-${visit + 1}-early`);
assert.ok(await waitFor(() => c.evaluate('Boolean(window.__open.visible)'), 45000), 'transcript visible');
await sleep(Math.max(0, 6500 - (performance.now() - tapped)));
const open = await c.evaluate('window.__open');
const reply = historyEvents.slice(firstEvent).find(e => e.phase === 'reply');
log.visits.push({
visit: visit + 1,
transcriptVisibleMs: Math.round(open.visible - open.started),
historyReplySentMs: reply ? Math.round(reply.at - tapped) : null,
historyRequests: historyEvents.slice(firstEvent).filter(e => e.phase === 'request').length,
longTaskTotalMs: Math.round(open.longTasks.reduce((sum, task) => sum + task.duration, 0)),
longestTaskMs: Math.round(Math.max(0, ...open.longTasks.map(task => task.duration))),
maxFrameGapMs: Math.round(Math.max(0, ...open.frames.slice(1).map((t, i) => t - open.frames[i]))),
});
await capture(c, output, log, `visit-${visit + 1}-settled`);
const back = await waitFor(async () => {
const nodes = (await axNodes(c)).filter(n => /^Back(?:\s|$)/.test(n.name?.value ?? '') && n.backendDOMNodeId);
return nodes.find(n => n.role?.value === 'button') ?? nodes[0];
}, 30000);
assert.ok(back, 'native Back control');
await tapNode(back);
assert.ok(await waitFor(() => c.evaluate("location.pathname === '/chat'"), 30000), 'back to list');
await sleep(900);
}
log.checks.push('every visit painted the transcript');
console.log(JSON.stringify(log, null, 2));
} catch (error) {
if (c) await capture(c, output, log, 'failure');
throw error;
} finally {
await writeFile(join(output, 'result.json'), JSON.stringify(log, null, 2));
c?.close(); const exited = once(chrome, 'exit'); chrome.kill(); await exited;
await fixture.close();
}