@@ -4,6 +4,7 @@ import path from "node:path";
44import { afterAll , afterEach , describe , expect , it , vi } from "vitest" ;
55import { setCurrentPluginMetadataSnapshot } from "../plugins/current-plugin-metadata-snapshot.js" ;
66import type { PluginCandidate , PluginDiscoveryResult } from "../plugins/discovery.js" ;
7+ import { clearPluginMetadataLifecycleCaches } from "../plugins/plugin-metadata-lifecycle.js" ;
78import {
89 applyPluginAutoEnable ,
910 detectPluginAutoEnableCandidates ,
@@ -1119,6 +1120,68 @@ describe("applyPluginAutoEnable core", () => {
11191120 expect ( setupRegistryMock . resolvePluginSetupAutoEnableReasons ) . toHaveBeenCalledTimes ( 2 ) ;
11201121 } ) ;
11211122
1123+ it ( "fingerprints identical snapshots once per plugin metadata lifecycle" , ( ) => {
1124+ const traversals = { candidates : 0 , config : 0 , env : 0 , plugins : 0 } ;
1125+ const config = new Proxy < OpenClawConfig > (
1126+ { } ,
1127+ {
1128+ ownKeys : ( target ) => {
1129+ traversals . config += 1 ;
1130+ return Reflect . ownKeys ( target ) ;
1131+ } ,
1132+ } ,
1133+ ) ;
1134+ const envSnapshot = new Proxy ( makeIsolatedEnv ( ) , {
1135+ ownKeys : ( target ) => {
1136+ traversals . env += 1 ;
1137+ return Reflect . ownKeys ( target ) ;
1138+ } ,
1139+ } ) ;
1140+ const discovery : PluginDiscoveryResult = {
1141+ candidates : new Proxy ( [ ] , {
1142+ get : ( target , property , receiver ) => {
1143+ if ( property === "map" ) {
1144+ traversals . candidates += 1 ;
1145+ }
1146+ return Reflect . get ( target , property , receiver ) ;
1147+ } ,
1148+ } ) ,
1149+ diagnostics : [ ] ,
1150+ } ;
1151+ const manifestRegistry = makeRegistry ( [ ] ) ;
1152+ manifestRegistry . plugins = new Proxy ( manifestRegistry . plugins , {
1153+ get : ( target , property , receiver ) => {
1154+ if ( property === "map" ) {
1155+ traversals . plugins += 1 ;
1156+ }
1157+ return Reflect . get ( target , property , receiver ) ;
1158+ } ,
1159+ } ) ;
1160+
1161+ const first = applyPluginAutoEnable ( {
1162+ config,
1163+ discovery,
1164+ env : envSnapshot ,
1165+ manifestRegistry,
1166+ } ) ;
1167+ const firstTraversalCounts = { ...traversals } ;
1168+
1169+ for ( let index = 0 ; index < 20 ; index += 1 ) {
1170+ expect ( applyPluginAutoEnable ( { config, discovery, env : envSnapshot , manifestRegistry } ) ) . toBe (
1171+ first ,
1172+ ) ;
1173+ }
1174+ expect ( traversals ) . toEqual ( firstTraversalCounts ) ;
1175+
1176+ clearPluginMetadataLifecycleCaches ( ) ;
1177+ applyPluginAutoEnable ( { config, discovery, env : envSnapshot , manifestRegistry } ) ;
1178+
1179+ expect ( traversals . config ) . toBeGreaterThan ( firstTraversalCounts . config ) ;
1180+ expect ( traversals . env ) . toBeGreaterThan ( firstTraversalCounts . env ) ;
1181+ expect ( traversals . candidates ) . toBeGreaterThan ( firstTraversalCounts . candidates ) ;
1182+ expect ( traversals . plugins ) . toBeGreaterThan ( firstTraversalCounts . plugins ) ;
1183+ } ) ;
1184+
11221185 it ( "does not reuse same-turn results for omitted metadata after current snapshot replacement" , ( ) => {
11231186 const config : OpenClawConfig = {
11241187 channels : { apn : { someKey : "value" } } ,
@@ -1196,7 +1259,7 @@ describe("applyPluginAutoEnable core", () => {
11961259 expect ( setupRegistryMock . resolvePluginSetupAutoEnableReasons ) . toHaveBeenCalledTimes ( 2 ) ;
11971260 } ) ;
11981261
1199- it ( "does not reuse same-turn auto-enable results after config mutates in place " , ( ) => {
1262+ it ( "refreshes auto-enable results after config mutates at a lifecycle boundary " , ( ) => {
12001263 const config : OpenClawConfig = { } ;
12011264 const manifestRegistry = makeRegistry ( [ { id : "apn-channel" , channels : [ "apn" ] } ] ) ;
12021265
@@ -1207,6 +1270,7 @@ describe("applyPluginAutoEnable core", () => {
12071270 manifestRegistry,
12081271 } ) ;
12091272 config . channels = { apn : { someKey : "value" } } ;
1273+ clearPluginMetadataLifecycleCaches ( ) ;
12101274 const second = applyPluginAutoEnable ( {
12111275 config,
12121276 discovery : emptyDiscovery ,
@@ -1219,7 +1283,7 @@ describe("applyPluginAutoEnable core", () => {
12191283 expect ( second ) . not . toBe ( first ) ;
12201284 } ) ;
12211285
1222- it ( "does not reuse same-turn auto-enable results after registry mutates in place " , ( ) => {
1286+ it ( "refreshes auto-enable results after registry mutates at a lifecycle boundary " , ( ) => {
12231287 const config : OpenClawConfig = {
12241288 channels : { apn : { someKey : "value" } } ,
12251289 } ;
@@ -1236,6 +1300,7 @@ describe("applyPluginAutoEnable core", () => {
12361300 registry . plugins . length ,
12371301 ...makeRegistry ( [ { id : "apn-channel" , channels : [ "apn" ] } ] ) . plugins ,
12381302 ) ;
1303+ clearPluginMetadataLifecycleCaches ( ) ;
12391304 const second = applyPluginAutoEnable ( {
12401305 config,
12411306 discovery : emptyDiscovery ,
@@ -1248,7 +1313,7 @@ describe("applyPluginAutoEnable core", () => {
12481313 expect ( second ) . not . toBe ( first ) ;
12491314 } ) ;
12501315
1251- it ( "does not reuse same-turn auto-enable results after discovery mutates in place " , ( ) => {
1316+ it ( "refreshes auto-enable results after discovery mutates at a lifecycle boundary " , ( ) => {
12521317 const config : OpenClawConfig = { } ;
12531318 const mutableDiscovery : PluginDiscoveryResult = { candidates : [ ] , diagnostics : [ ] } ;
12541319 const manifestRegistry = makeRegistry ( [
@@ -1270,6 +1335,7 @@ describe("applyPluginAutoEnable core", () => {
12701335 channelId : "cache-channel" ,
12711336 } ) ,
12721337 ) ;
1338+ clearPluginMetadataLifecycleCaches ( ) ;
12731339 const second = applyPluginAutoEnable ( {
12741340 config,
12751341 discovery : mutableDiscovery ,
@@ -1282,7 +1348,7 @@ describe("applyPluginAutoEnable core", () => {
12821348 expect ( second ) . not . toBe ( first ) ;
12831349 } ) ;
12841350
1285- it ( "does not reuse same-turn auto-enable results after env mutates in place " , ( ) => {
1351+ it ( "refreshes auto-enable results after env mutates at a lifecycle boundary " , ( ) => {
12861352 const config : OpenClawConfig = {
12871353 plugins : {
12881354 entries : {
@@ -1303,6 +1369,7 @@ describe("applyPluginAutoEnable core", () => {
13031369 manifestRegistry,
13041370 } ) ;
13051371 mutableEnv . OPENCLAW_TEST_CACHE_INPUT = "changed" ;
1372+ clearPluginMetadataLifecycleCaches ( ) ;
13061373 const second = applyPluginAutoEnable ( {
13071374 config,
13081375 discovery : emptyDiscovery ,
0 commit comments