Skip to content

Commit c17d442

Browse files
fix(ui): confirm disruptive plugin lifecycle changes (#131509)
* fix(ui): confirm disruptive plugin lifecycle changes * test(ui): cross plugin lifecycle confirmation * fix(ui): bind plugin confirmation to gateway epoch * fix(ui): fence queued plugin lifecycle dispatch * fix(ui): retain plugin install consent scope
1 parent 1754d4b commit c17d442

11 files changed

Lines changed: 524 additions & 157 deletions

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3418,7 +3418,9 @@ export const en: TranslationMap = {
34183418
remove: "Remove",
34193419
removing: "Removing…",
34203420
removeNamed: "Remove {name}",
3421-
removeConfirm: "Remove this plugin package and all of its entries?",
3421+
removeConfirmTitle: "Remove {name}?",
3422+
removeConfirmMessage:
3423+
"Removing this plugin package and all of its entries restarts the Gateway immediately and interrupts active sessions.",
34223424
cancel: "Cancel",
34233425
removedRestart: "Removed {name}. A Gateway restart is required to apply the change.",
34243426
verifiedSource: "Verified source",
@@ -3449,6 +3451,9 @@ export const en: TranslationMap = {
34493451
install: "Install",
34503452
installing: "Installing…",
34513453
installNamed: "Install {name}",
3454+
installConfirmTitle: "Install {name}?",
3455+
installConfirmMessage:
3456+
"Installing this plugin restarts the Gateway immediately and interrupts active sessions.",
34523457
policyReviewTitle: "Security review needed",
34533458
policyReviewBodyKnown: "Policy warnings: {count}. Not installed.",
34543459
policyReviewBodyReason: "{reason} Not installed.",

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export async function runPluginConfigMutation<T>(
8787
runtimeConfig: Pick<RuntimeConfigCapability, "runExternalMutation">,
8888
expectedClient: GatewayBrowserClient,
8989
task: (client: GatewayBrowserClient) => Promise<T>,
90+
options: { canDispatch?: () => boolean; dispatchError?: string } = {},
9091
): Promise<{ value: T; refreshError: string | null }> {
9192
let taskError: Error | undefined;
9293
const mutation = await runtimeConfig.runExternalMutation(async (client) => {
@@ -100,7 +101,7 @@ export async function runPluginConfigMutation<T>(
100101
taskError = error instanceof Error ? error : new Error(String(error));
101102
throw taskError;
102103
}
103-
});
104+
}, options);
104105
if (!mutation.ok) {
105106
throw taskError ?? new Error(mutation.error);
106107
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Plugin code mutations restart the Gateway, so their UI entry points share one
2+
// confirmation contract before either lifecycle request can be dispatched.
3+
import { showConfirmDialog } from "../../components/confirm-dialog.ts";
4+
import { t } from "../../i18n/index.ts";
5+
import type { PluginInstallRequest } from "../../lib/plugins/index.ts";
6+
7+
export function confirmPluginInstall(request: PluginInstallRequest): Promise<boolean> {
8+
const name = request.source === "official" ? request.pluginId : request.packageName;
9+
return showConfirmDialog({
10+
title: t("pluginsPage.installConfirmTitle", { name }),
11+
message: t("pluginsPage.installConfirmMessage"),
12+
confirmLabel: t("pluginsPage.install"),
13+
});
14+
}
15+
16+
export function confirmPluginUninstall(name: string): Promise<boolean> {
17+
return showConfirmDialog({
18+
title: t("pluginsPage.removeConfirmTitle", { name }),
19+
message: t("pluginsPage.removeConfirmMessage"),
20+
confirmLabel: t("pluginsPage.remove"),
21+
danger: true,
22+
});
23+
}

‎ui/src/pages/plugins/plugins-consent-controller.ts‎

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { GatewayBrowserClient } from "../../api/gateway.ts";
33
import type { ApplicationContext } from "../../app/context.ts";
44
import { t } from "../../i18n/index.ts";
55
import { formatUiError, formatUiExternalText } from "../../lib/format-error.ts";
6+
import type { GatewayConnectionScope } from "../../lib/gateway-connection-lifecycle.ts";
67
import {
78
inspectPlugin,
89
readPluginCapabilityConsentError,
@@ -19,6 +20,7 @@ import {
1920
import type { GatewayPageController } from "../../lit/gateway-page-controller.ts";
2021
import type { PluginConsentIntent, PluginConsentState } from "./consent-dialog.ts";
2122
import { readPluginInstallPolicyWarning } from "./install-policy-warning.ts";
23+
import { confirmPluginInstall } from "./plugin-lifecycle-confirmation.ts";
2224
import { pluginRowKey, type PluginRowMessage } from "./view.ts";
2325

2426
type PluginMutationSuccess<Result> = (
@@ -29,6 +31,11 @@ type PluginMutationSuccess<Result> = (
2931
isLatest: () => boolean,
3032
) => Promise<void>;
3133

34+
type PluginMutationOptions = {
35+
confirm?: () => Promise<boolean>;
36+
preserveMessageWhilePending?: boolean;
37+
};
38+
3239
type PluginsConsentControllerHost = {
3340
gateway: GatewayPageController;
3441
getContext: () => ApplicationContext;
@@ -73,27 +80,41 @@ export class PluginsConsentController {
7380

7481
private mutationToken = 0;
7582
private readonly mutationTokens = new Map<string, number>();
83+
// Server reviews continue one confirmed install only while its Gateway epoch survives.
84+
// Reconnect reset drops the scope before a surviving row warning can be acknowledged.
85+
private readonly confirmedInstallScopes = new Map<string, GatewayConnectionScope>();
7686

7787
constructor(private readonly host: PluginsConsentControllerHost) {}
7888

7989
reset(): void {
8090
this.close();
8191
this.mutationTokens.clear();
92+
this.confirmedInstallScopes.clear();
8293
}
8394

8495
async runMutation<Result>(
8596
rowKey: string,
8697
mutate: (client: GatewayBrowserClient) => Promise<Result>,
8798
onSuccess: PluginMutationSuccess<Result>,
88-
onError: (error: unknown) => void = (error) => {
99+
options: PluginMutationOptions = {},
100+
onError: (error: unknown, scope: GatewayConnectionScope) => void = (error) => {
89101
this.host.setMessage(rowKey, { kind: "error", text: formatUiError(error) });
90102
},
91-
options: { preserveMessageWhilePending?: boolean } = {},
92103
): Promise<void> {
93104
const scope = this.host.gateway.capture();
94105
if (!scope || !this.host.canMutate() || this.host.isBusy(rowKey)) {
95106
return;
96107
}
108+
if (
109+
options.confirm &&
110+
(!(await options.confirm()) ||
111+
!this.host.gateway.isCurrent(scope) ||
112+
!this.host.canMutate() ||
113+
this.host.isBusy(rowKey))
114+
) {
115+
return;
116+
}
117+
// Confirmation, config queue, and request share the captured Gateway epoch and client.
97118
this.host.clearPageNotice();
98119
const mutationToken = ++this.mutationToken;
99120
this.mutationTokens.set(rowKey, mutationToken);
@@ -109,13 +130,14 @@ export class PluginsConsentController {
109130
this.host.getContext().runtimeConfig,
110131
scope.client,
111132
mutate,
133+
{ canDispatch: () => isCurrent() && this.host.canMutate() },
112134
);
113135
if (isCurrent()) {
114136
await onSuccess(mutation.value, mutation.refreshError, scope.client, isCurrent, isLatest);
115137
}
116138
} catch (error) {
117139
if (isCurrent()) {
118-
onError(error);
140+
onError(error, scope);
119141
}
120142
} finally {
121143
if (this.mutationTokens.get(rowKey) === mutationToken) {
@@ -209,6 +231,13 @@ export class PluginsConsentController {
209231
}
210232

211233
async install(request: PluginInstallRequest, installIdentity: string): Promise<void> {
234+
const confirmedScope = this.confirmedInstallScopes.get(installIdentity);
235+
this.confirmedInstallScopes.delete(installIdentity);
236+
const isConfirmedContinuation =
237+
(request.acknowledgeInstallPolicyWarning === true ||
238+
request.acknowledgeCapabilities !== undefined) &&
239+
confirmedScope &&
240+
this.host.gateway.isCurrent(confirmedScope);
212241
// The server stages and inspects the requested artifact before asking for consent.
213242
// Catalog/search metadata cannot authorize that artifact's capabilities.
214243
await this.runMutation(
@@ -226,9 +255,14 @@ export class PluginsConsentController {
226255
);
227256
await this.host.refreshCatalogAfterMutation(client);
228257
},
229-
(error) => {
258+
{
259+
confirm: isConfirmedContinuation ? undefined : () => confirmPluginInstall(request),
260+
preserveMessageWhilePending: request.acknowledgeInstallPolicyWarning === true,
261+
},
262+
(error, scope) => {
230263
const consentDetails = readPluginCapabilityConsentError(error);
231264
if (consentDetails) {
265+
this.confirmedInstallScopes.set(installIdentity, scope);
232266
this.open(
233267
{ kind: "install", request, installIdentity },
234268
consentDetails.pluginId,
@@ -238,6 +272,7 @@ export class PluginsConsentController {
238272
}
239273
const policyWarning = readPluginInstallPolicyWarning(error);
240274
if (policyWarning) {
275+
this.confirmedInstallScopes.set(installIdentity, scope);
241276
this.host.setMessage(installIdentity, {
242277
kind: "warning",
243278
text: policyWarning.reason,
@@ -247,7 +282,6 @@ export class PluginsConsentController {
247282
}
248283
this.host.setMessage(installIdentity, { kind: "error", text: formatUiError(error) });
249284
},
250-
{ preserveMessageWhilePending: request.acknowledgeInstallPolicyWarning === true },
251285
);
252286
}
253287

@@ -273,6 +307,7 @@ export class PluginsConsentController {
273307
this.host.getContext().gateway.connect();
274308
}
275309
},
310+
{},
276311
(error) => {
277312
const details = readPluginCapabilityConsentError(error);
278313
if (enabled && details) {

‎ui/src/pages/plugins/plugins-page.consent.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ async function mountDiscover(handler: Parameters<typeof createClient>[0]) {
4848
return { page, request, available };
4949
}
5050

51+
async function confirmPendingPluginInstall() {
52+
await waitForFast(() =>
53+
expect(document.body.querySelector(".exec-approval-actions .btn.primary")).not.toBeNull(),
54+
);
55+
document.body.querySelector<HTMLButtonElement>(".exec-approval-actions .btn.primary")?.click();
56+
}
57+
5158
describe("PluginsPage consent", () => {
5259
beforeEach(async () => {
5360
await i18n.setLocale("en");
@@ -122,6 +129,7 @@ describe("PluginsPage consent", () => {
122129
}
123130

124131
await clickRowAction(page, row, "Install");
132+
await confirmPendingPluginInstall();
125133

126134
await waitForFast(() =>
127135
expect(request).toHaveBeenCalledWith("plugins.install", installRequest),
@@ -204,6 +212,7 @@ describe("PluginsPage consent", () => {
204212
});
205213

206214
await clickRowAction(page, '[data-plugin-id="calendar-runtime"]', "Install");
215+
await confirmPendingPluginInstall();
207216
await waitForFast(() =>
208217
expect(page.querySelector(".plugins-policy-review")?.textContent).toContain(
209218
"Review this plugin.",

0 commit comments

Comments
 (0)