@@ -3,6 +3,7 @@ import type { GatewayBrowserClient } from "../../api/gateway.ts";
33import type { ApplicationContext } from "../../app/context.ts" ;
44import { t } from "../../i18n/index.ts" ;
55import { formatUiError , formatUiExternalText } from "../../lib/format-error.ts" ;
6+ import type { GatewayConnectionScope } from "../../lib/gateway-connection-lifecycle.ts" ;
67import {
78 inspectPlugin ,
89 readPluginCapabilityConsentError ,
@@ -19,6 +20,7 @@ import {
1920import type { GatewayPageController } from "../../lit/gateway-page-controller.ts" ;
2021import type { PluginConsentIntent , PluginConsentState } from "./consent-dialog.ts" ;
2122import { readPluginInstallPolicyWarning } from "./install-policy-warning.ts" ;
23+ import { confirmPluginInstall } from "./plugin-lifecycle-confirmation.ts" ;
2224import { pluginRowKey , type PluginRowMessage } from "./view.ts" ;
2325
2426type 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+
3239type 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 ) {
0 commit comments