Repository navigation
perf(mobile): a streaming reply re-renders only the rows that changed #23840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
brennanb2025
merged 26 commits into
main
from
brennanb2025/mobile-chat-stable-transcript
Oct 26, 2026
Merged
Changes from 14 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
cbf7a0f
perf(mobile): a streaming reply re-renders only the rows that changed
brennanb2025 a76d732
perf(mobile): resolve native chat row props only when rows or turns c…
brennanb2025 9e4a0d5
refactor(native-chat): type the shared row reuse comparison
brennanb2025 5b0e2ae
perf(native-chat): compare rebuilt block lists with an index loop
brennanb2025 c1163c0
fix(mobile): carry the not-sent notice through the reused chat rows
nwparker b9b5230
perf(native-chat): keep the desktop list's row comparison as it was
nwparker 3bbf6ac
Merge origin/main into mobile-chat-stable-transcript
nwparker 82df627
Merge origin/main into mobile-chat-stable-transcript
nwparker 18cae17
Restore main's files the pre-commit formatter rewrote during the merge
nwparker b556c71
Allow the FlatList strictMode augmentation's interface
nwparker 3e95898
Pass FlatList strictMode through a spread instead of a type augmentation
nwparker 082ccd5
Merge main into phone transcript performance branch
brennanb2025 b84eeb0
Measure streaming row churn on the phone page against main
brennanb2025 6daf0ca
Keep unchanged phone chat row inputs stable during streaming
brennanb2025 c81a865
Project full-message recovery context with each phone chat row
brennanb2025 7552ec9
Cover recovery updates and stale requests in streaming row contract
brennanb2025 6d9705a
Inspect row messages through their recovery provider in view tests
brennanb2025 0e61b05
Use a type-only module import in the recovery mock
brennanb2025 c543567
Preserve chat recovery identity and skip unchanged row derivation
brennanb2025 239ad8e
Keep transcript reuse at mounted rows and publish current recovery
brennanb2025 ce32630
Merge origin/main before final transcript validation
brennanb2025 f287548
Remove duplicate React type import from transcript parity test
brennanb2025 2408d20
Use static type imports in transcript streaming tests
brennanb2025 02a4ee3
Complete the streaming recovery output fixture
brennanb2025 1b7117e
Temporarily expose failing mobile test compiler diagnostics
brennanb2025 98e8146
Correct streaming test callback and selector types
brennanb2025 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
324 changes: 324 additions & 0 deletions
324
mobile/src/session/MobileNativeChatView.streaming-rows.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,324 @@ | ||
| import { createElement, useMemo } from 'react' | ||
| import { performance } from 'node:perf_hooks' | ||
| import { readFileSync } from 'node:fs' | ||
| import { act, create, type ReactTestRenderer } from 'react-test-renderer' | ||
| import { afterEach, expect, it, vi } from 'vitest' | ||
| import type { AgentJournalRenderItem } from '../../../src/shared/agent-session-journal-types' | ||
| import { projectStructuredAgentSessionMessages } from '../../../src/shared/structured-agent-session-message-projection' | ||
| import { agentJournalTurnBody } from '../../../src/shared/agent-session-turn-record' | ||
| import { foldMobileNativeChatMessages } from './mobile-native-chat-render-data' | ||
| import { useMobileStructuredAgentTurnTiming } from './use-mobile-structured-agent-turn-timing' | ||
| import { MobileNativeChatPageList } from './MobileNativeChatPageList' | ||
| import type { NativeChatMessage } from '../../../src/shared/native-chat-types' | ||
| import { MobileNativeChatView } from './MobileNativeChatView' | ||
|
|
||
| vi.mock('react-native', async () => { | ||
| return { | ||
| ActivityIndicator: 'ActivityIndicator', | ||
| FlatList: 'FlatList', | ||
| ScrollView: 'ScrollView', | ||
| Platform: { OS: 'web' }, | ||
| Pressable: 'Pressable', | ||
| StyleSheet: { create: (styles: unknown) => styles, hairlineWidth: 1 }, | ||
| Text: 'Text', | ||
| View: 'View' | ||
| } | ||
| }) | ||
| vi.mock('react-native-safe-area-context', () => ({ | ||
| useSafeAreaInsets: () => ({ top: 0, bottom: 0, left: 0, right: 0 }) | ||
| })) | ||
| vi.mock('react-native-gesture-handler', () => { | ||
| const chain = { runOnJS: () => chain, onStart: () => chain, onUpdate: () => chain } | ||
| return { | ||
| Gesture: { Simultaneous: () => ({}), Native: () => ({}), Pinch: () => chain }, | ||
| GestureDetector: 'GestureDetector', | ||
| GestureHandlerRootView: 'GestureHandlerRootView' | ||
| } | ||
| }) | ||
| vi.mock('lucide-react-native', () => ({ | ||
| ArrowDown: 'ArrowDown', | ||
| ChevronsDownUp: 'ChevronsDownUp', | ||
| ChevronsUpDown: 'ChevronsUpDown', | ||
| Square: 'Square' | ||
| })) | ||
| const rendered = vi.hoisted(() => new Map<string, number>()) | ||
| vi.mock('./MobileNativeChatMessage', async () => { | ||
| const React = await import('react') | ||
| return { | ||
| MobileNativeChatMessage: React.memo((props: { message: NativeChatMessage }) => { | ||
| rendered.set(props.message.id, (rendered.get(props.message.id) ?? 0) + 1) | ||
| return React.createElement('ChatMessage', props) | ||
| }) | ||
| } | ||
| }) | ||
| vi.mock('./MobileNativeChatPromptCard', () => ({ MobileNativeChatPromptCard: 'PromptCard' })) | ||
| vi.mock('./MobileNativeChatLiveLine', () => ({ MobileNativeChatLiveLine: 'LiveStatus' })) | ||
| vi.mock('./MobileAgentWorkingIndicator', () => ({ | ||
| MobileAgentWorkingIndicator: 'WorkingIndicator' | ||
| })) | ||
| vi.mock('../components/ActionSheetModal', () => ({ ActionSheetModal: 'ActionSheetModal' })) | ||
| vi.mock('./MobileNativeChatAsk', () => ({ MobileNativeChatAsk: 'ChatAsk' })) | ||
| vi.mock('./MobileNativeChatPermission', () => ({ MobileNativeChatPermission: 'ChatPermission' })) | ||
| vi.mock('./MobileNativeChatQuestion', () => ({ MobileNativeChatQuestion: 'ChatQuestion' })) | ||
| vi.mock('./MobileNativeChatComposer', () => ({ MobileNativeChatComposer: 'Composer' })) | ||
|
|
||
| type ViewProps = Parameters<typeof MobileNativeChatView>[0] | ||
| const stable: Pick< | ||
| ViewProps, | ||
| | 'onSend' | ||
| | 'sendSurfaceId' | ||
| | 'getSendCompletionGeneration' | ||
| | 'getComposerEditGeneration' | ||
| | 'composerText' | ||
| | 'onComposerTextChange' | ||
| | 'pending' | ||
| | 'imagePreviewsByMessageId' | ||
| > = { | ||
| onSend: async () => true, | ||
| sendSurfaceId: 'perf-chat', | ||
| getSendCompletionGeneration: () => 0, | ||
| getComposerEditGeneration: () => 0, | ||
| composerText: '', | ||
| onComposerTextChange: () => {}, | ||
| pending: [ | ||
| { | ||
| id: 'pending', | ||
| text: 'follow up', | ||
| images: ['local:pending'], | ||
| sentAt: 10, | ||
| baselineTailMessageId: 'live-user' | ||
| } | ||
| ], | ||
| imagePreviewsByMessageId: { 'user-0': ['local:preview'] } | ||
| } | ||
|
|
||
| function Harness({ | ||
| items, | ||
| corpus = [] | ||
| }: { | ||
| items: AgentJournalRenderItem[] | ||
| corpus?: NativeChatMessage[] | ||
| }) { | ||
| const messages = useMemo( | ||
| () => [...corpus, ...projectStructuredAgentSessionMessages(items, [], [])], | ||
| [items, corpus] | ||
| ) | ||
| const folded = useMemo(() => foldMobileNativeChatMessages(messages, 'claude'), [messages]) | ||
| const timing = useMobileStructuredAgentTurnTiming( | ||
| { items, submissions: [], working: true }, | ||
| 'live' | ||
| ) | ||
| return createElement(MobileNativeChatView, { | ||
| ...stable, | ||
| ...timing, | ||
| messages, | ||
| folded, | ||
| status: 'ready', | ||
| agent: 'claude', | ||
| streaming: null, | ||
| structuredActivityUi: true, | ||
| agentWorking: true | ||
| }) | ||
| } | ||
|
|
||
| function journal(turns: number, text: string): AgentJournalRenderItem[] { | ||
| const items: AgentJournalRenderItem[] = [] | ||
| const add = (itemId: string, body: AgentJournalRenderItem['body']) => { | ||
| items.push({ | ||
| itemId, | ||
| body, | ||
| revision: 1, | ||
| sequence: items.length + 1, | ||
| observedAt: 1000 + items.length | ||
| }) | ||
| } | ||
| for (let i = 0; i < turns; i++) { | ||
| const turnId = `turn-${i}` | ||
| add(`user-${i}`, { | ||
| kind: 'message', | ||
| role: 'user', | ||
| blocks: [{ type: 'text', text: 'inspect the code' }] | ||
| }) | ||
| add(`tool-${i}`, { | ||
| kind: 'message', | ||
| role: 'assistant', | ||
| blocks: [ | ||
| { type: 'tool-call', name: 'Read', callId: `call-${i}`, input: { path: 'index.ts' } }, | ||
| { type: 'tool-result', callId: `call-${i}`, output: 'contents' } | ||
| ] | ||
| }) | ||
| add(`reason-${i}`, { | ||
| kind: 'message', | ||
| role: 'reasoning', | ||
| blocks: [{ type: 'text', text: 'checking' }] | ||
| }) | ||
| add(`answer-${i}`, { | ||
| kind: 'message', | ||
| role: 'assistant', | ||
| blocks: [{ type: 'text', text: 'finished' }] | ||
| }) | ||
| add( | ||
| `lifecycle-${i}`, | ||
| agentJournalTurnBody({ | ||
| turnId, | ||
| userItemId: `user-${i}`, | ||
| state: 'completed', | ||
| startedAt: 1000, | ||
| completedAt: 5000, | ||
| durationMs: 4000 | ||
| }) | ||
| ) | ||
| } | ||
| add('live-user', { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'continue' }] }) | ||
| add( | ||
| 'live-turn', | ||
| agentJournalTurnBody({ | ||
| turnId: 'live', | ||
| userItemId: 'live-user', | ||
| state: 'running', | ||
| startedAt: 6000 | ||
| }) | ||
| ) | ||
| add('live-answer', { kind: 'message', role: 'assistant', blocks: [{ type: 'text', text }] }) | ||
| return items | ||
| } | ||
|
|
||
| let renderer: ReactTestRenderer | null = null | ||
| afterEach(() => { | ||
| act(() => renderer?.unmount()) | ||
| renderer = null | ||
| rendered.clear() | ||
| }) | ||
|
|
||
| function measure(turns: number, corpus: NativeChatMessage[] = []) { | ||
| // Retain every loaded row by reaching the page's scroll-driven draw expansion. | ||
| const scroller = { | ||
| scrollTop: 0, | ||
| scrollHeight: 100, | ||
| clientHeight: 100, | ||
| addEventListener() {}, | ||
| removeEventListener() {}, | ||
| querySelectorAll: () => [] | ||
| } | ||
| const items = journal(turns, 'hello') | ||
| act(() => { | ||
| renderer = create(createElement(Harness, { items, corpus }), { | ||
| createNodeMock: (element) => | ||
| element.type === 'ScrollView' | ||
| ? { getScrollableNode: () => scroller, getInnerViewNode: () => null } | ||
| : null | ||
| }) | ||
| }) | ||
| expect(renderer!.root.findAllByType(MobileNativeChatPageList)).toHaveLength(1) | ||
| const mounted = renderer!.root.findAll((node) => Object.is(node.type, 'ChatMessage')) | ||
| expect(mounted.length).toBe( | ||
| foldMobileNativeChatMessages( | ||
| [...corpus, ...projectStructuredAgentSessionMessages(items, [], [])], | ||
| 'claude' | ||
| ).length + 1 | ||
| ) | ||
| expect(mounted.some((node) => node.props.workRun?.length > 1)).toBe(true) | ||
| expect( | ||
| mounted.filter((node) => node.props.turnStatus?.workedSeconds === 4).length | ||
| ).toBeGreaterThanOrEqual(turns) | ||
| const preview = mounted.find((node) => node.props.message.id === 'user-0')! | ||
| expect(preview.props.message.blocks).toContainEqual({ type: 'image-ref', url: 'local:preview' }) | ||
| expect(mounted.some((node) => node.props.message.id === 'pending')).toBe(true) | ||
| const samples: { renders: number; ms: number; ids: string[] }[] = [] | ||
| for (let batch = 0; batch < 20; batch++) { | ||
| rendered.clear() | ||
| const updated = items.map((item) => | ||
| item.itemId === 'live-answer' | ||
| ? { | ||
| ...item, | ||
| revision: batch + 2, | ||
| body: { | ||
| kind: 'message' as const, | ||
| role: 'assistant' as const, | ||
| blocks: [{ type: 'text' as const, text: 'hello' + '.'.repeat(batch + 1) }] | ||
| } | ||
| } | ||
| : item | ||
| ) | ||
| const start = performance.now() | ||
| act(() => renderer!.update(createElement(Harness, { items: updated, corpus }))) | ||
| samples.push({ | ||
| ms: performance.now() - start, | ||
| renders: [...rendered.values()].reduce((sum, n) => sum + n, 0), | ||
| ids: [...rendered.keys()] | ||
| }) | ||
| } | ||
| const result = { | ||
| turns, | ||
| rows: mounted.length, | ||
| renders: samples.map((sample) => sample.renders), | ||
| meanMs: samples.reduce((sum, sample) => sum + sample.ms, 0) / samples.length | ||
| } | ||
| process.stdout.write(`STREAM_PERF ${JSON.stringify(result)}\n`) | ||
| expect( | ||
| samples.every((sample) => sample.ids.length === 1 && sample.ids[0] === 'live-answer') | ||
| ).toBe(true) | ||
| expect( | ||
| renderer!.root | ||
| .findAll((node) => Object.is(node.type, 'ChatMessage')) | ||
| .find((node) => node.props.message.id === 'live-answer')!.props.message.blocks[0].text | ||
| ).toBe('hello' + '.'.repeat(20)) | ||
| } | ||
|
|
||
| it.each([10, 100])('bounds streaming renders at %i settled turns', (turns) => measure(turns)) | ||
| it.skipIf(!process.env.ORCA_CHAT_PERF_CORPUS)( | ||
| 'measures the private real-scale transcript without committing it', | ||
| () => { | ||
| const corpus: NativeChatMessage[] = JSON.parse( | ||
| readFileSync(process.env.ORCA_CHAT_PERF_CORPUS!, 'utf8') | ||
| ) | ||
| measure(10, corpus) | ||
| } | ||
| ) | ||
|
|
||
| it('reuses footer rows during a command and returns them to the list afterward', () => { | ||
| const show = (text: string, holding = true) => | ||
| act(() => { | ||
| const items = journal(0, text).map((item) => | ||
| item.body.kind === 'message' && item.body.role === 'user' && holding | ||
| ? { ...item, body: { ...item.body, command: { name: 'compact' } } } | ||
| : item | ||
| ) | ||
| const held: NativeChatMessage = { | ||
| id: 'held', | ||
| role: 'user', | ||
| blocks: [{ type: 'text', text: 'next' }], | ||
| timestamp: 3, | ||
| source: 'transcript', | ||
| queued: true | ||
| } | ||
| const folded = [...projectStructuredAgentSessionMessages(items, [], []), held] | ||
| const element = createElement(MobileNativeChatView, { | ||
| ...stable, | ||
| pending: [], | ||
| messages: folded, | ||
| folded, | ||
| status: 'ready', | ||
| streaming: null, | ||
| agentWorking: true, | ||
| structuredActivityUi: true, | ||
| turnJournal: { items, submissions: [] } | ||
| }) | ||
| if (renderer) { | ||
| renderer.update(element) | ||
| } else { | ||
| renderer = create(element) | ||
| } | ||
| }) | ||
| const listed = (): string[] => | ||
| renderer!.root | ||
| .findByType(MobileNativeChatPageList) | ||
| .props.rows.map((row: NativeChatMessage) => row.id) | ||
| show('hello') | ||
| expect(listed()).not.toContain('held') | ||
| rendered.clear() | ||
| show('hello again') | ||
| expect([...rendered.keys()]).toEqual(['live-answer']) | ||
| show('done', false) | ||
| expect(listed()).toContain('held') | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.