Skip to content

Commit 842f8f3

Browse files
authored
feat(ui): auto-load older external-session transcript on scroll (#105209)
* feat(ui): auto-load older catalog transcript on scroll with a stable viewport * fix(ui): surface the load-older fallback when auto-load is blocked on a short thread * fix(ui): clamp oversized aggregated tool output in catalog transcripts * fix(ui): stop catalog auto-pagination when an older read makes no progress * fix(ui): continue catalog paging through advancing empty pages; exhaust only on a stalled cursor * fix(ui): detect catalog cursor cycles to bound auto-pagination
1 parent 5b56a1f commit 842f8f3

7 files changed

Lines changed: 604 additions & 53 deletions

File tree

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

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ suite("Claude native session catalog", () => {
3030
await server?.close();
3131
});
3232

33-
it("uses native sidebar/chat pagination and disables paired-node continuation", async () => {
33+
it("auto-loads older chat without moving the viewport and disables paired-node continuation", async () => {
3434
const page = await browser.newPage();
3535
await page.clock.install();
3636
const catalogResponse = (threadId: string, name: string, nextCursor?: string) => ({
@@ -100,7 +100,14 @@ suite("Claude native session catalog", () => {
100100
response: {
101101
hostId: "node:devbox",
102102
threadId: "remote-thread",
103-
items: [{ id: "a1", type: "agentMessage", text: "newer answer" }],
103+
items: Array.from({ length: 40 }, (_, index) => ({
104+
id: `a${index + 1}`,
105+
type: index % 2 === 0 ? "agentMessage" : "userMessage",
106+
text:
107+
index === 0
108+
? "newer answer"
109+
: `recent transcript message ${index + 1} with enough text to fill the pane`,
110+
})),
104111
nextCursor: "older",
105112
},
106113
},
@@ -123,8 +130,32 @@ suite("Claude native session catalog", () => {
123130
await page.getByText("Older remote review", { exact: true }).waitFor();
124131
await page.getByText("Remote architecture review", { exact: true }).click();
125132
await expect.poll(() => page.getByText("newer answer", { exact: true }).count()).toBe(1);
126-
await page.getByRole("button", { name: "Load older" }).click();
133+
const thread = page.locator(".chat-thread");
134+
await expect
135+
.poll(() => thread.evaluate((element) => element.scrollHeight > element.clientHeight + 100))
136+
.toBe(true);
137+
const initialReadCount = (await gateway.getRequests("sessions.catalog.read")).length;
138+
await gateway.deferNext("sessions.catalog.read");
139+
const before = await thread.evaluate((element) => {
140+
element.scrollTop = 0;
141+
return { scrollHeight: element.scrollHeight, scrollTop: element.scrollTop };
142+
});
143+
await expect
144+
.poll(() => gateway.getRequests("sessions.catalog.read").then((requests) => requests.length))
145+
.toBe(initialReadCount + 1);
146+
await page.locator(".chat-history-loading").waitFor();
147+
expect(await page.getByRole("button", { name: "Load older" }).count()).toBe(0);
148+
await gateway.resolveDeferred("sessions.catalog.read");
127149
await expect.poll(() => page.getByText("older question", { exact: true }).count()).toBe(1);
150+
const after = await thread.evaluate((element) => ({
151+
scrollHeight: element.scrollHeight,
152+
scrollTop: element.scrollTop,
153+
}));
154+
expect(after.scrollTop).toBeGreaterThan(0);
155+
expect(after.scrollTop).toBeCloseTo(
156+
before.scrollTop + (after.scrollHeight - before.scrollHeight),
157+
0,
158+
);
128159
expect(await page.locator(".agent-chat__composer-combobox > textarea").isDisabled()).toBe(true);
129160
await expect
130161
.poll(() => page.getByText("This session is on a paired node and is view-only.").count())
@@ -133,6 +164,14 @@ suite("Claude native session catalog", () => {
133164
catalogId: "claude",
134165
cursor: "older",
135166
});
167+
const exhaustedReadCount = (await gateway.getRequests("sessions.catalog.read")).length;
168+
await thread.evaluate((element) => {
169+
element.scrollTop = 0;
170+
});
171+
await page.clock.runFor(500);
172+
expect(await page.locator(".chat-history-loading").count()).toBe(0);
173+
expect(await page.getByRole("button", { name: "Load older" }).count()).toBe(0);
174+
expect(await gateway.getRequests("sessions.catalog.read")).toHaveLength(exhaustedReadCount);
136175
await page.close();
137176
});
138177
});

‎ui/src/pages/chat/chat-pane.test.ts‎

Lines changed: 184 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { describe, expect, it, vi } from "vitest";
44
import type {
55
SessionCatalogSession,
6+
SessionCatalogTranscriptItem,
67
SessionsCatalogListResult,
78
SessionsCatalogReadResult,
89
TaskSuggestion,
@@ -37,7 +38,18 @@ type TestChatPane = HTMLElement & {
3738
onPaneSessionChange?: (paneId: string, sessionKey: string) => void;
3839
sessionKey: string;
3940
catalogSession: SessionCatalogSession | null;
40-
loadCatalogSession: (key: CatalogSessionKey, older: boolean) => Promise<void>;
41+
catalogItemMessage: (
42+
item: SessionCatalogTranscriptItem,
43+
index: number,
44+
) => Record<string, unknown> | null;
45+
handleTranscriptScroll: (event: Event) => void;
46+
historyAutoLoadBlocked: boolean;
47+
syncHistoryObserver: () => void;
48+
loadCatalogSession: (key: CatalogSessionKey, older: boolean) => Promise<boolean>;
49+
loadOlderMessages: () => Promise<void>;
50+
hasOlderMessages: () => boolean;
51+
catalogCursor: string | undefined;
52+
olderCursorsSeen: Set<string>;
4153
};
4254

4355
const suggestion: TaskSuggestion = {
@@ -115,6 +127,10 @@ function createTestChatPane(params: { client: GatewayBrowserClient; sessions: Se
115127
sessionsLoading: false,
116128
sidebarContent: null,
117129
sidebarOpen: false,
130+
// Minimal scroll host so scheduleChatScroll is a no-op instead of throwing.
131+
chatScrollGeneration: 0,
132+
chatScrollCommitCleanup: null,
133+
renderLifecycle: { afterCommit: () => () => {}, invalidate: () => {} },
118134
} as unknown as ChatPageHost;
119135
pane.context = createSessionContext(params.client, params.sessions);
120136
pane.state = state;
@@ -348,6 +364,173 @@ describe("chat pane catalog session lifecycle", () => {
348364
});
349365
expect(pane.catalogSession).toEqual(selectedSession);
350366
});
367+
368+
it.each([
369+
{
370+
name: "uses a raw command for an empty tool call",
371+
item: { type: "toolCall", raw: { command: "git status --short" } },
372+
expected: "Tool call\n\ngit status --short",
373+
},
374+
{
375+
name: "uses aggregated output for an empty tool result",
376+
item: { type: "toolResult", raw: { aggregatedOutput: "working tree clean" } },
377+
expected: "Tool result\n\nworking tree clean",
378+
},
379+
{
380+
name: "renders an empty reasoning item as its label alone",
381+
item: { type: "reasoning" },
382+
expected: "Thinking",
383+
},
384+
])("$name", ({ item, expected }) => {
385+
const client = { request: vi.fn() } as unknown as GatewayBrowserClient;
386+
const { pane } = createTestChatPane({ client, sessions: {} as SessionCapability });
387+
388+
const message = pane.catalogItemMessage(item as SessionCatalogTranscriptItem, 0) as {
389+
content: Array<{ text: string }>;
390+
};
391+
392+
expect(message.content[0]?.text).toBe(expected);
393+
expect(message.content[0]?.text).not.toContain("Unsupported external session item");
394+
});
395+
396+
it("clamps oversized aggregated tool output before rendering", () => {
397+
const client = { request: vi.fn() } as unknown as GatewayBrowserClient;
398+
const { pane } = createTestChatPane({ client, sessions: {} as SessionCapability });
399+
400+
const message = pane.catalogItemMessage(
401+
{
402+
type: "toolResult",
403+
raw: { aggregatedOutput: "x".repeat(5000) },
404+
} as SessionCatalogTranscriptItem,
405+
0,
406+
) as { content: Array<{ text: string }> };
407+
408+
// The 500-char preview cap keeps a single huge tool result from injecting
409+
// megabytes into one chat message; the "Tool result\n\n" prefix adds a bit.
410+
expect(message.content[0]?.text.length).toBeLessThan(600);
411+
expect(message.content[0]?.text.startsWith("Tool result")).toBe(true);
412+
});
413+
414+
it("skips an empty unknown catalog item", () => {
415+
const client = { request: vi.fn() } as unknown as GatewayBrowserClient;
416+
const { pane } = createTestChatPane({ client, sessions: {} as SessionCapability });
417+
418+
expect(pane.catalogItemMessage({ type: "other" }, 0)).toBeNull();
419+
});
420+
421+
it("exhausts pagination when an older read does not advance the cursor", async () => {
422+
const readPage: SessionsCatalogReadResult = {
423+
hostId: "gateway:local",
424+
threadId: "thread-1",
425+
items: [{ id: "u1", type: "userMessage", text: "hi" }],
426+
// Same cursor the request was made with: a stale provider that would loop.
427+
nextCursor: "cursor-1",
428+
};
429+
const client = {
430+
request: vi.fn(async () => readPage),
431+
} as unknown as GatewayBrowserClient;
432+
const { pane, state } = createTestChatPane({ client, sessions: {} as SessionCapability });
433+
const key = "catalog:claude:gateway%3Alocal:thread-1";
434+
state.sessionKey = key;
435+
pane.sessionKey = key;
436+
pane.catalogCursor = "cursor-1";
437+
438+
const progressed = await pane.loadCatalogSession(
439+
{ catalogId: "claude", hostId: "gateway:local", threadId: "thread-1" },
440+
true,
441+
);
442+
443+
expect(progressed).toBe(false);
444+
// Cursor cleared → hasOlderMessages() is false, so the observer will not refire.
445+
expect(pane.catalogCursor).toBeUndefined();
446+
});
447+
448+
it("keeps paging when an advancing older page renders nothing new", async () => {
449+
const readPage: SessionsCatalogReadResult = {
450+
hostId: "gateway:local",
451+
threadId: "thread-1",
452+
// A page of only unsupported/empty items renders nothing but still advances
453+
// the cursor: older renderable history may sit behind it, so paging continues.
454+
items: [{ id: "x1", type: "other" }],
455+
nextCursor: "cursor-2",
456+
};
457+
const client = {
458+
request: vi.fn(async () => readPage),
459+
} as unknown as GatewayBrowserClient;
460+
const { pane, state } = createTestChatPane({ client, sessions: {} as SessionCapability });
461+
const key = "catalog:claude:gateway%3Alocal:thread-1";
462+
state.sessionKey = key;
463+
pane.sessionKey = key;
464+
pane.catalogCursor = "cursor-1";
465+
466+
const progressed = await pane.loadCatalogSession(
467+
{ catalogId: "claude", hostId: "gateway:local", threadId: "thread-1" },
468+
true,
469+
);
470+
471+
expect(progressed).toBe(true);
472+
expect(pane.catalogCursor).toBe("cursor-2");
473+
});
474+
475+
it("exhausts pagination when an older read cycles back to a visited cursor", async () => {
476+
const readPage: SessionsCatalogReadResult = {
477+
hostId: "gateway:local",
478+
threadId: "thread-1",
479+
items: [{ id: "x1", type: "other" }],
480+
// Cursor points back to one already visited this session: a c1 -> c2 -> c1
481+
// cycle that would otherwise loop forever on empty pages.
482+
nextCursor: "cursor-1",
483+
};
484+
const client = {
485+
request: vi.fn(async () => readPage),
486+
} as unknown as GatewayBrowserClient;
487+
const { pane, state } = createTestChatPane({ client, sessions: {} as SessionCapability });
488+
const key = "catalog:claude:gateway%3Alocal:thread-1";
489+
state.sessionKey = key;
490+
pane.sessionKey = key;
491+
pane.catalogCursor = "cursor-2";
492+
pane.olderCursorsSeen.add("cursor-1");
493+
494+
const progressed = await pane.loadCatalogSession(
495+
{ catalogId: "claude", hostId: "gateway:local", threadId: "thread-1" },
496+
true,
497+
);
498+
499+
expect(progressed).toBe(false);
500+
expect(pane.catalogCursor).toBeUndefined();
501+
});
502+
503+
it("re-arms a failed older-page load only after another user scroll", () => {
504+
const client = { request: vi.fn() } as unknown as GatewayBrowserClient;
505+
const { pane, state } = createTestChatPane({ client, sessions: {} as SessionCapability });
506+
state.handleChatScroll = vi.fn();
507+
pane.historyAutoLoadBlocked = true;
508+
pane.syncHistoryObserver = vi.fn();
509+
const event = new Event("scroll");
510+
511+
pane.handleTranscriptScroll(event);
512+
513+
expect(pane.historyAutoLoadBlocked).toBe(false);
514+
expect(pane.syncHistoryObserver).toHaveBeenCalledOnce();
515+
expect(state.handleChatScroll).toHaveBeenCalledWith(event);
516+
});
517+
518+
it("re-arms a blocked auto-load when the manual fallback is clicked", async () => {
519+
const client = { request: vi.fn() } as unknown as GatewayBrowserClient;
520+
const { pane, state } = createTestChatPane({ client, sessions: {} as SessionCapability });
521+
// A short (non-scrollable) thread cannot emit a scroll event, so a blocked
522+
// auto-load must be recoverable through the fallback's loadOlderMessages call.
523+
state.sessionKey = "catalog:claude:gateway%3Alocal:thread-1";
524+
pane.historyAutoLoadBlocked = true;
525+
pane.loadCatalogSession = vi.fn(async () => false);
526+
pane.hasOlderMessages = vi.fn(() => true);
527+
528+
await pane.loadOlderMessages();
529+
530+
// loadOlderMessages clears the block on entry, so the retry is not stranded.
531+
expect(pane.loadCatalogSession).toHaveBeenCalledOnce();
532+
expect(pane.historyAutoLoadBlocked).toBe(true);
533+
});
351534
});
352535

353536
describe("chat pane task suggestion lifecycle", () => {

0 commit comments

Comments
 (0)