Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ each missing what the other had.

| Test | Contract it holds |
|---|---|
| `flowGroupRenderSegments.test.ts`, `flowGroupRenderSegments.concurrent.test.tsx`, `FlowGroupLifecycle.test.tsx` | unchanged segment reuse, immutable tail snapshots on append/replacement, retained callbacks during suspended renders, and collection lifecycle; local DOM fixtures, not live remote evidence |
| `../../store/FlowChatStore.test.ts`, `ModernFlowChatContainer.history-restore.test.tsx` | live arrival before hydration never caches a guessed global ordinal; first catalog adoption drops unversioned ranges while retaining canonical live bodies; catalog IDs and sparse legacy storage slots keep the first history Turn reachable on local/simulated Peer surfaces, including legacy `backendTurnIndex` payloads; not live remote evidence |
| `flowChatTurnSummaries.test.ts`, `../../utils/flowChatTurnIdentity.test.ts`, `../../utils/flowChatTurnOrdinal.test.ts` | stable summary identity across streamed body updates; append/rollback, catalog and storage-index corrections, partial-tail offsets, catalog-only/empty history windows, provisional usage reports and legacy identity compatibility; batched catalog lookup without per-Turn rescans |
| `../../timeline/useTimelineInteraction.test.tsx` | selection/focus retention and cleanup; mutation/resize bursts coalesce before dimension reads, inactive viewports keep leases without measurement and refresh on reveal, while explicit active selection/focus changes retain immediately; supplied DOM geometry, not native performance evidence |
Expand Down
11 changes: 6 additions & 5 deletions src/web-ui/src/flow_chat/components/modern/FlowGroupRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,12 @@ export const FlowGroupRenderer: React.FC<FlowGroupRendererProps> = React.memo(({
const allItems = useMemo(() => recordedItems.filter(isFlowItemVisible), [recordedItems]);
const parallelRows = useMemo(() => getConcurrentCapsuleRows(allItems), [allItems]);
const prevSegmentsRef = useRef<readonly FlowGroupRenderSegment[]>([]);
const renderSegments = useMemo(() => {
const next = buildFlowGroupRenderSegments(allItems, prevSegmentsRef.current);
prevSegmentsRef.current = next;
return next;
}, [allItems]);
const renderSegments = useMemo(
() => buildFlowGroupRenderSegments(allItems, prevSegmentsRef.current), [allItems],
);
useLayoutEffect(() => {
prevSegmentsRef.current = renderSegments;
}, [renderSegments]);
const toolItemCount = useMemo(() => allItems.filter(item => item.type === 'tool').length, [allItems]);
const filePath = category === 'file-edit' ? fileEditTarget(allItems.find(item => item.type === 'tool')) : undefined;
const fileStatus = useMemo(() => fileEditGroupStatus(category === 'file-edit' ? allItems : []), [category, allItems]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// @vitest-environment jsdom
import React, { act, Suspense, startTransition, useLayoutEffect, useMemo, useRef, useState } from 'react';
import { createRoot } from 'react-dom/client';
import { expect, it, vi } from 'vitest';
import type { FlowItem, FlowToolItem } from '../../types/flow-chat';
import { buildFlowGroupRenderSegments, type FlowGroupRenderSegment } from './flowGroupRenderSegments';

it('keeps retained render callbacks on committed members while an append suspends', async () => {
const read = (index: number): FlowToolItem => ({
id: `call-${index}`, type: 'tool', toolName: 'Read', timestamp: 1, status: 'completed',
toolCall: { id: `call-${index}`, input: {} },
} as FlowToolItem);
const initial: FlowItem[] = [read(0)];
let update!: (items: FlowItem[]) => void;
let committedRender!: () => string[];
let attempted = false;
const pending = new Promise<void>(() => {});

function Group({ items }: { items: FlowItem[] }) {
const previous = useRef<readonly FlowGroupRenderSegment[]>([]);
const segments = useMemo(() => buildFlowGroupRenderSegments(items, previous.current), [items]);
useLayoutEffect(() => {
previous.current = segments;
// DeferredContent retains a render closure over the source segment.
committedRender = () => segments[0].items.map(item => item.id);
}, [segments]);
if (items.length === 2) {
attempted = true;
throw pending;
}
return <div>{items.length} committed</div>;
}
function Host() {
const [items, setItems] = useState(initial);
update = setItems;
return <Suspense fallback="loading"><Group items={items} /></Suspense>;
}

vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true);
const container = document.createElement('div');
const root = createRoot(container);
try {
await act(async () => { root.render(<Host />); });
await act(async () => { startTransition(() => update([...initial, read(1)])); });
expect(attempted).toBe(true);
expect(container.textContent).toBe('1 committed');
expect(committedRender()).toEqual(['call-0']);
} finally {
await act(async () => root.unmount());
vi.unstubAllGlobals();
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,24 @@ it('reuses closed segment object references when appending new items', () => {
expect(secondPass[1]).toBe(firstPass[1]);
expect(secondPass.length).toBe(3);
});

it.each(['append', 'replace'] as const)('preserves the previous tail snapshot on %s', update => {
const initial = Array.from({ length: 17 }, (_, index) => read(index));
const before = buildFlowGroupRenderSegments(initial);
const tail = before[1];
const originalMembers = tail.items;
const originalHeight = tail.estimatedHeightPx;
const updated = update === 'append'
? [...initial, read(17)]
: [...initial.slice(0, -1), { ...initial[16], status: 'running' as const }];
const after = buildFlowGroupRenderSegments(updated, before);

expect(after[0]).toBe(before[0]);
expect(after[1]).not.toBe(tail);
expect(after[1].key).toBe(tail.key);
expect(after[1].items).toEqual(updated.slice(16));
expect(after[1].estimatedHeightPx).toBe(buildFlowGroupRenderSegments(updated)[1].estimatedHeightPx);
expect(tail.items).toBe(originalMembers);
expect(tail.items).toEqual(initial.slice(16));
expect(tail.estimatedHeightPx).toBe(originalHeight);
});
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function estimateFlowGroupItemsHeight(items: readonly FlowItem[]): number
/**
* Stable append boundaries keep reasoning beside its successor and capsule runs together.
* When `previous` segments are provided, settled prefix segments are reused verbatim by reference,
* and the growing trailing segment is incrementally updated, enabling high-frequency streaming reuse.
* while changed segments get new snapshots so retained render callbacks remain consistent.
*/
export function buildFlowGroupRenderSegments(
items: readonly FlowItem[],
Expand All @@ -44,7 +44,7 @@ export function buildFlowGroupRenderSegments(
const segIdx = segments.length;
const reusable = previous?.[segIdx];

// Case 1: Closed settled segment where member items are completely unchanged by reference
// Reuse only segments whose members are completely unchanged by reference.
if (
reusable &&
reusable.startIndex === start &&
Expand All @@ -54,25 +54,7 @@ export function buildFlowGroupRenderSegments(
) {
segments.push(reusable);
}
// Case 2: Live/growing trailing segment where items share the same key & prefix
else if (
reusable &&
reusable.startIndex === start &&
reusable.key === members[0]?.id &&
(
// Either identical length with replaced streaming tail object
(reusable.items.length === members.length &&
reusable.items.slice(0, -1).every((it, idx) => it === members[idx])) ||
// Or growing by appended items
(reusable.items.length <= members.length &&
reusable.items.every((it, idx) => it === members[idx]))
)
) {
reusable.items = members;
reusable.estimatedHeightPx = estimateFlowGroupItemsHeight(members);
segments.push(reusable);
}
// Case 3: Fresh segment
// Changed members must not mutate a snapshot held by a committed render.
else {
segments.push({
key: members[0]?.id ?? `seg-${start}`,
Expand Down
Loading