Skip to content

Commit 0bdf11c

Browse files
authored
fix(ui): surface companion clear failures (#123724)
* fix(ui): surface companion clear failures * fix(ui): scope companion reset errors * fix(ui): preserve companion clear async contract
1 parent f22e499 commit 0bdf11c

3 files changed

Lines changed: 194 additions & 28 deletions

File tree

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
import { mkdir } from "node:fs/promises";
2+
import path from "node:path";
3+
import type { Locator, Page } from "playwright";
4+
import { expect, it } from "vitest";
5+
import {
6+
installMockGateway,
7+
navigateToControlUiSession,
8+
type MockGatewayControls,
9+
} from "../test-helpers/control-ui-e2e.ts";
10+
import { createControlUiE2eSuite } from "./control-ui-e2e-suite.test-support.ts";
11+
12+
const suite = createControlUiE2eSuite({
13+
name: "session companion clear",
14+
startServerBeforeBrowser: true,
15+
});
16+
17+
const artifactDir = process.env.OPENCLAW_UI_E2E_ARTIFACT_DIR?.trim();
18+
const answer = "Keep this companion answer visible until the reset succeeds.";
19+
const initiatingSessionKey = "agent:main:companion-clear";
20+
const nextSessionKey = "agent:main:companion-next";
21+
const resetError = "Companion reset unavailable during reconnect";
22+
23+
type CompanionSurface = {
24+
companion: Locator;
25+
gateway: MockGatewayControls;
26+
menu: Locator;
27+
page: Page;
28+
};
29+
30+
async function withCompanion(run: (surface: CompanionSurface) => Promise<void>): Promise<void> {
31+
if (artifactDir) {
32+
await mkdir(artifactDir, { recursive: true });
33+
}
34+
await suite.withPage(
35+
{
36+
locale: "en-US",
37+
serviceWorkers: "block",
38+
viewport: { height: 800, width: 1200 },
39+
...(artifactDir
40+
? { recordVideo: { dir: artifactDir, size: { height: 800, width: 1200 } } }
41+
: {}),
42+
},
43+
async ({ page }) => {
44+
const gateway = await installMockGateway(page, {
45+
methodResponses: {
46+
"sessions.companion.reset": {
47+
__mockError: { code: "UNAVAILABLE", message: resetError },
48+
},
49+
"sessions.companion.state": {
50+
cases: [
51+
{
52+
match: { sessionKey: initiatingSessionKey },
53+
response: {
54+
exchanges: [{ question: "What changed?", answer, ts: Date.now() - 1_000 }],
55+
},
56+
},
57+
{ match: { sessionKey: nextSessionKey }, response: { exchanges: [] } },
58+
],
59+
},
60+
"sessions.list": {
61+
count: 2,
62+
defaults: { contextTokens: null, model: "gpt-5.5", modelProvider: "openai" },
63+
path: "",
64+
sessions: [
65+
{ key: initiatingSessionKey, kind: "direct", label: "Original", updatedAt: 2 },
66+
{ key: nextSessionKey, kind: "direct", label: "Next", updatedAt: 1 },
67+
],
68+
ts: Date.now(),
69+
},
70+
},
71+
sessionKey: initiatingSessionKey,
72+
});
73+
74+
await page.goto(`${suite.server.baseUrl}chat`);
75+
const stateRequest = await gateway.waitForRequest("sessions.companion.state");
76+
expect(stateRequest.params).toEqual({
77+
agentId: "main",
78+
sessionKey: initiatingSessionKey,
79+
});
80+
const companion = page.locator("openclaw-chat-session-rail");
81+
await companion.locator(".chat-session-rail__expand").click();
82+
await companion.getByText(answer, { exact: true }).waitFor();
83+
const menu = companion.locator("wa-dropdown.chat-session-rail__menu");
84+
await run({ companion, gateway, menu, page });
85+
},
86+
);
87+
}
88+
89+
async function clearCompanion(menu: Locator): Promise<void> {
90+
await menu.getByRole("button", { name: "More companion actions" }).click();
91+
await menu.locator('wa-dropdown-item[value="clear"]').click();
92+
}
93+
94+
suite.define(() => {
95+
it("shows a reset failure without clearing the thread, then clears after a successful retry", async () => {
96+
await withCompanion(async ({ companion, gateway, menu, page }) => {
97+
await clearCompanion(menu);
98+
await gateway.waitForRequest("sessions.companion.reset");
99+
100+
const alert = page.getByRole("alert").filter({ hasText: resetError });
101+
await alert.waitFor({ state: "visible" });
102+
await companion.getByText(answer, { exact: true }).waitFor();
103+
if (artifactDir) {
104+
await page.screenshot({
105+
fullPage: true,
106+
path: path.join(artifactDir, "reset-failure.png"),
107+
});
108+
}
109+
110+
await alert.getByRole("button", { name: "Dismiss error" }).click();
111+
await gateway.setMethodResponse("sessions.companion.reset", { ok: true });
112+
await clearCompanion(menu);
113+
114+
await expect
115+
.poll(async () => (await gateway.getRequests("sessions.companion.reset")).length)
116+
.toBe(2);
117+
await expect.poll(() => companion.getByText(answer, { exact: true }).count()).toBe(0);
118+
expect(await page.getByRole("alert").count()).toBe(0);
119+
if (artifactDir) {
120+
await page.screenshot({
121+
fullPage: true,
122+
path: path.join(artifactDir, "reset-success.png"),
123+
});
124+
}
125+
});
126+
});
127+
128+
it("does not publish a delayed reset rejection into a newly selected session", async () => {
129+
await withCompanion(async ({ gateway, menu, page }) => {
130+
await gateway.deferNext("sessions.companion.reset");
131+
await clearCompanion(menu);
132+
await gateway.waitForRequest("sessions.companion.reset");
133+
134+
await navigateToControlUiSession(page, nextSessionKey);
135+
await gateway.rejectDeferred("sessions.companion.reset", {
136+
code: "UNAVAILABLE",
137+
message: resetError,
138+
});
139+
140+
const visiblePane = page.locator("openclaw-chat-pane.chat-pane-cache__pane--visible");
141+
expect(await visiblePane.getByRole("alert").filter({ hasText: resetError }).count()).toBe(0);
142+
if (artifactDir) {
143+
await page.screenshot({
144+
fullPage: true,
145+
path: path.join(artifactDir, "stale-reset-error-suppressed.png"),
146+
});
147+
}
148+
149+
await navigateToControlUiSession(page, initiatingSessionKey);
150+
const initiatingPane = page.locator("openclaw-chat-pane.chat-pane-cache__pane--visible");
151+
expect(await initiatingPane.getByRole("alert").filter({ hasText: resetError }).count()).toBe(
152+
0,
153+
);
154+
await initiatingPane
155+
.locator("openclaw-chat-session-rail")
156+
.getByText(answer, { exact: true })
157+
.waitFor();
158+
if (artifactDir) {
159+
await page.screenshot({
160+
fullPage: true,
161+
path: path.join(artifactDir, "initiating-thread-preserved.png"),
162+
});
163+
}
164+
});
165+
});
166+
});

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ import {
5757
ChatSessionCompanionThreads,
5858
requestSessionCompanionAnswer,
5959
requestSessionCompanionState,
60-
resetSessionCompanion,
6160
} from "./chat-session-companion.ts";
6261
import { ChatStateController } from "./chat-state-controller.ts";
6362
import type { ChatPageHost } from "./chat-state-host.ts";
@@ -409,16 +408,6 @@ export abstract class ChatPaneBase extends OpenClawLightDomElement {
409408
);
410409
}
411410

412-
protected readonly clearSessionCompanion = async () => {
413-
const state = this.state;
414-
if (!state?.connected || !state.client || !state.sessionKey) {
415-
return;
416-
}
417-
const agentId = resolveChatAgentId(state);
418-
await this.sessionCompanionThreads
419-
.reset(state.sessionKey, (key) => resetSessionCompanion(state.client!, key, agentId), agentId)
420-
.catch(() => undefined);
421-
};
422411
protected resetConfirmation:
423412
| {
424413
sessionKey: string;

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

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
CHAT_COMPOSER_TEXTAREA_SELECTOR,
2828
type ChatPaneConnectionScope,
2929
} from "./chat-pane-shared.ts";
30+
import { resetSessionCompanion } from "./chat-session-companion.ts";
3031
import type { ChatPageHost } from "./chat-state-host.ts";
3132
import { resolveChatAgentId } from "./chat-state-route.ts";
3233
import {
@@ -35,6 +36,26 @@ import {
3536
} from "./components/chat-session-sharing.ts";
3637

3738
export abstract class ChatPaneSharing extends ChatPaneBase {
39+
protected readonly clearSessionCompanion = async () => {
40+
const scope = this.captureConnectionScope();
41+
const key = scope?.state.sessionKey;
42+
if (!scope || !key) {
43+
return;
44+
}
45+
const agentId = resolveChatAgentId(scope.state);
46+
await this.sessionCompanionThreads
47+
.reset(key, (sessionKey) => resetSessionCompanion(scope.client, sessionKey, agentId), agentId)
48+
.catch((error: unknown) => {
49+
if (
50+
this.presented &&
51+
this.isConnectionScopeCurrent(scope) &&
52+
scope.state.sessionKey === key
53+
) {
54+
this.publishHeaderError(error);
55+
}
56+
});
57+
};
58+
3859
protected setSessionSharingState(cacheKey: string, state: ChatSessionSharingState): void {
3960
this.sessionSharingStates = new Map(this.sessionSharingStates).set(cacheKey, state);
4061
}
@@ -142,8 +163,7 @@ export abstract class ChatPaneSharing extends ChatPaneBase {
142163
loading: false,
143164
error: String(error),
144165
});
145-
// Sharing errors stay with their session; the visible page slot belongs
146-
// only to the selected session after same-connection navigation.
166+
// Sharing errors stay with their session; the visible slot belongs only to the selected session.
147167
if (areUiSessionKeysEquivalent(this.state?.sessionKey, sessionKey)) {
148168
this.publishHeaderError(error);
149169
}
@@ -252,33 +272,24 @@ export abstract class ChatPaneSharing extends ChatPaneBase {
252272
}
253273

254274
protected captureConnectionScope(): ChatPaneConnectionScope | null {
255-
const context = this.context;
256275
const state = this.state;
257-
const client = state?.client;
258-
if (
259-
!this.isConnected ||
260-
!state?.connected ||
261-
!client ||
262-
this.connectedClient !== client ||
263-
context.gateway.snapshot.phase !== "connected" ||
264-
context.gateway.snapshot.client !== client
265-
) {
276+
if (!state?.client) {
266277
return null;
267278
}
268-
return {
269-
context,
279+
const scope = {
280+
context: this.context,
270281
state,
271-
client,
282+
client: state.client,
272283
generation: this.connectionGeneration,
273-
sessions: context.sessions,
284+
sessions: this.context.sessions,
274285
};
286+
return this.isConnectionScopeCurrent(scope) ? scope : null;
275287
}
276288

277289
protected isConnectionScopeCurrent(scope: ChatPaneConnectionScope): boolean {
278290
return (
279291
this.isConnected &&
280292
this.context === scope.context &&
281-
this.context.sessions === scope.sessions &&
282293
this.state === scope.state &&
283294
scope.state.connected &&
284295
scope.state.client === scope.client &&

0 commit comments

Comments
 (0)