Skip to content

Commit 816ae22

Browse files
steipeteampagent
andauthored
fix(ui): recover channel setup after status check failure (#124680)
* fix(ui): recover Custodian channel status Amp-Thread-ID: https://ampcode.com/threads/T-01a00ae0-190d-718b-8a76-b75f3e8d1fae * fix(ui): invalidate channel status on reconnect Amp-Thread-ID: https://ampcode.com/threads/T-01a00ae0-190d-718b-8a76-b75f3e8d1fae * fix(ui): retain channel retry feedback Amp-Thread-ID: https://ampcode.com/threads/T-01a00ae0-190d-718b-8a76-b75f3e8d1fae * chore(ci): refresh channel retry validation --------- Co-authored-by: Amp <[email protected]>
1 parent fecb223 commit 816ae22

10 files changed

Lines changed: 279 additions & 24 deletions

‎ui/src/e2e/custodian-channel-onboarding.e2e.test.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,30 @@ describeControlUiE2e("Control UI Custodian channel onboarding mocked Gateway E2E
134134
path: path.join(artifactDir, "01-after-model-ready-continue-setup.png"),
135135
});
136136

137+
await gateway.deferNext("channels.status", { probe: false });
137138
await continueSetup.click();
138139
await waitForControlUiRoute(page, {
139140
pathname: "/custodian",
140141
routeId: "custodian",
141142
search: "?onboarding=1",
142143
});
144+
await gateway.waitForRequest("channels.status");
145+
await gateway.rejectDeferred("channels.status", {
146+
code: "UNAVAILABLE",
147+
message: "channel status temporarily unavailable",
148+
retryable: true,
149+
});
150+
const channelStatusError = page.getByRole("alert").filter({
151+
hasText: "Channel status is unavailable",
152+
});
153+
await channelStatusError.waitFor();
154+
await page.screenshot({
155+
animations: "disabled",
156+
path: path.join(artifactDir, "02-channel-status-error-before-retry.png"),
157+
});
158+
159+
await gateway.setMethodResponse("channels.status", emptyChannelSnapshot);
160+
await channelStatusError.getByRole("button", { name: "Retry" }).click();
143161
const channelNudge = page.locator(".custodian__nudge--channel-onboarding");
144162
await channelNudge.waitFor();
145163
await expect.poll(() => channelNudge.textContent()).toContain("The web app already works");
@@ -185,8 +203,9 @@ describeControlUiE2e("Control UI Custodian channel onboarding mocked Gateway E2E
185203
await page.setViewportSize({ height: 900, width: 1280 });
186204

187205
const channelStatusRequests = await gateway.getRequests("channels.status");
188-
expect(channelStatusRequests).toHaveLength(1);
206+
expect(channelStatusRequests).toHaveLength(2);
189207
expect(channelStatusRequests[0]?.params).toMatchObject({ probe: false });
208+
expect(channelStatusRequests[1]?.params).toMatchObject({ probe: false });
190209

191210
await page.getByRole("button", { name: "Set up a channel" }).click();
192211
await waitForControlUiRoute(page, {

‎ui/src/i18n/locales/en.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2602,6 +2602,8 @@ export const en: TranslationMap = {
26022602
"The web app already works. Add a channel only if you want to message OpenClaw from another service.",
26032603
channelSetupAction: "Set up a channel",
26042604
channelSetupDismiss: "Keep using the web app",
2605+
channelStatusErrorTitle: "Channel status is unavailable",
2606+
channelStatusErrorBody: "Retry the check, or keep using the web app without a channel.",
26052607
},
26062608
},
26072609
mcpServers: {

‎ui/src/lib/channels/index.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,42 @@ describe("channels controller DM pairing", () => {
619619
});
620620

621621
describe("channel refresh sequencing", () => {
622+
it("clears a failed request generation on reconnect so status can recover", async () => {
623+
const request = vi
624+
.fn()
625+
.mockRejectedValueOnce(new Error("status unavailable"))
626+
.mockResolvedValueOnce(createChannelsSnapshot("recovered"));
627+
const client = { request };
628+
let snapshot = { client, phase: "connected" };
629+
const listeners = new Set<(next: typeof snapshot) => void>();
630+
const channels = createChannelCapability({
631+
get snapshot() {
632+
return snapshot;
633+
},
634+
subscribe(listener: (next: typeof snapshot) => void) {
635+
listeners.add(listener);
636+
return () => listeners.delete(listener);
637+
},
638+
} as never);
639+
640+
await channels.refresh();
641+
expect(channels.state.channelsError).toBe("status unavailable");
642+
643+
snapshot = { client, phase: "reconnecting" };
644+
for (const listener of listeners) {
645+
listener(snapshot);
646+
}
647+
snapshot = { client, phase: "connected" };
648+
for (const listener of listeners) {
649+
listener(snapshot);
650+
}
651+
652+
expect(channels.state.channelsError).toBeNull();
653+
await channels.refresh();
654+
expect(channels.state.channelsSnapshot?.channelLabels.test).toBe("recovered");
655+
channels.dispose();
656+
});
657+
622658
it("rejects an in-flight channel snapshot after read access is revoked", async () => {
623659
const pending = createDeferred<ChannelsStatusSnapshot | null>();
624660
const request = vi.fn(() => pending.promise);

‎ui/src/lib/channels/index.ts‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ async function loadChannels(
202202
state.channelsRefreshSeq = refreshSeq;
203203
state.channelsLoading = true;
204204
state.channelsLoadingProbe = probe;
205-
state.channelsError = null;
206205
const refresh = (async () => {
207206
try {
208207
const res = await client.request<ChannelsStatusSnapshot | null>("channels.status", {
@@ -213,6 +212,7 @@ async function loadChannels(
213212
return;
214213
}
215214
state.channelsSnapshot = res;
215+
state.channelsError = null;
216216
state.channelsLastSuccess = Date.now();
217217
} catch (err) {
218218
if (!isCurrentChannelRefresh(state, client, refreshSeq)) {
@@ -654,11 +654,9 @@ export function createChannelCapability(gateway: ChannelGateway): ChannelCapabil
654654
state.channelsLoading = false;
655655
state.channelsLoadingProbe = null;
656656
state.channelsRefreshSeq = (state.channelsRefreshSeq ?? 0) + 1;
657-
if (!nextChannelReadAccess) {
658-
state.channelsSnapshot = null;
659-
state.channelsError = null;
660-
state.channelsLastSuccess = null;
661-
}
657+
state.channelsError = connected || !nextChannelReadAccess ? null : state.channelsError;
658+
state.channelsSnapshot = null;
659+
state.channelsLastSuccess = null;
662660
}
663661
if (clientChanged || connectionChanged || whatsappAdminAccessChanged) {
664662
lifecycle.whatsappEpoch += 1;

‎ui/src/pages/channels/channels-page.test.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,16 @@ function createGateway(): TestGateway {
6363
commandOwnerConfigured: true,
6464
limits: { pendingPerAccount: 3, ttlMs: 3_600_000 },
6565
}
66-
: {},
66+
: method === "channels.status"
67+
? {
68+
ts: 0,
69+
channelOrder: [],
70+
channelLabels: {},
71+
channels: {},
72+
channelAccounts: {},
73+
channelDefaultAccountId: {},
74+
}
75+
: {},
6776
),
6877
} as unknown as GatewayBrowserClient;
6978
const snapshot: ApplicationGatewaySnapshot = {

‎ui/src/pages/custodian/custodian-page.channel-onboarding.test.ts‎

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/* @vitest-environment jsdom */
22

33
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
4+
import { createDeferred } from "../../../../test/helpers/promise.js";
45
import type { ChannelsStatusSnapshot } from "../../api/types.ts";
6+
import { channelSnapshotEntryIsActive, createChannelCapability } from "../../lib/channels/index.ts";
57
import { waitForFast } from "../../test-helpers/wait-for.ts";
68
import { createContext, mountPage } from "./custodian-page.test-harness.ts";
79

@@ -100,6 +102,136 @@ describe("custodian channel onboarding", () => {
100102
expect(context.channels.refresh).toHaveBeenNthCalledWith(2, false);
101103
});
102104

105+
it("awaits fresh channel status after reconnecting with a stale successful snapshot and error", async () => {
106+
const freshStatus = createDeferred<ChannelsStatusSnapshot>();
107+
let statusRequestCount = 0;
108+
const request = vi.fn((method: string) => {
109+
if (method === "channels.status") {
110+
statusRequestCount += 1;
111+
if (statusRequestCount === 1) {
112+
return Promise.resolve(channelSnapshot());
113+
}
114+
if (statusRequestCount === 2) {
115+
return Promise.reject(new Error("status unavailable"));
116+
}
117+
return freshStatus.promise;
118+
}
119+
return Promise.resolve({
120+
sessionId: "control-ui-onboarding-00000000-0000-4000-8000-000000000001",
121+
reply: "Ready.",
122+
action: "none",
123+
});
124+
});
125+
const { context, setGatewaySnapshot } = createContext(request);
126+
const channels = createChannelCapability(context.gateway);
127+
Object.assign(context, { channels });
128+
await channels.refresh();
129+
await channels.refresh();
130+
expect(channels.state.channelsSnapshot).not.toBeNull();
131+
expect(channels.state.channelsError).toBe("status unavailable");
132+
const { page } = await mountPage(context, { onboarding: true });
133+
134+
setGatewaySnapshot({ phase: "reconnecting" });
135+
setGatewaySnapshot({ phase: "connected" });
136+
137+
await waitForFast(() => expect(statusRequestCount).toBe(3));
138+
expect(channels.state.channelsSnapshot).toBeNull();
139+
expect(channels.state.channelsLoading).toBe(true);
140+
expect(page.querySelector(".custodian__nudge--channel-onboarding")).toBeNull();
141+
142+
freshStatus.resolve(
143+
channelSnapshot({
144+
channels: { telegram: { configured: true, running: true, connected: true } },
145+
}),
146+
);
147+
await waitForFast(() => expect(channels.state.channelsLoading).toBe(false));
148+
expect(channelSnapshotEntryIsActive(channels.state.channelsSnapshot, "telegram")).toBe(true);
149+
expect(page.querySelector(".custodian__nudge--channel-onboarding")).toBeNull();
150+
channels.dispose();
151+
});
152+
153+
it("shows an error instead of a healthy nudge when refresh fails with a stale snapshot", async () => {
154+
const request = vi.fn().mockResolvedValue({
155+
sessionId: "control-ui-onboarding-00000000-0000-4000-8000-000000000001",
156+
reply: "Ready.",
157+
action: "none",
158+
});
159+
const { context, setChannelsError } = createContext(request, ["openclaw.chat"], {
160+
channelsSnapshot: channelSnapshot(),
161+
});
162+
const { page } = await mountPage(context, { onboarding: true });
163+
164+
setChannelsError("status unavailable");
165+
await page.updateComplete;
166+
167+
expect(page.querySelector('[role="alert"]')?.textContent).toContain(
168+
"Channel status is unavailable",
169+
);
170+
expect(page.textContent).not.toContain("The web app already works");
171+
});
172+
173+
it("keeps retry feedback visible until a deferred channel refresh succeeds", async () => {
174+
const retryStatus = createDeferred<ChannelsStatusSnapshot>();
175+
let statusRequestCount = 0;
176+
const request = vi.fn((method: string) => {
177+
if (method === "channels.status") {
178+
statusRequestCount += 1;
179+
return statusRequestCount === 1
180+
? Promise.reject(new Error("status unavailable"))
181+
: retryStatus.promise;
182+
}
183+
return Promise.resolve({
184+
sessionId: "control-ui-onboarding-00000000-0000-4000-8000-000000000001",
185+
reply: "Ready.",
186+
action: "none",
187+
});
188+
});
189+
const { context } = createContext(request);
190+
const channels = createChannelCapability(context.gateway);
191+
Object.assign(context, { channels });
192+
await channels.refresh();
193+
const { page } = await mountPage(context, { onboarding: true });
194+
195+
const retry = page.querySelector<HTMLButtonElement>(".custodian__nudge-cta");
196+
expect(retry?.textContent).toContain("Retry");
197+
retry?.click();
198+
199+
await waitForFast(() => expect(statusRequestCount).toBe(2));
200+
await page.updateComplete;
201+
expect(channels.state.channelsLoading).toBe(true);
202+
expect(channels.state.channelsError).toBe("status unavailable");
203+
expect(page.querySelector('[role="alert"]')).not.toBeNull();
204+
expect(page.querySelector<HTMLButtonElement>(".custodian__nudge-cta")?.disabled).toBe(true);
205+
expect(page.querySelector(".custodian__nudge-cta")?.textContent).toContain("Loading");
206+
207+
retryStatus.resolve(
208+
channelSnapshot({
209+
channels: { telegram: { configured: true, running: true, connected: true } },
210+
}),
211+
);
212+
await waitForFast(() => expect(channels.state.channelsLoading).toBe(false));
213+
expect(channels.state.channelsError).toBeNull();
214+
expect(page.querySelector(".custodian__nudge--channel-onboarding")).toBeNull();
215+
channels.dispose();
216+
});
217+
218+
it("does not refresh after the channel prompt is dismissed", async () => {
219+
const request = vi.fn().mockResolvedValue({
220+
sessionId: "control-ui-onboarding-00000000-0000-4000-8000-000000000001",
221+
reply: "Ready.",
222+
action: "none",
223+
});
224+
const { context, setChannelsConnected } = createContext(request);
225+
const { page } = await mountPage(context, { onboarding: true });
226+
await waitForFast(() => expect(context.channels.refresh).toHaveBeenCalledOnce());
227+
228+
page.store.dismissChannelOnboardingNudge();
229+
setChannelsConnected(false);
230+
setChannelsConnected(true);
231+
232+
expect(context.channels.refresh).toHaveBeenCalledOnce();
233+
});
234+
103235
it.each([
104236
{
105237
name: "normal caretaker visits",

‎ui/src/pages/custodian/custodian-page.test-harness.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type ContextHarness = {
3030
setGatewayToken: (token: string) => void;
3131
setChannelsConnected: (connected: boolean) => void;
3232
setChannelsSnapshot: (snapshot: ChannelsStatusSnapshot | null) => void;
33+
setChannelsError: (error: string | null) => void;
3334
emitGatewayEvent: (event: Pick<GatewayEventFrame, "event" | "payload">) => void;
3435
};
3536

@@ -88,7 +89,7 @@ export function createContext(
8889
} as unknown as ApplicationGateway;
8990
const agentListeners = new Set<() => void>();
9091
const channelListeners = new Set<(state: ApplicationContext["channels"]["state"]) => void>();
91-
const channelState = {
92+
const channelState: ApplicationContext["channels"]["state"] = {
9293
client,
9394
connected: true,
9495
channelsLoading: false,
@@ -108,6 +109,13 @@ export function createContext(
108109
whatsappLoginConnected: null,
109110
whatsappBusy: false,
110111
};
112+
const refreshChannels = vi.fn(() => {
113+
channelState.channelsLoading = true;
114+
for (const listener of channelListeners) {
115+
listener(channelState);
116+
}
117+
return new Promise<void>(() => {});
118+
});
111119
const context = {
112120
gateway,
113121
agents: {
@@ -128,7 +136,7 @@ export function createContext(
128136
agentSelection: { state: { selectedId: "main" } },
129137
channels: {
130138
state: channelState,
131-
refresh: vi.fn().mockResolvedValue(undefined),
139+
refresh: refreshChannels,
132140
subscribe: (listener: (state: ApplicationContext["channels"]["state"]) => void) => {
133141
channelListeners.add(listener);
134142
return () => channelListeners.delete(listener);
@@ -152,6 +160,8 @@ export function createContext(
152160
},
153161
setChannelsConnected: (connected) => {
154162
channelState.connected = connected;
163+
channelState.channelsLoading = false;
164+
channelState.channelsError = null;
155165
for (const listener of channelListeners) {
156166
listener(channelState);
157167
}
@@ -163,6 +173,13 @@ export function createContext(
163173
listener(channelState);
164174
}
165175
},
176+
setChannelsError: (error: string | null) => {
177+
channelState.channelsLoading = false;
178+
channelState.channelsError = error;
179+
for (const listener of channelListeners) {
180+
listener(channelState);
181+
}
182+
},
166183
emitGatewayEvent: (event) => {
167184
for (const listener of eventListeners) {
168185
listener(event as GatewayEventFrame);

0 commit comments

Comments
 (0)