@@ -108,6 +108,12 @@ const listAmbientOnlyConfiguredChannelIds = vi.hoisted(() =>
108108 vi . fn ( ( _params : unknown ) => [ ] as string [ ] ) ,
109109) ;
110110const runStartupSessionMigration = vi . hoisted ( ( ) => vi . fn ( async ( _params : unknown ) => undefined ) ) ;
111+ const migrateLegacyDevicePairingStore = vi . hoisted ( ( ) =>
112+ vi . fn ( async ( _params : unknown ) => undefined ) ,
113+ ) ;
114+ const migrateLegacyNodePairingStore = vi . hoisted ( ( ) =>
115+ vi . fn ( async ( _params : unknown ) => undefined ) ,
116+ ) ;
111117vi . mock ( "../agents/agent-scope.js" , ( ) => ( {
112118 resolveAgentWorkspaceDir : ( ) => "/workspace" ,
113119 resolveDefaultAgentId : ( ) => "default" ,
@@ -130,6 +136,14 @@ vi.mock("../infra/openclaw-root.js", () => ({
130136 resolveOpenClawPackageRootSync : ( params : unknown ) => resolveOpenClawPackageRootSync ( params ) ,
131137} ) ) ;
132138
139+ vi . mock ( "../infra/device-pairing-migration.js" , ( ) => ( {
140+ migrateLegacyDevicePairingStore : ( params : unknown ) => migrateLegacyDevicePairingStore ( params ) ,
141+ } ) ) ;
142+
143+ vi . mock ( "../infra/node-pairing-migration.js" , ( ) => ( {
144+ migrateLegacyNodePairingStore : ( params : unknown ) => migrateLegacyNodePairingStore ( params ) ,
145+ } ) ) ;
146+
133147vi . mock ( "../plugins/channel-presence-policy.js" , ( ) => ( {
134148 listAmbientOnlyConfiguredChannelIds : ( params : unknown ) =>
135149 listAmbientOnlyConfiguredChannelIds ( params ) ,
@@ -222,7 +236,6 @@ async function prepareBootstrapWithRuntimeConfig(
222236
223237 return await prepareGatewayPluginBootstrap ( {
224238 cfgAtStart : cfg ,
225- startupRuntimeConfig : cfg ,
226239 minimalTestGateway : false ,
227240 log,
228241 ...options ,
@@ -246,6 +259,83 @@ function expectStartupPluginLoad(params: {
246259 expect ( startupInput . suppressPluginInfoLogs ) . toBe ( params . suppressPluginInfoLogs ) ;
247260}
248261
262+ describe ( "runGatewayStartupMaintenance" , ( ) => {
263+ beforeEach ( ( ) => {
264+ runChannelPluginStartupMaintenance . mockClear ( ) ;
265+ runStartupSessionMigration . mockClear ( ) ;
266+ migrateLegacyDevicePairingStore . mockClear ( ) ;
267+ migrateLegacyNodePairingStore . mockClear ( ) ;
268+ } ) ;
269+
270+ it ( "runs channel, session, and ordered pairing maintenance for a normal gateway" , async ( ) => {
271+ const log = createLog ( ) ;
272+ const { runGatewayStartupMaintenance } = await import ( "./server-startup-plugins.js" ) ;
273+
274+ await runGatewayStartupMaintenance ( {
275+ cfgAtStart : { } ,
276+ startupRuntimeConfig : { } ,
277+ minimalTestGateway : false ,
278+ log,
279+ } ) ;
280+
281+ expect ( runChannelPluginStartupMaintenance ) . toHaveBeenCalledWith ( {
282+ cfg : { } ,
283+ env : process . env ,
284+ log,
285+ } ) ;
286+ expect ( runStartupSessionMigration ) . toHaveBeenCalledWith ( {
287+ cfg : { } ,
288+ env : process . env ,
289+ log,
290+ } ) ;
291+ expect ( migrateLegacyDevicePairingStore ) . toHaveBeenCalledWith ( { log } ) ;
292+ expect ( migrateLegacyNodePairingStore ) . toHaveBeenCalledWith ( { log } ) ;
293+ const deviceMigrationOrder = migrateLegacyDevicePairingStore . mock . invocationCallOrder [ 0 ] ;
294+ const nodeMigrationOrder = migrateLegacyNodePairingStore . mock . invocationCallOrder [ 0 ] ;
295+ expect ( deviceMigrationOrder ) . toBeDefined ( ) ;
296+ expect ( nodeMigrationOrder ) . toBeDefined ( ) ;
297+ expect ( deviceMigrationOrder ! ) . toBeLessThan ( nodeMigrationOrder ! ) ;
298+ } ) ;
299+
300+ it ( "skips maintenance for a minimal gateway without channel config" , async ( ) => {
301+ const { runGatewayStartupMaintenance } = await import ( "./server-startup-plugins.js" ) ;
302+
303+ await runGatewayStartupMaintenance ( {
304+ cfgAtStart : { } ,
305+ startupRuntimeConfig : { } ,
306+ minimalTestGateway : true ,
307+ log : createLog ( ) ,
308+ } ) ;
309+
310+ expect ( runChannelPluginStartupMaintenance ) . not . toHaveBeenCalled ( ) ;
311+ expect ( runStartupSessionMigration ) . not . toHaveBeenCalled ( ) ;
312+ expect ( migrateLegacyDevicePairingStore ) . not . toHaveBeenCalled ( ) ;
313+ expect ( migrateLegacyNodePairingStore ) . not . toHaveBeenCalled ( ) ;
314+ } ) ;
315+
316+ it ( "runs only channel maintenance for a minimal gateway with recovered channel config" , async ( ) => {
317+ const log = createLog ( ) ;
318+ const recoveredConfig = slackConfig ( ) ;
319+ const { runGatewayStartupMaintenance } = await import ( "./server-startup-plugins.js" ) ;
320+
321+ await runGatewayStartupMaintenance ( {
322+ cfgAtStart : { } ,
323+ startupRuntimeConfig : recoveredConfig ,
324+ minimalTestGateway : true ,
325+ log,
326+ } ) ;
327+
328+ expect ( runChannelPluginStartupMaintenance ) . toHaveBeenCalledWith ( {
329+ cfg : recoveredConfig ,
330+ env : process . env ,
331+ log,
332+ } ) ;
333+ expect ( runStartupSessionMigration ) . not . toHaveBeenCalled ( ) ;
334+ expect ( migrateLegacyDevicePairingStore ) . not . toHaveBeenCalled ( ) ;
335+ expect ( migrateLegacyNodePairingStore ) . not . toHaveBeenCalled ( ) ;
336+ } ) ;
337+ } ) ;
338+
249339describe ( "prepareGatewayPluginBootstrap startup plugins" , ( ) => {
250340 beforeEach ( ( ) => {
251341 applyPluginAutoEnable . mockClear ( ) ;
@@ -263,7 +353,18 @@ describe("prepareGatewayPluginBootstrap startup plugins", () => {
263353 resolveOpenClawPackageRootSync . mockClear ( ) . mockReturnValue ( "/package" ) ;
264354 runChannelPluginStartupMaintenance . mockClear ( ) ;
265355 runStartupSessionMigration . mockClear ( ) ;
356+ migrateLegacyDevicePairingStore . mockClear ( ) ;
357+ migrateLegacyNodePairingStore . mockClear ( ) ;
266358 } ) ;
359+ it ( "does not run startup maintenance" , async ( ) => {
360+ await prepareBootstrapWithRuntimeConfig ( { } ) ;
361+
362+ expect ( runChannelPluginStartupMaintenance ) . not . toHaveBeenCalled ( ) ;
363+ expect ( runStartupSessionMigration ) . not . toHaveBeenCalled ( ) ;
364+ expect ( migrateLegacyDevicePairingStore ) . not . toHaveBeenCalled ( ) ;
365+ expect ( migrateLegacyNodePairingStore ) . not . toHaveBeenCalled ( ) ;
366+ } ) ;
367+
267368 it ( "derives startup activation from source config instead of runtime plugin defaults" , async ( ) => {
268369 const sourceConfig = {
269370 channels : {
@@ -328,7 +429,6 @@ describe("prepareGatewayPluginBootstrap startup plugins", () => {
328429 await prepareGatewayPluginBootstrap ( {
329430 cfgAtStart : runtimeConfig ,
330431 activationSourceConfig : sourceConfig ,
331- startupRuntimeConfig : runtimeConfig ,
332432 pluginMetadataSnapshot,
333433 minimalTestGateway : false ,
334434 log,
@@ -435,7 +535,6 @@ describe("prepareGatewayPluginBootstrap startup plugins", () => {
435535 const { prepareGatewayPluginBootstrap } = await import ( "./server-startup-plugins.js" ) ;
436536 const result = await prepareGatewayPluginBootstrap ( {
437537 cfgAtStart : { channels : { } } ,
438- startupRuntimeConfig : { channels : { } } ,
439538 minimalTestGateway : false ,
440539 ambientEnvTriggers : "suppress" ,
441540 log,
0 commit comments