Skip to content

Commit 7a7f6df

Browse files
authored
fix(ui): keep native history anchor stable (#123103)
1 parent 600126a commit 7a7f6df

3 files changed

Lines changed: 114 additions & 55 deletions

File tree

‎pnpm-lock.yaml‎

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎ui/package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
"@openclaw/session-url-contract": "workspace:*",
3333
"@openclaw/uirouter": "0.1.1",
3434
"@openclaw/workboard-contract": "workspace:*",
35-
"@tanstack/lit-virtual": "3.13.35",
36-
"@tanstack/virtual-core": "3.17.6",
35+
"@tanstack/lit-virtual": "3.13.36",
36+
"@tanstack/virtual-core": "3.17.7",
3737
"dompurify": "3.4.12",
3838
"ghostty-web": "0.4.0",
3939
"highlight.js": "11.11.1",

‎ui/src/e2e/claude-sessions.e2e.test.ts‎

Lines changed: 101 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ type VirtualRowPaintSample = {
2828
type VirtualRowPaintProbe = {
2929
frameIds: number[];
3030
observer: MutationObserver;
31+
pendingSamples: number;
32+
samples: VirtualRowPaintSample[];
33+
timerIds: number[];
34+
};
35+
36+
type VirtualRowPaintResult = {
37+
pending: boolean;
3138
samples: VirtualRowPaintSample[];
3239
};
3340

@@ -71,12 +78,15 @@ async function startVirtualRowPaintProbe(thread: Locator, anchor: VisibleVirtual
7178
if (staleProbe) {
7279
staleProbe.observer.disconnect();
7380
staleProbe.frameIds.forEach((frameId) => cancelAnimationFrame(frameId));
81+
staleProbe.timerIds.forEach((timerId) => clearTimeout(timerId));
7482
delete target.chatPrependPaintProbe;
7583
}
7684
const probe: VirtualRowPaintProbe = {
7785
frameIds: [],
7886
observer: new MutationObserver(() => undefined),
87+
pendingSamples: 0,
7988
samples: [],
89+
timerIds: [],
8090
};
8191
const sample = () => {
8292
const viewport = element.getBoundingClientRect();
@@ -100,18 +110,30 @@ async function startVirtualRowPaintProbe(thread: Locator, anchor: VisibleVirtual
100110
viewportTop: rect ? rect.top - viewport.top : null,
101111
});
102112
};
103-
const scheduleSample = () => {
104-
if (probe.frameIds.length > 0) {
105-
return;
113+
const removePendingId = (ids: number[], id: number) => {
114+
const index = ids.indexOf(id);
115+
if (index !== -1) {
116+
ids.splice(index, 1);
106117
}
118+
};
119+
const scheduleSample = () => {
120+
// Each mutation batch owns a post-paint sample; later mutations must not
121+
// cancel an earlier frame that could expose a visible anchor jump.
122+
probe.pendingSamples += 1;
107123
const firstFrame = requestAnimationFrame(() => {
124+
removePendingId(probe.frameIds, firstFrame);
108125
const secondFrame = requestAnimationFrame(() => {
109-
probe.frameIds = [];
110-
sample();
126+
removePendingId(probe.frameIds, secondFrame);
127+
const timerId = window.setTimeout(() => {
128+
removePendingId(probe.timerIds, timerId);
129+
sample();
130+
probe.pendingSamples -= 1;
131+
}, 0);
132+
probe.timerIds.push(timerId);
111133
});
112-
probe.frameIds = [secondFrame];
134+
probe.frameIds.push(secondFrame);
113135
});
114-
probe.frameIds = [firstFrame];
136+
probe.frameIds.push(firstFrame);
115137
};
116138
probe.observer = new MutationObserver(scheduleSample);
117139
probe.observer.observe(element, {
@@ -124,7 +146,24 @@ async function startVirtualRowPaintProbe(thread: Locator, anchor: VisibleVirtual
124146
}, anchor);
125147
}
126148

127-
async function stopVirtualRowPaintProbe(thread: Locator): Promise<VirtualRowPaintSample[]> {
149+
async function readVirtualRowPaintProbe(thread: Locator) {
150+
return thread.evaluate(() => {
151+
const probe = (
152+
globalThis as typeof globalThis & {
153+
chatPrependPaintProbe?: VirtualRowPaintProbe;
154+
}
155+
).chatPrependPaintProbe;
156+
if (!probe) {
157+
throw new Error("expected an active virtual row paint probe");
158+
}
159+
return {
160+
pendingSamples: probe.pendingSamples,
161+
samples: probe.samples,
162+
};
163+
});
164+
}
165+
166+
async function stopVirtualRowPaintProbe(thread: Locator): Promise<VirtualRowPaintResult> {
128167
return thread.evaluate(() => {
129168
const target = globalThis as typeof globalThis & {
130169
chatPrependPaintProbe?: VirtualRowPaintProbe;
@@ -133,42 +172,52 @@ async function stopVirtualRowPaintProbe(thread: Locator): Promise<VirtualRowPain
133172
if (!probe) {
134173
throw new Error("expected an active virtual row paint probe");
135174
}
175+
const pending = probe.pendingSamples > 0;
136176
probe.observer.disconnect();
137177
probe.frameIds.forEach((frameId) => cancelAnimationFrame(frameId));
178+
probe.timerIds.forEach((timerId) => clearTimeout(timerId));
138179
delete target.chatPrependPaintProbe;
139-
return probe.samples;
180+
return { pending, samples: probe.samples };
140181
});
141182
}
142183

143-
function expectPaintedVirtualRowAnchor(
144-
anchor: VisibleVirtualRow,
145-
samples: VirtualRowPaintSample[],
146-
) {
147-
const evidence = JSON.stringify({ anchor, samples });
148-
expect(samples.length, evidence).toBeGreaterThan(0);
149-
expect(
150-
samples.some(
184+
function virtualRowAnchorStatus(anchor: VisibleVirtualRow, samples: VirtualRowPaintSample[]) {
185+
return {
186+
advanced: samples.some(
151187
(sample) =>
152188
(sample.index !== null && sample.index > anchor.index) ||
153189
sample.totalSize > anchor.totalSize,
154190
),
155-
evidence,
156-
).toBe(true);
157-
expect(
158-
samples.every((sample) => sample.viewportTop !== null),
159-
evidence,
160-
).toBe(true);
161-
expect(
162-
samples.every((sample) => sample.intersectsViewport),
163-
evidence,
164-
).toBe(true);
165-
expect(
166-
samples.every(
191+
anchored: samples.every(
167192
(sample) =>
168193
sample.viewportTop !== null && Math.abs(sample.viewportTop - anchor.viewportTop) <= 2,
169194
),
195+
present: samples.length > 0 && samples.every((sample) => sample.viewportTop !== null),
196+
visible: samples.every((sample) => sample.intersectsViewport),
197+
};
198+
}
199+
200+
async function waitForPaintedVirtualRowAnchor(thread: Locator, anchor: VisibleVirtualRow) {
201+
await expect
202+
.poll(async () => {
203+
const probe = await readVirtualRowPaintProbe(thread);
204+
return probe.pendingSamples === 0 && virtualRowAnchorStatus(anchor, probe.samples).advanced;
205+
})
206+
.toBe(true);
207+
}
208+
209+
function expectPaintedVirtualRowAnchor(anchor: VisibleVirtualRow, result: VirtualRowPaintResult) {
210+
const evidence = JSON.stringify({ anchor, ...result });
211+
expect(
212+
{ pending: result.pending, ...virtualRowAnchorStatus(anchor, result.samples) },
170213
evidence,
171-
).toBe(true);
214+
).toEqual({
215+
pending: false,
216+
advanced: true,
217+
anchored: true,
218+
present: true,
219+
visible: true,
220+
});
172221
}
173222

174223
function resumableClaudeCatalog() {
@@ -704,7 +753,7 @@ suite.define(() => {
704753
expect(await catalogPane.getByRole("button", { name: "Load older" }).count()).toBe(0);
705754
const anchor = await captureTopVisibleVirtualRow(thread);
706755
await startVirtualRowPaintProbe(thread, anchor);
707-
let paintedSamples: VirtualRowPaintSample[];
756+
let paintResult: VirtualRowPaintResult;
708757
try {
709758
await gateway.resolveDeferred("sessions.catalog.read");
710759
await expect
@@ -715,12 +764,11 @@ suite.define(() => {
715764
),
716765
)
717766
.toBe(41);
718-
// Each mutation is sampled after a full paint, once anchor compensation has settled.
719767
await page.clock.runFor(100);
720768
} finally {
721-
paintedSamples = await stopVirtualRowPaintProbe(thread);
769+
paintResult = await stopVirtualRowPaintProbe(thread);
722770
}
723-
expectPaintedVirtualRowAnchor(anchor, paintedSamples);
771+
expectPaintedVirtualRowAnchor(anchor, paintResult);
724772
expect(
725773
await catalogPane.locator(".agent-chat__composer-combobox > textarea").isDisabled(),
726774
).toBe(true);
@@ -774,7 +822,7 @@ suite.define(() => {
774822

775823
it("auto-loads older native history with a spinner and stable viewport", async () => {
776824
const page = await suite.browser.newPage({ viewport: { width: 1280, height: 800 } });
777-
await page.clock.install();
825+
const artifactDir = process.env.OPENCLAW_UI_E2E_ARTIFACT_DIR?.trim();
778826
const historyMessage = (seq: number, prefix: string) => ({
779827
__openclaw: { seq },
780828
content: [
@@ -838,9 +886,16 @@ suite.define(() => {
838886
await page.locator('.chat-virtual-row:not([data-virtual-row-key="history"])').first().waitFor();
839887
await gateway.waitForRequest("chat.history");
840888
await page.locator(".chat-history-loading").waitFor();
889+
if (artifactDir) {
890+
await fs.mkdir(artifactDir, { recursive: true });
891+
await page.screenshot({
892+
path: path.join(artifactDir, "01-native-history-loading.png"),
893+
fullPage: true,
894+
});
895+
}
841896
const anchor = await captureTopVisibleVirtualRow(thread);
842897
await startVirtualRowPaintProbe(thread, anchor);
843-
let paintedSamples: VirtualRowPaintSample[];
898+
let paintResult: VirtualRowPaintResult;
844899
try {
845900
await gateway.resolveDeferred("chat.history");
846901
await expect
@@ -854,12 +909,17 @@ suite.define(() => {
854909
),
855910
)
856911
.toBe(140);
857-
// Each mutation is sampled after a full paint, once anchor compensation has settled.
858-
await page.clock.runFor(100);
912+
await waitForPaintedVirtualRowAnchor(thread, anchor);
859913
} finally {
860-
paintedSamples = await stopVirtualRowPaintProbe(thread);
914+
paintResult = await stopVirtualRowPaintProbe(thread);
915+
}
916+
expectPaintedVirtualRowAnchor(anchor, paintResult);
917+
if (artifactDir) {
918+
await page.screenshot({
919+
path: path.join(artifactDir, "02-native-history-prepended-stable.png"),
920+
fullPage: true,
921+
});
861922
}
862-
expectPaintedVirtualRowAnchor(anchor, paintedSamples);
863923
expect((await gateway.getRequests("chat.history")).at(-1)?.params).toMatchObject({
864924
limit: 100,
865925
offset: 100,
@@ -869,9 +929,8 @@ suite.define(() => {
869929
element.scrollTop = 0;
870930
element.dispatchEvent(new Event("scroll"));
871931
});
872-
await page.clock.runFor(100);
873932
await page.getByText(/^older native message 1\n/).waitFor();
874-
await page.clock.runFor(300);
933+
await expect.poll(() => page.locator(".chat-history-sentinel").count()).toBe(0);
875934
expect(await page.locator(".chat-history-loading").count()).toBe(0);
876935
expect(await gateway.getRequests("chat.history")).toHaveLength(exhaustedRequestCount);
877936
await page.close();

0 commit comments

Comments
 (0)