Skip to content

Commit 22d16f7

Browse files
committed
fix(ui): surface session sharing errors without leaking across chats
1 parent 1704ea5 commit 22d16f7

7 files changed

Lines changed: 146 additions & 17 deletions

File tree

‎ui/src/e2e/session-ownership.e2e.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { mkdir } from "node:fs/promises";
33
import path from "node:path";
44
import { chromium, type Browser, type Page } from "playwright";
5+
import { expect as expectBrowser } from "playwright/test";
56
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
67
import {
78
canRunPlaywrightChromium,
@@ -369,6 +370,49 @@ describeControlUiE2e("Control UI session ownership", () => {
369370
expect(await gateway.getRequests("session.visibility.set")).toHaveLength(1);
370371
});
371372

373+
it("keeps rejected visibility-only sharing changes visible after the menu closes", async () => {
374+
const context = await browser.newContext({ viewport: { height: 800, width: 1200 } });
375+
const currentPage = await context.newPage();
376+
page = currentPage;
377+
const sessions = draftSessionsList();
378+
const ownerSession = sessions.sessions[0];
379+
if (!ownerSession) {
380+
throw new Error("expected owner draft fixture");
381+
}
382+
Object.assign(ownerSession, { sharingRole: "owner" });
383+
const gateway = await installMockGateway(currentPage, {
384+
sessionKey: "agent:main:ada",
385+
allowedSessionVisibilities: ["shared", "draft"],
386+
deferredMethods: ["session.visibility.set"],
387+
featureMethods: ["chat.metadata", "chat.startup", "session.visibility.set"],
388+
operatorScopes: ["operator.write"],
389+
historyMessages: [{ role: "assistant", content: [{ type: "text", text: "Ready." }] }],
390+
methodResponses: { "sessions.list": sessions },
391+
});
392+
393+
await currentPage.goto(`${server?.baseUrl ?? ""}chat`);
394+
await currentPage.getByText("Ready.", { exact: true }).waitFor();
395+
await currentPage.getByRole("button", { name: "Thread sharing" }).click();
396+
const dropdown = currentPage.locator(".chat-pane__sharing-menu");
397+
await expect.poll(() => dropdown.getAttribute("open")).not.toBeNull();
398+
expect(await dropdown.locator(".chat-pane__sharing-title").count()).toBe(1);
399+
await currentPage.getByText("Publish draft", { exact: true }).click();
400+
await gateway.waitForRequest("session.visibility.set");
401+
await expect.poll(() => dropdown.getAttribute("open")).toBeNull();
402+
expect(await gateway.getRequests("session.members.list")).toHaveLength(0);
403+
404+
const message = "visibility change rejected";
405+
await gateway.rejectDeferred("session.visibility.set", {
406+
code: "INVALID_REQUEST",
407+
message,
408+
});
409+
const alert = currentPage.getByRole("alert").filter({ hasText: message });
410+
await expectBrowser(alert).toBeVisible();
411+
412+
await currentPage.getByRole("button", { name: "Thread sharing" }).click();
413+
await expectBrowser(dropdown.locator(".chat-pane__sharing-status--error")).toBeVisible();
414+
});
415+
372416
it("lets a read-scoped owner inspect sharing but blocks mutations", async () => {
373417
const context = await browser.newContext({ viewport: { height: 800, width: 1200 } });
374418
const currentPage = await context.newPage();

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,8 @@ export abstract class ChatPaneHeader extends ChatPaneContext {
468468
if (!this.state) {
469469
return;
470470
}
471-
this.state.chatError = error instanceof Error ? error.message : String(error);
471+
this.state.lastError = error instanceof Error ? error.message : String(error);
472+
this.state.chatError = this.state.lastError;
472473
this.state.requestUpdate?.();
473474
}
474475

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,8 @@ describe.each(mutations)("chat pane $name mutation connection ownership", (mutat
388388
expect(replacement.request).not.toHaveBeenCalled();
389389
expect(replacement.sessions.refreshReplacement).not.toHaveBeenCalled();
390390
expect(pane.sessionSharingStates.get(replacement.cacheKey)).toBe(replacement.sharingState);
391+
expect(state.lastError).toBeNull();
392+
expect(state.chatError).toBeNull();
391393
},
392394
);
393395

@@ -435,9 +437,53 @@ describe.each(mutations)("chat pane $name mutation connection ownership", (mutat
435437
expect(request).toHaveBeenCalledTimes(1);
436438
expect(sessions.refreshReplacement).not.toHaveBeenCalled();
437439
expect(pane.sessionSharingStates.get(cacheKey)).toBe(replacementState);
440+
expect(state.lastError).toBeNull();
441+
expect(state.chatError).toBeNull();
438442
},
439443
);
440444

445+
it("keeps a previous-session failure out of the newly selected session", async () => {
446+
const response = createDeferred<unknown>();
447+
const request = vi.fn((method: string) => {
448+
if (method !== mutation.method) {
449+
throw new Error(`unexpected request: ${method}`);
450+
}
451+
return response.promise;
452+
});
453+
const sessions = {
454+
refreshReplacement: vi.fn(),
455+
} as unknown as SessionCapability;
456+
const { pane: testPane, state } = createSharingTestChatPane({
457+
client: { request } as unknown as GatewayBrowserClient,
458+
sessions,
459+
});
460+
const pane = testPane as SharingPane;
461+
const previous = sessionRow();
462+
const selected = {
463+
...sessionRow(),
464+
key: "agent:main:selected",
465+
sessionId: "session-selected",
466+
};
467+
state.sessionsResult = {
468+
...sharingSessionsResult(previous),
469+
count: 2,
470+
sessions: [previous, selected],
471+
};
472+
const pending = mutation.invoke(pane, previous);
473+
state.sessionKey = selected.key;
474+
475+
response.reject(new Error(`${mutation.name} failed after session switch`));
476+
await pending;
477+
478+
expect(pane.sessionSharingStates.get(pane.sessionSharingCacheKey(previous.key))).toMatchObject({
479+
loading: false,
480+
error: `Error: ${mutation.name} failed after session switch`,
481+
});
482+
expect(state.lastError).toBeNull();
483+
expect(state.chatError).toBeNull();
484+
expect(sessions.refreshReplacement).not.toHaveBeenCalled();
485+
});
486+
441487
it("preserves the current connection failure in the sharing cache", async () => {
442488
const request = vi.fn(async (method: string) => {
443489
if (method === mutation.method) {
@@ -448,7 +494,7 @@ describe.each(mutations)("chat pane $name mutation connection ownership", (mutat
448494
const sessions = {
449495
refreshReplacement: vi.fn(),
450496
} as unknown as SessionCapability;
451-
const { pane: testPane } = createSharingTestChatPane({
497+
const { pane: testPane, state } = createSharingTestChatPane({
452498
client: { request } as unknown as GatewayBrowserClient,
453499
sessions,
454500
});
@@ -461,6 +507,8 @@ describe.each(mutations)("chat pane $name mutation connection ownership", (mutat
461507
loading: false,
462508
error: `Error: ${mutation.name} failed`,
463509
});
510+
expect(state.lastError).toBe(`${mutation.name} failed`);
511+
expect(state.chatError).toBe(state.lastError);
464512
expect(sessions.refreshReplacement).not.toHaveBeenCalled();
465513
});
466514
});

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,19 @@ export abstract class ChatPaneSharing extends ChatPaneBase {
136136
}
137137
}
138138

139+
protected publishSharingFailure(cacheKey: string, sessionKey: string, error: unknown): void {
140+
this.setSessionSharingState(cacheKey, {
141+
...(this.sessionSharingStates.get(cacheKey) ?? { loading: false }),
142+
loading: false,
143+
error: String(error),
144+
});
145+
// Sharing errors stay with their session; the visible page slot belongs
146+
// only to the selected session after same-connection navigation.
147+
if (areUiSessionKeysEquivalent(this.state?.sessionKey, sessionKey)) {
148+
this.publishHeaderError(error);
149+
}
150+
}
151+
139152
protected async setSessionVisibility(
140153
row: GatewaySessionRow,
141154
visibility: SessionVisibility,
@@ -181,11 +194,7 @@ export abstract class ChatPaneSharing extends ChatPaneBase {
181194
) {
182195
return;
183196
}
184-
this.setSessionSharingState(cacheKey, {
185-
...(this.sessionSharingStates.get(cacheKey) ?? { loading: false }),
186-
loading: false,
187-
error: String(error),
188-
});
197+
this.publishSharingFailure(cacheKey, currentRow.key, error);
189198
}
190199
}
191200

@@ -238,11 +247,7 @@ export abstract class ChatPaneSharing extends ChatPaneBase {
238247
) {
239248
return;
240249
}
241-
this.setSessionSharingState(cacheKey, {
242-
...(this.sessionSharingStates.get(cacheKey) ?? { loading: false }),
243-
loading: false,
244-
error: String(error),
245-
});
250+
this.publishSharingFailure(cacheKey, currentRow.key, error);
246251
}
247252
}
248253

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ describe("chat pane header state", () => {
365365
} satisfies GatewaySessionRow;
366366
pane.handleHeaderMenuAction("reveal", session, "/src/openclaw", null);
367367
await vi.waitFor(() => expect(state.chatError).toBe("No desktop available."));
368+
expect(state.lastError).toBe(state.chatError);
368369
});
369370
});
370371

‎ui/src/pages/chat/components/chat-session-sharing.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,34 @@ describe("chat session sharing menu", () => {
260260
expect(onOpen).toHaveBeenCalledOnce();
261261
expect(onVisibilityChange).toHaveBeenCalledWith("read-only");
262262
});
263+
264+
it.each([
265+
{ name: "visibility-only", membersAvailable: false },
266+
{ name: "member-enabled", membersAvailable: true },
267+
])("shows rejected sharing changes for $name Gateways", ({ membersAvailable }) => {
268+
const root = mount(
269+
renderChatSessionSharing({
270+
session: {
271+
key: "agent:main:main",
272+
kind: "direct",
273+
updatedAt: 1,
274+
visibility: "shared",
275+
sharingRole: "owner",
276+
},
277+
state: { loading: false, error: "Visibility update rejected" },
278+
allowedVisibilities: ["shared", "read-only"],
279+
membersAvailable,
280+
onOpen: vi.fn(),
281+
onVisibilityChange: vi.fn(),
282+
onMemberChange: vi.fn(),
283+
}),
284+
);
285+
286+
expect(root.querySelector(".chat-pane__sharing-status--error")?.textContent).toContain(
287+
"Visibility update rejected",
288+
);
289+
expect(root.querySelectorAll(".chat-pane__sharing-title")).toHaveLength(
290+
membersAvailable ? 2 : 1,
291+
);
292+
});
263293
});

‎ui/src/pages/chat/components/chat-session-sharing.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,13 @@ export function renderChatSessionSharing(props: ChatSessionSharingProps) {
172172
: html`<div class="chat-pane__sharing-status">
173173
${t("chat.sessionSharing.noPeople")}
174174
</div>`}
175-
${props.state?.error
176-
? html`<div class="chat-pane__sharing-status chat-pane__sharing-status--error">
177-
${props.state.error}
178-
</div>`
179-
: nothing}
180175
`
181176
: nothing}
177+
${props.state?.error
178+
? html`<div class="chat-pane__sharing-status chat-pane__sharing-status--error">
179+
${props.state.error}
180+
</div>`
181+
: nothing}
182182
</wa-dropdown>
183183
`;
184184
}

0 commit comments

Comments
 (0)