Skip to content

Commit 59f1f4f

Browse files
fix(ui): refresh automation alerts on cron events (#117648)
1 parent 3772f07 commit 59f1f4f

2 files changed

Lines changed: 94 additions & 15 deletions

File tree

‎ui/src/components/sidebar-attention.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ describe("sidebar attention refresh ownership", () => {
193193
password: "",
194194
},
195195
subscribe: () => () => undefined,
196+
subscribeEvents: () => () => undefined,
196197
} as unknown as ApplicationGateway;
197198
const overlays = {
198199
snapshot: { approvalQueue: [] },
@@ -240,6 +241,59 @@ describe("sidebar attention refresh ownership", () => {
240241
expect(element.loadedAtMs).toBe(200_000);
241242
expect(localStorage.getItem(dismissalStoreKey(gateway.connection.gatewayUrl))).not.toBeNull();
242243
});
244+
245+
it("clears a stale failure alert when the gateway reports an automation change", async () => {
246+
const responses = {
247+
"cron.list": [{ jobs: [cronJob("failed")] }, { jobs: [] }],
248+
"models.authStatus": [{ ts: 1, providers: [] }],
249+
};
250+
const request = vi.fn((method: keyof typeof responses) => {
251+
const response = responses[method].shift();
252+
if (!response) {
253+
throw new Error(`Unexpected request: ${method}`);
254+
}
255+
return Promise.resolve(response);
256+
});
257+
const client = { request } as unknown as GatewayBrowserClient;
258+
const snapshot = {
259+
client,
260+
phase: "connected",
261+
hello: null,
262+
assistantAgentId: "main",
263+
sessionKey: "agent:main:main",
264+
lastError: null,
265+
lastErrorCode: null,
266+
};
267+
let eventListener: Parameters<ApplicationGateway["subscribeEvents"]>[0] | undefined;
268+
const gateway = {
269+
snapshot,
270+
connection: {
271+
gatewayUrl: "ws://gateway.test",
272+
token: "",
273+
bootstrapToken: "",
274+
password: "",
275+
},
276+
subscribe: () => () => undefined,
277+
subscribeEvents: (listener: NonNullable<typeof eventListener>) => {
278+
eventListener = listener;
279+
return () => undefined;
280+
},
281+
} as unknown as ApplicationGateway;
282+
const overlays = {
283+
snapshot: { approvalQueue: [] },
284+
subscribe: () => () => undefined,
285+
} as unknown as ApplicationContext["overlays"];
286+
vi.stubGlobal("localStorage", createTestStorageMock());
287+
288+
const provider = createApplicationContextProvider({ gateway, overlays } as ApplicationContext);
289+
const element = document.createElement("openclaw-sidebar-attention") as SidebarAttentionElement;
290+
provider.append(element);
291+
document.body.append(provider);
292+
await waitForFast(() => expect(element.textContent).toContain("1 automation(s) failed"));
293+
294+
eventListener?.({ type: "event", event: "cron", payload: {} });
295+
await waitForFast(() => expect(element.textContent).not.toContain("automation(s) failed"));
296+
});
243297
});
244298

245299
describe("pruneDismissals", () => {

‎ui/src/components/sidebar-attention.ts‎

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,35 @@ class SidebarAttention extends OpenClawLightDomContentsElement {
5858
autoRun: false,
5959
// Gateway identity matters when a replacement source reuses the same client object.
6060
args: () =>
61-
[null as ApplicationContext["gateway"] | null, null as GatewayBrowserClient | null] as const,
62-
task: async ([gateway, client], { signal }) => {
61+
[
62+
null as ApplicationContext["gateway"] | null,
63+
null as GatewayBrowserClient | null,
64+
true as boolean,
65+
] as const,
66+
task: async ([gateway, client, refreshModelAuth], { signal }) => {
6367
if (!gateway || !client) {
6468
return initialState;
6569
}
6670
const cron = createInitialCronState({ client, connected: true });
67-
await Promise.allSettled([
71+
const loads: Promise<unknown>[] = [
6872
loadCronJobsPage(cron).then(() => {
6973
if (!signal.aborted) {
7074
this.cronJobs = cron.cronJobs;
7175
}
7276
}),
73-
loadModelAuthStatus(client, { signal })
74-
.catch(() => null)
75-
.then((modelAuthStatus) => {
76-
if (!signal.aborted) {
77-
this.modelAuthStatus = modelAuthStatus;
78-
}
79-
}),
80-
]);
77+
];
78+
if (refreshModelAuth) {
79+
loads.push(
80+
loadModelAuthStatus(client, { signal })
81+
.catch(() => null)
82+
.then((modelAuthStatus) => {
83+
if (!signal.aborted) {
84+
this.modelAuthStatus = modelAuthStatus;
85+
}
86+
}),
87+
);
88+
}
89+
await Promise.allSettled(loads);
8190
return true;
8291
},
8392
onComplete: () => {
@@ -94,6 +103,19 @@ class SidebarAttention extends OpenClawLightDomContentsElement {
94103
return gateway.subscribe(() => this.synchronize(gateway));
95104
},
96105
)
106+
.effect(
107+
() => this.context?.gateway,
108+
(gateway) =>
109+
gateway.subscribeEvents((event) => {
110+
if (this.context?.gateway !== gateway || event.event !== "cron") {
111+
return;
112+
}
113+
// The Automations page refreshes from the same event. Refresh this
114+
// independent snapshot too so its ambient alert cannot contradict it.
115+
this.loadedClient = null;
116+
this.synchronize(gateway, { refreshModelAuth: false });
117+
}),
118+
)
97119
.watch(
98120
() => this.context?.overlays,
99121
(overlays, notify) => overlays.subscribe(() => notify()),
@@ -136,21 +158,24 @@ class SidebarAttention extends OpenClawLightDomContentsElement {
136158
this.idleRefreshTimer = null;
137159
}
138160
this.subscriptions.clear();
139-
void this.loadTask.run([null, null]);
161+
void this.loadTask.run([null, null, false]);
140162
this.loadedClient = null;
141163
this.loadedGateway = null;
142164
super.disconnectedCallback();
143165
}
144166

145-
private synchronize(gateway: ApplicationContext["gateway"]) {
167+
private synchronize(
168+
gateway: ApplicationContext["gateway"],
169+
options: { refreshModelAuth?: boolean } = {},
170+
) {
146171
const snapshot = gateway.snapshot;
147172
const gatewayUrl = gateway.connection.gatewayUrl;
148173
if (gatewayUrl && gatewayUrl !== this.dismissedScope) {
149174
this.dismissedScope = gatewayUrl;
150175
this.dismissed = loadDismissals(gatewayUrl);
151176
}
152177
if (snapshot.phase !== "connected" || !snapshot.client) {
153-
void this.loadTask.run([null, null]);
178+
void this.loadTask.run([null, null, false]);
154179
this.loadedClient = null;
155180
this.loadedGateway = null;
156181
this.cronJobs = [];
@@ -162,7 +187,7 @@ class SidebarAttention extends OpenClawLightDomContentsElement {
162187
}
163188
this.loadedGateway = gateway;
164189
this.loadedClient = snapshot.client;
165-
void this.loadTask.run([gateway, snapshot.client]);
190+
void this.loadTask.run([gateway, snapshot.client, options.refreshModelAuth !== false]);
166191
}
167192

168193
// Re-arm stale snoozes only right after this tab's own data refresh: fresh

0 commit comments

Comments
 (0)