@@ -81,8 +81,8 @@ const startupMigrationLease = vi.hoisted(() => ({
8181 owner : "startup-test-owner" ,
8282 release : startupMigrationLeaseRelease ,
8383} ) ) ;
84- const acquireStartupMigrationLease = vi . hoisted ( ( ) =>
85- vi . fn ( ( _params : { env : NodeJS . ProcessEnv } ) => startupMigrationLease ) ,
84+ const acquireStartupMigrationLeaseWithWait = vi . hoisted ( ( ) =>
85+ vi . fn ( async ( _params : { env : NodeJS . ProcessEnv } ) => startupMigrationLease ) ,
8686) ;
8787const recordSuccessfulStateMigrations = vi . hoisted ( ( ) => vi . fn ( ) ) ;
8888const recordSuccessfulStartupMigrations = vi . hoisted ( ( ) => vi . fn ( ) ) ;
@@ -184,7 +184,7 @@ vi.mock("./doctor/cron/legacy-repair.js", () => ({
184184} ) ) ;
185185
186186vi . mock ( "../infra/startup-migration-checkpoint.js" , ( ) => ( {
187- acquireStartupMigrationLease ,
187+ acquireStartupMigrationLeaseWithWait ,
188188 needsStateMigrationCheckpoint,
189189 needsStartupMigrationCheckpoint,
190190 recordSuccessfulStateMigrations,
@@ -237,6 +237,7 @@ const { runDoctorConfigPreflight } = await import("./doctor-config-preflight.js"
237237describe ( "runDoctorConfigPreflight state migration" , ( ) => {
238238 beforeEach ( ( ) => {
239239 vi . clearAllMocks ( ) ;
240+ acquireStartupMigrationLeaseWithWait . mockResolvedValue ( startupMigrationLease ) ;
240241 pluginMigrationFingerprint . mockReset ( ) ;
241242 pluginMigrationFingerprint . mockReturnValue ( "plugin-migrations" ) ;
242243 findDoctorLegacyConfigIssues . mockReset ( ) ;
@@ -367,7 +368,7 @@ describe("runDoctorConfigPreflight state migration", () => {
367368 expect ( planStartupPluginConvergence ) . not . toHaveBeenCalled ( ) ;
368369 if ( needed && warnings . length === 0 ) {
369370 expect ( recordSuccessfulStateMigrations ) . toHaveBeenCalledWith ( {
370- env : acquireStartupMigrationLease . mock . calls [ 0 ] ?. [ 0 ] ?. env ,
371+ env : acquireStartupMigrationLeaseWithWait . mock . calls [ 0 ] ?. [ 0 ] ?. env ,
371372 identity : expectMigrationIdentity ( ) ,
372373 lease : startupMigrationLease ,
373374 } ) ;
@@ -399,6 +400,14 @@ describe("runDoctorConfigPreflight state migration", () => {
399400 registrySource : "derived" ,
400401 } ,
401402 } )
403+ . mockResolvedValueOnce ( {
404+ snapshot,
405+ pluginMetadataSnapshot : {
406+ configFingerprint : "plugin-migrations" ,
407+ index,
408+ registrySource : "derived" ,
409+ } ,
410+ } )
402411 . mockResolvedValueOnce ( {
403412 snapshot,
404413 pluginMetadataSnapshot : {
@@ -411,17 +420,17 @@ describe("runDoctorConfigPreflight state migration", () => {
411420
412421 await runDoctorConfigPreflight ( stateCheckpointOptions ) ;
413422
414- const pinnedEnv = acquireStartupMigrationLease . mock . calls [ 0 ] ?. [ 0 ] ?. env ;
423+ const pinnedEnv = acquireStartupMigrationLeaseWithWait . mock . calls [ 0 ] ?. [ 0 ] ?. env ;
415424 expect ( writePersistedInstalledPluginIndexWithLeaseSync ) . toHaveBeenCalledWith ( index , {
416425 env : pinnedEnv ,
417426 lease : startupMigrationLease ,
418427 } ) ;
419428 const writeOrder =
420429 writePersistedInstalledPluginIndexWithLeaseSync . mock . invocationCallOrder [ 0 ] ?? 0 ;
421430 const verificationReadOrder =
422- readConfigFileSnapshotWithPluginMetadata . mock . invocationCallOrder [ 2 ] ?? 0 ;
431+ readConfigFileSnapshotWithPluginMetadata . mock . invocationCallOrder [ 3 ] ?? 0 ;
423432 expect ( verificationReadOrder ) . toBeGreaterThan ( writeOrder ) ;
424- expect ( readConfigFileSnapshotWithPluginMetadata . mock . calls [ 2 ] ?. [ 0 ] ) . toEqual ( {
433+ expect ( readConfigFileSnapshotWithPluginMetadata . mock . calls [ 3 ] ?. [ 0 ] ) . toEqual ( {
425434 allowCurrentPluginMetadata : false ,
426435 } ) ;
427436 const checkpointOrder = recordSuccessfulStateMigrations . mock . invocationCallOrder [ 0 ] ?? 0 ;
@@ -486,7 +495,7 @@ describe("runDoctorConfigPreflight state migration", () => {
486495 ) . rejects . toThrow ( "selected config changed during startup" ) ;
487496
488497 expect ( needsStartupMigrationCheckpoint ) . not . toHaveBeenCalled ( ) ;
489- expect ( acquireStartupMigrationLease ) . not . toHaveBeenCalled ( ) ;
498+ expect ( acquireStartupMigrationLeaseWithWait ) . not . toHaveBeenCalled ( ) ;
490499 expect ( readConfigFileSnapshot ) . not . toHaveBeenCalled ( ) ;
491500 } ) ;
492501
@@ -495,7 +504,7 @@ describe("runDoctorConfigPreflight state migration", () => {
495504 const previousStateDir = process . env . OPENCLAW_STATE_DIR ;
496505 process . env . OPENCLAW_STATE_DIR = "/tmp/openclaw-original-state" ;
497506 let leaseEnv : NodeJS . ProcessEnv | undefined ;
498- acquireStartupMigrationLease . mockImplementationOnce ( ( { env } ) => {
507+ acquireStartupMigrationLeaseWithWait . mockImplementationOnce ( async ( { env } ) => {
499508 leaseEnv = env ;
500509 return {
501510 ...startupMigrationLease ,
@@ -620,33 +629,21 @@ describe("runDoctorConfigPreflight state migration", () => {
620629 expect ( result . cronCodexRuntimePolicyTargets ) . toEqual ( [ { modelRef : "openai/gpt-5.6-sol" } ] ) ;
621630 } ) ;
622631
623- it ( "records the startup migration checkpoint after clean startup migrations" , async ( ) => {
624- needsStartupMigrationCheckpoint . mockReturnValue ( true ) ;
632+ it ( "rechecks the checkpoint after acquisition before running migrations" , async ( ) => {
633+ needsStateMigrationCheckpoint . mockReturnValueOnce ( true ) . mockReturnValue ( false ) ;
634+ needsStartupMigrationCheckpoint . mockReturnValueOnce ( true ) . mockReturnValue ( false ) ;
625635
626636 await runDoctorConfigPreflight ( {
627637 migrateLegacyConfig : false ,
628638 invalidConfigNote : false ,
629639 requireStartupMigrationCheckpoint : true ,
630640 } ) ;
631641
632- const pinnedEnv = acquireStartupMigrationLease . mock . calls [ 0 ] ?. [ 0 ] ?. env ;
633- expect ( pinnedEnv ) . toBeDefined ( ) ;
634- expect ( pinnedEnv ) . not . toBe ( process . env ) ;
635- expect ( needsStartupMigrationCheckpoint ) . toHaveBeenCalledWith ( {
636- env : pinnedEnv ,
637- identity : expectMigrationIdentity ( ) ,
638- } ) ;
639- expect ( runPostCorePluginConvergence ) . toHaveBeenCalledWith ( {
640- cfg : { gateway : { mode : "local" , port : 19091 } } ,
641- env : process . env ,
642- compatibilityHostVersion : expect . any ( String ) ,
643- baselineInstallRecords : { } ,
644- } ) ;
645- expect ( recordSuccessfulStartupMigrations ) . toHaveBeenCalledWith ( {
646- env : pinnedEnv ,
647- identity : expectMigrationIdentity ( ) ,
648- lease : startupMigrationLease ,
649- } ) ;
642+ expect ( autoMigrateLegacyStateDir ) . not . toHaveBeenCalled ( ) ;
643+ expect ( autoMigrateLegacyState ) . not . toHaveBeenCalled ( ) ;
644+ expect ( recordSuccessfulStateMigrations ) . not . toHaveBeenCalled ( ) ;
645+ expect ( recordSuccessfulStartupMigrations ) . not . toHaveBeenCalled ( ) ;
646+ expect ( readConfigFileSnapshotWithPluginMetadata ) . toHaveBeenCalledTimes ( 2 ) ;
650647 expect ( startupMigrationLeaseRelease ) . toHaveBeenCalledOnce ( ) ;
651648 } ) ;
652649
@@ -680,6 +677,7 @@ describe("runDoctorConfigPreflight state migration", () => {
680677 it ( "refuses startup when plugin migration inputs change during convergence" , async ( ) => {
681678 needsStartupMigrationCheckpoint . mockReturnValue ( true ) ;
682679 pluginMigrationFingerprint
680+ . mockReturnValueOnce ( "plugin-migrations-before" )
683681 . mockReturnValueOnce ( "plugin-migrations-before" )
684682 . mockReturnValueOnce ( "plugin-migrations-before" )
685683 . mockReturnValueOnce ( "plugin-migrations-after" ) ;
@@ -693,7 +691,7 @@ describe("runDoctorConfigPreflight state migration", () => {
693691 ) . rejects . toThrow ( "plugin migration inputs changed during startup convergence" ) ;
694692
695693 expect ( recordSuccessfulStateMigrations ) . toHaveBeenCalledWith ( {
696- env : acquireStartupMigrationLease . mock . calls [ 0 ] ?. [ 0 ] ?. env ,
694+ env : acquireStartupMigrationLeaseWithWait . mock . calls [ 0 ] ?. [ 0 ] ?. env ,
697695 identity : expect . objectContaining ( {
698696 pluginMigrationFingerprint : "plugin-migrations-before" ,
699697 } ) ,
@@ -719,7 +717,7 @@ describe("runDoctorConfigPreflight state migration", () => {
719717 requireStartupMigrationCheckpoint : true ,
720718 } ) ;
721719
722- const pinnedEnv = acquireStartupMigrationLease . mock . calls [ 0 ] ?. [ 0 ] ?. env ;
720+ const pinnedEnv = acquireStartupMigrationLeaseWithWait . mock . calls [ 0 ] ?. [ 0 ] ?. env ;
723721 expect ( recordSuccessfulStartupMigrations ) . toHaveBeenCalledWith ( {
724722 env : pinnedEnv ,
725723 identity : expectMigrationIdentity ( ) ,
@@ -811,7 +809,7 @@ describe("runDoctorConfigPreflight state migration", () => {
811809 warnings : [ ] ,
812810 issues : [ ] ,
813811 } ,
814- 2 ,
812+ 3 ,
815813 ) ;
816814 runPostCorePluginConvergence . mockResolvedValueOnce (
817815 makeStartupConvergenceResult ( {
@@ -977,7 +975,7 @@ describe("runDoctorConfigPreflight state migration", () => {
977975 ) . rejects . toThrow ( "Configured plugin discord is not installed" ) ;
978976
979977 expect ( recordSuccessfulStateMigrations ) . toHaveBeenCalledWith ( {
980- env : acquireStartupMigrationLease . mock . calls [ 0 ] ?. [ 0 ] ?. env ,
978+ env : acquireStartupMigrationLeaseWithWait . mock . calls [ 0 ] ?. [ 0 ] ?. env ,
981979 identity : expectMigrationIdentity ( ) ,
982980 lease : startupMigrationLease ,
983981 } ) ;
@@ -1011,7 +1009,7 @@ describe("runDoctorConfigPreflight state migration", () => {
10111009 warnings : [ ] ,
10121010 issues : [ ] ,
10131011 } ,
1014- 3 ,
1012+ 4 ,
10151013 ) ;
10161014 runPostCorePluginConvergence . mockResolvedValueOnce (
10171015 makeStartupConvergenceResult ( {
@@ -1080,7 +1078,7 @@ describe("runDoctorConfigPreflight state migration", () => {
10801078 warnings : [ ] ,
10811079 issues : [ { path : "gateway.port" , message : "invalid" } ] ,
10821080 } ,
1083- 2 ,
1081+ 3 ,
10841082 ) ;
10851083
10861084 await expect (
0 commit comments