@@ -7,6 +7,7 @@ import type { SecretRef } from "../config/types.secrets.js";
77import { resetLogger , setLoggerOverride } from "../logging/logger.js" ;
88import { loggingState } from "../logging/state.js" ;
99import { resolveInstalledPluginIndexPolicyHash } from "./installed-plugin-index-policy.js" ;
10+ import type { PluginLoadOptions } from "./loader-types.js" ;
1011import { createEmptyPluginRegistry } from "./registry-empty.js" ;
1112import { appendRuntimePluginToolGrant } from "./tool-grant-allowlist.js" ;
1213
@@ -21,39 +22,51 @@ type MockRegistryToolEntry = {
2122} ;
2223
2324const loadOpenClawPluginsMock = vi . fn ( ) ;
24- const resolveCompatibleRuntimePluginRegistryMock = vi . fn ( ) ;
25- const getLoadedRuntimePluginRegistryMock = vi . fn ( ) ;
2625const applyPluginAutoEnableMock = vi . fn ( ) ;
2726const loadContextMocks = vi . hoisted ( ( ) => ( {
2827 actualResolve : undefined as
2928 | typeof import ( "./runtime/load-context.resolve.js" ) . resolvePluginRuntimeLoadContext
3029 | undefined ,
3130 resolve : vi . fn ( ) ,
3231} ) ) ;
33-
34- vi . mock ( "./loader.js" , ( ) => ( {
35- loadOpenClawPlugins : ( params : unknown ) => loadOpenClawPluginsMock ( params ) ,
36- loadPluginRegistryHandle : ( params : unknown ) => loadOpenClawPluginsMock ( params ) ,
37- resolveCompatibleRuntimePluginRegistry : ( params : unknown ) =>
38- resolveCompatibleRuntimePluginRegistryMock ( params ) ,
39- resolvePluginRegistryLoadCacheKey : ( params : unknown ) => JSON . stringify ( params ) ,
40- resolveRuntimePluginRegistry : ( params : unknown ) =>
41- resolveCompatibleRuntimePluginRegistryMock ( params ) ,
32+ const activeRegistryMocks = vi . hoisted ( ( ) => ( {
33+ actualGetLoadedRegistry : undefined as
34+ | typeof import ( "./active-runtime-registry.js" ) . getLoadedRuntimePluginRegistry
35+ | undefined ,
36+ getLoadedRegistry : vi . fn ( ) ,
4237} ) ) ;
4338
44- // resolveCompatibleRuntimePluginRegistry now lives in this module, so the
45- // ./loader.js seam no longer observes the intra-module call that tool resolution
46- // triggers. Count the boundary tools.ts actually crosses, keeping real behavior.
39+ // Only the load entry points are intercepted. The registry-resolution bindings
40+ // stay wired to their real owners so a consumer that crosses them observes
41+ // production behavior instead of a double that no production path reaches.
42+ vi . mock ( "./loader.js" , async ( ) => {
43+ const [ activeRuntimeRegistry , loaderCache ] = await Promise . all ( [
44+ import ( "./active-runtime-registry.js" ) ,
45+ import ( "./loader-cache.js" ) ,
46+ ] ) ;
47+ const loadPluginRegistryHandle = ( params : unknown ) => loadOpenClawPluginsMock ( params ) ;
48+ return {
49+ loadOpenClawPlugins : loadPluginRegistryHandle ,
50+ loadPluginRegistryHandle,
51+ resolveCompatibleRuntimePluginRegistry :
52+ activeRuntimeRegistry . resolveCompatibleRuntimePluginRegistry ,
53+ resolvePluginRegistryLoadCacheKey : loaderCache . resolvePluginRegistryLoadCacheKey ,
54+ resolveRuntimePluginRegistry : ( options ?: PluginLoadOptions ) =>
55+ activeRuntimeRegistry . resolveCompatibleRuntimePluginRegistry ( options ) ??
56+ loadPluginRegistryHandle ( { ...options , activate : false } ) ,
57+ } ;
58+ } ) ;
59+
60+ // resolvePluginToolRegistry reads the active registry through this module, so
61+ // this is the seam that decides reuse-vs-rebuild. It delegates to the real
62+ // implementation; tests drive it by installing an active registry.
4763vi . mock ( "./active-runtime-registry.js" , async ( importOriginal ) => {
4864 const actual = await importOriginal < typeof import ( "./active-runtime-registry.js" ) > ( ) ;
65+ activeRegistryMocks . actualGetLoadedRegistry = actual . getLoadedRuntimePluginRegistry ;
4966 return {
5067 ...actual ,
51- getLoadedRuntimePluginRegistry : (
52- params ?: Parameters < typeof actual . getLoadedRuntimePluginRegistry > [ 0 ] ,
53- ) => {
54- getLoadedRuntimePluginRegistryMock ( params ) ;
55- return actual . getLoadedRuntimePluginRegistry ( params ) ;
56- } ,
68+ getLoadedRuntimePluginRegistry : ( ...args : unknown [ ] ) =>
69+ activeRegistryMocks . getLoadedRegistry ( ...args ) ,
5770 } ;
5871} ) ;
5972
@@ -524,6 +537,10 @@ function mockCallParams(
524537 return call [ 0 ] as Record < string , unknown > ;
525538}
526539
540+ function activeRegistryRequiredPluginIds ( index = 0 ) : unknown {
541+ return mockCallParams ( activeRegistryMocks . getLoadedRegistry , index ) . requiredPluginIds ;
542+ }
543+
527544function expectLoaderSelectedOnlyPluginIds ( expectedPluginIds : readonly string [ ] ) {
528545 const selectedPluginIds = loadOpenClawPluginsMock . mock . calls . map (
529546 ( [ params ] ) => ( params as { onlyPluginIds ?: string [ ] } ) . onlyPluginIds ,
@@ -572,11 +589,13 @@ describe("resolvePluginTools optional tools", () => {
572589
573590 beforeEach ( ( ) => {
574591 loadOpenClawPluginsMock . mockReset ( ) ;
575- getLoadedRuntimePluginRegistryMock . mockReset ( ) ;
576- resolveCompatibleRuntimePluginRegistryMock . mockReset ( ) ;
577- resolveCompatibleRuntimePluginRegistryMock . mockImplementation ( ( ) =>
578- getActivePluginRegistry ?.( ) ,
579- ) ;
592+ activeRegistryMocks . getLoadedRegistry . mockReset ( ) ;
593+ activeRegistryMocks . getLoadedRegistry . mockImplementation ( ( ...args : unknown [ ] ) => {
594+ if ( ! activeRegistryMocks . actualGetLoadedRegistry ) {
595+ throw new Error ( "active-runtime-registry mock was not initialized" ) ;
596+ }
597+ return activeRegistryMocks . actualGetLoadedRegistry ( ...( args as [ never ] ) ) ;
598+ } ) ;
580599 applyPluginAutoEnableMock . mockReset ( ) ;
581600 applyPluginAutoEnableMock . mockImplementation ( ( { config } : { config : unknown } ) => ( {
582601 config,
@@ -1157,7 +1176,6 @@ describe("resolvePluginTools optional tools", () => {
11571176 "gateway-bindable" ,
11581177 "/tmp" ,
11591178 ) ;
1160- resolveCompatibleRuntimePluginRegistryMock . mockReturnValue ( partialRegistry ) ;
11611179 loadOpenClawPluginsMock . mockReturnValue ( fullRegistry ) ;
11621180
11631181 const tools = resolvePluginTools (
@@ -1168,6 +1186,7 @@ describe("resolvePluginTools optional tools", () => {
11681186 ) ;
11691187
11701188 expectResolvedToolNames ( tools , [ "other_tool" , "optional_tool" ] ) ;
1189+ expect ( activeRegistryMocks . getLoadedRegistry ) . toHaveReturnedWith ( partialRegistry ) ;
11711190 const loaderParams = mockCallParams ( loadOpenClawPluginsMock ) as {
11721191 activate ?: unknown ;
11731192 cache ?: unknown ;
@@ -1245,7 +1264,6 @@ describe("resolvePluginTools optional tools", () => {
12451264 "gateway-bindable" ,
12461265 "/tmp" ,
12471266 ) ;
1248- resolveCompatibleRuntimePluginRegistryMock . mockReturnValue ( staleRegistry ) ;
12491267 loadOpenClawPluginsMock . mockReturnValue ( freshRegistry ) ;
12501268
12511269 const tools = resolvePluginTools (
@@ -1256,6 +1274,7 @@ describe("resolvePluginTools optional tools", () => {
12561274 ) ;
12571275
12581276 expectResolvedToolNames ( tools , [ "other_tool" , "optional_tool" ] ) ;
1277+ expect ( activeRegistryMocks . getLoadedRegistry ) . toHaveReturnedWith ( staleRegistry ) ;
12591278 expect ( getActivePluginRegistry ?.( ) ) . toBe ( staleRegistry ) ;
12601279 expectLoaderSelectedOnlyPluginIds ( [ "multi" , "optional-demo" ] ) ;
12611280 expect ( freshRegistry . diagnostics ) . toStrictEqual ( [ ] ) ;
@@ -2855,7 +2874,6 @@ describe("resolvePluginTools optional tools", () => {
28552874 status : "loaded" ,
28562875 } ) ;
28572876 setActivePluginRegistry ?.( replacementRegistry as never , "provider-runtime" , "default" , "/tmp" ) ;
2858- resolveCompatibleRuntimePluginRegistryMock . mockReturnValue ( undefined ) ;
28592877 loadOpenClawPluginsMock . mockReset ( ) ;
28602878 loadOpenClawPluginsMock
28612879 . mockReturnValueOnce ( gatewayRegistry )
@@ -3086,7 +3104,6 @@ describe("resolvePluginTools optional tools", () => {
30863104
30873105 it ( "reuses a compatible active registry instead of loading again" , ( ) => {
30883106 const activeRegistry = createOptionalDemoActiveRegistry ( ) ;
3089- resolveCompatibleRuntimePluginRegistryMock . mockReturnValue ( activeRegistry ) ;
30903107
30913108 const tools = resolvePluginTools (
30923109 createResolveToolsParams ( {
@@ -3095,13 +3112,13 @@ describe("resolvePluginTools optional tools", () => {
30953112 ) ;
30963113
30973114 expectResolvedToolNames ( tools , [ "optional_tool" ] ) ;
3115+ expect ( activeRegistryMocks . getLoadedRegistry ) . toHaveReturnedWith ( activeRegistry ) ;
30983116 expect ( loadOpenClawPluginsMock ) . not . toHaveBeenCalled ( ) ;
30993117 } ) ;
31003118
31013119 it ( "reuses the gateway-bindable registry when it covers the tool runtime scope" , ( ) => {
31023120 const activeRegistry = createOptionalDemoActiveRegistry ( ) ;
31033121 setActivePluginRegistry ( activeRegistry as never , "gateway-startup" , "gateway-bindable" , "/tmp" ) ;
3104- resolveCompatibleRuntimePluginRegistryMock . mockReturnValue ( activeRegistry ) ;
31053122
31063123 const tools = resolvePluginTools (
31073124 createResolveToolsParams ( {
@@ -3111,7 +3128,8 @@ describe("resolvePluginTools optional tools", () => {
31113128 ) ;
31123129
31133130 expectResolvedToolNames ( tools , [ "optional_tool" ] ) ;
3114- expect ( getLoadedRuntimePluginRegistryMock ) . toHaveBeenCalledOnce ( ) ;
3131+ expect ( activeRegistryMocks . getLoadedRegistry ) . toHaveBeenCalledOnce ( ) ;
3132+ expect ( activeRegistryMocks . getLoadedRegistry ) . toHaveReturnedWith ( activeRegistry ) ;
31153133 expect ( loadOpenClawPluginsMock ) . not . toHaveBeenCalled ( ) ;
31163134 } ) ;
31173135
@@ -3139,7 +3157,6 @@ describe("resolvePluginTools optional tools", () => {
31393157 diagnostics : [ ] ,
31403158 } ;
31413159 setActivePluginRegistry ( activeRegistry as never , "gateway-startup" , "gateway-bindable" , "/tmp" ) ;
3142- resolveCompatibleRuntimePluginRegistryMock . mockReturnValue ( undefined ) ;
31433160 loadOpenClawPluginsMock . mockReturnValue ( activeRegistry ) ;
31443161
31453162 const tools = resolvePluginTools (
@@ -3151,7 +3168,8 @@ describe("resolvePluginTools optional tools", () => {
31513168
31523169 expectResolvedToolNames ( tools , [ "optional_tool" ] ) ;
31533170 expect ( heavyFactory ) . not . toHaveBeenCalled ( ) ;
3154- expect ( getLoadedRuntimePluginRegistryMock ) . toHaveBeenCalledOnce ( ) ;
3171+ expect ( activeRegistryMocks . getLoadedRegistry ) . toHaveBeenCalledOnce ( ) ;
3172+ expect ( activeRegistryRequiredPluginIds ( ) ) . toEqual ( [ "optional-demo" ] ) ;
31553173 expect ( loadOpenClawPluginsMock ) . not . toHaveBeenCalled ( ) ;
31563174 } ) ;
31573175
@@ -3198,7 +3216,6 @@ describe("resolvePluginTools optional tools", () => {
31983216 diagnostics : [ ] ,
31993217 } ;
32003218 setActivePluginRegistry ( activeRegistry as never , "gateway-startup" , "gateway-bindable" , "/tmp" ) ;
3201- resolveCompatibleRuntimePluginRegistryMock . mockReturnValue ( undefined ) ;
32023219 loadOpenClawPluginsMock . mockReturnValue ( activeRegistry ) ;
32033220
32043221 const tools = resolvePluginTools (
@@ -3211,7 +3228,10 @@ describe("resolvePluginTools optional tools", () => {
32113228
32123229 expectResolvedToolNames ( tools , [ "memory_search" , "memory_get" ] ) ;
32133230 expect ( memorySearchFactory ) . toHaveBeenCalledTimes ( 1 ) ;
3214- expect ( getLoadedRuntimePluginRegistryMock ) . toHaveBeenCalledOnce ( ) ;
3231+ expect ( activeRegistryMocks . getLoadedRegistry ) . toHaveBeenCalledOnce ( ) ;
3232+ // The disabled owner must never enter the runtime scope: asking the active
3233+ // registry for it would miss and force a pointless cold load.
3234+ expect ( activeRegistryRequiredPluginIds ( ) ) . toEqual ( [ "memory-core" ] ) ;
32153235 expect ( loadOpenClawPluginsMock ) . not . toHaveBeenCalled ( ) ;
32163236 } ) ;
32173237
@@ -3268,7 +3288,6 @@ describe("resolvePluginTools optional tools", () => {
32683288 "gateway-bindable" ,
32693289 "/tmp" ,
32703290 ) ;
3271- resolveCompatibleRuntimePluginRegistryMock . mockReturnValue ( undefined ) ;
32723291 loadOpenClawPluginsMock . mockReturnValue ( loadedRegistry ) ;
32733292
32743293 const tools = resolvePluginTools (
@@ -3316,7 +3335,6 @@ describe("resolvePluginTools optional tools", () => {
33163335 ] ,
33173336 } ) ;
33183337 setActivePluginRegistry ( activeRegistry as never , "gateway-startup" , "gateway-bindable" , "/tmp" ) ;
3319- resolveCompatibleRuntimePluginRegistryMock . mockReturnValue ( activeRegistry ) ;
33203338 loadOpenClawPluginsMock . mockReturnValue ( createToolRegistry ( [ ] ) ) ;
33213339
33223340 resolvePluginTools ( {
@@ -3327,7 +3345,7 @@ describe("resolvePluginTools optional tools", () => {
33273345 toolAllowlist : [ "*" , "tavily" ] ,
33283346 allowGatewaySubagentBinding : true ,
33293347 } ) ;
3330- expect ( getLoadedRuntimePluginRegistryMock ) . toHaveBeenCalledOnce ( ) ;
3348+ expect ( activeRegistryMocks . getLoadedRegistry ) . toHaveBeenCalledOnce ( ) ;
33313349 const loaderParams = mockCallParams ( loadOpenClawPluginsMock ) as {
33323350 onlyPluginIds ?: string [ ] ;
33333351 toolDiscovery ?: unknown ;
0 commit comments