Skip to content

Commit 89f6ab1

Browse files
authored
fix(ui): stop duplicate dashboard session queries (#124968)
1 parent e448c6b commit 89f6ab1

12 files changed

Lines changed: 694 additions & 303 deletions

‎ui/src/components/session-data-controller-events.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ type SidebarSessionListOwner = {
2020
requestSessionDataUpdate(): void;
2121
};
2222

23+
function filteredSidebarSessionQuery(agentId: string, archivedFilter: SidebarSessionStatusFilter) {
24+
return {
25+
agentId,
26+
archivedFilter,
27+
limit: SIDEBAR_AGENT_SESSION_LIST_LIMIT,
28+
includeDerivedTitles: true,
29+
includeLastMessage: true,
30+
} as const;
31+
}
32+
2333
export function publishSidebarSessionList(
2434
owner: SidebarSessionListOwner,
2535
snapshot: SessionListSnapshot,
@@ -43,7 +53,7 @@ export function subscribeFilteredSidebarSessions(
4353
archivedFilter: Exclude<SidebarSessionStatusFilter, "active">,
4454
isCurrent: () => boolean,
4555
): () => void {
46-
const scope = { agentId, archivedFilter };
56+
const scope = filteredSidebarSessionQuery(agentId, archivedFilter);
4757
const apply = (snapshot: SessionListSnapshot) => {
4858
if (!isCurrent()) {
4959
return;
@@ -85,11 +95,7 @@ export function refreshSidebarSessionList(
8595
return Promise.resolve();
8696
}
8797
return owner.context.sessions.refreshList({
88-
agentId,
89-
archivedFilter,
90-
limit: SIDEBAR_AGENT_SESSION_LIST_LIMIT,
91-
includeDerivedTitles: true,
92-
includeLastMessage: true,
98+
...filteredSidebarSessionQuery(agentId, archivedFilter),
9399
...(append && typeof offset === "number" ? { offset, append: true } : {}),
94100
force: true,
95101
});
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
// Control UI E2E proves dashboard tabs do not multiply server-owned session-list demand.
2+
import type { Page } from "playwright";
3+
import { expect, it } from "vitest";
4+
import {
5+
installMockGateway,
6+
waitForControlUiRoute,
7+
type MockGatewayControls,
8+
} from "../test-helpers/control-ui-e2e.ts";
9+
import { createControlUiE2eSuite } from "./control-ui-e2e-suite.test-support.ts";
10+
11+
const suite = createControlUiE2eSuite({
12+
name: "Control UI dashboard session-list demand",
13+
});
14+
15+
const DASHBOARD_REQUEST_PARAMS = {
16+
archived: "all",
17+
boardFace: "dashboard",
18+
configuredAgentsOnly: true,
19+
includeGlobal: true,
20+
includeUnknown: true,
21+
limit: 50,
22+
} as const;
23+
24+
function sessionsResult(key: string, label: string, updatedAt: number) {
25+
return {
26+
count: 1,
27+
defaults: { contextTokens: null, model: null, modelProvider: null },
28+
path: "",
29+
sessions: [
30+
{
31+
boardFace: "dashboard",
32+
key,
33+
kind: "direct",
34+
label,
35+
updatedAt,
36+
},
37+
],
38+
ts: updatedAt,
39+
};
40+
}
41+
42+
function isDashboardRequest(request: { params?: unknown }): boolean {
43+
return (
44+
typeof request.params === "object" &&
45+
request.params !== null &&
46+
"boardFace" in request.params &&
47+
request.params.boardFace === "dashboard"
48+
);
49+
}
50+
51+
async function requestCounts(gateways: MockGatewayControls[]) {
52+
const requests = (
53+
await Promise.all(gateways.map((gateway) => gateway.getRequests("sessions.list")))
54+
).flat();
55+
const dashboard = requests.filter(isDashboardRequest).length;
56+
return { canonical: requests.length - dashboard, dashboard, total: requests.length };
57+
}
58+
59+
suite.define(() => {
60+
it("keeps dashboard query demand at one request per real browser tab", async () => {
61+
const context = await suite.browser.newContext({ viewport: { height: 800, width: 1200 } });
62+
const tabs: Array<{
63+
gateway: MockGatewayControls;
64+
page: Page;
65+
canonicalLabel: string;
66+
dashboardLabel: string;
67+
updatedDashboardLabel: string;
68+
}> = [];
69+
try {
70+
for (const index of [1, 2]) {
71+
const page = await context.newPage();
72+
const dashboardLabel = `Dashboard tab ${index}`;
73+
const gateway = await installMockGateway(page, {
74+
deferredMethods: ["sessions.list"],
75+
methodResponses: {
76+
"sessions.list": {
77+
cases: [
78+
{
79+
match: { archived: "all", boardFace: "dashboard" },
80+
response: sessionsResult(`agent:main:dashboard-${index}`, dashboardLabel, index),
81+
},
82+
],
83+
},
84+
},
85+
});
86+
const canonicalLabel = `Older canonical tab ${index}`;
87+
const updatedDashboardLabel = `Updated dashboard tab ${index}`;
88+
tabs.push({ gateway, page, canonicalLabel, dashboardLabel, updatedDashboardLabel });
89+
}
90+
91+
await Promise.all(
92+
tabs.map(async ({ gateway, page }) => {
93+
await page.goto(suite.server.baseUrl);
94+
const canonical = await gateway.waitForRequest("sessions.list");
95+
expect(isDashboardRequest(canonical)).toBe(false);
96+
await page.waitForFunction(() => {
97+
const app = document.querySelector("openclaw-app") as HTMLElement & {
98+
runtime?: { context: { agents: { state: { agentsList: unknown } } } };
99+
};
100+
return app.runtime?.context.agents.state.agentsList != null;
101+
});
102+
await page.evaluate(() => {
103+
const app = document.querySelector("openclaw-app") as HTMLElement & {
104+
runtime?: {
105+
context: {
106+
navigate: (routeId: string) => void;
107+
agentSelection: {
108+
state: { scopeId: string | null };
109+
setScope: (agentId: string | null) => void;
110+
};
111+
};
112+
};
113+
};
114+
if (!app.runtime) {
115+
throw new Error("OpenClaw application runtime is unavailable");
116+
}
117+
app.runtime.context.agentSelection.setScope(null);
118+
if (app.runtime.context.agentSelection.state.scopeId !== null) {
119+
throw new Error("Control UI did not enter all-agent scope");
120+
}
121+
app.runtime.context.navigate("dashboards");
122+
});
123+
await waitForControlUiRoute(page, { pathname: "/dashboards", routeId: "dashboards" });
124+
}),
125+
);
126+
await Promise.all(
127+
tabs.map(({ page, dashboardLabel }) =>
128+
page.getByText(dashboardLabel, { exact: true }).waitFor(),
129+
),
130+
);
131+
expect(await requestCounts(tabs.map(({ gateway }) => gateway))).toEqual({
132+
canonical: 2,
133+
dashboard: 2,
134+
total: 4,
135+
});
136+
for (const tabGateway of tabs.map(({ gateway }) => gateway)) {
137+
const dashboardRequest = (await tabGateway.getRequests("sessions.list")).find(
138+
isDashboardRequest,
139+
);
140+
expect(dashboardRequest?.params).toEqual(DASHBOARD_REQUEST_PARAMS);
141+
expect(dashboardRequest?.params).not.toHaveProperty("agentId");
142+
}
143+
await Promise.all(
144+
tabs.map(async ({ canonicalLabel, gateway, page }, index) => {
145+
await gateway.resolveDeferred(
146+
"sessions.list",
147+
sessionsResult(`agent:main:canonical-${index + 1}`, canonicalLabel, 10 + index),
148+
);
149+
await page.getByText(canonicalLabel, { exact: true }).first().waitFor();
150+
}),
151+
);
152+
153+
expect(await requestCounts(tabs.map(({ gateway }) => gateway))).toEqual({
154+
canonical: 2,
155+
dashboard: 2,
156+
total: 4,
157+
});
158+
159+
const beforeWave = await Promise.all(
160+
tabs.map(({ gateway }) => gateway.getRequests("sessions.list")),
161+
);
162+
await Promise.all(
163+
tabs.map(async ({ gateway, updatedDashboardLabel }, index) => {
164+
await gateway.setMethodResponse("sessions.list", {
165+
cases: [
166+
{
167+
match: { archived: "all", boardFace: "dashboard" },
168+
response: sessionsResult(
169+
`agent:main:updated-dashboard-${index + 1}`,
170+
updatedDashboardLabel,
171+
20 + index,
172+
),
173+
},
174+
],
175+
});
176+
await gateway.emitGatewayEvent("sessions.changed", {
177+
agentId: "main",
178+
key: `agent:main:changed-${index + 1}`,
179+
kind: "direct",
180+
reason: "update",
181+
sessionKey: `agent:main:changed-${index + 1}`,
182+
updatedAt: 20 + index,
183+
});
184+
}),
185+
);
186+
await Promise.all(
187+
tabs.map(({ gateway }, index) =>
188+
expect
189+
.poll(async () => {
190+
const added = (await gateway.getRequests("sessions.list")).slice(
191+
beforeWave[index]?.length ?? 0,
192+
);
193+
const dashboard = added.filter(isDashboardRequest).length;
194+
return { canonical: added.length - dashboard, dashboard };
195+
})
196+
.toEqual({ canonical: 1, dashboard: 1 }),
197+
),
198+
);
199+
200+
const afterWave = await Promise.all(
201+
tabs.map(({ gateway }) => gateway.getRequests("sessions.list")),
202+
);
203+
for (const [index, requests] of afterWave.entries()) {
204+
const added = requests.slice(beforeWave[index]?.length ?? 0);
205+
expect(added.filter(isDashboardRequest)).toHaveLength(1);
206+
expect(added.filter((request) => !isDashboardRequest(request))).toHaveLength(1);
207+
}
208+
const dashboardRequests = afterWave.flat().filter(isDashboardRequest);
209+
expect(dashboardRequests).toHaveLength(4);
210+
for (const request of dashboardRequests) {
211+
expect(request.params).toEqual(DASHBOARD_REQUEST_PARAMS);
212+
expect(request.params).not.toHaveProperty("agentId");
213+
}
214+
await Promise.all(
215+
tabs.map(({ page, updatedDashboardLabel }) =>
216+
page.getByText(updatedDashboardLabel, { exact: true }).waitFor(),
217+
),
218+
);
219+
} finally {
220+
await context.close();
221+
}
222+
});
223+
});

‎ui/src/lib/sessions/index-list.test.ts‎

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ import type { GatewayBrowserClient } from "../../api/gateway.ts";
33
import type { SessionsListResult } from "../../api/types.ts";
44
import { createSessionCapability } from "./index.ts";
55

6-
type ListParams = { archived?: true | "all"; limit?: number; offset?: number };
6+
type ListParams = {
7+
agentId?: string;
8+
archived?: true | "all";
9+
boardFace?: "chat" | "dashboard";
10+
includeDerivedTitles?: boolean;
11+
includeLastMessage?: boolean;
12+
limit?: number;
13+
offset?: number;
14+
};
715

816
function listResult(keys: string[] = [], totalCount = keys.length, offset = 0): SessionsListResult {
917
const nextOffset = offset + keys.length;
@@ -112,8 +120,8 @@ describe("session list requests", () => {
112120
return listResult(keys.slice(offset, offset + (params?.limit ?? 50)), 4, offset);
113121
});
114122
const { sessions } = sessionHarness(request);
115-
const archivedScope = { agentId: "main", archivedFilter: "archived" as const };
116-
const allScope = { agentId: "main", archivedFilter: "all" as const };
123+
const archivedScope = { agentId: "main", archivedFilter: "archived" as const, limit: 2 };
124+
const allScope = { agentId: "main", archivedFilter: "all" as const, limit: 1 };
117125
const observeSnapshot = vi.fn();
118126
const unsubscribe = sessions.subscribeList(archivedScope, observeSnapshot);
119127
await sessions.refreshList({ agentId: "main", limit: 1, force: true });
@@ -135,6 +143,68 @@ describe("session list requests", () => {
135143
sessions.dispose();
136144
});
137145

146+
it("keeps dashboard and sidebar queries distinct without inventing a dashboard agent", async () => {
147+
const request = vi.fn(async (_method: string, params?: ListParams) =>
148+
listResult([
149+
params?.boardFace === "dashboard"
150+
? "agent:main:dashboard-result"
151+
: "agent:main:sidebar-result",
152+
]),
153+
);
154+
const { sessions } = sessionHarness(request);
155+
const dashboardQuery = {
156+
limit: 50,
157+
boardFace: "dashboard" as const,
158+
archivedFilter: "all" as const,
159+
};
160+
const sidebarQuery = {
161+
agentId: "main",
162+
limit: 60,
163+
includeDerivedTitles: true,
164+
includeLastMessage: true,
165+
archivedFilter: "all" as const,
166+
};
167+
const stopDashboard = sessions.subscribeList(dashboardQuery, () => undefined);
168+
const stopSidebar = sessions.subscribeList(sidebarQuery, () => undefined);
169+
170+
await sessions.refreshList({
171+
...dashboardQuery,
172+
includeGlobal: true,
173+
includeUnknown: true,
174+
configuredAgentsOnly: true,
175+
includeDerivedTitles: false,
176+
includeLastMessage: false,
177+
force: true,
178+
});
179+
await sessions.refreshList({ ...sidebarQuery, force: true });
180+
181+
expect(sessions.listSnapshot(dashboardQuery).result?.sessions[0]?.key).toBe(
182+
"agent:main:dashboard-result",
183+
);
184+
expect(sessions.listSnapshot(sidebarQuery).result?.sessions[0]?.key).toBe(
185+
"agent:main:sidebar-result",
186+
);
187+
expect(request.mock.calls[0]?.[1]).toEqual({
188+
includeGlobal: true,
189+
includeUnknown: true,
190+
configuredAgentsOnly: true,
191+
limit: 50,
192+
archived: "all",
193+
boardFace: "dashboard",
194+
});
195+
expect(request.mock.calls[0]?.[1]).not.toHaveProperty("agentId");
196+
expect(request.mock.calls[1]?.[1]).toMatchObject({
197+
agentId: "main",
198+
archived: "all",
199+
includeDerivedTitles: true,
200+
includeLastMessage: true,
201+
limit: 60,
202+
});
203+
stopDashboard();
204+
stopSidebar();
205+
sessions.dispose();
206+
});
207+
138208
it("retires stale filtered snapshots across same-client reconnects without losing subscribers", async () => {
139209
let resolveStale!: (result: SessionsListResult) => void;
140210
const staleResult = new Promise<SessionsListResult>((resolve) => {

0 commit comments

Comments
 (0)